Given a string of a million numbers, return all repeating 3 digit numbers

前端 未结 13 1547
误落风尘
误落风尘 2020-12-22 15:41

I had an interview with a hedge fund company in New York a few months ago and unfortunately, I did not get the internship offer as a data/software engineer. (They also asked

13条回答
  •  难免孤独
    2020-12-22 16:16

    Here is my solution:

    from collections import defaultdict
    string = "103264685134845354863"
    d = defaultdict(int)
    for elt in range(len(string)-2):
        d[string[elt:elt+3]] += 1
    d = {key: d[key] for key in d.keys() if d[key] > 1}
    

    With a bit of creativity in for loop(and additional lookup list with True/False/None for example) you should be able to get rid of last line, as you only want to create keys in dict that we visited once up to that point. Hope it helps :)

提交回复
热议问题