\n",
101 | " Str to dict: | \n",
102 | " {'d': 1, 'a': 2, 't': 3, 'r': 2, 'o': 2, '_': 1, 'u': 1, 'n': 1, 'i': 2, 'v': 1, 'e': 1, 's': 1, 'y': 1} | \n",
103 | "
"
104 | ]
105 | },
106 | {
107 | "cell_type": "markdown",
108 | "metadata": {
109 | "colab_type": "text",
110 | "id": "ELtxJQQLjPMi"
111 | },
112 | "source": [
113 | "#### 2. Implement function sec_smallest(numbers) which returns second smallest item in the list, without using the built-in sorting methods (your code mustn't contain such words as 'sort', 'sorted'):\n",
114 | "\n",
115 | "1 point\n",
116 | " "
117 | ]
118 | },
119 | {
120 | "cell_type": "code",
121 | "execution_count": 1,
122 | "metadata": {
123 | "colab": {
124 | "autoexec": {
125 | "startup": false,
126 | "wait_interval": 0
127 | }
128 | },
129 | "colab_type": "code",
130 | "id": "yJVJiK9jjPMj"
131 | },
132 | "outputs": [],
133 | "source": [
134 | "def sec_smallest(numbers):\n",
135 | " \"\"\"\n",
136 | " :param numbers: list[int]\n",
137 | " :return: int \n",
138 | " \"\"\"\n",
139 | " # YOUR CODE HERe\n",
140 | " smallest = min(numbers)\n",
141 | " sec_smallest = max(numbers)\n",
142 | " for elem in numbers:\n",
143 | " if sec_smallest > elem and elem > smallest:\n",
144 | " sec_smallest = elem\n",
145 | " \n",
146 | " return sec_smallest"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 2,
152 | "metadata": {
153 | "colab": {
154 | "autoexec": {
155 | "startup": false,
156 | "wait_interval": 0
157 | }
158 | },
159 | "colab_type": "code",
160 | "id": "pg4ry_8Csdbq"
161 | },
162 | "outputs": [
163 | {
164 | "name": "stdout",
165 | "output_type": "stream",
166 | "text": [
167 | "Sec_smallest: -2\n"
168 | ]
169 | }
170 | ],
171 | "source": [
172 | "print('Sec_smallest:', sec_smallest([1, 2, -8, -8, -2, 0]))"
173 | ]
174 | },
175 | {
176 | "cell_type": "markdown",
177 | "metadata": {
178 | "colab_type": "text",
179 | "id": "qVlYH9axsdbs"
180 | },
181 | "source": [
182 | "Expected Output: \n",
183 | "
\n",
184 | " \n",
185 | " Sec_smallest: | \n",
186 | " -2 | \n",
187 | "
"
188 | ]
189 | },
190 | {
191 | "cell_type": "markdown",
192 | "metadata": {
193 | "colab_type": "text",
194 | "id": "tzgLZ6VijPMo"
195 | },
196 | "source": [
197 | "#### 3. Implement function prime_nums(n) that returns list of numbers which are simple and < n:\n",
198 | "\n",
199 | "1 point
"
200 | ]
201 | },
202 | {
203 | "cell_type": "code",
204 | "execution_count": 3,
205 | "metadata": {
206 | "colab": {
207 | "autoexec": {
208 | "startup": false,
209 | "wait_interval": 0
210 | }
211 | },
212 | "colab_type": "code",
213 | "id": "jvVD9N32jPMp"
214 | },
215 | "outputs": [],
216 | "source": [
217 | "def prime_nums(n):\n",
218 | " \"\"\"\n",
219 | " :param n: int\n",
220 | " :return: list[int]\n",
221 | " \"\"\"\n",
222 | " # YOUR CODE HERE\n",
223 | " prime_numbers = list()\n",
224 | "# prime_numbers = [2, 3]\n",
225 | " \n",
226 | " for i in range(2, n):\n",
227 | " for k in range(2, i):\n",
228 | " if i % k == 0:\n",
229 | " break\n",
230 | " else:\n",
231 | " prime_numbers.append(i)\n",
232 | " \n",
233 | " return prime_numbers"
234 | ]
235 | },
236 | {
237 | "cell_type": "code",
238 | "execution_count": 4,
239 | "metadata": {},
240 | "outputs": [
241 | {
242 | "name": "stdout",
243 | "output_type": "stream",
244 | "text": [
245 | "Prime numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]\n"
246 | ]
247 | }
248 | ],
249 | "source": [
250 | "print('Prime numbers:', prime_nums(30))"
251 | ]
252 | },
253 | {
254 | "cell_type": "markdown",
255 | "metadata": {
256 | "colab": {
257 | "autoexec": {
258 | "startup": false,
259 | "wait_interval": 0
260 | }
261 | },
262 | "colab_type": "code",
263 | "id": "Zf9DMkN9sdbw"
264 | },
265 | "source": [
266 | "print('Prime numbers:', prime_nums(30))"
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "metadata": {
272 | "colab_type": "text",
273 | "id": "pYutFqaAsdbz"
274 | },
275 | "source": [
276 | "Expected Output:\n",
277 | "\n",
278 | " \n",
279 | " Prime numbers: | \n",
280 | " [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] | \n",
281 | "
"
282 | ]
283 | },
284 | {
285 | "cell_type": "markdown",
286 | "metadata": {},
287 | "source": [
288 | "**4. Implement function max_sum_index(tuples), which returnes index of tuple in the list with maximum sum of elements:**\n",
289 | "\n",
290 | "1 point
"
291 | ]
292 | },
293 | {
294 | "cell_type": "code",
295 | "execution_count": 5,
296 | "metadata": {},
297 | "outputs": [],
298 | "source": [
299 | "def max_sum_index(tuples): \n",
300 | " '''\n",
301 | " :param tuples: list[tuple]\n",
302 | " :return: int\n",
303 | " '''\n",
304 | " # YOUR CODE HERE\n",
305 | " max_sum_index = 0\n",
306 | " max_sum = 0\n",
307 | " for i in range(0, len(tuples)):\n",
308 | " small_list =tuples[i]\n",
309 | " if max_sum < sum(small_list):\n",
310 | " max_sum = sum(small_list) \n",
311 | " max_sum_index = i\n",
312 | "\n",
313 | "\n",
314 | " return max_sum_index"
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "execution_count": 6,
320 | "metadata": {},
321 | "outputs": [
322 | {
323 | "name": "stdout",
324 | "output_type": "stream",
325 | "text": [
326 | "Index: 1\n"
327 | ]
328 | }
329 | ],
330 | "source": [
331 | "print(\"Index:\", max_sum_index([(10, 20), (40, 32), (30, 25)]))\n"
332 | ]
333 | },
334 | {
335 | "cell_type": "markdown",
336 | "metadata": {},
337 | "source": [
338 | "Expected Output: \n",
339 | "\n",
340 | " \n",
341 | " Index: | \n",
342 | " 1 | \n",
343 | "
"
344 | ]
345 | },
346 | {
347 | "cell_type": "markdown",
348 | "metadata": {},
349 | "source": [
350 | "**5. Implement function gcd(x, y), which returns the greatest common divisor of n and m.**\n",
351 | "1 point
"
352 | ]
353 | },
354 | {
355 | "cell_type": "code",
356 | "execution_count": 7,
357 | "metadata": {},
358 | "outputs": [],
359 | "source": [
360 | "def gcd(a, b): \n",
361 | " '''\n",
362 | " :params m,n: int\n",
363 | " :return: int\n",
364 | " '''\n",
365 | " # YOUR CODE HERE\n",
366 | " while a != 0 and b != 0:\n",
367 | " if a > b:\n",
368 | " a = a % b\n",
369 | " else:\n",
370 | " b = b % a\n",
371 | " return a + b\n"
372 | ]
373 | },
374 | {
375 | "cell_type": "code",
376 | "execution_count": 8,
377 | "metadata": {},
378 | "outputs": [
379 | {
380 | "name": "stdout",
381 | "output_type": "stream",
382 | "text": [
383 | "GCD: 20\n"
384 | ]
385 | }
386 | ],
387 | "source": [
388 | "print(\"GCD:\", gcd(160, 180))"
389 | ]
390 | },
391 | {
392 | "cell_type": "markdown",
393 | "metadata": {},
394 | "source": [
395 | "Expected Output: \n",
396 | "\n",
397 | " \n",
398 | " GCD: | \n",
399 | " 20 | \n",
400 | "
"
401 | ]
402 | },
403 | {
404 | "cell_type": "markdown",
405 | "metadata": {},
406 | "source": [
407 | "#### 6. Implement recursive sum of the list:\n",
408 | "1 point
"
409 | ]
410 | },
411 | {
412 | "cell_type": "code",
413 | "execution_count": 9,
414 | "metadata": {},
415 | "outputs": [],
416 | "source": [
417 | "def recursive_list_sum(data_list):\n",
418 | " \"\"\"\n",
419 | " :param data_list: list[list]\n",
420 | " \"\"\"\n",
421 | " # YOUR CODE HERE\n",
422 | " some_list = []\n",
423 | " list_sum = 0\n",
424 | " for i in reversed(range(len(data_list))):\n",
425 | " print(data_list)\n",
426 | " if type(data_list[i]) == type(some_list):\n",
427 | " data_list[i] = sum(data_list[i])\n",
428 | " \n",
429 | " return sum(data_list)"
430 | ]
431 | },
432 | {
433 | "cell_type": "code",
434 | "execution_count": 10,
435 | "metadata": {},
436 | "outputs": [
437 | {
438 | "name": "stdout",
439 | "output_type": "stream",
440 | "text": [
441 | "[1, 2, [3, 4], [5, 6], [7, 8, 9]]\n",
442 | "[1, 2, [3, 4], [5, 6], 24]\n",
443 | "[1, 2, [3, 4], 11, 24]\n",
444 | "[1, 2, 7, 11, 24]\n",
445 | "[1, 2, 7, 11, 24]\n",
446 | "The sum of a list is 45\n"
447 | ]
448 | }
449 | ],
450 | "source": [
451 | "print('The sum of a list is', recursive_list_sum([1, 2, [3,4],[5,6], [7, 8, 9]]))"
452 | ]
453 | },
454 | {
455 | "cell_type": "markdown",
456 | "metadata": {},
457 | "source": [
458 | "Expected Output:\n",
459 | "\n",
460 | " \n",
461 | " The sum of a list is 45 | \n",
462 | "
"
463 | ]
464 | },
465 | {
466 | "cell_type": "markdown",
467 | "metadata": {},
468 | "source": [
469 | "#### 7. Implement decorator which returns function signature and it's return value:\n",
470 | "1 point
"
471 | ]
472 | },
473 | {
474 | "cell_type": "code",
475 | "execution_count": 173,
476 | "metadata": {},
477 | "outputs": [],
478 | "source": [
479 | "def debug(func):\n",
480 | " \"\"\"\n",
481 | " :param func: function\n",
482 | " \"\"\"\n",
483 | " # YOUR CODE HERE\n",
484 | " def wrapper(a, b):\n",
485 | " c = func.__name__ + \"(%s\" %a + \", %s)\" %b + \" was called and returned \" + \"%s\" %func(a, b)\n",
486 | " return c\n",
487 | " return wrapper\n",
488 | " "
489 | ]
490 | },
491 | {
492 | "cell_type": "code",
493 | "execution_count": 174,
494 | "metadata": {},
495 | "outputs": [
496 | {
497 | "data": {
498 | "text/plain": [
499 | "'add(3, 4) was called and returned 7'"
500 | ]
501 | },
502 | "execution_count": 174,
503 | "metadata": {},
504 | "output_type": "execute_result"
505 | }
506 | ],
507 | "source": [
508 | "@debug\n",
509 | "def add(a, b):\n",
510 | " return a + b\n",
511 | " \n",
512 | "add(3, 4)\n"
513 | ]
514 | },
515 | {
516 | "cell_type": "markdown",
517 | "metadata": {},
518 | "source": [
519 | "Expected Output:\n",
520 | "\n",
521 | " \n",
522 | " 'add(3, 4) was called and returned 7' | \n",
523 | "
"
524 | ]
525 | },
526 | {
527 | "cell_type": "markdown",
528 | "metadata": {
529 | "colab_type": "text",
530 | "id": "p96mEq8vjPMr"
531 | },
532 | "source": [
533 | "#### 8. Implement class Conv, that contains method to_roman(self, n), which converts decimal numbers to Roman numerals:\n",
534 | "\n",
535 | "2 points
"
536 | ]
537 | },
538 | {
539 | "cell_type": "code",
540 | "execution_count": 46,
541 | "metadata": {
542 | "colab": {
543 | "autoexec": {
544 | "startup": false,
545 | "wait_interval": 0
546 | }
547 | },
548 | "colab_type": "code",
549 | "id": "weYiaNWojPMs"
550 | },
551 | "outputs": [],
552 | "source": [
553 | "class Conv:\n",
554 | " def __init__(self):\n",
555 | " self.val = [\n",
556 | " 1000, 900, 500, 400,\n",
557 | " 100, 90, 50, 40,\n",
558 | " 10, 9, 5, 4, 1\n",
559 | " ]\n",
560 | "\n",
561 | " self.syb = [\n",
562 | " 'M', 'CM', 'D', 'CD',\n",
563 | " 'C', 'XC', 'L', 'XL',\n",
564 | " 'X', 'IX', 'V', 'IV',\n",
565 | " 'I'\n",
566 | " ]\n",
567 | " \n",
568 | " def to_roman(self, num): \n",
569 | " \"\"\"\n",
570 | " :param self:\n",
571 | " :param n: int\n",
572 | " :return: str \n",
573 | " \"\"\"\n",
574 | " # YOUR CODE HERE\n",
575 | " \n",
576 | " roman_num = \"\"\n",
577 | " i = 0\n",
578 | " while num > 0:\n",
579 | " for _ in range(num // self.val[i]):\n",
580 | " roman_num += self.syb[i]\n",
581 | " num -= self.val[i]\n",
582 | " i += 1\n",
583 | " return roman_num\n",
584 | " \n",
585 | " \n",
586 | "# roman = \"\"\n",
587 | "# for i in range(len(self.val)):\n",
588 | "# if num//self.val[i] == 0:\n",
589 | "# continue\n",
590 | "# else: \n",
591 | "# roman.append(self.syb[])\n",
592 | " "
593 | ]
594 | },
595 | {
596 | "cell_type": "code",
597 | "execution_count": 47,
598 | "metadata": {
599 | "colab": {
600 | "autoexec": {
601 | "startup": false,
602 | "wait_interval": 0
603 | }
604 | },
605 | "colab_type": "code",
606 | "id": "NdffBhZesdb6"
607 | },
608 | "outputs": [
609 | {
610 | "name": "stdout",
611 | "output_type": "stream",
612 | "text": [
613 | "Converted: XLIV\n"
614 | ]
615 | }
616 | ],
617 | "source": [
618 | "print('Converted:', Conv().to_roman(44))"
619 | ]
620 | },
621 | {
622 | "cell_type": "markdown",
623 | "metadata": {
624 | "colab_type": "text",
625 | "id": "lF6Acd9gsdb8"
626 | },
627 | "source": [
628 | "Expected Output:\n",
629 | "\n",
630 | " \n",
631 | " Converted: | \n",
632 | " XLIV | \n",
633 | "
"
634 | ]
635 | }
636 | ],
637 | "metadata": {
638 | "colab": {
639 | "collapsed_sections": [],
640 | "default_view": {},
641 | "name": "DataRoot University _ Data Science Module 1 _ Test 1.ipynb",
642 | "provenance": [],
643 | "version": "0.3.2",
644 | "views": {}
645 | },
646 | "kernelspec": {
647 | "display_name": "Python 3",
648 | "language": "python",
649 | "name": "python3"
650 | },
651 | "language_info": {
652 | "codemirror_mode": {
653 | "name": "ipython",
654 | "version": 3
655 | },
656 | "file_extension": ".py",
657 | "mimetype": "text/x-python",
658 | "name": "python",
659 | "nbconvert_exporter": "python",
660 | "pygments_lexer": "ipython3",
661 | "version": "3.7.6"
662 | }
663 | },
664 | "nbformat": 4,
665 | "nbformat_minor": 1
666 | }
667 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Data-Science-Fundamentals-Course
2 | Practical exercises from Data Science Fundamentals by DataRoot Labs
3 |
4 | Coursera ML
5 | https://dataplatform.cloud.ibm.com/analytics/notebooks/v2/93948b4b-f7f5-478e-90ab-278d6fdd2756/view?access_token=067f9493dcb3f02baa726bf6aba5f3f1db94d597aa9f3ea5a67915057406faf8
6 |
7 | Kaggle Housing Prices Competition for Kaggle Learn Users
8 | https://www.kaggle.com/dmytro21/exercise-xgboost
9 |
--------------------------------------------------------------------------------