4/17/2016 - 8:26 PM

Lesson 3

Lesson 3

data_fields = [:url, :name, :year, :country, :date, :genre, :time, :rating, :director, :actors]

films = []"movies.txt") {|f| films = {|l|"|")).to_h} }
# films = File.readlines("movies.txt").map {|f|"|")).to_h }
# films ="movies.txt").split("\n").map {|f|"|")).to_h }

puts "\n5 longest movies:"
films.sort_by {|f| f[:time].delete(" min").to_i} .reverse.first(5).each {|f| puts f[:name] + " : " + f[:time]}

puts "\nComedy, sorted by the release date:"
films.sort_by {|f| f[:date]} .select {|f| f[:genre].include?("Comedy")} .each {|f| puts f[:name] + " : " + f[:date]}

puts "\nA list of all the directors in alphabetical order (no repeats!):" {|f| f[:director]} .sort.uniq.each {|f| puts f}

print "\nThe number of films made not in the USA: "
puts films.reject {|f| f[:country].include?("USA")} .length

# Bonus!

puts "\nThe number of films grouped by director: "
films.group_by {|f| f[:director]} .sort_by {|v|} .each {|k, v| puts "#{k}: #{v.length} films"}

puts "\nThe number of films in which starred each actor: " {|k| k[:actors].chomp.split(",")} .flatten.sort.group_by {|i| i} .each {|k, v| puts "#{k}: #{v.length} films"}