update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
66
Task/Balanced-brackets/Aime/balanced-brackets.aime
Normal file
66
Task/Balanced-brackets/Aime/balanced-brackets.aime
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
integer
|
||||
unbalanced(text s)
|
||||
{
|
||||
integer b, i;
|
||||
|
||||
b = 0;
|
||||
i = 0;
|
||||
while (i < length(s)) {
|
||||
if (character(s, i) == '[') {
|
||||
b += 1;
|
||||
} else {
|
||||
b -= 1;
|
||||
if (b < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
text
|
||||
generate(integer d)
|
||||
{
|
||||
integer i;
|
||||
text s;
|
||||
|
||||
i = d;
|
||||
while (i) {
|
||||
s = insert(s, 0, '[');
|
||||
i -= 1;
|
||||
}
|
||||
|
||||
i = d;
|
||||
while (i) {
|
||||
s = insert(s, drand(length(s)), ']');
|
||||
i -= 1;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
integer i;
|
||||
|
||||
i = 0;
|
||||
while (i < 10) {
|
||||
text s;
|
||||
|
||||
s = generate(i);
|
||||
o_text(s);
|
||||
o_text(" is ");
|
||||
if (unbalanced(s)) {
|
||||
o_text("un");
|
||||
}
|
||||
o_text("balanced\n");
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -12,17 +12,3 @@
|
|||
(when (= (peek stack) \[)
|
||||
(recur coll (pop stack))))
|
||||
(zero? (count stack)))))
|
||||
|
||||
user> (->> (range 10)
|
||||
(map gen-brackets ,)
|
||||
(map (juxt identity balanced?) ,) vec)
|
||||
[["" true]
|
||||
["[]" true]
|
||||
["[[]]" true]
|
||||
["[][[]]" true]
|
||||
["[]][][][" nil]
|
||||
["[[[[[]]]]]" true]
|
||||
["]][[][][[[]]" nil]
|
||||
["[]]]][[[[]][][" nil]
|
||||
["][][[]]][[][][][" nil]
|
||||
["][][]]][]][[[][[[]" nil]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
import std.stdio, std.algorithm, std.random;
|
||||
import std.stdio, std.algorithm, std.random, std.range;
|
||||
|
||||
void main() {
|
||||
foreach (i; 1 .. 9) {
|
||||
string s;
|
||||
foreach (_; 0 .. i * 2)
|
||||
s ~= "[]"[uniform(0, 2)];
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, 2)]).array;
|
||||
writeln(s.balancedParens('[', ']') ? " OK: " : "bad: ", s);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,23 @@
|
|||
import std.stdio, std.random;
|
||||
import std.stdio, std.random, std.range, std.algorithm;
|
||||
|
||||
bool isBalanced(in string txt) pure nothrow {
|
||||
auto count = 0;
|
||||
|
||||
foreach (immutable c; txt) {
|
||||
if (c == ']') {
|
||||
count--;
|
||||
if (count < 0)
|
||||
return false;
|
||||
} else if (c == '[')
|
||||
count++;
|
||||
}
|
||||
|
||||
return count == 0;
|
||||
}
|
||||
|
||||
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);
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, 2)]).array;
|
||||
writeln(s.isBalanced ? " OK " : "Bad ", s);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,19 @@
|
|||
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 {
|
||||
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
|
||||
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);
|
||||
foreach (immutable i; 1 .. 9) {
|
||||
immutable s = iota(i * 2).map!(_ => "[]"[uniform(0, 2)]).array;
|
||||
writeln(s.isBalanced ? " OK " : "Bad ", s);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
29
Task/Balanced-brackets/Erlang/balanced-brackets.erl
Normal file
29
Task/Balanced-brackets/Erlang/balanced-brackets.erl
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
-module( balanced_brackets ).
|
||||
-export( [generate/1, is_balanced/1, task/0] ).
|
||||
|
||||
generate( N ) ->
|
||||
[generate_bracket(random:uniform()) || _X <- lists:seq(1, 2*N)].
|
||||
|
||||
is_balanced( String ) -> is_balanced_loop( String, 0 ).
|
||||
|
||||
task() ->
|
||||
lists:foreach( fun (N) ->
|
||||
String = generate( N ),
|
||||
Result = is_balanced( String ),
|
||||
io:fwrite( "~s is ~s~n", [String, task_balanced(Result)] )
|
||||
end,
|
||||
lists:seq(0, 5) ).
|
||||
|
||||
|
||||
|
||||
is_balanced_loop( _String, N ) when N < 0 -> false;
|
||||
is_balanced_loop( [], 0 ) -> true;
|
||||
is_balanced_loop( [], _N ) -> false;
|
||||
is_balanced_loop( [$[ | T], N ) -> is_balanced_loop( T, N + 1 );
|
||||
is_balanced_loop( [$] | T], N ) -> is_balanced_loop( T, N - 1 ).
|
||||
|
||||
generate_bracket( N ) when N =< 0.5 -> $[;
|
||||
generate_bracket( N ) when N > 0.5 -> $].
|
||||
|
||||
task_balanced( true ) -> "OK";
|
||||
task_balanced( false ) -> "NOT OK".
|
||||
|
|
@ -10,7 +10,7 @@ brk$(8) = "[]][][][[[]]"
|
|||
brk$(9) = "][]][["
|
||||
brk$(10) = "[]][][][[]"
|
||||
|
||||
for i = 1 to 10
|
||||
for i = 0 to 10
|
||||
b$ = brk$(i)
|
||||
while instr(b$,"[]") <> 0
|
||||
x = instr(b$,"[]")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue