Skip to content

Commit 5be596b

Browse files
committed
cleanup and tangle sample file
1 parent 707a098 commit 5be596b

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

spiral-matrix-path/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
This is [this leetcode problem](https://leetcode.com/problems/spiral-matrix-iii/description/) from [Underdog Devs](https://underdog-devs.slack.com/archives/C02FFHZT200/p1723032739134259)
22

3+
[Go to tangled solution file](./matrix-spiral-order.kts)
4+
35

46
# Problem Statement
57

spiral-matrix-path/README.org

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
This is [[https://leetcode.com/problems/spiral-matrix-iii/description/][this leetcode problem]] from [[https://underdog-devs.slack.com/archives/C02FFHZT200/p1723032739134259][Underdog Devs]]
55

6+
[[./matrix-spiral-order.kts][Go to tangled solution file]]
7+
68
* Problem Statement
79
You start at the cell ~(rStart, cStart)~ of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.
810

@@ -269,8 +271,7 @@ That's right, what if we started in column 2?
269271
: res3: kotlin.String = >>>
270272

271273
Oh that's cool. And the right answer. Lets see it with Example 2
272-
273-
#+begin_src kotlin
274+
#+begin_src kotlin :tangle matrix-spiral-order.kts
274275
var rows = 5
275276
var columns = 6
276277
var rStart = 1
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var rows = 5
2+
var columns = 6
3+
var rStart = 1
4+
var cStart = 4
5+
data class Coordinates(val row: Int, val column: Int)
6+
7+
8+
fun spiral(): Sequence<Coordinates> = sequence {
9+
var row = 0
10+
var col = 0
11+
12+
yield(Coordinates(row, col))
13+
14+
val ordinals = generateSequence(1) { it + 1}
15+
val sideSizes = ordinals.iterator()
16+
17+
while (true) {
18+
var sideSize = sideSizes.next()
19+
//
20+
for (n in 0 until sideSize)
21+
yield(Coordinates(row, ++col))
22+
//
23+
for (n in 0 until sideSize)
24+
yield(Coordinates(++row, col))
25+
26+
var nextSideSize = sideSizes.next()
27+
//
28+
for (n in 0 until nextSideSize)
29+
yield(Coordinates(row, --col))
30+
//
31+
for (n in 0 until nextSideSize)
32+
yield(Coordinates(--row, col))
33+
}
34+
}
35+
var spiralCoordinates = spiral().map{ Coordinates(it.row+rStart, it.column+cStart)}
36+
var onGridSpiral = spiralCoordinates.filter { it.row in 0..(rows-1) && it.column in 0..(columns-1) }
37+
var gridInSpiralOrder = onGridSpiral.take(rows*columns)
38+
"\n"+gridInSpiralOrder.map{ "[${it.row}, ${it.column}]"}.joinToString("\n")

0 commit comments

Comments
 (0)