#C14729. Sort Tuples by Second Element
Sort Tuples by Second Element
Sort Tuples by Second Element
You are given a list of tuples containing two elements. The first element is an integer and the second element is intended to be numeric (an integer or a float). Your task is to sort the list in ascending order based on the second element. If any tuple has a non‐numeric second element, output an error message in the format:
where X
is the offending element.
Note: The input is read from standard input and the result is printed to standard output. When converting numbers, if the number is an integer, it should be printed without a decimal point (for example, print 1
rather than 1.0
).
inputFormat
The first line contains an integer n — the number of tuples. The following n lines each contain two values separated by space. The first value is an integer and the second value is supposed to be numeric (either an integer or a float). If the second value is not numeric, your program should output an error message.
outputFormat
If all tuples contain numeric second elements, print the sorted list in Python list format. Each tuple should be represented as (a, b)
, where a
is the first element and b
is the second element. If a non‐numeric second element is found, print the error message exactly as described.
3
1 3
2 1
3 2
[(2, 1), (3, 2), (1, 3)]