summary refs log tree commit diff stats
path: root/tests/let/t7936.nim
blob: 3819dfc023a1b528e5f3ac885f4c71f4521954a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
discard """
  action: "run"
"""

import
  tables, deques, sequtils

const
  lookupTable = {'(': ')', '{': '}', '[': ']'}.toTable

proc isPaired*(value: string): bool =
  var stack = initDeque[char]() 

  for item in value:
    # echo "Looking at " & item
    if item in lookupTable:
      stack.addLast(item)
    if item in toSeq(lookupTable.values):
      if stack.len == 0:
        return false
      if lookupTable[stack.popLast()] != item:
        return false

  return stack.len == 0

doAssert isPaired("{[()]}") == true
doAssert isPaired("a)b(c") == false