Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's itertools.product in C

There is a function in Python which runs this way:

itertools.product("abc", repeat = 2)

returns the following:

("a", "a")
("a", "b")
("a", "c")
("b", "a")
("b", "b")
("b", "c")
("c", "a")
("c", "b")
("c", "c")

Changing the repeat variable will change how many items come back in a tuple. How can this be written in C to return an array of arrays of chars? (an array of strings)

UPDATE: I now have this function I wrote:

void cartesian(char *a, int al, char *b, int bl){
    int i, j;
    char c[al * bl][2];
    for(i = 0; i < al; i++){
        for(j = 0; j < bl; j++){
            c[(i * bl) + j][0] = *(a + i);
            c[(i * bl) + j][1] = *(b + j);
            printf("%c%c\n", *(a + i), *(b + j));
        }
    }
}

int main(){
    char a[] = "abc";
    char b[] = "ab";
    cartesian(a, strlen(a), b, strlen(b));
    return 0;
}

How can I change this function so it can take in an array of of arrays of char and make the cartesian product? The arrays could contain any number of chars and there could be any number of arrays

The function should look like:

void cartesian(char *a, int l){
    /*Do cartesian*/
}

Example array:

[
    ['a', 'b', 'c', '\0'],
    ['a', 'b', 'c', '\0'],
    ['a', 'b', 'c', '\0']
]

(nulls included to work out length of arrays) should produce

[
    ['a', 'a', 'a'],
    ['a', 'a', 'b'],
    ['a', 'a', 'c'],
    ['a', 'b', 'a'],
    ['a', 'b', 'b'],
    ['a', 'b', 'c'],
    ['a', 'c', 'a'],
    ['a', 'c', 'b'],
    ['a', 'c', 'c'],
    ['b', 'a', 'a'],
    ['b', 'a', 'b'],
    ['b', 'a', 'c'],
    ['b', 'b', 'a'],
    ['b', 'b', 'b'],
    ['b', 'b', 'c'],
    ['b', 'c', 'a'],
    ['b', 'c', 'b'],
    ['b', 'c', 'c'],
    ['c', 'a', 'a'],
    ['c', 'a', 'b'],
    ['c', 'a', 'c'],
    ['c', 'b', 'a'],
    ['c', 'b', 'b'],
    ['c', 'b', 'c'],
    ['c', 'c', 'a'],
    ['c', 'c', 'b'],
    ['c', 'c', 'c'],
]

1 Answers

Here's a C implementation of cartesian product according to your specification. Note though that the argument is char **a, not char *a as it's an array of strings.

This is a variation on a previous answer.

    void cartesian(char **a, unsigned int l)
    {
        unsigned int *indices = calloc(l, sizeof(int));
        unsigned int changed;
        do {
            unsigned int finished = 0;
            unsigned int i;
            changed = 0;
            /* Print the current tuple */
            for (i = 0; i < l; i++) {
                putchar(a[i][indices[i]]);
            }
            putchar('\n');
            /* Loop over the arrays in reverse order */
            for (i = l - 1; !changed && !finished; i--) {
                /* Increment */
                indices[i]++;
                if (a[i][indices[i]]) {
                    /* We moved to the next character */
                    changed = 1;
                }
                else {
                    /* End of string, so roll over */
                    indices[i] = 0;
                }
                finished = i == 0;
            }
        } while (changed);
        free(indices);
    }

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!