ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
words = ['b', 'a', 'c'] pairs = list(zip(sorted(words), range(10, 13))) print(pairs, dict(pairs)['c'], list(zip(*pairs))[0])
- A. [('a', 10), ('b', 11), ('c', 12)] 12 ('c', 'b', 'a')
- B. [('a', 10), ('b', 11), ('c', 12)] 12 ('a', 'b', 'c')
- C. [('a', 10), ('b', 11), ('c', 12)] 10 ('a', 'b', 'c')
- D. [('b', 10), ('a', 11), ('c', 12)] 12 ('a', 'b', 'c')
Correct answer: B. [('a', 10), ('b', 11), ('c', 12)] 12 ('a', 'b', 'c')
sorted(words) is ['a', 'b', 'c'], and zip pairs each letter with 10, 11, 12. dict(pairs)['c'] is 12. Transposing the pairs with zip(*pairs) gives two tuples, and the first one is the letters.
More Python Programming questions
- What is printed? python s = 'aabbbc' out = [] i = 0 while i < len(s): j = i while j < len(s) and s[j] == s[i]: j += 1 out.append(s[i] +…
- What is printed? python import math print(math.comb(10, 3) % 7, math.perm(5, 2), math.factorial(5) // math.factorial(3))
- What is printed? python x = [1, 2, 3] it = iter(x) print(next(it), sum(it), list(it))
- What is printed? python def tri(n): return n (n + 1) // 2 print(tri(10), tri(0), [tri(k) for k in range(4)])
- What is printed? python def f(xs): xs.sort() return xs data = [3, 1, 2] res = f(data) print(data is res, data)
- Which Python construct is the most efficient way to test whether a sorted list a contains x in O(log n) time without building extra…