Untitled
unknown
python
4 years ago
4.6 kB
5
Indexable
def readFile(fileName): start = -1 finish = [] graph = [] file = open(fileName, "r") nrLine = 0 for line in file: nrLine += 1 a = [] for word in line.split(): try: a.append(word) except ValueError: pass if nrLine == 1: start = a[0] elif nrLine == 2: finish = a else: graph.append(a) return start, finish, graph def keyboardInput(): print("1. Enter initial state") start = input() print("2. Enter final state") finish = input() print("3. Enter values until -1") graph = [] line = "" while 1: line = input() a = [] if line == "-1": break for word in line.split(): try: a.append(word) except ValueError: pass graph.append(a) return start, finish, graph def showRunMenu(): print("1. Multimea starilor") print("2. Alfabetul") print("3. Tranzitiile") print("4. Multimea starilor finale") print("5. Sequence check") print("6. Longest prefix") print("7. EXIT") return input() def showMenu(): print("MENU") print("1.Keyboard input") print("2.Read file") return input() def multimeaStarilor(graph): state = [] for pair in graph: if pair[0] not in state: state.append(pair[0]) if pair[1] not in state: state.append(pair[1]) return state def alfabetul(graph): alfabet = [] for pair in graph: if pair[2] not in alfabet: alfabet.append(pair[2]) return alfabet def tranzitii(graph): dictionary = {} for pair in graph: try: dictionary[pair[0]].append([pair[1], pair[2]]) except: dictionary[pair[0]] = [[pair[1], pair[2]]] return dictionary def sequenceCheck(start, finish, dictionary): print("1. Insert sequence") sequence = input() node = start ok = True while ok: if node not in dictionary: ok = False break near = dictionary[node] char = sequence[0] found = False for n in near: if n[1] == char: found = True next = n if found: sequence = sequence[1:] node = next[0] if len(sequence) == 0 and node not in finish: ok = False else: ok = False if len(sequence) == 0: break print(ok) def prefixCheck(start, finish, dictionary): print("1. Insert sequence for prefix") sequence = input() node = start current = "" if start in finish: prefix = "eps" else: prefix = "Invalid" ok = True while ok: if node not in dictionary: ok = False break near = dictionary[node] char = sequence[0] current += char found = False for n in near: if n[1] == char: found = True next = n if found: sequence = sequence[1:] node = next[0] if node in finish and sequence != "": prefix = current else: ok = False if len(sequence) == 0: break print(prefix) def run(start, finish, graph): print(start, finish, graph) while 1: runOption = showRunMenu() if runOption == "7": break elif runOption == "1": print(multimeaStarilor(graph)) elif runOption == "2": print(alfabetul(graph)) elif runOption == '3': dictionary = tranzitii(graph) for stare in dictionary: print(stare, dictionary[stare]) elif runOption == '4': print(finish) elif runOption == '5': dictionary = tranzitii(graph) sequenceCheck(start, finish, dictionary) elif runOption == '6': dictionary = tranzitii(graph) print(dictionary) prefixCheck(start, finish, dictionary) def main(): while 1: option = showMenu() if option == '1': start, finish, graph = keyboardInput() run(start, finish, graph) break elif option == '2': start, finish, graph = readFile("data.in") run(start, finish, graph) break else: print("Invalid input") main()
Editor is loading...