Best Python code snippet using fMBT_python
pset1_nfsa.py
Source:pset1_nfsa.py
1class FSA:2 def __init__(self, num_states = 0):3 self.num_states = num_states4 self.transitions = {}5 self.final_states = set()6 """ TODO: Add methods for adding transitions, setting final states, looking up next7 state in the state transitions table, checking whether or not a state is a final 8 (accepting) state. 9 """10 def AddTrans(self, input_symbol, state, next_state):11 self.transitions[(input_symbol, state)] = next_state 12 13 def AddTransList(self, input_list, state, next_state):14 for i in input_list:15 self.AddTrans(i, state, next_state)16 def SetFinalState(self, fs):17 self.final_states.add(fs)18 def FindNextState(self, input_symbol, state):19 return self.transitions[(input_symbol, state)]20 def CheckFinalState(self, state):21 return state in self.final_states22def NDRecognize(input, fsa):23 """ TODO: Implement ND-RECOGNIZE from SLP Figure 2.19, return true or false based on 24 whether or not the fsa object accepts or rejects the input string.25 """26 if len(input) == 0:27 return False28 agenda = [(0,0)]29 current_state, index = agenda.pop()30 while True:31 if AcceptState(input, fsa, current_state, index):32 return True33 else: 34 agenda.extend(GenerateNewStates(input[index], current_state, index, fsa))35 if len(agenda) == 0:36 return False37 else:38 current_state, index = agenda.pop()39 40def GenerateNewStates(input_symbol, current_state, index, fsa):41 if (input_symbol, current_state) in fsa.transitions:42 pot_next = fsa.FindNextState(input_symbol,current_state)43 pot_idx = index + 144 result = [(pot_next,pot_idx)]45 else:46 result = []47 if ('eps', current_state) in fsa.transitions:48 result.append((fsa.FindNextState('eps', current_state),index))49 return result50 51def AcceptState(input, fsa, current_state, index):52 if index == len(input) and fsa.CheckFinalState(current_state):53 return True54 55def NDRecognizeMulti(input, fsa_list):56 """ TODO: Extend D-RECOGNIZE such that it inputs a list of FSA instead of a single 57 one. This algorithm should accept/reject input strings such as 12/31/2000 based on 58 whether or not the string is in the language defined by the FSA that is the 59 concatenation of the input list of FSA.60 """61 62 """ Notice the case "//" in seps and perhaps "8" in months/days where the first is63 suppose to have only one character and the second two characters.64 """65 index = 066 current_state = 067 temp = True68 while len(fsa_list) - current_state:69 if fsa_list[current_state] == months:70 if len(input) < 2:71 temp &= NDRecognize(input, months)72 index += 173 else:74 mm_input = input[index: index+2]75 if mm_input[-1] not in list2:76 temp &= NDRecognize(mm_input[0], months)77 index += 178 else:79 temp &= NDRecognize(mm_input, months)80 index += 281 elif fsa_list[current_state] == days:82 if len(input) < 2:83 temp &= NDRecognize(input, days)84 index += 185 else:86 dd_input = input[index: index+2]87 if dd_input[-1] not in list2:88 temp &= NDRecognize(dd_input[0], days)89 index += 190 else:91 temp &= NDRecognize(dd_input, days)92 index += 293 elif fsa_list[current_state] == years:94 temp &= NDRecognize(input[index: index+4], years)95 index += 496 elif fsa_list[current_state] == seps:97 temp &= NDRecognize(input[index: index+1], seps)98 index += 199 else:100 return False101 if temp:102 current_state += 1103 else:104 return False105 if index == len(input):106 return True107 else:108 return False109 110""" Below are some test cases. Include the output of this in your write-up and provide 111explanations. 112"""113def Test(months, days, years, seps):114 print "\nTest Months FSA"115 for input in ["", "00", "01", "09", "10", "11", "12", "13", "3", "5"]:116 print "'%s'\t%s" %(input, NDRecognizeMulti(input, [months]))117 print "\nTest Days FSA"118 for input in ["", "00", "01", "09", "10", "11", "21", "31", "32", "4", "6"]:119 print "'%s'\t%s" %(input, NDRecognizeMulti(input, [days]))120 print "\nTest Years FSA"121 for input in ["", "1899", "1900", "1901", "1999", "2000", "2001", "2099", "2100"]:122 print "'%s'\t%s" %(input, NDRecognizeMulti(input, [years]))123 print "\nTest Separators FSA"124 for input in ["", ",", " ", "-", "/", "//", ":"]:125 print "'%s'\t%s" %(input, NDRecognizeMulti(input, [seps]))126 print "\nTest Date Expressions FSA"127 for input in ["", "1/20/1990", "02/5/2016", "5/6/2000", "12 6 1988", 128 "10-6-2015", "00 6 2015", "10 50 2000", 129 "10 12 2200"]:130 print "'%s'\t%s" %(input, 131 NDRecognizeMulti(input, [months, seps, days, seps, years]))132list1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]133list2 = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]134""" Build ND-FSA for Months """135months = FSA(3)136months.SetFinalState(3)137months.AddTrans("0", 0, 1)138months.AddTrans("1", 0, 2)139months.AddTransList(list1, 1, 3)140months.AddTransList(["0", "1", "2"], 2, 3)141months.AddTrans('eps', 0, 1) # new !!142""" Build ND-FSA for Days """143days = FSA(4)144days.SetFinalState(4)145days.AddTrans("0", 0, 1)146days.AddTransList(["1", "2"], 0, 2)147days.AddTrans("3", 0, 3)148days.AddTransList(list1, 1, 4)149days.AddTransList(list2, 2, 4)150days.AddTransList(["0", "1"], 3, 4)151days.AddTrans('eps', 0, 1) # new !!152""" Build FSA for Years """153years = FSA(5)154years.SetFinalState(5)155years.AddTrans("1", 0, 1)156years.AddTrans("2", 0, 2)157years.AddTrans("9", 1, 3)158years.AddTrans("0", 2, 3)159years.AddTransList(list2, 3, 4)160years.AddTransList(list2, 4, 5)161""" Build FSA for Separators """162seps = FSA(1)163seps.SetFinalState(1)164seps.AddTransList(["/", " ", "-"], 0, 1)165""" Testing DRecognize"""166"""167result1 = NDRecognize("1",months)168print result1169result2 = NDRecognize("35",months)170print result2171result3 = NDRecognize("25",days)172print result3173result4 = NDRecognize("6",days)174print result4175"""...
pset1_dfsa.py
Source:pset1_dfsa.py
1class FSA:2 def __init__(self, num_states = 0):3 self.num_states = num_states4 self.transitions = {}5 self.final_states = set()6 """ TODO: Add methods for adding transitions, setting final states, looking up next7 state in the state transitions table, checking whether or not a state is a final 8 (accepting) state. 9 """10 def AddTrans(self, input_symbol, state, next_state):11 self.transitions[(input_symbol, state)] = next_state12 13 def AddTransList(self, input_list, state, next_state):14 for i in input_list:15 self.AddTrans(i, state, next_state)16 def SetFinalState(self, fs):17 self.final_states.add(fs)18 def FindNextState(self, input_symbol, state):19 return self.transitions[(input_symbol, state)]20 def CheckFinalState(self, state):21 return state in self.final_states22def DRecognize(input, fsa):23 """ TODO: Implement D-RECOGNIZE from SLP Figure 2.12, return true or false based on 24 whether or not the fsa object accepts or rejects the input string.25 """26 index = 027 current_state = 028 while True:29 if index == len(input):30 if fsa.CheckFinalState(current_state):31 return True32 else:33 return False34 elif (input[index], current_state) not in fsa.transitions:35 return False36 else:37 current_state = fsa.FindNextState(input[index], current_state)38 index += 139def DRecognizeMulti(input, fsa_list):40 """ TODO: Extend D-RECOGNIZE such that it inputs a list of FSA instead of a single 41 one. This algorithm should accept/reject input strings such as 12/31/2000 based on 42 whether or not the string is in the language defined by the FSA that is the 43 concatenation of the input list of FSA.44 """45 46 """ Notice the case "//" in seps and perhaps "8" in months/days where the first is47 suppose to have only one character and the second two characters.48 """49 index = 050 current_state = 051 temp = True52 while len(fsa_list) - current_state:53 if fsa_list[current_state] == months:54 temp &= DRecognize(input[index: index+2], months)55 index += 256 elif fsa_list[current_state] == days:57 temp &= DRecognize(input[index: index+2], days)58 index += 259 elif fsa_list[current_state] == years:60 temp &= DRecognize(input[index: index+4], years)61 index += 462 elif fsa_list[current_state] == seps:63 temp &= DRecognize(input[index: index+1], seps)64 index += 165 else:66 return False67 if temp:68 current_state += 169 else:70 return False71 if index == len(input):72 return True73 else:74 return False75 76""" Below are some test cases. Include the output of this in your write-up and provide 77explanations. 78"""79def Test(months, days, years, seps):80 print "\nTest Months FSA"81 for input in ["", "00", "01", "09", "10", "11", "12", "13"]:82 print "'%s'\t%s" %(input, DRecognizeMulti(input, [months]))83 print "\nTest Days FSA"84 for input in ["", "00", "01", "09", "10", "11", "21", "31", "32"]:85 print "'%s'\t%s" %(input, DRecognizeMulti(input, [days]))86 print "\nTest Years FSA"87 for input in ["", "1899", "1900", "1901", "1999", "2000", "2001", "2099", "2100"]:88 print "'%s'\t%s" %(input, DRecognizeMulti(input, [years]))89 print "\nTest Separators FSA"90 for input in ["", ",", " ", "-", "/", "//", ":"]:91 print "'%s'\t%s" %(input, DRecognizeMulti(input, [seps]))92 print "\nTest Date Expressions FSA"93 for input in ["", "12 31 2000", "12/31/2000", "12-31-2000", "12:31:2000", 94 "00-31-2000", "12-00-2000", "12-31-0000", 95 "12-32-1987", "13-31-1987", "12-31-2150"]:96 print "'%s'\t%s" %(input, 97 DRecognizeMulti(input, [months, seps, days, seps, years]))98list1 = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]99list2 = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]100""" Build FSA for Months """101months = FSA(3)102months.SetFinalState(3)103months.AddTrans("0", 0, 1)104months.AddTrans("1", 0, 2)105months.AddTransList(list1, 1, 3)106months.AddTransList(["0", "1", "2"], 2, 3)107""" Build FSA for Days """108days = FSA(4)109days.SetFinalState(4)110days.AddTrans("0", 0, 1)111days.AddTransList(["1", "2"], 0, 2)112days.AddTrans("3", 0, 3)113days.AddTransList(list1, 1, 4)114days.AddTransList(list2, 2, 4)115days.AddTransList(["0", "1"], 3, 4)116""" Build FSA for Years """117years = FSA(5)118years.SetFinalState(5)119years.AddTrans("1", 0, 1)120years.AddTrans("2", 0, 2)121years.AddTrans("9", 1, 3)122years.AddTrans("0", 2, 3)123years.AddTransList(list2, 3, 4)124years.AddTransList(list2, 4, 5)125""" Build FSA for Separators """126seps = FSA(1)127seps.SetFinalState(1)128seps.AddTransList(["/", " ", "-"], 0, 1)129""" Testing DRecognize"""130"""131result1 = DRecognize("05",months)132print result1133result2 = DRecognize("35",months)134print result2135result3 = DRecognize("25",days)136print result3137result4 = DRecognize("56",days)138print result4139"""...
setOperations.py
Source:setOperations.py
1from setGraph import *2class setOperations:3 lan=set()4 def str_str(self, x):5 a = setGraph(l=set())6 [x, i] = x.SetFromA(2)7 a.ChangeInitialState(1)8 a.AddAcceptState(i)9 a.AddTrans(a.initialStates, x.initialStates, "epsilon")10 a.AddTrans(a.initialStates, a.acceptStates[0], "epsilon")11 a.AddTrans(x.acceptStates[0], a.acceptStates[0], "epsilon")12 a.AddTrans(x.acceptStates[0], x.initialStates, "epsilon")13 a.AddTransitionToDict(x.trans)14 return a15 def str_or(self, x, y):16 a = setGraph(l=set())17 [x, i] = x.SetFromA(2)18 [y, j] = y.SetFromA(i)19 a.ChangeInitialState(1)20 a.AddAcceptState(j)21 a.AddTrans(a.initialStates, x.initialStates, "epsilon")22 a.AddTrans(a.initialStates, y.initialStates, "epsilon")23 a.AddTrans(x.acceptStates[0], a.acceptStates[0], "epsilon")24 a.AddTrans(y.acceptStates[0], a.acceptStates[0], "epsilon")25 a.AddTransitionToDict(x.trans)26 a.AddTransitionToDict(y.trans)27 return a28 def str_d(self, x, y):29 a = setGraph(l=set())30 [x, i] = x.SetFromA(1)31 [y, j] = y.SetFromA(i)32 a.ChangeInitialState(1)33 a.AddAcceptState(j-1)34 a.AddTrans(x.acceptStates[0], y.initialStates, "epsilon")35 a.AddTransitionToDict(x.trans)36 a.AddTransitionToDict(y.trans)37 return a38 def str_m(self, x):39 a = setGraph(l=set())40 a.ChangeInitialState(1)41 a.AddAcceptState(2)42 a.AddTrans(1,2,x)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!