EXERCISE
Create a "shared" shopping list with a friend.
Create two data structures - one for your list of stuff, and one for your friend, e.g. you want milk, eggs, and bacon, and your friend wants beer, cookies, and apples.
Programmatically combine the two arrays into a single list, sort the result (alphabetically), and write it to the screen. If the two lists contain the same item, only show it once! Lastly, display each item in the list prepended with "buy ".
HINT
Learn to read the documentation!
https://ruby-doc.org/core-2.7.0/Array.html
First step is to construct 2 arrays with shopping list items for you and another person. We'll assign each to a descriptive variable.
ben_shopping_list = ["eggs", "oat milk", "beyond meat", "toilet paper"]
brian_shopping_list = ["beer", "milk", "toilet paper", "bacon"]
Next, we'll combine the arrays which we can easily do with +
:
combined_list = ben_shopping_list + brian_shopping_list
puts combined_list
At any point, to check our work and be sure there are no errors, we can display some output. For example, this seemed like a good time to add puts combined_list
to the code and run it to be sure it looks as expected and there are no errors. Afterwards, if it's not important to the overall code, we can delete or comment out that line of code.
Next, we want to order the combined list. We could look at the data and reorder it manually, but that would defeat the purpose - we want to do this in code. If we go to the Ruby Array documentation and scan the list of Array methods on the left, we'll find a method .sort
here, which, according to the documentation and example, appears to return an alphabetical version of the array data. So let's try that:
sorted_list = combined_list.sort
puts sorted_list
Next, we want to remove duplicates from the combined, ordered array - "toilet paper" appears in both lists, but it should only appear once in our combined list. Again, we don't want to do this manually, so we look at the documentation and find a method .uniq
here, which appears to return an array without any duplicates.
sorted_unique_list = sorted_list.uniq
puts sorted_unique_list
And the final step is to display a string that reads "buy ____" with each element from this combined, sorted, unique list:
puts "buy #{sorted_unique_list[0]}" # buy bacon
puts "buy #{sorted_unique_list[1]}" # buy beer
puts "buy #{sorted_unique_list[2]}" # buy beyond meat
puts "buy #{sorted_unique_list[3]}" # buy eggs
puts "buy #{sorted_unique_list[4]}" # buy milk
puts "buy #{sorted_unique_list[5]}" # buy oat milk
puts "buy #{sorted_unique_list[6]}" # buy toilet paper
And the final code is:
ben_shopping_list = ["eggs", "oat milk", "beyond meat", "toilet paper"]
brian_shopping_list = ["beer", "milk", "toilet paper", "bacon"]
combined_list = ben_shopping_list + brian_shopping_list
sorted_list = combined_list.sort
sorted_unique_list = sorted_list.uniq
puts "buy #{sorted_unique_list[0]}" # buy bacon
puts "buy #{sorted_unique_list[1]}" # buy beer
puts "buy #{sorted_unique_list[2]}" # buy beyond meat
puts "buy #{sorted_unique_list[3]}" # buy eggs
puts "buy #{sorted_unique_list[4]}" # buy milk
puts "buy #{sorted_unique_list[5]}" # buy oat milk
puts "buy #{sorted_unique_list[6]}" # buy toilet paper