ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from functools import total_ordering
@total_ordering
class V:
def __init__(self, n):
self.n = n
def __eq__(self, o):
return self.n == o.n
def __lt__(self, o):
return self.n < o.n
print(V(2) >= V(1), V(1) > V(1), max(V(3), V(7)).n, sorted([V(5), V(2)])[0].n)- A. False False 7 2
- B. True True 7 2
- C. True False 3 2
- D. True False 7 2
Correct answer: D. True False 7 2
total_ordering builds >= from < and ==, so 2 >= 1 is True. 1 > 1 is False, and max and sorted use the same ordering.
More Python Programming questions
- What is printed? python def make(): memo = {} def fib(n): if n in memo: return memo[n] memo[n] = n if n < 2 else fib(n - 1) + fib(n - 2)…
- What is printed? python words = 'the cat and the dog and the end'.split() cnt = {} for w in words: cnt[w] = cnt.get(w, 0) + 1 top =…
- What is printed? python def merge(iv): iv.sort() out = [iv[0]] for s, e in iv[1:]: if s <= out[-1][1]: out[-1][1] = max(out[-1][1], e)…
- What is printed? python import heapq def dijk(g, s): d = {s: 0} h = [(0, s)] while h: du, u = heapq.heappop(h) if du > d[u]: continue for…
- What is printed? python from collections import deque def topo(n, edges): indeg = [0] n adj = [[] for in range(n)] for u, v in edges…
- What is printed? python dp = [0] 16 for i in range(1, 16): dp[i] = dp[i >> 1] + (i & 1) print(dp[13], sum(dp))