Skip to content

Instantly share code, notes, and snippets.

@BrainScraps
Forked from janester/parsing.rb
Last active December 19, 2015 02:18
Show Gist options
  • Save BrainScraps/5881612 to your computer and use it in GitHub Desktop.
Save BrainScraps/5881612 to your computer and use it in GitHub Desktop.
I did not do the bonuses. But whatever.
require "pry"
require "pry-debugger"
require "soundcloud"
require "nokogiri"
require "open-uri"
#1. Parse through the wdi2 hash and make a function that returns a hash where the
#keys are each persons name and the value is their favorite movies
wdi2 = {:instructors => [
{:name => "jane", :gender => "female", :favorite_movies => ["the dark knight rises", "the dark knight", "batman begins"], :favorite_foods =>["tacos", "tamales", "enchiladas"]},
{:name => "sumeet", :gender => "male", :favorite_movies => ["x-men", "x2", "x3: the last stand", "x-men: first class"], :favorite_foods=>["pizza", "pasta", "cheese"]},
{:name => "dwayne", :gender => "male", :favorite_movies=>["harry potter 1", "harry potter 2", "harry potter 3", "harry potter 4"]}],
:students => [
{:name => "tom", :favorite_movie => "the hunger games"},
{:name => "christina", :favorite_movie=>"my fair lady"},
{:name=>"baylee", :favorite_movie=>"mary poppins"},
{:name =>"jason", :favorite_movie =>"the sound of music"},
{:name => "zara", :favorite_movie => "the princess diaries"}],
:date => "6/27/13"}
def stringify_movies(array)
result = ""
length = array.length
array.each_with_index {|x, i|
if length == i + 1
result << "#{x}"
else
result << "#{x}, "
end }
result
end
def name_movies(hash)
result = {}
hash[:instructors].each {|person|
result[person[:name]] = stringify_movies(person[:favorite_movies])
}
hash[:students].each {|person|
result[person[:name]] = person[:favorite_movie]
}
result
end
a = name_movies(wdi2)
binding.pry
#2. Write a function that returns a hash where the keys are the contestants
#and the value is their district name/specialty and their first specialty
typ_les = [
{:name => "c1", :district => {:name => "1", :specialty => "luxury goods", :population => "1,000,000"}, :rating => "11", :specialty => ["bow and arrow", "hand to hand combat", "sword throwing"]},
{:name => "c2", :district => {:name => "2", :specialty => "peacekeepers", :population => "1,687,098"}, :rating => "3", :specialty => ["sword throwing", "bow and arrow", "hand to hand combat"]},
{:name => "c3", :district => {:name => "3", :specialty => "electronics", :population => "239,908"}, :rating => "7", :specialty => ["hand to hand combat", "sword throwing", "bow and arrow"]},
{:name => "c4", :district => {:name => "4", :specialty => "fishing", :population => "987,123"}, :rating => "9", :specialty => ["trident throwing","hand to hand combat", "sword throwing", "bow and arrow"]},
{:name => "c5", :district => {:name => "5", :specialty => "power", :population => "123,987,324"}, :rating => "4", :specialty => ["hand to hand combat", "sword throwing", "bow and arrow"]},
{:name => "c6", :district => {:name => "6", :specialty => "transportation", :population => "724,345"}, :rating => "2", :specialty => ["knife throwing", "hand to hand combat", "sword throwing", "bow and arrow"]},
{:name => "c7", :district => {:name => "7", :specialty => "lumber", :population => "724,345"}, :rating => "8", :specialty => ["axe handling", "hand to hand combat", "sword throwing", "bow and arrow"]},
{:name => "c8", :district => {:name => "8", :specialty => "textiles", :population => "724,345"}, :rating => "9", :specialty => ["survival skills", "hand to hand combat", "sword throwing", "bow and arrow"]},
{:name => "c9", :district => {:name => "9", :specialty => "grain", :population => "2,309,832"}, :rating => "1", :specialty => ["survival skills", "bow and arrow"]},
{:name => "c10", :district => {:name => "10", :specialty => "livestock", :population => "2,309,832"}, :rating => "9", :specialty => ["knife throwing", "bow and arrow"]},
{:name => "c11", :district => {:name => "11", :specialty => "agriculture", :population => "2,309,832"}, :rating => "3", :specialty => ["survival skills", "bow and arrow"]},
{:name => "Katniss Everdeen", :district => {:name => "12", :specialty => "mining", :population => "324,789"}, :rating => "12", :specialty => ["bow and arrow"]}
]
def parseit(hash)
result = {}
hash.each { |x|
val = "District: #{x[:district][:name]} / #{x[:district][:specialty]}. Specialty: #{x[:specialty][0]}"
result[x[:name]] = val
}
result
end
b = parseit(typ_les)
binding.pry
#3.
##a. Go to http://developers.soundcloud.com/ and get an api key (if you don't
##already have one)
##b. Install the soundcloud gem
##c. Write a method that returns an array of hashes with just the title of the track, duration (bonus: convert it from milliseconds to a real time format), the artwork_url, and the permalink
client = Soundcloud.new(:client_id => '6ae916b071f482dfc78f26803c798803')
tracks = client.get('/tracks', :q => 'sea dramas', :licence => 'cc-by-sa')
def pars(response)
result = []
response.each {|x|
result << {:title => x[:title], :duration => x[:duration], :artwork_url => x[:artwork_url], :permalink => x[:permalink]}
}
result
end
c = pars(tracks)
binding.pry
#4(bonus). Write a method that Scrapes Hacker News (https://news.ycombinator.com/)
#and returns a hash of link titles paired with their actual url
#5(bonus). Alter the scraping method above to go to the next page of links and
#scrape that one and add those new links to the hash
@janester
Copy link

Why did you make all your values strings for the first two? Wouldn't arrays or hashes be better?

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