From 093b18c0913300e1e174a6e88b8d7dd0a4279ac8 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 26 Feb 2025 12:31:03 +0200 Subject: [PATCH] Added task 3465 --- .../readme.md | 62 +++++++++++++ .../script.sql | 4 + .../MysqlTest.kt | 93 +++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 src/main/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/script.sql create mode 100644 src/test/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.kt diff --git a/src/main/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/readme.md b/src/main/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/readme.md new file mode 100644 index 000000000..82b87fc4b --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/script.sql b/src/main/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/script.sql new file mode 100644 index 000000000..ff81a8fdb --- /dev/null +++ b/src/main/kotlin/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_26_Time_292_ms_(90.91%)_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/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.kt b/src/test/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.kt new file mode 100644 index 000000000..4cd7d9939 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers/MysqlTest.kt @@ -0,0 +1,93 @@ +package g3401_3500.s3465_find_products_with_valid_serial_numbers + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +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 +import java.io.BufferedReader +import java.io.FileNotFoundException +import java.io.FileReader +import java.sql.SQLException +import java.util.stream.Collectors +import javax.sql.DataSource + +@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');" + ), + ], +) +internal class MysqlTest { + @Test + @Throws(SQLException::class, FileNotFoundException::class) + fun testScript(@EmbeddedDatabase dataSource: DataSource) { + dataSource.connection.use { connection -> + connection.createStatement().use { statement -> + statement.executeQuery( + BufferedReader( + FileReader( + ( + "src/main/kotlin/g3401_3500/" + + "s3465_find_products_with_valid_serial_numbers/" + + "script.sql" + ), + ), + ) + .lines() + .collect(Collectors.joining("\n")) + .replace("#.*?\\r?\\n".toRegex(), ""), + ).use { resultSet -> + 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)) + } + } + } + } +}