Skip to content

Commit f2e6046

Browse files
committed
palindromes
1 parent cd45add commit f2e6046

7 files changed

Lines changed: 119 additions & 57 deletions

File tree

palindromes/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: pdf test
2+
3+
pdf:
4+
pandoc README.md -o README.pdf
5+
6+
test:
7+
pytest -v test.py

palindromes/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Palindromes
2+
3+
Write a Python program called `palindromic.py` that will find words that are palindromes in positional argument which is either a string or a file name.

palindromes/find_palindromes.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

palindromes/input.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Anna saw a Honda Civic carrying a kayak. Madam, you are my Mom! Wow!
2+
and I will do my level best to meet you at noon in a racecar outfitted with
3+
radar. You never saw anyone redder. Refer to the rotator or rotor when
4+
you perform your solos. Be sure the stats support the tenet of your argument.

palindromes/solution.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Ken Youens-Clark <kyclark@gmail.com>
4+
Date : 2019-05-29
5+
Purpose: Find palindromes in text
6+
"""
7+
8+
import argparse
9+
import os
10+
import re
11+
import sys
12+
13+
14+
# --------------------------------------------------
15+
def get_args():
16+
"""Get command-line arguments"""
17+
18+
parser = argparse.ArgumentParser(
19+
description='Find palindromes in text',
20+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
21+
22+
parser.add_argument('text', metavar='str', help='Input text or file')
23+
24+
parser.add_argument('-m',
25+
'--min',
26+
metavar='int',
27+
type=int,
28+
help='Minimum word length',
29+
default=3)
30+
31+
return parser.parse_args()
32+
33+
34+
# --------------------------------------------------
35+
def main():
36+
"""Make a jazz noise here"""
37+
38+
args = get_args()
39+
text = args.text
40+
min_length = args.min
41+
42+
if os.path.isfile(text):
43+
text = open(text).read()
44+
45+
for line in text.splitlines():
46+
for word in re.split('(\W+)', line.lower()):
47+
if len(word) >= min_length:
48+
rev = ''.join(reversed(word))
49+
if rev == word:
50+
print(word)
51+
52+
53+
# --------------------------------------------------
54+
if __name__ == '__main__':
55+
main()

palindromes/test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
"""tests for palindromic.py"""
3+
4+
import re
5+
import random
6+
from subprocess import getstatusoutput, getoutput
7+
8+
prg = './palindromic.py'
9+
10+
11+
# --------------------------------------------------
12+
def test_usage():
13+
"""usage"""
14+
15+
for flag in ['-h', '--help']:
16+
rv, out = getstatusoutput('{} {}'.format(prg, flag))
17+
assert rv == 0
18+
assert re.match("usage", out, re.IGNORECASE)
19+
20+
21+
# --------------------------------------------------
22+
def test_str():
23+
s = 'wow are you my mom?'
24+
out = getoutput('{} "Wow, are you my Mom?"'.format(prg)).splitlines()
25+
assert out == ['wow', 'mom']
26+
27+
28+
# --------------------------------------------------
29+
def test_file():
30+
out = getoutput('{} input.txt'.format(prg))
31+
expected = """
32+
anna
33+
civic
34+
kayak
35+
madam
36+
mom
37+
wow
38+
level
39+
noon
40+
racecar
41+
radar
42+
redder
43+
refer
44+
rotator
45+
rotor
46+
solos
47+
stats
48+
tenet
49+
""".strip()
50+
assert out.strip() == expected

palindromes/word_is_palindrome.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)