Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,63 @@
import tables
proc compress*(uncompressed: string): seq[int] =
## build the dictionary
var dictionary = initTable[string, int]()
for i in 0..255:
dictionary.add($char(i), i)
var w: string = newString(0)
var compressed = newSeq[int]()
for c in uncompressed:
var wc = w & c
if(dictionary.hasKey(wc)):
w = wc
else:
# writes w to output
compressed.add(dictionary[w])
# wc is a new sequence; add it to the dictionary
dictionary.add(wc, dictionary.len)
w = $c
# write remaining output if necessary
if(w != nil):
compressed.add(dictionary[w])
result = compressed
proc decompress*(compressed: var seq[int]): string =
# build the dictionary
var dictionary = initTable[int, string]()
for i in 0..255:
dictionary.add(i, $char(i))
var w: string = dictionary[compressed[0]]
compressed.delete(0)
var decompressed = w
for k in compressed:
var entry: string = newString(0)
if(dictionary.hasKey(k)):
entry = dictionary[k]
elif(k == dictionary.len):
entry = w & w[0]
else:
raise newException(ValueError, "Bad compressed k: " & $k)
decompressed &= entry
# new sequence; add it to the dictionary
dictionary.add(dictionary.len, w & entry[0])
w = entry
result = decompressed
when isMainModule:
var compressed = compress("TOBEORNOTTOBEORTOBEORNOT")
echo compressed
var decompressed = decompress(compressed)
echo decompressed

View file

@ -0,0 +1,72 @@
# Compress a string to a list of output symbols.
func compress(String uncompressed) -> Array {
 
# Build the dictionary.
var dict_size = 256
var dictionary = Hash()
 
for i in range(dict_size) {
dictionary{i.chr} = i.chr
}
 
var w = ''
var result = []
uncompressed.each { |c|
var wc = w+c
if (dictionary.exists(wc)) {
w = wc
} else {
result << dictionary{w}
# Add wc to the dictionary.
dictionary{wc} = dict_size
dict_size++
w = c
}
}
 
# Output the code for w.
if (w != '') {
result << dictionary{w}
}
 
return result
}
 
# Decompress a list of output ks to a string.
func decompress(Array compressed) -> String {
 
# Build the dictionary.
var dict_size = 256
var dictionary = Hash()
 
for i in range(dict_size) {
dictionary{i.chr} = i.chr
}
 
var w = compressed.shift
var result = w
compressed.each { |k|
var entry = nil
if (dictionary.exists(k)) {
entry = dictionary{k}
} elsif (k == dict_size) {
entry = w+w.first
} else {
die "Bad compressed k: #{k}"
}
result += entry
 
# Add w+entry[0] to the dictionary.
dictionary{dict_size} = w+entry.first
dict_size++
 
w = entry
}
return result
}
 
# How to use:
var compressed = compress('TOBEORNOTTOBEORTOBEORNOT')
say compressed.join(' ')
var decompressed = decompress(compressed)
say decompressed

View file

@ -0,0 +1,62 @@
class LZW {
class func compress(uncompressed:String) -> [Int] {
var dict = [String : Int]()
for i in 0 ..< 256 {
let s = String(UnicodeScalar(i))
dict[s] = i
}
var dictSize = 256
var w = ""
var result = [Int]()
for c in uncompressed {
let wc = w + String(c)
if dict[wc] != nil {
w = wc
} else {
result.append(dict[w]!)
dict[wc] = dictSize++
w = String(c)
}
}
if w != "" {
result.append(dict[w]!)
}
return result
}
class func decompress(compressed:[Int]) -> String? {
var dict = [Int : String]()
for i in 0 ..< 256 {
dict[i] = String(UnicodeScalar(i))
}
var dictSize = 256
var w = String(UnicodeScalar(compressed[0]))
var result = w
for k in compressed[1 ..< compressed.count] {
let entry : String
if let x = dict[k] {
entry = x
} else if k == dictSize {
entry = w + String(first(w)!)
} else {
return nil
}
result += entry
dict[dictSize++] = w + String(first(entry)!)
w = entry
}
return result
}
}
let comp = LZW.compress("TOBEORNOTTOBEORTOBEORNOT")
println(comp)
if let decomp = LZW.decompress(comp) {
println(decomp)
}

View file

@ -0,0 +1,46 @@
# LZW compression/decompression for strings
def lzw_compress:
def decode: [.] | implode;
# Build the dictionary:
256 as $dictSize
| (reduce range(0; $dictSize) as $i ({}; .[ $i | decode ] = $i)) as $dictionary
| reduce explode[] as $i
( [$dictionary, $dictSize, "", []]; # state: [dictionary, dictSize, w, result]
.[0] as $dictionary
| .[1] as $dictSize
| .[2] as $w
| ($i | decode) as $c
| ($w + $c ) as $wc
| if $dictionary[$wc] then .[2] = $wc
else
.[2] = $c # w = c
| .[3] += [$dictionary[$w]] # result += dictionary[w]
| .[0][$wc] = $dictSize # Add wc to the dictionary
| .[1] += 1 # dictSize ++
end
)
# Output the code for w unless w == "":
| if .[2] == "" then .[3]
else .[3] + [.[0][.[2]]]
end
;
def lzw_decompress:
def decode: [.] | implode;
# Build the dictionary - an array of strings
256 as $dictSize
| (reduce range(0; $dictSize) as $i ([]; .[ $i ] = ($i|decode))) as $dictionary
| (.[0]|decode) as $w
| reduce .[1:][] as $k
( [ $dictionary, $dictSize, $w, $w]; # state: [dictionary, dictSize, w, result]
.[0][$k] as $entry
| (if $entry then $entry
elif $k == .[1] then .[2] + .[2][0:1]
else error("lzw_decompress: k=\($k)")
end) as $entry
| .[3] += $entry # result += entry
| .[0][.[1]] = .[2] + $entry[0:1] # dictionary[dictSize] = w + entry.charAt(0);
| .[1] += 1 # dictSize++
| .[2] = $entry # w = entry
) | .[3]
;

View file

@ -0,0 +1 @@
"TOBEORNOTTOBEORTOBEORNOT" | lzw_compress| lzw_decompress