Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jehugaleahsa
Created August 9, 2016 15:08
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 jehugaleahsa/75998ae61ad0df86d11fa23fa4c77593 to your computer and use it in GitHub Desktop.
Save jehugaleahsa/75998ae61ad0df86d11fa23fa4c77593 to your computer and use it in GitHub Desktop.
Python - Count Occurrences
def getSegments(input):
start = 0
length = len(input)
while start != length:
current = input[start]
end = next((i for i in range(start, length) if input[i] != current), length)
count = end - start
yield (count, current)
start = end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment