Skip to content

Commit 100c9a8

Browse files
authored
added unit test example / lesson
a unit testing example -- starter code.
1 parent 4f7fd89 commit 100c9a8

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

Sources/code/test_sum_13.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
"""
2+
3+
Test driven development:
4+
5+
6+
Example from Coding Bat: List-2 > sum13
7+
8+
https://codingbat.com/prob/p167025
9+
10+
Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.
11+
12+
13+
sum13([1, 2, 2, 1]) → 6
14+
sum13([1, 1]) → 2
15+
sum13([1, 2, 2, 1, 13]) → 6
16+
sum13([1, 2, 2, 1]) → 6
17+
sum13([1, 1]) → 2
18+
sum13([1, 2, 2, 1, 13]) → 6
19+
sum13([1, 2, 13, 2, 1, 13]) → 4
20+
sum13([13, 1, 2, 13, 2, 1, 13]) → 3
21+
sum13([]) → 0
22+
sum13([13]) → 0
23+
sum13([13, 13]) → 0
24+
sum13([13, 0, 13]) → 0
25+
sum13([13, 1, 13]) → 0
26+
sum13([5, 7, 2]) → 14
27+
sum13([5, 13, 2]) → 5
28+
sum13([0]) → 0
29+
sum13([13, 0]) → 0
30+
"""
31+
32+
import pytest
33+
34+
# def sum13(nums):
35+
# """
36+
# non-functional -- but the tests will run (and fail)
37+
# """
38+
# return None
39+
40+
# def sum13(nums):
41+
# """
42+
# simple sum -- no special handling of 13 -- should pass some tests.
43+
# """
44+
# return sum(nums)
45+
46+
47+
# def sum13(nums):
48+
# """
49+
# using a comprehension to filter out the 13s
50+
51+
# - more tests should pass, but not all.
52+
# """
53+
# return sum(n for n in nums if n!=13)
54+
55+
56+
# def sum13(nums):
57+
# """
58+
# darn -- comprehension can't handle the "after a 13" case
59+
60+
# do it from scratch with while loop
61+
62+
# fails the two 13s in a row test!
63+
# """
64+
# total = 0
65+
# i = 0
66+
# while i < len(nums):
67+
# if nums[i] != 13:
68+
# total += nums[i]
69+
# else:
70+
# i += 1
71+
# i += 1
72+
# return total
73+
74+
75+
# def sum13(nums):
76+
# """
77+
# Use a for loop, and keep track of the previous 13
78+
79+
# passes all tests!
80+
# """
81+
# print(nums)
82+
# total = 0
83+
# prev_13 = False
84+
# for i, n in enumerate(nums):
85+
# if n == 13:
86+
# prev_13 = True
87+
# continue
88+
# elif prev_13:
89+
# prev_13 = False
90+
# continue
91+
# else:
92+
# total += n
93+
# return total
94+
95+
96+
def sum13(nums):
97+
"""
98+
Use the iterator protocol -- nifty? but not any simpler really.
99+
100+
Fails for repeated 13 in middle
101+
102+
Works with any iterable, so that's nice.
103+
"""
104+
total = 0
105+
nums_i = iter(nums)
106+
for n in nums_i:
107+
if n != 13:
108+
total += n
109+
else:
110+
try:
111+
next(nums_i)
112+
# this is necessary for the case where there's a 13 at the end.
113+
except StopIteration:
114+
break
115+
return total
116+
117+
# Using the nifty pytest.parametrize, so we only have to write one test
118+
119+
test_data = [
120+
([1, 2, 2, 1], 6),
121+
([1, 1], 2),
122+
([1, 2, 2, 1, 13], 6),
123+
([1, 2, 2, 1], 6),
124+
([1, 1], 2),
125+
([1, 2, 2, 1, 13], 6),
126+
([1, 2, 13, 2, 1, 13], 4),
127+
([13, 1, 2, 13, 2, 1, 13], 3),
128+
([], 0),
129+
([13], 0),
130+
([13, 13], 0),
131+
([13, 0, 13], 0),
132+
([13, 1, 13], 0),
133+
([5, 7, 2], 14),
134+
([5, 13, 2], 5),
135+
([0], 0),
136+
([13, 0], 0),
137+
# These are not part of original test suite
138+
# ([3, 13, 13, 2, 5], 8),
139+
# (iter([13, 1, 2, 13, 2, 1, 13]), 3), # Does it work with an iterable?
140+
]
141+
142+
@pytest.mark.parametrize('nums, result', test_data)
143+
def test_sum13(nums, result):
144+
assert sum13(nums) == result
145+
146+
147+
148+

0 commit comments

Comments
 (0)