r/programming • • 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

1.3k comments sorted by

View all comments

Show parent comments

-2

u/Paradox May 09 '15 edited May 09 '15

Just convert to strings, use your language's comparable, invert the comparison (i.e. 1, which would normally indicate greater-than, would indicate less than), and then cat the results together.

In ruby you can do it in a single line:

[50, 2, 1, 9].sort { |a, b| b.to_s <=> a.to_s }.join.to_i

Edit: nevermind, I was informed this doesnt work, as I forgot to pad the string. :(

2

u/[deleted] May 09 '15

Your solution is wrong, sorry. Try running it with [21,2], it will output 212 instead of 221.

2

u/Paradox May 09 '15 edited May 09 '15

Well shit, guess I get egg on my face too. I forgot to pad it.