t7936.nim 563 B

12345678910111213141516171819202122232425262728
  1. discard """
  2. action: "run"
  3. """
  4. import
  5. tables, deques, sequtils
  6. const
  7. lookupTable = {'(': ')', '{': '}', '[': ']'}.toTable
  8. proc isPaired*(value: string): bool =
  9. var stack = initDeque[char]()
  10. for item in value:
  11. # echo "Looking at " & item
  12. if item in lookupTable:
  13. stack.addLast(item)
  14. if item in toSeq(lookupTable.values):
  15. if stack.len == 0:
  16. return false
  17. if lookupTable[stack.popLast()] != item:
  18. return false
  19. return stack.len == 0
  20. doAssert isPaired("{[()]}") == true
  21. doAssert isPaired("a)b(c") == false