• Lisp problem to solve

    From Robert L.@21:1/5 to John Thingstad on Mon Feb 14 03:45:04 2022
    John Thingstad wrote:

    Pn++ Tue, 20 Nov 2007 23:59:24 +0100, skrev Mark Tarver <[email protected]>:

    On 20 Nov, 14:48, [email protected] 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)))

    (define (max-trans a b) (if (> (cadr b) (cadr a)) b a))

    ;; Without sorting.
    (car (reduce max-trans #f List))

    ===>
    T3

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to John Thingstad on Thu Aug 29 06:23:13 2024
    John Thingstad wrote:

    Pn++ Tue, 20 Nov 2007 23:59:24 +0100, skrev Mark Tarver


    On 20 Nov, 14:48, [email protected] 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to John Thingstad on Thu Jun 19 22:19:47 2025
    John Thingstad wrote:

    Pn++ Tue, 20 Nov 2007 23:59:24 +0100, skrev Mark Tarver <[email protected]>:

    On 20 Nov, 14:48, [email protected] 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)))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Mon Jun 30 22:34:19 2025
    B. Pym wrote:


    John Thingstad wrote:

    Pn++ Tue, 20 Nov 2007 23:59:24 +0100, skrev Mark Tarver <[email protected]>:

    On 20 Nov, 14:48, [email protected] 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))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)