ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from itertools import product
print(list(product('ab', repeat=2)), list(product([0, 1], 'x')))- A. [('aa'), ('ab'), ('ba'), ('bb')] [(0, 'x'), (1, 'x')]
- B. [('a', 'a'), ('a', 'b'), ('b', 'a'), ('b', 'b')] [(0, 'x'), (1, 'x')]
- C. [('a', 'a'), ('b', 'a'), ('a', 'b'), ('b', 'b')] [(0, 'x'), (1, 'x')]
- D. [('a', 'a'), ('a', 'b'), ('b', 'a'), ('b', 'b')] [(0, 'x')]
Correct answer: B. [('a', 'a'), ('a', 'b'), ('b', 'a'), ('b', 'b')] [(0, 'x'), (1, 'x')]
product is the nested loop in order: the rightmost position changes fastest. Each argument gives its items in order, so repeat=2 over 'ab' gives aa, ab, ba, bb as tuples.
More Python Programming questions
- What is printed? python d = {'pen': 3, 'cap': 5, 'ink': 3, 'box': 1} print(sorted(d.items(), key=lambda kv: (-kv[1], kv[0]))[:3])
- What is printed? python from collections import deque n = 4 edges = [(0, 2), (1, 2), (2, 3), (0, 3)] indeg = [0] n adj = [[] for in…
- What is printed? python def cnt(xs, T): xs = sorted(xs) lo, hi, total = 0, len(xs) - 1, 0 while lo < hi: if xs[lo] + xs[hi] <= T: total +=…
- What is printed? python def knap(items, cap): dp = [0] (cap + 1) for w, v in items: for c in range(cap, w - 1, -1): dp[c] = max(dp[c]…
- What is printed? python import heapq h = [] for x in [4, 9, 1, 7]: heapq.heappush(h, -x) print(-heapq.heappop(h), -h[0])
- What is printed? python from functools import partial def power(base, exp): return base exp sq = partial(power, exp=2) cubeofthree =…