Your question is Card Affordability Check. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
In a Brex rewards game simulation, each card has a color-specific gem cost. A player may pay a cost with gems of the same color or use wild gems to cover any remaining shortage. Write can_purchase() to determine whether the player can afford one card.
Use each gem at most once. Gems and costs are dictionaries mapping color names to nonnegative integers. The optional key "wild" represents gems that can substitute for any color. Missing color keys represent zero gems.
Implement can_purchase(gems, cost):
gems: a dictionary from color strings to available gem counts.cost: a dictionary from color strings to required gem counts.True if all required colored gems can be paid using matching gems plus available wild gems.False otherwise.A greedy strategy is sufficient: use matching gems first, then count the total remaining shortage and compare it with the wild-gem count.
def can_purchase(gems, cost):