Question

In: Computer Science

Using Python, Regular Expressions, .map() and other functions as appropriate to format existing address records and...

Using Python, Regular Expressions, .map() and other functions as appropriate to format existing address records and eliminate records with missing critical fields.Critical fieldsincludeFirstName, Lastname, Zipcode+4, and Phone number for customers. For this exercise, create an array to hold data with these 4 fields containing at least 25records. The Zipcode field should contain either traditional 5-digit Zipcode(e.g. 21801)or Zip+4 format(e.g 21801-1101). The phone numbers should contain 10-digit (e.g. 5555555555)or formatted 10-digit(e.g. 555-555-5555). Some records might be corrupt so the data needs to be munged. At this point, we assume only U.S data will be present therefor country code is not needed.

Your python code should label each column, properly format the Zip code to be either 11111 or 11111-1111 formats, properly formatthe phone numbers to always be 111-111-1111 format, and replace incorrect values with a blank string.

Notice invalid data was removed and formatting was applied as required. Commas can be left if desired. Default alignment with the column labels is acceptable.

1.Hardwire the 25 records.(i.e. the user doesn’t have to enter this)

2.Review the code in the textbook examples to simplify this work

3.Use comments to document your code

Solutions

Expert Solution

Summary-

An input array is taken appropriately to cover the four fields of "First name", "Last name", "ZIpcode", and "Phone number".

With the use of regular expressions, the code is executed and tested successfully.

Code- (Executed and tested in Anaconda IDE)

#importing the regular expressions package
import re

#receiving the input
input=[]
(Here an input list can be given according to the required choice)

#list to store the index of a corrupt record
flag=[0 for i in range(len(input))]

#iterating over the input list
for i in range(len(input)):
  
#First name
a= input[i][0]
#matching the string appropriately
if re.match(r'[A-Za-z]+',a):
flag[i]=1
else:
input[i][0]=""
  
#Last name
a= input[i][1]
#matching the string appropriately
if re.match(r'[A-Za-z]+',a):
flag[i]=1
else:
input[i][1]=""
  
#zip code
a= input[i][2]
# matching the zip code of 5 digits and an optional 4 digits
if re.match(r'\d{5}[\-[0-9]{4}]?',a):
flag[i]=1
else:
input[i][2]=""
  
#Phone number
a=input[i][3]
re.sub("-","",a)
if re.match(r'\d{10}',a):
flag[i]=1
else:
input[i][3]=""
  
print(input)

Output- (For a particular input of input=[["135","1223","12345-1235","9999999999"],["rahul","kamal","34512-123","666666666"]]


Related Solutions

For each of the following functions, find all the minimum SOP expressions using the Karnaugh map....
For each of the following functions, find all the minimum SOP expressions using the Karnaugh map. g(v,x,y,z,w)= Σm(0,1,4,5,8,9,10,15,16,18,19,20,24,26,28,31)       (1 solution)
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use “re” or “Regex” to denote regular expressions. Write the Python Code to return matching analytics word in the following given text. Write the Python Code to return how many times analytics word is provided in this text. Definitions are useful to the extent they serve a purpose. So, is defining analytics important? Yes and no. It’s not likely that we’ll ever arrive at a...
Create and test a python regular expression that matches a street address consisting of a number...
Create and test a python regular expression that matches a street address consisting of a number with one or more digits followed by two words of one or more characters each. The tokens should be separated by one space each, as in 123 Main Street.
Prove the following using the properties of regular expressions: (ab)* + c + c* = Λ...
Prove the following using the properties of regular expressions: (ab)* + c + c* = Λ + ab + (ab)* + c(Λ + c*)   Λ+ab+abab+ababab(ab)* = Λ + ∅* + (ab)* a(b+c*) + (d+e)* = ab + ac*c* + d + e + (d+e)* a*b + a*a*bc* + d* + ab = d* + ab + a*bc* + Λ a(b+cd*) = a(b+c) + acdd* (a+b)* = ∧* + ∅* + (a*b*)* (ab)*(c*+d*) = (ab)*(c+c*) + (ab)*( ∧ + d*)
Using PROC FORMAT and PROC FREQ for following data: (a) Define an appropriate format for the...
Using PROC FORMAT and PROC FREQ for following data: (a) Define an appropriate format for the gender variable. (b) Produce a 2 X 2 table with gender as the rows and lenses as the columns. (c) Calculate the relative risk and provide a one sentence written interpretation explicitly stating which groups are being compared and defining the outcome. (d) Perform a chi-squared test of association between gender and needing contact lenses. What are the results of the test (i.e. do...
Part 1(15%): Please write the Boolean expressions below in the file, part5_1.js, in the appropriate functions....
Part 1(15%): Please write the Boolean expressions below in the file, part5_1.js, in the appropriate functions. part5_1.js script will execute if you load the part5_1.html file into the browser. Write a Boolean expression that checks that number num, has n digits. Assume that num is an integer.( consider both the positive and negative case for num) Assuming the following are the ingredients for Jamie Oliver's fried rice: 1/2 dozen green onion 1 teaspoon chilli jam 2 cups rice 2 tablespoon...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the table below. Create a Python program that will calculate the user’s net pay based on the tax bracket he/she is in. Your program will prompt the user for their first name, last name, their monthly gross pay, and the number of dependents. The number of dependents will determine which tax bracket the user ends up in. The tax bracket is as follows: 0 –...
1) For each of the following functions, find all the minimum SOP expressions using the Karnaugh...
1) For each of the following functions, find all the minimum SOP expressions using the Karnaugh map. a) f(a,b,c) = Σm(0,1,5,6,7) (2 solutions) b) g(v,x,y,z,w)= Σm(0,1,4,5,8,9,10,15,16,18,19,20,24,26,28,31) (1 solution) c) h(a,b,c,d) = Σm(01,2,5,7,9)+Σd(6,8,11,13,14,15) (4 solutions) d) f(a,b,c,d) = Σm(5,7,9,11,13,14)+Σd(2,6,10,12,15) (4 solutions)
Using a concept map format arrange your identified quality gap into the PDSA cycle. (For bedside...
Using a concept map format arrange your identified quality gap into the PDSA cycle. (For bedside handoff).
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT