-
Notifications
You must be signed in to change notification settings - Fork 254
/
secret_handshake.rb
40 lines (33 loc) · 1.06 KB
/
secret_handshake.rb
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
38
39
40
# Problem: https://exercism.org/tracks/ruby/exercises/secret-handshake
# Solution
class SecretHandshake
ACTIONS = ["wink", "double blink", "close your eyes", "jump"]
def initialize(number)
@number = number.is_a?(Integer) ? number : 0
end
def commands
actions =[]
bit_len = @number.digits(2).length
return actions if @number.zero?
for i in 0..(bit_len>3 ? 3 : bit_len)
actions.push(ACTIONS[i]) if (@number & (1<<i)) >0
end
@number & (1<<4) >0 ? (actions.reverse) : actions
end
end
# Solution 2
class SecretHandshake
ACTIONS = ["wink", "double blink", "close your eyes", "jump"]
def initialize(number)
@binary_digits = number.is_a?(Integer) ? (number.digits(2)) : []
end
def commands
actions =[]
return actions if @binary_digits.empty?
len = @binary_digits.length
for i in 0...(len>4 ? len-1 : len)
actions.push(ACTIONS[i]) if @binary_digits[i]==1
end
(len>4 and @binary_digits[-1]==1) ? (actions.reverse) : actions
end
end