Skip to content

Commit a1813a0

Browse files
committed
change
1 parent cbb9466 commit a1813a0

3 files changed

Lines changed: 139 additions & 0 deletions

File tree

appendix/argparse/choices.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
"""Choices"""
3+
4+
import argparse
5+
6+
parser = argparse.ArgumentParser(
7+
description='Choices',
8+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
9+
10+
parser.add_argument('color', metavar='str', help='Color', choices=['red', 'yellow', 'blue'])
11+
12+
args = parser.parse_args()
13+
14+
print('color =', args.color)

coin_combos/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# First Bank of Change
2+
3+
Write a Python program that will figure out all the different combinations of pennies, nickels, dimes, and quarters in a given `value` provided as a single positional argument. The value must be greater than 0 and less than or equal to 100.
4+
5+
````
6+
$ ./combos.py
7+
usage: combos.py [-h] int
8+
combos.py: error: the following arguments are required: int
9+
$ ./combos.py -h
10+
usage: combos.py [-h] int
11+
12+
Coin combos for value
13+
14+
positional arguments:
15+
int Sum
16+
17+
optional arguments:
18+
-h, --help show this help message and exit
19+
$ ./combos.py 1
20+
If you give me 1 cent, I can give you:
21+
1: 1 penny
22+
$ ./combos.py 4
23+
If you give me 4 cents, I can give you:
24+
1: 4 pennies
25+
$ ./combos.py 6
26+
If you give me 6 cents, I can give you:
27+
1: 6 pennies
28+
2: 1 nickel, 1 penny
29+
$ ./combos.py 13
30+
If you give me 13 cents, I can give you:
31+
1: 13 pennies
32+
2: 1 dime, 3 pennies
33+
3: 1 nickel, 8 pennies
34+
4: 2 nickels, 3 pennies
35+
$ ./combos.py 27
36+
If you give me 27 cents, I can give you:
37+
1: 27 pennies
38+
2: 1 quarter, 2 pennies
39+
3: 1 dime, 17 pennies
40+
4: 2 dimes, 7 pennies
41+
5: 1 nickel, 22 pennies
42+
6: 1 dime, 1 nickel, 12 pennies
43+
7: 2 dimes, 1 nickel, 2 pennies
44+
8: 2 nickels, 17 pennies
45+
9: 1 dime, 2 nickels, 7 pennies
46+
10: 3 nickels, 12 pennies
47+
11: 1 dime, 3 nickels, 2 pennies
48+
12: 4 nickels, 7 pennies
49+
13: 5 nickels, 2 pennies
50+
````

coin_combos/solution.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)