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

Mark Waddingham mark at livecode.com
Sun May 10 11:34:07 EDT 2015


>> function problem4 pList
>>    local tLength
>>    put 0 into tLength
>>    repeat for each item tItem in pList
>>       put max(the number of chars in tItem, tLength) into tLength
>>    end repeat
>>    sort items of pList ascending numeric by the number of chars in 
>> each
>>    sort items of pList descending text by problem4_pad(each, tLength)
>>    replace comma with empty in pList
>>    return pList
>> end problem4
>> 
> 
> This fails on 43,434 -- it returns 43434, but should return 43443

Good catch. In which case, I wonder if there is a way to do it with two 
padded sorts rather than having a custom comparator which checks 
pairwise which of AB / BA is greater.

I was trying to find a way to do it using the sort primitives rather 
than having to write my own sort with a custom comparison routine. As it 
seemed slightly preferable: you have to prove that your comparator is 
transitive (A < B, B < C => A < C), which isn't entirely obvious in this 
instance I don't think.

-- 
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps




More information about the use-livecode mailing list