← 返回 databricks 的题目列表IP Firewall Rule Matching
类型:online_judge
Implement an IP firewall that evaluates rules in priority order.
Implement the following class:
class IpFirewall:
def __init__(self, rules: list[list[str]]):
...
def allowAccess(self, ip: str) -> bool:
...
Details:
rules are provided from highest to lowest priority. Each rule is [action, cidr]:
action is either "ALLOW" or "DENY".
cidr is either a single IPv4 address such as "1.2.3.4", or a CIDR network such as "192.168.0.0/16".
allowAccess(ip) receives an IPv4 address string.
Check rules in order. When the IP matches the first rule, return:
True for "ALLOW";
False for "DENY".
Every queried IP is guaranteed to match at least one rule.
Example
rules = [
["DENY", "192.168.1.0/24"],
["ALLOW", "192.168.0.0/16"],
["DENY", "0.0.0.0/0"]
]
firewall = IpFirewall(rules)
firewall.allowAccess("192.168.1.42") # False
firewall.allowAccess("192.168.2.10") # True
firewall.allowAccess("8.8.8.8") # False
Constraints
1 <= len(rules) <= 10^5
An IPv4 address consists of four decimal integers in [0, 255].
CIDR prefix lengths are in [0, 32].
A rule without /prefix is equivalent to a /32 rule.
Example
Input
3
DENY 192.168.1.0/24
ALLOW 192.168.0.0/16
DENY 0.0.0.0/0
3
192.168.1.42
192.168.2.10
8.8.8.8
Output
DENY
ALLOW
DENY