Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
39
Task/Move-to-front-algorithm/Tcl/move-to-front-algorithm.tcl
Normal file
39
Task/Move-to-front-algorithm/Tcl/move-to-front-algorithm.tcl
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package require Tcl 8.6
|
||||
|
||||
oo::class create MoveToFront {
|
||||
variable symbolTable
|
||||
constructor {symbols} {
|
||||
set symbolTable [split $symbols ""]
|
||||
}
|
||||
|
||||
method MoveToFront {table index} {
|
||||
list [lindex $table $index] {*}[lreplace $table $index $index]
|
||||
}
|
||||
method encode {text} {
|
||||
set t $symbolTable
|
||||
set r {}
|
||||
foreach c [split $text ""] {
|
||||
set i [lsearch -exact $t $c]
|
||||
lappend r $i
|
||||
set t [my MoveToFront $t $i]
|
||||
}
|
||||
return $r
|
||||
}
|
||||
method decode {numbers} {
|
||||
set t $symbolTable
|
||||
set r ""
|
||||
foreach n $numbers {
|
||||
append r [lindex $t $n]
|
||||
set t [my MoveToFront $t $n]
|
||||
}
|
||||
return $r
|
||||
}
|
||||
}
|
||||
|
||||
MoveToFront create mtf "abcdefghijklmnopqrstuvwxyz"
|
||||
foreach tester {"broood" "bananaaa" "hiphophiphop"} {
|
||||
set enc [mtf encode $tester]
|
||||
set dec [mtf decode $enc]
|
||||
puts [format "'%s' encodes to %s. This decodes to '%s'. %s" \
|
||||
$tester $enc $dec [expr {$tester eq $dec ? "Correct!" : "WRONG!"}]]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue