This is individual work. You are not allowed to spend more than 8 hours on this test. This includes any reading that you do to help you solve the problems.
Use helper functions as needed.
All your functions must have documentation with the signature (contract)
showing the types of
parameters and results and a function description. All functions must have check-expect
tests.
When defining your own structures, clearly explain what all the fields mean.
The problems provide some test cases, but you need to write more.
Comments, helpful variable names, and good code style and formatting
are a very important part of your grade. You don't have to look for the
shortest solution, but you should eliminate unnecessary code repetition if
possible (use helper functions).
You may use any of the following higher-order functions map, filter, foldr, foldl, sort
, as well as anonymous functions. However, all of the problems may be solved using direct recursion (some problems require recursive helper functions).
You are given a definition of a structure representing a shopping list item:
(define-struct item [name category dollars cents weight])
Here name
is the name of the product, such as "Apples", category
is one of "Meat", "Dairy", "Fruit", "Vegetable", "Other", dollars
and cents
are two non-negative integers that form the cost, and weight
is a positive real number (assumed to be in pounds). For instance, the following structure
(make-item "Apples" "Fruit" 3 55 1.2)
represents 1.2 lb of apples (an item in the category "Fruit") that costs $3.55. The cost is given for the entire amount, it's not the price per lb.
The following list will be used in the check-expects below:
(define shop-list
(list
(make-item "Apples" "Fruit" 3 55 1.2)
(make-item "Chicken" "Meat" 5 27 2)
(make-item "Avocado" "Vegetable" 1 00 0.25)
(make-item "Tomatoes" "Vegetable" 2 99 1.09)
(make-item "Greek yogurt" "Dairy" 3 50 0.5)))
You need to write the following functions on lists of shopping items:
total-cost
that, given a list of items, computes its total cost in cents:
(check-expect (total-cost shop-list) 1631)
change
that, given a shopping list and an amount (in whole dollars), returns a string that shows how much change you will get back:
(check-expect (change shop-list 20) "Your change is 3 dollars and 69 cents")
(check-expect (change (list (make-item "Potato chips" "Other" 2 00 0.1)) 2) "Your change is 0 dollars and 00 cents")
Note that this function doesn't need to be recursive and can use the previous function as a helper function. select-category
that takes the list and a category and returns all items on the list that are in that category:
(check-expect (select-category shop-list "Vegetable") (list
(make-item "Avocado" "Vegetable" 1 00 0.25)
(make-item "Tomatoes" "Vegetable" 2 99 1.09)))
heaviest
that, given a non-empty list of items, returns the item with the largest weight:
(check-expect (heaviest shop-list) (make-item "Chicken" "Meat" 5 27 2))
Don't worry about an empty list for this problem.
buy-what-you-can
that takes a shopping list, amount in dollars, and amount in cents, and returns the part of the list (starting in the beginning) for which the total cost is within the given amount.
(check-expect (buy-what-you-can shop-list 9 99)
(list
(make-item "Apples" "Fruit" 3 55 1.2)
(make-item "Chicken" "Meat" 5 27 2)
(make-item "Avocado" "Vegetable" 1 00 0.25))
sort-by-cost-per-lb
that takes a list of items and sorts them by cost-per-pound, in decreasing order. Use insertion sort as a model for your sorting, or use a predefined sort
function.
$2.99
At the bottom of the page the total is displayed, in the same format. Submit all your files as attachements in an email to me. The subject of the email must be Midterm 2. Please include your name and a task number at the beginning of the file, in a comment. In your email message please include any comments about what works and what doesn't, as well as thoughts on how you would've continued if you had more time (if you run out of time). Also please include the total time you spent on the test.