Gsub is producing a template string with '%s', this is later replaced by the value of s.delete(' ').chars.reverse, which is the string in reverse without spaces.
How?
Well, the % symbol between the two expressions is actually a formatting method, from the String class.
It recognizes the '%s' & replaces it with the value.
This means that s.gsub(/\w/, '%s') will produce strings like this:
"%s%s %s%s%%s %s"
Then this template is replaced into the final solution.
Hi, great job here with the string formatting, clever use of the clamp method, and the case statement.
Just two things I'd like to point out, if you're open to suggestions.
I like having blank lines between code sections (setup, logic, result) so I can quickly appreciate the different things that are happening inside, this reduced cognitive load
I don't like the else in this case statement, it makes an unecessary assumption, that the multiplication operation is the default. What if the packet instructions change? Maybe a new instruction is added that is not multiplication, but because you're defaulting to it this would result in an error in your program.
I like what you did with the string formatting at the end.
But I think you can improve this.
Here's how:
We usually put value constants as all capital letters so we can tell them apart from class names, which are also constants.
Also it helps to give it a name that means something, not an abreviation.
Because these sections represent math operations, I would name this constant OPERATIONS, or if you would like to follow the problem domain vocabulary you could use INSTRUCTIONS.
As a result, you get code that's easier to read & make sense of.
This comment is hidden because it contains spoiler information about the solution
using count count is inefficient
using count count is inefficient
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Beautiful solution!
Interesting solution!
For anyone wondering how this works...
Gsub is producing a template string with
'%s'
, this is later replaced by the value ofs.delete(' ').chars.reverse
, which is the string in reverse without spaces.How?
Well, the
%
symbol between the two expressions is actually a formatting method, from theString
class.It recognizes the
'%s'
& replaces it with the value.This means that
s.gsub(/\w/, '%s')
will produce strings like this:Then this template is replaced into the final solution.
Hope this helps! :)
Thank you very much!
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
What happens if I call your method with a Range instead of an Array?
Ranges implement
each
, but notjoin
.This comment is hidden because it contains spoiler information about the solution
Hi, great job here with the string formatting, clever use of the
clamp
method, and the case statement.Just two things I'd like to point out, if you're open to suggestions.
Let me know what you think :)
I like what you did with the string formatting at the end.
But I think you can improve this.
Here's how:
We usually put value constants as all capital letters so we can tell them apart from class names, which are also constants.
Also it helps to give it a name that means something, not an abreviation.
Because these sections represent math operations, I would name this constant
OPERATIONS
, or if you would like to follow the problem domain vocabulary you could useINSTRUCTIONS
.As a result, you get code that's easier to read & make sense of.
This comment is hidden because it contains spoiler information about the solution
Loading more items...