Lesson Nine Homework
Lesson nine hacks
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)
Hacks #2
-
Code 2 algorithms: (.25)
-
The first Algorithm should be efficient while the second should be innefficient. Then explain what distinguishes the efficient from the non-efficient one. (In your own words)
-
Explain algorithm efficiency in your own words (.25)
-
Code an efficient program that shows your daily tasks or schedule. (We have an example shown in our lesson) (.25)
class_to_do = []
i = 0
while i == 0:
task = input("What assignment do you have to do today?")
if task == "nothing":
i = i + 1
else:
class_to_do.append(task)
print("Tasks you have to do today: ", class_to_do)
taskone = input("What assignment do you have to do today?")
print("You have to do", taskone)
tasktwo = input("What's another assignment you have to do today?")
print("You have to do", tasktwo)
taskthree = input("What's another assignment you have to do today?")
print("You have to do", taskthree)
taskfour = input("What's another assignment you have to do today?")
print("You have to do", taskfour)
efficiency expained: The first algorithim is much more efficient than my second algorithim. My second algorithim has repetition and duplication of inputs and printing. My first algoritim fixes this issue with way fewer steps by having a loop that will allow a user to keep inputing tasks. This avoids duplication and is easier for a programmer to understand if they're are hundreds of tasks. In my second algorithim, a programer would have to keep adding individual task variables, inputs, and print them for every single task. This is very tedious if there are many tasks. My first algorithim only has nine lines of code with two or three steps, and can print unlimited number of tasks. My second algorithim requires two lines of code for every task, or two steps for every task. This means that the code would get very long if there are many tasks.
-
Algorithim efficiency is the ability of an algorithim to be concise, avoiding repetition and duplication. Efficient algorithims are short in length and require less steps to solve a problem.
-
Daily tasks program below: MAYBE EXTRA CREDIT pwetty pwease for making a COMPLEX efficient alforithim for all the months in the year. This algorithim below is efficient because it only requires two/three steps instead of writing out three steps for every single month with appending each list for that month.
yearly_schedule = {
'January' : [],
'February' : [],
'March' : [],
'April' : [],
'May' : [],
'June' : [],
'July' : [],
'August' : [],
'September' : [],
'October' : [],
'November' : [],
'December' : []
}
i = 0
while i == 0:
month = input("What month would you like to add to?")
if month in yearly_schedule:
event = input("What event would you like to add?")
yearly_schedule[month].append(event)
elif month == "None":
i = 1
else:
print("That is not a month in the year or you did not capitilize your month.")
print(yearly_schedule)