''' Created on Jan 24, 2015 @author: Brett Paufler Copyright Brett Paufler Intent is to be a test code snippet utilizing functional paradigms no reassignment of variables all variables are functions Principles of Functional Everything is a Function No reassignment of Functions/Variables b = lambda x: x + x moves = lambda : 200 virtualJump = lambda (x,y) : random.choice([(x+1,y),(x-1,y),(x,y+1),(x,y-1)]) ''' import math e = math.e e213 = 900.0/e print "Project Euler 213: %.7f" % e213 print "Project Euler 213: %.7f" % (900.0/math.e) import random def fleaJump((x,y)): a,b = random.choice([(x+1,y),(x-1,y),(x,y+1),(x,y-1)]) if a<0 or b<0 or a>29 or b>29: return fleaJump((x,y)) else: return (a,b) #virtualJump = lambda (x,y) : random.choice([(x+1,y),(x-1,y),(x,y+1),(x,y-1)]) #fleaJump = lambda (x,y) : virtualJump(x,y) def jumpAll(fleaList): fleasNext = [] for flea in fleaList: fleasNext.append(fleaJump(flea)) return fleasNext fleaStart = [(a,b) for a in range(30) for b in range(30)] turn = [fleaStart] moves = 200 for m in range(moves): turn.append(jumpAll(turn[-1])) emptySquaresPerTurn = [900 - len(set(t)) for t in turn] average = sum(emptySquaresPerTurn[100:]) / float(moves-100) print average