ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
words = ['hi', 'there', 'hey'] print(max(words), min(words, key=len), sorted(words, key=lambda w: (len(w), w)))
- A. there hi ['hey', 'hi', 'there']
- B. hi hi ['hi', 'hey', 'there']
- C. there hey ['hi', 'hey', 'there']
- D. there hi ['hi', 'hey', 'there']
Correct answer: D. there hi ['hi', 'hey', 'there']
max compares strings lexicographically, so 'there' is largest. min by length picks the first shortest word, 'hi'. The sort key is (length, word), which puts 'hi' before 'hey' and then 'there'.
More Python Programming questions
- What is printed? python print(pow(0, 0), 0 0 == 1, pow(2, -2, 5), pow(0, 0, 5))
- What is printed? python import math print(0.0 == -0.0, 1 / float('-inf'), 2 -1074 / 2 == 0, math.copysign(0.0, -1))
- What is printed? python try: print(int('')) except ValueError: print('empty') try: print(0.0 / 0) except ZeroDivisionError: print('zero')
- What is printed? python import math try: print(math.sqrt(-1)) except ValueError: print('domain') print(float('nan') 0, math.pow(0, 0))
- What is printed? python print('{:e}'.format(1234.5), '{:.3g}'.format(1234567), '{:g}'.format(0.0001), '{:g}'.format(0.00001))
- What is printed? python print('é'.encode('ascii', 'replace'), 'é'.encode('ascii', 'ignore'), b'hxc3xa9llo'.decode('ascii', 'ignore')…