1

So I am trying to solve mean, median and mode challenge on Hackerrank. I defined 3 functions to calculate mean, median and mode for a given array with length between 10 and 2500, inclusive.

I get an error with an array of 2500 integers, not sure why. I looked into python documentation and found no mentions of max length for lists. I know I can use statistics module but trying the hard way and being stubborn I guess. Any help and criticism is appreciated regarding my code. Please be honest and brutal if need be. Thanks

N = int(input())
var_list = [int(x) for x in input().split()]

def mean(sample_list):
    mean = sum(sample_list)/N
    print(mean)
    return

def median(sample_list):
    sorted_list = sorted(sample_list)
    if N%2 != 0:
        median = sorted_list[(N//2)]
    else:
        median = (sorted_list[N//2] + sorted_list[(N//2)-1])/2
    print(median)
    return

def mode(sample_list):
    sorted_list = sorted(sample_list)
    mode = min(sorted_list)
    max_count = sorted_list.count(mode)
    for i in sorted_list:
        if (i <= mode) and (sorted_list.count(i) >= max_count):
            mode = i
    print(mode)
    return

mean(var_list)
median(var_list)
mode(var_list)

    Compiler Message

    Wrong Answer

    Input (stdin)

        2500
        19325 74348 68955 98497 26622 32516 97390 64601 64410 10205 5173 25044 23966 60492 71098 13852 27371 40577 74997 42548 95799 26783 51505 25284 49987 99134 33865 25198 24497 19837 53534 44961 93979 76075 57999 93564 71865 90141 5736 54600 58914 72031 78758 30015 21729 57992 35083 33079 6932 96145 73623 55226 18447 15526 41033 46267 52486 64081 3705 51675 97470 64777 31060 90341 55108 77695 16588 64492 21642 56200 48312 5279 15252 20428 57224 38086 19494 57178 49084 37239 32317 68884 98127 79085 77820 2664 37698 84039 63449 63987 20771 3946 862 1311 77463 19216 57974 73012 78016 9412 90919 40744 24322 68755 59072 57407 4026 15452 82125 91125 99024 49150 90465 62477 30556 39943 44421 68568 31056 66870 63203 43521 78523 58464 38319 30682 77207 86684 44876 81896 58623 24624 14808 73395 92533 4398 8767 72743 1999 6507 49353 81676 71188 78019 88429 68320 59395 95307 95770 32034 57015 26439 2878 40394 33748 41552 64939 49762 71841 40393 38293 48853 81628 52111 49934 74061 98537 83075 83920 42792 96943 3357 83393{-truncated-}
        Download to view the full testcase

    Expected Output

        49921.5
        49253.5
        2184
B--rian
  • 4,650
  • 8
  • 25
  • 63
  • I cannot add all 2500 entries but some of them are in the post. Like you said the link is accesable only to me, sorry didn't think of that. – Kovus Seyit Aug 23 '19 at 02:00

3 Answers3

0

Your issue seems to be that you are actually using standard list operations rather than calculating things on the fly, while looping through the data once (for the average). sum(sample_list) will almost surely give you something which exceeds the double-limit, i.a.w. it becomes really big.

Further reading

B--rian
  • 4,650
  • 8
  • 25
  • 63
0

I figured out that you forgot to change the max_count variable inside the if block. Probably that causes the wrong result. I tested the debugged version on my computer and they seem to work well when I compare their result with the scipy's built-in functions. The correct mode function should be

def mode(sample_list):
    N = len(sample_list)
    sorted_list = sorted(sample_list)
    mode = min(sorted_list)
    max_count = sorted_list.count(mode)
    for i in sorted_list:
        if (sorted_list.count(i) >= max_count):
            mode = i
            max_count = sorted_list.count(i)
    print(mode)
aesari
  • 167
  • 6
0

I was busy with some stuff and now came back to completing this. I am happy to say that I have matured enough as a coder and solved this issue.

Here is the solution:

# Enter your code here. Read input from STDIN. Print output to STDOUT


# Input an array of numbers, convert it to integer array
n = int(input())
my_array = list(map(int, input().split()))
my_array.sort()

# Find mean
array_mean = sum(my_array) / n
print(array_mean)

# Find median
if (n%2) != 0:
    array_median = my_array[n//2]
else:
    array_median = (my_array[n//2 - 1] + my_array[n//2]) / 2
print(array_median)

# Find mode(I could do this using multimode method of statistics module for python 3.8)
def sort_second(array):
    return array[1]

modes = [[i, my_array.count(i)] for i in my_array]
modes.sort(key = sort_second, reverse=True)
array_mode = modes[0][0]
print(array_mode)