ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
words = ['tea', 'eat', 'tan', 'ate', 'nat']
groups = {}
for w in words:
groups.setdefault(''.join(sorted(w)), []).append(w)
print(len(groups), groups['aet'])- A. 2 ['tea', 'eat', 'ate']
- B. 3 ['tea', 'eat', 'ate']
- C. 2 ['tea', 'eat']
- D. 2 ['eat', 'tea', 'ate']
Correct answer: A. 2 ['tea', 'eat', 'ate']
Anagrams share the same sorted key. tea, eat and ate share the key aet, and tan and nat share ant, so there are 2 groups. The aet group keeps the input order.
More Python Programming questions
- What is printed? python print(0.2 + 0.4 + 0.1 == 0.7, 0.1 + (0.2 + 0.4) == 0.7, 0.2 + 0.1 + 0.4 == 0.7)
- What is printed? python print(sorted([(1, 'b'), (1, 'a'), (0, 'z')], reverse=True)[0])
- What is printed? python print(list(map(len, ['', 'ab', 'abc'])), sum(map(len, ['', 'ab', 'abc'])))
- What is printed? python print(10 in range(0, 20, 5), 11 in range(0, 20, 5))
- What is printed? python print(sum(range(1, 101)) 2 - sum(i i for i in range(1, 101)))
- What is printed? python print(len(str(264)), sum(map(int, str(264))))