Question

In: Computer Science

Write a python function to fulfill the requirements. The function accepts a string, a current state,...

Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.

            ### Finite State Machine Simulator in Python

### Provide s1 and s2 that are both accepted, but s1 != s2.

s1 = "bdf"

s2 = "bdgbdf"

edges = {(1,'a') : 2,

               (1,'b') : 3,

               (2,'c') : 4,

               (3,'d') : 5,

               (5,'c') : 2,

               (5,'f') : 6,

               (5,'g') : 1}

accepting = [6]

def fsmsim(string, current, edges, accepting):

   ### Your code here.

### Test the function

print(fsmsim(s1,1,edges,accepting))

# >>> True

print(fsmsim(s2,1,edges,accepting))

# >>> True

print(s1 != s2)

# >>> True

Solutions

Expert Solution

Python program :

def fsmsim(string, current , edges, accepting):
    
    for s in string:             # iterate through all characters in string
        state = edges[current,s] # go to the next state by traversing on the given current state and character in string 
        current = state
    if(current == accepting[0]): # check if both the state we got in current is equal to the accepting
        return True              # return true if accepting
    else:
        return False             # return false if not accepting
        
s1 = "bdf"
s2 = "bdgbdf"
edges = {(1,'a') : 2, (1,'b') : 3, (2,'c') : 4, (3,'d') : 5, (5,'c') : 2, (5,'f') : 6, (5,'g') : 1}
accepting = [6]

print(fsmsim(s1,1,edges,accepting)) # returns True as final state is accepting state

print(fsmsim(s2,1,edges,accepting)) # returns True as final state is accepting state

print(s1 != s2)  # return True as both strings are not equal

Screenshot :


Related Solutions

Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string....
Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string. The function should return a greeting message based on the hour given in the time object as follow: If the time is before noon: 'Good Morning.', if it is in the afternoon: 'Good Day.' Please use the following template for your python script to define the greetings() function and name it as mt_q3.py. Replace the place holder [seneca_id] with your Seneca email user name....
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',',...
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',', '!', '.') characters from the string, and returns the number of punctuation characters removed. For example, if the string contains ['C', 'p', 't', 'S', ',', '1', '2', '1', '.', 'i', 's', 'f', 'u', 'n', '!', '\0'], then the function should remove the punctuation characters. The function must remove the characters by shifting all characters to the right of each punctuation character, left by one...
In c++ Write a function that: accepts a string and an integer as it's only arguments...
In c++ Write a function that: accepts a string and an integer as it's only arguments uses the argument to open a file for writing writes the integer to the file if it fails to open the file, returns -1 if it succeeds in opening the file, returns 0 does not interact with the user in any way does not use global variables
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a function called format_name that accepts a string in the format of first name followed...
Write a function called format_name that accepts a string in the format of first name followed by last name as a parameter, and then returns a string in reverse order (i.e., last name, first name). You may assume that only a first and last name will be given. For example, format_name("Jared Smith") should return "Smith, Jared" Hint: The following String Methods will be useful: # Returns the lowest index in the string where substring is # found. Returns -1 if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT