Question

class DFA: def __init__(self, alpha, delta, accept): Constructs a DFA object!! self.alpha = alpha self.delta = delta self.aWrite a class for DFA type objects. Deterministic Finite Automata are commonly defined as a quintuple consisting of a set of states, a set of symbals, a transition function, a start state and a set of accept states

For this implementation let the alphabet be given as a string of symbols, the transition function as list of lists which represent an n by m matrix where the n rows represent the states and the m columns represent the alphabet symbols, and the accept states as a set. The states are integers 0, 1, 2, ..., n-1 and 0 is the start state. The input tape is a string of symbols from the alphabet.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

CODE:

class DFA:
    def __init__(self, alpha, delta, accept):
        self.alpha = alpha
        self.delta = delta
        self.accept = accept
    
        #transition is a dictionary storing the transition
        self.transition = {}
        for state in range(len(delta)):
            self.transition[state] = {}
            for alphabet in range(len(delta[state])):
                self.transition[state][alpha[alphabet]] = delta[state][alphabet]

    def __repr__(self):
        return "DFA with alphabet {}, transition {} and accept states {}".format(self.alpha,self.delta,self.accept)

    def accepts(self, tape):
        #as per question, initial state = 0
        present_state = 0
        for x in tape:
            #make a transition for symbol x in tape
            present_state = self.transition[present_state][x]
    
        # if we get present_state as a final state after finishing the tape,
        #then return true othetrwise return false
        if present_state in self.accept:
            return True
        else:
            return False
    
    #In complement of DFA , the final state becomes non-final and non-final become final state
    def comp(self):
        new_final_state = set()
        for i in range(len(self.delta)):
            #make those states final which are not final in current DFA
            if i not in self.accept:
                new_final_state.add(i)
        #return complemented DFA
        return DFA(self.alpha, self.delta, new_final_state)

#odd number of ’a’s
m=DFA("ab",[[1, 0], [0, 1]], {1})
print(m)
t = "aaabaaba"
print(m.accepts(t))

SCREENSHOT OF THE CODE:

class DFA: def init (self, alpha, delta, accept): self.alpha alpha self.delta = delta self.accept = accept #transition is a d#In complement of DFA, the final state becomes non-final and non-final become final state def comp (self): new_final_state =

OUTPUT:

>>> DFA with alphabet ab, transition [[1, 0], [0, 1]] and accept states set ([1]) False

Thank you.

Add a comment
Know the answer?
Add Answer to:
Write a class for DFA type objects. Deterministic Finite Automata are commonly defined as a quintuple...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me... 5. (a) Consider the deterministic finite automaton M with states S := {80,...

    Please help me... 5. (a) Consider the deterministic finite automaton M with states S := {80, 81, 82, 83}, start state so, single accepting state $3, and alphabet E = {0,1}. The following table describes the transition function T:S xHS. State 0 1 So So S1 So S1 S2 So $1 82 S3 S3 82 Draw the transition diagram for M. Let U = {01110,011100}. For each u EU describe the run for input u to M. Does M accept...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Python3 : question about object-oriented programming, Please write program in the template(critter.py),specific information is on the graphs the missing words in the 4th line are 'To save an attri...

    Python3 : question about object-oriented programming, Please write program in the template(critter.py),specific information is on the graphs the missing words in the 4th line are 'To save an attribute, attach it to this self keyword' W11 - Critters Implement the Critter class. Each critter C has attributes species, size, and age The constructor accepts arguments for the attributes above, in the order above. You can expect size and age to be numeric values Each critter C has a can_eat) method,...

  • It's my python homework. Not Java. Design type 1: # Creating an object of the class...

    It's my python homework. Not Java. Design type 1: # Creating an object of the class "DeAnzaBurger" theOrder = DeAnzaBurger() # calling the main method theOrder.main() # And the class is like: class DeAnzaBurger: # You may need to have a constructor def __init__(self): self._orderDict = {} self._priceBeforeTax = 0 self._priceAfterTax = 0 # you may have the tax rate also as an instance variable. But as I mentioned, you can use your # own deign.    .... # That...

  • can someone write me a wing101 python code for this please Question: For a projectile launched with a velocity vIm/s) at an angle to the horizontal 6 Iradl, the horizontal distance travelled (al...

    can someone write me a wing101 python code for this please Question: For a projectile launched with a velocity vIm/s) at an angle to the horizontal 6 Iradl, the horizontal distance travelled (also called the range) is given by where the altitude of the landing position is Δh higher than that of the launch position and g is the gravitational acceleration (use 9.81 m/s^2). Note: θ in theformula is in radians write a Python function that accepts ν, θ and...

  • Write a class called Course_xxx that represents a course taken at a school. Represent each student...

    Write a class called Course_xxx that represents a course taken at a school. Represent each student using the Student class from the Chapter 7 source files, Student.java. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called roll that prints all students in the course. Create a driver class...

  • Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this...

    Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...

  • This is my assignment in Python. Please help me thank you Design type 1: # Creating an object of ...

    This is my assignment in Python. Please help me thank you Design type 1: # Creating an object of the class "Burger" theOrder = Burger() # calling the main method theOrder.main() # And the class is like: class Burger: # You may need to have a constructor def __init__(self): self._orderDict = {} self._priceBeforeTax = 0 self._priceAfterTax = 0 # you may have the tax rate also as an instance variable. But as I mentioned, you can use your # own...

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT