def sort_by_digit_count(items): Sorting can be done according to arbitrarily chosen comparison criteria, as long as they satisfy the mathematical requirements of a total ordering relation. To play around with this concept, let us define a wacky ordering comparison of positive integers so that for any two integers, the one that contains the digit 9 more times than the other is considered to be larger, regardless of the magnitude and other digits of these numbers. For example, 99 > 12345678987654321 > 101000 in this ordering. If both integers contain the digit 9 the same number of times, the comparison proceeds to the next lower digit 8, and so on, until the first distinguishing digit has been discovered. If both integers contain every digit from 9 to 0 pairwise the same number of times, the ordinary integer order comparison will determine their mutual ordering.
def sort_by_digit_count(items):
Sorting can be done according to arbitrarily chosen comparison criteria, as long as they satisfy the mathematical requirements of a total ordering relation. To play around with this concept, let us define a wacky ordering comparison of positive integers so that for any two integers, the one that
contains the digit 9 more times than the other is considered to be larger, regardless of the magnitude and other digits of these numbers. For example, 99 > 12345678987654321 > 101000 in this ordering. If both integers contain the digit 9 the same number of times, the comparison
proceeds to the next lower digit 8, and so on, until the first distinguishing digit has been discovered. If both integers contain every digit from 9 to 0 pairwise the same number of times, the ordinary integer order comparison will determine their mutual ordering.
items | expected results |
[9876, 19, 4321, 99, 73, 241, 111111, 563, 33] |
[111111, 33, 241, 4321, 563, 73, 19, 9876, 99] |
[111, 19, 919, 1199, 911, 999] | [111, 19, 911, 919, 1199, 999] |
[1234, 4321, 3214, 2413] | [1234, 2413, 3214, 4321] |
list(range(100000)) | (a list of 100,000 elements whose first five elements are [0, 1, 10, 100, 1000] and the last five are [98999, 99899, 99989, 99998, 99999] ) |
please add steps and notes between codes so i understand
Step by step
Solved in 2 steps with 2 images