├── exercise_files ├── 11_02_b │ ├── useModule.py │ ├── usePackage.py │ ├── numbers │ │ ├── __init__.py │ │ ├── factors.py │ │ └── primes.py │ └── primes.py ├── 11_02_e │ ├── numbers │ │ ├── __init__.py │ │ ├── factors.py │ │ ├── __pycache__ │ │ │ ├── factors.cpython-310.pyc │ │ │ └── __init__.cpython-310.pyc │ │ └── primes.py │ ├── useModule.py │ ├── usePackage.py │ ├── __pycache__ │ │ └── primes.cpython-310.pyc │ └── primes.py ├── somefile.txt ├── hello.py ├── 01_04 │ └── hello.py ├── .ipynb_checkpoints │ ├── 03_06_Challenge-checkpoint.ipynb │ ├── 04_06_Challenge-checkpoint.ipynb │ ├── 02_05_Functions-checkpoint.ipynb │ ├── 04_04_List_Comprehensions-checkpoint.ipynb │ ├── 03_05_bytes-checkpoint.ipynb │ ├── 04_05_Dictionary_Comprehensions-checkpoint.ipynb │ ├── 04_02_Tuples_and_sets-checkpoint.ipynb │ ├── 04_03_Dictionaries-checkpoint.ipynb │ ├── 04_01_lists-checkpoint.ipynb │ ├── 03_03_booleans-checkpoint.ipynb │ ├── 03_02_Other_Numbers-checkpoint.ipynb │ ├── 06_01_AnatomyOfAFunction-checkpoint.ipynb │ ├── 01_05_JupyterNotebook-checkpoint.ipynb │ ├── 10_01_opening_reading_writing_files-checkpoint.ipynb │ ├── 10_03_json-checkpoint.ipynb │ ├── 05_04_Challenge-checkpoint.ipynb │ ├── 06_04_Challenge-checkpoint.ipynb │ ├── 05_01_IfElseIfelse-checkpoint.ipynb │ ├── 02_06_Classes_and_Instances-checkpoint.ipynb │ ├── 07_02_StaticMethods-checkpoint.ipynb │ ├── 03_01_Ints_and_Floats-checkpoint.ipynb │ ├── 07_04_Challenge-checkpoint.ipynb │ ├── 02_07_Challenge-checkpoint.ipynb │ ├── 10_04_Challenge-checkpoint.ipynb │ ├── 02_08_Solution-checkpoint.ipynb │ ├── 05_04_Challenge_Hints-checkpoint.ipynb │ ├── 07_05_Solution-checkpoint.ipynb │ ├── 08_04_Challenge-checkpoint.ipynb │ ├── 02_04_ControlFlow-checkpoint.ipynb │ ├── 07_03_ClassInheritance-checkpoint.ipynb │ ├── 09_02_Threads-checkpoint.ipynb │ ├── 02_07_Challenge_Hints-checkpoint.ipynb │ ├── 08_03_CustomExceptions-checkpoint.ipynb │ ├── 08_01_Errors_and_Exceptions-checkpoint.ipynb │ ├── 09_03_Processes-checkpoint.ipynb │ ├── 07_04_Challenge_Hints-checkpoint.ipynb │ └── 10_04_Challenge_Hints-checkpoint.ipynb ├── 11_1_writefile.py ├── 10_04_challenge_art_encoded.aa ├── 10_01_file.txt ├── 10_04_challenge_art_encoded.txt ├── 01_05_JupyterNotebook.ipynb ├── 05_04_Challenge.ipynb ├── 10_02_ma_prime.csv ├── 02_06_Classes_and_Instances.ipynb ├── 10_04_challenge_art.txt ├── 02_07_Challenge.ipynb ├── 07_04_Challenge.ipynb ├── 03_05_bytes.ipynb ├── 10_04_Challenge.ipynb ├── 04_05_Dictionary_Comprehensions.ipynb ├── 09_03_Processes.ipynb ├── 03_06_Challenge.ipynb ├── 10_03_json.ipynb ├── 02_04_ControlFlow.ipynb ├── 08_04_Challenge.ipynb ├── 05_04_Challenge_Hints.ipynb ├── 07_03_ClassInheritance.ipynb ├── 09_02_threads.ipynb ├── 07_05_Solution.ipynb ├── 02_05_Functions.ipynb ├── 08_03_CustomExceptions.ipynb ├── 08_01_Errors_and_Exceptions.ipynb ├── 02_08_Solution.ipynb ├── 07_02_StaticMethods.ipynb ├── 10_04_Challenge_Hints.ipynb └── 04_06_Challenge.ipynb ├── .gitignore ├── favorite_python_resources.pdf ├── .github ├── CODEOWNERS ├── PULL_REQUEST_TEMPLATE.md ├── workflows │ └── main.yml └── ISSUE_TEMPLATE.md ├── installation_troubleshooting.pdf ├── CONTRIBUTING.md ├── NOTICE └── README.md /exercise_files/11_02_b/useModule.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /exercise_files/11_02_b/usePackage.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /exercise_files/11_02_b/numbers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /exercise_files/11_02_e/numbers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /exercise_files/somefile.txt: -------------------------------------------------------------------------------- 1 | some text to write to the file 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .tmp 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /exercise_files/hello.py: -------------------------------------------------------------------------------- 1 | 2 | # Print "hello, world!" to the terminal 3 | print('Hello, World!') -------------------------------------------------------------------------------- /exercise_files/01_04/hello.py: -------------------------------------------------------------------------------- 1 | 2 | # Print "Hello, World!" to the terminal 3 | print('Hello, World!') -------------------------------------------------------------------------------- /exercise_files/11_02_e/useModule.py: -------------------------------------------------------------------------------- 1 | from primes import listPrimes 2 | 3 | 4 | print(listPrimes(100)) -------------------------------------------------------------------------------- /exercise_files/11_02_e/usePackage.py: -------------------------------------------------------------------------------- 1 | from numbers.factors import getFactors 2 | 3 | 4 | print(getFactors(100)) -------------------------------------------------------------------------------- /exercise_files/11_02_b/numbers/factors.py: -------------------------------------------------------------------------------- 1 | 2 | def getFactors(n): 3 | return [factor for factor in range(1, n+1) if n % factor == 0] 4 | -------------------------------------------------------------------------------- /exercise_files/11_02_e/numbers/factors.py: -------------------------------------------------------------------------------- 1 | 2 | def getFactors(n): 3 | return [factor for factor in range(1, n+1) if n % factor == 0] 4 | -------------------------------------------------------------------------------- /favorite_python_resources.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-essential-training-4314028/HEAD/favorite_python_resources.pdf -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) denotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /installation_troubleshooting.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-essential-training-4314028/HEAD/installation_troubleshooting.pdf -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/03_06_Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 5 6 | } 7 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/04_06_Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 5 6 | } 7 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /exercise_files/11_02_e/__pycache__/primes.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-essential-training-4314028/HEAD/exercise_files/11_02_e/__pycache__/primes.cpython-310.pyc -------------------------------------------------------------------------------- /exercise_files/11_02_e/numbers/__pycache__/factors.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-essential-training-4314028/HEAD/exercise_files/11_02_e/numbers/__pycache__/factors.cpython-310.pyc -------------------------------------------------------------------------------- /exercise_files/11_02_e/numbers/__pycache__/__init__.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/python-essential-training-4314028/HEAD/exercise_files/11_02_e/numbers/__pycache__/__init__.cpython-310.pyc -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1.2 13 | env: 14 | key: main 15 | -------------------------------------------------------------------------------- /exercise_files/11_02_b/primes.py: -------------------------------------------------------------------------------- 1 | 2 | def isPrime(n, foundPrimes=None): 3 | foundPrimes = range(2, int(n**0.5)) if foundPrimes is None else foundPrimes 4 | for factor in foundPrimes: 5 | if n % factor == 0: 6 | return False 7 | return True 8 | 9 | def listPrimes(max): 10 | foundPrimes = [] 11 | for n in range(2, max): 12 | if isPrime(n, foundPrimes): 13 | foundPrimes.append(n) 14 | return foundPrimes 15 | -------------------------------------------------------------------------------- /exercise_files/11_02_b/numbers/primes.py: -------------------------------------------------------------------------------- 1 | 2 | def isPrime(n, foundPrimes=None): 3 | foundPrimes = range(2, int(n**0.5)) if foundPrimes is None else foundPrimes 4 | for factor in foundPrimes: 5 | if n % factor == 0: 6 | return False 7 | return True 8 | 9 | def listPrimes(max): 10 | foundPrimes = [] 11 | for n in range(2, max): 12 | if isPrime(n, foundPrimes): 13 | foundPrimes.append(n) 14 | return foundPrimes 15 | 16 | -------------------------------------------------------------------------------- /exercise_files/11_02_e/numbers/primes.py: -------------------------------------------------------------------------------- 1 | 2 | def isPrime(n, foundPrimes=None): 3 | foundPrimes = range(2, int(n**0.5)) if foundPrimes is None else foundPrimes 4 | for factor in foundPrimes: 5 | if n % factor == 0: 6 | return False 7 | return True 8 | 9 | def listPrimes(max): 10 | foundPrimes = [] 11 | for n in range(2, max): 12 | if isPrime(n, foundPrimes): 13 | foundPrimes.append(n) 14 | return foundPrimes 15 | 16 | -------------------------------------------------------------------------------- /exercise_files/11_1_writefile.py: -------------------------------------------------------------------------------- 1 | from argparse import ArgumentParser 2 | 3 | parser = ArgumentParser() 4 | 5 | parser.add_argument('--output', '-o', required=True, help='The destination file for the output of this program') 6 | parser.add_argument('--text', '-t', required=True, help='The text to write to the file') 7 | 8 | 9 | args = parser.parse_args() 10 | 11 | with open(args.output, 'w') as f: 12 | f.write(args.text+'\n') 13 | 14 | print(f'Wrote "{args.text}" to file "{args.output}"') -------------------------------------------------------------------------------- /exercise_files/11_02_e/primes.py: -------------------------------------------------------------------------------- 1 | 2 | def isPrime(n, foundPrimes=None): 3 | foundPrimes = range(2, int(n**0.5)) if foundPrimes is None else foundPrimes 4 | for factor in foundPrimes: 5 | if n % factor == 0: 6 | return False 7 | return True 8 | 9 | def listPrimes(max): 10 | foundPrimes = [] 11 | for n in range(2, max): 12 | if isPrime(n, foundPrimes): 13 | foundPrimes.append(n) 14 | return foundPrimes 15 | 16 | print(f'primes.py module name is {__name__}') -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2024 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | Please note, this project may automatically load third party code from external 8 | repositories (for example, NPM modules, Composer packages, or other dependencies). 9 | If so, such third party code may be subject to other license terms than as set 10 | forth above. In addition, such third party code may also depend on and load 11 | multiple tiers of dependencies. Please review the applicable licenses of the 12 | additional dependencies. 13 | -------------------------------------------------------------------------------- /exercise_files/10_04_challenge_art_encoded.aa: -------------------------------------------------------------------------------- 1 | 2 |  P 3 |  P 4 |  %  5 |  %!  6 |  % %  7 |  % #%  8 |  % )% 9 |  % /% 10 |  11 | % 3% 12 |  % 7%  13 |  % % % %  14 |  % % % %  15 |  % % % %  16 |  % % % %  17 |  % % % %  18 |  % C%  19 |  % C%  20 |  % C%  21 |  % 6% %  22 |  % % (% %  23 |  % % '% %  24 |  % % %% %  25 |  % % "% %  26 |  % % % %  27 |  % 28 | % % 29 | %  30 |  31 | % % % % 32 |  % % % 33 |  % (%  34 |  % !%  35 |  % %  36 |  %  37 |  #% ! 38 |  P 39 |  Q 40 |  -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/02_05_Functions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "dd1cfd33", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [] 10 | } 11 | ], 12 | "metadata": { 13 | "kernelspec": { 14 | "display_name": "Python 3 (ipykernel)", 15 | "language": "python", 16 | "name": "python3" 17 | }, 18 | "language_info": { 19 | "codemirror_mode": { 20 | "name": "ipython", 21 | "version": 3 22 | }, 23 | "file_extension": ".py", 24 | "mimetype": "text/x-python", 25 | "name": "python", 26 | "nbconvert_exporter": "python", 27 | "pygments_lexer": "ipython3", 28 | "version": "3.10.2" 29 | } 30 | }, 31 | "nbformat": 4, 32 | "nbformat_minor": 5 33 | } 34 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/04_04_List_Comprehensions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "e5e2d532", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [] 10 | } 11 | ], 12 | "metadata": { 13 | "kernelspec": { 14 | "display_name": "Python 3 (ipykernel)", 15 | "language": "python", 16 | "name": "python3" 17 | }, 18 | "language_info": { 19 | "codemirror_mode": { 20 | "name": "ipython", 21 | "version": 3 22 | }, 23 | "file_extension": ".py", 24 | "mimetype": "text/x-python", 25 | "name": "python", 26 | "nbconvert_exporter": "python", 27 | "pygments_lexer": "ipython3", 28 | "version": "3.10.2" 29 | } 30 | }, 31 | "nbformat": 4, 32 | "nbformat_minor": 5 33 | } 34 | -------------------------------------------------------------------------------- /exercise_files/10_01_file.txt: -------------------------------------------------------------------------------- 1 | Beautiful is better than ugly. 2 | Explicit is better than implicit. 3 | Simple is better than complex. 4 | Complex is better than complicated. 5 | Flat is better than nested. 6 | Sparse is better than dense. 7 | Readability counts. 8 | Special cases aren't special enough to break the rules. 9 | Although practicality beats purity. 10 | Errors should never pass silently. 11 | Unless explicitly silenced. 12 | In the face of ambiguity, refuse the temptation to guess. 13 | There should be one-- and preferably only one --obvious way to do it. 14 | Although that way may not be obvious at first unless you're Dutch. 15 | Now is better than never. 16 | Although never is often better than *right* now. 17 | If the implementation is hard to explain, it's a bad idea. 18 | If the implementation is easy to explain, it may be a good idea. 19 | Namespaces are one honking great idea -- let's do more of those! -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/03_05_bytes-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "45ea1fc6", 6 | "metadata": {}, 7 | "source": [ 8 | "### Bytes" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "66da8b7f", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "b" 19 | ] 20 | } 21 | ], 22 | "metadata": { 23 | "kernelspec": { 24 | "display_name": "Python 3 (ipykernel)", 25 | "language": "python", 26 | "name": "python3" 27 | }, 28 | "language_info": { 29 | "codemirror_mode": { 30 | "name": "ipython", 31 | "version": 3 32 | }, 33 | "file_extension": ".py", 34 | "mimetype": "text/x-python", 35 | "name": "python", 36 | "nbconvert_exporter": "python", 37 | "pygments_lexer": "ipython3", 38 | "version": "3.10.2" 39 | } 40 | }, 41 | "nbformat": 4, 42 | "nbformat_minor": 5 43 | } 44 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/04_05_Dictionary_Comprehensions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d86a6d06", 6 | "metadata": {}, 7 | "source": [ 8 | "## Dictionary Comprehensions" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "92d3299d", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "animalList = [('a', 'aardvark'), ('b', 'bear'), ('c', 'cat'), ('d', 'dog')]\n" 19 | ] 20 | } 21 | ], 22 | "metadata": { 23 | "kernelspec": { 24 | "display_name": "Python 3 (ipykernel)", 25 | "language": "python", 26 | "name": "python3" 27 | }, 28 | "language_info": { 29 | "codemirror_mode": { 30 | "name": "ipython", 31 | "version": 3 32 | }, 33 | "file_extension": ".py", 34 | "mimetype": "text/x-python", 35 | "name": "python", 36 | "nbconvert_exporter": "python", 37 | "pygments_lexer": "ipython3", 38 | "version": "3.10.2" 39 | } 40 | }, 41 | "nbformat": 4, 42 | "nbformat_minor": 5 43 | } 44 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /exercise_files/10_04_challenge_art_encoded.txt: -------------------------------------------------------------------------------- 1 | 2 | |1~ |80~ 3 | |1~ |80~ 4 | |1~ |31~%|19~ |30~ 5 | |1~ |24~%|33~ |23~ 6 | |1~ |20~%|8~ |25~%|8~ |19~ 7 | |1~ |16~%|7~ |35~%|6~ |16~ 8 | |1~ |14~%|6~ |41~%|6~ |13~ 9 | |1~ |11~%|6~ |47~%|5~ |11~ 10 | |1~ |10~%|5~ |51~%|5~ |9~ 11 | |1~ |8~%|5~ |55~%|5~ |7~ 12 | |1~ |7~%|4~ |17~%|5~ |14~%|5~ |18~%|4~ |6~ 13 | |1~ |6~%|4~ |17~%|7~ |12~%|7~ |18~%|4~ |5~ 14 | |1~ |5~%|4~ |18~%|7~ |12~%|7~ |19~%|4~ |4~ 15 | |1~ |4~%|4~ |19~%|7~ |12~%|7~ |20~%|4~ |3~ 16 | |1~ |4~%|4~ |20~%|5~ |14~%|5~ |21~%|4~ |3~ 17 | |1~ |3~%|4~ |67~%|4~ |2~ 18 | |1~ |3~%|4~ |67~%|4~ |2~ 19 | |1~ |3~%|4~ |67~%|4~ |2~ 20 | |1~ |3~%|4~ |54~%|4~ |8~%|4~ |3~ 21 | |1~ |4~%|4~ |7~%|6~ |40~%|5~ |7~%|4~ |3~ 22 | |1~ |4~%|4~ |9~%|4~ |39~%|4~ |8~%|4~ |4~ 23 | |1~ |5~%|4~ |9~%|4~ |37~%|4~ |9~%|4~ |4~ 24 | |1~ |6~%|4~ |9~%|5~ |34~%|4~ |9~%|4~ |5~ 25 | |1~ |7~%|5~ |9~%|5~ |29~%|5~ |9~%|5~ |6~ 26 | |1~ |8~%|5~ |10~%|6~ |24~%|5~ |10~%|4~ |8~ 27 | |1~ |10~%|5~ |11~%|7~ |15~%|7~ |11~%|5~ |9~ 28 | |1~ |12~%|5~ |13~%|21~ |13~%|5~ |11~ 29 | |1~ |14~%|7~ |40~%|5~ |14~ 30 | |1~ |17~%|7~ |33~%|7~ |16~ 31 | |1~ |21~%|9~ |21~%|9~ |20~ 32 | |1~ |26~%|29~ |25~ 33 | |1~ |35~%|12~ |33~ 34 | |1~ |80~ 35 | |1~ |81~ 36 | |2 -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/04_02_Tuples_and_sets-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "9ace3b36", 6 | "metadata": {}, 7 | "source": [ 8 | "## Sets" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "1bb9b937", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "f39e6aee", 22 | "metadata": {}, 23 | "source": [ 24 | "## Tuples" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "id": "2841cbc2", 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [] 34 | } 35 | ], 36 | "metadata": { 37 | "kernelspec": { 38 | "display_name": "Python 3 (ipykernel)", 39 | "language": "python", 40 | "name": "python3" 41 | }, 42 | "language_info": { 43 | "codemirror_mode": { 44 | "name": "ipython", 45 | "version": 3 46 | }, 47 | "file_extension": ".py", 48 | "mimetype": "text/x-python", 49 | "name": "python", 50 | "nbconvert_exporter": "python", 51 | "pygments_lexer": "ipython3", 52 | "version": "3.10.2" 53 | } 54 | }, 55 | "nbformat": 4, 56 | "nbformat_minor": 5 57 | } 58 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/04_03_Dictionaries-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "4c1e4f06", 6 | "metadata": {}, 7 | "source": [ 8 | "## Dictionaries" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "e1f38d37", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "534c6faa", 22 | "metadata": {}, 23 | "source": [ 24 | "### The Default Dict" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "id": "ddec03b3", 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [] 34 | } 35 | ], 36 | "metadata": { 37 | "kernelspec": { 38 | "display_name": "Python 3 (ipykernel)", 39 | "language": "python", 40 | "name": "python3" 41 | }, 42 | "language_info": { 43 | "codemirror_mode": { 44 | "name": "ipython", 45 | "version": 3 46 | }, 47 | "file_extension": ".py", 48 | "mimetype": "text/x-python", 49 | "name": "python", 50 | "nbconvert_exporter": "python", 51 | "pygments_lexer": "ipython3", 52 | "version": "3.10.2" 53 | } 54 | }, 55 | "nbformat": 4, 56 | "nbformat_minor": 5 57 | } 58 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/04_01_lists-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "911e1284", 6 | "metadata": {}, 7 | "source": [ 8 | "## Lists" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "0f1cafc2", 14 | "metadata": {}, 15 | "source": [ 16 | "### List Slicing" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "3cba56d9", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "ab8fb837", 30 | "metadata": {}, 31 | "source": [ 32 | "### Modifying Lists" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "id": "ebc6255d", 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "Python 3 (ipykernel)", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.10.2" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 5 65 | } 66 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/03_03_booleans-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b0b4ced9", 6 | "metadata": {}, 7 | "source": [ 8 | "## Booleans" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "f6941665", 14 | "metadata": {}, 15 | "source": [ 16 | "### Casting Booleans" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "3f3d0b08", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "c512f867", 30 | "metadata": {}, 31 | "source": [ 32 | "### Boolean Logic" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "id": "22ea3349", 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "Python 3 (ipykernel)", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.10.2" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 5 65 | } 66 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/03_02_Other_Numbers-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "fe22704f", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "db1f0a8f", 14 | "metadata": {}, 15 | "source": [ 16 | "## Integers" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "cd9f4ef4", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "c2930a6e", 30 | "metadata": {}, 31 | "source": [ 32 | "## Decimals" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "id": "a08341aa", 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "Python 3 (ipykernel)", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.10.2" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 5 65 | } 66 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/06_01_AnatomyOfAFunction-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f4faef5f", 6 | "metadata": {}, 7 | "source": [ 8 | "## Functions" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "5df05257", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Foo\n" 22 | ] 23 | } 24 | ], 25 | "source": [] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "4ab92c6d", 30 | "metadata": {}, 31 | "source": [ 32 | "### Named Parameters" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "id": "4ebd385a", 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "data": { 43 | "text/plain": [ 44 | "'__main__'" 45 | ] 46 | }, 47 | "execution_count": 2, 48 | "metadata": {}, 49 | "output_type": "execute_result" 50 | } 51 | ], 52 | "source": [] 53 | } 54 | ], 55 | "metadata": { 56 | "kernelspec": { 57 | "display_name": "Python 3 (ipykernel)", 58 | "language": "python", 59 | "name": "python3" 60 | }, 61 | "language_info": { 62 | "codemirror_mode": { 63 | "name": "ipython", 64 | "version": 3 65 | }, 66 | "file_extension": ".py", 67 | "mimetype": "text/x-python", 68 | "name": "python", 69 | "nbconvert_exporter": "python", 70 | "pygments_lexer": "ipython3", 71 | "version": "3.10.2" 72 | } 73 | }, 74 | "nbformat": 4, 75 | "nbformat_minor": 5 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Essential Training 2 | This is the repository for the LinkedIn Learning course Python Essential Training. The full course is available from [LinkedIn Learning][lil-course-url]. 3 | 4 | ![Python Essential Training][lil-thumbnail-url] 5 | 6 | Python is one of the most commonly used dynamic languages for many large organizations, including Google, Yahoo and IBM. Supported on all major operating systems, it comes pre-installed on Macs, as well as most Linux and Unix-based systems. In this course, senior software engineer Ryan Mitchell guides you through all the essentials of learning and using Python. Learn how computers think, as well as how to install Python, pip, and Jupyter Notebook and the basics of writing a program. Explore variables and types, operators, functions, classes, objects, and more. Go over basic data types like ints and floats, Booleans, and strings. Deep dive into basic data structures, control flow, functions, classes, and objects. Find out how to handle errors and exceptions, as well as threads and processes. Plus, discover how to work with different types of files in Python, pass command-line arguments to your Python script, and create modules and packages. 7 | 8 | ### Instructor 9 | 10 | Ryan Mitchell 11 | 12 | Senior Software Engineer 13 | 14 | 15 | 16 | Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/ryan-mitchell). 17 | 18 | [lil-course-url]: https://www.linkedin.com/learning/python-essential-training-18764650?dApp=59033956 19 | [lil-thumbnail-url]: https://media.licdn.com/dms/image/C4E0DAQHBQo3TSa3IUg/learning-public-crop_675_1200/0/1674513192001?e=2147483647&v=beta&t=YWS_o8SlM4I6YEzJwQnAIP8Q0kfvzX3QbqA7Avrg7K8 20 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/01_05_JupyterNotebook-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "62ce1d82", 6 | "metadata": {}, 7 | "source": [ 8 | "# Title\n", 9 | "## Smaller Title" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "id": "12d15452", 15 | "metadata": {}, 16 | "source": [ 17 | "\\begin{equation}\n", 18 | "e^x=\\sum_{i=0}^\\infty \\frac{1}{i!}x^i\n", 19 | "\\end{equation}" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "id": "ab0b33ee", 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Doing some data science!\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "print('Doing some data science!')" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "id": "70df0832", 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "id": "23519052", 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3 (ipykernel)", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.10.4" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 5 78 | } 79 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/10_01_opening_reading_writing_files-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "8d15fa08", 6 | "metadata": {}, 7 | "source": [ 8 | "## Files" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "48ac6bd4", 14 | "metadata": {}, 15 | "source": [ 16 | "### Reading Files" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "be4247cf", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "fac3ce5d", 30 | "metadata": {}, 31 | "source": [ 32 | "### Writing Files" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "id": "c174437b", 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "id": "1bb84596", 46 | "metadata": {}, 47 | "source": [ 48 | "### Appending Files" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "id": "42b670c3", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3 (ipykernel)", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.10.2" 77 | } 78 | }, 79 | "nbformat": 4, 80 | "nbformat_minor": 5 81 | } 82 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/10_03_json-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "d3f30f14", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "a38d9aab", 14 | "metadata": {}, 15 | "source": [ 16 | "## JSON" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "2b7184fb", 22 | "metadata": {}, 23 | "source": [ 24 | "### Loading JSON" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "id": "13b6088d", 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "ed81cca9", 38 | "metadata": {}, 39 | "source": [ 40 | "### Dumping JSON" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "id": "b1f4fcf6", 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "cac216dd", 54 | "metadata": {}, 55 | "source": [ 56 | "### Custom JSON Decoders" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "id": "9559b0f4", 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [] 66 | } 67 | ], 68 | "metadata": { 69 | "kernelspec": { 70 | "display_name": "Python 3 (ipykernel)", 71 | "language": "python", 72 | "name": "python3" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": { 76 | "name": "ipython", 77 | "version": 3 78 | }, 79 | "file_extension": ".py", 80 | "mimetype": "text/x-python", 81 | "name": "python", 82 | "nbconvert_exporter": "python", 83 | "pygments_lexer": "ipython3", 84 | "version": "3.10.2" 85 | } 86 | }, 87 | "nbformat": 4, 88 | "nbformat_minor": 5 89 | } 90 | -------------------------------------------------------------------------------- /exercise_files/01_05_JupyterNotebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "62ce1d82", 6 | "metadata": {}, 7 | "source": [ 8 | "# Title\n", 9 | "## Smaller Title" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "id": "12d15452", 15 | "metadata": {}, 16 | "source": [ 17 | "\\begin{equation}\n", 18 | "e^x=\\sum_{i=0}^\\infty \\frac{1}{i!}x^i\n", 19 | "\\end{equation}" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": 2, 25 | "id": "ab0b33ee", 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Doing some data science!\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "print('Doing some data science!')" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 1, 43 | "id": "70df0832", 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "2\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "print(1 + 1)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "id": "23519052", 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3.10.4 64-bit", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.10.4" 84 | }, 85 | "vscode": { 86 | "interpreter": { 87 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 88 | } 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 5 93 | } 94 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/05_04_Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "3d331168", 6 | "metadata": {}, 7 | "source": [ 8 | "## Faster Prime Finding\n", 9 | "\n", 10 | "Write a function that returns a list of all primes up to a given number.\n", 11 | "\n", 12 | "For each number, in order to determine if it is prime, take the following steps:\n", 13 | "1. Find the square root of the number\n", 14 | "2. Find all the primes up to that square root\n", 15 | "3. Test to see if any of those primes are divisors \n", 16 | "\n", 17 | "If a number has no prime divisors, it is prime!\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "id": "37687918", 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "def allPrimesUpTo(num):\n", 28 | " pass" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "id": "76e6a11b", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "allPrimesUpTo(100)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "id": "53848a3f", 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "allPrimesUpTo(1000)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "id": "33e69550", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3 (ipykernel)", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.9.13" 77 | } 78 | }, 79 | "nbformat": 4, 80 | "nbformat_minor": 5 81 | } 82 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/06_04_Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6aa3058e", 6 | "metadata": {}, 7 | "source": [ 8 | "## Declaring a \"Winner\"\n", 9 | "\n", 10 | "Write a function that returns the first item in a list sorted according to some rule. The rule should be a function that takes in an item and returns a numeric value\n", 11 | "\n", 12 | "Create a few rules and pass them into the function" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 6, 18 | "id": "01d1f0d6", 19 | "metadata": {}, 20 | "outputs": [], 21 | "source": [ 22 | "def inspectFunction(func):\n", 23 | " pass\n", 24 | " output = func()\n", 25 | " print(locals())\n", 26 | "\n", 27 | "def myFunction():\n", 28 | " a = 2\n", 29 | " b = 3\n", 30 | " \n", 31 | " " 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 7, 37 | "id": "e01c5bfc", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "name": "stdout", 42 | "output_type": "stream", 43 | "text": [ 44 | "{'func': , 'output': None}\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "inspectFunction(myFunction)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "id": "d4e1723a", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3 (ipykernel)", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.9.13" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 5 82 | } 83 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/05_01_IfElseIfelse-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "eb3572f3", 6 | "metadata": {}, 7 | "source": [ 8 | "## If statements with \"FizzBuzz\"" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": null, 14 | "id": "9816da2b", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "# 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, 9, Buzz, 11, Fizz, 13 14, FizzBuzz\n", 19 | "\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "id": "e935bb69", 25 | "metadata": {}, 26 | "source": [ 27 | "### Else statements" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "id": "f6d0a515", 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "id": "b196aa6c", 41 | "metadata": {}, 42 | "source": [ 43 | "### Elif statements" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "id": "39fa2269", 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "id": "bfb49426", 57 | "metadata": {}, 58 | "source": [ 59 | "### Single Line if statements" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "id": "c5c4b674", 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [] 69 | } 70 | ], 71 | "metadata": { 72 | "kernelspec": { 73 | "display_name": "Python 3 (ipykernel)", 74 | "language": "python", 75 | "name": "python3" 76 | }, 77 | "language_info": { 78 | "codemirror_mode": { 79 | "name": "ipython", 80 | "version": 3 81 | }, 82 | "file_extension": ".py", 83 | "mimetype": "text/x-python", 84 | "name": "python", 85 | "nbconvert_exporter": "python", 86 | "pygments_lexer": "ipython3", 87 | "version": "3.10.2" 88 | } 89 | }, 90 | "nbformat": 4, 91 | "nbformat_minor": 5 92 | } 93 | -------------------------------------------------------------------------------- /exercise_files/05_04_Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "3d331168", 6 | "metadata": {}, 7 | "source": [ 8 | "## Faster Prime Finding\n", 9 | "\n", 10 | "Write a function that returns a list of all primes up to a given number.\n", 11 | "\n", 12 | "For each number, in order to determine if it is prime, take the following steps:\n", 13 | "1. Find the square root of the number\n", 14 | "2. Find all the primes up to that square root\n", 15 | "3. Test to see if any of those primes are divisors \n", 16 | "\n", 17 | "If a number has no prime divisors, it is prime!\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": null, 23 | "id": "37687918", 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "def allPrimesUpTo(num):\n", 28 | " pass" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "id": "76e6a11b", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "allPrimesUpTo(100)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "id": "53848a3f", 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "allPrimesUpTo(1000)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "id": "33e69550", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3.10.4 64-bit", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.10.4" 77 | }, 78 | "vscode": { 79 | "interpreter": { 80 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 81 | } 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 5 86 | } 87 | -------------------------------------------------------------------------------- /exercise_files/10_02_ma_prime.csv: -------------------------------------------------------------------------------- 1 | Woods Hole,Barnstable 2 | Chatham,Barnstable 3 | Hyannis Port,Barnstable 4 | Provincetown,Barnstable 5 | South Chatham,Barnstable 6 | South Wellfleet,Barnstable 7 | West Harwich,Barnstable 8 | Pittsfield,Berkshire 9 | Becket,Berkshire 10 | Glendale,Berkshire 11 | Lanesborough,Berkshire 12 | Southfield,Berkshire 13 | North Easton,Bristol 14 | Fairhaven,Bristol 15 | New Bedford,Bristol 16 | Raynham,Bristol 17 | Swansea,Bristol 18 | Westport Point,Bristol 19 | Edgartown,Dukes 20 | Oak Bluffs,Dukes 21 | Cuttyhunk,Dukes 22 | Haverhill,Essex 23 | Lynn,Essex 24 | Swampscott,Essex 25 | Amesbury,Essex 26 | Gloucester,Essex 27 | Middleton,Essex 28 | Newbury,Essex 29 | Andover,Essex 30 | Whately,Franklin 31 | Greenfield,Franklin 32 | Rowe,Franklin 33 | South Deerfield,Franklin 34 | Bondsville,Hampden 35 | Chicopee,Hampden 36 | Chicopee,Hampden 37 | Palmer,Hampden 38 | Woronoco,Hampden 39 | Springfield,Hampden 40 | Springfield,Hampden 41 | Springfield,Hampden 42 | Indian Orchard,Hampden 43 | Granby,Hampshire 44 | Haydenville,Hampshire 45 | Northampton,Hampshire 46 | Northampton,Hampshire 47 | Groton,Middlesex 48 | Ashland,Middlesex 49 | Carlisle,Middlesex 50 | Woburn,Middlesex 51 | Reading,Middlesex 52 | Tyngsboro,Middlesex 53 | North Reading,Middlesex 54 | Cambridge,Middlesex 55 | Somerville,Middlesex 56 | Medford,Middlesex 57 | Newton Center,Middlesex 58 | Chestnut Hill,Middlesex 59 | Watertown,Middlesex 60 | Dedham,Norfolk 61 | Medway,Norfolk 62 | Walpole,Norfolk 63 | Quincy,Norfolk 64 | Brookline Village,Norfolk 65 | East Bridgewater,Plymouth 66 | Hanover,Plymouth 67 | Hanson,Plymouth 68 | Lakeville,Plymouth 69 | Abington,Plymouth 70 | White Horse Beach,Plymouth 71 | Boston,Suffolk 72 | Boston,Suffolk 73 | Charlestown,Suffolk 74 | Roslindale,Suffolk 75 | Readville,Suffolk 76 | Boston,Suffolk 77 | Boston,Suffolk 78 | Boston,Suffolk 79 | Gilbertville,Worcester 80 | Harvard,Worcester 81 | Leominster,Worcester 82 | Lancaster,Worcester 83 | New Braintree,Worcester 84 | Rutland,Worcester 85 | Dudley,Worcester 86 | West Boylston,Worcester 87 | Worcester,Worcester 88 | Worcester,Worcester 89 | Worcester,Worcester 90 | Worcester,Worcester 91 | Hopedale,Worcester 92 | -------------------------------------------------------------------------------- /exercise_files/02_06_Classes_and_Instances.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7e7cecd6", 6 | "metadata": {}, 7 | "source": [ 8 | "## Classes" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 6, 14 | "id": "d5cde69b", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class Dog:\n", 19 | " def __init__(self, name):\n", 20 | " self.name = name\n", 21 | " self.legs = 4\n", 22 | " \n", 23 | " def speak(self):\n", 24 | " print(self.name + ' says: Bark!')\n", 25 | " \n" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 7, 31 | "id": "5b3dde35", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "my_dog = Dog('Rover')\n", 36 | "another_dog = Dog('Fluffy')" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 8, 42 | "id": "ceed1af6", 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "Rover says: Bark!\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "my_dog.speak()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 9, 60 | "id": "b85ea67b", 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "Fluffy says: Bark!\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "another_dog.speak()" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "id": "5d1838eb", 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [] 82 | } 83 | ], 84 | "metadata": { 85 | "kernelspec": { 86 | "display_name": "Python 3 (ipykernel)", 87 | "language": "python", 88 | "name": "python3" 89 | }, 90 | "language_info": { 91 | "codemirror_mode": { 92 | "name": "ipython", 93 | "version": 3 94 | }, 95 | "file_extension": ".py", 96 | "mimetype": "text/x-python", 97 | "name": "python", 98 | "nbconvert_exporter": "python", 99 | "pygments_lexer": "ipython3", 100 | "version": "3.10.2" 101 | } 102 | }, 103 | "nbformat": 4, 104 | "nbformat_minor": 5 105 | } 106 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/02_06_Classes_and_Instances-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7e7cecd6", 6 | "metadata": {}, 7 | "source": [ 8 | "## Classes" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 6, 14 | "id": "d5cde69b", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class Dog:\n", 19 | " def __init__(self, name):\n", 20 | " self.name = name\n", 21 | " self.legs = 4\n", 22 | " \n", 23 | " def speak(self):\n", 24 | " print(self.name + ' says: Bark!')\n", 25 | " \n" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 7, 31 | "id": "5b3dde35", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "my_dog = Dog('Rover')\n", 36 | "another_dog = Dog('Fluffy')" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 8, 42 | "id": "ceed1af6", 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "Rover says: Bark!\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "my_dog.speak()" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 9, 60 | "id": "b85ea67b", 61 | "metadata": {}, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "Fluffy says: Bark!\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "another_dog.speak()" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "id": "5d1838eb", 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [] 82 | } 83 | ], 84 | "metadata": { 85 | "kernelspec": { 86 | "display_name": "Python 3 (ipykernel)", 87 | "language": "python", 88 | "name": "python3" 89 | }, 90 | "language_info": { 91 | "codemirror_mode": { 92 | "name": "ipython", 93 | "version": 3 94 | }, 95 | "file_extension": ".py", 96 | "mimetype": "text/x-python", 97 | "name": "python", 98 | "nbconvert_exporter": "python", 99 | "pygments_lexer": "ipython3", 100 | "version": "3.10.2" 101 | } 102 | }, 103 | "nbformat": 4, 104 | "nbformat_minor": 5 105 | } 106 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/07_02_StaticMethods-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "9bd8b4ad", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import math" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "25307c3f", 16 | "metadata": {}, 17 | "source": [ 18 | "## Static Methods" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 8, 24 | "id": "8bccafd4", 25 | "metadata": {}, 26 | "outputs": [], 27 | "source": [ 28 | "class Orientation:\n", 29 | " pi = 3.14\n", 30 | " def __init__(self, x_pos, y_pos, degrees):\n", 31 | " self.x_pos = x_pos\n", 32 | " self.y_pos = y_pos\n", 33 | " self.x_dir, self.y_dir = Orientation.getUnitVectorFromDegrees(degrees)\n", 34 | " \n", 35 | " def getUnitVectorFromDegrees(degrees):\n", 36 | " radians = (degrees/180) * Orientation.pi\n", 37 | " return math.sin(radians), -math.cos(radians)\n", 38 | " \n", 39 | " def getNextPos(self):\n", 40 | " return self.x_pos + self.x_dir, self.y_pos + self.y_dir\n", 41 | " \n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 9, 47 | "id": "a8c51fb5", 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "(5.965925826289069, 4.741180954897479)" 54 | ] 55 | }, 56 | "execution_count": 9, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "myOrientation = Orientation(5, 5, 75)\n", 63 | "myOrientation.getNextPos()" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "id": "0de75dc5", 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [] 73 | } 74 | ], 75 | "metadata": { 76 | "kernelspec": { 77 | "display_name": "Python 3 (ipykernel)", 78 | "language": "python", 79 | "name": "python3" 80 | }, 81 | "language_info": { 82 | "codemirror_mode": { 83 | "name": "ipython", 84 | "version": 3 85 | }, 86 | "file_extension": ".py", 87 | "mimetype": "text/x-python", 88 | "name": "python", 89 | "nbconvert_exporter": "python", 90 | "pygments_lexer": "ipython3", 91 | "version": "3.10.2" 92 | } 93 | }, 94 | "nbformat": 4, 95 | "nbformat_minor": 5 96 | } 97 | -------------------------------------------------------------------------------- /exercise_files/10_04_challenge_art.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | %%%%%%%%%%%%%%%%%%% 5 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 6 | %%%%%%%% %%%%%%%% 7 | %%%%%%% %%%%%% 8 | %%%%%% %%%%%% 9 | %%%%%% %%%%% 10 | %%%%% %%%%% 11 | %%%%% %%%%% 12 | %%%% %%%%% %%%%% %%%% 13 | %%%% %%%%%%% %%%%%%% %%%% 14 | %%%% %%%%%%% %%%%%%% %%%% 15 | %%%% %%%%%%% %%%%%%% %%%% 16 | %%%% %%%%% %%%%% %%%% 17 | %%%% %%%% 18 | %%%% %%%% 19 | %%%% %%%% 20 | %%%% %%%% %%%% 21 | %%%% %%%%%% %%%%% %%%% 22 | %%%% %%%% %%%% %%%% 23 | %%%% %%%% %%%% %%%% 24 | %%%% %%%%% %%%% %%%% 25 | %%%%% %%%%% %%%%% %%%%% 26 | %%%%% %%%%%% %%%%% %%%% 27 | %%%%% %%%%%%% %%%%%%% %%%%% 28 | %%%%% %%%%%%%%%%%%%%%%%%%%% %%%%% 29 | %%%%%%% %%%%% 30 | %%%%%%% %%%%%%% 31 | %%%%%%%%% %%%%%%%%% 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 33 | %%%%%%%%%%%% 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/03_01_Ints_and_Floats-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "1433045d", 6 | "metadata": {}, 7 | "source": [ 8 | "## Ints and Floats" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "9b94ff5d", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "5.0" 21 | ] 22 | }, 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "20/4" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "id": "25204722", 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "8.0" 42 | ] 43 | }, 44 | "execution_count": 2, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "4 + 4.0" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 3, 56 | "id": "0bcbb2a4", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "8.0" 63 | ] 64 | }, 65 | "execution_count": 3, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "2 * 4.0" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 4, 77 | "id": "a24c86ae", 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "4.0" 84 | ] 85 | }, 86 | "execution_count": 4, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "2 ** 2.0" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "id": "f118cc45", 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "4" 105 | ] 106 | }, 107 | "execution_count": 5, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "int(4.0)" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "id": "7f91f8f2", 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [] 123 | } 124 | ], 125 | "metadata": { 126 | "kernelspec": { 127 | "display_name": "Python 3 (ipykernel)", 128 | "language": "python", 129 | "name": "python3" 130 | }, 131 | "language_info": { 132 | "codemirror_mode": { 133 | "name": "ipython", 134 | "version": 3 135 | }, 136 | "file_extension": ".py", 137 | "mimetype": "text/x-python", 138 | "name": "python", 139 | "nbconvert_exporter": "python", 140 | "pygments_lexer": "ipython3", 141 | "version": "3.10.2" 142 | } 143 | }, 144 | "nbformat": 4, 145 | "nbformat_minor": 5 146 | } 147 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/07_04_Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6d8279ab", 6 | "metadata": {}, 7 | "source": [ 8 | "## Extending the Messenger\n", 9 | "\n", 10 | "Create a class \"SaveMessages\" that extends the Messenger class\n", 11 | "\n", 12 | "- SaveMessages should add any messages it receives to a list, along with the time it was saved\n", 13 | "- Use the provided \"getCurrentTime\" function to get the current time as a string!\n", 14 | "\n", 15 | "Add a method called \"printMessages\" to the SaveMessages class that will print all collected messages on request.\n", 16 | "Run the provided code to see your solution in action\n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "35d4f1f5", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "from datetime import datetime\n", 27 | "\n", 28 | "def getCurrentTime():\n", 29 | " return datetime.now().strftime(\"%m-%d-%Y %H:%M:%S\")\n", 30 | "\n", 31 | "\n", 32 | "class Messenger:\n", 33 | " def __init__(self, listeners=[]):\n", 34 | " self.listeners = listeners\n", 35 | " \n", 36 | " def send(self, message):\n", 37 | " for listener in self.listeners:\n", 38 | " listener.receive(message)\n", 39 | "\n", 40 | " def receive(self, message):\n", 41 | " # Must be implemented by extending classes\n", 42 | " pass\n", 43 | "\n", 44 | "\n", 45 | "class SaveMessages(Messenger):\n", 46 | " # Your code here!\n", 47 | " pass\n", 48 | "\n" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 60, 54 | "id": "608e3112", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# Run this cell after you've written your solution\n", 59 | "listener = SaveMessages()\n", 60 | "\n", 61 | "sender = Messenger([listener])\n", 62 | "\n", 63 | "sender.send('Hello, there! This is the first message')\n" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "id": "2de8c179", 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "# Run this cell after you've written your solution\n", 74 | "sender.send('Oh hi! This is the second message!')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "id": "29c00698", 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "# Run this cell after you've written your solution\n", 85 | "sender.send('Hola! This is the third and final message!')\n", 86 | "\n", 87 | "listener.printMessages()" 88 | ] 89 | } 90 | ], 91 | "metadata": { 92 | "kernelspec": { 93 | "display_name": "Python 3 (ipykernel)", 94 | "language": "python", 95 | "name": "python3" 96 | }, 97 | "language_info": { 98 | "codemirror_mode": { 99 | "name": "ipython", 100 | "version": 3 101 | }, 102 | "file_extension": ".py", 103 | "mimetype": "text/x-python", 104 | "name": "python", 105 | "nbconvert_exporter": "python", 106 | "pygments_lexer": "ipython3", 107 | "version": "3.9.13" 108 | } 109 | }, 110 | "nbformat": 4, 111 | "nbformat_minor": 5 112 | } 113 | -------------------------------------------------------------------------------- /exercise_files/02_07_Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "48f2163c", 6 | "metadata": {}, 7 | "source": [ 8 | "## Factorial Challenge\n", 9 | "\n", 10 | "The factorial function gives the number of possible arrangements of a set of items of length \"n\"\n", 11 | "\n", 12 | "For example, there are 4! (\"four factorial\") or 24 ways to arrange four items, which can be calculated as: \n", 13 | "4 \\* 3 \\* 2 \\* 1\n", 14 | "\n", 15 | "5! = 5 \\* 4 \\* 3 \\* 2 \\* 1 = 120\n", 16 | "\n", 17 | "6! = 6 \\* 5 \\* 4 \\* 3 \\* 2 \\* 1 = 720\n", 18 | "\n", 19 | "etc.\n", 20 | "\n", 21 | "In a set of 0 items (an empty set) there is only one way to arrange the items, therefore, 0! = 1\n", 22 | "\n", 23 | "For the purposes of this exercise, factorials are only defined for **positive integers** (including 0)\n", 24 | "\n", 25 | "\n" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "id": "92af0386", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "# Returns the value of the factorial of num if it is defined, otherwise, returns None\n", 36 | "def factorial(num):\n", 37 | " pass\n" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "id": "1d10be7a", 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# return 120\n", 48 | "factorial(5)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "id": "580eeb3e", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# return 720\n", 59 | "factorial (6)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "id": "c43845c1", 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "# return 1\n", 70 | "factorial(0)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "id": "8f605415", 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "# return None\n", 81 | "factorial(-2)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "id": "5b973ff0", 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "# return None\n", 92 | "factorial(1.2)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "id": "67f07cca", 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# return None\n", 103 | "factorial('spam spam spam spam spam spam')" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "id": "714148cd", 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [] 113 | } 114 | ], 115 | "metadata": { 116 | "kernelspec": { 117 | "display_name": "Python 3 (ipykernel)", 118 | "language": "python", 119 | "name": "python3" 120 | }, 121 | "language_info": { 122 | "codemirror_mode": { 123 | "name": "ipython", 124 | "version": 3 125 | }, 126 | "file_extension": ".py", 127 | "mimetype": "text/x-python", 128 | "name": "python", 129 | "nbconvert_exporter": "python", 130 | "pygments_lexer": "ipython3", 131 | "version": "3.10.4" 132 | } 133 | }, 134 | "nbformat": 4, 135 | "nbformat_minor": 5 136 | } 137 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/02_07_Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "48f2163c", 6 | "metadata": {}, 7 | "source": [ 8 | "## Factorial Challenge\n", 9 | "\n", 10 | "The factorial function gives the number of possible arrangements of a set of items of length \"n\"\n", 11 | "\n", 12 | "For example, there are 4! (\"four factorial\") or 24 ways to arrange four items, which can be calculated as: \n", 13 | "4 \\* 3 \\* 2 \\* 1\n", 14 | "\n", 15 | "5! = 5 \\* 4 \\* 3 \\* 2 \\* 1 = 120\n", 16 | "\n", 17 | "6! = 6 \\* 5 \\* 4 \\* 3 \\* 2 \\* 1 = 720\n", 18 | "\n", 19 | "etc.\n", 20 | "\n", 21 | "In a set of 0 items (an empty set) there is only one way to arrange the items, therefore, 0! = 1\n", 22 | "\n", 23 | "For the purposes of this exercise, factorials are only defined for **positive integers** (including 0)\n", 24 | "\n", 25 | "\n" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "id": "92af0386", 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "# Returns the value of the factorial of num if it is defined, otherwise, returns None\n", 36 | "def factorial(num):\n", 37 | " pass\n" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "id": "1d10be7a", 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# return 120\n", 48 | "factorial(5)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "id": "580eeb3e", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# return 720\n", 59 | "factorial (6)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "id": "c43845c1", 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "# return 1\n", 70 | "factorial(0)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "id": "8f605415", 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "# return None\n", 81 | "factorial(-2)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "id": "5b973ff0", 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "# return None\n", 92 | "factorial(1.2)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": null, 98 | "id": "67f07cca", 99 | "metadata": {}, 100 | "outputs": [], 101 | "source": [ 102 | "# return None\n", 103 | "factorial('spam spam spam spam spam spam')" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "id": "714148cd", 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [] 113 | } 114 | ], 115 | "metadata": { 116 | "kernelspec": { 117 | "display_name": "Python 3 (ipykernel)", 118 | "language": "python", 119 | "name": "python3" 120 | }, 121 | "language_info": { 122 | "codemirror_mode": { 123 | "name": "ipython", 124 | "version": 3 125 | }, 126 | "file_extension": ".py", 127 | "mimetype": "text/x-python", 128 | "name": "python", 129 | "nbconvert_exporter": "python", 130 | "pygments_lexer": "ipython3", 131 | "version": "3.9.13" 132 | } 133 | }, 134 | "nbformat": 4, 135 | "nbformat_minor": 5 136 | } 137 | -------------------------------------------------------------------------------- /exercise_files/07_04_Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6d8279ab", 6 | "metadata": {}, 7 | "source": [ 8 | "## Extending the Messenger\n", 9 | "\n", 10 | "Create a class \"SaveMessages\" that extends the Messenger class that does the following things:\n", 11 | "\n", 12 | "- Add any messages it receives to a list, along with the time the message was received\n", 13 | "- Use the provided \"getCurrentTime\" function so that the received message time is a string\n", 14 | "- Contains a method called \"printMessages\" that prints all collected messages when it's called.\n", 15 | "\n", 16 | "You might also consider clearing the message list when \"printMessages\" is called. \n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "35d4f1f5", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "from datetime import datetime\n", 27 | "\n", 28 | "def getCurrentTime():\n", 29 | " return datetime.now().strftime(\"%m-%d-%Y %H:%M:%S\")\n", 30 | "\n", 31 | "\n", 32 | "class Messenger:\n", 33 | " def __init__(self, listeners=[]):\n", 34 | " self.listeners = listeners\n", 35 | " \n", 36 | " def send(self, message):\n", 37 | " for listener in self.listeners:\n", 38 | " listener.receive(message)\n", 39 | "\n", 40 | " def receive(self, message):\n", 41 | " # Must be implemented by extending classes\n", 42 | " pass\n", 43 | "\n", 44 | "\n", 45 | "class SaveMessages(Messenger):\n", 46 | " # Your code here!\n", 47 | " pass\n", 48 | "\n" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 60, 54 | "id": "608e3112", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# Run this cell after you've written your solution\n", 59 | "listener = SaveMessages()\n", 60 | "\n", 61 | "sender = Messenger([listener])\n", 62 | "\n", 63 | "sender.send('Hello, there! This is the first message')\n" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "id": "2de8c179", 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "# Run this cell after you've written your solution\n", 74 | "sender.send('Oh hi! This is the second message!')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "id": "29c00698", 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "# Run this cell after you've written your solution\n", 85 | "sender.send('Hola! This is the third and final message!')\n", 86 | "\n", 87 | "listener.printMessages()" 88 | ] 89 | } 90 | ], 91 | "metadata": { 92 | "kernelspec": { 93 | "display_name": "Python 3.10.4 64-bit", 94 | "language": "python", 95 | "name": "python3" 96 | }, 97 | "language_info": { 98 | "codemirror_mode": { 99 | "name": "ipython", 100 | "version": 3 101 | }, 102 | "file_extension": ".py", 103 | "mimetype": "text/x-python", 104 | "name": "python", 105 | "nbconvert_exporter": "python", 106 | "pygments_lexer": "ipython3", 107 | "version": "3.10.4" 108 | }, 109 | "vscode": { 110 | "interpreter": { 111 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 112 | } 113 | } 114 | }, 115 | "nbformat": 4, 116 | "nbformat_minor": 5 117 | } 118 | -------------------------------------------------------------------------------- /exercise_files/03_05_bytes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "fcb242f6", 6 | "metadata": {}, 7 | "source": [ 8 | "### Bytes" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "3c3ee6d8", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "b'\\x00\\x00\\x00\\x00'" 21 | ] 22 | }, 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "bytes(4)" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "id": "9a254973", 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "b'\\xf0\\x9f\\x99\\x84'" 42 | ] 43 | }, 44 | "execution_count": 2, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "smileyBytes = bytes('🙄', 'utf-8')\n", 51 | "smileyBytes" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "id": "a18b1572", 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "'🙄'" 64 | ] 65 | }, 66 | "execution_count": 3, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "smileyBytes.decode('utf-8')" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 4, 78 | "id": "48a506cd", 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "smileyBytes = bytearray('🙄', 'utf-8')" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 5, 88 | "id": "5e920566", 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "bytearray(b'\\xf0\\x9f\\x99\\x84')" 95 | ] 96 | }, 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "smileyBytes" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "id": "6f5cb2b0", 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "smileyBytes[3] = int('85', 16)" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 7, 119 | "id": "8b5cd28d", 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "'🙅'" 126 | ] 127 | }, 128 | "execution_count": 7, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "smileyBytes.decode('utf-8')" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "id": "fd1a7b83", 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [] 144 | } 145 | ], 146 | "metadata": { 147 | "kernelspec": { 148 | "display_name": "Python 3 (ipykernel)", 149 | "language": "python", 150 | "name": "python3" 151 | }, 152 | "language_info": { 153 | "codemirror_mode": { 154 | "name": "ipython", 155 | "version": 3 156 | }, 157 | "file_extension": ".py", 158 | "mimetype": "text/x-python", 159 | "name": "python", 160 | "nbconvert_exporter": "python", 161 | "pygments_lexer": "ipython3", 162 | "version": "3.9.13" 163 | } 164 | }, 165 | "nbformat": 4, 166 | "nbformat_minor": 5 167 | } 168 | -------------------------------------------------------------------------------- /exercise_files/10_04_Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "5ecfaeb8", 6 | "metadata": {}, 7 | "source": [ 8 | "## ASCII Art Compression\n", 9 | "\n", 10 | "Use the \"encodeString\" and \"decodeString\" functions from the Chapter 4 challenge, provided below\n", 11 | "\n", 12 | "Read in the ASCII art text file 10_04_challenge_art.txt and write it back to a new file that has a smaller file size than the original file. \n", 13 | "For example, the original 10_04_challenge_art.txt has a file size of 2.757kB (or 2,757 ASCII characters).\n", 14 | "\n", 15 | "- Any compression is great!\n", 16 | "- Is there any way you could get this file to 1kb?\n", 17 | "- Less than 1kb?\n", 18 | "\n", 19 | "After compressing the file, make sure to check your work by opening and decoding it again!\n", 20 | "\n" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "id": "0ec1174a", 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "import os\n", 31 | "\n", 32 | "def encodeString(stringVal):\n", 33 | " encodedList = []\n", 34 | " prevChar = None\n", 35 | " count = 0\n", 36 | " for char in stringVal:\n", 37 | " if prevChar != char and prevChar is not None:\n", 38 | " encodedList.append((prevChar, count))\n", 39 | " count = 0\n", 40 | " prevChar = char\n", 41 | " count = count + 1\n", 42 | " encodedList.append((prevChar, count))\n", 43 | " return encodedList\n", 44 | "\n", 45 | "def decodeString(encodedList):\n", 46 | " decodedStr = ''\n", 47 | " for item in encodedList:\n", 48 | " decodedStr = decodedStr + item[0] * item[1]\n", 49 | " return decodedStr" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "id": "9419d4f1", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "\n", 60 | "def encodeFile(filename, newFilename):\n", 61 | " # Your code here!\n", 62 | " pass\n", 63 | "\n", 64 | "def decodeFile(filename):\n", 65 | " # Your code here!\n", 66 | " pass\n", 67 | "\n" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "id": "bd2b41b6", 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "print(f'Original file size: {os.path.getsize(\"10_04_challenge_art.txt\")}')\n", 78 | "\n", 79 | "encodeFile('10_04_challenge_art.txt', '10_04_challenge_art_encoded.txt')\n", 80 | "\n", 81 | "print(f'New file size: {os.path.getsize(\"10_04_challenge_art_encoded.txt\")}')\n" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "id": "92945907", 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "decodeFile('10_04_challenge_art_encoded.txt')" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "id": "e57a4e0b", 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [] 101 | } 102 | ], 103 | "metadata": { 104 | "kernelspec": { 105 | "display_name": "Python 3 (ipykernel)", 106 | "language": "python", 107 | "name": "python3" 108 | }, 109 | "language_info": { 110 | "codemirror_mode": { 111 | "name": "ipython", 112 | "version": 3 113 | }, 114 | "file_extension": ".py", 115 | "mimetype": "text/x-python", 116 | "name": "python", 117 | "nbconvert_exporter": "python", 118 | "pygments_lexer": "ipython3", 119 | "version": "3.9.13" 120 | } 121 | }, 122 | "nbformat": 4, 123 | "nbformat_minor": 5 124 | } 125 | -------------------------------------------------------------------------------- /exercise_files/04_05_Dictionary_Comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "f76cf3b1", 6 | "metadata": {}, 7 | "source": [ 8 | "## Dictionary Comprehensions" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "92d3299d", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "{'a': 'aardvark', 'b': 'bear', 'c': 'cat', 'd': 'dog'}" 21 | ] 22 | }, 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "animalList = [('a', 'aardvark'), ('b', 'bear'), ('c', 'cat'), ('d', 'dog')]\n", 30 | "animals = {item[0]: item[1] for item in animalList}\n", 31 | "animals" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 4, 37 | "id": "68206d1c", 38 | "metadata": {}, 39 | "outputs": [ 40 | { 41 | "data": { 42 | "text/plain": [ 43 | "{'a': 'aardvark', 'b': 'bear', 'c': 'cat', 'd': 'dog'}" 44 | ] 45 | }, 46 | "execution_count": 4, 47 | "metadata": {}, 48 | "output_type": "execute_result" 49 | } 50 | ], 51 | "source": [ 52 | "animals = {key: value for key, value in animalList}\n", 53 | "animals" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 5, 59 | "id": "96bdf466", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "dict_items([('a', 'aardvark'), ('b', 'bear'), ('c', 'cat'), ('d', 'dog')])" 66 | ] 67 | }, 68 | "execution_count": 5, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "animals.items()" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 6, 80 | "id": "467b6984", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "data": { 85 | "text/plain": [ 86 | "[('a', 'aardvark'), ('b', 'bear'), ('c', 'cat'), ('d', 'dog')]" 87 | ] 88 | }, 89 | "execution_count": 6, 90 | "metadata": {}, 91 | "output_type": "execute_result" 92 | } 93 | ], 94 | "source": [ 95 | "list(animals.items())" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 7, 101 | "id": "494fe288", 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "data": { 106 | "text/plain": [ 107 | "[{'letter': 'a', 'name': 'aardvark'},\n", 108 | " {'letter': 'b', 'name': 'bear'},\n", 109 | " {'letter': 'c', 'name': 'cat'},\n", 110 | " {'letter': 'd', 'name': 'dog'}]" 111 | ] 112 | }, 113 | "execution_count": 7, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | } 117 | ], 118 | "source": [ 119 | "[{'letter': key, 'name': value} for key, value in animals.items()]" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "id": "3766f5e9", 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Python 3 (ipykernel)", 134 | "language": "python", 135 | "name": "python3" 136 | }, 137 | "language_info": { 138 | "codemirror_mode": { 139 | "name": "ipython", 140 | "version": 3 141 | }, 142 | "file_extension": ".py", 143 | "mimetype": "text/x-python", 144 | "name": "python", 145 | "nbconvert_exporter": "python", 146 | "pygments_lexer": "ipython3", 147 | "version": "3.10.2" 148 | } 149 | }, 150 | "nbformat": 4, 151 | "nbformat_minor": 5 152 | } 153 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/10_04_Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "5ecfaeb8", 6 | "metadata": {}, 7 | "source": [ 8 | "## ASCII Art Compression\n", 9 | "\n", 10 | "Use the \"encodeString\" and \"decodeString\" functions from the Chapter 4 challenge, provided below\n", 11 | "\n", 12 | "Read in the ASCII art text file 10_04_challenge_art.txt and write it back to a new file that has a smaller file size than the original file. \n", 13 | "For example, the original 10_04_challenge_art.txt has a file size of 2.757kB (or 2,757 ASCII characters).\n", 14 | "\n", 15 | "- Any compression is great!\n", 16 | "- Is there any way you could get this file to 1kb?\n", 17 | "- Less than 1kb?\n", 18 | "\n", 19 | "After compressing the file, make sure to check your work by opening and decoding it again!\n", 20 | "\n" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "id": "0ec1174a", 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "import os\n", 31 | "\n", 32 | "def encodeString(stringVal):\n", 33 | " encodedList = []\n", 34 | " prevChar = None\n", 35 | " count = 0\n", 36 | " for char in stringVal:\n", 37 | " if prevChar != char and prevChar is not None:\n", 38 | " encodedList.append((prevChar, count))\n", 39 | " count = 0\n", 40 | " prevChar = char\n", 41 | " count = count + 1\n", 42 | " encodedList.append((prevChar, count))\n", 43 | " return encodedList\n", 44 | "\n", 45 | "def decodeString(encodedList):\n", 46 | " decodedStr = ''\n", 47 | " for item in encodedList:\n", 48 | " decodedStr = decodedStr + item[0] * item[1]\n", 49 | " return decodedStr" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "id": "9419d4f1", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "\n", 60 | "def encodeFile(filename, newFilename):\n", 61 | " # Your code here!\n", 62 | " pass\n", 63 | "\n", 64 | "def decodeFile(filename):\n", 65 | " # Your code here!\n", 66 | " pass\n", 67 | "\n" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "id": "bd2b41b6", 74 | "metadata": {}, 75 | "outputs": [], 76 | "source": [ 77 | "print(f'Original file size: {os.path.getsize(\"10_04_challenge_art.txt\")}')\n", 78 | "\n", 79 | "encodeFile('10_04_challenge_art.txt', '10_04_challenge_art_encoded.txt')\n", 80 | "\n", 81 | "print(f'New file size: {os.path.getsize(\"10_04_challenge_art_encoded.txt\")}')\n" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "id": "92945907", 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "decodeFile('10_04_challenge_art_encoded.txt')" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "id": "e57a4e0b", 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [] 101 | } 102 | ], 103 | "metadata": { 104 | "kernelspec": { 105 | "display_name": "Python 3 (ipykernel)", 106 | "language": "python", 107 | "name": "python3" 108 | }, 109 | "language_info": { 110 | "codemirror_mode": { 111 | "name": "ipython", 112 | "version": 3 113 | }, 114 | "file_extension": ".py", 115 | "mimetype": "text/x-python", 116 | "name": "python", 117 | "nbconvert_exporter": "python", 118 | "pygments_lexer": "ipython3", 119 | "version": "3.9.13" 120 | } 121 | }, 122 | "nbformat": 4, 123 | "nbformat_minor": 5 124 | } 125 | -------------------------------------------------------------------------------- /exercise_files/09_03_Processes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "id": "34fcf0b8", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "from multiprocess import Process\n", 11 | "import time\n", 12 | "import threading" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "id": "b9bc0c6f", 18 | "metadata": {}, 19 | "source": [ 20 | "## Processes" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 6, 26 | "id": "37273e14", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "01\n", 34 | "4\n", 35 | "9\n", 36 | "Finished computing!Finished computing!16\n", 37 | "25Finished computing!\n", 38 | "\n", 39 | "\n", 40 | "36Finished computing!\n", 41 | "\n", 42 | "49Finished computing!64\n", 43 | "\n", 44 | "81Finished computing!\n", 45 | "Finished computing!\n", 46 | "\n", 47 | "\n", 48 | "\n", 49 | "Finished computing!Finished computing!\n", 50 | "Finished computing!\n", 51 | "\n", 52 | "\n" 53 | ] 54 | }, 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "[None, None, None, None, None, None, None, None, None, None]" 59 | ] 60 | }, 61 | "execution_count": 6, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "def longSquare(num, results):\n", 68 | " time.sleep(1)\n", 69 | " print(num**2)\n", 70 | " print('Finished computing!')\n", 71 | "\n", 72 | "results = {}\n", 73 | "processes = [Process(target=longSquare, args=(n,results)) for n in range(0, 10)]\n", 74 | "[p.start() for p in processes]\n", 75 | "[p.join() for p in processes]\n", 76 | "\n", 77 | "\n" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 9, 83 | "id": "ee636997", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "9163625496481\n", 91 | "Finished computing!\n", 92 | "14\n", 93 | "\n", 94 | "Finished computing!\n", 95 | "\n", 96 | "Finished computing!\n", 97 | "Finished computing!\n", 98 | "\n", 99 | "\n", 100 | "\n", 101 | "Finished computing!\n", 102 | "Finished computing!\n", 103 | "0Finished computing!\n", 104 | "Finished computing!\n", 105 | "\n", 106 | "\n", 107 | "Finished computing!\n", 108 | "Finished computing!\n", 109 | "\n", 110 | "{}\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "results = {}\n", 116 | "threads = [threading.Thread(target=longSquare, args=(n, results)) for n in range(0, 10)]\n", 117 | "[t.start() for t in threads]\n", 118 | "[t.join() for t in threads]\n", 119 | "print(results)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "id": "129b1aa4", 126 | "metadata": {}, 127 | "outputs": [], 128 | "source": [] 129 | } 130 | ], 131 | "metadata": { 132 | "kernelspec": { 133 | "display_name": "Python 3 (ipykernel)", 134 | "language": "python", 135 | "name": "python3" 136 | }, 137 | "language_info": { 138 | "codemirror_mode": { 139 | "name": "ipython", 140 | "version": 3 141 | }, 142 | "file_extension": ".py", 143 | "mimetype": "text/x-python", 144 | "name": "python", 145 | "nbconvert_exporter": "python", 146 | "pygments_lexer": "ipython3", 147 | "version": "3.10.2" 148 | } 149 | }, 150 | "nbformat": 4, 151 | "nbformat_minor": 5 152 | } 153 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/02_08_Solution-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "48f2163c", 6 | "metadata": {}, 7 | "source": [ 8 | "## Factorial Challenge\n", 9 | "\n", 10 | "The factorial function gives the number of possible arrangements of a set of items of length \"n\"\n", 11 | "\n", 12 | "For example, there are 4! (\"four factorial\") or 24 ways to arrange four items, which can be calculated as: \n", 13 | "4 \\* 3 \\* 2 \\* 1\n", 14 | "\n", 15 | "5! = 5 \\* 4 \\* 3 \\* 2 \\* 1 = 120\n", 16 | "\n", 17 | "6! = 6 \\* 5 \\* 4 \\* 3 \\* 2 \\* 1 = 720\n", 18 | "\n", 19 | "etc.\n", 20 | "\n", 21 | "In a set of 0 items (an empty set) there is only one way to arrange the items, therefore, 0! = 1\n", 22 | "\n", 23 | "For the purposes of this exercise, factorials are only defined for **positive integers** (including 0)\n" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 7, 29 | "id": "92af0386", 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "# Returns the value of the factorial of num if it is defined, otherwise, returns None\n", 34 | "def factorial(num):\n", 35 | " if type(num) is not int:\n", 36 | " return None\n", 37 | " if num < 0:\n", 38 | " return None\n", 39 | " if num == 0:\n", 40 | " return 1\n", 41 | " \n", 42 | " i = 0\n", 43 | " f = 1\n", 44 | " while i < num:\n", 45 | " i = i + 1\n", 46 | " f = f * i\n", 47 | " \n", 48 | " return f" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 8, 54 | "id": "1d10be7a", 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "120" 61 | ] 62 | }, 63 | "execution_count": 8, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "factorial(5)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 9, 75 | "id": "c43845c1", 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "1" 82 | ] 83 | }, 84 | "execution_count": 9, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "factorial(0)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 10, 96 | "id": "8f605415", 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "factorial(-2)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 11, 106 | "id": "5b973ff0", 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "factorial(1.2)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 12, 116 | "id": "67f07cca", 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "factorial('spam spam spam spam spam spam')" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "id": "714148cd", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [] 130 | } 131 | ], 132 | "metadata": { 133 | "kernelspec": { 134 | "display_name": "Python 3 (ipykernel)", 135 | "language": "python", 136 | "name": "python3" 137 | }, 138 | "language_info": { 139 | "codemirror_mode": { 140 | "name": "ipython", 141 | "version": 3 142 | }, 143 | "file_extension": ".py", 144 | "mimetype": "text/x-python", 145 | "name": "python", 146 | "nbconvert_exporter": "python", 147 | "pygments_lexer": "ipython3", 148 | "version": "3.9.13" 149 | } 150 | }, 151 | "nbformat": 4, 152 | "nbformat_minor": 5 153 | } 154 | -------------------------------------------------------------------------------- /exercise_files/03_06_Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "5cd90a72", 6 | "metadata": {}, 7 | "source": [ 8 | "## Converting Hexadecimal to Decimal\n", 9 | "\n", 10 | "Hexadecimal or \"base 16\" uses all of the numbers 0 - 9, plus a few others to signify higher numbers:\n", 11 | "\n", 12 | "A = 10\n", 13 | "\n", 14 | "B = 11\n", 15 | "\n", 16 | "C = 12\n", 17 | "\n", 18 | "D = 13\n", 19 | "\n", 20 | "E = 14\n", 21 | "\n", 22 | "F = 15\n", 23 | "\n", 24 | "Therefore, the number 'D' in hexadecimal would be 13 in decimal.\n", 25 | "\n", 26 | "The number '1A' in hexadecimal would be 26 in decimal. Just like we have the \"tens\" place in base 10, hexadecimal has the \"sixteens\" place. So 1A would be 16 + 10 or 26. \n", 27 | "\n", 28 | "And just like decimal has the \"hundreds\" place (because 10 * 10 is 100), hexadecimal has the \"256's\" place (because 16 * 16 is 256) So 'ABC' in hexadecimal is (256 * 10) + (16 * 11) + (1 * 12) or 2,748\n" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "id": "cfc54187", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "hexNumbers = {\n", 39 | " '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,\n", 40 | " 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15\n", 41 | "}\n", 42 | "\n", 43 | "# Converts a string hexadecimal number into an integer decimal\n", 44 | "# If hexNum is not a valid hexadecimal number, returns None\n", 45 | "def hexToDec(hexNum):\n", 46 | " pass" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "id": "6871966d", 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "# 10\n", 57 | "hexToDec('A')" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "id": "fbac89a5", 64 | "metadata": {}, 65 | "outputs": [], 66 | "source": [ 67 | "# 0\n", 68 | "hexToDec('0')" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "e8471b43", 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# 27\n", 79 | "hexToDec('1B')" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": null, 85 | "id": "828e6dce", 86 | "metadata": {}, 87 | "outputs": [], 88 | "source": [ 89 | "# 960\n", 90 | "hexToDec('3C0')" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "id": "86051c1a", 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "# None\n", 101 | "hexToDec('A6G')" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "id": "598538ff", 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [ 111 | "# None\n", 112 | "hexToDec('ZZTOP')" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "id": "0051662e", 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [] 122 | } 123 | ], 124 | "metadata": { 125 | "kernelspec": { 126 | "display_name": "Python 3.10.4 64-bit", 127 | "language": "python", 128 | "name": "python3" 129 | }, 130 | "language_info": { 131 | "codemirror_mode": { 132 | "name": "ipython", 133 | "version": 3 134 | }, 135 | "file_extension": ".py", 136 | "mimetype": "text/x-python", 137 | "name": "python", 138 | "nbconvert_exporter": "python", 139 | "pygments_lexer": "ipython3", 140 | "version": "3.10.4" 141 | }, 142 | "vscode": { 143 | "interpreter": { 144 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 145 | } 146 | } 147 | }, 148 | "nbformat": 4, 149 | "nbformat_minor": 5 150 | } 151 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/05_04_Challenge_Hints-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "3d331168", 6 | "metadata": {}, 7 | "source": [ 8 | "## Faster Prime Finding\n", 9 | "\n", 10 | "Write a function that returns a list of all primes up to a given number.\n", 11 | "\n", 12 | "For each number, in order to determine if it is prime, take the following steps:\n", 13 | "1. Find the square root of the number\n", 14 | "2. Find all the primes up to that square root\n", 15 | "3. Test to see if any of those primes are divisors \n", 16 | "\n", 17 | "If a number has no prime divisors, it is prime!\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "id": "c722d7d2", 23 | "metadata": {}, 24 | "source": [ 25 | "### Challenge Hints!" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "id": "447e6304", 31 | "metadata": {}, 32 | "source": [ 33 | "**Hint 1:** The function should collect a list of prime numbers as goes. This is the list that it adds \"new primes\" to as well as the list it uses to check for factors.\n" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "45c5dc7b", 39 | "metadata": {}, 40 | "source": [ 41 | "**Hint 2:** Try modifying the previously-written code:" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 1, 47 | "id": "747a1ebf", 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "2 is prime!\n", 55 | "3 is prime!\n", 56 | "5 is prime!\n", 57 | "7 is prime!\n", 58 | "11 is prime!\n", 59 | "13 is prime!\n", 60 | "17 is prime!\n", 61 | "19 is prime!\n", 62 | "23 is prime!\n", 63 | "29 is prime!\n", 64 | "31 is prime!\n", 65 | "37 is prime!\n", 66 | "41 is prime!\n", 67 | "43 is prime!\n", 68 | "47 is prime!\n", 69 | "53 is prime!\n", 70 | "59 is prime!\n", 71 | "61 is prime!\n", 72 | "67 is prime!\n", 73 | "71 is prime!\n", 74 | "73 is prime!\n", 75 | "79 is prime!\n", 76 | "83 is prime!\n", 77 | "89 is prime!\n", 78 | "97 is prime!\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "for number in range(2, 100):\n", 84 | " for factor in range(2, int(number ** 0.5) + 1):\n", 85 | " if number % factor == 0:\n", 86 | " break\n", 87 | " else:\n", 88 | " print(f'{number} is prime!')" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "id": "b27bfac4", 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 1, 102 | "id": "5fc8b019", 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "def allPrimesUpTo(num):\n", 107 | " pass" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 2, 113 | "id": "76e6a11b", 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "allPrimesUpTo(100)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 3, 123 | "id": "0084237b", 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "allPrimesUpTo(1000)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "id": "9c863053", 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | } 138 | ], 139 | "metadata": { 140 | "kernelspec": { 141 | "display_name": "Python 3 (ipykernel)", 142 | "language": "python", 143 | "name": "python3" 144 | }, 145 | "language_info": { 146 | "codemirror_mode": { 147 | "name": "ipython", 148 | "version": 3 149 | }, 150 | "file_extension": ".py", 151 | "mimetype": "text/x-python", 152 | "name": "python", 153 | "nbconvert_exporter": "python", 154 | "pygments_lexer": "ipython3", 155 | "version": "3.9.13" 156 | } 157 | }, 158 | "nbformat": 4, 159 | "nbformat_minor": 5 160 | } 161 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/07_05_Solution-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "dda9c8c6", 6 | "metadata": {}, 7 | "source": [ 8 | "## Extending the Messenger\n", 9 | "\n", 10 | "Create a class \"SaveMessages\" that extends the Messenger class\n", 11 | "\n", 12 | "- SaveMessages should add any messages it receives to a list, along with the time it was saved\n", 13 | "- Use the provided \"getCurrentTime\" function to get the current time as a string!\n", 14 | "\n", 15 | "Add a method called \"printMessages\" to the SaveMessages class that will print all collected messages on request.\n", 16 | "Run the provided code to see your solution in action\n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "id": "35d4f1f5", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "from datetime import datetime\n", 27 | "\n", 28 | "\n", 29 | "def getCurrentTime():\n", 30 | " return datetime.now().strftime(\"%m-%d-%Y %H:%M:%S\")\n", 31 | "\n", 32 | "\n", 33 | "class Messenger:\n", 34 | " def __init__(self, listeners=[]):\n", 35 | " self.listeners = listeners\n", 36 | " \n", 37 | " def send(self, message):\n", 38 | " for listener in self.listeners:\n", 39 | " listener.receive(message)\n", 40 | "\n", 41 | " def receive(self, message):\n", 42 | " # Must be implemented by extending classes\n", 43 | " pass\n", 44 | "\n", 45 | "\n", 46 | "class SaveMessages(Messenger):\n", 47 | " def __init__(self, listeners=[]):\n", 48 | " super().__init__(listeners)\n", 49 | " self.messages = []\n", 50 | " \n", 51 | " def receive(self, message):\n", 52 | " self.messages.append({'message': message, 'time': getCurrentTime()})\n", 53 | " \n", 54 | " def printMessages(self):\n", 55 | " for m in self.messages:\n", 56 | " print(f'Message: \"{m[\"message\"]}\" Time: {m[\"time\"]}')\n", 57 | " self.messages = []\n", 58 | "\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "id": "608e3112", 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "# Run this cell after you've written your solution\n", 69 | "listener = SaveMessages()\n", 70 | "\n", 71 | "sender = Messenger([listener])\n", 72 | "\n", 73 | "sender.send('Hello, there! This is the first message')\n" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "id": "2de8c179", 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "# Run this cell after you've written your solution\n", 84 | "sender.send('Oh hi! This is the second message!')" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "id": "5731a38e", 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "# Run this cell after you've written your solution\n", 95 | "sender.send('Hola! This is the third and final message!')\n", 96 | "\n" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "id": "6ee7752d", 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [ 106 | "listener.printMessages()" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": null, 112 | "id": "972c905b", 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [] 116 | } 117 | ], 118 | "metadata": { 119 | "kernelspec": { 120 | "display_name": "Python 3 (ipykernel)", 121 | "language": "python", 122 | "name": "python3" 123 | }, 124 | "language_info": { 125 | "codemirror_mode": { 126 | "name": "ipython", 127 | "version": 3 128 | }, 129 | "file_extension": ".py", 130 | "mimetype": "text/x-python", 131 | "name": "python", 132 | "nbconvert_exporter": "python", 133 | "pygments_lexer": "ipython3", 134 | "version": "3.10.4" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 5 139 | } 140 | -------------------------------------------------------------------------------- /exercise_files/10_03_json.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 10, 6 | "id": "d3f30f14", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import json\n", 11 | "from json import JSONDecodeError, JSONEncoder" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "4c55b8ed", 17 | "metadata": {}, 18 | "source": [ 19 | "## JSON" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "id": "b5d2a05c", 25 | "metadata": {}, 26 | "source": [ 27 | "### Loading JSON" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 6, 33 | "id": "13b6088d", 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "Could not parse JSON!\n" 41 | ] 42 | } 43 | ], 44 | "source": [ 45 | "jsonString = '{\"a\": \"apple\", \"b\": \"bear\", \"c\": \"cat\",}'\n", 46 | "try:\n", 47 | " json.loads(jsonString)\n", 48 | "except JSONDecodeError:\n", 49 | " print('Could not parse JSON!')" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "id": "bee8d345", 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "data": { 60 | "text/plain": [ 61 | "{'a': 'apple', 'b': 'bear', 'c': 'cat'}" 62 | ] 63 | }, 64 | "execution_count": 3, 65 | "metadata": {}, 66 | "output_type": "execute_result" 67 | } 68 | ], 69 | "source": [ 70 | "{'a': 'apple', 'b': 'bear', 'c': 'cat',}" 71 | ] 72 | }, 73 | { 74 | "cell_type": "markdown", 75 | "id": "91d66c40", 76 | "metadata": {}, 77 | "source": [ 78 | "### Dumping JSON" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": 7, 84 | "id": "28da99a7", 85 | "metadata": {}, 86 | "outputs": [ 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "'{\"a\": \"apple\", \"b\": \"bear\", \"c\": \"cat\"}'" 91 | ] 92 | }, 93 | "execution_count": 7, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "pythonDict = {'a': 'apple', 'b': 'bear', 'c': 'cat',}\n", 100 | "json.dumps(pythonDict)" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "id": "2cd8bb44", 106 | "metadata": {}, 107 | "source": [ 108 | "### Custom JSON Decoders" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 11, 114 | "id": "9559b0f4", 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "data": { 119 | "text/plain": [ 120 | "'{\"a\": \"aardvark\", \"b\": \"bear\", \"c\": \"cat\"}'" 121 | ] 122 | }, 123 | "execution_count": 11, 124 | "metadata": {}, 125 | "output_type": "execute_result" 126 | } 127 | ], 128 | "source": [ 129 | "class Animal:\n", 130 | " def __init__(self, name):\n", 131 | " self.name = name\n", 132 | "\n", 133 | "class AnimalEncoder(JSONEncoder):\n", 134 | " def default(self, o):\n", 135 | " if type(o) == Animal:\n", 136 | " return o.name\n", 137 | " return super().default(o)\n", 138 | " \n", 139 | "pythonDict = {'a': Animal('aardvark'), 'b': Animal('bear'), 'c': Animal('cat'),}\n", 140 | "json.dumps(pythonDict, cls=AnimalEncoder)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "id": "56613cee", 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3 (ipykernel)", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.10.2" 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 5 173 | } 174 | -------------------------------------------------------------------------------- /exercise_files/02_04_ControlFlow.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "4e835ee3", 6 | "metadata": {}, 7 | "source": [ 8 | "## Control Flow" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "2a65107b", 14 | "metadata": {}, 15 | "source": [ 16 | "### If / Else statements" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 5, 22 | "id": "03d16962", 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "It is true!\n", 30 | "Also print this\n", 31 | "Always print this\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "a = True\n", 37 | "if a:\n", 38 | " print('It is true!')\n", 39 | " print('Also print this')\n", 40 | "else:\n", 41 | " print('It is false!')\n", 42 | "print('Always print this')" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 8, 48 | "id": "c250abc7", 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "It is true!\n", 56 | "Also print this\n", 57 | "Always print this\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "a = True\n", 63 | "b = False\n", 64 | "c = True\n", 65 | "if a:\n", 66 | " print('It is true!')\n", 67 | " print('Also print this')\n", 68 | " if b:\n", 69 | " print('Both are true')\n", 70 | " if c:\n", 71 | " print('All three are true')\n", 72 | "else:\n", 73 | " print('It is false!')\n", 74 | "print('Always print this')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "id": "ecc930e8", 80 | "metadata": {}, 81 | "source": [ 82 | "### For loops" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 11, 88 | "id": "8b49fa67", 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "1\n", 96 | "2\n", 97 | "3\n", 98 | "4\n", 99 | "5\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "a = [1,2,3,4,5]\n", 105 | "for number in a:\n", 106 | " print(number)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 10, 112 | "id": "55d76100", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "True" 119 | ] 120 | }, 121 | "execution_count": 10, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "4 in a" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "id": "8b4d6361", 133 | "metadata": {}, 134 | "source": [ 135 | "### While loops" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 12, 141 | "id": "053eb644", 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "0\n", 149 | "1\n", 150 | "2\n", 151 | "3\n", 152 | "4\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "a = 0\n", 158 | "while a < 5:\n", 159 | " print(a)\n", 160 | " a = a + 1" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "id": "f8052766", 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [] 170 | } 171 | ], 172 | "metadata": { 173 | "kernelspec": { 174 | "display_name": "Python 3 (ipykernel)", 175 | "language": "python", 176 | "name": "python3" 177 | }, 178 | "language_info": { 179 | "codemirror_mode": { 180 | "name": "ipython", 181 | "version": 3 182 | }, 183 | "file_extension": ".py", 184 | "mimetype": "text/x-python", 185 | "name": "python", 186 | "nbconvert_exporter": "python", 187 | "pygments_lexer": "ipython3", 188 | "version": "3.9.13" 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 5 193 | } 194 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/08_04_Challenge-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "522e4770", 6 | "metadata": {}, 7 | "source": [ 8 | "## Message Exceptions\n", 9 | "\n", 10 | "The SaveMessages class now has limited memory and should only be able to hold a maximum of 10 messages at once.\n", 11 | "\n", 12 | "This challenge has three parts, outlined in comments below. \n", 13 | "\n", 14 | "**\\# 1. Finish creating the TooManyMessagesException class**\n", 15 | "\n", 16 | "Fill in the TooManyMessagesException class. Add a custom message!\n", 17 | "\n", 18 | "**\\# 2. Raise a TooManyMessagesException exception here**\n", 19 | "\n", 20 | "Make sure that the SaveMessages class doesn't get over-full and raises an Exception if the max_messages limit is reached.\n", 21 | "\n", 22 | "**\\# 3. Catch a TooManyMessagesException and print the messages**\n", 23 | "\n", 24 | "Modify this code so that, if an exception is raised when the message is sent, the messages are printed out (emptying the message list) and the message is re-sent. Make sure to print out any remaining messages at the end!\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 11, 30 | "id": "4261d50d", 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "from datetime import datetime\n", 35 | "\n", 36 | "def getCurrentTime():\n", 37 | " return datetime.now().strftime(\"%m-%d-%Y %H:%M:%S\")\n", 38 | "\n", 39 | "\n", 40 | "class Messenger:\n", 41 | " def __init__(self, listeners=[]):\n", 42 | " self.listeners = listeners\n", 43 | " \n", 44 | " def send(self, message):\n", 45 | " for listener in self.listeners:\n", 46 | " listener.receive(message)\n", 47 | "\n", 48 | " def receive(self, message):\n", 49 | " pass\n", 50 | "\n", 51 | "# 1. Finish creating the TooManyMessagesException class\n", 52 | "class TooManyMessagesException:\n", 53 | " pass\n", 54 | "\n", 55 | "class SaveMessages(Messenger):\n", 56 | " def __init__(self, listeners=[]):\n", 57 | " super().__init__(listeners)\n", 58 | " self.messages = []\n", 59 | " self.max_messages = 10\n", 60 | " \n", 61 | " def receive(self, message):\n", 62 | " if len(self.messages) >= self.max_messages:\n", 63 | " #2. Raise a TooManyMessagesException exception here\n", 64 | " pass\n", 65 | " self.messages.append({'message': message, 'time': getCurrentTime()})\n", 66 | " \n", 67 | " def printMessages(self):\n", 68 | " for m in self.messages:\n", 69 | " print(f'Message: \"{m[\"message\"]}\" Time: {m[\"time\"]}')\n", 70 | " self.messages = []\n", 71 | "\n" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 12, 77 | "id": "d4a45805", 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "listener = SaveMessages()\n", 82 | "sender = Messenger([listener])" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 13, 88 | "id": "6baf6405", 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "# 3. Catch a TooManyMessagesException and print the messages \n", 93 | "for i in range(0, 20):\n", 94 | " sender.send(f'This is message {i}')" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "id": "59c39e74", 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [] 104 | } 105 | ], 106 | "metadata": { 107 | "kernelspec": { 108 | "display_name": "Python 3 (ipykernel)", 109 | "language": "python", 110 | "name": "python3" 111 | }, 112 | "language_info": { 113 | "codemirror_mode": { 114 | "name": "ipython", 115 | "version": 3 116 | }, 117 | "file_extension": ".py", 118 | "mimetype": "text/x-python", 119 | "name": "python", 120 | "nbconvert_exporter": "python", 121 | "pygments_lexer": "ipython3", 122 | "version": "3.9.13" 123 | } 124 | }, 125 | "nbformat": 4, 126 | "nbformat_minor": 5 127 | } 128 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/02_04_ControlFlow-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "4e835ee3", 6 | "metadata": {}, 7 | "source": [ 8 | "## Control Flow" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "2a65107b", 14 | "metadata": {}, 15 | "source": [ 16 | "### If / Else statements" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 5, 22 | "id": "03d16962", 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "It is true!\n", 30 | "Also print this\n", 31 | "Always print this\n" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "a = True\n", 37 | "if a:\n", 38 | " print('It is true!')\n", 39 | " print('Also print this')\n", 40 | "else:\n", 41 | " print('It is false!')\n", 42 | "print('Always print this')" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 8, 48 | "id": "c250abc7", 49 | "metadata": {}, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "It is true!\n", 56 | "Also print this\n", 57 | "Always print this\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "a = True\n", 63 | "b = False\n", 64 | "c = True\n", 65 | "if a:\n", 66 | " print('It is true!')\n", 67 | " print('Also print this')\n", 68 | " if b:\n", 69 | " print('Both are true')\n", 70 | " if c:\n", 71 | " print('All three are true')\n", 72 | "else:\n", 73 | " print('It is false!')\n", 74 | "print('Always print this')" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "id": "ecc930e8", 80 | "metadata": {}, 81 | "source": [ 82 | "### For loops" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 11, 88 | "id": "8b49fa67", 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "1\n", 96 | "2\n", 97 | "3\n", 98 | "4\n", 99 | "5\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "a = [1,2,3,4,5]\n", 105 | "for number in a:\n", 106 | " print(number)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 10, 112 | "id": "55d76100", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "data": { 117 | "text/plain": [ 118 | "True" 119 | ] 120 | }, 121 | "execution_count": 10, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "4 in a" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "id": "8b4d6361", 133 | "metadata": {}, 134 | "source": [ 135 | "### While loops" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 12, 141 | "id": "053eb644", 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "0\n", 149 | "1\n", 150 | "2\n", 151 | "3\n", 152 | "4\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "a = 0\n", 158 | "while a < 5:\n", 159 | " print(a)\n", 160 | " a = a + 1" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "id": "f8052766", 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [] 170 | } 171 | ], 172 | "metadata": { 173 | "kernelspec": { 174 | "display_name": "Python 3 (ipykernel)", 175 | "language": "python", 176 | "name": "python3" 177 | }, 178 | "language_info": { 179 | "codemirror_mode": { 180 | "name": "ipython", 181 | "version": 3 182 | }, 183 | "file_extension": ".py", 184 | "mimetype": "text/x-python", 185 | "name": "python", 186 | "nbconvert_exporter": "python", 187 | "pygments_lexer": "ipython3", 188 | "version": "3.10.2" 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 5 193 | } 194 | -------------------------------------------------------------------------------- /exercise_files/08_04_Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "522e4770", 6 | "metadata": {}, 7 | "source": [ 8 | "## Message Exceptions\n", 9 | "\n", 10 | "The SaveMessages class now has limited memory and should only be able to hold a maximum of 10 messages at once.\n", 11 | "\n", 12 | "This challenge has three parts, outlined in comments below. \n", 13 | "\n", 14 | "**\\# 1. Finish creating the TooManyMessagesException class**\n", 15 | "\n", 16 | "Fill in the TooManyMessagesException class. Add a custom message!\n", 17 | "\n", 18 | "**\\# 2. Raise a TooManyMessagesException exception here**\n", 19 | "\n", 20 | "Make sure that the SaveMessages class doesn't get over-full and raises an Exception if the max_messages limit is reached.\n", 21 | "\n", 22 | "**\\# 3. Catch a TooManyMessagesException and print the messages**\n", 23 | "\n", 24 | "Modify this code so that, if an exception is raised when the message is sent, the messages are printed out (emptying the message list) and the message is re-sent. Make sure to print out any remaining messages at the end!\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 11, 30 | "id": "4261d50d", 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "from datetime import datetime\n", 35 | "\n", 36 | "def getCurrentTime():\n", 37 | " return datetime.now().strftime(\"%m-%d-%Y %H:%M:%S\")\n", 38 | "\n", 39 | "\n", 40 | "class Messenger:\n", 41 | " def __init__(self, listeners=[]):\n", 42 | " self.listeners = listeners\n", 43 | " \n", 44 | " def send(self, message):\n", 45 | " for listener in self.listeners:\n", 46 | " listener.receive(message)\n", 47 | "\n", 48 | " def receive(self, message):\n", 49 | " pass\n", 50 | "\n", 51 | "# 1. Finish creating the TooManyMessagesException class\n", 52 | "class TooManyMessagesException:\n", 53 | " pass\n", 54 | "\n", 55 | "class SaveMessages(Messenger):\n", 56 | " def __init__(self, listeners=[]):\n", 57 | " super().__init__(listeners)\n", 58 | " self.messages = []\n", 59 | " self.max_messages = 10\n", 60 | " \n", 61 | " def receive(self, message):\n", 62 | " if len(self.messages) >= self.max_messages:\n", 63 | " #2. Raise a TooManyMessagesException exception here\n", 64 | " pass\n", 65 | " self.messages.append({'message': message, 'time': getCurrentTime()})\n", 66 | " \n", 67 | " def printMessages(self):\n", 68 | " for m in self.messages:\n", 69 | " print(f'Message: \"{m[\"message\"]}\" Time: {m[\"time\"]}')\n", 70 | " self.messages = []\n", 71 | "\n" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 12, 77 | "id": "d4a45805", 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "listener = SaveMessages()\n", 82 | "sender = Messenger([listener])" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 13, 88 | "id": "6baf6405", 89 | "metadata": {}, 90 | "outputs": [], 91 | "source": [ 92 | "# 3. Catch a TooManyMessagesException and print the messages \n", 93 | "for i in range(0, 20):\n", 94 | " sender.send(f'This is message {i}')" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "id": "59c39e74", 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [] 104 | } 105 | ], 106 | "metadata": { 107 | "kernelspec": { 108 | "display_name": "Python 3 (ipykernel)", 109 | "language": "python", 110 | "name": "python3" 111 | }, 112 | "language_info": { 113 | "codemirror_mode": { 114 | "name": "ipython", 115 | "version": 3 116 | }, 117 | "file_extension": ".py", 118 | "mimetype": "text/x-python", 119 | "name": "python", 120 | "nbconvert_exporter": "python", 121 | "pygments_lexer": "ipython3", 122 | "version": "3.9.13" 123 | }, 124 | "vscode": { 125 | "interpreter": { 126 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 127 | } 128 | } 129 | }, 130 | "nbformat": 4, 131 | "nbformat_minor": 5 132 | } 133 | -------------------------------------------------------------------------------- /exercise_files/05_04_Challenge_Hints.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "3d331168", 6 | "metadata": {}, 7 | "source": [ 8 | "## Faster Prime Finding\n", 9 | "\n", 10 | "Write a function that returns a list of all primes up to a given number.\n", 11 | "\n", 12 | "For each number, in order to determine if it is prime, take the following steps:\n", 13 | "1. Find the square root of the number\n", 14 | "2. Find all the primes up to that square root\n", 15 | "3. Test to see if any of those primes are divisors \n", 16 | "\n", 17 | "If a number has no prime divisors, it is prime!\n" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "id": "c722d7d2", 23 | "metadata": {}, 24 | "source": [ 25 | "### Challenge Hints!" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "id": "447e6304", 31 | "metadata": {}, 32 | "source": [ 33 | "**Hint 1:** The function should collect a list of prime numbers as goes. This is the list that it adds \"new primes\" to as well as the list it uses to check for factors.\n" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "60c7327b", 39 | "metadata": {}, 40 | "source": [ 41 | "**Hint 2:** Don't try to test whether or not 2 is prime. You can assume it's prime and just add it to your list to start" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "45c5dc7b", 47 | "metadata": {}, 48 | "source": [ 49 | "**Hint 3:** Try modifying the previously-written code:" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 1, 55 | "id": "747a1ebf", 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "2 is prime!\n", 63 | "3 is prime!\n", 64 | "5 is prime!\n", 65 | "7 is prime!\n", 66 | "11 is prime!\n", 67 | "13 is prime!\n", 68 | "17 is prime!\n", 69 | "19 is prime!\n", 70 | "23 is prime!\n", 71 | "29 is prime!\n", 72 | "31 is prime!\n", 73 | "37 is prime!\n", 74 | "41 is prime!\n", 75 | "43 is prime!\n", 76 | "47 is prime!\n", 77 | "53 is prime!\n", 78 | "59 is prime!\n", 79 | "61 is prime!\n", 80 | "67 is prime!\n", 81 | "71 is prime!\n", 82 | "73 is prime!\n", 83 | "79 is prime!\n", 84 | "83 is prime!\n", 85 | "89 is prime!\n", 86 | "97 is prime!\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "for number in range(2, 100):\n", 92 | " for factor in range(2, int(number ** 0.5) + 1):\n", 93 | " if number % factor == 0:\n", 94 | " break\n", 95 | " else:\n", 96 | " print(f'{number} is prime!')" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": null, 102 | "id": "b27bfac4", 103 | "metadata": {}, 104 | "outputs": [], 105 | "source": [] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 1, 110 | "id": "5fc8b019", 111 | "metadata": {}, 112 | "outputs": [], 113 | "source": [ 114 | "def allPrimesUpTo(num):\n", 115 | " pass" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 2, 121 | "id": "76e6a11b", 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "allPrimesUpTo(100)" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 3, 131 | "id": "0084237b", 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "allPrimesUpTo(1000)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "id": "9c863053", 142 | "metadata": {}, 143 | "outputs": [], 144 | "source": [] 145 | } 146 | ], 147 | "metadata": { 148 | "kernelspec": { 149 | "display_name": "Python 3.10.4 64-bit", 150 | "language": "python", 151 | "name": "python3" 152 | }, 153 | "language_info": { 154 | "codemirror_mode": { 155 | "name": "ipython", 156 | "version": 3 157 | }, 158 | "file_extension": ".py", 159 | "mimetype": "text/x-python", 160 | "name": "python", 161 | "nbconvert_exporter": "python", 162 | "pygments_lexer": "ipython3", 163 | "version": "3.10.4" 164 | }, 165 | "vscode": { 166 | "interpreter": { 167 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 168 | } 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 5 173 | } 174 | -------------------------------------------------------------------------------- /exercise_files/07_03_ClassInheritance.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "3ca28428", 6 | "metadata": {}, 7 | "source": [ 8 | "## Class Inheritance" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 6, 14 | "id": "06ce42b1", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class Dog:\n", 19 | " _legs = 4\n", 20 | " def __init__(self, name):\n", 21 | " self.name = name\n", 22 | "\n", 23 | " def speak(self):\n", 24 | " print(self.name + ' says: Bark!')\n", 25 | " \n", 26 | " def getLegs(self):\n", 27 | " return self._legs\n", 28 | "\n", 29 | "\n", 30 | "class Chihuahua(Dog):\n", 31 | " def speak(self):\n", 32 | " print(f'{self.name} says: Yap yap yap!')\n", 33 | " \n", 34 | " def wagTail(self):\n", 35 | " print('Vigorous wagging!')\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 7, 41 | "id": "dd5dd985", 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "Roxy says: Yap yap yap!\n", 49 | "Vigorous wagging!\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "dog = Chihuahua('Roxy')\n", 55 | "dog.speak()\n", 56 | "dog.wagTail()" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 5, 62 | "id": "9f367505", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Rover says: Bark!\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "myDog = Dog('Rover')\n", 75 | "myDog.speak()" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "id": "fb776450", 81 | "metadata": {}, 82 | "source": [ 83 | "### Extending built-in classes" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 8, 89 | "id": "352c540a", 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "myList = list()" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 9, 99 | "id": "fcf877f7", 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "[1, 2]\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "class UniqueList(list):\n", 112 | " def append(self, item):\n", 113 | " if item in self:\n", 114 | " return\n", 115 | " super().append(item)\n", 116 | " \n", 117 | "uniqueList = UniqueList()\n", 118 | "uniqueList.append(1)\n", 119 | "uniqueList.append(1)\n", 120 | "uniqueList.append(2)\n", 121 | "\n", 122 | "print(uniqueList)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 10, 128 | "id": "e6ec6f25", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "Unique List!\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "class UniqueList(list):\n", 141 | " \n", 142 | " def __init__(self):\n", 143 | " super().__init__()\n", 144 | " self.someProperty = 'Unique List!'\n", 145 | " \n", 146 | "\n", 147 | " def append(self, item):\n", 148 | " if item in self:\n", 149 | " return\n", 150 | " super().append(item)\n", 151 | " \n", 152 | "uniqueList = UniqueList()\n", 153 | "uniqueList.append(1)\n", 154 | "uniqueList.append(1)\n", 155 | "uniqueList.append(2)\n", 156 | "\n", 157 | "print(uniqueList.someProperty)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "id": "6ea4c632", 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [] 167 | } 168 | ], 169 | "metadata": { 170 | "kernelspec": { 171 | "display_name": "Python 3 (ipykernel)", 172 | "language": "python", 173 | "name": "python3" 174 | }, 175 | "language_info": { 176 | "codemirror_mode": { 177 | "name": "ipython", 178 | "version": 3 179 | }, 180 | "file_extension": ".py", 181 | "mimetype": "text/x-python", 182 | "name": "python", 183 | "nbconvert_exporter": "python", 184 | "pygments_lexer": "ipython3", 185 | "version": "3.9.13" 186 | } 187 | }, 188 | "nbformat": 4, 189 | "nbformat_minor": 5 190 | } 191 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/07_03_ClassInheritance-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "3ca28428", 6 | "metadata": {}, 7 | "source": [ 8 | "## Class Inheritance" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 6, 14 | "id": "06ce42b1", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "class Dog:\n", 19 | " _legs = 4\n", 20 | " def __init__(self, name):\n", 21 | " self.name = name\n", 22 | "\n", 23 | " def speak(self):\n", 24 | " print(self.name + ' says: Bark!')\n", 25 | " \n", 26 | " def getLegs(self):\n", 27 | " return self._legs\n", 28 | "\n", 29 | "\n", 30 | "class Chihuahua(Dog):\n", 31 | " def speak(self):\n", 32 | " print(f'{self.name} says: Yap yap yap!')\n", 33 | " \n", 34 | " def wagTail(self):\n", 35 | " print('Vigorous wagging!')\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 7, 41 | "id": "dd5dd985", 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "Roxy says: Yap yap yap!\n", 49 | "Vigorous wagging!\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "dog = Chihuahua('Roxy')\n", 55 | "dog.speak()\n", 56 | "dog.wagTail()" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 5, 62 | "id": "9f367505", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Rover says: Bark!\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "myDog = Dog('Rover')\n", 75 | "myDog.speak()" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "id": "fb776450", 81 | "metadata": {}, 82 | "source": [ 83 | "### Extending built-in classes" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 8, 89 | "id": "352c540a", 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "myList = list()" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 9, 99 | "id": "fcf877f7", 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "[1, 2]\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "class UniqueList(list):\n", 112 | " def append(self, item):\n", 113 | " if item in self:\n", 114 | " return\n", 115 | " super().append(item)\n", 116 | " \n", 117 | "uniqueList = UniqueList()\n", 118 | "uniqueList.append(1)\n", 119 | "uniqueList.append(1)\n", 120 | "uniqueList.append(2)\n", 121 | "\n", 122 | "print(uniqueList)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 10, 128 | "id": "e6ec6f25", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "Unique List!\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "class UniqueList(list):\n", 141 | " \n", 142 | " def __init__(self):\n", 143 | " super().__init__()\n", 144 | " self.someProperty = 'Unique List!'\n", 145 | " \n", 146 | "\n", 147 | " def append(self, item):\n", 148 | " if item in self:\n", 149 | " return\n", 150 | " super().append(item)\n", 151 | " \n", 152 | "uniqueList = UniqueList()\n", 153 | "uniqueList.append(1)\n", 154 | "uniqueList.append(1)\n", 155 | "uniqueList.append(2)\n", 156 | "\n", 157 | "print(uniqueList.someProperty)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "id": "6ea4c632", 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [] 167 | } 168 | ], 169 | "metadata": { 170 | "kernelspec": { 171 | "display_name": "Python 3 (ipykernel)", 172 | "language": "python", 173 | "name": "python3" 174 | }, 175 | "language_info": { 176 | "codemirror_mode": { 177 | "name": "ipython", 178 | "version": 3 179 | }, 180 | "file_extension": ".py", 181 | "mimetype": "text/x-python", 182 | "name": "python", 183 | "nbconvert_exporter": "python", 184 | "pygments_lexer": "ipython3", 185 | "version": "3.10.2" 186 | } 187 | }, 188 | "nbformat": 4, 189 | "nbformat_minor": 5 190 | } 191 | -------------------------------------------------------------------------------- /exercise_files/09_02_threads.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "48a15793", 7 | "metadata": { 8 | "scrolled": true 9 | }, 10 | "outputs": [], 11 | "source": [ 12 | "import threading\n", 13 | "import time" 14 | ] 15 | }, 16 | { 17 | "cell_type": "markdown", 18 | "id": "5be113b0", 19 | "metadata": {}, 20 | "source": [ 21 | "## Threads" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "id": "230c796a", 28 | "metadata": { 29 | "scrolled": true 30 | }, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "[0, 1, 4, 9, 16]" 36 | ] 37 | }, 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "def longSquare(num):\n", 45 | " time.sleep(1)\n", 46 | " return num**2\n", 47 | "\n", 48 | "[longSquare(n) for n in range(0, 5)]\n" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "id": "f105891d", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "t1 = threading.Thread(target=longSquare, args=(1,))\n", 59 | "t2 = threading.Thread(target=longSquare, args=(2,))\n", 60 | "\n", 61 | "t1.start()\n", 62 | "t2.start()\n", 63 | "\n", 64 | "t1.join()\n", 65 | "t2.join()\n" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "id": "5be33021", 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "{2: 4, 1: 1}\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "\n", 84 | "\n", 85 | "def longSquare(num, results):\n", 86 | " time.sleep(1)\n", 87 | " results[num] = num**2\n", 88 | "\n", 89 | "results = {}\n", 90 | "t1 = threading.Thread(target=longSquare, args=(1,results))\n", 91 | "t2 = threading.Thread(target=longSquare, args=(2,results))\n", 92 | "\n", 93 | "t1.start()\n", 94 | "t2.start()\n", 95 | "\n", 96 | "t1.join()\n", 97 | "t2.join()\n", 98 | "\n", 99 | "print(results)\n" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 6, 105 | "id": "6f9a6201", 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "{2: 4, 1: 1, 0: 0, 8: 64, 9: 81, 16: 256, 17: 289, 19: 361, 30: 900, 25: 625, 28: 784, 29: 841, 3: 9, 13: 169, 15: 225, 21: 441, 22: 484, 4: 16, 14: 196, 6: 36, 20: 400, 39: 1521, 40: 1600, 5: 25, 42: 1764, 43: 1849, 44: 1936, 45: 2025, 47: 2209, 7: 49, 38: 1444, 37: 1369, 32: 1024, 41: 1681, 46: 2116, 10: 100, 11: 121, 12: 144, 18: 324, 33: 1089, 23: 529, 70: 4900, 75: 5625, 82: 6724, 85: 7225, 86: 7396, 91: 8281, 92: 8464, 94: 8836, 95: 9025, 96: 9216, 97: 9409, 98: 9604, 99: 9801, 63: 3969, 81: 6561, 26: 676, 35: 1225, 36: 1296, 49: 2401, 50: 2500, 51: 2601, 52: 2704, 53: 2809, 60: 3600, 66: 4356, 74: 5476, 77: 5929, 79: 6241, 83: 6889, 84: 7056, 87: 7569, 93: 8649, 24: 576, 27: 729, 64: 4096, 61: 3721, 65: 4225, 69: 4761, 73: 5329, 78: 6084, 90: 8100, 62: 3844, 31: 961, 34: 1156, 67: 4489, 68: 4624, 80: 6400, 88: 7744, 89: 7921, 48: 2304, 57: 3249, 71: 5041, 72: 5184, 76: 5776, 54: 2916, 56: 3136, 55: 3025, 58: 3364, 59: 3481}\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "\n", 118 | "\n", 119 | "def longSquare(num, results):\n", 120 | " time.sleep(1)\n", 121 | " results[num] = num**2\n", 122 | "\n", 123 | "results = {}\n", 124 | "threads = [threading.Thread(target=longSquare, args=(n, results)) for n in range(0, 100)]\n", 125 | "[t.start() for t in threads]\n", 126 | "[t.join() for t in threads]\n", 127 | "print(results)\n" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "id": "c63a0969", 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | } 138 | ], 139 | "metadata": { 140 | "kernelspec": { 141 | "display_name": "Python 3 (ipykernel)", 142 | "language": "python", 143 | "name": "python3" 144 | }, 145 | "language_info": { 146 | "codemirror_mode": { 147 | "name": "ipython", 148 | "version": 3 149 | }, 150 | "file_extension": ".py", 151 | "mimetype": "text/x-python", 152 | "name": "python", 153 | "nbconvert_exporter": "python", 154 | "pygments_lexer": "ipython3", 155 | "version": "3.10.2" 156 | } 157 | }, 158 | "nbformat": 4, 159 | "nbformat_minor": 5 160 | } 161 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/09_02_Threads-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "48a15793", 7 | "metadata": { 8 | "scrolled": true 9 | }, 10 | "outputs": [], 11 | "source": [ 12 | "import threading\n", 13 | "import time" 14 | ] 15 | }, 16 | { 17 | "cell_type": "markdown", 18 | "id": "5be113b0", 19 | "metadata": {}, 20 | "source": [ 21 | "## Threads" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "id": "230c796a", 28 | "metadata": { 29 | "scrolled": true 30 | }, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "[0, 1, 4, 9, 16]" 36 | ] 37 | }, 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "def longSquare(num):\n", 45 | " time.sleep(1)\n", 46 | " return num**2\n", 47 | "\n", 48 | "[longSquare(n) for n in range(0, 5)]\n" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "id": "f105891d", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "t1 = threading.Thread(target=longSquare, args=(1,))\n", 59 | "t2 = threading.Thread(target=longSquare, args=(2,))\n", 60 | "\n", 61 | "t1.start()\n", 62 | "t2.start()\n", 63 | "\n", 64 | "t1.join()\n", 65 | "t2.join()\n" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "id": "5be33021", 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "{2: 4, 1: 1}\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "\n", 84 | "\n", 85 | "def longSquare(num, results):\n", 86 | " time.sleep(1)\n", 87 | " results[num] = num**2\n", 88 | "\n", 89 | "results = {}\n", 90 | "t1 = threading.Thread(target=longSquare, args=(1,results))\n", 91 | "t2 = threading.Thread(target=longSquare, args=(2,results))\n", 92 | "\n", 93 | "t1.start()\n", 94 | "t2.start()\n", 95 | "\n", 96 | "t1.join()\n", 97 | "t2.join()\n", 98 | "\n", 99 | "print(results)\n" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 6, 105 | "id": "6f9a6201", 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "{2: 4, 1: 1, 0: 0, 8: 64, 9: 81, 16: 256, 17: 289, 19: 361, 30: 900, 25: 625, 28: 784, 29: 841, 3: 9, 13: 169, 15: 225, 21: 441, 22: 484, 4: 16, 14: 196, 6: 36, 20: 400, 39: 1521, 40: 1600, 5: 25, 42: 1764, 43: 1849, 44: 1936, 45: 2025, 47: 2209, 7: 49, 38: 1444, 37: 1369, 32: 1024, 41: 1681, 46: 2116, 10: 100, 11: 121, 12: 144, 18: 324, 33: 1089, 23: 529, 70: 4900, 75: 5625, 82: 6724, 85: 7225, 86: 7396, 91: 8281, 92: 8464, 94: 8836, 95: 9025, 96: 9216, 97: 9409, 98: 9604, 99: 9801, 63: 3969, 81: 6561, 26: 676, 35: 1225, 36: 1296, 49: 2401, 50: 2500, 51: 2601, 52: 2704, 53: 2809, 60: 3600, 66: 4356, 74: 5476, 77: 5929, 79: 6241, 83: 6889, 84: 7056, 87: 7569, 93: 8649, 24: 576, 27: 729, 64: 4096, 61: 3721, 65: 4225, 69: 4761, 73: 5329, 78: 6084, 90: 8100, 62: 3844, 31: 961, 34: 1156, 67: 4489, 68: 4624, 80: 6400, 88: 7744, 89: 7921, 48: 2304, 57: 3249, 71: 5041, 72: 5184, 76: 5776, 54: 2916, 56: 3136, 55: 3025, 58: 3364, 59: 3481}\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "\n", 118 | "\n", 119 | "def longSquare(num, results):\n", 120 | " time.sleep(1)\n", 121 | " results[num] = num**2\n", 122 | "\n", 123 | "results = {}\n", 124 | "threads = [threading.Thread(target=longSquare, args=(n, results)) for n in range(0, 100)]\n", 125 | "[t.start() for t in threads]\n", 126 | "[t.join() for t in threads]\n", 127 | "print(results)\n" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "id": "c63a0969", 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [] 137 | } 138 | ], 139 | "metadata": { 140 | "kernelspec": { 141 | "display_name": "Python 3 (ipykernel)", 142 | "language": "python", 143 | "name": "python3" 144 | }, 145 | "language_info": { 146 | "codemirror_mode": { 147 | "name": "ipython", 148 | "version": 3 149 | }, 150 | "file_extension": ".py", 151 | "mimetype": "text/x-python", 152 | "name": "python", 153 | "nbconvert_exporter": "python", 154 | "pygments_lexer": "ipython3", 155 | "version": "3.10.2" 156 | } 157 | }, 158 | "nbformat": 4, 159 | "nbformat_minor": 5 160 | } 161 | -------------------------------------------------------------------------------- /exercise_files/07_05_Solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "dda9c8c6", 6 | "metadata": {}, 7 | "source": [ 8 | "## Extending the Messenger\n", 9 | "\n", 10 | "Create a class \"SaveMessages\" that extends the Messenger class that does the following things:\n", 11 | "\n", 12 | "- Add any messages it receives to a list, along with the time the message was received\n", 13 | "- Use the provided \"getCurrentTime\" function so that the received message time is a string\n", 14 | "- Contains a method called \"printMessages\" that prints all collected messages when it's called.\n", 15 | "\n", 16 | "You might also consider clearing the message list when \"printMessages\" is called. " 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "id": "35d4f1f5", 23 | "metadata": {}, 24 | "outputs": [], 25 | "source": [ 26 | "from datetime import datetime\n", 27 | "\n", 28 | "\n", 29 | "def getCurrentTime():\n", 30 | " return datetime.now().strftime(\"%m-%d-%Y %H:%M:%S\")\n", 31 | "\n", 32 | "\n", 33 | "class Messenger:\n", 34 | " def __init__(self, listeners=[]):\n", 35 | " self.listeners = listeners\n", 36 | " \n", 37 | " def send(self, message):\n", 38 | " for listener in self.listeners:\n", 39 | " listener.receive(message)\n", 40 | "\n", 41 | " def receive(self, message):\n", 42 | " # Must be implemented by extending classes\n", 43 | " pass\n", 44 | "\n", 45 | "\n", 46 | "class SaveMessages(Messenger):\n", 47 | " def __init__(self, listeners=[]):\n", 48 | " super().__init__(listeners)\n", 49 | " self.messages = []\n", 50 | " \n", 51 | " def receive(self, message):\n", 52 | " self.messages.append({'message': message, 'time': getCurrentTime()})\n", 53 | " \n", 54 | " def printMessages(self):\n", 55 | " for m in self.messages:\n", 56 | " print(f'Message: \"{m[\"message\"]}\" Time: {m[\"time\"]}')\n", 57 | " self.messages = []\n", 58 | "\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 2, 64 | "id": "608e3112", 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [ 68 | "# Run this cell after you've written your solution\n", 69 | "listener = SaveMessages()\n", 70 | "\n", 71 | "sender = Messenger([listener])\n", 72 | "\n", 73 | "sender.send('Hello, there! This is the first message')\n" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 3, 79 | "id": "2de8c179", 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "# Run this cell after you've written your solution\n", 84 | "sender.send('Oh hi! This is the second message!')" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 4, 90 | "id": "5731a38e", 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "# Run this cell after you've written your solution\n", 95 | "sender.send('Hola! This is the third and final message!')\n", 96 | "\n" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 5, 102 | "id": "6ee7752d", 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "Message: \"Hello, there! This is the first message\" Time: 10-09-2022 11:36:00\n", 110 | "Message: \"Oh hi! This is the second message!\" Time: 10-09-2022 11:36:02\n", 111 | "Message: \"Hola! This is the third and final message!\" Time: 10-09-2022 11:36:03\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "listener.printMessages()" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "id": "972c905b", 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [] 126 | } 127 | ], 128 | "metadata": { 129 | "kernelspec": { 130 | "display_name": "Python 3 (ipykernel)", 131 | "language": "python", 132 | "name": "python3" 133 | }, 134 | "language_info": { 135 | "codemirror_mode": { 136 | "name": "ipython", 137 | "version": 3 138 | }, 139 | "file_extension": ".py", 140 | "mimetype": "text/x-python", 141 | "name": "python", 142 | "nbconvert_exporter": "python", 143 | "pygments_lexer": "ipython3", 144 | "version": "3.9.13" 145 | }, 146 | "vscode": { 147 | "interpreter": { 148 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 149 | } 150 | } 151 | }, 152 | "nbformat": 4, 153 | "nbformat_minor": 5 154 | } 155 | -------------------------------------------------------------------------------- /exercise_files/02_05_Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ba586f0c", 6 | "metadata": {}, 7 | "source": [ 8 | "## Functions" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "dd1cfd33", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Hello, World!\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "print('Hello, World!')" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "id": "ad0049c8", 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "12" 39 | ] 40 | }, 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "def multiplyByThree(val):\n", 48 | " return 3 * val\n", 49 | "\n", 50 | "multiplyByThree(4)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 3, 56 | "id": "5b73fcaa", 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "def multiply(val1, val2):\n", 61 | " return val1 * val2" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "id": "9b62e051", 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "data": { 72 | "text/plain": [ 73 | "12" 74 | ] 75 | }, 76 | "execution_count": 4, 77 | "metadata": {}, 78 | "output_type": "execute_result" 79 | } 80 | ], 81 | "source": [ 82 | "multiply(3, 4)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 5, 88 | "id": "2cec9e99", 89 | "metadata": {}, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "[1, 2, 3, 4]\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "a = [1,2,3]\n", 101 | "\n", 102 | "def appendFour(myList):\n", 103 | " myList.append(4)\n", 104 | " \n", 105 | "appendFour(a)\n", 106 | "print(a)" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 7, 112 | "id": "beb2bc22", 113 | "metadata": {}, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "Hello, World!\n", 120 | "None\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "print(print('Hello, World!'))" 126 | ] 127 | }, 128 | { 129 | "cell_type": "code", 130 | "execution_count": 8, 131 | "id": "ebcd84f9", 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "NoneType" 138 | ] 139 | }, 140 | "execution_count": 8, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "type(None)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 9, 152 | "id": "856a2d61", 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "ename": "TypeError", 157 | "evalue": "unsupported operand type(s) for +: 'NoneType' and 'int'", 158 | "output_type": "error", 159 | "traceback": [ 160 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 161 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 162 | "Input \u001b[0;32mIn [9]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\n", 163 | "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'NoneType' and 'int'" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "None + 1" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "id": "8a011328", 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | } 179 | ], 180 | "metadata": { 181 | "kernelspec": { 182 | "display_name": "Python 3 (ipykernel)", 183 | "language": "python", 184 | "name": "python3" 185 | }, 186 | "language_info": { 187 | "codemirror_mode": { 188 | "name": "ipython", 189 | "version": 3 190 | }, 191 | "file_extension": ".py", 192 | "mimetype": "text/x-python", 193 | "name": "python", 194 | "nbconvert_exporter": "python", 195 | "pygments_lexer": "ipython3", 196 | "version": "3.10.2" 197 | } 198 | }, 199 | "nbformat": 4, 200 | "nbformat_minor": 5 201 | } 202 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/02_07_Challenge_Hints-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "48f2163c", 6 | "metadata": {}, 7 | "source": [ 8 | "## Factorial Challenge\n", 9 | "\n", 10 | "The factorial function gives the number of possible arrangements of a set of items of length \"n\"\n", 11 | "\n", 12 | "For example, there are 4! (\"four factorial\") or 24 ways to arrange four items, which can be calculated as: \n", 13 | "4 \\* 3 \\* 2 \\* 1\n", 14 | "\n", 15 | "5! = 5 \\* 4 \\* 3 \\* 2 \\* 1 = 120\n", 16 | "\n", 17 | "6! = 6 \\* 5 \\* 4 \\* 3 \\* 2 \\* 1 = 720\n", 18 | "\n", 19 | "etc.\n", 20 | "\n", 21 | "In a set of 0 items (an empty set) there is only one way to arrange the items, therefore, 0! = 1\n", 22 | "\n", 23 | "For the purposes of this exercise, factorials are only defined for **positive integers** (including 0)\n", 24 | "\n" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "76c229aa", 30 | "metadata": {}, 31 | "source": [ 32 | "### Challenge Hints!" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "102ebdab", 38 | "metadata": {}, 39 | "source": [ 40 | "**HINT 1:** You can figure out whether a variable is of a certain type in the following way:\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 14, 46 | "id": "df2ebdca", 47 | "metadata": { 48 | "scrolled": true 49 | }, 50 | "outputs": [ 51 | { 52 | "name": "stdout", 53 | "output_type": "stream", 54 | "text": [ 55 | "myVar is an integer!\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "myVar = 1\n", 61 | "\n", 62 | "if type(myVar) == int:\n", 63 | " print('myVar is an integer!')\n", 64 | "\n", 65 | "if type(myVar) != int:\n", 66 | " print('myVar is NOT an integer!')" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "id": "77f06600", 72 | "metadata": {}, 73 | "source": [ 74 | "**HINT 2:** You can create a while loop and manipulate multiple variables inside of it:" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 12, 80 | "id": "6ac23a6d", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "55\n" 88 | ] 89 | } 90 | ], 91 | "source": [ 92 | "# This prints the sum of all of the numbers from 1 to 10\n", 93 | "i = 0\n", 94 | "s = 0\n", 95 | "while i < 10:\n", 96 | " i = i + 1\n", 97 | " s = s + i\n", 98 | "print(s)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 16, 104 | "id": "92af0386", 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "# Returns the value of the factorial of num if it is defined, otherwise, returns None\n", 109 | "def factorial(num):\n", 110 | " pass\n" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 17, 116 | "id": "1d10be7a", 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [ 120 | "factorial(5)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 18, 126 | "id": "c43845c1", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "factorial(0)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 19, 136 | "id": "8f605415", 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "factorial(-2)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 20, 146 | "id": "5b973ff0", 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "factorial(1.2)" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 21, 156 | "id": "67f07cca", 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "factorial('spam spam spam spam spam spam')" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "id": "714148cd", 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [] 170 | } 171 | ], 172 | "metadata": { 173 | "kernelspec": { 174 | "display_name": "Python 3 (ipykernel)", 175 | "language": "python", 176 | "name": "python3" 177 | }, 178 | "language_info": { 179 | "codemirror_mode": { 180 | "name": "ipython", 181 | "version": 3 182 | }, 183 | "file_extension": ".py", 184 | "mimetype": "text/x-python", 185 | "name": "python", 186 | "nbconvert_exporter": "python", 187 | "pygments_lexer": "ipython3", 188 | "version": "3.9.13" 189 | } 190 | }, 191 | "nbformat": 4, 192 | "nbformat_minor": 5 193 | } 194 | -------------------------------------------------------------------------------- /exercise_files/08_03_CustomExceptions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b0f022fe", 6 | "metadata": {}, 7 | "source": [ 8 | "## Custom Exceptions" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 3, 14 | "id": "a15ba290", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "ename": "CustomException", 19 | "evalue": "You called the causeError function!", 20 | "output_type": "error", 21 | "traceback": [ 22 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 23 | "\u001b[0;31mCustomException\u001b[0m Traceback (most recent call last)", 24 | "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CustomException(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mYou called the causeError function!\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 8\u001b[0m \u001b[43mcauseError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 25 | "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36mcauseError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CustomException(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mYou called the causeError function!\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", 26 | "\u001b[0;31mCustomException\u001b[0m: You called the causeError function!" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "class CustomException(Exception):\n", 32 | " pass\n", 33 | "\n", 34 | "\n", 35 | "def causeError():\n", 36 | " raise CustomException('You called the causeError function!')\n", 37 | " \n", 38 | "causeError()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "b0cae9c9", 44 | "metadata": {}, 45 | "source": [ 46 | "### Adding Attributes" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 4, 52 | "id": "1b6caf83", 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "ename": "ServerError", 57 | "evalue": "Status code: 500 and message is: The server messed up!", 58 | "output_type": "error", 59 | "traceback": [ 60 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 61 | "\u001b[0;31mServerError\u001b[0m Traceback (most recent call last)", 62 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mraiseServerError\u001b[39m():\n\u001b[1;32m 16\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ServerError()\n\u001b[0;32m---> 18\u001b[0m \u001b[43mraiseServerError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 63 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36mraiseServerError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mraiseServerError\u001b[39m():\n\u001b[0;32m---> 16\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ServerError()\n", 64 | "\u001b[0;31mServerError\u001b[0m: Status code: 500 and message is: The server messed up!" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "class HttpException(Exception):\n", 70 | " statusCode = None\n", 71 | " message = None\n", 72 | " def __init__(self):\n", 73 | " super().__init__(f'Status code: {self.statusCode} and message is: {self.message}')\n", 74 | " \n", 75 | "class NotFound(HttpException):\n", 76 | " statusCode = 404\n", 77 | " message = 'Resource not found'\n", 78 | " \n", 79 | "class ServerError(HttpException):\n", 80 | " statusCode = 500\n", 81 | " message = 'The server messed up!'\n", 82 | " \n", 83 | "def raiseServerError():\n", 84 | " raise ServerError()\n", 85 | " \n", 86 | "raiseServerError()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "id": "2eabeda5", 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | } 97 | ], 98 | "metadata": { 99 | "kernelspec": { 100 | "display_name": "Python 3 (ipykernel)", 101 | "language": "python", 102 | "name": "python3" 103 | }, 104 | "language_info": { 105 | "codemirror_mode": { 106 | "name": "ipython", 107 | "version": 3 108 | }, 109 | "file_extension": ".py", 110 | "mimetype": "text/x-python", 111 | "name": "python", 112 | "nbconvert_exporter": "python", 113 | "pygments_lexer": "ipython3", 114 | "version": "3.9.13" 115 | } 116 | }, 117 | "nbformat": 4, 118 | "nbformat_minor": 5 119 | } 120 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/08_03_CustomExceptions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b0f022fe", 6 | "metadata": {}, 7 | "source": [ 8 | "## Custom Exceptions" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 3, 14 | "id": "a15ba290", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "ename": "CustomException", 19 | "evalue": "You called the causeError function!", 20 | "output_type": "error", 21 | "traceback": [ 22 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 23 | "\u001b[0;31mCustomException\u001b[0m Traceback (most recent call last)", 24 | "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CustomException(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mYou called the causeError function!\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 8\u001b[0m \u001b[43mcauseError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 25 | "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36mcauseError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[0;32m----> 6\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CustomException(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mYou called the causeError function!\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", 26 | "\u001b[0;31mCustomException\u001b[0m: You called the causeError function!" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "class CustomException(Exception):\n", 32 | " pass\n", 33 | "\n", 34 | "\n", 35 | "def causeError():\n", 36 | " raise CustomException('You called the causeError function!')\n", 37 | " \n", 38 | "causeError()" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "b0cae9c9", 44 | "metadata": {}, 45 | "source": [ 46 | "### Adding Attributes" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 4, 52 | "id": "1b6caf83", 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "ename": "ServerError", 57 | "evalue": "Status code: 500 and message is: The server messed up!", 58 | "output_type": "error", 59 | "traceback": [ 60 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 61 | "\u001b[0;31mServerError\u001b[0m Traceback (most recent call last)", 62 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mraiseServerError\u001b[39m():\n\u001b[1;32m 16\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ServerError()\n\u001b[0;32m---> 18\u001b[0m \u001b[43mraiseServerError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 63 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36mraiseServerError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mraiseServerError\u001b[39m():\n\u001b[0;32m---> 16\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ServerError()\n", 64 | "\u001b[0;31mServerError\u001b[0m: Status code: 500 and message is: The server messed up!" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "class HttpException(Exception):\n", 70 | " statusCode = None\n", 71 | " message = None\n", 72 | " def __init__(self):\n", 73 | " super().__init__(f'Status code: {self.statusCode} and message is: {self.message}')\n", 74 | " \n", 75 | "class NotFound(HttpException):\n", 76 | " statusCode = 404\n", 77 | " message = 'Resource not found'\n", 78 | " \n", 79 | "class ServerError(HttpException):\n", 80 | " statusCode = 500\n", 81 | " message = 'The server messed up!'\n", 82 | " \n", 83 | "def raiseServerError():\n", 84 | " raise ServerError()\n", 85 | " \n", 86 | "raiseServerError()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "id": "2eabeda5", 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [] 96 | } 97 | ], 98 | "metadata": { 99 | "kernelspec": { 100 | "display_name": "Python 3 (ipykernel)", 101 | "language": "python", 102 | "name": "python3" 103 | }, 104 | "language_info": { 105 | "codemirror_mode": { 106 | "name": "ipython", 107 | "version": 3 108 | }, 109 | "file_extension": ".py", 110 | "mimetype": "text/x-python", 111 | "name": "python", 112 | "nbconvert_exporter": "python", 113 | "pygments_lexer": "ipython3", 114 | "version": "3.10.2" 115 | } 116 | }, 117 | "nbformat": 4, 118 | "nbformat_minor": 5 119 | } 120 | -------------------------------------------------------------------------------- /exercise_files/08_01_Errors_and_Exceptions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7d005096", 6 | "metadata": {}, 7 | "source": [ 8 | "## Errors and Exceptions" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 3, 14 | "id": "926640b3", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "ename": "ZeroDivisionError", 19 | "evalue": "division by zero", 20 | "output_type": "error", 21 | "traceback": [ 22 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 23 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 24 | "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m1\u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m0\u001b[39m\n\u001b[0;32m----> 4\u001b[0m \u001b[43mcauseError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 25 | "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36mcauseError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n", 26 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "def causeError():\n", 32 | " return 1/0\n", 33 | "\n", 34 | "causeError()" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 4, 40 | "id": "145a7c4e", 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "ename": "ZeroDivisionError", 45 | "evalue": "division by zero", 46 | "output_type": "error", 47 | "traceback": [ 48 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 49 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 50 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcallCauseError\u001b[39m():\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m causeError()\n\u001b[0;32m----> 7\u001b[0m \u001b[43mcallCauseError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 51 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36mcallCauseError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcallCauseError\u001b[39m():\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcauseError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 52 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36mcauseError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n", 53 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "def causeError():\n", 59 | " return 1/0\n", 60 | "\n", 61 | "def callCauseError():\n", 62 | " return causeError()\n", 63 | "\n", 64 | "callCauseError()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "id": "4a0bef51", 70 | "metadata": {}, 71 | "source": [ 72 | "### Try / Except" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "id": "bced3e74", 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "try:\n", 91 | " 1/0\n", 92 | "except Exception as e:\n", 93 | " print(type(e))" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "id": "9863cebc", 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [] 103 | } 104 | ], 105 | "metadata": { 106 | "kernelspec": { 107 | "display_name": "Python 3 (ipykernel)", 108 | "language": "python", 109 | "name": "python3" 110 | }, 111 | "language_info": { 112 | "codemirror_mode": { 113 | "name": "ipython", 114 | "version": 3 115 | }, 116 | "file_extension": ".py", 117 | "mimetype": "text/x-python", 118 | "name": "python", 119 | "nbconvert_exporter": "python", 120 | "pygments_lexer": "ipython3", 121 | "version": "3.10.2" 122 | } 123 | }, 124 | "nbformat": 4, 125 | "nbformat_minor": 5 126 | } 127 | -------------------------------------------------------------------------------- /exercise_files/02_08_Solution.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "48f2163c", 6 | "metadata": {}, 7 | "source": [ 8 | "## Factorial Challenge\n", 9 | "\n", 10 | "The factorial function gives the number of possible arrangements of a set of items of length \"n\"\n", 11 | "\n", 12 | "For example, there are 4! (\"four factorial\") or 24 ways to arrange four items, which can be calculated as: \n", 13 | "4 \\* 3 \\* 2 \\* 1\n", 14 | "\n", 15 | "5! = 5 \\* 4 \\* 3 \\* 2 \\* 1 = 120\n", 16 | "\n", 17 | "6! = 6 \\* 5 \\* 4 \\* 3 \\* 2 \\* 1 = 720\n", 18 | "\n", 19 | "etc.\n", 20 | "\n", 21 | "In a set of 0 items (an empty set) there is only one way to arrange the items, therefore, 0! = 1\n", 22 | "\n", 23 | "For the purposes of this exercise, factorials are only defined for **positive integers** (including 0)\n" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "id": "92af0386", 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "# Returns the value of the factorial of num if it is defined, otherwise, returns None\n", 34 | "def factorial(num):\n", 35 | " if type(num) is not int:\n", 36 | " return None\n", 37 | " if num < 0:\n", 38 | " return None\n", 39 | " if num == 0:\n", 40 | " return 1\n", 41 | " \n", 42 | " i = 0\n", 43 | " f = 1\n", 44 | " while i < num:\n", 45 | " i = i + 1\n", 46 | " f = f * i\n", 47 | " \n", 48 | " return f" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 2, 54 | "id": "0682bb91", 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "# Returns the value of the factorial of num if it is defined, otherwise, returns None\n", 59 | "def factorial(num):\n", 60 | " if type(num) is not int:\n", 61 | " return None\n", 62 | " if num < 0:\n", 63 | " return None\n", 64 | " if num == 0:\n", 65 | " return 1\n", 66 | " \n", 67 | " return num * factorial(num - 1)\n" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 3, 73 | "id": "1d10be7a", 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "120" 80 | ] 81 | }, 82 | "execution_count": 3, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "# return 120\n", 89 | "factorial(5)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 4, 95 | "id": "129006a6", 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "720" 102 | ] 103 | }, 104 | "execution_count": 4, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "# return 720\n", 111 | "factorial(6)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 5, 117 | "id": "c43845c1", 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "data": { 122 | "text/plain": [ 123 | "1" 124 | ] 125 | }, 126 | "execution_count": 5, 127 | "metadata": {}, 128 | "output_type": "execute_result" 129 | } 130 | ], 131 | "source": [ 132 | "# return 1\n", 133 | "factorial(0)" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 6, 139 | "id": "8f605415", 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [ 143 | "# return None\n", 144 | "factorial(-2)" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 7, 150 | "id": "5b973ff0", 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "# return None\n", 155 | "factorial(1.2)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 8, 161 | "id": "67f07cca", 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "# return None\n", 166 | "factorial('spam spam spam spam spam spam')" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "id": "714148cd", 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [] 176 | } 177 | ], 178 | "metadata": { 179 | "kernelspec": { 180 | "display_name": "Python 3 (ipykernel)", 181 | "language": "python", 182 | "name": "python3" 183 | }, 184 | "language_info": { 185 | "codemirror_mode": { 186 | "name": "ipython", 187 | "version": 3 188 | }, 189 | "file_extension": ".py", 190 | "mimetype": "text/x-python", 191 | "name": "python", 192 | "nbconvert_exporter": "python", 193 | "pygments_lexer": "ipython3", 194 | "version": "3.9.13" 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 5 199 | } 200 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/08_01_Errors_and_Exceptions-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7d005096", 6 | "metadata": {}, 7 | "source": [ 8 | "## Errors and Exceptions" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 3, 14 | "id": "926640b3", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "ename": "ZeroDivisionError", 19 | "evalue": "division by zero", 20 | "output_type": "error", 21 | "traceback": [ 22 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 23 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 24 | "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m1\u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m0\u001b[39m\n\u001b[0;32m----> 4\u001b[0m \u001b[43mcauseError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 25 | "Input \u001b[0;32mIn [3]\u001b[0m, in \u001b[0;36mcauseError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n", 26 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "def causeError():\n", 32 | " return 1/0\n", 33 | "\n", 34 | "causeError()" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 4, 40 | "id": "145a7c4e", 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "ename": "ZeroDivisionError", 45 | "evalue": "division by zero", 46 | "output_type": "error", 47 | "traceback": [ 48 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 49 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 50 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcallCauseError\u001b[39m():\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m causeError()\n\u001b[0;32m----> 7\u001b[0m \u001b[43mcallCauseError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 51 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36mcallCauseError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcallCauseError\u001b[39m():\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcauseError\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", 52 | "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36mcauseError\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcauseError\u001b[39m():\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241;43m1\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n", 53 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "def causeError():\n", 59 | " return 1/0\n", 60 | "\n", 61 | "def callCauseError():\n", 62 | " return causeError()\n", 63 | "\n", 64 | "callCauseError()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "id": "4a0bef51", 70 | "metadata": {}, 71 | "source": [ 72 | "### Try / Except" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "id": "bced3e74", 79 | "metadata": {}, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "try:\n", 91 | " 1/0\n", 92 | "except Exception as e:\n", 93 | " print(type(e))" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "id": "9863cebc", 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [] 103 | } 104 | ], 105 | "metadata": { 106 | "kernelspec": { 107 | "display_name": "Python 3 (ipykernel)", 108 | "language": "python", 109 | "name": "python3" 110 | }, 111 | "language_info": { 112 | "codemirror_mode": { 113 | "name": "ipython", 114 | "version": 3 115 | }, 116 | "file_extension": ".py", 117 | "mimetype": "text/x-python", 118 | "name": "python", 119 | "nbconvert_exporter": "python", 120 | "pygments_lexer": "ipython3", 121 | "version": "3.10.2" 122 | } 123 | }, 124 | "nbformat": 4, 125 | "nbformat_minor": 5 126 | } 127 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/09_03_Processes-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 66, 6 | "id": "efb27ade", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "from multiprocess import Process\n", 11 | "import threading\n", 12 | "import time" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "id": "8935e418", 18 | "metadata": {}, 19 | "source": [ 20 | "## Processes" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 72, 26 | "id": "64198686", 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "name": "stdout", 31 | "output_type": "stream", 32 | "text": [ 33 | "1\n", 34 | "Finished computing 1 squared!4\n", 35 | "\n", 36 | "Finished computing 2 squared!\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "def longSquare(num, results):\n", 42 | " time.sleep(1)\n", 43 | " results[num] = num**2\n", 44 | " print(num**2)\n", 45 | " print(f'Finished computing {num} squared!')\n", 46 | "\n", 47 | "\n", 48 | "results = {}\n", 49 | "\n", 50 | "p1 = Process(target=longSquare, args=(1, results))\n", 51 | "p2 = Process(target=longSquare, args=(2, results))\n", 52 | "\n", 53 | "p1.start()\n", 54 | "p2.start()\n", 55 | "p1.join()\n", 56 | "p2.join()\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 74, 62 | "id": "61d5c310", 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "01\n", 70 | "Finished computing 0 squared!4\n", 71 | "9\n", 72 | "Finished computing 1 squared!\n", 73 | "\n", 74 | "16Finished computing 3 squared!25Finished computing 2 squared!\n", 75 | "\n", 76 | "36\n", 77 | "\n", 78 | "\n", 79 | "49Finished computing 4 squared!\n", 80 | "6481Finished computing 5 squared!\n", 81 | "\n", 82 | "\n", 83 | "Finished computing 6 squared!Finished computing 9 squared!\n", 84 | "Finished computing 7 squared!\n", 85 | "\n", 86 | "Finished computing 8 squared!\n", 87 | "\n", 88 | "\n" 89 | ] 90 | }, 91 | { 92 | "data": { 93 | "text/plain": [ 94 | "[None, None, None, None, None, None, None, None, None, None]" 95 | ] 96 | }, 97 | "execution_count": 74, 98 | "metadata": {}, 99 | "output_type": "execute_result" 100 | } 101 | ], 102 | "source": [ 103 | "def longSquare(num):\n", 104 | " print(num**2)\n", 105 | " print(f'Finished computing {num} squared!')\n", 106 | "processes = [Process(target=longSquare, args=(n,)) for n in range(0, 10)]\n", 107 | "[p.start() for p in processes]\n", 108 | "[p.join() for p in processes]" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 75, 114 | "id": "59125cc7", 115 | "metadata": {}, 116 | "outputs": [ 117 | { 118 | "name": "stdout", 119 | "output_type": "stream", 120 | "text": [ 121 | "0\n", 122 | "Finished computing 0 squared!\n", 123 | "14\n", 124 | "\n", 125 | "Finished computing 2 squared!\n", 126 | "9\n", 127 | "Finished computing 3 squared!\n", 128 | "Finished computing 1 squared!\n", 129 | "16\n", 130 | "Finished computing 4 squared!\n", 131 | "2536\n", 132 | "Finished computing 5 squared!\n", 133 | "49\n", 134 | "\n", 135 | "Finished computing 7 squared!\n", 136 | "Finished computing 6 squared!\n", 137 | "64\n", 138 | "Finished computing 8 squared!\n", 139 | "81\n", 140 | "Finished computing 9 squared!\n" 141 | ] 142 | }, 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "[None, None, None, None, None, None, None, None, None, None]" 147 | ] 148 | }, 149 | "execution_count": 75, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "threads = [threading.Thread(target=longSquare, args=(n,)) for n in range(0, 10)]\n", 156 | "[t.start() for t in threads]\n", 157 | "[t.join() for t in threads]" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "id": "241a1b54", 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [] 167 | } 168 | ], 169 | "metadata": { 170 | "kernelspec": { 171 | "display_name": "Python 3 (ipykernel)", 172 | "language": "python", 173 | "name": "python3" 174 | }, 175 | "language_info": { 176 | "codemirror_mode": { 177 | "name": "ipython", 178 | "version": 3 179 | }, 180 | "file_extension": ".py", 181 | "mimetype": "text/x-python", 182 | "name": "python", 183 | "nbconvert_exporter": "python", 184 | "pygments_lexer": "ipython3", 185 | "version": "3.10.2" 186 | } 187 | }, 188 | "nbformat": 4, 189 | "nbformat_minor": 5 190 | } 191 | -------------------------------------------------------------------------------- /exercise_files/07_02_StaticMethods.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ca340ac5", 6 | "metadata": {}, 7 | "source": [ 8 | "## Static and Instance Methods" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 4, 14 | "id": "abae6b1e", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "{'a', 'add', 'another', 'is', 'to', 'hi', 'sentence', 'here', 'want', 'i', 'ryan', 'im'}\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "class WordSet:\n", 27 | " def __init__(self):\n", 28 | " self.words = set()\n", 29 | " \n", 30 | " def addText(self, text):\n", 31 | " text = WordSet.cleanText(text)\n", 32 | " for word in text.split():\n", 33 | " self.words.add(word)\n", 34 | " \n", 35 | " \n", 36 | " def cleanText(text):\n", 37 | " # chaining functions\n", 38 | " text = text.replace('!', '').replace('.', '').replace(',', '').replace('\\'', '')\n", 39 | " return text.lower()\n", 40 | " \n", 41 | " \n", 42 | "wordSet = WordSet()\n", 43 | "\n", 44 | "wordSet.addText('Hi, I\\'m Ryan! Here is a sentence I want to add!')\n", 45 | "wordSet.addText('Here is another sentence I want to add.')\n", 46 | "\n", 47 | "print(wordSet.words)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 6, 53 | "id": "b022da1d", 54 | "metadata": {}, 55 | "outputs": [ 56 | { 57 | "name": "stdout", 58 | "output_type": "stream", 59 | "text": [ 60 | "{'a', 'add', 'another', 'is', 'to', 'hi', 'sentence', 'here', 'want', 'i', 'ryan', 'im'}\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "class WordSet:\n", 66 | " replacePuncs = ['!', '.', ',', '\\'']\n", 67 | " def __init__(self):\n", 68 | " self.words = set()\n", 69 | " \n", 70 | " def addText(self, text):\n", 71 | " text = WordSet.cleanText(text)\n", 72 | " for word in text.split():\n", 73 | " self.words.add(word)\n", 74 | " \n", 75 | " \n", 76 | " def cleanText(text):\n", 77 | " # chaining functions\n", 78 | " for punc in WordSet.replacePuncs:\n", 79 | " text = text.replace(punc, '')\n", 80 | " return text.lower()\n", 81 | " \n", 82 | " \n", 83 | "wordSet = WordSet()\n", 84 | "\n", 85 | "wordSet.addText('Hi, I\\'m Ryan! Here is a sentence I want to add!')\n", 86 | "wordSet.addText('Here is another sentence I want to add.')\n", 87 | "\n", 88 | "print(wordSet.words)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": null, 94 | "id": "b23df77f", 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "id": "bec9bbe4", 102 | "metadata": {}, 103 | "source": [ 104 | "### Decorators " 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 7, 110 | "id": "87b6c48c", 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "{'a', 'add', 'another', 'is', 'to', 'hi', 'sentence', 'here', 'want', 'i', 'ryan', 'im'}\n" 118 | ] 119 | } 120 | ], 121 | "source": [ 122 | "class WordSet:\n", 123 | " replacePuncs = ['!', '.', ',', '\\'']\n", 124 | " def __init__(self):\n", 125 | " self.words = set()\n", 126 | " \n", 127 | " def addText(self, text):\n", 128 | " text = self.cleanText(text)\n", 129 | " for word in text.split():\n", 130 | " self.words.add(word)\n", 131 | " \n", 132 | " @staticmethod\n", 133 | " def cleanText(text):\n", 134 | " # chaining functions\n", 135 | " for punc in WordSet.replacePuncs:\n", 136 | " text = text.replace(punc, '')\n", 137 | " return text.lower()\n", 138 | " \n", 139 | " \n", 140 | "wordSet = WordSet()\n", 141 | "\n", 142 | "wordSet.addText('Hi, I\\'m Ryan! Here is a sentence I want to add!')\n", 143 | "wordSet.addText('Here is another sentence I want to add.')\n", 144 | "\n", 145 | "print(wordSet.words)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "id": "bf70671d", 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [] 155 | } 156 | ], 157 | "metadata": { 158 | "kernelspec": { 159 | "display_name": "Python 3 (ipykernel)", 160 | "language": "python", 161 | "name": "python3" 162 | }, 163 | "language_info": { 164 | "codemirror_mode": { 165 | "name": "ipython", 166 | "version": 3 167 | }, 168 | "file_extension": ".py", 169 | "mimetype": "text/x-python", 170 | "name": "python", 171 | "nbconvert_exporter": "python", 172 | "pygments_lexer": "ipython3", 173 | "version": "3.10.4" 174 | } 175 | }, 176 | "nbformat": 4, 177 | "nbformat_minor": 5 178 | } 179 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/07_04_Challenge_Hints-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "dda9c8c6", 6 | "metadata": {}, 7 | "source": [ 8 | "## Extending the Messenger\n", 9 | "\n", 10 | "A common pattern (or \"way of doing things\") in programming is to send messages between senders and receivers. The receivers of the messages are sometimes called \"listeners\" -- the wait and listen for the messages to be sent. Once they receive the message, what they do with it is up to them!\n", 11 | "\n", 12 | "- Create a class \"SaveMessages\" that extends the Messenger class\n", 13 | " - SaveMessages should add any messages it receives to a list, along with the time it was saved\n", 14 | " - Use the provided \"getCurrentTime\" function to get the current time as a string!\n", 15 | "- Add a method called \"printMessages\" to the SaveMessages class that will print all collected messages on request.\n", 16 | "- Run the provided code to see your solution in action\n" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "f4a633c4", 22 | "metadata": {}, 23 | "source": [ 24 | "**Hint 1:** Make sure to override the \"receive\" method in your SaveMessages class, in addition to adding the \"printMessages\" method. You'll want something like this:" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 6, 30 | "id": "41574cef", 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [ 34 | "class SaveMessages:\n", 35 | " def receive(self, message):\n", 36 | " # Save the message here!\n", 37 | " pass\n", 38 | "\n", 39 | " def printMessages(self):\n", 40 | " # print your messages here!\n", 41 | " pass" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "0a4e8a12", 47 | "metadata": {}, 48 | "source": [ 49 | "**Hint 2:** Consider using an array of dictionaries to hold the message data" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 14, 55 | "id": "bfb3981c", 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "Message: \"Hello, there! This is the first message\" Time: 08-30-2022 21:18:09\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "message = 'Hello, there! This is the first message'\n", 68 | "\n", 69 | "messages = []\n", 70 | "messages.append({'message': message, 'time': getCurrentTime()})\n", 71 | "\n", 72 | "for m in messages:\n", 73 | " print(f'Message: \"{m[\"message\"]}\" Time: {m[\"time\"]}')" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 9, 79 | "id": "35d4f1f5", 80 | "metadata": {}, 81 | "outputs": [], 82 | "source": [ 83 | "from datetime import datetime\n", 84 | "\n", 85 | "def getCurrentTime():\n", 86 | " return datetime.now().strftime(\"%m-%d-%Y %H:%M:%S\")\n", 87 | "\n", 88 | "\n", 89 | "class Messenger:\n", 90 | " def __init__(self, listeners=[]):\n", 91 | " self.listeners = listeners\n", 92 | " \n", 93 | " def send(self, message):\n", 94 | " for listener in self.listeners:\n", 95 | " listener.receive(message)\n", 96 | "\n", 97 | " def receive(self, message):\n", 98 | " # Must be implemented by extending classes\n", 99 | " pass\n", 100 | "\n", 101 | "\n", 102 | "class SaveMessages(Messenger):\n", 103 | " # Your code here!\n", 104 | " pass\n", 105 | "\n" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 15, 111 | "id": "608e3112", 112 | "metadata": {}, 113 | "outputs": [], 114 | "source": [ 115 | "# Run this cell after you've written your solution\n", 116 | "listener = SaveMessages()\n", 117 | "\n", 118 | "sender = Messenger([listener])\n", 119 | "\n", 120 | "sender.send('Hello, there! This is the first message')\n" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "id": "2de8c179", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "# Run this cell after you've written your solution\n", 131 | "sender.send('Oh hi! This is the second message!')" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": null, 137 | "id": "5731a38e", 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "# Run this cell after you've written your solution\n", 142 | "sender.send('Hola! This is the third and final message!')\n", 143 | "\n", 144 | "listener.printMessages()" 145 | ] 146 | } 147 | ], 148 | "metadata": { 149 | "kernelspec": { 150 | "display_name": "Python 3 (ipykernel)", 151 | "language": "python", 152 | "name": "python3" 153 | }, 154 | "language_info": { 155 | "codemirror_mode": { 156 | "name": "ipython", 157 | "version": 3 158 | }, 159 | "file_extension": ".py", 160 | "mimetype": "text/x-python", 161 | "name": "python", 162 | "nbconvert_exporter": "python", 163 | "pygments_lexer": "ipython3", 164 | "version": "3.9.13" 165 | } 166 | }, 167 | "nbformat": 4, 168 | "nbformat_minor": 5 169 | } 170 | -------------------------------------------------------------------------------- /exercise_files/10_04_Challenge_Hints.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b8bb2983", 6 | "metadata": {}, 7 | "source": [ 8 | "## ASCII Art Compression\n", 9 | "\n", 10 | "Use the \"encodeString\" and \"decodeString\" functions from the Chapter 4 challenge, provided below\n", 11 | "\n", 12 | "Read in the ASCII art text file 10_04_challenge_art.txt and write it back to a new file that has a smaller file size than the original file. \n", 13 | "For example, the original 10_04_challenge_art.txt has a file size of 2.757kB (or 2,757 ASCII characters).\n", 14 | "\n", 15 | "- Any compression is great!\n", 16 | "- Is there any way you could get this file to 1kb?\n", 17 | "- Less than 1kb?\n", 18 | "\n", 19 | "After compressing the file, make sure to check your work by opening and decoding it again!\n", 20 | "\n" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "a354bdd4", 26 | "metadata": {}, 27 | "source": [ 28 | "**HINT 1:** Doing something like this will technically meet the requirements of this challenge:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 6, 34 | "id": "312efa3a", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "json.dumps(encodeString(text))" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "df78ab6f", 44 | "metadata": {}, 45 | "source": [ 46 | "However, I hope you can find a more efficient compression algorithm than that!" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "8ae1d0ad", 52 | "metadata": {}, 53 | "source": [ 54 | "**HINT 2:** Writing a list of tuples, there are a lot of instances of \"),(\" and lots of extra quotes and things, which is a lot of characters to devote to where perhaps a single comma would suffice..." 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "a13f0f7b", 60 | "metadata": {}, 61 | "source": [ 62 | "**HINT 3:** If you're looking for a longer challenge, you can look into writing bytes to a file. This is absolutely not necessary, however!" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 8, 68 | "id": "0ec1174a", 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "import os\n", 73 | "\n", 74 | "def encodeString(stringVal):\n", 75 | " encodedList = []\n", 76 | " prevChar = None\n", 77 | " count = 0\n", 78 | " for char in stringVal:\n", 79 | " if prevChar != char and prevChar is not None:\n", 80 | " encodedList.append((prevChar, count))\n", 81 | " count = 0\n", 82 | " prevChar = char\n", 83 | " count = count + 1\n", 84 | " encodedList.append((prevChar, count))\n", 85 | " return encodedList\n", 86 | "\n", 87 | "def decodeString(encodedList):\n", 88 | " decodedStr = ''\n", 89 | " for item in encodedList:\n", 90 | " decodedStr = decodedStr + item[0] * item[1]\n", 91 | " return decodedStr" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 9, 97 | "id": "d4bb1637", 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "\n", 102 | "def encodeFile(filename, newFilename):\n", 103 | " # Your code here!\n", 104 | " pass\n", 105 | "\n", 106 | "def decodeFile(filename):\n", 107 | " # Your code here!\n", 108 | " pass\n", 109 | "\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 10, 115 | "id": "2740024f", 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "Original file size: 2757\n", 123 | "New file size: 1007\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "print(f'Original file size: {os.path.getsize(\"10_04_challenge_art.txt\")}')\n", 129 | "\n", 130 | "encodeFile('10_04_challenge_art.txt', '10_04_challenge_art_encoded.txt')\n", 131 | "\n", 132 | "print(f'New file size: {os.path.getsize(\"10_04_challenge_art_encoded.txt\")}')\n", 133 | "\n", 134 | "\n", 135 | "\n", 136 | "\n" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 11, 142 | "id": "985e9278", 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "decodeFile('10_04_challenge_art_encoded.txt')" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "id": "9fe8cf03", 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [] 156 | } 157 | ], 158 | "metadata": { 159 | "kernelspec": { 160 | "display_name": "Python 3 (ipykernel)", 161 | "language": "python", 162 | "name": "python3" 163 | }, 164 | "language_info": { 165 | "codemirror_mode": { 166 | "name": "ipython", 167 | "version": 3 168 | }, 169 | "file_extension": ".py", 170 | "mimetype": "text/x-python", 171 | "name": "python", 172 | "nbconvert_exporter": "python", 173 | "pygments_lexer": "ipython3", 174 | "version": "3.9.13" 175 | }, 176 | "vscode": { 177 | "interpreter": { 178 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 179 | } 180 | } 181 | }, 182 | "nbformat": 4, 183 | "nbformat_minor": 5 184 | } 185 | -------------------------------------------------------------------------------- /exercise_files/.ipynb_checkpoints/10_04_Challenge_Hints-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "b8bb2983", 6 | "metadata": {}, 7 | "source": [ 8 | "## ASCII Art Compression\n", 9 | "\n", 10 | "Use the \"encodeString\" and \"decodeString\" functions from the Chapter 4 challenge, provided below\n", 11 | "\n", 12 | "Read in the ASCII art text file 10_04_challenge_art.txt and write it back to a new file that has a smaller file size than the original file. \n", 13 | "For example, the original 10_04_challenge_art.txt has a file size of 2.757kB (or 2,757 ASCII characters).\n", 14 | "\n", 15 | "- Any compression is great!\n", 16 | "- Is there any way you could get this file to 1kb?\n", 17 | "- Less than 1kb?\n", 18 | "\n", 19 | "After compressing the file, make sure to check your work by opening and decoding it again!\n", 20 | "\n" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "a354bdd4", 26 | "metadata": {}, 27 | "source": [ 28 | "**HINT 1:** Doing something like this will technically meet the requirements of this challenge:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 6, 34 | "id": "312efa3a", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "json.dumps(encodeString(text))" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "df78ab6f", 44 | "metadata": {}, 45 | "source": [ 46 | "However, I hope you can find a more efficient compression algorithm than that!" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "8ae1d0ad", 52 | "metadata": {}, 53 | "source": [ 54 | "**HINT 2:** Writing a list of tuples, there are a lot of instances of \"),(\" and lots of extra quotes and things, which is a lot of characters to devote to where perhaps a single comma would suffice..." 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "a13f0f7b", 60 | "metadata": {}, 61 | "source": [ 62 | "**HINT 3:** If you're looking for a longer challenge, you can look into writing bytes to a file. This is absolutely not necessary, however!" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 8, 68 | "id": "0ec1174a", 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "import os\n", 73 | "\n", 74 | "def encodeString(stringVal):\n", 75 | " encodedList = []\n", 76 | " prevChar = None\n", 77 | " count = 0\n", 78 | " for char in stringVal:\n", 79 | " if prevChar != char and prevChar is not None:\n", 80 | " encodedList.append((prevChar, count))\n", 81 | " count = 0\n", 82 | " prevChar = char\n", 83 | " count = count + 1\n", 84 | " encodedList.append((prevChar, count))\n", 85 | " return encodedList\n", 86 | "\n", 87 | "def decodeString(encodedList):\n", 88 | " decodedStr = ''\n", 89 | " for item in encodedList:\n", 90 | " decodedStr = decodedStr + item[0] * item[1]\n", 91 | " return decodedStr" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 9, 97 | "id": "d4bb1637", 98 | "metadata": {}, 99 | "outputs": [], 100 | "source": [ 101 | "\n", 102 | "def encodeFile(filename, newFilename):\n", 103 | " # Your code here!\n", 104 | " pass\n", 105 | "\n", 106 | "def decodeFile(filename):\n", 107 | " # Your code here!\n", 108 | " pass\n", 109 | "\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 10, 115 | "id": "2740024f", 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "Original file size: 2757\n", 123 | "New file size: 1007\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "print(f'Original file size: {os.path.getsize(\"10_04_challenge_art.txt\")}')\n", 129 | "\n", 130 | "encodeFile('10_04_challenge_art.txt', '10_04_challenge_art_encoded.txt')\n", 131 | "\n", 132 | "print(f'New file size: {os.path.getsize(\"10_04_challenge_art_encoded.txt\")}')\n", 133 | "\n", 134 | "\n", 135 | "\n", 136 | "\n" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 11, 142 | "id": "985e9278", 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "decodeFile('10_04_challenge_art_encoded.txt')" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "id": "9fe8cf03", 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [] 156 | } 157 | ], 158 | "metadata": { 159 | "kernelspec": { 160 | "display_name": "Python 3 (ipykernel)", 161 | "language": "python", 162 | "name": "python3" 163 | }, 164 | "language_info": { 165 | "codemirror_mode": { 166 | "name": "ipython", 167 | "version": 3 168 | }, 169 | "file_extension": ".py", 170 | "mimetype": "text/x-python", 171 | "name": "python", 172 | "nbconvert_exporter": "python", 173 | "pygments_lexer": "ipython3", 174 | "version": "3.9.13" 175 | }, 176 | "vscode": { 177 | "interpreter": { 178 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 179 | } 180 | } 181 | }, 182 | "nbformat": 4, 183 | "nbformat_minor": 5 184 | } 185 | -------------------------------------------------------------------------------- /exercise_files/04_06_Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6a6c3649", 6 | "metadata": {}, 7 | "source": [ 8 | "## ASCII Art Encoding\n", 9 | "\n", 10 | "Write a function \"encodeString\" that will encode a string like 'AAAAABBBBAAA' as a list of tuples: [('A', 5), ('B', 4), ('A', 3)] meaning that the string has \"5 A's, followed by 4 B's, followed by 3 A's\"\n", 11 | "\n", 12 | "Then use that function to compress a string containing \"ASCII Art\" (https://en.wikipedia.org/wiki/ASCII_art)\n", 13 | "\n", 14 | "Write a corresponding function \"decodeString\" that will take in a list of tuples and print the original string.\n" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "id": "7777d38e", 21 | "metadata": {}, 22 | "outputs": [], 23 | "source": [ 24 | "def encodeString(stringVal):\n", 25 | " pass\n", 26 | "\n", 27 | "def decodeString(encodedList):\n", 28 | " pass" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "id": "3eee6256", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "art = '''\n", 39 | "\n", 40 | " \n", 41 | " \n", 42 | " %%%%%%%%%%%%%%%%%%% \n", 43 | " %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \n", 44 | " %%%%%%%% %%%%%%%% \n", 45 | " %%%%%%% %%%%%% \n", 46 | " %%%%%% %%%%%% \n", 47 | " %%%%%% %%%%% \n", 48 | " %%%%% %%%%% \n", 49 | " %%%%% %%%%% \n", 50 | " %%%% %%%%% %%%%% %%%% \n", 51 | " %%%% %%%%%%% %%%%%%% %%%% \n", 52 | " %%%% %%%%%%% %%%%%%% %%%% \n", 53 | " %%%% %%%%%%% %%%%%%% %%%% \n", 54 | " %%%% %%%%% %%%%% %%%% \n", 55 | " %%%% %%%% \n", 56 | " %%%% %%%% \n", 57 | " %%%% %%%% \n", 58 | " %%%% %%%% %%%% \n", 59 | " %%%% %%%%%% %%%%% %%%% \n", 60 | " %%%% %%%% %%%% %%%% \n", 61 | " %%%% %%%% %%%% %%%% \n", 62 | " %%%% %%%%% %%%% %%%% \n", 63 | " %%%%% %%%%% %%%%% %%%%% \n", 64 | " %%%%% %%%%%% %%%%% %%%% \n", 65 | " %%%%% %%%%%%% %%%%%%% %%%%% \n", 66 | " %%%%% %%%%%%%%%%%%%%%%%%%%% %%%%% \n", 67 | " %%%%%%% %%%%% \n", 68 | " %%%%%%% %%%%%%% \n", 69 | " %%%%%%%%% %%%%%%%%% \n", 70 | " %%%%%%%%%%%%%%%%%%%%%%%%%%%%% \n", 71 | " %%%%%%%%%%%% \n", 72 | " \n", 73 | " \n", 74 | "\n", 75 | "'''" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "id": "dec27033", 82 | "metadata": {}, 83 | "outputs": [], 84 | "source": [ 85 | "encodedString = encodeString(art)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "id": "942d4ff8", 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [ 95 | "decodeString(encodedString)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "id": "e1485f53", 102 | "metadata": {}, 103 | "outputs": [], 104 | "source": [] 105 | } 106 | ], 107 | "metadata": { 108 | "kernelspec": { 109 | "display_name": "Python 3.10.4 64-bit", 110 | "language": "python", 111 | "name": "python3" 112 | }, 113 | "language_info": { 114 | "codemirror_mode": { 115 | "name": "ipython", 116 | "version": 3 117 | }, 118 | "file_extension": ".py", 119 | "mimetype": "text/x-python", 120 | "name": "python", 121 | "nbconvert_exporter": "python", 122 | "pygments_lexer": "ipython3", 123 | "version": "3.10.4" 124 | }, 125 | "vscode": { 126 | "interpreter": { 127 | "hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1" 128 | } 129 | } 130 | }, 131 | "nbformat": 4, 132 | "nbformat_minor": 5 133 | } 134 | --------------------------------------------------------------------------------