How to resolve the algorithm Run-length encoding step by step in the Logo programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Run-length encoding step by step in the Logo programming language
Table of Contents
Problem Statement
Given a string containing uppercase characters (A-Z), compress repeated 'runs' of the same character by storing the length of that run, and provide a function to reverse the compression. The output can be anything, as long as you can recreate the input with it.
Note: the encoding step in the above example is the same as a step of the Look-and-say sequence.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Run-length encoding step by step in the Logo programming language
Source code in the logo programming language
to encode :str [:out "||] [:count 0] [:last first :str]
if empty? :str [output (word :out :count :last)]
if equal? first :str :last [output (encode bf :str :out :count+1 :last)]
output (encode bf :str (word :out :count :last) 1 first :str)
end
to reps :n :w
output ifelse :n = 0 ["||] [word :w reps :n-1 :w]
end
to decode :str [:out "||] [:count 0]
if empty? :str [output :out]
if number? first :str [output (decode bf :str :out 10*:count + first :str)]
output (decode bf :str word :out reps :count first :str)
end
make "foo "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
make "rle encode :foo
show equal? :foo decode :rle
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the Crystal programming language
You may also check:How to resolve the algorithm Window creation/X11 step by step in the Racket programming language
You may also check:How to resolve the algorithm Number reversal game step by step in the VBA programming language
You may also check:How to resolve the algorithm Date manipulation step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Haskell programming language