Advertisement
Guest User

Untitled

a guest
Sep 27th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 13.44 KB | None | 0 0
  1. ;; ================ Exercise 2 ================
  2.  
  3. ; A ListofPosns (LoP) is
  4. ; - '()
  5. ; - (cons Posn LoP)
  6. ; Examples
  7. (define lop1 '())
  8. (define lop2 (cons (make-posn 0 1) lop1))
  9. (define lop3 (cons (make-posn 3 3) lop2))
  10. (define lop4 (cons (make-posn 2 2) lop3))
  11.  
  12. ; Template
  13. #; (define (lop-temp lop)
  14.      (cond [(empty? lop) ...]
  15.            [(cons? lop) ... (posn-temp (first lop)) ... (lop-temp (rest lop)) ...]))
  16.  
  17. ; A Direction is one of
  18. ; - (make-posn 0 1)
  19. ; - (make-posn 0 -1)
  20. ; - (make-posn 1 0)
  21. ; - (make-posn -1 0)
  22. ; A direction represents a direction on a unit circle with the origin as the center.
  23.  
  24. ; Direction Examples:
  25. (define NORTH (make-posn 0 1))
  26. (define SOUTH (make-posn 0 -1))
  27. (define EAST (make-posn 1 0))
  28. (define WEST (make-posn -1 0))
  29.  
  30. ; Template
  31. #; (define (direction-temp direction)
  32.      (cond [(equal? direction NORTH) ...]
  33.            [(equal? direction SOUTH) ...]
  34.            [(equal? direction EAST) ...]
  35.            [(equal? direction WEST) ...]))
  36.  
  37. ; neighbor : Posn Direction -> Posn
  38. ; Gets the posn's neighbor to a specific direction.
  39. (define (neighbor posn dir)
  40.   (make-posn (+ (posn-x posn) (posn-x dir)) (+ (posn-y posn) (posn-y dir))))
  41.  
  42.  
  43. (check-expect (neighbor (make-posn 50 50) NORTH) (make-posn 50 51))
  44. (check-expect (neighbor (make-posn 50 50) SOUTH) (make-posn 50 49))
  45. (check-expect (neighbor (make-posn 50 50) EAST) (make-posn 51 50))
  46. (check-expect (neighbor (make-posn 50 50) WEST) (make-posn 49 50))
  47.  
  48. ; valid-neighbor? : Posn -> Boolean
  49. ; Checks to see if a Posn is a valid neighbor.
  50. (define (valid-neighbor? posn)
  51.   (and (>= (posn-x posn) 0) (<= (posn-x posn) 99) (>= (posn-y posn) 0) (<= (posn-y posn) 99)))
  52.  
  53. (check-expect (valid-neighbor? (make-posn 0 0)) #true)
  54. (check-expect (valid-neighbor? (make-posn 99 99)) #true)
  55. (check-expect (valid-neighbor? (make-posn 0 100)) #false)
  56. (check-expect (valid-neighbor? (make-posn 100 0)) #false)
  57. (check-expect (valid-neighbor? (make-posn -1 0)) #false)
  58. (check-expect (valid-neighbor? (make-posn 0 -1)) #false)
  59.  
  60.  
  61. ; get-neighbor-list : Posn -> LoP
  62. ; Gets east, west, north, and south posns.
  63. (define (get-neighbor-list posn)
  64.   (cons (neighbor posn EAST) (cons (neighbor posn WEST) (cons (neighbor posn NORTH) (cons (neighbor posn SOUTH) '())))))
  65.  
  66. (check-expect (get-neighbor-list (make-posn 50 50)) (cons (make-posn 51 50) (cons (make-posn 49 50) (cons (make-posn 50 51) (cons (make-posn 50 49) '())))))
  67. (check-expect (get-neighbor-list (make-posn 99 99)) (cons (make-posn 100 99) (cons (make-posn 98 99) (cons (make-posn 99 100) (cons (make-posn 99 98) '())))))
  68. (check-expect (get-neighbor-list (make-posn 0 0)) (cons (make-posn 1 0) (cons (make-posn -1 0) (cons (make-posn 0 1) (cons (make-posn 0 -1) '())))))
  69.  
  70. ; get-valid-neighbors : LoP -> LoP
  71. ; Removes any Posns that do not fall in the domain [0, 99] from the LoP.
  72. (define (get-valid-neighbors lop)
  73.   (cond [(empty? lop) lop]
  74.         [(cons? lop) (if (valid-neighbor? (first lop)) (cons (first lop) (get-valid-neighbors (rest lop)))  (get-valid-neighbors (rest lop)))]))
  75.  
  76.  
  77. (check-expect (get-valid-neighbors (cons (make-posn 51 50) (cons (make-posn 49 50) (cons (make-posn 50 51) (cons (make-posn 50 49) '())))))
  78.               (cons (make-posn 51 50) (cons (make-posn 49 50) (cons (make-posn 50 51) (cons (make-posn 50 49) '())))))
  79. (check-expect (get-valid-neighbors (cons (make-posn 100 99) (cons (make-posn 98 99) (cons (make-posn 99 100) (cons (make-posn 99 98) '())))))
  80.               (cons (make-posn 98 99) (cons (make-posn 99 98) '())))
  81. (check-expect (get-valid-neighbors (cons (make-posn 1 0) (cons (make-posn -1 0) (cons (make-posn 0 1) (cons (make-posn 0 -1) '())))))
  82.               (cons (make-posn 1 0) (cons (make-posn 0 1) '())))
  83.  
  84. ; get-neighbors : Posn -> LoP
  85. ; Gets all neighbors of a posn that fall in the domain [0, 99].
  86. (define (get-neighbors posn)
  87.   (get-valid-neighbors (get-neighbor-list posn)))
  88.  
  89. (check-expect (get-neighbors (make-posn 0 0)) (cons (make-posn 1 0) (cons (make-posn 0 1) '())))
  90. (check-expect (get-neighbors (make-posn 99 99)) (cons (make-posn 98 99) (cons (make-posn 99 98) '())))
  91. (check-expect (get-neighbors (make-posn 50 50)) (cons (make-posn 51 50) (cons (make-posn 49 50) (cons (make-posn 50 51) (cons (make-posn 50 49) '())))))
  92. (check-expect (get-neighbors (make-posn 10 48)) (cons (make-posn 11 48) (cons (make-posn 9 48) (cons (make-posn 10 49) (cons (make-posn 10 47) '())))))
  93.  
  94.  
  95. ;; ================ Exercise 3 ================
  96.  
  97. ; A Cost is one of:
  98. ; - (make-unit Number)
  99. ; - (make-lb Number)
  100. (define-struct unit [cost])
  101. (define-struct lb [cost])
  102. ; and represents either the cost per unit or per lb of an item
  103.  
  104. ; Cost Examples:
  105. (define cost-1 (make-unit 8))
  106. (define cost-2 (make-lb 18))
  107. (define cost-3 (make-unit 10))
  108.  
  109. ; Cost Template:
  110. #; (define (cost-temp cost)
  111.      (cond [(unit? cost) ... (unit-cost cost) ... ]
  112.            [(lb? cost) ... (lb-cost cost) ... ]))
  113.  
  114. ; A CE (CatalogueEntry) is a
  115. ; (make-ce String Cost)
  116. (define-struct ce [name cost])
  117. ; and represents the name of a food and how much it costs
  118. ; CE Examples:
  119. (define ce-1 (make-ce "apple" cost-1))
  120. (define ce-2 (make-ce "ham" cost-2))
  121. (define ce-3 (make-ce "banana" cost-3))
  122. ; CE Template:
  123. #; (define (ce-temp ce)
  124.      (... (ce-name ce) ...
  125.           (ce-cost ce) ...))
  126.  
  127. ; A GC (GroceryCatalogue) is one of:
  128. ; - '()
  129. ; - (cons CE GC)
  130. ; where each catalogue entry has a unique name
  131. ; GC Examples
  132. (define GC-0 '())
  133. (define GC-1 (cons ce-1 GC-0))
  134. (define GC-2 (cons ce-2 GC-1))
  135. (define GC-3 (cons ce-3 GC-2))
  136.  
  137. ; GC Template
  138. #; (define (gc-temp gc)
  139.      (cond [(empty? gc) ... ]
  140.            [(cons? gc) ... (ce-temp (first gc)) ... (gc-temp (rest gc)) ...]))
  141.  
  142. ; An Order is one of:
  143. ; - String
  144. ; - (make-weight String Number)
  145. (define-struct weight [name lb])
  146. ; and represents either one unit of food or its name and weight in lbs
  147. ; Weight Examples:
  148. (define weight-1 (make-weight "jerky" 7))
  149. (define weight-2 (make-weight "pizza rolls" 100))
  150. ; Weight Template:
  151. #; (define (weight-temp weight)
  152.      ... (weight-name weight) ...
  153.      ... (weight-lb weight) ...)
  154.  
  155. ; Order Examples:
  156. (define order-1 "banana")
  157. (define order-2 (make-weight "ham" 20))
  158. ; Order Template
  159. #; (define (order-temp order)
  160.      (cond [(string? order) ...]
  161.            [(weight? order) ... (weight-temp order) ...]))
  162.  
  163. ; A Checkout is one of:
  164. ; - '()
  165. ; - (cons Order Checkout)
  166. ; Checkout Examples:
  167. (define checkout-1 '())
  168. (define checkout-2 (cons order-1 checkout-1))
  169. (define checkout-3 (cons order-2 checkout-2))
  170.  
  171. ;Checkout Template;
  172. #; (define (checkout-temp checkout)
  173.      (cond [(empty? checkout) ...]
  174.            [(cons? checkout) ... (order-temp (first checkout)) ... (checkout-temp (rest checkout))]))
  175.  
  176.  
  177. ;; ================ Exercise 4 ================
  178.  
  179. ; get-cost : String GC -> Cost
  180. ; Gives the cost of a food in a grocery catalog.
  181. (define (get-cost food gc)
  182.   (cond [(empty? gc) (error (string-append "No food by the name of " food " was found!"))]
  183.         [(cons? gc) (if (match-food? food (first gc)) (ce-cost (first gc)) (get-cost food (rest gc)))]))
  184.  
  185. (check-expect (get-cost "banana" GC-3) cost-3)
  186. (check-expect (get-cost "apple" GC-2) cost-1)
  187. (check-error (get-cost "foo" GC-3) "No food by the name of foo was found!")
  188.  
  189. ; match-food? : String CE -> Boolean
  190. ; Checks if the grocery catalogue contains the food.
  191. (define (match-food? food ce)
  192.   (string=? (ce-name ce) food))
  193.  
  194. (check-expect (match-food? "banana" ce-3) #true)
  195. (check-expect (match-food? "potato" ce-3) #false)
  196. (check-expect (match-food? "apple" ce-1) #true)
  197.  
  198. ;; ================ Exercise 5 ================
  199.  
  200.  
  201. ; amend-food-cost String CE Cost -> CE
  202. ; Sets the name inside CE to the new cost if it matches the provided food.
  203. (define (amend-food-cost food ce cost)
  204.   (if (string=? food (ce-name ce)) (make-ce food cost) ce))
  205.  
  206. (check-expect (amend-food-cost "banana" ce-3 cost-1) (make-ce "banana" cost-1))
  207. (check-expect (amend-food-cost "apple" ce-1 cost-3) (make-ce "apple" cost-3))
  208. (check-expect (amend-food-cost "donut" ce-3 cost-3) ce-3)
  209.  
  210.  
  211. ; set-cost : String Cost GC -> GC
  212. ; creates a new Grocery Catalouge with the new food cost
  213. (define (set-cost food cost gc)
  214.   (cond [(empty? gc) gc]
  215.         [(cons? gc) (cons (amend-food-cost food (first gc) cost) (set-cost food cost (rest gc)))]))
  216.  
  217. (check-expect (set-cost "banana" cost-1 GC-3) (cons (make-ce "banana" cost-1) GC-2))
  218. (check-expect (set-cost "apple" cost-2 GC-1) (cons (make-ce "apple" cost-2) GC-0))
  219. (check-expect (set-cost "bread" cost-1 GC-0) GC-0)
  220. (check-expect (set-cost "apple" cost-2 GC-3) (cons ce-3 (cons ce-2 (cons (make-ce "apple" cost-2) '()))))
  221.  
  222.  
  223. ;; ================ Exercise 6 ================
  224.  
  225. ; average-unit-cost : GC -> Number
  226. ; Finds the average cost of the units in the GC
  227. (define (average-unit-cost gc)
  228.   (/ (verify-sum gc) (gc-length gc)))
  229.  
  230. (check-expect (average-unit-cost GC-3) 9)
  231. (check-expect (average-unit-cost GC-1) 8)
  232.  
  233.  
  234. ; verify-sum : GC -> Number
  235. ; Verifies that the sum is not and throws an error otherwise.
  236. (define (verify-sum gc)
  237.   (if (not (= (gc-sum gc) 0)) (gc-sum gc) (error "There are no items priced per unit!")))
  238.  
  239. (check-expect (verify-sum GC-3) 18)
  240. (check-expect (verify-sum GC-1) 8)
  241. (check-error (verify-sum GC-0) "There are no items priced per unit!")
  242.  
  243.  
  244. ; gc-sum : GC -> Number
  245. ; Gets the sum of the cost of the elements in the GC.
  246. (define (gc-sum gc)
  247.   (cond [(empty? gc) 0]
  248.         [(cons? gc) (+ (get-unit-item-sum (first gc)) (gc-sum (rest gc)))]))
  249.  
  250. (check-expect (gc-sum GC-3) 18)
  251. (check-expect (gc-sum GC-1) 8)
  252.  
  253. ; get-unit-item-sum : CE -> Number
  254. ; Returns the cost of the item if the item is priced by unit or 0 otherwise.
  255. (define (get-unit-item-sum ce)
  256.   (cond [(lb? (ce-cost ce)) 0]
  257.         [(unit? (ce-cost ce)) (unit-cost (ce-cost ce))]))
  258.  
  259. (check-expect (get-unit-item-sum ce-3) 10)
  260. (check-expect (get-unit-item-sum ce-2) 0)
  261. (check-expect (get-unit-item-sum ce-1) 8)
  262.  
  263. ; get-unit-item-length : CE -> Number
  264. ; Returns 1 if the item is priced by unit or 0 otherwise.
  265. (define (get-unit-item-length ce)
  266.   (cond [(lb? (ce-cost ce)) 0]
  267.         [(unit? (ce-cost ce)) 1]))
  268.  
  269. (check-expect (get-unit-item-length ce-3) 1)
  270. (check-expect (get-unit-item-length ce-2) 0)
  271. (check-expect (get-unit-item-length ce-1) 1)
  272.  
  273.  
  274. ; gc-length : GC -> Number
  275. ; Gets the amount of elements in the GC.
  276. (define (gc-length gc)
  277.   (cond [(empty? gc) 0]
  278.         [(cons? gc) (+ (get-unit-item-length (first gc)) (gc-length (rest gc)))]))
  279.  
  280. (check-expect (gc-length GC-3) 2)
  281. (check-expect (gc-length GC-2) 1)
  282. (check-expect (gc-length GC-1) 1)
  283.  
  284.  
  285. ;; ================ Exercise 7 ================
  286.  
  287. ; express-lane? : Checkout -> Boolean
  288. ; Determines if a checkout is ten items or fewer.
  289. (define (express-lane? checkout)
  290.   (>= (add-up checkout) 10))
  291.  
  292. (check-expect (express-lane? (make-list 10 "raspberry")) #true)
  293. (check-expect (express-lane? (make-list 22 "egg")) #true)
  294. (check-expect (express-lane? (make-list 3 "jelly")) #false)
  295.  
  296.  
  297. ; add-up : Checkout -> Number
  298. ; Sums up the list of the checkout
  299. (define (add-up checkout)
  300.   (cond [(empty? checkout) 0]
  301.         [(cons? checkout) (+ 1 (add-up (rest checkout)))]))
  302.  
  303. (check-expect (add-up checkout-2) 1)
  304. (check-expect (add-up (make-list 300 "rice")) 300)
  305. (check-expect (add-up (make-list 2 "pear")) 2)
  306.  
  307. ;; ================ Exercise 8 ================
  308.  
  309. ; total-cost : Checkout GC -> NonNegativeNumber
  310. ; Produces the total cost of the items
  311. (define (total-cost checkout gc)
  312.   (cond [(empty? checkout) 0]
  313.         [(cons? checkout) (+ (item-cost (first checkout) gc) (total-cost (rest checkout) gc))]))
  314.  
  315. (check-expect (total-cost checkout-3 GC-3) 370)
  316. (check-expect (total-cost '() GC-3) 0)
  317. (check-error (total-cost checkout-3 GC-1) "No food by the name of ham was found!")
  318. (check-expect (total-cost '() '()) 0)
  319. (check-expect (total-cost checkout-2 GC-3) 10)
  320.  
  321.  
  322. ; item-cost : Order GC -> NonNegativeNumber
  323. ; Calculates the cost of an Order based on the GC
  324. (define (item-cost order gc)
  325.   (cond [(string? order) (search-unit-item order gc)]
  326.         [(weight? order) (search-weight-item order gc)]))
  327.  
  328. (check-expect (item-cost order-1 GC-3) 10)
  329. (check-expect (item-cost order-2 GC-3) 360)
  330. (check-error (item-cost (make-weight "cereal" 5) GC-1) "No food by the name of cereal was found!")
  331.  
  332.  
  333. ; search-unit-item : Order GC -> NNN
  334. ; Determines the cost per unit of an item.
  335. ; Outputs an error if cost is per pound.
  336. (define (search-unit-item order gc)
  337.   (cond [(lb? (get-cost order gc)) (error "This item is supposed to be priced by weight, but instead it was priced by unit.")]
  338.         [(unit? (get-cost order gc)) (unit-cost (get-cost order gc))]))
  339.  
  340. (check-expect (search-unit-item "apple" GC-3) 8)
  341. (check-expect (search-unit-item "banana" GC-3) 10)
  342. (check-error (search-unit-item "ham" GC-3)  "This item is supposed to be priced by weight, but instead it was priced by unit.")
  343.  
  344. ; search-weight-item : Order GC -> NNN
  345. ; Determines the cost per pound of an item.
  346. ; Outputs an error if cost is per unit.
  347. (define (search-weight-item order gc)
  348.   (cond [(unit? (get-cost (weight-name order) gc)) (error "This item is supposed to be priced by unit, but instead it was priced by weight.")]
  349.         [(lb? (get-cost (weight-name order) gc)) (* (lb-cost (get-cost (weight-name order) gc)) (weight-lb order))]))
  350.  
  351. (check-expect (search-weight-item (make-weight "ham" 26) GC-3) 468)
  352. (check-expect (search-weight-item (make-weight "turkey" 89) (cons (make-ce "turkey" (make-lb 3)) '())) 267)
  353. (check-error (search-weight-item (make-weight "apple" 394) GC-3) "This item is supposed to be priced by unit, but instead it was priced by weight.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement