ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
people = [('ann', 25), ('bob', 30), ('cy', 25), ('dan', 30)]
people.sort(key=lambda p: p[0])
people.sort(key=lambda p: p[1], reverse=True)
print([p[0] for p in people])- A. ['bob', 'dan', 'cy', 'ann']
- B. ['bob', 'dan', 'ann', 'cy']
- C. ['dan', 'bob', 'cy', 'ann']
- D. ['ann', 'bob', 'cy', 'dan']
Correct answer: B. ['bob', 'dan', 'ann', 'cy']
The first sort orders names alphabetically. The second sort is stable, and with reverse=True equal ages keep the order from the first pass, so bob comes before dan and ann before cy.
More Python Programming questions
- What is printed? python print(-5 // 2 2 + -5 % 2 == -5, 5 // -2 -2 + 5 % -2 == 5)
- What is printed? python a = [1, 2, 3] a.append(a) print(len(a), a[-1][-1][0])
- What is printed? python x = 'a,b;c d' print(x.replace(',', ' ').replace(';', ' ').split(), x.replace(',', ' ').split(' '))
- What is printed? python import string print(string.asciilowercase.index('z') - string.asciiuppercase.index('A'), len(string.digits +…
- What is printed? python a = [5, 1, 4] b = a a = sorted(a) c = a.copy() c.reverse() print(a, b, c)
- What is printed? python def f(a, b=[]): b.append(a) return len(b) print(f(1), f(2, []), f(3))