49
Awesome Team
Philippe Cholet
https://github.com/Philippe-Cholet
Last seen 2 hours ago
Member for 6 years, 3 months, 11 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.
Do not overuse parentheses, while, return and ... are not functions:
def beginning_zeros(number: str) -> int:
i = 0
while number[i] == "0":
i += 1
if len(number) <= i:
break
return i
# It can even be
def beginning_z
More
Great use of takewhile function. Not the most obvious choice here.
More
So `checkio = np.linalg.det`. lambda is not always needed.
If you see other solutions with rounding methods, it's because the mission lately evolved.
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
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
* Use else when needed, we exit the function when we `return` something.
* `not ... == ...` is the same as `... != ...`.
* A number that does not finish with a zero does not seem to be a special case to me, but 0 does.
* You do not really use the index, but you want to iterate on the string in a rev
More
Ahah, we wrote the same code. They are minor changes obviously but it is the same.
More
@kudinov.feodor Well, a simple fix (keep separator in "name part"):
def _sort_by_ext_key(filename):
name, sep, ext = filename.rpartition('.')
# assert sep in ('', '.')
return (ext, name) if name else ('', sep + ext)
More
Looking `delete` and since you imported product from `itertools`, I thought about `itertools.compress` I never used before, but it is appropriate here with a little change: exchange True and False (and reverse masks), which then mean "keep" instead of "delete".
```python
from itertools import produ
More
As I did for "replace first", I add this fourth solution:
```python
# numpy.roll generalize our function.
import functools as fn
import numpy as np
replace_last = fn.partial(np.roll, shift=1)
```
More
From another solution I "simplified", I would like to add a fourth one:
```python
import functools as fn
import numpy as np
replace_first = fn.partial(np.roll, shift=-1)
```
More
About `[[]] * rows`, be careful.
>>> L = [[]] * 5
>>> L[0].append(1)
>>> L
[[1], [1], [1], [1], [1]]
# Same object five times.
More
No need to have sets. ranges are enough. Test if a number is in a range is fast too.
More
`if res[i] != True` then `res[i]` is already `False`. so lines 7 - 11 can be much shorter. Plus two pythonic changes:
def stones(pile, moves):
res = [True] + pile * [False]
for i in range(1, len(res)):
for j in moves:
if i - j > 0 and not res[i - j]:
More
def match(comp, test):
count = 0
# you do not use the index j but comp[j] and test[j]
for c, t in zip(comp, test):
if c == t:
count += 1
# "count == 2" is already True or False, return it
return count == 2
# or
def match
More
It's not 3rd party.
Plus
# You do not use the index i but a[i] and b[i].
def hamming(a, b):
return sum(s != t for s, t in zip(str(a), str(b)))
def is_doublet(numbers):
# something with zip too to avoid the use of "i".
More
Just in case you don't know
from collections import defaultdict
graph = defaultdict(list)
# then
graph[num1].append(num2)
# instead of
graph[num1] = graph.get(num1,[]) + [num2]
Or without collections module
graph.setdefault(num1, []).append(num2)
More
Sum comprehension is great.
def is_adjacent(i, j):
'''Return true if x and y differ by one digit, false otherwise'''
return sum(i_digit != j_digit
for i_digit, j_digit in zip(str(i), str(j)))
Great job!
More
`0 if m==n else 1` or just `m != n`. Booleans are integers 0 and 1, so we can sum them.
More
`min` has a default parameter that can be useful. And `len` is great function on its own.
min(variants, key=lambda x: len(x)) if variants else False
min(variants, key=len, default=False)
More