Skip to content

Commit b8d1d85

Browse files
committed
Problem 15 solution
1 parent bb612c3 commit b8d1d85

File tree

3 files changed

+218
-14
lines changed

3 files changed

+218
-14
lines changed

15_kentucky_friar/friar.py

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-06-29
5+
Purpose: Southern fry text
6+
"""
7+
8+
import argparse
9+
import os
10+
import re
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""Get command-line arguments"""
16+
17+
parser = argparse.ArgumentParser(
18+
description="Southern fry text",
19+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
20+
)
21+
22+
parser.add_argument("text", metavar="text", help="Input text or file")
23+
24+
args = parser.parse_args()
25+
if os.path.isfile(args.text):
26+
with open(args.text, "rt", encoding="utf-8") as fh:
27+
args.text = fh.read().rstrip()
28+
29+
return args
30+
31+
32+
# --------------------------------------------------
33+
def drop_gs(text):
34+
"""Drop the g's from two-syllable texts ending in 'ing'"""
35+
pattern = re.compile(r"\b(\w*[aeiou]+\w*in)g\b", flags=re.I)
36+
return pattern.sub(r"\1'", text)
37+
38+
39+
# --------------------------------------------------
40+
def test_drop_gs():
41+
"""Test drop_gs function"""
42+
assert drop_gs("") == ""
43+
assert drop_gs("sing") == "sing"
44+
assert drop_gs("cooking") == "cookin'"
45+
assert drop_gs("SINGING") == "SINGIN'"
46+
47+
48+
def test_drop_gs_sentence():
49+
"""Test drop_gs function for full lines of text"""
50+
text = "That's some good looking chicken."
51+
expected_output = "That's some good lookin' chicken."
52+
assert drop_gs(text) == expected_output
53+
54+
55+
def test_drop_gs_first_word():
56+
"""Test drop_gs function for first word of sentence"""
57+
text = "Looking good, toots."
58+
expected_output = "Lookin' good, toots."
59+
assert drop_gs(text) == expected_output
60+
61+
62+
def test_drop_gs_punctuation():
63+
"""Test drop_gs function for words followed by punctuation"""
64+
text = "Hey good looking, what's cooking?"
65+
expected_output = "Hey good lookin', what's cookin'?"
66+
assert drop_gs(text) == expected_output
67+
68+
69+
# --------------------------------------------------
70+
def hey_yall(text):
71+
"""Change 'you' to 'y'all'"""
72+
pattern = re.compile(r"\b([yY])ou\b")
73+
return pattern.sub(r"\1'all", text)
74+
75+
76+
# --------------------------------------------------
77+
def test_hey_yall_one_match():
78+
"""Test case 1: The text contains exactly one 'you'"""
79+
text = "You are learning Python."
80+
expected_output = "Y'all are learning Python."
81+
assert hey_yall(text) == expected_output
82+
83+
84+
def test_hey_yall_no_match():
85+
"""Test case 2: The text does not contain 'you'"""
86+
text = "Hello world!"
87+
expected_output = "Hello world!"
88+
assert hey_yall(text) == expected_output
89+
90+
91+
def test_hey_yall_multiple_matches():
92+
"""
93+
Test case 3: The text contains multiple 'you' but
94+
only the last one should be replaced
95+
"""
96+
text = "You are so much fun. You're going to have a great time!"
97+
expected_output = "Y'all are so much fun. " "Y'all're going to have a great time!"
98+
assert hey_yall(text) == expected_output
99+
100+
101+
def test_hey_yall_match_case():
102+
"""
103+
Test case 4: The text contains 'You', but it should be
104+
replaced with 'Y'all' as the function is case-sensitive
105+
"""
106+
text = "You are awesome."
107+
expected_output = "Y'all are awesome."
108+
assert hey_yall(text) == expected_output
109+
110+
111+
def test_hey_yall_your():
112+
"""Test case 5: The text contains 'your'"""
113+
text = "You should watch your mouth."
114+
expected_output = "Y'all should watch your mouth."
115+
assert hey_yall(text) == expected_output
116+
117+
118+
def test_hey_yall_empty_string():
119+
"""Test case 6: The input text is an empty string"""
120+
text = ""
121+
expected_output = ""
122+
assert hey_yall(text) == expected_output
123+
124+
125+
# --------------------------------------------------
126+
def main():
127+
"""Make a jazz noise here"""
128+
129+
args = get_args()
130+
for line in args.text.splitlines():
131+
print(hey_yall(drop_gs(line)))
132+
133+
134+
# --------------------------------------------------
135+
if __name__ == "__main__":
136+
main()

15_kentucky_friar/friar_2.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-06-29
5+
Purpose: Southern fry text
6+
"""
7+
8+
import argparse
9+
import os
10+
import re
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""Get command-line arguments"""
16+
17+
parser = argparse.ArgumentParser(
18+
description="Southern fry text",
19+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
20+
)
21+
22+
parser.add_argument("text", metavar="text", help="Input text or file")
23+
24+
args = parser.parse_args()
25+
if os.path.isfile(args.text):
26+
with open(args.text, "rt", encoding="utf-8") as fh:
27+
args.text = fh.read().rstrip()
28+
29+
return args
30+
31+
32+
# --------------------------------------------------
33+
def fry(word):
34+
"""Drog the g from -ing word, you to y'all"""
35+
you_match = re.match(r"([Yy])ou", word)
36+
ing_match = re.match(r"(^\w*[aeiou]+\w*in)g$", word, flags=re.I)
37+
if you_match:
38+
word = you_match.group(1) + "'all"
39+
elif ing_match:
40+
word = ing_match.group(1) + "'"
41+
42+
return word
43+
44+
45+
# --------------------------------------------------
46+
def test_fry():
47+
"""Test fry"""
48+
49+
assert fry("") == ""
50+
assert fry("you") == "y'all"
51+
assert fry("You") == "Y'all"
52+
assert fry("fishing") == "fishin'"
53+
assert fry("Aching") == "Achin'"
54+
assert fry("swing") == "swing"
55+
56+
57+
# --------------------------------------------------
58+
def main():
59+
"""Make a jazz noise here"""
60+
61+
args = get_args()
62+
for line in args.text.splitlines():
63+
print("".join(map(fry, re.split(r"(\W+)", line.rstrip()))))
64+
65+
66+
# --------------------------------------------------
67+
if __name__ == "__main__":
68+
main()

15_kentucky_friar/test.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
import re
66
from subprocess import getstatusoutput, getoutput
77

8-
prg = './friar.py'
8+
PRG = "./friar.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-
rv, out = getstatusoutput(f'{prg} {flag}')
22+
for flag in ["-h", "--help"]:
23+
rv, out = getstatusoutput(f"python {PRG} {flag}")
2424
assert rv == 0
2525
assert re.match("usage", out, re.IGNORECASE)
2626

@@ -31,7 +31,7 @@ def test_two_syllable_ing_words():
3131

3232
tests = [("cooking", "cookin'"), ("Fishing", "Fishin'")]
3333
for given, expected in tests:
34-
out = getoutput(f'{prg} {given}')
34+
out = getoutput(f"python {PRG} {given}")
3535
assert out.strip() == expected.strip()
3636

3737

@@ -41,7 +41,7 @@ def test_one_syllable_ing_words():
4141

4242
tests = [("sing", "sing"), ("Fling", "Fling")]
4343
for given, expected in tests:
44-
out = getoutput(f'{prg} {given}')
44+
out = getoutput(f"python {PRG} {given}")
4545
assert out.strip() == expected.strip()
4646

4747

@@ -51,7 +51,7 @@ def test_you_yall():
5151

5252
tests = [("you", "y'all"), ("You", "Y'all")]
5353
for given, expected in tests:
54-
out = getoutput(f'{prg} {given}')
54+
out = getoutput(f"python {PRG} {given}")
5555
assert out.strip() == expected.strip()
5656

5757

@@ -60,45 +60,45 @@ def run_file(file):
6060
"""run with file"""
6161

6262
assert os.path.isfile(file)
63-
expected_file = file + '.out'
63+
expected_file = file + ".out"
6464

6565
assert os.path.isfile(expected_file)
6666
expected = open(expected_file).read()
6767

68-
out = getoutput(f'{prg} {file}')
68+
out = getoutput(f"python {PRG} {file}")
6969
assert out.strip() == expected.strip()
7070

7171

7272
# --------------------------------------------------
7373
def test_blake():
7474
"""blake"""
7575

76-
run_file('inputs/blake.txt')
76+
run_file("inputs/blake.txt")
7777

7878

7979
# --------------------------------------------------
8080
def test_banner():
8181
"""banner"""
8282

83-
run_file('inputs/banner.txt')
83+
run_file("inputs/banner.txt")
8484

8585

8686
# --------------------------------------------------
8787
def test_raven():
8888
"""raven"""
8989

90-
run_file('inputs/raven.txt')
90+
run_file("inputs/raven.txt")
9191

9292

9393
# --------------------------------------------------
9494
def test_dickinson():
9595
"""dickinson"""
9696

97-
run_file('inputs/dickinson.txt')
97+
run_file("inputs/dickinson.txt")
9898

9999

100100
# --------------------------------------------------
101101
def test_shakespeare():
102102
"""shakespeare"""
103103

104-
run_file('inputs/shakespeare.txt')
104+
run_file("inputs/shakespeare.txt")

0 commit comments

Comments
 (0)