Ruby JOB Interview Question – Parsing JSON and displaying movies in ascending order with average rating

I came across one site for javascript conferences where there was one job posting linked to this ruby quiz. I am just novice ruby developer so I tried to solve this job level quiz to check my ruby skills.

ruby_quiz

You could download (pull) this quiz repository from employer’s github page.
Problem – Parse json file stored in data/movies.json and sort them alphabetically with average rating.

Json File:-

{
  "movies": [
    {
      "name": "Inception",
      "rating": 8.8,
      "genres": [
        "Action",
        "Adventure",
        "Sci-Fi"
      ]
    },
    {
      "name": "Godfather",
      "rating": 9.2,
      "genres": [
        "Crime",
        "Drama"
      ]
    },
    {
      "name": "The Pink Panther",
      "rating": 5.3,
      "genres": [
        "Adventure",
        "Comedy",
        "Crime"
      ]
    },
    {
      "name": "12 Angry Men",
      "rating": 8.9,
      "genres": [
        "Drama"
      ]
    },
    {
      "name": "Citizen Kane",
      "rating": 8.5,
      "genres": [
        "Drama",
        "Mystery"
      ]
    },
    {
      "name": "Remember the Titans",
      "rating": 7.6,
      "genres": [
        "Biography",
        "Drama",
        "Sport"
      ]
    }
  ]
}

Ruby Implementation:-

  • First of all use json class to parse data with
    JSON.parse(IO.read(FILE_PATH))
  • Then create two hash variables. One to store Movies Genres and Other to store Ratings.
  • Our Goal is to print on console like this
...
7.25 Crime
8.55 Drama
8.50 Mystery
...

 

  • So, iterate over variable[‘movies’] using each iteration function.
  • Then check for if genre exists for the movie or not?
    variable[‘movies’][index][‘genres’] > 0
  • If genre exist just store into genre hash key and rating as value, however if hash key already exist add its rating to previous value. Also, store occurrence of genre into another hash with genre as key and occurrence as value.
if sort_data[k1] ==  nil
				sort_data[k1] = k['rating']
				sort_data_type[k1] = 1
			else
				sort_data_type[k1] = sort_data_type[k1] + 1
				temp = k['rating'] + sort_data[k1]
				sort_data[k1] = temp
end
  • Now sort genre=>rating hash with ruby sort function
    sort_data = sort_data.sort
  • Iterate it and display average rating on console to finish up assignment.
sort_data.each do |k,v|
	rating = v/sort_data_type[k]
	print "%.2f #{k}\n" %rating
end

So Final Ruby Code would be

 

# This is a sample file with only the outline of the program to get you
# started if you're using Ruby. Edit this file and modify the code to get the
# correct output.

require 'json' # `gem install json` if you don't already have json gem installed

raw_json = IO.read('data/movies.json')

json_parse = JSON.parse(raw_json)
sort_data = Hash.new
sort_data_type = Hash.new

json_parse['movies'].each do |k|
	if k['genres'].length > 0
		k['genres'].each do |k1|

			if sort_data[k1] ==  nil
				sort_data[k1] = k['rating']
				sort_data_type[k1] = 1
			else
				sort_data_type[k1] = sort_data_type[k1] + 1
				temp = k['rating'] + sort_data[k1]
				sort_data[k1] = temp

			end
		end 
	end

end
sort_data = sort_data.sort
sort_data.each do |k,v|
	rating = v/sort_data_type[k]
	print "%.2f #{k}\n" %rating
end

My console output is.

8.80 Action
7.05 Adventure
7.60 Biography
5.30 Comedy
7.25 Crime
8.55 Drama
8.50 Mystery
8.80 Sci-Fi
7.60 Sport
[Finished in 0.5s]
Total
0
Shares
Previous Post

HTML select element manipulation using jQuery

Next Post

How to track remote javascript errors using Google Analytics?

Related Posts