Sort by Second Digit
Given a list of N integers, sort them in ascending order based on their second digit. If two numbers have the same second digit, maintain their relative order from the original list (stable sort). If a number has only one digit, consider its second digit to be 0.
Input
The first line contains an integer N (1 <= N <= 1000). The second line contains N space-separated integers, a_1, a_2, ..., a_N.
Output
Print the N integers sorted according to the rule, separated by spaces.
Constraints
1 <= N <= 1000 1 <= a_i <= 10^9
Example 1
5 123 45 6 789 10
6 10 123 45 789
Sorted by second digit: 0 (from 6, 10), 2 (from 123), 5 (from 45), 8 (from 789). Original relative order for 6 and 10 is preserved.
Example 2
4 99 88 77 66
66 77 88 99
All numbers have the same second digit (9, 8, 7, 6). The list is sorted based on these digits. The relative order of numbers with same second digit is maintained.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 123 45 6 789 10
4 99 88 77 66