-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.py
59 lines (52 loc) · 1.45 KB
/
D.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
import re
def convertSize(byte):
size = {'GB': 0, 'MB': 0, 'KB': 0, 'B': 0}
if byte >= 1073741824:
size['GB'] = byte // 1073741824
byte %= 1073741824
if byte >= 1048576:
size['MB'] = byte // 1048576
byte %= 1048576
if byte >= 1024:
size['KB'] = byte // 1024
byte %= 1024
size['B'] = byte
return size
def printSize(l):
size = convertSize(l)
ans = ''
# 正常情况下直接遍历字典即可,但我不太确定考试环境的3.8中的字典是否有序。3.9以后肯定是有序的了
if size['GB'] > 0:
ans += f'{size["GB"]}GB'
if size['MB'] > 0:
ans += f'{size["MB"]}MB'
if size['KB'] > 0:
ans += f'{size["KB"]}KB'
if size['B'] > 0:
ans += f'{size["B"]}B'
print(ans)
typs = {'int': 4, 'long': 8, 'String': 1}
n = int(input())
l = 0
for i in range(n):
op = input()
os = op.split(maxsplit=1) # 分割字符串
typ = os[0] # 取数据类型
tmp = os[1] # 取赋值式
if '[]' in typ:
tp = typ.replace('[]', '')
zkh = re.compile('\[\d+?\]')
ts = zkh.findall(tmp)
for j in ts:
lt = int(j[1:-1]) * typs[tp]
l += lt
else:
if typ == 'String':
sy = re.compile('”.+?”')
ts = sy.findall(tmp)
for j in ts:
l += len(j) - 2
else:
dh = tmp.count(',') + 1
l += typs[typ] * dh
printSize(l)