RosettaCodeData/Task/Balanced-brackets/Nim/balanced-brackets.nim

25 lines
447 B
Nim
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
from random import random, randomize, shuffle
from strutils import repeat
2016-12-05 23:44:36 +01:00
2017-09-23 10:01:46 +02:00
randomize()
2016-12-05 23:44:36 +01:00
proc gen(n: int): string =
2017-09-23 10:01:46 +02:00
result = "[]".repeat(n)
2016-12-05 23:44:36 +01:00
shuffle(result)
proc balanced(txt: string): bool =
var b = 0
for c in txt:
case c
of '[':
inc(b)
of ']':
dec(b)
if b < 0: return false
else: discard
b == 0
for n in 0..9:
let s = gen(n)
echo "'", s, "' is ", (if balanced(s): "balanced" else: "not balanced")