avatar Bite 62. Data structures matter - speed up your Python code

In this Bite we provide you with 5 functions which you have to try to make faster. Some require different data structures or tactics. Knowing about these techniques goes a long way specially when your data sets grow and performance becomes an issue.

Notice that:

  • you need to complete the 5 functions containing _fast
  • the timing decorator can be ignored, the tests use this to time each function
  • we added some type hinting to help you, but some functions might require a different return type (so change it or get rid of the typing)
  • bisect would be a candidate for contains_fast but we think set (or dict) is the best way so this is the only function with a different input argument type for the fast equivalent (type casting was too expensive)
  • The tests ensure that the fast functions A. return the same result, B. are indeed faster

For some this may be easy, for others this might be an eye opener, regardless have fun and keep calm and code in Python!

Login and get coding
go back Intermediate level
Bitecoin 3X

192 out of 196 users completed this Bite.
Will you be Pythonista #193 to crack this Bite?
Resolution time: ~40 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 3.7 on a 1-10 difficulty scale.
» You can do it! 😌

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help