Unit 3.17-3.18 Hacks
My Hacks from Lesson 3.17-3.18
def collatz(i):
while i != 1:
if i % 2 > 0:
i =((3 * i) + 1)
list_.append(i)
else:
i = (i / 2)
list_.append(i)
return list_
print('Please enter a number: ', end='')
while True:
try:
i = int(input())
list_ = [i]
break
except ValueError:
print('Invaid selection, try again: ', end='')
l = collatz(i)
print('')
print("Sequence: ", list_)
print('Number of iterations:', len(l) - 1)
i = 0
while i < 10:
if i <=2:
print("you got ", i, " hours of sleep")
print("did u stay up reading...")
elif i <=5:
print("you got ", i, " hours of sleep")
print("were you watching netflix?")
elif i <=8:
print("you got ", i, " hours of sleep")
print("ehh... you still need more sleep")
else:
print("you got ", i, " hours of sleep")
print("congratulations you got 9 hours!!!!!")
i = i+1
if i == 10:
break
s = 1
if s ==1:
print("you got ", s, " hours of sleep")
print("did u stay up reading...")
s = s+1
if s ==2:
print("you got ", s, " hours of sleep")
print("did u stay up reading...")
s = s+1
if s ==3:
print("you got ", s, " hours of sleep")
print("did u stay up reading...")
s = s+1
if s ==4:
print("you got ", s, " hours of sleep")
print("did u stay up reading...")
s = s+1
if s ==5:
print("you got ", s, " hours of sleep")
print("were you watching netflix?")
s = s+1
if s ==6:
print("you got ", s, " hours of sleep")
print("were you watching netflix?")
s = s+1
if s ==7:
print("you got ", s, " hours of sleep")
print("were you watching netflix?")
s = s+1
if s ==8:
print("you got ", s, " hours of sleep")
print("ehh... you still need more sleep")
s = s+1
if s == 9:
print("you got ", s, " hours of sleep")
print("congratulations you got 9 hours!!!!!")
Explanation:
The first code is way more efficient than the second as it utilizes loops and ranges of numbers within conditionals to run the code faster. This way was more efficient as it took less steps to write out the program, as well as for the algorithm to run. This way was also easier to debug as the code was short and easy to read and fix mistakes.
Hacks #3
What is Algorithm Efficiency?
Algorithm Efficiency is the number of steps required to run through an algorithm or find a solution. An efficient algorithm would have less steps to get to the end of an sequence. For example, in the efficient code from Hack #2, I used a while loop and for loop to make less steps for writing out a sleep schedule. An inefficient code, like the second part of Hack #2, might be repetitive or could be condensed into a much simpler code that gets the same results.
period = int(input("What period is it?"))
Class = ["Spanish", "US History", "Physics 2", "APSCP",]
def complete_day(Class):
if period == 1:
print("period: ", period)
for Class in Class:
if Class == "Spanish":
span = str(input("What Spanish HW"))
print("Spanish: ")
print(span)
elif period == 2:
print("Period: ", period)
if Class == "US History":
history = str(input("What History HW?"))
print("History: ")
print(history)
elif period == 3:
print("Period: ", period)
if Class == "Physics 2":
physics = str(input("What Physics HW?"))
print("Physics: ")
print(physics)
else:
print("Period: ", period)
apcsp = str(input("What APCSP HW?"))
print("APSCP: ")
print(apcsp)
complete_day(Class)
homework = ["Spanish", "US History", "Physics 2", "APSCP",]
def complete_hw(homework):
for homework in homework:
if homework == "Spanish":
span = str(input("What Spanish HW?"))
print("Spanish: ")
print(span)
elif homework == "US History":
history = str(input("What History HW?"))
print("History: ")
print(history)
elif homework == "Physics 2":
physics = str(input("What Physics HW?"))
print("Physics: ")
print(physics)
else:
apcsp = str(input("What APCSP HW?"))
print("APSCP: ")
print(apcsp)
complete_hw(homework)