Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
22
Task/Move-to-front-algorithm/Nim/move-to-front-algorithm.nim
Normal file
22
Task/Move-to-front-algorithm/Nim/move-to-front-algorithm.nim
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import algorithm, sequtils, strformat
|
||||
|
||||
const SymbolTable = toSeq('a'..'z')
|
||||
|
||||
func encode(s: string): seq[int] =
|
||||
var symtable: seq[char] = SymbolTable
|
||||
for c in s:
|
||||
let idx = symtable.find(c)
|
||||
result.add idx
|
||||
symtable.rotateLeft(0..idx, -1)
|
||||
|
||||
func decode(s: seq[int]): string =
|
||||
var symtable = SymbolTable
|
||||
for idx in s:
|
||||
result.add symtable[idx]
|
||||
symtable.rotateLeft(0..idx, -1)
|
||||
|
||||
for word in ["broood", "babanaaa", "hiphophiphop"]:
|
||||
let encoded = word.encode()
|
||||
let decoded = encoded.decode()
|
||||
let status = if decoded == word: "correctly" else: "incorrectly"
|
||||
echo &"'{word}' encodes to {encoded} which {status} decodes to '{decoded}'."
|
||||
Loading…
Add table
Add a link
Reference in a new issue