Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pda_stack = []
- def pda(input_string):
- # Convert string to list
- input_string = list(input_string)
- # Reverse to achieve a stack implementation
- input_string.reverse()
- if not input_string[-1] == 'a':
- print("Rejected")
- return
- while len(input_string) > 0 and input_string[-1] == 'a':
- pda_stack.append('a')
- input_string.pop()
- last_part(input_string)
- def last_part(input_string):
- if len(input_string) > 0 and input_string[-1] == 'b':
- input_string.pop()
- if len(input_string) > 0 and input_string[-1] == 'b':
- input_string.pop()
- if len(pda_stack) > 0 and pda_stack.pop() == 'a':
- if len(input_string) == 1 and input_string[-1] == 'b':
- input_string.pop()
- print("Accepted!")
- return
- elif len(input_string) > 0 and input_string[-1] == 'b':
- input_string.pop()
- last_part(input_string)
- print("Rejected")
- if __name__ == "__main__":
- input_string = raw_input("Enter input string: ")
- pda(input_string)
Add Comment
Please, Sign In to add comment