Skip to main content

Hackerrank > Ruby > Enumerable



In control structures tutorial, we learned about various methods to iterate over a collection like unlessloop and the most commonly used each method.
Ruby, however, provides an Enumerable module which packages a bunch of methods which can be used with any other class by including it (referred to asmixing in). That means that programmers don't have to write all those methods many different times for different objects. As long as the custom object defines an each method and includes Enumerable module, it can get access to all of its magic.
In this challenge, you have been provided with a custom object called colorsthat defines it's own each method. You need to iterate over the items and return an Array containing the values.
def iterate_colors(colors)
    arr = []
    colors.each do |n|


In the previous challenge, we learned about each method being central to all of the methods provided by Enumerable class. One of such useful methods iseach_with_index which allows you to iterate over items along with an index keeping count of the item.
For example,
> colors = ['red', 'green', 'blue']
> colors.each_with_index { |item, index| p "#{index}:#{item}" }
As you can note, the counting of items starts from 0.
In this challenge, your task is to complete the skip_animals method that takes an animals array and a skip integer and returns an array of all elements except first skip number of items.
For example,
> skip_animals(['leopard', 'bear', 'fox', 'wolf'], 2)
=> ["2:fox", "3:wolf"]
It is guaranteed that number of items in animals array is greater than the value of skip.
def skipanimals(animals, skip)
    arr = []
    animals.eachwith_index do |item, index|
        arr.push("#{index}:#{item}") if index >=skip
    return arr


Beside simple methods to iterate over objects, 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 asmap.
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,
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!
def rot13(secret_messages) { |x|'A-Za-z', 'N-ZA-Mn-za-m') }


A common scenario that arises when using a collection of any sort, is to get perform a single type of operation with all the elements and collect the result.
For example, a sum(array) function might wish to add all the elements passed as the array and return the result.
A generalized abstraction of same functionality is provided in Ruby in the name of reduce (inject is an alias). That is, these methods iterate over a collection and accumulate the value of an operation on elements in a base value using an operator and return that base value in the end.
Let's take an example for better understanding.
>>> (5..10).inject(1) {|product, n| product * n }
=> 151200
In above example, we have the following elements: a base value 1, an enumerable (5..10), and a block with expressions instructing how to add the calculated value to base value (i.e., multiply the array element to product, where product is initialized with base value)
So the execution follows something like this:
# loop 1
n = 1
product = 1
return value = 1*1

# loop 2
n = 2
product = 1
return value = 1*2

n = 3
product = 2
return value = 2*3

As you can notice, the base value is continually updated as the expression loops through the element of container, thus returning the final value of base value as result.
Other examples,
>>> (5..10).reduce(1, :*)   # :* is shorthand for multiplication
=> 151200
Consider and arithmetico-geometric sequence where the nth term of the sequence is denoted by tn=n2+1,n>0 . In this challenge, your task is to complete the sum method which takes an integer n and returns the sum to the n terms of the series.
def sum_terms(n)
    series = []
    1.upto(n) do |i|
        series.push(i ** 2 + 1)
    series.reduce(0, :+)

05-any, all, none, find

Ruby offers various enumerables on collections that checks for validity of the objects within it. Some of them are
Consider the following example
> arr = [1, 2, 3, 4, 5, 6]
 => [1, 2, 3, 4, 5] # and
> h = {"a" => 1, "b" => 2, "c" => 3}
 => {"a" => 1, "b" => 2, "c" => 3}
any? : This method returns false if the block never returns true on passing each element of the collection to it.
> arr.any? {|a| a % 2 == 0} # checks if any number in the array is even
 => True
> h.any? {|key, value| value.is_a? String} # checks if any value of the Hash object is of the type String
 => False
all? : This method returns true if the block never returns false or nil on passing each element of the collection to it.
> arr.all? {|a| a.is_a? Integer} # checks if all elements of the array are of the type Integer
 => True
> h.all? {|key, value| key.is_a? String} # checks if all keys of the Hash object are of the type String
 => True
none? : This method returns true if the block never returns true for any of the elements passed through it.
> arr.none? {|a| a.nil?} # Checks if none of the elements in the array are of nil type
 => True
> h.none? {|key, value| value < 3} # checks if all values of the Hash object are less than 3
 => False
find : This method returns the first element when passed to a block return anything other than false
> arr.find {|a| if a > 5} # returns the first element greater than 5 and `nil` if none satisfies the condition
 => True
> h.find? {|key, value| key == "b"} # returns an Array of the first match [key, value] that satisfies the condition and nil otherwise
 => ["b", 2]
Based on what you have learnt above, your task is to complete the functions given in the code-editor.
# Check and return if any key object within the hash is of the type Integer
def funcany(hash)
    hash.any? {|key, value| key.isa? Integer}
# Check and return if all the values within the hash are Integers and are < 10
def funcall(hash)
    hash.all? {|key, value| value.isa? Integer and value < 10}
# Check and return if none of the values within the hash are nil
def funcnone(hash)
    hash.none? {|key, value| value.nil?}
# Check and return the first object that satisfies the property
# [key, value] pair where the key is an Integer and the value is < 20 
# or [key, value] pair where the key is a String and the value is a String starting 
# with the character a
def funcfind(hash)
    hash.find { |key, value| (key.isa? Integer and value.isa? Integer and value < 20) || (key.isa? String and value.isa? String and value.split(//).first == "a") }


Another function often used with data collections is one which groups the elements according to some evaluation result.
Consider the following example. Let's say you have a list of 100 integers and you want to group them according to their even and odd value.
In Ruby, you can easily do this by using group_by method provide by Enumerable module.
> (1..5).group_by {|x| x%2}
{1=>[1,3,5], 0=>[2, 4]}
Of course this is a very simple example and it's use can be of much complicated nature.
In this challenge, your task is to group the students into two categories corresponding to their marks obtained in a test. The list of students will be provided in a marks hash with student name as key and marks obtained (out of 100) as value pair, along with the pass_marks as argument.
The method group_by_marks must return a Hash containing an array of students who passed as value to Passed key, and those who failed as value toFailed key. If a particular key is empty, don't return that key.
For example,
> marks = {"Ramesh":23, "Vivek":40, "Harsh":88, "Mohammad":60}
> group_by_marks(marks, 30)
=> {"Failed"=>[[:Ramesh, 23]], "Passed"=>[[:Vivek, 40], [:Harsh, 88], [:Mohammad, 60]]}
def group_by_marks(marks, n)
    marks.group_by { |key, value| value<n ? "Failed" : "Passed" }


Popular posts from this blog

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …

Hackerrank > SQL > Basic Select

01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
02-Select by ID
Given a City table, whose fields are described as