Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; ================ Exercise 2 ================
- ; A ListofPosns (LoP) is
- ; - '()
- ; - (cons Posn LoP)
- ; Examples
- (define lop1 '())
- (define lop2 (cons (make-posn 0 1) lop1))
- (define lop3 (cons (make-posn 3 3) lop2))
- (define lop4 (cons (make-posn 2 2) lop3))
- ; Template
- #; (define (lop-temp lop)
- (cond [(empty? lop) ...]
- [(cons? lop) ... (posn-temp (first lop)) ... (lop-temp (rest lop)) ...]))
- ; A Direction is one of
- ; - (make-posn 0 1)
- ; - (make-posn 0 -1)
- ; - (make-posn 1 0)
- ; - (make-posn -1 0)
- ; A direction represents a direction on a unit circle with the origin as the center.
- ; Direction Examples:
- (define NORTH (make-posn 0 1))
- (define SOUTH (make-posn 0 -1))
- (define EAST (make-posn 1 0))
- (define WEST (make-posn -1 0))
- ; Template
- #; (define (direction-temp direction)
- (cond [(equal? direction NORTH) ...]
- [(equal? direction SOUTH) ...]
- [(equal? direction EAST) ...]
- [(equal? direction WEST) ...]))
- ; neighbor : Posn Direction -> Posn
- ; Gets the posn's neighbor to a specific direction.
- (define (neighbor posn dir)
- (make-posn (+ (posn-x posn) (posn-x dir)) (+ (posn-y posn) (posn-y dir))))
- (check-expect (neighbor (make-posn 50 50) NORTH) (make-posn 50 51))
- (check-expect (neighbor (make-posn 50 50) SOUTH) (make-posn 50 49))
- (check-expect (neighbor (make-posn 50 50) EAST) (make-posn 51 50))
- (check-expect (neighbor (make-posn 50 50) WEST) (make-posn 49 50))
- ; valid-neighbor? : Posn -> Boolean
- ; Checks to see if a Posn is a valid neighbor.
- (define (valid-neighbor? posn)
- (and (>= (posn-x posn) 0) (<= (posn-x posn) 99) (>= (posn-y posn) 0) (<= (posn-y posn) 99)))
- (check-expect (valid-neighbor? (make-posn 0 0)) #true)
- (check-expect (valid-neighbor? (make-posn 99 99)) #true)
- (check-expect (valid-neighbor? (make-posn 0 100)) #false)
- (check-expect (valid-neighbor? (make-posn 100 0)) #false)
- (check-expect (valid-neighbor? (make-posn -1 0)) #false)
- (check-expect (valid-neighbor? (make-posn 0 -1)) #false)
- ; get-neighbor-list : Posn -> LoP
- ; Gets east, west, north, and south posns.
- (define (get-neighbor-list posn)
- (cons (neighbor posn EAST) (cons (neighbor posn WEST) (cons (neighbor posn NORTH) (cons (neighbor posn SOUTH) '())))))
- (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) '())))))
- (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) '())))))
- (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) '())))))
- ; get-valid-neighbors : LoP -> LoP
- ; Removes any Posns that do not fall in the domain [0, 99] from the LoP.
- (define (get-valid-neighbors lop)
- (cond [(empty? lop) lop]
- [(cons? lop) (if (valid-neighbor? (first lop)) (cons (first lop) (get-valid-neighbors (rest lop))) (get-valid-neighbors (rest lop)))]))
- (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) '())))))
- (cons (make-posn 51 50) (cons (make-posn 49 50) (cons (make-posn 50 51) (cons (make-posn 50 49) '())))))
- (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) '())))))
- (cons (make-posn 98 99) (cons (make-posn 99 98) '())))
- (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) '())))))
- (cons (make-posn 1 0) (cons (make-posn 0 1) '())))
- ; get-neighbors : Posn -> LoP
- ; Gets all neighbors of a posn that fall in the domain [0, 99].
- (define (get-neighbors posn)
- (get-valid-neighbors (get-neighbor-list posn)))
- (check-expect (get-neighbors (make-posn 0 0)) (cons (make-posn 1 0) (cons (make-posn 0 1) '())))
- (check-expect (get-neighbors (make-posn 99 99)) (cons (make-posn 98 99) (cons (make-posn 99 98) '())))
- (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) '())))))
- (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) '())))))
- ;; ================ Exercise 3 ================
- ; A Cost is one of:
- ; - (make-unit Number)
- ; - (make-lb Number)
- (define-struct unit [cost])
- (define-struct lb [cost])
- ; and represents either the cost per unit or per lb of an item
- ; Cost Examples:
- (define cost-1 (make-unit 8))
- (define cost-2 (make-lb 18))
- (define cost-3 (make-unit 10))
- ; Cost Template:
- #; (define (cost-temp cost)
- (cond [(unit? cost) ... (unit-cost cost) ... ]
- [(lb? cost) ... (lb-cost cost) ... ]))
- ; A CE (CatalogueEntry) is a
- ; (make-ce String Cost)
- (define-struct ce [name cost])
- ; and represents the name of a food and how much it costs
- ; CE Examples:
- (define ce-1 (make-ce "apple" cost-1))
- (define ce-2 (make-ce "ham" cost-2))
- (define ce-3 (make-ce "banana" cost-3))
- ; CE Template:
- #; (define (ce-temp ce)
- (... (ce-name ce) ...
- (ce-cost ce) ...))
- ; A GC (GroceryCatalogue) is one of:
- ; - '()
- ; - (cons CE GC)
- ; where each catalogue entry has a unique name
- ; GC Examples
- (define GC-0 '())
- (define GC-1 (cons ce-1 GC-0))
- (define GC-2 (cons ce-2 GC-1))
- (define GC-3 (cons ce-3 GC-2))
- ; GC Template
- #; (define (gc-temp gc)
- (cond [(empty? gc) ... ]
- [(cons? gc) ... (ce-temp (first gc)) ... (gc-temp (rest gc)) ...]))
- ; An Order is one of:
- ; - String
- ; - (make-weight String Number)
- (define-struct weight [name lb])
- ; and represents either one unit of food or its name and weight in lbs
- ; Weight Examples:
- (define weight-1 (make-weight "jerky" 7))
- (define weight-2 (make-weight "pizza rolls" 100))
- ; Weight Template:
- #; (define (weight-temp weight)
- ... (weight-name weight) ...
- ... (weight-lb weight) ...)
- ; Order Examples:
- (define order-1 "banana")
- (define order-2 (make-weight "ham" 20))
- ; Order Template
- #; (define (order-temp order)
- (cond [(string? order) ...]
- [(weight? order) ... (weight-temp order) ...]))
- ; A Checkout is one of:
- ; - '()
- ; - (cons Order Checkout)
- ; Checkout Examples:
- (define checkout-1 '())
- (define checkout-2 (cons order-1 checkout-1))
- (define checkout-3 (cons order-2 checkout-2))
- ;Checkout Template;
- #; (define (checkout-temp checkout)
- (cond [(empty? checkout) ...]
- [(cons? checkout) ... (order-temp (first checkout)) ... (checkout-temp (rest checkout))]))
- ;; ================ Exercise 4 ================
- ; get-cost : String GC -> Cost
- ; Gives the cost of a food in a grocery catalog.
- (define (get-cost food gc)
- (cond [(empty? gc) (error (string-append "No food by the name of " food " was found!"))]
- [(cons? gc) (if (match-food? food (first gc)) (ce-cost (first gc)) (get-cost food (rest gc)))]))
- (check-expect (get-cost "banana" GC-3) cost-3)
- (check-expect (get-cost "apple" GC-2) cost-1)
- (check-error (get-cost "foo" GC-3) "No food by the name of foo was found!")
- ; match-food? : String CE -> Boolean
- ; Checks if the grocery catalogue contains the food.
- (define (match-food? food ce)
- (string=? (ce-name ce) food))
- (check-expect (match-food? "banana" ce-3) #true)
- (check-expect (match-food? "potato" ce-3) #false)
- (check-expect (match-food? "apple" ce-1) #true)
- ;; ================ Exercise 5 ================
- ; amend-food-cost String CE Cost -> CE
- ; Sets the name inside CE to the new cost if it matches the provided food.
- (define (amend-food-cost food ce cost)
- (if (string=? food (ce-name ce)) (make-ce food cost) ce))
- (check-expect (amend-food-cost "banana" ce-3 cost-1) (make-ce "banana" cost-1))
- (check-expect (amend-food-cost "apple" ce-1 cost-3) (make-ce "apple" cost-3))
- (check-expect (amend-food-cost "donut" ce-3 cost-3) ce-3)
- ; set-cost : String Cost GC -> GC
- ; creates a new Grocery Catalouge with the new food cost
- (define (set-cost food cost gc)
- (cond [(empty? gc) gc]
- [(cons? gc) (cons (amend-food-cost food (first gc) cost) (set-cost food cost (rest gc)))]))
- (check-expect (set-cost "banana" cost-1 GC-3) (cons (make-ce "banana" cost-1) GC-2))
- (check-expect (set-cost "apple" cost-2 GC-1) (cons (make-ce "apple" cost-2) GC-0))
- (check-expect (set-cost "bread" cost-1 GC-0) GC-0)
- (check-expect (set-cost "apple" cost-2 GC-3) (cons ce-3 (cons ce-2 (cons (make-ce "apple" cost-2) '()))))
- ;; ================ Exercise 6 ================
- ; average-unit-cost : GC -> Number
- ; Finds the average cost of the units in the GC
- (define (average-unit-cost gc)
- (/ (verify-sum gc) (gc-length gc)))
- (check-expect (average-unit-cost GC-3) 9)
- (check-expect (average-unit-cost GC-1) 8)
- ; verify-sum : GC -> Number
- ; Verifies that the sum is not and throws an error otherwise.
- (define (verify-sum gc)
- (if (not (= (gc-sum gc) 0)) (gc-sum gc) (error "There are no items priced per unit!")))
- (check-expect (verify-sum GC-3) 18)
- (check-expect (verify-sum GC-1) 8)
- (check-error (verify-sum GC-0) "There are no items priced per unit!")
- ; gc-sum : GC -> Number
- ; Gets the sum of the cost of the elements in the GC.
- (define (gc-sum gc)
- (cond [(empty? gc) 0]
- [(cons? gc) (+ (get-unit-item-sum (first gc)) (gc-sum (rest gc)))]))
- (check-expect (gc-sum GC-3) 18)
- (check-expect (gc-sum GC-1) 8)
- ; get-unit-item-sum : CE -> Number
- ; Returns the cost of the item if the item is priced by unit or 0 otherwise.
- (define (get-unit-item-sum ce)
- (cond [(lb? (ce-cost ce)) 0]
- [(unit? (ce-cost ce)) (unit-cost (ce-cost ce))]))
- (check-expect (get-unit-item-sum ce-3) 10)
- (check-expect (get-unit-item-sum ce-2) 0)
- (check-expect (get-unit-item-sum ce-1) 8)
- ; get-unit-item-length : CE -> Number
- ; Returns 1 if the item is priced by unit or 0 otherwise.
- (define (get-unit-item-length ce)
- (cond [(lb? (ce-cost ce)) 0]
- [(unit? (ce-cost ce)) 1]))
- (check-expect (get-unit-item-length ce-3) 1)
- (check-expect (get-unit-item-length ce-2) 0)
- (check-expect (get-unit-item-length ce-1) 1)
- ; gc-length : GC -> Number
- ; Gets the amount of elements in the GC.
- (define (gc-length gc)
- (cond [(empty? gc) 0]
- [(cons? gc) (+ (get-unit-item-length (first gc)) (gc-length (rest gc)))]))
- (check-expect (gc-length GC-3) 2)
- (check-expect (gc-length GC-2) 1)
- (check-expect (gc-length GC-1) 1)
- ;; ================ Exercise 7 ================
- ; express-lane? : Checkout -> Boolean
- ; Determines if a checkout is ten items or fewer.
- (define (express-lane? checkout)
- (>= (add-up checkout) 10))
- (check-expect (express-lane? (make-list 10 "raspberry")) #true)
- (check-expect (express-lane? (make-list 22 "egg")) #true)
- (check-expect (express-lane? (make-list 3 "jelly")) #false)
- ; add-up : Checkout -> Number
- ; Sums up the list of the checkout
- (define (add-up checkout)
- (cond [(empty? checkout) 0]
- [(cons? checkout) (+ 1 (add-up (rest checkout)))]))
- (check-expect (add-up checkout-2) 1)
- (check-expect (add-up (make-list 300 "rice")) 300)
- (check-expect (add-up (make-list 2 "pear")) 2)
- ;; ================ Exercise 8 ================
- ; total-cost : Checkout GC -> NonNegativeNumber
- ; Produces the total cost of the items
- (define (total-cost checkout gc)
- (cond [(empty? checkout) 0]
- [(cons? checkout) (+ (item-cost (first checkout) gc) (total-cost (rest checkout) gc))]))
- (check-expect (total-cost checkout-3 GC-3) 370)
- (check-expect (total-cost '() GC-3) 0)
- (check-error (total-cost checkout-3 GC-1) "No food by the name of ham was found!")
- (check-expect (total-cost '() '()) 0)
- (check-expect (total-cost checkout-2 GC-3) 10)
- ; item-cost : Order GC -> NonNegativeNumber
- ; Calculates the cost of an Order based on the GC
- (define (item-cost order gc)
- (cond [(string? order) (search-unit-item order gc)]
- [(weight? order) (search-weight-item order gc)]))
- (check-expect (item-cost order-1 GC-3) 10)
- (check-expect (item-cost order-2 GC-3) 360)
- (check-error (item-cost (make-weight "cereal" 5) GC-1) "No food by the name of cereal was found!")
- ; search-unit-item : Order GC -> NNN
- ; Determines the cost per unit of an item.
- ; Outputs an error if cost is per pound.
- (define (search-unit-item order gc)
- (cond [(lb? (get-cost order gc)) (error "This item is supposed to be priced by weight, but instead it was priced by unit.")]
- [(unit? (get-cost order gc)) (unit-cost (get-cost order gc))]))
- (check-expect (search-unit-item "apple" GC-3) 8)
- (check-expect (search-unit-item "banana" GC-3) 10)
- (check-error (search-unit-item "ham" GC-3) "This item is supposed to be priced by weight, but instead it was priced by unit.")
- ; search-weight-item : Order GC -> NNN
- ; Determines the cost per pound of an item.
- ; Outputs an error if cost is per unit.
- (define (search-weight-item order gc)
- (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.")]
- [(lb? (get-cost (weight-name order) gc)) (* (lb-cost (get-cost (weight-name order) gc)) (weight-lb order))]))
- (check-expect (search-weight-item (make-weight "ham" 26) GC-3) 468)
- (check-expect (search-weight-item (make-weight "turkey" 89) (cons (make-ce "turkey" (make-lb 3)) '())) 267)
- (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