import sys fsa_file = "./medium.in" # FSA FILE fsa = {} with open(fsa_file, 'r', encoding='utf-8') as f: for line in f: parts = line.strip().split('\t') if len(parts) == 3: state, next_state, symbol = parts state, next_state = int(state), int(next_state) if (state, '') not in fsa: fsa[(state, '')] = {} fsa[(state, '')][symbol] = next_state output_file_path = "./test.txt" # OUTPUT FILE def get_paths(current_state, current_path): if (current_state, '') not in fsa: return [current_path] paths = [] for symbol, next_state in fsa[(current_state, '')].items(): paths += get_paths(next_state, current_path + symbol) return paths with open(output_file_path, 'w', encoding='utf-8') as output_file: start_state = min(state for state, _ in fsa) paths = get_paths(start_state, '') for path in paths: output_file.write(path + '\n')