Skip to content

Commit 4d53e38

Browse files
committed
Merge intervals
1 parent 77b04cc commit 4d53e38

File tree

1 file changed

+94
-91
lines changed

1 file changed

+94
-91
lines changed

quickbook.ipynb

Lines changed: 94 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -66,194 +66,197 @@
6666
},
6767
{
6868
"cell_type": "code",
69-
"execution_count": null,
69+
"execution_count": 59,
7070
"metadata": {
7171
"id": "IaxB1auxONix",
7272
"colab": {
7373
"base_uri": "https://localhost:8080/"
7474
},
75-
"outputId": "1c86b6bb-c58c-4b9e-88e1-6e3c071c49cd"
75+
"outputId": "810bf461-0f64-4580-f4eb-09f7cf2f846a"
7676
},
7777
"outputs": [
7878
{
7979
"output_type": "stream",
8080
"name": "stdout",
8181
"text": [
82-
"Overwriting find_first_last.mojo\n"
82+
"Overwriting merge_intervals.mojo\n"
8383
]
8484
}
8585
],
8686
"source": [
87-
"%%writefile find_first_last.mojo\n",
87+
"%%writefile merge_intervals.mojo\n",
8888
"\n",
89-
"### Find First/Last\n",
90-
"### Find first and last index of a target value in a sorted array\n",
89+
"### Merge Itervals\n",
90+
"### Merge overlapping intervals\n",
9191
"\n",
92-
"fn find_first_last(arr: List[Int], target: Int) -> (Int, Int):\n",
93-
" result = (-1, -1)\n",
94-
" if len(arr) == 0:\n",
95-
" return result\n",
96-
" left, right = 0, len(arr) -1\n",
9792
"\n",
98-
" while left <= right:\n",
99-
" mid = (left + right) // 2\n",
100-
" if arr[mid] == target:\n",
101-
" result[1] = mid\n",
102-
" left = mid + 1\n",
103-
" elif arr[mid] > target:\n",
104-
" right = mid - 1\n",
105-
" else:\n",
106-
" left = mid + 1\n",
107-
" left, right = 0, result[1] # result[1] -1 would keep left index at -1 for single occurence of target\n",
93+
"@parameter\n",
94+
"fn compare_fn(interval1: (Int, Int), interval2: (Int, Int)) -> Bool:\n",
95+
" return interval1[0] < interval2[0]\n",
96+
"\n",
97+
"\n",
98+
"fn merge_intervals(mut intervals: List[(Int, Int)]) -> List[(Int, Int)]:\n",
99+
" if len(intervals) == 0:\n",
100+
" return List[(Int, Int)]()\n",
108101
"\n",
109-
" while left <= right:\n",
110-
" mid = (left + right) // 2\n",
111-
" if arr[mid] == target:\n",
112-
" result[0] = mid\n",
113-
" right = mid - 1\n",
114-
" elif arr[mid] > target:\n",
115-
" right = mid - 1\n",
102+
" sort[compare_fn](intervals)\n",
103+
"\n",
104+
" result = List[(Int, Int)]()\n",
105+
" result.append(intervals[0])\n",
106+
" for curr_interval in intervals[1:]:\n",
107+
" start, end = curr_interval[]\n",
108+
" last_interval = result[-1]\n",
109+
" last_start, last_end = last_interval\n",
110+
" if start <= last_end:\n",
111+
" result[len(result) - 1] = (last_start, max(last_end, end))\n",
116112
" else:\n",
117-
" left = mid + 1\n",
113+
" result.append(curr_interval[])\n",
114+
"\n",
118115
" return result\n",
119116
"\n",
117+
"\n",
120118
"from testing import assert_true\n",
121119
"\n",
120+
"\n",
122121
"fn main() raises:\n",
123-
" arr = List(5,7,7,8,8,10)\n",
124-
" target = 8\n",
125-
" result = find_first_last(arr, target)\n",
126-
" assert_true(result[0] == 3 and result[1] == 4, \"Assertion failed\")\n",
127-
" target = 6\n",
128-
" result = find_first_last(arr, target)\n",
129-
" assert_true(result[0] == -1 and result[1] == -1, \"Assertion failed\")\n",
122+
" intervals = List[(Int, Int)]((1, 3), (2, 6), (8, 10), (15, 18))\n",
123+
" expected = List[(Int, Int)]((1, 6), (8, 10), (15, 18))\n",
124+
" result = merge_intervals(intervals)\n",
125+
" i = 0\n",
126+
" for each in result:\n",
127+
" assert_true(each[][0] == expected[i][0] and each[][1] == expected[i][1], \"Assertion failed\")\n",
128+
" i += 1\n",
130129
"\n",
131-
" arr = List(5,7,7,8,10)\n",
132-
" target = 8\n",
133-
" result = find_first_last(arr, target)\n",
134-
" assert_true(result[0] == 3 and result[1] == 3, \"Assertion failed\")\n",
135-
"\n"
130+
" intervals = List[(Int, Int)]((1,4),(4,5))\n",
131+
" expected = List[(Int, Int)]((1, 5))\n",
132+
" result = merge_intervals(intervals)\n",
133+
" i = 0\n",
134+
" for each in result:\n",
135+
" assert_true(each[][0] == expected[i][0] and each[][1] == expected[i][1], \"Assertion failed\")\n",
136+
" i += 1"
136137
]
137138
},
138139
{
139140
"cell_type": "code",
140-
"execution_count": null,
141+
"execution_count": 60,
141142
"metadata": {
142143
"colab": {
143144
"base_uri": "https://localhost:8080/"
144145
},
145146
"id": "h2k9wkDaONiz",
146-
"outputId": "c374f027-c4ce-454f-ca08-1f4f9c2bf141"
147+
"outputId": "eb626910-8fa3-4efa-c748-11dd140951ab"
147148
},
148149
"outputs": [
149150
{
150151
"output_type": "stream",
151152
"name": "stdout",
152153
"text": [
153-
"\u001b[32m⠁\u001b[0m \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K3 3\n"
154+
"\u001b[32m⠁\u001b[0m \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K"
154155
]
155156
}
156157
],
157158
"source": [
158-
"!magic run mojo find_first_last.mojo"
159+
"!magic run mojo merge_intervals.mojo"
159160
]
160161
},
161162
{
162163
"cell_type": "code",
163-
"execution_count": null,
164+
"execution_count": 61,
164165
"metadata": {
165166
"colab": {
166167
"base_uri": "https://localhost:8080/"
167168
},
168169
"id": "bSglX7bNONi0",
169-
"outputId": "77b66492-8da2-483e-a074-c99d2fca3a79"
170+
"outputId": "8b032107-fc51-4c3e-c72e-73ed37c2585e"
170171
},
171172
"outputs": [
172173
{
173174
"output_type": "stream",
174175
"name": "stdout",
175176
"text": [
176-
"\u001b[32m⠁\u001b[0m \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[1mreformatted find_first_last.mojo\u001b[0m\n",
177+
"\u001b[32m⠁\u001b[0m \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[1mreformatted merge_intervals.mojo\u001b[0m\n",
177178
"\n",
178179
"\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n",
179180
"\u001b[34m\u001b[1m1 file \u001b[0m\u001b[1mreformatted\u001b[0m.\n"
180181
]
181182
}
182183
],
183184
"source": [
184-
"!magic run mojo format find_first_last.mojo"
185+
"!magic run mojo format merge_intervals.mojo"
185186
]
186187
},
187188
{
188189
"cell_type": "code",
189190
"source": [
190-
"!cat find_first_last.mojo"
191+
"!cat merge_intervals.mojo"
191192
],
192193
"metadata": {
193194
"colab": {
194195
"base_uri": "https://localhost:8080/"
195196
},
196197
"id": "z2OxrAfbJYKc",
197-
"outputId": "10fd7ac4-728c-4acc-c4c8-8fb863464700"
198+
"outputId": "a908ca4c-119f-45ff-df1f-c70edd0b4750"
198199
},
199-
"execution_count": null,
200+
"execution_count": 62,
200201
"outputs": [
201202
{
202203
"output_type": "stream",
203204
"name": "stdout",
204205
"text": [
205-
"### Find First/Last\n",
206-
"### Find first and last index of a target value in a sorted array\n",
206+
"### Merge Itervals\n",
207+
"### Merge overlapping intervals\n",
207208
"\n",
208209
"\n",
209-
"fn find_first_last(arr: List[Int], target: Int) -> (Int, Int):\n",
210-
" result = (-1, -1)\n",
211-
" if len(arr) == 0:\n",
212-
" return result\n",
213-
" left, right = 0, len(arr) - 1\n",
210+
"@parameter\n",
211+
"fn compare_fn(interval1: (Int, Int), interval2: (Int, Int)) -> Bool:\n",
212+
" return interval1[0] < interval2[0]\n",
214213
"\n",
215-
" while left <= right:\n",
216-
" mid = (left + right) // 2\n",
217-
" if arr[mid] == target:\n",
218-
" result[1] = mid\n",
219-
" left = mid + 1\n",
220-
" elif arr[mid] > target:\n",
221-
" right = mid - 1\n",
222-
" else:\n",
223-
" left = mid + 1\n",
224-
" left, right = (\n",
225-
" 0,\n",
226-
" result[1],\n",
227-
" ) # result[1] -1 would keep left index at -1 for single occurence of target\n",
228214
"\n",
229-
" while left <= right:\n",
230-
" mid = (left + right) // 2\n",
231-
" if arr[mid] == target:\n",
232-
" result[0] = mid\n",
233-
" right = mid - 1\n",
234-
" elif arr[mid] > target:\n",
235-
" right = mid - 1\n",
215+
"fn merge_intervals(mut intervals: List[(Int, Int)]) -> List[(Int, Int)]:\n",
216+
" if len(intervals) == 0:\n",
217+
" return List[(Int, Int)]()\n",
218+
"\n",
219+
" sort[compare_fn](intervals)\n",
220+
"\n",
221+
" result = List[(Int, Int)]()\n",
222+
" result.append(intervals[0])\n",
223+
" for curr_interval in intervals[1:]:\n",
224+
" start, end = curr_interval[]\n",
225+
" last_interval = result[-1]\n",
226+
" last_start, last_end = last_interval\n",
227+
" if start <= last_end:\n",
228+
" result[len(result) - 1] = (last_start, max(last_end, end))\n",
236229
" else:\n",
237-
" left = mid + 1\n",
230+
" result.append(curr_interval[])\n",
231+
"\n",
238232
" return result\n",
239233
"\n",
240234
"\n",
241235
"from testing import assert_true\n",
242236
"\n",
243237
"\n",
244238
"fn main() raises:\n",
245-
" arr = List(5, 7, 7, 8, 8, 10)\n",
246-
" target = 8\n",
247-
" result = find_first_last(arr, target)\n",
248-
" assert_true(result[0] == 3 and result[1] == 4, \"Assertion failed\")\n",
249-
" target = 6\n",
250-
" result = find_first_last(arr, target)\n",
251-
" assert_true(result[0] == -1 and result[1] == -1, \"Assertion failed\")\n",
239+
" intervals = List[(Int, Int)]((1, 3), (2, 6), (8, 10), (15, 18))\n",
240+
" expected = List[(Int, Int)]((1, 6), (8, 10), (15, 18))\n",
241+
" result = merge_intervals(intervals)\n",
242+
" i = 0\n",
243+
" for each in result:\n",
244+
" assert_true(\n",
245+
" each[][0] == expected[i][0] and each[][1] == expected[i][1],\n",
246+
" \"Assertion failed\",\n",
247+
" )\n",
248+
" i += 1\n",
252249
"\n",
253-
" arr = List(5, 7, 7, 8, 10)\n",
254-
" target = 8\n",
255-
" result = find_first_last(arr, target)\n",
256-
" assert_true(result[0] == 3 and result[1] == 3, \"Assertion failed\")\n"
250+
" intervals = List[(Int, Int)]((1, 4), (4, 5))\n",
251+
" expected = List[(Int, Int)]((1, 5))\n",
252+
" result = merge_intervals(intervals)\n",
253+
" i = 0\n",
254+
" for each in result:\n",
255+
" assert_true(\n",
256+
" each[][0] == expected[i][0] and each[][1] == expected[i][1],\n",
257+
" \"Assertion failed\",\n",
258+
" )\n",
259+
" i += 1\n"
257260
]
258261
}
259262
]

0 commit comments

Comments
 (0)