Skip to main content

Hackerrank > Ruby > Control Structures

                       Control Structures

01-Each

Ruby offers control structures that let you iterate through its collections. One such control structure is each.
As you already know, HackerRank conducts many contests, and for every user who participates in a contest we update their score once the contest ends. You will be given a method called scoring with an array passed as an argument. Elements of the array are of the class User.
User class has a method update_score.
Your task is to iterate through each of the elements in the array using each and call the method update_score on every element.
Hint
array.each do |user|
    # call update_score on `a` here
end
Solution
def scoring(array)
    array.each do |user|
        user.update_score
    end
end

02-Unless

You've updated the score of every HackerRank user who participated in a contest. Sometimes, HackerRank admins also participate in a given contest but care is taken to ensure that their submissions do not get any score and their score is not updated.
Like the previous challenge, you are given a method scoring with an array passed as an argument. Each element of the array is of class User.
User has two public methods, is_admin? and update_score. Your task in this challenge is to use the control structure unless and update all elements of the array who are not admins.
Hint
unless user.is_admin?
    user.update_score
end
or
user.update_score unless user.is_admin? 
The above code is a Ruby one liner.
Explanation
unless is the logical equivalent of if not
Solution
def scoring(array)
    array.each do |user|
        user.updatescore unless user.isadmin?
    end
end

03-Infinite Loop

This is a wonderful exercise which explains the concept of infinite loops.
"A coder practices on HackerRank until he reaches the rating O(1) read as (Oh-one)"
An infinite loop in Ruby is of the form
loop do
end
Use an infinite loop and call the method coder.practice within it and break ifcoder.oh_one? is true.
break if conditions in Ruby are of the form
if <condition>
    break
end
or a one-liner
break if <condition>  
Solution
loop do
    coder.practice
    break if coder.oh_one?
end

04-Until

This challenge has a beautiful one-liner answer.
"A coder practices on HackerRank until he reaches the rating O(1) read as (Oh-one)"
Call the method coder.practice until coder.oh_one? becomes true.
Use the until control structure.
until is the logical equivalent of while not.
Hint
while not <condition>
    <code>
end
or
until <condition>
    <code>
end
or the beautiful one-liner
<code> until <condition>  
Solution
coder.practice until coder.oh_one?

05-Case

This is a bonus question. Feel free to skip to the next challenge.
HackerRank is written in RoR and we have various classes defined in it. Some of them are
  1. Hacker
  2. Submission
  3. TestCase
  4. Contest
etc.
You have been given a function where an object which may or may not be of the above mentioned type is sent as an argument. You have to use the case control structure in Ruby to identify the class to which the object belongs and print the following output:
  • if Hacker, output "It's a Hacker!"
  • if Submission, output "It's a Submission!"
  • if TestCase, output "It's a TestCase!"
  • if Contest, output "It's a Contest!"
  • for any other object, output "It's an unknown model"
Note
  • use case (switch statement of Ruby)
  • use puts for printing
Solution
def identify_class(obj)
    case obj
    when Hacker
        puts "It's a Hacker!"
    when Submission
        puts "It's a Submission!"
    when TestCase
        puts "It's a TestCase!"
    when Contest
        puts "It's a Contest!"
    else
        puts "It's an unknown model"
    end
end

Comments

Popular posts from this blog

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…

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