Code:-
#Shortest DFA implementation in Python
S,D,F=input()
s=1
for c in S:s=D[s,c]
print(["Not a chance!","Accepted!"][F&s>0])
Input:- Input is a triple of string S, a delta function D, and final state mask F. I number each state with a power of 2, so F is just the OR of each accepting state. D is a map from (state,input char) -> state.
Example input (accepts all strings ending in b):
'abab',{(1,'a'):1,(1,'b'):2,(2,'a'):1,(2,'b'):2},2
Output:-
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-506f09a31940> in <module>()
1 #Shortest DFA implementation in Python
----> 2 S,D,F=input()
3 s=1
4 for c in S:s=D[s,c]
5 print(["Not a chance!","Accepted!"][F&s>0])
ValueError: too many values to unpack (expected 3)
Similarly, the following code is for implementing an NFA. But it is throwing a different kind of error, even after adding the eval(). The code is as follows:-
#Shortest NFA implementation
S,D,F=eval(input())
s=1
for c in S:
t,s=s,0
for a,b in D[c]:s|=t/a%2*b
print(["Not a chance!","Accepted!"][F&s>0])
Input:- We number states as powers of 2 as before. D is a map from input character to a list of transitions labelled by that character.
example input (accepts all strings ending in b):
'abab',{'a':[(1,1)],'b':[(1,1),(1,2)]},2
The output obtained is:-
'abab',{'a':[(1,1)],'b':[(1,1),(1,2)]},2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-34-7e5418d2d6fc> in <module>()
4 for c in S:
5 t,s=s,0
----> 6 for a,b in D[c]:s|=t/a%2*b
7 print(["Not a chance!","Accepted!"][F&s>0])
TypeError: unsupported operand type(s) for |=: 'int' and 'float'
Please tell me the necessary but minimal modifications required in the above codes for it to execute successfully and produce the desired output.
This code is valid in Python 2, not in Python 3. I suspect the code is actually designed for Python 2 when you are using Python 3. input
in Python 2 not only reads in a string from the input, but it also performs an eval
statement which means that it will take the string and convert it into Python syntax. input
in Python 3 returns a string of what you put in. The eval
is not performed anymore as you see in Python 2. To mimic what you see in Python 2, you need to run eval
on top of input
:
In [1]: S,D,F = eval(input())
'abab',{(1,'a'):1,(1,'b'):2,(2,'a'):1,(2,'b'):2},2
In [2]: S
Out[2]: 'abab'
In [3]: D
Out[3]: {(1, 'a'): 1, (1, 'b'): 2, (2, 'a'): 1, (2, 'b'): 2}
In [4]: F
Out[4]: 2
With your new code that you're running, the culprit is the division operation in the loop: t/a
. This returns a floating-point number by default whereas in Python 2, if t
and a
are both integers, it will return an integer. You have to be careful running Python 2 code in a Python 3 environment. Simply wrap t/a
in an int
call: int(t/a)
.
In [7]: S,D,F=eval(input())
...: s=1
...: for c in S:
...: t,s=s,0
...: for a,b in D[c]:s|=int(t/a)%2*b
...: print(["Not a chance!","Accepted!"][F&s>0])
...:
'abab',{'a':[(1,1)],'b':[(1,1),(1,2)]},2
Accepted!
My final note to you is to be exceptionally careful running Python 2 code in a Python 3 environment. If possible, run the code in the environment it's designed for. If not, this useful comparison guide of idioms from Python 2 to Python 3 should help: http://python-future.org/compatible_idioms.html. In particular:
input
: http://python-future.org/compatible_idioms.html#input