Skip to content

Commit ea4f991

Browse files
committed
Problem 14 solution
1 parent 15ecbcc commit ea4f991

File tree

2 files changed

+86
-20
lines changed

2 files changed

+86
-20
lines changed

14_rhymer/rhymer.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-06-29
5+
Purpose: Make rhyming "words"
6+
"""
7+
8+
import argparse
9+
import re
10+
11+
12+
# --------------------------------------------------
13+
def get_args():
14+
"""Get command-line arguments"""
15+
16+
parser = argparse.ArgumentParser(
17+
description='Make rhyming "words"',
18+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
19+
)
20+
21+
parser.add_argument("str", metavar="str", help="A word to rhyme")
22+
23+
return parser.parse_args()
24+
25+
26+
# --------------------------------------------------
27+
def stemmer(word: str):
28+
"""Break a word into its leading consonants and the remainder"""
29+
pattern = re.compile(r"^([^aeiou]*)(.*)$")
30+
return pattern.search(word.lower()).groups()
31+
32+
33+
# --------------------------------------------------
34+
def test_stemmer():
35+
"""Test stemmer"""
36+
assert stemmer("") == ("", "")
37+
assert stemmer("cake") == ("c", "ake")
38+
assert stemmer("chair") == ("ch", "air")
39+
assert stemmer("APPLE") == ("", "apple")
40+
assert stemmer("RDNZL") == ("rdnzl", "")
41+
42+
43+
# --------------------------------------------------
44+
def main():
45+
"""Make a jazz noise here"""
46+
47+
consonants = list("bcdfghjklmnpqrstvwxyz")
48+
clusters = """
49+
bl br ch cl cr dr fl fr gl gr pl pr sc
50+
sh sk sl sm sn sp st sw th tr tw thw wh wr
51+
sch scr shr sph spl spr squ str thr""".split()
52+
prefixes = sorted(consonants + clusters)
53+
54+
args = get_args()
55+
word = args.str
56+
start, rest = stemmer(word)
57+
58+
if rest:
59+
print("\n".join([p + rest for p in prefixes if p != start]))
60+
else:
61+
print(f'Cannot rhyme "{word}"')
62+
63+
64+
# --------------------------------------------------
65+
if __name__ == "__main__":
66+
main()

14_rhymer/test.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,70 @@
55
import random
66
from subprocess import getoutput
77

8-
prg = './rhymer.py'
8+
PRG = "./rhymer.py"
99

1010

1111
# --------------------------------------------------
1212
def test_exists():
1313
"""exists"""
1414

15-
assert os.path.isfile(prg)
15+
assert os.path.isfile(PRG)
1616

1717

1818
# --------------------------------------------------
1919
def test_usage():
2020
"""usage"""
2121

22-
for flag in ['', '-h', '--help']:
23-
out = getoutput(f'{prg} {flag}')
24-
assert out.lower().startswith('usage')
22+
for flag in ["", "-h", "--help"]:
23+
out = getoutput(f"python {PRG} {flag}")
24+
assert out.lower().startswith("usage")
2525

2626

2727
# --------------------------------------------------
2828
def test_take():
2929
"""leading consonant"""
3030

31-
out = getoutput(f'{prg} take').splitlines()
31+
out = getoutput(f"python {PRG} take").splitlines()
3232
assert len(out) == 56
33-
assert out[0] == 'bake'
34-
assert out[-1] == 'zake'
33+
assert out[0] == "bake"
34+
assert out[-1] == "zake"
3535

3636

3737
# --------------------------------------------------
3838
def test_chair():
3939
"""consonant cluster"""
4040

41-
out = getoutput(f'{prg} chair').splitlines()
41+
out = getoutput(f"python {PRG} chair").splitlines()
4242
assert len(out) == 56
43-
assert out[1] == 'blair'
44-
assert out[-2] == 'yair'
43+
assert out[1] == "blair"
44+
assert out[-2] == "yair"
4545

4646

4747
# --------------------------------------------------
4848
def test_chair_uppercase():
4949
"""consonant cluster"""
5050

51-
out = getoutput(f'{prg} CHAIR').splitlines()
51+
out = getoutput(f"python {PRG} CHAIR").splitlines()
5252
assert len(out) == 56
53-
assert out[1] == 'blair'
54-
assert out[-2] == 'yair'
53+
assert out[1] == "blair"
54+
assert out[-2] == "yair"
5555

5656

5757
# --------------------------------------------------
5858
def test_apple():
5959
"""leading vowel"""
6060

61-
out = getoutput(f'{prg} apple').splitlines()
61+
out = getoutput(f"python {PRG} apple").splitlines()
6262
assert len(out) == 57
63-
assert out[10] == 'flapple'
64-
assert out[-10] == 'thwapple'
63+
assert out[10] == "flapple"
64+
assert out[-10] == "thwapple"
6565

6666

6767
# --------------------------------------------------
6868
def test_no_vowels():
6969
"""no vowels"""
7070

71-
consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
72-
bad = ''.join(random.sample(consonants, k=random.randint(4, 10)))
73-
out = getoutput(f'{prg} {bad}')
71+
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
72+
bad = "".join(random.sample(consonants, k=random.randint(4, 10)))
73+
out = getoutput(f"python {PRG} {bad}")
7474
assert out == f'Cannot rhyme "{bad}"'

0 commit comments

Comments
 (0)