Evaluate Ordered Action Rules
Learn this problemProblem statement
You are given ordered decision rules and one record of named attributes. Each rule has the form ACTION if CONDITION, where ACTION is ACCEPT or BLOCK.
An atomic condition has the form field operator literal. Supported operators are =, !=, >, >=, <, and <=. Literals and attribute values are either signed integers or lowercase boolean values. Equality operators support either type; ordering operators are used only with integers. A condition may contain atomic conditions joined entirely by AND or entirely by OR .
Each attribute string has the form field=value. Evaluate rules in insertion order and return the action of the first matching rule. Thus an earlier matching ACCEPT prevents any later BLOCK rule from running. Return REVIEW if no rule matches.
Function
evaluateOrderedRules(rules: String[], attributes: String[]) → StringExamples
Example 1
rules = ["BLOCK if fraud_flag=true","ACCEPT if trusted_partner=true","BLOCK if amount>10000"]attributes = ["fraud_flag=false","trusted_partner=true","amount=15000"]return = "ACCEPT"The first rule does not match. The trusted-partner rule matches and returns ACCEPT, so the later amount rule is not evaluated.
Example 2
rules = ["BLOCK if amount>100","ACCEPT if trusted_partner=true"]attributes = ["amount=200","trusted_partner=true"]return = "BLOCK"The first rule already matches, so insertion order makes its BLOCK result final.
Example 3
rules = ["ACCEPT if trusted_partner=true AND amount<=5000","BLOCK if fraud_flag=true OR amount>=20000"]attributes = ["trusted_partner=true","amount=8000","fraud_flag=false"]return = "REVIEW"The conjunction fails because the amount is too large, and both parts of the disjunction are false.
Constraints
0 <= rules.length <= 10000.- Every rule follows the stated grammar and contains at most
20atomic conditions. - Every referenced field appears exactly once in
attributes. - Every integer fits in a signed
64-bit value. - A compound condition uses only
ANDor onlyOR, not both.