Sunday, October 15. 2006Steve the Gambler = Follow upComments
Display comments as
(Linear | Threaded)
(defun max-sseq (lst)
(rest (reduce (lambda (curr el) (let ((meh (max (+ (first curr) el) 0))) (cons meh (max meh (rest curr))))) lst :initial-value '(0 . 0))))
** shameless vim plug **
vim automates the conversion of formatted code to html with a built in function :2html. Should you ever find the need, that should greatly reduce your burden.
J solution: (www.jsoftware.com)
scanl =: (@:|.)\ >./ (0 >. +)/scanl DATA or as a 1 liner: >./ (0 >. +)/@:|.\ DATA
www.jsoftware.com
its free. wikis and tutorials pretty easy to find from homepage.
Your pseudo-code version is one or two changes away from a python version:
x = [44, ... , 1] n = len(x) maxsofar = 0 maxendinghere = 0 for i in range(n): maxendinghere = max(maxendinghere + x[i], 0) maxsofar = max(maxsofar, maxendinghere) print "The maximum Steve could make is", maxsofar
I also used python:
wins = [44, ..., 1] so_far = end_here = (0,0,0) # start, end, max for i, w in enumerate(wins): __if end_here[2] + w > 0: ____end_here = (end_here[0], i, end_here[2] + w) __else: ____end_here = (i+1, i+1, 0) __if end_here[2] > so_far[2]: ____so_far = end_here print so_far >> (18, 27, 383)
Not completely beautiful but this code returns the list of winnings, not just the total. See the bottom for an example.
(define (break-down lst) (reverse (let loop ((total 0) (side 1) (rest lst) (nums '()) (so-far '())) (cond ((null? rest) (cons (list total nums) so-far)) ((and (> side 0) (>= (car rest) 0)) (loop (+ total (car rest)) side (cdr rest) (reverse (cons (car rest) (reverse nums))) so-far)) ((and (> side 0) (< (car rest) 0)) (loop (car rest) (- side) (cdr rest) (list (car rest)) (cons (list total nums) so-far))) ((and (< side 0) (< (car rest) 0)) (loop (+ total (car rest)) side (cdr rest) (reverse (cons (car rest) (reverse nums))) so-far)) ((and (< side 0) (> (car rest) 0)) (loop (car rest) (- side) (cdr rest) (list (car rest)) (cons (list total nums) so-far))))))) (define-struct gamble (total days)) (define (best-days lst) (define (combine g1 g2) (make-gamble (+ (gamble-total g1) (gamble-total g2)) (append (gamble-days g1) (gamble-days g2)))) (define (best g1 g2) (if (> (gamble-total g1) (gamble-total g2)) g1 g2)) (define next (case-lambda ((total current) (gamble-days (best current total))) ((total current n1) (let ((g1 (make-gamble (car n1) (cadr n1)))) (if (positive? (gamble-total g1)) (next (combine current g1) total) (next current total)))) ((total current n1 n2 . rest) (let ((g1 (make-gamble (car n1) (cadr n1))) (g2 (make-gamble (car n2) (cadr n2)))) (cond ((positive? (gamble-total g1)) (apply next total (combine current g1) n2 rest)) (else (let ((left (gamble-total current)) (middle (+ (gamble-total current) (gamble-total g1) (gamble-total g2))) (right (gamble-total g2))) (let ((m (max left middle right))) (cond ((= left m) (apply next (best total current) (make-gamble 0 '()) n2 rest)) ((= middle m) (apply next total (combine current (combine g1 g2)) rest)) ((= right m) (apply next (best total current) g2 rest))))))))))) (apply next (make-gamble 0 '()) (make-gamble 0 '()) (if (negative? (caar lst)) (cdr lst) lst))) (define (run lst) (best-days2 (break-down lst))) ;; numbers is the original list (run numbers) > (120 19 7 3 29 8 65 42 2 88) |
About:Switch to Dark on Light!
This website is the online diary of me, Des Traynor, a User Experience Researcher in Dublin, Ireland. I work with Contrast. I usually write on 5 topics: I update about 3-4 times per month. Be sure to subscribe so you don't miss this good stuff. If this is your first time here, check out the archives.My official homepage provides more information about who I am, and what I research. You can contact me at destraynor [at] gmail [dot] com Quicksearch |
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:53