Advanced Python Development Course
Chapter
>
Level
Advanced Lists
Sorting Sets
Objective
Organize and cook a meals by using Sets to sort and compose ingredients.
It would be a good idea to start preparing some food, there are a few ingredients in the fridge all rapped up as well as some herbs you could use to give the meal flavor. All materials are stored in lists so it would be a good idea to use Sets in order to consolidate items and get the most of your cooking.
There are three (3) list constants, two (2) are meats, named: pack1 and pack2, and one (1) is cooking materials, named ingredients. Walk to the dark X mark on the orange carpet and face the fridge, use the collect() function to grab pack1 and pack2 from the fridge, like this: player.collect(pack1).
Once the meat has been collected, walk to the light X mark and face the cutting board, here we will verify the contents of the packs by using the speak() function with both list constants you collected. From here we will consolidate the items by using Sets like in the previous level. Create two Sets named set1 and set2, and using the set() function assign pack1 and pack2 to them, for example: set1 = set(pack1).
Once the lists have been consolidated into Sets, it’s time to sort and compartmentalize the items into smaller Sets for convenience.
The first method to achieve this is called systemic_difference, this will allow us to separate the items that are not shared between each Set. Create a Set called unique and use the two Sets to compare and identify the unique items using systemic_difference, like this: unique = set1.symmetric_difference(set2). Use the speak() method with the Set you created to confirm the set’s contents.
Follow up with the sorting by creating a Set called shared and use the intersection method learned in the previous level to identify items found on both Sets. Use the two Sets to compare and identify the items, like this: shared = set1.intersection(set2). Use the speak() method with the Set you created to confirm the set’s contents.
Lastly use a method called difference, this will allow us to separate the items that are only found in one Set and not the other. Create two (2) Sets called meat1 and meat2, use the two original Sets to compare and identify thee items using difference , like this: meat1 = set1.difference(set2) and meat2 = set2.difference(set1). Use the speak() method with both meat Sets you created to confirm each set’s contents.
With this the meats have been sorted, walk to the dark X mark above the blue carpet, face the fridge and use the place() function to store the shared Set you created, like this: player.place(shared). With this you’ll put away the ingredients you aren’t cooking.
Walk to the gold X mark, face the cabinet and use the collect() function to acquire the last list constant named ingredients and use the speak() function to confirm it’s contents. Pass on the elements from ingredients to a new Set named set3. Now that you have all the ingredients create two Sets, named meal1 and meal2 using the union method combining all elements needed to cook.
For meal1, combine set3 and meat1 like this, meal1 = set3.union(meat1). Walk to the dark X mark over the purple carpet and with the meal1 Set use the speak() function to confirm the contents and the place() function to cook the meal in the pot.
For meal2 do the same, but instead use the union method with set3 and meat2. Walk to the dark X mark over the yellow carpet and face the pot, use the speak() and place() functions with meal2 in order to complete the level.