Deutsch   English   Français   Italiano  
<v8oh95$6a6h$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: rec.puzzles
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
Date: Sun, 4 Aug 2024 18:28:57 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <v8oh95$6a6h$1@dont-email.me>
References: <v7u7qd$2dgbs$1@dont-email.me> <6f90c2b4abed28c153dea46de3af408d@www.novabbs.com> <v88ood$jnp6$1@dont-email.me> <v8fkps$23nr5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 04 Aug 2024 20:28:57 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e14708043080897b7486957644b6603d";
	logging-data="207057"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18H/Q8KLhtymHFf4xkKy2CL"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:Yd4SENTd7doJgoamPB5xCRoyPsY=
Bytes: 2550

B. Pym wrote:

> HenHanna wrote:
> 
> > > > e.g. -------- For the (street)  Numbers (1,2,3,4,5,6,7,8)
> > > > 
> > > >        (1,2,3,4,5)  and  (7,8)  both add up to 15.
> > > > 
> > > > 
> > > > 
> > > > "In a given street of houses with consecutive numbers between
> > > > 50 and 500, find the house number, for which, the sum of
> > > > numbers on the left is equal to the sum of numbers on the
> > > > right"
> 
> 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))))))
> 
> (strand '(1 2 3 4 5 6 7 8))
>   ===>
> 6
> 
> (lrange 2 5)
>   ===>
> (2 3 4)
> 
> (any
>   (lambda (n)
>     (if (strand (lrange 50 n))
>       n
>       #f))
>   (lrange 500 50 -1))
>   ===>
> 352
> 
> (strand (lrange 50 352))
>   ===>
> 251


newLISP

(define (strand lst)
  (let (left-sum 0
        right-sum (apply + (rest lst) 2))
    (while (< left-sum right-sum)
      (++ left-sum (pop lst))
      (-- right-sum (first lst)))
    (and (= left-sum right-sum) (first lst))))

(sequence 2 4)
  ===>
(2 3 4)

(exists (fn (n) (strand (sequence 50 n))) (sequence 500 50))
  ===>
351

(strand (sequence 50 351))
  ===>
251