im so stupid
Why not? This is the first thing which came to my mind when reading the question.
there is no way this is the most common solution....i suspect some dirty cheaters in the kata
now this is just awesome how elegent this looks
What's the point in talking about speed if the inputs are so small that any approach will work in a fraction of a second? How about testing it with 10kk elements which yields actually useful results?
Generator - 998.33ms
List comprehension - 851.93ms
Loop - 1295.81ms
Thank you for the debate @mauro-1!
The test cases for this kata do not include a list of 1000 numbers. The most robust case (the 6th one) specifies an array of up to 120 numbers, summed 40 times. Under the specified test case, the difference in initialization time matters (although not by much).
Here are some results using the 6th test case exactly (python 3.8.5);
GenComp time: 0.0066030025482177734
ListComp time: 0.004992961883544922
loop time: 0.004717111587524414
These results are a bit misleading. Over several trials, each time fluctuates, and the loop time is usually, but not always the fastest.
IMHO the readability of comprehension vs loop depends on which I tend to be using the most at the time.
For loop is not faster than comprehension. For loop initialization is faster than comprehension initialization, and with 3 elements you measured that.
With a list of 1000 numbers:
3.40 set comprehension (not applicable to this kata)
3.48 list comprehension
3.80 generator comprehension
3.91 for loop
4.34 tuple comprehension
IMHO sum/comprehension is much easier to read, it's immediately clear that it's a sum and what is summed.
This comment is hidden because it contains spoiler information about the solution
This is not list comprehension, but rather, it's a generator expression. Rather than creating a list like list comprehension does, a generator expression returns an iterator object, meaning that it returns each result one at a time. The only difference is that the generator expression doesn't keep all the values, so it uses less memory. Not important for small programs like this one, but I thought is was worth mentioning.
The "similar code variations" tester is not very good. On a lot of katas, there is code that is marked as different just because the variable names are different. This results in LOTS of duplicate solutions with just slight variations that the variations tester does not catch. For example, in my solution, I used i as a variable name instead of x and it's in a totally different solution group... Not good for learners who want to see a list of distinct solutions. Not this kata's problem, a problem with codewars in general.
This is awesome. I'm pretty new and was struggling with this problem for a minute. I liked my answer but it's always so cool to see things put so succinctly! I feel like I learn just as much from practicing my own kata as I do from studying how other people achieve theirs.
f-strings have only been around since pyton 3
why not just use an f-string?
Excelent explanation. Thank you so much!
it's a generator expression. Instead of creating a list of all elements it creates an iterable object that returns a single element at a time, which sum() can still operate on. You could also add square brackets and it should still work (because both lists and generator objects are iterable) but will use more memory.