Sort by Digit Count
EasyPractice library · +10 XPsortingcounting sortcustom order
Given a list of N integers, sort them primarily by the number of digits each integer has (ascending). If two integers have the same number of digits, sort them by their value (ascending). Print the sorted list.
Input
The first line contains a single integer N. The second line contains N space-separated integers.
Output
Print the N space-separated integers in the specified sorted order.
Constraints
1 <= N <= 1000. Each integer will be between -10^9 and 10^9.
Time limit 1 s (native code; slower languages get more)Memory 256 MB15 hidden tests
Example 1
Input
5 10 3 -12 100 0
Output
0 3 -12 10 100
Sorted by digit count (1, 2, 2, 3, 3), then value: 0, 3, -12, 10, 100
Example 2
Input
4 55 7 99 10
Output
7 10 55 99
Sorted by digit count (1, 1, 2, 2), then value: 7, 55, 10, 99
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 10 3 -12 100 0
Example 2 input
4 55 7 99 10