patternpythonModerate
Mode Calculator
Viewed 0 times
modecalculatorstackoverflow
Problem
I was doing a puzzle on Coderbyte, and here is what the puzzle stated:
Have the function
I also realize I can use other programs, but I am just looking for a way to optimize this one.
Have the function
SimpleMode(arr) take the array of numbers stored in arr and return the number that appears most frequently (the mode). For example: if arr contains [10, 4, 5, 2, 4] the output should be 4. If there is more than one mode return the one that appeared in the array first (i.e. [5, 10, 10, 6, 5] should return 5 because it appeared first). If there is no mode return -1. The array will not be empty.import time
from random import randrange
def SimpleMode(arr):
bestMode=0
numTimes=0
for x in range(len(arr)):
if len(arr)>0:
currentNum=arr[0]
currentMode=0
while currentNum in arr:
currentMode+=1
arr.remove(currentNum)
if currentMode>numTimes:
numTimes=currentMode
bestMode=currentNum
else: break
if numTimes==1: bestMode=-1
return bestMode
start_time = time.time()
numbers = [randrange(1,10) for x in range(0, 1000)]
print(SimpleMode(numbers))
print("--- %s seconds ---" % (time.time() - start_time))I also realize I can use other programs, but I am just looking for a way to optimize this one.
Solution
You have many violations of PEP 8, which specifies four-space indentation, a preference for
The loop…
… would be better written as…
The function has a side-effect of trashing the array contents as it works, which is surprising and undesirable.
Your algorithm is approximately O(n3), which is rather slow. The nested loop makes it O(n2), and the
If the problem had not specified that ties had to be resolved in favour of the element that appears first, the solution would be easy:
Alas,
The call to
snake_case(), and a space before and after operators like >.The loop…
for x in range(len(arr)):
if len(arr)>0:
# Code that does not involve x and removes some elements from arr
else: break… would be better written as…
while arr:
# Code that removes some elements from arrThe function has a side-effect of trashing the array contents as it works, which is surprising and undesirable.
SimpleMode([]) returns 0. By my interpretation of the specs, it should return -1.Your algorithm is approximately O(n3), which is rather slow. The nested loop makes it O(n2), and the
.remove() calls slow it down by a factor of n, since removing elements from the middle of an array requires a lot of copying to fill in the gap.If the problem had not specified that ties had to be resolved in favour of the element that appears first, the solution would be easy:
from collections import Counter
def simple_mode(arr):
return Counter(arr).most_common(1)[0]Alas,
Counter.most_common() makes no guarantee of the order, so we have to write some more code. Fortunately, Python's sorted() function guarantees stable sorting.def simple_mode(arr):
if not arr:
return -1
count = Counter(arr)
mode = sorted(arr, key=lambda a: -count[a])[0]
return mode if count[mode] > 1 else -1The call to
sorted() dominates the processing time, making this implementation O(n log n).Code Snippets
for x in range(len(arr)):
if len(arr)>0:
# Code that does not involve x and removes some elements from arr
else: breakwhile arr:
# Code that removes some elements from arrfrom collections import Counter
def simple_mode(arr):
return Counter(arr).most_common(1)[0]def simple_mode(arr):
if not arr:
return -1
count = Counter(arr)
mode = sorted(arr, key=lambda a: -count[a])[0]
return mode if count[mode] > 1 else -1Context
StackExchange Code Review Q#96303, answer score: 10
Revisions (0)
No revisions yet.