You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank Ruby Hash - Initialization Solution

Hackerrank Ruby Hash - Initialization Solution

Beeze Aal
Beeze Aal

.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}