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 Lambdas problem solution in ruby

YASH PAL, 31 July 2024

In this HackerRank Lambdas, the problem solution in ruby programming Lambdas is anonymous functions. Lambdas in Ruby are objects of the class Proc. They are useful in most of the situations where you would use a proc. The simplest lambda takes no argument and returns nothing as shown below:

Example:

#Ruby version <= 1.8

    lambda { …. } 

    lambda do

        ….

    end

#Ruby version >= 1.9, “stabby lambda” syntax is added

    -> { …. }

    -> do

        ….

    end

Ruby version >= 1.9 can use both lambda and stabby lambda, ->.

Lambdas can be used as arguments to higher-order functions. They can also be used to construct the result of a higher-order function that needs to return a function.

You are given a partially complete code. Your task is to fill in the blanks (______).

There are 5 variables defined below:

  1. square is a lambda that squares an integer.
  2. plus_one is a lambda that increments an integer by 1.
  3. into_2 is a lambda that multiplies an integer by 2.
  4. adder is a lambda that takes two integers and adds them.
  5. values_only is a lambda that takes a hash and returns an array of hash values.
hackerrank lambdas problem solution in ruby programming

Problem solution.

# Write a lambda which takes an integer and square it
square      = ->(a) { a * a }

# Write a lambda which takes an integer and increment it by 1
plus_one    = ->(a)  { a += 1}

# Write a lambda which takes an integer and multiply it by 2
into_2      = ->(a) { a * 2 } 

# Write a lambda which takes two integers and adds them
adder       = ->(a,b) { a + b } 

# Write a lambda which takes a hash and returns an array of hash values
values_only = ->(a) { a.values } 


input_number_1 = gets.to_i
input_number_2 = gets.to_i
input_hash = eval(gets)

a = square.(input_number_1); b = plus_one.(input_number_2);c = into_2.(input_number_1); 
d = adder.(input_number_1, input_number_2);e = values_only.(input_hash)

p a; p b; p c; p d; p e

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