29
Last seen 4 years ago
Member for 10 years, 6 months, 27 days
Difficulty Normal
Learned Python through pyquick, blogs, and checkio. Trying to get good enough to help others with Python. 2LwithGA
Good. Using the "ord" function we can define a function "alphanumber" to replace the parallel array look-up. With this the code becomes. If "ord" didn't exists we could use string indexing as commented out.
def checkio(str_number, radix):
alphanumber = lambda x:ord(x)-55 # e
More
Good. The method "join" is helpful here. Similar to "join" in javascript.
# '77'.join(['aaa','bbb','ccc']) --> 'aaa77bbb77ccc'
def left_join(phrases):
return ','.join(phrases).replace('right','left')
Join is useful for writing formatted data to files. Also we can use it to join
More
Nice. Good use of itertools.groupby. For checking isalpha, you can use str.isalpha directly rather than creating a lambda. Both are the function taking a character x as input and returning whether x is alpha as output. Also, endswith may be a little nicer than subj[-3:].
def is_stressful(su
More
Nice. Nicer with getting rid of "c for c in." Even nicer with lambda. No need for r=0, right?
checkio=lambda a:a[-1]*sum(a[::2]) if a else 0
More
Good, but move your lambda. No need for it after key: "key=lambda n:abs(n)" is the same as "key=abs".
checkio = lambda i:sorted(i,key=abs)
More
Like the use of Floyd's algorithm, and itertools.product is useful for many of these problems (I keep forgetting it).
More
Not bad. Not not good. But converting the result to a bool may be clearer.
def checkio(data: str) -> bool:
return bool(len(data) >= 10 and re.search("[a-z]+", data) \
and re.search("[A-Z]+", data) \
an
More
Good. You can shorten "len(args) > 1" to "args[1:]" if you like. You can also perform the if-else in a single line "args = list(args) if len(args)>1 else args[0]"
More
Good. You can shorten array[len(array)-1] to just array[-1]. You can also shorten "if len(array) != 0" to "if len(array)" or just "if array".
More
Good. When finding the min and max value in the dictionary, we can set the key to the "get" method rather than creating a lambda.
def fastest_horse(horses):
leaderboard = {}
for race in horses:
places = {position: int(time.split(':')[0]) * 60 + int(time.split(':
More
For checking a dictionary, you can use "get" to supply a default value to return in case the key is not in the dictionary. With this you have:
def checkio(text):
text = text.lower()
freq = {}
for letter in text:
if letter.isalpha():
#if lette
More
Good. You can use "get" to provide a default return value in case a key is not in the dictionary. Also, you could use collections.defaultdict.
def checkio(text):
dict_letters = dict()
for a in text:
if a.isalpha():
#if a.lower() in dict_letters:
More