Skip to content

Commit e2735c7

Browse files
authored
Added task 3465
1 parent f99d0cd commit e2735c7

File tree

3 files changed

+159
-0
lines changed
  • src
    • main/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers
    • test/kotlin/g3401_3500/s3465_find_products_with_valid_serial_numbers

3 files changed

+159
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3465\. Find Products with Valid Serial Numbers
2+
3+
Easy
4+
5+
Table: `products`
6+
7+
+--------------+------------+
8+
| Column Name | Type |
9+
+--------------+------------+
10+
| product_id | int |
11+
| product_name | varchar |
12+
| description | varchar |
13+
+--------------+------------+
14+
(product_id) is the unique key for this table.
15+
Each row in the table represents a product with its unique ID, name, and description.
16+
17+
Write a solution to find all products whose description **contains a valid serial number** pattern. A valid serial number follows these rules:
18+
19+
* It starts with the letters **SN** (case-sensitive).
20+
* Followed by exactly `4` digits.
21+
* It must have a hyphen (-) **followed by exactly** `4` digits.
22+
* The serial number must be within the description (it may not necessarily start at the beginning).
23+
24+
Return _the result table ordered by_ `product_id` _in **ascending** order_.
25+
26+
The result format is in the following example.
27+
28+
**Example:**
29+
30+
**Input:**
31+
32+
products table:
33+
34+
+------------+--------------+------------------------------------------------------+
35+
| product_id | product_name | description |
36+
+------------+--------------+------------------------------------------------------+
37+
| 1 | Widget A | This is a sample product with SN1234-5678 |
38+
| 2 | Widget B | A product with serial SN9876-1234 in the description |
39+
| 3 | Widget C | Product SN1234-56789 is available now |
40+
| 4 | Widget D | No serial number here |
41+
| 5 | Widget E | Check out SN4321-8765 in this description |
42+
+------------+--------------+------------------------------------------------------+
43+
44+
**Output:**
45+
46+
+------------+--------------+------------------------------------------------------+
47+
| product_id | product_name | description |
48+
+------------+--------------+------------------------------------------------------+
49+
| 1 | Widget A | This is a sample product with SN1234-5678 |
50+
| 2 | Widget B | A product with serial SN9876-1234 in the description |
51+
| 5 | Widget E | Check out SN4321-8765 in this description |
52+
+------------+--------------+------------------------------------------------------+
53+
54+
**Explanation:**
55+
56+
* **Product 1:** Valid serial number SN1234-5678
57+
* **Product 2:** Valid serial number SN9876-1234
58+
* **Product 3:** Invalid serial number SN1234-56789 (contains 5 digits after the hyphen)
59+
* **Product 4:** No serial number in the description
60+
* **Product 5:** Valid serial number SN4321-8765
61+
62+
The result table is ordered by product\_id in ascending order.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2025_02_26_Time_292_ms_(90.91%)_Space_0.0_MB_(100.00%)
3+
SELECT * FROM products WHERE description REGEXP 'SN[0-9]{4}-[0-9]{4}$'
4+
OR description REGEXP 'SN[0-9]{4}-[0-9]{4}[^0-9]+' ORDER BY product_id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package g3401_3500.s3465_find_products_with_valid_serial_numbers
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import org.zapodot.junit.db.annotations.EmbeddedDatabase
7+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest
8+
import org.zapodot.junit.db.common.CompatibilityMode
9+
import java.io.BufferedReader
10+
import java.io.FileNotFoundException
11+
import java.io.FileReader
12+
import java.sql.SQLException
13+
import java.util.stream.Collectors
14+
import javax.sql.DataSource
15+
16+
@EmbeddedDatabaseTest(
17+
compatibilityMode = CompatibilityMode.MySQL,
18+
initialSqls = [
19+
(
20+
" CREATE TABLE products (" +
21+
" product_id INT," +
22+
" product_name VARCHAR(50)," +
23+
" description VARCHAR(100)" +
24+
");" +
25+
"insert into products (product_id, product_name, description) values " +
26+
"(1, 'Widget A', 'This is a sample product with SN1234-5678');" +
27+
"insert into products (product_id, product_name, description) values " +
28+
"(2, 'Widget B', 'A product with serial SN9876-1234 in the description');" +
29+
"insert into products (product_id, product_name, description) values " +
30+
"(3, 'Widget C', 'Product SN1234-56789 is available now');" +
31+
"insert into products (product_id, product_name, description) values " +
32+
"(4, 'Widget D', 'No serial number here');" +
33+
"insert into products (product_id, product_name, description) values " +
34+
"(5, 'Widget E', 'Check out SN4321-8765 in this description');"
35+
),
36+
],
37+
)
38+
internal class MysqlTest {
39+
@Test
40+
@Throws(SQLException::class, FileNotFoundException::class)
41+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
42+
dataSource.connection.use { connection ->
43+
connection.createStatement().use { statement ->
44+
statement.executeQuery(
45+
BufferedReader(
46+
FileReader(
47+
(
48+
"src/main/kotlin/g3401_3500/" +
49+
"s3465_find_products_with_valid_serial_numbers/" +
50+
"script.sql"
51+
),
52+
),
53+
)
54+
.lines()
55+
.collect(Collectors.joining("\n"))
56+
.replace("#.*?\\r?\\n".toRegex(), ""),
57+
).use { resultSet ->
58+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
59+
assertThat<String>(resultSet.getNString(1), equalTo<String>("1"))
60+
assertThat<String>(
61+
resultSet.getNString(2),
62+
equalTo<String>("Widget A"),
63+
)
64+
assertThat<String>(
65+
resultSet.getNString(3),
66+
equalTo<String>("This is a sample product with SN1234-5678"),
67+
)
68+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
69+
assertThat<String>(resultSet.getNString(1), equalTo<String>("2"))
70+
assertThat<String>(
71+
resultSet.getNString(2),
72+
equalTo<String>("Widget B"),
73+
)
74+
assertThat<String>(
75+
resultSet.getNString(3),
76+
equalTo<String>("A product with serial SN9876-1234 in the description"),
77+
)
78+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true))
79+
assertThat<String>(resultSet.getNString(1), equalTo<String>("5"))
80+
assertThat<String>(
81+
resultSet.getNString(2),
82+
equalTo<String>("Widget E"),
83+
)
84+
assertThat<String>(
85+
resultSet.getNString(3),
86+
equalTo<String>("Check out SN4321-8765 in this description"),
87+
)
88+
assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(false))
89+
}
90+
}
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)