반응형
Exclusive "or" (xor) Logical Operator
Overview
In some scripting languages like PHP, there exists a logical operator (e.g. &&, ||, and, or, etc.) called the "Exclusive Or" (hence the name of this Kata). The exclusive or evaluates two booleans. It then returns true if exactly one of the two expressions are true, false otherwise. For example:
false xor false == false // since both are false
true xor false == true // exactly one of the two expressions are true
false xor true == true // exactly one of the two expressions are true
true xor true == false // Both are true. "xor" only returns true if EXACTLY one of the two expressions evaluate to true.
Task
Since we cannot define keywords in Javascript (well, at least I don't know how to do it), your task is to define a function xor(a, b) where a and b are the two expressions to be evaluated. Your xor function should have the behaviour described above, returning true if exactly one of the two expressions evaluate to true, false otherwise.
Solution:
def xor(a,b):
return len(''.join(map(str, [a,b]))) == 9
def xor(a,b):
return a ^ b
def xor(a,b):
return a != b
from operator import xor
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
1027. Filling an array (part 1) (0) | 2022.10.28 |
---|---|
1026. Beginner Series #4 Cockroach (0) | 2022.10.26 |
1024. Sum The Strings (0) | 2022.10.24 |
1023. No Loops 2 - You only need one (0) | 2022.10.23 |
1022. Area of a Square (0) | 2022.10.22 |