Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Balanced-brackets/Ceylon/balanced-brackets.ceylon
Normal file
27
Task/Balanced-brackets/Ceylon/balanced-brackets.ceylon
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import com.vasileff.ceylon.random.api {
|
||||
platformRandom,
|
||||
Random
|
||||
}
|
||||
"""Run the example code for Rosetta Code ["Balanced brackets" task] (http://rosettacode.org/wiki/Balanced_brackets)."""
|
||||
shared void run() {
|
||||
value rnd = platformRandom();
|
||||
for (len in (0..10)) {
|
||||
value c = generate(rnd, len);
|
||||
print("``c.padTrailing(20)`` - ``if (balanced(c)) then "OK" else "NOT OK" ``");
|
||||
}
|
||||
}
|
||||
|
||||
String generate(Random rnd, Integer count)
|
||||
=> if (count == 0) then ""
|
||||
else let(length = 2*count,
|
||||
brackets = zipEntries(rnd.integers(length).take(length),
|
||||
"[]".repeat(count))
|
||||
.sort((a,b) => a.key<=>b.key)
|
||||
.map(Entry.item))
|
||||
String(brackets);
|
||||
|
||||
Boolean balanced(String input)
|
||||
=> let (value ints = { for (c in input) if (c == '[') then 1 else -1 })
|
||||
ints.filter((i) => i != 0)
|
||||
.scan(0)(plus<Integer>)
|
||||
.every((i) => i >= 0);
|
||||
Loading…
Add table
Add a link
Reference in a new issue