Skip to content

rustomax/nim-stacks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nim-stacks

Pure Nim stack implementation based on sequences

Build Status

Version 0.4.2

Installation

nimble install stacks

Example

import stacks

proc isPaired*(s: string): bool =
    ## Algorithm to detect unbalanced brackets using a stack
    var stack = Stack[char]()
    
    for c in s:
        case c:
            of '{': stack.push('}')
            of '[': stack.push(']')
            of '(': stack.push(')')
            of '}', ']', ')':
                if stack.isEmpty or stack.pop() != c: return false
            else: discard

    stack.isEmpty()

when isMainModule:
    assert isPaired("(((185 + 223.85) * 15) - 543)/2") == true
    assert isPaired("for (i = 1; i < 11; ++i)\n{printf(\"i\");}\nreturn 0;}\n}") == false

About

Pure Nim stack implementation based on sequences

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages