Deutsch   English   Français   Italiano  
<103v3d7$2dubv$1@dont-email.me>

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

Path: nntp.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp
Subject: Re: Lisp problem to solve
Date: Mon, 30 Jun 2025 22:34:19 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <103v3d7$2dubv$1@dont-email.me>
References: <10322e2$5qeu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 01 Jul 2025 00:34:19 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="14001aedbb9fe848b5ad73cc8a407c26";
	logging-data="2554239"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+ZJRgAAFSIoTStn8m2qwAm"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:DXgY698Cf0I8n5S6oswwqeZLaIw=

B. Pym wrote:

> 
> John Thingstad wrote:
> 
> > Pn++ Tue, 20 Nov 2007 23:59:24 +0100, skrev Mark Tarver
> > <dr.mtarver@ukonline.co.uk>:
> > 
> > > On 20 Nov, 14:48, ryan.dufra...@gmail.com wrote:
> > >> You are given a list of transactions along with the
> > >> profits from the transactions. Write code that will return the
> > >> transaction with the maximum profit. If many transactions have the
> > >> same and maximusm profit, the code can return any of these. For
> > >> example,
> > >> given ((T1 20) (T2 88) (T3 188) (T4 99) (T5 66)), the code should.
> > >> return T3.
> > > > 
> > >> I am looking for code or pseudocode.
> > > 
> > > (SETQ LIST '((T1 20) (T2 88) (T3 188) (T4 99) (T5 66)))
> > > 
> > > Shortest solution is 1 line.
> > > 
> > > (FIRST (FIRST (SORT LIST (FUNCTION (LAMBDA (X Y) (> (SECOND X)
> > > (SECOND Y)))))))
> > > 
> > > Mark
> > > 
> > 
> > or (caar (sort list #'> :key #'second))
> 
> Gauche Scheme:
> 
> (define List '((T1 20) (T2 88) (T3 188) (T4 99) (T5 66)))
> 
> (caar (sort List > last))
>   ===>
> T3
> 
> Without sorting:
> 
> (car
>   (reduce
>     (lambda(b a) (if (> (last b) (last a)) b a))
>     #f
>     List))
> 
> 
> Reverse look-up in association list:
> 
> (car
>   (let ((biggest (apply max (map last List))))
>     (rassoc (list biggest) List)))


(car (max-by cadr List))

Given:

(define (max-by key seq)
  (reduce
    (lambda(b a) (if (> (key b)(key a)) b a))
    #f   ;; Returned if list is empty.
    seq))