Jump to content

Run-length encoding: Difference between revisions

sql language example added
(sql language example added)
Line 2,887:
12W1B12W3B24W1B14W
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW</pre>
 
=={{header|SQL}}==
{{works with|T-SQL}}
* RLE encoding
<lang tsql>
declare @string varchar(1000)
set @string = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
 
;with
ints(num) as
(
select 1
union all
select num+1
from ints
where num+1 <= LEN(@string)
)
,
chars(num,chr,nextChr,isGroupEnd) as
(
select tmp.*, case when tmp.nextChr <> tmp.chr then 1 else 0 end groupEnds
from (
select num,
substring(@string, num, 1) chr,
(select substring(@string, num+1, 1)) nextChr
from ints
) tmp
)
select @string Input, cast((
select cast(maxNoWithinGroup as varchar(10)) + chr
from (
select *, max(noWithinGroup) over (partition by chr, groupNo) maxNoWithinGroup
from (
select num,
chr,
groupNo,
row_number() over( partition by chr, groupNo order by num) noWithinGroup
from (
select *, (select count(*)
from chars chars2
where chars2.isGroupEnd = 1 and
chars2.chr = chars.chr and
chars2.num < chars.num) groupNo
from chars
) tmp
) sub
) final
where noWithinGroup = 1
order by num
for xml path('')) as xml).value('.','varchar(max)') RleCompressed
option (maxrecursion 0)
</lang>
 
=={{header|Standard ML}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.