40
Last seen 4 days ago
Member for 9 years, 10 months, 19 days
Difficulty Advanced
Hi,
1. Look at _str.endswith()_.
2. Look at `operator` module.
3. In this a _generator expression_ or list comprehension would be faster than _map()_ and IMHO cleaner:
[x != y and len(x) > len(y) and x[-len(y):] == y for x, y in itertools.product(words_set, words_set)]
More
Hi,
1. _list()_ is redundant.
2. You could use `&` operator for set intersection.
More
Hi, lambda is redudant. _abs()_ is a function:
return sorted(numbers_array, key=abs) # pay attention that it is abs and not abs()
More
Hi,
1. You don't need the `result` variable.
2. Look at _all()_, e. g. you can write this instead of line 8:
if all(j.isalpha() for j in wlist[i:i+3]):
3. You don't need that `else` branch.
4. Look at _any()_. Using it you can refactor lines 3 and 6-10 like this:
return any(all(j.is
More
Hi,
1. all those inner `if...else`s are redundant, e. g.:
return not (x == 1 and y == 0) # for lines 13-16
2. you don't have to enumerate the cases. Just use `or`, `and` and `not` built-ins and `==` and `!=` operators.
More
Hi,
1. `== True` is redundant as _str.isupper()_ returns __bool__.
2. __str__ is immutable, therefore it is not the best type for accumulation.
3. Last but not least __str__ is _iterable_ so you could write:
for i in text:
if i.isupper():
word += i
# or even better
More
Hi, you don't need all those internal `if...else`s e. g.:
if operation == "equivalence":
return x == y
More
Hi,
1. you don't need _re_:
counts = Counter(filter(str.islower, text.lower()))
2. you don't need `max_counts`:
return sorted(x for x in counts if counts[x] == max(counts.values()))[0]
More
Hi, _sorting_ function is redundant:
return sorted(numbers_array, key=abs)
More
Hi, lines 20 and 21 are redundant. In fact all that `if...else` is redundant, as it's a precondition.
More
Hi,
1. For line 3 you can use _filter()_:
text = filter(str.isalpha, text.lower())
2. In fact you don't need the `text` variable:
common = Counter(filter(str.isalpha, text.lower()))
3. With _sorted()_ you can get rid of lines 5 and 6:
return sorted(x for (x,y) in common if
More
Hi,
1. `== True` is redundant. _str.isupper()_ returns __bool__.
2. `ch` is a __str__ so _str()_ on line is redundant.
3. Look at _generator expressions_ and the _str.join()_ method.
More
Hi,
1. _any()_ instead of lines 40-43?
return any(first in i and second in i for i in groups)
2. `names = net.keys()`
3. `net = {name: {} for rel in new for name in rel}`
etc.
More