DrunkBear

S

Mar 20th, 2019
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. file = open('input.txt')
  2. stack = []
  3. s = ''
  4.  
  5. for line in file:
  6. line = line.strip()
  7.  
  8. if '+' in line:
  9. if line[1:] not in stack:
  10. stack.append(line[1:])
  11. else:
  12. s = 'ERROR'
  13. break
  14.  
  15. elif '#' in line:
  16. if line[1:] not in stack:
  17. stack.insert(0, line[1:])
  18. else:
  19. s = 'ERROR'
  20. break
  21.  
  22. elif line == '^':
  23. if len(stack) != 0:
  24. stack.pop(-1)
  25. else:
  26. s = 'ERROR'
  27. break
  28.  
  29. elif line == '/':
  30. if len(stack) != 0:
  31. stack.pop(0)
  32. else:
  33. s = 'ERROR'
  34. break
  35. #print(stack)
  36.  
  37. file.close()
  38.  
  39. if s != 'ERROR' and len(stack) == 0:
  40. s = 'EMPTY'
  41. if s != 'ERROR' and s != 'EMPTY':
  42. stack.reverse()
  43. s = ' '.join(stack)
  44.  
  45. file = open('output.txt', 'w')
  46. file.write(s)
  47. file.close()
Add Comment
Please, Sign In to add comment