We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 0: Mean, Median, and Mode
Day 0: Mean, Median, and Mode
Sort by
recency
|
872 Discussions
|
Please Login in order to post a comment
include
include
include
include // For controlling the output format
using namespace std;
int main() { // Step 1: Read the number of elements (not actually used, we just need the list) int n; cin >> n;
}
For anyone having trouble in Java: I have yet to optimize this but I was able to pass all test cases except the last one (TestCase 3):
Enter your code here. Read input from STDIN. Print output to STDOUT
''' from statistics import mean, median, mode
n = input() x = sorted(list(map(int, input().split(" "))))
print(mean(x)) print(median(x)) print(mode(x)) ''' from collections import Counter
n = input() x = sorted(list(map(int, input().split(" "))))
def mean(i): output = sum(i)/len(i) return output
def median(i): i.sort() mid = len(i) // 2
def mode(i): count = Counter(i) max_count = max(count.values()) modes = [key for key, count in count.items() if count == max_count] return min(modes) if len(modes) > 1 else modes[0]
print(mean(x)) print(median(x)) print(mode(x))
n = int(input()) raw = input().split() x = [int(i) for i in raw]
def mean(arr): return sum(arr)/len(arr)
def median(arr): sorted_arr = sorted(arr) a = (len(arr)-1)//2 b = a + 1 return (sorted_arr[a]+sorted_arr[b])/2
def mode(arr): sorted_arr = sorted(arr) sorted_set = list( set(sorted_arr)) occurances = {item : sorted_arr.count(item) for item in sorted_set} #in case there are multiple occurances of multiple items n_dept_occ = [] for key, value in occurances.items(): if max(occurances.values()) == 1: return min(occurances.keys()) elif value == max(occurances.values()): n_dept_occ.append(key) return min(n_dept_occ)
print(mean(x)) print(median(x)) print(mode(x))