注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
从网上看到这题基本上给一字串, 字串里有两种匹配符iOS, Android+ID+频率 要回传平均频率的list
写了一下求批 求意见
One message system contains two device type message that each message is formatted with device type identifier with device id and message count. Write a function to parse the message based on device type and message count and return a list with each device id in even message frequency
rule: input string: message
device identifier
1:iOS device ID identifier: start with 'I' following with 3 character, total length is 4 character
2:Android device ID identifier: start with 'A' following with 2 character, total length is 3 character
3:message count is following by device id until next device ID
ex: input: Asq2: {'Asq': 2} Asq with 2 message count
output: ['Asq', 'Asq']
input: Akb2IAld3: ID: {'Akb': 2, 'IAld2': 3} Akb with 2 message count, IAld with 3 message count
output: ['Akb', 'IAld', 'Akb', 'IAld', 'IAld']
input: Aqp1Iasd10Aqp4IAbd1Iasd2: {'Aqp': 5, 'Iasd': 12, 'IAbd': 1}
output: ['Aqp', 'Iasd', 'IAbd', 'Aqp', 'Iasd', 'Aqp', 'Aqp', 'Aqp', 'Iasd', 'Iasd', 'Iasd', 'Iasd', 'Iasd', 'Iasd'..., 'Iasd']
- def parse_message(string) -> List:
- i, j, ids_map, n, ids = 0, 0, dict(), len(string), ''
- while i < n:
- if string[i] in ('I', 'A') or i == n - 1:
- if ids:
- if i == n - 1:
- ids_map[ids] = ids_map.get(ids, 0) + int(string[j:])
- else:
- ids_map[ids] = ids_map.get(ids, 0) + int(string[j:i])
- j = i + 4 if string[i] == 'I' else i + 3
- ids = string[i:j]
- i = j - 1
- i += 1
- res = []
- while any(i > 0 for i in ids_map.values()):
- for k, v in ids_map.items():
- if v > 0:
- res.append(k)
- ids_map[k] -= 1
- return res
复制代码
```
|