注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
还是那道扑克宝石的题,三个问题也如:
- We want to write a function can_purchase() such that, given a particular card and collection of gems for a player, we return true if the player can afford the card, and false if they cannot.
- We w should be able to purchase it for 1 G, 1 R, and 1 B.
1和2我是在如下代码上的修改:- def helper(card: Dict[str, int], player_tokens: Dict[str, int]) -> bool:
- for token in player_tokens.keys():
- if token not in card:
- return False
- card[token] -= player_tokens[token]
- for token in card.keys():
- if card[token] > 0:
- return False
- return True
- def can_afford(cards: Dict[str, Dict[str, int]],
- player_tokens: Dict[str, int]) -> List[bool]:
- result: List[bool] = list()
- for name in cards:
- card: Dict[str, int] = cards[name]
- result.append(helper(card, player_tokens))
- return result
复制代码 第3问我是用了Dict[str, List[Dict[str, int]]]来保存获得的扑克。不知为什么挂了,请大家讨论。
|