ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
from collections import deque
def knight(src, dst):
seen = {src: 0}
q = deque([src])
moves = [(1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), (2, -1), (-1, -2), (-2, -1)]
while q:
r, c = q.popleft()
if (r, c) == dst:
return seen[(r, c)]
for dr, dc in moves:
nr, nc = r + dr, c + dc
if 0 <= nr < 8 and 0 <= nc < 8 and (nr, nc) not in seen:
seen[(nr, nc)] = seen[(r, c)] + 1
q.append((nr, nc))
print(knight((0, 0), (2, 1)), knight((0, 0), (7, 7)))- A. 2 6
- B. 1 6
- C. 1 7
- D. 2 7
Correct answer: B. 1 6
Breadth-first search over the eight knight moves gives the minimum number of moves. One move reaches (2, 1), and six moves are needed to reach the opposite corner (7, 7).
More Python Programming questions
- For an array of n = 10^5 values, which approach counts the distinct values in every window of size k = n / 2 in the least total time?
- What is printed? python a = [1, 2, 3, 4] try: print(a[len(a) / 2]) except TypeError: print('TypeError') print(a[len(a) // 2])
- What is printed? python s = 'u00b2' print(s.isdigit(), s.isdecimal(), s.isnumeric())
- What is printed? python d = {1: 'a'} d[1.0] = 'b' d[True] = 'c' print(d)
- What is printed? python def acc(): total = 0 while True: x = yield total if x is None: return total total += x g = acc() next(g) g.send(5)…
- What is printed? python class P: def init(self, x): self.x = x def eq(self, o): return self.x == o.x try: s = {P(1)} except TypeError…