4.3 KiB
4.3 KiB
Hints
General
- Sets are mutable, unordered collections with no duplicate elements.
- Sets can contain any data type, as long as all elements are hashable.
- Sets are iterable.
- Sets are most often used to quickly dedupe other collections or for membership testing.
- Sets also support mathematical operations like
union,intersection,difference, andsymmetric difference
1. Clean up Dish Ingredients
- The
set()constructor can take any iterable as an argument. concept: lists are iterable. - Remember: concept: tuples can be formed using
(<element_1>, <element_2>)or via thetuple()constructor.
2. Cocktails and Mocktails
- A
setis disjoint from another set if the two sets share no elements. - The
set()constructor can take any iterable as an argument. concept: lists are iterable. - In Python, concept: strings can be concatenated with the
+sign.
3. Categorize Dishes
- Using concept: loops to iterate through the available meal categories might be useful here.
- If all the elements of
<set_1>are contained within<set_2>, then<set_1> <= <set_2>. - The method equivalent of
<=is<set>.issubset(<iterable>) - concept: tuples can contain any data type, including other tuples. Tuples can be formed using
(<element_1>, <element_2>)or via thetuple()constructor. - Elements within concept: tuples can be accessed from the left using a 0-based index number, or from the right using a -1-based index number.
- The
set()constructor can take any iterable as an argument. concept: lists are iterable. - concept: strings can be concatenated with the
+sign.
4. Label Allergens and Restricted Foods
- A set intersection are the elements shared between
<set_1>and<set_2>. - The set method equivalent of
&is<set>.intersection(<iterable>) - Elements within concept: tuples can be accessed from the left using a 0-based index number, or from the right using a -1-based index number.
- The
set()constructor can take any iterable as an argument. concept: lists are iterable. - concept: tuples can be formed using
(<element_1>, <element_2>)or via thetuple()constructor.
5. Compile a "Master List" of Ingredients
- A set union is where
<set_1> and<set_2>are combined into a singleset - The set method equivalent of
|is<set>.union(<iterable>) - Using concept: loops to iterate through the various dishes might be useful here.
6. Pull out Appetizers for Passing on Trays
- A set difference is where the elements of
<set_2>are removed from<set_1>, e.g.<set_1> - <set_2>. - The set method equivalent of
-is<set>.difference(<iterable>) - The
set()constructor can take any iterable as an argument. concept: lists are iterable. - The concept: list constructor can take any iterable as an argument. Sets are iterable.
7. Find Ingredients Used in Only One Recipe
- A set symmetric difference is where elements appear in
<set_1>or<set_2>, but not both sets. - A set symmetric difference is the same as subtracting the
setintersection from thesetunion, e.g.(<set_1> | <set_2>) - (<set_1> & <set_2>) - A symmetric difference of more than two
setswill include elements that are repeated more than two times across the inputsets. To remove these cross-set repeated elements, the intersections between set pairs needs to be subtracted from the symmetric difference. - Using concept: loops to iterate through the various dishes might be useful here.