Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
# a string of length 2 under the combination
# of string a, b
?
class DFA:
def __init__(self, sigma, q_o, q_f, Q, trans=None):
self.sigma = sigma
self.q_o = q_o
self.q_f = q_f
self.Q = Q
self.trans = trans
def display(self):
print(f"Input of DFA(sigma): {self.sigma}")
print(f"Initial State: {self.q_o}")
print(f"Final State: {self.q_f}")
print(f"Set of all possible state(Q): {self.Q}")
print(f"Transition function: {self.trans}")
if self.trans == None:
print("n Invalid String")
else:
k = self.trans.pop()
if k[1] == self.q_f:
print("n String accepted")
else:
print("n String not accepted")
def t_f(self):
s = input("Enter string: ")
t = []
j = self.q_0
for i in s:
if i == 'a' or i == 'b':
if j+1 <= self.q_f:
t.append((i, j+1))
j += 1
else:
t.append((i, 'Dead State'))
else:
t = None
break
self.trans = t
?
obj = DFA(['a', 'b'], 0, 2, [0, 1, 2, 'Dead State'])
obj.t_f()
obj.display()
Enter string:
# Write a program to design a DFA over the input
# symbol a, b which accept the string where number
# of a's are divisible by 2
?
class DFA:
def __init__(self, sigma, q_o, q_f, Q, trans=None):
self.sigma = sigma
self.q_o = q_o
self.q_f = q_f
self.Q = Q
self.trans = trans
def display(self):
print(f"Input of DFA(sigma): {self.sigma}")
print(f"Initial State: {self.q_o}")
print(f"Final State: {self.q_f}")
print(f"Set of all possible state(Q): {self.Q}")
print(f"Transition function: {self.trans}")
if self.trans == None:
print("n Invalid String")
else:
k = self.trans.pop()
if k[1] == 0:
print("n String accepted")
else:
print("n String not accepted")
def t_f(self):
s = input("Enter string: ")
t = []
c = 1
for i in s:
if i == 'a' and c % 2 != 0:
t.append(('a', 1))
c += 1
elif i == 'a' and c % 2 == 0:
t.append(('a', 0))
c += 1
elif i == 'b':
if t[c-2][1] == 0:
t.append(('b', 0))
if t[c-2][1] == 1:
t.append(('b', 1))
else:
t = None
break
self.trans = t
?
obj = DFA(['a', 'b'], 0, 1, [0, 1])
obj.t_f()
obj.display()
# Write a DFA class to simulate the behaviours
# of DFA which accepts there string in which
# substring is abc. Create an object of this
# class and check the validity of input string
?
class DFA:
def __init__(self, sigma, q_o, q_f, Q, trans=None):
self.sigma = sigma
self.q_o = q_o
self.q_f = q_f
self.Q = Q
self.trans = trans
def display(self):
print(f"Input of DFA(sigma): {self.sigma}")
print(f"Initial State: {self.q_o}")
print(f"Final State: {self.q_f}")
print(f"Set of all possible state(Q): {self.Q}")
print(f"Transition function: {self.trans}")
if self.trans == None:
print("n Invalid String")
else:
k = self.trans.pop()
if k[1] == self.q_f:
print("n String accepted")
else:
print("n String not accepted")
def t_f(self):
s = input("Enter string: ")
t = []
j = self.q_o
for i in s:
if i == 'a' or i == 'b':
if j == 0:
if i == 'a':
t.append((i, j+1))
j += 1
else:
t.append((i, j))
elif j == 1:
if i == 'b':
t.append((i, j+1))
j += 1
else:
t.append((i, j))
elif j == 2:
if i == 'a':
t.append((i, j+1))
j += 1
else:
t.append((i, j))
elif j == 3:
if i == 'a' or i == 'b':
t.append((i, j))
else:
t = None
break
self.trans = t
?
obj = DFA(['a', 'b'], 0, 3, [0, 1, 2, 3])
obj.t_f()
obj.display()
# Write a program to design a DFA which accept
# all the string of a, b where string start
# and end with same symbol
?
s = input("Enter string")
c = 0;
for i in s:
if i == 'a' or i == 'b':
c += 1
if c == len(s):
if c == 1:
print("String Accepted")
else:
if s[0] == s[len(s) - 1 ]:
print("String Accepted")
else:
print("String not Accepted")
else:
print("Invalid input")
?
# class
#
class DFA:
def __init__(self, sigma, q_o, q_f, Q, trans=None):
self.sigma = sigma
self.q_o = q_o
self.q_f = q_f
self.Q = Q
self.trans = trans
def display(self):
print(f"Input of DFA(sigma): {self.sigma}")
print(f"Initial State: {self.q_o}")
print(f"Final State: {self.q_f}")
print(f"Set of all possible state(Q): {self.Q}")
print(f"Transition function: {self.trans}")
if self.trans == None:
print("n Invalid String")
else:
k = self.trans.pop()
if k[1] == self.q_f:
print("n String accepted")
else:
print("n String not accepted")
def t_f(self):
s = input("Enter string: ")
t = []
j = self.q_o
for i in s:
if i == 'a' or i == 'b':
if j == 0:
if i == 'a':
t.append((i, j+1))
j += 1
elif i == 'b':
t.append((i, j+2))
j += 2
elif j == 1:
if i == 'a':
t.append((i, j))
elif i == 'b':
t.append((i, j+2))
j += 2
elif j == 2:
if i == 'a':
t.append((i, j+2))
j += 2
elif i == 'b':
t.append((i, j))
elif j == 3:
if i == 'a':
t.append((i, j-2))
j -= 2
elif j == 4:
if i == 'a':
t.append((i, j))
elif i == 'b':
t.append((i, j-2))
j -= 2
else:
t = None
break
self.trans = t
?
obj = DFA(['a', 'b'], 0, [1, 2], [0, 1, 2, 3, 4])
obj.t_f()
obj.display()
# Write a program to design a DFA that accept
# all string of 0 and 1 where string contain
# even number of 1's and odd number of 0's
?
class DFA:
def __init__(self, sigma, q_o, q_f, Q, trans=None):
self.sigma = sigma
self.q_o = q_o
self.q_f = q_f
self.Q = Q
self.trans = trans
def display(self):
print(f"Input of DFA(sigma): {self.sigma}")
print(f"Initial State: {self.q_o}")
print(f"Final State: {self.q_f}")
print(f"Set of all possible state(Q): {self.Q}")
print(f"Transition function: {self.trans}")
if self.trans == None:
print("n Invalid String")
else:
k = self.trans.pop()
if k[1] == self.q_f:
print("n String accepted")
else:
print("n String not accepted")
def t_f(self):
s = input("Enter string: ")
t = []
j = self.q_o
for i in s:
if i == '0' or i == '1':
if j == 0:
if i == '0':
t.append((i, j+1))
j += 1
elif i == '1':
t.append((i, j+3))
j += 3
elif j == 1:
if i == '0':
t.append((i, j-1))
j -= 1
elif i == '1':
t.append((i, j+1))
j += 1
elif j == 2:
if i == '0':
t.append((i, j+1))
j += 1
elif i == '1':
t.append((i, j-1))
j -= 1
elif j == 3:
if i == '0':
t.append((i, j-1))
j -= 1
elif i == '1':
t.append((i, j-3))
j -= 3
else:
t = None
break
self.trans = t
?
obj = DFA(['0', '1'], 0, 1, [0, 1, 2, 3])
obj.t_f()
obj.display()
# Write a program to design a DFA which recognise
# the identifier of python language in given string
?
lst = [
'False', 'def', 'if', 'raise', 'None', 'del', 'import', 'return',
'True', 'elif', 'in', 'try', 'and', 'else', 'is', 'while',
'as', 'except', 'lambda', 'with', 'assert', 'finally', 'nonlocal', 'yield',
'break', 'for', 'not', 'class', 'from', 'or',
'continue', 'global', 'pass'
]
?
string = input("Enter the string")
c = ord(string[0])
?
if string in lst:
print("Not Accepted")
elif c in range(97, 122) or c in range(65, 75) or string[0] == '__':
print("Accepted")
else:
print("Not Accepted")
![]() |
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team