Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
28
Task/Move-to-front-algorithm/R/move-to-front-algorithm.r
Normal file
28
Task/Move-to-front-algorithm/R/move-to-front-algorithm.r
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
encodeMTF <- function(str, symtable = letters) {
|
||||
encode <- function(ch) {
|
||||
r <- which(symtable == ch)
|
||||
symtable <<- c(ch, symtable[-r])
|
||||
return(r)
|
||||
}
|
||||
sapply(strsplit(str, "")[[1]], encode)
|
||||
}
|
||||
|
||||
decodeMTF <- function(arr, symtable = letters) {
|
||||
decode <- function(i) {
|
||||
r <- symtable[i]
|
||||
symtable <<- c(r, symtable[-i])
|
||||
return(r)
|
||||
}
|
||||
paste(sapply(arr, decode), collapse = "")
|
||||
}
|
||||
|
||||
testset <- c("broood", "bananaaa", "hiphophiphop")
|
||||
encoded <- lapply(testset, encodeMTF)
|
||||
decoded <- mapply(decodeMTF, encoded)
|
||||
|
||||
for (i in seq_along(testset)) {
|
||||
cat("Test string:", testset[i], "\n -> Encoded:", paste(encoded[[i]], collapse = ", "), "\n -> Decoded:", decoded[i], "\n\n")
|
||||
}
|
||||
|
||||
# Test if decoded strings equal original
|
||||
all(sapply(seq_along(testset), function(i) identical(testset[i], decoded[i])))
|
||||
Loading…
Add table
Add a link
Reference in a new issue