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
47
Task/Run-length-encoding/Lasso/run-length-encoding.lasso
Normal file
47
Task/Run-length-encoding/Lasso/run-length-encoding.lasso
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
define rle(str::string)::string => {
|
||||
local(orig = #str->values->asCopy,newi=array, newc=array, compiled=string)
|
||||
while(#orig->size) => {
|
||||
if(not #newi->size) => {
|
||||
#newi->insert(1)
|
||||
#newc->insert(#orig->first)
|
||||
#orig->remove(1)
|
||||
else
|
||||
if(#orig->first == #newc->last) => {
|
||||
#newi->get(#newi->size) += 1
|
||||
else
|
||||
#newi->insert(1)
|
||||
#newc->insert(#orig->first)
|
||||
}
|
||||
#orig->remove(1)
|
||||
}
|
||||
}
|
||||
loop(#newi->size) => {
|
||||
#compiled->append(#newi->get(loop_count)+#newc->get(loop_count))
|
||||
}
|
||||
return #compiled
|
||||
}
|
||||
define rlde(str::string)::string => {
|
||||
local(o = string)
|
||||
while(#str->size) => {
|
||||
loop(#str->size) => {
|
||||
if(#str->isualphabetic(loop_count)) => {
|
||||
if(loop_count == 1) => {
|
||||
#o->append(#str->get(loop_count))
|
||||
#str->removeLeading(#str->get(loop_count))
|
||||
loop_abort
|
||||
}
|
||||
local(num = integer(#str->substring(1,loop_count)))
|
||||
#o->append(#str->get(loop_count)*#num)
|
||||
#str->removeLeading(#num+#str->get(loop_count))
|
||||
loop_abort
|
||||
}
|
||||
}
|
||||
}
|
||||
return #o
|
||||
}
|
||||
//Tests:
|
||||
rle('WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW')
|
||||
rle('dsfkjhhkdsjfhdskhshdjjfhhdlsllw')
|
||||
|
||||
rlde('12W1B12W3B24W1B14W')
|
||||
rlde('1d1s1f1k1j2h1k1d1s1j1f1h1d1s1k1h1s1h1d2j1f2h1d1l1s2l1w')
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
function rlEncode str
|
||||
local charCount
|
||||
put 1 into charCount
|
||||
repeat with i = 1 to the length of str
|
||||
if char i of str = char (i + 1) of str then
|
||||
add 1 to charCount
|
||||
else
|
||||
put char i of str & charCount after rle
|
||||
put 1 into charCount
|
||||
end if
|
||||
end repeat
|
||||
return rle
|
||||
end rlEncode
|
||||
|
||||
function rlDecode str
|
||||
repeat with i = 1 to the length of str
|
||||
if char i of str is not a number then
|
||||
put char i of str into curChar
|
||||
put 0 into curNum
|
||||
else
|
||||
repeat with n = i to len(str)
|
||||
if isnumber(char n of str) then
|
||||
put char n of str after curNum
|
||||
else
|
||||
put repeatString(curChar,curNum) after rldec
|
||||
put n - 1 into i
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
end if
|
||||
if i = len(str) then --dump last char
|
||||
put repeatString(curChar,curNum) after rldec
|
||||
end if
|
||||
end repeat
|
||||
return rldec
|
||||
end rlDecode
|
||||
|
||||
function repeatString str,rep
|
||||
repeat rep times
|
||||
put str after repStr
|
||||
end repeat
|
||||
return repStr
|
||||
end repeatString
|
||||
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)])
|
||||
12
Task/Run-length-encoding/Oforth/run-length-encoding.oforth
Normal file
12
Task/Run-length-encoding/Oforth/run-length-encoding.oforth
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
: encode(s)
|
||||
StringBuffer new
|
||||
s group apply(#[ tuck size asString << swap first <<c ]) ;
|
||||
|
||||
: decode(s)
|
||||
| c i |
|
||||
StringBuffer new
|
||||
0 s forEach: c [
|
||||
c isDigit ifTrue: [ 10 * c asDigit + continue ]
|
||||
loop: i [ c <<c ] 0
|
||||
]
|
||||
drop ;
|
||||
30
Task/Run-length-encoding/Phix/run-length-encoding.phix
Normal file
30
Task/Run-length-encoding/Phix/run-length-encoding.phix
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
function encode(sequence s)
|
||||
sequence out = {}
|
||||
integer prev_char,count = 1
|
||||
if length(s) then
|
||||
prev_char = s[1]
|
||||
for i=2 to length(s) do
|
||||
if s[i]!=prev_char then
|
||||
out &= {count,prev_char}
|
||||
prev_char = s[i]
|
||||
count = 1
|
||||
else
|
||||
count += 1
|
||||
end if
|
||||
end for
|
||||
out &= {count,prev_char}
|
||||
end if
|
||||
return out
|
||||
end function
|
||||
|
||||
function decode(sequence s)
|
||||
sequence out = {}
|
||||
for i=1 to length(s) by 2 do
|
||||
out &= repeat(s[i+1],s[i])
|
||||
end for
|
||||
return out
|
||||
end function
|
||||
|
||||
sequence s = encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW")
|
||||
pp(s)
|
||||
?decode(s)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
func encode(str) {
|
||||
str.gsub(/((.)(\2*))/, {|a,b| "#{a.len}#{b}" });
|
||||
}
|
||||
|
||||
func decode(str) {
|
||||
str.gsub(/(\d+)(.)/, {|a,b| b * a.to_i });
|
||||
}
|
||||
12
Task/Run-length-encoding/Sidef/run-length-encoding-2.sidef
Normal file
12
Task/Run-length-encoding/Sidef/run-length-encoding-2.sidef
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
func encode(str) {
|
||||
str.gsub(/(.)(\1{0,254})/, {|a,b| b.len+1 -> chr + a});
|
||||
}
|
||||
|
||||
func decode(str) {
|
||||
var chars = str.chars;
|
||||
var r = '';
|
||||
(chars.len/2 -> int).range.each { |i|
|
||||
r += (chars[2*i + 1] * chars[2*i].ord);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
14
Task/Run-length-encoding/Swift/run-length-encoding-1.swift
Normal file
14
Task/Run-length-encoding/Swift/run-length-encoding-1.swift
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import Foundation
|
||||
|
||||
// "WWWBWW" -> [(3, W), (1, B), (2, W)]
|
||||
func encode(input: String) -> [(Int, Character)] {
|
||||
return input.characters.reduce([(Int, Character)]()) {
|
||||
if $0.last?.1 == $1 { var r = $0; r[r.count - 1].0++; return r }
|
||||
return $0 + [(1, $1)]
|
||||
}
|
||||
}
|
||||
|
||||
// [(3, W), (1, B), (2, W)] -> "WWWBWW"
|
||||
func decode(encoded: [(Int, Character)]) -> String {
|
||||
return encoded.reduce("") { $0 + String(count: $1.0, repeatedValue: $1.1) }
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
let input = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
|
||||
let output = decode(encode(input))
|
||||
print(output == input)
|
||||
15
Task/Run-length-encoding/Swift/run-length-encoding-3.swift
Normal file
15
Task/Run-length-encoding/Swift/run-length-encoding-3.swift
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// "3W1B2W" -> "WWWBWW"
|
||||
func decode(encoded: String) -> String {
|
||||
let scanner = NSScanner(string: encoded)
|
||||
var char: NSString? = nil
|
||||
var count: Int = 0
|
||||
var out = ""
|
||||
|
||||
while scanner.scanInteger(&count) {
|
||||
while scanner.scanCharactersFromSet(NSCharacterSet.letterCharacterSet(), intoString: &char) {
|
||||
out += String(count: count, repeatedValue: Character(char as! String))
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
let encodedString = encode(input).reduce("") { $0 + "\($1.0)\($1.1)" }
|
||||
print(encodedString)
|
||||
let outputString = decode(encodedString)
|
||||
print(outputString == input)
|
||||
9
Task/Run-length-encoding/jq/run-length-encoding-1.jq
Normal file
9
Task/Run-length-encoding/jq/run-length-encoding-1.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def runs:
|
||||
reduce .[] as $item
|
||||
( [];
|
||||
if . == [] then [ [ $item, 1] ]
|
||||
else .[length-1] as $last
|
||||
| if $last[0] == $item then .[length-1] = [$item, $last[1] + 1]
|
||||
else . + [[$item, 1]]
|
||||
end
|
||||
end ) ;
|
||||
9
Task/Run-length-encoding/jq/run-length-encoding-2.jq
Normal file
9
Task/Run-length-encoding/jq/run-length-encoding-2.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def run_length_encode:
|
||||
explode | runs | reduce .[] as $x (""; . + "\($x[1])\([$x[0]]|implode)");
|
||||
|
||||
def run_length_decode:
|
||||
reduce (scan( "[0-9]+[A-Z]" )) as $pair
|
||||
( "";
|
||||
($pair[0:-1] | tonumber) as $n
|
||||
| $pair[-1:] as $letter
|
||||
| . + ($n * $letter)) ;
|
||||
1
Task/Run-length-encoding/jq/run-length-encoding-3.jq
Normal file
1
Task/Run-length-encoding/jq/run-length-encoding-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
"ABBCCC" | run_length_encode | run_length_decode
|
||||
2
Task/Run-length-encoding/jq/run-length-encoding-4.jq
Normal file
2
Task/Run-length-encoding/jq/run-length-encoding-4.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$ jq -n -f Run_length_encoding.jq
|
||||
"ABBCCC"
|
||||
Loading…
Add table
Add a link
Reference in a new issue