Hackerrank Lambdas Solution
Lambdas are 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…
Lambdas are 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…
Passing blocks is one way to pass functions as arguments to other functions. Blocks are one of the very few exceptions to the "everything is an object" rule in Ruby. Blocks are not objects, and they can't be saved to…
Higher order functions are one of the key components of functional programming. A higher order function is a tool that takes other functions as parameters or returns them as a result. Blocks are nameless methods that…
In our previous challenges, we explored some ways to pass arguments to our methods; however, they were limited in terms of the number of arguments we can pass to them. Using default parameters allows us to lower the…
In our previous challenges, we have been using methods (def method() .. endconstruct) to abstract compound operations, perform data manipulations and learn various concepts of the language, without talking in much…
In this challenge, we will show you ways in which we can add key-value pairs to Hash objects, delete keys from them, and retain them based on a logic. Consider the following Hash object: A new key-value pair can be…
You've seen the control structure each used on an array. Similarly, it is available for the Hash collection, as well. On Hash, it works in two ways. Consider the example Using each, each element can be iterated as or…
Hashes, also called associative arrays, are dictionary-like data structures which are similar to arrays. Instead of using integers to index an object, however, hashes use any object as its index. In this challenge, your…
The array class also allows to select and return a subset of an array based on some criteria defined in a block (a block is a group of code within {} that accepts a variable and returns a value). Selecting elements that…
The array class has various methods of removing elements from the array. Let's look at the array arr = [5, 6, 5, 4, 3, 1, 2, 5, 4, 3, 3, 3] Delete an element from the end of the array > arr.pop => 3 Delete an element…
Arrays provide a variety of methods that allow to add elements to them. push allows one to add an element to the end of the list. >x = [1,2] >x.push(3) => [1,2,3] insert allows one to add one or more elements starting…
Here are some other ways to access array objects in Ruby. To access the elements from the end of the list, we can use negative indices. For the array, arr = [9, 5, 1, 2, 3, 4, 0, -1] > arr[-1] => -1 The first element of…
Array collections offer various ways to access their elements. The positions are 0 indexed. Objects of the array can be accessed using the [] method which may take various arguments, as explained below. arr = [9, 5, 1…
One of the most commonly used data structures in Ruby is a Ruby Array, and below we see various methods of initializing a ruby array. Your task is to initialize three different variables as explained below. Initialize…
This challenge has a beautiful one-liner answer. "A hacker practices on HackerRank until getting to a rating of O(1) read as (Oh-one)" Call the method coder.practice until coder.oh_one? becomes true. Use the until…
This is a wonderful exercise which explains the concept of infinite loops. "A hacker practices on HackerRank until getting to a rating of O(1) read as (Oh-one)" An infinite loop in Ruby is of the form Use an infinite…
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…
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…
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…
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 an even? method associated with it, which returns true or false based on…