Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

HackerRank Ruby – Enumerable – collect problem solution

YASH PAL, 31 July 2024

In this HackerRank Ruby – Enumerable – collect problem solution Ruby Enumerable provides a number of important higher order constructs that we shall explore in further challenges. One of such important methods is collect method, also known as map.

map as the name may suggest, takes a function and maps (applies) it to a collection of values one by one and returns the collection of result.

That is,

    map(f(x), [x1,x2,x3,..,xn]) -> [f(x1),f(x2),..,f(xn)]

This single powerful method helps us to operate on a large number of values at once.

For example,

>>> [1,2,3].map { |x| 2*x }

=> [2, 4, 6]

>>> {:a=>1, :b=>2, :c=>3}.collect { |key, value| 2*value }

=> [2, 4, 6]

Note that these methods are different from each in the respect that these methods return a new collection while former returns the original collection, irrespective of whatever happens inside the block.

In this challenge, your task is to write a method which takes an array of strings (containing secret enemy message bits!) and decodes its elements using ROT13 cipher system; returning an array containing the final messages.

For example, this is how ROT13 algorithm works,

Original text:

Why did the chicken cross the road?

Gb trg gb gur bgure fvqr!

On application of ROT13,

Jul qvq gur puvpxra pebff gur ebnq?

To get to the other side!

HackerRank Ruby - Enumerable - collect problem solution

Problem solution.

def rot13(secret_messages)
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    
    secret_messages.map do |msg|
        msg.scan(/./).map do |c|
            c == ' ' ? c : alphabet[alphabet.index(c) - 13]
        end.join ''
    end.to_a
end

Second solution.

def rot13(secret_messages)
    secret_messages.map{|e| e.tr 'A-Za-z','N-ZA-Mn-za-m'}
end

Third solution.

def rot13(secret_messages)
    messages = []
    secret_messages.each { |msg| messages << msg.tr('A-Za-z', 'N-ZA-Mn-za-m') }
    messages
end

coding problems solutions Ruby Solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes