Lessons Live Review Three
Notes and hacks for the third review of lessons (12/8-12/12). Lessons 6 and 7 (Unit 3 Sections 12-15).
- Hack Grades
- Lesson Vocabulary Linked below:
- Unit 3 Sections 12-13 Notes
- Determining Procedure Results
- Unit 3 Sections 14-15 Notes
- Lesson Unit 3 Section 16 Notes:
- Unit 3 Sections 17-18 Notes
Unit 3 Sections 12-13 Notes
Procedures
-
A procedure is a named set of instructions that can take in parameters and return values.
- May be called "method" or "function" in different programming languages.
-
Parameters are independent variables used in the procedure to produce a result.
- allows a procedure to execute without initially knowing specific input values.
- calling a procedure = write the name of the procedure followed by the parentheses with the parameters of the procedure
Example:
x = 5
y = 3
def multiply(x, y): # (x,y) is a parameter
product = x * y
return product
answer = multiply(x, y) # this is the procedure being called
print("The product of", x, "times", y, "is", answer)
Determining Procedure Results
- To determine the result = follow the code line by line and see what each one does
- look at: function parameters, return values, and statements
- return values = write the syntax return followed by the expression you would like to return
- A return statement exits a function and instructs python to continue executing the program and to return a certain value
Example:
def divide(num1,num2):
x = num1/num2
return x # return value returns value of variable x
Managing Complexity
Vocab:
- Modularity - the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program
- Abstraction - the practice of hiding the details of how a particular code or system works and exposing only the essential features or functions that are necessary for other parts of the program to use
- Duplication - having multiple duplicate code blocks, often decreasing readability and efficiency
- Logic - the sequence of steps and operations that a computer follows to execute a program, including the specific instructions and decision-making processes built into the code
Modularity and Abstraction example form hack (avoids duplication):
questionnumber = 3
meals = []
questions = [
"What did you eat for breakfast? Bacon, eggs, or cereal?",
"What did you eat for lunch? A sandwich, mac and cheese, or a lunchable?",
"What did you eat for dinner? Pizza, pasta, or a salad?"
]
def mealask(question):
print("Question: ", question)
response = input()
print("Your response: ", response)
meals.append(response)
for number in range(questionnumber):
mealask(questions[number])
for i in meals:
if i == "cereal" or i == "lunchable" or i == "pizza":
print(i, " was not a healthy choice")
elif i == "bacon" or i == "mac and cheese" or i == "pasta":
print(i, " was not a bad choice")
else:
print(i, " was a healthy choice")
Developing Procedures
- Procedure - a module of code that is created to complete a certain task, this is basically a function
- Procedure Name - the name that is given to a function/procedure -Parameters - a variable that is used in a function to allow for data to be imported into a function -Arguments - a way to provide information to a function, usually defined outside a function and then imported into a function with parameters
below is the basic formatting for procedures in python:
def function(a,b): # function is defined
print(a+b) # prints output of variables
function(1,2) # one instance that it can be used
function(2,3) # another instance
below is the basic formatting for procedures in javascript:
function Newfunction(a,b) {
return a + b;
}
Function(1,2)
Function(2,3)
<!-- function is called here -->
<button id="enter" onclick="print(a,b)">HI</button>
<p id="result"></p>
<!-- javascript -->
<script>
function print(a,b) {
document.getElementById("result").innerHTML = a + b // math
}
// variables are defined
var a = 1
var b = 2
</script>
Unit 3 Sections 14-15 Notes
College Board Essential Knowledge
- A software library contains procedures that can be used in the creation of new programs = a collection of code from an external source that can be used to add functionality to a program.
- Existing segments of code can come from internal or external sources, ie. libraries or previously written code (ex. the random.randint from random)
- libraries simplify the task of creating complex programs (code already written)
-
Application program interfaces (APIs) are specifications for how the procedures in a library behave and can be used.
-
Randomization generates a value between two numbers
Example of libraries and randomization:
import random #random library
#This is a magic globe that will predict how many toys you will get this winter
print("How many toys will you get this Christmas?")
randomnumber = random.randint(0,100)
print("You will get ", randomnumber, " toys this Christmas")
The Different Methods of the Random Function (in random library)
< mark> Method | Description </mark>
seed() | Initialize the random number generator
getstate() | Returns the current internal state of the random number generator
setstate() | Restores the internal state of the random number generator
getrandbits() | Returns a number representing the random bits
randrange() | Returns a random number between the given range
randint() | Returns a random number between the given range
choice() | Returns a random element from the given sequence
choices() | Returns a list with a random selection from the given sequence
shuffle() | Takes a sequence and returns the sequence in a random order
sample() | Returns a given sample of a sequence
random() | Returns a random float number between 0 and 1
uniform() | Returns a random float number between two given parameters
betavariate() | Returns a random float number between 0 and 1 based on the Beta distribution (used in statistics)
expovariate() | Returns a random float number based on the Exponential distribution (used in statistics)
gammavariate() | Returns a random float number based on the Gamma distribution (used in statistics)
gauss() | Returns a random float number based on the Gaussian distribution (used in probability theories)
lognormvariate() | Returns a random float number based on a log-normal distribution (used in probability theories)
normalvariate() | Returns a random float number based on the normal distribution (used in probability theories)
vonmisesvariate() | Returns a random float number based on the von Mises distribution (used in directional statistics)
paretovariate() | Returns a random float number based on the Pareto distribution (used in probability theories)
weibullvariate() | Returns a random float number based on the Weibull distribution (used in statistics)
import random
colorspinner = random.randint(1,8)
if colorspinner <= 3:
print("green")
elif colorspinner <= 5:
print("blue")
elif colorspinner <= 6:
print("purple")
elif colorspinner <= 7:
print("red")
else:
print("orange")
Lesson Unit 3 Section 16 Notes:
This is our lesson: refer to this presentation for notes and guidance
Unit 3 Sections 17-18 Notes
-
Collatz= conjecture that is one of the most famous unsolved problems in mathematics. The conjecture asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1.
-
Hailstone numbers= The sequence of integers generated by Collatz conjecture are called Hailstone Numbers. Examples:Input : N = 7 Output : Hailstone Numbers: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 No.> ### Iteration The action or a process of iterating or repeating:such as. : a procedure in which repetition of a sequence of operations yields results successively closer to a desired result.
-
Undecidable problems= An undecidable problem is one that should give a "yes" or "no" answer, but yet no algorithm exists that can answer correctly on all inputs.
-
Unsolvable problems= An unsolvable problem is one for which no algorithm can ever be written to find the solution.
sequence = []
def combinedcollatz(i):
while i > 1:
if (i % 2): # i is odd
sequence.append(i)
i = 3*i + 1
else: # i is even
sequence.append(i)
i = i//2
if i == 1:
sequence.append(1)
else:
print(i, "is invalid, please try again") #this will print if the number is invalid
i = int(input('Enter i: '))
combinedcollatz(i)
print("Hailstone numbers: ", sequence, "\nNumber of iterations: ", len(sequence)-1)
- Algorithmic efficiency is an aspect of algorithmic programming that measures the number of steps needed to solve a problem
class_to_do = []
i = 0
while i == 0:
task = input("What assignment do you have to do today?")
if task == "nothing":
i = i + 1
else:
class_to_do.append(task)
print("Tasks you have to do today: ", class_to_do)
taskone = input("What assignment do you have to do today?")
print("You have to do", taskone)
tasktwo = input("What's another assignment you have to do today?")
print("You have to do", tasktwo)
taskthree = input("What's another assignment you have to do today?")
print("You have to do", taskthree)
taskfour = input("What's another assignment you have to do today?")
print("You have to do", taskfour)