jfz-2023-s473579/TaskC02/run.py

66 lines
1.8 KiB
Python
Raw Normal View History

2023-11-16 21:40:35 +01:00
import sys
2023-11-19 17:38:19 +01:00
2023-11-16 21:40:35 +01:00
sys.setrecursionlimit(5000)
2023-11-25 23:43:26 +01:00
arg = [['0', '0', 'a'],
['0', '0', 'b'],
['0' ,'0', 'c'],
['0', '1', 'a'],
['1','2', 'b'],
['2', '0', 'a'],
['2' ,'0', 'b'],
['2', '0', 'c'],
'2']
2023-11-16 21:40:35 +01:00
def write_answer(answer):
with open(output_file, 'a') as file:
file.write(answer+'\n')
def line_checking(row, position):
2023-11-19 17:38:19 +01:00
stack = [(row, position)]
while stack:
help_row, help_position = stack.pop()
if help_row == '\n':
if help_position in what_is_the_ended_positions():
return True
2023-11-16 21:40:35 +01:00
else:
2023-11-19 17:38:19 +01:00
return False
next_character_array = find_next_position(help_position, help_row[0])
if next_character_array:
for element_in_next_position_array in next_character_array:
2023-11-16 21:40:35 +01:00
2023-11-19 17:38:19 +01:00
stack.append((help_row[1:], element_in_next_position_array))
return False
2023-11-16 21:40:35 +01:00
def find_next_position(position, character):
2023-11-25 23:43:26 +01:00
take_all_possible_positions = []
for row_used_table in arg:
if len(row_used_table) != 1:
if position == row_used_table[0] and character == row_used_table[2]:
take_all_possible_positions.append(row_used_table[1])
2023-11-16 21:40:35 +01:00
2023-11-25 23:43:26 +01:00
return take_all_possible_positions
2023-11-19 17:38:19 +01:00
2023-11-16 21:40:35 +01:00
def what_is_the_ended_positions():
array = []
2023-11-25 23:43:26 +01:00
for row in arg:
if len(row) == 1:
array += row
return array
2023-11-19 17:38:19 +01:00
# used_table = 'test.arg'
# input_file = 'test.in'
# output_file = 'test.out'
2023-11-25 23:43:26 +01:00
# used_table = sys.argv[1]
input_file = sys.argv[1]
output_file = sys.argv[2]
2023-11-16 21:40:35 +01:00
with open(output_file, 'w') as readed_output_file:
with open(input_file, 'r') as readed_input_file:
for row_input_file in readed_input_file:
2023-11-19 17:38:19 +01:00
if line_checking(row_input_file, '0'):
2023-11-16 21:40:35 +01:00
write_answer('YES')
else:
write_answer('NO')