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
|
|
@ -0,0 +1,25 @@
|
|||
func encode(str) {
|
||||
var table = ('a'..'z' -> join);
|
||||
str.chars.map { |c|
|
||||
var s = '';
|
||||
table.sub!(Regex('(.*?)' + c), {|s1| s=s1; c + s1});
|
||||
s.len;
|
||||
}
|
||||
}
|
||||
|
||||
func decode(nums) {
|
||||
var table = ('a'..'z' -> join);
|
||||
nums.map { |n|
|
||||
var s = '';
|
||||
table.sub!(Regex('(.{' + n + '})(.)'), {|s1, s2| s=s2; s2 + s1});
|
||||
s;
|
||||
}.join;
|
||||
}
|
||||
|
||||
%w(broood bananaaa hiphophiphop).each { |test|
|
||||
var encoded = encode(test);
|
||||
say "#{test}: #{encoded}";
|
||||
var decoded = decode(encoded);
|
||||
print "in" if (decoded != test);
|
||||
say "correctly decoded to #{decoded}";
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
module MoveToFront {
|
||||
|
||||
define ABC = @("a".."z")
|
||||
|
||||
func m2f(ar,i) {
|
||||
[ar.delete_index(i)] + ar
|
||||
}
|
||||
|
||||
func encode(str) {
|
||||
var ar = ABC+[]
|
||||
gather {
|
||||
str.each_char { |char|
|
||||
take(var i = ar.index(char))
|
||||
ar = m2f(ar, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func decode(indices) {
|
||||
var ar = ABC+[]
|
||||
gather {
|
||||
indices.each { |i|
|
||||
take ar[i];
|
||||
ar = m2f(ar, i)
|
||||
}
|
||||
}.join
|
||||
}
|
||||
}
|
||||
|
||||
%w(broood bananaaa hiphophiphop).each { |test|
|
||||
var encoded = MoveToFront::encode(test);
|
||||
say "#{test}: #{encoded}";
|
||||
var decoded = MoveToFront::decode(encoded);
|
||||
print "in" if (decoded != test);
|
||||
say "correctly decoded to #{decoded}";
|
||||
}
|
||||
19
Task/Move-to-front-algorithm/jq/move-to-front-algorithm-1.jq
Normal file
19
Task/Move-to-front-algorithm/jq/move-to-front-algorithm-1.jq
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Input is the string to be encoded, st is the initial symbol table (an array)
|
||||
# Output: the encoded string (an array)
|
||||
def m2f_encode(st):
|
||||
reduce explode[] as $ch
|
||||
( [ [], st]; # state: [ans, st]
|
||||
(.[1]|index($ch)) as $ix
|
||||
| .[1] as $st
|
||||
| [ (.[0] + [ $ix ]), [$st[$ix]] + $st[0:$ix] + $st[$ix+1:] ] )
|
||||
| .[0];
|
||||
|
||||
# Input should be the encoded string (an array)
|
||||
# and st should be the initial symbol table (an array)
|
||||
def m2f_decode(st):
|
||||
reduce .[] as $ix
|
||||
( [ [], st]; # state: [ans, st]
|
||||
.[1] as $st
|
||||
| [ (.[0] + [ $st[$ix] ]), [$st[$ix]] + $st[0:$ix] + $st[$ix+1:] ] )
|
||||
| .[0]
|
||||
| implode;
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
("abcdefghijklmnopqrstuvwxyz" | explode) as $ST
|
||||
| ("broood", "bananaaa", "hiphophiphop")
|
||||
| . as $string
|
||||
| m2f_encode($ST)
|
||||
| . as $encoded
|
||||
| m2f_decode($ST) as $decoded
|
||||
| if $string == $decoded then "\($string) => \($encoded) => \($decoded)"
|
||||
else "INTERNAL ERROR: encoding of \($string) => \($encoded) => \($decoded)"
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
$ jq -r -n -f move_to_front.jq
|
||||
broood => [1,17,15,0,0,5] => broood
|
||||
bananaaa => [1,1,13,1,1,1,0,0] => bananaaa
|
||||
hiphophiphop => [7,8,15,2,15,2,2,3,2,2,3,2] => hiphophiphop
|
||||
Loading…
Add table
Add a link
Reference in a new issue