notes

  • sequances must tbe sequanced quicku
  • Algorithms with iteration repeat a function until a goal is reached
    • To more easily represent an algorithm without showing all the repeated steps, we can use iteration
  • Algorithms with selection only go through certain functions if certain things are true or false
  • However, Algorithms that look similar might not always have the same result
  • Different algorithms can be used to solve the same problem When collaborating or working on group projects, two people might come up with two different ways to solve a problem, and that happens a lot.
  • know that same goal can be achieved in many ways (the possibilities are endless)
  • make notes in you code! (explain how it works to others or you future self)

Hack #1

  1. why is it important to know that algorithms that look different can do the same thing and that algorithms that look the same might have different results?(0.15)
    • some people might use their variables differently tha you do so that they feel comfortable the way they look at the code.
haveMoney = False
storeOpen = True
driveSchool = not(haveMoney) and storeOpen
if driveSchool == False:
    print("Buy the PS5")
if driveSchool == True:
    print("Don't buy the PS5 :(")
Don't buy the PS5 :(

Hacks #2

Develop your own complex algorithm using a flowchart and natural language, then code it!

Requirements:

  • Includes both a flowchart AND natural language
  • Working code of the same algorithm
  • Incorporates selection AND/OR iteration
  • Make it creative!

(need vpn to see images)

image

tried = True
work = False

w = "wake up"
g = "go back to sleep"

if tried == True and work == True:
    print(w)
if tried == False and work == True:
    print(w)
if tried == False and work == False:
    print(g)
if tried == True and work == False:
    print(g)
go back to sleep
import random

#sets variables for the game
num_guesses = 0
user_guess = 0
upper_bound = 100
lower_bound = 0

#generates a random number
number = random.randint(1,100)

# print(number)     #for testing purposes

print(f"I'm thinking of a number between 1 and 100.")

#Write a function that gets a guess from the user using input()
def guess():
    user_input = int(input())
    return user_input

#Change the print statements to give feedback on whether the player guessed too high or too low
def search(number, guess):
    global lower_bound, upper_bound
    if guess < number:
        print("Higher") #change this
        lower_bound = guess
    elif guess > number:
        print("Lower") #change this
        upper_bound = guess
    return lower_bound, upper_bound

while user_guess != number:
    user_guess = guess()
    num_guesses += 1
    print(f"You guessed {user_guess}.")
    lower_bound, upper_bound = search(number, user_guess)
    print(f"Guess a number between {lower_bound} and {upper_bound}.")

print(f"You guessed the number in {num_guesses} guesses!")
I'm thinking of a number between 1 and 100.
You guessed 45.
Higher
Guess a number between 45 and 100.
You guessed 70.
Higher
Guess a number between 70 and 100.
You guessed 90.
Lower
Guess a number between 70 and 90.
You guessed 80.
Lower
Guess a number between 70 and 80.
You guessed 75.
Higher
Guess a number between 75 and 80.
You guessed 75.
Higher
Guess a number between 75 and 80.
You guessed 78.
Higher
Guess a number between 78 and 80.
You guessed 78.
Higher
Guess a number between 78 and 80.
You guessed 79.
Guess a number between 78 and 80.
You guessed the number in 9 guesses!
  1. calculate the middle index and create a binary tree for each of these lists
    • 12, 14, 43, 57, 79, 80, 99
    • 92, 43, 74, 66, 30, 12, 1
      • 1, 12, 30, 43, 66, 74, 92 iamge

image2

- 7, 13, 96, 111, 33, 84, 60
    - 7, 13, 33, <mark> 60 </mark>, 84, 96, 111

image

  1. Using one of the sets of numbers from the question above, what would be the second number looked at in a binary search if the number is more than the middle number?
    • 96, third example.
  2. Which of the following lists can NOT a binary search be used in order to find a targeted value?

    a. ["amy", "beverly", "christian", "devin"]

    b. [-1, 2, 6, 9, 19]

    c. [3, 2, 8, 12, 99]

    d. ["xylophone", "snowman", "snake", "doorbell", "author"]