ProgrammingModerateTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import itertools
print([''.join(p) for p in itertools.permutations('abc', 2)][:4])- A. ['ab', 'ac', 'ba', 'bc']
- B. ['ab', 'ba', 'ac', 'ca']
- C. ['ab', 'ac', 'bc', 'ba']
- D. ['ab', 'bc', 'ac', 'ba']
Correct answer: A. ['ab', 'ac', 'ba', 'bc']
permutations(iterable, r) emits tuples in lexicographic order of the input positions. The first prefix is 'a' with each later letter, so ab then ac, then the prefix 'b' gives ba and bc.
More Python Programming questions
- What is printed? python import itertools print(len(list(itertools.combinationswithreplacement(range(5), 3))))
- 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])))