r/programming • • 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.1k comments sorted by

View all comments

Show parent comments

2

u/jcdyer3 May 08 '15

Try that with 56, 5, 54

1

u/DerJawsh May 08 '15

I think that can be solved by making it so we only take the shorter number if the next digit of the larger number is smaller than the first digit of the shorter number.

2

u/jcdyer3 May 08 '15

Alternatively, to keep the sorting logic simpler, I think you could sort each number with digits ABC..N by the key ABC..NA.

In python:

ints = sorted(ints, key=lambda x: str(x) + str(x)[0])

1

u/[deleted] May 08 '15

What if you wrote a comparison function where:

A > B if 
    the ith digit of A is greater than the ith digit of B
    OR
    A does not have an ith digit and its i-1th digit is >= the ith digit of of B

Then sort using that?