49
Awesome Team
Philippe Cholet
https://github.com/Philippe-Cholet
Last seen 21 minutes ago
Member for 6 years, 2 months, 27 days
Difficulty Advanced
I was a math teacher in France for two years. I'm currently reconverting to IT, I'm here to improve my Python skills, and practice English.
You can write `int` instead of `lambda: int()`. A simple dict with "(int, int)" keys would be enough. And the returned value is surely equal to `dp[i][weight]`. I prefered using a list but defaultdict is also nice.
More
After I deobfuscated it, I would change `[...].pop()` for `next(...)` since it has a single element. And since we are in creative section, I would even add it is shorter by 2 chars. Or even shorter `[...][0]`, but I prefer next.
_Next_ deobfuscated version:
from calendar import Calendar
fr
More
I did not know about this log determinant, thanks. But I have to say that `_` is usually used for variables we don't use, such as
for _ in range(5):
print('Hi!')
time.sleep(1)
Here I had to see the doc to know it was the sign of the det. I could have guessed but I did not, the
More
Great use of takewhile function. Not the most obvious choice here.
More
I was just curious about "pure code" in the title. But so much to tell about this creative code.
"not len(l)==0" is equivalent to "l"
"1%len(l) == (1 if len(l) > 1 else 0)" since len(l) > 0 but "1" is enough
after that it becomes "l[1:]+l[:1] if l else l"
but "l[1:]+l[:1]" is enough
More
while sum(answer) < N: # sum(answer if answer else [0])
len(list(filter(None, answer)))
# It count the number of true elements so equivalent to
sum(map(bool, answer))
More
`[crossword[i] for i in (0, 2, 4)] == crossword[::2]`
More
Great, I think I did not thought of `title` method. But to make it a bit simplier:
def to_camel_case(name):
names = name.replace('_', ' ')
return names.title().replace(' ', '') # delete spaces
`join` is not the only useful method 😉
More
You do not need to make a list, args is a tuple (which is "like" an immutable list).
# example: checkio(1, 2, 3)
# args = (1, 2, 3) (type tuple)
# your list: [1, 2, 3] (type list)
# there are not the same but they behave the same in this situation.
def checkio(*args):
More
while ...:
for pair in '()', '[]', '{}':
sstr = sstr.replace(pair, '')
return not sstr
More
Well it is clearly nicely short. After looking tests, which seem to be strong enough, I must admit it has to be right.
Then I thought about it... And while I easily admit than every line has to be in a "perimeter" in one way or the other, I have hard time understanding why it is sufficient to have
More
First, it is great to not run so algorithm on the tree for each pair.
It's nearly perfect.
The only thing I don't like here is about `index_prefix`.
We can for example have `str(10)==str(1)+str(0)` hence have the same `index_prefix` without being on the same path.
The easiest fix would be to have
More
`sorted(list(map(lambda x: (abs(x-one), x), values)))[0][1]` is equivalent to...
# You don't need to sort the entire thing to only get the minimal element:
# sorted(...)[0] == min(...)
min(list(map(lambda x: (abs(x-one), x), values)))[1]
# No need to give a list to sorted/min/max s
More
The `+[1]` is because `structure` can be empty sometimes, but note that `max` function has an argument for such cases: "default".
max([1 + how_deep(el) for el in structure], default=1)
Or without "for loop".
1 + max(map(how_deep, structure), default=0)
More
You asked me why no-one seems to use `re.Scanner` for this. Well personally I did not know it, I probably looked at it someday and forgot about it (or not after writing this message).
I just went to the documentation and type `help(re.Scanner)` and I still don't know what it does exactly. Looking a
More
Do you know about `map`? I like the key argument and list comprehensions but sometimes, `map` can be quite useful:
len(max(rows, key = len))
# or
max(map(len, rows))
["".join(item) for item in zip(*rows)]
# or
map("".join, zip(*rows))
# could be list(map...) to be reall
More
Great curry and good comment-explanations, as always. I particularly like `blah.__get__(4)` replacing `partial(blah, 4)`, it's a neat trick.
More
_, size, split = distance[0] # smallest item without popping it
split += 1
heapreplace(distance, (split/size, size, split)) # faster than heapop followed by heappush
It's still `O(log n)` but the factor is a bit lower.
After testing this change on all tests 10000
More
The sequencer part is a bit weird, you don't need it. `triangular_numbers` can be a generator function itself.
That's why I wrote this, what comes next is more optional.
Second, insert an element at the beginning of the list suggest you instead need a queue (or we could append to a list, and rever
More
`True if boolean_expr else False` is simply equivalent to `boolean_expr`.
`False not in [boolean_expr for ... in ...]` is simply equivalent to `all(boolean_expr for ... in ...)`
More