Ended TaskC05

This commit is contained in:
IgnBys 2023-11-21 17:14:36 +01:00
parent bb0d611890
commit 96e7136c5e
6 changed files with 78 additions and 45 deletions

View File

@ -1 +1,2 @@
dom
biały

2
TaskC05/elem.out Normal file
View File

@ -0,0 +1,2 @@
dom;N
biały;ADJ

7
TaskC05/medium.out Normal file
View File

@ -0,0 +1,7 @@
arbuz;N
arbuza;N
arbuzowi;ADJ
arbuzowi;N
azylant;N
azylanci;N
azylantowie;OOV

2
TaskC05/multi.out Normal file
View File

@ -0,0 +1,2 @@
piła;N
piła;V

1
TaskC05/oov.out Normal file
View File

@ -0,0 +1 @@
budynek;OOV

View File

@ -1,66 +1,86 @@
import sys
sys.setrecursionlimit(5000)
def write_answer(answer):
with open(output_file, 'a') as file:
file.write(answer+'\n')
# def remove_first_symbol_from_the_list():
def write_answer(row, answer):
with open(output_file, 'a', encoding='utf-8') as file:
file.write(row.strip()+';'+answer+'\n')
def line_checking(row, position):
help_row = row
help_position = position
next_character_array =[]
bool_value = False
for element in row:
if element=='\n':
if position in what_is_the_ended_positions():
return True
else:
return False
next_character_array = find_next_position(help_position, element)
if next_character_array != []:
if len(next_character_array)>1:
for element_in_next_character_array in next_character_array:
if bool_value ==True:
break
else:
bool_value =line_checking(help_row[1:], element_in_next_character_array)
else:
# remove_first_symbol_from_the_list()
help_row = help_row[1:]
bool_value = line_checking(help_row, next_character_array[0])
else:
break
return bool_value
stack = [(row, position)]
help_row, help_position=row,position
while stack:
help_row, help_position = stack.pop()
if help_row == '':
help_row = '\n'
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:
stack.append((help_row[1:], element_in_next_position_array))
if help_row == '\n':
next_character_array = find_next_position(help_position, ';')
if next_character_array:
help_position_array, character_array = find_character_after_semicolon(next_character_array[0])
character_array = sorted(character_array)
help_position_array = sorted(help_position_array)
for i in range(len(character_array)):
ended_answer = find_position_to_print_the_answer(help_position_array[i], character_array[i])
write_answer(row,ended_answer)
else:
write_answer(row, 'OOV')
# return False
def find_position_to_print_the_answer(position, character):
with open(used_table, 'r', encoding='utf-8') as readed_used_table:
last_pos = position
word = character
# if last_pos == what_is_the_ended_positions():
# return word
for row_used_table in readed_used_table:
line = row_used_table.strip().split('\t')
if len(line) != 1:
if last_pos == line[0]:
word+=line[2]
last_pos = line[1]
return word
def find_character_after_semicolon(position):
with open(used_table, 'r', encoding='utf-8') as readed_used_table:
last_position = []
take_all_possible_characters = []
for row_used_table in readed_used_table:
line = row_used_table.strip().split('\t')
if len(line) != 1:
if position == line[0]:
take_all_possible_characters.append(line[2])
last_position.append((line[1]))
return last_position,take_all_possible_characters
def find_next_position(position, character):
with open(used_table, 'r') as readed_used_table:
with open(used_table, 'r', encoding='utf-8') as readed_used_table:
take_all_possible_positions = []
for row_used_table in readed_used_table:
line = row_used_table.strip().split(' ')
line = row_used_table.strip().split('\t')
if len(line) != 1:
if position == line[0] and character == line[2]:
take_all_possible_positions.append(line[1])
return take_all_possible_positions
def what_is_the_ended_positions():
array = []
with open(used_table, 'r') as file:
for row in file:
line = row.strip().split('\t')
if len(line)==1:
if len(line) == 1:
array += line
return array
used_table = 'test.arg'
input_file = 'test.in'
output_file = 'test.out'
# what_is_the_ended_positions()
with open(output_file, 'w') as readed_output_file:
with open(input_file, 'r') as readed_input_file:
used_table = 'multi.arg'
input_file = 'multi.in'
output_file = 'multi.out'
# used_table = sys.argv[1]
# input_file = sys.argv[2]
# output_file = sys.argv[3]
with open(output_file, 'w', encoding='utf-8') as readed_output_file:
with open(input_file, 'r', encoding='utf-8') as readed_input_file:
for row_input_file in readed_input_file:
if line_checking(row_input_file, '0')==True:
write_answer('YES')
else:
write_answer('NO')
line_checking(row_input_file, '0')