Skip to content

Commit bcc1424

Browse files
authored
Added tasks 193, 194, 195, 196.
1 parent e4e2b56 commit bcc1424

File tree

10 files changed

+180
-0
lines changed

10 files changed

+180
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
628628

629629
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
630630
|-|-|-|-|-|-
631+
| 0196 |[Delete Duplicate Emails](src/main/kotlin/g0101_0200/s0196_delete_duplicate_emails/script.sql)| Easy | Database | 593 | 94.17
631632

632633
#### Day 3 String Processing Functions
633634

@@ -1571,6 +1572,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15711572
| 0200 |[Number of Islands](src/main/kotlin/g0101_0200/s0200_number_of_islands/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph | 252 | 95.41
15721573
| 0199 |[Binary Tree Right Side View](src/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_16_Tree, Level_2_Day_15_Tree | 194 | 92.89
15731574
| 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming | 156 | 92.24
1575+
| 0196 |[Delete Duplicate Emails](src/main/kotlin/g0101_0200/s0196_delete_duplicate_emails/script.sql)| Easy | Database, SQL_I_Day_2_Select_and_Order | 593 | 94.17
1576+
| 0195 |[Tenth Line](src/main/kotlin/g0101_0200/s0195_tenth_line/script.sh)| Easy | Shell | 36 | 87.50
1577+
| 0194 |[Transpose File](src/main/kotlin/g0101_0200/s0194_transpose_file/script.sh)| Medium | Shell | 477 | 28.60
1578+
| 0193 |[Valid Phone Numbers](src/main/kotlin/g0101_0200/s0193_valid_phone_numbers/script.sh)| Easy | Shell | 98 | 88.64
15741579
| 0192 |[Word Frequency](src/main/kotlin/g0101_0200/s0192_word_frequency/script.sh)| Medium | Shell | 114 | 73.60
15751580
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation, Algorithm_I_Day_13_Bit_Manipulation, Programming_Skills_I_Day_2_Operator, Udemy_Bit_Manipulation | 237 | 68.44
15761581
| 0190 |[Reverse Bits](src/main/kotlin/g0101_0200/s0190_reverse_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Bit_Manipulation | 198 | 81.82
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
193\. Valid Phone Numbers
2+
3+
Easy
4+
5+
Given a text file `file.txt` that contains a list of phone numbers (one per line), write a one-liner bash script to print all valid phone numbers.
6+
7+
You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
8+
9+
You may also assume each line in the text file must not contain leading or trailing white spaces.
10+
11+
**Example:**
12+
13+
Assume that `file.txt` has the following content:
14+
15+
987-123-4567
16+
123 456 7890
17+
(123) 456-7890
18+
19+
Your script should output the following valid phone numbers:
20+
21+
987-123-4567
22+
(123) 456-7890
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Read from the file file.txt and output all valid phone numbers to stdout.
2+
# #Easy #Shell #2022_10_18_Time_98_ms_(88.64%)_Space_3.1_MB_(55.55%)
3+
egrep '^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}$' file.txt
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
194\. Transpose File
2+
3+
Medium
4+
5+
Given a text file `file.txt`, transpose its content.
6+
7+
You may assume that each row has the same number of columns, and each field is separated by the `' '` character.
8+
9+
**Example:**
10+
11+
If `file.txt` has the following content:
12+
13+
name age
14+
alice 21
15+
ryan 30
16+
17+
Output the following:
18+
19+
name alice ryan
20+
age 21 30
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Read from the file file.txt and print its transposed content to stdout.
2+
# #Medium #Shell #2022_10_18_Time_477_ms_(28.60%)_Space_3.9_MB_(33.97%)
3+
wordcount=$(head -1 file.txt | wc -w)
4+
col_n=1
5+
while [[ $col_n -le $wordcount ]]; do
6+
awk "{ print \$$col_n }" file.txt | paste -sd " "
7+
col_n=$((col_n + 1))
8+
done
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
195\. Tenth Line
2+
3+
Easy
4+
5+
Given a text file `file.txt`, print just the 10th line of the file.
6+
7+
**Example:**
8+
9+
Assume that `file.txt` has the following content:
10+
11+
Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10
12+
13+
Your script should output the tenth line, which is:
14+
15+
Line 10
16+
17+
**Note:**
18+
1\. If the file contains less than 10 lines, what should you output?
19+
2\. There's at least three different solutions. Try to explore all possibilities.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Read from the file file.txt and output the tenth line to stdout.
2+
# #Easy #Shell #2022_10_18_Time_36_ms_(87.50%)_Space_3.6_MB_(81.52%)
3+
sed -n 10p file.txt
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
196\. Delete Duplicate Emails
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Person`
8+
9+
+-------------+---------+
10+
| Column Name | Type |
11+
+-------------+---------+
12+
| id | int |
13+
| email | varchar |
14+
+-------------+---------+
15+
id is the primary key column for this table.
16+
Each row of this table contains an email. The emails will not contain uppercase letters.
17+
18+
Write an SQL query to **delete** all the duplicate emails, keeping only one unique email with the smallest `id`.
19+
20+
Return the result table in **any order**.
21+
22+
The query result format is in the following example.
23+
24+
**Example 1:**
25+
26+
**Input:**
27+
28+
Person table:
29+
+----+------------------+
30+
| id | email |
31+
+----+------------------+
32+
| 1 | john@example.com |
33+
| 2 | bob@example.com |
34+
| 3 | john@example.com |
35+
+----+------------------+
36+
37+
**Output:**
38+
39+
+----+------------------+
40+
| id | email |
41+
+----+------------------+
42+
| 1 | john@example.com |
43+
| 2 | bob@example.com |
44+
+----+------------------+
45+
46+
**Explanation:** john@example.com is repeated two times. We keep the row with the smallest Id = 1.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #SQL_I_Day_2_Select_and_Order #2022_10_18_Time_593_ms_(94.17%)_Space_0B_(100.00%)
3+
DELETE FROM Person
4+
WHERE Id NOT IN (SELECT id FROM (SELECT Email, MIN(Id) AS id FROM Person GROUP BY Email) t)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0101_0200.s0196_delete_duplicate_emails
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.joining
14+
import javax.sql.DataSource
15+
16+
@EmbeddedDatabaseTest(
17+
compatibilityMode = CompatibilityMode.MySQL,
18+
initialSqls = [
19+
"CREATE TABLE Person(id INTEGER PRIMARY KEY, email VARCHAR); " +
20+
"INSERT INTO Person(id, email) VALUES (1, 'john@example.com'); " +
21+
"INSERT INTO Person(id, email) VALUES (2, 'bob@example.com'); " +
22+
"INSERT INTO Person(id, email) VALUES (3, 'john@example.com'); "
23+
]
24+
)
25+
internal class MysqlTest {
26+
@Test
27+
@Throws(SQLException::class, FileNotFoundException::class)
28+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
29+
dataSource.connection.use { connection ->
30+
connection.createStatement().use { statement ->
31+
statement.executeUpdate(
32+
BufferedReader(
33+
FileReader(
34+
"src/main/kotlin/g0101_0200/s0196_delete_duplicate_emails/script.sql"
35+
)
36+
)
37+
.lines()
38+
.collect(joining("\n"))
39+
.replace("#.*?\\r?\\n".toRegex(), "")
40+
)
41+
val resultSet = statement.executeQuery("select email from Person")
42+
assertThat(resultSet.next(), equalTo(true))
43+
assertThat(resultSet.getNString(1), equalTo("john@example.com"))
44+
assertThat(resultSet.next(), equalTo(true))
45+
assertThat(resultSet.getNString(1), equalTo("bob@example.com"))
46+
assertThat(resultSet.next(), equalTo(false))
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)