r/programming • u/svpino • May 08 '15
Five programming problems every Software Engineer should be able to solve in less than 1 hour
https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
2.5k
Upvotes
2
u/psymunn May 08 '15 edited May 08 '15
That's a smart observation, and one I believe is correct. First, we can make some observations:
The operation is transitive. You can sort you numbers in any order and end with the correct result.
5 and 55 are a tie. Their order doesn't matter, which is indeed true. An unbroken string of numbers can be considered equal. your sort preserves this.
9 or a string of 9s will never be lower than another number. 1 or a string of 1s will never be higher.
For a digit starting with 9, we can always sort using the second digit, if there is one. If there's not the 9 is the higher number. So, 9 beats a 9 followed by anthything, and a 97 will beat a 96 but lose to a 98 regardless of following digits. For 1, this is the opposite.
Okay. What about numbers in the middle. Well it starts to emerge that, when the first digit ties, you go to the second digit. If the second digit is greater than the previous digit, the number is considered higher. What about a number missing a digit. Well lets look:
5, 56, 54
It becomes obvious pretty quickly, that 56 beats 5, but 54 loses to it. This will incidentally sort exactly the same way as 55, 56, 54. You can effectively pad your number by
adding as many copies of the last digit as needed. 565 will be lower than 56, because 56 will sort the same as 566.repeating the string of numbers over and over. 56 loses to 566, and beats 564 because it's the equivalent of 565.Your sorting method implicitly obeys all these rules, and rather succinctly!
EDIT: Ugh, I was wrong about the padding. luciusplc has data that shows that that is incorrect. I missorted: 45456, 45, 45452, because I treated 45 as 45555. Treating it as 45454 makes the sorting more obvious. You pad a number by repeating it. so 123 padded to 8 digits will be 12312312.