### How to take this Python code and convert it into C++: list(product([True, False], repeat=len(some list)]))

closed account (26q2b7Xj)
I've been thinking hard on this idea of how to replicate itertools.product. Any thoughts?
some people here know python. I have used it a tiny bit but not sure what that statement does nor what itertools is. It would go a long way if you could just say in english what you want to do. I took a look at itertools for 30 seconds or so. C++ already has efficient iterators (the #1 point of the library), but if you wanted to bundle up all the extras you can certainly do so.

My thoughts are, what problem are you trying to solve, beyond the exercise of doing it? When you use it in python, which parts do you return to over and over as really useful? Which of the things this library does cannot be done in c++ already with built in tools, and do we generally have a need to do those things? If you see a useful thing here, by all means go for it. May want to check ... is recreating this thing allowed?

closed account (26q2b7Xj)
The idea is to generate all possible combinations of truth values base on a proposition. The end goal is to generate a Truth Table. Generate all length-n permutations of True/False basically. I'm a noob so I find it harder in C++.

So if n was 2 then: [[True, True], [True, False], [False, True], [False, False]] is what I am trying to get at.
Last edited on
you can certainly build something to do that, but its doable already, so you would just be wrapping up a small # of c++ statements.

I will try to clever it up, but here is a starting point using the most simple techniques. Making it shorter will make it harder to follow for a beginner...

 ``1234567891011121314151617181920`` `````` int main() { int n = 3; int p2 = 1 << n; const string tf[2]{"false","true"}; vector tbl(n,false); int tmp; for(int i = 0; i < p2; i++) { tmp = i; int x = 0; while(tmp) { tbl[x++] = tmp%2; tmp = tmp >> 1; } for(int b = 0; b
Last edited on
This is, essentially, a bignum addition exercise. C++ doesn't typically support coding patterns that generate list temporaries for gcomputing a result -- not like python does.

A more generalized function to produce a list would be to step through the additions using a single add until you have generated (and stored) all solutions. The number of additions is a simple power: the size of your alphabet raised to the "repeat" power. In this case you have a binary alphabet: false and true.

000
001
010
011
100
101
110
111
1000 -> done

Maybe I'll come back with some code...

Hope this helps.
Last edited on
the trouble is the problem is in a vacuum ... now that you generated it, what did you want it for? Do you need it in a container or just printed? Is binary OK for printing it? If so bitset's to string looks like a winner. Or if you just want it printed, a for loop over integers is a winner. There are a lot of ways to do it, which one depends on what you do with it next, to some extent.
He marked it done with the off the cuff crude code, so I did not go back...

I did exactly what Duthomhas said... the bit shift is 2 to the power (5) ... the for loop does the addition ... the rest of it is just output related ... and its terrible lol...
Last edited on
Topic archived. No new replies allowed.