18
1
Introduction
Suppose you and your friend are playing a game.
Your friend thinks of some particular sequence of n
bits, and your task is to deduce the sequence by asking them questions.
However, the only type of question you're allowed to ask is "How long is the longest common subsequence of your sequence and S
", where S
is any sequence of bits.
The fewer questions you need, the better.
The task
Your task is to write a program or function that takes as input a positive integer n
, and a binary sequence R
of length n
.
The sequence may be an array of integers, a string, or some other reasonable type of your choice.
Your program shall output the sequence R
.
Your program is not allowed to access the sequence R
directly.
The only thing it's allowed to do to R
is to give it as input to the function len_lcs
along with another binary sequence S
.
The function len_lcs(R, S)
returns the length of the longest common subsequence of R
and S
.
This means the longest sequence of bits which occurs as a (not necessarily contiguous) subsequence in both R
and S
.
The inputs of len_lcs
which may be of different lengths.
The program should call this function on R
and other sequences some number of times, and then reconstruct the sequence R
based on that information.
Example
Consider the inputs n = 4
and R = "1010"
.
First, we might evaluate len_lcs(R, "110")
, which gives 3
, since "110"
is the longest common subsequence of "1010"
and "110"
.
Then we know that R
is obtained from "110"
by inserting one bit at some position.
Next, we might try len_lcs(R, "0110")
, which returns 3
since the longest common subsequences are "110"
and "010"
, so "0110"
is not correct.
Then we try len_lcs(R, "1010")
, which returns 4
.
Now we know that R == "1010"
, so we can return that sequence as the correct output.
This required 3 calls to len_lcs
.
Rules and scoring
In this repository, you'll find a file called subsequence_data.txt
containing 100 random binary sequences of lengths between 75 and 124.
They were generated by taking three random floats between 0 and 1, taking their average as a
, and then flipping an a
-biased coin n
times.
You score is the average number of calls to len_lcs
on these sequences, lower score being better.
Your submission should record the number of calls.
There are no time limits, except that you should run your program on the file before submitting it.
Your submission shall be deterministic.
PRNGs are permitted, but they must use today's date, 200116
(or closest equivalent), as the random seed.
You are not allowed to optimize your submission against these particular test cases.
If I suspect this is happening, I will generate a new batch.
This is not code golf, so you're encouraged to write readable code.
Rosetta Code has a page on the longest common subsequence; you may use that to implement len_lcs
in your language of choice.
Cool idea! Does this have any applications? – flawr – 2016-01-21T09:12:45.730
@flawr I don't know of any direct applications. The idea came from query complexity theory, a subfield of computer science, and that has loads of applications. – Zgarb – 2016-01-21T16:13:32.357
I think it would be great to have the same challenge again but where you can access
lcs
instead oflen_lcs
. – flawr – 2016-01-21T19:28:14.227@flawr That wouldn't be very interesting, since
lcs(R, "01"*2*n)
returnsR
. ;) But that could work if callinglcs(R, S)
would increase the score bylen(S)
instead of 1, or something like that... – Zgarb – 2016-01-21T19:32:33.637Oh right, you'd really need another restriction=) – flawr – 2016-01-21T20:02:07.753
1I'd love to see other answers =S – flawr – 2016-01-24T00:38:04.247