Skip to content

Commit

Permalink
add dictionary interface to matchq
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Apr 6, 2022
1 parent a4a5e96 commit 9e8f2a7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ answer = (1,3,2) # indices of correct
matchq(questions, choices, answer)
```

The above shows that the number of choices need not match the number of questions. When they do, a dictionary can be used to specify the choices and the answers will be computed:

```@example quiz_question
d = Dict("Select a Volvo" => "XC90", "Select a Mercedes" => "GLE 350",
"Select an Audi" => "A4")
matchq(d)
```

----

Currently only a few question types are available:
Expand Down
22 changes: 19 additions & 3 deletions src/question_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ end

"""
matchq(questions, choices, answers; label="", hint="")
matchq(d::Dictionary; label="", hint="")
Use a drop down to select the right match for each question.
Expand All @@ -182,30 +183,45 @@ Arguments:
* `answers`: collection of correct indices for each question.
* `d`: As an alternative, a dictionary of questions and answers can be specified. The choices will be taken from the values then randomized, the answers will be computed.
* `label`: optional label for the form element
* `hint`: optional plain-text hint that can be seen on hover
Example:
## Examples:
```
questions = ("Select a Volvo", "Select a Mercedes", "Select an Audi")
choices = ("XC90", "A4", "GLE 350", "X1") # may be more than questions
answer = (1,3,2) # indices of correct
matchq(questions, choices, answer)
d = Dict("Select a Volvo" => "XC90", "Select a Mercedes" => "GLE 250")
matchq(d)
```
"""
function matchq(questions, choices, answers;
label="", hint="", inline::Bool=(hint!=""),
keep_order::Bool=false)
label="", hint="")

@assert length(questions) == length(answers)

Matchq(questions, choices, answers,
label, hint)
end

function matchq(d::AbstractDict; kwargs...)

questions = collect(keys(d))
choices = collect(values(d))
n = length(questions)
inds = shuffle(1:n)

matchq(questions, choices[inds], sortperm(inds); kwargs...)
end



"""
booleanq(ans; [label, hint])
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ using Test
@test radioq(1:3, 1, keep_order=true).answer == 1

@test multiq(("one","two","three"), (2, 3), label="Primes?").label == "Primes?"

d = Dict("Select a Volvo" => "XC90", "Select a Mercedes" => "GLE 350",
"Select an Audi" => "A4")
r = matchq(d)
i = rand(1:3)
@test d[r.questions[i]] == r.choices[r.answer[i]]

end

0 comments on commit 9e8f2a7

Please sign in to comment.