Unit 3 Sections 9 and 11
During this lesson sections 9 and 11 hacks will be completed
3.9.1 Hacks
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? It is important to know that algorithms that look different can do the same thing because you can have multiple sets of instructions that have the same output. On the other hand, algorithms can have different output but similar code which is also another reason why analyzation of code and little detials is important when trouble shooting.
NoMoney = False
HasMoney = True
if NoMoney == True:
print("You need to work more.")
else:
if HasMoney == True:
print("Spend it and make more")
else:
print("Have a good day")
isDay = False
isNight = True
# setting variables here (same as above to make comparison easier)
Nighttime = not(isDay) and isNight
if Nighttime == False:
print("It's daytime")
if Nighttime == True:
print("It's nightime")
Chores = 5
Assignments = 19
while (Chores or Assignments) > 0:
if Chores > 0:
Chores -= 1
if Assignments > 0:
Assignments -= 1
print("Chores left:",Chores,"Assignments left:",Assignments)
import random
num_guesses = 0
user_guess = 0
upper_bound = 100
lower_bound = 0
number = random.randint(1,100)
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():
num = input("What number?")
return int(num)
def search(number, guess):
global lower_bound, upper_bound
if guess < number:
print("Nice Try!")
lower_bound = guess
elif guess > number:
print("Good luck next time! :(")
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!")
- Indexing Code: (pasted below)
index = [12, 14, 43, 57, 79, 80, 99]
mid = int(len(index) / 2)
print("Index 1")
print(f"middle == {index[mid]}")
index = [92, 43, 74, 66, 30, 12, 1]
mid = int(len(index) / 2)
print("Index 2")
print(f"middle == {index[mid]}")
index = [7, 13, 96, 111, 33, 84, 60]
mid = int(len(index) / 2)
print("Index 3")
print(f"middle == {index[mid]}")
- 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?
- Set one = 80
- Set two = 74
- Set three = 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 is C because it's not sorted.