From 3fc990152e429501721e674c58f5528a9e82acfa Mon Sep 17 00:00:00 2001 From: wojtekgoralewski Date: Sat, 11 Nov 2023 20:54:01 +0100 Subject: [PATCH] task b05 --- TaskB04/run.py | 5 ++-- TaskB04/test.out | 8 ------ TaskB05/fsa_description.arg | 56 +++++++++++++++++++++++++++++++++++++ TaskB05/run.py | 45 +++++++++++++++++++++++++++++ TaskB05/test.out | 6 ++++ 5 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 TaskB05/fsa_description.arg create mode 100644 TaskB05/run.py create mode 100644 TaskB05/test.out diff --git a/TaskB04/run.py b/TaskB04/run.py index e4b2fb9..654f39c 100644 --- a/TaskB04/run.py +++ b/TaskB04/run.py @@ -6,7 +6,7 @@ def read_aut(fsa_path): accepting_states = set() for line in file: parts = line.strip().split() - print(parts) + #print(parts) if len(parts) == 1: accepting_states.add(int(parts[0])) #print(accepting_states) @@ -15,7 +15,7 @@ def read_aut(fsa_path): state_to = int(parts[1]) symbol = parts[2] transisions[(state_from, symbol)] = state_to - print(transisions) + #print(transisions) return transisions, accepting_states #print(transisions) @@ -24,6 +24,7 @@ def is_accepting(transitions, accepting_states, line): for symbol in line.strip(): if (current_state, symbol) in transitions: current_state = transitions[(current_state, symbol)] + #print(current_state) else: return "NO" if current_state in accepting_states: diff --git a/TaskB04/test.out b/TaskB04/test.out index 375544b..d40ad22 100644 --- a/TaskB04/test.out +++ b/TaskB04/test.out @@ -1,11 +1,3 @@ -['0', '1', '1'] -['0', '2', '0'] -['1', '1', '1'] -['1', '2', '0'] -['2', '2', '1'] -['2', '1', '0'] -['2'] -{(0, '1'): 1, (0, '0'): 2, (1, '1'): 1, (1, '0'): 2, (2, '1'): 2, (2, '0'): 1} YES YES NO diff --git a/TaskB05/fsa_description.arg b/TaskB05/fsa_description.arg new file mode 100644 index 0000000..cff81f0 --- /dev/null +++ b/TaskB05/fsa_description.arg @@ -0,0 +1,56 @@ +0 1 1 +0 0 0 +0 0 2 +0 0 3 +0 0 4 +0 0 5 +0 0 6 +0 0 7 +0 0 8 +0 0 9 +0 0 x +1 0 0 +1 1 1 +1 0 2 +1 0 3 +1 0 4 +1 0 5 +1 0 6 +1 0 7 +1 0 8 +1 2 9 +1 0 x +2 0 x +2 3 0 +2 3 1 +2 3 2 +2 3 3 +2 3 4 +2 3 5 +2 3 6 +2 3 7 +2 3 8 +2 3 9 +3 0 x +3 4 0 +3 4 1 +3 4 2 +3 4 3 +3 4 4 +3 4 5 +3 4 6 +3 4 7 +3 4 8 +3 4 9 +4 4 0 +4 4 1 +4 4 2 +4 4 3 +4 4 4 +4 4 5 +4 4 6 +4 4 7 +4 4 8 +4 4 9 +4 4 x +4 \ No newline at end of file diff --git a/TaskB05/run.py b/TaskB05/run.py new file mode 100644 index 0000000..654f39c --- /dev/null +++ b/TaskB05/run.py @@ -0,0 +1,45 @@ +import sys + +def read_aut(fsa_path): + with open(fsa_path, "r", encoding="utf8") as file: + transisions = {} + accepting_states = set() + for line in file: + parts = line.strip().split() + #print(parts) + if len(parts) == 1: + accepting_states.add(int(parts[0])) + #print(accepting_states) + if len(parts) == 3: + state_from = int(parts[0]) + state_to = int(parts[1]) + symbol = parts[2] + transisions[(state_from, symbol)] = state_to + #print(transisions) + return transisions, accepting_states + #print(transisions) + +def is_accepting(transitions, accepting_states, line): + current_state = 0 #zakładam, że zawsze stan początkowy to 0 dla uproszczenia + for symbol in line.strip(): + if (current_state, symbol) in transitions: + current_state = transitions[(current_state, symbol)] + #print(current_state) + else: + return "NO" + if current_state in accepting_states: + return "YES" + else: + return "NO" + + + +fsa_path = sys.argv[1] +transitions, accepting_states = read_aut(fsa_path) + +for line in sys.stdin: + result = is_accepting(transitions, accepting_states, line) + #sys.stdout.write(result + '\n') + print(result) + +#python run.py fsa_description.arg < test1.in > test1.out \ No newline at end of file diff --git a/TaskB05/test.out b/TaskB05/test.out new file mode 100644 index 0000000..4877f25 --- /dev/null +++ b/TaskB05/test.out @@ -0,0 +1,6 @@ +NO +YES +NO +NO +YES +YES