Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e759712

Browse files
committed
Add solution for 11_bottles
1 parent ee991b6 commit e759712

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

11_bottles_of_beer/bottles.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Ryan Alcoran <r.alcoran>
4+
Date : 2021-03-22
5+
Purpose: 11_Bottles
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Bottles of beer song',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('-n',
20+
'--num',
21+
help='How many Bottles',
22+
metavar='number',
23+
type=int,
24+
default=10)
25+
26+
args = parser.parse_args()
27+
if args.num < 1:
28+
parser.error(f'--num "{args.num}" must be greater than 0')
29+
30+
return args
31+
32+
33+
# --------------------------------------------------
34+
def verse(num):
35+
if num > 2:
36+
bottle = 'bottles'
37+
next_bottle = f'{num - 1} bottles'
38+
elif num == 2:
39+
bottle = 'bottles'
40+
next_bottle = f'1 bottle'
41+
else:
42+
bottle = 'bottle'
43+
next_bottle = f'No more bottles'
44+
l1 = f'{num} {bottle} of beer on the wall,\n'
45+
l2 = f'{num} {bottle} of beer,\n'
46+
l3 = f'Take one down, pass it around,\n'
47+
l4 = f'{next_bottle} of beer on the wall!'
48+
49+
return l1 + l2 + l3 + l4
50+
51+
52+
# --------------------------------------------------
53+
def test_verse():
54+
"""Test verse"""
55+
56+
last_verse = verse(1)
57+
assert last_verse == '\n'.join([
58+
'1 bottle of beer on the wall,', '1 bottle of beer,',
59+
'Take one down, pass it around,',
60+
'No more bottles of beer on the wall!'
61+
])
62+
63+
two_bottles = verse(2)
64+
assert two_bottles == '\n'.join([
65+
'2 bottles of beer on the wall,', '2 bottles of beer,',
66+
'Take one down, pass it around,', '1 bottle of beer on the wall!'
67+
])
68+
69+
70+
# --------------------------------------------------
71+
def main():
72+
"""Make a jazz noise here"""
73+
74+
args = get_args()
75+
number = args.num
76+
for i in list(range(number, 0, -1)):
77+
print(verse(i), end='\n' * (2 if i > 1 else 1))
78+
79+
80+
# --------------------------------------------------
81+
if __name__ == '__main__':
82+
main()

0 commit comments

Comments
 (0)