📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 1064| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[字符串] 从字串里找匹配符以相同频率回传匹配符

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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']

  1. def parse_message(string) -> List:
  2.     i, j, ids_map, n, ids = 0, 0, dict(), len(string), ''

  3.     while i < n:
  4.         if string[i] in ('I', 'A') or i == n - 1:
  5.             if ids:
  6.                 if i == n - 1:
  7.                     ids_map[ids] = ids_map.get(ids, 0) + int(string[j:])
  8.                 else:
  9.                     ids_map[ids] = ids_map.get(ids, 0) + int(string[j:i])
  10.             j = i + 4 if string[i] == 'I' else i + 3
  11.             ids = string[i:j]
  12.             i = j - 1
  13.         i += 1
  14.     res = []
  15.     while any(i > 0 for i in ids_map.values()):
  16.         for k, v in ids_map.items():
  17.             if v > 0:
  18.                 res.append(k)
  19.                 ids_map[k] -= 1
  20.     return res
复制代码

```

评分

参与人数 1大米 +10 收起 理由
14417335 + 10 很有用的信息!

查看全部评分


上一篇:[讨论讨论,加米加米] Leetcode 881 Boats to Save People 救生艇
下一篇:狗家 建水井
 楼主| imiochen24 2019-3-29 22:08:31 | 只看该作者
全局:
14417335 发表于 2019-3-29 20:25
请问“even message frequency”是如何定义的?

我猜测的解释应该是尽量不相连。比如

就是不同device id 会有不同messsage count. 不能相同device id一起输出like {idA: 200, idB:20} 要idA, idB, idA, idB...二十组, 之后才连续的idA 180 .. 因为已经没有idB了, 是也可以说成不相连除非其他id count为0了
回复

使用道具 举报

🔗
14417335 2019-3-29 20:25:47 | 只看该作者
全局:
请问“even message frequency”是如何定义的?

我猜测的解释应该是尽量不相连。比如
input: Aqp1Iasd10Aqp4IAbd1Iasd2: {'Aqp': 5, 'Iasd': 12, 'IAbd': 1}

输出的顺序应该是
Iasd Aqp Iasd Aqp Iasd Aqp Iasd Aqp Iasd Aqp Iasd IAbd Iasd Iasd Iasd Iasd Iasd Iasd

不知道理解对否。
回复

使用道具 举报

🔗
 楼主| imiochen24 2019-3-29 22:12:19 | 只看该作者
全局:
14417335 发表于 2019-3-29 20:25
请问“even message frequency”是如何定义的?

我猜测的解释应该是尽量不相连。比如

应该为
{'Aqp': 5, 'Iasd': 12, 'IAbd': 1}
Aqp, Iasd, IAbd, Aqp, Iasd, Aqp, Iasd, Aqp, Iasd, Aqp, Iasd, Iasd, Iasd, Iasd, Iasd, Iasd, Iasd, Iasd
因为有 1 IAbd
回复

使用道具 举报

🔗
337845818 2019-3-29 22:15:06 | 只看该作者
全局:
output再解释一下, 你自己写的代码能出你那结果吗..
回复

使用道具 举报

🔗
 楼主| imiochen24 2019-3-30 01:10:14 | 只看该作者
全局:
337845818 发表于 2019-3-29 22:15
output再解释一下, 你自己写的代码能出你那结果吗..

可以, 从input里会得到 一个 dictionary, key 为id, value为id到下一个id所有的次数output 就是每次把dictionary 里所有的key loop一次加到return list里将value减ㄧ一直到所有key的value为0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表