Lesson 3.9 3.11 hack
Hack 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 looks different is because there will be a difference can make it wrong, even they looks the same.
- for the converted conditional to boolean conversion
isSolid = False
isSoft = True
if isSolid == True:
print("Don't Eat it")
else:
if isLiquid == True:
print("Eat it")
else:
print("Don't eat it")
isSolid = False
isSoft = True
eatTime = not(isSolid) and isSoft
if eatTime == False:
print("Don't eat it")
if eatTime == True:
print("Eat it")
Hack 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!
- Input what show user is looking for
- check if show is in show dictionary
- If it is in dictionary, move onto step 4. If not, print "not available"
- check if the show has a rating of 3 or higher
- If it does, print "recommended"; if it doesn't print "not recommended"
itemdictionary = {
'item1':2,
'item3':3,
'item4':5,
'item5':1
}
show = input("What item are you looking for?")
if show in showdictionary.keys():
print(show + " is available")
if showdictionary[show] >= 2:
print("recommended")
else:
print("not recommended")
else:
print(show + " is not available")
import random
num_guesses = 0
user_input = -1
upper_bound = 10
lower_bound = 0
number = random.randint(0,10)
print("Think a number between 0 and 100.")
def guess():
num = input("Choose a number!")
return num
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_input != number:
user_input = guess()
num_guesses += 1
print(f"You guessed {user_input}.")
lower_bound, upper_bound = search(number, user_input)
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!")
Hack 4
- 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
- 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?
- It's 80, 74, and 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"]
C
index = [12, 14, 43, 57, 79, 80, 99]
index.sort()
mid = int(len(index) / 2)
print("middle is",index[mid])
index =[1, 12, 30, 43, 66, 74, 92]
index.sort()
mid = int(len(index) / 2)
print("middle is",index[mid])
index = [7, 13, 33, 60, 84, 96, 111]
index.sort()
mid = int(len(index) / 2)
print("middle is",index[mid])