Build a program to analyze coin flip sequence choices

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.

Joe Z.

Posted 2015-01-05T05:17:25.387

Reputation: 30 589

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.170

By "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

Answers

4

Python 3 (139 136 134 132 126 115 143)

Uses Conway's Algorithm as described at here. Handles all sequence pairs as long as the first is not a terminating subsequence of the second (as per instructions).

def f(a,b):c=lambda x,y=a:sum((x[~i:]==y[:i+1])<<i for i in range(len(x)));return 0 if b in a else(1/(1+(c(a)-c(a,b))/(c(b,b)-c(b))),1)[a in b]

Thanks xnor for shaving 6 bytes off. Thanks Zgarb for spotting a bug with subsequences.

Uri Granta

Posted 2015-01-05T05:17:25.387

Reputation: 2 675

The current version doesn't work for me. For the input "HTT" and "TTT", o has the value -1 and it divides it by 0. – Jakube – 2015-01-05T14:49:12.570

1Nice golf! I like the default argument trick. A couple of (untested) tips: you can multiply by 2**i with <<i, and the output probability can be written as 1/(1/o + 1), into which you can put the reciprocal of o directly. – xnor – 2015-01-05T14:53:24.947

Thanks. Good spot re o/(1+o). Somewhat embarrassed to have missed <<! – Uri Granta – 2015-01-05T15:32:14.857

@Jakube Sorry, didn't spot your comment! Current version works fine for me with "HTT" and "TTT". – Uri Granta – 2015-01-05T17:26:55.013

I changed the spec to say "program or function" so you don't have to take input and print stuff. – Joe Z. – 2015-01-05T18:52:01.283

1This gives a nonzero answer for HTH and T, even though the first player cannot win. The other answer has the same problem. – Zgarb – 2015-01-06T21:28:23.390

Good spot. It fails for any two sequences where one is a non-initial subsequence of the other. Before I patch it up, can you think of any issues or test cases? – Uri Granta – 2015-01-06T22:38:42.037

Joe Z said in the comments that you won't have one string be a terminal substring of the other, since that would allow a tie. – xnor – 2015-01-07T00:54:15.860

Thanks, I saw. My code gives wrong results for these, except the cases where one string is also a non-terminal substring of the other (e.g. T and TT) where it gives the correct result since a tie isn't possible. – Uri Granta – 2015-01-07T08:16:27.783

Been checking out your code but when I run it like f('HHH','THH') it returns 0, if I swap them it returns 1. Not familiar with python been running it online http://repl.it/languages/Python am I doing something wrong?

– Teun Pronk – 2015-01-07T10:07:48.403

The code requires Python 3 to work correctly, while the repl you're using is Python 2.7.2 (for example / in Python 2 is integer division rather than float division). – Uri Granta – 2015-01-07T10:38:29.580

@UriZarfaty Thanks for the tip, do you know any online tools for python 3? – Teun Pronk – 2015-01-07T10:56:04.657

Here's one: http://www.tutorialspoint.com/execute_python3_online.php. Web searches will find others.

– Uri Granta – 2015-01-07T13:09:41.607

3

CJam, 44 38 36 bytes

Using the same Conway's Algorithm as in here.

ll]_m*{~1$,,@f>\f{\#!}2b}/\-:X--Xd\/

Input is the two distinct sequences in two lines. The output is the probability of first winning over second. The inputs need not be of same lengths

I am using the formula for winning odds (p) for first player A as

enter image description here

Then the probability is defined as

enter image description here

which, after simplifying becomes

enter image description here

and after some simplification, becomes

enter image description here


Example input:

HTT
TTT

Output:

0.875

Try it online here

Optimizer

Posted 2015-01-05T05:17:25.387

Reputation: 25 836

Joe said in comments (after this was posted) that the strings are not necessarily the same length. Still, +1 because I don't understand CJam. – mdc32 – 2015-01-05T22:47:30.047

@mdc32 fixed, 1 byte longer now :( – Optimizer – 2015-01-05T22:56:12.567

You already let me believe that codegolfSE now supports LaTeX... =( – flawr – 2015-01-06T09:23:07.117

@flawr HAHA. Sorry about that :( . These are PNGs from online LaTeX editor. – Optimizer – 2015-01-06T09:36:20.790

This gives a nonzero answer for HTH and T, even though the first player cannot win. The other answer has the same problem. – Zgarb – 2015-01-06T21:28:05.190

@Zgarb One string cannot be a terminal sub string of the other. But I wonder if the algorithm really works for non similar lengths... – Optimizer – 2015-01-07T05:33:18.080

@Optimizer I understood terminal substring as suffix, so this should be a valid input. As for the algorithm, at least the linked article does not specify how exactly it should be generalized to inputs of differing lengths. I tried to do it in one way, but realized later that my solution gave wrong results in many cases. – Zgarb – 2015-01-07T08:49:14.880

@Zgarb I will have to specially manage these cases, I think. Let me see if any non-substring cases fail too. – Optimizer – 2015-01-07T09:39:33.213

0

Lua 211 190 184

Also using Conway's Algorithm. Still new to Lua so this can be golfed more for sure.

z=io.read;e=function(s,t)r='';for d in s:gmatch"."do r=r..(d==t:sub(1,1)and 1 or 0);end;return tonumber(r,2);end;a=z();b=z();print((e(a,a)-e(a,b))/(e(b,b)-e(b,a))/(1/((1/2)^b:len())));

Ungolfed

z=io.read;
e=function(s,t)
r='';
    for d in s:gmatch"."do 
        r=r..(d==t:sub(1,1)and 1 or 0);
    end;
    return tonumber(r,2);
end;
a=z();
b=z();
print((e(a,a)-e(a,b))/(e(b,b)-e(b,a))/(1/((1/2)^b:len())));

First version

z=io.read;
e=function(s,t) 
    r=0;
    for d in s:gmatch"."do 
        r=r*10;
        if d==t:sub(1,1)then r=r+1 end;
    end
    return tonumber(r,2);
end;
f=function(n,o)
    return ((e(n,n)-e(n,o))/(e(o,o)-e(o,n)))/(1/((1/2)^3));
end;
print(f(z(),z()));

Teun Pronk

Posted 2015-01-05T05:17:25.387

Reputation: 2 599