Thursday, September 23, 2010

Rock Paper Scissors bot

I read this post and wrote some Python code which uses 2 of the hints given in the diagram. This bot will win 57% of the games.

I simulate a human draw using the % given and hence the bot never draws scissors unless it's a response to a 2 in a row from the human player.

#simulate a human player
#and answer with best strategy

import random


def humandraw(hist):
    if rand<.296: d='P'
    elif rand<.296+.354: d='R'
    else: d='S'
    if len(hist)>1:
        if hist[-1]==hist[-2]:
            while d==hist[-1]:
                if rand<.296: d='P'
                elif rand<.296+.354: d='R'
                else: d='S'           
    return d

def winner(p1,p2):
    if p1==p2: win=0
    elif p1=='R' and p2=='S': win=1
    elif p1=='S' and p2=='R': win=2
    elif p1=='P' and p2=='R': win=1
    elif p1=='R' and p2=='P': win=2
    elif p1=='S' and p2=='P': win=1
    elif p1=='P' and p2=='S': win=2
    return win
for g in range(games):
    if rand<.5: a='P'
    else: a='R'
    if len(hist)>1:
        if hist[-1]==hist[-2]:
            if hist[-1]=='P': #will draw S or R
                if rand<.5: a='R'
                else: a='P'
            elif hist[-1]=='R': #will draw P or S
                if rand<.5: a='R'
                else: a='S'
            elif hist[-1]=='S': #will draw P or R
                if rand<.5: a='S'
                else: a='P'
    #print a,d,win
    if win==1: wins+=1
    elif win==0: draws+=1

print games,wins,draws
print float(wins)/(games-draws)

Monday, September 13, 2010

Google's new data structure

Google Caffeine — the remodeled search infrastructure rolled out  across Google's worldwide data center network earlier this year — is not based on MapReduce, the distributed number-crunching platform that famously underpinned the company's previous indexing system. As the likes of Yahoo!, Facebook, and Microsoft work to duplicate MapReduce through the open source Hadoop project, Google is moving on.