Ad
  • Default User Avatar

    You can have 10 computational operations in 1 line or 10 lines, it's still 10 operations for the compiler and the transistors. Fewer lines ≠ better efficiency.

    Industry standard is generally "good code is code that your mom can read and get an idea how it works without squinting too hard." Code really is for humans to read, even if there were tiny inefficiencies in one implementation over another, it's usually still best to go for the implementation people will have an easier time understanding. That's why recursion is rarely accepted in real-world code reviews: it usually makes more sense as a loop.

  • Default User Avatar

    I wasn’t familiar with DFS algorithms, until now :) I'm fairly algo ignorant still, but a little less so because of you. Thanks for taking the time to put the logic into words so well.

  • Default User Avatar

    I noticed that! First thing I did was check this person's profile, and I was happy to see it banned. I'm glad the mods take it seriously :)

  • Default User Avatar

    I didn't realize you could just cheat at Codewars lol.

  • Default User Avatar

    Some actual variable names or comments would help people understand what's happening here and why.

  • Default User Avatar

    Python lets you perform multiplication on lists! That's what's being demonstrated here.

    [0] * 5 equals [0, 0, 0, 0, 0]

    [1, 2, 3] * 3 equals [1, 2, 3, 1, 2, 3, 1, 2, 3]

    ([very]*2) + [big] equals [very, very, big]

  • Default User Avatar

    Don't be discouraged! These one-liner solutions get upvoted because they show off a lot of cool stuff Python can do, but it's a terrible way to code (unless you're practicing and want to get intimately familiar with every neat trick).

    Coding is about documenting logical steps that are human-readable, hopefully with comments that provide more context for the programmers maintaining it. If these were instructions for a Lego set, this would be the equivalent of "Step 1: put all the Legos together."

  • Default User Avatar

    Why

    while True
      if p0 < p:
    

    and not just while p0 < p?

  • Default User Avatar

    OK, so, Python one-liners: I see these tend to get a lot of attention on this site. I think they're a great example of "just because you can, doesn't mean you should."

    Now, imagine you're talking to people. Would you rather try to look smart with big words that are fancier than necessary, or would you rather people have the best chance of actually understanding what you're saying to them?

    Believe it or not, code isn't there for computers. It's for people. Code is for talking to people, folks.

    The computer doesn't care if the code you write takes up one line or 100; the whole job of the interpreter/compiler is to boil the code's fundamental logic all the way down for you. You can write the same logic in a dozen different ways, and the interpreter/compiler knows how to reduce it to the same thing at the machine level. So why write code one way over another way?

    Code is a tool, a tool that helps us spell out the logic of whatever we're trying to do in a way that makes sense to humans. Code works well because human brains can work well with groups of visible steps.

    So then, what's the point of stripping away all the visible steps from your code blocks and smushing 10+ logical steps into one line? Is that heping the computer? No, in most practical cases I think the computer couldn't care less. Is it helping the humans? This varies from person to person of course, but I say definitely not.

    Don't get me wrong, it can be good for testing your knowledge of the language you're working with (or just for showing off). But I really don't think it's practical to write code this way just because Python lets it happen and people are fascinated by neat tricks that get upvoted.