Sujet : Re: How do i get multiple Min() values?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.python comp.lang.lispDate : 13. Jul 2024, 21:11:07
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v6ujg9$3nd4k$1@dont-email.me>
References : 1 2
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:
HenHanna wrote:
How do i get multiple Min() values?
e.g. for Y = (x-2)*(x-3) for x in range(-10,10)
the min Y is hit twice
print( min( ((x-2)*(x-3), (x, (x-2, x-3)))
for x in range(-10,10) ) )
is this easy in Scheme(Gauche) ?
Gauche Scheme
(use gauche.collection) ;; fold2
(define (min-by fn lst)
(if (null? lst)
(values '() #f)
(fold2
(lambda (x best worth)
(let ((score (fn x)))
(cond ((< score worth) (values (list x) score))
((= score worth) (values (cons x best) worth))
(#t (values best worth)))))
(take lst 1) (fn (car lst))
(cdr lst))))
(min-by (lambda(x) (* (- x 2) (- x 3))) (lrange -10 11))
===>
(3 2)
0
(use gauche.collection) ;; fold2
(define (min-max-by fn lst)
(define (use compare x score best worth)
(cond ((compare score worth) (list (list x) score))
((= score worth) (list (cons x best) worth))
(#t (list best worth))))
(if (null? lst)
(values (list '() #f) (list '() #f))
(let ((initial (list (take lst 1) (fn (car lst)))))
(fold2
(lambda (x mini maxi)
(let ((score (fn x)))
(values
(apply use < x score mini)
(apply use > x score maxi))))
initial initial
(cdr lst)))))
(min-max-by (lambda(x) (* (- x 2) (- x 3))) (lrange -10 11))
===>
((3 2) 0)
((-10) 156)