Hackerrank Ruby Hash - Initialization Solution
1 min read

Hackerrank Ruby Hash - Initialization Solution

Hackerrank Ruby Hash - Initialization Solution

.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue}

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 task is to create three different Hash collections as explained below.

  • Initialize an empty Hash with the variable name empty_hash

Hint

empty_hash = Hash.new 
  • Initialize an empty Hash with the variable name default_hash and the default value of every key set to 1.

Hint

default_hash = Hash.new(1)

or

default_hash = Hash.new
default_hash.default = 1

Initialize a hash with the variable name hackerrank and having the key-value pairs

"simmy", 100  
"vivmbbs",200

Hint

hackerrank = {"simmy" => 100, "vivmbbs" => 200}

Hash can be defined using a new method

hackerrank = Hash.new
hackerrank["simmy"] = 100
hackerrank["vivmbbs"] = 200

Solution in ruby

Approach 1.

# Initialize 3 variables here as explained in the problem statement
empty_hash = {}
default_hash = Hash.new(1)
hackerrank = {"simmy" => 100, "vivmbbs" => 200}

Approach 2.

# Initialize 3 variables here as explained in the problem statement
empty_hash = Hash.new
default_hash = Hash.new(1)
hackerrank = {"simmy" => 100, "vivmbbs" => 200}

Approach 3.

# Initialize 3 variables here as explained in the problem statement
empty_hash = Hash.new
default_hash = Hash.new(1)
hackerrank = {"simmy" => 100, "vivmbbs" => 200}

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×