|
| 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 | +# def sum13(nums): |
| 47 | +# """ |
| 48 | +# using a comprehension to filter out the 13s |
| 49 | + |
| 50 | +# - more tests should pass, but not all. |
| 51 | +# """ |
| 52 | +# return sum(n for n in nums if n!=13) |
| 53 | + |
| 54 | + |
| 55 | +# def sum13(nums): |
| 56 | +# """ |
| 57 | +# darn -- comprehension can't handle the "after a 13" case |
| 58 | + |
| 59 | +# do it from scratch with while loop |
| 60 | + |
| 61 | +# fails the two 13s in a row test! |
| 62 | +# """ |
| 63 | +# total = 0 |
| 64 | +# i = 0 |
| 65 | +# while i < len(nums): |
| 66 | +# if nums[i] != 13: |
| 67 | +# total += nums[i] |
| 68 | +# else: |
| 69 | +# i += 1 |
| 70 | +# i += 1 |
| 71 | +# return total |
| 72 | + |
| 73 | + |
| 74 | +# def sum13(nums): |
| 75 | +# """ |
| 76 | +# Use a for loop, and keep track of the previous 13 |
| 77 | + |
| 78 | +# passes all tests! |
| 79 | +# """ |
| 80 | +# print(nums) |
| 81 | +# total = 0 |
| 82 | +# prev_13 = False |
| 83 | +# for i, n in enumerate(nums): |
| 84 | +# if n == 13: |
| 85 | +# prev_13 = True |
| 86 | +# continue |
| 87 | +# elif prev_13: |
| 88 | +# prev_13 = False |
| 89 | +# continue |
| 90 | +# else: |
| 91 | +# total += n |
| 92 | +# return total |
| 93 | + |
| 94 | + |
| 95 | +# def sum13(nums): |
| 96 | +# """ |
| 97 | +# Use the iterator protocol -- nifty? but not any simpler really. |
| 98 | + |
| 99 | +# Fails for repeated 13 in middle |
| 100 | + |
| 101 | +# Works with any iterable, so that's nice. |
| 102 | +# """ |
| 103 | +# total = 0 |
| 104 | +# nums_i = iter(nums) |
| 105 | +# for n in nums_i: |
| 106 | +# if n != 13: |
| 107 | +# total += n |
| 108 | +# else: |
| 109 | +# try: |
| 110 | +# next(nums_i) |
| 111 | +# # this is necessary for the case where there's a 13 at the end. |
| 112 | +# except StopIteration: |
| 113 | +# break |
| 114 | +# return total |
0 commit comments