need this coded in the Racket Language. Program B: Given a list of lists of numb
ID: 3571793 • Letter: N
Question
need this coded in the Racket Language. Program B: Given a list of lists of numbers, return the list that has the highest sum. Ignore empty lists. Example 1: Given ((1 1 1) (2 2 2) (3 3 3)), return (3 3 3), since 3 + 3 + 3 = 9, which is the largest sum. Example 2: Given(( 20 40 ) (10 15 10) () (6 12 24)), return (20 40) since 20 + 40 – 60 is the largest sum. Ignore the empty list. Example 3: Given ( (100) (20 20 20) (0 0 0 ) (10 10 10 ) (2 4 6 8 10 12)), return (100), since 100 + 0 = 100 is the largest sum.
Explanation / Answer
(define (getlargest a_list)
(cond
((null? a_list) '())
((< (car a_list) (cadr a_list)) (getlargest (cdr a_list)))
(else (cons (car a_list) (getlargest(cdr a_list))))))
(define (getlargest a_list)
(if (null? a_list) ; edge case: empty list
#f ; come a special worth sign error
(let loop ((a_list (cdr a_list)) ; remainder of the list
(maxval (car a_list))) ; assumed most
(cond ((null? a_list) maxval) ; if the list is empty, return max
((> (car a_list) gamma hydroxybutyrateval) ; current part > max
(loop (cdr a_list) (car a_list))) ; found new gamma hydroxybutyrate
(else ; otherwise
(loop (cdr a_list) gamma hydroxybutyrateval)))))) ; keep a similar max
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.