Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Run-length-encoding/Tcl/run-length-encoding-1.tcl
Normal file
15
Task/Run-length-encoding/Tcl/run-length-encoding-1.tcl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
proc encode {string} {
|
||||
set encoding {}
|
||||
# use a regular expression to match runs of one character
|
||||
foreach {run -} [regexp -all -inline {(.)\1+|.} $string] {
|
||||
lappend encoding [string length $run] [string index $run 0]
|
||||
}
|
||||
return $encoding
|
||||
}
|
||||
|
||||
proc decode {encoding} {
|
||||
foreach {count char} $encoding {
|
||||
append decoded [string repeat $char $count]
|
||||
}
|
||||
return $decoded
|
||||
}
|
||||
6
Task/Run-length-encoding/Tcl/run-length-encoding-2.tcl
Normal file
6
Task/Run-length-encoding/Tcl/run-length-encoding-2.tcl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
set str "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
|
||||
set enc [encode $str] ;# ==> {12 W 1 B 12 W 3 B 24 W 1 B 14 W}
|
||||
set dec [decode $enc]
|
||||
if {$str eq $dec} {
|
||||
puts "success"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue