Hone logo
Hone
Problems

Python Collection Navigator

This challenge will test your understanding of Python's built-in collection types: lists, tuples, sets, and dictionaries. You'll be tasked with creating a program that can store, organize, and retrieve different types of data using these collections, simulating a common scenario where you might need to manage various pieces of information efficiently. Mastering these collections is fundamental to writing effective and Pythonic code.

Problem Description

Your task is to build a system that manages a collection of items, each with a unique ID, a name, and a category. You need to store this information and then be able to perform specific operations on it.

What needs to be achieved:

  1. Data Storage: Store a list of items, where each item is represented by its ID, name, and category.
  2. Categorization: Group items by their category.
  3. Search/Retrieval: Retrieve items based on their ID or category.
  4. Uniqueness: Ensure that no two items have the same ID.

Key requirements:

  • Use a suitable collection to store the initial list of items.
  • Use a dictionary to group items by category. The keys of this dictionary will be the category names (strings), and the values will be collections of items belonging to that category.
  • Implement functions to perform the required operations.
  • Handle potential errors gracefully (e.g., trying to retrieve an item by a non-existent ID).

Expected behavior:

  • When items are added, they should be stored and categorized.
  • Searching by ID should return the specific item's details.
  • Searching by category should return all items belonging to that category.
  • Attempting to add an item with a duplicate ID should be prevented or handled appropriately.

Edge cases to consider:

  • Empty collections.
  • Searching for an ID or category that does not exist.
  • Items with identical names but different IDs/categories.

Examples

Example 1:

Input data (as a list of dictionaries):
[
    {"id": 101, "name": "Laptop", "category": "Electronics"},
    {"id": 102, "name": "Mouse", "category": "Electronics"},
    {"id": 201, "name": "Book", "category": "Books"},
    {"id": 103, "name": "Keyboard", "category": "Electronics"},
    {"id": 301, "name": "T-Shirt", "category": "Apparel"}
]

Operations:
- Add item: {"id": 202, "name": "Novel", "category": "Books"}
- Get item by ID: 102
- Get items by category: "Electronics"

Expected Output:
Categorized Data:
{
    "Electronics": [
        {"id": 101, "name": "Laptop", "category": "Electronics"},
        {"id": 102, "name": "Mouse", "category": "Electronics"},
        {"id": 103, "name": "Keyboard", "category": "Electronics"}
    ],
    "Books": [
        {"id": 201, "name": "Book", "category": "Books"}
    ],
    "Apparel": [
        {"id": 301, "name": "T-Shirt", "category": "Apparel"}
    ]
}

Item with ID 102:
{"id": 102, "name": "Mouse", "category": "Electronics"}

Items in category "Electronics":
[
    {"id": 101, "name": "Laptop", "category": "Electronics"},
    {"id": 102, "name": "Mouse", "category": "Electronics"},
    {"id": 103, "name": "Keyboard", "category": "Electronics"}
]

Explanation: The initial data is processed and stored. The new item is added. Then, the system retrieves and displays the item with ID 102 and all items belonging to the "Electronics" category. Notice that the Novel has been added to the "Books" category.

Example 2:

Input data: (empty list)

Operations:
- Add item: {"id": 501, "name": "Hat", "category": "Apparel"}
- Get item by ID: 501
- Get items by category: "Apparel"
- Attempt to add item with duplicate ID: {"id": 501, "name": "Cap", "category": "Apparel"}

Expected Output:
Categorized Data:
{
    "Apparel": [
        {"id": 501, "name": "Hat", "category": "Apparel"}
    ]
}

Item with ID 501:
{"id": 501, "name": "Hat", "category": "Apparel"}

Items in category "Apparel":
[
    {"id": 501, "name": "Hat", "category": "Apparel"}
]

Error: Item with ID 501 already exists.

Explanation: The system correctly handles an empty initial state. It adds the item, retrieves it, and then reports an error when a duplicate ID is attempted.

Example 3: (Edge Case - Non-existent ID/Category)

Input data:
[
    {"id": 101, "name": "Laptop", "category": "Electronics"}
]

Operations:
- Get item by ID: 999
- Get items by category: "Books"

Expected Output:
Item with ID 999:
Not found.

Items in category "Books":
[]

Explanation: The system correctly indicates when an ID or category is not found, returning appropriate representations (e.g., "Not found." or an empty list).

Constraints

  • Item IDs will be integers and unique within the collection.
  • Item names and categories will be strings.
  • The input will be a list of dictionaries, where each dictionary represents an item with "id", "name", and "category" keys.
  • Your solution should be able to handle at least 1000 items efficiently.
  • The solution should be implemented as a class for better organization.

Notes

  • Consider what Python collection types are best suited for:
    • Storing the initial list of items.
    • Efficiently looking up items by ID.
    • Grouping items by category.
    • Storing the items within each category.
  • Think about how to handle duplicate IDs when adding new items.
  • The set data structure could be useful for checking ID uniqueness quickly.
  • For retrieving items by ID, a dictionary where IDs are keys might be more efficient than iterating through a list.
  • When getting items by category, consider what you should return if the category doesn't exist.
Loading editor...
python