Data update
This commit is contained in:
parent
ed705008a8
commit
0df55f9f24
2196 changed files with 32999 additions and 3075 deletions
|
|
@ -1,46 +1,50 @@
|
|||
// Generate a string with N opening brackets ("[") and N closing brackets ("]"), in some arbitrary order.
|
||||
// Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order),
|
||||
// none of which mis-nest.
|
||||
|
||||
import system'routines;
|
||||
import extensions;
|
||||
import extensions'text;
|
||||
|
||||
randomBrackets(len)
|
||||
{
|
||||
if (0 == len)
|
||||
{
|
||||
^emptyString
|
||||
}
|
||||
else
|
||||
{
|
||||
var brackets :=
|
||||
Array.allocate(len).populate:(i => $91)
|
||||
+
|
||||
Array.allocate(len).populate:(i => $93);
|
||||
if (0 == len)
|
||||
{
|
||||
^emptyString
|
||||
}
|
||||
else
|
||||
{
|
||||
var brackets :=
|
||||
Array.allocate(len).populate::(i => $91)
|
||||
+
|
||||
Array.allocate(len).populate::(i => $93);
|
||||
|
||||
brackets := brackets.randomize(len * 2);
|
||||
brackets := brackets.randomize(len * 2);
|
||||
|
||||
^ brackets.summarize(new StringWriter()).toString()
|
||||
}
|
||||
^ brackets.summarize(new StringWriter()).toString()
|
||||
}
|
||||
}
|
||||
|
||||
extension op
|
||||
{
|
||||
get isBalanced()
|
||||
{
|
||||
var counter := new Integer(0);
|
||||
get isBalanced()
|
||||
{
|
||||
var counter := new Integer(0);
|
||||
|
||||
self.seekEach:(ch => counter.append((ch==$91).iif(1,-1)) < 0);
|
||||
self.seekEach::(ch => counter.append((ch==$91).iif(1,-1)) < 0);
|
||||
|
||||
^ (0 == counter)
|
||||
}
|
||||
^ (0 == counter)
|
||||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
for(int len := 0, len < 9, len += 1)
|
||||
{
|
||||
var str := randomBrackets(len);
|
||||
for(int len := 0; len < 9; len += 1)
|
||||
{
|
||||
var str := randomBrackets(len);
|
||||
|
||||
console.printLine("""",str,"""",str.isBalanced ? " is balanced" : " is not balanced")
|
||||
};
|
||||
console.printLine("""",str,"""",str.isBalanced ? " is balanced" : " is not balanced")
|
||||
};
|
||||
|
||||
console.readChar()
|
||||
console.readChar()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue