Untitled

 avatar
unknown
plain_text
2 years ago
2.2 kB
10
Indexable
# an array with letters from a to z
a_z = list(map(chr, range(ord('a'), ord('z')+1)))

accepting_states = {"q11", "q12"}

initial_state = "q1"

def f (array):
    return lambda char: char in array

transitions = {
'q1': {f(a_z) : 'q2'}
}

def transition(state, char):
    if not state in transitions:
        return 'q0'
    for func, next_state in transitions[state].items():
        if func(char):
            return next_state
    return 'q0'

def process_string(input_string):
    # Print the starting state
    print("Start State:", initial_state)

    current_state = initial_state
    for char in input_string:
        # Print the character and current state
        # print(f"Character: {char}, Current State: {current_state}")

        # Update the current state based on the transition function
        current_state = transition(current_state, char)
        
        print(f"Character: {char}, Reached Current State: {current_state}")
    print ("End of string.")
    # Check if the final state is an accepting state
    if current_state in accepting_states:
        print("String Accepted")
    else:
        print(f"{current_state} is not a final state. String Rejected")

def main():
    print("DFA for Email Addresses")
    
    lis = ["[email protected]", "[email protected]", "[email protected]" , "[email protected]" , "[email protected]", "[email protected]" , "[email protected]." , "[email protected]" , "random@com" , "[email protected]" , "[email protected]", "[email protected]" , "[email protected]" , "[email protected]®", "[email protected]" , "[email protected]" , "@abcde.com" , "[email protected]", "[email protected]" ]
    lis=["a"]
    for item in lis:
        print(f"String: {item}")
        process_string(item)
    while False:
        user_input = input("Do you want to enter an email address? (y/n): ").strip().lower()
        if user_input == "n":
            break
        elif user_input == "y":
            input_string = input("Enter an email address: ").strip()
            print("Input Email Address:", input_string)
            process_string(input_string)
        else:
            print("Invalid input. Please enter 'y' or 'n'.")

if __name__ == "__main__":
    main()
Editor is loading...