Hacks #1

Collatz Sequence and Iteration

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)
Please enter a number: Sequence:  [7, 22, 11.0, 34.0, 17.0, 52.0, 26.0, 13.0, 40.0, 20.0, 10.0, 5.0, 16.0, 8.0, 4.0, 2.0, 1.0]
Number of iterations: 16

Hack 2

Efficient Algorithm:

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
you got  0  hours of sleep
did u stay up reading...
you got  1  hours of sleep
did u stay up reading...
you got  2  hours of sleep
did u stay up reading...
you got  3  hours of sleep
were you watching netflix?
you got  4  hours of sleep
were you watching netflix?
you got  5  hours of sleep
were you watching netflix?
you got  6  hours of sleep
ehh... you still need more sleep
you got  7  hours of sleep
ehh... you still need more sleep
you got  8  hours of sleep
ehh... you still need more sleep
you got  9  hours of sleep
congratulations you got 9 hours!!!!!

Inefficient Algorithm

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!!!!!")
you got  1  hours of sleep
did u stay up reading...
you got  2  hours of sleep
did u stay up reading...
you got  3  hours of sleep
did u stay up reading...
you got  4  hours of sleep
did u stay up reading...
you got  5  hours of sleep
were you watching netflix?
you got  6  hours of sleep
were you watching netflix?
you got  7  hours of sleep
were you watching netflix?
you got  8  hours of sleep
ehh... you still need more sleep
you got  9  hours of sleep
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.

Hacks #4 Tasks

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)
period:  1
Spanish: 
nada! felices fiestas!

EXTRA: Agenda Planner based on Mini Activity Idea

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)
Spanish: 
preterite and imperfect
History: 
study for unit test
Physics: 
marshmallow microwave lab
APSCP: 
HACKS