ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
import itertools
print(list(itertools.accumulate([1, 2, 3, 4])), list(itertools.accumulate('ab')), sum(itertools.islice(itertools.count(5), 3)))- A. [1, 3, 6, 10] ['a', 'b'] 18
- B. [1, 2, 3, 4] ['a', 'ab'] 18
- C. [1, 3, 6, 10] ['a', 'ab'] 18
- D. [1, 3, 6, 10] ['a', 'ab'] 15
Correct answer: C. [1, 3, 6, 10] ['a', 'ab'] 18
accumulate gives running totals, so strings concatenate as well. count starting at 5 supplies 5, 6 and 7 for islice, which sum to 18.
More Python Programming questions
- What is printed? python print(len([x for x in range(1, 100) if x % 3 == 0 or x % 5 == 0]), sum(x for x in range(1, 100) if x % 3 == 0 or x…
- What is printed? python print(bin(10)[::-1], int(''.join(reversed(bin(10)[2:])), 2), format(10, 'b').zfill(6))
- What is printed? python from collections import Counter c = Counter(a=1) c.subtract({'a': 3, 'b': 1}) print(c, c['a'] + c['b'])
- What is printed? python print(sorted([(2, 'b'), (1, 'z'), (3, 'a')], reverse=True))
- What is printed? python print(3 in {1, 2, 3} and 'x' in 'xyz', [] or () or None)
- What is printed? python def flat(x): for i in x: if isinstance(i, list): yield from flat(i) else: yield i print(list(flat([1, [2, [3, 4]]…