Quant Questions


Random walk card game

If you have N cards, half red, half green. You begin with drawing card: for each red, you win 1 dollar; for each green, you lose 1 dollar. You may stop at any time. what is the value of this game assuming you play optimal. Write a program to compute this value.

def compute(r, g):
	if r == 0:
		return 0
	if g ==0 :
		return r
	return max(0, r/(r+g)*(1+compute(r-1,g)) + g/(r+g)*(-1+compute(r,g-1)))

N = #input
print(compute(N/2, N/2))

Leave a comment

Blog at WordPress.com.