TOA Assignment 1
TOA Assignment 1
ASSIGNMENT SUBMITTED TO
Dr. Aftab A Shaikh
Course
Theory of Automata
Spring-2023
Prepared By:
Asad Ali (MSC-21F-001)
Asad Ali (MSC-21F-001) 2023
Code#1:
import re return processed_name
def process_name(name): else:
# Regular expression pattern to match last # No match found, return the original name
name, first name, and optional suffix return name
# Test cases
pattern = r'^(\w+(?: \w+)*),\s*([\w. names = [
]+)\s*(\w*\.*\s*)$' "Dijkstra, Edsger W.",
# Extract the components of the name using "Hoare, C. Anthony R..",
the pattern "Peterson 3rd, Gordon E.",
match = re.match(pattern, name) "Key Point Software",
if match: "Evergreen Valley College"
last_name = match.group(1) ]
first_name = match.group(2) for name in names:
suffix = match.group(3) processed_name = process_name(name)
# Construct the "first-name-first" format print(processed_name)
processed_name = f"{first_name.strip()}
{last_name}"
Asad Ali (MSC-21F-001) 2023
Output-1:
Code#2
import re
def process_phone(phone):
phone = re.sub(r'^(\d{3})-(\d{4})$', r'(0300) \1-\2', phone)
phone = re.sub(r'^(\d{3})-(\d{3})-(\d{4})$', r'(0333) \2-\3', phone)
phone = re.sub(r'^\((\d{3})\) (\d{3})-(\d{4})$', r'(0300) \2-\3', phone)
phone = re.sub(r'^(\d{3})\s(\d{3})-(\d{4})$', r'(0\1) \2-\3', phone)
return phone
phone_numbers = [
'274-7900',
'510-555-0297',
'312 555-8763',
'(800) 775-7731'
]
for phone in phone_numbers:
processed_phone = process_phone(phone)
print(processed_phone)
Code#3:
import re
def process_name(name):
# Check if the name contains a comma
if ',' in name:
# Split the name into last name and first name
last_name, first_name = re.split(r',\s*', name)
# Reorder the names in "first-name-first" form
return f"{first_name} {last_name}"
else:
return name
def process_phone(phone):
Asad Ali (MSC-21F-001) 2023
Output-3