title: Lesson17-18 hacks description: Lesson 17-18 hacks toc: true

comments: true

Take the two codes above and combine them so one imput gives the output that contains both the hailstone numbers and the number of iterations it takes i = 1. (.25) (Extra credit: If your code is more efficient it will recieve a higher grade.)

i = int(input("Please input a number: "))
list = []

def collatz_seq(n):
    list = []
    while n != 1:
        if (n % 2):
            n = 3*n + 1
        else:
            n = n/2
        list.append(n)
    return list

result = collatz_seq(i)
print("Input:" , i)
print(result)
print("Iteration count:", len(result))
Input: 2
[1.0]
Iteration count: 1

Hack #2 1) Code 2 algorithms: (.25)

The first Algorithm should be efficient while the second should be inefficient. Then explain what distinguishes the efficient from the non-efficient one. (In your own words)

car = []

car.append({
    "Make": "BMW",
    "Model": "M4 CSL",
    "Year": "2023",
})
def loop():
    for data in car:
        print(data)

loop()
{'Make': 'BMW', 'Model': 'M4 CSL', 'Year': '2023'}
car = "Make: BMW", "Model: M4 CSL", "Year: 2023"
print(car)
('Make: BMW', 'Model: M4 CSL', 'Year: 2023')

Algorithm 1 properly displays efficiency, as it appends the list and utilizes a for loop in order to display the data of the given car in an orderly manner. Algorithm 2 is inefficient because the data is only displayed in the order that it is typed in.

Explain algorithm efficiency in your own words (.25)

Algorithm efficiency in computer science is the measure of how well a particular algorithm performs when compared to other algorithms. It is typically calculated by measuring the amount of time and space it takes to complete a task.

tasks = ["Berry", "Apple", "Heading to school!", "Strawberry", "Banana"]
 
def daily_routine(tasks):
 for task in tasks:
 
    if task == "Berry":
     print("Berry")
    elif task == "Apple":
     print("Apple")
    elif task == "Grape":
       print("Grape")
    elif task == "Strawberry":
       print("Strawberry")
    elif task == "Banana":
       print("Banana")

daily_routine(tasks)
Berry
Apple
Strawberry
Banana