From b20842e0001490a5b590bbb1254097ffd54ddcab Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Tue, 9 Feb 2021 09:41:55 -0500 Subject: [PATCH] finished regex lab B --- Unit 4/Lab_B_AI_Grader.py | 83 +++++++++++++++++++++++++++++++++++++ Unit 4/Lab_B_self_check.py | 76 +++++++++++++++++++++++++++++++++ Unit 4/Umaretiya_r_U4_B.txt | 10 +++++ 3 files changed, 169 insertions(+) create mode 100644 Unit 4/Lab_B_AI_Grader.py create mode 100644 Unit 4/Lab_B_self_check.py create mode 100644 Unit 4/Umaretiya_r_U4_B.txt diff --git a/Unit 4/Lab_B_AI_Grader.py b/Unit 4/Lab_B_AI_Grader.py new file mode 100644 index 0000000..d3dadd9 --- /dev/null +++ b/Unit 4/Lab_B_AI_Grader.py @@ -0,0 +1,83 @@ +# Nicole Kim, 2/5/2019, edited on 2/3/2021 + +'''In Q40-42, An Othello board is any string of length 64 made up of only the characters in "xX.Oo". +An Othello edge is any string of length 8 made up of only the characters in "xX.Oo".''' + +def num_40(subject): + # Write a regular expression that will match on an Othello board represented as a string. + pattern = "/^[xo.]{64}$/i" #notice that python does not want / / + return pattern + match = re.match(pattern, subject, re.I) + print (match.group(0)) if match != None else print (match) + +def num_41(subject): + # Given a string of length 8, determine whether it could represent an Othello edge with exactly one hole. + pattern = "/^[xo]*\.[xo]*$/i" + return pattern + match = re.match(pattern, subject, re.I) + print (match.group(0)) if match != None else print (match) + +def num_42(subject): + # Given an Othello edge as a string, determine whether there is a hole such that if X plays to the hole (assuming it could), + # it will be connected to one of the corners through X tokens. Specifically, this means that one of the ends must be a hole, + # or starting from an end there is a sequence of at least one x followed immediately by a sequence (possibly empty) of o, + # immediately followed by a hole. + pattern = "/^\.o*x|xo*\.$/i" + return pattern + match = re.search(pattern, subject, re.I) + print (match.group(0)) if match != None else print (match) + +def num_43(subject): + # Match on all strings of odd length. + pattern = "/^(..)*.$/" + return pattern + match = re.match(pattern, subject, re.DOTALL) + print (match.group(0)) if match != None else print (match) + +def num_44(subject): + # Match on all odd length binary strings starting with 0, and on even length binary strings starting with 1. + pattern = "/^(0|1[01])([01]{2})*$/" + return pattern + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +def num_45(subject): + # Match all words having two adjacent vowels that differ. + pattern = "/\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w*/i" + return pattern + matches = re.finditer(pattern, subject, re.I) + print ([m.group() for m in matches]) + +def num_46(subject): + # Match on all binary strings which DO NOT contain the substring 110. + pattern = "/^(1?0)*1*$/" + return pattern + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +def num_47(subject): + # Match on all non-empty strings over the alphabet {a, b, c} that contain at most one a. + pattern = "/^(a[bc]*|[bc]+a?[bc]*)$/" + return pattern + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +def num_48(subject): + # Match on all non-empty strings over the alphabet {a, b, c} that contain an even number of a's. + pattern = "/^[bc]*(a[bc]*a[bc]*)*$/" + return pattern + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +def num_49(subject): + # Match on all positive, even, base 3 integer strings. + pattern = "/^[02]*(1[02]*1[02]*)*$/" + return pattern + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +import sys; +args = sys.argv[1:] +idx = int(args[0])-40 + +print(eval("num_" + args[0])("")) \ No newline at end of file diff --git a/Unit 4/Lab_B_self_check.py b/Unit 4/Lab_B_self_check.py new file mode 100644 index 0000000..575ddaa --- /dev/null +++ b/Unit 4/Lab_B_self_check.py @@ -0,0 +1,76 @@ +# Nicole Kim, 2/5/2019, edited on 2/3/2021 +import re + +'''In Q40-42, An Othello board is any string of length 64 made up of only the characters in "xX.Oo". +An Othello edge is any string of length 8 made up of only the characters in "xX.Oo".''' + +def num_40(subject): + # Write a regular expression that will match on an Othello board represented as a string. + pattern = "^[xo.]{64}$" #notice that python does not want / / + match = re.match(pattern, subject, re.I) + print (match.group(0)) if match != None else print (match) + +def num_41(subject): + # Given a string of length 8, determine whether it could represent an Othello edge with exactly one hole. + pattern = "[xo]*\.[xo]*" + match = re.match(pattern, subject, re.I) + print (match.group(0)) if match != None else print (match) + +def num_42(subject): + # Given an Othello edge as a string, determine whether there is a hole such that if X plays to the hole (assuming it could), + # it will be connected to one of the corners through X tokens. Specifically, this means that one of the ends must be a hole, + # or starting from an end there is a sequence of at least one x followed immediately by a sequence (possibly empty) of o, + # immediately followed by a hole. + pattern = "^\.[o]|[o]\.$" + match = re.search(pattern, subject, re.I) + print (match.group(0)) if match != None else print (match) + +def num_43(subject): + # Match on all strings of odd length. + pattern = "/^(..)*.$/" + match = re.match(pattern, subject, re.DOTALL) + print (match.group(0)) if match != None else print (match) + +def num_44(subject): + # Match on all odd length binary strings starting with 0, and on even length binary strings starting with 1. + pattern = "" + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +def num_45(subject): + # Match all words having two adjacent vowels that differ. + pattern = "" + matches = re.finditer(pattern, subject, re.I) + print ([m.group() for m in matches]) + +def num_46(subject): + # Match on all binary strings which DO NOT contain the substring 110. + pattern = "" + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +def num_47(subject): + # Match on all non-empty strings over the alphabet {a, b, c} that contain at most one a. + pattern = "" + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +def num_48(subject): + # Match on all non-empty strings over the alphabet {a, b, c} that contain an even number of a's. + pattern = "^[bc]*(a[bc]*a[bc]*)+$" + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +def num_49(subject): + # Match on all positive, even, base 3 integer strings. + pattern = "" + match = re.match(pattern, subject) + print (match.group(0)) if match != None else print (match) + +while(True): + input_num = input("Choose the exercise # (40 - 49 or -1 to terminate):") + if input_num == '-1': exit("Good bye") + input_str = input("Input string: ") + eval("num_"+input_num)(input_str) + print() + diff --git a/Unit 4/Umaretiya_r_U4_B.txt b/Unit 4/Umaretiya_r_U4_B.txt new file mode 100644 index 0000000..9a165f7 --- /dev/null +++ b/Unit 4/Umaretiya_r_U4_B.txt @@ -0,0 +1,10 @@ +^[xo.]{64}$ +^[xo]*\.[xo]*$ +^\.o*x|xo*\.$ +^(..)*.$ +^(0|1[01])([01]{2})*$ +\w*(a[eiou]|e[aiou]|i[aeou]|o[aeiu]|u[aeio])\w* +^(1?0)*1*$ +^(a[bc]*|[bc]+a?[bc]*)$ +^[bc]*(a[bc]*a[bc]*)*$ +^[02]*(1[02]*1[02]*)*$ \ No newline at end of file