Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
Created September 26, 2013 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanbrennan/6721636 to your computer and use it in GitHub Desktop.
Save ivanbrennan/6721636 to your computer and use it in GitHub Desktop.
The Second Program
# Given this List of Songs, Construct Arrays by Artist and Album
# Hint: Make use of the split() String Method
# http://www.ruby-doc.org/core-1.9.3/String.html#method-i-split
# Simple Example of Data Parsing
songs = [
"The Magnetic Fields - 69 Love Songs - Parades Go By",
"The Magnetic Fields - Get Lost - Smoke and Mirrors",
"Neutral Milk Hotel - In An Aeroplane Over the Sea - Holland 1945",
"The Magnetic Fields - Get Lost - You, Me, and the Moon",
"The Magnetic Fields - 69 Love Songs - The Book of Love",
"Neutral Milk Hotel - In An Aeroplane Over the Sea - The King of Carrot Flowers"
]
# Your goal is to get it to print this list:
# Neutral Milk Hotel - In An Aeroplane Over the Sea - Holland 1945
# Neutral Milk Hotel - In An Aeroplane Over the Sea - The King of Carrot Flowers
# The Magnetic Fields - 69 Love Songs - Parades Go By
# The Magnetic Fields - 69 Love Songs - The Book of Love
# The Magnetic Fields - Get Lost - Smoke and Mirrors
# The Magnetic Fields - Get Lost - You, Me, and the Moon
aeroplane = []
love_songs = []
get_lost = []
neutral_milk_hotel = [aeroplane]
the_magnetic_fields = [love_songs, get_lost]
songs.each do |song|
album = song.split(" - ")[1]
case album
when "In An Aeroplane Over the Sea"
aeroplane << song
when "69 Love Songs"
love_songs << song
when "Get Lost"
get_lost << song
end
end
neutral_milk_hotel.each do |album|
album.each {|song| puts song}
end
the_magnetic_fields.each do |album|
album.each {|song| puts song}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment