4 ms·
Run it online here: https://onlinegdb.com/rkZhWVniH https://onlinegdb.com/rkZhWVniH
by thegeekpirate 7y ago
Run it online here: https://onlinegdb.com/rkZhWVniH https://onlinegdb.com/rkZhWVniH
- jcims 7y agoThanks for posting this, because I'm a Python n00b and I had no idea what this line was doing: itertools.product((True, False), repeat=6) The site made it easy for me to spend 30 seconds figuring it out. My interpretation is that that line generates what I'll call a truth table (not sure if that's what it formally is) then brute force searches for rows that satisfy all of the listed constraints. I'd also hazard that the fact that there's only one answer is engineered into the question and not a natural product of this search.
- jdmichal 7y agoI think you have the general idea down. But as you suspected it's not a truth table. It's all cross-product permutations (hence the name `product`) of six items from the set {True, False}. So that line is creating a iterator that looks like this: (True, True, True, True, True, True) (True, True, True, True, True, False) (True, True, True, True, False, True) (True, True, True, True, False, False) (True, True, True, False, True, True) Though not necessarily in this order... I'm unfamiliar with the actual implementation. Since the values are bits, they could have achieved the same effect by incrementing a six-bit integer. Which is a nice proof that the number of permutations here is 2^6. (Generally it's s^n, where s is the set size and n is the count of elements.)
- jcims 7y agoAwesome thank you!
- Asooka 7y agoThe product of two iterables is a list of pairs of each item in the first iterable and each item in the second iterable (same as set product in math): [a, b] × [c, d] = [(a, b), (a, c), (b, c), (b, d)] The number of elements of the product of two iterables is the product of the number of elements of each. You can similarly have a product of 3, 4, etc. iterables. The repeat keyword argument to product specifies that we want not a product of several different iterables, but the product of an iterable with itself N times. So we pass a tuple of all possible boolean values (True, False) and ask that it be raised to the 6th power, giving us all possible 6-tuples of all possible boolean values.