Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -9,13 +9,13 @@ BEGIN {
|
|||
}
|
||||
|
||||
function isbb(x) {
|
||||
s = 0;
|
||||
s = 0
|
||||
for (k=1; k<=length(x); k++) {
|
||||
c = substr(x,k,1);
|
||||
if (c=="[") {s++;}
|
||||
else { if (c=="]") s--; }
|
||||
c = substr(x,k,1)
|
||||
if (c=="[") {s++}
|
||||
else { if (c=="]") s-- }
|
||||
|
||||
if (s<0) {return 0};
|
||||
if (s<0) {return 0}
|
||||
}
|
||||
return (s==0);
|
||||
return (s==0)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
import std.stdio, std.random, std.conv, std.range, std.algorithm;
|
||||
import std.stdio, std.random, std.range, std.algorithm;
|
||||
|
||||
bool isBalanced(in string s) pure nothrow {
|
||||
static bool bal(in string s, in int nb = 0) pure nothrow {
|
||||
if (nb == 0 && s.empty) return true;
|
||||
if (s.empty || nb < 0) return false;
|
||||
if (s[0] == '[') return bal(s[1 .. $], nb + 1);
|
||||
if (s[0] == ']') return bal(s[1 .. $], nb - 1);
|
||||
return bal(s[1 .. $], nb); // Ignore char.
|
||||
bool isBalanced(in string s, in string pars="[]") pure nothrow {
|
||||
bool bal(in string t, in int nb = 0) pure nothrow {
|
||||
if (!nb && t.empty) return true;
|
||||
if (t.empty || nb < 0) return false;
|
||||
if (t[0] == pars[0]) return bal(t.dropOne, nb + 1);
|
||||
if (t[0] == pars[1]) return bal(t.dropOne, nb - 1);
|
||||
return bal(t.dropOne, nb); // Ignore char.
|
||||
}
|
||||
return bal(s);
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, 2)]).array;
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, $)]).array;
|
||||
writeln(s.isBalanced ? " OK " : "Bad ", s);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
19
Task/Balanced-brackets/Deja-Vu/balanced-brackets.djv
Normal file
19
Task/Balanced-brackets/Deja-Vu/balanced-brackets.djv
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
matching?:
|
||||
swap 0
|
||||
for c in chars:
|
||||
if = c "]":
|
||||
++
|
||||
elseif = c "[":
|
||||
if not dup:
|
||||
drop
|
||||
return false
|
||||
--
|
||||
not
|
||||
|
||||
!. matching? ""
|
||||
!. matching? "[]"
|
||||
!. matching? "[][]"
|
||||
!. matching? "[[][]]"
|
||||
!. matching? "]["
|
||||
!. matching? "][]["
|
||||
!. matching? "[]][[]"
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
#define system.
|
||||
#define system'routines.
|
||||
#define extensions.
|
||||
|
||||
#symbol randomBrackets = &&:aLength
|
||||
[
|
||||
#var aBrackets := Array new &type'length:aLength &action: &&:i[ CharValue new:"[" ] + Array new &type'length:aLength &action: &&:i[ CharValue new:"]" ].
|
||||
#var aBrackets := Array new &type'length:aLength &function: &&:i[ CharValue new:91 ] + Array new &type'length:aLength &function: &&:i[ CharValue new:93 ].
|
||||
|
||||
randomControl randomize:(aLength * 2) &array:aBrackets.
|
||||
|
||||
|
|
|
|||
62
Task/Balanced-brackets/PL-I/balanced-brackets.pli
Normal file
62
Task/Balanced-brackets/PL-I/balanced-brackets.pli
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
*process m or(!) s attributes source;
|
||||
cb: Proc Options(main);
|
||||
/* PL/I program to check for balanced brackets [] ********************
|
||||
* 07.12.2013 Walter Pachl translated from REXX Version 2
|
||||
*********************************************************************/
|
||||
Dcl v Char(20) Var;
|
||||
Dcl (i,j) Bin Fixed(31);
|
||||
Dcl r Bin Float(53);
|
||||
|
||||
Call testbal(''); /* first some user written tests */
|
||||
Call testbal('[][][][[]]');
|
||||
Call testbal('[][][][[]]][');
|
||||
Call testbal('[');
|
||||
Call testbal(']');
|
||||
Call testbal('[]');
|
||||
Call testbal('][');
|
||||
Call testbal('][][');
|
||||
Call testbal('[[]]');
|
||||
Call testbal('[[[[[[[]]]]]]]');
|
||||
Call testbal('[[[[[]]]][]');
|
||||
Call testbal('[][]');
|
||||
Call testbal('[]][[]');
|
||||
Call testbal(']]][[[[]');
|
||||
Call testbal('[[a]][b]');
|
||||
Put Edit(' ')(Skip,a);
|
||||
r=random(12345); /* then some generated ones */
|
||||
Do i=1 To 10;
|
||||
v='';
|
||||
Do j=1 To 10;
|
||||
r=random();
|
||||
If r>0.5 Then v=v!!']';
|
||||
Else v=v!!'[';
|
||||
End;
|
||||
Call testbal(v);
|
||||
End;
|
||||
Return;
|
||||
|
||||
testbal: Proc(s); /* test the given string and show result */
|
||||
Dcl s Char(*);
|
||||
Dcl yesno(0:1) Char(20) Var Init('unbalanced',' balanced');
|
||||
Put Edit(yesno(checkbal(s)),''''!!s!!'''')(Skip,a,x(1),a);
|
||||
End;
|
||||
|
||||
checkBal: proc(s) Returns(Bin Fixed(31));
|
||||
/*check for balanced brackets [] */
|
||||
Dcl s Char(*);
|
||||
Dcl nest Bin Fixed(31) Init(0);
|
||||
Dcl i Bin Fixed(31);
|
||||
Do i=1 To length(s);
|
||||
Select(substr(s,i,1));
|
||||
When('[') nest+=1;
|
||||
When(']') Do;
|
||||
If nest=0 Then return(0);
|
||||
nest-=1;
|
||||
End;
|
||||
Otherwise;
|
||||
End;
|
||||
End;
|
||||
Return(nest=0);
|
||||
End;
|
||||
|
||||
End;
|
||||
|
|
@ -37,8 +37,8 @@ For i=1 To 5
|
|||
TestString$ = Generate(i)
|
||||
Print(TestString$)
|
||||
If Balanced(TestString$)
|
||||
PrintN(" is ballanced.")
|
||||
PrintN(" is balanced.")
|
||||
Else
|
||||
PrintN(" is not ballanced")
|
||||
PrintN(" is not balanced")
|
||||
EndIf
|
||||
Next
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue