# Python Challenge - Calculate the Mean and Mode

``````
def stats_finder(array):
mean_mode = [0,0]
total = 0
mode = 0
repeat = 0
temp = 0
for number in array:
total += number
mean = total/len(array)
mean_mode[0] = mean
for number in array:
repeat = 0
for i in range(len(array)):
if number == array[i]:
repeat += 1
if repeat > temp:
temp = repeat
mode = number
#print(mode)
elif repeat == temp:
if mode > number:
mode = number
mean_mode[1] = mode
return mean_mode

print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))
``````
def stats_finder(array): def find_Mean(array): mean = sum(array) / len(array) mean = round(mean, 2) return mean def find_Mode(array): counter_dict = {} for i in array: if i not in counter_dict.keys(): counter_dict[i]=1 else: counter_dict[i]=counter_dict[i]+1 modes = {k:v for k,v in counter_dict.items() if v==max(counter_dict.values())} mode = min(modes.keys()) return mode return [find_Mean(array), find_Mode(array)] print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

def stats_finder(array):

modeValue = findMode(array)
meanValue = findMean(array)
result = [meanValue, modeValue]
return result

def findMode(lst):

# Find the number with a largest counter

new_dict = {}
for element in lst:
if element in new_dict:
new_dict[element] += 1
else:
new_dict[element] = 1
modeValue = None
for key, value in new_dict.items():
if modeValue is None:
modeValue = (key, value)
else:
if new_dict[key] > modeValue[1]:
modeValue = (key, value)
elif new_dict[key] == modeValue[1]:
if key < modeValue[0]:
modeValue = (key, value)
return modeValue[0]

def findMean(lst):
nLen = len(lst)
nSum = sum(lst)
return nSum / nLen

print(findMean([500, 400, 400, 375, 300, 350, 325, 300]))
print(findMode([500, 400, 400, 375, 300, 350, 325, 300]))
print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

def stats_finder(array):
dicc ={}
list_mod =
list_mean_mode=
for i in array:

for i in array:
if i not in dicc:
dicc[i] = 1
else:
dicc[i]+=1
freq = max(dicc.values())
for key, values in dicc.items():
if values == freq:
list_mod.append(key)
mode = min(list_mod)
list_mean_mode.append(mean)
list_mean_mode.append(mode)
return list_mean_mode

print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

def stats_finder(lst): hist = {} for x in lst: hist[x] = 1 if x not in hist else hist[x] + 1 return [sum(lst)/len(lst),(min([x for x in hist if hist[x] == max(hist.values())]))]

def stats_finder(numbers) :
res =
mode_list =

``````#Finding mean
mean = sum(numbers)/len(numbers)

#Finding modes
modes = 0

for i in numbers :
if numbers.count(i) > modes :
modes = numbers.count(i)

for i in numbers :
if numbers.count(i) == modes :
mode_list.append(i)

mode_result = min(mode_list)

res.append(mean)
res.append(mode_result)

return res
``````

print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

def stats_finder(numbers):
res =
mode_list =

numbers.sort()

# Finding mean

mean = sum(numbers) / len(numbers)

# Finding modes

modes = max(numbers,key=numbers.count)

res.append(mean)
res.append(modes)

return res

print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

This is way better, thankyou forum

def insert(arr, n): len_arr = len(arr) i = 0 while i < len_arr and arr[i] < n: i += 1 arr.insert(i, n) count = 1 while i+1 < len_arr and arr[i+1] == n: count += 1 i += 1 return count def stats_finder(arr): arr_sorted = [] arr_sum = 0 mode = [0, 0] # [value, count] for n in arr: arr_sum += n c = insert(arr_sorted, n) if c == mode[1] and n < mode[0]: mode[0] = n elif c > mode[1]: mode[0] = n mode[1] = c mean = arr_sum/len(arr) return [mean, mode[0]] print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

Tried to do this one as “from scratch” as possible (no `sum()`, `sort()`, `count()`, etc.). Is it efficient? Probably not. Does it look pythonesque? God no. But it works and it doesn’t invoke any additional functions besides `len()` and `insert()`. And with some tweaks those two could probably be replaced too.

The idea is: As I have to touch every array element at least once anyway to compute the mean, I create a second array into which I insertion-sort the elements from the first. With each insertion I check for existing occurences of the inserted value and keep track of the one occuring most often.

If you can think of improvements to this code that don’t use additional function calls, bring them!

Cheers

def stats_finder(array):
lst =
mean = 0
mode = 0

summ = 0
for item in array:
summ += item
mean = summ / len(array)

lst.append(mean)

array = sorted(array)

mydict = {}
for item in array:
instance = 0
for j in array:
if j == item:
instance += 1
mydict.update({item:instance})

maxx = float("-inf")
mode = None

for k,v in mydict.items():
if v > maxx:
mode = k
maxx = v
lst.append(mode)

return lst

print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

def stats_finder(array):

average = 0
mode = 0
peak = 0
count = 1
array.sort()

average = sum(array)
average /= len(array)

mode = array[0]

for i in range(1, len(array)):

``````if array[i] != array[i - 1]:
count = 1

elif array[i] == array[i - 1]:
count += 1

if count > peak:

peak = count

if mode != array [i]:
mode = array[i]
count = 1
``````

return[average,mode]

print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

``````def stats_finder(array):
array  = sorted(array)
median = sum(array) / len(array)

for every_number in range(0,len(array)) :
counts = array.count(array[every_number])
if counts >= 2 :
mode = array[every_number]
break

return [median, mode]
``````

hello everyone, i am new to coding and i was practicing with this project but my code was extremely long as i took a look at some of these answers i came up with the line of code above but i did not understand what it does. so, can someone please explain it to me please?

Python max() function - ThePythonGuru.com

A set contains only unique items, unordered. This eliminates duplicates. We should note that it does not alter the original list. That is why the `.count` method still works.

1 Like
def stats_finder(array): total = 0 for num in array: total += num mean = total / len(array) counter = {} for num in array: if num not in counter: counter[num] = 1 else: counter[num] += 1 mode = 0 mode_value = 0 for key in counter: if counter[key] > mode_value: mode = key mode_value = counter[key] elif counter[key] == mode_value: if key < mode: mode = key else: pass else: pass return [mean, mode] print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))

gg ez

``````def stats_finder(array):

array.sort()
mean = 0
mode = 0

for i in range(0, len(array)):
mean += array[i] / len(array)

tempMode = [0, 0]

for i in array:
if array.count(i) > tempMode[1]:
tempMode = [i, array.count(i)]

mode = tempMode[0]

return [mean, mode]
print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))
``````

def stats_finder(array):

lst =
total = 0
for element in array:
total += element
mean = total/len(array)
mode = max(set(array), key = array.count)
lst.append(mean)
lst.append(mode)
return lst

print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300, ]))

def find_mean(array): return sum(array) / len(array) def find_mode(array): kv_pairs = {} for i in array: if i in kv_pairs.keys(): kv_pairs[i] += 1 else: kv_pairs[i] = 1 return get_max_key(kv_pairs) def get_max_key(my_dict): my_max_v = 0 my_max_k = [] my_real_max_k = [] for i in my_dict: if my_dict[i] >= my_max_v: my_max_v = my_dict[i] my_max_k.append(i) for i in my_dict: if my_dict[i] >= my_max_v: my_max_v = my_dict[i] my_real_max_k.append(i) if len(my_real_max_k) == 1: return my_real_max_k[0] else: my_min_max_k = 10000000 for i in my_real_max_k: if my_dict[i] < my_min_max_k: my_min_max_k = i return my_min_max_k def stats_finder(array): stats = [] stats.append(find_mean(array)) stats.append(find_mode(array)) return stats print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))
def stats_finder(array): mean = sum(array)/len(array) counts = {x:array.count(x) for x in array} mode = min([k for k in counts.keys() if counts[k] > 1]) return [mean,mode] print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))
def stats_finder(array): mean = sum(array)/len(array) mode = {} for num in array: if num not in mode: mode[num] = 1 else: mode[num] += 1 x = array[0] for num in mode.keys(): if mode[num] > mode[x]: x = num elif mode[num] == mode[x] and num < x: x = num return [mean,x] print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))
def stats_finder(array): counts = {} mean = 0 for number in array: mean += number counts[number] = counts.get(number, 0) + 1 mean /= len(array) mode = None for number in sorted(counts.keys()): if counts[number] > counts.get(mode, 0): mode = number return [mean, mode] print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))