Replies To: Understanding Xor

Re: Understanding Xor

Posted 25 September 2012 - 02:50 PM

Operations are not limited to the basic ones (+, -, /, *) for example XOR could be classed as an operation, AND, OR, EQUIVALANCE.. don't you have some reference as to what operations are valid? Or perhaps any function at all?

In Python, functions are first class so, they can be passed as parameters so that isn't a problem. Commutative basically means that the operation will work if you switch the operands around:

So,

4 + 5 is equal to 5 + 4 so "+" is commutative (or add(x, y) as a function)

4 - 5 is NOT equal to 5 - 4 so "-" is not commutative (or subtract(x, y) as a function)

How would you go about testing if an operand (or function) is commutative or not?

Re: Understanding Xor

Posted 25 September 2012 - 02:59 PM

Simown, on 25 September 2012 - 03:50 PM, said:

Operations are not limited to the basic ones (+, -, /, *) for example XOR could be classed as an operation, AND, OR, EQUIVALANCE.. don't you have some reference as to what operations are valid? Or perhaps any function at all?

In Python, functions are first class so, they can be passed as parameters so that isn't a problem. Commutative basically means that the operation will work if you switch the operands around:

So,

4 + 5 is equal to 5 + 4 so "+" is commutative (or add(x, y) as a function)

4 - 5 is NOT equal to 5 - 4 so "-" is not commutative (or subtract(x, y) as a function)

How would you go about testing if an operand (or function) is commutative or not?

Hi, thanks for the informative response!

Well to test if something was commutative in a set S, then I would go about testing if S[0] + S[1] = S[1] + S[0] and traverse the set from there. Is this correct logic? And if so, would I need a for loop to keep track of the index?

Re: Understanding Xor

Posted 25 September 2012 - 03:07 PM

Yes, that's exactly how to test! But, we want to test every element against every other element to ensure the entire set of items is commutative against the same set without writing S[0] + S[1] = S[1] + S[0].... S[39] + S[40]..... + S[81]... well, you get it.

So, we need to loop, I assume you understand them?

It will go something like this:

def commutative(op, S):
# First iteration of the set
for first in S:
# Second iteration of the same set so every item is compared against the first
for second in S:
# Remember the double equals
if(op(first,second) == op(second, first))

Re: Understanding Xor

Posted 25 September 2012 - 03:10 PM

Simown, on 25 September 2012 - 04:07 PM, said:

Yes, that's exactly how to test! But, we want to test every element against every other element to ensure the entire set it commutative in the set without writing S[0] + S[1] = S[1] + S[0].... S[39] + S[40]..... + S[81]... well, you get it.

So, we need to loop, I assume you understand them?

It will go something like this:

def(op, S):
# First iteration of the set
for first in S:
# Second iteration of the same set
for second in S:
# Remember the double equals
if(op(first,second) == op(second, first)