Skip to content

Instantly share code, notes, and snippets.

@BillBarnhill
Created February 4, 2021 13:48
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 BillBarnhill/6471bdc118e06e8e3fea7747b83dde15 to your computer and use it in GitHub Desktop.
Save BillBarnhill/6471bdc118e06e8e3fea7747b83dde15 to your computer and use it in GitHub Desktop.
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