Notes6
Defining algerithums
Lesson 1 | Defining Algorithms What is an algorithm? An algorithm is a process or set of rules to be followed through CODE. There are set limitations, this is what makes algorithms fun, you can your imagination and create whatever you wan with your own instructions!
Algorithms can be written in different ways and still accomplish the same tasks
Algorithms that appear similar can yield different side effects or results.
Some conditional statements can be written as the same as Boolean expressions (VICE VERSA)
Different algorithms can be developed or use to solve the same problem.
Algoritum is just a set of instructions
temp = int(input("Select a temperature from 0 to 99 degrees F"))
if (temp >= 90):
print("It's too hot outside!")
else:
if (temp >= 65):
print("Sure I will play outside!")
else:
print("It is too cold outside!")
# Input 54 and then 95, what do you notice?
temp = int(input("Select a temperature from 0 to 99 degrees F"))
if (temp >= 27000000):
print("Its hotter than the core of the sun!")
elif (temp >= 212):
print("IM MELTING!!!!")
elif (temp >= 90):
print("It's too hot outside!")
elif (temp >= 65):
print("Sure I will play outside!")
elif (temp >= -30):
print("It is too cold outside!")
elif (temp < -30):
print("I'll get frostbite!")
# Input 54 and then Input 95, what do you notice?
IsHoliday = False
IsWeekday = True
if IsHoliday:
driveWork = True
else:
if IsWeekday:
driveWork = True
else:
driveWork = False
print(driveWork)
Selection:
A process used in algorithms where a conditional if-statement leads to one of two outcomes
Outcome 1: if the conditional statement is true, something will happen
Outcome 2: if the conditional statement is false, something else will happen
Ex: see Example A
Iteration
A process used in algorithms that allows certain things to happen until a condition is satisfied
Once the condition is satisfied, then an outcome is produced
This can take the form of a for-loop, while-loop, and/or if-statement
food = ["apple", "banna", "orange"]
price = ["1.99", "2.99", "3.99"]
choice = input()
l = 0
for i in food:
l = 1 + 1
if i == choice:
print(price[l])
print("choose value for x")
varx=int(input("Enter any positive Integer"))
if (varx %2 == 0):
print("the number is even")
else:
print("the number is odd")
# Run this cell to see how it works
print("choose value for x")
varx=int(input("Enter any positive Integer"))
print(varx)
while varx != 1:
if (varx %2 == 0):
varx = varx/2
print(varx) # add Display
else:
varx = varx * 3 + 1
print(varx) # add Display
print(varx)
Dont remake everyting reuse what you have.
What is Binary Search?
Binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. An algorithm for iterating to find a value inside a data set
numbers = [7,1,12,19,21,16,30,10,6,5]
x = int(input())
inlist = False
for i in numbers:
if i == x:
print(i)
inlist = True
break
if inlist != True:
print("-1")