Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. ; Computer Science 110
  2. ; 2016W1
  3. ;
  4. ; Graded Problem Set 6
  5. ;
  6. ; Computer Science id (Student 1): w5x0b
  7. ; Computer Science id (Student 2): ________
  8.  
  9.  
  10. ;; DO NOT PUT ANY PERSONALLY IDENTIFYING INFORMATION IN THIS FILE.
  11. ;; YOUR COMPUTER SCIENCE IDs WILL BE SUFFICIENT TO IDENTIFY YOU
  12. ;; AND, IF YOU HAVE ONE, YOUR PARTNER
  13.  
  14.  
  15. ; Overview:
  16. ;
  17. ; You are asked to design a system to help research groups in a University track research funds.
  18. ; Each research group has a name, a list of Students funded, the amount of grant money in
  19. ; thousands of dollars and a list of research groups that this group contains.
  20. ;
  21. ; A student has a name, a Degree they are working on, and the amount of money they are funded per year
  22. ; where a Degree is either UGrad, MSc or PhD.
  23. ;
  24. ; The following image illustrates this relationship
  25. ;
  26. ; .
  27. ;
  28. ; NOTE: The university also charges a yearly space fee for each research student.
  29. ; A fee of $500 for undergrads, $750 for MSc students, $1000 for PhD students.
  30. ; Constants are defined for each of these charge types in thousands of dollars below.
  31. ;
  32. ;
  33.  
  34.  
  35. (define UGRAD-SPACE .5)
  36. (define MSC-SPACE .75)
  37. (define PHD-SPACE 1)
  38.  
  39.  
  40. ; Problem 1:
  41. ;
  42. ; Design all of the data definitions necessary for the system.
  43. ; You must include examples in your data definitions that are
  44. ; sufficient to capture the data in the image above.
  45. ;
  46.  
  47.  
  48. ;; Degree is one of:
  49. ;; - "UGrad"
  50. ;; - "MSc"
  51. ;; - "PhD"
  52. ;; interp. level of university degree
  53.  
  54. ;; <examples are redundant for enumerations>
  55.  
  56. #; (define (fn-for-degree d)
  57. (cond [(string=? "UGrad" d) (...)]
  58. [(string=? "MSc" d) (...)]
  59. [(string=? "PhD" d) (...)]))
  60.  
  61. ;; Template rules used:
  62. ;; - one of 3 cases
  63. ;; - atomic distinct "UGrad"
  64. ;; - atomic distinct "MSc"
  65. ;; - atomic distinct "PhD"
  66.  
  67. ; ##################################
  68.  
  69. (define-struct student (name degree funds))
  70. ;; Student is (make-student String Degree Number)
  71.  
  72. (define SA (make-student "Yosuke" "UGrad" 4.2))
  73. (define SB (make-student "Chie" "UGrad" 5))
  74. (define SC (make-student "Souji" "PhD" 15.8))
  75. (define SD (make-student "Kanji" "UGrad" 6.2))
  76. (define SE (make-student "Naoto" "PhD" 24.2))
  77. (define SF (make-student "Yukiko" "MSc" 11))
  78. (define SG (make-student "Teddie" "PhD" 15.8))
  79.  
  80. #; (define (fn-for-student s)
  81. (... (student-name s) ;; String
  82. (student-degree s) ;; Enumeration (see definition of Degree)
  83. (student-funds s))) ;; Number (in thousands)
  84.  
  85. ;; Template rules used:
  86. ;; - compound: 3 fields
  87.  
  88. ; ##################################
  89.  
  90. ;; ListOfStudent is one of:
  91. ;; - empty
  92. ;; - (list Student ListOfStudent)
  93. ;; interp. a list of students
  94.  
  95. (define LOSD empty)
  96. (define LOSE empty)
  97.  
  98. (define LOSA (list SA SB SC))
  99. (define LOSB (list SD))
  100. (define LOSC (list SE SF SG))
  101.  
  102. #; (define (fn-for-los los)
  103. (cond [(empty? los) (...)]
  104. [else (... (fn-for-student (first los))
  105. (fn-for-los (rest los)))]))
  106.  
  107. ;; Template rules used:
  108. ;; - one of: 2 cases
  109. ;; - atomic distinct: empty
  110. ;; - compound: (list Student ListOfStudent)
  111. ;; - reference: (first los) is Student
  112. ;; - self reference: (rest los) is ListOfStudent
  113.  
  114. ; ###### MUTUALLY REFERENTIAL #######
  115.  
  116. (define-struct group (name los grant sub))
  117. ;; Group is (make-group String ListOfStudent Number ListOfGroup)
  118. ;; 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
  119.  
  120. ;; ListOfGroups is one of:
  121. ;; - empty
  122. ;; (list Group ListOfGroup)
  123. ;; interp. a list of research groups
  124.  
  125. (define GROUPD (make-group "Group D" LOSD 27 empty))
  126. (define GROUPE (make-group "Group E" LOSE 40 empty))
  127.  
  128. (define GROUPB (make-group "Group B" LOSB 817 empty))
  129. (define GROUPC (make-group "Group C" LOSC 6587 (list GROUPE)))
  130. (define GROUPA (make-group "Group A" LOSA 9987 (list GROUPB GROUPC GROUPD)))
  131.  
  132. #; (define (fn-for-group g)
  133. (... (group-name g) ;String
  134. (group-los g) ;ListOfStudents
  135. (group-grant g) ;Number
  136. (fn-for-log (group-sub g)))) ;Mutal recursion from mutual reference
  137.  
  138. ;; Template rules used:
  139. ;; - compound: 4 fields
  140. ;; - reference: los field is ListOfStudent
  141. ;; - mutaul reference: log field is ListOfGroup
  142.  
  143. #; (define (fn-for-log log)
  144. (cond [(empty? log) (...)]
  145. [else (... (fn-for-group (first log)) ;Mutual recursion from mutual reference
  146. (fn-for-log (rest log)))]))
  147.  
  148. ;; Template rules used:
  149. ;; - one of: 2 cases
  150. ;; - atomic distinct: empty
  151. ;; - compound: (list Group ListOfGroup)
  152. ;; - mutual reference: (first log) is Group
  153. ;; - self-reference: (rest log) is ListOfGroup
  154.  
  155. ; Problem 2
  156. ;
  157. ; Design a function that takes a Group and produces the total grant money
  158. ; in thousands of dollars awarded to this group and all subgroups.
  159. ;
  160.  
  161.  
  162. ;; Group -> Number
  163. ;; interp. total grant money awarded to given group + subgroups
  164.  
  165. (check-expect (sumtotal GROUPD) 27)
  166. (check-expect (sumtotal GROUPC) (+ 6587 40))
  167. (check-expect (sumtotal GROUPA) 17458)
  168.  
  169. ; (define (sumtotal g) 0) ; stub
  170.  
  171. ;; template from Group
  172.  
  173. (define (sumtotal g)
  174. (cond [(empty? (group-sub g)) (group-grant g)]
  175. [else (if (empty? (group-sub (first (group-sub g))))
  176. (+ (group-grant (first (group-sub g)))
  177. (sumtotal (make-group (group-name g) (group-los g) (group-grant g) (rest (group-sub g)))))
  178. (+ (sumtotal (first (group-sub g)))
  179. (sumtotal (make-group (group-name g) (group-los g) (group-grant g) (rest (group-sub g))))))]))
  180.  
  181.  
  182. ; Problem 3
  183. ;
  184. ; Design a function that takes a Group and a Degree *which* produces a list of all
  185. ; Students' names in this Group and all subgroups working on the specified Degree.
  186. ;
  187.  
  188.  
  189. ;; Group Degree -> ListOfString
  190. ;; produces a list of names of students studying a given degree in a given group
  191.  
  192. (check-expect (dfinder GROUPE "UGrad") empty)
  193. (check-expect (dfinder GROUPB "UGrad") (list "Kanji"))
  194. (check-expect (dfinder GROUPC "PhD") (list "Naoto" "Teddie"))
  195. (check-expect (dfinder GROUPC "UGrad") empty) ; when searching for a degree in a valid list without that degree
  196. (check-expect (dfinder GROUPA "UGrad") (list "Kanji" "Yosuke" "Chie")) ; note that list of names are in order of tree traversal (deepest to shallowest leaf)
  197. (check-expect (dfinder GROUPA "PhD") (list "Naoto" "Teddie" "Souji")) ; same as above
  198.  
  199. ; (define (dfinder g d) empty) ; stub
  200.  
  201. ; template from Group
  202.  
  203. (define (dfinder g d)
  204. (listnames (checkdegree d (student-lister g))))
  205.  
  206. ; ########################################
  207.  
  208. ;; Group -> ListOfStudent
  209. ;; interp. given a group, produces a list of all students including within subgroups and group in order of tree traversal (deepest to shallowest leaf)
  210.  
  211. (check-expect (student-lister GROUPD) empty)
  212. (check-expect (student-lister GROUPA) (list SD SE SF SG SA SB SC))
  213.  
  214. ; (define (student-lister g) empty) ; stub
  215.  
  216. ; template from group
  217.  
  218. (define (student-lister g)
  219. (cond [(empty? (group-sub g)) (group-los g)]
  220. [else (if (empty? (group-sub (first (group-sub g))))
  221. (append (group-los (first (group-sub g)))
  222. (student-lister (make-group (group-name g) (group-los g) (group-grant g) (rest (group-sub g)))))
  223. (append (student-lister (first (group-sub g)))
  224. (student-lister (make-group (group-name g) (group-los g) (group-grant g) (rest (group-sub g))))))]))
  225.  
  226. ; ########################################
  227.  
  228. ;; Degree ListOfStudent -> ListOfStudent
  229. ;; interp. given a degree and a list of students, returns new list of student where the degree matches the given d
  230.  
  231. (check-expect (checkdegree "PhD" LOSD) empty)
  232. (check-expect (checkdegree "PhD" LOSA) (list SC))
  233. (check-expect (checkdegree "PhD" LOSC) (list SE SG))
  234. (check-expect (checkdegree "PhD" LOSB) empty)
  235.  
  236. ; (define (checkdegree d los) empty) ; stub
  237.  
  238. ;; template from ListOfStudent with added parameter d for degree
  239.  
  240. (define (checkdegree d los)
  241. (cond [(empty? los) empty]
  242. [else (if (string=? d (student-degree (first los)))
  243. (cons (first los) (checkdegree d (rest los)))
  244. (checkdegree d (rest los)))]))
  245.  
  246. ; #########################################
  247.  
  248. ;; ListOfStudent -> ListOfString
  249. ;; interp. given a list of students, returns a new list of strings of every name
  250.  
  251. (check-expect (listnames LOSD) empty)
  252. (check-expect (listnames LOSA) (list "Yosuke" "Chie" "Souji"))
  253.  
  254. ; (define (listnames los) empty) ; stub
  255.  
  256. ; template from ListOfStudent
  257.  
  258. (define (listnames los)
  259. (cond [(empty? los) empty]
  260. [else (cons (student-name (first los)) (listnames (rest los)))]))
  261.  
  262. ; Problem 4
  263. ;
  264. ;
  265. ; Design a function that takes a Group and produces the amount of Grant money remaining
  266. ; after cost of students and space is taken off for this Group and all subgroups.
  267. ;
  268.  
  269.  
  270. ;; Group -> Number
  271.  
  272.  
  273.  
  274. ; ##########################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement