如何在 python 中使用具有 2500 数组的自定义均值、中值、众数函数?

How to use custom mean, median, mode functions with array of 2500 in python?

所以我正在尝试解决 mean, median and mode challenge on Hackerrank。我定义了 3 个函数来计算长度在 10 到 2500 之间的给定数组的均值、中位数和众数。

我收到一个包含 2500 个整数的数组错误,不知道为什么。我查看了 python 文档,发现没有提及列表的最大长度。我知道我可以使用统计模块,但我想是在努力尝试并且很固执。感谢您对我的代码提供任何帮助和批评。如果需要,请诚实和残酷。谢谢

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

您的问题似乎是您实际上使用的是标准列表操作,而不是动态计算,同时循环数据一次(对于平均值)。 sum(sample_list) 几乎肯定会给你一些超过双倍限制的东西,i.a.w。它变得 真的 大。

进一步阅读

  • Calculating the mean, variance, skewness, and kurtosis on the fly
  • How do I determine the standard deviation (stddev) of a set of values?
  • Rolling variance algorithm
  • What is a good solution for calculating an average where the sum of all values exceeds a double's limits?
  • How do I determine the standard deviation (stddev) of a set of values?

我发现您忘记更改 if 块中的 max_count 变量。可能这会导致错误的结果。我在我的计算机上测试了调试版本,当我将它们的结果与 scipy 的内置函数进行比较时,它们似乎运行良好。正确的 mode 函数应该是

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)

我正在忙一些事情,现在回来完成这个。我很高兴地说,我作为一名编码员已经足够成熟并解决了这个问题。

解决方法如下:

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