49
Awesome Team
Philippe Cholet
https://github.com/Philippe-Cholet
Last seen 8 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.
I kinda like it. It would be a bit ineficient to iterate over an entire book and list all indexes but
indexes = (i for i, a in enumerate(text) if a == symbol)
next(indexes, None) # first or None
return next(indexes, None) # second or None
More
I like to refactor a bit so here: types and a few simplifications/tricks...
```python
from collections import defaultdict
from typing import Defaultdict, List, Tuple
# GraphCosts = defaultdict(lambda: defaultdict(int)) # Not a type but a dict.
GraphCosts = Defaultdict[str, Defaultdict[str, int]]
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
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
Or `x_min = min(map(int, xs), default=0)`.
You may not have python3.9 on your local machine but checkio has it and with it
from functools import cache
@cache # equivalent to "lru_cache(maxsize=None)"
...
But the documentation does not say if it has `cache_info` too. Will need to test
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
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
Great, I tried with graphlib a bit (I asked oduvan to support the graphlib module) but it did not feel great for the task. But it's nice to see this. And "match/case" seems useful here.
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
Note that `if len(result) > 0:` and `if result:` are equivalent for a list.
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
I could have explain the two last lines.
**line 16:** A flood zone starts at index i if the current altitude is found again, strictly after. If not then water can't be here. This assures index i is underwater. Since we enumerate possibles index, the first time altitudes.index don't raise a ValueErr
More
Nice treasure from the deprecated stdlib. Only looking at docs, I could not find this gem.
Curious, I looked the source code (with "inspect.getsource") and saw a comment "_This will die of IndexError when counter is too big_". So after some calls, I found out that it fails for 4000 (and bigger numb
More
You could use `float('inf')` or `from math import inf` for min_cost.
And global variable are tricky to use. You can define a function into another. Then you won't have to use a global variable.
def cheapest_flights():
min_cost = float('inf')
def cost_calc():
# cost_
More