diff options
Diffstat (limited to 'challenge-323/packy-anderson/python')
| -rwxr-xr-x | challenge-323/packy-anderson/python/ch-1.py | 33 | ||||
| -rwxr-xr-x | challenge-323/packy-anderson/python/ch-2.py | 62 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-323/packy-anderson/python/ch-1.py b/challenge-323/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..5cbccba4aa --- /dev/null +++ b/challenge-323/packy-anderson/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def incDec(operations): + x = 0 + old_x = 0 + o = '' + explain = [] + for op in operations: + x = x + 1 if "++" in op else x - 1 + o = '+' if "++" in op else '-' + explain.append( + 'Operation "{}" => {:2d} {} 1 => {:2d}'.format( + op, old_x, o, x + ) + ) + old_x = x + return x, "\n".join(explain) + +def solution(operations): + display = '"' + '", "'.join(operations) + '"' + print(f'Input: @operations = ({display})') + output, explain = incDec(operations) + print(f'Output: {output}\n\n{explain}') + + +print('Example 1:') +solution(["--x", "x++", "x++"]) + +print('\nExample 2:') +solution(["x++", "++x", "x++"]) + +print('\nExample 3:') +solution(["x++", "++x", "--x", "x--"]) diff --git a/challenge-323/packy-anderson/python/ch-2.py b/challenge-323/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..c5c646192d --- /dev/null +++ b/challenge-323/packy-anderson/python/ch-2.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +def fmt(num): + if num > 0: + # display to 100ths place + return '{:0.2f}'.format(num) + else: + # format it as a string + return f"{int(num)}" + +def taxAmt(income, tax): + if income == 0: + return 0, "" # special case + + last_max = 0 + total = 0 + operations = [] + subtotals = [] + for max_income, rate in tax: + # adjust the maximum amount at this rate + # to be relative to the last rate + max_income -= last_max + # the amount to be taxed at this rate + amt = min(income, max_income) + tax = amt * rate/100 + total += tax + # make sure we display to 100ths place + # if we hace a non-zero tax amount + tax = fmt(tax) + # save the steps so we can display them at the end + operations.append(f"({amt} * {rate}/100)") + subtotals.append(tax) + # we've just taxed $amt, so remove it from $income + income -= amt + # adjust the last rate for the next loop + last_max += max_income + # bail if there's no more income + if income <= 0: break + + return ( + total, + "Total Tax => " + ' + '.join(operations) + "\n" + + " => " + ' + '.join(subtotals) + "\n" + + " => " + fmt(total) + ) + +def solution(income, tax): + brackets = [ f"[{t[0]},{t[1]}]" for t in tax ] + display = ", ".join(brackets) + print(f'Input: $income = {income}, @tax = ({display})') + output, explain = taxAmt(income, tax) + print(f'Output: {output}\n\n{explain}') + + +print('Example 1:') +solution(10, [[3, 50], [7, 10], [12,25]]) + +print('\nExample 2:') +solution(2, [[1, 0], [4, 25], [5,50]]) + +print('\nExample 3:') +solution(0, [[2, 50],]) |
