Ok so, I was doing a puzzle on coderbyte, and here is what the puzzle stated:
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 (ie. [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.
So here is my program:
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))
And here is a much simpler program which someone else wrote:
import time
from random import randrange
def SimpleMode(arr):
best = -1
best_count = 1
for c in arr:
if arr.count(c) > best_count:
best = c
best_count = arr.count(c)
return best
start_time = time.time()
numbers = [randrange(1,10) for x in range(0, 1000)]
print(SimpleMode(numbers))
print("--- %s seconds ---" % (time.time() - start_time))
Now I know that using my method of timing this it depends on what my CPU is doing and whatnot so this is not the most accurate way, but leaving that aside what I found was that my computer took 0.012000 seconds to run my program, yet it took 0.025001 seconds to run the second program.
Now here is where I am puzzled. My program which I have written myself takes less than half the time the other program takes which uses a built-in python function and has only one for-loop whereas my program has a while loop inside a for-loop.
Can anyone provide any insight into this?
len(arr) > 0will likely be true before you've iterated through the complete array. The second algorithm iterates through the full array no matter what, so depending on the array, one algorithm can beat the other way. Have you tried with an array like[1, 2, 3, 4, 5, 6, ...]?