Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
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 thensum()
it, and then throw the list away/ Also, just a tad bit easier to read without extra brackets in there :)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.
This comment is hidden because it contains spoiler information about the solution
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.
No need for
el
inelif
, or theelse
, since you're returning early. Could be, e.g.: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 rightsum()
(which refer to the right and the left halves ofarr
, respectively).This comment is hidden because it contains spoiler information about the solution