Hack 1: Collatz and printing Hailstone Numbers

sequence = []
def combinedcollatz(i):
    while i > 1:
        if (i % 2):    # i is odd
            sequence.append(i)
            i = 3*i + 1
        else:   # i is even
            sequence.append(i)
            i = i//2
    if i == 1:
        sequence.append(1)
    else:
        print(i, "is invalid, please try again") #this will print if the number is invalid
i = int(input('Enter i: '))
combinedcollatz(i)
print("Hailstone numbers: ", sequence, "\nNumber of iterations: ", len(sequence)-1)
Hailstone numbers:  [34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] 
Number of iterations:  13

Hack 2: Write 2 algorithms (efficent/innefficent)

i = 0 # efficient code
while i < 10:
    print("mmmm mmmm not enought studying my friend")
    i += 1 
    if i >= 10:
        print("Great job! You've studied enough")
        break
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
Great job! You've studied enough
i = 0  # Inefficient code
if i == 0:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 1:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 2:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 3:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 4:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 5:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 6:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 7:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 8:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i == 9:
    print("mmmm mmmm not enought studying my friend")
    i += 1
if i >= 10:
    print("Great job! You've studied enough")
    i += 1
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
mmmm mmmm not enought studying my friend
Great job! You've studied enough

The difference in efficiency

  • The inefficient piece of code uses an if statement for every single hour of studying. It has a lot of repetition like the print("mmmm mmmm not enought studying my friend"), as well as the i += 1 for every single hour.
  • The efficient code uses a while loop. The small piece of code iterates through the hours, and compares them to the conditions. It doesn't have repetition (modularity), allowing it to be faster and easier to change/read with the same result.

Hack 3 Algorithm efficiency:

  • Algorithm efficiency is the amount of "effort", or steps, that an algorithm must go through to complete it's purpose. An efficent algorithm is fast and goes through fewer proccesses.

Hack 4 Daily Tasks

tasks = ["brush teeth", "skin care", "pack water/lunch", "go to school", "homework"]
finished = []
 
def complete_tasks(tasks):
  for task in tasks:
# code to complete each task goes here
    print("Doing now: ", task)
    finished.append(task)

 
# and so on for each task in the list
 
# call the function to complete the tasks
complete_tasks(tasks)
print("Done: ", finished)
Doing now:  brush teeth
Doing now:  skin care
Doing now:  pack water/lunch
Doing now:  go to school
Doing now:  homework
Done:  ['brush teeth', 'skin care', 'pack water/lunch', 'go to school', 'homework']

This code takes the item in the tasks list and prints them as they go. Then, it appends the item into a "finished" list. It is also an efficient algorithm since it utilizes a for loop and calls the item within the definition of the function.

Extra Credit

class_schedule = []
i = 0
while i < 5:
    period = input("What is a class in your schedule?")
    class_schedule.append(period)
    i += 1
    if i == 5:
        break
print("Your schedule: ", class_schedule)
print("Period 1: ", class_schedule[0], " Time: 8:35 AM-9:44 AM")
print("Period 2: ", class_schedule[1], "Time: 9:49 AM-10:58 AM")
print("Break: 10:58 AM-11:08 AM")
print("Period 3: ", class_schedule[2], "Time: 11:13 AM-12:22 PM")
print("Lunch: 12:22 PM-12:52 PM")
print("Period 4: ", class_schedule[3], "Time: 12:57 PM-2:06 PM")
print("Office Hours: 2:06 PM-2:31 PM")
print("Period 5: ", class_schedule[4], "Time: 2:36 PM-3:45 PM")
Your schedule:  ['Offroll', 'AP Calculus AB', 'ASB', 'APCSP', 'Photography']
Period 1:  Offroll  Time: 8:35 AM-9:44 AM
Period 2:  AP Calculus AB Time: 9:49 AM-10:58 AM
Break: 10:58 AM-11:08 AM
Period 3:  ASB Time: 11:13 AM-12:22 PM
Lunch: 12:22 PM-12:52 PM
Period 4:  APCSP Time: 12:57 PM-2:06 PM
Office Hours: 2:06 PM-2:31 PM
Period 5:  Photography Time: 2:36 PM-3:45 PM