-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6
95 lines (33 loc) · 1.25 KB
/
6
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
codewars.com
You are given an array (which will have a length of at least 3, but could be very large) containing integers.
The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N.
Write a method that takes the array as an argument and returns this "outlier" N.
Examples
[2, 4, 0, 100, 4, 11, 2602, 36]
Should return: 11 (the only odd number)
[160, 3, 1719, 19, 11, 13, -21]
Should return: 160 (the only even number)
def find_outlier(integers):
return #TODO
test.assert_equals(find_outlier([2, 4, 6, 8, 10, 3]), 3)
test.assert_equals(find_outlier([2, 4, 0, 100, 4, 11, 2602, 36]), 11)
test.assert_equals(find_outlier([160, 3, 1719, 19, 11, 13, -21]), 160)
#my_solution
def find_outlier(integers):
odd = 0
even = 0
for i in integers:
if (i % 2) == 0:
odd += 1
else:
even += 1
if odd == 1:
#array is even, outlier is odd
for i in integers:
if (i % 2) == 0:
return i #return outlier as odd
if even == 1:
#array is odd, outlier is even
for i in integers:
if (i % 2) == 1:
return i # return outlier as even