Hone logo
Hone
Problems

Implementing Structural Pattern Matching in Python

Python 3.10 introduced structural pattern matching, a powerful feature that allows you to match against the structure of data. This challenge will test your understanding of how to use this feature to elegantly process and react to different data shapes. You'll build a function that categorizes and processes various input types based on their structure.

Problem Description

Your task is to implement a function process_data(data) that utilizes Python's structural pattern matching (match and case statements) to handle different types and structures of input data. The function should categorize the input and perform specific actions based on the matched pattern.

Key Requirements:

  • Handle basic types: Match against integers, strings, and booleans.
  • Handle collections: Match against lists and tuples, including checking their lengths and the types of their elements.
  • Handle dictionaries: Match against dictionaries, checking for specific keys and their values.
  • Handle nested structures: Match against combinations of the above, including nested lists, tuples, and dictionaries.
  • Wildcard matching: Use the wildcard (_) to ignore parts of the structure or to match anything.
  • Variable binding: Bind parts of the matched structure to variables for further use.

Expected Behavior:

The process_data function should return a descriptive string indicating what type of data was matched and potentially some extracted information. If no specific pattern matches, it should return a default "Unknown data structure" message.

Important Edge Cases:

  • Empty lists, tuples, and dictionaries.
  • Lists/tuples containing mixed data types.
  • Dictionaries with missing expected keys.

Examples

Example 1:

Input: 10
Output: "Matched an integer: 10"
Explanation: The input is a simple integer, and the function should identify and return a string indicating this.

Example 2:

Input: "hello"
Output: "Matched a string: hello"
Explanation: The input is a string, and the function should identify and return a string indicating this.

Example 3:

Input: [1, 2, 3]
Output: "Matched a list of 3 integers."
Explanation: The input is a list containing three integers. The pattern should capture the length and confirm the element type.

Example 4:

Input: ("apple", 5, True)
Output: "Matched a tuple with an element of type str, int, and bool."
Explanation: The input is a tuple with specific types at each position. The pattern should identify the types of each element.

Example 5:

Input: {"name": "Alice", "age": 30}
Output: "Matched a dictionary with name: Alice and age: 30."
Explanation: The input is a dictionary. The pattern should extract the values for "name" and "age".

Example 6:

Input: [1, "test", {"status": "active"}]
Output: "Matched a list containing an int, a str, and a dictionary."
Explanation: This demonstrates matching a list with mixed types, including a nested dictionary.

Example 7:

Input: []
Output: "Matched an empty list."
Explanation: Handles the edge case of an empty list.

Example 8:

Input: {"id": 123}
Output: "Matched a dictionary with id: 123."
Explanation: Handles a dictionary with only one known key.

Example 9:

Input: [1, 2, [3, 4]]
Output: "Matched a list containing a nested list."
Explanation: Demonstrates matching nested list structures.

Example 10:

Input: {"user": {"username": "Bob", "id": 456}}
Output: "Matched a nested dictionary."
Explanation: Demonstrates matching nested dictionaries.

Example 11:

Input: None
Output: "Unknown data structure"
Explanation: The input does not match any of the defined patterns.

Constraints

  • The input data can be any Python object.
  • The process_data function should return a string.
  • Focus on demonstrating the capabilities of structural pattern matching for common Python data structures.

Notes

  • You are encouraged to explore the different forms of pattern matching, including sequence patterns, mapping patterns, class patterns (though not strictly required for this problem), and the use of guards (if) within case statements if you want to add more complexity.
  • Think about how you can create distinct case statements to cover the various scenarios described.
  • Consider using the wildcard (_) effectively to simplify your patterns.
Loading editor...
python