Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

Advanced Lists
Maps, Filter, Reduce

Objective

Finish mixing and producing ice cream by using Maps, Filter and Reduce.

Some of the ice cream flavors are low or empty but we have enough materials to compensate and put together more ice cream. To do this you must use Map, a powerful tool that allows you to minimize the amount of code you need to write by condensing loops and branching code. The Filter and Reduce functions are complementary to the Map function and provide much needed versatility to your coding.

To start off you need to import the Reduce from the functools library in order to use the function by adding this code at the beginning of your program: from functools import reduce . The Map and Filter functions are built into Python and don’t need to be imported.

First you need to grab some ingredients from the walk in freezer, walk to the gold X mark, face the door and open it using the open() function. Grab all the jars of "cream" in the freezer then walk to the light X mark over the green carpet.

There are three (3) list constants in the field named: vanilla , strawberry and chocolate . Each constant has a combination of ingredients that constitutes the ice cream flavor. At the green X mark verify the contents of the strawberry list constant. First use the place() function to deposit the "cream" you previously grabbed and use the speak() function to verify the contents of the strawberry list constant.

Once verified it’s time to use the Map function with the strawberry list constant, the contents of the list are all decimals. Using the map() function, you can round all the elements in a list, replacing the need of iterating each element in a loop in order to round them one at a time. To do this, create a list named map1 and use the map() function in conjunction with the strawberry list and the round() function, like this: map1 = list(map(round, strawberry)) . Once done use the speak() function with map1 in order to verify the result.

Once that is done we move on to Filter, this works in the same vein as Map but instead it requires a function to be used in conjunction. At the top of the editor you will have a preset function named main_ingredients which is used to determines if a number is higher than the number 25 .

def main_ingredients(value): return value > 25

Instead of going through a loop you can use this function in conjunction with the filter() function in order to compose a new list with items that are over 25, those being the main ingredients. Create a new list named filter1 and apply the filter() function with main_ingredients and map1 in order to determine all main ingredients found in the Map list you created , like this: filter1 = list(filter(main_ingredients, value)) . Once done use the speak() function with filter1 in order to verify the result.

Now we move onto Reduce, this is used in the same fashion as Filter, but instead you can use it to collapse or reduce a list to a single unit. Much like Filter, it uses a custom function, this time named custom_sum which dynamically adds two number together.

def custom_sum(first, second): return first + second

Create a variable named reduce1 and use the reduce() function in conjunction with the custom_sum function and filter1 in order to consolidate all items into a single unit, like this: reduce1 = reduce(custom_sum, filter1) . Once done use the speak() function with reduce1 in order to verify the result.

Now that you’ve completed the Map, Filter, Reduce cycle, repeat the same cycle on the light X marks on red and blue carpets. On the red carpet, use the vanilla list constant, and create map2 , filter2 and reduce2 . On the blue carpet , use the chocolate list constant, and create map3 , filter3 and reduce3 .

Walk to the final light X mark which is over the purple carpet, add reduce1 , reduce2 and reduce3 putting together the main ingredients of vanilla, strawberry and chocolate. Create a variable named neapolitan and add all Reduce results, like this: neapolitan = reduce1+reduce2+reduce3 . Use the speak() function with the combination in order to verify it and use the place() function with it in order to create Neapolitan ice cream and complete the level.

Code book