Last active
December 14, 2017 17:07
-
-
Save Casual3498/bd687d7ba04e9ba543b13c917e1af5dc to your computer and use it in GitHub Desktop.
3lesson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
filename = 'movies.txt' | |
keys_array = %i[href name year country date genres duration rank director actors] | |
films_array =[] | |
def output_films (arr) | |
arr.each do |film| | |
puts "#{film[:name]} :(#{film[:date]} ; #{film[:genres]}) - #{film[:duration]}" | |
puts "-----------" | |
end | |
end | |
#check if exists command line parameter | |
filename = ARGV[0] || filename | |
#check if exists file | |
unless File.exist?(filename) | |
puts "File #{filename} not found!" | |
return | |
end | |
#parse file and fill films_array | |
File.open(filename) do |file| | |
films_array = file.map { |string| keys_array.zip(string.split('|')).to_h } | |
end | |
puts "5 longest films:" | |
puts "----------------" | |
output_films films_array.sort_by { |x| x[:duration][0..-3].to_f }.reverse.take(5) | |
puts "\n\n" | |
puts "10 comedys by date:" | |
puts "----------------_" | |
output_films films_array.select { |x| x[:genres].include?('Comedy') }.sort_by {|x| x[:date] }.take(10) | |
puts "\n\n" | |
puts "All directors:" | |
puts "----------------" | |
puts films_array.map { |elem| elem[:director] }.uniq.sort_by { |x| x.split(' ').last } | |
puts "\n\n" | |
puts "non USA films count: " | |
puts films_array.count { |film| !film[:country].include?('USA') } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment