Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,13 @@
MTF_Encode(string){
str := "abcdefghijklmnopqrstuvwxyz"
loop, parse, string
code .= (A_Index>1 ? ",":"") . InStr(str, A_LoopField) - 1 , str := A_LoopField . StrReplace(str, A_LoopField)
return code
}
MTF_Decode(code){
str := "abcdefghijklmnopqrstuvwxyz"
loop, parse, code, `,
string .= (letter := SubStr(str, A_LoopField+1, 1)) , str := letter . StrReplace(str, letter)
return string
}

View file

@ -0,0 +1,5 @@
testStrings = broood,bananaaa,hiphophiphop
loop, parse, testStrings, `,
Output .= A_LoopField "`t" MTF_Encode(A_LoopField) "`t" MTF_Decode(MTF_Encode(A_LoopField)) "`n"
MsgBox % Output
return

View file

@ -0,0 +1,26 @@
defmodule MoveToFront do
@table Enum.to_list(?a..?z)
def encode(text), do: encode(to_char_list(text), @table, [])
defp encode([], _, output), do: Enum.reverse(output)
defp encode([h|t], table, output) do
i = Enum.find_index(table, &(&1 == h))
encode(t, move2front(table, i), [i | output])
end
def decode(indices), do: decode(indices, @table, [])
defp decode([], _, output), do: Enum.reverse(output) |> to_string
defp decode([h|t], table, output) do
decode(t, move2front(table, h), [Enum.at(table, h) | output])
end
def move2front(table, i), do: [Enum.at(table,i) | List.delete_at(table, i)]
end
Enum.each(["broood", "bananaaa", "hiphophiphop"], fn word ->
IO.inspect word
IO.inspect enc = MoveToFront.encode(word)
IO.puts "#{word == MoveToFront.decode(enc)}\n"
end)

View file

@ -18,3 +18,14 @@ var decodeMTF = function (numList) {
return acc;
}, init).word;
};
//test our algorithms
var words = ['broood', 'bananaaa', 'hiphophiphop'];
var encoded = words.map(encodeMTF);
var decoded = encoded.map(decodeMTF);
//print results
console.log("from encoded:");
console.log(encoded);
console.log("from decoded:");
console.log(decoded);

View file

@ -2,7 +2,7 @@ sub encode ( Str $word ) {
my @sym = 'a' .. 'z';
gather for $word.comb -> $c {
die "Symbol '$c' not found in @sym" if $c eq @sym.none;
@sym[0 .. take (Nil, @sym ... $c).end] .= rotate(-1);
@sym[0 .. take (@sym ... $c).end] .= rotate(-1);
}
}

View file

@ -0,0 +1,75 @@
Function Test-MTF
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,Position=0)]
[string]$word,
[Parameter(Mandatory=$false)]
[string]$SymbolTable = 'abcdefghijklmnopqrstuvwxyz'
)
Begin
{
Function Encode
{
Param
(
[Parameter(Mandatory=$true,Position=0)]
[string]$word,
[Parameter(Mandatory=$false)]
[string]$SymbolTable = 'abcdefghijklmnopqrstuvwxyz'
)
foreach ($letter in $word.ToCharArray())
{
$index = $SymbolTable.IndexOf($letter)
$prop = [ordered]@{
Input = $letter
Output = [int]$index
SymbolTable = $SymbolTable
}
New-Object PSobject -Property $prop
$SymbolTable = $SymbolTable.Remove($index,1).Insert(0,$letter)
}
}
Function Decode
{
Param
(
[Parameter(Mandatory=$true,Position=0)]
[int[]]$index,
[Parameter(Mandatory=$false)]
[string]$SymbolTable = 'abcdefghijklmnopqrstuvwxyz'
)
foreach ($i in $index)
{
#Write-host $i -ForegroundColor Red
$letter = $SymbolTable.Chars($i)
$prop = [ordered]@{
Input = $i
Output = $letter
SymbolTable = $SymbolTable
}
New-Object PSObject -Property $prop
$SymbolTable = $SymbolTable.Remove($i,1).Insert(0,$letter)
}
}
}
Process
{
#Encoding
Write-Host "Encoding $word" -NoNewline
$Encoded = (Encode -word $word).output
Write-Host -NoNewline ": $($Encoded -join ',')"
#Decoding
Write-Host "`nDecoding $($Encoded -join ',')" -NoNewline
$Decoded = (Decode -index $Encoded).output -join ''
Write-Host -NoNewline ": $Decoded`n"
}
End{}
}

View file

@ -1,21 +1,21 @@
/*REXX pgm demonstrates move─to─front algorithm encode/decode sym table.*/
parse arg xxx; if xxx='' then xxx='broood bananaaa hiphophiphop'
one=1 /*for task's requirement. */
do j=1 for words(xxx); x=word(xxx,j) /*process one word at a time*/
@='abcdefghijklmnopqrstuvwxyz' /*sym table: lower alphabet.*/
$= /*set decode string to null.*/
do k=1 for length(x); z=substr(x,k,1) /*encrypt a char in word. */
_=pos(z,@); if _==0 then iterate /*char position in sym table*/
$=$ _-one; @=z || delstr(@,_,1) /*adjust the symbol table. */
end /*k*/ /* [↑] move─to─front encode*/
/*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. */
@='abcdefghijklmnopqrstuvwxyz' /*sym table: lower alphabet.*/
!= /*set encode string to null.*/
do m=1 for words($); n=word($,m)+one /*decode the sequence table.*/
y=substr(@,n,1); !=! || y /*decode character of word. */
@=y || delstr(@,n,1) /*rebuild the symbol table. */
end /*m*/ /* [↑] move─to─front decode*/
@=@@ /*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*/ /*done encoding/decoding words. */
/*stick a fork in it, we're done.*/
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. */

View file

@ -0,0 +1,49 @@
Function mtf_encode(s)
'create the array list and populate it with the initial symbol position
Set symbol_table = CreateObject("System.Collections.ArrayList")
For j = 97 To 122 'a to z in decimal.
symbol_table.Add Chr(j)
Next
output = ""
For i = 1 To Len(s)
char = Mid(s,i,1)
If i = Len(s) Then
output = output & symbol_table.IndexOf(char,0)
symbol_table.RemoveAt(symbol_table.LastIndexOf(char))
symbol_table.Insert 0,char
Else
output = output & symbol_table.IndexOf(char,0) & " "
symbol_table.RemoveAt(symbol_table.LastIndexOf(char))
symbol_table.Insert 0,char
End If
Next
mtf_encode = output
End Function
Function mtf_decode(s)
'break the function argument into an array
code = Split(s," ")
'create the array list and populate it with the initial symbol position
Set symbol_table = CreateObject("System.Collections.ArrayList")
For j = 97 To 122 'a to z in decimal.
symbol_table.Add Chr(j)
Next
output = ""
For i = 0 To UBound(code)
char = symbol_table(code(i))
output = output & char
If code(i) <> 0 Then
symbol_table.RemoveAt(symbol_table.LastIndexOf(char))
symbol_table.Insert 0,char
End If
Next
mtf_decode = output
End Function
'Testing the functions
wordlist = Array("broood","bananaaa","hiphophiphop")
For Each word In wordlist
WScript.StdOut.Write word & " encodes as " & mtf_encode(word) & " and decodes as " &_
mtf_decode(mtf_encode(word)) & "."
WScript.StdOut.WriteBlankLines(1)
Next