Teacher's Guide

Chapter 3 / Level 4: Creating a Set

Teaching Objective:

  • Students understand what a Set is and it’s uses
  • Understands how to use set based functions to make the best use out of sets
  • Understands how sets can optimize code and remove unnecessary items in lists

Guide

In this exercise we’ll be working with Sets, these are lists that have no repeating elements present. Sets also have a series of methods that allow you to filter content from these lists and consolidate or combine items.

The objective of the level is to retrieve condiments from the cabinet, organize them using Sets, and store them in shelves. There are three (3) list constants named: condiments1 , condiments2 and condiments3 start off by walking to the light X mark and verifying the contents of the lists as well as collecting them using the speak() and collect() functions.

player.speak(condiments1)
player.collect(condiments1)

Do this for all three list constants wile facing the cabinet. Follow this up by placing the lists in Sets in order to remove any duplicate entries from each list, using the set() method. Walk to the dark X mark above the green carpet and use the speak() function wit the new Sets to verify them.

set1 = set(condiments1)

set2 = set(condiments2)

set3 = set(condiments3)

.............

player.speak(set1)
player.speak(set2)
player.speak(set3)

Walk to the dark X mark above the blue carpet and use the intersection() method comparing all three (3) Sets. This allows you to find items that are present on all three Sets. Assign the Set to the name duplicates, once done use the speak() function to verify the contents.

duplicates = set1.intersection(set2,set3)

player.speak(duplicates)

Walk to the dark X mark above the red carpet and use the union() method comparing all three (3) Sets. This allows you to combine all three (3) Sets into a single set, removing any duplicates. Assign the Set to the name all_items , once done use the speak() function to verify the contents.

all_items = set1.union(set2,set3)
player.speak(all_items)

Once the Sets have been consolidated, walk to the gold X mark and place() the all_items Set, this will allow you to populate the shelves and complete the level.

player.place(all_items)

Map overview and path

Map overview and path

Code Solution

import player

player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward(4)

player.speak(condiments1)
player.collect(condiments1)
player.speak(condiments2)
player.collect(condiments2)
player.speak(condiments3)
player.collect(condiments3)

set1 = set(condiments1)

set2 = set(condiments2)

set3 = set(condiments3)

player.turn_left()
player.move_forward(3)
player.turn_left()
player.move_forward()

player.speak(set1)
player.speak(set2)
player.speak(set3)

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

duplicates = set1.intersection(set2,set3)

player.speak(duplicates)

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

all_items = set1.union(set2,set3)
player.speak(all_items)

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

player.place(all_items)

v

Code Solution

import player

player.turn_left()
player.move_forward(4)
player.turn_right()
player.move_forward()
player.speak(left_sack)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.speak(middle_sack)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.speak(right_sack)
potatoes = [x for x in left_sack if not "Spoiled" in x]
onions = [x for x in middle_sack if not "Bad" in x]
garlic = [x for x in right_sack if not "Rotten" in x]
player.turn_left()
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward()
player.turn_right()
player.place(garlic)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.place(potatoes)
player.turn_left()
player.move_forward(2)
player.turn_right()
player.place(onions)
Chapter 3 / Level 4: Creating a Set | Teacher's Guide