Ad
  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Just a tip = no need to make the list-comprehension inside sum() - it takes any iterable, including generator expressions. Technically speaking, it's wasteful to create the entire list in memory and then sum() it, and then throw the list away/ Also, just a tad bit easier to read without extra brackets in there :)

  • Default User Avatar

    This is not best practice. For others learing Python code - don't do this. It works, and it's clever, and clever code is bane of every maintainer's existence.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Um, not so fast. There's no way its 7kb; in fact, there's no way it's more than 148 bytes. It may be slower, but it's not more wasteful space-wise. On the one hand, if it builds the dictionary new every time, that means it doesn't save it anywhere, so once it goes out of scope, you reclaim those 148 bytes (subject to when garbage collection runs, but that's not really the point - it'll run if it needs to). On the other hand - Python may very well save the dictionary when you create the function, similar to a const static in C - even though it's inside a function, it only gets initialized once.

    So now, the question is - does any other function need this mapping? If yes, then the mapping should be outside the function, so you don't duplicate effort. If no one else needs it though, then it belongs inside the function - this dictionary shouldn't be running around, possibly getting mutated, and cluttering up your namespace, simply because you think, maybe, it might not be the absolute most efficient thing, even though you you aren't quite sure what's happening under the hood. Premature optimization is the root of all evil, and I'd say pushing this dict outside the loop just because you think it's faster is not a clean design decision.

  • Default User Avatar

    No need for el in elif, or the else, since you're returning early. Could be, e.g.:

    ## first line as is
    return names[0]['name'] if names else ''
    
  • Default User Avatar

    I was trying to figure out how this one worked, since the array indexes seem off - but it's just adding the "middle" number (arr[i]) to both the left and the right sum() (which refer to the right and the left halves of arr, respectively).

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution