Skip to main content

Hackerrank > Ruby > Tutorial

                                  Tutorial

01-Hello World!

Let's get started with Ruby!
Go ahead and type the following code in the code-editor:
print "Hello HackerRank!!"
Solution
print "Hello HackerRank!!"

02-Everything is an Object

Everything is an object in Ruby. For instance, if you type
print self
in the code-editor, Ruby treats self as the object in which it is currently referred to.
Solution
print self

03-Object Methods

Each object in Ruby may have methods associated with it. To demonstrate this, we want you to print whether a number is even or odd. A number has aneven? method associated with it, which returns true or false based on the parity of the number.
Assuming a variable number is already defined, check whether a given numberis even or not.
Hint
Type in the code-editor
return number.even?
or
number.even?
Solution
number.even?

04-Object Method Parameters

A method may take zero or more parameters as input. To demonstrate this, we look at the asserts we use on HackerRank. Sometimes, we have to check whether a given number a is within the range b and c (where b  c, and both inclusive ).
Three variables ab, and c are already defined. Your task is to write code that checks whether a is within the range of b and c by calling the methodrange? (which we have defined for you as a method for this example) on aand passing b and c as arguments.
Hint
a.range?(b, c)
or
return a.range?(b, c)
or
a.range? b, c
or
return a.range? b, c
Solution
a.range? b, c

Comments

  1. really Good blog post.provided a helpful information.I hope that you will post more updates like this Ruby on Rails Online Training

    ReplyDelete

Post a Comment

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

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
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as