Skip to content

Instantly share code, notes, and snippets.

@Prigin
Last active October 18, 2019 15:01
Show Gist options
  • Save Prigin/91b919f76d3d50124ac25f5704774a17 to your computer and use it in GitHub Desktop.
Save Prigin/91b919f76d3d50124ac25f5704774a17 to your computer and use it in GitHub Desktop.
final(?) task 4
require 'csv'
require 'ostruct'
doc = "movies.txt"
syms = [:link, :title, :year, :country,
:date, :genre, :duration,
:rating, :director, :stars]
mas = CSV.readlines(doc, headers: syms, col_sep: "|").map{|unit| OpenStruct.new(unit.to_h)}
def Show_em (array_hash)
array_hash.each do |unit|
puts unit.title + " (" + unit.date + "; " + unit.genre + ") - " + unit.duration
end
end
puts
puts"=========LONGEST========="
Show_em mas.max_by(5){|a| a.duration.split(" ").first.to_i}
puts
puts"====EARLIEST_COMEDIES===="
Show_em mas.select{|unit| unit.genre.include?("Comedy")}.min_by(10, &:date)
puts
puts"========DIRECTORS========"
puts mas.collect(&:director).uniq.sort_by{|a| a.split(" ").last}
puts
puts"=========NOT_USA========="
puts mas.collect(&:country).count{|unit| unit != "USA"}
puts
#months = [["jan", 0],["feb", 0], ["mar", 0], ["apr", 0], ["may", 0], ["jun", 0],["jul", 0], ["aug", 0], ["sep", 0], ["oct", 0], ["nov", 0], ["dec", 0]]
#mas.each do |unit|
# months.each_index{|index| months[index][1] += 1 if index + 1 == unit.date.split("-")[1].to_i}
#end
#months = mas.each_with_object(
# [["jan", 0], ["feb", 0], ["mar", 0],
# ["apr", 0], ["may", 0], ["jun", 0],
# ["jul", 0], ["aug", 0], ["sep", 0],
# ["oct", 0], ["nov", 0], ["dec", 0]]) do |base, unit|
# buff = base.date.split("-")[1]
# unit[buff.to_i-1][1] += 1 if buff != nil
#end
#months.each{|unit| puts unit[0] + " -->" + unit[1].to_s + " title(s)"}
months = mas.each_with_object(Hash.new(0)){|unit, hash_new|
hash_new[unit.date.split("-")[1]] += 1}.reject{|unit| unit == nil}.sort
months.each{|mon, count| puts "#{Date::MONTHNAMES[mon.to_i]} -->#{count} title(s)"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment