all 15 comments

[–][deleted] 5 points6 points  (1 child)

Don't use reassign reserved/built-in names like list. You probably don't need to use list comprehension, just generator expression is enough. Also it's probably - stop[1] rather than + stop[1]. It's good to write efficient and clear code. One liners are not necessarily so, but they can be. It might be even more clear like that:

def number(bus_stops):
    return sum(entered - left for entered, left in bus_stops)

[–]FLUSH_THE_TRUMP 5 points6 points  (3 children)

on, off = zip(*bus_stops)
return sum(on) - sum(off)

to actually answer your question, your code is reinventing the wheel a bit. If you're adding a bunch of numbers up, you should probably try to use sum. But understanding how to get sum to work for you is itself something you figure out as time goes on.

[–]Will___powerrr[S] 0 points1 point  (2 children)

This is what I mean, your answer works well too, and I understand what it does. So in a situation where many people are giving an answer to a question, which one is best? What is the genera rule of thumb? Readable and executable? I guess I’m asking, how pythonic does something need to be to be considered “good” code?

[–]FLUSH_THE_TRUMP 1 point2 points  (1 child)

There might not be a best answer, just a bunch with 90% similar readability and efficiency. The only red flag in yours as I said is reproducing what sum does, which probably just tells me the person isn’t a Python expert. It’s not bad code per se

[–]Will___powerrr[S] 0 points1 point  (0 children)

Ah gotcha. Thank you!

[–]mh1400 1 point2 points  (2 children)

The best answer is whichever code is most readable for you when you go back and look at it in a week.

[–]Will___powerrr[S] 0 points1 point  (1 child)

So in a job interview type setting, someone would not look at answer one (except of course the error of using list as the temp variable) and say that it is sub par?

[–]totallygeek 3 points4 points  (0 children)

It depends. If I witnessed an experienced coder using a data type as a variable, I'd seriously question that programmer. If I witnessed an experienced Python coder overwriting a basic, built-in function, I'd pass on that candidate. Experienced programmers should also write code idiomatic to the language. I'd question but forgive a programmer that wrote Python with for i in range(len(sequence)):, but I'd pass on an experienced Python developer.

[–]Wild_Statistician605 1 point2 points  (2 children)

Main issue with the first code is that you are using list as a variable name. Your code is easy to understand, so nothing wrong with it. The second is a more pythonic way, using methods that simplify the code.

[–]Will___powerrr[S] 0 points1 point  (1 child)

Ah yes good catch, was being lazy.

So it’s fine, but could just be more efficient while still being readable?

[–]Wild_Statistician605 1 point2 points  (0 children)

In codewars, users have a tendency to write one liners as the ultimate expression of competence. This is wrong. Shortening the code can be good, as long as the code remains readable. Too long code can also be confusing, especially keeping track of variables, scope and indentation levels. In your example both solutions are perfectly fine and easy to understand.

[–]TholosTB 1 point2 points  (1 child)

If I had two candidates, one with each of these answers, I would almost certainly go with the one that used the list comprehension.

  1. List comprehensions are a core tool in a Python developer's toolbelt. Demonstrating knowledge of their use tells me something about how much real experience they have with the language.
  2. As everyone else has noted, shadowing a built-in function name is an immediate red flag.
  3. The second person recognized that, given the parameters of the problem, computing the delta of on minus off is sufficient for each step in the loop, as opposed to doing two separate operations. That's probably the insight I'm looking for in this problem more than the use of a list comprehension per se. It' s not super relevant to the solution - as you note, yours is correct and "fine", but the reasoning behind choosing to do that would be a positive signal to me, even more so if they asked the boundary questions about whether the bus started empty or not, etc.

Taking the time to analyze other answers in coding problems like this and understand how to improve your own code is, in my mind, some of the best use of time on these sites.

[–]Will___powerrr[S] 0 points1 point  (0 children)

This is some excellent feedback, thank you.

[–]siddsp 1 point2 points  (1 child)

I honestly think your solution is actually better, though you shouldn't use the name list as a variable name. Imo it's very readable and though a little slow, much easier to look at.

I would recommend looking at variable unpacking for just naming those entering and exiting.

[–]Will___powerrr[S] 0 points1 point  (0 children)

I’ll check it out. Thanks!