},
        # e is the state (choose item)
        'e': {
            '0': 'trap',
            '1': 'f'
        },
        #f is the state dispense item
        'f': {
            '0': 'a',
            '1': 'g'
        },
        # g is the state (balance) where the customer gets his balance if any
        'g': {
            '0': 'a',
            '1': 'a'
        },
        # trap state
        'trap': {
            '0': 'trap',
            '1': 'trap'
        }
    },
    initial_state='a',
    final_states={'g', 'f'})
#'1101111' is accepted and when we changed the value to '111110' the output will be  rejected
if (dfa.accepts_input('1101111')):
    print("Accepted")

else:
    print("Rejected")
from automata.fa.dfa import DFA

Machine = DFA(states={"0", "1", "2", "3"},
              input_symbols={'a', 'b'},
              transitions={
                  "0": {
                      "a": "1",
                      "b": "3"
                  },
                  "1": {
                      "a": "0",
                      "b": "2"
                  },
                  "2": {
                      "a": "3",
                      "b": "1"
                  },
                  "3": {
                      "a": "2",
                      "b": "0"
                  }
              },
              initial_state="0",
              final_states={"0"})
string = input("Enter the input you want to recognise")
if Machine.accepts_input(string):
    print('accepted')
else:
    print('rejected')
Beispiel #3
0
        'st3':{'0':'st4(trap)','1':'st5'}, # st3 is the state called validate money where if it is true then it goes to item selection(st5) or if it is false then it goes to eject money state(st4) which is trap state
        
        # trap state
        'st4(trap)':{'0':'st4(trap)','1':'st4(trap)'},
        
        'st5':{'0':'st5','1':'st6'}, # st5 is the state selection of item. If its true then it checks the amount of money which is st6
        
        'st6':{'0':'st5','1':'st7'}, # st6 is the state to check the amount of money. If its true then it proceeds to next state otherwise it goes back to the st5 to select item again.
         
        'st7':{'0':'st5','1':'st8'}, # st7 is the checking availability state. If the item is available then it drops the item which is st8 or final state otherwise it goes back to the st5 to select item again.
         
        'st8':{'0':'st1','1':'st1'}, # st8 is the state where it drops the item and finishes the procedure
        
        
        
        
    },
   initial_state = 'st1',
   final_states ={'st8','st9(cancel)'}

)


# Testing phase...
# U can try many examples you want
if (dfa.accepts_input('1011111')): # here I am testing string '1011111' which will be accepted but if we try '1110' then it will be rejected
    print("Accepted")
    
else:
    print("Rejected")
        break
    else:
        print(
            "Please enter STATE_STATE and ACCEPT_STATES that are in Q : {}.\nAccept states should be a valid subset of Q\n"
            .format(Q))

CURRENT_STATE = None
dfa = DFA(states=Q,
          input_symbols=SIGMA,
          transitions=transition_dict,
          initial_state=initial,
          final_states=set(final))
if dfa.validate():
    print("\nThe DFA is accepted.")
    s = input("Enter the Input String: ")
    if dfa.accepts_input(s):
        print('Accepted!')
        print("The final state that the DFA stopped on:", dfa.read_input(s))
    else:
        print('Rejected!')
else:
    print("\nThe DFA is rejected.")
""" Non-Deterministic Finite Automata (NFA) """

nfa = NFA(
    states={'q0', 'q1', 'q2'},
    input_symbols={'a', 'b'},
    transitions={
        'q0': {
            'a': {'q1'}
        },
#Q. Write a deterministic automata code for the language with ∑={0,1} accepts the set of all strings with three consecutive 1's.
#!pip install automata-lib


from automata.fa.dfa import DFA 

dfa = DFA( states={'q0', 'q1', 'q2','q3'}, input_symbols={'0', '1'}, transitions={ 'q0': {'1': 'q1','0':'q0'},
'q1': {'0': 'q2', '1': 'q1'}, 'q2': {'0': 'q2', '1': 'q3'},
'q3': {'0': 'q3', '1': 'q3'},
},initial_state='q0', final_states={'q3 '} )

dfa.read_input('111')

if dfa.accepts_input('111'): 
    print('accepted' ) 

else: 
    print('rejected')


#Q. Write a deterministic automata code for the language with ∑={0,1} accepts even Number of 0's and even number of 1's.
#pip install automata-lib

from automata.fa.dfa import DFA 

dfa = DFA(
states={'q0', 'q1', 'q2','q3'},
input_symbols={'0', '1'}, transitions={ 'q0': {'1': 'q1','0':'q0'}, 'q1': {'0': 'q2', '1': 'q1'},
'q2': {'0': 'q2', '1': 'q3'},
'q3': {'0': 'q3', '1': 'q3'},
 },