ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import itertools print(len(list(itertools.combinations_with_replacement(range(5), 3))))
- A. 125
- B. 35
- C. 60
- D. 10
Correct answer: B. 35
combinations_with_replacement of n items taken r at a time has C(n + r - 1, r) results. Here C(7, 3) = 35. Plain combinations would give C(5, 3) = 10, and product would give 125.
More Python Programming questions
- What is printed? python import itertools print(sum(1 for t in itertools.product('01', repeat=10) if t.count('1') == 3))
- What is printed? python import itertools g = itertools.groupby('aab') k, grp = next(g) next(g) print(k, list(grp))
- What is printed? python import itertools g = iter(range(10)) print(list(itertools.islice(g, 2, 7, 2)), next(g))
- What is printed? python import itertools print(list(itertools.ziplongest('ab', 'xyz', fillvalue='-')))
- What is printed? python import itertools print(sum(a < b for a, b in itertools.pairwise([1, 3, 2, 5, 4, 6])))
- What is printed? python import itertools print(list(itertools.batched(range(7), 3))[-1])