3.9 and 3.11 Student Lesson (group 5)
My homework for the 3.9 and 3.11 lesson
- 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?
- This knowledge is important since it will cause people to look at and compare code more carefully. We can catch mistakes, make code more efficient, and overall learn from others. Mistakes are learning too!
isalexatired = True
havehw = True
if isalexatired == True and havehw == False:
print("Stay home and sleep!")
else:
if havehw == True:
print("Go to school")
else:
print("Stay home and sleep!")
# Boolean conversion
gotoschool = not(isalexatired) or isalexatired and havehw
if gotoschool == True:
print("Go do you work at school!")
if gotoschool == False:
print("Stay home and sleep queen")
Natural Language for Netflix Code
- Input what show user is looking for
- check if show is in showdictionary
- If it is in dictionary, move onto step 4. If not, print "show is not available on Netflix"
- check if the show has a rating of 3 or higher
- If it does, print "show is recommended"; if it doesn't print "how is not recommended"
Flow chart is in review ticket
showdictionary = {
'Wednesday':5, # key=title name and value=rating out of 5 stars
'You':3,
'Knight Before Christmas':4,
'Red Notice':5,
'Truth or Dare':1
}
show = input("What show are you looking for?")
if show in showdictionary.keys():
print(show + " is available on Netflix")
if showdictionary[show] >= 3:
print("show/movie is recommended")
else:
print("show is not recommended")
else:
print(show + " is not available on Netflix")
import random
#sets variables for the game
num_guesses = 0
user_guess = -1
upper_bound = 100
lower_bound = 0
#generates a random number
number = random.randint(0,100)
# print(number) #for testing purposes
print(f"I'm thinking of a number between 0 and 100.")
#Write a function that gets a guess from the user using input()
def guess():
guessednum = input("Choose a number!")
return guessednum
#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 int(guess) < int(number):
print("You guessed too low")
lower_bound = guess
return lower_bound, upper_bound
elif int(guess) > int(number):
print("You guessed too high")
upper_bound = guess
return lower_bound, upper_bound
else:
upper_bound, lower_bound = guess, 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)
if int(upper_bound) == int(number):
break
else:
print(f"Guess a number between {lower_bound} and {upper_bound}.")
print(f"You guessed the number in {num_guesses} guesses!")
Hacks 4
- calculate the middle index and create a binary tree for each of these lists
-
Binary Trees in review ticket
- 12, 14, 43, 57, 79, 80, 99
- 92, 43, 74, 66, 30, 12, 1
- 7, 13, 96, 111, 33, 84, 60
-
Binary Trees in review ticket
- 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?:
- In the first set of numbers, the second number looked at in a binary search if it is more than the middle number would be 80.
- In the second set of numbers, the second number looked at in a binary search if it is more than the middle number would be 74.
- In the third set of numbers, the second number looked at in a binary search if it is more than the middle number would be 96.
-
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"]
Answer = C
index = [12, 14, 43, 57, 79, 80, 99]
index.sort()
mid = int(len(index) / 2)
print(mid)
print("middle is",index[mid])
index =[1, 12, 30, 43, 66, 74, 92]
index.sort()
mid = int(len(index) / 2)
print(mid)
print("middle is",index[mid])
index = [7, 13, 33, 60, 84, 96, 111]
index.sort()
mid = int(len(index) / 2)
print(mid)
print("middle is",index[mid])