Exercises 7 to 9 - SOLVED#

Now the problems are more challenging, the code is longer, and you need to understand what the code is doing. This is more representative of “real life” debugging (especially if you have to debug someone else’s code), but these problems are hard. We would not ask questions like this in the coursework, but something like exercise 6 could be used.

Exercise 7 - Knight in chess#

Here’s the problem this code is intended to solve

  • Given a position of a knight on the standard chessboard, find the number of different moves the knight can perform.

Positions on a chessboard are given like a1 or a8 or h1 or h8 which are the four corners.

Except the code doesn’t work. Find out why not and fix the problem

def exercise_7(cell):
    # If you don't know what ord does, you can look it up. Type ord? in a blank cell!
    # Then try doing things like ord('c') - ord('a') to see what I'm doing here.
    # And things like ord('4') - ord('0')
    row = ord(cell[1]) - ord('0') # FIXED: Needed to be cell[1], not cell[0] again!
    column = ord(cell[0]) - ord('a') + 1
    steps = [
            [-2, -1], [-1, -2], [1, -2], [2, -1],
            [2, 1], [1, 2], [-1, 2], [-2, 1]
            ]
    answer = 0

    for i in range(len(steps)):
        tmpRow = row + steps[i][0]
        tmpColumn = column + steps[i][1]
        if (tmpRow >= 1 and tmpRow <= 8 and
            tmpColumn >= 1 and tmpColumn <= 8):
            answer += 1

    return answer

print("exercise_7('a1') gives:",exercise_7('a1'), "\n Should be 2")
print()
print("exercise_7('c2') gives:",exercise_7('c2'), "\n What should it be?") # Fix syntax error here
print()
print("exercise_7('d4') gives:",exercise_7('d4'), "\n What should it be?")
print()
print("exercise_7('g6') gives:",exercise_7('g6'), "\n What should it be?")
print()
print("exercise_7('a3') gives:",exercise_7('a3'), "\n What should it be?")
print()
print("exercise_7('b7') gives:",exercise_7('b7'), "\n What should it be?")
exercise_7('a1') gives: 2 
 Should be 2

exercise_7('c2') gives: 6 
 What should it be?

exercise_7('d4') gives: 8 
 What should it be?

exercise_7('g6') gives: 6 
 What should it be?

exercise_7('a3') gives: 4 
 What should it be?

exercise_7('b7') gives: 4 
 What should it be?

Exercise 8 - Number of triangles#

Here’s the problem that this code is intended to solve

  • You have N sticks. Given an array of the lengths of each stick, find how many different triangles that can be constructed from taking any three of these sticks. Note that the sum of the lengths of any two sides of a triangle must be greater than the third side (here we are not considering a triangle with angles (90,0 and 90) degrees to be a triangle, see third example). Equivalent triangles that are constructed from different sticks are considered different.

So for example if sticks=[3,5,7,9] the answer should be 3. You can make triangles with [3,5,7], [5,7,9] and [3,7,9] but [3,5,9] cannot form a triangle.

Except the code doesn’t work. Find out why not and fix the problem

WARNING: This and the next exercise are challenging! Especially here the code author didn’t bother with comments making this very difficult to follow!

def exercise_8(sticks):
    sticks = sorted(sticks) # FIXED: Need to sort the array

    ans = 0
    for i in range(0, len(sticks) - 2):
        for j in range(i + 1, len(sticks) - 1):
            mx = sticks[i] + sticks[j]
            l = j
            r = len(sticks)
            while r - l > 1:
                m = (l + r) // 2
                if sticks[m] >= mx:
                    r = m
                else:
                    l = m
            ans += l - j # FIXED l-j here not r-j

    return ans

print("exercise_8([3,5,7,9]) gives:",exercise_8([3,5,7,9]), "\n Should be 3") # FIXED: Error in print statement "should be 3", not "should be 2"
print()
print("exercise_8([4, 4, 4, 4]) gives:",exercise_8([4, 4, 4, 4]), "\n Should be 4")
print()
print("exercise_8([1,2,3]) gives:",exercise_8([1,2,3]), "\n Should be 0")
print()
print("exercise_8([3,5,9,7]) gives:",exercise_8([3,5,9,7]), "\n Should be 3")
print()
print("exercise_8([4,9,2,4,7,2,5,10,12,100,3,12]) gives:",exercise_8([4,9,2,4,7,2,5,10,12,100,3,12]), "\n Should be 62")
print()
exercise_8([3,5,7,9]) gives: 3 
 Should be 3

exercise_8([4, 4, 4, 4]) gives: 4 
 Should be 4

exercise_8([1,2,3]) gives: 0 
 Should be 0

exercise_8([3,5,9,7]) gives: 3 
 Should be 3

exercise_8([4,9,2,4,7,2,5,10,12,100,3,12]) gives: 62 
 Should be 62

Exercise 9 - Factor Sum#

Here someone has written a function that takes an integer A (for example 24). It then computes the prime factors (Google it) of A. For 24, that is (2,2,2,3) because 2*2*2*3=24 and that’s how to express 24 in terms of only prime numbers. Then take the sum of these factors 2+2+2+3 = 9. Then repeat the process until you get a prime number or until you get to a point where the number isn’t changing. So 9=3*3 -> 3+3 = 6 -> 6=3*2 -> 3+2=5 -> 5=5. And as 5 is a prime number we stop and return 5.

As a second example let A=4. Then 4=2*2 -> 2+2=4. As the number didn’t change (we started with 4 and came back to 4), we return 4.

… Except the code that was written to do this does not return what we expect. See if you can debug it and fix the code

# ORIGINAL CODE
def exercise_9(n):

    prevValue = 0
    currentValue = 0
    nextValue = n

    while nextValue != prevValue:
        divisor = 2
        currentValue = nextValue
        prevValue = nextValue
        nextValue = 0
        while divisor * divisor <= currentValue:
            if currentValue % divisor == 0:
                currentValue //= divisor # FIXED: Better for this to be //= not /=
                nextValue += divisor
            else:
                divisor += 1
        nextValue += currentValue # FIXED: This should be currentValue not divisor

    return nextValue

print("exercise_9(24) gives:",exercise_9(24), "\n Should be 5")
print()
print("exercise_9(35) gives:",exercise_9(35), "\n Should be 7")
print()
print("exercise_9(156) gives:",exercise_9(156), "\n Should be 5")
print()
print("exercise_9(4) gives:",exercise_9(4), "\n Should be 4")
print()
print("exercise_9(31) gives:",exercise_9(31), "\n Should be 31")
print()
print("exercise_9(39) gives:",exercise_9(39), "\n Should be 5")
print()
print("exercise_9(39) gives:",exercise_9(39), "\n What should it give?")
print("39 = 3*13; 3+13 = 16 = 2*2*2*2; 2+2+2+2 = 8 = 2*2*2; 2+2+2=6=2*3; 2+3=5=5 => Should give 5")
exercise_9(24) gives: 5 
 Should be 5

exercise_9(35) gives: 7 
 Should be 7

exercise_9(156) gives: 5 
 Should be 5

exercise_9(4) gives: 4 
 Should be 4

exercise_9(31) gives: 31 
 Should be 31

exercise_9(39) gives: 5 
 Should be 5

exercise_9(39) gives: 5 
 What should it give?
39 = 3*13; 3+13 = 16 = 2*2*2*2; 2+2+2+2 = 8 = 2*2*2; 2+2+2=6=2*3; 2+3=5=5 => Should give 5