Code Break

Find the Lockbox Code as a Group

Solve the following problems using Python on your computer. You may not use GenAI to do so, and you must use programming to solve the problems-- do not solve by hand.

Once you solve the three problems, use the answers that are printed out as a lockbox code. Go to the front of the classroom and use this code on the lockbox. If people are waiting, you only have 1 try before going to the back of the line again.

Problem 1

Write a function that takes in an integer (n) and calculates a sequence such that the next number is the sum of the square of the previous number's digits (i.e. if n = 157, the next number will be 1^2+5^2+7^2 = 1 + 25 + 49 = 75). Repeat this process until n becomes a single-digit number.

RETURN the single-digit number.

Then run your function with an input of n=63946.

Code to get you started:

 
    def sum_of_squared_digits(n):
        next_value = n

        counter = 0
        while #REPLACE WITH  YOUR CODE:  # Continue until next_value becomes a single digit
            #REPLACE WITH YOUR CODE to get the sum of the squares of digits as the next_value
            #Hint: You can convert the number to a string to access individual digits
            #Hint 2: You might want to have a break statement while testing code so it doesn't run forever (if the code isn't right on the first try). 
            # This is what the counter is for. You can remove it from the final code.
            counter += 1
            if counter > 100:
                break
        return next_value
    
    sum_of_squared_digits(63946)
    

Problem 2

Write a function that takes in a list (l) as an argument and calculates a total by going through the items in the list and:

  1. Adds any numbers that are even to the total
  2. Subtracts any numbers that are odd and divisible by 3 to the total

RETURN the total.

Then run your function with an input of l = [4,6,2,8,4,6,9,3,1,1,1,2,5,4,9,7,12,15,3,0,2,3,9,6,5,10,14,19,15,9,4]

Code to get you started:

 
    def calculate_total(lst):
        total = 0
        for num in lst:
            if #REPLACE WITH YOUR CODE:  
                #REPLACE WITH YOUR CODE
            elif #REPLACE WITH YOUR CODE:  
                #REPLACE WITH YOUR CODE
        return total
    
    l =[4,6,2,8,4,6,9,3,1,1,1,2,5,4,9,7,12,15,3,0,2,3,9,6,5,10,14,19,15,9,4]  
    calculate_total(l)
    

Problem 3

Write a function that takes in an integer (n) and calculates a sequence of integers using the following rules:
  1. If the number is even, divide it by 2
  2. If the number is odd, multiply it by 3 and add 1
(This sequence is related to the Collatz Conjecture, which asks whether this sequence will always reach 1 for any starting number.)

RETURN the first digit that is less than 10 in the sequence.

Then run your function with an input of n = 703.

Code to get you started:

 
    def collatz_sequence(n):
        sequence = [] 
        next_number = n

        while #REPLACE WITH YOUR CODE:  # Continue until next_number becomes a single digit
            if #REPLACE WITH YOUR CODE:
                #REPLACE WITH YOUR CODE to update the next_number variable accordingly
            else:
                #REPLACE WITH YOUR CODE  to update the next_number variable accordingly
        return next_number

    collatz_sequence(703)