ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
def hanoi(n, a, b, c, out):
    if n == 0:
        return
    hanoi(n - 1, a, c, b, out)
    out.append((a, c))
    hanoi(n - 1, b, a, c, out)
moves = []
hanoi(3, 'A', 'B', 'C', moves)
print(len(moves), moves[0], moves[3])
  1. A. 7 ('A', 'B') ('A', 'C')
  2. B. 6 ('A', 'B') ('A', 'C')
  3. C. 7 ('A', 'C') ('A', 'C')
  4. D. 7 ('A', 'B') ('B', 'C')

Correct answer: C. 7 ('A', 'C') ('A', 'C')

The first move takes the smallest disk from A to C, and the fourth move takes the largest disk from A to C. Three disks need 2^3 - 1 = 7 moves.

More Python Programming questions

Exams that ask this topic