You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank Ruby - Strings - Iteration Solution

Hackerrank Ruby - Strings - Iteration Solution

Beeze Aal
Beeze Aal

In our encoding tutorial, we learned about the different ways Ruby 1.8 and Ruby 1.9 (and higher versions) represent strings internally. The major difference is a wide range of encoding (non-ascii) support in the later versions. This change, however, also overhauls the way strings were iterated between the two versions.

In Ruby 1.8, there's a single each method (remember Enumerable?) which allowed it to iterate over lines of data. While it might seem like a logical option to have, how would one go about iterating on each byte or each character? It turns out that it was not so clean, and people had to resort to tricks for some of these functionalities.

With Ruby 1.9, each was removed from the String class and is no longer an Enumerable. Instead, we have more explicit choices based on what we need to iterate - bytes, chars, lines or codepoints.

  • each_byte iterates sequentially through the individual bytes that comprise a string;
  • each_char iterates the characters and is more efficient than [] operator or character indexing;
  • each_codepoint iterates over the ordinal values of characters in the string;
  • each_line iterates the lines.

For example:> money = "¥1000"> money.each_byte {|x| p x} # first char represented by two bytes19416549484848> money.each_char {|x| p x} # prints each character"¥""1""0""0""0"

Without a doubt, Ruby 1.9 makes iteration easier to understand and implement. Hence, we'll stick with Ruby 1.9 and later versions for current and other challenges (unless otherwise stated).

Challenge: Write the method count_multibyte_charwhich takes a string as input and returns the number of multibyte characters (byte size > 1) in it.

For example:> count_multibyte_char('¥1000')1

Solution in ruby

Approach 1.

# Your code here
def count_multibyte_char(s)
    s.chars.count { |c| c.bytesize > 1 }

Approach 2.

# Your code here
def count_multibyte_char(n)
  cnt = 0

  n.each_char{|x| cnt += 1 if x.bytesize > 1}


Approach 3.

# Your code here
def count_multibyte_char(str)
    counter = 0
    str.each_char {|c| counter = counter + 1 if c.bytesize > 1}