Skip to content

Instantly share code, notes, and snippets.

@brenopoggiali
Last active January 30, 2019 17:36
Show Gist options
  • Select an option

  • Save brenopoggiali/6ee0e28394cb7df70692733869241d17 to your computer and use it in GitHub Desktop.

Select an option

Save brenopoggiali/6ee0e28394cb7df70692733869241d17 to your computer and use it in GitHub Desktop.
Program that prints the number of lines spoken by each character in "The Tragedy of Macbeth" by Shakespeare to train my Ruby skills
source 'https://rubygems.org'
ruby '2.5.1'
gem "nokogiri", "~> 1.10.1"
require 'nokogiri'
require 'open-uri'
print "Copy your play.xml URL here: "
play = gets.chomp
doc = Nokogiri::XML(open(play))
speeches = doc.xpath('//SPEECH')
people = Hash.new
speeches.each do |speech|
person = speech.at('./SPEAKER').content
people[person] ||= 0
speech.xpath('LINE').each do |line|
people[person] += 1
end
end
people.reject! { |key, value| key == "ALL" }
people = people.sort { |key, value| value[1]<=>key[1] }
people.each do |person, lines|
puts "#{lines} #{person}"
end
@brenopoggiali
Copy link
Author

Engraçado que não consegui usar o sort!. Imagino que talvez ele não funcione para Hash. Ai acabei fazendo people = mesmo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment