File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ .PHONY : pdf test
2+
3+ pdf :
4+ pandoc README.md -o README.pdf
5+
6+ test :
7+ pytest -v test.py
Original file line number Diff line number Diff line change 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.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments