-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscholarships.py
executable file
·97 lines (73 loc) · 2.56 KB
/
scholarships.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
from flask import Flask, jsonify, make_response
import numpy as np
scholarships = Flask(__name__)
# Our pseudo-database:
data = [] # Put lists in this list
"""
In case you're generating with np.random.randint:
data = data.tolist()
"""
max_scholarship = []
@scholarships.route('/', methods=['GET'])
def getData():
return jsonify({'data': data})
@scholarships.route('/max_scholarship', methods=['GET'])
def getMaxScholarship():
return jsonify(max_scholarship)
@scholarships.route('/max_scholarship', methods=['POST'])
def maxScholarship():
ms = MaxScholarship()
vals = ms.findGreatest(data, 11)
max_scholarship.append(vals)
return jsonify(max_scholarship), 201
@scholarships.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
class MaxScholarship(object):
def findGreatest(self, lists, k):
matrix = np.array(lists)
# Horizontal span
horizontal = self.spanLists(lists, k)
# Vertical span
vertMat = map(list, zip(*matrix)) # Vertical columns work as rows
vertical = self.spanLists(vertMat, k)
# L-diag, R-diag
diags = [matrix[::-1, :].diagonal(i) for i in range(-matrix.shape[0]+1, matrix.shape[1])]
diags.extend(matrix.diagonal(i) for i in range(matrix.shape[1]-1, -matrix.shape[0], -1))
diags = [n.tolist() for n in diags]
diagnol = self.spanLists(diags, k)
great = 0
for val in [horizontal, vertical, diagnol]:
if val['total'] > great:
great = val['total']
correct = val
else:
pass
return correct
def spanLists(self, lists, k):
"""Takes in matrix, finds k consecutive values that multiply to largest value"""
sequence = []
maxTotal = 0
tmp = 0
response = {}
for seq in lists:
if len(seq) >= k:
for i in range(len(seq)):
if (i+k-1) <= len(seq):
tmp = np.prod(seq[i:(i+k)])
if tmp > maxTotal:
maxTotal = tmp
del sequence[:]
sequence.append(seq[i:(i+k)])
else:
pass
else:
tmp = 0
else:
pass
print maxTotal
response['sequence'] = sequence[0]
response['total'] = maxTotal
return response
if __name__ == '__main__':
scholarships.run(debug=True)