Created
February 4, 2021 13:48
-
-
Save BillBarnhill/6471bdc118e06e8e3fea7747b83dde15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f(sz): | |
assert sz, "Input cannot be empty/falsy" | |
assert "__getitem__" in dir(sz), "Input must be subscribtable" | |
result = [] | |
current = sz[0] | |
count = 0; | |
for c in sz: | |
if c == current: | |
count += 1 | |
else: | |
result.append((current, count)) | |
current = c | |
count = 1 | |
result.append((current, count)) | |
return result | |
if __name__ == "__main__": | |
input = "aaaabbbcca" | |
result = f(input) | |
expected = [("a",4), ("b",3), ("c",2), ("a",1)] | |
assert result == expected, "Should be {}".format(expected) | |
print(result) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment