# Define the alphabet
alphabet = {'0', '1', '2', '3', '5'}
# Define the states
states = {'q0', 'q1', 'q2', 'q3', 'q4'}
# Define the initial state
initial = 'q0'
# Define the final state
final = 'q4'
# Define the transition function
def delta (state, symbol):
# Check if the symbol is valid
if symbol not in alphabet:
return None
# Check if the state is valid
if state not in states:
return None
# Define the transition rules
if state == 'q0':
if symbol in {'1', '3', '5'}:
return 'q1'
else:
return None
elif state == 'q1':
return 'q2'
elif state == 'q2':
return 'q3'
elif state == 'q3':
if symbol == '2':
return 'q4'
else:
return 'q2'
elif state == 'q4':
return None
# Define a function to check if a string is accepted by the DFA
def accepts (string):
# Start from the initial state
current = initial
# Loop through the string
for symbol in string:
# Get the next state
current = delta (current, symbol)
# If there is no next state, reject the string
if current is None:
return False
# Check if the final state is reached
return current == final
# Test some examples
print (accepts ('13')) # True
print (accepts ('152')) # True
print (accepts ('302')) # False
print (accepts ('1234')) # False