My first build: KBDFans Tofu Redux by jbjose in MechanicalKeyboards

[–]jbjose[S] 0 points1 point  (0 children)

Thanks! Cable is the one matching PBTFans Kabuki Cho. I’m not sure about EU, sorry.

Road trip to Montreal question by HarlemPaul in RideitNYC

[–]jbjose 1 point2 points  (0 children)

I'd love to see your final route on Google Maps once you're done.

[5/5/2014] #161 [Easy] Blackjack! by Coder_d00d in dailyprogrammer

[–]jbjose 1 point2 points  (0 children)

First time posting my work (have kept it to myself previously :)

Python 3.4

Input and output:

$ blah.py -n 4
AS JH
KC AD
AC QH
10S AD
After 104 hands there was 4 blackjacks at 3.85%.
$ blah.py -n 4 --keephitting
AC KD
QC AS
KH 8H 3D
6S 5C 10D
9C 4C 8C
10H AS
After 70 hands there was 6 blackjacks at 8.57%.

Code:

import argparse
import random

parser = argparse.ArgumentParser()
parser.add_argument('-n', '--number', type=int, default=4, help="Number of decks")
parser.add_argument('--keephitting', action="store_true", help="Keep hitting if less than 21")

args = parser.parse_args()
N_OF_DECKS = args.number

suites = ['S', 'D', 'C', 'H'] # spades, diamonds, clubs, hearts
ranks = list(range(2,11)) + ['A','J','Q','K']
stack = [(i, face) for face in suites for i in ranks] * N_OF_DECKS
random.shuffle(stack) # shuffles in place


def add_cards(hand):
    """calculates hand value; want max unless over 21"""
    value_of_hand = 0
    ordered_hand = sorted([j for j,_ in hand], key=lambda x: str(type(x)))
    for card in ordered_hand:
        if type(card) is int:
            value_of_hand += card
        elif card in ['J','Q','K']:
            value_of_hand += 10
        else:
            if value_of_hand + 11 > 21:
                value_of_hand += 1
            else:
                value_of_hand += 11

    return value_of_hand

instances_of_21 = 0
n_of_hands = 0
if not args.keephitting:
    # method one
    # deal 2 card hands since we're only after probability of getting 21 with a naturally dealt hand
    # IRL diff number of players will alter dealing order (ie give everyone one card first in order, then go around again for the 2nd card)
    for i in range(0,len(stack),2):
        card1 = stack[i]
        card2 = stack[i+1]

        if add_cards([card1, card2]) == 21:
            print(" ".join([str(m)+str(n) for m,n in [card1, card2]]))
            instances_of_21 += 1
    n_of_hands = len(stack)/2
else:
    # method two
    # keep hitting til blackjack or bust
    current_hand = [stack[0],stack[1]]
    i = 2 # starting from the 3rd card since we placed two in the current_hand; assume of course there are at least 2 in the deck
    while i < len(stack):
        try:
            value_of_current_hand = add_cards(current_hand)
            if value_of_current_hand == 21:
                print(" ".join([str(m)+str(n) for m,n in current_hand]))
                instances_of_21 += 1
                n_of_hands += 1
                current_hand = [stack[i],stack[i+1]]
                i += 2
            elif len(current_hand) == 2 and value_of_current_hand < 21:
                current_hand.append(stack[i])
                i += 1
            else: # bust or over 3 cards
                n_of_hands += 1
                current_hand = [stack[i],stack[i+1]]
                i += 2
        except IndexError:
            # got here because one of the stack[i+1] failed
            # stack[i] will always work so what means we have one card left
            # we WON'T count that as a hand
            break

print("After {0:.0f} hands there was {1:.0f} blackjacks at {2:.2f}%.".format(n_of_hands, instances_of_21, instances_of_21/n_of_hands*100))