Skip to content

Instantly share code, notes, and snippets.

@wmalarski
Last active March 21, 2017 11:33
Show Gist options
  • Save wmalarski/c6358a5ab37b1d7e03fbd3b77f8be999 to your computer and use it in GitHub Desktop.
Save wmalarski/c6358a5ab37b1d7e03fbd3b77f8be999 to your computer and use it in GitHub Desktop.
Python lab2
from __future__ import division
import random
l = [0, 1, 2]
def generate_choice():
choice = random.choice(l)
cross = random.choice(l)
show = random.choice(list(set(l) - {choice, cross}))
return choice, cross, show
def generate_choice_list(n):
return [generate_choice() for i in range(n)]
def calc_no_change(n):
def red(cnt, (choice, cross, _)):
return (cnt + 1) if choice == cross else cnt
return reduce(red, generate_choice_list(n), 0)/n
def calc_change(n):
def red(cnt, (choice, cross, show)):
choice = (set(l) - {choice, show}).pop()
return (cnt + 1) if choice == cross else cnt
return reduce(red, generate_choice_list(n), 0)/n
print calc_no_change(1000)
print calc_change(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment