Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -6,7 +6,7 @@ unbalanced(text s)
|
|||
b = 0;
|
||||
i = 0;
|
||||
while (i < length(s)) {
|
||||
if (character(s, i) == '[') {
|
||||
if (s[i] == '[') {
|
||||
b += 1;
|
||||
} else {
|
||||
b -= 1;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import std.stdio, std.random, std.range, std.algorithm;
|
||||
|
||||
bool isBalanced(in string s, in string pars="[]") pure nothrow {
|
||||
bool bal(in string t, in int nb = 0) pure nothrow {
|
||||
bool isBalanced(in string s, in char[2] pars="[]") pure nothrow @safe @nogc {
|
||||
bool bal(in string t, in int nb = 0) pure nothrow @safe @nogc {
|
||||
if (!nb && t.empty) return true;
|
||||
if (t.empty || nb < 0) return false;
|
||||
if (t[0] == pars[0]) return bal(t.dropOne, nb + 1);
|
||||
|
|
|
|||
|
|
@ -2,32 +2,40 @@
|
|||
#define system'routines.
|
||||
#define extensions.
|
||||
|
||||
#symbol randomBrackets = &&:aLength
|
||||
// --- RandomBrackets ---
|
||||
|
||||
#symbol randomBrackets = (:aLength)
|
||||
[
|
||||
#var aBrackets := Array new &type'length:aLength &function: &&:i[ CharValue new:91 ] + Array new &type'length:aLength &function: &&:i[ CharValue new:93 ].
|
||||
^ (0 == aLength)
|
||||
? [ emptyLiteralValue ]
|
||||
! [
|
||||
#var aBrackets := arrayControl new &length:(aLength int) &each: i[ CharValue new &short:91 ] + arrayControl new &length:(aLength int) &each: i[ CharValue new &short:93 ].
|
||||
|
||||
randomControl randomize:(aLength * 2) &array:aBrackets.
|
||||
randomControl randomize:(aLength * 2) &array:aBrackets.
|
||||
|
||||
^ Summing new:(String new) foreach:aBrackets Literal.
|
||||
^ Summing new:(String new) foreach:aBrackets literal.
|
||||
].
|
||||
].
|
||||
|
||||
#symbol isBalanced = &&:aLiteral
|
||||
#symbol isBalanced = (:aLiteral)
|
||||
[
|
||||
#var aCounter := Integer new:0.
|
||||
|
||||
control foreach:aLiteral &until: &&:aChar [ aCounter append:(aChar => "[" ? [ 1 ] "]" ? [ -1 ]) < 0 ].
|
||||
control foreach:aLiteral &until: aChar [ aCounter append:(aChar => "[" ? [ 1 ] "]" ? [ -1 ]) < 0 ].
|
||||
|
||||
^ (0 == aCounter).
|
||||
].
|
||||
|
||||
// --- Program ---
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
control from:0 &till:10 &do: &&:aLength
|
||||
control forrange &int:0 &int:9 &do: (&int:aLength)
|
||||
[
|
||||
#var anStr := randomBrackets:aLength.
|
||||
#var balanced := isBalanced:anStr.
|
||||
|
||||
console write:"""" write:anStr write:"""" writeLine:(balanced => true ? [ " is balanced" ] false ? [ " is not balanced" ]).
|
||||
consoleEx writeLine:"""":anStr:"""":(balanced => true ? [ " is balanced" ] false ? [ " is not balanced" ]).
|
||||
].
|
||||
|
||||
console readChar.
|
||||
|
|
|
|||
|
|
@ -21,17 +21,16 @@ func generate(n uint) string {
|
|||
}
|
||||
|
||||
func testBalanced(s string) {
|
||||
fmt.Printf("%s: ", s)
|
||||
var open int
|
||||
for i := 0; i < len(s); i++ {
|
||||
switch s[i] {
|
||||
fmt.Print(s + ": ")
|
||||
open := 0
|
||||
for _,c := range s {
|
||||
switch c {
|
||||
case '[':
|
||||
open++
|
||||
case ']':
|
||||
if open == 0 {
|
||||
fmt.Println("not ok")
|
||||
return
|
||||
|
||||
}
|
||||
open--
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -1,8 +1,5 @@
|
|||
use Regexp::Common 'balanced';
|
||||
my $re = qr/^$RE{balanced}{-parens=>'[]'}$/;
|
||||
sub balanced {
|
||||
my $depth = 0;
|
||||
for (split //, shift) {
|
||||
if ($_ eq '[') { ++$depth }
|
||||
elsif ($_ eq ']') { return if --$depth < 0 }
|
||||
}
|
||||
return !$depth
|
||||
return shift =~ $re;
|
||||
}
|
||||
|
|
|
|||
8
Task/Balanced-brackets/Perl/balanced-brackets-4.pl
Normal file
8
Task/Balanced-brackets/Perl/balanced-brackets-4.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
sub balanced {
|
||||
my $depth = 0;
|
||||
for (split //, shift) {
|
||||
if ($_ eq '[') { ++$depth }
|
||||
elsif ($_ eq ']') { return if --$depth < 0 }
|
||||
}
|
||||
return !$depth
|
||||
}
|
||||
|
|
@ -1,25 +1,21 @@
|
|||
import scala.util.Random
|
||||
import scala.util.Random.shuffle
|
||||
|
||||
object BalancedParensApp extends App {
|
||||
object BalancedBracketsApp extends App {
|
||||
|
||||
val triesCount = 10
|
||||
|
||||
(0 until triesCount).foreach { i =>
|
||||
val str = randomString(length = i)
|
||||
val msg = if (isBalanced(str)) "ok" else "NOT ok"
|
||||
println(s"$str - $msg")
|
||||
for (length <- 0 until 10) {
|
||||
val str = randomBrackets(length)
|
||||
if (is_balanced(str))
|
||||
println(s"$str - ok")
|
||||
else
|
||||
println(s"$str - NOT ok")
|
||||
}
|
||||
|
||||
def randomString(length: Int) = {
|
||||
val parensPairs = ("[]" * length).toSeq
|
||||
val parensOfGivenLength = parensPairs.take(length)
|
||||
val shuffledParens = Random.shuffle(parensOfGivenLength)
|
||||
shuffledParens.mkString
|
||||
}
|
||||
def randomBrackets(length: Int): String =
|
||||
shuffle(("[]" * length).toSeq).mkString
|
||||
|
||||
def isBalanced(parensString: String): Boolean = {
|
||||
def isBalanced(bracketString: String): Boolean = {
|
||||
var balance = 0
|
||||
parensString.foreach { char =>
|
||||
for (char <- bracketString) {
|
||||
char match {
|
||||
case '[' => balance += 1
|
||||
case ']' => balance -= 1
|
||||
|
|
|
|||
19
Task/Balanced-brackets/Scala/balanced-brackets-3.scala
Normal file
19
Task/Balanced-brackets/Scala/balanced-brackets-3.scala
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import scala.util.Random.shuffle
|
||||
import scala.annotation.tailrec
|
||||
|
||||
// ...
|
||||
|
||||
def isBalanced(str: String): Boolean = isBalanced(str.toList, balance = 0)
|
||||
|
||||
@tailrec
|
||||
def isBalanced(str: List[Char], balance: Int = 0): Boolean =
|
||||
str match {
|
||||
case _ if (balance < 0) => false
|
||||
case Nil => balance == 0
|
||||
case char :: rest =>
|
||||
val newBalance = char match {
|
||||
case '[' => balance + 1
|
||||
case ']' => balance -1
|
||||
}
|
||||
isBalanced(rest, newBalance)
|
||||
}
|
||||
|
|
@ -7,16 +7,16 @@
|
|||
((< i len) (list-vector vec))
|
||||
((inc i))
|
||||
(let ((j (random r len))
|
||||
(temp (vecref vec i)))
|
||||
(set (vecref vec i) (vecref vec j))
|
||||
(set (vecref vec j) temp))))
|
||||
(temp [vec i]))
|
||||
(set [vec i] [vec j])
|
||||
(set [vec j] temp))))
|
||||
|
||||
(defun generate-1 (count)
|
||||
(for ((i 0) chars) ((< i count) (cat-str (shuffle chars) "")) ((inc i))
|
||||
(push #\[ chars)
|
||||
(push #\] chars)))
|
||||
(let ((bkt (repeat "[]" count)))
|
||||
(cat-str (shuffle bkt))))
|
||||
|
||||
(defun generate-list (num count)
|
||||
(for ((i 0) list) ((< i num) list) ((inc i))
|
||||
(push (generate-1 count) list))))
|
||||
[[generate tf (op generate-1 count)] 0..num]))
|
||||
@(next :list @(generate-list 22 6))
|
||||
@(output)
|
||||
INPUT MATCHED REST
|
||||
|
|
|
|||
38
Task/Balanced-brackets/UNIX-Shell/balanced-brackets.sh
Normal file
38
Task/Balanced-brackets/UNIX-Shell/balanced-brackets.sh
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
generate() {
|
||||
local b=()
|
||||
local i j tmp
|
||||
for ((i=1; i<=$1; i++)); do
|
||||
b+=( '[' ']')
|
||||
done
|
||||
for ((i=${#b[@]}-1; i>0; i--)); do
|
||||
j=$(rand $i)
|
||||
tmp=${b[j]}
|
||||
b[j]=${b[i]}
|
||||
b[i]=$tmp
|
||||
done
|
||||
local IFS=
|
||||
echo "${b[*]}"
|
||||
}
|
||||
|
||||
# a random number in the range [0,n)
|
||||
rand() {
|
||||
echo $(( $RANDOM % $1 ))
|
||||
}
|
||||
|
||||
balanced() {
|
||||
local -i lvl=0
|
||||
local i
|
||||
for ((i=0; i<${#1}; i++)); do
|
||||
case ${1:i:1} in
|
||||
'[') ((lvl++));;
|
||||
']') (( --lvl < 0 )) && return 1;;
|
||||
esac
|
||||
done
|
||||
(( lvl == 0 )); return $?
|
||||
}
|
||||
|
||||
for ((i=0; i<=10; i++)); do
|
||||
test=$(generate $i)
|
||||
balanced "$test" && result=OK || result="NOT OK"
|
||||
printf "%s\t%s\n" "$test" "$result"
|
||||
done
|
||||
Loading…
Add table
Add a link
Reference in a new issue