Wednesday, November 30, 2016

Coupon Collector Problem and Python

Collecting Coupons: This is a famous problem in Mosteller's book.
Coupons in cereal boxes are numbered 1 to 5, and a set of one of each is required for a prize. With one coupon per box, how many boxes on the average are required to make a complete set?

Here is a python program to simulate it

"""We store the outputs in a list a until we get all the coupons
The coupons are generated from the randint function which generates
both the end values
The sum variable keep tracks of number of boxes we have been counting
"""
import random
flag = 1
a=[]
sum = 0
n = 1000 # number of times you want to run the experiment
coupNum = 30 # number of different coupons
for i in range(n):
    count = 0
    a=[]
    flag =1
    while flag:
        x=random.randint(1,coupNum)
        #generates random number between 1 and coupNum inclusive
        count += 1
#        print(x)
        if x not in a:
            a.append(x)
            flag = 1
        elif len(a)==coupNum:
            flag=0
#    print(count)
    sum = sum+count
#    print(a,count,sum)
    del a[:] #delete the list
print(sum/n)

Labels: , ,

Odds of Craps

Craps

 

The game of craps, played with two dice, is one of America's fastest and most popular gambling games. Calculating the odds associated with it is an instructive exercise. The rules are these. Only totals for the two dice count. The player throws the dice and wins at once if the total for the first throw is 7 or 11, loses at once if it is 2, 3 or 12. Any other throw is called his "point". If the first throw is a point, the player throws the dice repeatedly until he either wins by throwing his point again or loses by throwing 7. What is the player's chance to win.

Python Program to find the final probability
sum = 8/36
a = {4:3/36,5:4/36,6:5/36,8:5/36,9:4/36,10:3/36}
for i in a:
    sum = sum+6*(a[i])**2/(6*a[i]+1)
print(sum)

Labels: , ,

Friday, November 18, 2016

Prisoner hat problem

This is a problem about a prison where an evil wizard comes and tell the 64 inmates there that tomorrow he will come and put a hat of either black or white color on their head after making them stand in a line. So that they can only see the prisoners in front of them and their hat. He would then start from the inmate at the very end who can see everyone else's hat except for his own. He would ask each of them what color is their hat. They can only answer black or white and no other words. If they are correct he would leave them otherwise he will kill them. They have a whole night to plan their strategy once he puts them in line they cannot communicate.

The problem is to find out a strategy which would allow maximum number of inmates to escape death.

[sourcecode language="python"]
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 16 21:03:20 2016

@author: Sumant
The hats take the input as a STRING of Black and White
Black: Represents even number of black hats
White: Represents odd number of black hats
Black = 1
White = 0
def whatTheySee()
  returns an array which contains the parity of black hats they see in front
  of them

def whatTheySay()
  returns an array which contains what they say

Algorithm: WhatTheySay
  We need to use the array whatTheySee and we return an array which contains
  what each person should say: except for the first entry which corresponds to
  the last number we see. All others are always correct.
  The nth position of the array we return populates itself with the responses
  of previous n-1 responses and add what the parity of black hats the person
  standing at nth position sees.

Input: WWWWBB
Output: WWWWBB

Input: BBBBB
Output: BBBBB

Input: BBBB
Output: WBBB

Input: BWBWBB
Output: WWBWBB
"""

hats="BWBWBB"




def binaryTohat(hat):
    change=[]
    for i in hat:
        if i == 1:
            change.append('B')
        else:
            change.append('W')
    return change
   
def hatTobinary(hat):
    change=[]
    for i in hat:
        if i == 'B':
            change.append(1)
        else:
            change.append(0)
    return change
       
HATS=hatTobinary(hats)

def whatTheySee(hats):

    a=[]
    for j in range(len(hats)):
        sum = 1
        for i in range(j+1,len(hats)):
            sum= sum+HATS[i]
        sum = sum %2
        a.append(sum)
       
    return a

def whatTheySay(hats):
    a=[]
    val=0
    a.append(hats[0])
    for i in range(1,len(hats)):
        val=sum(a)+hats[i]
        a.append(val%2)
    return a      

def stringArraytoString(st):
    a=""
    for i in st:
        a+=i
    return a
print("What wizard see",hats)
print("What they say",stringArraytoString(binaryTohat(whatTheySay(whatTheySee(HATS)))))

[/sourcecode]

Labels: ,

Wednesday, November 09, 2016

Donald Trump's Win a slap to Islamists

Donald Trump win is finally a big slap to islamists who have been infiltrating the western world and yet keeping their islamic identities. Islam is not compatible with modern view of the world and its a known fact that when muslims grow above 10 percent in any society they start to impose their sharia centric view on their hosts. A barometer test to tell if a particular muslim is a threat to a civil society is to check their views about Sharia laws. No matter how liberal they call themselves if they have any sympathy for Sharia law sooner or later they will be threat to their host. So its the first time a nation has taken a collective step to stop Sharia laws. Sharia laws are the most regressive set of laws which discriminate women, gays and atheists. Hopefully this will lead more countries to curb this barbaric, outdated laws.
  Borak Obama will be remembered as one of the most charasmatic leader but his weakness to condemn Islam was the sole reason democrats lost and Trump won! The rise of islamic terrorism is so widespread and yet people till before this election were not saying the spade a spade has forever changed with this election. Officially over 60 percent of Americans have a negative view of Islam and in reality this is much higher. Hopefully Americans now can build a better society from here. They finally have a person who seems to look after American interests. Congratulations to Americans for taking this courageous step

Labels: , ,

Sunday, November 06, 2016

Sabun Pani Zindabad.

I am currently reading a spanish book called Choco encuentra una mamá. It reminded me of some early books that I enjoyed as a kid. One was sabun pani zindabad. It's published in 1905 by Kornaee Chukowaski. I googled it and i found it. I still remember the pages where wash basin was chasing the kid. There were quite a few books given to us by M.N. Singh uncle and many of them were my favorite. Given that its still available in print in India is pretty incredible.
Site Meter