Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
27
Task/Run-length-encoding/Elixir/run-length-encoding.elixir
Normal file
27
Task/Run-length-encoding/Elixir/run-length-encoding.elixir
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
defmodule Run_length do
|
||||
def encode(str) when is_bitstring(str) do
|
||||
to_char_list(str) |> encode |> to_string
|
||||
end
|
||||
def encode(list) when is_list(list) do
|
||||
Enum.chunk_by(list, &(&1))
|
||||
|> Enum.flat_map(fn chars -> to_char_list(length(chars)) ++ [hd(chars)] end)
|
||||
end
|
||||
|
||||
def decode(str) when is_bitstring(str) do
|
||||
Regex.scan(~r/(\d+)(.)/, str)
|
||||
|> Enum.map_join(fn [_,n,c] -> String.duplicate(c, String.to_integer(n)) end)
|
||||
end
|
||||
def decode(list) when is_list(list) do
|
||||
to_string(list) |> decode |> to_char_list
|
||||
end
|
||||
end
|
||||
|
||||
text = [ string: "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW",
|
||||
char_list: 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW' ]
|
||||
|
||||
Enum.each(text, fn {type, txt} ->
|
||||
IO.puts type
|
||||
txt |> IO.inspect
|
||||
|> Run_length.encode |> IO.inspect
|
||||
|> Run_length.decode |> IO.inspect
|
||||
end)
|
||||
2
Task/Run-length-encoding/J/run-length-encoding-4.j
Normal file
2
Task/Run-length-encoding/J/run-length-encoding-4.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
torle=: (#, {.);.1~ 1,2 ~:/\ ]
|
||||
frle=: #/@|:
|
||||
10
Task/Run-length-encoding/J/run-length-encoding-5.j
Normal file
10
Task/Run-length-encoding/J/run-length-encoding-5.j
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
torle a.i.'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
|
||||
12 87
|
||||
1 66
|
||||
12 87
|
||||
3 66
|
||||
24 87
|
||||
1 66
|
||||
14 87
|
||||
u: frle torle a.i.'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
|
||||
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
|
||||
|
|
@ -1,13 +1,7 @@
|
|||
# functional approach (return the encoded or decoded string)
|
||||
sub encode {
|
||||
(my $str = shift) =~ s {(.)(\1*)} {length($&).$1}gse;
|
||||
return $str; }
|
||||
sub decode {
|
||||
(my $str = shift) =~ s {(\d+)(.)} {$2 x $1}gse;
|
||||
return $str;}
|
||||
shift =~ s/(.)\1*/length($&).$1/grse;
|
||||
}
|
||||
|
||||
# procedural approach (modify the argument in place)
|
||||
sub encode {
|
||||
$_[0] =~ s {(.)(\1*)} {length($&).$1}gse; }
|
||||
sub decode {
|
||||
$_[0] =~ s {(\d+)(.)} {$2 x $1}gse; }
|
||||
shift =~ s/(\d+)(.)/$2 x $1/grse;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,7 @@
|
|||
sub encode
|
||||
{my $str = shift;
|
||||
$str =~ s {(.)(\1{0,254})} {pack("C",(length($2) + 1)) . $1 }gse;
|
||||
return $str;}
|
||||
|
||||
sub decode
|
||||
{
|
||||
my @str = split //, shift;
|
||||
my $r = "";
|
||||
foreach my $i (0 .. scalar(@str)/2-1) {
|
||||
$r .= $str[2*$i + 1] x unpack("C", $str[2*$i]);
|
||||
}
|
||||
return $r;
|
||||
sub encode {
|
||||
shift =~ s/(.)\1{0,254}/pack("C", length($&)).$1/grse;
|
||||
}
|
||||
|
||||
sub decode {
|
||||
shift =~ s/(.)(.)/$2 x unpack("C", $1)/grse;
|
||||
}
|
||||
|
|
|
|||
32
Task/Run-length-encoding/Perl/run-length-encoding-3.pl
Normal file
32
Task/Run-length-encoding/Perl/run-length-encoding-3.pl
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
sub encode {
|
||||
my $str = shift;
|
||||
my $ret = "";
|
||||
my $nonrep = "";
|
||||
while ($str =~ m/(.)\1{0,127}|\z/gs) {
|
||||
my $len = length($&);
|
||||
if (length($nonrep) && (length($nonrep) == 127 || $len != 1)) {
|
||||
$ret .= pack("C", 128 + length($nonrep)) . $nonrep;
|
||||
$nonrep = "";
|
||||
}
|
||||
if ($len == 1) { $nonrep .= $1 }
|
||||
elsif ($len > 1) { $ret .= pack("C", $len) . $1 }
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub decode {
|
||||
my $str = shift;
|
||||
my $ret = "";
|
||||
for (my $i = 0; $i < length($str);) {
|
||||
my $len = unpack("C", substr($str, $i, 1));
|
||||
if ($len <= 128) {
|
||||
$ret .= substr($str, $i + 1, 1) x $len;
|
||||
$i += 2;
|
||||
}
|
||||
else {
|
||||
$ret .= substr($str, $i + 1, $len - 128);
|
||||
$i += 1 + $len - 128;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
4
Task/Run-length-encoding/Perl/run-length-encoding-4.pl
Normal file
4
Task/Run-length-encoding/Perl/run-length-encoding-4.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use Data::Dump qw(dd);
|
||||
dd my $str = "XXXXXABCDEFGHIoooooooooooooooooooooooooAAAAAA";
|
||||
dd my $enc = encode($str);
|
||||
dd decode($enc);
|
||||
|
|
@ -1,22 +1,19 @@
|
|||
/*REXX program encodes a string by using a run-length scheme. */
|
||||
parse arg x . /*normally, input would be a file*/
|
||||
/*═══ arg x . ═══*/ /*◄── use if X must be uppercase.*/
|
||||
/*REXX program encodes a string by using a run─length encoding scheme. */
|
||||
parse arg x . /*normally, input would be in a file. */
|
||||
def= 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
|
||||
if x='' then x=def /*No input? Then use the default.*/
|
||||
Lx=length(x) /*get the length of the X string.*/
|
||||
y= /*Y is the output string (so far)*/
|
||||
do j=1 by 0 to Lx /*J is incremented (below). */
|
||||
c=substr(x,j,1) /*pick a character, check for err*/
|
||||
if \datatype(c,'M') then do; say "error!: data isn't alphabetic:" c; exit 13; end
|
||||
r=0 /*R is NOT the number of chars. */
|
||||
do k=j+1 to Lx while substr(x,k,1)==c
|
||||
r=r+1 /*R is a replication count. */
|
||||
end /*k*/
|
||||
j=j+1+r /*modify (add to) the do index. */
|
||||
if r==0 then r= /*don't use R if R is zero.*/
|
||||
Y = Y || r || c /*add it to the encoded string.*/
|
||||
end /*j*/
|
||||
if x='' then x=def /*Input not specified? Then use default*/
|
||||
Lx=length(x) /*get the length of the X string. */
|
||||
y= /*Y: is the output string (so far). */
|
||||
do j=1 by 0 to Lx /*J: is incremented within the loop. */
|
||||
c=substr(x,j,1) /*pick a character, check for an error.*/
|
||||
if \datatype(c,'M') then do; say "error!: data isn't alphabetic:" c; exit 13; end
|
||||
r=0 /*R: is NOT the number of characters. */
|
||||
do k=j+1 to Lx while substr(x,k,1)==c; r=r+1
|
||||
end /*k*/ /*R: is a replication count for a char*/
|
||||
j=j+1+r /*increment (add to) the DO loop index.*/
|
||||
if r==0 then r= /*don't use R if it is equal to zero.*/
|
||||
Y = Y || r || c /*add character to the encoded string. */
|
||||
end /*j*/
|
||||
|
||||
say ' input=' x
|
||||
say 'encoded=' y
|
||||
/*stick a fork in it, we're done.*/
|
||||
say ' input=' x
|
||||
say 'encoded=' y /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,23 +1,21 @@
|
|||
/*REXX program decodes a string by using a run-length scheme. */
|
||||
parse arg x . /*normally, input would be a file*/
|
||||
if x=='' then x='11WB11W2B23WB13W' /*No input? Then use the default*/
|
||||
Lx=length(x) /*get the length of the X string.*/
|
||||
y= /*Y is the output string (so far)*/
|
||||
do j=1 by 0 to Lx /*warning! J is modified below.*/
|
||||
/*REXX program decodes a string by using a run─length decoding scheme. */
|
||||
parse arg x . /*normally, input would be in a file. */
|
||||
if x=='' then x=11WB11W2B23WB13W /*X not specified? Then use default.*/
|
||||
Lx=length(x) /*get the length of the input string. */
|
||||
y= /*Y: is the output string (so far). */
|
||||
do j=1 by 0 to Lx /*warning! J is modified within loop.*/
|
||||
c=substr(x,j,1)
|
||||
if \datatype(c,'W') then do /*a loner char, simply add to OUT*/
|
||||
if \datatype(c,'W') then do /*a loner char, simply add to output. */
|
||||
y=y || c; j=j+1; iterate /*j*/
|
||||
end
|
||||
d=1
|
||||
do k=j+1 to Lx while datatype(substr(x,k,1),'w') /*look for #end*/
|
||||
d=d+1 /*D is the number of digs so far.*/
|
||||
end /*k*/
|
||||
d=1 /* [↓] W: a Whole number.*/
|
||||
do k=j+1 to Lx while datatype(substr(x,k,1),'w'); d=d+1 /*end of #?*/
|
||||
end /*k*/ /*D: is the number of characters so far*/
|
||||
|
||||
n=substr(x,j,d)+1 /*D is length of encoded number.*/
|
||||
y=y || copies(substr(x,k,1),n) /*N is now the number of chars. */
|
||||
j=j+1+d /*increment the DO loop index. */
|
||||
n=substr(x,j,d)+1 /*D: is length of the encoded number. */
|
||||
y=y || copies(substr(x,k,1), n) /*N: is now the number of characters. */
|
||||
j=j+1+d /*increment the DO loop index by D+1. */
|
||||
end /*j*/
|
||||
|
||||
say ' input=' x
|
||||
say 'decoded=' y
|
||||
/*stick a fork in it, we're done.*/
|
||||
say 'decoded=' y /*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
39
Task/Run-length-encoding/REXX/run-length-encoding-3.rexx
Normal file
39
Task/Run-length-encoding/REXX/run-length-encoding-3.rexx
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
s='WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
|
||||
Say ' s='s
|
||||
enc=encode(s)
|
||||
Say 'enc='enc
|
||||
dec=decode(enc)
|
||||
Say 'dec='dec
|
||||
if dec==s Then Say 'OK'
|
||||
Exit
|
||||
|
||||
encode: Procedure
|
||||
Parse Arg s
|
||||
c=left(s,1)
|
||||
cnt=1
|
||||
ol=''
|
||||
Do i=2 To length(s)
|
||||
If substr(s,i,1)=c Then
|
||||
cnt=cnt+1
|
||||
Else Do
|
||||
Call o cnt||c
|
||||
c=substr(s,i,1)
|
||||
cnt=1
|
||||
End
|
||||
End
|
||||
Call o cnt||c
|
||||
Return ol
|
||||
|
||||
decode: Procedure
|
||||
Parse Arg s
|
||||
abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
ol=''
|
||||
Do While s<>''
|
||||
p=verify(s,abc,'M')
|
||||
Parse Var s cnt =(p) c +1 s
|
||||
Call o copies(c,cnt)
|
||||
End
|
||||
Return ol
|
||||
|
||||
o: ol=ol||arg(1)
|
||||
Return
|
||||
48
Task/Run-length-encoding/REXX/run-length-encoding-4.rexx
Normal file
48
Task/Run-length-encoding/REXX/run-length-encoding-4.rexx
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
s='WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
|
||||
Say ' s='s
|
||||
enc=encode(s)
|
||||
Say 'enc='enc
|
||||
dec=decode(enc)
|
||||
Say 'dec='dec
|
||||
if dec==s Then Say 'OK'
|
||||
Exit
|
||||
|
||||
encode: Procedure
|
||||
Parse Arg s
|
||||
c=left(s,1)
|
||||
cnt=1
|
||||
ol=''
|
||||
Do i=2 To length(s)
|
||||
If substr(s,i,1)=c Then
|
||||
cnt=cnt+1
|
||||
Else Do
|
||||
If cnt=1 Then
|
||||
Call o c
|
||||
Else
|
||||
Call o cnt||c
|
||||
c=substr(s,i,1)
|
||||
cnt=1
|
||||
End
|
||||
End
|
||||
Call o cnt||c
|
||||
Return ol
|
||||
|
||||
decode: Procedure
|
||||
Parse Arg s
|
||||
abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
ol=''
|
||||
Do While s<>''
|
||||
p=verify(s,abc,'M')
|
||||
If pos(left(s,1),abc)>0 Then Do
|
||||
Parse Var s c +1 s
|
||||
Call o c
|
||||
End
|
||||
Else Do
|
||||
Parse Var s cnt =(p) c +1 s
|
||||
Call o copies(c,cnt)
|
||||
End
|
||||
End
|
||||
Return ol
|
||||
|
||||
o: ol=ol||arg(1)
|
||||
Return
|
||||
|
|
@ -1,9 +1,14 @@
|
|||
def encode(string)
|
||||
string.scan(/(.)(\1*)/).collect do |char, repeat|
|
||||
[1 + repeat.length, char]
|
||||
end.join
|
||||
# run_encode("aaabbbbc") #=> [["a", 3], ["b", 4], ["c", 1]]
|
||||
def run_encode(string)
|
||||
string
|
||||
.chars
|
||||
.chunk{|i| i}
|
||||
.map {|kind, array| [kind, array.length]}
|
||||
end
|
||||
|
||||
def decode(string)
|
||||
string.scan(/(\d+)(\D)/).collect {|length, char| char * length.to_i}.join
|
||||
# run_decode([["a", 3], ["b", 4], ["c", 1]]) #=> "aaabbbbc"
|
||||
def run_decode(char_counts)
|
||||
char_counts
|
||||
.map{|char, count| char * count}
|
||||
.join
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
def encode(string)
|
||||
string.scan(/(.)(\1*)/).inject("") do |encoding, (char, repeat)|
|
||||
encoding << (1 + repeat.length).to_s << char
|
||||
end
|
||||
string.scan(/(.)(\1*)/).collect do |char, repeat|
|
||||
[1 + repeat.length, char]
|
||||
end.join
|
||||
end
|
||||
|
||||
def decode(string)
|
||||
string.scan(/(\d+)(\D)/).inject("") do |decoding, (length, char)|
|
||||
decoding << char * length.to_i
|
||||
end
|
||||
string.scan(/(\d+)(\D)/).collect {|length, char| char * length.to_i}.join
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
def encode(str)
|
||||
str.gsub(/(.)\1*/) {$&.length.to_s + $1}
|
||||
def encode(string)
|
||||
string.scan(/(.)(\1*)/).inject("") do |encoding, (char, repeat)|
|
||||
encoding << (1 + repeat.length).to_s << char
|
||||
end
|
||||
end
|
||||
|
||||
def decode(str)
|
||||
str.gsub(/(\d+)(\D)/) {$2 * $1.to_i}
|
||||
def decode(string)
|
||||
string.scan(/(\d+)(\D)/).inject("") do |decoding, (length, char)|
|
||||
decoding << char * length.to_i
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
orig = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
|
||||
p enc = encode(orig)
|
||||
p dec = decode(enc)
|
||||
puts "success!" if dec == orig
|
||||
def encode(str)
|
||||
str.gsub(/(.)\1*/) {$&.length.to_s + $1}
|
||||
end
|
||||
|
||||
def decode(str)
|
||||
str.gsub(/(\d+)(\D)/) {$2 * $1.to_i}
|
||||
end
|
||||
|
|
|
|||
4
Task/Run-length-encoding/Ruby/run-length-encoding-5.rb
Normal file
4
Task/Run-length-encoding/Ruby/run-length-encoding-5.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
orig = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
|
||||
p enc = encode(orig)
|
||||
p dec = decode(enc)
|
||||
puts "success!" if dec == orig
|
||||
Loading…
Add table
Add a link
Reference in a new issue