ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
def spf(n):
s = list(range(n + 1))
for i in range(2, int(n ** 0.5) + 1):
if s[i] == i:
for j in range(i * i, n + 1, i):
if s[j] == j:
s[j] = i
return s
s = spf(30)
out = []
x = 28
while x > 1:
out.append(s[x])
x //= s[x]
print(out, s[29])- A. [2, 2, 7] 29
- B. [2, 7] 29
- C. [2, 2, 7] 2
- D. [2, 2, 7, 7] 29
Correct answer: A. [2, 2, 7] 29
The smallest-prime-factor table lets each step divide out one prime. 28 gives 2, then 14 gives 2, then 7, so the list is [2, 2, 7]. s[29] is 29 because 29 is prime.
More Python Programming questions
- What is printed? python def row(n): r = [1] for in range(n): r = [1] + [r[i] + r[i + 1] for i in range(len(r) - 1)] + [1] return r…
- What is printed? python def pairs(a, t): a = sorted(a) l, r = 0, len(a) - 1 out = [] while l < r: s = a[l] + a[r] if s == t…
- What is printed? python class N: def init(self, v, nxt=None): self.v, self.nxt = v, nxt def rev(h): prev = None while h: h.nxt, prev, h =…
- What is printed? python def cntpal(s): n = len(s) c = 0 for ctr in range(2 n - 1): l, r = ctr // 2, ctr // 2 + ctr % 2 while l >= 0 and r…
- What is printed? python import bisect a = [9, 7, 5, 3, 1] print(bisect.bisectleft(a, -6, key=lambda v: -v), bisect.bisectleft(a, 6…
- What is printed? python from collections import deque d = deque([1, 2, 3, 4, 5], maxlen=5) d.rotate(2) d.appendleft(0) print(list(d)…