We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Firstly, because my idea isn't based on a maths trick, there's no need to parse the strings into numbers - you can just add them to the sets as they are. The integer parsing actually takes up a significant amount of the total effort; removing it brings even your implementation of my idea close to the speed of the maths trick.
Secondly, if - having removed the unnecessary integer parsing - you just change
then the set-based solution becomes faster than the math trick, while also having all the other virtues mentioned (like not needing to load the entire data set into memory at once).
It's surpising how many of the challenges on this site expect you to parse strings into numbers when it isn't actually necessary. Realising that has let me solve a number of the challenges in Haskell when it might otherwise have been difficult. In the case of this challenge, it helps deal with the problem that Python code is much slower than the built-in and core library functions (which are typically written in C or Java, depending upon the implementation used).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Captain's Room
You are viewing a single comment's thread. Return to all comments →
You're wrong.
Firstly, because my idea isn't based on a maths trick, there's no need to parse the strings into numbers - you can just add them to the sets as they are. The integer parsing actually takes up a significant amount of the total effort; removing it brings even your implementation of my idea close to the speed of the maths trick.
Secondly, if - having removed the unnecessary integer parsing - you just change
to
then the set-based solution becomes faster than the math trick, while also having all the other virtues mentioned (like not needing to load the entire data set into memory at once).
It's surpising how many of the challenges on this site expect you to parse strings into numbers when it isn't actually necessary. Realising that has let me solve a number of the challenges in Haskell when it might otherwise have been difficult. In the case of this challenge, it helps deal with the problem that Python code is much slower than the built-in and core library functions (which are typically written in C or Java, depending upon the implementation used).