3.17 and 3.18 Student Lesson (group 9)
My homework for the 3.17-3.18 lesson
- Hack 1: Collatz and printing Hailstone Numbers
- Hack 2: Write 2 algorithms (efficent/innefficent)
- The difference in efficiency
- Hack 3 Algorithm efficiency:
- Hack 4 Daily Tasks
- Extra Credit
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)
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
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
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.
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)
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.
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")