Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0bf673f

Browse files
exercism-73 Secret handshake
1 parent d27e987 commit 0bf673f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
294294
70. [Beer Song](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/beer_song.rb)
295295
71. [Protein Translation](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/protein_translation.rb)
296296
72. [Wordy](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/wordy.rb)
297+
73. [Secret Handshake](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/secret_handshake.rb)
297298

298299
<a name="leetcode"/>
299300

‎exercism/secret_handshake.rb‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/secret-handshake
2+
3+
# Solution
4+
class SecretHandshake
5+
ACTIONS = ["wink", "double blink", "close your eyes", "jump"]
6+
7+
def initialize(number)
8+
@number = number.is_a?(Integer) ? number : 0
9+
end
10+
11+
def commands
12+
actions =[]
13+
bit_len = @number.digits(2).length
14+
return actions if @number.zero?
15+
16+
for i in 0..(bit_len>3 ? 3 : bit_len)
17+
actions.push(ACTIONS[i]) if (@number & (1<<i)) >0
18+
end
19+
@number & (1<<4) >0 ? (actions.reverse) : actions
20+
end
21+
end
22+
23+
# Solution 2
24+
class SecretHandshake
25+
ACTIONS = ["wink", "double blink", "close your eyes", "jump"]
26+
def initialize(number)
27+
@binary_digits = number.is_a?(Integer) ? (number.digits(2)) : []
28+
end
29+
30+
def commands
31+
actions =[]
32+
return actions if @binary_digits.empty?
33+
34+
len = @binary_digits.length
35+
for i in 0...(len>4 ? len-1 : len)
36+
actions.push(ACTIONS[i]) if @binary_digits[i]==1
37+
end
38+
(len>4 and @binary_digits[-1]==1) ? (actions.reverse) : actions
39+
end
40+
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /