Exercises 4 - 6 - SOLVED#

Some slightly longer examples now. These are all code written to do some action, or solve some problem, but don’t work. Why not? Please fix them!

Exercise 4 - Arithmetic expression#

We want to write a function that will take three integers A, B or C.

We want the code to check if A+B = C OR if A-B =C OR if A*B=C OR if A/B=C. If it is return True. Otherwise return False. We’ve written the following code, and sometimes it works, but sometimes it doesn’t.

Find out why and edit the code to fix the problem

# CHANGE THIS CODE TO FIX THE PROBLEM
def example_4(a, b, c):

    if a + b == c or a * b == c or b * c == a or a - b == c:
        return True
    return False


print("example_4(1,2,3)", example_4(1,2,3)) # Should be True, 1 + 2 = 3

print("example_4(3,6,17)", example_4(3,6,17)) # Should be False

print("example_4(10,2,5)", example_4(10,2,5)) # Should be True 10 / 2 = 5

print("example_4(2,3,6)", example_4(2,3,6)) # Should be True, 2 * 3 = 6
example_4(1,2,3) True
example_4(3,6,17) False
example_4(10,2,5) True
example_4(2,3,6) True

Exercise 5 - The non-reproducible function#

Here we want to create a simple function that takes an integer \(a\), and then returns sum(a,a+1,a+2). We run this on 3 examples and the first example is correct, but the other answers are not. Then we run it again and now get silly output. What’s going on here? … This is an example of a well documented issue in python, but this is also a good case for using PDB to try to understand what is happening.

Find the error and fix the code so that the function does what it’s supposed to

# What is wrong, if anything, with this code??

def exercise_5(a):
    vals = [] # Moved vals = [] inside the function!!
    for i in range(3):
        vals.append(a + i)
    
    return sum(vals)
print(exercise_5(0), exercise_5(1), exercise_5(2))
# And we'll run it again, because why not.
print(exercise_5(0), exercise_5(1), exercise_5(2))
3 6 9
3 6 9

Exercise 6 - Square digits#

Here the task is to write a code to do the following.

  • Consider a sequence of numbers \(a_0\), \(a_1\), …, \(a_n\), in which an element is equal to the sum of squared digits of the previous element. The sequence ends once an element that has already been in the sequence appears again. Given the first element a0, find the length of the sequence.

So for example if a0 = 16. 1**2 + 6**2 = 37 -> 3**2 + 7**2 = 58 -> 5**2 + 8**2 = 89 -> 8**2 + 9**2 = 145 -> 1**2 + 4**2 + 5**2 = 42 -> 4**2 + 2**2 = 20 -> 2**2 + 0**2 = 4 -> 4**2 = 16 We’ve already seen 16 so we stop here. The sequence is [16,37,58,89,145,42,20,4,16] which has a length of 9, return 9.

Except the code we’ve written doesn’t return this. What is wrong?

def exercise_6(a0):

    cur = a0
    was = set()

    while not (cur in was):
        was.add(cur)
        nxt = 0
        while cur > 0:
            nxt += (cur % 10) * (cur % 10)
            cur //= 10
        cur = nxt
    # Need to add one on here as sets cannot store duplicate values and the last value in the sequence must
    # be a duplicate (and the only duplicate)
    return len(was) + 1

print("exercise_6(103) gives:",exercise_6(103), "\n Should be 4")
print()
print("exercise_6(1) gives:",exercise_6(1), "\n Should be 2")
print()
print("exercise_6(13) gives:",exercise_6(13), "\n Should be 4")
print()
print("exercise_6(16) gives:",exercise_6(16), "\n Should be 9") # Oops, this one also called exercise_5, not exercise_6!!
print()
print("exercise_6(612) gives:",exercise_6(612), "\n Should be 16")
print()
print("exercise_6(89) gives:",exercise_6(89), "\n What should it be?")
exercise_6(103) gives: 4 
 Should be 4

exercise_6(1) gives: 2 
 Should be 2

exercise_6(13) gives: 4 
 Should be 4

exercise_6(16) gives: 9 
 Should be 9

exercise_6(612) gives: 16 
 Should be 16

exercise_6(89) gives: 9 
 What should it be?