Showing posts with label lists. Show all posts
Showing posts with label lists. Show all posts

Friday, May 26, 2017

Rows to columns transposition

Rows to columns transposition.
Works best with even length lists.

In other case, columns will be padded with undefined.
Usage:

rows = [[40, 16, 54, 36, 36, 42, 67, 17, 40] ,
        [76, 20, 60, 51, 58, 73, 11, 17, 48] ,
        [50, 72, 11, 57,  3, 94, 33, 93, 13] ];
cols=rows_to_columns(rows);
echo ( rows);echo (  cols);

function rows_to_columns(l)= 
let(longest_list=max( [for(i=[0:len(l)-1])len(l[i])-1]))
[for( i=[0:longest_list])[for( j=[0:len(l)-1]) l[j][i]]];
 

Thursday, October 13, 2016

Run-length encoding (RLE)

Run-length encoding (RLE) is a very simple form of lossless data compression in which runs of data are stored as a single data value and count.

Encoder and decoder function showcase two kinds of conditional flow. Either type can be used for both jobs.
First case the values are prepared with conditions before a single call.
Second the condition is done first selecting between two calls of different values passes.

 Usage:



string1="hksdjfhkkkkdsfkljhriirenfcnlknd";
echo(RLEencode(string1));
string2="hhksdjfhkkkkdsfkljhriirenfcnlknd";
echo(RLEencode(string2));
string3=[0,1,1,2,2,2,2,1,1,0,-1,-1,-2,-2,-2,-2,-1,-1,0,1,1,2,2,2,2,1,1,0,-1,-1];
de=(RLEencode(string3));
echo(string3);
echo(de);
echo(RLEdecode(de));

function RLEencode(s,o,run=0,n=1)=
n==len(s)+1?o:
let(
newrun=s[n]==s[n-1]?run+1:0,
newo=s[n]==s[n-1]?o:concat(  o!=undef?o:[],[[run+1,s[ n-1]]])
)
RLEencode(s,newo,newrun,n+1)
;

function RLEdecode(s,o=[],run=1,n=-1)=
n==len(s)?o:
 let(next=[s[n][1]])
run ==1?

RLEdecode(s,concat(   o,next!=undef&&n>-1?next:[]),s[n+1][0],n+1)
:
RLEdecode(s,concat(   o,next!=undef?next:[] ),run-1,n);