← 返回 rippling 的题目列表Implement an Expense Rule Engine
类型:online_judge
Implement an Expense Rule Engine
Implement a simplified expense rule engine that evaluates an expense against a list of rules and returns which rules are triggered.
Rules
Each rule consists of:
a condition evaluated on an expense field (e.g., numeric comparisons, string equality/contains), and
an action/result returned when the condition matches (e.g., "REJECT", "APPROVE", "FLAG", or returning the rule id).
Support at least these comparison operators: ==, !=, >, >=, <, <=.
Input
Read from stdin two parts:
One expense (a JSON object)
A list of rules (a JSON array). Each rule includes id, field, op, value, result.
Parse the input and evaluate the rules.
Output
Print a JSON array of triggered rules (in the original rule order). The array elements can be either the rule id or the result, but must be consistent.
Constraints
Number of rules N: 1 to 10^4.
Number of expense fields: up to 100.
Field value types: int/float/string/bool.
Example
Input
{"amount":120.5,"currency":"USD","merchant":"Uber","category":"Travel"}
[
{"id":"r1","field":"amount","op":">","value":100,"result":"FLAG"},
{"id":"r2","field":"currency","op":"==","value":"USD","result":"OK"},
{"id":"r3","field":"category","op":"==","value":"Meals","result":"REJECT"}
]
Output
["r1","r2"]
Example
Input
{"amount":120.5,"currency":"USD","merchant":"Uber","category":"Travel"}
[
{"id":"r1","field":"amount","op":">","value":100,"result":"FLAG"},
{"id":"r2","field":"currency","op":"==","value":"USD","result":"OK"},
{"id":"r3","field":"category","op":"==","value":"Meals","result":"REJECT"}
]
Output
["r1", "r2"]