回复: 12
收起左侧

Flexport Token Game 4问全过完整代码!

|只看干货
本楼: 👍   66% (2)
 
 
33% (1)   👎
全局: 👍   96% (120)
 
 
4% (5)    👎

2023(1-3月) 码农类General 硕士 全职@flexport - 内推 - 技术电面  | 😐 Neutral 😐 AverageWaitList | 在职跳槽

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

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

x
各位注意理解discount。不是真的7折,5折。 而是每个discount可以抵扣一张对应颜色的token! 比如买了2张蓝色的卡,以后每次购买都可以重复抵扣2张蓝色token!
"""
https://images-na.ssl-images-ama ... 0v8qNysKL.SX522.jpg
                +-----+
Point value --> |2   G| <-- Card Color
                |     |
       Cost --> |5B   |
                |3G   |
                +-----+
Notation: Red - R, Blue - B, Black - K, Green - G, White - W
Player:
    the amount of tokens
    card_list
Card:
    point
    cost[(token_type, amount)]
    color
can_purchase(Player, Card): boolean
make_purchase(Player, Card): void
Q:
1. discount? free discount.
2. If I buy a blue card with 0 point, get a discount.
GoldTokens:
1.Card won't need gold tokens.
2.Use A
edge case:
1.Valid
"""
from collections import defaultdict
class Player:
    def __init__(self, token_dic):
        #dic['R'] = amount
        self.token_dic = token_dic
        self.card_list = []
        self.discount_dic = defaultdict(int)
class Card:
    def __init__(self, point, cost_token_dic, color):
        self.point = point
        self.cost_token_dic = cost_token_dic
        self.color = color
def can_purchase(player, card):
    #Iterate every token that the card needs
    gold_token_remain = player.token_dic['A']
    for token, amount in card.cost_token_dic.items():
        if player.token_dic[token] + player.discount_dic[token] + gold_token_remain < amount:
            return False
        if player.token_dic[token] + player.discount_dic[token] < amount:
            gold_token_remain -= (amount - player.token_dic[token] - player.discount_dic[token])
    retur
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
ot;
print(can_purchase(player, card))
make_purchase(player, card)
print(player.token_dic, player.card_list)
print(can_purchase(player, card))
cost_token_dic2 = {'R':2}
card2 = Card(0, cost_token_dic2, 'G')
print(can_purchase(player, card))
cost_token_dic3 = {'B':2}
card3 = Card(0, cost_token_dic3, 'G')
print(can_purchase(player, card))
cost_token_dic4 = {'G':2}
card4 = Card(0, cost_token_dic4, 'G')
print(can_purchase(player, card))
# discount_dic['G'] = 3
# discount_dic['B'] = 1
"""

评分

参与人数 6大米 +27 收起 理由
yukai + 1 很有用的信息!
清道神君 + 22
玉米种植专业户 + 1 给你点个赞!
FiFiFiFimon + 1 很有用的信息!
十分惊奇 + 1 给你点个赞!

查看全部评分


上一篇:狗23年2月新鲜店面
下一篇:求加米 UBS OA Back-end Java Expert Challenge
691220 2023-3-14 02:17:19 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   94% (569)
 
 
5% (30)    👎
你这个有个bug其实
如果amount < discount 你amount -= player.discount_dic[token]直接减下去会变成负数 然后下面player.token_dic[token] -= amount 会负负得正加token。。。你在下面减token的时候要判定 amount是不是大于0
回复

使用道具 举报

地里匿名用户
匿名用户-LLLKX  2023-2-8 11:51:57
本楼: 👍   0% (0)
 
 
0% (0)   👎
楼主你好~ 我想问一下如果make_purchase() 里 player.discount_dic[token] > amount 怎么办?还有如果最后 gold_token_remain用了还是不够买这张卡怎么办?感谢!
回复

使用道具 举报

sssphon 2023-3-5 08:46:07 | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   44% (88)
 
 
55% (108)    👎
有个小问题,player 的card_list 和discount_list是不是可以合并一下呢,就card_list换成dict这样?
回复

使用道具 举报

地里匿名用户
匿名用户-52UUD  2023-2-15 05:37:11
本楼: 👍   0% (0)
 
 
0% (0)   👎
感谢楼主,请问约上team match了嘛
回复

使用道具 举报

地里匿名用户
匿名用户-YJTSO  2023-2-22 11:54:06
本楼: 👍   0% (0)
 
 
0% (0)   👎
请问是用hackrank写code的吗?用hackrank写OOD和平时写coding有什么不同的地方吗?
回复

使用道具 举报

地里匿名用户
匿名用户-YJTSO  2023-2-22 14:38:06
本楼: 👍   0% (0)
 
 
0% (0)   👎
电面是什么时候的?
回复

使用道具 举报

地里匿名用户
匿名用户-YJTSO  2023-2-23 07:53:07
本楼: 👍   0% (0)
 
 
0% (0)   👎
请问写coding的时候可以用python写这题的ood吗?
回复

使用道具 举报

 楼主| ilovejunghyun2 2023-2-24 01:33:48 来自APP | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   96% (120)
 
 
4% (5)    👎
匿名用户 发表于 2023-02-07 19:51:57
楼主你好~ 我想问一下如果make_purchase() 里 player.discount_dic > amount 怎么办?还有如果最后gold_token_remain用了还是不够买这张卡怎么办
1.最多抵扣amount数量。
2.所以必须要先检查。这个我是和面试官确认清楚的。
回复

使用道具 举报

 楼主| ilovejunghyun2 2023-2-24 01:34:04 来自APP | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   96% (120)
 
 
4% (5)    👎
匿名用户 发表于 2023-02-14 13:37:11
感谢楼主,请问约上team match了嘛
约上了,下周
回复

使用道具 举报

 楼主| ilovejunghyun2 2023-2-24 01:34:34 来自APP | 显示全部楼层
本楼: 👍   0% (0)
 
 
0% (0)   👎
全局: 👍   96% (120)
 
 
4% (5)    👎
匿名用户 发表于 2023-02-22 15:53:07
请问写coding的时候可以用python写这题的ood吗?
这。。。我就是用的python呀?
回复

使用道具 举报

地里匿名用户
匿名用户-52UUD  2023-2-24 01:50:05 来自APP
本楼: 👍   0% (0)
 
 
0% (0)   👎
可以问下哪天vo什么时间约的吗? vo完两周都玩失踪了 谢谢楼主
回复

使用道具 举报

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

本版积分规则

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