39
Axel
Last seen 2 months ago
Member for 2 years, 9 months, 8 days
Difficulty Normal
try to use sort functions with tuple of count and index
something like this - only one line
return sorted(items, key=lambda x: (items.count(x), -items.index(x)), reverse=True)
More
It's improvement of my [previous solution](https://py.checkio.org/mission/calculator-i/publications/CDG.Axel/python-3/10-lines-proc-simple/)
More
Another idea:
from collections import Counter
from itertools import product as prod
def automaton(step: int) -> int:
Z, D, R = {0}, (1, -1, 1j, -1j, 1-1j, 1j-1), range(step - 1)
[Z.update({z for z, c in Counter(map(sum, prod(Z, D))).items() if c == 1}) for _ in R]
More
alternative solution with sets (4 symbols longer - new expression inside 'sum')
sum(i.weekday() < 5 for i in set(ds + dt.timedelta(i) for i in range(1 + (de - ds).days)) - set(holy))
More
It's not necessary to use list[str]:
vowels = ["a","e","i","o","u","y"]
return character in vowels
-->
vowels = "aeiouy"
return character in vowels
In addition you can slightly this 6 lines:
if current_character != " ":
if is_vowel(current_character):
More
It's not necessary to use list-comprehension instead of a generator. And you can use standard 'filter' function for zero removing (with None as first argument):
sorted_list = sorted([x for x in items if x != 0])
-->
sorted_list = sorted(x for x in items if x != 0)
-->
so
More
Good way to illustrate EAFP (Easy Ask Forgiveness than Permission) :)
But index variable isn't necessary...
More
Great! But it's slightly shorter:
return items[items.index(border) if border in items else 0:]
More
There is good function groupby in itertools. It helps shorten solution to one line
More
Wow, it looks... PEP unfriednly :)
First, integer division "//" better than math.floor() and math.ceil(). Plus == 1 in condition is useless:
math.floor(len(data)/2)
-->
len(data) // 2
math.ceil(len(data)/2)
-->
(len(data) + 1) // 2
return ... if len(data) % 2 == 1 el
More
great!
but
sum(map(str.isdigit, password))
is slightly shorter than
sum([i.isdigit() for i in password])
More
you can use “return condition” instead “return True if condition else False”
return not len(set(string.ascii_lowercase) - set(text.lower()))
More
it looks like C++ code )
if can use something like this
res = ['_' + i.lower() if i.isupper() else i for i in name]
More
map(str.strip, a.splitlines())
map(str.strip,i.split(':'))
looks sligtly shorter than
[i.strip() for i in a.split('\n') if i]
(i.strip() for i in elem.split(':'))
More
for first line you can use "sorted" and filter with first argument None to avoid cycle:
l = sorted(filter(None, items))
last line looks slightly better with some editions:
return [l.pop(0) if i else 0 for i in items]
PS: used pop(0) to avoid reverse sorting
More