15
2
In a puzzle in an old book of mine, a game is defined in which two players choose sequences of coin flips that they believe will appear first when a coin is repeatedly flipped. (It was actually odd and even dice rolls, but this little detail doesn't matter in terms of problem equivalence.)
It is noted that if player 1 chooses TTT
and player 2 chooses HTT
, that player 2 has a 7/8 chance of winning the game, since the only way TTT
can come before HTT
is if the first three flips are all tails.
Your job is to create a program or function that will deduce the probability that one of two chosen sequences will comes first. Your program will take two lines of input (or two strings as arguments), each representing a sequence of length 10 or less:
HTT
TTT
And output the probability that the first player will win, in either fraction or decimal form:
7/8
0.875
The shortest code to do this in any language wins.
6Are the sequences always the same length as each other? – Uri Granta – 2015-01-05T08:43:20.580
1@UriZarfaty No, not necessarily. – Joe Z. – 2015-01-05T16:39:40.937
Though presumably the sequences have to be distinct (since the output can't specify a tie). – Uri Granta – 2015-01-05T16:53:39.983
Yes, the sequences must be distinct. – Joe Z. – 2015-01-05T16:54:52.007
More specifically, one cannot be a terminal substring of the other. – Joe Z. – 2015-01-05T16:55:33.207
Can they be arguments to a program? – TheNumberOne – 2015-01-05T18:42:55.037
Yes. Usually I specify that way at first, although for some reason this time around it was specifically a program. – Joe Z. – 2015-01-05T18:51:20.293
Sorry for asking but I dont get the comment where it says
one cannot be a terminal substring of the other
what exactly does that mean? is it so that the 2nd string cannot be the same as the start or end of the other string since that could result in a tie? – Teun Pronk – 2015-01-07T08:12:32.170By "terminal substring", I mean that one string cannot be the same as the end portion of another string. (It can be the same start portion; that just means that the substring will win 100% of the time.) – Joe Z. – 2015-01-08T07:49:26.007