-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiff_Solution.py.m4
More file actions
41 lines (35 loc) · 1.08 KB
/
Copy pathdiff_Solution.py.m4
File metadata and controls
41 lines (35 loc) · 1.08 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
diff --git a/CodeJam/Solution.py.m4 b/examples/template_tests/golden_section_search/int/Solution.py.m4
index ecd70df..104fce4 100644
--- a/CodeJam/Solution.py.m4
+++ b/examples/template_tests/golden_section_search/int/Solution.py.m4
@@ -460,12 +460,35 @@ def init():
def readInput():
- global result
+ global result, t, N, params
+ t = cin()
+ N = cin()
+ params = cin(N)
+
+
+def f(x):
+ if t == "polynom":
+ r = params[0]
+ for p in params[1:]:
+ r *= x
+ r += p
+ return r
+ if t == "linear":
+ assert N % 2 == 0
+ xs, ys = params[: N // 2], params[N // 2 :]
+ assert xs[0] <= x <= xs[-1]
+ for i in range(N // 2 - 1):
+ if xs[i] <= x <= xs[i + 1]:
+ pi = (xs[i + 1] - x) / (xs[i + 1] - xs[i])
+ return pi * ys[i] + (1 - pi) * ys[i + 1]
# write to result
def calcFunction():
global result
+ a, fa = gss(f, -100, 100, tol=1)
+ b, fb = gss(f, -100, 100, tol=1, upper=True)
+ result = "%d %d %d %d" % (a, b, fa, fb)
if __name__ == "__main__":