📣 独立日限时特惠: VIP通行证立减$68
查看: 1384| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[高频题] 脸书求real-time avg time spent for each step

🔗
匿名用户-IRVCN  2021-1-26 17:41:32 |倒序浏览

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

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

x
先谢谢大家,求问一道脸书的python题。 写code 把stream 改成 real-time data 实时更新每步的平均时间。

input:[session, step, time_start] | data like [[a,1,110], [a,2,120], [a,3,150], [b,1,110],[b,2,190],[b,3,220]]
output: {1:45, 2:35}

我的理解是,点在于如何随时有新的[session, step, time_start] 进行实时更新output。使用extend() 新的input吗?求各位大神赐教

万分感谢

评分

参与人数 1大米 +1 收起 理由
Alexhui + 1 赞一个

查看全部评分


上一篇:刷题网和leetcode的区别是啥?
下一篇:新手请问大家是如何准备和现场做OA的?
🔗
14417335 2021-1-26 22:43:16 | 只看该作者
全局:
"抱歉,您不能对匿名帖评分"

如果希望大米还请不要匿名发帖。无法给米。
回复

使用道具 举报

🔗
BruceZhao 2021-1-27 01:11:52 | 只看该作者
全局:
Here is my first thought here, let me know if there is any doubts.

  1. outputDict = {} #{1:45, 2:35}
  2. sessionDict = {} #{'a':[1,110], 'b':[1,110]} keep the latest step of each session
  3. stepCountDict = {} #{1:2, 2:2}

  4. #input [a,1,110]
  5. def step_avg_time(session, step, time_start):
  6.         last_session = sessionDict.get(session)
  7.         #for new session, no change
  8.         if not last_session:
  9.                 sessionDict[session] = [step, time_start]
  10.                 return outputDict

  11.         time_spend = time_start - last_session[1]
  12.         last_step = last_session[0]
  13.         step_avg = outputDict.get(last_step, 0.0)
  14.         #update the average
  15.         step_avg = step_avg + (time_spend - step_avg) / (stepCountDict.get(last_step,0) + 1)
  16.         outputDict[last_step] = step_avg
  17.         #update the sessionDict
  18.         sessionDict[session] = [step, time_start]
  19.         #update stepCountDict
  20.         stepCountDict[last_step] = stepCountDict[last_step] + 1
  21.         return outputDict

  22. #call the function
  23. #inputList: [[a,1,110], [a,2,120], [a,3,150], [b,1,110],[b,2,190],[b,3,220]]
  24. for i in inputList:
  25.         print(step_avg_time(*i))
复制代码
回复

使用道具 举报

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

本版积分规则

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