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...
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 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 in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
Language: Python 3 Compose a function process which accepts a string filename. process should return a...
Language: Python 3 Compose a function process which accepts a string filename. process should return a list of records contained in the file. #define your function here # Create a blank list called `entries` and an empty string called `current_style`. # Open the file `filename`, read the data using readlines(), and close it. # Loop through each line of the file and do the following: # Strip the whitespace off of the ends of the line using the `strip` method....
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT