ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print(sorted(['b10', 'b9', 'a100'], key=lambda s: (s[0], int(s[1:]))))
- A. ['b9', 'b10', 'a100']
- B. ['b10', 'b9', 'a100']
- C. ['a100', 'b9', 'b10']
- D. ['a100', 'b10', 'b9']
Correct answer: C. ['a100', 'b9', 'b10']
The key sorts by the letter first and then by the number as an int, so b9 comes before b10. Sorting the raw strings would put b10 before b9.
More Python Programming questions
- What is printed? python print(round(3.14159, 3), round(12345, -2), round(0.5), round(1.5), round(2.5, 0))
- What is printed? python a = [3, 1, 2] b = sorted(a, reverse=True)[:2] c = sorted(a)[-2:] print(b, c, sum(b) + sum(c))
- What is printed? python word = 'Level' print(word.lower() == word.lower()[::-1], word == word[::-1], word.casefold() == 'level')
- What is printed? python d = {} for w in ['b', 'a', 'b']: d[w] = d.get(w, 0) + 1 print(d, max(d, key=d.get), sorted(d.values()…
- Which is the time complexity of checking whether a string s of length n is a palindrome, using s == s[::-1]?
- What is printed? python values = [0, 1, 2, 3] print([v for v in values if v] == values[1:], any(values), all(values[1:]))