29
Last seen 4 years ago
Member for 10 years, 6 months, 13 days
Difficulty Normal
Learned Python through pyquick, blogs, and checkio. Trying to get good enough to help others with Python. 2LwithGA
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
Nice. You could also check the length via regular expression as well re.search('.{10,}',data).
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. You can shorten range(0,len(words)) to just range(len(words)). You can also loop over the values of words instead of the indices.
for w in words:
if w.lower() in text.lower():
WordsCounter+=1
More
Good. You can include reverse=True as an optional argument instead of reversing args.
def min(*args, **kwargs):
key = kwargs.get("key", None)
if len(args) == 1:
args = list(args[0]) # one single list or generator object
return sorted(args,key=key)[0]
More
Python has slicing similar to Matlab, e.g. array[::2].
Also in Python, any object can be tested for truth. The values that return false are what you expect. E.g. for numbers 0 is false, for arrays the empty array [] is false, etc.
def checkio(array):
if array:
return array[-1]*s
More
Nice.
You can use lambda inside sorted, rather than defining the key function elsewhere.
return sorted(counts.items(),key=lambda x:(-x[1],x[0]))[0][0]
Also rather than sorted(...)[0] you can use min(...). Then you have the following instead.
return min(counts.items(),key=lambda x:(-x[1]
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. 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
Nice as always. Function parameter should be "races" as you have it. Not "horses" as provided.
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
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.
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.
Another way to generate all the start/stop indices for substrings is via
itertools.combinations(range(len(string)),2)
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
Nice solution. May want to be careful not to override built-in functions (min).
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
Cute solution for small lists. Starts to hit run-time issues for lists of length 11 or 12 right above the precondition limit due to overlapping subproblems.
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. 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