• This is the py2 habit. Py3 code would be more readable by simply using '/' (without the casting).
    Some nasty mistakes I have made though when I forgot to use casting in C++ division.

  • Copy/paste from Details )

  • Correct– .sort() works inplace. While that reduces memory overhead, that is bad practice for most functions because of Python's implicit pass-by-assignment mechanic. The function would have the side-effect of sorting the original list, which might or might not cause a very difficult to find bug later.

  • AFAIK .sort() is method for lists and it doesnt return anything.

  • Ha! Seeing this it's suddenly so simple, I went on a bit of a diversion, oops!

  • that doesn't change anything.

    Going once through the array or twice saying one is best than the other is like saying that N*2 is worse/better than 2*N. Both are just the same, about time complexity. And for instance, this solution is more efficient beacuse of python's interpreter when it comes to compare actual perfs.

  • Hi. it is true. do you want me to modify the kata solution?

  • This is nice and consise, but goes over the seq twice.

  • +1 @PomPolock, I wrote the lambda until I realized I had to list() the filter object. Then re-wrote as a list comprehension. Surprised that the testing structure doesn't let you check for the method used (see https://www.codewars.com/kata/59441520102eaa25260000bf).

  • Yeah just for the purpose of the kata since that is the direct equivalent JS's Array Filter function. It's good to understand the lambda syntax for usage with map and reduce, as well as other more involved filter functions (where it wouldn't be as appropriate to use the "x for x" expression). Objectively though, neither solution is better than the other if the only goal is to return an array with only even integers.

    It's kind of similar in a way to the dynamic programming problems with the fibonacci or factorial sequence where you utilize recursion. For the purpose of writing a function that generates those sequences, recursion isn't necessary but it makes for good practice to start developing an understanding of it.

    For many complex programming topics, it's usually easier to understand how they work in a simple problem that may not necessarily require their usage. But learning how they work on the simple problems will make the more complex problems that require them more easy to understand.

  • Ah, so you're trying to say thah it would have been better to use filter with a lambda function other than a conditional like this ?

  • While this solution effectively works the same and is definitely a correct answer to the problem, I would say that it somewhat defeats the purpose of the kata. Due to the nature of python, it is not reasonably possible to limit syntax usage in manner that you're able to in JS, especially for this problem. Perhaps I might have been able to have the function return the filter expression instead of the filtered array itself, but that might have been overkill for a white kata.

    This is not at all a wrong way of "filtering" any array using a conditional, but doesn't do anything to teach the usage of lambda expressions in python. In most practical environments you would probably use the "element for element in iterable" expressions, but it is useful to understand how lambda expressions work.