Guest User

Untitled

a guest
Jul 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. pda_stack = []
  2.  
  3. def pda(input_string):
  4. # Convert string to list
  5. input_string = list(input_string)
  6. # Reverse to achieve a stack implementation
  7. input_string.reverse()
  8.  
  9. if not input_string[-1] == 'a':
  10. print("Rejected")
  11. return
  12.  
  13. while len(input_string) > 0 and input_string[-1] == 'a':
  14. pda_stack.append('a')
  15. input_string.pop()
  16.  
  17. last_part(input_string)
  18.  
  19. def last_part(input_string):
  20. if len(input_string) > 0 and input_string[-1] == 'b':
  21. input_string.pop()
  22. if len(input_string) > 0 and input_string[-1] == 'b':
  23. input_string.pop()
  24. if len(pda_stack) > 0 and pda_stack.pop() == 'a':
  25. if len(input_string) == 1 and input_string[-1] == 'b':
  26. input_string.pop()
  27. print("Accepted!")
  28. return
  29. elif len(input_string) > 0 and input_string[-1] == 'b':
  30. input_string.pop()
  31. last_part(input_string)
  32. print("Rejected")
  33.  
  34.  
  35. if __name__ == "__main__":
  36. input_string = raw_input("Enter input string: ")
  37.  
  38. pda(input_string)
Add Comment
Please, Sign In to add comment