Skip to content

Instantly share code, notes, and snippets.

@cscorley
Created January 30, 2012 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscorley/1702625 to your computer and use it in GitHub Desktop.
Save cscorley/1702625 to your computer and use it in GitHub Desktop.
generates the number of valid strings for DFA in a particular homework problem I had
def A(s, c):
if len(s) <= c:
return False
if s[c] == '0':
return B(s, c+1)
elif s[c] == '1':
return C(s, c+1)
def B(s, c):
if len(s) <= c:
return False
if s[c] == '1':
return D(s, c+1)
else:
return False
def C(s, c):
if len(s) <= c:
return False
if s[c] == '0':
return D(s, c+1)
else:
return False
def D(s, c):
if len(s) <= c:
return True
if s[c] == '0':
return A(s, c+1)
elif s[c] == '1':
return B(s, c+1)
def gencheck(k):
# generate strings of len k
count = 0
for i in range(0, 2**k):
s = bin(i)
s = s[2:]
if len(s) < k:
s = ('0' * (k-len(s))) + s
if A(s, 0):
count += 1
print('N(%d) = %d' % (k, count))
if __name__ == '__main__':
for i in range(0, 20):
gencheck(i)
N(0) = 0
N(1) = 0
N(2) = 2
N(3) = 0
N(4) = 2
N(5) = 4
N(6) = 2
N(7) = 8
N(8) = 10
N(9) = 12
N(10) = 26
N(11) = 32
N(12) = 50
N(13) = 84
N(14) = 114
N(15) = 184
N(16) = 282
N(17) = 412
N(18) = 650
N(19) = 976
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment