Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
26
Task/Run-length-encoding/Nim/run-length-encoding.nim
Normal file
26
Task/Run-length-encoding/Nim/run-length-encoding.nim
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import strutils
|
||||
|
||||
type RunLength = tuple[c: char, n: int]
|
||||
|
||||
proc encode(inp): seq[RunLength] =
|
||||
result = @[]
|
||||
var count = 1
|
||||
var prev: char
|
||||
|
||||
for c in inp:
|
||||
if c != prev:
|
||||
if prev != chr(0):
|
||||
result.add((prev,count))
|
||||
count = 1
|
||||
prev = c
|
||||
else:
|
||||
inc(count)
|
||||
result.add((prev,count))
|
||||
|
||||
proc decode(lst: openarray[RunLength]): string =
|
||||
result = ""
|
||||
for x in lst:
|
||||
result.add(repeatChar(x.n, x.c))
|
||||
|
||||
echo encode("aaaaahhhhhhmmmmmmmuiiiiiiiaaaaaa")
|
||||
echo decode([('a', 5), ('h', 6), ('m', 7), ('u', 1), ('i', 7), ('a', 6)])
|
||||
Loading…
Add table
Add a link
Reference in a new issue