-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswLab02Work.py
173 lines (136 loc) · 5.36 KB
/
swLab02Work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import lib601.sm as sm
##################################
# Double Delay SM
##################################
class Delay2Machine(sm.SM):
def __init__(self, val0, val1):
self.startState = (val0, val1)
def getNextValues(self, state, inp):
(previousPreviousInput, previousInput) = state
return ((previousInput, inp), previousPreviousInput)
def runTestsDelay():
# Testcases for Delay2Machine using transduce.
print 'Test1:', Delay2Machine(100, 10).transduce([1,0,2,0,0,3,0,0,0,4])
print 'Test2:', Delay2Machine(10, 100).transduce([0,0,0,0,0,0,1])
print 'Test3:', Delay2Machine(-1, 0).transduce([1,2,-3,1,2,-3])
# Test that self.state is not being changed.
m = Delay2Machine(100, 10)
m.start()
[m.getNextValues(m.state, i) for i in [-1,-2,-3,-4,-5,-6]]
print 'Test4:', [m.step(i) for i in [1,0,2,0,0,3,0,0,0,4]]
print runTestsDelay()
# execute runTestsDelay() to carry out the testing, you should get:
#Test1: [100, 10, 1, 0, 2, 0, 0, 3, 0, 0]
#Test2: [10, 100, 0, 0, 0, 0, 0]
#Test3: [-1, 0, 1, 2, -3, 1]
#Test4: [100, 10, 1, 0, 2, 0, 0, 3, 0, 0]
# DONE!
##################################
# Comments SM
##################################
x1 = '''def f(x): # func
if x: # test
# comment
return 'foo' '''
x2 = '''#initial comment
def f(x): # func
if x: # test
# comment
return 'foo' '''
class CommentsSM(sm.SM):
# Don't need to initializate because you have no value to begin.
startState = 'close'
# I choose the state close: when it is not a comment. and opne: when it is a comment.
def getNextValues(self, state, inp):
if inp == '#':
if state == 'close':
return ('open', inp)
elif inp == '\n':
if state == 'open':
return ('close', None)
else:
if state == 'open':
return ('open', inp)
if state == 'close':
return ('close', None)
def runTestsComm():
m = CommentsSM()
# Return only the outputs that are not None
print 'Test1:', [c for c in CommentsSM().transduce(x1) if not c==None]
print 'Test2:', [c for c in CommentsSM().transduce(x2) if not c==None]
# Test that self.state is not being changed.
m = CommentsSM()
m.start()
[m.getNextValues(m.state, i) for i in ' #foo #bar']
print 'Test3:', [c for c in [m.step(i) for i in x2] if not c==None]
print runTestsComm()
# execute runTestsComm() to carry out the testing, you should get:
#Test1: ['#', ' ', 'f', 'u', 'n', 'c', '#', ' ', 't', 'e', 's', 't', '#', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't']
#Test2: ['#', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't', '#', ' ', 'f', 'u', 'n', 'c', '#', ' ', 't', 'e', 's', 't', '#', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't']
#Test3: ['#', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't', '#', ' ', 'f', 'u', 'n', 'c', '#', ' ', 't', 'e', 's', 't', '#', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't']
# DONE!
##################################
# First Word SM
##################################
# Test 1
test1 = '''hi
ho'''
# This can also be writtent as:
# test1 = 'hi\nho'
#Test 2
test2 = ''' hi
ho'''
# This can also be writtent as:
# test2 = ' hi\nho'
#Test 3
test3 = '''
hi
ho ho ho
ha ha ha'''
# This can also be writtent as:
# test3 ='\n\n hi \nho ho ho\n\n ha ha ha'
class FirstWordSM(sm.SM):
# States I am looking for the first word.
startState = 'looking'
def getNextValues(self, state, inp):
if inp == ' ':
# Looking for still.
if state == 'looking':
# Keep saying I am still looking for.
return ('looking', None)
# Regardless of the state now, I am not looking for anymore.
# Therefore I'll just say 'not' and None.
else:
return ('not', None)
elif inp == '\n':
# Regardless of the state, it should start to look for again.
return ('looking', None)
else:
# I am looking for the first word.
if state == 'looking':
# Found the first word.
return ('found', inp)
# Keep showing what the first word is.
if state == 'found':
return ('found', inp)
# I already did my job with the first word.
if state == 'not':
return ('not', None)
def runTestsFW():
m = FirstWordSM()
print 'Test1:', m.transduce(test1)
print 'Test2:', m.transduce(test2)
print 'Test3:', m.transduce(test3)
m = FirstWordSM()
m.start()
[m.getNextValues(m.state, i) for i in '\nFoo ']
print 'Test 4', [m.step(i) for i in test1]
print runTestsFW()
# execute runTestsFW() to carry out the testing, you should get:
# HINT: I just needed to recognize that I would need to use three states instead of 2. I can use as many states I want...
# states --> looking for the first word, already found the first word, I am not in the first word anymore.
# DONE!
#Test1: ['h', 'i', None, 'h', 'o']
#Test2: [None, None, 'h', 'i', None, 'h', 'o']
#Test3: [None, None, None, 'h', 'i', None, None, 'h', 'o', None, None, None, None, None, None, None, None, None, 'h', 'a', None, None, None, None, None, None]
#Test4: ['h', 'i', None, 'h', 'o']