3.9/3.11- Deveoping Algorithms/Binary Search
- Algorithms
- You can make a grade callulator based on a certain persecntage and arithmetic
- When collaborating or working on group projects, two people might come up with two different ways to solve a problem, and that happens a lot.
- know that same goal can be achieved in many ways (the possibilities are endless)
- make notes in you code! (explain how it works to others or you future self)
- Boolean statements can be put into nested conditionals to cause a chain of asking wheter an item is true or false with a lot of questions.
- Outlining an algorithm is very important to know how to organize your code.
- Iteration is like a loop
- Existing Algorithm:
- determining min or max of two or more numbers
- computing the sum or average
- identifying if an integer is even or odd
- Binary Search: To divide a serach interval in half multiple times until something stops it
- It is important to know that algorithms that look different can do the same thing and algorithms that look the same might have differnt results for multiple reason. If you are working with a team you have to understand what their code is saying so you can use it with them. Also it allows you to make different types of code and understand it better making you an even better programmer.
isFriday = True
isThurday = False
#If Statements
if isFriday == True:
print("Go to Canes")
else:
print("It's not Friday and that sucks")
if isThurday == True:
print("Go to practice")
isFriday = False
isThursday = True
# Setting Var
doHomework = isThursday or not(isFriday)
# If statements
if doHomework == False:
print("Don't do Homework")
else:
print("Do Homework")
age = 17
money = 4
#When they are under age they wait a year until they are over
while age <= 20:
age = age + 1
print(age)
if age >= 21:
if money >= 3.99:
print("Here is your Drink")
#if they don't have enough money they will say they are poor
else:
print("im poor")
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 1 and 100.")
#Write a function that gets a guess from the user using input()
def guess():
#add something here
num = input("What is the number you are thinking of?")
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("You guessed too low") #change this
lower_bound = guess
return lower_bound, upper_bound
elif int(guess) > int(number):
print("You guessed 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!")
index = [12, 14, 43, 57, 79, 80, 99]
mid = int(len(index) / 2)
print(mid)
print(index[mid])
index = [92, 43, 74, 66, 30, 12, 1]
mid = int(len(index) / 2)
print(mid)
print(index[mid])
index = [7, 13, 96, 111, 33, 84, 60]
mid = int(len(index) / 2)
print(mid)
print(index[mid])
- Set 1: 80, Set 2: 74, Set 3: 96
- C is unsorted therefore it is correct.
Vocab
- Existing Algorithm: determining min or max of two or more numbers,computing the sum or average, identifying if an integer is even or odd
- Binary Search: To divide a serach interval in half multiple times until something stops it
- Boolean statments: Put into nested conditionals to cause a chain of asking wheter an item is true or false with a lot of questions.
def binary_search(arr, target):
start = 0
end = len(arr) - 1
while start <= end:
mid = (start + end) // 2
if target > arr[mid]:
start = mid + 1
elif target < arr[mid]:
end = mid - 1
else:
return mid
return -1
SkyBlue = True
if SkyBlue == True:
print("The Sky is Blue")