Teacher's Guide

Chapter 3 / Level 6: Maps, Filter, Reduce

Teaching Objective:

  • Student learns how to condense loops and iterating lists using Map, Filter, Reduce
  • Leans how to optimize code and various use cases for Map, Filter, Reduce functions
  • Understands how to use the custom functions to add flexibility to the Filter and Reduce functions

Guide

In this exercise we will be working with Map, Filter and Reduce, these are functions that can help optimizing your code by simplifying irritative procedures without using loops. These functions are a powerful tool for managing the output on lists and in this level we’ll use them in order to harness ice cream ingredients to create a new flavor.

First we need to ensure reduce() is imported from functools as unlike map() and filter() , it’s not built into python. We must also set up the main_ingredients() function for use with filter(), this function checks if a number is over 25. The custom_sum() custom function is also added for use with reduce() , this function adds two numbers together in a dynamic fashion.

# Import Reduce function
from functools import reduce

# Function checks if a value is larger than 25
def main_ingredients(value):
    return value > 25

# Function does a custom sum between 2 numbers
def custom_sum(first, second):
    return first + second

Start off by walking to the gold X mark and facing the door the using the open() function to get inside the freezer and collect three (3) jars of cream. Once done walk to the light X marks in order to consolidate ingredients.

There are three (3) list constants in this level named strawberry , vanilla and chocolate , each have a different combination of ingredients represented by decimal numbers. On each X mark you need to place() the “cream” you collected in the freezer and from there use the speak() function to verify the ingredients of the corresponding ice cream via the list constant.

On each X mark you set the Map where you round out each element of the list using map() with round and the corresponding list constant for the X mark. Set the Filter in the same way using filter() the main_ingredients() function and the name of the Map you created. And lastly set Reduce using the custom_sum() function and the Filter you created with the reduce() function.

player.place("cream")
player.speak(strawberry)

map1 = list(map(round, strawberry))
player.speak(map1)

filter1 = list(filter(main_ingredients, map1))
player.speak(filter1)

reduce1 = reduce(custom_sum, filter1)
player.speak(reduce1)

The three (3) light X marks on the left with colored carpets correspond to each list constant, with green carpet corresponding to strawberry and the names for the stored values being map1 , filter1 and reduce1. Then the red carpet corresponding to vanilla and the names for the stored values being map2 , filter2 and reduce2. Finally the blue carpet corresponding to chocolate and the names for the stored values being map3 , filter3 and reduce3.

Once all elements have been consolidated, walk to the light X mark on the purple carpet and add all the reduce values in a variable named neapolitan. Use the speak() function to confirm it’s value and use place() in order to set it in it’s container.

neapolitan = reduce1+reduce2+reduce3

player.speak(neapolitan)

player.place(neapolitan)

Map overview and path

Map overview and path

Code Solution

import player

# Import Reduce function
from functools import reduce

# Function checks if a value is larger than 25
def main_ingredients(value):
    return value > 25

# Function does a custom sum between 2 numbers
def custom_sum(first, second):
    return first + second
    
player.turn_left()
player.move_forward(3)
player.turn_left()
player.open()
player.move_forward(2)
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward()
player.turn_right()
player.move_forward(3)
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward()
player.place("cream")
player.speak(strawberry)

map1 = list(map(round, strawberry))
player.speak(map1)

filter1 = list(filter(main_ingredients, map1))
player.speak(filter1)

reduce1 = reduce(custom_sum, filter1)
player.speak(reduce1)

player.turn_right()
player.move_forward()
player.turn_left()

player.place("cream")
player.speak(vanilla)

map2 = list(map(round, vanilla))
player.speak(map2)

filter2 = list(filter(main_ingredients, map2))
player.speak(filter2)

reduce2 = reduce(custom_sum, filter2)
player.speak(reduce2)

player.turn_right()
player.move_forward()
player.turn_left()

player.place("cream")
player.speak(chocolate)

map3 = list(map(round, chocolate))
player.speak(map3)

filter3 = list(filter(main_ingredients, map3))
player.speak(filter3)

reduce3 = reduce(custom_sum, filter3)
player.speak(reduce3)

player.turn_right()
player.move_forward(2)
player.turn_left()

neapolitan = reduce1+reduce2+reduce3

player.speak(neapolitan)
player.place(neapolitan)
Chapter 3 / Level 6: Maps, Filter, Reduce | Teacher's Guide