Skip to content

Commit 353b150

Browse files
authored
Added tests.
1 parent 1202148 commit 353b150

File tree

8 files changed

+207
-29
lines changed

8 files changed

+207
-29
lines changed

src.save/main/kotlin/g0101_0200/s0138_copy_list_with_random_pointer/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package g0101_0200.s0138_copy_list_with_random_pointer
44
// #Programming_Skills_II_Day_14 #Udemy_Linked_List
55
// #2022_09_03_Time_274_ms_(80.58%)_Space_40.5_MB_(58.99%)
66

7-
import com_github_leetcode.Node
7+
import com_github_leetcode.random.Node
88

99
class Solution {
1010
fun copyRandomList(head: Node?): Node? {

src.save/test/kotlin/g0101_0200/s0138_copy_list_with_random_pointer/SolutionTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g0101_0200.s0138_copy_list_with_random_pointer
22

3-
import com_github_leetcode.Node
3+
import com_github_leetcode.random.Node
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test

src/main/kotlin/com_github_leetcode/Node.kt

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
11
package com_github_leetcode
22

33
import java.util.StringJoiner
4+
import kotlin.collections.ArrayList
45

56
class Node {
67
var `val`: Int
7-
var next: Node? = null
8-
var random: Node? = null
8+
var neighbors: List<Node>
99

1010
constructor() {
1111
`val` = 0
12+
neighbors = ArrayList()
1213
}
1314

1415
constructor(`val`: Int) {
1516
this.`val` = `val`
17+
neighbors = ArrayList()
1618
}
1719

18-
constructor(`val`: Int, next: Node?, random: Node?) {
20+
constructor(`val`: Int, neighbors: List<Node>) {
1921
this.`val` = `val`
20-
this.next = next
21-
this.random = random
22+
this.neighbors = neighbors
2223
}
2324

2425
override fun toString(): String {
2526
val result = StringJoiner(",", "[", "]")
26-
val result2 = StringJoiner(",", "[", "]")
27-
result2.add(`val`.toString())
28-
if (random == null) {
29-
result2.add("null")
30-
} else {
31-
result2.add(random!!.`val`.toString())
32-
}
33-
result.add(result2.toString())
34-
var curr = next
35-
while (curr != null) {
36-
val result3 = StringJoiner(",", "[", "]")
37-
result3.add(curr.`val`.toString())
38-
if (curr.random == null) {
39-
result3.add("null")
27+
for (node in neighbors) {
28+
if (node.neighbors.isEmpty()) {
29+
result.add(node.`val`.toString())
4030
} else {
41-
var randomIndex = 0
42-
var curr2: Node? = this
43-
while (curr2!!.next != null && curr2 !== curr.random) {
44-
randomIndex += 1
45-
curr2 = curr2.next
31+
val result2 = StringJoiner(",", "[", "]")
32+
for (nodeItem in node.neighbors) {
33+
result2.add(nodeItem.`val`.toString())
4634
}
47-
result3.add(randomIndex.toString())
35+
result.add(result2.toString())
4836
}
49-
result.add(result3.toString())
50-
curr = curr.next
5137
}
5238
return result.toString()
5339
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com_github_leetcode.random
2+
3+
import java.util.StringJoiner
4+
5+
class Node {
6+
var `val`: Int
7+
var next: Node? = null
8+
var random: Node? = null
9+
10+
constructor() {
11+
`val` = 0
12+
}
13+
14+
constructor(`val`: Int) {
15+
this.`val` = `val`
16+
}
17+
18+
constructor(`val`: Int, next: Node?, random: Node?) {
19+
this.`val` = `val`
20+
this.next = next
21+
this.random = random
22+
}
23+
24+
override fun toString(): String {
25+
val result = StringJoiner(",", "[", "]")
26+
val result2 = StringJoiner(",", "[", "]")
27+
result2.add(`val`.toString())
28+
if (random == null) {
29+
result2.add("null")
30+
} else {
31+
result2.add(random!!.`val`.toString())
32+
}
33+
result.add(result2.toString())
34+
var curr = next
35+
while (curr != null) {
36+
val result3 = StringJoiner(",", "[", "]")
37+
result3.add(curr.`val`.toString())
38+
if (curr.random == null) {
39+
result3.add("null")
40+
} else {
41+
var randomIndex = 0
42+
var curr2: Node? = this
43+
while (curr2!!.next != null && curr2 !== curr.random) {
44+
randomIndex += 1
45+
curr2 = curr2.next
46+
}
47+
result3.add(randomIndex.toString())
48+
}
49+
result.add(result3.toString())
50+
curr = curr.next
51+
}
52+
return result.toString()
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com_github_leetcode
2+
3+
import org.hamcrest.CoreMatchers
4+
import org.hamcrest.MatcherAssert
5+
import org.junit.jupiter.api.Test
6+
7+
internal class ListNodeTest {
8+
@Test
9+
fun constructor() {
10+
val listNode = ListNode()
11+
MatcherAssert.assertThat(listNode.toString(), CoreMatchers.equalTo("0"))
12+
}
13+
14+
@Test
15+
fun constructor2() {
16+
val listNode = ListNode(1)
17+
MatcherAssert.assertThat(listNode.toString(), CoreMatchers.equalTo("1"))
18+
}
19+
20+
@Test
21+
fun constructor3() {
22+
val listNode = ListNode(3, ListNode(4))
23+
MatcherAssert.assertThat(listNode.toString(), CoreMatchers.equalTo("3, 4"))
24+
}
25+
26+
@Test
27+
fun constructor4() {
28+
val listNode = ListNode(3, ListNode(4, ListNode(5)))
29+
MatcherAssert.assertThat(listNode.toString(), CoreMatchers.equalTo("3, 4, 5"))
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com_github_leetcode
2+
3+
import org.hamcrest.CoreMatchers
4+
import org.hamcrest.MatcherAssert
5+
import org.junit.jupiter.api.Test
6+
7+
internal class NodeTest {
8+
@Test
9+
fun constructor() {
10+
val node = Node()
11+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(0))
12+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("[]"))
13+
}
14+
15+
@Test
16+
fun constructor2() {
17+
val node = Node(1)
18+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(1))
19+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("[]"))
20+
}
21+
22+
@Test
23+
fun constructor3() {
24+
val node: Node = Node(1, listOf(Node(2)))
25+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(1))
26+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("[2]"))
27+
}
28+
29+
@Test
30+
fun constructor4() {
31+
val node: Node = Node(
32+
1,
33+
listOf(
34+
Node(2, listOf(Node(3)))
35+
)
36+
)
37+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(1))
38+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("[[3]]"))
39+
}
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com_github_leetcode.left_right
2+
3+
import org.hamcrest.CoreMatchers
4+
import org.hamcrest.MatcherAssert
5+
import org.junit.jupiter.api.Test
6+
7+
internal class NodeTest {
8+
@Test
9+
fun constructor() {
10+
val node = Node(1)
11+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(1))
12+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("Node{val=1,left=null,right=null,next=null}"))
13+
}
14+
15+
@Test
16+
fun constructor2() {
17+
val node = Node(1, Node(2), Node(3), Node(4))
18+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(1))
19+
MatcherAssert.assertThat(
20+
node.toString(),
21+
CoreMatchers.equalTo(
22+
"Node{val=1,left=Node{val=2,left=null,right=null," +
23+
"next=null},right=Node{val=3,left=null,right=null,next=null},next=Node{val=4," +
24+
"left=null,right=null,next=null}}"
25+
)
26+
)
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com_github_leetcode.random
2+
3+
import org.hamcrest.CoreMatchers
4+
import org.hamcrest.MatcherAssert
5+
import org.junit.jupiter.api.Test
6+
7+
internal class NodeTest {
8+
@Test
9+
fun constructor() {
10+
val node = Node()
11+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(0))
12+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("[[0,null]]"))
13+
}
14+
15+
@Test
16+
fun constructor2() {
17+
val node = Node(1)
18+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(1))
19+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("[[1,null]]"))
20+
}
21+
22+
@Test
23+
fun constructor3() {
24+
val node = Node(1, Node(2), Node(3))
25+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(1))
26+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("[[1,3],[2,null]]"))
27+
}
28+
29+
@Test
30+
fun constructor4() {
31+
val node = Node(
32+
1,
33+
Node(2, Node(21), Node(22)),
34+
Node(3, null, Node(32))
35+
)
36+
MatcherAssert.assertThat(node.`val`, CoreMatchers.equalTo(1))
37+
MatcherAssert.assertThat(node.toString(), CoreMatchers.equalTo("[[1,3],[2,2],[21,null]]"))
38+
}
39+
}

0 commit comments

Comments
 (0)