Skip to content

Instantly share code, notes, and snippets.

@gregeng
Created September 27, 2013 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gregeng/6723133 to your computer and use it in GitHub Desktop.
Save gregeng/6723133 to your computer and use it in GitHub Desktop.
Day 4: 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
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