Validate Phone Numbers Practice Problem
This data science coding problem helps you practice Data Type Conversion & Validation, validate phone numbers, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Data Type Conversion & Validation.
- Problem ID: 43
- Problem key: 43-validate-phone-numbers
- URL: https://datacrack.app/solve/43-validate-phone-numbers
- Difficulty: medium
- Topic: Data Type Conversion & Validation
- Module: Data Cleaning
Problem Statement
# Validate Phone Numbers
### 🎯 Goal
Validate phone numbers by checking that they contain between 10 and 15 digits after removing common separators.
### 💻 Task
Implement `validate_phone_numbers(data, column)` that:
1. Converts the input dictionary to a DataFrame
2. For each phone number, strips separators (`-`, spaces, parentheses, `+`, `.`) and checks if the remaining characters are 10–15 digits
3. Adds a new boolean column `"{column}_valid"` indicating validity
4. Returns the resulting DataFrame
---
### 📥 Input
- `data`: A dictionary where keys are column names and values are lists of phone number strings
- `column`: The name of the column containing phone numbers
### 📤 Output
- A pandas DataFrame with the original column plus a new `"{column}_valid"` boolean column
---
### 🧩 Starter Code
```python
import pandas as pd
import numpy as np
import re
def validate_phone_numbers(data, column):
"""
Validate phone numbers by digit count after removing separators.
Args:
data (dict): Input data as dictionary with phone number strings
column (str): Name of the column containing phone numbers
Returns:
pd.DataFrame: DataFrame with original column and a new boolean validity column
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Define a function to validate a single phone number
# TODO: Remove separators (-, spaces, parens, +, .) and check digit count (10-15)
# TODO: Apply the validation function to create the validity column
# TODO: Return the resulting DataFrame
pass
```
---
### 💡 Examples
**Example 1:** Various formats
```python
data = {"phone": ["+1-555-123-4567", "(555) 123-4567", "5551234567", "123", "abc"]}
validate_phone_numbers(data, "phone")
```
```
phone phone_valid
0 +1-555-123-4567 True
1 (555) 123-4567 True
2 5551234567 True
3 123 False
4 abc False
```
**Example 2:** International numbers
```python
data = {"phone": ["+44 20 7946 0958", "12345", "+91-9876543210"]}
validate_phone_numbers(data, "phone")
```
```
phone phone_valid
0 +44 20 7946 0958 True
1 12345 False
2 +91-9876543210 True
```
**Example 3:** Dot-separated and empty
```python
data = {"phone": ["555.123.4567", "+1 (555) 123-4567", ""]}
validate_phone_numbers(data, "phone")
```
```
phone phone_valid
0 555.123.4567 True
1 +1 (555) 123-4567 True
2 False
```