Skip to content

Commit 47f1bb3

Browse files
committed
Problem 16 solution
1 parent b8d1d85 commit 47f1bb3

File tree

2 files changed

+90
-16
lines changed

2 files changed

+90
-16
lines changed

16_scrambler/scrambler.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-07-01
5+
Purpose: Scramble the letters of words
6+
"""
7+
8+
import argparse
9+
import os
10+
import random
11+
import re
12+
13+
14+
# --------------------------------------------------
15+
def get_args():
16+
"""Get command-line arguments"""
17+
18+
parser = argparse.ArgumentParser(
19+
description="Scramble the letters of words",
20+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
21+
)
22+
23+
parser.add_argument("text", metavar="text", help="Input text or file")
24+
parser.add_argument("-s", "--seed", help="Random seed", metavar="int", type=int)
25+
26+
args = parser.parse_args()
27+
if os.path.isfile(args.text):
28+
with open(args.text, "rt", encoding="utf-8") as fh:
29+
args.text = fh.read().rstrip()
30+
31+
return args
32+
33+
34+
# --------------------------------------------------
35+
def shuffle(word):
36+
"""Scramble the inside letters of a word"""
37+
if len(word) > 3 and re.match(r"\w+", word):
38+
first, middle, last = word[0], list(word[1:-1]), word[-1]
39+
random.shuffle(middle)
40+
word = first + "".join(middle) + last
41+
return word
42+
43+
44+
# --------------------------------------------------
45+
def test_shuffle():
46+
"""Test the shuffle function"""
47+
state = random.getstate()
48+
random.seed(1)
49+
50+
assert shuffle("") == ""
51+
assert shuffle("a") == "a"
52+
assert shuffle("at") == "at"
53+
assert shuffle("hat") == "hat"
54+
assert shuffle("potato") == "ptoato"
55+
assert shuffle("scrambled") == "slerbmcad"
56+
57+
random.setstate(state)
58+
59+
60+
# --------------------------------------------------
61+
def main():
62+
"""Make a jazz noise here"""
63+
64+
args = get_args()
65+
random.seed(args.seed)
66+
splitter = re.compile(r"([a-zA-Z](?:[a-zA-Z']*[a-zA-Z])?)")
67+
68+
for line in args.text.splitlines():
69+
print("".join(map(shuffle, splitter.split(line))))
70+
71+
72+
# --------------------------------------------------
73+
if __name__ == "__main__":
74+
main()

16_scrambler/test.py

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

8-
prg = './scrambler.py'
9-
fox = '../inputs/fox.txt'
10-
bustle = '../inputs/the-bustle.txt'
11-
spiders = '../inputs/spiders.txt'
8+
PRG = "./scrambler.py"
9+
FOX = "../inputs/fox.txt"
10+
BUSTLE = "../inputs/the-bustle.txt"
11+
SPIDERS = "../inputs/spiders.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,17 +32,17 @@ def test_usage():
3232
def test_text1():
3333
"""Text"""
3434

35-
out = getoutput(f'{prg} foobar -s 1')
36-
assert out.strip() == 'faobor'
35+
out = getoutput(f"python {PRG} foobar -s 1")
36+
assert out.strip() == "faobor"
3737

3838

3939
# --------------------------------------------------
4040
def test_text2():
4141
"""Text"""
4242

43-
text = 'The quick brown fox jumps over the lazy dog.'
44-
expected = 'The qicuk bworn fox jpmus over the lzay dog.'
45-
out = getoutput(f'{prg} "{text}" -s 2')
43+
text = "The quick brown fox jumps over the lazy dog."
44+
expected = "The qicuk bworn fox jpmus over the lzay dog."
45+
out = getoutput(f'python {PRG} "{text}" -s 2')
4646
assert out.strip() == expected
4747

4848

@@ -62,22 +62,22 @@ def test_file_bustle():
6262
Unitl eettnriy.
6363
""".strip()
6464

65-
out = getoutput(f'{prg} --seed 3 {bustle}')
65+
out = getoutput(f"python {PRG} --seed 3 {BUSTLE}")
6666
assert out.strip() == expected.strip()
6767

6868

6969
# --------------------------------------------------
7070
def test_file_fox():
7171
"""File input"""
7272

73-
out = getoutput(f'{prg} --seed 4 {fox}')
74-
assert out.strip() == 'The qciuk bworn fox jpums oevr the lzay dog.'
73+
out = getoutput(f"python {PRG} --seed 4 {FOX}")
74+
assert out.strip() == "The qciuk bworn fox jpums oevr the lzay dog."
7575

7676

7777
# --------------------------------------------------
7878
def test_file_spiders():
7979
"""File input"""
8080

81-
out = getoutput(f'{prg} --seed 9 {spiders}')
81+
out = getoutput(f"python {PRG} --seed 9 {SPIDERS}")
8282
expected = "Do'nt wrory, sedrpis,\nI keep hsoue\ncalusaly."
8383
assert out.strip() == expected

0 commit comments

Comments
 (0)