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
1
u/code-master May 09 '15 edited May 09 '15
One of the solutions avoiding string concatenation:
When you've got number with 5 digits 15111 and number 1 [15111 1] you must append 4 missing digits from first number at the end of second number taking them from left to right, so e.g. : 15111 would be compared to 11511 (1xxxx) and then its decided to switch their positions or not.
eg. [50 507] becomes [50x 507] -> [505 507] and because 507 is greater than 505, 507 is placed at the front, so result is 50750
lets test it against some input:
[10 5] -> [10 5x] -> [10 51] (swap)-> 5,10
[51 5] -> [51 5x] -> [51 55] (swap) -> 5,51
[56 5] -> [56 5x] -> [56 55] (no swap) -> 56,5
[55 5] -> [55 5x] -> [55 55] (no swap) -> 55, 5
[95 9] -> [95 9x] -> [95 99] (swap) -> 9,95
[15 1] -> [15 1x] -> [15 11] (no swap) -> 15,1
[11561 1] -> [1151 1xxxx] -> [11561 11156] (no swap)
[9998 9] -> [9998 9xxx] -> [9998 9999] (swap) ...
I don't know if this method is more efficient than plain string concatenation, but it works. Something tells me it is. If digits were converted to BCD (binary coded decimal) it would be easier to do comparisons. Or just convert to readable hex, e.g. 999 -> 0x999 and do the math.
I think nobody came to such original solution like this, so from the whole crowd here I would hire only myself.
Still, this is not a real-life programming problem.