38
Awesome Team
Jürgen Strobel
Last seen 22 hours ago
Member for 5 years, 1 month, 11 days
Difficulty Normal
That's a nice showcase of itertools (although I prefer importing the functions directly). I especially like how `avoidable` works.
It's not very efficient though, `filter(satisfied, ...)` checks whole boards created by `product(*places)`. A lot of the search space could be pruned if placing queens
More
Yeah that's the right way to do it. I realized too late that *everything* is to be lowercased except the escapes, and that there is no reason to parse and handle the different parts of the URL separately.
Of course, if the path would countain `:80` followed by a non word character this would break,
More
`decimal` can be used to do quantization, rounding and formatting of the result too.
Funnily I didn't use it to find the right power as it can be done with integer math easily.
More
The search is much better structured now, but I'd still prefer doing it in integer arithmetic.
if number // base ** i ...
or even
if number >= base ** i or not i:
Yes I may need to repeat the last calculation in Decimal and can't show off a walrus, but given int operations are **much**
More
In retrospect, the pattern could be simplified to `rf'({_l}{_u})|({_u}{_u}{_l})'`.
More
I am wondering if binary search is strictly optimal, or an approximate solution (polynomial) could be used to guide the search ;-)
More
This theoretically fast algorithm avoids a lot of repeated additions and multiplications by intertwining creating permutations and their inner products with `coeffs`.
But in practice eschewing itertools.permutations or numpy does not pay off well, compare to [this](https://py.checkio.org/mission/cr
More
Nice use of setdefault(), but there's a collections.Counter class would make this even more elegant.
Also there's a way to avoid the sorting step, max (and min) can take a key= parameter and compare by 2 properties in a tuple at once, one being the count, the other the character itself, and one mul
More
It's also O(n) in space though, and you'd just need to keep the last 2 elements of `values` while iterating, making it O(1) in space.
More
OK let me be blunt: The len()==1 case shouldn't be necessary even for your code, as the next case would handle it just fine. Also you could just pick the middle 1 or 2 list elements by indexing [len(data)//2)] instead of removing everything else. I don't see how i is used, it's completely superfluou
More
In Battle if an army is empty this will end with an UnboundLocalError since soldier_1_alive is never assigned to. No precondition to assure armies have units.
More
Index-based iteration in Battle.fight is non-pythonic (some might even say clumsy). Better options are explicit iterators + next() or popping the dead units off the armies.
Btw catching the IndexError doesn't save you if army u1 is empty, since u1.members[-1] will throw another one.
More
I don't see how the key lambda can work? In the first example both 6 and 2 get the same frequency hence the output should be in order of input: [4, 4, 4, 4, 6, 2, 2, 6]. That was my first attempt too but it got rejected.
More
Works, but rather brute force. Can be done in a single loop by adjusting a sliding window's sum up and down.
More
Works, but rather brute force. Can be done in a single loop by adjusting a sliding window’s sum up and down.
More