ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
def egcd(a, b):
if b == 0:
return a, 1, 0
g, x, y = egcd(b, a % b)
return g, y, x - (a // b) * y
g, x, y = egcd(240, 46)
print(g, 240 * x + 46 * y)- A. 2 0
- B. 2 2
- C. 46 2
- D. 1 2
Correct answer: B. 2 2
The recursive extended Euclidean algorithm returns g together with x and y such that 240x + 46y = g. Here g is 2, and the combination also equals 2.
More Python Programming questions
- What is printed? python def canjump(a): far = 0 for i, x in enumerate(a): if i > far: return False far = max(far, i + x) return True…
- What is printed? python import copy a = [[1], [2]] b = a[:] c = copy.deepcopy(a) a[0].append(9) print(b, c)
- What is printed? python def f(x, acc=[]): acc.append(x) return acc f(1) f(2) print(f(3, []), f(4))
- What is printed? python def gridpaths(r, c, memo={}): if r == 0 or c == 0: return 1 if (r, c) not in memo: memo[(r, c)] = gridpaths(r - 1…
- What is printed? python def most2(s): cnt = {} l = best = 0 for r, ch in enumerate(s): cnt[ch] = cnt.get(ch, 0) + 1 while len(cnt) > 2…
- What is printed? python class Bag: items = [] def add(self, x): self.items.append(x) a, b = Bag(), Bag() a.add(1) b.add(2) print(a.items…