Royal Treasury: Currency Validator
๐ฆ Royal Treasury: Currency Validator
Difficulty: Beginner Tags: regex, validation, strings, parsing Series: CS 101: Fundamentals
Problem
The Royal Treasury needs to validate currency amounts in their ledgers. Determine if a string represents a valid currency amount.
Given a string, return True if it's a valid currency amount, False otherwise.
Valid format rules:
- No currency symbols (no $, โฌ, etc.)
- No spaces
- Commas for thousands separators (must be every 3 digits)
- Optional decimal point with up to 2 digits
- Can start with decimal point (.99)
- No leading zeros (except for 0.xx)
Real-World Application
Currency validation appears in: - E-commerce platforms - validating product prices, cart totals - Banking systems - transaction amount validation - Accounting software - financial data entry - Payment gateways - processing payment amounts - Invoice systems - validating invoice line items - Expense trackers - user input validation - POS systems - price entry validation - Tax calculators - amount validation
Input
data = str # String to validate as currency
Output
bool # True if valid currency format, False otherwise
Constraints
- String length โค 100
- Only digits, commas, and decimal points
- No currency symbols or spaces
Examples
Valid Examples
'1,234.56'- Standard format with commas and cents'12,345'- Thousands with comma, no decimal'0.10'- Ten cents'100'- Plain integer'.99'- Cents only (99 cents)'12,000'- Even thousands'1,000,000.00'- One million dollars'5'- Single digit
Invalid Examples
'1,234,'- Trailing comma'1,23.4'- Incorrect comma placement (not every 3 digits)'00.5'- Leading zero'1,234.'- Decimal point with no digits after'1,'- Comma with insufficient digits''- Empty string'$100'- Currency symbol'1 000'- Space instead of comma'1.234'- More than 2 decimal places'.- Just decimal point
What You'll Learn
- Regular expression patterns for validation
- Understanding grouping and alternation in regex
- Translating format specifications to regex
- Handling optional patterns
- Edge case validation
Why This Matters
Regex is a fundamental skill for: - Input validation in web forms - Data parsing and extraction - Text processing and transformation - Pattern matching at scale
Mastering regex patterns is essential for any software developer.
Starter Code
def challenge_function(data):
"""
Validate if string represents valid currency amount.
Args:
data: str to validate
Returns:
bool: True if valid currency format, False otherwise
"""
# Your implementation here
pass