30
Leonid Selivanov
Last seen 1 month ago
Member for 10 years, 12 days
Difficulty Normal
Strings in python are iterative. You don't need to preconvert them to a list:
>>> from string import ascii_lowercase
>>> ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> set(ascii_lowercase)
{'q', 'z', 'm', 'e', 'i', 'd', 'p', 'k', 'o', 'c', 'r', 'b', 'w', 'l', 'g', 'n', 'u', 'x
More
In this case:
answer = text.split()[0]
give the same result.
You can read about this in the documentation: https://docs.python.org/3.8/library/stdtypes.html#str.split
More
You used a very intricate algorithm to determine the indexes. It would be convenient to use enumerate() function in case of this solution:
for i, n in enumerate(items):
if n == 0:
zind.append(i)
Or with list comprehension:
zind = [i for i, n in enumerate(items) if n =
More
raise ("%s - number expected after the command" % cmd)
not correct expression
More
Сlass "Friends" essentially combines a list and two functions (two because lists have their own "clear" method), only one instance is used, all methods are "classmethod". It would be easier to use a global list and two global functions I think. This looks like an incorrect use of classes.
More
The code would be faster and more readable if you put repeated calculations in a variable:
num = math.ceil(len(items)/2)
return [items[:num], items[num:]]
More
But bool() is do nothing in this case =)
This is equal to:
return not num % 2
More
This part of the code:
int(d if d[0] != '0' else d[1])
can be replaced with:
int(d)
Because:
>>> int("9")
9
>>> int("09")
9
But it would be best to use:
from datetime import datetime
t = datetime.strptime(t, "%H:%M")
Good luck to learn!
More
An interesting algorithm! Thanks!
One note: PEP8 recommends using lowercase letters for function and variable
names.
More
In this case no need for use str() function. format() returns a string:
print(type(format(4,"b")))
More
Some code is repeated several times. This could have been avoided, for
example:
for i in range(1, len(data)):
if data[i] - data[i - 1] != 1:
res.append((s, e))
s = data[i]
e = data[i]
else:
res.append((s, e))
"else" when used with the "for" l
More
I don't understand what the joke is? After all, the code works perfectly without a "chain" function.
def expand_intervals(items):
return [x for it in items for x in range(it[0], it[1] + 1)]
More
Strings have a built in isupper() method:
"PYTHON IS 1'ST CODING L@NG!".isupper() # True
"PYTHON IS 1'ST CODING L@NG! AND a".isupper() # False
More
This solution can have few optimizations.
First you don't need capital letters at all.
text = text.lower()
"freq" list generates duplicate values. To get rid of this, and at the same time from the unnecessary variable "frset", you can immediately use set() in list comprehension:
freq=[(i
More
The "sorted" function, as well as "map" and "filter" as the key, can accept any function name that will be applied to the original value and return some other value that will be used for sorting. So:
list=sorted([i for i in numbers_array], key=lambda x:abs(x))
is equal:
list=sorted([i for
More
datetime from datetime have a cool method "strptime". It allow to make datetime object from string of date or string of date and time in any format:
def age(self):
return (datetime.strptime("01.01.2018", "%d.%m.%Y") - datetime.strptime(self.birth_date, "%d.%m.%Y")).days // 365
Hav
More