49
Awesome Team
Philippe Cholet
https://github.com/Philippe-Cholet
Last seen 7 hours 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
You do not really use `i` but `number_str[i]` so
for digit in number_str:
list_num.append(int(digit))
Second, list comprehension is great
list_num = [int(digit) for digit in number_str]
Third, or just
return max(int(digit) for digit in str(number))
More
* the second return is useless.
* you could list words on the sentence, then join them with spaces
def morse_decoder(code):
word_list = code.split(' ')
words = []
for x in word_list:
chars = [MORSE.get(z) for z in x.split(' ')]
# or MORSE[z
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
while ...:
for pair in '()', '[]', '{}':
sstr = sstr.replace(pair, '')
return not sstr
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
@ogoro Just after looking your recent publication for this mission, I've dug up my [old solution](https://py.checkio.org/mission/box-probability/publications/Phil15/python-3/markov-chains/) and improved it.
It's still "good" math but I found "spdiags" that handles matrix diagonals the right way. I
More
With _lancers_ who can attack _two units_, I felt the need to _start over_, looking at future missions, to have a more global view of these missions.
1) Battle.fight method and management of the dead warriors (Army properties) are completely reviewed to allow lancers to hit a second unit.
2) Differ
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
`replace_all('qwerty', '.', '')` would return `''` because `.` as a regex match any character.
There is sadly no test about this.
To avoid that (do we want to avoid that?), `re.escape` would be useful (but then why use "re" at all?).
def replace_all(mainText: str, target: str, repl: str) -> s
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
To answer your comment on my solution, I agree. Liked solutions are not necessarily the best ones. But I give you a boost.
Just for fun, I considered making it shorter with this, not necessarily better:
from collections import Counter
from itertools import combinations, starmap
line_c
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
`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
Note that `if len(result) > 0:` and `if result:` are equivalent for a list.
More