How to resolve the algorithm Run-length encoding step by step in the Vedit macro language programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Run-length encoding step by step in the Vedit macro language 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 Vedit macro language programming language
Source code in the vedit programming language
:RL_ENCODE:
BOF
While (!At_EOF) {
if (At_EOL) { Line(1) Continue } // skip newlines
#1 = Cur_Char // #1 = character
Match("(.)\1*", REGEXP) // count run length
#2 = Chars_Matched // #2 = run length
if (#2 > 127) { #2 = 127 } // can be max 127
if (#2 > 1 || #1 > 127) {
Del_Char(#2)
Ins_Char(#2 | 128) // run length (high bit set)
Ins_Char(#1) // character
} else { // single ASCII char
Char // skip
}
}
Return
:RL_DECODE:
BOF
While (!At_EOF) {
#2 = Cur_Char
if (#2 > 127) { // is this run length?
#1 = Cur_Char(1) // #1 = character value
Del_Char(2)
Ins_Char(#1, COUNT, #2 & 127)
} else { // single ASCII char
Char
}
}
Return
You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the PHP programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the Ada programming language
You may also check:How to resolve the algorithm Steffensen's method step by step in the RATFOR programming language
You may also check:How to resolve the algorithm HTTP step by step in the FreeBASIC programming language