From ec99937a8a171112f82626e2a6edd873784e5dfb Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 25 Feb 2025 14:34:43 +0200 Subject: [PATCH 1/2] Added task 3465 --- .../readme.md | 62 +++++++++++++++ .../script.sql | 4 + .../MysqlTest.java | 75 +++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/readme.md create mode 100644 src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/script.sql create mode 100644 src/test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.java diff --git a/src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/readme.md b/src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/readme.md new file mode 100644 index 000000000..82b87fc4b --- /dev/null +++ b/src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/readme.md @@ -0,0 +1,62 @@ +3465\. Find Products with Valid Serial Numbers + +Easy + +Table: `products` + + +--------------+------------+ + | Column Name | Type | + +--------------+------------+ + | product_id | int | + | product_name | varchar | + | description | varchar | + +--------------+------------+ + (product_id) is the unique key for this table. + Each row in the table represents a product with its unique ID, name, and description. + +Write a solution to find all products whose description **contains a valid serial number** pattern. A valid serial number follows these rules: + +* It starts with the letters **SN** (case-sensitive). +* Followed by exactly `4` digits. +* It must have a hyphen (-) **followed by exactly** `4` digits. +* The serial number must be within the description (it may not necessarily start at the beginning). + +Return _the result table ordered by_ `product_id` _in **ascending** order_. + +The result format is in the following example. + +**Example:** + +**Input:** + +products table: + + +------------+--------------+------------------------------------------------------+ + | product_id | product_name | description | + +------------+--------------+------------------------------------------------------+ + | 1 | Widget A | This is a sample product with SN1234-5678 | + | 2 | Widget B | A product with serial SN9876-1234 in the description | + | 3 | Widget C | Product SN1234-56789 is available now | + | 4 | Widget D | No serial number here | + | 5 | Widget E | Check out SN4321-8765 in this description | + +------------+--------------+------------------------------------------------------+ + +**Output:** + + +------------+--------------+------------------------------------------------------+ + | product_id | product_name | description | + +------------+--------------+------------------------------------------------------+ + | 1 | Widget A | This is a sample product with SN1234-5678 | + | 2 | Widget B | A product with serial SN9876-1234 in the description | + | 5 | Widget E | Check out SN4321-8765 in this description | + +------------+--------------+------------------------------------------------------+ + +**Explanation:** + +* **Product 1:** Valid serial number SN1234-5678 +* **Product 2:** Valid serial number SN9876-1234 +* **Product 3:** Invalid serial number SN1234-56789 (contains 5 digits after the hyphen) +* **Product 4:** No serial number in the description +* **Product 5:** Valid serial number SN4321-8765 + +The result table is ordered by product\_id in ascending order. \ No newline at end of file diff --git a/src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/script.sql b/src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/script.sql new file mode 100644 index 000000000..d27d8cf0c --- /dev/null +++ b/src/main/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/script.sql @@ -0,0 +1,4 @@ +# Write your MySQL query statement below +# #Easy #Database #2025_02_25_Time_716_ms_(100.00%)_Space_0.0_MB_(100.00%) +SELECT * FROM products WHERE description REGEXP 'SN[0-9]{4}-[0-9]{4}$' +OR description REGEXP 'SN[0-9]{4}-[0-9]{4}[^0-9]+' ORDER BY product_id diff --git a/src/test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.java b/src/test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.java new file mode 100644 index 000000000..ec3f1e9ba --- /dev/null +++ b/src/test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.java @@ -0,0 +1,75 @@ +package g3401_3500.s3465_find_products_with_valid_serial_numbers; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.stream.Collectors; +import javax.sql.DataSource; +import org.junit.jupiter.api.Test; +import org.zapodot.junit.db.annotations.EmbeddedDatabase; +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest; +import org.zapodot.junit.db.common.CompatibilityMode; + +@EmbeddedDatabaseTest( + compatibilityMode = CompatibilityMode.MySQL, + initialSqls = + " CREATE TABLE products (" + + " product_id INT," + + " product_name VARCHAR(50)," + + " description VARCHAR(100)" + + ");" + + "insert into products (product_id, product_name, description) values " + + "(1, 'Widget A', 'This is a sample product with SN1234-5678');" + + "insert into products (product_id, product_name, description) values " + + "(2, 'Widget B', 'A product with serial SN9876-1234 in the description');" + + "insert into products (product_id, product_name, description) values " + + "(3, 'Widget C', 'Product SN1234-56789 is available now');" + + "insert into products (product_id, product_name, description) values " + + "(4, 'Widget D', 'No serial number here');" + + "insert into products (product_id, product_name, description) values " + + "(5, 'Widget E', 'Check out SN4321-8765 in this description');") +class MysqlTest { + @Test + void testScript(@EmbeddedDatabase DataSource dataSource) + throws SQLException, FileNotFoundException { + try (final Connection connection = dataSource.getConnection()) { + try (final Statement statement = connection.createStatement(); + final ResultSet resultSet = + statement.executeQuery( + new BufferedReader( + new FileReader( + "src/main/java/g3401_3500/" + + "s3465_find_products_with_valid_serial_numbers/script.sql")) + .lines() + .collect(Collectors.joining("\n")) + .replaceAll("#.*?\\r?\\n", ""))) { + assertThat(resultSet.next(), equalTo(true)); + assertThat(resultSet.getNString(1), equalTo("1")); + assertThat(resultSet.getNString(2), equalTo("Widget A")); + assertThat( + resultSet.getNString(3), + equalTo("This is a sample product with SN1234-5678")); + assertThat(resultSet.next(), equalTo(true)); + assertThat(resultSet.getNString(1), equalTo("2")); + assertThat(resultSet.getNString(2), equalTo("Widget B")); + assertThat( + resultSet.getNString(3), + equalTo("A product with serial SN9876-1234 in the description")); + assertThat(resultSet.next(), equalTo(true)); + assertThat(resultSet.getNString(1), equalTo("5")); + assertThat(resultSet.getNString(2), equalTo("Widget E")); + assertThat( + resultSet.getNString(3), + equalTo("Check out SN4321-8765 in this description")); + assertThat(resultSet.next(), equalTo(false)); + } + } + } +} From d91783390969a11d4a84a4732fd4bbf2ac63d58a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 25 Feb 2025 14:41:43 +0200 Subject: [PATCH 2/2] Fixed format --- .../MysqlTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.java b/src/test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.java index ec3f1e9ba..4cdb8842b 100644 --- a/src/test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.java +++ b/src/test/java/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.java @@ -46,7 +46,8 @@ void testScript(@EmbeddedDatabase DataSource dataSource) new BufferedReader( new FileReader( "src/main/java/g3401_3500/" - + "s3465_find_products_with_valid_serial_numbers/script.sql")) + + "s3465_find_products_with_valid_serial_numbers/" + + "script.sql")) .lines() .collect(Collectors.joining("\n")) .replaceAll("#.*?\\r?\\n", ""))) {