Skip to content

Instantly share code, notes, and snippets.

@jcelliott
Created September 11, 2013 20:31
Show Gist options
  • Save jcelliott/6529374 to your computer and use it in GitHub Desktop.
Save jcelliott/6529374 to your computer and use it in GitHub Desktop.
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