From 33091865347682a5fda1093ef20dee0b0295ad57 Mon Sep 17 00:00:00 2001 From: "Chi Ting, Low" Date: Mon, 20 Mar 2023 21:23:03 +0800 Subject: [PATCH 1/6] Created using Colaboratory --- Python_Exercise.ipynb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Python_Exercise.ipynb diff --git a/Python_Exercise.ipynb b/Python_Exercise.ipynb new file mode 100644 index 000000000..5584b5eed --- /dev/null +++ b/Python_Exercise.ipynb @@ -0,0 +1,39 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyOkSkXUvTNQCLZG9l5izgI3", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "qvP-tF0sLxO3" + }, + "outputs": [], + "source": [] + } + ] +} \ No newline at end of file From 93311b18beea776c323af8e474a0404e019f9fd4 Mon Sep 17 00:00:00 2001 From: "Chi Ting, Low" Date: Mon, 20 Mar 2023 21:25:29 +0800 Subject: [PATCH 2/6] Created using Colaboratory --- Python_Exercise.ipynb | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/Python_Exercise.ipynb b/Python_Exercise.ipynb index 5584b5eed..998fc3d93 100644 --- a/Python_Exercise.ipynb +++ b/Python_Exercise.ipynb @@ -4,7 +4,7 @@ "metadata": { "colab": { "provenance": [], - "authorship_tag": "ABX9TyOkSkXUvTNQCLZG9l5izgI3", + "authorship_tag": "ABX9TyM/OpGh7cnwnIH0++Cg87sy", "include_colab_link": true }, "kernelspec": { @@ -28,12 +28,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { - "id": "qvP-tF0sLxO3" + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qvP-tF0sLxO3", + "outputId": "1381dfdd-3909-492f-d930-50b36ecec7f6" }, - "outputs": [], - "source": [] + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Hello, world!\n", + "What is your name?\n", + "Alice\n", + "Hello, Alice\n" + ] + } + ], + "source": [ + "# Exercise 1\n", + "\n", + "print(\"Hello, world!\")\n", + "print(\"What is your name?\")\n", + "name = input()\n", + "print(\"Hello, \" + name)" + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "XL5vGccAMLCg" + }, + "execution_count": null, + "outputs": [] } ] } \ No newline at end of file From a9135608858ec5b33000aed3f5bb025719fb6894 Mon Sep 17 00:00:00 2001 From: "Chi Ting, Low" Date: Mon, 20 Mar 2023 21:57:20 +0800 Subject: [PATCH 3/6] Created using Colaboratory --- Python_Exercise.ipynb | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/Python_Exercise.ipynb b/Python_Exercise.ipynb index 998fc3d93..c061104cb 100644 --- a/Python_Exercise.ipynb +++ b/Python_Exercise.ipynb @@ -4,7 +4,7 @@ "metadata": { "colab": { "provenance": [], - "authorship_tag": "ABX9TyM/OpGh7cnwnIH0++Cg87sy", + "authorship_tag": "ABX9TyMUF5GbnRet08NQyqJx32Hy", "include_colab_link": true }, "kernelspec": { @@ -49,7 +49,7 @@ } ], "source": [ - "# Exercise 1\n", + "# Exercise 1 input statement\n", "\n", "print(\"Hello, world!\")\n", "print(\"What is your name?\")\n", @@ -59,10 +59,37 @@ }, { "cell_type": "code", - "source": [], + "source": [ + "# Temperature conversion\n", + "import sys\n", + "import traceback\n", + "\n", + "def convertToFahrenheit(degreesCelsius):\n", + " fahrenheit = degreesCelsius * (9/5) + 32\n", + " return fahrenheit\n", + "\n", + "def convertToCelsius(degreesFahrenheit):\n", + " celcius = (degreesFahrenheit - 32) * (5 / 9)\n", + " return celcius\n", + "\n", + "assert convertToCelsius(0) == -17.77777777777778\n", + "assert convertToCelsius(180) == 82.22222222222223\n", + "assert convertToFahrenheit(0) == 32\n", + "assert convertToFahrenheit(100) == 212\n", + "assert convertToCelsius(convertToFahrenheit(15)) == 15\n" + ], "metadata": { "id": "XL5vGccAMLCg" }, + "execution_count": 14, + "outputs": [] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "vs4MTxqoTsf1" + }, "execution_count": null, "outputs": [] } From 220e733e4509936ad37dbeb0f5ca0befa5d56926 Mon Sep 17 00:00:00 2001 From: "Chi Ting, Low" Date: Mon, 20 Mar 2023 22:20:03 +0800 Subject: [PATCH 4/6] Created using Colaboratory --- Python_Exercise.ipynb | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Python_Exercise.ipynb b/Python_Exercise.ipynb index c061104cb..8d1486cee 100644 --- a/Python_Exercise.ipynb +++ b/Python_Exercise.ipynb @@ -4,7 +4,7 @@ "metadata": { "colab": { "provenance": [], - "authorship_tag": "ABX9TyMUF5GbnRet08NQyqJx32Hy", + "authorship_tag": "ABX9TyORiw3mTQ62G2y9if5q20DD", "include_colab_link": true }, "kernelspec": { @@ -86,10 +86,37 @@ }, { "cell_type": "code", - "source": [], + "source": [ + "# Odd even\n", + "def isOdd(number):\n", + " return number % 2 == 1\n", + "\n", + "def isEven(number):\n", + " return number % 2 == 0\n", + "\n", + "assert isOdd(42) == False\n", + "assert isOdd(9999) == True\n", + "assert isOdd(-10) == False\n", + "assert isOdd(-11) == True\n", + "assert isOdd(3.1415) == False\n", + "assert isEven(42) == True\n", + "assert isEven(9999) == False\n", + "assert isEven(-10) == True\n", + "assert isEven(-11) == False\n", + "assert isEven(3.1415) == False\n" + ], "metadata": { "id": "vs4MTxqoTsf1" }, + "execution_count": 15, + "outputs": [] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "ocyphgksY35f" + }, "execution_count": null, "outputs": [] } From 5330211ec539ebbe710be070ed2c02f7d7a77199 Mon Sep 17 00:00:00 2001 From: "Chi Ting, Low" Date: Mon, 20 Mar 2023 22:31:21 +0800 Subject: [PATCH 5/6] Created using Colaboratory --- Python_Exercise.ipynb | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/Python_Exercise.ipynb b/Python_Exercise.ipynb index 8d1486cee..be704e3ed 100644 --- a/Python_Exercise.ipynb +++ b/Python_Exercise.ipynb @@ -4,7 +4,7 @@ "metadata": { "colab": { "provenance": [], - "authorship_tag": "ABX9TyORiw3mTQ62G2y9if5q20DD", + "authorship_tag": "ABX9TyP43LF+OpYoJzGWFMnSb7zK", "include_colab_link": true }, "kernelspec": { @@ -113,10 +113,45 @@ }, { "cell_type": "code", - "source": [], + "source": [ + "# Area and Volume\n", + "def area(length, width):\n", + " return length * width\n", + "\n", + "def perimeter(length, width):\n", + " return (length * 2) + (width * 2)\n", + "\n", + "def volume(length, width, height):\n", + " return length * width * height\n", + "\n", + "def surfaceArea(length, width, height):\n", + " return (length * width * 2) + (length * height * 2) + (width * height * 2)\n", + "\n", + "assert area(10, 10) == 100\n", + "assert area(0, 9999) == 0\n", + "assert area(5, 8) == 40\n", + "assert perimeter(10, 10) == 40\n", + "assert perimeter(0, 9999) == 19998\n", + "assert perimeter(5, 8) == 26\n", + "assert volume(10, 10, 10) == 1000\n", + "assert volume(9999, 0, 9999) == 0\n", + "assert volume(5, 8, 10) == 400\n", + "assert surfaceArea(10, 10, 10) == 600\n", + "assert surfaceArea(9999, 0, 9999) == 199960002\n", + "assert surfaceArea(5, 8, 10) == 340\n" + ], "metadata": { "id": "ocyphgksY35f" }, + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "J4DYA3-vbeNO" + }, "execution_count": null, "outputs": [] } From ab6565c496ed2ff0b52db4d270cd10f8601c68b1 Mon Sep 17 00:00:00 2001 From: "Chi Ting, Low" Date: Thu, 23 Mar 2023 12:37:25 +0800 Subject: [PATCH 6/6] Created using Colaboratory --- Python_Exercise.ipynb | 473 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 466 insertions(+), 7 deletions(-) diff --git a/Python_Exercise.ipynb b/Python_Exercise.ipynb index be704e3ed..20e47c32f 100644 --- a/Python_Exercise.ipynb +++ b/Python_Exercise.ipynb @@ -4,7 +4,7 @@ "metadata": { "colab": { "provenance": [], - "authorship_tag": "ABX9TyP43LF+OpYoJzGWFMnSb7zK", + "authorship_tag": "ABX9TyMP55Ut8OGYErOwIeNFshhT", "include_colab_link": true }, "kernelspec": { @@ -28,13 +28,13 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "qvP-tF0sLxO3", - "outputId": "1381dfdd-3909-492f-d930-50b36ecec7f6" + "outputId": "3a101753-13e2-4e27-d896-339dc796db93" }, "outputs": [ { @@ -81,7 +81,7 @@ "metadata": { "id": "XL5vGccAMLCg" }, - "execution_count": 14, + "execution_count": 2, "outputs": [] }, { @@ -108,7 +108,7 @@ "metadata": { "id": "vs4MTxqoTsf1" }, - "execution_count": 15, + "execution_count": 3, "outputs": [] }, { @@ -143,14 +143,473 @@ "metadata": { "id": "ocyphgksY35f" }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# FizzBuzz\n", + "def fizzBuzz(upTo):\n", + " for i in range(1, upTo):\n", + " if i % 3 == 0 and i % 5 == 0:\n", + " print('FizzBuz', end = ' ')\n", + " elif i % 3 == 0:\n", + " print(\"Fizz\", end = ' ')\n", + " elif i % 5 == 0:\n", + " print('Buzz', end = ' ')\n", + " else:\n", + " print(i, end = ' ')\n", + "\n", + "fizzBuzz(1000)" + ], + "metadata": { + "id": "J4DYA3-vbeNO", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6dcadf6e-8305-40e7-e6fc-21087d90f10e" + }, "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz 101 Fizz 103 104 FizzBuz 106 107 Fizz 109 Buzz Fizz 112 113 Fizz Buzz 116 Fizz 118 119 FizzBuz 121 122 Fizz 124 Buzz Fizz 127 128 Fizz Buzz 131 Fizz 133 134 FizzBuz 136 137 Fizz 139 Buzz Fizz 142 143 Fizz Buzz 146 Fizz 148 149 FizzBuz 151 152 Fizz 154 Buzz Fizz 157 158 Fizz Buzz 161 Fizz 163 164 FizzBuz 166 167 Fizz 169 Buzz Fizz 172 173 Fizz Buzz 176 Fizz 178 179 FizzBuz 181 182 Fizz 184 Buzz Fizz 187 188 Fizz Buzz 191 Fizz 193 194 FizzBuz 196 197 Fizz 199 Buzz Fizz 202 203 Fizz Buzz 206 Fizz 208 209 FizzBuz 211 212 Fizz 214 Buzz Fizz 217 218 Fizz Buzz 221 Fizz 223 224 FizzBuz 226 227 Fizz 229 Buzz Fizz 232 233 Fizz Buzz 236 Fizz 238 239 FizzBuz 241 242 Fizz 244 Buzz Fizz 247 248 Fizz Buzz 251 Fizz 253 254 FizzBuz 256 257 Fizz 259 Buzz Fizz 262 263 Fizz Buzz 266 Fizz 268 269 FizzBuz 271 272 Fizz 274 Buzz Fizz 277 278 Fizz Buzz 281 Fizz 283 284 FizzBuz 286 287 Fizz 289 Buzz Fizz 292 293 Fizz Buzz 296 Fizz 298 299 FizzBuz 301 302 Fizz 304 Buzz Fizz 307 308 Fizz Buzz 311 Fizz 313 314 FizzBuz 316 317 Fizz 319 Buzz Fizz 322 323 Fizz Buzz 326 Fizz 328 329 FizzBuz 331 332 Fizz 334 Buzz Fizz 337 338 Fizz Buzz 341 Fizz 343 344 FizzBuz 346 347 Fizz 349 Buzz Fizz 352 353 Fizz Buzz 356 Fizz 358 359 FizzBuz 361 362 Fizz 364 Buzz Fizz 367 368 Fizz Buzz 371 Fizz 373 374 FizzBuz 376 377 Fizz 379 Buzz Fizz 382 383 Fizz Buzz 386 Fizz 388 389 FizzBuz 391 392 Fizz 394 Buzz Fizz 397 398 Fizz Buzz 401 Fizz 403 404 FizzBuz 406 407 Fizz 409 Buzz Fizz 412 413 Fizz Buzz 416 Fizz 418 419 FizzBuz 421 422 Fizz 424 Buzz Fizz 427 428 Fizz Buzz 431 Fizz 433 434 FizzBuz 436 437 Fizz 439 Buzz Fizz 442 443 Fizz Buzz 446 Fizz 448 449 FizzBuz 451 452 Fizz 454 Buzz Fizz 457 458 Fizz Buzz 461 Fizz 463 464 FizzBuz 466 467 Fizz 469 Buzz Fizz 472 473 Fizz Buzz 476 Fizz 478 479 FizzBuz 481 482 Fizz 484 Buzz Fizz 487 488 Fizz Buzz 491 Fizz 493 494 FizzBuz 496 497 Fizz 499 Buzz Fizz 502 503 Fizz Buzz 506 Fizz 508 509 FizzBuz 511 512 Fizz 514 Buzz Fizz 517 518 Fizz Buzz 521 Fizz 523 524 FizzBuz 526 527 Fizz 529 Buzz Fizz 532 533 Fizz Buzz 536 Fizz 538 539 FizzBuz 541 542 Fizz 544 Buzz Fizz 547 548 Fizz Buzz 551 Fizz 553 554 FizzBuz 556 557 Fizz 559 Buzz Fizz 562 563 Fizz Buzz 566 Fizz 568 569 FizzBuz 571 572 Fizz 574 Buzz Fizz 577 578 Fizz Buzz 581 Fizz 583 584 FizzBuz 586 587 Fizz 589 Buzz Fizz 592 593 Fizz Buzz 596 Fizz 598 599 FizzBuz 601 602 Fizz 604 Buzz Fizz 607 608 Fizz Buzz 611 Fizz 613 614 FizzBuz 616 617 Fizz 619 Buzz Fizz 622 623 Fizz Buzz 626 Fizz 628 629 FizzBuz 631 632 Fizz 634 Buzz Fizz 637 638 Fizz Buzz 641 Fizz 643 644 FizzBuz 646 647 Fizz 649 Buzz Fizz 652 653 Fizz Buzz 656 Fizz 658 659 FizzBuz 661 662 Fizz 664 Buzz Fizz 667 668 Fizz Buzz 671 Fizz 673 674 FizzBuz 676 677 Fizz 679 Buzz Fizz 682 683 Fizz Buzz 686 Fizz 688 689 FizzBuz 691 692 Fizz 694 Buzz Fizz 697 698 Fizz Buzz 701 Fizz 703 704 FizzBuz 706 707 Fizz 709 Buzz Fizz 712 713 Fizz Buzz 716 Fizz 718 719 FizzBuz 721 722 Fizz 724 Buzz Fizz 727 728 Fizz Buzz 731 Fizz 733 734 FizzBuz 736 737 Fizz 739 Buzz Fizz 742 743 Fizz Buzz 746 Fizz 748 749 FizzBuz 751 752 Fizz 754 Buzz Fizz 757 758 Fizz Buzz 761 Fizz 763 764 FizzBuz 766 767 Fizz 769 Buzz Fizz 772 773 Fizz Buzz 776 Fizz 778 779 FizzBuz 781 782 Fizz 784 Buzz Fizz 787 788 Fizz Buzz 791 Fizz 793 794 FizzBuz 796 797 Fizz 799 Buzz Fizz 802 803 Fizz Buzz 806 Fizz 808 809 FizzBuz 811 812 Fizz 814 Buzz Fizz 817 818 Fizz Buzz 821 Fizz 823 824 FizzBuz 826 827 Fizz 829 Buzz Fizz 832 833 Fizz Buzz 836 Fizz 838 839 FizzBuz 841 842 Fizz 844 Buzz Fizz 847 848 Fizz Buzz 851 Fizz 853 854 FizzBuz 856 857 Fizz 859 Buzz Fizz 862 863 Fizz Buzz 866 Fizz 868 869 FizzBuz 871 872 Fizz 874 Buzz Fizz 877 878 Fizz Buzz 881 Fizz 883 884 FizzBuz 886 887 Fizz 889 Buzz Fizz 892 893 Fizz Buzz 896 Fizz 898 899 FizzBuz 901 902 Fizz 904 Buzz Fizz 907 908 Fizz Buzz 911 Fizz 913 914 FizzBuz 916 917 Fizz 919 Buzz Fizz 922 923 Fizz Buzz 926 Fizz 928 929 FizzBuz 931 932 Fizz 934 Buzz Fizz 937 938 Fizz Buzz 941 Fizz 943 944 FizzBuz 946 947 Fizz 949 Buzz Fizz 952 953 Fizz Buzz 956 Fizz 958 959 FizzBuz 961 962 Fizz 964 Buzz Fizz 967 968 Fizz Buzz 971 Fizz 973 974 FizzBuz 976 977 Fizz 979 Buzz Fizz 982 983 Fizz Buzz 986 Fizz 988 989 FizzBuz 991 992 Fizz 994 Buzz Fizz 997 998 Fizz " + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Ordinal Suffix\n", + "\n", + "def ordinalSuffix(number):\n", + " numberStr = str(number)\n", + " if numberStr[-2:] in ('11', '12', '13'):\n", + " return numberStr+'th'\n", + " if numberStr[-1:] == '1':\n", + " return numberStr + 'st'\n", + " if numberStr[-1:] == '2':\n", + " return numberStr + 'nd'\n", + " if numberStr[-1:] == '3':\n", + " return numberStr + 'rd'\n", + " return numberStr + 'th'\n", + " \n", + "assert ordinalSuffix(0) == '0th'\n", + "assert ordinalSuffix(1) == '1st'\n", + "assert ordinalSuffix(2) == '2nd'\n", + "assert ordinalSuffix(3) == '3rd'\n", + "assert ordinalSuffix(4) == '4th'\n", + "assert ordinalSuffix(10) == '10th'\n", + "assert ordinalSuffix(11) == '11th'\n", + "assert ordinalSuffix(12) == '12th'\n", + "assert ordinalSuffix(13) == '13th'\n", + "assert ordinalSuffix(14) == '14th'\n", + "assert ordinalSuffix(101) == '101st'\n" + ], + "metadata": { + "id": "lVLwcub2F0RI" + }, + "execution_count": 19, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# ASCII table\n", + "def printASCIITable():\n", + " # Loop over integers 32 up to and including 126:\n", + " for i in range(32, 127):\n", + " # Print the integer and its ASCII text character:\n", + " print(i, chr(i), end = '\\t')\n", + "\n", + "printASCIITable()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IJcr7FNDSIwi", + "outputId": "848aac67-d884-4f62-ed26-5153ba2c0eb0" + }, + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "32 \t33 !\t34 \"\t35 #\t36 $\t37 %\t38 &\t39 '\t40 (\t41 )\t42 *\t43 +\t44 ,\t45 -\t46 .\t47 /\t48 0\t49 1\t50 2\t51 3\t52 4\t53 5\t54 6\t55 7\t56 8\t57 9\t58 :\t59 ;\t60 <\t61 =\t62 >\t63 ?\t64 @\t65 A\t66 B\t67 C\t68 D\t69 E\t70 F\t71 G\t72 H\t73 I\t74 J\t75 K\t76 L\t77 M\t78 N\t79 O\t80 P\t81 Q\t82 R\t83 S\t84 T\t85 U\t86 V\t87 W\t88 X\t89 Y\t90 Z\t91 [\t92 \\\t93 ]\t94 ^\t95 _\t96 `\t97 a\t98 b\t99 c\t100 d\t101 e\t102 f\t103 g\t104 h\t105 i\t106 j\t107 k\t108 l\t109 m\t110 n\t111 o\t112 p\t113 q\t114 r\t115 s\t116 t\t117 u\t118 v\t119 w\t120 x\t121 y\t122 z\t123 {\t124 |\t125 }\t126 ~\t" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Read and write file\n", + "def writeToFile(filename, text):\n", + " # Open the file in write mode:\n", + " with open(filename, 'w') as fileObj:\n", + " # Write the text to the file:\n", + " fileObj.write(text)\n", + "\n", + "def appendToFile(filename, text):\n", + " # Open the file in append mode:\n", + " with open(filename, 'a') as fileObj:\n", + " # Write the text to the end of the file:\n", + " fileObj.write(text)\n", + "\n", + "def readFromFile(filename):\n", + " # Open the file in read mode:\n", + " with open(filename) as fileObj:\n", + " # Read all of the text in the file and return it as a string:\n", + " return fileObj.read()\n", + "\n", + "writeToFile('greet.txt', 'Hello!\\n')\n", + "appendToFile('greet.txt', 'Goodbye!\\n')\n", + "assert readFromFile('greet.txt') == 'Hello!\\nGoodbye!\\n'\n", + "\n", + "\n" + ], + "metadata": { + "id": "ShGDTTHaikn9" + }, + "execution_count": 1, "outputs": [] }, { "cell_type": "code", - "source": [], + "source": [ + "# Chessboard color\n", + "def getChessSquareColor(column, row):\n", + " # If the column and row is out of bounds, return a blank string:\n", + " if column < 1 or column > 8 or row < 1 or row > 8:\n", + " return ''\n", + "\n", + " # If the even/oddness of the column and row match, return 'white':\n", + " if column % 2 == row % 2:\n", + " return 'white'\n", + " # If they don't match, then return 'black':\n", + " else:\n", + " return 'black'\n", + "\n", + "assert getChessSquareColor(1, 1) == 'white'\n", + "assert getChessSquareColor(2, 1) == 'black'\n", + "assert getChessSquareColor(1, 2) == 'black'\n", + "assert getChessSquareColor(8, 8) == 'white'\n", + "assert getChessSquareColor(0, 8) == ''\n", + "assert getChessSquareColor(2, 9) == ''\n" + ], + "metadata": { + "id": "80QLgfawk7Ol" + }, + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Find and replace\n", + "def findAndReplace(text, oldText, newText):\n", + " replacedText = ''\n", + " i = 0\n", + " while i < len(text):\n", + " # If index i in text is the start of the oldText pattern, add\n", + " # the replacement text:\n", + " if text[i:i + len(oldText)] == oldText:\n", + " # Add the replacement text:\n", + " replacedText += newText\n", + " # Increment i by the length of oldText:\n", + " i += len(oldText)\n", + " # Otherwise, add the characters at text[i] and increment i by 1:\n", + " else:\n", + " replacedText += text[i]\n", + " i += 1\n", + " return replacedText\n", + "\n", + "assert findAndReplace('The fox', 'fox', 'dog') == 'The dog'\n", + "assert findAndReplace('fox', 'fox', 'dog') == 'dog'\n", + "assert findAndReplace('Firefox', 'fox', 'dog') == 'Firedog'\n", + "assert findAndReplace('foxfox', 'fox', 'dog') == 'dogdog'\n", + "assert findAndReplace('The Fox and fox.', 'fox', 'dog') == 'The Fox and dog.'" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "2QLoxCrYlVav", + "outputId": "2b8a0af9-ab70-44dc-d47a-3369aa581861" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'white'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 4 + } + ] + }, + { + "cell_type": "code", + "source": [ + "## Time \n", + "def getHoursMinutesSeconds(totalSeconds):\n", + " # If totalSeconds is 0, just return '0s':\n", + " if totalSeconds == 0:\n", + " return '0s'\n", + "\n", + " # Set hours to 0, then add an hour for every 3600 seconds removed from\n", + " # totalSeconds until totalSeconds is less than 3600:\n", + " hours = 0\n", + " while totalSeconds >= 3600:\n", + " hours += 1\n", + " totalSeconds -= 3600\n", + "\n", + " # Set minutes to 0, then add a minute for every 60 seconds removed from\n", + " # totalSeconds until totalSeconds is less than 60:\n", + " minutes = 0\n", + " while totalSeconds >= 60:\n", + " minutes += 1\n", + " totalSeconds -= 60\n", + "\n", + " # Set seconds to the remaining totalSeconds value:\n", + " seconds = totalSeconds\n", + "\n", + " # Create an hms list that contains the string hour/minute/second amounts:\n", + " hms = []\n", + " # If there are one or more hours, add the amount with an 'h' suffix:\n", + " if hours > 0:\n", + " hms.append(str(hours) + 'h')\n", + " # If there are one or more minutes, add the amount with an 'm' suffix:\n", + " if minutes > 0:\n", + " hms.append(str(minutes) + 'm')\n", + " # If there are one or more seconds, add the amount with an 's' suffix:\n", + " if seconds > 0:\n", + " hms.append(str(seconds) + 's')\n", + "\n", + " # Join the hour/minute/second strings with a space in between them:\n", + " return ' '.join(hms)\n", + "\n", + "\n", + "assert getHoursMinutesSeconds(30) == '30s'\n", + "assert getHoursMinutesSeconds(60) == '1m'\n", + "assert getHoursMinutesSeconds(90) == '1m 30s'\n", + "assert getHoursMinutesSeconds(3600) == '1h'\n", + "assert getHoursMinutesSeconds(3601) == '1h 1s'\n", + "assert getHoursMinutesSeconds(3661) == '1h 1m 1s'\n", + "assert getHoursMinutesSeconds(90042) == '25h 42s'\n", + "assert getHoursMinutesSeconds(0) == '0s'" + ], + "metadata": { + "id": "LdCg-oMtlmHZ" + }, + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "## Get smallest\n", + "def getSmallest(numbers):\n", + " # If the numbers list is empty, return None:\n", + " if len(numbers) == 0:\n", + " return None\n", + "\n", + " # Create a variable that tracks the smallest value so far, and start\n", + " # it off a the first value in the list:\n", + " smallest = numbers[0]\n", + " # Loop over each number in the numbers list:\n", + " for number in numbers:\n", + " # If the number is smaller than the current smallest value, make\n", + " # it the new smallest value:\n", + " if number < smallest:\n", + " smallest = number\n", + " # Return the smallest value found:\n", + " return smallest\n", + "\n", + "assert getSmallest([1, 2, 3]) == 1\n", + "assert getSmallest([3, 2, 1]) == 1\n", + "assert getSmallest([28, 25, 42, 2, 28]) == 2\n", + "assert getSmallest([1]) == 1\n", + "assert getSmallest([]) == None" + ], + "metadata": { + "id": "h5K4noQ8ql8t" + }, + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Sum product\n", + "def calculateSum(numbers):\n", + " # Start the sum result at 0:\n", + " result = 0\n", + " # Loop over all the numbers in the numbers parameter, and add them\n", + " # to the running sum result:\n", + " for number in numbers:\n", + " result += number\n", + " # Return the final sum result:\n", + " return result\n", + "\n", + "def calculateProduct(numbers):\n", + " # Start the product result at 1:\n", + " result = 1\n", + " # Loop over all the numbers in the numbers parameter, and multiply\n", + " # them by the running product result:\n", + " for number in numbers:\n", + " result *= number\n", + " # Return the final product result:\n", + " return result\n", + "\n", + "\n", + "assert calculateSum([]) == 0\n", + "assert calculateSum([2, 4, 6, 8, 10]) == 30\n", + "assert calculateProduct([]) == 1\n", + "assert calculateProduct([2, 4, 6, 8, 10]) == 3840" + ], + "metadata": { + "id": "tbMpu7r8quyT" + }, + "execution_count": 8, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Average\n", + "def average(numbers):\n", + " # Special case: If the numbers list is empty, return None:\n", + " if len(numbers) == 0:\n", + " return None\n", + "\n", + " # Start the total at 0:\n", + " total = 0\n", + "\n", + " # Loop over each number in numbers:\n", + " for number in numbers:\n", + " # Add the number to the total:\n", + " total += number\n", + "\n", + " # Get the average by dividing the total by how many numbers there are:\n", + " return total / len(numbers)\n", + "\n", + "\n", + "assert average([1, 2, 3]) == 2\n", + "assert average([1, 2, 3, 1, 2, 3, 1, 2, 3]) == 2\n", + "assert average([12, 20, 37]) == 23\n", + "assert average([0, 0, 0, 0, 0]) == 0\n", + "import random\n", + "random.seed(42)\n", + "testData = [1, 2, 3, 1, 2, 3, 1, 2, 3]\n", + "for i in range(1000):\n", + " random.shuffle(testData)\n", + " assert average(testData) == 2" + ], + "metadata": { + "id": "V0lWw7Tgq7kF" + }, + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Median\n", + "\n", + "def median(numbers):\n", + " # Special case: If the numbers list is empty, return None:\n", + " if len(numbers) == 0:\n", + " return None\n", + "\n", + " # Sort the numbers list:\n", + " numbers.sort()\n", + "\n", + " # Get the index of the middle number:\n", + " middleIndex = len(numbers) // 2\n", + "\n", + " # If the numbers list has an even length, return the average of the\n", + " # middle two numbers:\n", + " if len(numbers) % 2 == 0:\n", + " return (numbers[middleIndex] + numbers[middleIndex - 1]) / 2\n", + " # If the numbers list has an odd length, return the middlemost number:\n", + " else:\n", + " return numbers[middleIndex]\n", + "\n", + "\n", + "assert median([]) == None\n", + "assert median([1, 2, 3]) == 2\n", + "assert median([3, 7, 10, 4, 1, 9, 6, 5, 2, 8]) == 5.5\n", + "assert median([3, 7, 10, 4, 1, 9, 6, 2, 8]) == 6\n", + "import random\n", + "testData = [3, 7, 10, 4, 1, 9, 6, 2, 8]\n", + "for i in range(1000):\n", + " random.shuffle(testData)\n", + " assert median(testData) == 6" + ], + "metadata": { + "id": "OXAIFyRBwJxS" + }, + "execution_count": 10, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# Mode\n", + "\n", + "def mode(numbers):\n", + " # Special case: If the numbers list is empty, return None:\n", + " if len(numbers) == 0:\n", + " return None\n", + "\n", + " # Dictionary with keys of numbers and values of how often they appear:\n", + " numberCount = {}\n", + "\n", + " # Track the most frequent number and how often it appears:\n", + " mostFreqNumber = None\n", + " mostFreqNumberCount = 0\n", + "\n", + " # Loop through all the numbers, counting how often they appear:\n", + " for number in numbers:\n", + " # If the number hasn't appeared before, set it's count to 0.\n", + " if number not in numberCount:\n", + " numberCount[number] = 0\n", + " # Increment the number's count:\n", + " numberCount[number] += 1\n", + " # If this is more frequent than the most frequent number, it\n", + " # becomes the new most frequent number:\n", + " if numberCount[number] > mostFreqNumberCount:\n", + " mostFreqNumber = number\n", + " mostFreqNumberCount = numberCount[number]\n", + " # The function returns the most frequent number:\n", + " return mostFreqNumber\n", + "\n", + "assert mode([]) == None\n", + "assert mode([1, 2, 3, 4, 4]) == 4\n", + "assert mode([1, 1, 2, 3, 4]) == 1\n", + "import random\n", + "random.seed(42)\n", + "testData = [1, 2, 3, 4, 4]\n", + "for i in range(1000):\n", + " random.shuffle(testData)\n", + " assert mode(testData) == 4" + ], "metadata": { - "id": "J4DYA3-vbeNO" + "id": "_CEpdmDEwRXJ" }, "execution_count": null, "outputs": []