fork download
  1. def pda_accept(string):
  2. stack = []
  3. for char in string:
  4. if char == 'a':
  5. stack.append('a')
  6. elif char == 'b':
  7. if len(stack) == 0 or stack[-1] != 'a':
  8. return False
  9. stack.pop()
  10. return len(stack) == 0
  11.  
  12. print(pda_accept("ab")) # True
  13. print(pda_accept("aabb")) # True
  14. print(pda_accept("aab")) # False
Success #stdin #stdout #stderr 0.02s 6904KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/RAK7XU/prog:14:31: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit