ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
s = "a1b22c333"
digits = "".join(ch for ch in s if ch.isdigit())
letters = [ch for ch in s if ch.isalpha()]
print(digits, sum(map(int, digits)), letters, s.translate(str.maketrans("abc", "xyz", "2")))- A. 122333 122333 ['a', 'b', 'c'] x1yz333
- B. 122333 14 ['a', 'b', 'c'] x1y22z333
- C. 122333 14 ['a', 'b', 'c'] x1yz333
- D. 123 6 ['a', 'b', 'c'] x1yz333
Correct answer: C. 122333 14 ['a', 'b', 'c'] x1yz333
Digit characters are kept in order: '122333', whose digit sum is 1+2+2+3+3+3 = 14. maketrans's third argument lists characters to delete, so the 2s vanish while a, b, c become x, y, z.
More Python Programming questions
- What is printed? python def f(n, memo={}): if n in memo: return memo[n] r = n if n < 2 else f(n - 1) + f(n - 2) memo[n] = r return r f(10)…
- What is printed? python def f(): x = 1 def g(): return x x = 2 return g def h(): for x in [1, 2]: pass return lambda: x print(f()(), h()())
- What is printed? python class Node: def init(self, v, nxt=None): self.v, self.nxt = v, nxt def iter(self): n = self while n: yield n.v n =…
- What is printed? python import heapq h = [5, 1, 4] heapq.heapify(h) heapq.heappush(h, 0) small = heapq.heappop(h) print(small, h[0]…
- What is printed? python from collections import deque d = deque([1, 2, 3], maxlen=3) d.append(4) d.appendleft(0) d.rotate(1)…
- What is printed? python from collections import namedtuple P = namedtuple("P", "x y", defaults=[0]) p = P(1) q = p.replace(y=5) print(p…