0

I am a novice python coder and I am trying to write a card game for a test.

I would like to find the value of a card and the sum of all values inside the "deck" equal to the value of the card.

deck = [[7, 'Cups'], [2, 'Cups'], ['K', 'Swords'], ['Q', 'Clubs'], ['K', 'Cups'], [10, 'Clubs'], [9, 'Clubs'], [1, 'Swords']]
card = [10, 'Coins']

So python should return:

[[10, 'Clubs'], [[9, 'Clubs'], [1, 'Swords']], [[7, 'Cups'], [2, 'Cups'],[1, 'Swords']]]

Thanks everybody for your help.

gr2099
  • 3
  • 1
  • 2
    What have you tried so far? – votelessbubble Apr 12 '22 at 09:17
  • @votelessbubble First I saved the value of the card ``` cardtofind = player_1[0][0] ``` then I loop through the deck list ```for cards in deck: if cardtofind in cards: return cards ``` ... but i cannot figurate the sum part – gr2099 Apr 12 '22 at 09:22
  • If you are not interested in the suits, then I'd suggest you to start with numbers only, and only once you achieve it you should start adding complexity. Moreover, I'd replace `'J'`, `'Q'`, `'K'` with `11`, `12`, `13` – ALai Apr 12 '22 at 09:37
  • [https://stackoverflow.com/a/34517664/13541354](https://stackoverflow.com/a/34517664/13541354) this could be a great start – ALai Apr 12 '22 at 09:39
  • @ALai Thank you. I've tried this but it doesn't help because i need to preserve the card associated to the sum of values. My idea is to print out the cards in the deck whose sum is equal to the card in the hand. Something like ```You can pick [9, 'Clubs'], [1, 'Swords'] ```. Bear in mind that i've already wrote other parts of the code that so far work well. – gr2099 Apr 12 '22 at 09:43
  • Once you get the numbers, you can recover the card by iterating on it. If you edit your question to add anything you've done so far, it's easier for us to help you – ALai Apr 12 '22 at 09:46

1 Answers1

0

Bringing it all together, thanks to this answer, you should be able to achieve your result like this:

import itertools

# Inputs
deck = [[7, 'Cups'], [2, 'Cups'], ['K', 'Swords'], ['Q', 'Clubs'], ['K', 'Cups'], [10, 'Clubs'], [9, 'Clubs'], [1, 'Swords']]
card = [10, 'Coins']

# Replace figures
mapping = {'J': 11, 'Q': 12, 'K': 13}
deck = [[mapping.get(cc[0], cc[0]), cc[1]] for cc in deck]

# Fill target
target = card[0]

result = [seq for i in range(len(deck), 0, -1)
          for seq in itertools.combinations(deck, i)
          if sum(s[0] for s in seq) == target]

print(result)
# prints [([7, 'Cups'], [2, 'Cups'], [1, 'Swords']), ([9, 'Clubs'], [1, 'Swords']), ([10, 'Clubs'],)]
  • J, Q, and K are mapped to numbers (you may map them back if you wish).
  • Your target is the value of card, while you do not need numbers since you want to keep the suits too.
  • You should just update the list comprehension for your result by ensuring you are checking the values only -> thus, sum(seq) becomes sum(s[0] for s in seq) since each seq is a value-suit pair now.
ALai
  • 739
  • 9
  • 18
  • Thank you! as i said I'm a novice and I wasn't familiar with the mapping function. It seems to work. – gr2099 Apr 12 '22 at 14:54