Skip to content

Instantly share code, notes, and snippets.

@willtownes
Created February 5, 2021 16:46
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 willtownes/30af9b333d3df174883638f5067af77a to your computer and use it in GitHub Desktop.
Save willtownes/30af9b333d3df174883638f5067af77a to your computer and use it in GitHub Desktop.
Grigorev twitter interview question
"""
From https://twitter.com/Al_Grigor/status/1357028887209902088
Most candidates cannot solve this interview problem:
* Input: "aaaabbbcca"
* Output: [("a", 4), ("b", 3), ("c", 2), ("a", 1)]
Write a function that converts the input to the output
I ask it in the screening interview and give it 25 minutes
How would you solve it?
"""
def rle(x):
res = [[x[0],1]]
for i in range(1,len(x)):
if x[i] == x[i-1]:
res[-1][1]+=1
else:
res.append([x[i],1])
return [tuple(j) for j in res]
if __name__=="__main__":
print(rle("aaaabbbcca"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment