Submission Details
Last week we had lots of fun making an autograded quiz with lots of different question types. But what about more fun quizzes where the result is an outcome rather than some specific score? Think a BuzzFeed-style-personality quiz. That’s what we’ll work on this week with the help of our new associative data structure the Hash Map!
Our main goals are to:
- Understand the concept of Key-Value maps (namely the hash map).
- Create your own Buzzfeed-style Personality Quiz!
Just like last week, we’re going to take an object-oriented approach to creating our personality quiz. However, here we no longer have a concept of “correctness.” Instead each question contributes to a possible outcome (e.g. “What would your Patronus be?”, “Which character from The West Wing are you most like?”). The quiz then should return the outcome with the highest “vote” after you’ve answered all the questions.
The sample quiz included template file includes a fun quiz titled “Which Stranger Things kid are you?”
Screenshot of Finished Quiz
Part 1. The fun-question Type
Since our questions in this quiz won’t have correct answers, we’ll need a new struct which we’ll call fun-question. The template file already contains the entire implementation for this struct.
Example
; a fun-question is a ...
; - (make-fun-question string (listof string) (listof string))
; If a user answers with a particular choice, you can think of that as a vote
; for a particular outcome (the result of the quiz as a whole).
(define-struct fun-question (text choices outcomes)
#:methods
...)
(define q1
(make-fun-question
"Choose a favorite snack:"
(list "Ice Cream" "Reese's Pieces" "Red Vines" "Eggo Waffles" "Snickers" "Mac & Cheese")
(list "Max" "Will" "Lucas" "Eleven" "Dustin" "Mike")))
Note: thatoutcomes in each question could be ordered differently.
A fun-question object contains the question text, the options that the user may choose, and the voting outcomes for those corresponding options. In the example above, q1 contains the various choices of snacks ((listof string)) and the voting outcomes are the list of character names ((listof string)). When the user enters 5, it means that the user has chosen “Snickers” and the corresponding outcome is “Dustin”. When taking the quiz, the choices (but not the outcomes) will be printed (see earlier screenshot).
fun-question has two methods that might be helpful to look at: choice-ref (which allows one to lookup the chosen option in the list of options) and outcome-ref (which allows one to lookup the corresponding outcome for a given choice).
There’s nothing to complete for this section. Just wanted to make sure you read it!
Part 2. The quiz Type
The quiz struct will keep track of votes using a Hash Map by mapping keys of outcomes to values that represent the vote counts for that outcome. The struct will have four attributes:
| Name | Type | Description |
|---|---|---|
title | string | the title of the quiz |
questions | (listOf fun-question) | the list of questions for the quiz |
possible-outcomes | (listOf string) | the list of possible outcomes (like “Eleven”, “Max”, etc.) |
scoring-of-outcomes | (hashOf string number) | a Hash Map that associates possible outcome with a vote count |
Example
This example is included in the template file:
(define my-quiz
(make-quiz "Which Stranger Things kid are you?"
(list q1 q2 q3)
(list "Eleven" "Lucas" "Max" "Will" "Mike" "Dustin")
(make-hash)))
Your job is to implement three methods for this struct (the actual struct def is already included).
Hash Map Cheat Sheet
Believe it or not, you'll only need a Hash Map constructor, accessor, and mutator. You can see the type signatures and purpose statements of these functions on the slides from last week. Here's a direct link to the slide. You can also see some examples in the lecture files for that day!
Activity 2.1. reset-scoring-hash
; reset-scoring-hash : quiz -> (void)
; For each outcome in possible-outcomes, sets the value associated it
; in the hash table in scoring-of-outcomes to 0.
; Effect: scoring-of-outcomes property has mutated, all values set to 0.
The reset-scoring-hash method takes a quiz and sets all of the vote counts in the scoring-of-outcomes Hash Map and sets them to 0s (i.e. sets all the values for every key to 0).
Example
For the same
(make-hash (list (list "Eleven" 0) (list "Lucas" 0) (list "Max" 0)
(list "Will" 0) (list "Mike" 0) (list "Dustin" 0)))
Activity 2.2. update-scoring-hash
; update-scoring-hash : quiz string -> void
; Increments the scoring-of-outcomes hash, adding one to the value
; associated with the given outcome.
; Effect: scoring-of-outcomes property has mutated, incrementing value
; associated with the given outcome by 1.
The updates-scoring-hash method takes a quiz and an outcome (a string), and increments the count of the vote associated with the given outcome.
Example
(begin
(reset-scoring-hash testquiz) ; set all votes to 0
(update-scoring-hash testquiz "Lucas") ; 1 vote for Lucas
(update-scoring-hash testquiz "Eleven") ; 1 vote for Eleven
(update-scoring-hash testquiz "Eleven") ; 1 more vote for Eleven (2 total)
(update-scoring-hash testquiz "Max") ; 1 vote for Max
(update-scoring-hash testquiz "Dustin") ; 1 vote for Dustin
(update-scoring-hash testquiz "Eleven")) ; 1 more vote for Eleven (3 total)
(make-hash (list (list "Eleven" 3) (list "Lucas" 1) (list "Max" 1)
(list "Will" 0) (list "Mike" 0) (list "Dustin" 1)))
Note: you can assume
reset-scoring-hash will have been called before any calls toupdate-scoring-hash will be made when the quiz itself is actually run.
Activity 2.3. get-scoring-outcome
; get-scoring-outcome : quiz -> String
; Iterates over the scoring-of-outcomes hash to find and return the
; outcome with the highest associated value.
The get-scoring-outcome method takes a quiz and outputs the outcome associated with the highest vote count.
Example
Calling
Note: If you have multiple “max vote” outcomes (i.e. a tie), just return one of them as the definitive result. Don’t worry about tie breaks.
Part 3. Creating your own Quiz
Believe it or not…that’s it for the programming part. In the template, the Stranger Things character “fun quiz” is provided as an example called
- Be stored in the
my-quizvariable - Have at least 4
fun-questions - Each
fun-questionmust have at least 4 choices and 4 outcomes - It cannot be the template quiz. You need to write your own. Checkout some online personality quizzes if you need some inspiration. Otherwise, just pick your favorite piece of fiction (be it novel, video game, show, movie, musical, etc.) and come up with your own questions!
Make sure to take your quiz by calling (runquiz my-quiz) in the Interactions Window (or uncomment it in the definitions window).
Note: Yes indeed this assignment is shorter than usual. Use the extra time to study for the quiz or complete your travels safely. Happy Thanksgiving!
Double Checking your Work
Make sure you’ve followed the process outlined in the introduction for every function, and that you’ve thoroughly tested your functions! The included examples would be a good place to start.
Before turning your assignment in, run the file one last time to make sure that it runs properly and doesn’t generate any exceptions, and all the tests pass. Make sure you’ve also spent some time writing your OWN check-expect calls to test your code.
Assuming they do, submit only your exercise_8.rkt file on Canvas.