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])- A. 7 ('A', 'B') ('A', 'C')
- B. 6 ('A', 'B') ('A', 'C')
- C. 7 ('A', 'C') ('A', 'C')
- 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
- What is printed? python from collections import deque def islands(g): R, C = len(g), len(g[0]) seen = set() cnt = 0 for r in range(R): for…
- What is printed? python import heapq def refuel(target, start, stations): h = [] i = 0 fuel = start stops = 0 while fuel < target: while i…
- What is printed? python import heapq def jobs(items): items = sorted(items, key=lambda x: x[0]) h = [] for d, p in items…
- What is printed? python try: print(sorted([3, None, 1])) except TypeError: print('TypeError') print(max(['b', 'a'], key=str.upper)…
- What is printed? python s = 'hello' try: s[0] = 'j' except TypeError: print('immutable') t = s.replace('h', 'j') print(t, s)
- What is printed? python print(1020 / 1019, 1020 // 1019, 264 % 1000, 1020 == 1e20)