Thanks again to everyone who submitted solutions to the second gambling puzzle. I am impressed at the amount of solutions I get to these problems, and also the variety of languages used. I promise I will fix the comment box for the next programming puzzle, (it'll respect whitespace and allow characters like > <).
Thanks again to everyone who submitted solutions to the second gambling puzzle. I am impressed at the amount of solutions I get to these problems, and also the variety of languages used. I promise I will fix the comment box for the next programming puzzle, (it'll respect whitespace and allow characters like > <).
Firstly , the correct answer is $5798, as was noted by several people. Here are the solutions in order of submission...
First in was this Erlang code by Jason. This is a pretty cool solution, well done Jason.
-module(gambler).
-compile(export_all).
-define(DATA
start() ->
lists:foldl(fun({Sum
{ASum+Sum
{0
split([]
split([H|T]
split([H|T]
look([]) -> {0
look([H|T]) when H =< 0 -> look(T);
look([H|T]) ->
{SumY
case look(tail(T)) of
{SumN
{SumN + H
_ ->
{SumY
end.
tail([]) -> [];
tail([_|T]) -> T.
Second was this Python snippet from Taylor...
#!/usr/bin/python
wins = [158
end = len(wins) - 1
def maxSeq(start):
if start == end:
return [wins[start]
elif start + 1 == end:
opt1 = [wins[start]
opt2 = [wins[end]
elif start + 2 == end:
opt1 = [wins[start]+wins[end]
opt2 = [wins[start+1]
else:
subseq1 = maxSeq(start+2)
subseq2 = maxSeq(start+3)
opt1 = [wins[start] + subseq1[0]
opt2 = [wins[start+1] + subseq2[0]
if opt1[0] > opt2[0]:
return opt1
return opt2
&nsbp;
sol = maxSeq(0)
print "Max:"
print sol[1]
Next up is some Haskell from Cale Gibbard
import Data.List
gamble = [158 ... 380]
gen [] = [[]]
gen [x] = [[x]] -- note we assume all elements are positive here
gen (x:y:xs) = gen (y:xs) ++ map (x:) (gen xs)
comparing p x y = compare (p x) (p y)
solve = maximumBy (comparing sum) . gen
splitBy p xs =
case break p xs of
(a,[]) -> [a]
(a,_:b) -> a : splitBy p b
main = do
let sol = splitBy (<= 0) gamble >>= solve
print sol
Andrew then chimed in with another Haskell solution, this one not relying on recursive calls.
-module(gambler).
-compile(export_all).
-define(DATA
start() -> {{Sum
{Sum
look([]) -> {{0
look([H|T]) -> {{Sum1
if Sum2 + H > Sum1 ->
{{Sum2 + H
true ->
{{Sum1
end.
Soren then provided 2 different versions of the solution in C, a recursive solution, and a non-recursive linear solution.
Aaron then provided a O(1) solution, that compiles in O(N), which was clever I thought 
Going back towards the short sweet and a little bit weird, we have an Objective Caml solution provided by Cameron.
let gambler_max l =
let rec gambler_max_aux l max_list
max_total max_not_last_list max_not_last_total =
match l with
h::t ->
if max_not_last_total + h > max_total then
gambler_max_aux t (h::max_not_last_list) (max_not_last_total+h) max_list max_total
else gambler_max_aux t max_list max_total max_list max_total
| [] -> max_list
in
gambler_max_aux l [] 0 [] 0;;
Bart mailed me a Perl solution, which I will post once my laptop returns to life, or once Bart mails it to me again
Two other interesting offerings were a C# one from Kaerber, and a Ruby solution from Nikhil Gupte.
C#
public static void Try( int[] input
{
int length = input.Length;
int sumC
for( int i = 0; i < length; i++ )
{
sumC = ( sumPP > sumPPP ? sumPP : sumPPP ) + input[i];
sumPPP = sumPP;
sumPP = sumP;
sumP = sumC;
}
int result = sumP > sumPP ? sumP : sumPP;
output.Text = string.Format( "${0}"
}
And of course the newest 13 year language there is, Ruby
days = [158 ... 380]
total = 0
soln = Array.new
while (days.length > 0) do
a = days[0] unless days[0].nil?
b = days[1] unless days[1].nil?
c = days[2] unless days[2].nil?
d = days[3] unless days[3].nil?
if a + c > b + d || a + d > b + d then
soln < < a
2.times {days.shift}
else
soln < < b
3.times {days.shift}
end
total += soln.last
end
p soln
p 'total: ' + total.to_s
That might be the end of Steve for a short while, I'm trying to find/create other interesting less known programming puzzles to see the solutions. I get a lot of emails (well 5 anyways) from people saying they find these really educational, and for that I have to thank everyone who posts solutions or comments. If I didn't include your solution it's not cause it's bad, it's because I forgot. If you mail me, I'll post it up, with apologies. I'd still like to see a J solution to this also
Time for another programming puzzle, this was given to a friend in an interview for a mobile phone operator. Apparently it maps neatly on to some problem that must regularly be solved in mobile phone networks, but seeing as said friend didn't get the job,
Tracked: Dec 05, 20:52