<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 627,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import itertools\n",
    "import random\n",
    "import numpy\n",
    "\n",
    "ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']\n",
    "suits = ['♣', '♦', '♥', '♠']\n",
    "deck = ['{}{}'.format(i[0],i[1]) for i in list(itertools.product(ranks, suits))]\n",
    "\n",
    "ranks_weight = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7,  '8':8,  '9':9,  'T':10,  'J':11,  'Q':12,  'K':13, 'A':14}\n",
    "suits_weight = {'♣':1, '♦':2, '♥':3, '♠':4}\n",
    "\n",
    "def get_sorted_rank(H):\n",
    "    return sorted(H, key=lambda k: ranks_weight[k[0]], reverse=True)\n",
    "\n",
    "def get_sorted_suit(H):\n",
    "    return sorted(H, key=lambda k: suits_weight[k[1]], reverse=True)\n",
    "\n",
    "def highest_rank(H):\n",
    "    return H[:1], H[1:]\n",
    "\n",
    "def highest_pair(H):\n",
    "    r = [],H\n",
    "    for i in range(len(H)-1):\n",
    "        if H[i][0]==H[i+1][0]:\n",
    "            r = H[i:i+2],H[0:i]+H[i+2:]\n",
    "            break\n",
    "    return r\n",
    "\n",
    "def highest_2_pairs(H):\n",
    "    r = [],H\n",
    "    a,b = highest_pair(H[:])\n",
    "    c,d = highest_pair(b[:]) if a else (False, None)\n",
    "    if c: r = a+c,d\n",
    "    return r\n",
    "\n",
    "def highest_three(H):\n",
    "    r = [],H\n",
    "    for i in range(len(H)-2):\n",
    "        if H[i][0]==H[i+1][0]==H[i+2][0]:\n",
    "            r = H[i:i+3],H[0:i]+H[i+3:]\n",
    "            break\n",
    "    return r\n",
    "\n",
    "def highest_straight(H):\n",
    "    r = [],[i for i in H]\n",
    "    v0 = 6 if H[0][0]=='A' else 0\n",
    "    U,D = [],[]\n",
    "    last = H.pop(0)\n",
    "    U.append(last)\n",
    "    for i in range(len(H)):\n",
    "        new = H.pop(0)\n",
    "        if last[0]==new[0]:\n",
    "            D.append(new)\n",
    "        else:\n",
    "            U.append(new)\n",
    "            last = new\n",
    "    v1=v2=v3=v4=v5=-1\n",
    "    for i in range(len(U)-4):\n",
    "        v1 = ranks_weight[U[i][0]]\n",
    "        v2 = ranks_weight[U[i+1][0]]+1\n",
    "        v3 = ranks_weight[U[i+2][0]]+2\n",
    "        v4 = ranks_weight[U[i+3][0]]+3\n",
    "        v5 = ranks_weight[U[i+4][0]]+4\n",
    "        if v1==v2==v3==v4==v5:\n",
    "            r = U[i:i+5],get_sorted_rank(U[i+5:]+U[0:i]+D)\n",
    "            break\n",
    "    if v0==v2==v3==v4==v5:\n",
    "        r = U[-4:]+U[0:1],get_sorted_rank(D+U[1:-4])\n",
    "\n",
    "    return r\n",
    "\n",
    "def highest_flush(H):\n",
    "    H = get_sorted_suit(H[:])\n",
    "    r = [],H\n",
    "    for i in range(len(H)-4):\n",
    "        if H[i][1]==H[i+1][1]==H[i+2][1]==H[i+3][1]==H[i+4][1]:\n",
    "            r = get_sorted_rank(H[i:i+5]), get_sorted_rank(H[0:i]+H[i+5:])\n",
    "            break\n",
    "    return r\n",
    "\n",
    "def highest_full_house(H):\n",
    "    r = [],H\n",
    "    a,b = highest_three(H[:])\n",
    "    c,d = highest_pair(b[:]) if a else (False, None)\n",
    "    if c: r = a+c,d\n",
    "    return r\n",
    "\n",
    "def highest_four(H):\n",
    "    r = [],H\n",
    "    for i in range(len(H)-3):\n",
    "        if H[i][0]==H[i+1][0]==H[i+2][0]==H[i+3][0]:\n",
    "            r = H[i:i+4],H[0:i]+H[i+4:]\n",
    "            break\n",
    "    return r\n",
    "\n",
    "\n",
    "def _highest_count_suit(H):\n",
    "    S = {}\n",
    "    for k,v in suits_weight.items():\n",
    "        S[v] = []\n",
    "    for i in range(len(H)):\n",
    "        new = H.pop(0)\n",
    "        S[suits_weight[new[1]]].append(new)\n",
    "    m = -1\n",
    "    l = -1\n",
    "    for k,v in suits_weight.items():\n",
    "        x = len(S[v])\n",
    "        if x>m: \n",
    "            m=x\n",
    "            l=v\n",
    "    r1,r2 =[],[]\n",
    "    for k,v in suits_weight.items():\n",
    "        if v==l:\n",
    "            r1 = r1 + S[v]\n",
    "        else:\n",
    "            r2 = r2 + S[v]\n",
    "    return get_sorted_rank(r1),get_sorted_rank(r2)\n",
    "\n",
    "def highest_straight_flush(H):\n",
    "    r = [],H\n",
    "    a,b = _highest_count_suit(H[:])\n",
    "    c,d = highest_straight(a[:]) if len(a)>=5 else (False, None)\n",
    "    if c: r = c,get_sorted_rank(b+d)\n",
    "    return r\n",
    "\n",
    "def highest_royal_straight_flush(H):\n",
    "    r = [],H\n",
    "    a,b = highest_straight_flush(H[:])\n",
    "    if a and a[0][0]=='A': r = a,b\n",
    "    return r\n",
    "\n",
    "hand_names = {\n",
    "    1: 'highest_rank',\n",
    "    2: 'highest_pair',\n",
    "    3: 'highest_2_pairs',\n",
    "    4: 'highest_three',\n",
    "    5: 'highest_straight',\n",
    "    6: 'highest_flush',\n",
    "    7: 'highest_full_house',\n",
    "    8: 'highest_four',\n",
    "    9: 'highest_straight_flush',\n",
    "    10:'highest_royal_straight_flush'\n",
    "}\n",
    "\n",
    "def check_hand(t, h=[], check=False):\n",
    "    H = h+t\n",
    "    hand = get_sorted_rank(H)\n",
    "    ranking = {\n",
    "        10:highest_royal_straight_flush,\n",
    "        9: highest_straight_flush,\n",
    "        8: highest_four,\n",
    "        7: highest_full_house,\n",
    "        6: highest_flush,\n",
    "        5: highest_straight,\n",
    "        4: highest_three,\n",
    "        3: highest_2_pairs,\n",
    "        2: highest_pair,\n",
    "        1: highest_rank\n",
    "    }\n",
    "    \n",
    "    rank, best_hand = 0, []\n",
    "    \n",
    "    for i in range(10,0,-1):\n",
    "        h,r = ranking[i](hand[:])\n",
    "        if h:\n",
    "            rank, best_hand = i, h\n",
    "            break\n",
    "            \n",
    "    tie = [ranks_weight[u[0]] for u in best_hand]\n",
    " \n",
    "    return [rank]+tie, best_hand, check\n",
    "\n",
    "def check_table(table, hand_list):\n",
    "    results = []\n",
    "    for i in range(len(hand_list)):\n",
    "        hand = hand_list[i]\n",
    "        results.append(check_hand(table, hand, i==0))\n",
    "        \n",
    "    results = sorted(results, key=lambda k: k[0]+[k[2]], reverse=True)\n",
    "    won = results[0][2]\n",
    "    tie = results[0][0]==results[1][0]\n",
    "    \n",
    "    return won, tie, results\n",
    "\n",
    "def check_random(d, N=2):\n",
    "    H=2\n",
    "    T=5\n",
    "    sample = random.sample(d, T + H*N)\n",
    "    game = sample[:T],[sample[T+i*H:T+(i+1)*H] for i in range(N)]\n",
    "    won, tie, result = check_table(game[0], game[1])\n",
    "    result = sorted(result, key=lambda k: k[0]+[k[2]], reverse=True)\n",
    "    return won, tie, result, game\n",
    "\n",
    "def check_hand_random(d, h, t=[], N_PLAYERS=2):\n",
    "    N=N_PLAYERS-1\n",
    "    H=2\n",
    "    T=5-len(t)\n",
    "    deck_copy = d.copy()\n",
    "    for card in h+t:\n",
    "        deck_copy.remove(card)\n",
    "    sample = random.sample(deck_copy, T+H*N)\n",
    "    t = t + sample[:T]\n",
    "    opponent_hands = [sample[T+i*H:T+(i+1)*H] for i in range(N)]\n",
    "    won, tie, result = check_table(t, [h]+opponent_hands)\n",
    "    return won, tie, result, [t, [h]+opponent_hands]\n",
    "\n",
    "def check_hand_random_n(d, h, t=[], n=100, N_PLAYERS=2):\n",
    "    N = N_PLAYERS-1\n",
    "    H=2\n",
    "    T=5-len(t)\n",
    "    deck_copy = d.copy()\n",
    "    for card in h+t:\n",
    "        deck_copy.remove(card)\n",
    "    dict_turns = {'won':0.0, 'tie':0.0, 'lost':0.0}\n",
    "    step = 1/n\n",
    "    for i in range(n):\n",
    "        sample = random.sample(deck_copy, T+H*N)\n",
    "        tf = t + sample[:T]\n",
    "        opponent_hands = [sample[T+i*H:T+(i+1)*H] for i in range(N)]\n",
    "        won, tie, result = check_table(tf, [h]+opponent_hands)\n",
    "        if not won:\n",
    "            dict_turns['lost'] += step\n",
    "        elif won and not tie:\n",
    "            dict_turns['won'] += step\n",
    "        elif won and tie:\n",
    "            dict_turns['tie'] += step\n",
    "            \n",
    "    for key in dict_turns.keys():\n",
    "        dict_turns[key] = round(dict_turns[key], 3)\n",
    "        \n",
    "    return dict_turns\n",
    "\n",
    "def gen_game(N=2, H=2, T=5):\n",
    "    ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']\n",
    "    suits = ['♣', '♦', '♥', '♠']\n",
    "    deck = ['{}{}'.format(i[0],i[1]) for i in list(itertools.product(ranks, suits))]\n",
    "    sample = random.sample(d, T + H*N)\n",
    "    return sample[:T],[sample[T+i*H:T+(i+1)*H] for i in range(N)]\n",
    "\n",
    "def run_turn(N=2, H=2, T=5, show=3, n=10000):\n",
    "    t,hands = gen_game(N, H, T)\n",
    "    print(get_sorted_rank(t[:show]),hands[0])\n",
    "    r = float(input('prob: '))\n",
    "    s = check_table(t, hands)\n",
    "    state = 'lost'\n",
    "    if s[0] and not s[1]:\n",
    "        state='won'\n",
    "    elif s[0] and s[1]:\n",
    "        state='tie'\n",
    "    print('state: ', state)\n",
    "    print('table: ', get_sorted_rank(t))\n",
    "    for i in range(len(s[2])):\n",
    "        print('P{}{}: '.format(i,'*' if s[2][i][2] else ''), s[2][i][1])\n",
    "\n",
    "    p = check_hand_random_n(d, hands[0],t[:show], n, N)\n",
    "    print(p)\n",
    "    print('expected: ', round(N*p['won']+0.5*N*p['tie']-p['lost'], 3))\n",
    "    print()\n",
    "    return r-p['won']-0.5*p['tie']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 628,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Q♦', '9♠', '3♠'] ['7♠', 'J♣']\n",
      "prob: 10\n",
      "state:  lost\n",
      "table:  ['Q♦', '9♠', '6♠', '4♠', '3♠']\n",
      "P0:  ['9♠', '8♠', '6♠', '4♠', '3♠']\n",
      "P1*:  ['9♠', '7♠', '6♠', '4♠', '3♠']\n",
      "P2:  ['6♦', '6♠', '4♥', '4♠']\n",
      "P3:  ['K♦']\n",
      "{'won': 0.086, 'lost': 0.837, 'tie': 0.077}\n",
      "expected:  -0.339\n",
      "\n",
      "finish: \n",
      "['K♦', 'J♦', '2♦'] ['K♣', 'A♥']\n",
      "prob: 40\n",
      "state:  won\n",
      "table:  ['K♦', 'K♠', 'J♦', '6♠', '2♦']\n",
      "P0*:  ['K♣', 'K♦', 'K♠']\n",
      "P1:  ['K♦', 'K♠', 'J♠', 'J♦']\n",
      "P2:  ['K♦', 'K♠', 'T♥', 'T♦']\n",
      "P3:  ['K♦', 'K♠']\n",
      "{'won': 0.353, 'lost': 0.55, 'tie': 0.097}\n",
      "expected:  1.056\n",
      "\n",
      "finish: 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(24.739999999999998, 14.859999999999999)"
      ]
     },
     "execution_count": 628,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = []\n",
    "for i in range(100):\n",
    "    l.append(run_turn(4))\n",
    "    x = input('finish: ')\n",
    "    if x: break\n",
    "round(numpy.mean(l),2),round(numpy.std(l),2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "txt = \"\"\"\n",
    "1suit 45 %\n",
    "\n",
    "2samesuit 6 %\n",
    "\n",
    "1rank - 4.2% each in deck\n",
    "    1specific 4.2 %\n",
    "\n",
    "2rank - 0.37% each in deck\n",
    "    2specific 0.1 %\n",<commit_msg>Updates<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 634,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import itertools\n",
    "import random\n",
    "import numpy\n",
    "\n",
    "ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']\n",
    "suits = ['♣', '♦', '♥', '♠']\n",
    "deck = ['{}{}'.format(i[0],i[1]) for i in list(itertools.product(ranks, suits))]\n",
    "\n",
    "ranks_weight = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7,  '8':8,  '9':9,  'T':10,  'J':11,  'Q':12,  'K':13, 'A':14}\n",
    "suits_weight = {'♣':1, '♦':2, '♥':3, '♠':4}\n",
    "\n",
    "def get_sorted_rank(H):\n",
    "    return sorted(H, key=lambda k: ranks_weight[k[0]], reverse=True)\n",
    "\n",
    "def get_sorted_suit(H):\n",
    "    return sorted(H, key=lambda k: suits_weight[k[1]], reverse=True)\n",
    "\n",
    "def highest_rank(H):\n",
    "    return H[:1], H[1:]\n",
    "\n",
    "def highest_pair(H):\n",
    "    r = [],H\n",
    "    for i in range(len(H)-1):\n",
    "        if H[i][0]==H[i+1][0]:\n",
    "            r = H[i:i+2],H[0:i]+H[i+2:]\n",
    "            break\n",
    "    return r\n",
    "\n",
    "def highest_2_pairs(H):\n",
    "    r = [],H\n",
    "    a,b = highest_pair(H[:])\n",
    "    c,d = highest_pair(b[:]) if a else (False, None)\n",
    "    if c: r = a+c,d\n",
    "    return r\n",
    "\n",
    "def highest_three(H):\n",
    "    r = [],H\n",
    "    for i in range(len(H)-2):\n",
    "        if H[i][0]==H[i+1][0]==H[i+2][0]:\n",
    "            r = H[i:i+3],H[0:i]+H[i+3:]\n",
    "            break\n",
    "    return r\n",
    "\n",
    "def highest_straight(H):\n",
    "    r = [],[i for i in H]\n",
    "    v0 = 6 if H[0][0]=='A' else 0\n",
    "    U,D = [],[]\n",
    "    last = H.pop(0)\n",
    "    U.append(last)\n",
    "    for i in range(len(H)):\n",
    "        new = H.pop(0)\n",
    "        if last[0]==new[0]:\n",
    "            D.append(new)\n",
    "        else:\n",
    "            U.append(new)\n",
    "            last = new\n",
    "    v1=v2=v3=v4=v5=-1\n",
    "    for i in range(len(U)-4):\n",
    "        v1 = ranks_weight[U[i][0]]\n",
    "        v2 = ranks_weight[U[i+1][0]]+1\n",
    "        v3 = ranks_weight[U[i+2][0]]+2\n",
    "        v4 = ranks_weight[U[i+3][0]]+3\n",
    "        v5 = ranks_weight[U[i+4][0]]+4\n",
    "        if v1==v2==v3==v4==v5:\n",
    "            r = U[i:i+5],get_sorted_rank(U[i+5:]+U[0:i]+D)\n",
    "            break\n",
    "    if v0==v2==v3==v4==v5:\n",
    "        r = U[-4:]+U[0:1],get_sorted_rank(D+U[1:-4])\n",
    "\n",
    "    return r\n",
    "\n",
    "def highest_flush(H):\n",
    "    H = get_sorted_suit(H[:])\n",
    "    r = [],H\n",
    "    for i in range(len(H)-4):\n",
    "        if H[i][1]==H[i+1][1]==H[i+2][1]==H[i+3][1]==H[i+4][1]:\n",
    "            r = get_sorted_rank(H[i:i+5]), get_sorted_rank(H[0:i]+H[i+5:])\n",
    "            break\n",
    "    return r\n",
    "\n",
    "def highest_full_house(H):\n",
    "    r = [],H\n",
    "    a,b = highest_three(H[:])\n",
    "    c,d = highest_pair(b[:]) if a else (False, None)\n",
    "    if c: r = a+c,d\n",
    "    return r\n",
    "\n",
    "def highest_four(H):\n",
    "    r = [],H\n",
    "    for i in range(len(H)-3):\n",
    "        if H[i][0]==H[i+1][0]==H[i+2][0]==H[i+3][0]:\n",
    "            r = H[i:i+4],H[0:i]+H[i+4:]\n",
    "            break\n",
    "    return r\n",
    "\n",
    "\n",
    "def _highest_count_suit(H):\n",
    "    S = {}\n",
    "    for k,v in suits_weight.items():\n",
    "        S[v] = []\n",
    "    for i in range(len(H)):\n",
    "        new = H.pop(0)\n",
    "        S[suits_weight[new[1]]].append(new)\n",
    "    m = -1\n",
    "    l = -1\n",
    "    for k,v in suits_weight.items():\n",
    "        x = len(S[v])\n",
    "        if x>m: \n",
    "            m=x\n",
    "            l=v\n",
    "    r1,r2 =[],[]\n",
    "    for k,v in suits_weight.items():\n",
    "        if v==l:\n",
    "            r1 = r1 + S[v]\n",
    "        else:\n",
    "            r2 = r2 + S[v]\n",
    "    return get_sorted_rank(r1),get_sorted_rank(r2)\n",
    "\n",
    "def highest_straight_flush(H):\n",
    "    r = [],H\n",
    "    a,b = _highest_count_suit(H[:])\n",
    "    c,d = highest_straight(a[:]) if len(a)>=5 else (False, None)\n",
    "    if c: r = c,get_sorted_rank(b+d)\n",
    "    return r\n",
    "\n",
    "def highest_royal_straight_flush(H):\n",
    "    r = [],H\n",
    "    a,b = highest_straight_flush(H[:])\n",
    "    if a and a[0][0]=='A': r = a,b\n",
    "    return r\n",
    "\n",
    "hand_names = {\n",
    "    1: 'highest_rank',\n",
    "    2: 'highest_pair',\n",
    "    3: 'highest_2_pairs',\n",
    "    4: 'highest_three',\n",
    "    5: 'highest_straight',\n",
    "    6: 'highest_flush',\n",
    "    7: 'highest_full_house',\n",
    "    8: 'highest_four',\n",
    "    9: 'highest_straight_flush',\n",
    "    10:'highest_royal_straight_flush'\n",
    "}\n",
    "\n",
    "def check_hand(t, h=[], check=False):\n",
    "    H = h+t\n",
    "    hand = get_sorted_rank(H)\n",
    "    ranking = {\n",
    "        10:highest_royal_straight_flush,\n",
    "        9: highest_straight_flush,\n",
    "        8: highest_four,\n",
    "        7: highest_full_house,\n",
    "        6: highest_flush,\n",
    "        5: highest_straight,\n",
    "        4: highest_three,\n",
    "        3: highest_2_pairs,\n",
    "        2: highest_pair,\n",
    "        1: highest_rank\n",
    "    }\n",
    "    \n",
    "    rank, best_hand = 0, []\n",
    "    \n",
    "    for i in range(10,0,-1):\n",
    "        h,r = ranking[i](hand[:])\n",
    "        if h:\n",
    "            rank, best_hand = i, h\n",
    "            break\n",
    "            \n",
    "    tie = [ranks_weight[u[0]] for u in best_hand]\n",
    " \n",
    "    return [rank]+tie, best_hand, check\n",
    "\n",
    "def check_table(table, hand_list):\n",
    "    results = []\n",
    "    for i in range(len(hand_list)):\n",
    "        hand = hand_list[i]\n",
    "        results.append(check_hand(table, hand, i==0))\n",
    "        \n",
    "    results = sorted(results, key=lambda k: k[0]+[k[2]], reverse=True)\n",
    "    won = results[0][2]\n",
    "    tie = results[0][0]==results[1][0]\n",
    "    \n",
    "    return won, tie, results\n",
    "\n",
    "def check_random(d, N=2):\n",
    "    H=2\n",
    "    T=5\n",
    "    sample = random.sample(d, T + H*N)\n",
    "    game = sample[:T],[sample[T+i*H:T+(i+1)*H] for i in range(N)]\n",
    "    won, tie, result = check_table(game[0], game[1])\n",
    "    result = sorted(result, key=lambda k: k[0]+[k[2]], reverse=True)\n",
    "    return won, tie, result, game\n",
    "\n",
    "def check_hand_random(d, h, t=[], N_PLAYERS=2):\n",
    "    N=N_PLAYERS-1\n",
    "    H=2\n",
    "    T=5-len(t)\n",
    "    deck_copy = d.copy()\n",
    "    for card in h+t:\n",
    "        deck_copy.remove(card)\n",
    "    sample = random.sample(deck_copy, T+H*N)\n",
    "    t = t + sample[:T]\n",
    "    opponent_hands = [sample[T+i*H:T+(i+1)*H] for i in range(N)]\n",
    "    won, tie, result = check_table(t, [h]+opponent_hands)\n",
    "    return won, tie, result, [t, [h]+opponent_hands]\n",
    "\n",
    "def check_hand_random_n(d, h, t=[], n=100, N_PLAYERS=2):\n",
    "    N = N_PLAYERS-1\n",
    "    H=2\n",
    "    T=5-len(t)\n",
    "    deck_copy = d.copy()\n",
    "    for card in h+t:\n",
    "        deck_copy.remove(card)\n",
    "    dict_turns = {'won':0.0, 'tie':0.0, 'lost':0.0}\n",
    "    step = 1/n\n",
    "    for i in range(n):\n",
    "        sample = random.sample(deck_copy, T+H*N)\n",
    "        tf = t + sample[:T]\n",
    "        opponent_hands = [sample[T+i*H:T+(i+1)*H] for i in range(N)]\n",
    "        won, tie, result = check_table(tf, [h]+opponent_hands)\n",
    "        if not won:\n",
    "            dict_turns['lost'] += step\n",
    "        elif won and not tie:\n",
    "            dict_turns['won'] += step\n",
    "        elif won and tie:\n",
    "            dict_turns['tie'] += step\n",
    "            \n",
    "    for key in dict_turns.keys():\n",
    "        dict_turns[key] = round(dict_turns[key], 3)\n",
    "        \n",
    "    return dict_turns\n",
    "\n",
    "def gen_game(N=2, H=2, T=5):\n",
    "    ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']\n",
    "    suits = ['♣', '♦', '♥', '♠']\n",
    "    deck = ['{}{}'.format(i[0],i[1]) for i in list(itertools.product(ranks, suits))]\n",
    "    sample = random.sample(d, T + H*N)\n",
    "    return sample[:T],[sample[T+i*H:T+(i+1)*H] for i in range(N)]\n",
    "\n",
    "def run_turn(N=2, H=2, T=5, show=3, n=10000):\n",
    "    t,hands = gen_game(N, H, T)\n",
    "    print(get_sorted_rank(t[:show]),hands[0])\n",
    "    r = float(input('prob: '))\n",
    "    s = check_table(t, hands)\n",
    "    state = 'lost'\n",
    "    if s[0] and not s[1]:\n",
    "        state='won'\n",
    "    elif s[0] and s[1]:\n",
    "        state='tie'\n",
    "    print('state: ', state)\n",
    "    print('table: ', get_sorted_rank(t))\n",
    "    for i in range(len(s[2])):\n",
    "        print('P{}{}: '.format(i,'*' if s[2][i][2] else ''), s[2][i][1])\n",
    "\n",
    "    p = check_hand_random_n(d, hands[0],t[:show], n, N)\n",
    "    print(p)\n",
    "    print('expected: ', round(N*p['won']+0.5*N*p['tie']-p['lost'], 3))\n",
    "    print()\n",
    "    return r-p['won']-0.5*p['tie']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['K♣', 'T♠', '5♦'] ['7♥', 'K♥']\n",
      "prob: 18\n",
      "state:  won\n",
      "table:  ['K♣', 'T♠', '9♦', '5♦', '4♦']\n",
      "P0*:  ['K♥', 'K♣']\n",
      "P1:  ['T♦', 'T♠']\n",
      "{'won': 0.802, 'lost': 0.143, 'tie': 0.055}\n",
      "expected:  1.516\n",
      "\n",
      "finish: \n",
      "['A♠', '4♠', '3♠'] ['9♣', 'Q♣']\n",
      "prob: 10\n",
      "state:  lost\n",
      "table:  ['A♠', 'K♠', '6♥', '4♠', '3♠']\n",
      "P0:  ['A♠', 'K♠', 'J♠', '4♠', '3♠']\n",
      "P1*:  ['A♠']\n",
      "{'won': 0.119, 'lost': 0.591, 'tie': 0.29}\n",
      "expected:  -0.063\n",
      "\n",
      "finish: \n",
      "['J♥', '5♥', '4♠'] ['A♣', '2♣']\n"
     ]
    }
   ],
   "source": [
    "l = []\n",
    "for i in range(100):\n",
    "    l.append(run_turn(2))\n",
    "    x = input('finish: ')\n",
    "    if x: break\n",
    "round(numpy.mean(l),2),round(numpy.std(l),2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "txt = \"\"\"\n",
    "1suit 45 %\n",
    "\n",
    "2samesuit 6 %\n",
    "\n",
    "1rank - 4.2% each in deck\n",
    "    1specific 4.2 %\n",
    "\n",
    "2rank - 0.37% each in deck\n",
    "    2specific 0.1 %\n",
{'fraction_non_alphanumeric': 0.2725581395348837, 'fraction_numerical': 0.03255813953488372, 'mean_word_length': 1.8440413202317965, 'pattern_counts': {'":': 50, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5765810', 'n_tokens_mistral': 10458, 'n_tokens_neox': 10288, 'n_words': 1699}