Python Forum
Take use input in sets and Dictionaries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Take use input in sets and Dictionaries (/thread-14172.html)



Take use input in sets and Dictionaries - usman - Nov-18-2018

class DFA:
    current_state = None;
    def __init__(self, states, alphabet, transition_function, start_state, accept_states):
        self.states = states;
        self.alphabet = alphabet;
        self.transition_function = transition_function;
        self.start_state = start_state;
        self.accept_states = accept_states;
        self.current_state = start_state;
        return;
    
    def transition_to_state_with_input(self, input_value):
        if ((self.current_state, input_value) not in self.transition_function.keys()):
            self.current_state = None;
            return;
        self.current_state = self.transition_function[(self.current_state, input_value)];
        return;
    
    def in_accept_state(self):
        return self.current_state in accept_states;
    
    def go_to_initial_state(self):
        self.current_state = self.start_state;
        return;
    
    def run_with_input_list(self, input_list):
        self.go_to_initial_state();
        for inp in input_list:
            self.transition_to_state_with_input(inp);
            continue;
        return self.in_accept_state();
    pass;



states = {0, 1, 2, 3};
alphabet = {'a', 'b', 'c', 'd'};

tf = dict();
tf[(0, 'a')] = 1;
tf[(0, 'b')] = 2;
tf[(0, 'c')] = 3;
tf[(0, 'd')] = 0;
tf[(1, 'a')] = 1;
tf[(1, 'b')] = 2;
tf[(1, 'c')] = 3;
tf[(1, 'd')] = 0;
tf[(2, 'a')] = 1;
tf[(2, 'b')] = 2;
tf[(2, 'c')] = 3;
tf[(2, 'd')] = 0;
tf[(3, 'a')] = 1;
tf[(3, 'b')] = 2;
tf[(3, 'c')] = 3;
tf[(3, 'd')] = 0;
start_state = 0;
accept_states = {2, 3};

d = DFA(states, alphabet, tf, start_state, accept_states);

inp_program = list('abcdabcdabcd');

print (d.run_with_input_list(inp_program))
This is DFA program which takes States and Alphabets (as hard coded values) and then add them in dictionary
and for each states and alphabets we give some value to dictionary

I want to make this program user defined by
1. Takes input from user and add in set named "Set"
2. Takes input from user and add in set named "alphabets"
3 for each pair of sets and alphabets ask user to add value

Can someone help me ?

Thanks


RE: Take use input in sets and Dictionaries - Larz60+ - Nov-18-2018

Is this a paying job?


RE: Take use input in sets and Dictionaries - usman - Nov-18-2018

(Nov-18-2018, 09:36 AM)Larz60+ Wrote: Is this a paying job?

No this is my school assignment


RE: Take use input in sets and Dictionaries - stranac - Nov-18-2018

In that case, we'll need to see some effort from you.
What did you try to get the results you want and what problem are you facing?


RE: Take use input in sets and Dictionaries - usman - Nov-18-2018

(Nov-18-2018, 12:30 PM)stranac Wrote: In that case, we'll need to see some effort from you.
What did you try to get the results you want and what problem are you facing?

I have tried to get input from user and add it to sets , i was succeeded but how to take user input in loop from two sets