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?(0.15)
  • for the converted conditional to boolean conversion(0.10)
  • total: 0.25
  • 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")
Go to school
Go do you work at school!

Hacks 2

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!

Natural Language for Netflix Code

  1. Input what show user is looking for
  2. check if show is in showdictionary
  3. If it is in dictionary, move onto step 4. If not, print "show is not available on Netflix"
  4. check if the show has a rating of 3 or higher
  5. 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")
You is available on Netflix
show/movie is recommended

Hacks 3

Fix the number guessing game

  1. Make a flow chart for the algorithm number guessing game <- flow chart is in review ticket
  2. Make a function that gets the user guess
  3. Modify the existing search function to give more encouraging feedback
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!")
I'm thinking of a number between 0 and 100.
You guessed 72.
You guessed too high
Guess a number between 0 and 72.
You guessed 34.
You guessed too low
Guess a number between 34 and 72.
You guessed 45.
You guessed too low
Guess a number between 45 and 72.
You guessed 56.
You guessed too high
Guess a number between 45 and 56.
You guessed 48.
You guessed too low
Guess a number between 48 and 56.
You guessed 49.
You guessed too low
Guess a number between 49 and 56.
You guessed 52.
You guessed the number in 7 guesses!

Hacks 4

  1. 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
  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?:
    • 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.
  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 = C

index = [12, 14, 43, 57, 79, 80, 99]
index.sort()
mid = int(len(index) / 2) 
print(mid)
print("middle is",index[mid])
3
middle is 57
index =[1, 12, 30, 43, 66, 74, 92]
index.sort()
mid = int(len(index) / 2) 
print(mid)
print("middle is",index[mid])
3
middle is 43
index = [7, 13, 33, 60, 84, 96, 111]
index.sort()
mid = int(len(index) / 2) 
print(mid)
print("middle is",index[mid])
3
middle is 60