-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
154 lines (139 loc) · 4.25 KB
/
Copy pathutil.py
File metadata and controls
154 lines (139 loc) · 4.25 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
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
from math import *
from operator import *
from generator import *
from db import *
import random
import base64
operators = ['+', '-', '*', '/', '%', ')', '(', '^']
vars = ['x', 'y']
def sample_slope(x0, x1, y0, y1, x_step, y_step):
expr = random.choice(get_equations())
return (expr, generate_slopes(expr, x0, x1, y0, y1, x_step, y_step))
def random_slope(x0, x1, y0, y1, x_step, y_step):
expr = random_expr()
slopes = generate_slopes(expr, x0, x1, y0, y1, x_step, y_step, True)
slopes, interesting = slopes[1], slopes[0]
if not interesting:
return random_slope(x0, x1, y0, y1, x_step, y_step)
return (expr, generate_slopes(expr, x0, x1, y0, y1, x_step, y_step))
def frange(start, end, step):
while start < end:
yield start
start += step
def generate_slopes(expr, x0, x1, y0, y1, x_step, y_step, check_interesting=False):
expr = expr.replace('^', '**')
slope0 = None
interesting = False #checks whether all slopes are the same
slopes = []
for y in frange(y0, y1, y_step):
temp = []
for x in frange(x0, x1, x_step):
try:
slope = eval(expr)
if isinstance(slope, complex):
slope = "NaN"
temp.append(slope)
except ZeroDivisionError:
temp.append('inf')
except BaseException as e:
temp.append("NaN")
if slope0 is None:
slope0 = temp[-1]
if temp[-1] != slope0:
interesting = True
slopes.append(temp)
if check_interesting:
return (interesting, slopes)
return slopes
def format_expr(expr):
if is_float(expr):
return str(expr)
expr = expr.lower().replace(' ', '')
result = ''
current = ''
unclosed_parens = 0
while len(expr) > 0:
char = expr[0]
expr = expr[1:]
if current == '':
current = char
elif is_float(current) or current == '.' or current in ['pi', 'e']:
if (char.isdigit() or char == '.') and current not in ['pi', 'e']:
current += char
elif char in operators:
result += current
if unclosed_parens:
current += ')'
unclosed_parens -= 1
current = char
else:
result += current
if unclosed_parens:
current += ')'
unclosed_parens -= 1
result += '*'
current = char
if char == '(':
result += char
current = ''
elif current in operators:
result += current
current = char
if char == '(':
result += char
current = ''
elif current in vars:
if unclosed_parens:
current += ')'
unclosed_parens -= 1
if char in operators:
result += current
current = char
else:
result += current
result += '*'
current = char
if char == '(':
result += char
current = ''
else: #assume current is a function
if char == '(':
result += current
result += char
current = ''
elif char.isdigit() or char == '.' or char in vars:
result += current
result += '('
unclosed_parens += 1
current = char
else:
current += char
result += current
while unclosed_parens:
result += ')'
unclosed_parens -= 1
return result
def is_float(str):
try:
float(str)
return True
except ValueError:
return False
def str_to_float(str):
if is_float(str):
return float(str)
return str
def sec(x):
return 1/cos(x)
def csc(x):
return 1/sin(x)
def cot(x):
return 1/tan(x)
def ln(x):
return log(x)
def reciprocal(x):
return 1/x
def div(x, y):
return x/y
def check_password(encoded):
return str(base64.b64decode(encoded))[2: -1] == "cs61afall2018"