Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the link to the word list txt file. https://drive.google.com/file/d/0B4a

ID: 3795391 • Letter: T

Question

This is the link to the word list txt file.
https://drive.google.com/file/d/0B4akmIRkT2AaTDlpY2g5V2doR2c/view?usp=sharing

Your task for this assignment is to write a simple program in Python 2.7 that will extract the words matching the certain patterns defined by the following regular expressions All alphanumeric words with no symbols. (0...9+ | A...Z+ | a...z+)+ Example valid words: hotel'. laTechl928' 93894'. 'zxvylk' All valid LaTech e-mail addresses, (a...z+ | 0... l+)+@(a., .z+ |0... 1+)+.latech.edu Example valid words: 1234@latech.edu', narnia@latech.edu', robl@coes.lalech.edu' All valid identifier names in C: (_ | A...Z | a...z) (_ | 0... 9+ | A...Z+ | a...z+)* Example valid words: '_a constant I ', this is_my_variable ' Write only one function that will extract words matching all three regular expressions. The following has been provided: An input file ' input_words.txt' containing random words. Pass an input file through a command-line. Import 're' library Read one word at a time from an input file. Only spaces separate words. Write a function to check whether a word matches any of the defined regular expressions. All matching words must be written in a new line into an 'extracted_words.txt' file Write comments to document your code appropriately.

Explanation / Answer

import sys
import re

alpha = re.compile(r'[a-zA-Z0-9]+')
email = re.compile(r'[0-9a-z]+@[0-9a-z]+.latech.edu')
identifier = re.compile(r'^[^dW]w*Z')

def isValidWord(word):
if alpha.match(word):
print "alpha"
return True
elif email.match(word):
return True
elif identifier.match(word):
return True
else:
return False

out = open("extracted_words.txt", "w")

with open(sys.argv[1], 'r') as f:
for line in f:
words = line.split()
for word in words:
if isValidWord(word):
out.write(word + " ")

out.close()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote