A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
39
Task/Balanced-brackets/Fantom/balanced-brackets.fantom
Normal file
39
Task/Balanced-brackets/Fantom/balanced-brackets.fantom
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
class Main
|
||||
{
|
||||
static Bool matchingBrackets (Str[] brackets)
|
||||
{
|
||||
Int opened := 0
|
||||
Int i := 0
|
||||
while (i < brackets.size)
|
||||
{
|
||||
if (brackets[i] == "[")
|
||||
opened += 1
|
||||
else
|
||||
opened -= 1
|
||||
if (opened < 0) return false
|
||||
i += 1
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
public static Void main (Str[] args)
|
||||
{
|
||||
if (args.size == 1 && Int.fromStr(args[0], 10, false) != null)
|
||||
{
|
||||
n := Int.fromStr(args[0])
|
||||
Str[] brackets := [,]
|
||||
20.times
|
||||
{
|
||||
brackets = [,]
|
||||
// create a random set of brackets
|
||||
n.times { brackets.addAll (["[", "]"]) }
|
||||
n.times { brackets.swap(Int.random(0..<2*n), Int.random(0..<2*n)) }
|
||||
// report if matching or not
|
||||
if (matchingBrackets(brackets))
|
||||
echo (brackets.join(" ") + " Matching")
|
||||
else
|
||||
echo (brackets.join(" ") + " not matching")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue