|
66 | 66 | }, |
67 | 67 | { |
68 | 68 | "cell_type": "code", |
69 | | - "execution_count": null, |
| 69 | + "execution_count": 59, |
70 | 70 | "metadata": { |
71 | 71 | "id": "IaxB1auxONix", |
72 | 72 | "colab": { |
73 | 73 | "base_uri": "https://localhost:8080/" |
74 | 74 | }, |
75 | | - "outputId": "1c86b6bb-c58c-4b9e-88e1-6e3c071c49cd" |
| 75 | + "outputId": "810bf461-0f64-4580-f4eb-09f7cf2f846a" |
76 | 76 | }, |
77 | 77 | "outputs": [ |
78 | 78 | { |
79 | 79 | "output_type": "stream", |
80 | 80 | "name": "stdout", |
81 | 81 | "text": [ |
82 | | - "Overwriting find_first_last.mojo\n" |
| 82 | + "Overwriting merge_intervals.mojo\n" |
83 | 83 | ] |
84 | 84 | } |
85 | 85 | ], |
86 | 86 | "source": [ |
87 | | - "%%writefile find_first_last.mojo\n", |
| 87 | + "%%writefile merge_intervals.mojo\n", |
88 | 88 | "\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", |
91 | 91 | "\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", |
97 | 92 | "\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", |
108 | 101 | "\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", |
116 | 112 | " else:\n", |
117 | | - " left = mid + 1\n", |
| 113 | + " result.append(curr_interval[])\n", |
| 114 | + "\n", |
118 | 115 | " return result\n", |
119 | 116 | "\n", |
| 117 | + "\n", |
120 | 118 | "from testing import assert_true\n", |
121 | 119 | "\n", |
| 120 | + "\n", |
122 | 121 | "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", |
130 | 129 | "\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" |
136 | 137 | ] |
137 | 138 | }, |
138 | 139 | { |
139 | 140 | "cell_type": "code", |
140 | | - "execution_count": null, |
| 141 | + "execution_count": 60, |
141 | 142 | "metadata": { |
142 | 143 | "colab": { |
143 | 144 | "base_uri": "https://localhost:8080/" |
144 | 145 | }, |
145 | 146 | "id": "h2k9wkDaONiz", |
146 | | - "outputId": "c374f027-c4ce-454f-ca08-1f4f9c2bf141" |
| 147 | + "outputId": "eb626910-8fa3-4efa-c748-11dd140951ab" |
147 | 148 | }, |
148 | 149 | "outputs": [ |
149 | 150 | { |
150 | 151 | "output_type": "stream", |
151 | 152 | "name": "stdout", |
152 | 153 | "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" |
154 | 155 | ] |
155 | 156 | } |
156 | 157 | ], |
157 | 158 | "source": [ |
158 | | - "!magic run mojo find_first_last.mojo" |
| 159 | + "!magic run mojo merge_intervals.mojo" |
159 | 160 | ] |
160 | 161 | }, |
161 | 162 | { |
162 | 163 | "cell_type": "code", |
163 | | - "execution_count": null, |
| 164 | + "execution_count": 61, |
164 | 165 | "metadata": { |
165 | 166 | "colab": { |
166 | 167 | "base_uri": "https://localhost:8080/" |
167 | 168 | }, |
168 | 169 | "id": "bSglX7bNONi0", |
169 | | - "outputId": "77b66492-8da2-483e-a074-c99d2fca3a79" |
| 170 | + "outputId": "8b032107-fc51-4c3e-c72e-73ed37c2585e" |
170 | 171 | }, |
171 | 172 | "outputs": [ |
172 | 173 | { |
173 | 174 | "output_type": "stream", |
174 | 175 | "name": "stdout", |
175 | 176 | "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", |
177 | 178 | "\n", |
178 | 179 | "\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n", |
179 | 180 | "\u001b[34m\u001b[1m1 file \u001b[0m\u001b[1mreformatted\u001b[0m.\n" |
180 | 181 | ] |
181 | 182 | } |
182 | 183 | ], |
183 | 184 | "source": [ |
184 | | - "!magic run mojo format find_first_last.mojo" |
| 185 | + "!magic run mojo format merge_intervals.mojo" |
185 | 186 | ] |
186 | 187 | }, |
187 | 188 | { |
188 | 189 | "cell_type": "code", |
189 | 190 | "source": [ |
190 | | - "!cat find_first_last.mojo" |
| 191 | + "!cat merge_intervals.mojo" |
191 | 192 | ], |
192 | 193 | "metadata": { |
193 | 194 | "colab": { |
194 | 195 | "base_uri": "https://localhost:8080/" |
195 | 196 | }, |
196 | 197 | "id": "z2OxrAfbJYKc", |
197 | | - "outputId": "10fd7ac4-728c-4acc-c4c8-8fb863464700" |
| 198 | + "outputId": "a908ca4c-119f-45ff-df1f-c70edd0b4750" |
198 | 199 | }, |
199 | | - "execution_count": null, |
| 200 | + "execution_count": 62, |
200 | 201 | "outputs": [ |
201 | 202 | { |
202 | 203 | "output_type": "stream", |
203 | 204 | "name": "stdout", |
204 | 205 | "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", |
207 | 208 | "\n", |
208 | 209 | "\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", |
214 | 213 | "\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", |
228 | 214 | "\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", |
236 | 229 | " else:\n", |
237 | | - " left = mid + 1\n", |
| 230 | + " result.append(curr_interval[])\n", |
| 231 | + "\n", |
238 | 232 | " return result\n", |
239 | 233 | "\n", |
240 | 234 | "\n", |
241 | 235 | "from testing import assert_true\n", |
242 | 236 | "\n", |
243 | 237 | "\n", |
244 | 238 | "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", |
252 | 249 | "\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" |
257 | 260 | ] |
258 | 261 | } |
259 | 262 | ] |
|
0 commit comments