Welcome to Subscribe On Youtube

3415. Find Products with Three Consecutive Digits 🔒

Description

Table: Products

+-++
\| product_id  \| int     \|
\| name        \| varchar \|
+-+-----+

Explanation:

  • Product 1: ABC123XYZ contains the digits 123.
  • Product 5: 789Product contains the digits 789.
  • Product 6: Item003Description contains 003, which is exactly three digits.

Note:

  • Results are ordered by product_id in ascending order.
  • Only products with exactly three consecutive digits in their names are included in the result.

</div>

Solutions

Solution 1: Regex Matching

We can use regular expressions to match product names that contain three consecutive digits.

  • import pandas as pd
    
    
    def find_products(products: pd.DataFrame) -> pd.DataFrame:
        filtered = products[
            products["name"].str.contains(r"(^|[^0-9])[0-9]{3}([^0-9]|$)", regex=True)
        ]
        return filtered.sort_values(by="product_id")
    
    
  • # Write your MySQL query statement below
    SELECT product_id, name
    FROM Products
    WHERE name REGEXP '(^|[^0-9])[0-9]{3}([^0-9]|$)'
    ORDER BY 1;
    
    

All Problems

All Solutions