2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -91,4 +91,12 @@ Decoding the indices back to the original symbol order:
* Show the strings and their encoding here.
* Add a check to ensure that the decoded string is the same as the original.
The strings are: 'broood', 'bananaaa', and 'hiphophiphop'. (Note the spellings).
<br>
The strings are:
<big> broood </big>
<big> bananaaa </big>
<big> hiphophiphop </big>
(Note the spellings.)
<br><br>

View file

@ -1,46 +1,47 @@
package main
import (
"bytes"
"fmt"
"bytes"
"fmt"
)
type moveToFront string
type symbolTable string
func (symbols moveToFront) encode(s string) []int {
seq := make([]int, len(s))
pad := []byte(symbols)
c1 := []byte{0}
for i := 0; i < len(s); i++ {
c := s[i]
c1[0] = c
x := bytes.Index(pad, c1)
seq[i] = x
copy(pad[1:], pad[:x])
pad[0] = c
}
return seq
func (symbols symbolTable) encode(s string) []byte {
seq := make([]byte, len(s))
pad := []byte(symbols)
c1 := []byte{0}
for i := 0; i < len(s); i++ {
c := s[i]
c1[0] = c
x := byte(bytes.Index(pad, c1))
seq[i] = x
copy(pad[1:], pad[:x])
pad[0] = c
}
return seq
}
func (symbols moveToFront) decode(seq []int) string {
chars := make([]byte, len(seq))
pad := []byte(symbols)
for i, x := range seq {
c := pad[x]
chars[i] = c
copy(pad[1:], pad[:x])
pad[0] = c
}
return string(chars)
func (symbols symbolTable) decode(seq []byte) string {
chars := make([]byte, len(seq))
pad := []byte(symbols)
for i, x := range seq {
c := pad[x]
chars[i] = c
copy(pad[1:], pad[:x])
pad[0] = c
}
return string(chars)
}
func main() {
m := moveToFront("abcdefghijklmnopqrstuvwxyz")
for _, s := range []string{"broood", "bananaaa", "hiphophiphop"} {
enc := m.encode(s)
dec := m.decode(enc)
fmt.Println(s, enc, dec)
if dec != s {
panic("Whoops!")
}
}
m := symbolTable("abcdefghijklmnopqrstuvwxyz")
for _, s := range []string{"broood", "bananaaa", "hiphophiphop"} {
enc := m.encode(s)
dec := m.decode(enc)
fmt.Println(s, enc, dec)
if dec != s {
panic("Whoops!")
}
}
}

View file

@ -0,0 +1,46 @@
-- Return table of the alphabet in lower case
function getAlphabet ()
local letters = {}
for ascii = 97, 122 do table.insert(letters, string.char(ascii)) end
return letters
end
-- Move the table value at ind to the front of tab
function moveToFront (tab, ind)
local toMove = tab[ind]
for i = ind - 1, 1, -1 do tab[i + 1] = tab[i] end
tab[1] = toMove
end
-- Perform move-to-front encoding on input
function encode (input)
local symbolTable, output, index = getAlphabet(), {}
for pos = 1, #input do
for k, v in pairs(symbolTable) do
if v == input:sub(pos, pos) then index = k end
end
moveToFront(symbolTable, index)
table.insert(output, index - 1)
end
return table.concat(output, " ")
end
-- Perform move-to-front decoding on input
function decode (input)
local symbolTable, output = getAlphabet(), ""
for num in input:gmatch("%d+") do
output = output .. symbolTable[num + 1]
moveToFront(symbolTable, num + 1)
end
return output
end
-- Main procedure
local testCases, output = {"broood", "bananaaa", "hiphophiphop"}
for _, case in pairs(testCases) do
output = encode(case)
print("Original string: " .. case)
print("Encoded: " .. output)
print("Decoded: " .. decode(output))
print()
end

View file

@ -0,0 +1,19 @@
(de encode (Str)
(let Table (chop "abcdefghijklmnopqrstuvwxyz")
(mapcar
'((C)
(dec
(prog1
(index C Table)
(rot Table @) ) ) )
(chop Str) ) ) )
(de decode (Lst)
(let Table (chop "abcdefghijklmnopqrstuvwxyz")
(pack
(mapcar
'((N)
(prog1
(get Table (inc 'N))
(rot Table N) ) )
Lst ) ) ) )

View file

@ -0,0 +1,14 @@
(test (1 17 15 0 0 5)
(encode "broood") )
(test "broood"
(decode (1 17 15 0 0 5)) )
(test (1 1 13 1 1 1 0 0)
(encode "bananaaa") )
(test "bananaaa"
(decode (1 1 13 1 1 1 0 0)) )
(test (7 8 15 2 15 2 2 3 2 2 3 2)
(encode "hiphophiphop") )
(test "hiphophiphop"
(decode (7 8 15 2 15 2 2 3 2 2 3 2)) )

View file

@ -1,21 +1,18 @@
/*REXX program demonstrates move─to─front algorithm encode/decode symbol table*/
parse arg xxx; if xxx='' then xxx='broood bananaaa hiphophiphop' /*default*/
one=1 /*(offset) for task's requirement.*/
do j=1 for words(xxx); x=word(xxx,j) /*process one word at a time. */
@='abcdefghijklmnopqrstuvwxyz'; @@=@ /*symbol table: lowercase alphabet*/
$= /*set the decode string to a null.*/
do k=1 for length(x); z=substr(x,k,1) /*encrypt a symbol in the word. */
_=pos(z,@); if _==0 then iterate /*symbol position in symbol table.*/
$=$ _-one; @=z || delstr(@,_,1) /*adjust the symbol table string. */
end /*k*/ /* [↑] move─to─front encoding. */
/*REXX program demonstrates the move─to─front algorithm encode/decode symbol table. */
parse arg xxx; if xxx='' then xxx= 'broood bananaaa hiphophiphop' /*use the default?*/
one=1 /*(offset) for this task's requirement.*/
do j=1 for words(xxx); x=word(xxx, j) /*process a single word at a time. */
@= 'abcdefghijklmnopqrstuvwxyz'; @@=@ /*symbol table: the lowercase alphabet */
$= /*set the decode string to a null. */
do k=1 for length(x); z=substr(x, k, 1) /*encrypt a symbol in the word. */
_=pos(z, @); if _==0 then iterate /*the symbol position in symbol table. */
$=$ _ - one; @=z || delstr(@, _, 1) /*adjust the symbol table string. */
end /*k*/ /* [↑] the move─to─front encoding. */
!= /*set the encode string to a null. */
do m=1 for words($); n=word($, m) +one /*decode the sequence table string. */
y=substr(@@, n, 1); !=! || y /*the decode symbol for the word. */
@@=y || delstr(@@, n, 1) /*rebuild the symbol table string. */
end /*m*/ /* [↑] the move─to─front decoding. */
@=@@ /*symbol table: lowercase alphabet*/
!= /*set the encode string to a null.*/
do m=1 for words($); n=word($,m)+one /*decode the sequence table string*/
y=substr(@,n,1); !=! || y /*the decode symbol of the word. */
@=y || delstr(@,n,1) /*rebuild the symbol table string.*/
end /*m*/ /* [↑] move─to─front decoding. */
say 'word: ' left(x,20) "encoding:" left($,35) word('wrong OK',1+(!==x))
end /*j*/ /*all done encoding/decoding the words.*/
/*stick a fork in it, we're all done. */
say ' word: ' left(x, 20) "encoding:" left($, 35) word('wrong OK', 1+(!==x) )
end /*j*/ /*stick a fork in it, we're all done. */

View file

@ -0,0 +1,97 @@
package rosetta
import scala.annotation.tailrec
object MoveToFront {
/**
* Default radix
*/
private val R = 256
/**
* Default symbol table
*/
private def symbolTable = (0 until R).map(_.toChar).mkString
/**
* Apply move-to-front encoding using default symbol table.
*/
def encode(s: String): List[Int] = {
encode(s, symbolTable)
}
/**
* Apply move-to-front encoding using symbol table <tt>symTable</tt>.
*/
def encode(s: String, symTable: String): List[Int] = {
val table = symTable.toCharArray
@inline @tailrec def moveToFront(ch: Char, index: Int, tmpout: Char): Int = {
val tmpin = table(index)
table(index) = tmpout
if (ch != tmpin)
moveToFront(ch, index + 1, tmpin)
else {
table(0) = ch
index
}
}
@tailrec def encodeString(output: List[Int], s: List[Char]): List[Int] = s match {
case Nil => output
case x :: xs => {
encodeString(moveToFront(x, 0, table(0)) :: output, s.tail)
}
}
encodeString(Nil, s.toList).reverse
}
/**
* Apply move-to-front decoding using default symbol table.
*/
def decode(ints: List[Int]): String = {
decode(ints, symbolTable)
}
/**
* Apply move-to-front decoding using symbol table <tt>symTable</tt>.
*/
def decode(lst: List[Int], symTable: String): String = {
val table = symTable.toCharArray
@inline def moveToFront(c: Char, index: Int) {
for (i <- index-1 to 0 by -1)
table(i+1) = table(i)
table(0) = c
}
@tailrec def decodeList(output: List[Char], lst: List[Int]): List[Char] = lst match {
case Nil => output
case x :: xs => {
val c = table(x)
moveToFront(c, x)
decodeList(c :: output, xs)
}
}
decodeList(Nil, lst).reverse.mkString
}
def test(toEncode: String, symTable: String) {
val encoded = encode(toEncode, symTable)
println(toEncode + ": " + encoded)
val decoded = decode(encoded, symTable)
if (toEncode != decoded)
print("in")
println("correctly decoded to " + decoded)
}
}
/**
* Unit tests the <tt>MoveToFront</tt> data type.
*/
object RosettaCodeMTF extends App {
val symTable = "abcdefghijklmnopqrstuvwxyz"
MoveToFront.test("broood", symTable)
MoveToFront.test("bananaaa", symTable)
MoveToFront.test("hiphophiphop", symTable)
}