Skip to content

Commit 1913bcc

Browse files
committed
Add 10_quiz_limited example for testing quiz functions
1 parent 0247451 commit 1913bcc

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

examples/10_quiz_limited/quiz3.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Quiz 3.
2+
3+
Name: Some Student
4+
"""
5+
6+
7+
def sum_positive(numbers):
8+
"""Return the sum of all positive numbers in a list.
9+
10+
Args:
11+
numbers (list): Zero or more integers.
12+
13+
Returns:
14+
int: Sum of the positive numbers.
15+
16+
Example:
17+
>>> sum_positive([1, -2, 3, -4, 5])
18+
9
19+
>>> sum_positive([-1, -2, -3])
20+
0
21+
"""
22+
total = 0
23+
for n in numbers:
24+
if n > 0:
25+
total += n
26+
return total
27+
28+
29+
def first_upper(text):
30+
"""Return the first uppercase letter in a string.
31+
32+
Args:
33+
text (str): The string to search.
34+
35+
Returns:
36+
str or None: First uppercase letter, or None.
37+
38+
Example:
39+
>>> first_upper("hello World")
40+
'W'
41+
>>> first_upper("python")
42+
"""
43+
for c in text:
44+
if c.isupper():
45+
return c
46+
return None
47+
48+
49+
if __name__ == "__main__":
50+
import doctest
51+
doctest.testmod()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Limited feedback is shown to students during the quiz. Before grading, the
2+
# instructor clicks "Regrade All Submissions" to show the complete results.
3+
4+
import quiz3
5+
from jmu_pytest_utils.meta import submission_closed
6+
from jmu_pytest_utils.quiz import check_docstring, check_return_types
7+
8+
9+
def sum_positive(numbers):
10+
# Wrapper to avoid "from import" errors.
11+
return quiz3.sum_positive(numbers)
12+
13+
14+
def first_upper(text):
15+
# Wrapper to avoid "from import" errors.
16+
return quiz3.first_upper(text)
17+
18+
19+
def test_docs():
20+
check_docstring(quiz3)
21+
22+
23+
def test_types(tmp_path):
24+
check_return_types(
25+
(int, sum_positive, ([1, -2, 3, -4, 5],)),
26+
(str, first_upper, ("hello World",)),
27+
)
28+
29+
30+
if submission_closed():
31+
32+
def test_sum_positive():
33+
"""sum_positive: typical cases"""
34+
assert sum_positive([1, -2, 3, -4, 5]) == 9
35+
assert sum_positive([-1, -2, -3]) == 0
36+
assert sum_positive([0, 0, 0]) == 0
37+
assert sum_positive([10, 20, 30]) == 60
38+
39+
def test_sum_positive_edge():
40+
"""sum_positive: edge cases"""
41+
assert sum_positive([]) == 0
42+
assert sum_positive([-5, 5]) == 5
43+
assert sum_positive([-1, 2, -3, 4]) == 6
44+
45+
def test_first_upper():
46+
"""first_upper: typical cases"""
47+
assert first_upper("helloWorld") == "W"
48+
assert first_upper("Python") == "P"
49+
assert first_upper("aBcDe") == "B"
50+
51+
def test_first_upper_lower():
52+
"""first_upper: all lowercase"""
53+
assert first_upper("python") is None
54+
assert first_upper("") is None
55+
56+
def test_first_upper_edge():
57+
"""first_upper: edge cases"""
58+
# Edge cases
59+
assert first_upper("123ABC") == "A"
60+
assert first_upper("!@#") is None

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The following "assignments" are used for testing `jmu_pytest_utils` and provided
1111
* [7_class_package](7_class_package) -- classes and packages
1212
* [8_test_coverage](8_test_coverage) -- grading student's tests
1313
* [9_leaderboard](9_leaderboard) -- Gradescope leaderboard
14+
* [10_quiz_limited](10_quiz_limited) -- feedback during a quiz
1415

1516
## API Docs
1617

0 commit comments

Comments
 (0)