Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Computer Science 110
- ; 2016W1
- ;
- ; Graded Problem Set 6
- ;
- ; Computer Science id (Student 1): w5x0b
- ; Computer Science id (Student 2): ________
- ;; DO NOT PUT ANY PERSONALLY IDENTIFYING INFORMATION IN THIS FILE.
- ;; YOUR COMPUTER SCIENCE IDs WILL BE SUFFICIENT TO IDENTIFY YOU
- ;; AND, IF YOU HAVE ONE, YOUR PARTNER
- ; Overview:
- ;
- ; You are asked to design a system to help research groups in a University track research funds.
- ; Each research group has a name, a list of Students funded, the amount of grant money in
- ; thousands of dollars and a list of research groups that this group contains.
- ;
- ; A student has a name, a Degree they are working on, and the amount of money they are funded per year
- ; where a Degree is either UGrad, MSc or PhD.
- ;
- ; The following image illustrates this relationship
- ;
- ; .
- ;
- ; NOTE: The university also charges a yearly space fee for each research student.
- ; A fee of $500 for undergrads, $750 for MSc students, $1000 for PhD students.
- ; Constants are defined for each of these charge types in thousands of dollars below.
- ;
- ;
- (define UGRAD-SPACE .5)
- (define MSC-SPACE .75)
- (define PHD-SPACE 1)
- ; Problem 1:
- ;
- ; Design all of the data definitions necessary for the system.
- ; You must include examples in your data definitions that are
- ; sufficient to capture the data in the image above.
- ;
- ;; Degree is one of:
- ;; - "UGrad"
- ;; - "MSc"
- ;; - "PhD"
- ;; interp. level of university degree
- ;; <examples are redundant for enumerations>
- #; (define (fn-for-degree d)
- (cond [(string=? "UGrad" d) (...)]
- [(string=? "MSc" d) (...)]
- [(string=? "PhD" d) (...)]))
- ;; Template rules used:
- ;; - one of 3 cases
- ;; - atomic distinct "UGrad"
- ;; - atomic distinct "MSc"
- ;; - atomic distinct "PhD"
- ; ##################################
- (define-struct student (name degree funds))
- ;; Student is (make-student String Degree Number)
- (define SA (make-student "Yosuke" "UGrad" 4.2))
- (define SB (make-student "Chie" "UGrad" 5))
- (define SC (make-student "Souji" "PhD" 15.8))
- (define SD (make-student "Kanji" "UGrad" 6.2))
- (define SE (make-student "Naoto" "PhD" 24.2))
- (define SF (make-student "Yukiko" "MSc" 11))
- (define SG (make-student "Teddie" "PhD" 15.8))
- #; (define (fn-for-student s)
- (... (student-name s) ;; String
- (student-degree s) ;; Enumeration (see definition of Degree)
- (student-funds s))) ;; Number (in thousands)
- ;; Template rules used:
- ;; - compound: 3 fields
- ; ##################################
- ;; ListOfStudent is one of:
- ;; - empty
- ;; - (list Student ListOfStudent)
- ;; interp. a list of students
- (define LOSD empty)
- (define LOSE empty)
- (define LOSA (list SA SB SC))
- (define LOSB (list SD))
- (define LOSC (list SE SF SG))
- #; (define (fn-for-los los)
- (cond [(empty? los) (...)]
- [else (... (fn-for-student (first los))
- (fn-for-los (rest los)))]))
- ;; Template rules used:
- ;; - one of: 2 cases
- ;; - atomic distinct: empty
- ;; - compound: (list Student ListOfStudent)
- ;; - reference: (first los) is Student
- ;; - self reference: (rest los) is ListOfStudent
- ; ###### MUTUALLY REFERENTIAL #######
- (define-struct group (name los grant sub))
- ;; Group is (make-group String ListOfStudent Number ListOfGroup)
- ;; interp. a research group that has a name, a list of students, the grant money associated with it, and a list of research groups within it
- ;; ListOfGroups is one of:
- ;; - empty
- ;; (list Group ListOfGroup)
- ;; interp. a list of research groups
- (define GROUPD (make-group "Group D" LOSD 27 empty))
- (define GROUPE (make-group "Group E" LOSE 40 empty))
- (define GROUPB (make-group "Group B" LOSB 817 empty))
- (define GROUPC (make-group "Group C" LOSC 6587 (list GROUPE)))
- (define GROUPA (make-group "Group A" LOSA 9987 (list GROUPB GROUPC GROUPD)))
- #; (define (fn-for-group g)
- (... (group-name g) ;String
- (group-los g) ;ListOfStudents
- (group-grant g) ;Number
- (fn-for-log (group-sub g)))) ;Mutal recursion from mutual reference
- ;; Template rules used:
- ;; - compound: 4 fields
- ;; - reference: los field is ListOfStudent
- ;; - mutaul reference: log field is ListOfGroup
- #; (define (fn-for-log log)
- (cond [(empty? log) (...)]
- [else (... (fn-for-group (first log)) ;Mutual recursion from mutual reference
- (fn-for-log (rest log)))]))
- ;; Template rules used:
- ;; - one of: 2 cases
- ;; - atomic distinct: empty
- ;; - compound: (list Group ListOfGroup)
- ;; - mutual reference: (first log) is Group
- ;; - self-reference: (rest log) is ListOfGroup
- ; Problem 2
- ;
- ; Design a function that takes a Group and produces the total grant money
- ; in thousands of dollars awarded to this group and all subgroups.
- ;
- ;; Group -> Number
- ;; interp. total grant money awarded to given group + subgroups
- (check-expect (sumtotal GROUPD) 27)
- (check-expect (sumtotal GROUPC) (+ 6587 40))
- (check-expect (sumtotal GROUPA) 17458)
- ; (define (sumtotal g) 0) ; stub
- ;; template from Group
- (define (sumtotal g)
- (cond [(empty? (group-sub g)) (group-grant g)]
- [else (if (empty? (group-sub (first (group-sub g))))
- (+ (group-grant (first (group-sub g)))
- (sumtotal (make-group (group-name g) (group-los g) (group-grant g) (rest (group-sub g)))))
- (+ (sumtotal (first (group-sub g)))
- (sumtotal (make-group (group-name g) (group-los g) (group-grant g) (rest (group-sub g))))))]))
- ; Problem 3
- ;
- ; Design a function that takes a Group and a Degree *which* produces a list of all
- ; Students' names in this Group and all subgroups working on the specified Degree.
- ;
- ;; Group Degree -> ListOfString
- ;; produces a list of names of students studying a given degree in a given group
- (check-expect (dfinder GROUPE "UGrad") empty)
- (check-expect (dfinder GROUPB "UGrad") (list "Kanji"))
- (check-expect (dfinder GROUPC "PhD") (list "Naoto" "Teddie"))
- (check-expect (dfinder GROUPC "UGrad") empty) ; when searching for a degree in a valid list without that degree
- (check-expect (dfinder GROUPA "UGrad") (list "Kanji" "Yosuke" "Chie")) ; note that list of names are in order of tree traversal (deepest to shallowest leaf)
- (check-expect (dfinder GROUPA "PhD") (list "Naoto" "Teddie" "Souji")) ; same as above
- ; (define (dfinder g d) empty) ; stub
- ; template from Group
- (define (dfinder g d)
- (listnames (checkdegree d (student-lister g))))
- ; ########################################
- ;; Group -> ListOfStudent
- ;; interp. given a group, produces a list of all students including within subgroups and group in order of tree traversal (deepest to shallowest leaf)
- (check-expect (student-lister GROUPD) empty)
- (check-expect (student-lister GROUPA) (list SD SE SF SG SA SB SC))
- ; (define (student-lister g) empty) ; stub
- ; template from group
- (define (student-lister g)
- (cond [(empty? (group-sub g)) (group-los g)]
- [else (if (empty? (group-sub (first (group-sub g))))
- (append (group-los (first (group-sub g)))
- (student-lister (make-group (group-name g) (group-los g) (group-grant g) (rest (group-sub g)))))
- (append (student-lister (first (group-sub g)))
- (student-lister (make-group (group-name g) (group-los g) (group-grant g) (rest (group-sub g))))))]))
- ; ########################################
- ;; Degree ListOfStudent -> ListOfStudent
- ;; interp. given a degree and a list of students, returns new list of student where the degree matches the given d
- (check-expect (checkdegree "PhD" LOSD) empty)
- (check-expect (checkdegree "PhD" LOSA) (list SC))
- (check-expect (checkdegree "PhD" LOSC) (list SE SG))
- (check-expect (checkdegree "PhD" LOSB) empty)
- ; (define (checkdegree d los) empty) ; stub
- ;; template from ListOfStudent with added parameter d for degree
- (define (checkdegree d los)
- (cond [(empty? los) empty]
- [else (if (string=? d (student-degree (first los)))
- (cons (first los) (checkdegree d (rest los)))
- (checkdegree d (rest los)))]))
- ; #########################################
- ;; ListOfStudent -> ListOfString
- ;; interp. given a list of students, returns a new list of strings of every name
- (check-expect (listnames LOSD) empty)
- (check-expect (listnames LOSA) (list "Yosuke" "Chie" "Souji"))
- ; (define (listnames los) empty) ; stub
- ; template from ListOfStudent
- (define (listnames los)
- (cond [(empty? los) empty]
- [else (cons (student-name (first los)) (listnames (rest los)))]))
- ; Problem 4
- ;
- ;
- ; Design a function that takes a Group and produces the amount of Grant money remaining
- ; after cost of students and space is taken off for this Group and all subgroups.
- ;
- ;; Group -> Number
- ; ##########################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement