Again! I have the right answers but 3/5 tests passed
in [500, 400, 400, 375, 300, 350, 325, 300, 400, 200, 200]
400 appears 3 times
200 appears 2 times
but your code gives the mode as 200.
Take out the rounding stuff earlier.
Also, you need to update the count that you’re comparing new counts against, if you found a higher count,
so
#if the new count it's greater than the default count, we select the new mode
if new_count_mode > default_count_mode:
default_element_mode = new_element_mode
needs to include
default_count_mode = new_count_mode
Hello,
I’ve added my code below, it completes the breif although it’s a bit lengthier than I would of liked. Does anyone have any suggestions on how this could of been streamlined a bit?
Many Thanks
My solution for the mean/mode Python Coding Challenge
Calculate the Mean and Mode
Create a stats_finder()
function that takes in a list of numbers and returns a list containing the mean and mode, in that order. As a reminder, the mean is the average of the values and the mode is the most occurring value. If there are multiple modes, return the mode with the lowest value. Make sure that you write your functions and find these answers from scratch – don’t use imported tools!
For example, stats_finder([500, 400, 400, 375, 300, 350, 325, 300])
should return [368.75, 300]
.
sort it, then only call the one at index 0
My code below. I really like some of the other posted solutions for finding the smallest mode. Certainly min() would be easier than sorting and indexing.
Sorry, I’ve forgotten which mode to return, the first? or the last?
The first. You could also use the min() function as well.
This is what I could come up with, is there a more efficient way to do the mode? Any feedback on readability would also be amazing
type or paste code def stats_finder(array):
#sum part
s = 0
length = 0
mode_dict = {}
for i in array:
s += i
length += 1
if i not in mode_dict:
mode_dict[i] = 1
else:
mode_dict[i] += 1
mean = s/length
# mode part
max_count = 0
most_values = list()
for key, value in mode_dict.items():
if value > max_count:
most_values = []
max_count = value
most_values.append((key,value))
elif value == max_count:
most_values.append((key,value))
mode = None
for (key, value) in most_values:
if mode is None:
mode = key
elif mode > key:
mode = key
return [mean, mode]
print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))
def stats_finder(array):
# Write your code here
length_lists = len(array)
i = 0
y = []
mode_container = []
total = 0
mean = 0
while i < length_lists:
for num in array:
x = array.count(num)
total += array[i]
i += 1
y.append(x)
mode_occ = max(y)
mean = total/length_lists
for num_max in array:
if array.count(num_max) == mode_occ:
if num_max not in mode_container:
mode_container.append(num_max)
if len(mode_container) > 1:
mode = min(mode_container)
else:
mode = mode_container
return [mean, mode]
print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))
Here’s my solution for the Mean and Mode challenge, do enlighten me on how to make the script better, also I wonder what are the other test cases though
def stats_finder(items):
meanandmode = []
meanandmode.append(sum(items)/len(items))
findmode ={}
for n in items:
if n not in findmode:
findmode[n]= 1
else:
findmode[n] += 1
sorteditems = sorted(findmode.items(),key= lambda x: x[1])
highest_value = sorteditems[-1][-1]
H_V_list = [x[0] for x in sorteditems if x[1]== highest_value]
H_V_list = sorted(H_V_list)
meanandmode.append(H_V_list[0])
return meanandmode
here is my not so efficient solution
def stats_finder(array):
Write your code here
total = sum(array)
size = len(array)
mean = total/size
mode = max(set(array), key=array.count)
return[mean, mode]
print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))
Can’t remember, are we supposed to return the first mode or the second one?
I think it says the mode with the lowest value.
from numpy import mean
from scipy import stats
stats_finder = lambda i: [mean(i), stats.mode(i)[0][0]]
print(stats_finder([500, 400, 400, 375, 300, 350, 325, 300]))