Sort by

recency

|

871 Discussions

|

  • + 0 comments

    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):

    public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            List<Float> arr = new ArrayList<>();
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()) {
                float f = sc.nextFloat();
                arr.add(f);
            }
            int size = arr.size()-1;
            arr.remove(0); // remove first element because it's just size of array
            float mean = 0;
            float median = 0;
            float mode = 0;
            
            // Sort array
            Collections.sort(arr);
            
            // Calculate mean
            for(int i = 0; i < arr.size(); i++) {
                mean += arr.get(i);
            }
            mean = (mean/arr.size());
            System.out.println(mean); // Print out mean
            
            // Calculate median 
            if(arr.size() % 2 == 0) {
                float num1 = arr.get((int)(arr.size()/2));
                float num2 = arr.get((int)(arr.size()/2)-1);
                median = ((num1+num2)/2);
            } else {
                median = arr.get(((int)arr.size()/2)+1);
            }
            System.out.println(median); // Print out median
            
            // Calculate mode
            Map<Float,Integer> modeMap = new TreeMap<>();
            
            for(float f: arr) {
                if(modeMap.containsKey(f)) {
                    int count = modeMap.get(f);
                    modeMap.replace(f,count++);
                } else {
                    modeMap.put(f,0);
                }
            }
            
            int count = 1; // counter to exit loop
            for(Map.Entry<Float,Integer> entry1 : modeMap.entrySet()) {
                while(count < arr.size()){
                    for(Map.Entry<Float,Integer> entry2 : modeMap.entrySet()) {
                        if(entry1.getValue() >= entry2.getValue() && entry1.getKey() < entry2.getKey()) {
                            mode = entry1.getKey();
                            break;
                        }
                        else if(entry1.getKey() == entry2.getKey()) {
                            mode = entry1.getKey();
                        }
                        count++;
                    }
                }
            }
        
            System.out.println(mode); // Print out mode
            
        }
    
  • + 0 comments

    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

    if len(i) % 2 == 0:
        med = (i[mid] + i[mid - 1]) / 2
    else:
        med = i[mid]
    return med
    

    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))

  • + 0 comments

    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))

  • + 0 comments
    from statistics import mean,median,mode
    N = input()
    X = sorted(list(map(int,input().split(" "))))
    print(mean(X))
    print(median(X))
    print(mode(X))
    
  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    N=int(input())
    X=list(map(int, input().split()))
    
    mean=sum(X)/N
    X.sort()
    
    if N% 2 == 0:
        median = (X[int(N/2)-1] + X[int(N/2)])/2
        
    else:
        median=X[int(N/2)]/2
        
    unique_X=list(set(X))
    unique_X.sort()
    
    dic = {element: X.count(element) for element in unique_X}
    
    for key, value in dic.items():
        if value == max(dic.values()):
            mode=key
            break
        
    
    print(round(mean, 1))
    print(round(median, 1))
    print(mode)