Skip to content

Commit b118650

Browse files
committed
Add find rectangle
1 parent 8ac57ad commit b118650

File tree

9 files changed

+148
-75
lines changed

9 files changed

+148
-75
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ time. Take your time before you take a look at the presented solution. **Good lu
7777
- [Fizz Buzz](src/test/kotlin/com/igorwojda/integer/fizzbuzz/desc.md)
7878
- [Caesar Cipher](src/test/kotlin/com/igorwojda/string/caesarcipher/desc.md)
7979
- [Has repeated char](src/test/kotlin/com/igorwojda/string/hasrepeatedcharacter/desc.md)
80+
- [Has repeated char](src/test/kotlin/com/igorwojda/string/hasrepeatedcharacter/desc.md)
8081

8182
**Warrior**
8283

8384
- [Spiral matrix generator](src/test/kotlin/com/igorwojda/matrix/spiralmatrixgenerator/desc.md)
85+
- [Find rectangle](src/test/kotlin/com/igorwojda/matrix/findrectangle/desc.md)
8486
- [Queue](src/test/kotlin/com/igorwojda/queue/basic/desc.md)
8587
- [Combine queues](src/test/kotlin/com/igorwojda/queue/combine/desc.md)
8688
- [Stack](src/test/kotlin/com/igorwojda/stack/basic/desc.md)

misc/PuzzleGroups.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ We use sliding window instead of nested loops which decreases complexity from `O
127127
- [Print numbers with steps](../src/test/kotlin/com/igorwojda/integer/printnumber/steps/desc.md)
128128
- [Pyramid generator](../src/test/kotlin/com/igorwojda/integer/pyramidgenerator/desc.md)
129129
- [Reverse Int](../src/test/kotlin/com/igorwojda/integer/reverse/desc.md)
130-
- [Spiral matrix generator](../src/test/kotlin/com/igorwojda/integer/spiralmatrixgenerator/desc.md)
131130
- [Steps generator](../src/test/kotlin/com/igorwojda/integer/stepsgenerator/desc.md)
132131

133132
## String
@@ -149,6 +148,11 @@ We use sliding window instead of nested loops which decreases complexity from `O
149148
- [Find the vowels](../src/test/kotlin/com/igorwojda/string/vowels/desc.md)
150149
- [Format train route](../src/test/kotlin/com/igorwojda/list/formattrainroute/desc.md)
151150

151+
## Matrix
152+
153+
- [Spiral matrix generator](../src/test/kotlin/com/igorwojda/integer/spiralmatrixgenerator/desc.md)
154+
- [Find rectangle](src/test/kotlin/com/igorwojda/matrix/findrectangle/desc.md)
155+
152156
## Range
153157

154158
- [Contains range?](../src/test/kotlin/com/igorwojda/range/containsrange/desc.md)
File renamed without changes.

src/test/kotlin/com/igorwojda/matrix/findimage/challange.kt

-15
This file was deleted.

src/test/kotlin/com/igorwojda/matrix/findimage/desc.md

-46
This file was deleted.

src/test/kotlin/com/igorwojda/matrix/findimage/solution.kt

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.igorwojda.matrix.findrectangle
2+
3+
import org.amshove.kluent.shouldBeEqualTo
4+
import org.junit.jupiter.api.Test
5+
6+
private fun findRectangle(image: List<List<Int>>): List<Int?> {
7+
TODO("not implemented")
8+
}
9+
10+
private class Test {
11+
@Test
12+
fun `find rectangle in image 1`() {
13+
val image = listOf(
14+
listOf(1, 1, 1, 1, 1, 1, 1),
15+
listOf(1, 1, 1, 1, 1, 1, 1),
16+
listOf(1, 1, 1, 0, 0, 0, 1),
17+
listOf(1, 1, 1, 0, 0, 0, 1),
18+
listOf(1, 1, 1, 1, 1, 1, 1)
19+
)
20+
21+
findRectangle(image) shouldBeEqualTo listOf(2, 3, 3, 5)
22+
}
23+
24+
@Test
25+
fun `find rectangle in image 2`() {
26+
val image = listOf(
27+
listOf(1, 1, 1, 1, 1, 1, 1),
28+
listOf(1, 1, 1, 1, 1, 1, 1),
29+
listOf(1, 1, 1, 1, 1, 1, 1),
30+
listOf(1, 1, 1, 1, 1, 1, 1),
31+
listOf(1, 1, 1, 1, 1, 1, 0)
32+
)
33+
34+
findRectangle(image) shouldBeEqualTo listOf(4, 6, 4, 6)
35+
}
36+
37+
@Test
38+
fun `find rectangle in image 3`() {
39+
val image = listOf(
40+
listOf(1, 1, 1, 1, 1, 1, 1),
41+
listOf(1, 1, 1, 1, 1, 1, 1),
42+
listOf(1, 1, 1, 1, 1, 1, 1),
43+
listOf(1, 1, 1, 1, 1, 0, 0),
44+
listOf(1, 1, 1, 1, 1, 0, 0)
45+
)
46+
47+
findRectangle(image) shouldBeEqualTo listOf(3, 5, 4, 6)
48+
}
49+
50+
@Test
51+
fun `find rectangle in image 4`() {
52+
val image = listOf(
53+
listOf(0, 1, 1, 1, 1, 1, 1),
54+
listOf(1, 1, 1, 1, 1, 1, 1),
55+
listOf(1, 1, 1, 1, 1, 1, 1),
56+
listOf(1, 1, 1, 1, 1, 1, 1),
57+
listOf(1, 1, 1, 1, 1, 1, 1)
58+
)
59+
60+
findRectangle(image) shouldBeEqualTo listOf(0, 0, 0, 0)
61+
}
62+
63+
@Test
64+
fun `find rectangle in image 5`() {
65+
val image = listOf(
66+
listOf(1, 1, 1, 1, 1, 1, 1),
67+
listOf(1, 1, 1, 1, 1, 1, 1),
68+
listOf(0, 0, 1, 1, 1, 1, 1),
69+
listOf(0, 0, 1, 1, 1, 1, 1),
70+
listOf(1, 1, 1, 1, 1, 1, 1)
71+
)
72+
73+
findRectangle(image) shouldBeEqualTo listOf(2, 0, 3, 1)
74+
}
75+
76+
@Test
77+
fun `find rectangle in image 6`() {
78+
val image = listOf(
79+
listOf(0)
80+
)
81+
82+
findRectangle(image) shouldBeEqualTo listOf(0, 0, 0, 0)
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Find rectangle
2+
3+
## Instructions
4+
5+
Image is represent as a simple 2D array where every pixel is a 1 or a 0. The image you get is known to have a single
6+
rectangle of 0s on a background of 1s.
7+
8+
Write a function that takes in the image and returns list containing coordinates of top-left and bottom-right pixels
9+
represented as list of internees:
10+
11+
[challenge.kt](challenge.kt) | [solution](solution.kt)
12+
13+
## Examples
14+
15+
Example 1
16+
17+
```kotlin
18+
val image = listOf(
19+
listOf(1, 1, 0, 0, 0, 1),
20+
listOf(1, 1, 0, 0, 0, 1),
21+
listOf(1, 1, 1, 1, 1, 1),
22+
listOf(1, 1, 1, 1, 1, 1)
23+
)
24+
25+
findRectangle(image) // [0, 2, 1, 4]
26+
27+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.igorwojda.matrix.findrectangle
2+
3+
// Time complexity: O(n*m)
4+
private object Solution1 {
5+
private fun findRectangle(image: List<List<Int>>): List<Int?> {
6+
var top: Pair<Int, Int>? = null
7+
var bottom: Pair<Int, Int>? = null
8+
9+
image.forEachIndexed { rowIndex, rowItem ->
10+
rowItem.forEachIndexed { columnIndex, pixelValue ->
11+
if (pixelValue == 0) {
12+
if (top == null) {
13+
top = rowIndex to columnIndex
14+
} else {
15+
bottom = rowIndex to columnIndex
16+
}
17+
}
18+
}
19+
}
20+
21+
// handle edge case where rectangle has 1 pixel
22+
if (bottom == null) {
23+
bottom = top
24+
}
25+
26+
return listOf(top?.first, top?.second, bottom?.first, bottom?.second)
27+
}
28+
}
29+
30+
private object KtLintWillNotComplain

0 commit comments

Comments
 (0)