LEGO Set Processing

LEGO 1 LEGO 2 LEGO 3

LEGO is arguably one of the most popular children’s (and “grown-up” ?) toys in existence. Most are probably familiar with LEGO, but for those that are not: LEGO sets are sets of interlocking bricks and pieces that can be assembled to create various structures. The pieces that compose a LEGO set range from very basic pieces, such as 2x2 and 2x4 bricks, to more intricate ones. A sampling of a few LEGO pieces (including both a 2x2 and a 2x4 brick) are shown below:

Piece 1 Piece 2 Piece 3 Piece 4 Piece 5

Every LEGO piece and every LEGO set has an ID. Over the years, the LEGO company has created and sold thousands of LEGO sets of various sizes, and themes. There is also a LEGO enthusiast community, out of which come many custom LEGO creations. There are also numerous online communities for LEGO creators and enthusiasts.

One such website the LEGO community may use is rebrickable.com. This website hosts a number of LEGO resources, including LEGO creation images, set lists, and instructions. They even have a location on their website where you can download several data files which represent the sets and pieces for thousands of LEGO sets! (For those interested, go here to visit that portion of the website).

In this programming assignment, your program will read in LEGO-set data files and process various searches on the LEGO set data. Using the program, users will be able to search through various LEGO sets, and figure out which sets do and do not have compatible pieces Name the file lego.py.

Program Behavior

This program will take 4 total inputs from the user.

I recommend you start by implementing your main() function. In this function, ask the user for these four inputs. After you have implemented this, the next step is to get the LEGO set data loaded into a data structure.

Loading the input data (input 1)

The program will have to load LEGO set information from a data file. Each line of the input file will represent a single LEGO set, and will be formatted like so:

LEGO_set_name,LEGO_set_id ||| piece_id_1,piece_id_2,piece_id_3,...,piece_id_N

The first two parts of the line are the LEGO set name and ID, separated by a comma. The string ` ||| ` separates the name and ID from a comma-separated set of piece IDs.

The input file will be composed of a number of lines like this. Your program should read in the contents of the file, split up the lines appropriately, and store the information in a dictionary. Here’s an example with three small, made-up LEGO sets:

Star Wars Battle Set,1050 ||| 30,55,72,103,45,21,17
City Car,2401 ||| 10,15,78,30,75,55
Pirate Boat,955 ||| 95,77,10,15,78,30,75,55

The keys of the dictionary should be tuples, containing the name and id. The key should map to a set, containing the piece IDs for that set. Below is what the dictionary should look like, given the very simple example input file contents shown above.

lego_sets = { ('Star Wars Battle Set', '1050') : {'30', '55', '72', '103', '45', '21', '17'},
              ('City Car', '2401') : {'10', '15', '78', '30', '75', '55'},
              ('Pirate Boat', '955') : {'95', '77', '10', '15', '78', '30', '75', '55'} }

I recommend that you create a function that has the job of opening up and reading in this data. Call this function from main, and make sure that this function returns the dictionary to main.

Search type (input 2)

There are two search types that the user can choose, sets or pieces. If pieces is selected, the user should enter a space-separated sequence of piece ID numbers as the fourth input. These piece numbers should be stored into a set, and the search can be done using these pieces.

If sets is selected, the user should enter a space-separated sequence of set ID numbers as the fourth input. All of the pieces from all of the listed sets should be put into a set, and then used for the search.

IDs (input 3)

The third input is a space-separated of set ids (if the user selected sets previously) or piece ids (if the user selected pieces previously). Regardless of whether they enter set IDs or piece IDs, the desired result from this input is a set of piece IDs to search for. If the user enters set IDs, say:

Enter search: 95 55 30 78

Then you should end up with the set: search_ids = {'95', '55', '30', '78'}

If the user enters set IDs instead, you should add all of the pieces from each of the sets into a single set. For example, if the user enters:

Enter search: 1050 2401

Then all of the pieces from the sets with these two IDs should be combined into one set, producing: search_ids = {'30', '55', '72', '103', '45', '21', '17', '10', '15', '78', '75'}

Search type (input 4)

The last input determines the type of search to do. In other words, this determined how to use the search_ids from the last step for searching through the sets. The user can select to to a subset, superset or none (nothing in common).

Test Cases

Similar to the spellcheck PA, there will be a number of test cases to test your program in various chunks. There will be one or more tests for each of the below scenarios:

Thus, don’t feel like you need to get everything working on the first try. You can implement piece ID search before set ID search, and you can work on subset, superset, and none searches one-at-a-time.

Complete Examples

3 set data file, search by pieces

sets.txt subset search
Star Wars Battle Set,1050 ||| 30,55,72,103,45,21,17
City Car,2401 ||| 10,15,78,30,75,55
Pirate Boat,955 ||| 95,77,10,15,78,30,75,55
LEGO set file name:
sets.txt
Search by sets or pieces?:
pieces
Search type (subset or superset or none):
subset
Search IDs:
95 77 10 15 78 30 75 55

2401 : City Car
955 : Pirate Boat
superset search none search
LEGO set file name:
sets.txt
Search by sets or pieces?:
pieces
Search type (subset or superset or none):
superset
Search IDs:
95 77 75 55

955 : Pirate Boat
LEGO set file name:
sets.txt
Search by sets or pieces?:
pieces
Search type (subset or superset or none):
none
Search IDs:
95 77 75

1050 : Star Wars Battle Set

10 set data file, search by sets

sets.txt subset search
Star Wars Battle Set,1050 ||| 30,55,72,103,45,21,17
Star Wars A-Wing,1451 ||| 105,135,14,7,54,57,10,19,37
City Car,2300 ||| 10,15,78,30,75,55
City Motorcycle,472 ||| 5,8,23,78,104,32,74
City Police Station,2401 ||| 89,40,35,23,94,64,107,117,116,20,5
Pirate Boat,955 ||| 95,77,10,15,78,30,75,55
Pirate Ship,2701 ||| 95,104,77,107,10,58,15,25,78,48,30,86,75,55
Small Set,100 ||| 10,14,15
Small Set,200 ||| 58,78,86
Small Set,300 ||| 200,201,205
LEGO set file name:
sets_2.txt
Search by sets or pieces?:
sets
Search type (subset or superset or none):
subset
Search IDs:
2701

2300 : City Car
955 : Pirate Boat
2701 : Pirate Ship
200 : Small Set
superset search none search
LEGO set file name:
sets_2.txt
Search by sets or pieces?:
sets
Search type (subset or superset or none):
superset
Search IDs:
200

2701 : Pirate Ship
200 : Small Set
LEGO set file name:
sets_2.txt
Search by sets or pieces?:
sets
Search type (subset or superset or none):
none
Search IDs:
300 200 100

2401 : City Police Station
1050 : Star Wars Battle Set

Starter Code

Use the starter code linked below. It includes a function for printing out the search results in order.

lego.py

Data file

Here’s a data file that has actual lego set data, rather than made-up data:

sw_sets.txt

Submission

Submit to Gradescope by April 23th by 7pm. Try to get as many gradescope test cases passing as you can.