forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXOR.rb
More file actions
37 lines (30 loc) · 1.11 KB
/
XOR.rb
File metadata and controls
37 lines (30 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
=begin
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.
=end
# My Solution
def xor(a,b)
a || b ? (a && b ? false : true) : false
end
# Better Solution
def xor(a,b)
a ^ b
end
# Another Solution
def xor(a,b)
return a != b
end