Created
February 5, 2021 16:46
-
-
Save willtownes/30af9b333d3df174883638f5067af77a to your computer and use it in GitHub Desktop.
Grigorev twitter interview question
This file contains hidden or 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
""" | |
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