Unit 3.9 & 3.11 Hacks
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)
- 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 :(")
(need vpn to see images)
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)
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!")
- 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
- 7, 13, 96, 111, 33, 84, 60
- 7, 13, 33, <mark> 60 </mark>, 84, 96, 111
- 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.
-
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"]