Close
Close

Practice questions on Hashes

Level 1

1.
Ask user to give name and marks of 10 different students. Store them in hash.

2.
Sort the hash created in previous example according to marks.
This type of example is given in tutorial

3.
Use hash to store antonyms of words. E.g.- 'Right'=>'Left', 'Up'=>'Down', etc. Display all words and then ask user to enter a word and display antonym of it.

4.
Count the number of occurrence of each leter in word "MISSISSIPPI". Store count of every leter with the leter in a dictionary.

5.
From the previous question, sort according to the number of letters.

6.
Take an array containg only strings. Now, take a string input from user and rearrange the elements of the list according to the number of occurence of the string taken from user in the elements of the list.
E.g.-LIST : ["no bun","bug bun bug bun bug bug","bunny bug","buggy bug bug buggy"]
STRING TAKEN : "bug"
OUTPUT LIST:["bug bun bug bun bug bug","buggy bug bug buggy","bunny bug","no bun"]
a = ["no bun","bug bun bug bun bug bug","bunny bug","buggy bug bug buggy"]
b = "bug"
c = {}
for i in a
  count = 0
  for j in i.split()
    if j == b
      count = count+1
    end
  end
  c[count]=i
end
d = []
for s in c.keys.sort
  d.push(c[s])
end
puts "#{d.reverse}"

Level 2

Level 3

Ask Yours
Post Yours