Skip to main content
Counter examples.
Source Link
Gloweye
  • 1.7k
  • 12
  • 20
def modify(segment): return [e+1 for e in segment] 

This function is used only in one place, and is only one line. That often means it's better to just inline it:

modifiedA[x:K+x] = modify(A[x:K+x]) # Becomes: modifiedA[x:K+x] = [e+1 for e in A[x:K+x]] 

Use meaningful variable names. No matter what your challenge says, K, M and A are not meaningful variable names. It also seems like you're not doing anything with that M, so why do we even pass it to the function?

In your dominant() function, you look like you want to use collections.Counter. For practice with a language it can be good to check how to set it up yourself, but sometimes we have a good solution ready-made and available, and much better tested than any individual developer could ever do.

With Counter, you can make it work like this:

from collections import Counter def dominant(string): count = Counter(string) return [key for key, value in count.items() if value == count.most_common(1)[0][1]] 

Yeah, it's as simple as that. You could split it out over multiple lines, for clarity:

from collections import Counter def dominant(string): count = Counter(string) dom_list = [] max_occurred = count.most_common(1)[0][1] for key, value in count.items(): if value >= max_occurred: dom_list.append(key) return dom_list 
def modify(segment): return [e+1 for e in segment] 

This function is used only in one place, and is only one line. That often means it's better to just inline it:

modifiedA[x:K+x] = modify(A[x:K+x]) # Becomes: modifiedA[x:K+x] = [e+1 for e in A[x:K+x]] 

Use meaningful variable names. No matter what your challenge says, K, M and A are not meaningful variable names. It also seems like you're not doing anything with that M, so why do we even pass it to the function?

In your dominant() function, you look like you want to use collections.Counter. For practice with a language it can be good to check how to set it up yourself, but sometimes we have a good solution ready-made and available, and much better tested than any individual developer could ever do.

def modify(segment): return [e+1 for e in segment] 

This function is used only in one place, and is only one line. That often means it's better to just inline it:

modifiedA[x:K+x] = modify(A[x:K+x]) # Becomes: modifiedA[x:K+x] = [e+1 for e in A[x:K+x]] 

Use meaningful variable names. No matter what your challenge says, K, M and A are not meaningful variable names. It also seems like you're not doing anything with that M, so why do we even pass it to the function?

In your dominant() function, you look like you want to use collections.Counter. For practice with a language it can be good to check how to set it up yourself, but sometimes we have a good solution ready-made and available, and much better tested than any individual developer could ever do.

With Counter, you can make it work like this:

from collections import Counter def dominant(string): count = Counter(string) return [key for key, value in count.items() if value == count.most_common(1)[0][1]] 

Yeah, it's as simple as that. You could split it out over multiple lines, for clarity:

from collections import Counter def dominant(string): count = Counter(string) dom_list = [] max_occurred = count.most_common(1)[0][1] for key, value in count.items(): if value >= max_occurred: dom_list.append(key) return dom_list 
Source Link
Gloweye
  • 1.7k
  • 12
  • 20

def modify(segment): return [e+1 for e in segment] 

This function is used only in one place, and is only one line. That often means it's better to just inline it:

modifiedA[x:K+x] = modify(A[x:K+x]) # Becomes: modifiedA[x:K+x] = [e+1 for e in A[x:K+x]] 

Use meaningful variable names. No matter what your challenge says, K, M and A are not meaningful variable names. It also seems like you're not doing anything with that M, so why do we even pass it to the function?

In your dominant() function, you look like you want to use collections.Counter. For practice with a language it can be good to check how to set it up yourself, but sometimes we have a good solution ready-made and available, and much better tested than any individual developer could ever do.