-
Notifications
You must be signed in to change notification settings - Fork 0
[Chuck norris task] Update solution #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
4d025a9 to
d2278e1
Compare
e98757a to
42d02b3
Compare
b8e5d88 to
73f6435
Compare
42d02b3 to
58f2017
Compare
Training/Easy/chuck_norris.rb
Outdated
| end | ||
|
|
||
| def start | ||
| binary = @message.each_char.each_with_object('') do |char, b| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to chain each_char with each_with_object - @message has a chars method
Training/Easy/chuck_norris.rb
Outdated
| end | ||
| array = split_series binary.each_char.map(&:to_i) | ||
| output = norris_parse array | ||
| array = split_series(binary.each_char.map(&:to_i)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here - binary has a chars method
Training/Easy/chuck_norris.rb
Outdated
| @@ -27,25 +27,24 @@ def split_series(array) | |||
| result | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use each_with_object in the split_series method as well
Also, use meaningful variable names - a and c are not meaningful enough
| result += '0' * a[1] + ' ' | ||
| def norris_parse(series_array) | ||
| parsed = series_array.each_with_object('') do |a, result| | ||
| result << if a[0].zero? |
There was a problem hiding this comment.
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(' ')
| result += '0' * a[1] + ' ' | ||
| def norris_parse(series_array) | ||
| parsed = series_array.each_with_object('') do |a, result| | ||
| result << if a[0].zero? |
There was a problem hiding this comment.
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
Training/Easy/chuck_norris.rb
Outdated
| def norris_parse(series_array) | ||
| parsed = series_array.each_with_object('') do |a, result| | ||
| result << if a[0].zero? | ||
| '00 ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ternary if/else operator might be used here as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vasil-gochev you mean something like this ?
serie[0].zero? ? result << '00' : result << '0'
Training/Easy/chuck_norris.rb
Outdated
| output = norris_parse array | ||
| array = split_series(binary.each_char.map(&:to_i)) | ||
| output = norris_parse(array) | ||
| puts output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as #1 - you can print the answer after getting the answer outside of the class
Training/Easy/chuck_norris.rb
Outdated
| @@ -27,25 +27,24 @@ def split_series(array) | |||
| result | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need an attr_accessor for :message if you're not going to set it
No description provided.