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

Preliminaries For this lab you will be working with regular expressions in Pytho

ID: 3816441 • Letter: P

Question

Preliminaries For this lab you will be working with regular expressions in Python. Various functions for working with regular expressions are available in the re module. Fortunately, Python makes it pretty easy to see if a string matches a particular pattern. At the top of the file we must import the re module: import re Then we can use the search() function to test whether a string matches a pattern. In the example below, the regular expression has been saved in a string called pattern for convenience: phone = ’123-456-7890’ pattern = r’ˆd{3}-d{3}-d{4}$’ if re.search(pattern, phone): print(’The string matches the pattern.’) else: print(’The string does not match the pattern.’) The r that precedes the pattern string is not a typo. Rather, the r indicates that the string is a “raw” string. In a raw string, as opposed to a “normal” string, any backslash character is interpreted as simply a backslash, as opposed to defining an escape sequence like or . Make sure you use raw strings in your Python code when defining regular expressions. The ˆ and $ at the beginning and end of the regular expression indicate that the entire string must match the regular expression, and not just part of the string. Make sure you include these symbols in your regular expressions too! Part I: Categorizing Product Labels (3 points) Write a function category counts() that takes a single argument consisting of a list of product labels given as strings and returns a list of four integer counts representing how many products fall into each of four categories: • Category 1: the labels starts with the letters PQ, followed by a 5-digit number (00000 through 99999), and ending with either an uppercase or lowercase letter X • Category 2: the labels starts with the letters RFT, followed by any combination of 7 uppercase letters and digits, and always ending with one additional digit • Category 3: the labels starts with the letter A, followed by 7-10 uppercase letters, followed by 3-7 digits • Category 4: the label does not match any of the above patterns

Example:

Part II: Calculating Enrollments (3 points) Write a function enrollments() that takes a list of strings representing course IDs (e.g., “CSE 220”) and returns a count of how many strings define valid courses. Courses should be expressed as a three-letter subject in mixed uppercase/lowercase letters, followed by an optional space, followed by a three-digit number. So, ideally, each course is given in a form similar to "CSE 101" or "Phy 132". One obstacle to solving this problem is that sometimes extra characters – specifically, combinations of periods, dashes and commas – are inadvertently inserted in between letters. In such cases we still consider the course IDs to be valid.

Examples:

Part III: Identifying Credit Cards (3 points) Write a function credit card() that takes an integer representing a credit card number and returns the brand of the credit card. The returned brand is given in all uppercase letters. If the card does not match any brand, the function returns None. The table below gives the details about the credit card number formats for several companies:

Example:

Function Call Return Value category-counts (I' PQ928 19x TKSNC82 J4A. 1271827' (1, 1, 1, 21 AJEIZMFWEL2 RETJ9WZMS28 CIW 92KJSD category counts C'PQ38400X pq 38323X RET 8291912 C1, 0,0, 31 AJSKWJS 83928' [1 2, 0, 41 category counts C' PO999 99XX RFT 00000009 RRET TKM ZSDDS4 4', CSE101 WOWZA. RETT K9DMG 828 BAWDKMJDW829 1242 PO12 341X' J)

Explanation / Answer

# pastebin link for code: https://pastebin.com/CYhsEkA1

import re

def category_counts(productList):
countList = [0]*4
  
pattern1 = r'^PQd{5}[xX]$'
pattern2 = r'^RFT[A-Z0-9]{7}d$'
pattern3 = r'^A[A-Z]{7,10}d{3,7}$'
  
for product in productList:
matched = 0
if re.search(pattern1, product):
matched = 1
countList[0] += 1
if re.search(pattern2, product):
matched = 1
countList[1] += 1
if re.search(pattern3, product):
matched = 1
countList[2] += 1
if not matched:
countList[3] += 1
return countList

def enrollments(courses):
pattern = r'^[A-Za-z][-.,]*[A-Za-z][-.,]*[A-Za-z]s?ddd$'
count = 0
for course in courses:
if re.search(pattern, course):
count += 1
print
return count

def credit_card(card):
ALPHA = r'^(4026|417500|4405)'
BETA = r'^5[0-4]d'
GAMMA = r'^62'
OMEGA = r'^30[0-5]'
  
if len(card) == 16 and re.search(ALPHA, card):
return "ALPHA"
  
if len(card) == 16 and re.search(BETA, card):
return "BETA"
  
if len(card) >= 16 and len(card) <= 19 and re.search(GAMMA, card):
return "GAMMA"
  
if len(card) == 14 and re.search(OMEGA, card):
return "OMEGA"
  
return None

if __name__ == '__main__':
  
# part 1
print(category_counts(['PQ92819x', 'TKSNC82J4A', 'AJEIZMFWEL271827', 'RFTJ9WZMS28', 'CIW92KJSD']))
print(category_counts(['PQ38400x', 'pq38323X', 'RFT8291912', 'AJSKWJ83928']))
print(category_counts(['PQ99999XX', 'RFT00000009', 'RRFTKMZSDDS4', 'CSE101WOWZA', 'RFTK9DMG828', 'BAWDKMJDW8291242', 'PQ12341X']))
  
# part 2
courses1 = ['CSE 101', 'AMS 310', 'PHY 132', 'Wrt 102']
courses2 = ['CSE114', 'ECO330', 'CHNN 101', 'Ams 261', 'MAT 200', 'WRT101', 'frn1012', 'che 299']
courses3 = ['C-S-E 114', 'C.S..E215', 'AMS-211', 'B,,,I.-O 255', '-ECO 102']
print('Test enrollments() with courses = ' + str(courses1))
print(' Return value: ' + str(enrollments(courses1)))
print('Test enrollments() with courses = ' + str(courses2))
print(' Return value: ' + str(enrollments(courses2)))
print('Test enrollments() with courses = ' + str(courses3))
print(' Return value: ' + str(enrollments(courses3)))
  
# part 3
print(credit_card("4175007282312321"))
print(credit_card("5025007282312321"))
print(credit_card("62250072875454563"))
print(credit_card("30428763928172"))
print(credit_card("41750072823127458"))
print(credit_card("50550072823127458"))
print(credit_card("62250072875441254563"))
print(credit_card("30628763928172"))

Please give positiv feedbakc if this solved your question.