r/programming • u/SilasX • May 09 '15
"Real programmers can do these problems easily"; author posts invalid solution to #4
https://blog.svpino.com/2015/05/08/solution-to-problem-4
3.1k
Upvotes
r/programming • u/SilasX • May 09 '15
2
u/Kaligraphic May 09 '15 edited May 09 '15
I did it a lazy way. I copied the numbers into a second array, but converted to floats. For each value, while that value is >10, I divided it by 10. That gave me single-digit numbers that I could sort.
Then I sorted the copy, but with a twist - I did the comparisons on the second array, but I did the moves on both arrays. This effectively sorted both arrays by the highest first digit. (I just did a bubble sort for proof of concept and because I'm a lazy git who normally uses Array.Sort() or equivalent, but a quicksort or better is fine too.)
then I spit out the string representations of every value in the first array, in order.
Then I realized that 9.1 sorts before 9, when I should put the 9 first. At this point, I realize that even if I had used a padding trick, 910 sorts before 900, so it would have been the same result, and I ended up concluding that brute force may actually be the only workable option.