Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )

Liste des GroupesRevenir à r puzzles 
Sujet : Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : rec.puzzles
Date : 02. Aug 2024, 13:32:32
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v8ijkv$2qmma$1@dont-email.me>
References : 1 2 3 4
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

Gauche Scheme
 
(define (strand lst)
  (let go ((left-sum 0) (tail lst))
    (if (null? tail)
      #f
      (let ((right-sum (fold + 0 (cdr tail))))
        (cond ((< left-sum right-sum)
               (go (+ left-sum (car tail)) (cdr tail)))
              ((= left-sum right-sum) (car tail))
              (#t #f))))))

Faster:

(define (strand lst)
  (let go ((left-sum 0) (right-sum (fold + 0 (cdr lst))) (tail lst))
    (cond ((< left-sum right-sum)
           (go (+ left-sum (car tail))
               (- right-sum (cadr tail))
               (cdr tail)))
          ((= left-sum right-sum) (car tail))
          (else  #f))))
 


Date Sujet#  Auteur
25 Jul 24 * The "Strand" puzzle14HenHanna
26 Jul 24 `* Re: The "Strand" puzzle13IlanMayer
29 Jul 24  `* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or Python? )12HenHanna
30 Jul 24   +* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or Python? )3IlanMayer
31 Jul 24   i+- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or Python? )1HenHanna
20 Aug 24   i`- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp or Python? )1Phil Carmody
1 Aug 24   `* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )8B. Pym
1 Aug 24    +* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )2HenHanna
1 Aug 24    i`- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1Moebius
2 Aug 24    +* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )3B. Pym
2 Aug 24    i`* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )2Doc O'Leary ,
2 Aug 24    i `- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1Doc O'Leary ,
2 Aug 24    +- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1B. Pym
4 Aug 24    `- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal