ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
words = 'the quick the lazy'.split()
counts = {}
for w in words:
counts[w] = counts.get(w, 0) + 1
print(counts, max(counts, key=counts.get))- A. {'quick': 1, 'the': 2, 'lazy': 1} the
- B. {'the': 2, 'quick': 1, 'lazy': 1} the
- C. {'the': 2, 'quick': 1, 'lazy': 1} quick
- D. {'the': 2, 'lazy': 1, 'quick': 1} the
Correct answer: B. {'the': 2, 'quick': 1, 'lazy': 1} the
A dict keeps keys in the order they were first inserted, so the, quick and lazy come in that order. max with key=counts.get returns the key with the largest count, which is the.
More Python Programming questions
- What is printed? python print(sorted(['B', 'a', 'C'], key=str.casefold) == ['a', 'B', 'C'], sorted(['B', 'a', 'C']) == ['B', 'C', 'a']…
- What is printed? python print(int('1000') + int('01'), '1000'.replace('', '') == '1000', float('10.50'), '%.1f' % float('10.50'))
- What is printed? python print(round(-1234.5, -2), round(123456.0, -3), round(0.0, 2), round(1e-7, 7))
- What is printed? python print(str(1.0), str(1), str(1.0e0), str(10.0 0), repr(1e0))
- What is printed? python print(7 & ~1, 7 >> 1 << 1, (7 | 8) & 12, 6 ^ 3 ^ 3)
- What is printed? python print(sorted([(-1, 'a'), (1, 'b'), (-1, 'c')], key=lambda t: t[0], reverse=True))