Lesson 3.9 & 3.11 Notes

  • Different algorithms can be used to achieve the same goal
  • This is important within collaborating
    • Allows for debugging
print("The parking rate is as follows: \n Less than one hour: Free \n 1-2 hours: $5 \n 2-3 hours: $8 \n 3-4 hours: $10 \n 4+ hours: $12")

time = float(input("How many hours have you parked at this garage?"))
print("How many hours have you parked at this garage?")
print(time, "hours costs:")

if time < 1 :
    print("Free")
elif time >= 1 and time < 2 :
    print("$5")
elif time >= 2 and time < 3 :
    print("$8")
elif time >= 3 and time < 4 :
    print("$10")
else:
    print("$12")

print("Have a good day!")
The parking rate is as follows: 
 Less than one hour: Free 
 1-2 hours: $5 
 2-3 hours: $8 
 3-4 hours: $10 
 4+ hours: $12
How many hours have you parked at this garage?
3.0 hours costs:
$10
Have a good day!
  1. Start
  2. Input number of hours parked
  3. If hours is less than 1, cost is free
  4. If hours is between 1 and 2, cost is $5
  5. If hours is between 2 and 3, cost is $8
  6. If hours is between 3 and 4, cost is $10
  7. If hours is more than 4, cost is $12
  8. Display cost and goodbye
  9. Finish

3.12-13 Notes

Vocabulary:

Procedure:
  • A procedure is a piece of code that is made to have accomplish a certain goal (like a function)
Parameter:
  • A parameter is a variable that allows data to be imported into a function.
    • A parameter can look like "a" and "b" in random.randint(a,b)
Return Value:
  • A return value is a value that returns to a procedure using a return statement
    • Python to continues executing the program and returns a certain value (Return Value)
    • Using a return statement
Procedure Name:

The name of a function

Arguments:

An argument provides data to a function

  • Often defined outside of a function
  • Imported into function via parameters

Creating Functions in Python and Javascript

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
3
5

Python is similar to the Collegeboard example, where def defines the function, function, and then is followed by parameters a,b, which can later be interchanged with any numbers as shown with function(1,2). The numbers are called arguments, which are information provided to the function with parameters. In this case, the parameters are being added within the function and then printed.

Javascript

function Function(a,b) {
  return a + b;
}

Function(1,2)
Function(2,3)
3
5

Javascript in this case is almost the exact same as Python, the only differences being that function is called with function and that the formatting is a little different. Otherwise, it does the exact same thing as the Python example.

Unit 3.14-15 Notes

College Board Knowledge

  • A software library contains procedures that can be used in the creation of new programs.
  • Existing segments of code can come from internal or external sources, ie. libraries or previously written code.
  • The use of libraries simplifies the task of creating complex programs.
  • Application program interfaces (APIs) are specifications for how the procedures in a library behave and can be used.
  • Documentation for a library or API is necessary in understanding the key behaviors provided by the API/library and how to utilize them in your work.

What is a Library?

  • A library is a collection of code from an external source that can be used to add functionality to a program.
  • Libraries are very useful, as they can be used to save time and effort in the development process.
  • Libraries are usually included in a program using a special keyword called " ." This keyword tells the program to look for the library and use its code.

What is randomization?

  • Randomization generates a value between two numbers. For example RANDOM(1,3) may result as 1 or 2 or 3, either one of those.
import random

number = random.randint(0,10)

if number>3:
    print("Team Baddies for life")
elif number>6:
    print("I can't wait for break!")
else: 
    print("APCSP is okay")
Team Baddies for life

Other Uses for "random" function:

Method | Description

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)

Unit 3 Section 17-18 Notes

Vocabulary / Important Info

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.

  • EX: Collatz Series

Unsolvable problems:

Cannot create an algorithm to find a solution to the problem.

Collatz series: Undecidable problem

  • There is an infinite number of inputs you could put in so its undecidable whether or not all numbers will terminate

Hailstone Numbers

The sequence of integers generated by Collatz conjecture

  • 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.>

What is Algorithm Efficiency?

Algorithm Efficiency is the number of steps required to run through an algorithm or find a solution. An efficient algorithm would have less steps to get to the end of an sequence. For example, in the efficient code from Hack #2, I used a while loop and for loop to make less steps for writing out a sleep schedule. An inefficient code, like the second part of Hack #2, might be repetitive or could be condensed into a much simpler code that gets the same results.

Mini activity

  • A homework sorter to automate and organize what work I need to complete:
  • (Spanish - )