Skip to content

Commit 4a4e58f

Browse files
committed
renumbering dirs
1 parent 797dc92 commit 4a4e58f

File tree

192 files changed

+11854
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

192 files changed

+11854
-0
lines changed

02_crowsnest/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: test
2+
3+
test:
4+
pytest -xv test.py

02_crowsnest/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Crow's Nest
2+
3+
Write a program that will announce the appearance of something "off the larboard bow" to the captain of the ship.
4+
Note that you need to "a" before a word starting with a consonant:
5+
6+
```
7+
$ ./crowsnest.py narwhall
8+
Ahoy, Captain, a narwhall off the larboard bow!
9+
```
10+
11+
Or "an" before a word starting with a vowel:
12+
13+
```
14+
$ ./crowsnest.py octopus
15+
Ahoy, Captain, an octopus off the larboard bow!
16+
```
17+
18+
Given no arguments, the program should print a brief usage:
19+
20+
```
21+
$ ./crowsnest.py
22+
usage: crowsnest.py [-h] str
23+
crowsnest.py: error: the following arguments are required: str
24+
```
25+
26+
It should print a longer usage for `-h` and `--help`:
27+
28+
```
29+
$ ./crowsnest.py -h
30+
usage: crowsnest.py [-h] str
31+
32+
Crow's Nest -- choose the correct article
33+
34+
positional arguments:
35+
str A word
36+
37+
optional arguments:
38+
-h, --help show this help message and exit
39+
```
40+
41+
A passing test suite looks like this:
42+
43+
```
44+
$ make test
45+
pytest -xv test.py
46+
============================= test session starts ==============================
47+
...
48+
collected 6 items
49+
50+
test.py::test_exists PASSED [ 16%]
51+
test.py::test_usage PASSED [ 33%]
52+
test.py::test_consonant PASSED [ 50%]
53+
test.py::test_consonant_upper PASSED [ 66%]
54+
test.py::test_vowel PASSED [ 83%]
55+
test.py::test_vowel_upper PASSED [100%]
56+
57+
============================== 6 passed in 2.89s ===============================
58+
```

02_crowsnest/solution.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
"""Crow's Nest"""
3+
4+
import argparse
5+
6+
7+
# --------------------------------------------------
8+
def get_args():
9+
"""Get command-line arguments"""
10+
11+
parser = argparse.ArgumentParser(
12+
description="Crow's Nest -- choose the correct article",
13+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
14+
15+
parser.add_argument('word', metavar='str', help='A word')
16+
17+
return parser.parse_args()
18+
19+
20+
# --------------------------------------------------
21+
def main():
22+
"""Make a jazz noise here"""
23+
24+
args = get_args()
25+
word = args.word
26+
article = 'an' if word[0].lower() in 'aeiou' else 'a'
27+
28+
print(f'Ahoy, Captain, {article} {word} off the larboard bow!')
29+
30+
31+
# --------------------------------------------------
32+
if __name__ == '__main__':
33+
main()

02_crowsnest/test.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
"""tests for crowsnest.py"""
3+
4+
import os
5+
from subprocess import getstatusoutput, getoutput
6+
7+
prg = './crowsnest.py'
8+
consonant_words = [
9+
'brigatine', 'clipper', 'dreadnought', 'frigate', 'galleon', 'haddock',
10+
'junk', 'ketch', 'longboat', 'mullet', 'narwhal', 'porpoise', 'quay',
11+
'regatta', 'submarine', 'tanker', 'vessel', 'whale', 'xebec', 'yatch',
12+
'zebrafish'
13+
]
14+
vowel_words = ['aviso', 'eel', 'iceberg', 'octopus', 'upbound']
15+
template = 'Ahoy, Captain, {} {} off the larboard bow!'
16+
17+
18+
# --------------------------------------------------
19+
def test_exists():
20+
"""exists"""
21+
22+
assert os.path.isfile(prg)
23+
24+
25+
# --------------------------------------------------
26+
def test_usage():
27+
"""usage"""
28+
29+
for flag in ['-h', '--help']:
30+
rv, out = getstatusoutput(f'{prg} {flag}')
31+
assert rv == 0
32+
assert out.lower().startswith('usage')
33+
34+
35+
# --------------------------------------------------
36+
def test_consonant():
37+
"""brigatine -> a brigatine"""
38+
39+
for word in consonant_words:
40+
out = getoutput(f'{prg} {word}')
41+
assert out.strip() == template.format('a', word)
42+
43+
44+
# --------------------------------------------------
45+
def test_consonant_upper():
46+
"""brigatine -> a Brigatine"""
47+
48+
for word in consonant_words:
49+
out = getoutput(f'{prg} {word.title()}')
50+
assert out.strip() == template.format('a', word.title())
51+
52+
53+
# --------------------------------------------------
54+
def test_vowel():
55+
"""octopus -> an octopus"""
56+
57+
for word in vowel_words:
58+
out = getoutput(f'{prg} {word}')
59+
assert out.strip() == template.format('an', word)
60+
61+
62+
# --------------------------------------------------
63+
def test_vowel_upper():
64+
"""octopus -> an Octopus"""
65+
66+
for word in vowel_words:
67+
out = getoutput(f'{prg} {word.upper()}')
68+
assert out.strip() == template.format('an', word.upper())

03_picnic/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: test
2+
3+
test:
4+
pytest -xv test.py

03_picnic/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Picnic
2+
3+
Write a program that will correctly format the items we're taking on our picnic.
4+
For one item, it should print the one item:
5+
6+
```
7+
$ ./picnic.py sandwiches
8+
You are bringing sandwiches.
9+
```
10+
11+
For two items, place "and" in between:
12+
13+
```
14+
$ ./picnic.py sandwiches chips
15+
You are bringing sandwiches and chips.
16+
```
17+
18+
For three or more items, use commas and "and":
19+
20+
```
21+
$ ./picnic.py sandwiches chips cake
22+
You are bringing sandwiches, chips, and cake.
23+
```
24+
25+
If the `--sorted` flag is present, the items should first be sorted:
26+
27+
```
28+
$ ./picnic.py sandwiches chips cake --sorted
29+
You are bringing cake, chips, and sandwiches.
30+
```
31+
32+
If no items are given, print a brief usage:
33+
34+
```
35+
$ ./picnic.py
36+
usage: picnic.py [-h] [-s] str [str ...]
37+
picnic.py: error: the following arguments are required: str
38+
```
39+
40+
Respond to `-h` and `--help` with a longer usage:
41+
42+
```
43+
$ ./picnic.py -h
44+
usage: picnic.py [-h] [-s] str [str ...]
45+
46+
Picnic game
47+
48+
positional arguments:
49+
str Item(s) to bring
50+
51+
optional arguments:
52+
-h, --help show this help message and exit
53+
-s, --sorted Sort the items (default: False)
54+
```
55+
56+
Run the test suite to ensure your program is correct:
57+
58+
```
59+
$ make test
60+
pytest -xv test.py
61+
============================= test session starts ==============================
62+
...
63+
collected 7 items
64+
65+
test.py::test_exists PASSED [ 14%]
66+
test.py::test_usage PASSED [ 28%]
67+
test.py::test_one PASSED [ 42%]
68+
test.py::test_two PASSED [ 57%]
69+
test.py::test_more_than_two PASSED [ 71%]
70+
test.py::test_two_sorted PASSED [ 85%]
71+
test.py::test_more_than_two_sorted PASSED [100%]
72+
73+
============================== 7 passed in 0.51s ===============================
74+
```

03_picnic/solution.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
"""Picnic game"""
3+
4+
import argparse
5+
6+
7+
# --------------------------------------------------
8+
def get_args():
9+
"""Get command-line arguments"""
10+
11+
parser = argparse.ArgumentParser(
12+
description='Picnic game',
13+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
14+
15+
parser.add_argument('item',
16+
metavar='str',
17+
nargs='+',
18+
help='Item(s) to bring')
19+
20+
parser.add_argument('-s',
21+
'--sorted',
22+
action='store_true',
23+
help='Sort the items')
24+
25+
return parser.parse_args()
26+
27+
28+
# --------------------------------------------------
29+
def main():
30+
"""Make a jazz noise here"""
31+
32+
args = get_args()
33+
items = args.item
34+
num = len(items)
35+
36+
if args.sorted:
37+
items.sort()
38+
39+
bringing = ''
40+
if num == 1:
41+
bringing = items[0]
42+
elif num == 2:
43+
bringing = ' and '.join(items)
44+
else:
45+
items[-1] = 'and ' + items[-1]
46+
bringing = ', '.join(items)
47+
48+
print('You are bringing {}.'.format(bringing))
49+
50+
51+
# --------------------------------------------------
52+
if __name__ == '__main__':
53+
main()

03_picnic/test.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
"""tests for picnic.py"""
3+
4+
import os
5+
from subprocess import getoutput
6+
7+
prg = './picnic.py'
8+
9+
10+
# --------------------------------------------------
11+
def test_exists():
12+
"""exists"""
13+
14+
assert os.path.isfile(prg)
15+
16+
17+
# --------------------------------------------------
18+
def test_usage():
19+
"""usage"""
20+
21+
for flag in ['', '-h', '--help']:
22+
out = getoutput('{} {}'.format(prg, flag))
23+
assert out.lower().startswith('usage')
24+
25+
26+
# --------------------------------------------------
27+
def test_one():
28+
"""one item"""
29+
30+
out = getoutput(f'{prg} chips')
31+
assert out.strip() == 'You are bringing chips.'
32+
33+
34+
# --------------------------------------------------
35+
def test_two():
36+
"""two items"""
37+
38+
out = getoutput(f'{prg} soda "french fries"')
39+
assert out.strip() == 'You are bringing soda and french fries.'
40+
41+
42+
# --------------------------------------------------
43+
def test_more_than_two():
44+
"""more than two items"""
45+
46+
arg = '"potato chips" coleslaw cupcakes "French silk pie"'
47+
out = getoutput(f'{prg} {arg}')
48+
expected = ('You are bringing potato chips, coleslaw, '
49+
'cupcakes, and French silk pie.')
50+
assert out.strip() == expected
51+
52+
# --------------------------------------------------
53+
def test_two_sorted():
54+
"""two items sorted output"""
55+
56+
out = getoutput(f'{prg} -s soda candy')
57+
assert out.strip() == 'You are bringing candy and soda.'
58+
59+
# --------------------------------------------------
60+
def test_more_than_two_sorted():
61+
"""more than two items sorted output"""
62+
63+
arg = 'bananas apples dates cherries'
64+
out = getoutput(f'{prg} {arg} --sorted')
65+
expected = ('You are bringing apples, bananas, cherries, and dates.')
66+
assert out.strip() == expected

04_jump_the_five/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: test
2+
3+
test:
4+
pytest -xv test.py

0 commit comments

Comments
 (0)