×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Python Program to Implement Regular Expressions Assignment Solution

June 13, 2024
Dr. Melissa
Dr. Melissa
🇺🇸 United States
Python
Dr. Melissa, with over 5 years of experience, earned her doctorate from the prestigious University of California, Berkeley. She has successfully completed 300+ Python assignments, demonstrating her deep understanding of programming concepts and her ability to deliver top-notch solutions. Driven by a passion for teaching and problem-solving, Dr. Melissa is dedicated to helping students excel in their Python endeavors.
Key Topics
  • Instructions
    • Objective
  • Requirements and Specifications
Tip of the day
Familiarize yourself with OCaml's pattern matching; it simplifies handling recursive data structures like lists and trees, making your code concise and easier to debug.
News
In 2024, Girls Who Code introduced a Data Science + AI track in their free summer programs for high school students, fostering skills in cybersecurity and creative coding​

Instructions

Objective

Write a python assignment program to implement regular expressions.

Requirements and Specifications

Using Regular Expressions (Regex)

Objectives

  • Demonstrate ability to formulate regular expressions (regex) for common system administration tasks
  • Demonstrate ability to search for texts/files using regex (application area IP address search, etc.)
  • Demonstrate ability to pass regex as arguments to functions/methods that perform file searches (content and filename)

Tasks:

  1. The file attached (empdata.csv) contains records of employees. Upon reviewing your log files as a system administrator, you realized some employees’ computers have been compromised. The compromised computers are in the 200.10.15.0/24 subnet. Implement a function that would print the names, department, and email address of employees using such machines to a file called compromised.csv .Please note that, certain IP addresses in the given address range cannot be assigned to end-user computers.
  2. Following up on 1 above, you also realized that the function to enforce password complexity did not work properly. Given that the password policy has the requirements below, implement a function to print out the names of all employees with weak passwords.
  • At least 8 characters in length
  • Must have a combination of upper- and lower-case characters
  • Must have at least 1 digit
  1. Management of your organization has informed you that the name of the Accounting department has been changed to Finance. Write a function to create a new file (empdata-new.csv) containing information of all employees with the updated department names. Your program should display the names of employees from the Finance Department.
  2. Using the functions implemented above, implement a menu system that allows any of the functions above to be select by users and the corresponding tasks performed.

NB: Submit a single script that addresses all the requirements stated above.

Source Code

import re def CompromisedComputers(lines): """This is the function for Question 1 It receives a list of lines, and then searchs for compromised users Args: lines (List[str]): List containing all lines from file """ # Create a line to store the matches matches = list() # Loop over lines for line in lines: # Split using regex row = re.split(',', line) first_name = row[0] last_name = row[1] email = row[2] ip_address = row[3] department = row[4] #Now, get the subnet using regex subnet = re.search('200.10.15.', ip_address) if subnet != None: #print(str(subnet), department) # Save match matches.append((first_name, last_name, department, email)) # Now if there is at least one match, save to 'compromised.csv' if len(matches) > 0: fout = open('compromised.csv', 'w+') for match in matches: line = ','.join(list(match)) fout.write(line+'\n') fout.close() ; else: print("There are no matches for the given subnet.") def PrintWeakPasswordEmployees(lines): # Loop over lines for line in lines: # Remove line breaks line = re.sub('\n', '', line) # Split row = re.split(',', line) # Get name firstname = row[0] lastname = row[1] # Get password password = row[6] # Check password conditions # CONDITION 1: Password must be at least 8 characters long condition1 = re.match(r"^[a-zA-Z]{8,}$", password) # CONDITION 2: Password must have combination of upper and lower cases ; condition2 = re.match(r"(?=.*[a-z])(?=.*[A-Z])", password) # CONDITION 3: Check that it has at least one digit condition3 = re.match(r"(?=.*\d)", password) # Now, if at least one of the three conditions are not met, consider it a weak password if not condition1 or not condition2 or not condition3: print(firstname, lastname) def ReplaceAccountingWithFinance(lines): # Open the output file fout = open('empdata-new.csv', 'w+') # Loop over lines for line in lines: # Remove line breaks line = re.sub('\n', '', line) # Now, if the line contains the word 'Accounting', replace with 'Finance' if re.search(r"Accounting", line): line = re.sub(r'\bAccounting\b', 'Finance', line) # Write to new empdata file fout.write(line+'\n') # Close fout.close() if __name__ == '__main__': # Read the file file = open('empdata.csv', 'r') ; # Read all lines lines = file.readlines() # Close file ; file.close() # Skip first line (headers) lines.pop(0) # Display menu print("1) Generate file with compromised computers.") print("2) Print employees with weak passwords.") print("3) Replace 'Accounting' department with 'Finance' and generate new data file.") # Ask for option option = int(input("Please enter option: ")) if option == 1: print("Generating file with compromised employes... ", end="") CompromisedComputers(lines) print("done!") elif option == 2: print("The employees with weak passwords are:") PrintWeakPasswordEmployees(lines) elif option == 3: print("Generating new employees data... ", end="") ReplaceAccountingWithFinance(lines) print("done!") else: print("Invalid option. Exiting program...")

Similar Samples

Discover our sample solutions to see the exceptional quality of our work. Each example illustrates our proficiency in various programming languages and problem-solving skills. These samples reflect our dedication to providing reliable and top-tier programming homework help.