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:41:14 EDT 2015
> I thought briefly about doing this but it didn't gel in my mind as
> quickly
> as the substitution method I used. I quite like how this turned out!
I realized after posting my versions that my version of problem5 could
be even more succinct code-wise:
function problem5
local tResults
repeat with i = 0 to 3^8
local tScheme, tSum
put empty into tSum
put format("%08s", baseConvert(i, 10, 3)) & "0" into tScheme
repeat with j = 1 to 9
put j after tSum
put item (char j of tScheme + 1) of ",+,-" after tSum
end repeat
if value(tSum) is 100 then
put tSum & return after tResults
end if
end repeat
return tResults
end problem5
Although it is perhaps a little more opaque.
It's an interesting problem - the 'enumerate all permutations and then
evaluate' approach (which I think is essential what both your's and mine
are) certainly works in this smaller case as there are only 3^8
permutations.
Generally though I suspect some element of dynamic programming (i.e.
caching the value of subsequences) and recursion would be more tractable
if the number of digits was greater.
--
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps
More information about the use-livecode
mailing list