תרגיל 6 scheme

bennylisp, Racket, schemeLeave a Comment

פיתרון 1


#lang racket



#| א |#
#| data |#
(define jctMarks '((12345 75 "English")
                   (23452 83 "Physics")
                   (23560 81 "Statistics")
                   (23415 61 "Computer")
                   (23459 90 "Physics")    
                   (12345 75 "Computer")
                   (23452 100 "Statistics")))

(define teacherName '(("Aharoni" "English")
                      ("Melamed" "Physics")
                      ("Kaner" "Computer")
                      ("Zloti" "Statistics")
                      ("Korman" "Philosophy")))

#| utilities |#
(define (sum list)
  (if (null? list)
    0
    (+ (car list) (sum (cdr list)))
  )
)
(define (average list)
  (when (< 0 (length list))
    (/ (sum list) (length list))
  )
)

#| function |#
(define (teachersAvg list1 list2)
  (define (findTeacherFromCourse course)
    (car (findf (lambda (i)(equal? course (cadr i))) list2))
  )
  (define groups (group-by caddr list1))
  (map (lambda(item)
    (define marks (findf (lambda (i)(equal? (cadr item) (caddar i))) groups))
    (when (eq? marks #f) (set! marks '()))
      (list
       (car item)
       (append
        (map car marks)
        (list (average (map cadr marks)))
       )
      )
  ) list2)
)

#| execute |#
(teachersAvg jctMarks teacherName)
#| the response example is incorrect, the name of the teacher is "Zloti" not "Zolti" and there are more issues, based on the example there are only 5 students eventhough there are 7 students |#

#| ב |#

#| data |#
(define courses '(("English" (("Aharoni" ((12345 75) (54689 50) (23452 95) (21437 70))) ("Michaeli" ((12346 70) (89756 65)))))
                  ("Physics" (("Melamed" ((23452 83) (12345 90))) ("Levy" ((23454 85))) ("Cohen" ((23444 90)))))
                  ("Computer" (("Kaner" ((23415 61))) ("Katz" ((12345 75) (54689 80)))))
                  ("Statistics" (("Zloti" ((23560 81) (23452 85)))))
                  ("Philosophy" (("Korman" ((54689 90) (23452 85)))))
                 )
)

#| utilities |#
(define (flatten lst)
  (match lst
    [(list) (list)]
    [(? (compose not list?)) (list lst)]
    [(list x xs ...) (append (flatten x) (flatten xs))]))

#| function |#
(define (coursesAvg list1)
  (map (lambda (item)
    (define course (map cadr (cadr item)))
    (define ids (flatten (map (lambda (item)(map car item)) course)))
    (define scores (flatten (map (lambda (item)(map cadr item)) course)))
    (list
      (car item)
      (append 
        ids
        (list (average scores))
      )
    )  
  ) list1 )
)

(coursesAvg courses)

y,levental

פיתרון 2


#lang racket

(define (rounder N)
  (let ((FN (floor N)))
       (+
          FN
          (if (< (- N FN) 0.5)
              0
              1
          )
       )
  )
)

(define (IDs&avarage jctMarks [RES null])
  (if (null? jctMarks)
      (if (null? RES)
          null
          (append (cdr RES) (list (rounder (/ (car RES) (- (length RES) 1)))))
      )
      (IDs&avarage
         (cdr jctMarks)
         (if (null? RES)
             (list
                (cadar jctMarks)
                (caar jctMarks)
             )
             (append
                (cons (+ (car RES) (cadar jctMarks)) (cdr RES))
                (list (caar jctMarks))
             )
         )
      )
  )
)

(define (teachersAvg jctMarks teacherName)
  (map
     (lambda (teacherAvg)
       (list
          (car teacherAvg)
          (IDs&avarage
             (filter
                (lambda (x)
                        (string=? (caddr x) (cadr teacherAvg))
                )
                jctMarks
             )
          )
       )
     )
     teacherName
  )
)

(define (coursesAvg courses)
  (map
     (lambda (course)
       (list
          (car course)
          (IDs&avarage
             (apply
                append
                (map
                   (lambda (teacher)
                           (cadr teacher)
                   )
                   (cadr course)
                )
             )
          )
       )
     )
     courses
  )
)

H.s

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *