Advanced Python Development Course
Chapter
>
Level
Advanced Lists
Creating a Set
Objective
Organize the condiments in the cabinet into proper categories and store them in the shelves using Sets.
There are several condiments that are stored in a cabinet, they are all ready for use but they are disorganized and not easily accessible. They need to be properly distributed on the empty shelves next to them which has already been depleted.
The condiments are spread across three (3) lists which can be reorganized by using Sets. Sets are lists that have no repeated items, they can also be sorted in various arrangements and compared to other Sets to find or highlight specific items.
There are three constant lists stored in the cabinet, these are called: condiments1 , condiments2 and condiments3. Walk to the light X mark to check the contents of the lists using the speak() function and collect all three (3) lists by using the collect() function, like this: player.speak(condiments1),player.collect(condiments1) .
Sets are created by writing set() with the list or list items as an argument, then assigned a name for the Set. Create three (3) Sets named: set1 , set2 and set3, assign them to their equivalent numbered list constants, for example: set1 = set(condiments1) .
Once collected, walk to the dark X mark above the green carpet, face the table and filter the lists through Sets. Use the speak() function with all three Sets you just created, for example: player.speak(set1) .
Follow this by walking to the dark X mark above the blue carpet and consolidate items by checking what condiments are present on all three (3) lists. Do this by using the intersection() method, this allows you to compare the Sets and find the items are present in all three. Name the resulting Set duplicates and consolidate, like this: duplicates = set1.intersection(set2,set3) , use the speak() function to verify the resulting Set.
Walk to the red carpet with the dark X mark and consolidate all three Sets into a single Set, this is done by using the union() method. It’s used in the same manner as the intersection() method but instead it takes all items on all three (3) Sets and places them in a single Set without any duplicates. Name the resulting Set all_items and consolidate, like this: all_items = set1.union(set2,set3) , use the speak() function to verify the resulting Set.
Once everything has been consolidated, walk to the gold X mark and face the shelves, use the place() function to place all the condiments into the empty shelf in order to complete the level, like this: player.place(all_items) .