├── .gitignore ├── Day01-15 ├── a.txt ├── abc.txt ├── b.txt ├── c.txt ├── day01.ipynb ├── day02.ipynb ├── day03.ipynb ├── day04.ipynb ├── day05.ipynb ├── day06.ipynb ├── day07.ipynb ├── day08.ipynb ├── day09.ipynb ├── day10.ipynb ├── day11.ipynb ├── day12.ipynb ├── day13.ipynb ├── day14.ipynb ├── day15.ipynb ├── test.PNG └── tets.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .idea 3 | *.pyc 4 | __pycache__ 5 | 6 | ### VisualStudioCode ### 7 | .vscode/* 8 | .vscode/** 9 | !.vscode/settings.json 10 | !.vscode/tasks.json 11 | !.vscode/launch.json 12 | !.vscode/extensions.json 13 | 14 | ### VisualStudioCode Patch ### 15 | # Ignore all local history of files 16 | .history 17 | 18 | ### JupyterNotebook Files ### 19 | .ipynb_checkpoints 20 | -------------------------------------------------------------------------------- /Day01-15/a.txt: -------------------------------------------------------------------------------- 1 | 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 -------------------------------------------------------------------------------- /Day01-15/abc.txt: -------------------------------------------------------------------------------- 1 | line1 line1 line1 2 | line2 line2 line2 3 | line3 line3 line3 4 | line4 line4 line4 5 | line5 line5 line5 6 | line6 line6 line6 7 | line7 line7 line7 8 | line8 line8 line8 9 | line9 line9 line9 -------------------------------------------------------------------------------- /Day01-15/b.txt: -------------------------------------------------------------------------------- 1 | 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 -------------------------------------------------------------------------------- /Day01-15/c.txt: -------------------------------------------------------------------------------- 1 | 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973 4987 4993 4999 5003 5009 5011 5021 5023 5039 5051 5059 5077 5081 5087 5099 5101 5107 5113 5119 5147 5153 5167 5171 5179 5189 5197 5209 5227 5231 5233 5237 5261 5273 5279 5281 5297 5303 5309 5323 5333 5347 5351 5381 5387 5393 5399 5407 5413 5417 5419 5431 5437 5441 5443 5449 5471 5477 5479 5483 5501 5503 5507 5519 5521 5527 5531 5557 5563 5569 5573 5581 5591 5623 5639 5641 5647 5651 5653 5657 5659 5669 5683 5689 5693 5701 5711 5717 5737 5741 5743 5749 5779 5783 5791 5801 5807 5813 5821 5827 5839 5843 5849 5851 5857 5861 5867 5869 5879 5881 5897 5903 5923 5927 5939 5953 5981 5987 6007 6011 6029 6037 6043 6047 6053 6067 6073 6079 6089 6091 6101 6113 6121 6131 6133 6143 6151 6163 6173 6197 6199 6203 6211 6217 6221 6229 6247 6257 6263 6269 6271 6277 6287 6299 6301 6311 6317 6323 6329 6337 6343 6353 6359 6361 6367 6373 6379 6389 6397 6421 6427 6449 6451 6469 6473 6481 6491 6521 6529 6547 6551 6553 6563 6569 6571 6577 6581 6599 6607 6619 6637 6653 6659 6661 6673 6679 6689 6691 6701 6703 6709 6719 6733 6737 6761 6763 6779 6781 6791 6793 6803 6823 6827 6829 6833 6841 6857 6863 6869 6871 6883 6899 6907 6911 6917 6947 6949 6959 6961 6967 6971 6977 6983 6991 6997 7001 7013 7019 7027 7039 7043 7057 7069 7079 7103 7109 7121 7127 7129 7151 7159 7177 7187 7193 7207 7211 7213 7219 7229 7237 7243 7247 7253 7283 7297 7307 7309 7321 7331 7333 7349 7351 7369 7393 7411 7417 7433 7451 7457 7459 7477 7481 7487 7489 7499 7507 7517 7523 7529 7537 7541 7547 7549 7559 7561 7573 7577 7583 7589 7591 7603 7607 7621 7639 7643 7649 7669 7673 7681 7687 7691 7699 7703 7717 7723 7727 7741 7753 7757 7759 7789 7793 7817 7823 7829 7841 7853 7867 7873 7877 7879 7883 7901 7907 7919 7927 7933 7937 7949 7951 7963 7993 8009 8011 8017 8039 8053 8059 8069 8081 8087 8089 8093 8101 8111 8117 8123 8147 8161 8167 8171 8179 8191 8209 8219 8221 8231 8233 8237 8243 8263 8269 8273 8287 8291 8293 8297 8311 8317 8329 8353 8363 8369 8377 8387 8389 8419 8423 8429 8431 8443 8447 8461 8467 8501 8513 8521 8527 8537 8539 8543 8563 8573 8581 8597 8599 8609 8623 8627 8629 8641 8647 8663 8669 8677 8681 8689 8693 8699 8707 8713 8719 8731 8737 8741 8747 8753 8761 8779 8783 8803 8807 8819 8821 8831 8837 8839 8849 8861 8863 8867 8887 8893 8923 8929 8933 8941 8951 8963 8969 8971 8999 9001 9007 9011 9013 9029 9041 9043 9049 9059 9067 9091 9103 9109 9127 9133 9137 9151 9157 9161 9173 9181 9187 9199 9203 9209 9221 9227 9239 9241 9257 9277 9281 9283 9293 9311 9319 9323 9337 9341 9343 9349 9371 9377 9391 9397 9403 9413 9419 9421 9431 9433 9437 9439 9461 9463 9467 9473 9479 9491 9497 9511 9521 9533 9539 9547 9551 9587 9601 9613 9619 9623 9629 9631 9643 9649 9661 9677 9679 9689 9697 9719 9721 9733 9739 9743 9749 9767 9769 9781 9787 9791 9803 9811 9817 9829 9833 9839 9851 9857 9859 9871 9883 9887 9901 9907 9923 9929 9931 9941 9949 9967 9973 -------------------------------------------------------------------------------- /Day01-15/day01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "The Zen of Python, by Tim Peters\n", 13 | "\n", 14 | "Beautiful is better than ugly.\n", 15 | "Explicit is better than implicit.\n", 16 | "Simple is better than complex.\n", 17 | "Complex is better than complicated.\n", 18 | "Flat is better than nested.\n", 19 | "Sparse is better than dense.\n", 20 | "Readability counts.\n", 21 | "Special cases aren't special enough to break the rules.\n", 22 | "Although practicality beats purity.\n", 23 | "Errors should never pass silently.\n", 24 | "Unless explicitly silenced.\n", 25 | "In the face of ambiguity, refuse the temptation to guess.\n", 26 | "There should be one-- and preferably only one --obvious way to do it.\n", 27 | "Although that way may not be obvious at first unless you're Dutch.\n", 28 | "Now is better than never.\n", 29 | "Although never is often better than *right* now.\n", 30 | "If the implementation is hard to explain, it's a bad idea.\n", 31 | "If the implementation is easy to explain, it may be a good idea.\n", 32 | "Namespaces are one honking great idea -- let's do more of those!\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "import this" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "# Try the given example 1\n", 47 | "import turtle\n", 48 | "\n", 49 | "turtle.pensize(4)\n", 50 | "turtle.pencolor('red')\n", 51 | "\n", 52 | "turtle.forward(100)\n", 53 | "turtle.right(90)\n", 54 | "turtle.forward(100)\n", 55 | "turtle.right(90)\n", 56 | "turtle.forward(100)\n", 57 | "turtle.right(90)\n", 58 | "turtle.forward(100)\n", 59 | "\n", 60 | "turtle.mainloop()" 61 | ] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 3", 67 | "language": "python", 68 | "name": "python3" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 3 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython3", 80 | "version": "3.7.6" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 2 85 | } 86 | -------------------------------------------------------------------------------- /Day01-15/day02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "fahrenheit:72\n", 13 | "72.0 fahrenheit = 22.2 celsius\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "# Exercise 1\n", 19 | "f = float(input('fahrenheit:'))\n", 20 | "c = (f - 32) / 1.8\n", 21 | "print('%.1f fahrenheit = %.1f celsius' % (f, c))" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "year is 2004\n", 34 | "True\n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "# Exercise 2\n", 40 | "y = int(input('year is '))\n", 41 | "leap_year = y%4 == 0 and y%100 != 0 or y%400 == 0\n", 42 | "print(leap_year)" 43 | ] 44 | } 45 | ], 46 | "metadata": { 47 | "kernelspec": { 48 | "display_name": "Python 3", 49 | "language": "python", 50 | "name": "python3" 51 | }, 52 | "language_info": { 53 | "codemirror_mode": { 54 | "name": "ipython", 55 | "version": 3 56 | }, 57 | "file_extension": ".py", 58 | "mimetype": "text/x-python", 59 | "name": "python", 60 | "nbconvert_exporter": "python", 61 | "pygments_lexer": "ipython3", 62 | "version": "3.7.6" 63 | } 64 | }, 65 | "nbformat": 4, 66 | "nbformat_minor": 2 67 | } 68 | -------------------------------------------------------------------------------- /Day01-15/day03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "length = 23\n", 13 | "unit is cm\n", 14 | "23.000000 cm = 9.055118 inch\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "# Exercise 1\n", 20 | "value = float(input('length = '))\n", 21 | "unit = str(input('unit is '))\n", 22 | "\n", 23 | "if unit == 'inch':\n", 24 | " print('%f inch = %f cm' % (value, value * 2.54))\n", 25 | "elif unit == 'cm':\n", 26 | " print('%f cm = %f inch' % (value, value / 2.54))\n", 27 | "else:\n", 28 | " print('Please enter another unit')" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "the score is 96\n", 41 | "The grade is A\n" 42 | ] 43 | } 44 | ], 45 | "source": [ 46 | "# Exercise 2\n", 47 | "score = float(input('the score is '))\n", 48 | "if score >= 90:\n", 49 | " grade = 'A'\n", 50 | "elif score >= 80:\n", 51 | " grade = 'B'\n", 52 | "elif score >= 70:\n", 53 | " grade = 'C'\n", 54 | "elif score >= 60:\n", 55 | " grade = 'D'\n", 56 | "else:\n", 57 | " grade = 'E'\n", 58 | " \n", 59 | "print('The grade is ', grade)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "a = 5\n", 72 | "b = 6\n", 73 | "c = 7\n", 74 | "perimeter: 18.000000\n", 75 | "are: 14.696938\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "# Exercise 3\n", 81 | "a = float(input('a = '))\n", 82 | "b = float(input('b = '))\n", 83 | "c = float(input('c = '))\n", 84 | "\n", 85 | "if a + b > c and a + c > b and b + c > a:\n", 86 | " print('perimeter: %f' % (a + b + c))\n", 87 | " p = (a + b + c) / 2\n", 88 | " area = (p * (p - a) * (p - b) * (p - c)) ** 0.5\n", 89 | " print('are: %f' % (area))\n", 90 | "else:\n", 91 | " print('not a triangle')" 92 | ] 93 | } 94 | ], 95 | "metadata": { 96 | "kernelspec": { 97 | "display_name": "Python 3", 98 | "language": "python", 99 | "name": "python3" 100 | }, 101 | "language_info": { 102 | "codemirror_mode": { 103 | "name": "ipython", 104 | "version": 3 105 | }, 106 | "file_extension": ".py", 107 | "mimetype": "text/x-python", 108 | "name": "python", 109 | "nbconvert_exporter": "python", 110 | "pygments_lexer": "ipython3", 111 | "version": "3.7.6" 112 | } 113 | }, 114 | "nbformat": 4, 115 | "nbformat_minor": 2 116 | } 117 | -------------------------------------------------------------------------------- /Day01-15/day04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "enter a positive integer:23\n", 13 | "23 is a prime number.\n" 14 | ] 15 | } 16 | ], 17 | "source": [ 18 | "# Exercise 1\n", 19 | "from math import sqrt\n", 20 | "x = int(input('enter a positive integer:'))\n", 21 | "x_sqrt = int(sqrt(x))\n", 22 | "prime = True\n", 23 | "for num in range(2, x_sqrt + 1):\n", 24 | " if x % num == 0:\n", 25 | " prime = False\n", 26 | " break\n", 27 | "if prime == True and x != 1:\n", 28 | " print('%d is a prime number.' % x)\n", 29 | "else:\n", 30 | " print('%d is not a prime number.' % x)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "name": "stdout", 40 | "output_type": "stream", 41 | "text": [ 42 | "enter a integer x = 9\n", 43 | "enter a integer y = 7\n", 44 | "The greatest common factor of 9 and 7 is 1.\n", 45 | "The least common multiple of 9 and 7 is 63.\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "# Exercise 2\n", 51 | "x = int(input('enter a integer x = '))\n", 52 | "y = int(input('enter a integer y = '))\n", 53 | "\n", 54 | "for gcf in range (min(x, y), 0, -1):\n", 55 | " if x % gcf == 0 and y % gcf == 0:\n", 56 | " print('The greatest common factor of %d and %d is %d.' % (x, y, gcf))\n", 57 | " print('The least common multiple of %d and %d is %d.' % (x, y, x * y / gcf))" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "*\n", 70 | "**\n", 71 | "***\n", 72 | "****\n", 73 | "*****\n", 74 | " *\n", 75 | " **\n", 76 | " ***\n", 77 | " ****\n", 78 | "*****\n", 79 | " *\n", 80 | " ***\n", 81 | " *****\n", 82 | " *******\n", 83 | "*********\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "# Exercise 3\n", 89 | "for i in range(5):\n", 90 | " for j in range(i + 1):\n", 91 | " print('*', end='')\n", 92 | " print()\n", 93 | "\n", 94 | "for i in range(5):\n", 95 | " for j in range(5):\n", 96 | " if j < 5 - 1 - i:\n", 97 | " print(' ', end='')\n", 98 | " else:\n", 99 | " print('*', end='')\n", 100 | " print()\n", 101 | " \n", 102 | "for i in range(5):\n", 103 | " for j in range(5 - 1 - i):\n", 104 | " print(' ', end='')\n", 105 | " for k in range(2 * i + 1):\n", 106 | " print('*', end='')\n", 107 | " print()" 108 | ] 109 | } 110 | ], 111 | "metadata": { 112 | "kernelspec": { 113 | "display_name": "Python 3", 114 | "language": "python", 115 | "name": "python3" 116 | }, 117 | "language_info": { 118 | "codemirror_mode": { 119 | "name": "ipython", 120 | "version": 3 121 | }, 122 | "file_extension": ".py", 123 | "mimetype": "text/x-python", 124 | "name": "python", 125 | "nbconvert_exporter": "python", 126 | "pygments_lexer": "ipython3", 127 | "version": "3.7.6" 128 | } 129 | }, 130 | "nbformat": 4, 131 | "nbformat_minor": 2 132 | } 133 | -------------------------------------------------------------------------------- /Day01-15/day05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Exercise 1\n", 10 | "prev_two = 0\n", 11 | "prev_one = 1\n", 12 | "\n", 13 | "print('Number 1 in the Fibonacci sequence is %d.' % prev_one)\n", 14 | "\n", 15 | "for i in range(19):\n", 16 | " current = prev_two + prev_one\n", 17 | " prev_two = prev_one\n", 18 | " prev_one = current\n", 19 | " print('Number %d in the Fibonacci sequence is %d.' % (i + 2, current))" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "# Exercise 1-2\n", 29 | "\n", 30 | "a = 0\n", 31 | "b = 1\n", 32 | "for _ in range(20):\n", 33 | " a, b = b, a + b\n", 34 | " print(a, end=' ')" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 3, 40 | "metadata": {}, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "6 is the perfect number.\n", 47 | "28 is the perfect number.\n", 48 | "496 is the perfect number.\n", 49 | "8128 is the perfect number.\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "# Exercise 2\n", 55 | "for num in range(1, 10001):\n", 56 | " sum = 0\n", 57 | " for i in range(1, num):\n", 58 | " if num % i == 0:\n", 59 | " sum += i\n", 60 | " if sum == num:\n", 61 | " print('%d is the perfect number.' % num)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "2 is a prime number.\n", 74 | "3 is a prime number.\n", 75 | "5 is a prime number.\n", 76 | "7 is a prime number.\n", 77 | "11 is a prime number.\n", 78 | "13 is a prime number.\n", 79 | "17 is a prime number.\n", 80 | "19 is a prime number.\n", 81 | "23 is a prime number.\n", 82 | "29 is a prime number.\n", 83 | "31 is a prime number.\n", 84 | "37 is a prime number.\n", 85 | "41 is a prime number.\n", 86 | "43 is a prime number.\n", 87 | "47 is a prime number.\n", 88 | "53 is a prime number.\n", 89 | "59 is a prime number.\n", 90 | "61 is a prime number.\n", 91 | "67 is a prime number.\n", 92 | "71 is a prime number.\n", 93 | "73 is a prime number.\n", 94 | "79 is a prime number.\n", 95 | "83 is a prime number.\n", 96 | "89 is a prime number.\n", 97 | "97 is a prime number.\n" 98 | ] 99 | } 100 | ], 101 | "source": [ 102 | "# Exercise 3\n", 103 | "from math import sqrt\n", 104 | "\n", 105 | "for num in range(2, 101):\n", 106 | " prime = True\n", 107 | " for i in range(2, int(sqrt(num)) + 1):\n", 108 | " if num % i == 0:\n", 109 | " prime = False\n", 110 | " break\n", 111 | " if prime == True:\n", 112 | " print('%d is a prime number.' % num)" 113 | ] 114 | } 115 | ], 116 | "metadata": { 117 | "kernelspec": { 118 | "display_name": "Python 3", 119 | "language": "python", 120 | "name": "python3" 121 | }, 122 | "language_info": { 123 | "codemirror_mode": { 124 | "name": "ipython", 125 | "version": 3 126 | }, 127 | "file_extension": ".py", 128 | "mimetype": "text/x-python", 129 | "name": "python", 130 | "nbconvert_exporter": "python", 131 | "pygments_lexer": "ipython3", 132 | "version": "3.7.6" 133 | } 134 | }, 135 | "nbformat": 4, 136 | "nbformat_minor": 2 137 | } 138 | -------------------------------------------------------------------------------- /Day01-15/day06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "(3, 18)" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "# Exercise 1\n", 21 | "def gcf_lcm(x, y):\n", 22 | " for gcf in range (min(x, y), 0, -1):\n", 23 | " if x % gcf == 0 and y % gcf == 0:\n", 24 | " return(int(gcf), int(x * y / gcf))\n", 25 | " \n", 26 | "gcf_lcm(6, 9)" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "metadata": {}, 33 | "outputs": [ 34 | { 35 | "data": { 36 | "text/plain": [ 37 | "True" 38 | ] 39 | }, 40 | "execution_count": 2, 41 | "metadata": {}, 42 | "output_type": "execute_result" 43 | } 44 | ], 45 | "source": [ 46 | "# Exercise 2\n", 47 | "def is_Palindromic(x):\n", 48 | " temp = x\n", 49 | " transform = 0\n", 50 | " while temp > 0:\n", 51 | " transform = transform * 10 + temp % 10\n", 52 | " temp //= 10\n", 53 | " return transform == x\n", 54 | "\n", 55 | "is_Palindromic(12321)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "True" 67 | ] 68 | }, 69 | "execution_count": 3, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "# Exercise 3\n", 76 | "from math import sqrt\n", 77 | "\n", 78 | "def is_Prime(x):\n", 79 | " x_sqrt = int(sqrt(x))\n", 80 | " for num in range(2, x_sqrt + 1):\n", 81 | " if x % num == 0:\n", 82 | " return False\n", 83 | " return True if num != 1 else False\n", 84 | "\n", 85 | "is_Prime(23)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 4, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "enter a positive integer: 131\n", 98 | "131 is palindromic and prime.\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "# Exercise 4\n", 104 | "if __name__ == '__main__':\n", 105 | " x = int(input('enter a positive integer: '))\n", 106 | " if is_Palindromic(x) and is_Prime(x):\n", 107 | " print('%d is palindromic and prime.' % x)\n", 108 | " else:\n", 109 | " print('%d is not palindromic and prime.' % x)" 110 | ] 111 | } 112 | ], 113 | "metadata": { 114 | "kernelspec": { 115 | "display_name": "Python 3", 116 | "language": "python", 117 | "name": "python3" 118 | }, 119 | "language_info": { 120 | "codemirror_mode": { 121 | "name": "ipython", 122 | "version": 3 123 | }, 124 | "file_extension": ".py", 125 | "mimetype": "text/x-python", 126 | "name": "python", 127 | "nbconvert_exporter": "python", 128 | "pygments_lexer": "ipython3", 129 | "version": "3.7.6" 130 | } 131 | }, 132 | "nbformat": 4, 133 | "nbformat_minor": 2 134 | } 135 | -------------------------------------------------------------------------------- /Day01-15/day07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "abcdefghijklmnopqrstuvwxyz\n", 13 | "bcdefghijklmnopqrstuvwxyza\n", 14 | "cdefghijklmnopqrstuvwxyzab\n", 15 | "defghijklmnopqrstuvwxyzabc\n", 16 | "efghijklmnopqrstuvwxyzabcd\n" 17 | ] 18 | }, 19 | { 20 | "ename": "KeyboardInterrupt", 21 | "evalue": "", 22 | "output_type": "error", 23 | "traceback": [ 24 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 25 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 26 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 27 | "\u001b[1;32m\u001b[0m in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'cls'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# os.system('clear')\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcontent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 11\u001b[0m \u001b[0mcontent\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcontent\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mcontent\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", 28 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: " 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "# Exercise 1\n", 34 | "import os\n", 35 | "import time\n", 36 | "\n", 37 | "def main():\n", 38 | " content = 'abcdefghijklmnopqrstuvwxyz'\n", 39 | " while True:\n", 40 | " os.system('cls') # os.system('clear')\n", 41 | " print(content)\n", 42 | " time.sleep(1)\n", 43 | " content = content[1:] + content[0]\n", 44 | "\n", 45 | "if __name__ == '__main__':\n", 46 | " main()" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 2, 52 | "metadata": {}, 53 | "outputs": [ 54 | { 55 | "name": "stdout", 56 | "output_type": "stream", 57 | "text": [ 58 | "wbepXjrnQ3\n" 59 | ] 60 | } 61 | ], 62 | "source": [ 63 | "# Exercise 2\n", 64 | "import random\n", 65 | "\n", 66 | "def generate_code(code_len=4):\n", 67 | " characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'\n", 68 | " length = len(characters) - 1\n", 69 | " code = ''\n", 70 | " for i in range(code_len):\n", 71 | " index = random.randint(0, length)\n", 72 | " code += characters[index]\n", 73 | " return code\n", 74 | "\n", 75 | "if __name__ == '__main__':\n", 76 | " print(generate_code(code_len=10))" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 3, 82 | "metadata": {}, 83 | "outputs": [ 84 | { 85 | "name": "stdout", 86 | "output_type": "stream", 87 | "text": [ 88 | "txt\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "# Exercise 3\n", 94 | "def get_suffix(filename, has_dot=False):\n", 95 | " pos = filename.rfind('.')\n", 96 | " if 0 < pos < len(filename) - 1:\n", 97 | " index = pos if has_dot else pos + 1\n", 98 | " return filename[index:]\n", 99 | " else:\n", 100 | " return ''\n", 101 | " \n", 102 | "if __name__ == '__main__':\n", 103 | " print(get_suffix('abc.txt', has_dot=False))" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "(23, 12)\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "# Exercise 4\n", 121 | "def mm(a_list):\n", 122 | " max1, max2 = (a_list[0], a_list[1]) if a_list[0] > a_list[1] else (a_list[1], a_list[0])\n", 123 | " for i in range(2, len(a_list)):\n", 124 | " if a_list[i] > max1:\n", 125 | " max2 = max1\n", 126 | " max1 = a_list[i]\n", 127 | " elif a_list[i] > max2:\n", 128 | " max2 = a_list[i]\n", 129 | " return max1, max2\n", 130 | "\n", 131 | "if __name__ == '__main__':\n", 132 | " print(mm([1, 3, 5, 12, 23]))" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 5, 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "333\n", 145 | "365\n", 146 | "1\n", 147 | "61\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "# Exercise 5\n", 153 | "def is_leap_year(year):\n", 154 | " return year % 4 == 0 and year % 100 != 0 or year % 400 == 0\n", 155 | "\n", 156 | "def which_day(year, month, date):\n", 157 | " days_of_month = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],\n", 158 | " [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]][is_leap_year(year)]\n", 159 | " total = 0\n", 160 | " for index in range(month - 1):\n", 161 | " total += days_of_month[index]\n", 162 | " return total + date\n", 163 | "\n", 164 | "def main():\n", 165 | " print(which_day(1980, 11, 28))\n", 166 | " print(which_day(1981, 12, 31))\n", 167 | " print(which_day(2018, 1, 1))\n", 168 | " print(which_day(2016, 3, 1))\n", 169 | "\n", 170 | "\n", 171 | "if __name__ == '__main__':\n", 172 | " main()" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 6, 178 | "metadata": {}, 179 | "outputs": [ 180 | { 181 | "name": "stdout", 182 | "output_type": "stream", 183 | "text": [ 184 | "number of rows is 5\n", 185 | "1\t\n", 186 | "1\t1\t\n", 187 | "1\t2\t1\t\n", 188 | "1\t3\t3\t1\t\n", 189 | "1\t4\t6\t4\t1\t\n" 190 | ] 191 | } 192 | ], 193 | "source": [ 194 | "# Exercise 6\n", 195 | "def main():\n", 196 | " nrows = int(input('number of rows is '))\n", 197 | " yh_tri = [[]] * nrows\n", 198 | " for row in range(nrows):\n", 199 | " yh_tri[row] = [1] * (row + 1)\n", 200 | " for col in range(len(yh_tri[row])):\n", 201 | " if col == 0 or col == row:\n", 202 | " yh_tri[row][col] = 1\n", 203 | " else:\n", 204 | " yh_tri[row][col] = yh_tri[row - 1][col - 1] + yh_tri[row-1][col]\n", 205 | " print(yh_tri[row][col], end='\\t')\n", 206 | " print()\n", 207 | "\n", 208 | "if __name__ == '__main__':\n", 209 | " main()" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 7, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "name": "stdout", 219 | "output_type": "stream", 220 | "text": [ 221 | "机选几注: 5\n", 222 | "03 08 11 19 25 31 | 05 \n", 223 | "06 10 16 18 28 31 | 05 \n", 224 | "03 05 06 07 08 10 | 03 \n", 225 | "01 05 07 18 22 26 | 16 \n", 226 | "04 10 15 20 23 32 | 16 \n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "# Try the given example 1\n", 232 | "from random import randrange, randint, sample\n", 233 | "\n", 234 | "\n", 235 | "def display(balls):\n", 236 | " \"\"\"\n", 237 | " 输出列表中的双色球号码\n", 238 | " \"\"\"\n", 239 | " for index, ball in enumerate(balls):\n", 240 | " if index == len(balls) - 1:\n", 241 | " print('|', end=' ')\n", 242 | " print('%02d' % ball, end=' ')\n", 243 | " print()\n", 244 | "\n", 245 | "\n", 246 | "def random_select():\n", 247 | " \"\"\"\n", 248 | " 随机选择一组号码\n", 249 | " \"\"\"\n", 250 | " red_balls = [x for x in range(1, 34)]\n", 251 | " selected_balls = []\n", 252 | " selected_balls = sample(red_balls, 6)\n", 253 | " selected_balls.sort()\n", 254 | " selected_balls.append(randint(1, 16))\n", 255 | " return selected_balls\n", 256 | "\n", 257 | "\n", 258 | "def main():\n", 259 | " n = int(input('机选几注: '))\n", 260 | " for _ in range(n):\n", 261 | " display(random_select())\n", 262 | "\n", 263 | "\n", 264 | "if __name__ == '__main__':\n", 265 | " main()" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 8, 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "基基基基非非非非非基基非基基基非基非非基基非非非基非非基基非" 278 | ] 279 | } 280 | ], 281 | "source": [ 282 | "# Try the given example 2\n", 283 | "# Josephus problem\n", 284 | "def main():\n", 285 | " persons = [True] * 30\n", 286 | " counter, index, number = 0, 0, 0\n", 287 | " while counter < 15:\n", 288 | " if persons[index]:\n", 289 | " number += 1\n", 290 | " if number == 9:\n", 291 | " persons[index] = False\n", 292 | " counter += 1\n", 293 | " number = 0\n", 294 | " index += 1\n", 295 | " index %= 30\n", 296 | " for person in persons:\n", 297 | " print('基' if person else '非', end='')\n", 298 | "\n", 299 | "\n", 300 | "if __name__ == '__main__':\n", 301 | " main()" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [ 309 | { 310 | "name": "stdout", 311 | "output_type": "stream", 312 | "text": [ 313 | " | | \n", 314 | "-+-+-\n", 315 | " | | \n", 316 | "-+-+-\n", 317 | " | | \n", 318 | "轮到x走棋, 请输入位置: BR\n", 319 | " | | \n", 320 | "-+-+-\n", 321 | " | | \n", 322 | "-+-+-\n", 323 | " | |x\n", 324 | "轮到o走棋, 请输入位置: MM\n", 325 | " | | \n", 326 | "-+-+-\n", 327 | " |o| \n", 328 | "-+-+-\n", 329 | " | |x\n" 330 | ] 331 | } 332 | ], 333 | "source": [ 334 | "# Try the given example 3\n", 335 | "import os\n", 336 | "\n", 337 | "\n", 338 | "def print_board(board):\n", 339 | " print(board['TL'] + '|' + board['TM'] + '|' + board['TR'])\n", 340 | " print('-+-+-')\n", 341 | " print(board['ML'] + '|' + board['MM'] + '|' + board['MR'])\n", 342 | " print('-+-+-')\n", 343 | " print(board['BL'] + '|' + board['BM'] + '|' + board['BR'])\n", 344 | "\n", 345 | "\n", 346 | "def main():\n", 347 | " init_board = {\n", 348 | " 'TL': ' ', 'TM': ' ', 'TR': ' ',\n", 349 | " 'ML': ' ', 'MM': ' ', 'MR': ' ',\n", 350 | " 'BL': ' ', 'BM': ' ', 'BR': ' '\n", 351 | " }\n", 352 | " begin = True\n", 353 | " while begin:\n", 354 | " curr_board = init_board.copy()\n", 355 | " begin = False\n", 356 | " turn = 'x'\n", 357 | " counter = 0\n", 358 | " os.system('clear')\n", 359 | " print_board(curr_board)\n", 360 | " while counter < 9:\n", 361 | " move = input('轮到%s走棋, 请输入位置: ' % turn)\n", 362 | " if curr_board[move] == ' ':\n", 363 | " counter += 1\n", 364 | " curr_board[move] = turn\n", 365 | " if turn == 'x':\n", 366 | " turn = 'o'\n", 367 | " else:\n", 368 | " turn = 'x'\n", 369 | " os.system('clear')\n", 370 | " print_board(curr_board)\n", 371 | " choice = input('再玩一局?(yes|no)')\n", 372 | " begin = choice == 'yes'\n", 373 | "\n", 374 | "\n", 375 | "if __name__ == '__main__':\n", 376 | " main()" 377 | ] 378 | } 379 | ], 380 | "metadata": { 381 | "kernelspec": { 382 | "display_name": "Python 3", 383 | "language": "python", 384 | "name": "python3" 385 | }, 386 | "language_info": { 387 | "codemirror_mode": { 388 | "name": "ipython", 389 | "version": 3 390 | }, 391 | "file_extension": ".py", 392 | "mimetype": "text/x-python", 393 | "name": "python", 394 | "nbconvert_exporter": "python", 395 | "pygments_lexer": "ipython3", 396 | "version": "3.7.6" 397 | } 398 | }, 399 | "nbformat": 4, 400 | "nbformat_minor": 2 401 | } 402 | -------------------------------------------------------------------------------- /Day01-15/day08.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "15:30:53\n", 13 | "15:30:54\n", 14 | "15:30:55\n", 15 | "15:30:56\n", 16 | "15:30:57\n", 17 | "15:30:58\n", 18 | "15:30:59\n", 19 | "15:31:00\n", 20 | "15:31:01\n", 21 | "15:31:02\n", 22 | "15:31:03\n" 23 | ] 24 | }, 25 | { 26 | "ename": "KeyboardInterrupt", 27 | "evalue": "", 28 | "output_type": "error", 29 | "traceback": [ 30 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 31 | "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 32 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 30\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 31\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 33 | "\u001b[1;32m\u001b[0m in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m 25\u001b[0m \u001b[0mc1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mClock\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m15\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m30\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m52\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 27\u001b[1;33m \u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 28\u001b[0m \u001b[0mc1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 29\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mc1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 34 | "\u001b[1;31mKeyboardInterrupt\u001b[0m: " 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "# Exercise 1\n", 40 | "from time import sleep\n", 41 | "\n", 42 | "class Clock(object):\n", 43 | " def __init__(self, hour, minute, second):\n", 44 | " self.h = hour\n", 45 | " self.m = minute\n", 46 | " self.s = second\n", 47 | " \n", 48 | " def run(self):\n", 49 | " self.s += 1\n", 50 | " if self.s == 60:\n", 51 | " self.s = 0\n", 52 | " self.m += 1\n", 53 | " if self.m == 60:\n", 54 | " self.m = 0\n", 55 | " self.h += 1\n", 56 | " if self.h == 24:\n", 57 | " self.h = 0\n", 58 | " \n", 59 | " def __str__(self):\n", 60 | " return '%02d:%02d:%02d' % (self.h, self.m, self.s)\n", 61 | " \n", 62 | "def main():\n", 63 | " c1 = Clock(15, 30, 52)\n", 64 | " while True:\n", 65 | " sleep(1)\n", 66 | " c1.run()\n", 67 | " print(c1)\n", 68 | " \n", 69 | "if __name__ == '__main__':\n", 70 | " main()" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 2, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "(2, 7)\n", 83 | "(3, 9)\n", 84 | "3.1622776601683795\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "# Exercise 2\n", 90 | "from math import sqrt\n", 91 | "\n", 92 | "class Point(object):\n", 93 | " def __init__(self, x, y):\n", 94 | " self.x = x\n", 95 | " self.y = y\n", 96 | " \n", 97 | " def move(self, x, y):\n", 98 | " self.x = x\n", 99 | " self.y = y\n", 100 | " \n", 101 | " def distance(self, other):\n", 102 | " return sqrt((self.x - other.x) ** 2 + (self.y - other.y))\n", 103 | " \n", 104 | " def __str__(self):\n", 105 | " return '(%s, %s)' % (str(self.x), str(self.y))\n", 106 | " \n", 107 | "\n", 108 | "def main():\n", 109 | " p1 = Point(1, 3)\n", 110 | " p2 = Point(2, 7)\n", 111 | " print(p2)\n", 112 | " p2.move(3, 9)\n", 113 | " print(p2)\n", 114 | " print(p2.distance(p1))\n", 115 | "if __name__ == '__main__':\n", 116 | " main()" 117 | ] 118 | } 119 | ], 120 | "metadata": { 121 | "kernelspec": { 122 | "display_name": "Python 3", 123 | "language": "python", 124 | "name": "python3" 125 | }, 126 | "language_info": { 127 | "codemirror_mode": { 128 | "name": "ipython", 129 | "version": 3 130 | }, 131 | "file_extension": ".py", 132 | "mimetype": "text/x-python", 133 | "name": "python", 134 | "nbconvert_exporter": "python", 135 | "pygments_lexer": "ipython3", 136 | "version": "3.7.6" 137 | } 138 | }, 139 | "nbformat": 4, 140 | "nbformat_minor": 2 141 | } 142 | -------------------------------------------------------------------------------- /Day01-15/day09.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "========第01回合========\n", 13 | "骆昊使用普通攻击打了王大锤.\n", 14 | "骆昊的魔法值恢复了3点.\n", 15 | "王大锤回击了骆昊.\n", 16 | "~~~骆昊奥特曼~~~\n", 17 | "生命值: 983\n", 18 | "魔法值: 123\n", 19 | "\n", 20 | "~~~狄仁杰小怪兽~~~\n", 21 | "生命值: 250\n", 22 | "~~~白元芳小怪兽~~~\n", 23 | "生命值: 500\n", 24 | "~~~王大锤小怪兽~~~\n", 25 | "生命值: 729\n", 26 | "========第02回合========\n", 27 | "骆昊使用了魔法攻击.\n", 28 | "狄仁杰回击了骆昊.\n", 29 | "~~~骆昊奥特曼~~~\n", 30 | "生命值: 969\n", 31 | "魔法值: 103\n", 32 | "\n", 33 | "~~~狄仁杰小怪兽~~~\n", 34 | "生命值: 238\n", 35 | "~~~白元芳小怪兽~~~\n", 36 | "生命值: 489\n", 37 | "~~~王大锤小怪兽~~~\n", 38 | "生命值: 719\n", 39 | "========第03回合========\n", 40 | "骆昊使用普通攻击打了白元芳.\n", 41 | "骆昊的魔法值恢复了3点.\n", 42 | "白元芳回击了骆昊.\n", 43 | "~~~骆昊奥特曼~~~\n", 44 | "生命值: 955\n", 45 | "魔法值: 106\n", 46 | "\n", 47 | "~~~狄仁杰小怪兽~~~\n", 48 | "生命值: 238\n", 49 | "~~~白元芳小怪兽~~~\n", 50 | "生命值: 466\n", 51 | "~~~王大锤小怪兽~~~\n", 52 | "生命值: 719\n", 53 | "========第04回合========\n", 54 | "骆昊使用普通攻击打了白元芳.\n", 55 | "骆昊的魔法值恢复了8点.\n", 56 | "白元芳回击了骆昊.\n", 57 | "~~~骆昊奥特曼~~~\n", 58 | "生命值: 935\n", 59 | "魔法值: 114\n", 60 | "\n", 61 | "~~~狄仁杰小怪兽~~~\n", 62 | "生命值: 238\n", 63 | "~~~白元芳小怪兽~~~\n", 64 | "生命值: 450\n", 65 | "~~~王大锤小怪兽~~~\n", 66 | "生命值: 719\n", 67 | "========第05回合========\n", 68 | "骆昊使用普通攻击打了王大锤.\n", 69 | "骆昊的魔法值恢复了3点.\n", 70 | "王大锤回击了骆昊.\n", 71 | "~~~骆昊奥特曼~~~\n", 72 | "生命值: 917\n", 73 | "魔法值: 117\n", 74 | "\n", 75 | "~~~狄仁杰小怪兽~~~\n", 76 | "生命值: 238\n", 77 | "~~~白元芳小怪兽~~~\n", 78 | "生命值: 450\n", 79 | "~~~王大锤小怪兽~~~\n", 80 | "生命值: 703\n", 81 | "========第06回合========\n", 82 | "骆昊使用普通攻击打了狄仁杰.\n", 83 | "骆昊的魔法值恢复了1点.\n", 84 | "狄仁杰回击了骆昊.\n", 85 | "~~~骆昊奥特曼~~~\n", 86 | "生命值: 901\n", 87 | "魔法值: 118\n", 88 | "\n", 89 | "~~~狄仁杰小怪兽~~~\n", 90 | "生命值: 217\n", 91 | "~~~白元芳小怪兽~~~\n", 92 | "生命值: 450\n", 93 | "~~~王大锤小怪兽~~~\n", 94 | "生命值: 703\n", 95 | "========第07回合========\n", 96 | "骆昊使用普通攻击打了王大锤.\n", 97 | "骆昊的魔法值恢复了5点.\n", 98 | "王大锤回击了骆昊.\n", 99 | "~~~骆昊奥特曼~~~\n", 100 | "生命值: 881\n", 101 | "魔法值: 123\n", 102 | "\n", 103 | "~~~狄仁杰小怪兽~~~\n", 104 | "生命值: 217\n", 105 | "~~~白元芳小怪兽~~~\n", 106 | "生命值: 450\n", 107 | "~~~王大锤小怪兽~~~\n", 108 | "生命值: 681\n", 109 | "========第08回合========\n", 110 | "骆昊使用普通攻击打了狄仁杰.\n", 111 | "骆昊的魔法值恢复了7点.\n", 112 | "狄仁杰回击了骆昊.\n", 113 | "~~~骆昊奥特曼~~~\n", 114 | "生命值: 869\n", 115 | "魔法值: 130\n", 116 | "\n", 117 | "~~~狄仁杰小怪兽~~~\n", 118 | "生命值: 202\n", 119 | "~~~白元芳小怪兽~~~\n", 120 | "生命值: 450\n", 121 | "~~~王大锤小怪兽~~~\n", 122 | "生命值: 681\n", 123 | "========第09回合========\n", 124 | "骆昊使用了魔法攻击.\n", 125 | "王大锤回击了骆昊.\n", 126 | "~~~骆昊奥特曼~~~\n", 127 | "生命值: 858\n", 128 | "魔法值: 110\n", 129 | "\n", 130 | "~~~狄仁杰小怪兽~~~\n", 131 | "生命值: 190\n", 132 | "~~~白元芳小怪兽~~~\n", 133 | "生命值: 439\n", 134 | "~~~王大锤小怪兽~~~\n", 135 | "生命值: 669\n", 136 | "========第10回合========\n", 137 | "骆昊使用普通攻击打了狄仁杰.\n", 138 | "骆昊的魔法值恢复了5点.\n", 139 | "狄仁杰回击了骆昊.\n", 140 | "~~~骆昊奥特曼~~~\n", 141 | "生命值: 840\n", 142 | "魔法值: 115\n", 143 | "\n", 144 | "~~~狄仁杰小怪兽~~~\n", 145 | "生命值: 169\n", 146 | "~~~白元芳小怪兽~~~\n", 147 | "生命值: 439\n", 148 | "~~~王大锤小怪兽~~~\n", 149 | "生命值: 669\n", 150 | "========第11回合========\n", 151 | "骆昊使用了魔法攻击.\n", 152 | "白元芳回击了骆昊.\n", 153 | "~~~骆昊奥特曼~~~\n", 154 | "生命值: 827\n", 155 | "魔法值: 95\n", 156 | "\n", 157 | "~~~狄仁杰小怪兽~~~\n", 158 | "生命值: 155\n", 159 | "~~~白元芳小怪兽~~~\n", 160 | "生命值: 429\n", 161 | "~~~王大锤小怪兽~~~\n", 162 | "生命值: 659\n", 163 | "========第12回合========\n", 164 | "骆昊使用究极必杀技虐了王大锤.\n", 165 | "王大锤回击了骆昊.\n", 166 | "~~~骆昊奥特曼~~~\n", 167 | "生命值: 812\n", 168 | "魔法值: 45\n", 169 | "\n", 170 | "~~~狄仁杰小怪兽~~~\n", 171 | "生命值: 155\n", 172 | "~~~白元芳小怪兽~~~\n", 173 | "生命值: 429\n", 174 | "~~~王大锤小怪兽~~~\n", 175 | "生命值: 165\n", 176 | "========第13回合========\n", 177 | "骆昊使用普通攻击打了狄仁杰.\n", 178 | "骆昊的魔法值恢复了6点.\n", 179 | "狄仁杰回击了骆昊.\n", 180 | "~~~骆昊奥特曼~~~\n", 181 | "生命值: 802\n", 182 | "魔法值: 51\n", 183 | "\n", 184 | "~~~狄仁杰小怪兽~~~\n", 185 | "生命值: 130\n", 186 | "~~~白元芳小怪兽~~~\n", 187 | "生命值: 429\n", 188 | "~~~王大锤小怪兽~~~\n", 189 | "生命值: 165\n", 190 | "========第14回合========\n", 191 | "骆昊使用了魔法攻击.\n", 192 | "白元芳回击了骆昊.\n", 193 | "~~~骆昊奥特曼~~~\n", 194 | "生命值: 782\n", 195 | "魔法值: 31\n", 196 | "\n", 197 | "~~~狄仁杰小怪兽~~~\n", 198 | "生命值: 118\n", 199 | "~~~白元芳小怪兽~~~\n", 200 | "生命值: 417\n", 201 | "~~~王大锤小怪兽~~~\n", 202 | "生命值: 152\n", 203 | "========第15回合========\n", 204 | "骆昊使用普通攻击打了王大锤.\n", 205 | "骆昊的魔法值恢复了1点.\n", 206 | "王大锤回击了骆昊.\n", 207 | "~~~骆昊奥特曼~~~\n", 208 | "生命值: 765\n", 209 | "魔法值: 32\n", 210 | "\n", 211 | "~~~狄仁杰小怪兽~~~\n", 212 | "生命值: 118\n", 213 | "~~~白元芳小怪兽~~~\n", 214 | "生命值: 417\n", 215 | "~~~王大锤小怪兽~~~\n", 216 | "生命值: 129\n", 217 | "========第16回合========\n", 218 | "骆昊使用了魔法攻击.\n", 219 | "狄仁杰回击了骆昊.\n", 220 | "~~~骆昊奥特曼~~~\n", 221 | "生命值: 752\n", 222 | "魔法值: 12\n", 223 | "\n", 224 | "~~~狄仁杰小怪兽~~~\n", 225 | "生命值: 107\n", 226 | "~~~白元芳小怪兽~~~\n", 227 | "生命值: 406\n", 228 | "~~~王大锤小怪兽~~~\n", 229 | "生命值: 117\n", 230 | "========第17回合========\n", 231 | "骆昊使用普通攻击打了白元芳.\n", 232 | "骆昊的魔法值恢复了6点.\n", 233 | "白元芳回击了骆昊.\n", 234 | "~~~骆昊奥特曼~~~\n", 235 | "生命值: 741\n", 236 | "魔法值: 18\n", 237 | "\n", 238 | "~~~狄仁杰小怪兽~~~\n", 239 | "生命值: 107\n", 240 | "~~~白元芳小怪兽~~~\n", 241 | "生命值: 382\n", 242 | "~~~王大锤小怪兽~~~\n", 243 | "生命值: 117\n", 244 | "========第18回合========\n", 245 | "骆昊使用普通攻击打了王大锤.\n", 246 | "骆昊的魔法值恢复了2点.\n", 247 | "王大锤回击了骆昊.\n", 248 | "~~~骆昊奥特曼~~~\n", 249 | "生命值: 722\n", 250 | "魔法值: 20\n", 251 | "\n", 252 | "~~~狄仁杰小怪兽~~~\n", 253 | "生命值: 107\n", 254 | "~~~白元芳小怪兽~~~\n", 255 | "生命值: 382\n", 256 | "~~~王大锤小怪兽~~~\n", 257 | "生命值: 92\n", 258 | "========第19回合========\n", 259 | "骆昊使用了魔法攻击.\n", 260 | "白元芳回击了骆昊.\n", 261 | "~~~骆昊奥特曼~~~\n", 262 | "生命值: 703\n", 263 | "魔法值: 0\n", 264 | "\n", 265 | "~~~狄仁杰小怪兽~~~\n", 266 | "生命值: 96\n", 267 | "~~~白元芳小怪兽~~~\n", 268 | "生命值: 368\n", 269 | "~~~王大锤小怪兽~~~\n", 270 | "生命值: 77\n", 271 | "========第20回合========\n", 272 | "骆昊使用魔法失败.\n", 273 | "狄仁杰回击了骆昊.\n", 274 | "~~~骆昊奥特曼~~~\n", 275 | "生命值: 686\n", 276 | "魔法值: 0\n", 277 | "\n", 278 | "~~~狄仁杰小怪兽~~~\n", 279 | "生命值: 96\n", 280 | "~~~白元芳小怪兽~~~\n", 281 | "生命值: 368\n", 282 | "~~~王大锤小怪兽~~~\n", 283 | "生命值: 77\n", 284 | "========第21回合========\n", 285 | "骆昊使用魔法失败.\n", 286 | "王大锤回击了骆昊.\n", 287 | "~~~骆昊奥特曼~~~\n", 288 | "生命值: 670\n", 289 | "魔法值: 0\n", 290 | "\n", 291 | "~~~狄仁杰小怪兽~~~\n", 292 | "生命值: 96\n", 293 | "~~~白元芳小怪兽~~~\n", 294 | "生命值: 368\n", 295 | "~~~王大锤小怪兽~~~\n", 296 | "生命值: 77\n", 297 | "========第22回合========\n", 298 | "骆昊使用普通攻击打了王大锤.\n", 299 | "骆昊的魔法值恢复了7点.\n", 300 | "王大锤回击了骆昊.\n", 301 | "~~~骆昊奥特曼~~~\n", 302 | "生命值: 652\n", 303 | "魔法值: 7\n", 304 | "\n", 305 | "~~~狄仁杰小怪兽~~~\n", 306 | "生命值: 96\n", 307 | "~~~白元芳小怪兽~~~\n", 308 | "生命值: 368\n", 309 | "~~~王大锤小怪兽~~~\n", 310 | "生命值: 61\n", 311 | "========第23回合========\n", 312 | "骆昊使用魔法失败.\n", 313 | "王大锤回击了骆昊.\n", 314 | "~~~骆昊奥特曼~~~\n", 315 | "生命值: 637\n", 316 | "魔法值: 7\n", 317 | "\n", 318 | "~~~狄仁杰小怪兽~~~\n", 319 | "生命值: 96\n", 320 | "~~~白元芳小怪兽~~~\n", 321 | "生命值: 368\n", 322 | "~~~王大锤小怪兽~~~\n", 323 | "生命值: 61\n", 324 | "========第24回合========\n", 325 | "骆昊使用魔法失败.\n", 326 | "狄仁杰回击了骆昊.\n", 327 | "~~~骆昊奥特曼~~~\n", 328 | "生命值: 626\n", 329 | "魔法值: 7\n", 330 | "\n", 331 | "~~~狄仁杰小怪兽~~~\n", 332 | "生命值: 96\n", 333 | "~~~白元芳小怪兽~~~\n", 334 | "生命值: 368\n", 335 | "~~~王大锤小怪兽~~~\n", 336 | "生命值: 61\n", 337 | "========第25回合========\n", 338 | "骆昊使用魔法失败.\n", 339 | "狄仁杰回击了骆昊.\n", 340 | "~~~骆昊奥特曼~~~\n", 341 | "生命值: 614\n", 342 | "魔法值: 7\n", 343 | "\n", 344 | "~~~狄仁杰小怪兽~~~\n", 345 | "生命值: 96\n", 346 | "~~~白元芳小怪兽~~~\n", 347 | "生命值: 368\n", 348 | "~~~王大锤小怪兽~~~\n", 349 | "生命值: 61\n", 350 | "========第26回合========\n", 351 | "骆昊使用普通攻击打了白元芳.\n", 352 | "骆昊的魔法值恢复了3点.\n", 353 | "白元芳回击了骆昊.\n", 354 | "~~~骆昊奥特曼~~~\n", 355 | "生命值: 594\n", 356 | "魔法值: 10\n", 357 | "\n", 358 | "~~~狄仁杰小怪兽~~~\n", 359 | "生命值: 96\n", 360 | "~~~白元芳小怪兽~~~\n", 361 | "生命值: 353\n", 362 | "~~~王大锤小怪兽~~~\n", 363 | "生命值: 61\n", 364 | "========第27回合========\n", 365 | "骆昊使用普通攻击打了白元芳.\n", 366 | "骆昊的魔法值恢复了8点.\n", 367 | "白元芳回击了骆昊.\n", 368 | "~~~骆昊奥特曼~~~\n", 369 | "生命值: 574\n", 370 | "魔法值: 18\n", 371 | "\n", 372 | "~~~狄仁杰小怪兽~~~\n", 373 | "生命值: 96\n", 374 | "~~~白元芳小怪兽~~~\n", 375 | "生命值: 336\n", 376 | "~~~王大锤小怪兽~~~\n", 377 | "生命值: 61\n", 378 | "========第28回合========\n", 379 | "骆昊使用普通攻击打了白元芳.\n", 380 | "骆昊的魔法值恢复了10点.\n", 381 | "白元芳回击了骆昊.\n", 382 | "~~~骆昊奥特曼~~~\n", 383 | "生命值: 560\n", 384 | "魔法值: 28\n", 385 | "\n", 386 | "~~~狄仁杰小怪兽~~~\n", 387 | "生命值: 96\n", 388 | "~~~白元芳小怪兽~~~\n", 389 | "生命值: 321\n", 390 | "~~~王大锤小怪兽~~~\n", 391 | "生命值: 61\n", 392 | "========第29回合========\n", 393 | "骆昊使用普通攻击打了白元芳.\n", 394 | "骆昊的魔法值恢复了1点.\n", 395 | "白元芳回击了骆昊.\n", 396 | "~~~骆昊奥特曼~~~\n", 397 | "生命值: 548\n", 398 | "魔法值: 29\n", 399 | "\n", 400 | "~~~狄仁杰小怪兽~~~\n", 401 | "生命值: 96\n", 402 | "~~~白元芳小怪兽~~~\n", 403 | "生命值: 302\n", 404 | "~~~王大锤小怪兽~~~\n", 405 | "生命值: 61\n", 406 | "========第30回合========\n", 407 | "骆昊使用普通攻击打了白元芳.\n", 408 | "骆昊的魔法值恢复了1点.\n", 409 | "白元芳回击了骆昊.\n", 410 | "~~~骆昊奥特曼~~~\n", 411 | "生命值: 533\n", 412 | "魔法值: 30\n", 413 | "\n", 414 | "~~~狄仁杰小怪兽~~~\n", 415 | "生命值: 96\n", 416 | "~~~白元芳小怪兽~~~\n", 417 | "生命值: 282\n", 418 | "~~~王大锤小怪兽~~~\n", 419 | "生命值: 61\n", 420 | "========第31回合========\n", 421 | "骆昊使用普通攻击打了白元芳.\n", 422 | "骆昊的魔法值恢复了8点.\n", 423 | "白元芳回击了骆昊.\n", 424 | "~~~骆昊奥特曼~~~\n", 425 | "生命值: 522\n", 426 | "魔法值: 38\n", 427 | "\n", 428 | "~~~狄仁杰小怪兽~~~\n", 429 | "生命值: 96\n", 430 | "~~~白元芳小怪兽~~~\n", 431 | "生命值: 266\n", 432 | "~~~王大锤小怪兽~~~\n", 433 | "生命值: 61\n", 434 | "========第32回合========\n", 435 | "骆昊使用普通攻击打了白元芳.\n", 436 | "骆昊的魔法值恢复了5点.\n", 437 | "白元芳回击了骆昊.\n", 438 | "~~~骆昊奥特曼~~~\n", 439 | "生命值: 504\n", 440 | "魔法值: 43\n", 441 | "\n", 442 | "~~~狄仁杰小怪兽~~~\n", 443 | "生命值: 96\n", 444 | "~~~白元芳小怪兽~~~\n", 445 | "生命值: 244\n", 446 | "~~~王大锤小怪兽~~~\n", 447 | "生命值: 61\n", 448 | "========第33回合========\n", 449 | "骆昊使用普通攻击打了狄仁杰.\n", 450 | "骆昊的魔法值恢复了4点.\n", 451 | "狄仁杰回击了骆昊.\n", 452 | "~~~骆昊奥特曼~~~\n", 453 | "生命值: 486\n", 454 | "魔法值: 47\n", 455 | "\n", 456 | "~~~狄仁杰小怪兽~~~\n", 457 | "生命值: 76\n", 458 | "~~~白元芳小怪兽~~~\n", 459 | "生命值: 244\n", 460 | "~~~王大锤小怪兽~~~\n", 461 | "生命值: 61\n", 462 | "========第34回合========\n", 463 | "骆昊使用普通攻击打了狄仁杰.\n", 464 | "骆昊的魔法值恢复了4点.\n", 465 | "狄仁杰回击了骆昊.\n", 466 | "~~~骆昊奥特曼~~~\n", 467 | "生命值: 472\n", 468 | "魔法值: 51\n", 469 | "\n", 470 | "~~~狄仁杰小怪兽~~~\n", 471 | "生命值: 55\n", 472 | "~~~白元芳小怪兽~~~\n", 473 | "生命值: 244\n", 474 | "~~~王大锤小怪兽~~~\n", 475 | "生命值: 61\n", 476 | "========第35回合========\n", 477 | "骆昊使用了魔法攻击.\n", 478 | "狄仁杰回击了骆昊.\n", 479 | "~~~骆昊奥特曼~~~\n", 480 | "生命值: 458\n", 481 | "魔法值: 31\n", 482 | "\n", 483 | "~~~狄仁杰小怪兽~~~\n", 484 | "生命值: 40\n", 485 | "~~~白元芳小怪兽~~~\n", 486 | "生命值: 230\n", 487 | "~~~王大锤小怪兽~~~\n", 488 | "生命值: 51\n", 489 | "========第36回合========\n", 490 | "骆昊使用普通攻击打了狄仁杰.\n", 491 | "骆昊的魔法值恢复了7点.\n", 492 | "狄仁杰回击了骆昊.\n", 493 | "~~~骆昊奥特曼~~~\n", 494 | "生命值: 439\n", 495 | "魔法值: 38\n", 496 | "\n", 497 | "~~~狄仁杰小怪兽~~~\n", 498 | "生命值: 21\n", 499 | "~~~白元芳小怪兽~~~\n", 500 | "生命值: 230\n", 501 | "~~~王大锤小怪兽~~~\n", 502 | "生命值: 51\n", 503 | "========第37回合========\n", 504 | "骆昊使用普通攻击打了白元芳.\n", 505 | "骆昊的魔法值恢复了1点.\n", 506 | "白元芳回击了骆昊.\n", 507 | "~~~骆昊奥特曼~~~\n", 508 | "生命值: 426\n", 509 | "魔法值: 39\n", 510 | "\n", 511 | "~~~狄仁杰小怪兽~~~\n", 512 | "生命值: 21\n", 513 | "~~~白元芳小怪兽~~~\n", 514 | "生命值: 205\n", 515 | "~~~王大锤小怪兽~~~\n", 516 | "生命值: 51\n", 517 | "========第38回合========\n", 518 | "骆昊使用普通攻击打了狄仁杰.\n", 519 | "骆昊的魔法值恢复了2点.\n", 520 | "~~~骆昊奥特曼~~~\n", 521 | "生命值: 426\n", 522 | "魔法值: 41\n", 523 | "\n", 524 | "~~~狄仁杰小怪兽~~~\n", 525 | "生命值: 0\n", 526 | "~~~白元芳小怪兽~~~\n", 527 | "生命值: 205\n", 528 | "~~~王大锤小怪兽~~~\n", 529 | "生命值: 51\n", 530 | "========第39回合========\n", 531 | "骆昊使用了魔法攻击.\n", 532 | "王大锤回击了骆昊.\n", 533 | "~~~骆昊奥特曼~~~\n", 534 | "生命值: 406\n", 535 | "魔法值: 21\n", 536 | "\n", 537 | "~~~狄仁杰小怪兽~~~\n", 538 | "生命值: 0\n", 539 | "~~~白元芳小怪兽~~~\n", 540 | "生命值: 192\n", 541 | "~~~王大锤小怪兽~~~\n", 542 | "生命值: 36\n", 543 | "========第40回合========\n", 544 | "骆昊使用普通攻击打了王大锤.\n", 545 | "骆昊的魔法值恢复了1点.\n", 546 | "王大锤回击了骆昊.\n", 547 | "~~~骆昊奥特曼~~~\n", 548 | "生命值: 393\n", 549 | "魔法值: 22\n", 550 | "\n", 551 | "~~~狄仁杰小怪兽~~~\n", 552 | "生命值: 0\n", 553 | "~~~白元芳小怪兽~~~\n", 554 | "生命值: 192\n", 555 | "~~~王大锤小怪兽~~~\n", 556 | "生命值: 11\n", 557 | "========第41回合========\n", 558 | "骆昊使用了魔法攻击.\n", 559 | "白元芳回击了骆昊.\n", 560 | "~~~骆昊奥特曼~~~\n", 561 | "生命值: 379\n", 562 | "魔法值: 2\n", 563 | "\n", 564 | "~~~狄仁杰小怪兽~~~\n", 565 | "生命值: 0\n", 566 | "~~~白元芳小怪兽~~~\n", 567 | "生命值: 177\n", 568 | "~~~王大锤小怪兽~~~\n", 569 | "生命值: 1\n", 570 | "========第42回合========\n", 571 | "骆昊使用魔法失败.\n", 572 | "白元芳回击了骆昊.\n", 573 | "~~~骆昊奥特曼~~~\n", 574 | "生命值: 367\n", 575 | "魔法值: 2\n", 576 | "\n", 577 | "~~~狄仁杰小怪兽~~~\n", 578 | "生命值: 0\n", 579 | "~~~白元芳小怪兽~~~\n", 580 | "生命值: 177\n", 581 | "~~~王大锤小怪兽~~~\n", 582 | "生命值: 1\n", 583 | "========第43回合========\n", 584 | "骆昊使用魔法失败.\n", 585 | "王大锤回击了骆昊.\n", 586 | "~~~骆昊奥特曼~~~\n", 587 | "生命值: 352\n", 588 | "魔法值: 2\n", 589 | "\n", 590 | "~~~狄仁杰小怪兽~~~\n", 591 | "生命值: 0\n", 592 | "~~~白元芳小怪兽~~~\n", 593 | "生命值: 177\n", 594 | "~~~王大锤小怪兽~~~\n", 595 | "生命值: 1\n", 596 | "========第44回合========\n", 597 | "骆昊使用魔法失败.\n", 598 | "白元芳回击了骆昊.\n", 599 | "~~~骆昊奥特曼~~~\n", 600 | "生命值: 336\n", 601 | "魔法值: 2\n", 602 | "\n", 603 | "~~~狄仁杰小怪兽~~~\n", 604 | "生命值: 0\n", 605 | "~~~白元芳小怪兽~~~\n", 606 | "生命值: 177\n", 607 | "~~~王大锤小怪兽~~~\n", 608 | "生命值: 1\n", 609 | "========第45回合========\n", 610 | "骆昊使用魔法失败.\n", 611 | "白元芳回击了骆昊.\n", 612 | "~~~骆昊奥特曼~~~\n", 613 | "生命值: 323\n", 614 | "魔法值: 2\n", 615 | "\n", 616 | "~~~狄仁杰小怪兽~~~\n", 617 | "生命值: 0\n", 618 | "~~~白元芳小怪兽~~~\n", 619 | "生命值: 177\n", 620 | "~~~王大锤小怪兽~~~\n", 621 | "生命值: 1\n", 622 | "========第46回合========\n", 623 | "骆昊使用普通攻击打了白元芳.\n", 624 | "骆昊的魔法值恢复了5点.\n", 625 | "白元芳回击了骆昊.\n", 626 | "~~~骆昊奥特曼~~~\n", 627 | "生命值: 303\n", 628 | "魔法值: 7\n", 629 | "\n", 630 | "~~~狄仁杰小怪兽~~~\n", 631 | "生命值: 0\n", 632 | "~~~白元芳小怪兽~~~\n", 633 | "生命值: 152\n", 634 | "~~~王大锤小怪兽~~~\n", 635 | "生命值: 1\n", 636 | "========第47回合========\n", 637 | "骆昊使用普通攻击打了王大锤.\n", 638 | "骆昊的魔法值恢复了7点.\n", 639 | "~~~骆昊奥特曼~~~\n", 640 | "生命值: 303\n", 641 | "魔法值: 14\n", 642 | "\n", 643 | "~~~狄仁杰小怪兽~~~\n", 644 | "生命值: 0\n", 645 | "~~~白元芳小怪兽~~~\n", 646 | "生命值: 152\n", 647 | "~~~王大锤小怪兽~~~\n", 648 | "生命值: 0\n", 649 | "========第48回合========\n", 650 | "骆昊使用普通攻击打了白元芳.\n", 651 | "骆昊的魔法值恢复了7点.\n", 652 | "白元芳回击了骆昊.\n", 653 | "~~~骆昊奥特曼~~~\n", 654 | "生命值: 290\n", 655 | "魔法值: 21\n", 656 | "\n", 657 | "~~~狄仁杰小怪兽~~~\n", 658 | "生命值: 0\n", 659 | "~~~白元芳小怪兽~~~\n", 660 | "生命值: 134\n", 661 | "~~~王大锤小怪兽~~~\n", 662 | "生命值: 0\n", 663 | "========第49回合========\n", 664 | "骆昊使用普通攻击打了白元芳.\n", 665 | "骆昊的魔法值恢复了4点.\n", 666 | "白元芳回击了骆昊.\n", 667 | "~~~骆昊奥特曼~~~\n", 668 | "生命值: 277\n", 669 | "魔法值: 25\n", 670 | "\n", 671 | "~~~狄仁杰小怪兽~~~\n", 672 | "生命值: 0\n", 673 | "~~~白元芳小怪兽~~~\n", 674 | "生命值: 112\n", 675 | "~~~王大锤小怪兽~~~\n", 676 | "生命值: 0\n", 677 | "========第50回合========\n", 678 | "骆昊使用了魔法攻击.\n", 679 | "白元芳回击了骆昊.\n", 680 | "~~~骆昊奥特曼~~~\n", 681 | "生命值: 264\n", 682 | "魔法值: 5\n", 683 | "\n", 684 | "~~~狄仁杰小怪兽~~~\n", 685 | "生命值: 0\n", 686 | "~~~白元芳小怪兽~~~\n", 687 | "生命值: 101\n", 688 | "~~~王大锤小怪兽~~~\n", 689 | "生命值: 0\n", 690 | "========第51回合========\n", 691 | "骆昊使用普通攻击打了白元芳.\n", 692 | "骆昊的魔法值恢复了3点.\n", 693 | "白元芳回击了骆昊.\n", 694 | "~~~骆昊奥特曼~~~\n", 695 | "生命值: 246\n", 696 | "魔法值: 8\n", 697 | "\n", 698 | "~~~狄仁杰小怪兽~~~\n", 699 | "生命值: 0\n", 700 | "~~~白元芳小怪兽~~~\n", 701 | "生命值: 86\n", 702 | "~~~王大锤小怪兽~~~\n", 703 | "生命值: 0\n", 704 | "========第52回合========\n", 705 | "骆昊使用普通攻击打了白元芳.\n", 706 | "骆昊的魔法值恢复了5点.\n", 707 | "白元芳回击了骆昊.\n", 708 | "~~~骆昊奥特曼~~~\n", 709 | "生命值: 231\n", 710 | "魔法值: 13\n", 711 | "\n", 712 | "~~~狄仁杰小怪兽~~~\n", 713 | "生命值: 0\n", 714 | "~~~白元芳小怪兽~~~\n", 715 | "生命值: 70\n", 716 | "~~~王大锤小怪兽~~~\n", 717 | "生命值: 0\n", 718 | "========第53回合========\n", 719 | "骆昊使用普通攻击打了白元芳.\n", 720 | "骆昊的魔法值恢复了10点.\n", 721 | "白元芳回击了骆昊.\n", 722 | "~~~骆昊奥特曼~~~\n", 723 | "生命值: 215\n", 724 | "魔法值: 23\n", 725 | "\n", 726 | "~~~狄仁杰小怪兽~~~\n", 727 | "生命值: 0\n", 728 | "~~~白元芳小怪兽~~~\n", 729 | "生命值: 52\n", 730 | "~~~王大锤小怪兽~~~\n", 731 | "生命值: 0\n", 732 | "========第54回合========\n", 733 | "骆昊使用普通攻击打了白元芳.\n", 734 | "骆昊的魔法值恢复了9点.\n", 735 | "白元芳回击了骆昊.\n", 736 | "~~~骆昊奥特曼~~~\n", 737 | "生命值: 198\n", 738 | "魔法值: 32\n", 739 | "\n", 740 | "~~~狄仁杰小怪兽~~~\n", 741 | "生命值: 0\n", 742 | "~~~白元芳小怪兽~~~\n", 743 | "生命值: 31\n", 744 | "~~~王大锤小怪兽~~~\n", 745 | "生命值: 0\n", 746 | "========第55回合========\n", 747 | "骆昊使用普通攻击打了白元芳.\n", 748 | "骆昊的魔法值恢复了6点.\n", 749 | "白元芳回击了骆昊.\n", 750 | "~~~骆昊奥特曼~~~\n", 751 | "生命值: 183\n", 752 | "魔法值: 38\n", 753 | "\n", 754 | "~~~狄仁杰小怪兽~~~\n", 755 | "生命值: 0\n", 756 | "~~~白元芳小怪兽~~~\n", 757 | "生命值: 12\n", 758 | "~~~王大锤小怪兽~~~\n", 759 | "生命值: 0\n", 760 | "========第56回合========\n", 761 | "骆昊使用普通攻击打了白元芳.\n", 762 | "骆昊的魔法值恢复了2点.\n", 763 | "~~~骆昊奥特曼~~~\n", 764 | "生命值: 183\n", 765 | "魔法值: 40\n", 766 | "\n", 767 | "~~~狄仁杰小怪兽~~~\n", 768 | "生命值: 0\n", 769 | "~~~白元芳小怪兽~~~\n", 770 | "生命值: 0\n", 771 | "~~~王大锤小怪兽~~~\n", 772 | "生命值: 0\n", 773 | "\n", 774 | "========战斗结束!========\n", 775 | "\n", 776 | "骆昊奥特曼胜利!\n" 777 | ] 778 | } 779 | ], 780 | "source": [ 781 | "# Try the given example 1\n", 782 | "from abc import ABCMeta, abstractmethod\n", 783 | "from random import randint, randrange\n", 784 | "\n", 785 | "\n", 786 | "class Fighter(object, metaclass=ABCMeta):\n", 787 | " \"\"\"战斗者\"\"\"\n", 788 | "\n", 789 | " # 通过__slots__魔法限定对象可以绑定的成员变量\n", 790 | " __slots__ = ('_name', '_hp')\n", 791 | "\n", 792 | " def __init__(self, name, hp):\n", 793 | " \"\"\"初始化方法\n", 794 | "\n", 795 | " :param name: 名字\n", 796 | " :param hp: 生命值\n", 797 | " \"\"\"\n", 798 | " self._name = name\n", 799 | " self._hp = hp\n", 800 | "\n", 801 | " @property\n", 802 | " def name(self):\n", 803 | " return self._name\n", 804 | "\n", 805 | " @property\n", 806 | " def hp(self):\n", 807 | " return self._hp\n", 808 | "\n", 809 | " @hp.setter\n", 810 | " def hp(self, hp):\n", 811 | " self._hp = hp if hp >= 0 else 0\n", 812 | "\n", 813 | " @property\n", 814 | " def alive(self):\n", 815 | " return self._hp > 0\n", 816 | "\n", 817 | " @abstractmethod\n", 818 | " def attack(self, other):\n", 819 | " \"\"\"攻击\n", 820 | "\n", 821 | " :param other: 被攻击的对象\n", 822 | " \"\"\"\n", 823 | " pass\n", 824 | "\n", 825 | "\n", 826 | "class Ultraman(Fighter):\n", 827 | " \"\"\"奥特曼\"\"\"\n", 828 | "\n", 829 | " __slots__ = ('_name', '_hp', '_mp')\n", 830 | "\n", 831 | " def __init__(self, name, hp, mp):\n", 832 | " \"\"\"初始化方法\n", 833 | "\n", 834 | " :param name: 名字\n", 835 | " :param hp: 生命值\n", 836 | " :param mp: 魔法值\n", 837 | " \"\"\"\n", 838 | " super().__init__(name, hp)\n", 839 | " self._mp = mp\n", 840 | "\n", 841 | " def attack(self, other):\n", 842 | " other.hp -= randint(15, 25)\n", 843 | "\n", 844 | " def huge_attack(self, other):\n", 845 | " \"\"\"究极必杀技(打掉对方至少50点或四分之三的血)\n", 846 | "\n", 847 | " :param other: 被攻击的对象\n", 848 | "\n", 849 | " :return: 使用成功返回True否则返回False\n", 850 | " \"\"\"\n", 851 | " if self._mp >= 50:\n", 852 | " self._mp -= 50\n", 853 | " injury = other.hp * 3 // 4\n", 854 | " injury = injury if injury >= 50 else 50\n", 855 | " other.hp -= injury\n", 856 | " return True\n", 857 | " else:\n", 858 | " self.attack(other)\n", 859 | " return False\n", 860 | "\n", 861 | " def magic_attack(self, others):\n", 862 | " \"\"\"魔法攻击\n", 863 | "\n", 864 | " :param others: 被攻击的群体\n", 865 | "\n", 866 | " :return: 使用魔法成功返回True否则返回False\n", 867 | " \"\"\"\n", 868 | " if self._mp >= 20:\n", 869 | " self._mp -= 20\n", 870 | " for temp in others:\n", 871 | " if temp.alive:\n", 872 | " temp.hp -= randint(10, 15)\n", 873 | " return True\n", 874 | " else:\n", 875 | " return False\n", 876 | "\n", 877 | " def resume(self):\n", 878 | " \"\"\"恢复魔法值\"\"\"\n", 879 | " incr_point = randint(1, 10)\n", 880 | " self._mp += incr_point\n", 881 | " return incr_point\n", 882 | "\n", 883 | " def __str__(self):\n", 884 | " return '~~~%s奥特曼~~~\\n' % self._name + \\\n", 885 | " '生命值: %d\\n' % self._hp + \\\n", 886 | " '魔法值: %d\\n' % self._mp\n", 887 | "\n", 888 | "\n", 889 | "class Monster(Fighter):\n", 890 | " \"\"\"小怪兽\"\"\"\n", 891 | "\n", 892 | " __slots__ = ('_name', '_hp')\n", 893 | "\n", 894 | " def attack(self, other):\n", 895 | " other.hp -= randint(10, 20)\n", 896 | "\n", 897 | " def __str__(self):\n", 898 | " return '~~~%s小怪兽~~~\\n' % self._name + \\\n", 899 | " '生命值: %d\\n' % self._hp\n", 900 | "\n", 901 | "\n", 902 | "def is_any_alive(monsters):\n", 903 | " \"\"\"判断有没有小怪兽是活着的\"\"\"\n", 904 | " for monster in monsters:\n", 905 | " if monster.alive > 0:\n", 906 | " return True\n", 907 | " return False\n", 908 | "\n", 909 | "\n", 910 | "def select_alive_one(monsters):\n", 911 | " \"\"\"选中一只活着的小怪兽\"\"\"\n", 912 | " monsters_len = len(monsters)\n", 913 | " while True:\n", 914 | " index = randrange(monsters_len)\n", 915 | " monster = monsters[index]\n", 916 | " if monster.alive > 0:\n", 917 | " return monster\n", 918 | "\n", 919 | "\n", 920 | "def display_info(ultraman, monsters):\n", 921 | " \"\"\"显示奥特曼和小怪兽的信息\"\"\"\n", 922 | " print(ultraman)\n", 923 | " for monster in monsters:\n", 924 | " print(monster, end='')\n", 925 | "\n", 926 | "\n", 927 | "def main():\n", 928 | " u = Ultraman('骆昊', 1000, 120)\n", 929 | " m1 = Monster('狄仁杰', 250)\n", 930 | " m2 = Monster('白元芳', 500)\n", 931 | " m3 = Monster('王大锤', 750)\n", 932 | " ms = [m1, m2, m3]\n", 933 | " fight_round = 1\n", 934 | " while u.alive and is_any_alive(ms):\n", 935 | " print('========第%02d回合========' % fight_round)\n", 936 | " m = select_alive_one(ms) # 选中一只小怪兽\n", 937 | " skill = randint(1, 10) # 通过随机数选择使用哪种技能\n", 938 | " if skill <= 6: # 60%的概率使用普通攻击\n", 939 | " print('%s使用普通攻击打了%s.' % (u.name, m.name))\n", 940 | " u.attack(m)\n", 941 | " print('%s的魔法值恢复了%d点.' % (u.name, u.resume()))\n", 942 | " elif skill <= 9: # 30%的概率使用魔法攻击(可能因魔法值不足而失败)\n", 943 | " if u.magic_attack(ms):\n", 944 | " print('%s使用了魔法攻击.' % u.name)\n", 945 | " else:\n", 946 | " print('%s使用魔法失败.' % u.name)\n", 947 | " else: # 10%的概率使用究极必杀技(如果魔法值不足则使用普通攻击)\n", 948 | " if u.huge_attack(m):\n", 949 | " print('%s使用究极必杀技虐了%s.' % (u.name, m.name))\n", 950 | " else:\n", 951 | " print('%s使用普通攻击打了%s.' % (u.name, m.name))\n", 952 | " print('%s的魔法值恢复了%d点.' % (u.name, u.resume()))\n", 953 | " if m.alive > 0: # 如果选中的小怪兽没有死就回击奥特曼\n", 954 | " print('%s回击了%s.' % (m.name, u.name))\n", 955 | " m.attack(u)\n", 956 | " display_info(u, ms) # 每个回合结束后显示奥特曼和小怪兽的信息\n", 957 | " fight_round += 1\n", 958 | " print('\\n========战斗结束!========\\n')\n", 959 | " if u.alive > 0:\n", 960 | " print('%s奥特曼胜利!' % u.name)\n", 961 | " else:\n", 962 | " print('小怪兽胜利!')\n", 963 | "\n", 964 | "\n", 965 | "if __name__ == '__main__':\n", 966 | " main()" 967 | ] 968 | }, 969 | { 970 | "cell_type": "code", 971 | "execution_count": 2, 972 | "metadata": {}, 973 | "outputs": [ 974 | { 975 | "name": "stdout", 976 | "output_type": "stream", 977 | "text": [ 978 | "东邪: [♠5, ♠7, ♠8, ♠Q, ♣9, ♣K, ♥9, ♥10, ♥Q, ♥K, ♦3, ♦4, ♦6]\n", 979 | "西毒: [♠3, ♠4, ♠K, ♣3, ♣5, ♣J, ♣Q, ♥3, ♥6, ♦A, ♦8, ♦9, ♦K]\n", 980 | "南帝: [♠A, ♠2, ♣2, ♣6, ♣10, ♥A, ♥5, ♥7, ♦2, ♦5, ♦10, ♦J, ♦Q]\n", 981 | "北丐: [♠6, ♠9, ♠10, ♠J, ♣A, ♣4, ♣7, ♣8, ♥2, ♥4, ♥8, ♥J, ♦7]\n" 982 | ] 983 | } 984 | ], 985 | "source": [ 986 | "# Try the given example 2\n", 987 | "import random\n", 988 | "\n", 989 | "\n", 990 | "class Card(object):\n", 991 | " \"\"\"一张牌\"\"\"\n", 992 | "\n", 993 | " def __init__(self, suite, face):\n", 994 | " self._suite = suite\n", 995 | " self._face = face\n", 996 | "\n", 997 | " @property\n", 998 | " def face(self):\n", 999 | " return self._face\n", 1000 | "\n", 1001 | " @property\n", 1002 | " def suite(self):\n", 1003 | " return self._suite\n", 1004 | "\n", 1005 | " def __str__(self):\n", 1006 | " if self._face == 1:\n", 1007 | " face_str = 'A'\n", 1008 | " elif self._face == 11:\n", 1009 | " face_str = 'J'\n", 1010 | " elif self._face == 12:\n", 1011 | " face_str = 'Q'\n", 1012 | " elif self._face == 13:\n", 1013 | " face_str = 'K'\n", 1014 | " else:\n", 1015 | " face_str = str(self._face)\n", 1016 | " return '%s%s' % (self._suite, face_str)\n", 1017 | "\n", 1018 | " def __repr__(self):\n", 1019 | " return self.__str__()\n", 1020 | "\n", 1021 | "\n", 1022 | "class Poker(object):\n", 1023 | " \"\"\"一副牌\"\"\"\n", 1024 | "\n", 1025 | " def __init__(self):\n", 1026 | " self._cards = [Card(suite, face) \n", 1027 | " for suite in '♠♥♣♦'\n", 1028 | " for face in range(1, 14)]\n", 1029 | " self._current = 0\n", 1030 | "\n", 1031 | " @property\n", 1032 | " def cards(self):\n", 1033 | " return self._cards\n", 1034 | "\n", 1035 | " def shuffle(self):\n", 1036 | " \"\"\"洗牌(随机乱序)\"\"\"\n", 1037 | " self._current = 0\n", 1038 | " random.shuffle(self._cards)\n", 1039 | "\n", 1040 | " @property\n", 1041 | " def next(self):\n", 1042 | " \"\"\"发牌\"\"\"\n", 1043 | " card = self._cards[self._current]\n", 1044 | " self._current += 1\n", 1045 | " return card\n", 1046 | "\n", 1047 | " @property\n", 1048 | " def has_next(self):\n", 1049 | " \"\"\"还有没有牌\"\"\"\n", 1050 | " return self._current < len(self._cards)\n", 1051 | "\n", 1052 | "\n", 1053 | "class Player(object):\n", 1054 | " \"\"\"玩家\"\"\"\n", 1055 | "\n", 1056 | " def __init__(self, name):\n", 1057 | " self._name = name\n", 1058 | " self._cards_on_hand = []\n", 1059 | "\n", 1060 | " @property\n", 1061 | " def name(self):\n", 1062 | " return self._name\n", 1063 | "\n", 1064 | " @property\n", 1065 | " def cards_on_hand(self):\n", 1066 | " return self._cards_on_hand\n", 1067 | "\n", 1068 | " def get(self, card):\n", 1069 | " \"\"\"摸牌\"\"\"\n", 1070 | " self._cards_on_hand.append(card)\n", 1071 | "\n", 1072 | " def arrange(self, card_key):\n", 1073 | " \"\"\"玩家整理手上的牌\"\"\"\n", 1074 | " self._cards_on_hand.sort(key=card_key)\n", 1075 | "\n", 1076 | "\n", 1077 | "# 排序规则-先根据花色再根据点数排序\n", 1078 | "def get_key(card):\n", 1079 | " return (card.suite, card.face)\n", 1080 | "\n", 1081 | "\n", 1082 | "def main():\n", 1083 | " p = Poker()\n", 1084 | " p.shuffle()\n", 1085 | " players = [Player('东邪'), Player('西毒'), Player('南帝'), Player('北丐')]\n", 1086 | " for _ in range(13):\n", 1087 | " for player in players:\n", 1088 | " player.get(p.next)\n", 1089 | " for player in players:\n", 1090 | " print(player.name + ':', end=' ')\n", 1091 | " player.arrange(get_key)\n", 1092 | " print(player.cards_on_hand)\n", 1093 | "\n", 1094 | "\n", 1095 | "if __name__ == '__main__':\n", 1096 | " main()" 1097 | ] 1098 | }, 1099 | { 1100 | "cell_type": "code", 1101 | "execution_count": 3, 1102 | "metadata": {}, 1103 | "outputs": [ 1104 | { 1105 | "name": "stdout", 1106 | "output_type": "stream", 1107 | "text": [ 1108 | "刘备本月工资为: ¥15000.0元\n", 1109 | "请输入诸葛亮本月工作时间: 240\n", 1110 | "诸葛亮本月工资为: ¥36000.0元\n", 1111 | "曹操本月工资为: ¥15000.0元\n", 1112 | "请输入荀彧本月销售额: 10000\n", 1113 | "荀彧本月工资为: ¥1700.0元\n", 1114 | "请输入吕布本月销售额: 8000\n", 1115 | "吕布本月工资为: ¥1600.0元\n", 1116 | "请输入张辽本月工作时间: 270\n", 1117 | "张辽本月工资为: ¥40500.0元\n", 1118 | "请输入赵云本月工作时间: 230\n", 1119 | "赵云本月工资为: ¥34500.0元\n" 1120 | ] 1121 | } 1122 | ], 1123 | "source": [ 1124 | "# Try the given example 3\n", 1125 | "\"\"\"\n", 1126 | "某公司有三种类型的员工 分别是部门经理、程序员和销售员\n", 1127 | "需要设计一个工资结算系统 根据提供的员工信息来计算月薪\n", 1128 | "部门经理的月薪是每月固定15000元\n", 1129 | "程序员的月薪按本月工作时间计算 每小时150元\n", 1130 | "销售员的月薪是1200元的底薪加上销售额5%的提成\n", 1131 | "\"\"\"\n", 1132 | "from abc import ABCMeta, abstractmethod\n", 1133 | "\n", 1134 | "\n", 1135 | "class Employee(object, metaclass=ABCMeta):\n", 1136 | " \"\"\"员工\"\"\"\n", 1137 | "\n", 1138 | " def __init__(self, name):\n", 1139 | " \"\"\"\n", 1140 | " 初始化方法\n", 1141 | "\n", 1142 | " :param name: 姓名\n", 1143 | " \"\"\"\n", 1144 | " self._name = name\n", 1145 | "\n", 1146 | " @property\n", 1147 | " def name(self):\n", 1148 | " return self._name\n", 1149 | "\n", 1150 | " @abstractmethod\n", 1151 | " def get_salary(self):\n", 1152 | " \"\"\"\n", 1153 | " 获得月薪\n", 1154 | "\n", 1155 | " :return: 月薪\n", 1156 | " \"\"\"\n", 1157 | " pass\n", 1158 | "\n", 1159 | "\n", 1160 | "class Manager(Employee):\n", 1161 | " \"\"\"部门经理\"\"\"\n", 1162 | "\n", 1163 | " def get_salary(self):\n", 1164 | " return 15000.0\n", 1165 | "\n", 1166 | "\n", 1167 | "class Programmer(Employee):\n", 1168 | " \"\"\"程序员\"\"\"\n", 1169 | "\n", 1170 | " def __init__(self, name, working_hour=0):\n", 1171 | " super().__init__(name)\n", 1172 | " self._working_hour = working_hour\n", 1173 | "\n", 1174 | " @property\n", 1175 | " def working_hour(self):\n", 1176 | " return self._working_hour\n", 1177 | "\n", 1178 | " @working_hour.setter\n", 1179 | " def working_hour(self, working_hour):\n", 1180 | " self._working_hour = working_hour if working_hour > 0 else 0\n", 1181 | "\n", 1182 | " def get_salary(self):\n", 1183 | " return 150.0 * self._working_hour\n", 1184 | "\n", 1185 | "\n", 1186 | "class Salesman(Employee):\n", 1187 | " \"\"\"销售员\"\"\"\n", 1188 | "\n", 1189 | " def __init__(self, name, sales=0):\n", 1190 | " super().__init__(name)\n", 1191 | " self._sales = sales\n", 1192 | "\n", 1193 | " @property\n", 1194 | " def sales(self):\n", 1195 | " return self._sales\n", 1196 | "\n", 1197 | " @sales.setter\n", 1198 | " def sales(self, sales):\n", 1199 | " self._sales = sales if sales > 0 else 0\n", 1200 | "\n", 1201 | " def get_salary(self):\n", 1202 | " return 1200.0 + self._sales * 0.05\n", 1203 | "\n", 1204 | "\n", 1205 | "def main():\n", 1206 | " emps = [\n", 1207 | " Manager('刘备'), Programmer('诸葛亮'),\n", 1208 | " Manager('曹操'), Salesman('荀彧'),\n", 1209 | " Salesman('吕布'), Programmer('张辽'),\n", 1210 | " Programmer('赵云')\n", 1211 | " ]\n", 1212 | " for emp in emps:\n", 1213 | " if isinstance(emp, Programmer):\n", 1214 | " emp.working_hour = int(input('请输入%s本月工作时间: ' % emp.name))\n", 1215 | " elif isinstance(emp, Salesman):\n", 1216 | " emp.sales = float(input('请输入%s本月销售额: ' % emp.name))\n", 1217 | " # 同样是接收get_salary这个消息但是不同的员工表现出了不同的行为(多态)\n", 1218 | " print('%s本月工资为: ¥%s元' %\n", 1219 | " (emp.name, emp.get_salary()))\n", 1220 | "\n", 1221 | "\n", 1222 | "if __name__ == '__main__':\n", 1223 | " main()" 1224 | ] 1225 | } 1226 | ], 1227 | "metadata": { 1228 | "kernelspec": { 1229 | "display_name": "Python 3", 1230 | "language": "python", 1231 | "name": "python3" 1232 | }, 1233 | "language_info": { 1234 | "codemirror_mode": { 1235 | "name": "ipython", 1236 | "version": 3 1237 | }, 1238 | "file_extension": ".py", 1239 | "mimetype": "text/x-python", 1240 | "name": "python", 1241 | "nbconvert_exporter": "python", 1242 | "pygments_lexer": "ipython3", 1243 | "version": "3.7.6" 1244 | } 1245 | }, 1246 | "nbformat": 4, 1247 | "nbformat_minor": 2 1248 | } 1249 | -------------------------------------------------------------------------------- /Day01-15/day10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Try the given example 1\n", 10 | "import tkinter\n", 11 | "import tkinter.messagebox\n", 12 | "\n", 13 | "\n", 14 | "def main():\n", 15 | " flag = True\n", 16 | "\n", 17 | " # 修改标签上的文字\n", 18 | " def change_label_text():\n", 19 | " nonlocal flag\n", 20 | " flag = not flag\n", 21 | " color, msg = ('red', 'Hello, world!')\\\n", 22 | " if flag else ('blue', 'Goodbye, world!')\n", 23 | " label.config(text=msg, fg=color)\n", 24 | "\n", 25 | " # 确认退出\n", 26 | " def confirm_to_quit():\n", 27 | " if tkinter.messagebox.askokcancel('温馨提示', '确定要退出吗?'):\n", 28 | " top.quit()\n", 29 | "\n", 30 | " # 创建顶层窗口\n", 31 | " top = tkinter.Tk()\n", 32 | " # 设置窗口大小\n", 33 | " top.geometry('240x160')\n", 34 | " # 设置窗口标题\n", 35 | " top.title('小游戏')\n", 36 | " # 创建标签对象并添加到顶层窗口\n", 37 | " label = tkinter.Label(top, text='Hello, world!', font='Arial -32', fg='red')\n", 38 | " label.pack(expand=1)\n", 39 | " # 创建一个装按钮的容器\n", 40 | " panel = tkinter.Frame(top)\n", 41 | " # 创建按钮对象 指定添加到哪个容器中 通过command参数绑定事件回调函数\n", 42 | " button1 = tkinter.Button(panel, text='修改', command=change_label_text)\n", 43 | " button1.pack(side='left')\n", 44 | " button2 = tkinter.Button(panel, text='退出', command=confirm_to_quit)\n", 45 | " button2.pack(side='right')\n", 46 | " panel.pack(side='bottom')\n", 47 | " # 开启主事件循环\n", 48 | " tkinter.mainloop()\n", 49 | "\n", 50 | "\n", 51 | "# if __name__ == '__main__':\n", 52 | "# main()" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 2, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "pygame 1.9.6\n", 65 | "Hello from the pygame community. https://www.pygame.org/contribute.html\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "# Try the given example 2\n", 71 | "\n", 72 | "from enum import Enum, unique\n", 73 | "from math import sqrt\n", 74 | "from random import randint\n", 75 | "\n", 76 | "import pygame\n", 77 | "\n", 78 | "\n", 79 | "@unique\n", 80 | "class Color(Enum):\n", 81 | " \"\"\"颜色\"\"\"\n", 82 | "\n", 83 | " RED = (255, 0, 0)\n", 84 | " GREEN = (0, 255, 0)\n", 85 | " BLUE = (0, 0, 255)\n", 86 | " BLACK = (0, 0, 0)\n", 87 | " WHITE = (255, 255, 255)\n", 88 | " GRAY = (242, 242, 242)\n", 89 | "\n", 90 | " @staticmethod\n", 91 | " def random_color():\n", 92 | " \"\"\"获得随机颜色\"\"\"\n", 93 | " r = randint(0, 255)\n", 94 | " g = randint(0, 255)\n", 95 | " b = randint(0, 255)\n", 96 | " return (r, g, b)\n", 97 | "\n", 98 | "\n", 99 | "class Ball(object):\n", 100 | " \"\"\"球\"\"\"\n", 101 | "\n", 102 | " def __init__(self, x, y, radius, sx, sy, color=Color.RED):\n", 103 | " \"\"\"初始化方法\"\"\"\n", 104 | " self.x = x\n", 105 | " self.y = y\n", 106 | " self.radius = radius\n", 107 | " self.sx = sx\n", 108 | " self.sy = sy\n", 109 | " self.color = color\n", 110 | " self.alive = True\n", 111 | "\n", 112 | " def move(self, screen):\n", 113 | " \"\"\"移动\"\"\"\n", 114 | " self.x += self.sx\n", 115 | " self.y += self.sy\n", 116 | " if self.x - self.radius <= 0 or \\\n", 117 | " self.x + self.radius >= screen.get_width():\n", 118 | " self.sx = -self.sx\n", 119 | " if self.y - self.radius <= 0 or \\\n", 120 | " self.y + self.radius >= screen.get_height():\n", 121 | " self.sy = -self.sy\n", 122 | "\n", 123 | " def eat(self, other):\n", 124 | " \"\"\"吃其他球\"\"\"\n", 125 | " if self.alive and other.alive and self != other:\n", 126 | " dx, dy = self.x - other.x, self.y - other.y\n", 127 | " distance = sqrt(dx ** 2 + dy ** 2)\n", 128 | " if distance < self.radius + other.radius \\\n", 129 | " and self.radius > other.radius:\n", 130 | " other.alive = False\n", 131 | " self.radius = self.radius + int(other.radius * 0.146)\n", 132 | "\n", 133 | " def draw(self, screen):\n", 134 | " \"\"\"在窗口上绘制球\"\"\"\n", 135 | " pygame.draw.circle(screen, self.color,\n", 136 | " (self.x, self.y), self.radius, 0)\n", 137 | " \n", 138 | "def main():\n", 139 | " # 定义用来装所有球的容器\n", 140 | " balls = []\n", 141 | " # 初始化导入的pygame中的模块\n", 142 | " pygame.init()\n", 143 | " # 初始化用于显示的窗口并设置窗口尺寸\n", 144 | " screen = pygame.display.set_mode((800, 600))\n", 145 | " # 设置当前窗口的标题\n", 146 | " pygame.display.set_caption('大球吃小球')\n", 147 | " running = True\n", 148 | " # 开启一个事件循环处理发生的事件\n", 149 | " while running:\n", 150 | " # 从消息队列中获取事件并对事件进行处理\n", 151 | " for event in pygame.event.get():\n", 152 | " if event.type == pygame.QUIT:\n", 153 | " running = False\n", 154 | " # 处理鼠标事件的代码\n", 155 | " if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:\n", 156 | " # 获得点击鼠标的位置\n", 157 | " x, y = event.pos\n", 158 | " radius = randint(10, 100)\n", 159 | " sx, sy = randint(-10, 10), randint(-10, 10)\n", 160 | " color = Color.random_color()\n", 161 | " # 在点击鼠标的位置创建一个球(大小、速度和颜色随机)\n", 162 | " ball = Ball(x, y, radius, sx, sy, color)\n", 163 | " # 将球添加到列表容器中\n", 164 | " balls.append(ball)\n", 165 | " screen.fill((255, 255, 255))\n", 166 | " # 取出容器中的球 如果没被吃掉就绘制 被吃掉了就移除\n", 167 | " for ball in balls:\n", 168 | " if ball.alive:\n", 169 | " ball.draw(screen)\n", 170 | " else:\n", 171 | " balls.remove(ball)\n", 172 | " pygame.display.flip()\n", 173 | " # 每隔50毫秒就改变球的位置再刷新窗口\n", 174 | " pygame.time.delay(50)\n", 175 | " for ball in balls:\n", 176 | " ball.move(screen)\n", 177 | " # 检查球有没有吃到其他的球\n", 178 | " for other in balls:\n", 179 | " ball.eat(other)\n", 180 | "\n", 181 | "\n", 182 | "# if __name__ == '__main__':\n", 183 | "# main()" 184 | ] 185 | } 186 | ], 187 | "metadata": { 188 | "kernelspec": { 189 | "display_name": "Python 3", 190 | "language": "python", 191 | "name": "python3" 192 | }, 193 | "language_info": { 194 | "codemirror_mode": { 195 | "name": "ipython", 196 | "version": 3 197 | }, 198 | "file_extension": ".py", 199 | "mimetype": "text/x-python", 200 | "name": "python", 201 | "nbconvert_exporter": "python", 202 | "pygments_lexer": "ipython3", 203 | "version": "3.7.6" 204 | } 205 | }, 206 | "nbformat": 4, 207 | "nbformat_minor": 4 208 | } 209 | -------------------------------------------------------------------------------- /Day01-15/day11.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Use try/except\n", 10 | "def main():\n", 11 | " f = None\n", 12 | " try:\n", 13 | " f = open('abc.txt', 'r', encoding='utf-8')\n", 14 | " except FileNotFoundError:\n", 15 | " print('Cannot find the specified file.')\n", 16 | " except UnicodeDecodeError:\n", 17 | " print('Fail to decode.')\n", 18 | " finally:\n", 19 | " if f:\n", 20 | " f.close()\n", 21 | " \n", 22 | "if __name__ == '__main__':\n", 23 | " main()" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "line1 line1 line1\n", 36 | "line2 line2 line2\n", 37 | "line3 line3 line3\n", 38 | "line4 line4 line4\n", 39 | "line5 line5 line5\n", 40 | "line6 line6 line6\n", 41 | "line7 line7 line7\n", 42 | "line8 line8 line8\n", 43 | "line9 line9 line9\n", 44 | "line1 line1 line1\n", 45 | "line2 line2 line2\n", 46 | "line3 line3 line3\n", 47 | "line4 line4 line4\n", 48 | "line5 line5 line5\n", 49 | "line6 line6 line6\n", 50 | "line7 line7 line7\n", 51 | "line8 line8 line8\n", 52 | "line9 line9 line9\n", 53 | "['line1 line1 line1\\n', 'line2 line2 line2\\n', 'line3 line3 line3\\n', 'line4 line4 line4\\n', 'line5 line5 line5\\n', 'line6 line6 line6\\n', 'line7 line7 line7\\n', 'line8 line8 line8\\n', 'line9 line9 line9']\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "# Try the given example 1\n", 59 | "# Read in by lines\n", 60 | "import time\n", 61 | "\n", 62 | "def main():\n", 63 | " with open('abc.txt', 'r', encoding='utf-8') as f:\n", 64 | " print(f.read())\n", 65 | "\n", 66 | " with open('abc.txt', mode='r') as f:\n", 67 | " for line in f:\n", 68 | " print(line, end='')\n", 69 | " time.sleep(0.5)\n", 70 | " print()\n", 71 | "\n", 72 | " with open('abc.txt') as f:\n", 73 | " lines = f.readlines()\n", 74 | " print(lines)\n", 75 | " \n", 76 | "if __name__ == '__main__':\n", 77 | " main()" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "Writing finished\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "# Try the given example 2\n", 95 | "# Write prime numbers from 1-9999 into three different files\n", 96 | "from math import sqrt\n", 97 | "\n", 98 | "def is_prime(n):\n", 99 | " assert n > 0\n", 100 | " for factor in range(2, int(sqrt(n)) + 1):\n", 101 | " if n % factor == 0:\n", 102 | " return False\n", 103 | " return True if n != 1 else False\n", 104 | "\n", 105 | "\n", 106 | "def main():\n", 107 | " filenames = ('a.txt', 'b.txt', 'c.txt')\n", 108 | " fs_list = []\n", 109 | " try:\n", 110 | " for filename in filenames:\n", 111 | " fs_list.append(open(filename, 'w', encoding='utf-8'))\n", 112 | " for number in range(1, 10000):\n", 113 | " if is_prime(number):\n", 114 | " if number < 100:\n", 115 | " fs_list[0].write(str(number) + ' ')\n", 116 | " elif number < 1000:\n", 117 | " fs_list[1].write(str(number) + ' ')\n", 118 | " else:\n", 119 | " fs_list[2].write(str(number) + ' ')\n", 120 | " print('Writing finished')\n", 121 | " except:\n", 122 | " print('Error occurs!')\n", 123 | " finally:\n", 124 | " for fs in fs_list:\n", 125 | " fs.close()\n", 126 | "\n", 127 | "if __name__ == '__main__':\n", 128 | " main()" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 4, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "Cannot open the specified file.\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "# Try the given example 3\n", 146 | "# Read in binary data\n", 147 | "def main():\n", 148 | " try:\n", 149 | " with open('read.jpg', 'rb') as f1:\n", 150 | " data = f1.read()\n", 151 | " with open('write.jpg', 'wb') as f2:\n", 152 | " f2.write(data)\n", 153 | " except FileNotFoundError:\n", 154 | " print('Cannot open the specified file.')\n", 155 | " except IOError:\n", 156 | " print('Error while wrting.')\n", 157 | " \n", 158 | "if __name__ == '__main__':\n", 159 | " main()" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 5, 165 | "metadata": {}, 166 | "outputs": [], 167 | "source": [ 168 | "# Try the given example 4\n", 169 | "# Write JSON data\n", 170 | "import json\n", 171 | "dict1 = {\n", 172 | " 'a1': 1,\n", 173 | " 'b2': 'abc',\n", 174 | " 'c3': ['a', 'b', 'c'],\n", 175 | " 'd4': [\n", 176 | " {'aa1': 1},\n", 177 | " {'aa2': 2}\n", 178 | " ],\n", 179 | " 'e5': [1, 2, 3, 4, 5]\n", 180 | "}\n", 181 | "\n", 182 | "with open('tets.json', 'w', encoding='utf-8') as f1:\n", 183 | " json.dump(dict1, f1)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 6, 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "name": "stdout", 193 | "output_type": "stream", 194 | "text": [ 195 | "\n", 196 | "\n", 197 | "\n", 198 | "{'code': 230, 'msg': 'key错误或为空'}\n" 199 | ] 200 | } 201 | ], 202 | "source": [ 203 | "# Try the given example 5\n", 204 | "# Use the requests library for HTTP\n", 205 | "import json\n", 206 | "import requests\n", 207 | "\n", 208 | "def main():\n", 209 | " data = requests.get('http://api.tianapi.com/guonei/?key=APIKey&num=10')\n", 210 | " data_loaded = json.loads(data.text)\n", 211 | " print(type(data))\n", 212 | " print(data)\n", 213 | " print(type(data_loaded))\n", 214 | " print(data_loaded)\n", 215 | " \n", 216 | "if __name__ == '__main__':\n", 217 | " main()" 218 | ] 219 | } 220 | ], 221 | "metadata": { 222 | "kernelspec": { 223 | "display_name": "Python 3", 224 | "language": "python", 225 | "name": "python3" 226 | }, 227 | "language_info": { 228 | "codemirror_mode": { 229 | "name": "ipython", 230 | "version": 3 231 | }, 232 | "file_extension": ".py", 233 | "mimetype": "text/x-python", 234 | "name": "python", 235 | "nbconvert_exporter": "python", 236 | "pygments_lexer": "ipython3", 237 | "version": "3.7.6" 238 | } 239 | }, 240 | "nbformat": 4, 241 | "nbformat_minor": 4 242 | } 243 | -------------------------------------------------------------------------------- /Day01-15/day12.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "enter the username: chziha\n", 13 | "enter the qq name: 038301176\n", 14 | "Try another qq name.\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "# Try the example 1\n", 20 | "# Check if the user name follows the rule:\n", 21 | "# only a-Z, 0-9, _, between 6-20 characters\n", 22 | "# and if the QQ name follows the rule:\n", 23 | "# only 0-9, between 5-12 characters, not starting with 0\n", 24 | "\n", 25 | "import re\n", 26 | "\n", 27 | "def is_username_qq():\n", 28 | " username = input('enter the username: ')\n", 29 | " qq = input('enter the qq name: ')\n", 30 | " match1 = re.fullmatch(r'[0-9a-zA-Z-]{6,20}', username)\n", 31 | " match2 = re.fullmatch(r'^[1-9][0-9]{4,11}', qq)\n", 32 | " if not match1:\n", 33 | " print('Try another user name.')\n", 34 | " if not match2:\n", 35 | " print('Try another qq name.')\n", 36 | " \n", 37 | "if __name__ == '__main__':\n", 38 | " is_username_qq()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 2, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "13512346789\n", 51 | "15600998765\n", 52 | "15600998765\n" 53 | ] 54 | } 55 | ], 56 | "source": [ 57 | "# Try the example 2\n", 58 | "import re\n", 59 | "\n", 60 | "def capture_phone_number():\n", 61 | " pattern = re.compile(r'(?<=\\D)1[34578]\\d{9}(?=\\D)')\n", 62 | " example = '''\n", 63 | " 重要的事情说8130123456789遍,我的手机号是13512346789这个靓号,\n", 64 | " 不是15600998765,也是110或119,王大锤的手机号才是15600998765。\n", 65 | " '''\n", 66 | " for match in re.finditer(pattern, example):\n", 67 | " print(match.group())\n", 68 | " \n", 69 | "if __name__ == '__main__':\n", 70 | " capture_phone_number()" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 3, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "13512346789\n", 83 | "15600998765\n", 84 | "15600998765\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "# Example 2\n", 90 | "import re\n", 91 | "\n", 92 | "def capture_phone_number():\n", 93 | " pattern = re.compile(r'(?<=\\D)1[34578]\\d{9}(?=\\D)')\n", 94 | " example = '''\n", 95 | " 重要的事情说8130123456789遍,我的手机号是13512346789这个靓号,\n", 96 | " 不是15600998765,也是110或119,王大锤的手机号才是15600998765。\n", 97 | " '''\n", 98 | " for m in re.findall(pattern, example):\n", 99 | " print(m) \n", 100 | " \n", 101 | "if __name__ == '__main__':\n", 102 | " capture_phone_number()" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 4, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "13512346789\n", 115 | "15600998765\n", 116 | "15600998765\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "# Example 2\n", 122 | "import re\n", 123 | "\n", 124 | "def capture_phone_number():\n", 125 | " pattern = re.compile(r'(?<=\\D)1[34578]\\d{9}(?=\\D)')\n", 126 | " example = '''\n", 127 | " 重要的事情说8130123456789遍,我的手机号是13512346789这个靓号,\n", 128 | " 不是15600998765,也是110或119,王大锤的手机号才是15600998765。\n", 129 | " '''\n", 130 | " m = re.search(pattern, example)\n", 131 | " while m:\n", 132 | " print(m.group())\n", 133 | " m = pattern.search(example, m.end()) # m.end() returns index of the end of the substring matched by group\n", 134 | " \n", 135 | "if __name__ == '__main__':\n", 136 | " capture_phone_number()" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 5, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "***defg***kL*****rstuvwxy*\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "# Try the example 3\n", 154 | "# Replace\n", 155 | "\n", 156 | "import re\n", 157 | "\n", 158 | "\n", 159 | "def replace_with_asterisk():\n", 160 | " example = 'abCdefghIjkLmnopqrstuvwxyZ'\n", 161 | " replaced = re.sub('[abc]|h|i|j|[mnopq]|z',\n", 162 | " '*', example, flags=re.IGNORECASE)\n", 163 | " print(replaced)\n", 164 | "\n", 165 | "\n", 166 | "if __name__ == '__main__':\n", 167 | " replace_with_asterisk()" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 6, 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "床前明月光\n", 180 | "疑是地上霜\n", 181 | "举头望明月\n", 182 | "低头思故乡\n", 183 | "\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "# Try the example 4\n", 189 | "# Split\n", 190 | "\n", 191 | "import re\n", 192 | "\n", 193 | "def split_poem_sentence():\n", 194 | " poem = '床前明月光,疑是地上霜。举头望明月,低头思故乡。'\n", 195 | " sentences = re.split(r'[,。, .]', poem)\n", 196 | " for s in sentences:\n", 197 | " print(s)\n", 198 | "\n", 199 | "if __name__ == '__main__':\n", 200 | " split_poem_sentence()" 201 | ] 202 | } 203 | ], 204 | "metadata": { 205 | "kernelspec": { 206 | "display_name": "Python 3", 207 | "language": "python", 208 | "name": "python3" 209 | }, 210 | "language_info": { 211 | "codemirror_mode": { 212 | "name": "ipython", 213 | "version": 3 214 | }, 215 | "file_extension": ".py", 216 | "mimetype": "text/x-python", 217 | "name": "python", 218 | "nbconvert_exporter": "python", 219 | "pygments_lexer": "ipython3", 220 | "version": "3.7.6" 221 | } 222 | }, 223 | "nbformat": 4, 224 | "nbformat_minor": 4 225 | } 226 | -------------------------------------------------------------------------------- /Day01-15/day13.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "PID is 8028.\n", 15 | "time for aaa is 2 s.\n", 16 | "PID is 8028.\n", 17 | "time for bbb is 3 s.\n", 18 | "total time is 5.00 s.\n", 19 | "13188\n", 20 | "16108\n", 21 | "total time is 0.12 s.\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "# Effect of multiprocessing\n", 27 | "\n", 28 | "import random\n", 29 | "import time\n", 30 | "from multiprocessing import Process\n", 31 | "from os import getpid\n", 32 | "\n", 33 | "def count_time(a_str):\n", 34 | " time_elapse = random.randint(1, 5)\n", 35 | " print('PID is %d.' % getpid())\n", 36 | " time.sleep(time_elapse)\n", 37 | " print('time for %s is %d s.' % (a_str, time_elapse))\n", 38 | " \n", 39 | "def main():\n", 40 | " t0 = time.time()\n", 41 | " count_time('aaa')\n", 42 | " count_time('bbb')\n", 43 | " print('total time is %.2f s.' % (time.time() - t0))\n", 44 | " \n", 45 | "# Multiprocessing\n", 46 | "def main_multi():\n", 47 | " t0 = time.time()\n", 48 | " p1 = Process(target=count_time, args=('aaa', ))\n", 49 | " p2 = Process(target=count_time, args=('bbb', ))\n", 50 | " p1.start()\n", 51 | " p2.start()\n", 52 | " p1.join()\n", 53 | " p2.join()\n", 54 | " print(p1.pid)\n", 55 | " print(p2.pid)\n", 56 | " print('total time is %.2f s.' % (time.time() - t0))\n", 57 | "\n", 58 | "if __name__ == '__main__':\n", 59 | " main()\n", 60 | " main_multi()" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 2, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "# Try the example 2\n", 70 | "from multiprocessing import Process\n", 71 | "from time import sleep\n", 72 | "\n", 73 | "counter = 0\n", 74 | "\n", 75 | "\n", 76 | "def sub_task(string):\n", 77 | " global counter\n", 78 | " while counter < 10:\n", 79 | " print(string, end='', flush=True)\n", 80 | " counter += 1\n", 81 | " sleep(0.01)\n", 82 | "\n", 83 | "\n", 84 | "# Have a counter variable in each sub-process\n", 85 | "# 10 'Ping' and 10 'Pong'\n", 86 | "# Use Queue\n", 87 | "\n", 88 | "def main():\n", 89 | " Process(target=sub_task, args=('Ping', )).start()\n", 90 | " Process(target=sub_task, args=('Pong', )).start()\n", 91 | "\n", 92 | "if __name__ == '__main__':\n", 93 | " main()" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 3, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "PID is 8028.\n", 106 | "PID is 8028.\n", 107 | "time for aaa is 1 s.\n", 108 | "time for bbb is 2 s.\n", 109 | "total time is 2.01 s.\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "# Effect of multithreading\n", 115 | "\n", 116 | "import random\n", 117 | "import time\n", 118 | "from threading import Thread\n", 119 | "from os import getpid\n", 120 | "\n", 121 | "# Define a class inherited from Thread\n", 122 | "class count_time(Thread):\n", 123 | " def __init__(self, a_str):\n", 124 | " super().__init__()\n", 125 | " self._name = a_str\n", 126 | " \n", 127 | " def run(self):\n", 128 | " time_elapse = random.randint(1, 5)\n", 129 | " print('PID is %d.' % getpid())\n", 130 | " time.sleep(time_elapse)\n", 131 | " print('time for %s is %d s.' % (self._name, time_elapse))\n", 132 | "\n", 133 | "# Multithreading\n", 134 | "def main():\n", 135 | " t0 = time.time()\n", 136 | " t1 = count_time('aaa')\n", 137 | " t2 = count_time('bbb')\n", 138 | " t1.start()\n", 139 | " t2.start()\n", 140 | " t1.join()\n", 141 | " t2.join()\n", 142 | " print('total time is %.2f s.' % (time.time() - t0))\n", 143 | " \n", 144 | "if __name__ == '__main__':\n", 145 | " main()" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 4, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "total money in the account is 100.\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "# Use of Lock in multithreading\n", 163 | "import time\n", 164 | "from threading import Thread, Lock\n", 165 | "\n", 166 | "class Account(object):\n", 167 | " def __init__(self):\n", 168 | " self._balance = 0\n", 169 | " self._lock = Lock()\n", 170 | " \n", 171 | " def deposit(self, money):\n", 172 | " # Once a thread has acquired it, subsequent attempts to acquire it block, until it is released\n", 173 | " self._lock.acquire()\n", 174 | " try:\n", 175 | " new_balance = self._balance + money\n", 176 | " time.sleep(0.001)\n", 177 | " self._balance = new_balance\n", 178 | " # Make sure released in the finally block\n", 179 | " finally:\n", 180 | " self._lock.release()\n", 181 | " \n", 182 | " @property\n", 183 | " def balance(self):\n", 184 | " return self._balance\n", 185 | " \n", 186 | "class deposit_threads(Thread):\n", 187 | " def __init__(self, account, money):\n", 188 | " super().__init__()\n", 189 | " self._account = account\n", 190 | " self._money = money\n", 191 | " \n", 192 | " def run(self):\n", 193 | " self._account.deposit(self._money)\n", 194 | " \n", 195 | "def main():\n", 196 | " account = Account()\n", 197 | " threads = []\n", 198 | " for i in range(100):\n", 199 | " t = deposit_threads(account, 1)\n", 200 | " threads.append(t)\n", 201 | " t.start()\n", 202 | " for t in threads:\n", 203 | " t.join()\n", 204 | " print('total money in the account is %d.' % account.balance)\n", 205 | " \n", 206 | "if __name__ == '__main__':\n", 207 | " main()" 208 | ] 209 | } 210 | ], 211 | "metadata": { 212 | "kernelspec": { 213 | "display_name": "Python 3", 214 | "language": "python", 215 | "name": "python3" 216 | }, 217 | "language_info": { 218 | "codemirror_mode": { 219 | "name": "ipython", 220 | "version": 3 221 | }, 222 | "file_extension": ".py", 223 | "mimetype": "text/x-python", 224 | "name": "python", 225 | "nbconvert_exporter": "python", 226 | "pygments_lexer": "ipython3", 227 | "version": "3.7.6" 228 | } 229 | }, 230 | "nbformat": 4, 231 | "nbformat_minor": 4 232 | } 233 | -------------------------------------------------------------------------------- /Day01-15/day14.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "# Try the example of using socket\n", 10 | "from socket import socket, SOCK_STREAM, AF_INET\n", 11 | "from datetime import datetime\n", 12 | "\n", 13 | "\n", 14 | "def main():\n", 15 | " # 1.创建套接字对象并指定使用哪种传输服务\n", 16 | " # family=AF_INET - IPv4地址\n", 17 | " # family=AF_INET6 - IPv6地址\n", 18 | " # type=SOCK_STREAM - TCP套接字\n", 19 | " # type=SOCK_DGRAM - UDP套接字\n", 20 | " # type=SOCK_RAW - 原始套接字\n", 21 | " server = socket(family=AF_INET, type=SOCK_STREAM)\n", 22 | " # 2.绑定IP地址和端口(端口用于区分不同的服务)\n", 23 | " # 同一时间在同一个端口上只能绑定一个服务否则报错\n", 24 | " server.bind(('192.168.1.2', 6789))\n", 25 | " # 3.开启监听 - 监听客户端连接到服务器\n", 26 | " # 参数512可以理解为连接队列的大小\n", 27 | " server.listen(512)\n", 28 | " print('服务器启动开始监听...')\n", 29 | " while True:\n", 30 | " # 4.通过循环接收客户端的连接并作出相应的处理(提供服务)\n", 31 | " # accept方法是一个阻塞方法如果没有客户端连接到服务器代码不会向下执行\n", 32 | " # accept方法返回一个元组其中的第一个元素是客户端对象\n", 33 | " # 第二个元素是连接到服务器的客户端的地址(由IP和端口两部分构成)\n", 34 | " client, addr = server.accept()\n", 35 | " print(str(addr) + '连接到了服务器.')\n", 36 | " # 5.发送数据\n", 37 | " client.send(str(datetime.now()).encode('utf-8'))\n", 38 | " # 6.断开连接\n", 39 | " client.close()\n", 40 | "\n", 41 | "if __name__ == '__main__':\n", 42 | " main()" 43 | ] 44 | } 45 | ], 46 | "metadata": { 47 | "kernelspec": { 48 | "display_name": "Python 3", 49 | "language": "python", 50 | "name": "python3" 51 | }, 52 | "language_info": { 53 | "codemirror_mode": { 54 | "name": "ipython", 55 | "version": 3 56 | }, 57 | "file_extension": ".py", 58 | "mimetype": "text/x-python", 59 | "name": "python", 60 | "nbconvert_exporter": "python", 61 | "pygments_lexer": "ipython3", 62 | "version": "3.7.6" 63 | } 64 | }, 65 | "nbformat": 4, 66 | "nbformat_minor": 4 67 | } 68 | -------------------------------------------------------------------------------- /Day01-15/test.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chziha/Python-100-Days-Works/af232845c77947626f760c6f10bb4a1ed31ab1dc/Day01-15/test.PNG -------------------------------------------------------------------------------- /Day01-15/tets.json: -------------------------------------------------------------------------------- 1 | {"a1": 1, "b2": "abc", "c3": ["a", "b", "c"], "d4": [{"aa1": 1}, {"aa2": 2}], "e5": [1, 2, 3, 4, 5]} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-100-Days-Works 2 | Works toward Python-100-Days by jackfrued (骆昊) 3 | 4 | ## Motivation 5 | Consolidating skills in Python through working toward the project Python-100-Days 6 | 7 | ## File Description 8 | The Jupyter notebooks named by 'day' plus the number of the day are the works to the examples/problems given in the tutorials. 9 | 10 | ## Authors & Acknowledgements 11 | All the tutorials are created by jackfrued (骆昊) as in the project named [“Python-100-Days”](). 12 | Thanks for jackfrued's great help for sharing these. 13 | --------------------------------------------------------------------------------