Dmytr1K
4/17/2016 - 8:26 PM

Lesson 3

Lesson 3

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

films = []
File.open("movies.txt") {|f| films = f.map {|l| data_fields.zip(l.split("|")).to_h} }
# films = File.readlines("movies.txt").map {|f| data_fields.zip(l.chomp.split("|")).to_h }
# films = File.read("movies.txt").split("\n").map {|f| data_fields.zip(l.split("|")).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!):"
films.map {|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: "
films.map {|k| k[:actors].chomp.split(",")} .flatten.sort.group_by {|i| i} .each {|k, v| puts "#{k}: #{v.length} films"}