Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import sys
  3. import re
  4. transitions = {}
  5. accepting = []
  6. for line in sys.stdin:
  7. if re.search("^\d+\s\d+\s[a-z]$", line):
  8. accept = re.split("\s", line)
  9. if not transitions.get(str(accept[2])):
  10. transitions[str(accept[2])] = [str(accept[0]), str(accept[1])]
  11. else :
  12. transitions[str(accept[2])].append(str(accept[0]))
  13. transitions[str(accept[2])].append(str(accept[1]))
  14. elif re.search("^\d+$", line):
  15. accepting.append(int(line.replace('\n','')))
  16.  
  17. def gotostate(currentState, char):
  18. trans = transitions.get(char)
  19. if trans:
  20. for i in range(0, len(trans), 2):
  21. if int(trans[i]) == currentState:
  22. return int(trans[i+1])
  23. return None
  24.  
  25. def iscorrect(word):
  26. state = 0
  27. for char in word:
  28. state = gotostate(state, char)
  29. if state == None:
  30. return False
  31. for i in accepting:
  32. if i == state:
  33. return True
  34. return False
  35.  
  36. file = open(sys.argv[1])
  37. for line in file:
  38. word = line.replace('\n','')
  39. if iscorrect(word):
  40. print("TRUE", word)
  41. else:
  42. print("FALSE", word)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement