3.17-3.18 Hacks
efficiancy
def collatz(i):
while i != 1:
if i % 2 > 0:
i =((3 * i) + 1)
list_.append(int(i))
else:
i = (i / 2)
list_.append(int(i))
return list_
print('Please enter a number: ', end='' + "\n")
while True:
try:
i = int(input())
list_ = [i]
break
except ValueError:
print('Invaid selection, try again: ', end='')
l = collatz(i)
print('Sequence: ')
print(*l, sep=" ")
print('Number of iterations:', len(l) - 1)
Hack 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)
a = 1
b = 2
c = 3
print(a)
print(b)
print(c)
a = [1, 2, 3]
print(a)
this one is more efficient because it uses a list instead of three seperate variables with three values
tasks = ["have a snack", "homework", "gym", "eat", "shower", "sleep"]
# use of list for efficiency
def complete_tasks(tasks):
for task in tasks:
if task == "have a sanck":
print("slay")
elif task == "homework":
print("do not disturb")
elif task == "gym":
print("do not distub")
elif task == "eat":
print("slay")
elif task == "shower":
print("good job")
elif task == "sleep":
print("do not disturb")
complete_tasks(tasks)