Unit 3.9.1 Hacks

1) It is important to know that algorithms can have different results even though they look the same because it is good to know different ways to accomplish the same thing. This can help visualized the steps and process to make the coding process more efficient.

In addition algorithms having different outputs but similar code is important to know when trouble shooting code in order to find out what is wrong.

isTeams = False
isScore = True

if isTeams == True:
   print("You didn't win the game yet!")
else: 
   if isScore == True:
      print("You won!")
   else: 
      print("You didn't win the game yet!")
You won!
isTeams = False
isScore = True

winGame = not(isTeams) and isScore
if winGame == False:
    print("You didn't win the game yet!")
if winGame == True:
    print("You won the game!")
You won the game!

3.9.2 Hacks

Flow chart: natural lan

So if it is raining, and the temp is less then 45 degrees, then you should wear multiple layers, but if it isn't then, you can wear what ever u want. Although if it isn't raining, then it asks

isRaining = True
temp = 70
if isRaining == True and temp <  45:
      print("Wear multiple layers.")
else:
   if temp > 45:
      print("Wear whatever u want")
   else:
      if isRaining == False:
         bro = input("is it sunny or snowing?") 
      else:
          print("what is the weather like?")
Wear whatever u want

3.9.3 Hacks

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():
    num = input("What number?")
    #add something here
    return num #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 int(guess) < int(number):
        print("Too low") #change this
        lower_bound = guess
        return lower_bound, upper_bound
    elif int(guess) > int(number):
        print("Too high") #change this
        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!")
I'm thinking of a number between 0 and 100.
You guessed 10.
Too low
Guess a number between 10 and 100.
You guessed 20.
Too low
Guess a number between 20 and 100.
You guessed 30.
Too high
Guess a number between 20 and 30.
You guessed 25.
Too high
Guess a number between 20 and 25.
You guessed 25.
Too high
Guess a number between 20 and 25.
You guessed 23.
You guessed the number in 6 guesses!

num = random.randint(0, 100)
#print(num)
var = False
while var == False:
   put = input("I am thinking of a number from between 1-100.")
   print(put)
   if num < int(put):
      print("Guess a lower number")
   if int(put) < num:
      print("Guess a higher number")
   if num == int(put):
      print("Good Job, You guessed the right number!")
      var = True
   
50
Guess a higher number
60
Guess a higher number
70
Guess a higher number
80
Guess a higher number
90
Guess a lower number
90
Guess a lower number
85
Guess a lower number
83
Guess a higher number
84
Good Job, You guessed the right number!

3.11 Hacks

index = [12, 14, 43, 57, 79, 80, 99]
index[x].sort()
mid = int(len(index) / 2) 
print(mid)
print(index[mid])
3
57
index =[92, 43, 74, 66, 30, 12, 1]
index.sort()
mid = int(len(index) / 2) 
print(mid)
print(index[mid])
3
43
index = [7, 13, 96, 111, 33, 84, 60]
index.sort()
mid = int(len(index) / 2) 
print(mid)
print(index[mid])
3
60

2) Set one : 80

Set two: 74

Set three: 96

3) C, because it is unsorted

Going above and beyond!

I made this fibonacci algorithm in order to further my understanding on the topic.

I used elements from the lesson such as flow charts to organize my data and plan was I was going to code, and I used booleans to accomplish this task set in the algorithm.

def fib(num): # define function parameter = num
   if num == 0 or num == 1: # if num is equal to 1 or 0, then just return them
      return num
   return fib(num - 1) + fib(num - 2)  # else add the numbers 1 and 2 before the original one

for i in range(20): #loop 20 times 
   print(fib(i))