Created
September 11, 2013 20:31
-
-
Save jcelliott/6529374 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import fileinput | |
def find_alliteration(line, smallest_seq): | |
# print("examining line: "+line) | |
seq = 1 | |
words = line.split() | |
letter = words[0][0] | |
# print("looking for: "+letter) | |
for word in words[1:]: | |
# print("looking at word: "+word) | |
if word[0] == letter: | |
seq += 1 | |
if seq >= smallest_seq: | |
# print("FOUND alliteration: "+line) | |
return line | |
else: | |
letter = word[0] | |
# print("looking for: "+letter) | |
seq = 1 | |
return None | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-n", "--number", type=int, default=3, | |
help="minimum number of words to count as alliteration") | |
parser.add_argument("files", nargs=argparse.REMAINDER) | |
args = parser.parse_args() | |
count = 0 | |
out = open('output.txt', 'w') | |
for line in fileinput.input(args.files): | |
found = find_alliteration(line, args.number) | |
if found: | |
count += 1 | |
out.write(found) | |
print("found " + str(count) +" alliterations") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment