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")
Spend it and make more
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")
It's nightime

3.9.2 Hacks

Steps:

  1. Check to see if Chores or Assignments is > 0
    • If chores & assignemnts == 0 then it's done!
  2. If chores and assignments > 0 then repeat following steps until they are both 0.
    • Chores = 5 then do 5 chores until it's 0
    • Assignmentss = 19 then do 19 assignments.
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)
Chores left: 0 Assignments left: 0

3.9.3 Hacks

Number Guessing Flowchart

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!")
I'm thinking of a number between 1 and 100.
You guessed 5.
Nice Try!
Guess a number between 5 and 100.
You guessed 6.
Nice Try!
Guess a number between 6 and 100.
You guessed 8.
Good luck next time! :(
Guess a number between 6 and 8.
You guessed 45.
Good luck next time! :(
Guess a number between 6 and 45.
You guessed 23.
Good luck next time! :(
Guess a number between 6 and 23.
You guessed 43.
Good luck next time! :(
Guess a number between 6 and 43.
You guessed 32.
Good luck next time! :(
Guess a number between 6 and 32.
You guessed 27.
Good luck next time! :(
Guess a number between 6 and 27.
You guessed 24.
Good luck next time! :(
Guess a number between 6 and 24.
You guessed 21.
Good luck next time! :(
Guess a number between 6 and 21.
You guessed 12.
Good luck next time! :(
Guess a number between 6 and 12.
You guessed 10.
Good luck next time! :(
Guess a number between 6 and 10.
You guessed 13.
Good luck next time! :(
Guess a number between 6 and 13.
You guessed 7.
Guess a number between 6 and 13.
You guessed the number in 14 guesses!

3.11 Hacks

  1. 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 1
middle == 57
index = [92, 43, 74, 66, 30, 12, 1]

mid = int(len(index) / 2) 
print("Index 2")
print(f"middle == {index[mid]}")
Index 2
middle == 66
index = [7, 13, 96, 111, 33, 84, 60]

mid = int(len(index) / 2) 
print("Index 3")
print(f"middle == {index[mid]}")
Index 3
middle == 111
  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?
    • Set one = 80
    • Set two = 74
    • Set three = 96
  1. 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.