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
|
874 Discussions
|
Please Login in order to post a comment
python
import numpy as np
from collections import Counter
array_size = int(input())
array_numbers = list(map(int, input().split()))
mean = np.mean(array_numbers)
median = np.median(array_numbers)
counts= Counter(array_numbers)
max_count= max(counts.values())
modes= [number for number, count in counts.items() if count == max_count]
mode = min(modes)
print(mean) print(median) print(mode)
or
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))