Business Rule Validation Practice Problem
This data science coding problem helps you practice Data Consistency & Validation, business rule validation, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Data Consistency & Validation.
- Problem ID: 175
- Problem key: 175-business-rule-validation
- URL: https://datacrack.app/solve/175-business-rule-validation
- Difficulty: medium
- Topic: Data Consistency & Validation
- Module: Data Cleaning
Problem Statement
# Business Rule Validation
### 🎯 Goal
Beyond generic range checks, every domain has its own **business rules**: an order quantity must be positive, a customer must be at least 18, a discount cannot exceed 100%. These constraints differ per dataset, so we need a flexible, data-driven validator that accepts a *list of rules* and checks them all.
This function takes a list of constraint specifications and flags each row as valid only if it satisfies **every** rule.
### 💻 Task
Implement `validate_business_rules(data, rules)` that:
1. Converts the input dictionary to a DataFrame
2. Applies each rule in `rules`, where a rule is a dict with keys `"column"`, `"operator"`, and `"value"`
3. Supports the operators `">"`, `">="`, `"<"`, `"<="`, `"=="`, `"!="`
4. Marks a row **valid** only when it passes **all** rules (logical AND)
5. Adds a new boolean column named `"valid"` and returns the DataFrame as a dictionary
**Important:** A row must satisfy *every* rule to be valid. With an empty rule list, all rows are valid.
---
### 📥 Input
- `data`: A dictionary where keys are column names and values are lists
- `rules`: A list of dicts, each shaped like `{"column": str, "operator": str, "value": number}`
### 📤 Output
- A dictionary representing the DataFrame with an added `"valid"` boolean column
---
### 🧩 Starter Code
```python
import operator
import pandas as pd
def validate_business_rules(data, rules):
"""
Validate each row against a list of custom constraints. A row is valid only if
EVERY rule passes.
Args:
data (dict): Input data as dictionary
rules (list): List of dicts with keys "column", "operator", "value"
Returns:
dict: DataFrame as dictionary with an added "valid" boolean column
"""
# TODO: Convert input dictionary to DataFrame
# TODO: Map operator strings to functions (operator.gt, operator.ge, ...)
# TODO: Start with all rows valid, then AND-in each rule's result
# TODO: Store the result in a "valid" column
# TODO: Return DataFrame as dictionary
pass
```
---
### 💡 Examples
**Example 1:** An order needs positive quantity AND non-negative price
```python
data = {"quantity": [5, -1, 0, 10], "price": [100, 50, 30, -5]}
rules = [{"column": "quantity", "operator": ">", "value": 0},
{"column": "price", "operator": ">=", "value": 0}]
validate_business_rules(data, rules)
```
```
{"quantity": [5, -1, 0, 10], "price": [100, 50, 30, -5],
"valid": [True, False, False, False]}
```
**Example 2:** Adults only, score within bounds
```python
data = {"age": [25, 17, 40], "score": [80, 90, 50]}
rules = [{"column": "age", "operator": ">=", "value": 18},
{"column": "score", "operator": "<=", "value": 100}]
validate_business_rules(data, rules)
```
```
{"age": [25, 17, 40], "score": [80, 90, 50],
"valid": [True, False, True]}
```
**Example 3:** A single in-stock rule
```python
data = {"stock": [0, 5, 12, 3]}
rules = [{"column": "stock", "operator": ">", "value": 0}]
validate_business_rules(data, rules)
```
```
{"stock": [0, 5, 12, 3],
"valid": [False, True, True, True]}
```