Find maximum Collatz sequence from a dating list

I am new to schema syntax. this is the last part of a project I am working on. I was able to find the maximum of the Collatz sequence, but this part of the project requires finding the maximum length from multiple Collatz sequence lists. So, for example, giving this list: '((1 10) (10 200) (201 210) (900 1000) and the output should be like this:' (20 125 89 174) I need to find the maximum length between a number from 1 to 10 and then 10 to 200 ets here is my code:

#lang racket
; Part I
(define (sequence n)
  (cond  [(= n 1)
      (list n)]
  [(even? n)
   ( cons n(sequence( / n 2)))]
  [(odd? n) 
   ( cons n(sequence (+(* n 3) 1))) ] ))

(sequence 10)

; Part II
(define (find-length items)
  (if (null? items)          
  (list )                   
  (cons                  
   (length (sequence(car items)))        
   (find-length (rest items))))
   )
 (find-length (list 10 16 22 90 123 169))


;Part III
(define max-in-list (lambda (ls)
(let ( (head (car ls)) (tail (cdr ls)))
  (if (null? tail)
    ; list contains only one item, return it
    head
    ; else find largest item in tail
    (let ((max-in-tail (max-in-list tail)))
      ; return the larger of 'head' and 'max-in-tail'
      (if (> head max-in-tail)
        head
        max-in-tail
      )
    )
  )
)
  ))

(define (find-max i j)
 ( if (= i j)
   (list)
  (cons
  (max-in-list (find-length(sequence i)))
  (find-max (+ 1 i ) j)
  )) 
)
(max-in-list(find-max 1 10))

(define (max-length-list items )
  (if (null? items)
  (list)

  (cons
  (find-max ? ?) ) ; how i can call this function ?
  (max-length-list (?) ) ; how i can call this function ?
  )))

(max-length-list  '((1 10) (10 200) (201 210) (900 1000) ))

      

0


source to share


1 answer


Each item in the list that you navigate to max-length-list

is a list with two numbers and nil

eg. (cons 1 (cons 2 '()))

...
The first number is (car (car items))

.
The second is (car (cdr (car items)))

.

Or, if you are let ((head (car items))

, they (car head)

and (car (cdr head))

.



The recursive call is trivial; you processed the first element with find-max

, now you just need to process the rest. You obviously already know how to do this since you did it.

0


source







All Articles