-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatString.py
More file actions
107 lines (90 loc) · 3.31 KB
/
Copy pathFormatString.py
File metadata and controls
107 lines (90 loc) · 3.31 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
# function to find if the given argument is real number ex: "50.32" or "-34.0" or "+2" is real numbers so this
# function returns a boolean value true else false.
def is_real_num(num: str):
for token in num:
if token.isdecimal():
return True
return False
# class to format the given expression to be parsed correctly by the InfixToPostfix parser
class FormatString:
def __init__(self, expr: str):
self.__expr = expr
def get_expr(self):
return self.__expr
def set_expr(self, expr: str):
self.__expr = expr
# function to remove space in the __expr attribute
def remove_space(self):
expr = list()
for token in self.__expr:
if not token.isspace():
expr.append(token)
self.__expr = expr
# function to join separated digits in the __expr attribute. (i.e) '1', '2' to '12'->twelve
def format_number(self):
expr = list()
whole_num = str()
for index, token in enumerate(self.__expr):
if token.isdecimal() or token == '.':
if (index + 1) == len(self.__expr):
whole_num += token
expr.append(whole_num)
whole_num = ""
continue
if self.__expr[index + 1].isdecimal() or self.__expr[index + 1] == '.':
whole_num += token
else:
whole_num += token
expr.append(whole_num)
whole_num = ""
else:
expr.append(token)
self.__expr = expr
# function to identify and convert integer as negative or positive integers
def to_int(self):
integer = str()
expr = list()
updated = False
unary_operators = ('-', '+')
for index, token in enumerate(self.__expr):
if index == 0:
if token in unary_operators and is_real_num(self.__expr[index + 1]):
integer += token + self.__expr[index + 1]
expr.append(integer)
integer = ''
updated = True
continue
else:
expr.append(token)
continue
if (index + 1) == len(self.__expr):
expr.append(token)
continue
if (token in unary_operators) and \
(not (is_real_num(self.__expr[index - 1]))) and \
(is_real_num(self.__expr[index + 1])):
integer += token + self.__expr[index + 1]
expr.append(integer)
integer = ''
updated = True
continue
if not updated:
expr.append(token)
updated = False
self.__expr = expr
# test code
if __name__ == '__main__':
e = input("Enter the expr: ")
is_real = is_real_num(e)
print("expr is real = ", is_real)
f = FormatString(e)
f.remove_space()
print("expr without space = ", f.get_expr())
f.set_expr(e)
print("assigned new expr = ", f.get_expr())
f.remove_space()
print("expr without space = ", f.get_expr())
f.format_number()
print("formatted expression = ", f.get_expr())
f.to_int()
print("final expr = ", f.get_expr())