Unit 3.9 & 3.11 Selection, Sequences, and Iterations
- Notes
- 3.9.1 Hacks
- 3.9.2 Hacks
- 3.9.3 Hacks
- 3.9.3 Hacks
- What I have learned and how I can apply this in the future
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 ")
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")
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")
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")
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!")
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])
- 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.
-
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"]