-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_complete.py
155 lines (118 loc) · 3.6 KB
/
sample_complete.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import math
import traceback
def print_term(degree, factor):
f, d = factor, degree
term = ""
if d == "exp(x)":
return str(factor) + "exp(x)"
elif d == "sin(x)":
return str(factor) + "sin(x)"
elif d == "cos(x)":
return str(factor) + "cos(x)"
if f < 0:
term += "-"
if not (abs(f) == 1 and d != 0):
term += str(abs(f))
if d != 0:
term += "x"
if d != 0 and d != 1:
term += "^" + str(d)
return term
def print_equation(terms):
term_str = []
for d, f in terms.items():
term_str.append(print_term(d, f))
return " + ".join(term_str)
def parse_term(term_str):
if term_str.endswith('(x)'):
d = term_str[-6:]
f_str = term_str[:-6]
if f_str == "-" or f_str == "":
f_str += "1"
return d, float(f_str)
elif "x" in term_str:
f_str, d_str = term_str.split("x")
if f_str == "-" or f_str == "":
f_str += "1"
f = float(f_str)
d = float(d_str[1:]) if d_str else 1.0
return d, f
else:
return 0.0, float(term_str)
def parse_equation(equation):
terms = {}
for term_str in equation.split(" + "):
d, f = parse_term(term_str)
terms[d] = terms.get(d, 0) + f
return terms
def d_dx_as_terms(terms):
derivative = {}
for d, f in terms.items():
if d == "sin(x)":
derivative["cos(x)"] = derivative.get("cos(x)", 0) + f
elif d == "cos(x)":
derivative["sin(x)"] = derivative.get("sin(x)", 0) - f
elif d == "exp(x)":
derivative["exp(x)"] = derivative.get("exp(x)", 0) + f
elif d != 0:
derivative[d - 1] = derivative.get(d - 1, 0) + f * d
if not len(derivative):
derivative[0] = 0
return derivative
def d_dx(equation):
terms = parse_equation(equation)
terms = d_dx_as_terms(terms)
return print_equation(terms)
def integral_as_terms(terms, constant):
result = {0: constant}
for d, f in terms.items():
if d == "sin(x)":
result["cos(x)"] = result.get("cos(x)", 0) - f
elif d == "cos(x)":
result["sin(x)"] = result.get("sin(x)", 0) + f
elif d == "exp(x)":
result["exp(x)"] = result.get("exp(x)", 0) + f
else:
result[d + 1] = result.get(d + 1, 0) + f / (d + 1)
return result
def integral(equation, constant):
terms = parse_equation(equation)
terms = integral_as_terms(terms, constant)
return print_equation(terms)
def compute_as_terms(terms, x):
y = 0
for d, f in terms.items():
if d == "sin(x)":
y += f * math.sin(x)
elif d == "cos(x)":
y += f * math.cos(x)
elif d == "exp(x)":
y += f * math.exp(x)
else:
y += f * (x ** d)
return y
def compute(equation, x):
terms = parse_equation(equation)
y = compute_as_terms(terms, x)
return str(y)
def solve_query(line):
try:
tokens = line.split(',')
command = tokens[0]
if command == 'D':
return d_dx(tokens[1])
elif command == 'I':
return integral(tokens[1], float(tokens[2]))
elif command == 'C':
return compute(tokens[1], float(tokens[2]))
except:
traceback.print_exc()
return ''
def solve(input_path, output_path):
input_f = open(input_path)
output_f = open(output_path, 'w')
for line in input_f:
line = solve_query(line.strip())
output_f.write(line + '\n')
input_f.close()
output_f.close()