Skip to content

Commit d27dbb6

Browse files
committed
bacronym discussion
1 parent 5d289f5 commit d27dbb6

File tree

8 files changed

+237015
-166
lines changed

8 files changed

+237015
-166
lines changed

bacronym/Makefile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
.PHONY: pdf test
2-
3-
pdf:
4-
pandoc README.md -o README.pdf
1+
.PHONY: test
52

63
test:
7-
pytest -v test.py
4+
pytest -xv test.py bacronym.py

bacronym/README.md

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Bacronym
22

3-
Write a Python program called `bacronym.py` that takes a string like "FBI" and retrofits some `-n|--number` (default `5`) of acronyms by reading a `-w|--wordlist` argument (defualt `/usr/share/dict/words`), skipping over words to `-e|--exclude` (default `a, an, the`) and randomly selecting words that start with each of the letters. Be sure to include a `-s|--seed` argument (default `None`) to pass to `random.seed` for the test suite.
3+
Write a Python program called `bacronym.py` that takes a string like "FBI" and retrofits some `-n|--num` (default `5`) of acronyms by reading a `-w|--wordlist` argument (default `/usr/share/dict/words`), skipping over words to `-e|--exclude` (default `a, an, the`) and randomly selecting words that start with each of the letters. Be sure to include a `-s|--seed` argument (default `None`) to pass to `random.seed` for the test suite.
4+
5+
If provided the `-h|--help` flags or no arguments, the program should print a usage:
46

57
````
68
$ ./bacronym.py
7-
usage: bacronym.py [-h] [-n NUM] [-w STR] [-x STR] [-s INT] STR
9+
usage: bacronym.py [-h] [-n NUM] [-w STR] [-x STR [STR ...]] [-s INT] STR
810
bacronym.py: error: the following arguments are required: STR
911
$ ./bacronym.py -h
10-
usage: bacronym.py [-h] [-n NUM] [-w STR] [-x STR] [-s INT] STR
12+
usage: bacronym.py [-h] [-n NUM] [-w STR] [-x STR [STR ...]] [-s INT] STR
1113
1214
Explain acronyms
1315
@@ -19,14 +21,90 @@ optional arguments:
1921
-n NUM, --num NUM Maximum number of definitions (default: 5)
2022
-w STR, --wordlist STR
2123
Dictionary/word file (default: /usr/share/dict/words)
22-
-x STR, --exclude STR
23-
List of words to exclude (default: a,an,the)
24+
-x STR [STR ...], --exclude STR [STR ...]
25+
List of words to exclude (default: ['a', 'an', 'the'])
2426
-s INT, --seed INT Random seed (default: None)
25-
$ ./bacronym.py FBI -s 1
27+
````
28+
29+
Because I'm including a `--seed` argumment, you should get this exact output if using the same `--wordlist`:
30+
31+
````
32+
$ ./bacronym.py -s 1 FBI
2633
FBI =
2734
- Fecundity Brokage Imitant
2835
- Figureless Basketmaking Ismailite
2936
- Frumpery Bonedog Irregardless
3037
- Foxily Blastomyces Inedited
3138
- Fastland Bouncingly Idiospasm
3239
````
40+
41+
The program should create errors and usage for `--num` less than 1:
42+
43+
````
44+
$ ./bacronym.py -n -3 AAA
45+
usage: bacronym.py [-h] [-n NUM] [-w STR] [-x STR [STR ...]] [-s INT] STR
46+
bacronym.py: error: --num "-3" must be > 0
47+
````
48+
49+
And for a bad `--wordlist`:
50+
51+
````
52+
$ ./bacronym.py -w mnvdf AAA
53+
usage: bacronym.py [-h] [-n NUM] [-w STR] [-x STR [STR ...]] [-s INT] STR
54+
bacronym.py: error: argument -w/--wordlist: can't open 'mnvdf': \
55+
[Errno 2] No such file or directory: 'mnvdf'
56+
````
57+
58+
The acronym must be composed entirely of letters:
59+
60+
````
61+
$ ./bacronym.py 666
62+
usage: bacronym.py [-h] [-n NUM] [-w STR] [-x STR [STR ...]] [-s INT] STR
63+
bacronym.py: error: Acronym "666" must be >1 in length, only use letters
64+
````
65+
66+
And be greater than 1 character in length:
67+
68+
````
69+
$ ./bacronym.py A
70+
usage: bacronym.py [-h] [-n NUM] [-w STR] [-x STR [STR ...]] [-s INT] STR
71+
bacronym.py: error: Acronym "A" must be >1 in length, only use letters
72+
````
73+
74+
Hints:
75+
76+
* See how much error checking you can put into the `get_args` function and use `parser.error` to throw the errors
77+
* The `--wordlist` need not be a system dictionary file with one lower-case word on each line. Assume that you can read any file with many words on each line and that might include punctuation. I suggest you use a regualar expression to remove anything that is not an alphabet character with `re.sub('[^a-z]', '')`. Be sure that words are only represented once in your list.
78+
* In my version, I write two important functions: one (`group_words`) that reads the wordlist and returns a grouping of words by their first letter, and another (`make_definitions`) that produces plausible definitions from that grouping of words by letters for a given acronym. I place the following test functions into my program and run `pytest` to verify that the functions work properly.
79+
80+
````
81+
def test_group_words():
82+
"""Test group_words()"""
83+
84+
words = io.StringIO('apple, "BANANA," The Coconut! Berry; A cabbage.')
85+
stop = 'a an the'.split()
86+
words_by_letter = group_words(words, stop)
87+
assert words_by_letter['a'] == ['apple']
88+
assert words_by_letter['b'] == ['banana', 'berry']
89+
assert words_by_letter['c'] == ['coconut', 'cabbage']
90+
assert 't' not in words_by_letter
91+
92+
def test_make_definitions():
93+
"""Test make_definitions()"""
94+
95+
words = {
96+
'a': ['apple'],
97+
'b': ['banana', 'berry'],
98+
'c': ['coconut', 'cabbage']
99+
}
100+
101+
random.seed(1)
102+
assert make_definitions('ABC', words) == ['Apple Banana Cabbage']
103+
random.seed(2)
104+
assert make_definitions('ABC', words) == ['Apple Banana Coconut']
105+
random.seed(3)
106+
assert make_definitions('AAA', words) == ['Apple Apple Apple']
107+
random.seed(4)
108+
assert make_definitions('YYZ', words) == ['? ? ?']
109+
random.seed(None)
110+
````

0 commit comments

Comments
 (0)