ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
trie = {}
for w in ['car', 'cat', 'dog']:
node = trie
for ch in w:
node = node.setdefault(ch, {})
node['#'] = True
print(sorted(trie), sorted(trie['c']), len(trie['c']['a']))- A. ['c', 'd', 'x'] ['a'] 2
- B. ['car', 'dog'] ['a'] 2
- C. ['c', 'd'] ['a'] 2
- D. ['c', 'd'] ['a'] 3
Correct answer: C. ['c', 'd'] ['a'] 2
The root has two child keys, 'c' and 'd'. 'c' leads to 'a', and the node for 'ca' has the children 'r' and 't', so its length is 2.
More Python Programming questions
- What is printed? python h = 0 for ch in 'abc': h = (h 31 + ord(ch)) % 1000 print(h)
- What is printed? python grid = ['a..', '.a.', '...'] cnt = sum(row.count('.') for row in grid) diag = ''.join(grid[i][i] for i in…
- What is printed? python from collections import Counter c = Counter('abracadabra') print(sum(v for v in c.values() if v % 2), len(c)…
- What is printed? python x = 0b1010 y = 0o17 z = 0xFF print(x ^ y, x | z, z >> 4, y & ~x)
- What is printed? python a = 'Python' print(a[::-1][1:4], a[-2:] + a[:2], a.upper()[::2])
- What is printed? python n = 10 6 s = 0 i = 1 while i <= n: s += n // i i = 10 print(s)