Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You have a strong appeal for members of the opposite sex.


comp / comp.lang.scheme / Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )

SubjectAuthor
* Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )B. Pym
`- Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )B. Pym

1
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 2 Aug 2024 12:47 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
Date: Fri, 2 Aug 2024 12:47:00 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <v8ikg2$2qrts$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: Fri, 02 Aug 2024 14:47:00 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7aeb7d3f9bdbec13555d482a35ea9d08";
logging-data="2977724"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++9L3GbcNBzWu0oiqyfdoJ"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:iW6HGhiEGtOxM8g+HNk9TpX0bTs=
View all headers

B. Pym 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

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))))

Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sat, 3 Aug 2024 02:39 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: The "Strand" puzzle --- ( Continued Fractions using Lisp orPython? )
Date: Sat, 3 Aug 2024 02:39:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <v8k59k$382b5$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> <v8ikg2$2qrts$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 03 Aug 2024 04:39:52 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a358947f45c9b99589af96dcca686d10";
logging-data="3410277"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199gXi9Br2gbfjCcexf/NKF"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:Zt7P8ANA+N4CnCChTJXH32k9O1c=
View all headers

B. Pym wrote:

> B. Pym 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
>
>
> 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))
>

Using "do":

(define (strand lst)
(do ((tail lst (cdr tail))
(left-sum 0 (+ left-sum (car tail)))
(right-sum (fold + 0 (cdr lst)) (- right-sum (cadr tail))))
((>= left-sum right-sum)
(if (= left-sum right-sum) (car tail) #f))))

1

rocksolid light 0.9.8
clearnet tor