Drill

ProblemsPython › games

Find a card after drawing

easygamesPython

A card game draws a number of cards from the top of a deck. Find which card ends up at a given position in the remaining stack.

card_position(deck: list<int>, draws: int, position: int) → int

Solve it in the editor →

Where you start

def card_position(deck: list[int], draws: int, position: int) -> int:
    

Worked examples

CallResult
card_position([10, 20, 30, 40, 50], 2, 1)40
card_position([1, 2, 3], 0, 0)1
card_position([1, 2, 3], 3, 0)-1
card_position([5, 6, 7, 8], 3, 0)8

Hint

The answer lives at index draws + position in the original deck, if that index is in bounds.

Reference solution in Python
def card_position(deck: list[int], draws: int, position: int) -> int:
    idx = draws + position
    return deck[idx] if idx < len(deck) else -1

The same problem in another language

More games problems in Python