|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Coin combos for value""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +from itertools import product |
| 6 | +from functools import partial |
| 7 | + |
| 8 | + |
| 9 | +# -------------------------------------------------- |
| 10 | +def get_args(): |
| 11 | + """Get command-line arguments""" |
| 12 | + |
| 13 | + parser = argparse.ArgumentParser( |
| 14 | + description='Coin combos for value', |
| 15 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 16 | + |
| 17 | + parser.add_argument('value', metavar='int', type=int, help='Sum') |
| 18 | + |
| 19 | + args = parser.parse_args() |
| 20 | + |
| 21 | + if not 0 < args.value <= 100: |
| 22 | + parser.error('value "{}" must be > 1 and <= 100'.format(args.value)) |
| 23 | + |
| 24 | + return args |
| 25 | + |
| 26 | + |
| 27 | +# -------------------------------------------------- |
| 28 | +def main(): |
| 29 | + """Make a jazz noise here""" |
| 30 | + |
| 31 | + args = get_args() |
| 32 | + value = args.value |
| 33 | + nickels = range((value // 5) + 1) |
| 34 | + dimes = range((value // 10) + 1) |
| 35 | + quarters = range((value // 25) + 1) |
| 36 | + fig = partial(figure, value) |
| 37 | + combos = [c for c in map(fig, product(nickels, dimes, quarters)) if c] |
| 38 | + |
| 39 | + print('If you give me {} cent{}, I can give you:'.format( |
| 40 | + value, '' if value == 1 else 's')) |
| 41 | + |
| 42 | + for i, combo in enumerate(combos, 1): |
| 43 | + print('{:3}: {}'.format(i, fmt_combo(combo))) |
| 44 | + |
| 45 | + |
| 46 | +# -------------------------------------------------- |
| 47 | +def fmt_combo(combo): |
| 48 | + """English version of combo""" |
| 49 | + |
| 50 | + out = [] |
| 51 | + for coin, val in zip(('quarter', 'dime', 'nickel', 'penny'), combo): |
| 52 | + if val: |
| 53 | + plural = 'pennies' if coin == 'penny' else coin + 's' |
| 54 | + out.append('{} {}'.format(val, coin if val == 1 else plural)) |
| 55 | + |
| 56 | + return ', '.join(out) |
| 57 | + |
| 58 | + |
| 59 | +# -------------------------------------------------- |
| 60 | +def figure(value, coins): |
| 61 | + """ |
| 62 | + If there is a valid combo of 'coins' in 'value', |
| 63 | + return a tuple of ints for (quarters, dimes, nickels, pennies) |
| 64 | + """ |
| 65 | + |
| 66 | + nickels, dimes, quarters = coins |
| 67 | + big_coins = (5 * nickels) + (10 * dimes) + (25 * quarters) |
| 68 | + |
| 69 | + if big_coins <= value: |
| 70 | + return (quarters, dimes, nickels, value - big_coins) |
| 71 | + |
| 72 | + |
| 73 | +# -------------------------------------------------- |
| 74 | +if __name__ == '__main__': |
| 75 | + main() |
0 commit comments