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

Part I: Validate Street Addresses (10 points) Write a function street.addresses(

ID: 3720340 • Letter: P

Question

Part I: Validate Street Addresses (10 points) Write a function street.addresses(),which takes a single argument addresses, which is a list of strings. The function analyzes each string, and, if the string's format matches the pattern of a valid street address as described below, the function appends the string's index in addresses into a returned list. A validly formatted address is defined by the following components, in the given order: 1. one or more digits 2. exactly one space 3. an uppercase letter, followed by any combination of uppercase letters, lowercase letters and spaces exactly one space 5. the word Street', 'Road' or ' Path', capitalized exactly as given 6. optionally, the following text: a. exactly one space b. the string 'Apt. ', capitalized exactly as given c. exactly one space d. exactly one uppercase letter Examples of validly formatted addresses are ' 58 Gnarled Oak Street' and '173 East Main Road Apt. Q' Suppose the following list were passed as addresses: '21 Main Street', '12 Main Drive', '5 Elm Apt. E,'6 Elm Path Apt. 5'] The function would return [0, 31 because only the first and last strings in this example match the pattern defined above.

Explanation / Answer

Tested following code with online python compiler.Try to cover all the scenario mentined above.

# Hello World program in Python
import re

def street_address(a):
out_list = []
ctr =0
for x in a:
  
#check for space it is optional
searchspace = re.findall('\b \b',x)
if searchspace:
print 'double space'
  
#check for number
if any(char.isdigit() for char in x):
print 'digit is present'
else:
continue
  
#checkfor uppercase conditon
searchupper = re.search('[A-Z]+[a-z]+s',x)
if searchupper:
print 'find upper'
else:
continue
  
#check for exact condition
if re.search(r'' + 'Street' + r'', x) or re.search(r'' + 'Path' + r'', x) or re.search(r'' + 'Road' + r'', x) :
print 'found word Street'
else:
continue
  
#conition is optional
if re.search(r'' + 'Apt.' + r'', x):
print 'found word Apt .'   
  
  
out_list.append(ctr)
ctr = ctr + 1
return out_list   
  
  
list_data = street_address(['12 Main Street','12 Main Drive'])
print list_data