Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions Training/Easy/chuck_norris.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Solution for https://www.codingame.com/ide/puzzle/chuck-norris
class ChuckNorris
attr_accessor :message
attr_reader :message

def initialize
@message = gets.chomp
Expand All @@ -15,40 +15,37 @@ def convert_in_binary(char)
end

def split_series(array)
result = []
c = 0
array.each_with_index do |item, index|
c += 1
count = 0
array.each_with_object([]).each_with_index do |(item, result), index|
count += 1
next if item == array[index + 1]
a = [item, c]
result << a
c = 0
result << [item, count]
count = 0
end
result
end

def norris_parse(array)
result = ''
array.each do |a|
if a[0].zero?
result += '00 '
else
result += '0 '
end
result += '0' * a[1] + ' '
def norris_parse(series_array)
parsed = series_array.each_with_object([]) do |serie, result|
# serie[0].zero? ? result << '00' : result << '0'
result << if a[0].zero?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding items with whitespace to a string object and then stripping it, you can add the same items without whitespace to an array object, after which you can use join(' ')

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to give meaning to the object's elements if it's not clear what they are - so a[0] and a[1] should have some meaning. You can assign them to local variables and then use them

'00'
else
'0'
end
result << '0' * serie[1]
end
result.strip
parsed.join(' ')
end

def start
binary = @message.each_char.each_with_object('') do |char, b|
b << convert_in_binary(convert_in_ascii(char))
binary = ''
message.chars do |char|
binary << convert_in_binary(convert_in_ascii(char))
end
array = split_series binary.each_char.map(&:to_i)
output = norris_parse array
puts output
array = split_series(binary.chars.map(&:to_i))
norris_parse(array)
end
end

obj = ChuckNorris.new
obj.start
puts obj.start