Archive for May, 2011

Python and Project Euler (spoiler warning)

Python tools for visual studio is now available as a beta 2 release. I have tried using Eclipse for python development earlier and I guess that was alright but this one just seemed to be a better match for me. I decided to try it out also hoping to pick up some more python syntax along the road. I needed a task and the solution for problem 54 in project Euler was still missing on my list so… why not?

I do not normally publish the solutions to project Euler problems but I make an exception for this one since poker hand rating do exist outside the domain of project Euler. Reflecting back on the experience I conclude that:

  • Python tools does a good job providing intellisense, but of course, due to the dynamic nature of python, it does not compare to typed languages like F# or C#.
  • List comprehensions is a truly nice feature that is included in the python language and along with lambda expressions, tuples and local functions it helps keeping the code reasonably compact and clear.

import itertools

import os


def readCards(filename = ‘pokersamples.txt’):

    def parseValue(ch):

        if ch == ‘T’:   return 10

        elif ch == ‘J’: return 11

        elif ch == ‘Q’: return 12

        elif ch == ‘K’: return 13

        elif ch == ‘A’: return 14

        else: return int(ch)   


    file = open(filename)

    result = []

    for line in file:

        all = []

        for card in line.rstrip().split(‘ ‘):

            all.append((parseValue(card[0]), card[1]))

        player1 = all[0:5]

        player2 = all[5:10]

        result.append((player1, player2))


    return result


def rateHand(hand):       

    class Classifications:

        HighCard = 1; OnePair = 2; TwoPairs = 3

        ThreeOfAKind = 4; Straight = 5; Flush = 6

        FullHouse = 7; FourOfAKind = 8

        StraightFlush = 9; RoyalStraighFlush = 10

    def distinct(l): return list(set(l))

    def fsort(orig): copy = list(orig); copy.sort(key=lambda x: -x); return copy   


    colors = [ card[1] for card in hand ]

    values = [ card[0] for card in hand ]

    values.sort(key=lambda x: -x)

    unique = len(distinct(values)) == 5

    if unique:

        flush = len(distinct(colors)) == 1;

        straight = values[0] – 4 == values[-1] or values == [14,5,4,3,2]

        if straight:

            royal = values[0] == 14

            if royal and flush:

                return [Classifications.RoyalStraighFlush, values[0]]

            elif flush:

                return [Classifications.StraightFlush, values[0]]


                return [Classifications.Straight, values[0]]

        elif flush:

            return [Classifications.Flush] + values


            return [Classifications.HighCard] + values


        groups = [[v for v in values] for key, values in itertools.groupby(values, lambda x:x)]

        groups.sort(key=lambda x:len(x))

        groupSizes = [len(g) for g in groups]

        if groupSizes == [1,1,1,2]:

            return [Classifications.OnePair, groups[3][0]] + fsort([groups[0][0], groups[1][0], groups[2][0]])

        elif groupSizes == [1,2,2]:

            return [Classifications.TwoPairs] + fsort([groups[1][0], groups[2][0]]) + [groups[0][0]]

        elif groupSizes == [1,1,3]:

            return  [Classifications.ThreeOfAKind, groups[2][0]] + fsort([groups[0][0], groups[1][0]])

        elif groupSizes == [2,3]:       

            return [Classifications.FullHouse, groups[1][0], groups[0][0]]

        elif groupSizes == [1,4]:

            return [Classifications.FourOfAKind, groups[1][0], groups[0][0]]


games = readCards(‘poker.txt’)

count = 0

for player1, player2 in games:

    rate1 = rateHand(player1)

    rate2 = rateHand(player2)

    if rate1 > rate2:


print(‘player 1 total wins: ‘ + str(count))       


Categories: Uncategorized

Powershell remoting: Free book available

Ravikanth Chaganti has published a free book on powershell remoting. It seems to be well written and well worth spending some time reading.

Categories: Uncategorized