diff options
| -rw-r--r-- | challenge-323/steven-wilson/python/ch-1.py | 29 | ||||
| -rw-r--r-- | challenge-323/steven-wilson/python/ch-2.py | 30 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-323/steven-wilson/python/ch-1.py b/challenge-323/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..88020f8772 --- /dev/null +++ b/challenge-323/steven-wilson/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + + +def increment_decrement(operations): + """ Given a list of operations, return the final value after performing the + given operations in order. The initial value is always 0. + + >>> increment_decrement(["--x", "x++", "x++"]) + 1 + >>> increment_decrement(["x++", "++x", "x++"]) + 3 + >>> increment_decrement(["x++", "++x", "--x", "x--"]) + 0 + """ + increment = {"++x", "x++"} + decrement = {"--x", "x--"} + value = 0 + for op in operations: + if op in increment: + value += 1 + elif op in decrement: + value -= 1 + return value + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) diff --git a/challenge-323/steven-wilson/python/ch-2.py b/challenge-323/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..2bb7797e8e --- /dev/null +++ b/challenge-323/steven-wilson/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + + +def tax_amount(income, tax): + """Given an income amount and tax brackets, calculate the total tax amount. + + >>> tax_amount(10, [[3, 50], [7, 10], [12,25]]) + 2.65 + >>> tax_amount(2, [[1, 0], [4, 25], [5,50]]) + 0.25 + >>> tax_amount(0, [[2, 50]]) + 0 + """ + lower = 0 + bracket_amount = [] + for t in tax: + if income >= t[0]: + bracket_amount.append(t[0] - lower) + elif income > lower: + bracket_amount.append(income - lower) + else: + break + lower = t[0] + return sum(amount * (percent[1] / 100) for amount, percent in zip(bracket_amount, tax)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
