Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

For example I have the following.

The inputs are on the left. The outputs are on the right. The second term in the input which is the number 3 is the number of output numbers I want.

It would be really useful if there was a program which given this pattern could just give me the code written to produce it. Is there such a thing?

0 3 -> 0 1 2
1 3 -> 3 1 2
2 3 -> 3 4 2
3 3 -> 3 4 5
4 3 -> 6 4 5
5 3 -> 6 7 5
...
share|improve this question

migrated from stackoverflow.com May 23 '13 at 16:10

This question came from our site for professional and enthusiast programmers.

3  
I'm sure there is a function which can generate that, but there isn't enough detail there to understand how the inputs relate to the outputs... and we're not here to do your job for you anyways. –  Marc B May 23 '13 at 16:03
1  
No, it does not exist. There are an infinite number of solutions to any such problem, and determining the solution that "makes the most sense" requires intelligence. –  jnylen May 23 '13 at 16:06
3  
Discover the algorithm based upon the given inputs and outputs. -- That's what a programmer does. You need reasoning ability to accomplish that. –  Robert Harvey May 23 '13 at 16:07
1  
@RobertHarvey Pretty sure there exist algorithms for automatic pattern inference (with constraints and limitations, of course), which is what this question seems to be about. A programmer can infer a rule, but that doesn't rule out the help of automated systems presenting likely candidates. –  Andres F. May 23 '13 at 17:15
1  
You mean en.wikipedia.org/wiki/Supervised_learning ? Sure, lots of algorithms... –  jzx May 23 '13 at 17:18

3 Answers 3

AS Jnylen points out, given a finite set of inputs and outputs there are infinite possible functions that have these inputs -> outputs as subsets of their domain and range.

That said there are ways you could automate finding a function that fits e.g.

  • put the inputs and outputs in a lookup table, optionally interpolating or extrapolating for inputs not in the table
  • fit a polynomial function to the data with degree N-1 for N input data points

but you will still need to know what is appropriate for your domain here.

share|improve this answer

Of course there are trivial solutions to this problem - just throw the known inputs and corresponding outputs in a lookup table. This is not a desirable solution though, as it is comparable to building a calculator by hard-coding a few dozen example calculations and having it fail on anything else, e.g.:

function runCalculator($calculation) {
    switch ($calculation) {
        case "1 + 1": return "2";
        case "23 + 15": return "38";
        case "5 * 5": return "25";
        /* ... */
        default: return "ERR";
    }
}

Instead, what you want is an algorithm that finds the underlying pattern and returns it in a form that allows it to be applied to arbitrary other well-formed inputs. This, however, is a subject that fills libraries; it is commonly known as Pattern Recognition, Machine Learning, or maybe even Artificial Intelligence, and it is closely intertwined with Statistics.

A general-purpose, fool-proof solution is impossible, and even with detailed knowledge of the problem domain and a rock solid background in applied mathematics, getting useful results is still challenging.

share|improve this answer

0 3 -> 0 1 2
input = 0,
n = 3,
outputs = {0,1,2}

    int outputs[] = new int[n];
    for (int i = 0; i < n; i++) {
        outputs[i] = i;
    }

    for (int i = 1; i <= input; i++) {
        outputs[(i-1) % n] += n;
    }

    for (int output : outputs) {
        System.out.print(output + " ");
    }

Just for fun :p
I don't know if it exists or not.

share|improve this answer
3  
This solves this particular example, but the question was about a generalized solution. –  Andres F. May 23 '13 at 17:11
    
I know. That's why I said "just for fun". Oh well.. –  Surya Wijaya Madjid May 24 '13 at 11:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.