3.9.1 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?

    • 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.
  2. 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!")
At least you're not missing practice
Sick = False
Practice = True

Bed = not(Practice) and (Sick)
if Bed == True:
    print("Stay in bed!")
elif Bed == False:
    print("Go to practice!")
Go to practice!

3.9.2 Hacks

  1. Print what directions I need the user to know
  2. State the price of the products
  3. Ask if they are paying with Credit Card or money
  4. Ask how much money you have that you are going to pay with if cash, ignore if credit
  5. Ask How many shirts you have
  6. ASk how many pants you have
  7. 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")
Welcome to a shop
Shirts are $3, Pants are $4
If you do not have enough money, we will ask that you put away some items and try again
Are you going to pay with cash or credit?
credit
How many shirts do you have?
4
How many pants do you have
2
Your total is: 20, have a good day

3.9.3

n = int(input("choose a number "))
y = 2
print(n)

if n % y == 0:
    print("n is even")
else:
    print("n is odd")
9
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!")
I'm thinking of a number between 1 and 100.
You guessed 50.
too high, try again! :)
Guess a number between 0 and 50.
You guessed 25.
too high, try again! :)
Guess a number between 0 and 25.
You guessed 11.
Too low, try again! :)
Guess a number between 11 and 25.
You guessed 17.
too high, try again! :)
Guess a number between 11 and 17.
You guessed 13.
Too low, try again! :)
Guess a number between 13 and 17.
You guessed 15.
Too low, try again! :)
Guess a number between 15 and 17.
You guessed 16.
Guess a number between 15 and 17.
You guessed the number in 12 guesses!

3.11

  1. calculate the middle index and create a binary tree for each of these lists
  1. 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
  2. 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)
[1, 12, 30, 43, 66, 74, 92]
[7, 13, 33, 60, 84, 96, 111]