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
Nice.
Another way to generate all the start/stop indices for substrings is via
itertools.combinations(range(len(string)),2)
More
Good.
You can use i.split() instead of i.split(' '); the default value for split is any whitespace which is different from just the space character ' ', but for this exercise they produce the same result.
Also, rather than checking for inclusion in a dictionary for setting the initial value you ca
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
Very nice. Very nice use of itertools.groupby. Also good uses of endswith, any, and join. Would recommend only one small improvement. For removing non-alphabetic and non-whitespace, you can split into words before checking i == " ". This eliminates the checking for space.
import
More
Good. You can call "all" with the result of the map function directly, no need to convert to a list.
def all_the_same(elements: List[Any]) -> bool:
return all(map(lambda x: x == elements[0], elements))
You could also call "all" on the generator comprehension comparing elements to the
More
Good. Since "len(set(elements))>1" is a bool you can return it directly. You could return "not len(set(elements)) > 1" or the "len(set(elements)) <= 1" as below.
def all_the_same(elements: List[Any]) -> bool:
return len(set(elements)) <= 1
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
Good. Instead of looping over the indices of data, you can loop over the values. Then you have the following.
def checkio(data: str) -> bool:
if len(data) < 10:
return False
num = 0
upp = 0
low = 0
for d in data:
num = num + d.is
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
Nice as always. Function parameter should be "races" as you have it. Not "horses" as provided.
More
Good. We can remove the "!=None" checking; None is evaluated as False and re.search match objects are evaluated as True. With this we have.
def checkio(data):
if len(data)>=10:
if re.search("[A-Z]", data):
if re.search("[a-z]", data):
if
More
Good. By using default arguments, we can eliminate the need for the extra function "check." Then we just have one function called "checkio."
def checkio(data,i=0):
if len(data)==0: # base case length 0
return i
elif len(data)==1: # base case length 1
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
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. No need to cast to list. The sorted function takes in any iterable, e.g. lists and tuples.
Also the key function "lambda item: abs(item)" is the function taking x as input and returning the absolute value of x as output. But this is the definition of the built-in "abs" function, so you c
More
Good. You can use key sorting on the original list values directly. Then the sorting algorithm makes the comparison f(x) < f(y) rather than x < y, where f is the key. The original list items don't have to be comparable (x < y may not make sense), only their keys f(x) and f(y) have to be comparable
More
Good. Once you have the dictionary you can use "sorted" or "max" supplying a tuple as the sort key. "max(dic.keys(),key=lambda x:(dic[x],-ord(x)))" returns the maximum value where values are sorted by "(dic[x],-ord(x))", that is by (1st) the count of each letter, and (2nd - tiebreaker) the positio
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
Good. No need for else/pass - this can be commented out. Also casting to lowercase first "text = text.lower()" simplifies the logic.
def checkio(text):
text = text.lower()
frequency=[0]*26
for i in range(len(text)):
ascii=ord(text[i])
More