Skip to content

Instantly share code, notes, and snippets.

@isbm
Last active August 8, 2016 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isbm/4a45610936a1ceb034f339b93db5bf2a to your computer and use it in GitHub Desktop.
Save isbm/4a45610936a1ceb034f339b93db5bf2a to your computer and use it in GitHub Desktop.
"aaaaabbbbccccccaaaaaaa" to "5a4b6c7a"
Gustavo Moreira Freitas writes (https://www.linkedin.com/groups/25827/25827-6166706414627627011):
------------------------------------------
A python test for an interview #1.
Today i did a simple test that you give a string to a function,
and then the return output should count the letters sequencially;
as following example:
in: "aaaaabbbbccccccaaaaaaa" => out: "5a4b6c7a"
I confess that i did not complete the question on the desired time,
however i wrote this following code that answer the question.
------------------------------------------
@isbm
Copy link
Author

isbm commented Aug 8, 2016

request = "5a4b6c7a"
data = "aaaaabbbbccccccaaaaaaa"

def shrink(data):
    last_item = None
    cnt = 1
    out = []
    for item in data:
        if last_item != item:
            if last_item:
                out.append("{cnt}{char}".format(char=last_item, cnt=cnt))
            last_item = item
            cnt = 1
        else:
            cnt += 1
    out.append("{cnt}{char}".format(char=last_item, cnt=cnt))
    return ''.join(out)

assert shrink(data) == request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment