Practice

Problem 5 of 10

Problem · Medium · 20 min

Shorter text

  • Medium
  • Strings

A run is a group of the same character that stand side by side. In "aaaabbbcc" there are three runs: aaaa, bbb and cc.

Write compress(text). It writes each run as the character and the length of the run. "aaaabbbcc" becomes "a4b3c2".

  • If the result is not shorter than the original text, return the original text. "aabb" would become "a2b2", which has the same length, so return "aabb".
  • The text has no digits. It can be empty.

Examples

Call Result
compress('aaaabbbcc') 'a4b3c2'
compress('aabb') 'aabb'
compress('') ''

Tasks

2 to do

  1. Task 1, not checked yet. Make compress pass the example tests.

  2. Task 2, not checked yet. Make compress pass the hidden tests.

Task 1 of 2

Make compress pass the example tests.

All tasks

main.py

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.