Coding Challenge 8: The last coding challenge

Assigned
Monday, 6 April 2026
Summary
In this coding challenge we practice working with dictionaries, higher-order procedures, and vectors.
Collaboration
Normal collaboration policies for coding challenges apply to this assignment. See the syllabus for specifics.

Instructions

We are providing you with starter code, CC8-template.scm. You should download this file and upload the given code to a new .scm file named LASTNAME-last.scm where you replace LASTNAME with your last name.

You are required to document every procedure that you write using the documentation style of our course. Tests are optional, but likely helpful in determining if your code is working as desired.

At the top of your file, make sure to include your name, and any acknowledgements according to our collaboration policy. Now is a great time to review those collaboration policies.

Problem 1: Selective vector updates

Write a procedure, (vector-selective-map! vec pred? proc), that updates all the elements in vec for which pred? holds by applying proc and leaves the other elements the same. That is, vector-selective-map! is like map, but for vectors and for only for some of the members of vec.

> (define numbers (vector 1 2 3 4 5 6 7 8 9))
> (vector-selective-map! numbers odd? square)
> numbers
(vector 1 2 9 4 25 6 49 8 81)

Note that the vector can have mixed values. If the vector has mixed values, there may be limits on what predicates will work, as some of the following examples suggest. You can assume that the predicate will work on each element of the list.

> (define stuff (vector "a" 2 "bee" (list "c" 2 3) 15 "more"))
> stuff
(vector "a" 2 "bee" (list "c" 2 3) 15 "more")
> (vector-selective-map! stuff number? square)
> stuff
(vector "a" 4 "bee" (list "c" 2 3) 225 "more")
> (vector-selective-map! stuff (lambda (x) (and (number? x) (odd? x))) (section + _ 1))
> stuff
(vector "a" 4 "bee" (list "c" 2 3) 226 "more")
> (vector-selective-map! stuff
                         (lambda (val) 
                           (and (string? val) (char=? (string-ref val 0) #\b)))
                         string-upcase)
> stuff
(vector "a" 4 "BEE" (list "c" 2 3) 226 "more")
> (vector-selective-map! stuff string? (section substring _ 0 2))
> stuff
(vector "a" 4 "BE" ("c" 2 3) 226 "mo")
> (vector-selective-map! stuff odd? square)
error!

Problem 2: Design your own inventory

On a recent lab, we created a letter inventory to keep track of the frequencies of alphabetic characters in strings. In this problem, you will create your own inventory, using an application of your choice.

Your solution here should minimally include

  • a way to make an empty inventory
  • a procedure (update-inventory inv {??}) which increments a value by one.
  • a procedure (make-inventory {??}) which creates an inventory from given input data.
  • examples of running make-inventory on different inputs.

For example, I might create an inventory of weather events behaving in the following way:

> empty-inventory
(list (pair "rain" 0)
	(pair "snow" 0)
	(pair "sun" 0)
	(pair "tornado" 0)
	(pair "hurricane" 0))
> (update-inventory empty-inventory "snow")
(list (pair "rain" 0)
	(pair "snow" 1)
	(pair "sun" 0)
	(pair "tornado" 0)
	(pair "hurricane" 0))
> (make-inventory (list "rain" "snow" "hurricane" "sharknado" "sun" "sun" "sun" "hurricane"))
(list (pair "rain" 1)
	(pair "snow 1)
	(pair "sun" 3)
	(pair "tornado" 0)
	(pair "hurricane" 2))

Notice the following:

  • In my design solution I opted to not add new weather events to my inventory if they appear in the data, you may choose to make other design choices
  • In my update-inventory procedure, I expect the second parameter to be a string, depending on your application, your choice may be different.
  • In my make-inventory procedure, I expect the second parameter to be a list of strings, depending on your application, your choice may be different.

Your documentation for these procedures should make the parameter types very clear, as well as the design choices you made (i.e. does a new element get added if it’s not already in the inventory?).

Your solution should rely on the association-list procedures, assoc-key?, assoc-ref, and assoc-set whenever possible.

Be creative! Here are some other ideas of what your inventory could track:

  • The frequency of digits in numbers like 3.14159265358979323846264
  • Votes in an election
  • Wellnes metrics such as “number of minutes walking”, “number of minutes reading”, “number of glasses of water consumed”

Submission guidelines

Submit your .scm file to Gradescope, using the name according to the top of these instructions.

Grading rubric

In grading your submission, we will look for the following at each level. Note that if a criteria does not pass a lower level, we will likely not check for criteria at the higher levels. We may also identify other characteristics that move your work between levels.

You should read through the rubric and verify that your submission meets the rubric.

Redo or above

Submissions that lack any of these characteristics will get an N.

[] Includes the specified file (correctly named).
[] Includes an appropriate header on the file that indicates the course, author, acknowledgements, etc.
[] Acknowledges appropriately.
[] Code runs in scamper.

Meets expectations or above

Submissions that lack any of these characteristics but have all of the prior characteristics will get an R.

[] Code is well-formatted with appropriate names and indentation.
[] Code submission is organized with comments to indicate the start of new problems.
[] Solution for problem 1 does not use lists, `vector-filter`, or `vector-append`
[] All of our tests pass for problem 1.
[] Solution for problem 2 includes the two procedures `update-inventory` and `make-inventory`
[] Solution for problem 2 includes several examples of running the code. 
[] Documentation in the 151 style is included for all code, and contains correct information. Documentation is especially important in problem 2, where you are designing your own code. 

Exemplary / Exceeds expectations

Submissions that lack any of these characteristics but have all of the prior characteristics will get an M.

[] All code is exceptionally organized and easy to read, through the use of comments (to explain the purpose of different pieces of the code), decomposition, and highly intuitive naming choices.
[] Problem 1 includes a wide variety of tests which cover edge cases beyond those presented in the assignment. 
[] Application in Problem 2 is especially unique or interesting