6 kyu

Basic Compression

396 of 458MichaelSel

Description:

First, You will write a very basic compression algorithm

It gets a string:
string="aaaaaaaabaaaa"
And generates an array that sums all the repeating characters like so:
compressed=[[8,"a"],[1,"b"],[4,"a"]]
The compressed version is turned into a string:
compressed='[[8,"a"],[1,"b"],[4,"a"]]'

Finally,
If the compressed version is shorter than the original string, the function will return the compressed version.
Otherwise it will return the original string.

Example1:
string1="aaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaa"
output1='[[26,"a"],[1,"b"],[18,"a"]]'

Example2:
string2="abcde"
output2="abcde"

After you created the compression algorithm, create a decompression algorithm:

It gets a string (output1, output2, etc.).
If the string is comrpessed, it returns the uncompressed version,
If it is uncompressed, it returns the original string unchanged.

//output1='[[26,"a"],[1,"b"],[18,"a"]]'
uncompress(output1)
//returns
"aaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaa"

//output2="abcde"
uncompress(output2)
//returns
"abcde"

Note: The original string may not contain "[]" for obvious reasons.

You are always welcome to check out some of my other katas:

Very Easy (Kyu 8)

Add Numbers

Easy (Kyu 7-6)

Convert Color image to greyscale
Array Transformations
Basic Compression
Find Primes in Range
No Ifs No Buts

Medium (Kyu 5-4)

Identify Frames In An Image
Photoshop Like - Magic Wand
Scientific Notation
Vending Machine - FSA
Find Matching Parenthesis

Hard (Kyu 3-2)

Ascii Art Generator

Fundamentals

Stats:

CreatedMay 11, 2017
PublishedMay 11, 2017
Warriors Trained1395
Total Skips18
Total Code Submissions4725
Total Times Completed458
JavaScript Completions396
Java Completions63
Total Stars34
% of votes with a positive feedback rating91% of 120
Total "Very Satisfied" Votes101
Total "Somewhat Satisfied" Votes16
Total "Not Satisfied" Votes3
Total Rank Assessments10
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • MichaelSel Avatar
  • docgunthrop Avatar
  • Reargem Avatar
Ad