49
Awesome Team
Philippe Cholet
https://github.com/Philippe-Cholet
Last seen 4 hours ago
Member for 6 years, 2 months, 19 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.
I started timeit random tests before I see you already did it. But I test the current ten published functions (sometimes a bit changed).
**1000 sentences from 1 to 100 words from 3 to 13 letters, tested 10000 times.**
Functions are sorted in order to have decreasing performances.
from timeit
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
@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
Just so you know, instead of `num.insert(0, rem)`, you could have done `num.append(rem)`. The elements of `num` would be reversed, but it would not change the final result. That and `append` is contant time (`O(1)` complexity, it just add the element at the end) while `num.insert(0, ...)` does `len(
More
`range(len(items)-1)` is enough because `range(0)` and `range(-1)` are both empty.
More
An eventually dead unit_1 still wrongfully hits unit_2 line 21. Lucky it passed tests.
More
Others units than Warrior are subclasses of Warrior, so you don't have to copy is_alive property each time, only one time in the Warrior class.
If you think Army class like a list, consider (It wasn't my first idea but it's realy great)
class Army(list):
#Each instance of Army is a lis
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
Reduce is useful to apply a function to a whole sequence so
mult_two = lambda *args: reduce(mul, args)
# is even more powerful without difficulty.
mult_two(15, 6, 1991)
mult_two(*range(1,6))
More
I didn't know about `defaultdict(lambda: defaultdict(type))`, thanks.
I thought about `defaultdict(defaultdict(type))` but that was wrong.
More
For info, `next_enemy[int(not i)]` and `next_enemy[not i]` are equivalent.
We have
list[True] == list[1] and list[False] == list[0]
It's because we have
True == 1 and False == 0
More
I did use yield from too but I prefered `yield from reversed(run)` to slicing making a copy. I quibble a little. =)
More
Or a more "complex" unpacking instead of the whole try/except block:
value, weight, limit, *_ = item + (total_weight,)
# there is also this below but I don't like it.
value, weight, limit = (item + (total_weight,))[:3]
More
I restarted this serie with your forum post. I thought about dataclass for weapons, not for warrior classes, it's interresting.
More
We are all (except you) reinventing the wheel when sympy (and you) does it naturally. **+5**
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
`lambda x: str.strip(x)` is the same as `str.strip`, just more complicated.
Comments this way seems unusual to me: `# Currenlty, only ``int`` is considered.` for example.
More
Great job with numpy! Even if I prefer my way with complex numbers. `cmath` module could do a part of the job for you. And there is probably a way to do graham stuff with complexes.
`a[randint(0, len(a) - 1)]` or `choice(a)` (from random too).
`del sorted_pts[...]`, not `sorted_pts.pop(...)` ?
More
`roots` could be a list instead of a dict. More importantly, since you only need the last element in roots, root could be the last element.
roots = [(1, w_count, b_count)]
for _ in range(1, step + 1): # don't need n variable anymore
nlist = []
for root in ro
More
for subtree in tree[1]:
if result := find_node(subtree, node): # python 3.8+
return result
# No need to return None, it returns None when it does not encounter any return statement.
You call `find_node` a lot. Hopefully I did not provide too big trees in random tests.
More