3.9.1 Hacks:

  • 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)
  • for the converted conditional to boolean conversion(0.10)
  • total: 0.25

Completion:

  1. It is important to know that algorithims that look different can do the same thing and that algorithims that look the same might have different results because users must look and examine algorithims carefully to predict the outcomes of algorithims. There is more than one way to create certain algorithims or algorithims may look the same and a person must be carefull not to make any assumptions.

  2. conditional to boolean below:

sweaty = True
dirty = False

if sweaty == True:
    print("Take a shower!")

else:
    if dirty == True:
        print("Take a shower!")
    
    else: 
        print("No need to take a shower. You're clean!")


# second way!

showering = dirty or sweaty 
if showering == True:
    print("Take a shower!")
if showering == False:
    print("No need to take a shower. You're clean!")
Take a shower!
Take a shower!

3.9.2 Hacks:

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!

Tips:

  • This site is good for making flowcharts!
  • Natural language should just be a list
  • Think about the whole process, not just the end result

Completiom:

Flowchart

Linked in review ticket

Natural Language:

  1. Start
  2. The number of miles left of gas is 30 miles.
  3. Ask if you would like to go to school.
  4. Do you have 5 or more miles of gas?
  5. Every trip to school is 5 miles. You go to school and lose 5 miles.
  6. How many miles of gas are left?
  7. Decide if you would like to go to school again.
  8. Repeat steps 3-6 until you are out of gas or you don't want to go to school again.
  9. State gas level.
  10. End
miles_of_gas = 30

while miles_of_gas >= 5:
    school = input("Would you like to go to school?")
    if school == "yes":
        print("You have gone to school")
        miles_of_gas -= 5
        print("You now have ", miles_of_gas, " miles of gas left")
    else:
        print("You have ", miles_of_gas, " miles of gas left")
        miles_of_gas = 0
You have gone to school
You now have  25  miles of gas left
You have gone to school
You now have  20  miles of gas left
You have  20  miles of gas left

3.9.3 Hacks:

Fix the number guessing game

  1. Make a flow chart for the algorithm number guessing game
  2. Make a function that gets the user guess
  3. Modify the existing search function to give more encouraging feedback

Flow chart linked in review ticket!

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():
    userguess = input("Choose a number!")
    return userguess

#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("Your guess is too high") 
        lower_bound = guess
        return lower_bound, upper_bound
    elif int(guess) > int(number):
        print("Your guess is too low")
        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!")
I'm thinking of a number between 0 and 100.
You guessed 50.
Your guess is too low
Guess a number between 0 and 50.
You guessed 50.
Your guess is too low
Guess a number between 0 and 50.
You guessed 25.
Your guess is too high
Guess a number between 25 and 50.
You guessed 35.
Your guess is too low
Guess a number between 25 and 35.
You guessed 27.
Your guess is too high
Guess a number between 27 and 35.
You guessed 30.
Your guess is too high
Guess a number between 30 and 35.
You guessed 32.
Your guess is too low
Guess a number between 30 and 32.
You guessed 31.
You guessed the number in 8 guesses!

3.11 Hacks:

  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
    • 7, 13, 96, 111, 33, 84, 60
  2. 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?
  3. 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"]

Hacks 3 Work:

  1. Below
listone = [12, 14, 43, 57, 79, 80, 99]

middle = listone[int(len(listone)/2)]
print("The first middle index is: ", middle)
The first middle index is:  57

My binary tree is linked in my review ticket

listtwo = [92,43,74,66,30,12,1]

listtwo.sort() #puts list in order

print(listtwo)

middle = listtwo[int(len(listtwo)/2)]
print("The second middle index is: ", middle)
[1, 12, 30, 43, 66, 74, 92]
The second middle index is:  43

My binary tree is linked in my review ticket

listthree = [7, 13, 96, 111, 33, 84, 60]

listthree.sort() #puts list in order

print(listthree)

middle = listthree[int(len(listthree)/2)]
print("The third middle index is: ", middle)
[7, 13, 33, 60, 84, 96, 111]
The third middle index is:  60
    1. In the this list of numbers: 12, 14, 43, 57, 79, 80, 99: The second number looked at in a binary search if the number is more than the middle number would be 80.
    2. Second list: 1, 12, 30, 43, 66, 74, 92: The second number looked at would be 74.
    3. Third list: 7, 13, 33, 60, 84, 96, 111 The second number looked at would be 96
  1. The answer is C because the list is unsorted. In order to use the binary search, a user would have to sort the list like I have with the lists above.