Unit 3.18

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)
Please enter a number: 
Sequence: 
50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Number of iterations: 24

Unit 3.17

num_list = [43,12,4,5,63,1,6,10]

search_num = 5
if num_list[0] == search_num:
    print("The number was at index 0.")
if num_list[1] == search_num:
    print("The number was at index 1.")
if num_list[2] == search_num:
    print("The number was at index 2.")
if num_list[3] == search_num:
    print("The number was at index 3.")
if num_list[4] == search_num:
    print("The number was at index 4.")
if num_list[5] == search_num:
    print("The number was at index 5.")
if num_list[6] == search_num:
    print("The number was at index 6.")
if num_list[7] == search_num:
    print("The number was at index 7.")
The number was at index 3.
num_list = [43,12,4,5,63,1,6,5]
search_num = 5

index = num_list.index(search_num)
print(f"The number was at index {index}.")
The number was at index 3.

For this linear search algorithm, the inefficient code needs to manually input the index of the num_list. As seen each index is compared with the command

if num_list[0] == search_num:
    print("The number was at index 0.")

This makes the code extremely long as two lines of code needs to be written for each index. However, with a the index function, given an input, it will return the index at which the number appears, thus decreasing the lines of code from 18 to 4.

Algorithm efficiency: a property of an algorithm which relates to the amount of computational resources used by the algorithm, the more efficient an algorithm is, the better the code is because it will take less time and code

tasks = ["finish homework", "shower", "skincare",  "read", "sleep"]
 
def complete_tasks(tasks):
    for task in tasks: 
        if task == "finish homework":
            print("Finishing homework now!")
        elif task == "shower":
            print("Showering now!")
        elif task == "skincare":
            print("Skincare now!")
        elif task == "read":
            print("Reading now!")
        elif task == "sleep":
            print("Sleeping now!")
        
complete_tasks(tasks)

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)