57
Awesome Team
Vedran Čačić
https://web.math.hr/~veky
Last seen 17 hours ago
Member for 11 years, 6 months, 6 days
Difficulty Advanced
We shall not cease from exploration, and the end of all our exploring will be to arrive where we started and know the place for the first time.
roundoff = lambda x: '{:.{}f}'.format(x, decimals) if decimals else str(int(x))
return roundoff(number / base**i) + p + suffix
Same length, clearer IMO.
More
That empty line 5 is not really needed, or at least it should be less than empty space in line 14 (which would have to be 2 empty lines in that case:), and of course the context of lines 1, 2 and 15 is also not needed. But line 5 could be filled with a docstring. ;-)
Also, Python's `not` operator i
More
Of course, you can just
return [x for x in ...]
But in this CiO context, giving a return value a semantic name is even justified. Though of course, not in the real code - then the _function name_ should be used for that purpose.
More
Too many indices! :-o But this is minor.
Real problem is using 10 and 100 as magic numbers. Besides being
maintainability hell (network expands, and in some random moment in the
future, your code gives wrong results silently), documentation is lacking.
Comments do help at the moment of assignm
More
First, precondition says that all elements of numbers_array are ints. So `int(` in line 4 is unnecessary.
Second, you then have for your key
lambda x: abs(x)
What's that function? Takes x and gives abs(x). I know a much simpler function that does exactly the same. Of course,
key=abs
Als
More
This is a great example of OOP vs ADT dichotomy (see http://www.cs.utexas.edu/users/wcook/papers/OOPvsADT/CookOOPvsADT90.pdf). If you put everything in an object, it looks like above. But if you use smart data types, you see that reached and infected are just sets, index is id of node, security_leve
More
Was it really easier to build that str for eval, than to build the product itself? *= is as complicated as +=. :-)
More
Nice. However:
len([y for y in blah if cond]) ~~~> any(cond for y in blah)
is much clearer and it shortcircuits, while len-version doesn't. But even better IMO would be if connected were a _function_ taking an index and returning a set. (It could be local to `capture`, so it could access
More
Nice and explicit, but you might want to learn about "set" type, and & operator on them.
More
You do realize that you don't need max_count at all? As soon as count reaches 3, you can return True.
More
You're repeating len(data) // 2 too much.
half, odd = divmod(len(data), 2)
if odd:
... data[half]
else:
... (data[half] + data[half-1]) / 2
Also, `...` should be direct returns. This is not Pascal, you don't have to single-exit your functions. As soon as you find the re
More
Usage of Counter is cool, but still you're naming your return value what function should have been named. Just
return [x for x in data if count[x] > 1]
Also, a controversial point: recently, Python style seems to be moving towards precise imports (with namespace packages, blurring the line bet
More
Nice, but wasn't adding +"!" simpler than replacing "n"?
Also, drop the [] inside join().
More
Nice idea, but there are many stab-in-the-eye problems with it. It can be polished much further. For example: reusing builtin names like len and iter. Not incorporating arg (and that's a horrible name!) into iter. Writing trivial genexps such as "a for a in blah" instead of just blah. Not using some
More
Line 5 is just "if not array:" (or reverse the condition). Otherwise, nice command of slices and negative indices. ;-)
More
OK, you win. :-) I thought int would refuse to take string starting with "00" since Guido shunned old octal notation, but no, it works fine. BTW it's really sad that len(bin(b)), even with that //4 afterwards, is not longer than b.bit_length(). :-/
More
You can use if...else expression. return -1 if len(array) <= n else array[n]**n.
Also, (binary) pow can be written as an operator **.
Otherwise, fine.
More
Wow, look, LukeSolo wrote a for loop! Of course, you wouldn't be you if you didn't squeeze a generator inside. :-P
for a in (b for c in d):
smth
is more clearly expressed as:
for c in d:
a = b
smth
One additional assignment is worth it. :-)
Also, for upper limit
More
Ok, not only does single-exiting make your code convoluted, but you have to invent new names, even if you really are not inspired. :-P (Pascal at least helps you by naming the return value same as your function.:) Just return it when you get it, no need to name it. And especially no need to name it
More
a > b - 1 should probably be a >= b. Easier to understand what you're comparing. Or you could rely on list's own way to check index and just catch IndexError. :-)
More