My solution uses itertools.product to compute possible variations of Ace counting.
itertools.product
from itertools import product def score(hand): royalty = ('J', 'Q', 'K',) ace = ('A',) aces = 0 _score = 0 for card in hand: if card in royalty: _score += 10 elif card not in ace: _score += int(card) else: aces += 1 if aces == 0: return _score aces_combinations = [] variations = [_score + sum(ace_values) for ace_values in product((1,11), repeat=aces)] variations_vs_delta = [abs(21 - _s) for _s in variations] return variations[variations_vs_delta.index(min(variations_vs_delta))] def bj_score(hand): s = score(hand) if s > 21: print 'Busted!' return s cases = [ ["A"], ["A", "J"], ["A", "10", "A"], ["5", "3", "7"], ["5", "4", "3", "2", "A", "K"] ] for case in cases: print(case) print(bj_score(case))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My solution uses
itertools.product
to compute possible variations of Ace counting.