aboutsummaryrefslogtreecommitdiff
path: root/mean_arr_remove_element.py
blob: 40b77a0a27771c6e5e0036ce09cef3a63a0a1309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 1619. Mean of Array After Removing Some Elements
import statistics 

"""
givn an integer array 'arr', return the mean of the remaining integers after
removing the smallest 5% and the largest 5% of the elements. answers within
10^-5 of the actual answer will be considered accepted.
"""


class Solution(object):
    def trimMean(self, arr):
        """
        :type arr: List[int]
        :rtype: float
        """
        arr.sort()
        n = int(0.05 * len(arr))
        return statistics.mean(arr[n : len(arr) - n])


if __name__ == "__main__":
    obj = Solution()
    print(
        obj.trimMean(arr=[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3])
    )
    print(
        obj.trimMean(
            arr=[
                6,
                0,
                7,
                0,
                7,
                5,
                7,
                8,
                3,
                4,
                0,
                7,
                8,
                1,
                6,
                8,
                1,
                1,
                2,
                4,
                8,
                1,
                9,
                5,
                4,
                3,
                8,
                5,
                10,
                8,
                6,
                6,
                1,
                0,
                6,
                10,
                8,
                2,
                3,
                4,
            ]
        )
    )
    print(
        obj.trimMean(arr=[6, 2, 7, 5, 1, 2, 0, 3, 10, 2, 5, 0, 5, 5, 0, 8, 7, 6, 8, 0])
    )