← Back to challenges

String Compression from Characters Array

PythonHardarrayscontrol_flowlanguage_fundamentalsstrings

Instructions

The function is given an array of characters. Compress the array into a string using the following rules. For each group of consecutively repeating characters:

  • If the number of repeating characters is one, append the string with only this character.
  • If the number n of repeating characters x is greater than one, append the string with "x" + str(n).

Examples

compress(["a", "a", "b", "b", "c", "c", "c"]) ➞ "a2b2c3"

compress(["a"]) ➞ "a"

compress(["a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"]) ➞ "ab12"

compress(["a", "a", "a", "b", "b", "a", "a"]) ➞ "a3b2a2"

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.