0

I am new to Python programming and I am trying to write code to check if a DFA( Deterministic Finite Automata) has empty language or not.

I am using 2-d array to store state of the DFA. On executing the code I keep getting list index out of range. How can I fix this?

Below is the code

top=-1
count=0
n=int(input("\nEnter the no of states"))
mat=[[]]
b=[]
print("\nEnter the transition table")
for i in range(0,n):
   for j in range(0,n):
    mat.append(input())
finalState=input("\nEnter the final state:")
startState=input("\nEnter the start state:")      

for i in range(0,n):
   for j in range(0,n):
      if mat[i][j]:
        b[++top]=j
for k in range(top):
      if b[k]==finalState:
      count+=1
if count>0:
print("\nLanguage is  not empty")
else:
print("\nLanguage is empty")
2
  • In b[++top]=j I am getting list assignment index out of range Commented Sep 23, 2015 at 13:50
  • I'll edit my answer to address that problem too. Commented Sep 23, 2015 at 13:58

1 Answer 1

6

when you make a 2x2 table, you want mat to be [[1,2],[3,4]], but you're getting [[],1,2,3,4] right now.

Instead, try:

mat = []
for i in range(n):
    row = []
    for j in range(n):
        row.append(input())
    mat.append(row)

Also, Python does not have a "++" operator, so b[++top]=j is the same as b[top] = j. If you want to increment top, you have to do that on its own line before you use it to index the list.

On top of that, b has zero elements, so indexing it in any way will cause a crash. If you're trying to increase the size of b by adding new items to it, use append. Then you don't need a top variable at all.

b.append(j)
Sign up to request clarification or add additional context in comments.

1 Comment

Really sorry for late reply, and thanks for helping me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.