CSci 1301: Takehome 2

Due: Tuesday, November 22nd at 11:59pm by e-mail (no late work accepted)

Total: 40 points

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).

The problem description: shopping list

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:

  1. Write a function total-cost that, given a list of items, computes its total cost in cents:
      
          (check-expect (total-cost shop-list) 1631) 
        
  2. Write a function 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.
    If the amount isn't enough to pay the cost of the shopping list, return a string "Not enough money".
  3. Write a 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))) 
        
  4. Write a function 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.
  5. Write a function 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))
        
  6. Write a function 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.
  7. Develop a world program in which the world state is the list of items. The items are displayed as a vertically ordered list. Only the name of the item and its cost are displayed. The format of the cost is $2.99 At the bottom of the page the total is displayed, in the same format.
    The height of an item is fixed, and there may be no more than 9 items in the given list.
  8. Add a key handler that, when a number key is pressed, removes the corresponding item from the list. For instance, when "2" is pressed, the second item is removed, and the items are shifted up. The total changes accordingly.
  9. Add a click handler so that when an item is clicked (based on its y coordinate only), it is removed from the list.

Happy programming!

What to submit

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.


CSci 1301 course web site.