Skip to content

Commit 19f151e

Browse files
committed
Problem 18 solution
1 parent e6206ff commit 19f151e

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

18_gematria/gematria.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-07-01
5+
Purpose: Gematria
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="Gematria", formatter_class=argparse.ArgumentDefaultsHelpFormatter
19+
)
20+
21+
parser.add_argument("text", metavar="text", help="Input text or file")
22+
23+
args = parser.parse_args()
24+
if os.path.isfile(args.text):
25+
with open(args.text, "rt", encoding="utf-8") as fh:
26+
args.text = fh.read().rstrip()
27+
28+
return args
29+
30+
31+
# --------------------------------------------------
32+
def word_to_num(word: str) -> str:
33+
"""Return the sum of ascii character values for a word"""
34+
35+
return str(sum(map(ord, re.sub(r"[^a-zA-Z0-9]", "", word))))
36+
37+
38+
# --------------------------------------------------
39+
def test_word_to_num():
40+
"""Test word_to_num function"""
41+
42+
assert word_to_num("a") == "97"
43+
assert word_to_num("abc") == "294"
44+
assert word_to_num("ab'c") == "294"
45+
assert word_to_num("4a-b'c,") == "346"
46+
47+
48+
# --------------------------------------------------
49+
def main():
50+
"""Make a jazz noise here"""
51+
52+
args = get_args()
53+
54+
for line in args.text.splitlines():
55+
print(" ".join(map(word_to_num, line.split())))
56+
57+
58+
# --------------------------------------------------
59+
if __name__ == "__main__":
60+
main()

18_gematria/test.py

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

8-
prg = './gematria.py'
9-
spiders = '../inputs/spiders.txt'
10-
fox = '../inputs/fox.txt'
11-
sonnet = '../inputs/sonnet-29.txt'
8+
PRG = "./gematria.py"
9+
SPIDERS = "../inputs/spiders.txt"
10+
FOX = "../inputs/fox.txt"
11+
SONNET = "../inputs/sonnet-29.txt"
1212

1313

1414
# --------------------------------------------------
1515
def test_exists():
1616
"""exists"""
1717

18-
assert os.path.isfile(prg)
18+
assert os.path.isfile(PRG)
1919

2020

2121
# --------------------------------------------------
2222
def test_usage():
2323
"""usage"""
2424

25-
for flag in ['-h', '--help']:
26-
rv, out = getstatusoutput(f'{prg} {flag}')
25+
for flag in ["-h", "--help"]:
26+
rv, out = getstatusoutput(f"python {PRG} {flag}")
2727
assert rv == 0
2828
assert re.match("usage", out, re.IGNORECASE)
2929

@@ -32,31 +32,31 @@ def test_usage():
3232
def test_text():
3333
"""Text"""
3434

35-
out = getoutput(f'{prg} "foo bar baz"')
36-
assert out.strip() == '324 309 317'
35+
out = getoutput(f'python {PRG} "foo bar baz"')
36+
assert out.strip() == "324 309 317"
3737

3838

3939
# --------------------------------------------------
4040
def test_fox():
4141
"""File"""
4242

43-
out = getoutput(f'{prg} {fox}')
44-
assert out.strip() == '289 541 552 333 559 444 321 448 314'
43+
out = getoutput(f"python {PRG} {FOX}")
44+
assert out.strip() == "289 541 552 333 559 444 321 448 314"
4545

4646

4747
# --------------------------------------------------
4848
def test_spiders():
4949
"""File"""
5050

51-
out = getoutput(f'{prg} {spiders}')
52-
assert out.strip() == '405 579 762\n73 421 548\n862'
51+
out = getoutput(f"python {PRG} {SPIDERS}")
52+
assert out.strip() == "405 579 762\n73 421 548\n862"
5353

5454

5555
# --------------------------------------------------
5656
def test_sonnet():
5757
"""File"""
5858

59-
out = getoutput(f'{prg} {sonnet}')
59+
out = getoutput(f"python {PRG} {SONNET}")
6060
expected = """
6161
631 107
6262
719 1132

0 commit comments

Comments
 (0)