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:
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
.
This program will take 4 total inputs from the user.
sets
or pieces
for this input.sets
previously) or piece IDs (if the user selected pieces
previously).subset
, superset
, or none
(nothing in common).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.
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.
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.
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'}
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).
subset
search searches for all LEGO sets from the data file whose pieces are a subset of the pieces specified by the user to search for.
For instance, if the user specified pieces IDs
95 77 10 15 78 30 75 55
as their search, the search results should have two sets City Car
and Pirate Boat
.
Essentially, this type of search answers the question “What LEGO sets can one build with these pieces?”
superset
(superset) search searches for all LEGO sets that contain all of the pieces specified by the user to search for.
For instance, if the user specified pieces IDs
95 77 75 55
as their search, the search results should have only one set Pirate Boat
.
Essentially, this type of search answers the question “What LEGO sets have these pieces in them?”
none
search searches for all LEGO sets that contain none of the pieces specified by the user to search for.
For instance, if the user specified pieces IDs
95 77 75
as their search, the search results should have only one set Star Wars Battle Set
.
Essentially, this type of search answers the question “What LEGO sets can one build that don’t require these pieces?”
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.
sets.txt | subset search |
|
|
superset search | none search |
|
|
sets.txt | subset search |
|
|
superset search | none search |
|
|
Use the starter code linked below. It includes a function for printing out the search results in order.
Here’s a data file that has actual lego set data, rather than made-up data:
Submit to Gradescope by April 23th by 7pm. Try to get as many gradescope test cases passing as you can.