Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Balanced-brackets/Tcl/balanced-brackets-1.tcl
Normal file
28
Task/Balanced-brackets/Tcl/balanced-brackets-1.tcl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
proc generate {n} {
|
||||
if {!$n} return
|
||||
set l [lrepeat $n "\[" "\]"]
|
||||
set len [llength $l]
|
||||
while {$len} {
|
||||
set tmp [lindex $l [set i [expr {int($len * rand())}]]]
|
||||
lset l $i [lindex $l [incr len -1]]
|
||||
lset l $len $tmp
|
||||
}
|
||||
return [join $l ""]
|
||||
}
|
||||
|
||||
proc balanced s {
|
||||
set n 0
|
||||
foreach c [split $s ""] {
|
||||
# Everything unmatched is ignored, which is what we want
|
||||
switch -exact -- $c {
|
||||
"\[" {incr n}
|
||||
"\]" {if {[incr n -1] < 0} {return false}}
|
||||
}
|
||||
}
|
||||
expr {!$n}
|
||||
}
|
||||
|
||||
for {set i 0} {$i < 15} {incr i} {
|
||||
set s [generate $i]
|
||||
puts "\"$s\"\t-> [expr {[balanced $s] ? {OK} : {NOT OK}}]"
|
||||
}
|
||||
8
Task/Balanced-brackets/Tcl/balanced-brackets-2.tcl
Normal file
8
Task/Balanced-brackets/Tcl/balanced-brackets-2.tcl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
proc constructBalancedString {n} {
|
||||
set s ""
|
||||
for {set i 0} {$i < $n} {incr i} {
|
||||
set x [expr {int(rand() * ([string length $s] + 1))}]
|
||||
set s "[string range $s 0 [expr {$x-1}]]\[\][string range $s $x end]"
|
||||
}
|
||||
return $s
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue