Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Ruby – Strings – Iteration problem solution

YASH PAL, 31 July 2024

In this HackerRank Ruby – Strings – Iteration problem solution 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 bytes

194

165

49

48

48

48

> 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_char which takes a string as input and returns the number of multibyte characters (byte size > 1) in it.

For example:

> count_multibyte_char(‘¥1000’)

1

HackerRank Ruby - Strings - Iteration problem solution

Problem solution.

# Your code here
def count_multibyte_char (str)
    character_count = 0
    str.each_char do|char| 
        byte_count = 0
        char.each_byte do |byte|
            byte_count += 1
        end
        if byte_count > 1
            character_count += 1
        end
    end
    return character_count
end

Second solution.

# Your code here
def count_multibyte_char(s)
    count = 0
    s.each_char do |c|
        count += 1 if c.bytesize > 1
    end
    count
end

Third solution.

# Your code here
def count_multibyte_char(str)
  str.each_codepoint.count{|bytes| bytes > 256}
end

coding problems solutions Ruby Solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes