This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,33 @@
#include <algorithm>
#include <iostream>
#include <string>
std::string generate(int n, char left = '[', char right = ']')
{
std::string str(std::string(n, left) + std::string(n, right));
std::random_shuffle(str.begin(), str.end());
return str;
}
bool balanced(const std::string &str, char left = '[', char right = ']')
{
int count = 0;
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
{
if (*it == left)
count++;
else if (*it == right)
if (--count < 0) return false;
}
return count == 0;
}
int main()
{
srand(time(NULL)); // seed rng
for (int i = 0; i < 9; ++i)
{
std::string s(generate(i));
std::cout << (balanced(s) ? " ok: " : "bad: ") << s << "\n";
}
}

View file

@ -0,0 +1,27 @@
(defun string-of-brackets (n)
(let ((result (make-string (* 2 n)))
(opening n)
(closing n))
(dotimes (i (* 2 n) result)
(setf (aref result i)
(cond
((zerop opening) #\])
((zerop closing) #\[)
(t (if (= (random 2) 0)
(progn (decf opening) #\[)
(progn (decf closing) #\]))))))))
(defun balancedp (string)
(zerop (reduce (lambda (nesting bracket)
(ecase bracket
(#\] (if (= nesting 0)
(return-from balancedp nil)
(1- nesting)))
(#\[ (1+ nesting))))
string
:initial-value 0)))
(defun show-balanced-brackets ()
(dotimes (i 10)
(let ((s (string-of-brackets i)))
(format t "~3A: ~A~%" (balancedp s) s))))

View file

@ -0,0 +1,10 @@
import std.stdio, std.algorithm, std.random;
void main() {
foreach (i; 1 .. 9) {
string s;
foreach (_; 0 .. i * 2)
s ~= "[]"[uniform(0, 2)];
writeln(s.balancedParens('[', ']') ? " OK: " : "bad: ", s);
}
}

View file

@ -0,0 +1,20 @@
import std.stdio, std.random;
void main() {
NEXT_STR:
foreach (j; 1 .. 9) {
string s;
foreach (_; 0 .. j * 2)
s ~= "[]"[uniform(0, 2)];
int balance;
foreach (i, c; s) {
balance += (c == '[') ? 1 : ((c == ']') ? -1 : 0);
if (balance < 0 || balance >= s.length - i) {
writefln("BAD: %s (%s)", s, balance < 0 ? "-" : "+");
continue NEXT_STR;
}
}
writefln(" OK: %s (=)", s);
}
}

View file

@ -0,0 +1,20 @@
import std.stdio, std.random, std.conv, std.range, std.algorithm;
bool isBalanced(in char[] s) pure nothrow {
static bool bal(in char[] 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
}
return bal(s);
}
void main() {
foreach (i; 1 .. 9) {
auto sr = iota(i * 2).map!(_ => ['[',']'][uniform(0, 2)])();
auto s = sr.array();
writefln("%5s: %s", s.isBalanced().text(), s);
}
}

View file

@ -0,0 +1,31 @@
procedure Balanced_Brackets;
var BracketsStr : string;
TmpStr : string;
I,J : integer;
begin
Randomize;
for I := 1 to 9 do
begin
{ Create a random string of 2*N chars with N*"[" and N*"]" }
TmpStr := '';
for J := 1 to I do
TmpStr := '['+TmpStr+']';
BracketsStr := '';
while TmpStr > '' do
begin
J := Random(Length(TmpStr))+1;
BracketsStr := BracketsStr+TmpStr[J];
Delete(TmpStr,J,1);
end;
TmpStr := BracketsStr;
{ Test for balanced brackets }
while Pos('[]',TmpStr) > 0 do
Delete(TmpStr,Pos('[]',TmpStr),2);
if TmpStr = '' then
writeln(BracketsStr+': OK')
else
writeln(BracketsStr+': not OK');
end;
end;

View file

@ -0,0 +1,32 @@
#define std'dictionary'*.
#define std'basic'*.
#define std'patterns'*.
#define ext'utils'*.
#define ext'routines'*.
#symbol RandomBrackets : aLength =
Randomized
&&count:(aLength * 2)
&base:(FilledWideLiteral &&pattern:"[" &count:aLength + (FilledWideLiteral &&pattern:"]" &count:aLength)).
#symbol IsBalanced : aLiteral =
[
#var aCounter := Integer::0.
#if Scan::aLiteral seek: aChar = (aCounter append:(aChar ifequal:"[" ^^1 | ^^-1) < 0).
^ (0 == aCounter).
].
#symbol Program =
[
loop &&from:0 &till:10 run: aLength =
[
#var anStr := RandomBrackets::aLength.
#var balanced := IsBalanced::anStr.
'program'output << """" << anStr << (balanced is ^^""" is balanced%n" | ^^""" is not balanced%n").
].
'program'input get.
].

View file

@ -0,0 +1,45 @@
function check_brackets(sequence s)
integer level
level = 0
for i = 1 to length(s) do
if s[i] = '[' then
level += 1
elsif s[i] = ']' then
level -= 1
if level < 0 then
return 0
end if
end if
end for
return level = 0
end function
function generate_brackets(integer n)
integer opened,closed,r
sequence s
opened = n
closed = n
s = ""
for i = 1 to n*2 do
r = rand(opened+closed)
if r<=opened then
s &= '['
opened -= 1
else
s &= ']'
closed -= 1
end if
end for
return s
end function
sequence s
for i = 1 to 10 do
s = generate_brackets(3)
puts(1,s)
if check_brackets(s) then
puts(1," OK\n")
else
puts(1," NOT OK\n")
end if
end for