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