Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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
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"
]
love_songs = []
get_lost = []
over_sea = []
songs.each do |album|
if album.split(" - ")[1] == "69 Love Songs" && album.split(" - ")[0] == "The Magnetic Fields"
love_songs << album
elsif album.split(" - ")[1] == "Get Lost" && album.split(" - ")[0] == "The Magnetic Fields"
get_lost << album
else album.split(" - ")[1] == "In An Aeroplane Over the Sea" && album.split(" - ")[0] == "Neutral Milk Hotel"
over_sea << album
end
end
sorted_list = [over_sea, love_songs, get_lost]
puts sorted_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.