#C14232. Filtering Dictionaries Based on Criteria

    ID: 43859 Type: Default 1000ms 256MiB

Filtering Dictionaries Based on Criteria

Filtering Dictionaries Based on Criteria

You are given a list of dictionaries in JSON format and a filtering dictionary (also in JSON format). Each dictionary contains key‐value pairs, and the filtering dictionary specifies a set of key‐value pairs.

A dictionary d matches the filtering criteria f if for every key-value pair \( (k, v) \) in \( f \), the dictionary d contains the key \( k \) and its corresponding value equals \( v \). In other words, \( d \) is a match if \( \forall (k,v) \in f,\ d[k] = v \).

Your task is to output a JSON array of all dictionaries that match the filtering criteria, preserving their original order. Note that if the filtering dictionary is empty, then every dictionary is considered a match.

inputFormat

The input is provided in two lines via standard input.

  • The first line contains a JSON array of dictionaries.
  • The second line contains a JSON object representing the filtering criteria.

You may assume that the input is valid JSON.

outputFormat

Output a JSON array (to standard output) containing those dictionaries from the input array that satisfy all the filtering criteria. The output format must be valid JSON.

## sample
[{"name": "Alice", "age": 30, "city": "New York"}, {"name": "Bob", "age": 25, "city": "Los Angeles"}, {"name": "Charlie", "age": 30, "city": "New York"}]
{}
[{"name": "Alice", "age": 30, "city": "New York"}, {"name": "Bob", "age": 25, "city": "Los Angeles"}, {"name": "Charlie", "age": 30, "city": "New York"}]