forked from alexysxeightn/MADE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskA.py
59 lines (48 loc) · 1.44 KB
/
taskA.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
def is_digit(c):
return 0 <= ord(c) - ord('0') <= 9
def is_number(s):
if '0' == s:
return True
if is_digit(s[0]) and ord(s[0]) != ord('0'):
for symbol in s[1:]:
if not is_digit(symbol):
return False
else:
return False
return True
class Token:
def __init__(self, token):
self.type = None
self.subtype = None
self.value = token
if token in '+-':
self.type = 'sum'
self.subtype = 'plus' if '+' == token else 'minus'
elif '*' == token:
self.type = 'product'
self.subtype = 'multiplication'
elif token in '()':
self.type = 'bracket'
self.subtype = 'left_bracket' if '(' == token else 'right_bracket'
elif '.' == token:
self.type = 'end_token'
elif is_number(token):
self.type, self.value = 'number', int(token)
else:
self.type = 'unknown'
class Lexer:
def __init__(self, s):
self.tokens = []
num_str = ''
for symbol in s:
if is_digit(symbol):
num_str += symbol
else:
if num_str:
self.tokens.append(Token(num_str))
num_str = ''
self.tokens.append(Token(symbol))
s = input()
lexer = Lexer(s)
for token in lexer.tokens[:-1]:
print(token.value)