40
Last seen 4 days ago
Member for 9 years, 10 months, 19 days
Difficulty Advanced
Hi,
`lambda` is redundant as `abs()` is taking one argument and functions are first class citizens in python: `return sorted(numbers_array, key=abs)`.
Regards,
suic
More
Hi,
```python
text = text.split(' ') # This splits the string by spaces.
text[0] = text[0].capitalize() # can be replaced with text[0].upper()
text = (' ').join(text) # the first paren in redundant
if text[-1] != '.': # not pythonic. There's the str.endswith
More
Hi, interesting approach. The 0 on line three is redundant. Regards, suic
More
Let Python do the work for you:
```python
from operator import itemgetter
def bigger_price(limit, data):
return sorted(data, key=itemgetter('price'), reverse=True)[:limit]
```
More
Hi, you could use `sorted()` instead of `list.sort`:
```python
return sorted(data, key=itemgetter('price', 'name'), reverse=True)[:limit]
```
Regards,
suic
More
Hi, a bit shorter:
```python
import time as t
def time_converter(time):
time = ('0' + time if len(time) < 10 else time).replace('.', '')
return t.strftime("%H:%M", t.strptime(time,"%I:%M %p"))
```
Regards,
suic
More
Hi, very nice concise solution. When you need queue in Python, use [collections. deque](https://docs.python.org/3/library/collections.html?highlight=deque#collections.deque). `list.pop(0)` is very inefficient.
```python
from collections import deque
# ...
queue = deque((r,c) for r,row in en
More
In Python `super().__init__()` is enough. Also, you could make `is_alive` a
property which prevents accidental assignments. Regards, suic
More
Compared to other creative solutions, this one has at least a `lambda` :)
More
As you actually don't use `item` except for decomposing it, you can do the decomposition directly in the for loop:
```python
for first, last in items:
# ...
```
Regards,
suic
More
This is an anti-pattern:
```python
string = ''
for i in self.connected:
string += i.name + ' '
string = string[:-1]
```
You could write this instead:
```python
string = " ".join(i.name for i in self.connected)
```
This is alternative for 85-89:
```python
return {node.name for node in nodes
More
Hi, `if all(digit in possible for digit in number):` is a shorter alternative for line 5. Regards, suic
More
Hi, I finally found a solution which doesn't use exceptions. However, this is quite inefficient:
1. `str_number = str_number[1:]` creates unnecessary intermediate strings. `for` loop is the preferred way to iterate over an iterable including `str`.
2. No 1. is actually needed because of `len(str_nu
More
Alternative:
```python
def convert(n, d, f=__import__('itertools').product, r=range(1, 100), s=__import__('re').sub, q= __import__('decimal')):
q.getcontext().prec = 100
k, p = str(q.Decimal(n)/d).replace('.', ''), str(n/d).find('.')
for a, b in [(k[:x], k[x:][:y]) for x, y in f(r, r) if
More
I agree with the previous comment. This is "refactorable" to one function without nested functions in one minute.
More