Lesson Five Homework
Lesson five hacks
Completion:
-
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.
-
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!")
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:
- Start
- The number of miles left of gas is 30 miles.
- Ask if you would like to go to school.
- Do you have 5 or more miles of gas?
- Every trip to school is 5 miles. You go to school and lose 5 miles.
- How many miles of gas are left?
- Decide if you would like to go to school again.
- Repeat steps 3-6 until you are out of gas or you don't want to go to school again.
- State gas level.
- 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
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!")
3.11 Hacks:
- 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?
-
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"]
listone = [12, 14, 43, 57, 79, 80, 99]
middle = listone[int(len(listone)/2)]
print("The first middle index is: ", middle)
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)
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)
-
- 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.
- Second list: 1, 12, 30, 43, 66, 74, 92: The second number looked at would be 74.
- Third list: 7, 13, 33, 60, 84, 96, 111 The second number looked at would be 96
- 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.