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

Geoff Canyon gcanyon at gmail.com
Sun May 10 11:12:56 EDT 2015


On Sat, May 9, 2015 at 7:01 PM, Geoff Canyon <gcanyon at gmail.com> wrote:

> Yeah, I'm now trying to salvage my padding solution, which is better than
> the padding solutions he gave on the site, but still wrong.


Okay, I think this works. It pads with the first character, which covers
most cases, and then depends on the fact that when combining numbers like
4344 and 43, or 2252 and 225 (which would end up in the same bucket when
padded) if the first digit that is not the first digit of the number (i.e.
the 3 in the first pair and the 5 in the second pair) is greater than the
first digit, then the numbers should be ordered from shortest to longest,
while if it is less, the numbers should be ordered from longest to
shortest. In the examples, the correct orders are:

4344,43
225,2252


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
      if the number of lines of C[K] > 1 then
         put line 1 of C[K] into orderCheck
         put char 1 of orderCheck into B
         repeat with i = 2 to length(orderCheck)
            if char i of orderCheck = B then next repeat
            if char i of orderCheck > B then sort lines of C[K] by
length(each)
            if char i of orderCheck < B then sort lines of C[K] descending
by length(each)
            exit repeat
         end repeat
      end if
      put C[K] after R
   end repeat
   replace cr with empty in R
   return R
end maxValue


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

Test Output
26262
26262
43443
43443
664243
664241
666164243
56550
42423420
434434434343
434434434343



More information about the use-livecode mailing list