Five programming problems every Software Engineer should be able to solve in less than 1 hour

Geoff Canyon gcanyon at gmail.com
Sat May 9 19:49:33 EDT 2015


Problem 4

Write a function that given a list of non negative integers, arranges them
such that they form the largest possible number. For example, given [50, 2,
1, 9], the largest formed number is 95021.


Again, not enough test cases, and in this case his initial solution failed
when tested more rigorously. If this were a discussion at my work, this is
the point where someone would break out the sound from The Price is Right
when someone screws up (wa, wa, wa, Waaaaaaa<sad note>).

Interestingly, the solution he chose -- padding -- is what I chose as well.
He bailed on it once people pointed out cases that fail. My implementation
stands up so far, but I wouldn't bet my left thumb it can't be broken. The
difference is that I pad with the first digit (not the last, not 0). So in
the final example, 42 gets ranked as 424 and thus comes before 423.

However, (this conversation just happened) another test case makes it fail.
So this is still a work in progress...

function maxValue L
   put 0 into ml
   repeat for each item i in L
      if length(i) > ml then put length(i) into ml
   end repeat
   repeat with i = 0 to 9
      repeat ml
         put i after d[i]
      end repeat
   end repeat
   repeat for each item i in L
      put i & cr after C[char 1 to ml of (i & d[char 1 of i])]
   end repeat
   put keys of C into kList
   sort kList descending numeric
   repeat for each line K in kList
      put C[K] after R
   end repeat
   replace cr with empty in R
   return R
end maxValue


Test Data
642,6,4,3
642,6,4,1
642,6,661,4,3
5,50,56
420,42,423
262,26
26,262

Test Results
664243
664241
666164243
56550
42423420
26226  -- fail
26262



More information about the use-livecode mailing list