← 返回 rippling 的题目列表Object-Oriented Rule Engine
类型:online_judge
Problem: Object-Oriented Rule Engine
Implement an extensible rule engine that filters records according to rules. The implementation must use an object-oriented design and must not hard-code the examples.
A record is a key-value object, for example:
{"country":"US","age":25,"plan":"enterprise"}
A rule combines conditions with logical operators. Support at least:
Comparison operators: eq, ne, gt, ge, lt, le
Collection operator: in
Logical operators: and, or, not
Nested conditions
Implement evaluate(rule, record), which returns whether a record matches a rule.
Rules are represented by the following JSON DSL:
{
"and": [
{"field": "country", "op": "eq", "value": "US"},
{
"or": [
{"field": "age", "op": "ge", "value": 21},
{"field": "plan", "op": "in", "value": ["enterprise", "business"]}
]
}
]
}
Input
The first line is a JSON rule DSL object. The second line is a JSON array of records.
Output
Print every matching record in input order, one JSON object per line.
Constraints
Number of records: N <= 100,000
Number of condition nodes in one rule: M <= 1,000
Field values may be strings, numbers, booleans, or arrays.
A missing field is non-matching, except for ne, for which a missing field is considered different from the target value.
The design should make adding operators and condition types straightforward.
Example
Input
{"and":[{"field":"country","op":"eq","value":"US"},{"field":"age","op":"ge","value":21}]}
[{"name":"Alice","country":"US","age":25},{"name":"Bob","country":"CA","age":30},{"name":"Carol","country":"US","age":18}]
Output
{"name":"Alice","country":"US","age":25}