Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
2.2 kB
2
Indexable
Never
# 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 = ["z@c.com", "ba@ba.co", "cdef@.com" , "webcom.a.bc.d.efg@z.ai.cs.stanford.com" , "foo@moo.com..com", "abqe.@book.com" , "comp@comp.com." , "redblue@orange..com" , "random@com" , "pore@a.com.com" , "www@com.comp", "wwwc@com.co" , ".two@four.com" , "food@fo.com®", "comp@company.co.com" , "computer@company.com.co" , "@abcde.com" , "people.dog.cat@.com", "cable..cord@fort.com" ]
    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()