forked from noisebridge/PythonClass
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsolutions.py
More file actions
executable file
·56 lines (39 loc) · 1.34 KB
/
solutions.py
File metadata and controls
executable file
·56 lines (39 loc) · 1.34 KB
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
#! /usr/bin/env python
import numpy
values = numpy.random.randint(low=-100, high=100, size=100)
def solution_1(values):
current_max = 0
for i in range(len(values)):
for j in range(i, len(values)):
current_max = max(current_max, sum(values[i:j]))
return current_max
def solution_2(values):
current_max = 0
for i in range(len(values)):
current_sum = 0
for val in values[i:]:
current_sum += val
current_max = max(current_max, current_sum)
return current_max
def solution_3(values):
cumulative = numpy.zeros(len(values) + 1, dtype=int)
current_sum = 0
# This is equivalent to itertools.accumulate plus a leading zero
for i, v in enumerate(values):
current_sum += v
cumulative[i + 1] = current_sum
current_max = 0
for i, cumulative_i in enumerate(cumulative):
for j, cumulative_j in enumerate(cumulative[i:]):
current_sum = cumulative_j - cumulative_i
current_max = max(current_max, current_sum)
return current_max
def solution_4(values):
current_max = 0
max_ending_here = 0
for val in values:
max_ending_here = max(max_ending_here + val, 0)
current_max = max(current_max, max_ending_here)
return current_max
if __name__ == '__main__':
print(solution_1(values))