-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.py
84 lines (57 loc) · 1.45 KB
/
test2.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
76
77
78
79
80
81
82
83
84
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'betterCompression' function below.
#
# The function is expected to return a STRING.
# The function accepts STRING s as parameter.
#
def betterCompression(s):
# Write your code here
array = []
temp = ''
temp += s[0]
for i in range(1, len(s)):
if s[i] >= 'a' and s[i] <= 'z':
array.append(temp)
temp = s[i]
else:
temp += s[i]
array.append(temp)
array.sort()
result = ''
temp = array[0]
new_str = ''
while len(array) > 0:
flag = 0
for i in range(1, len(array)):
if i >= len(array):
break
if temp[0] == array[i][0]:
flag = 1
new_str = temp[0]
new_str += str(int(temp[1:]) + int(array[i][1:]))
temp = new_str
array.pop(i)
else:
break
if flag == 0:
result += temp
array.pop(0)
if len(array) > 0:
temp = array[0]
new_str = ''
return result
if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')
# s = input()
# result = betterCompression(s)
# fptr.write(result + '\n')
# fptr.close()
s = 'a12c56a12c5a2b4a7'
result = betterCompression(s)
print(result + '\n')