Oh, I see. Thank you.
The custom in this site for the easier katas, is not to be restricted by performance and memory constraints. At most katas leveled kyu 8 to kyu 6, almost all algorithms will work. They are meant to be easy training, with fewer and smaller test cases - so just do something that works and you'll pass. The harder katas - around kyu 5,4 and up may require "performance" - i.e. slower algos may fail etc. Browse "Kata" on the top left menu to get to harder questions...
Did anyone check the complexity of this solution?
It's very short and nice, but is it optimal in terms of CPU/RAM usage?
As I can see we will get one list traversal for getting a set. Then we will get a traversal per each element in the set. Which i think is suboptimal.
This comment is hidden because it contains spoiler information about the solution
why I don't know '.count()'
Thanks a lot for your generic solution, which is really really impressive.
I learned a lot from it. And for the sake of my understanding, I added some comments to your source code.
yes, that's it. For instance, here is what it could look like, having a generic solution.
Thank you for your reply.
Are you sure that '3' comes from the number of arguments?
Well, I think you might've meant the number of parameters except 'x', which is also the length of
the list containing a, b, c, etc., right?
And I should've said, "Before replacing '3' with 'len(l)', you have to manually type in as many parameters as you want."
that won't be more generic since the 3 comes from the number of arguments. ;p
And if you make it more genrice with any number of values, this will become especially inefficient because of index.
You also inspired me!
To make it more generic, we can replace '3' with len(l).
You have a good point there, indeed.
I would measure it against a simple if index is even .. else loop, but that's probably not any faster. Thanks for the heads up!
if index is even .. else
IIRC, a slice has a time complexity of O(k) (with k == the number of items). So, going over two halves of the array are roughly equivalent to going over the array once. I imagine there is a bit of overhead for the stepping, but I doubt it's much. I'm really not sure how you'd optimize this code in pure python.
Not really best practice, because you traverse the array twice, but at least it's clear :-)