Comprehensions and functional programming · 11 / 15 Exercise Medium

Sorting with key

Objective

Create a Python program that sorts the words of a sentence in two different ways using the key parameter of sorted.

Instructions

  • Read a line with words separated by spaces.
  • On the first line show the words sorted from shortest to longest (key=len).
  • On the second line show the words sorted alphabetically by their last letter (key=lambda w: w[-1]).
  • When two words tie, the order in which they appeared is kept (Python sorting is stable).
  • Separate the words with a space.
Input
sol cielo mar luna
Output
sol mar luna cielo
luna sol cielo mar

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise