-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-expression-processor.py
More file actions
90 lines (69 loc) · 2.83 KB
/
Copy pathstring-expression-processor.py
File metadata and controls
90 lines (69 loc) · 2.83 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
def process_operations(input_str):
input_str = input_str.replace('"', '')
while True:
mul_pos = input_str.find('*')
pow_pos = input_str.find('**')
add_pos = input_str.find('+')
op_pos = -1
op_type = None
candidates = []
if pow_pos != -1:
candidates.append((pow_pos, '**'))
if mul_pos != -1:
candidates.append((mul_pos, '*'))
if add_pos != -1:
candidates.append((add_pos, '+'))
if not candidates:
break
op_pos, op_type = min(candidates, key=lambda x: x[0])
left_part = input_str[:op_pos]
right_part = input_str[op_pos + len(op_type):]
if op_type == '+':
next_op_pos = len(right_part)
for op in ['+', '*', '**']:
pos = right_part.find(op)
if pos != -1 and pos < next_op_pos:
next_op_pos = pos
str2 = right_part[:next_op_pos]
remaining = right_part[next_op_pos:]
result = []
max_len = max(len(left_part), len(str2))
for i in range(max_len):
if i < len(left_part):
result.append(left_part[i])
if i < len(str2):
result.append(str2[i])
input_str = ''.join(result) + remaining
elif op_type == '*':
next_op_pos = len(right_part)
for op in ['+', '*', '**']:
pos = right_part.find(op)
if pos != -1 and pos < next_op_pos:
next_op_pos = pos
num_str = right_part[:next_op_pos]
remaining = right_part[next_op_pos:]
try:
num = int(num_str)
except ValueError:
num = 1
result = []
for char in left_part:
result.append(char * num)
input_str = ''.join(result) + remaining
elif op_type == '**':
next_op_pos = len(right_part)
for op in ['+', '*', '**']:
pos = right_part.find(op)
if pos != -1 and pos < next_op_pos:
next_op_pos = pos
num_str = right_part[:next_op_pos]
remaining = right_part[next_op_pos:]
try:
num = int(num_str)
except ValueError:
num = 1
result = left_part * num
input_str = result + remaining
return input_str
input_string = input().strip()
print(process_operations(input_string))