-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReHandler.py
75 lines (65 loc) · 1.5 KB
/
ReHandler.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
#!/usr/bin/env python
import re
def get_coefficient(expr):
''' Return the leading coefficient of a string
>>> get_coefficient('23')
'23'
>>> get_coefficient('34z1')
'34'
>>> get_coefficient('2q2p91')
'2'
>>> get_coefficient('z3')
'1'
>>> get_coefficient('1')
'1'
'''
if isinstance(expr, int):
return str(expr)
coef = re.match('([0-9]*)(.*)', expr).groups()[0]
if coef == '':
return '1'
return coef
def get_variables(expr):
''' Return the variables in a string
>>> get_variables('z2')
['z2']
>>> get_variables('32')
[]
>>> get_variables('q23p7')
['q23', 'p7']
>>> get_variables('4qp9')
['q', 'p9']
'''
coef, variables = re.match('([0-9]*)(.*)', expr).groups(0)
variables = re.findall('([a-z][0-9]*)', variables)
return variables
def MatchZVariable(param):
pattern = re.compile('^z+')
m = pattern.match(str(param))
if m:
return param
return ""
def MatchProductWithNatural(params):
pattern = re.compile('^\d+\w+')
m = pattern.match(params)
if m:
pattern2 = re.compile('^\d+')
m2 = pattern2.match(params)
if m2:
return m2.end()
return ""
def CountLettersInString(param):
try:
y = len(re.findall('[a-z]', param))
except:
print param
return y
def IsInt(string1):
try:
x = int(string1)
return x
except ValueError:
return -1
if __name__ == "__main__":
import doctest
doctest.testmod()