3.9 and 3.11 Hacks
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?
- It is important to know that algorithms that look different can do the same thing because there are a multitude of different methods and structures that can be used to receive the same answer. For example, in the examples given in the presentation, there are different methods that were used to derive similar results. One used if and else statements to find out which conditions best suited the predicament. The other one simply used factors A, B, and C to categorize the value given, and although both displayed the same result, different inputs may result in different outcomes (outputs). Debugging will also vary.
Converted Boolean conversion:
Sick = True
Practice = False
if Sick == True and Practice == True:
print("I think you should skip practice today")
elif Sick == True and Practice == False:
print("At least you're not missing practice")
elif Sick == False and Practice == True:
print("Go to practice!")
Sick = False
Practice = True
Bed = not(Practice) and (Sick)
if Bed == True:
print("Stay in bed!")
elif Bed == False:
print("Go to practice!")
- Print what directions I need the user to know
- State the price of the products
- Ask if they are paying with Credit Card or money
- Ask how much money you have that you are going to pay with if cash, ignore if credit
- Ask How many shirts you have
- ASk how many pants you have
- Ring up the total, give the following responses based on your balance:
- If you can afford, say the total and the change
- If you cannot afford the total, ask to put something back
- if you cannot afford anything in the first place (less than 3), tell them that they can't afford anything in the first place
- if you spend more than 100 dollars with a credit card, you will have exceeded your CC spending limit
print("Welcome to a shop")
print("Shirts are $3, Pants are $4")
print("If you do not have enough money, we will ask that you put away some items and try again")
shirts = 3
pants = 4
print("Are you going to pay with cash or credit?")
Question = input("In all lowercases: Cash or Credit?")
print(Question)
if Question == "credit":
print("How many shirts do you have?")
num_shirts = int(input("How many shirts do you have? "))
print(num_shirts)
print("How many pants do you have")
num_pants = int(input("How many pants do you have? "))
print(num_pants)
prod = shirts * num_shirts
prod2 = pants * num_pants
if (prod + prod2) <= 100 and (prod + prod2) <= 5:
print("Your total is: " + str(prod + prod2) + ", have a good day")
elif (prod + prod2) > 100:
print("You exceeded your spending limit")
elif Question == "cash":
print("How much money do you have?")
money = int(input("How much money do you have? "))
print(money)
print("How many shirts do you have?")
num_shirts = int(input("How many shirts do you have? "))
print(num_shirts)
print("How many pants do you have")
num_pants = int(input("How many pants do you have? "))
print(num_pants)
prod = shirts * num_shirts
prod2 = pants * num_pants
if money >= prod + prod2:
print("Your change is " + str(money - (prod + prod2)) + ", have a good day")
elif money < prod + prod2 and money >= 3:
print("please put something back, you are " + str((prod + prod2) - money) + " short")
else:
print("Bro you can't afford anything in the first place")
n = int(input("choose a number "))
y = 2
print(n)
if n % y == 0:
print("n is even")
else:
print("n is odd")
import random
#sets variables for the game
num_guesses = 5
user_guess = 0
upper_bound = 100
lower_bound = 0
#generates a random number
number = random.randint(1,100)
# print(number) #for testing purposes
print(f"I'm thinking of a number between 1 and 100.")
#Write a function that gets a guess from the user using input()
def guess():
numero = int(input("Number"))
return numero#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("Too low, try again! :)") #change this
lower_bound = guess
elif guess > number:
print("too high, try again! :)") #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!")
3.11
- 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
- 1, 12, 30, 43, 66, 74, 92
- 43 -Link to Binary Tree 2
- 7, 13, 96, 111, 33, 84, 60
- sort into order: 7, 13, 33, 60, 84, 96, 111
- 7, 13, 33, 60, 84, 96, 111
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?
- 12, 14, 43, 57, 79, 80 99
- 80
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. [3, 2, 8, 12, 99] because the numbers are not sorted into ascending or descending order like b and it is not in alphabetical order like d and a!!!!
Binary2 = [92, 43, 74, 66, 30, 12, 1]
Binary2.sort()
print(Binary2)
Binary3 = [7, 13, 96, 111, 33, 84, 60]
Binary3.sort()
print(Binary3)