#K77702. Sort Mixed Numbers
Sort Mixed Numbers
Sort Mixed Numbers
You are given a list of elements which may be either integers or strings representing numbers. The valid strings are defined as follows:
\( \text{one} = 1, \; \text{two} = 2, \( \text{three} = 3, \; \text{four} = 4, \text{five} = 5, \text{six} = 6, \text{seven} = 7, \text{eight} = 8, \text{nine} = 9, \text{ten} = 10 \)
Your task is to sort the list in ascending order based on the numerical value of each element. When comparing elements, convert any valid string to its equivalent integer value. The output must preserve the original representation of each element (i.e. if it was a word, output the word; if it was an integer, output the integer).
If any string in the list is not one of the valid words, the program should output KeyError
(without quotes) and not perform any sorting.
Input/Output Method: Your solution must read from stdin
and print to stdout
.
inputFormat
The input consists of two lines:
- The first line contains a single integer \( n \) representing the number of elements.
- The second line contains \( n \) space-separated tokens. Each token is either an integer or a lowercase English word representing a number (from "one" to "ten").
outputFormat
If all tokens are valid, output the sorted list as a single line of space-separated tokens, preserving the original representation.
If any token is an invalid word, output KeyError
.
3
5 2 9
2 5 9