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.
Of course your set-based version is slow; you load the entire list into memory and then traverse it again. If you were to use a for loop to iterate over the input, parsing each input and testing set membership - or use a generator comprehension to parse the list and return the values - then you would only be traversing the list once and performing the necessary calculations in constant space.
Of course, using a generator would speed up either version, but the lack of a generator - the fact that the entire input is loaded, kept in memory and then traversed a second time - is the main reason I'm criticising the solution. (The fact that it's a one-off math trick that is entirely irrelevant to general set problems being the important secondary criticisum).
How much faster the generator solution will depend a lot on the size of the input and whether that is smaller than the basic filesystem/filecache block. So results will vary.
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 →
Of course your set-based version is slow; you load the entire list into memory and then traverse it again. If you were to use a for loop to iterate over the input, parsing each input and testing set membership - or use a generator comprehension to parse the list and return the values - then you would only be traversing the list once and performing the necessary calculations in constant space.
Of course, using a generator would speed up either version, but the lack of a generator - the fact that the entire input is loaded, kept in memory and then traversed a second time - is the main reason I'm criticising the solution. (The fact that it's a one-off math trick that is entirely irrelevant to general set problems being the important secondary criticisum).
How much faster the generator solution will depend a lot on the size of the input and whether that is smaller than the basic filesystem/filecache block. So results will vary.