50 lines
1.1 KiB
Nim
50 lines
1.1 KiB
Nim
|
import
|
||
|
std / [setutils, sequtils, tables]
|
||
|
|
||
|
const
|
||
|
textContent* = readFile("wizard.txt")
|
||
|
|
||
|
########### Text Endocing/Decoding ############
|
||
|
proc createStringTable*(text: string): Table[char, int] =
|
||
|
|
||
|
let charSet = text.toSet
|
||
|
|
||
|
var stringToInt = initTable[char, int]()
|
||
|
|
||
|
for id, glyph in charSet.toSeq:
|
||
|
stringToInt[glyph] = id
|
||
|
|
||
|
result = stringToInt
|
||
|
|
||
|
proc createIntTable*(text: string): Table[int,char] =
|
||
|
|
||
|
let charSet = text.toSet
|
||
|
|
||
|
var intToString = initTable[int, char]()
|
||
|
|
||
|
for id, glyph in charSet.toSeq:
|
||
|
intToString[id] = glyph
|
||
|
|
||
|
result = intToString
|
||
|
|
||
|
proc encodeString*(str: string, stringToInt: Table[char, int]): seq[int] =
|
||
|
result = @[]
|
||
|
|
||
|
for glyph in str:
|
||
|
result.add(stringToInt[glyph])
|
||
|
|
||
|
proc decodeString*(list: seq[int], intToString: Table[int, char]): string =
|
||
|
result = ""
|
||
|
|
||
|
for item in list:
|
||
|
result.add(intToString[item])
|
||
|
|
||
|
proc decodeString*(letter: int, intToString: Table[int, char]): char =
|
||
|
result = intToString[letter]
|
||
|
|
||
|
|
||
|
|
||
|
let
|
||
|
stringToInt*: Table[char, int] = createStringTable(textContent)
|
||
|
intToString*: Table[int, char] = createIntTable(textContent)
|