I'm practicing for the upcoming ACM programming competition in a week and I've gotten stumped on this programming problem.
The problem is as follows:
You have a puzzle consisting of a square grid of size 4. Each grid square holds a single coin; each coin is showing either heads (H) and tails (T). One such puzzle is shown here:
H...
In unbiased coin flip H or T occurs 50% of times.
But I want to simulate coin which gives H with probability 'p' and T with probability '(1-p)'.
something like this:
def flip(p):
'''this function return H with probability p'''
# do something
return result
>> [flip(0.8) for i in xrange(10)]
[H,H,T,H,H,H,T,H,H,H]
...
I'm taking my first steps into recursion and dynamic programming and have a question about forming subproblems to model the recursion.
Problem:
How many different ways are there to
flip a fair coin 5 times and not have
three or more heads in a row?
If some could put up some heavily commented code (Ruby preferred but not esse...
Right now i have
return 'Heads' if Math.random() < 0.5
Is there a better way to do this?
Thanks
edit: please ignore the return value and "better" means exact 50-50 probability.
...
From Feller (1950) An Introduction to Probability Theory:
A path of length n can be interpreted as the record of an ideal experiment consisting of n successive tosses of a coin. If +1 stands for heads, then Sk equals the (positive or negative) excess of the accumulated number of heads over tails at the conclusion of the kth trial. Th...
I have a sequence of ones and zeros and I would like to count the number of alternations. e.g.
x <- rbinom(10, 1, 1/2)
> x
[1] 0 0 1 1 1 1 1 0 1 0
Thus I would like to count (in R) how many times the sequence alternates (or flips) from one to zero. In the above sequence the number of alternations (counted by hand) is 4.
...