Most of my projects are on Bitbucket at the moment but will be moved to GitHub soon due to the shutdown of mercurial support.
See: https://bitbucket.org/labscript_suite/
Location
Australia
Education
PhD (Physics), BSc Advanced with honours (first class honours in Physics, majors: physics, maths)
I suspect using a collections.Counter dict would be beneficial. If you convert the input list to one of those you can then just iterate over the keys and check if dict[dict[key]+1] > 0, and if yes, add dict[key] to a running summation.
The loop would be O(N) and dictionary lookup is typically O(1), which I think would be faster than your algorithm above.
The question is whether the initial conversion of input data is performant, which I don't know! But from a practical point of view, collections.Counter is good to know about!
Hey Phil, thank you for suggesting the collections.Counter approach. I am yet to check out that module. But, yes I did read it somewhere it helps in achieving efficiency.
Also, with these, I am purposefully avoiding using libraries. :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I suspect using a collections.Counter dict would be beneficial. If you convert the input list to one of those you can then just iterate over the keys and check if dict[dict[key]+1] > 0, and if yes, add dict[key] to a running summation.
The loop would be O(N) and dictionary lookup is typically O(1), which I think would be faster than your algorithm above.
The question is whether the initial conversion of input data is performant, which I don't know! But from a practical point of view, collections.Counter is good to know about!
Hey Phil, thank you for suggesting the collections.Counter approach. I am yet to check out that module. But, yes I did read it somewhere it helps in achieving efficiency.
Also, with these, I am purposefully avoiding using libraries. :)