-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path036_Sum without highest and lowest number.py
105 lines (73 loc) · 2.28 KB
/
036_Sum without highest and lowest number.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Codewars Coding Challenge
Sum without highest and lowest number
Task
Sum all the numbers of a given array ( cq. list ), except the highest and the lowest element ( by value, not by index! ).
The highest or lowest element respectively is a single element at each edge, even if there are more than one with the same value.
Mind the input validation.
Example
{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6
Input validation
If an empty value ( null, None, Nothing etc. ) is given instead of an array, or the given array is an empty list or a list with only 1 element, return 0.
def sum_array(arr):
#your code here
https://www.codewars.com/kata/576b93db1129fcf2200001e6/train/python
"""
# My Solution
def sum_array(arr):
if not arr or len(arr) <= 1:
return 0
return sum(arr) - max(arr) - min(arr)
# print(sum_array([6, 2, 1, 8, 10]))
"""
Sample Tests
import codewars_test as test
from solution import sum_array
@test.describe("Fixed Tests")
def fixed_tests():
@test.it('None or Empty')
def basic_test_cases():
test.assert_equals(sum_array(None), 0)
test.assert_equals(sum_array([]), 0)
@test.it("Only one Element")
def one_test_cases():
test.assert_equals(sum_array([3]), 0)
test.assert_equals(sum_array([-3]), 0)
@test.it("Only two Element")
def two_test_cases():
test.assert_equals(sum_array([ 3, 5]), 0)
test.assert_equals(sum_array([-3, -5]), 0)
@test.it("Real Tests")
def real_test_cases():
test.assert_equals(sum_array([6, 2, 1, 8, 10]), 16)
test.assert_equals(sum_array([6, 0, 1, 10, 10]), 17)
test.assert_equals(sum_array([-6, -20, -1, -10, -12]), -28)
test.assert_equals(sum_array([-6, 20, -1, 10, -12]), 3)
"""
"""
Perfect Solution From Codewars
=1=
def sum_array(arr):
return sum(sorted(arr)[1:-1]) if arr and len(arr) > 1 else 0
=2=
def sum_array(arr):
return sum(sorted(arr)[1:-1]) if arr else 0
=3=
def sum_array(arr):
return sum(sorted(arr or [])[1:-1])
=4=
sum_array=lambda a:a and sum(sorted(a)[1:-1])or 0
=5=
def sum_array(arr):
if arr is None or len(arr) < 2:
return 0
mi, ma, s = arr[0], arr[0], 0
for x in arr:
if x > ma:
ma = x
elif x < mi:
mi = x
s += x
return s - mi - ma
"""