Notes

  • The three components of an algorithm are selection, sequences, and iterations. ### 3.9.1
  • Algorithms can be written in different ways yet still work in teh same way.
  • The flaw on the second code is that there is no "or equal to" sign for 70 so if someone has a grade of 70, nothing would print.
  • When two algorithms look similar, it does not mean that they do the same exact thing
  • Notes can help your future self to search for any bugs ### 3.9.2
  • When creating an algorithm, it is good to outline the process before coding.
  • Represent the code with a flowchart to help planning which makes the coding process more efficient.
  • Selection chooses from different options ### 3.9.3
  • It is important to use pre-existing algorithms as it can oftentimes make your life easier ### 3.11
  • Binary search divides a search interval in half
  • Binary search needs to put the numbers in order
  • Then it needs to find the middle number
  • Lists can be made with strings
  • You can show binary search with a binary tree

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?(0.15) It is important to know this because knowing that we can write code in different ways could save time through shortening certain parts of code. It is also important to know that different results can occur from similar code if we shift the parameters even slightly, such as removing an "or equal to" sign or shifting a number up or down.

Converted Conditional to Boolean Stuff:

CarColor = "Gray"
BodyStyle = "Sedan"

if CarColor == "Gray":
    if BodyStyle == "Sedan":
        print('You have a gray sedan')
    if BodyStyle != "Sedan":
        print('You have a gray car, but its not a sedan')

else:
    if BodyStyle == "Sedan":
        print("You have a sedan, but its not gray")
    if BodyStyle != "Sedan":
        print("Idk what car you have ")
You have a gray sedan
gray = True
sedan = True

CarColor = gray
BodyStyle = sedan

carColorStyle = CarColor and BodyStyle
if carColorStyle == True:
    print("you have a gray sedan")
if carColorStyle == False:
    print("You do not have a gray sedan")
you have a gray sedan

What I learned from this: I learned that nesting conditionals can shorten code as well as organize it better, this can also then be extracted from a flowchart which allows usage of this in the real world. Something that I could have improved on from the code is adding notes so that anyone else can tell what I am accomplishing.

gate = 5
i = 0
while (i >= 0):
    if i == gate:
        print("This is gate 5")
        break
    else:
        i = i+1
        print("gate " + str(i) + " is not gate 5, obviously")
gate 1 is not gate 5, obviously
gate 2 is not gate 5, obviously
gate 3 is not gate 5, obviously
gate 4 is not gate 5, obviously
gate 5 is not gate 5, obviously
This is gate 5

3.9.3 Hacks

num = 13

while (num != 1):
    if num % 2 == 0:
            num = num / 2
            print(num)
    else:
        if num != 1:
            num = num * 3 + 1
            print(num)
else:
    print("it is now number 1")
40
20.0
10.0
5.0
16.0
8.0
4.0
2.0
1.0
it is now number 1
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(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():
    number = input("guess")
    #add something here
    return number #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, try again! I TOTALLY BELIVE IN YOU") #change this
        lower_bound = guess
        return lower_bound, upper_bound
    elif int(guess) > int(number):
        print("Too high, try again! I TOTALLY BELIVE IN YOU") #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 1 and 100.
You guessed 0.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 0 and 100.
You guessed 1.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 1 and 100.
You guessed 2.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 2 and 100.
You guessed 3.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 3 and 100.
You guessed 4.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 4 and 100.
You guessed 5.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 5 and 100.
You guessed 6.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 6 and 100.
You guessed 7.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 7 and 100.
You guessed 8.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 8 and 100.
You guessed 9.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 9 and 100.
You guessed 10.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 10 and 100.
You guessed 11.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 11 and 100.
You guessed 12.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 12 and 100.
You guessed 13.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 13 and 100.
You guessed 14.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 14 and 100.
You guessed 15.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 15 and 100.
You guessed 16.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 16 and 100.
You guessed 17.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 17 and 100.
You guessed 18.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 18 and 100.
You guessed 19.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 19 and 100.
You guessed 20.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 20 and 100.
You guessed 21.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 21 and 100.
You guessed 22.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 22 and 100.
You guessed 23.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 23 and 100.
You guessed 24.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 24 and 100.
You guessed 25.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 25 and 100.
You guessed 26.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 26 and 100.
You guessed 27.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 27 and 100.
You guessed 28.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 28 and 100.
You guessed 29.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 29 and 100.
You guessed 30.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 30 and 100.
You guessed 31.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 31 and 100.
You guessed 32.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 32 and 100.
You guessed 33.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 33 and 100.
You guessed 34.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 34 and 100.
You guessed 35.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 35 and 100.
You guessed 36.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 36 and 100.
You guessed 37.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 37 and 100.
You guessed 38.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 38 and 100.
You guessed 39.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 39 and 100.
You guessed 40.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 40 and 100.
You guessed 41.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 41 and 100.
You guessed 42.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 42 and 100.
You guessed 43.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 43 and 100.
You guessed 44.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 44 and 100.
You guessed 45.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 45 and 100.
You guessed 46.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 46 and 100.
You guessed 47.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 47 and 100.
You guessed 48.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 48 and 100.
You guessed 49.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 49 and 100.
You guessed 50.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 50 and 100.
You guessed 51.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 51 and 100.
You guessed 52.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 52 and 100.
You guessed 53.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 53 and 100.
You guessed 54.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 54 and 100.
You guessed 55.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 55 and 100.
You guessed 56.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 56 and 100.
You guessed 57.
Too low, try again! I TOTALLY BELIVE IN YOU
Guess a number between 57 and 100.
You guessed 58.
You guessed the number in 59 guesses!

3.9.3 Hacks

  1. 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
listOne = [12,14,44,57,79,80,99]
listTwo = [92,43,74,66,30,12,1]
listThree = [7,13,96,111,33,84,60]
numLists = [listOne, listTwo, listThree]
for x in range(len(numLists)):
    numLists[x].sort()
    middle = int(len(numLists[x])/2)
    print("The middle indices are",numLists[x][middle])
The middle indices are 57
The middle indices are 43
The middle indices are 60
  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? For the first list, the next number would be 80, for the second list the next number would be 74, and for the third list, the next number looked at would be 96.
  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] Because it is not sorted in order.

    d. ["xylophone", "snowman", "snake", "doorbell", "author"]

What I have learned and how I can apply this in the future

I have learned that making flowcharts is actually very useful when coding as it can save time. This is because it can help you to form an outline for your code an lowers your chances of any human errors in coding.