Skip to content

Commit 80e54c9

Browse files
committed
Problem 12 solution
1 parent ff5dfce commit 80e54c9

File tree

2 files changed

+92
-25
lines changed

2 files changed

+92
-25
lines changed

12_ransom/ransom.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-28
5+
Purpose: Ransom Note
6+
"""
7+
8+
import argparse
9+
import os
10+
import random
11+
12+
# import string
13+
14+
15+
# --------------------------------------------------
16+
def get_args():
17+
"""Get command-line arguments"""
18+
19+
parser = argparse.ArgumentParser(
20+
description="Ransom Note",
21+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
22+
)
23+
24+
parser.add_argument("text", metavar="text", help="Input text or file")
25+
parser.add_argument("-s", "--seed", help="Random seed", metavar="int", type=int)
26+
27+
args = parser.parse_args()
28+
if os.path.isfile(args.text):
29+
with open(args.text, "rt", encoding="utf-8") as fh:
30+
args.text = fh.read().rstrip()
31+
32+
return args
33+
34+
35+
# --------------------------------------------------
36+
def choose(char: str) -> str:
37+
"""Choose a random case for a character"""
38+
39+
return random.choice([char.lower(), char.upper()])
40+
41+
42+
# --------------------------------------------------
43+
def test_choose():
44+
"""Test choose"""
45+
46+
state = random.getstate()
47+
random.seed(1)
48+
assert choose("a") == "a"
49+
assert choose("b") == "b"
50+
assert choose("c") == "C"
51+
assert choose("d") == "d"
52+
random.setstate(state)
53+
54+
55+
# --------------------------------------------------
56+
def main():
57+
"""Make a jazz noise here"""
58+
59+
args = get_args()
60+
random.seed(args.seed)
61+
print("".join(map(choose, args.text)))
62+
63+
64+
# --------------------------------------------------
65+
if __name__ == "__main__":
66+
main()

12_ransom/test.py

+26-25
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@
66
import random
77
from subprocess import getstatusoutput
88

9-
prg = './ransom.py'
10-
fox = '../inputs/fox.txt'
11-
now = '../inputs/now.txt'
9+
PRG = "./ransom.py"
10+
FOX = "../inputs/FOX.txt"
11+
NOW = "../inputs/NOW.txt"
1212

1313

1414
# --------------------------------------------------
1515
def seed_flag():
16-
return '-s' if random.randint(0, 1) else '--seed'
16+
"""seed"""
17+
return "-s" if random.randint(0, 1) else "--seed"
1718

1819

1920
# --------------------------------------------------
2021
def test_exists():
2122
"""exists"""
2223

23-
assert os.path.isfile(prg)
24+
assert os.path.isfile(PRG)
2425

2526

2627
# --------------------------------------------------
2728
def test_usage():
2829
"""usage"""
2930

30-
for flag in ['-h', '--help']:
31-
rv, out = getstatusoutput(f'{prg} {flag}')
31+
for flag in ["-h", "--help"]:
32+
rv, out = getstatusoutput(f"python {PRG} {flag}")
3233
assert rv == 0
3334
assert re.match("usage", out, re.IGNORECASE)
3435

@@ -37,12 +38,14 @@ def test_usage():
3738
def test_text1():
3839
"""Test"""
3940

40-
in_text = 'The quick brown fox jumps over the lazy dog.'
41-
tests = [('1', 'thE QUICk BrOWn Fox jumpS OveR tHe LAzY dOg.'),
42-
('3', 'thE quICk BROwn Fox jUmPS OVEr the lAZY DOG.')]
41+
in_text = "The quick brown FOX jumps over the lazy dog."
42+
tests = [
43+
("1", "thE QUICk BrOWn Fox jumpS OveR tHe LAzY dOg."),
44+
("3", "thE quICk BROwn Fox jUmPS OVEr the lAZY DOG."),
45+
]
4346

4447
for seed, expected in tests:
45-
rv, out = getstatusoutput(f'{prg} {seed_flag()} {seed} "{in_text}"')
48+
rv, out = getstatusoutput(f'python {PRG} {seed_flag()} {seed} "{in_text}"')
4649
assert rv == 0
4750
assert out.strip() == expected
4851

@@ -51,16 +54,14 @@ def test_text1():
5154
def test_text2():
5255
"""Test"""
5356

54-
in_text = 'Now is the time for all good men to come to the aid of the party.'
57+
in_text = "Now is the time for all good men to come to the aid of the party."
5558
tests = [
56-
('2',
57-
'now iS the TIME fOR ALl good meN TO COMe To THE AID oF THE PArTY.'),
58-
('5',
59-
'NOw is tHE Time FOr all good men To coME TO tHe AiD OF THe ParTy.')
59+
("2", "now iS the TIME fOR ALl good meN TO COMe To THE AID oF THE PArTY."),
60+
("5", "NOw is tHE Time FOr all good men To coME TO tHe AiD OF THe ParTy."),
6061
]
6162

6263
for seed, expected in tests:
63-
rv, out = getstatusoutput(f'{prg} {seed_flag()} {seed} "{in_text}"')
64+
rv, out = getstatusoutput(f'python {PRG} {seed_flag()} {seed} "{in_text}"')
6465
assert rv == 0
6566
assert out.strip() == expected
6667

@@ -69,11 +70,13 @@ def test_text2():
6970
def test_file1():
7071
"""Test"""
7172

72-
tests = [('1', 'thE QUICk BrOWn Fox jumpS OveR tHe LAzY dOg.'),
73-
('3', 'thE quICk BROwn Fox jUmPS OVEr the lAZY DOG.')]
73+
tests = [
74+
("1", "thE QUICk BrOWn Fox jumpS OveR tHe LAzY dOg."),
75+
("3", "thE quICk BROwn Fox jUmPS OVEr the lAZY DOG."),
76+
]
7477

7578
for seed, expected in tests:
76-
rv, out = getstatusoutput(f'{prg} {seed_flag()} {seed} {fox}')
79+
rv, out = getstatusoutput(f"python {PRG} {seed_flag()} {seed} {FOX}")
7780
assert rv == 0
7881
assert out.strip() == expected
7982

@@ -83,13 +86,11 @@ def test_file2():
8386
"""Test"""
8487

8588
tests = [
86-
('2',
87-
'now iS the TIME fOR ALl good meN TO COMe To THE AID oF THE PArTY.'),
88-
('5',
89-
'NOw is tHE Time FOr all good men To coME TO tHe AiD OF THe ParTy.')
89+
("2", "now iS the TIME fOR ALl good meN TO COMe To THE AID oF THE PArTY."),
90+
("5", "NOw is tHE Time FOr all good men To coME TO tHe AiD OF THe ParTy."),
9091
]
9192

9293
for seed, expected in tests:
93-
rv, out = getstatusoutput(f'{prg} {seed_flag()} {seed} {now}')
94+
rv, out = getstatusoutput(f"python {PRG} {seed_flag()} {seed} {NOW}")
9495
assert rv == 0
9596
assert out.strip() == expected

0 commit comments

Comments
 (0)