Reputation: 1317
My code produces satisfied result in hacker rank compiler window for eleven out of fifteen test input in a certain problem. However, when i make subtle change in my code, all the the test inputs are run as OK.
This is a snippet of my previous code which shows error (terminated due to timeout).
for ind, letter in enumerate(string):
if letter in vowels:
kevin += len(string[ind:])
else:
stuart += len(string[ind:])
When i changed the above code as below all inputs are run successfully.
for ind, letter in enumerate(string):
if letter in vowels:
kevin += len(string) - ind
else:
stuart += len(string) - ind
Aren't these two codes equivalent?
Upvotes: 2
Views: 600
Reputation: 26941
These code snippets are entirely equivalent apart from two things:
string
.__len__
and __iter__
.As you've added the error, saying it's a timeout error, I'm leaning towards issue #1 which is the string creation.
If you're creating a subset of a very long string you do the following operations for each iteration:
calloc()
)For long strings, this whole sequence can be a heavy operation, especially done for every iteration.
Second algortithm does this:
Upvotes: 2