3.9 & 3.11 Hacks
These are my quiz results.
- Hack 1
- Examples of Algorithms
- How Nested Conditionals are Related to Boolean Expressions
- Hack 2
- Developing Algorithms
- Hack 3
- Hack 4
Hack 1
Three components of an algorithm:
- sequence
- selection
- Iteration
It is important to know that algorithms can be written in different ways in a way they can do the same thing because the outputs are the same in different ways.
It is important to know that algorithms that look the same might have different results because the outputs are different even though the codes are almost the same.
Examples of Algorithms
print("What Grade Did You Get?")
grade = int(input(79))
if grade >= 95:
print("Wow! Good job!")
if 70 <= grade < 95:
print("Nice!")
elif grade < 70:
print("Do Better")
How Nested Conditionals are Related to Boolean Expressions
The following 2 pieces of code are supposed to print “don’t go to school” unless:
isLatestart = False
isEarlyStart = True
if isLatestart == True:
print("go to school late!")
else:
if isEarlyStart == True:
print("go to school early!")
else:
print("go to school late!")
isLatestart = False
isEarlyStart = True
driveSchool = not(isLatestart) and isEarlyStart
if driveSchool == False:
print("go to school late!")
if driveSchool == True:
print("go to school early!")
Hack 2
Developing Algorithms
When creating an algorithm, it is extremely important to outline the process before coding.
print("The parking rate is as follows: \n Less than one hour: Free \n 1-2 hours: $5 \n 2-3 hours: $8 \n 3-4 hours: $10 \n 4+ hours: $12")
time = float(input("How many hours have you parked at this garage?"))
print("How many hours have you parked at this garage?")
print(time, "hours costs:")
if time < 1 :
print("Free")
elif time >= 1 and time < 2 :
print("$5")
elif time >= 2 and time < 3 :
print("$8")
elif time >= 3 and time < 4 :
print("$10")
else:
print("$12")
print("Have a good day!")
Hack 3
import random
#sets variables for the game
num_guesses = 0
user_guess = 0
upper_bound = 100
lower_bound = 0
#generates a random number
number = random.randint(1,100)
# print(number) #for testing purposes
print(number)
print("I'm thinking of a number between 1 and 100.")
#Write a function that gets a guess from the user using input()
def guess():
g = int(input("Choose a number"))
return g #add something here
#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 guess < number:
print("Higher, your getting there") #change this
lower_bound = guess
elif guess > number:
print("Lower, your getting closer") #change this
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!")
Hack 4
One = [12,14,44,57,79,80,99]
Two = [92,43,74,66,30,12,1]
Three = [7,13,96,111,33,84,60]
Lists = [One, Two, Three]
# loops through the range of the length of lists
for x in range(len(Lists)):
#sorts the list in order
Lists[x].sort()
#takes the middle index
middleindex = int(len(Lists[x])/2)
#outputs
print("Middle Index of List #",x+1,"is",Lists[x][middleindex])
Middle Index of List # 1 is 57
Middle Index of List # 2 is 43
Middle Index of List # 3 is 60
Set 1: 80, Set 2: 74, Set 3: 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 is out of order so therefore it is c