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 17:55:54 EDT 2015


Problem 1

Write three functions that compute the sum of the numbers in a given list
using a for-loop, a while-loop, and recursion.

Note that he doesn't provide any test cases, so for each problem I provided
my own in a field, and then called the functions for each line in the test
field, putting the output in another field. On to the functions:

function sumFunction X
  -- just because I could, not part of the original requirements
   return sum(X)
end sumFunction

function sumFor X
   repeat for each item i in X
      add i to R
   end repeat
   return R
end sumFor

function sumWhile X
   repeat while X is not empty
      add item 1 of X to R
      delete item 1 of X
   end repeat
   return R
end sumWhile

function sumRecurse X
   if the number of items of X <= 1
     then return X
     else return item 1 of X + sumRecurse(item 2 to -1 of X)
end sumRecurse

Test data (first line was blank on purpose):

5
2,3,5,6,2,3,1
-4,4
-4,4,-5,5

Test output (not entirely happy about the disparity in results for empty
input):
0
5 5 5 5
22 22 22 22
0 0 0 0
0 0 0 0



More information about the use-livecode mailing list