Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Write function which takes string AAAAaaBCCCDDe as argument and returns its
- compressed version A4a2B1C3D2e1
- """
- def string_compressor(string):
- compresssed = ""
- count = 1
- # Add in the very first character
- compresssed += string[0]
- # Iterate, skipping the last one
- for i in range(len(string) - 1):
- if string[i] == string[i + 1]:
- count += 1
- else:
- compresssed += str(count)
- compresssed += string[i + 1]
- count = 1
- compresssed += str(count)
- return compresssed
- def test_string_compressor():
- test_cases = [
- ("AAAAaaBCCCDDe", "A4a2B1C3D2e1"),
- ("AAAaBCCCDDe", "A3a1B1C3D2e1"),
- ]
- for (arg, answer) in test_cases:
- result = string_compressor(arg)
- print("Running Test with data:", arg, "gave = ", result)
- if result != answer:
- print("\x1b[0;30;41m" + "Test with data:", arg, "failed" + "\x1b[0m")
- else:
- print("\x1b[6;30;42m" + "Test with data:", arg, "passed" + "\x1b[0m")
- test_string_compressor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement