40
Last seen 4 days ago
Member for 9 years, 10 months, 19 days
Difficulty Advanced
Hi, the built-in [__set__](https://docs.python.org/3.5/library/stdtypes.html?highlight=str.count#set-types-set-frozenset) object is better for this purpose than __list__. Look at [_set.intersection()_](https://docs.python.org/3.5/library/stdtypes.html?highlight=str.count#set.intersection).
More
Hi, here's a bit shorter alternative:
```python
import string
def checkio(str_number, radix):
d = dict(zip(string.digits + string.ascii_uppercase, range(36)))
decimals = [d[num] for num in str_number]
if radix <= max(decimals):
return -1
return sum(dec * radix ** (l
More
Hi, when you have the following pattern:
if condition:
continue
else:
do_something
is better to rewrite it like this:
if not condition:
do_something
e. g.:
if len(result) != 0:
del result[0]
Then have a look at [this](https://docs.python.org/3.5/l
More
Hi, `bin()` returns a __`str`__ so `str()` is redundant.
More
Hi, this can be made more compact with `filter`:
return "".join(filter(str.isupper, text))
Regards,
suic
More
Hi, you can shorten it using _any()_:
return any(a.endswith(b) for a, b in permutations(words_set, 2))
More
Hi, consider the fact, that __str__ is immutable in Python. And look at _filter()_ function or _list comprehensions_/_generator expressions_.
# E. g.:
return "".join(filter(str.isupper, text)) # That's it :)
More
Hi, why try...except? You can easily handle it by _if_ or _and_. E. g.:
if array:
...
else:
return 0
More
Hi, look at [extended slices](https://docs.python.org/2/whatsnew/2.3.html#extended-slices):
```python
array[::2]
# is the same as
[num for i, num in enumerate(array) if i % 2 == 0]
```
Regards,
suic
More
Hi,
using Pythonic way of iterating over an iterable `for element in seq:` in combination with `itertool` can make your code more compact and readable (see the example below).
Regards,
suic
```python
from itertools import combinations, chain
def build_sides(my_sides, dif):
forl = [sorted(c
More
Hi, look at documentation for _sorted()_. Sorted has _key_ argument, which is quite useful.
More
This is so funny :)
1. "all the magic" (including `itertools.groupby`) on one-line.
2. wrapped by a typical newbie `if...else...` anti-pattern.
```python
return (
subj.isupper() # bool
or subj.endswith('!!!') # bool
or any(...) # bool
)
More
Hi, you don't need the `result` variable as well as the `number` variable. Line 19 is redundant.
More
Hi, _bin()_ returns a __str__, which has _str.count()_ method:
return bin(number).count('1') # That's it :)
More
Hi, I just would mention, that [] can be used instead of list() in this case:
[... for ... in ... if ...]
More
Hi, I have two comments:
1. You don't need to assign the value of enumerate (lines 2 and 6). You could write this:
for i, _ in enumerate(matrix):
...
for j, _ in enumerate(line):
2. But in fact using enumerate on lines 2 and 6 is inappropriate. This would be better:
fo
More