diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 000000000000..f7e6eb212bc8 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,4 @@ +[report] +sort = Cover +omit = + .env/* diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000000..176a458f94e0 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 000000000000..260b9704eda7 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,88 @@ +# This is a comment. +# Each line is a file pattern followed by one or more owners. + +# More details are here: https://help.github.com/articles/about-codeowners/ + +# The '*' pattern is global owners. + +# Order is important. The last matching pattern has the most precedence. + +/.* @cclauss @dhruvmanila + +# /arithmetic_analysis/ + +# /backtracking/ + +# /bit_manipulation/ + +# /blockchain/ + +# /boolean_algebra/ + +# /cellular_automata/ + +# /ciphers/ @cclauss # TODO: Uncomment this line after Hacktoberfest + +# /compression/ + +# /computer_vision/ + +# /conversions/ @cclauss # TODO: Uncomment this line after Hacktoberfest + +# /data_structures/ @cclauss # TODO: Uncomment this line after Hacktoberfest + +/digital_image_processing/ @mateuszz0000 + +# /divide_and_conquer/ + +/dynamic_programming/ @Kush1101 + +# /file_transfer/ + +# /fuzzy_logic/ + +# /genetic_algorithm/ + +# /geodesy/ + +# /graphics/ + +# /graphs/ + +# /greedy_method/ + +# /hashes/ + +# /images/ + +# /linear_algebra/ + +# /machine_learning/ + +/maths/ @Kush1101 + +# /matrix/ + +# /networking_flow/ + +# /neural_network/ + +# /other/ @cclauss # TODO: Uncomment this line after Hacktoberfest + +/project_euler/ @dhruvmanila @Kush1101 + +# /quantum/ + +# /scheduling/ + +# /scripts/ + +# /searches/ + +/sorts/ @mateuszz0000 + +# /strings/ @cclauss # TODO: Uncomment this line after Hacktoberfest + +# /traversals/ + +/web_programming/ @cclauss diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 000000000000..103ecf7c288a --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,20 @@ +### **Describe your change:** + + + +* [ ] Add an algorithm? +* [ ] Fix a bug or typo in an existing algorithm? +* [ ] Documentation change? + +### **Checklist:** +* [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md). +* [ ] This pull request is all my own work -- I have not plagiarized. +* [ ] I know that pull requests will not be merged if they fail the automated tests. +* [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms. +* [ ] All new Python files are placed inside an existing directory. +* [ ] All filenames are in all lowercase characters with no spaces or dashes. +* [ ] All functions and variable names follow Python naming conventions. +* [ ] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html). +* [ ] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing. +* [ ] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation. +* [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`. diff --git a/.github/stale.yml b/.github/stale.yml new file mode 100644 index 000000000000..36ca56266b26 --- /dev/null +++ b/.github/stale.yml @@ -0,0 +1,63 @@ +# Configuration for probot-stale - https://github.com/probot/stale + +# Number of days of inactivity before an Issue or Pull Request becomes stale +daysUntilStale: 30 + +# Number of days of inactivity before an Issue or Pull Request with the stale label is closed. +# Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale. +daysUntilClose: 7 + +# Only issues or pull requests with all of these labels are check if stale. Defaults to `[]` (disabled) +onlyLabels: [] + +# Issues or Pull Requests with these labels will never be considered stale. Set to `[]` to disable +exemptLabels: + - "Status: on hold" + +# Set to true to ignore issues in a project (defaults to false) +exemptProjects: false + +# Set to true to ignore issues in a milestone (defaults to false) +exemptMilestones: false + +# Set to true to ignore issues with an assignee (defaults to false) +exemptAssignees: false + +# Label to use when marking as stale +staleLabel: stale + +# Limit the number of actions per hour, from 1-30. Default is 30 +limitPerRun: 5 + +# Comment to post when removing the stale label. +# unmarkComment: > +# Your comment here. + +# Optionally, specify configuration settings that are specific to just 'issues' or 'pulls': +pulls: + # Comment to post when marking as stale. Set to `false` to disable + markComment: > + This pull request has been automatically marked as stale because it has not had + recent activity. It will be closed if no further activity occurs. Thank you + for your contributions. + + # Comment to post when closing a stale Pull Request. + closeComment: > + Please reopen this pull request once you commit the changes requested + or make improvements on the code. If this is not the case and you need + some help, feel free to seek help from our [Gitter](https://gitter.im/TheAlgorithms) + or ping one of the reviewers. Thank you for your contributions! + +issues: + # Comment to post when marking as stale. Set to `false` to disable + markComment: > + This issue has been automatically marked as stale because it has not had + recent activity. It will be closed if no further activity occurs. Thank you + for your contributions. + + # Comment to post when closing a stale Issue. + closeComment: > + Please reopen this issue once you add more information and updates here. + If this is not the case and you need some help, feel free to seek help + from our [Gitter](https://gitter.im/TheAlgorithms) or ping one of the + reviewers. Thank you for your contributions! diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 000000000000..9e15d18ade8e --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,27 @@ +name: "build" + +on: + pull_request: + schedule: + - cron: "0 0 * * *" # Run everyday + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: "3.9" + - uses: actions/cache@v2 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip setuptools six wheel + python -m pip install pytest-cov -r requirements.txt + - name: Run tests + run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. . + - if: ${{ success() }} + run: scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md diff --git a/.github/workflows/directory_writer.yml b/.github/workflows/directory_writer.yml new file mode 100644 index 000000000000..be8154a32696 --- /dev/null +++ b/.github/workflows/directory_writer.yml @@ -0,0 +1,21 @@ +# The objective of this GitHub Action is to update the DIRECTORY.md file (if needed) +# when doing a git push +name: directory_writer +on: [push] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 # v1, NOT v2 + - uses: actions/setup-python@v2 + - name: Write DIRECTORY.md + run: | + scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md + git config --global user.name github-actions + git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY + - name: Update DIRECTORY.md + run: | + git add DIRECTORY.md + git commit -am "updating DIRECTORY.md" || true + git push --force origin HEAD:$GITHUB_REF || true diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 000000000000..96175cfecea5 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,21 @@ +name: pre-commit + +on: [push, pull_request] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/cache@v2 + with: + path: | + ~/.cache/pre-commit + ~/.cache/pip + key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }} + - uses: actions/setup-python@v2 + - name: Install pre-commit + run: | + python -m pip install --upgrade pip + python -m pip install --upgrade pre-commit + - run: pre-commit run --verbose --all-files --show-diff-on-failure diff --git a/.github/workflows/project_euler.yml b/.github/workflows/project_euler.yml new file mode 100644 index 000000000000..995295fcaa9a --- /dev/null +++ b/.github/workflows/project_euler.yml @@ -0,0 +1,35 @@ +on: + pull_request: + # Run only if a file is changed within the project_euler directory and related files + paths: + - "project_euler/**" + - ".github/workflows/project_euler.yml" + - "scripts/validate_solutions.py" + schedule: + - cron: "0 0 * * *" # Run everyday + +name: "Project Euler" + +jobs: + project-euler: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - name: Install pytest and pytest-cov + run: | + python -m pip install --upgrade pip + python -m pip install --upgrade pytest pytest-cov + - run: pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=project_euler/ project_euler/ + validate-solutions: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - name: Install pytest and requests + run: | + python -m pip install --upgrade pip + python -m pip install --upgrade pytest requests + - run: pytest scripts/validate_solutions.py + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 5f9132236c26..574cdf312836 100644 --- a/.gitignore +++ b/.gitignore @@ -7,9 +7,7 @@ __pycache__/ *.so # Distribution / packaging -.vscode/ .Python -env/ build/ develop-eggs/ dist/ @@ -21,12 +19,14 @@ lib64/ parts/ sdist/ var/ +wheels/ *.egg-info/ .installed.cfg *.egg +MANIFEST # PyInstaller -# Usually these files are written by a python script from a template +# Usually these files are written by a Python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec @@ -43,8 +43,9 @@ htmlcov/ .cache nosetests.xml coverage.xml -*,cover +*.cover .hypothesis/ +.pytest_cache/ # Translations *.mo @@ -53,6 +54,7 @@ coverage.xml # Django stuff: *.log local_settings.py +db.sqlite3 # Flask stuff: instance/ @@ -67,7 +69,7 @@ docs/_build/ # PyBuilder target/ -# IPython Notebook +# Jupyter Notebook .ipynb_checkpoints # pyenv @@ -76,17 +78,32 @@ target/ # celery beat schedule file celerybeat-schedule -# dotenv -.env +# SageMath parsed files +*.sage.py -# virtualenv +# Environments +.env +.venv +env/ venv/ ENV/ +env.bak/ +venv.bak/ # Spyder project settings .spyderproject +.spyproject # Rope project settings .ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +.DS_Store .idea -.DS_Store \ No newline at end of file +.try +.vscode/ diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 000000000000..5975b8b8e983 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,2 @@ +tasks: + - init: pip3 install -r ./requirements.txt diff --git a/.lgtm.yml b/.lgtm.yml deleted file mode 100644 index ec550ab72705..000000000000 --- a/.lgtm.yml +++ /dev/null @@ -1,12 +0,0 @@ -extraction: - python: - python_setup: - version: 3 - after_prepare: - - python3 -m pip install --upgrade --user flake8 - before_index: - - python3 -m flake8 --version # flake8 3.6.0 on CPython 3.6.5 on Linux - # stop the build if there are Python syntax errors or undefined names - - python3 -m flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - - python3 -m flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000000..a3288e1c5eef --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,60 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: check-executables-have-shebangs + - id: check-yaml + - id: end-of-file-fixer + types: [python] + - id: trailing-whitespace + exclude: | + (?x)^( + data_structures/heap/binomial_heap.py + )$ + - id: requirements-txt-fixer + - repo: https://github.com/psf/black + rev: stable + hooks: + - id: black + - repo: https://github.com/PyCQA/isort + rev: 5.5.3 + hooks: + - id: isort + args: + - --profile=black + - repo: https://gitlab.com/pycqa/flake8 + rev: 3.8.3 + hooks: + - id: flake8 + args: + - --ignore=E203,W503 + - --max-complexity=25 + - --max-line-length=88 +# FIXME: fix mypy errors and then uncomment this +# - repo: https://github.com/pre-commit/mirrors-mypy +# rev: v0.782 +# hooks: +# - id: mypy +# args: +# - --ignore-missing-imports + - repo: https://github.com/codespell-project/codespell + rev: v1.17.1 + hooks: + - id: codespell + args: + - --ignore-words-list=ans,fo,followings,hist,iff,mater,secant,som,tim + - --skip="./.*,./other/dictionary.txt,./other/words,./project_euler/problem_022/p022_names.txt" + - --quiet-level=2 + exclude: | + (?x)^( + other/dictionary.txt | + other/words | + project_euler/problem_022/p022_names.txt + )$ + - repo: local + hooks: + - id: validate-filenames + name: Validate filenames + entry: ./scripts/validate_filenames.py + language: script + pass_filenames: false diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 5fba6987bb66..000000000000 --- a/.travis.yml +++ /dev/null @@ -1,26 +0,0 @@ -language: python -cache: pip -python: - - 2.7 - - 3.6 - #- nightly - #- pypy - #- pypy3 -matrix: - allow_failures: - - python: nightly - - python: pypy - - python: pypy3 -install: - #- pip install -r requirements.txt - - pip install flake8 # pytest # add another testing frameworks later -before_script: - # stop the build if there are Python syntax errors or undefined names - - flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics -script: - - true # pytest --capture=sys # add other tests here -notifications: - on_success: change - on_failure: change # `always` will be the setting once code changes slow down diff --git a/.vs/Python/v15/.suo b/.vs/Python/v15/.suo deleted file mode 100644 index 0e3f4807567d..000000000000 Binary files a/.vs/Python/v15/.suo and /dev/null differ diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite deleted file mode 100644 index 2fe4a449f121..000000000000 Binary files a/.vs/slnx.sqlite and /dev/null differ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000000..e4c81a5ecd98 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,180 @@ +# Contributing guidelines + +## Before contributing + +Welcome to [TheAlgorithms/Python](https://github.com/TheAlgorithms/Python)! Before sending your pull requests, make sure that you **read the whole guidelines**. If you have any doubt on the contributing guide, please feel free to [state it clearly in an issue](https://github.com/TheAlgorithms/Python/issues/new) or ask the community in [Gitter](https://gitter.im/TheAlgorithms). + +## Contributing + +### Contributor + +We are very happy that you consider implementing algorithms and data structure for others! This repository is referenced and used by learners from all over the globe. Being one of our contributors, you agree and confirm that: + +- You did your work - no plagiarism allowed + - Any plagiarized work will not be merged. +- Your work will be distributed under [MIT License](LICENSE.md) once your pull request is merged +- You submitted work fulfils or mostly fulfils our styles and standards + +**New implementation** is welcome! For example, new solutions for a problem, different representations for a graph data structure or algorithm designs with different complexity but **identical implementation** of an existing implementation is not allowed. Please check whether the solution is already implemented or not before submitting your pull request. + +**Improving comments** and **writing proper tests** are also highly welcome. + +### Contribution + +We appreciate any contribution, from fixing a grammar mistake in a comment to implementing complex algorithms. Please read this section if you are contributing your work. + +Your contribution will be tested by our [automated testing on Travis CI](https://travis-ci.org/TheAlgorithms/Python/pull_requests) to save time and mental energy. After you have submitted your pull request, you should see the Travis tests start to run at the bottom of your submission page. If those tests fail, then click on the ___details___ button try to read through the Travis output to understand the failure. If you do not understand, please leave a comment on your submission page and a community member will try to help. + +Please help us keep our issue list small by adding fixes: #{$ISSUE_NO} to the commit message of pull requests that resolve open issues. GitHub will use this tag to auto close the issue when the PR is merged. + +#### What is an Algorithm? + +An Algorithm is one or more functions (or classes) that: +* take one or more inputs, +* perform some internal calculations or data manipulations, +* return one or more outputs, +* have minimal side effects (Ex. print(), plot(), read(), write()). + +Algorithms should be packaged in a way that would make it easy for readers to put them into larger programs. + +Algorithms should: +* have intuitive class and function names that make their purpose clear to readers +* use Python naming conventions and intuitive variable names to ease comprehension +* be flexible to take different input values +* have Python type hints for their input parameters and return values +* raise Python exceptions (ValueError, etc.) on erroneous input values +* have docstrings with clear explanations and/or URLs to source materials +* contain doctests that test both valid and erroneous input values +* return all calculation results instead of printing or plotting them + +Algorithms in this repo should not be how-to examples for existing Python packages. Instead, they should perform internal calculations or manipulations to convert input values into different output values. Those calculations or manipulations can use data types, classes, or functions of existing Python packages but each algorithm in this repo should add unique value. + +#### Pre-commit plugin +Use [pre-commit](https://pre-commit.com/#installation) to automatically format your code to match our coding style: + +```bash +python3 -m pip install pre-commit # required only once +pre-commit install +``` +That's it! The plugin will run every time you commit any changes. If there are any errors found during the run, fix them and commit those changes. You can even run the plugin manually on all files: + +```bash +pre-commit run --all-files --show-diff-on-failure +``` + +#### Coding Style + +We want your work to be readable by others; therefore, we encourage you to note the following: + +- Please write in Python 3.7+. For instance: __print()__ is a function in Python 3 so __print "Hello"__ will _not_ work but __print("Hello")__ will. +- Please focus hard on naming of functions, classes, and variables. Help your reader by using __descriptive names__ that can help you to remove redundant comments. + - Single letter variable names are _old school_ so please avoid them unless their life only spans a few lines. + - Expand acronyms because __gcd()__ is hard to understand but __greatest_common_divisor()__ is not. + - Please follow the [Python Naming Conventions](https://pep8.org/#prescriptive-naming-conventions) so variable_names and function_names should be lower_case, CONSTANTS in UPPERCASE, ClassNames should be CamelCase, etc. + +- We encourage the use of Python [f-strings](https://realpython.com/python-f-strings/#f-strings-a-new-and-improved-way-to-format-strings-in-python) where they make the code easier to read. + +- Please consider running [__psf/black__](https://github.com/python/black) on your Python file(s) before submitting your pull request. This is not yet a requirement but it does make your code more readable and automatically aligns it with much of [PEP 8](https://www.python.org/dev/peps/pep-0008/). There are other code formatters (autopep8, yapf) but the __black__ formatter is now hosted by the Python Software Foundation. To use it, + + ```bash + python3 -m pip install black # only required the first time + black . + ``` + +- All submissions will need to pass the test __flake8 . --ignore=E203,W503 --max-line-length=88__ before they will be accepted so if possible, try this test locally on your Python file(s) before submitting your pull request. + + ```bash + python3 -m pip install flake8 # only required the first time + flake8 . --ignore=E203,W503 --max-line-length=88 --show-source + ``` + +- Original code submission require docstrings or comments to describe your work. + +- More on docstrings and comments: + + If you used a Wikipedia article or some other source material to create your algorithm, please add the URL in a docstring or comment to help your reader. + + The following are considered to be bad and may be requested to be improved: + + ```python + x = x + 2 # increased by 2 + ``` + + This is too trivial. Comments are expected to be explanatory. For comments, you can write them above, on or below a line of code, as long as you are consistent within the same piece of code. + + We encourage you to put docstrings inside your functions but please pay attention to indentation of docstrings. The following is a good example: + + ```python + def sum_ab(a, b): + """ + Return the sum of two integers a and b. + """ + return a + b + ``` + +- Write tests (especially [__doctests__](https://docs.python.org/3/library/doctest.html)) to illustrate and verify your work. We highly encourage the use of _doctests on all functions_. + + ```python + def sum_ab(a, b): + """ + Return the sum of two integers a and b + >>> sum_ab(2, 2) + 4 + >>> sum_ab(-2, 3) + 1 + >>> sum_ab(4.9, 5.1) + 10.0 + """ + return a + b + ``` + + These doctests will be run by pytest as part of our automated testing so please try to run your doctests locally and make sure that they are found and pass: + + ```bash + python3 -m doctest -v my_submission.py + ``` + + The use of the Python builtin __input()__ function is **not** encouraged: + + ```python + input('Enter your input:') + # Or even worse... + input = eval(input("Enter your input: ")) + ``` + + However, if your code uses __input()__ then we encourage you to gracefully deal with leading and trailing whitespace in user input by adding __.strip()__ as in: + + ```python + starting_value = int(input("Please enter a starting value: ").strip()) + ``` + + The use of [Python type hints](https://docs.python.org/3/library/typing.html) is encouraged for function parameters and return values. Our automated testing will run [mypy](http://mypy-lang.org) so run that locally before making your submission. + + ```python + def sum_ab(a: int, b: int) -> int: + return a + b + ``` + +- [__List comprehensions and generators__](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) are preferred over the use of `lambda`, `map`, `filter`, `reduce` but the important thing is to demonstrate the power of Python in code that is easy to read and maintain. + +- Avoid importing external libraries for basic algorithms. Only use those libraries for complicated algorithms. +- If you need a third party module that is not in the file __requirements.txt__, please add it to that file as part of your submission. + +#### Other Requirements for Submissions +- If you are submitting code in the `project_euler/` directory, please also read [the dedicated Guideline](https://github.com/TheAlgorithms/Python/blob/master/project_euler/README.md) before contributing to our Project Euler library. +- The file extension for code files should be `.py`. Jupyter Notebooks should be submitted to [TheAlgorithms/Jupyter](https://github.com/TheAlgorithms/Jupyter). +- Strictly use snake_case (underscore_separated) in your file_name, as it will be easy to parse in future using scripts. +- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure. +- If possible, follow the standard *within* the folder you are submitting to. +- If you have modified/added code work, make sure the code compiles before submitting. +- If you have modified/added documentation work, ensure your language is concise and contains no grammar errors. +- Do not update the README.md or DIRECTORY.md file which will be periodically autogenerated by our Travis CI processes. +- Add a corresponding explanation to [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation) (Optional but recommended). +- All submissions will be tested with [__mypy__](http://www.mypy-lang.org) so we encourage to add [__Python type hints__](https://docs.python.org/3/library/typing.html) where it makes sense to do so. + +- Most importantly, + - **Be consistent in the use of these guidelines when submitting.** + - **Join** [Gitter](https://gitter.im/TheAlgorithms) **now!** + - Happy coding! + +Writer [@poyea](https://github.com/poyea), Jun 2019. diff --git a/DIRECTORY.md b/DIRECTORY.md new file mode 100644 index 000000000000..4f17cf9c03ed --- /dev/null +++ b/DIRECTORY.md @@ -0,0 +1,916 @@ + +## Arithmetic Analysis + * [Bisection](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/bisection.py) + * [Gaussian Elimination](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/gaussian_elimination.py) + * [In Static Equilibrium](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/in_static_equilibrium.py) + * [Intersection](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/intersection.py) + * [Lu Decomposition](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/lu_decomposition.py) + * [Newton Forward Interpolation](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_forward_interpolation.py) + * [Newton Method](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_method.py) + * [Newton Raphson](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_raphson.py) + * [Secant Method](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/secant_method.py) + +## Backtracking + * [All Combinations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_combinations.py) + * [All Permutations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_permutations.py) + * [All Subsequences](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_subsequences.py) + * [Coloring](https://github.com/TheAlgorithms/Python/blob/master/backtracking/coloring.py) + * [Hamiltonian Cycle](https://github.com/TheAlgorithms/Python/blob/master/backtracking/hamiltonian_cycle.py) + * [Knight Tour](https://github.com/TheAlgorithms/Python/blob/master/backtracking/knight_tour.py) + * [Minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py) + * [N Queens](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens.py) + * [N Queens Math](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens_math.py) + * [Rat In Maze](https://github.com/TheAlgorithms/Python/blob/master/backtracking/rat_in_maze.py) + * [Sudoku](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sudoku.py) + * [Sum Of Subsets](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sum_of_subsets.py) + +## Bit Manipulation + * [Binary And Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_and_operator.py) + * [Binary Count Setbits](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_count_setbits.py) + * [Binary Count Trailing Zeros](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_count_trailing_zeros.py) + * [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py) + * [Binary Xor Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_xor_operator.py) + * [Single Bit Manipulation Operations](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/single_bit_manipulation_operations.py) + +## Blockchain + * [Chinese Remainder Theorem](https://github.com/TheAlgorithms/Python/blob/master/blockchain/chinese_remainder_theorem.py) + * [Diophantine Equation](https://github.com/TheAlgorithms/Python/blob/master/blockchain/diophantine_equation.py) + * [Modular Division](https://github.com/TheAlgorithms/Python/blob/master/blockchain/modular_division.py) + +## Boolean Algebra + * [Quine Mc Cluskey](https://github.com/TheAlgorithms/Python/blob/master/boolean_algebra/quine_mc_cluskey.py) + +## Cellular Automata + * [Conways Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/conways_game_of_life.py) + * [One Dimensional](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/one_dimensional.py) + +## Ciphers + * [A1Z26](https://github.com/TheAlgorithms/Python/blob/master/ciphers/a1z26.py) + * [Affine Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/affine_cipher.py) + * [Atbash](https://github.com/TheAlgorithms/Python/blob/master/ciphers/atbash.py) + * [Base16](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base16.py) + * [Base32](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base32.py) + * [Base64 Encoding](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base64_encoding.py) + * [Base85](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base85.py) + * [Beaufort Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/beaufort_cipher.py) + * [Brute Force Caesar Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/brute_force_caesar_cipher.py) + * [Caesar Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/caesar_cipher.py) + * [Cryptomath Module](https://github.com/TheAlgorithms/Python/blob/master/ciphers/cryptomath_module.py) + * [Decrypt Caesar With Chi Squared](https://github.com/TheAlgorithms/Python/blob/master/ciphers/decrypt_caesar_with_chi_squared.py) + * [Deterministic Miller Rabin](https://github.com/TheAlgorithms/Python/blob/master/ciphers/deterministic_miller_rabin.py) + * [Diffie](https://github.com/TheAlgorithms/Python/blob/master/ciphers/diffie.py) + * [Elgamal Key Generator](https://github.com/TheAlgorithms/Python/blob/master/ciphers/elgamal_key_generator.py) + * [Enigma Machine2](https://github.com/TheAlgorithms/Python/blob/master/ciphers/enigma_machine2.py) + * [Hill Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/hill_cipher.py) + * [Mixed Keyword Cypher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/mixed_keyword_cypher.py) + * [Mono Alphabetic Ciphers](https://github.com/TheAlgorithms/Python/blob/master/ciphers/mono_alphabetic_ciphers.py) + * [Morse Code Implementation](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code_implementation.py) + * [Onepad Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/onepad_cipher.py) + * [Playfair Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py) + * [Porta Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/porta_cipher.py) + * [Rabin Miller](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rabin_miller.py) + * [Rail Fence Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rail_fence_cipher.py) + * [Rot13](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rot13.py) + * [Rsa Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rsa_cipher.py) + * [Rsa Factorization](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rsa_factorization.py) + * [Rsa Key Generator](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rsa_key_generator.py) + * [Shuffled Shift Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/shuffled_shift_cipher.py) + * [Simple Keyword Cypher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/simple_keyword_cypher.py) + * [Simple Substitution Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/simple_substitution_cipher.py) + * [Trafid Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/trafid_cipher.py) + * [Transposition Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/transposition_cipher.py) + * [Transposition Cipher Encrypt Decrypt File](https://github.com/TheAlgorithms/Python/blob/master/ciphers/transposition_cipher_encrypt_decrypt_file.py) + * [Vigenere Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/vigenere_cipher.py) + * [Xor Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/xor_cipher.py) + +## Compression + * [Burrows Wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.py) + * [Huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py) + * [Lempel Ziv](https://github.com/TheAlgorithms/Python/blob/master/compression/lempel_ziv.py) + * [Lempel Ziv Decompress](https://github.com/TheAlgorithms/Python/blob/master/compression/lempel_ziv_decompress.py) + * [Peak Signal To Noise Ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py) + +## Computer Vision + * [Harriscorner](https://github.com/TheAlgorithms/Python/blob/master/computer_vision/harriscorner.py) + * [Meanthreshold](https://github.com/TheAlgorithms/Python/blob/master/computer_vision/meanthreshold.py) + +## Conversions + * [Binary To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_decimal.py) + * [Binary To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_octal.py) + * [Decimal To Any](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_any.py) + * [Decimal To Binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py) + * [Decimal To Binary Recursion](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary_recursion.py) + * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py) + * [Decimal To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_octal.py) + * [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/hexadecimal_to_decimal.py) + * [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py) + * [Octal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/octal_to_decimal.py) + * [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py) + * [Roman Numerals](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_numerals.py) + * [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py) + * [Weight Conversion](https://github.com/TheAlgorithms/Python/blob/master/conversions/weight_conversion.py) + +## Data Structures + * Binary Tree + * [Avl Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/avl_tree.py) + * [Basic Binary Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/basic_binary_tree.py) + * [Binary Search Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_search_tree.py) + * [Binary Search Tree Recursive](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_search_tree_recursive.py) + * [Binary Tree Mirror](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_tree_mirror.py) + * [Binary Tree Traversals](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/binary_tree_traversals.py) + * [Fenwick Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/fenwick_tree.py) + * [Lazy Segment Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/lazy_segment_tree.py) + * [Lowest Common Ancestor](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/lowest_common_ancestor.py) + * [Non Recursive Segment Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/non_recursive_segment_tree.py) + * [Number Of Possible Binary Trees](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/number_of_possible_binary_trees.py) + * [Red Black Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/red_black_tree.py) + * [Segment Tree](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/segment_tree.py) + * [Segment Tree Other](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/segment_tree_other.py) + * [Treap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/treap.py) + * Disjoint Set + * [Alternate Disjoint Set](https://github.com/TheAlgorithms/Python/blob/master/data_structures/disjoint_set/alternate_disjoint_set.py) + * [Disjoint Set](https://github.com/TheAlgorithms/Python/blob/master/data_structures/disjoint_set/disjoint_set.py) + * Hashing + * [Double Hash](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/double_hash.py) + * [Hash Table](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/hash_table.py) + * [Hash Table With Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/hash_table_with_linked_list.py) + * Number Theory + * [Prime Numbers](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/number_theory/prime_numbers.py) + * [Quadratic Probing](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/quadratic_probing.py) + * Heap + * [Binomial Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/binomial_heap.py) + * [Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/heap.py) + * [Heap Generic](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/heap_generic.py) + * [Max Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/max_heap.py) + * [Min Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/min_heap.py) + * [Randomized Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/randomized_heap.py) + * [Skew Heap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/heap/skew_heap.py) + * Linked List + * [Circular Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/circular_linked_list.py) + * [Deque Doubly](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/deque_doubly.py) + * [Doubly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/doubly_linked_list.py) + * [Doubly Linked List Two](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/doubly_linked_list_two.py) + * [From Sequence](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/from_sequence.py) + * [Has Loop](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/has_loop.py) + * [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/is_palindrome.py) + * [Merge Two Lists](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/merge_two_lists.py) + * [Middle Element Of Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/middle_element_of_linked_list.py) + * [Print Reverse](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/print_reverse.py) + * [Singly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/singly_linked_list.py) + * [Skip List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/skip_list.py) + * [Swap Nodes](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/swap_nodes.py) + * Queue + * [Circular Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/circular_queue.py) + * [Double Ended Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/double_ended_queue.py) + * [Linked Queue](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/linked_queue.py) + * [Priority Queue Using List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/priority_queue_using_list.py) + * [Queue On List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/queue_on_list.py) + * [Queue On Pseudo Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/queue/queue_on_pseudo_stack.py) + * Stacks + * [Balanced Parentheses](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/balanced_parentheses.py) + * [Dijkstras Two Stack Algorithm](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/dijkstras_two_stack_algorithm.py) + * [Evaluate Postfix Notations](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/evaluate_postfix_notations.py) + * [Infix To Postfix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_postfix_conversion.py) + * [Infix To Prefix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_prefix_conversion.py) + * [Linked Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/linked_stack.py) + * [Next Greater Element](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/next_greater_element.py) + * [Postfix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/postfix_evaluation.py) + * [Prefix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/prefix_evaluation.py) + * [Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack.py) + * [Stack Using Dll](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_using_dll.py) + * [Stock Span Problem](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stock_span_problem.py) + * Trie + * [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py) + +## Digital Image Processing + * [Change Brightness](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_brightness.py) + * [Change Contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py) + * [Convert To Negative](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/convert_to_negative.py) + * Dithering + * [Burkes](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/dithering/burkes.py) + * Edge Detection + * [Canny](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/edge_detection/canny.py) + * Filters + * [Bilateral Filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/bilateral_filter.py) + * [Convolve](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/convolve.py) + * [Gaussian Filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/gaussian_filter.py) + * [Median Filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/median_filter.py) + * [Sobel Filter](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/filters/sobel_filter.py) + * Histogram Equalization + * [Histogram Stretch](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/histogram_equalization/histogram_stretch.py) + * [Index Calculation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/index_calculation.py) + * Resize + * [Resize](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/resize/resize.py) + * Rotation + * [Rotation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/rotation/rotation.py) + * [Sepia](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/sepia.py) + * [Test Digital Image Processing](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/test_digital_image_processing.py) + +## Divide And Conquer + * [Closest Pair Of Points](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/closest_pair_of_points.py) + * [Convex Hull](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/convex_hull.py) + * [Heaps Algorithm](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm.py) + * [Heaps Algorithm Iterative](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm_iterative.py) + * [Inversions](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/inversions.py) + * [Kth Order Statistic](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/kth_order_statistic.py) + * [Max Difference Pair](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_difference_pair.py) + * [Max Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_subarray_sum.py) + * [Mergesort](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/mergesort.py) + * [Peak](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/peak.py) + * [Power](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/power.py) + * [Strassen Matrix Multiplication](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/strassen_matrix_multiplication.py) + +## Dynamic Programming + * [Abbreviation](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/abbreviation.py) + * [Bitmask](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/bitmask.py) + * [Climbing Stairs](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/climbing_stairs.py) + * [Coin Change](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/coin_change.py) + * [Edit Distance](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/edit_distance.py) + * [Factorial](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/factorial.py) + * [Fast Fibonacci](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/fast_fibonacci.py) + * [Fibonacci](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/fibonacci.py) + * [Floyd Warshall](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/floyd_warshall.py) + * [Fractional Knapsack](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/fractional_knapsack.py) + * [Fractional Knapsack 2](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/fractional_knapsack_2.py) + * [Integer Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/integer_partition.py) + * [Iterating Through Submasks](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/iterating_through_submasks.py) + * [Knapsack](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/knapsack.py) + * [Longest Common Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_common_subsequence.py) + * [Longest Increasing Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_increasing_subsequence.py) + * [Longest Increasing Subsequence O(Nlogn)](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_increasing_subsequence_o(nlogn).py) + * [Longest Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_sub_array.py) + * [Matrix Chain Order](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/matrix_chain_order.py) + * [Max Non Adjacent Sum](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_non_adjacent_sum.py) + * [Max Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sub_array.py) + * [Max Sum Contiguous Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sum_contiguous_subsequence.py) + * [Minimum Cost Path](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_cost_path.py) + * [Minimum Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_partition.py) + * [Minimum Steps To One](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_steps_to_one.py) + * [Optimal Binary Search Tree](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/optimal_binary_search_tree.py) + * [Rod Cutting](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/rod_cutting.py) + * [Subset Generation](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/subset_generation.py) + * [Sum Of Subset](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/sum_of_subset.py) + +## Electronics + * [Electric Power](https://github.com/TheAlgorithms/Python/blob/master/electronics/electric_power.py) + * [Ohms Law](https://github.com/TheAlgorithms/Python/blob/master/electronics/ohms_law.py) + +## File Transfer + * [Receive File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/receive_file.py) + * [Send File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/send_file.py) + * Tests + * [Test Send File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/tests/test_send_file.py) + +## Fuzzy Logic + * [Fuzzy Operations](https://github.com/TheAlgorithms/Python/blob/master/fuzzy_logic/fuzzy_operations.py) + +## Genetic Algorithm + * [Basic String](https://github.com/TheAlgorithms/Python/blob/master/genetic_algorithm/basic_string.py) + +## Geodesy + * [Haversine Distance](https://github.com/TheAlgorithms/Python/blob/master/geodesy/haversine_distance.py) + * [Lamberts Ellipsoidal Distance](https://github.com/TheAlgorithms/Python/blob/master/geodesy/lamberts_ellipsoidal_distance.py) + +## Graphics + * [Bezier Curve](https://github.com/TheAlgorithms/Python/blob/master/graphics/bezier_curve.py) + * [Vector3 For 2D Rendering](https://github.com/TheAlgorithms/Python/blob/master/graphics/vector3_for_2d_rendering.py) + +## Graphs + * [A Star](https://github.com/TheAlgorithms/Python/blob/master/graphs/a_star.py) + * [Articulation Points](https://github.com/TheAlgorithms/Python/blob/master/graphs/articulation_points.py) + * [Basic Graphs](https://github.com/TheAlgorithms/Python/blob/master/graphs/basic_graphs.py) + * [Bellman Ford](https://github.com/TheAlgorithms/Python/blob/master/graphs/bellman_ford.py) + * [Bfs Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs_shortest_path.py) + * [Bfs Zero One Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs_zero_one_shortest_path.py) + * [Bidirectional A Star](https://github.com/TheAlgorithms/Python/blob/master/graphs/bidirectional_a_star.py) + * [Bidirectional Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/bidirectional_breadth_first_search.py) + * [Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search.py) + * [Breadth First Search 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search_2.py) + * [Breadth First Search Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search_shortest_path.py) + * [Check Bipartite Graph Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_bfs.py) + * [Check Bipartite Graph Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_dfs.py) + * [Connected Components](https://github.com/TheAlgorithms/Python/blob/master/graphs/connected_components.py) + * [Depth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search.py) + * [Depth First Search 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_2.py) + * [Dijkstra](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra.py) + * [Dijkstra 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_2.py) + * [Dijkstra Algorithm](https://github.com/TheAlgorithms/Python/blob/master/graphs/dijkstra_algorithm.py) + * [Dinic](https://github.com/TheAlgorithms/Python/blob/master/graphs/dinic.py) + * [Directed And Undirected (Weighted) Graph](https://github.com/TheAlgorithms/Python/blob/master/graphs/directed_and_undirected_(weighted)_graph.py) + * [Edmonds Karp Multiple Source And Sink](https://github.com/TheAlgorithms/Python/blob/master/graphs/edmonds_karp_multiple_source_and_sink.py) + * [Eulerian Path And Circuit For Undirected Graph](https://github.com/TheAlgorithms/Python/blob/master/graphs/eulerian_path_and_circuit_for_undirected_graph.py) + * [Even Tree](https://github.com/TheAlgorithms/Python/blob/master/graphs/even_tree.py) + * [Finding Bridges](https://github.com/TheAlgorithms/Python/blob/master/graphs/finding_bridges.py) + * [Frequent Pattern Graph Miner](https://github.com/TheAlgorithms/Python/blob/master/graphs/frequent_pattern_graph_miner.py) + * [G Topological Sort](https://github.com/TheAlgorithms/Python/blob/master/graphs/g_topological_sort.py) + * [Gale Shapley Bigraph](https://github.com/TheAlgorithms/Python/blob/master/graphs/gale_shapley_bigraph.py) + * [Graph List](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_list.py) + * [Graph Matrix](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_matrix.py) + * [Graphs Floyd Warshall](https://github.com/TheAlgorithms/Python/blob/master/graphs/graphs_floyd_warshall.py) + * [Greedy Best First](https://github.com/TheAlgorithms/Python/blob/master/graphs/greedy_best_first.py) + * [Kahns Algorithm Long](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_long.py) + * [Kahns Algorithm Topo](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_topo.py) + * [Karger](https://github.com/TheAlgorithms/Python/blob/master/graphs/karger.py) + * [Minimum Spanning Tree Boruvka](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_boruvka.py) + * [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py) + * [Minimum Spanning Tree Kruskal2](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal2.py) + * [Minimum Spanning Tree Prims](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_prims.py) + * [Minimum Spanning Tree Prims2](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_prims2.py) + * [Multi Heuristic Astar](https://github.com/TheAlgorithms/Python/blob/master/graphs/multi_heuristic_astar.py) + * [Page Rank](https://github.com/TheAlgorithms/Python/blob/master/graphs/page_rank.py) + * [Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/prim.py) + * [Scc Kosaraju](https://github.com/TheAlgorithms/Python/blob/master/graphs/scc_kosaraju.py) + * [Strongly Connected Components](https://github.com/TheAlgorithms/Python/blob/master/graphs/strongly_connected_components.py) + * [Tarjans Scc](https://github.com/TheAlgorithms/Python/blob/master/graphs/tarjans_scc.py) + * Tests + * [Test Min Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_kruskal.py) + * [Test Min Spanning Tree Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_prim.py) + +## Hashes + * [Adler32](https://github.com/TheAlgorithms/Python/blob/master/hashes/adler32.py) + * [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py) + * [Djb2](https://github.com/TheAlgorithms/Python/blob/master/hashes/djb2.py) + * [Enigma Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/enigma_machine.py) + * [Hamming Code](https://github.com/TheAlgorithms/Python/blob/master/hashes/hamming_code.py) + * [Md5](https://github.com/TheAlgorithms/Python/blob/master/hashes/md5.py) + * [Sdbm](https://github.com/TheAlgorithms/Python/blob/master/hashes/sdbm.py) + * [Sha1](https://github.com/TheAlgorithms/Python/blob/master/hashes/sha1.py) + +## Knapsack + * [Greedy Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/greedy_knapsack.py) + * [Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/knapsack.py) + * Tests + * [Test Greedy Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/tests/test_greedy_knapsack.py) + * [Test Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/tests/test_knapsack.py) + +## Linear Algebra + * Src + * [Conjugate Gradient](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/conjugate_gradient.py) + * [Lib](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/lib.py) + * [Polynom For Points](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/polynom_for_points.py) + * [Power Iteration](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/power_iteration.py) + * [Rayleigh Quotient](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/rayleigh_quotient.py) + * [Test Linear Algebra](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/test_linear_algebra.py) + * [Transformations 2D](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/transformations_2d.py) + +## Machine Learning + * [Astar](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/astar.py) + * [Data Transformations](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/data_transformations.py) + * [Decision Tree](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/decision_tree.py) + * Forecasting + * [Run](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/forecasting/run.py) + * [Gaussian Naive Bayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gaussian_naive_bayes.py) + * [Gradient Boosting Regressor](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gradient_boosting_regressor.py) + * [Gradient Descent](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gradient_descent.py) + * [K Means Clust](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/k_means_clust.py) + * [K Nearest Neighbours](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/k_nearest_neighbours.py) + * [Knn Sklearn](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/knn_sklearn.py) + * [Linear Discriminant Analysis](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/linear_discriminant_analysis.py) + * [Linear Regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/linear_regression.py) + * [Logistic Regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/logistic_regression.py) + * [Multilayer Perceptron Classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/multilayer_perceptron_classifier.py) + * [Polymonial Regression](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/polymonial_regression.py) + * [Random Forest Classifier](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_classifier.py) + * [Random Forest Regressor](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/random_forest_regressor.py) + * [Scoring Functions](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/scoring_functions.py) + * [Sequential Minimum Optimization](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/sequential_minimum_optimization.py) + * [Similarity Search](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/similarity_search.py) + * [Support Vector Machines](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/support_vector_machines.py) + * [Word Frequency Functions](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/word_frequency_functions.py) + +## Maths + * [3N Plus 1](https://github.com/TheAlgorithms/Python/blob/master/maths/3n_plus_1.py) + * [Abs](https://github.com/TheAlgorithms/Python/blob/master/maths/abs.py) + * [Abs Max](https://github.com/TheAlgorithms/Python/blob/master/maths/abs_max.py) + * [Abs Min](https://github.com/TheAlgorithms/Python/blob/master/maths/abs_min.py) + * [Add](https://github.com/TheAlgorithms/Python/blob/master/maths/add.py) + * [Aliquot Sum](https://github.com/TheAlgorithms/Python/blob/master/maths/aliquot_sum.py) + * [Allocation Number](https://github.com/TheAlgorithms/Python/blob/master/maths/allocation_number.py) + * [Area](https://github.com/TheAlgorithms/Python/blob/master/maths/area.py) + * [Area Under Curve](https://github.com/TheAlgorithms/Python/blob/master/maths/area_under_curve.py) + * [Armstrong Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/armstrong_numbers.py) + * [Average Mean](https://github.com/TheAlgorithms/Python/blob/master/maths/average_mean.py) + * [Average Median](https://github.com/TheAlgorithms/Python/blob/master/maths/average_median.py) + * [Average Mode](https://github.com/TheAlgorithms/Python/blob/master/maths/average_mode.py) + * [Bailey Borwein Plouffe](https://github.com/TheAlgorithms/Python/blob/master/maths/bailey_borwein_plouffe.py) + * [Basic Maths](https://github.com/TheAlgorithms/Python/blob/master/maths/basic_maths.py) + * [Binary Exp Mod](https://github.com/TheAlgorithms/Python/blob/master/maths/binary_exp_mod.py) + * [Binary Exponentiation](https://github.com/TheAlgorithms/Python/blob/master/maths/binary_exponentiation.py) + * [Binomial Coefficient](https://github.com/TheAlgorithms/Python/blob/master/maths/binomial_coefficient.py) + * [Binomial Distribution](https://github.com/TheAlgorithms/Python/blob/master/maths/binomial_distribution.py) + * [Bisection](https://github.com/TheAlgorithms/Python/blob/master/maths/bisection.py) + * [Ceil](https://github.com/TheAlgorithms/Python/blob/master/maths/ceil.py) + * [Chudnovsky Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/chudnovsky_algorithm.py) + * [Collatz Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/collatz_sequence.py) + * [Combinations](https://github.com/TheAlgorithms/Python/blob/master/maths/combinations.py) + * [Decimal Isolate](https://github.com/TheAlgorithms/Python/blob/master/maths/decimal_isolate.py) + * [Entropy](https://github.com/TheAlgorithms/Python/blob/master/maths/entropy.py) + * [Euclidean Distance](https://github.com/TheAlgorithms/Python/blob/master/maths/euclidean_distance.py) + * [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py) + * [Explicit Euler](https://github.com/TheAlgorithms/Python/blob/master/maths/explicit_euler.py) + * [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py) + * [Factorial Iterative](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_iterative.py) + * [Factorial Python](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_python.py) + * [Factorial Recursive](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_recursive.py) + * [Factors](https://github.com/TheAlgorithms/Python/blob/master/maths/factors.py) + * [Fermat Little Theorem](https://github.com/TheAlgorithms/Python/blob/master/maths/fermat_little_theorem.py) + * [Fibonacci](https://github.com/TheAlgorithms/Python/blob/master/maths/fibonacci.py) + * [Fibonacci Sequence Recursion](https://github.com/TheAlgorithms/Python/blob/master/maths/fibonacci_sequence_recursion.py) + * [Find Max](https://github.com/TheAlgorithms/Python/blob/master/maths/find_max.py) + * [Find Max Recursion](https://github.com/TheAlgorithms/Python/blob/master/maths/find_max_recursion.py) + * [Find Min](https://github.com/TheAlgorithms/Python/blob/master/maths/find_min.py) + * [Find Min Recursion](https://github.com/TheAlgorithms/Python/blob/master/maths/find_min_recursion.py) + * [Floor](https://github.com/TheAlgorithms/Python/blob/master/maths/floor.py) + * [Gamma](https://github.com/TheAlgorithms/Python/blob/master/maths/gamma.py) + * [Gaussian](https://github.com/TheAlgorithms/Python/blob/master/maths/gaussian.py) + * [Greatest Common Divisor](https://github.com/TheAlgorithms/Python/blob/master/maths/greatest_common_divisor.py) + * [Hardy Ramanujanalgo](https://github.com/TheAlgorithms/Python/blob/master/maths/hardy_ramanujanalgo.py) + * [Is Square Free](https://github.com/TheAlgorithms/Python/blob/master/maths/is_square_free.py) + * [Jaccard Similarity](https://github.com/TheAlgorithms/Python/blob/master/maths/jaccard_similarity.py) + * [Kadanes](https://github.com/TheAlgorithms/Python/blob/master/maths/kadanes.py) + * [Karatsuba](https://github.com/TheAlgorithms/Python/blob/master/maths/karatsuba.py) + * [Krishnamurthy Number](https://github.com/TheAlgorithms/Python/blob/master/maths/krishnamurthy_number.py) + * [Kth Lexicographic Permutation](https://github.com/TheAlgorithms/Python/blob/master/maths/kth_lexicographic_permutation.py) + * [Largest Of Very Large Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/largest_of_very_large_numbers.py) + * [Least Common Multiple](https://github.com/TheAlgorithms/Python/blob/master/maths/least_common_multiple.py) + * [Line Length](https://github.com/TheAlgorithms/Python/blob/master/maths/line_length.py) + * [Lucas Lehmer Primality Test](https://github.com/TheAlgorithms/Python/blob/master/maths/lucas_lehmer_primality_test.py) + * [Lucas Series](https://github.com/TheAlgorithms/Python/blob/master/maths/lucas_series.py) + * [Matrix Exponentiation](https://github.com/TheAlgorithms/Python/blob/master/maths/matrix_exponentiation.py) + * [Miller Rabin](https://github.com/TheAlgorithms/Python/blob/master/maths/miller_rabin.py) + * [Mobius Function](https://github.com/TheAlgorithms/Python/blob/master/maths/mobius_function.py) + * [Modular Exponential](https://github.com/TheAlgorithms/Python/blob/master/maths/modular_exponential.py) + * [Monte Carlo](https://github.com/TheAlgorithms/Python/blob/master/maths/monte_carlo.py) + * [Monte Carlo Dice](https://github.com/TheAlgorithms/Python/blob/master/maths/monte_carlo_dice.py) + * [Newton Raphson](https://github.com/TheAlgorithms/Python/blob/master/maths/newton_raphson.py) + * [Number Of Digits](https://github.com/TheAlgorithms/Python/blob/master/maths/number_of_digits.py) + * [Numerical Integration](https://github.com/TheAlgorithms/Python/blob/master/maths/numerical_integration.py) + * [Perfect Cube](https://github.com/TheAlgorithms/Python/blob/master/maths/perfect_cube.py) + * [Perfect Number](https://github.com/TheAlgorithms/Python/blob/master/maths/perfect_number.py) + * [Perfect Square](https://github.com/TheAlgorithms/Python/blob/master/maths/perfect_square.py) + * [Pi Monte Carlo Estimation](https://github.com/TheAlgorithms/Python/blob/master/maths/pi_monte_carlo_estimation.py) + * [Polynomial Evaluation](https://github.com/TheAlgorithms/Python/blob/master/maths/polynomial_evaluation.py) + * [Power Using Recursion](https://github.com/TheAlgorithms/Python/blob/master/maths/power_using_recursion.py) + * [Prime Check](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_check.py) + * [Prime Factors](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_factors.py) + * [Prime Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_numbers.py) + * [Prime Sieve Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_sieve_eratosthenes.py) + * [Pythagoras](https://github.com/TheAlgorithms/Python/blob/master/maths/pythagoras.py) + * [Qr Decomposition](https://github.com/TheAlgorithms/Python/blob/master/maths/qr_decomposition.py) + * [Quadratic Equations Complex Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/quadratic_equations_complex_numbers.py) + * [Radians](https://github.com/TheAlgorithms/Python/blob/master/maths/radians.py) + * [Radix2 Fft](https://github.com/TheAlgorithms/Python/blob/master/maths/radix2_fft.py) + * [Relu](https://github.com/TheAlgorithms/Python/blob/master/maths/relu.py) + * [Runge Kutta](https://github.com/TheAlgorithms/Python/blob/master/maths/runge_kutta.py) + * [Segmented Sieve](https://github.com/TheAlgorithms/Python/blob/master/maths/segmented_sieve.py) + * Series + * [Geometric Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/geometric_series.py) + * [Harmonic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/harmonic_series.py) + * [P Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/p_series.py) + * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/sieve_of_eratosthenes.py) + * [Sigmoid](https://github.com/TheAlgorithms/Python/blob/master/maths/sigmoid.py) + * [Simpson Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/simpson_rule.py) + * [Softmax](https://github.com/TheAlgorithms/Python/blob/master/maths/softmax.py) + * [Square Root](https://github.com/TheAlgorithms/Python/blob/master/maths/square_root.py) + * [Sum Of Arithmetic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_arithmetic_series.py) + * [Sum Of Digits](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_digits.py) + * [Sum Of Geometric Progression](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_geometric_progression.py) + * [Test Prime Check](https://github.com/TheAlgorithms/Python/blob/master/maths/test_prime_check.py) + * [Trapezoidal Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/trapezoidal_rule.py) + * [Ugly Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/ugly_numbers.py) + * [Volume](https://github.com/TheAlgorithms/Python/blob/master/maths/volume.py) + * [Zellers Congruence](https://github.com/TheAlgorithms/Python/blob/master/maths/zellers_congruence.py) + +## Matrix + * [Count Islands In Matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/count_islands_in_matrix.py) + * [Inverse Of Matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/inverse_of_matrix.py) + * [Matrix Class](https://github.com/TheAlgorithms/Python/blob/master/matrix/matrix_class.py) + * [Matrix Operation](https://github.com/TheAlgorithms/Python/blob/master/matrix/matrix_operation.py) + * [Nth Fibonacci Using Matrix Exponentiation](https://github.com/TheAlgorithms/Python/blob/master/matrix/nth_fibonacci_using_matrix_exponentiation.py) + * [Rotate Matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/rotate_matrix.py) + * [Searching In Sorted Matrix](https://github.com/TheAlgorithms/Python/blob/master/matrix/searching_in_sorted_matrix.py) + * [Sherman Morrison](https://github.com/TheAlgorithms/Python/blob/master/matrix/sherman_morrison.py) + * [Spiral Print](https://github.com/TheAlgorithms/Python/blob/master/matrix/spiral_print.py) + * Tests + * [Test Matrix Operation](https://github.com/TheAlgorithms/Python/blob/master/matrix/tests/test_matrix_operation.py) + +## Networking Flow + * [Ford Fulkerson](https://github.com/TheAlgorithms/Python/blob/master/networking_flow/ford_fulkerson.py) + * [Minimum Cut](https://github.com/TheAlgorithms/Python/blob/master/networking_flow/minimum_cut.py) + +## Neural Network + * [2 Hidden Layers Neural Network](https://github.com/TheAlgorithms/Python/blob/master/neural_network/2_hidden_layers_neural_network.py) + * [Back Propagation Neural Network](https://github.com/TheAlgorithms/Python/blob/master/neural_network/back_propagation_neural_network.py) + * [Convolution Neural Network](https://github.com/TheAlgorithms/Python/blob/master/neural_network/convolution_neural_network.py) + * [Perceptron](https://github.com/TheAlgorithms/Python/blob/master/neural_network/perceptron.py) + +## Other + * [Activity Selection](https://github.com/TheAlgorithms/Python/blob/master/other/activity_selection.py) + * [Anagrams](https://github.com/TheAlgorithms/Python/blob/master/other/anagrams.py) + * [Autocomplete Using Trie](https://github.com/TheAlgorithms/Python/blob/master/other/autocomplete_using_trie.py) + * [Binary Exponentiation](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation.py) + * [Binary Exponentiation 2](https://github.com/TheAlgorithms/Python/blob/master/other/binary_exponentiation_2.py) + * [Davis–Putnam–Logemann–Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davis–putnam–logemann–loveland.py) + * [Detecting English Programmatically](https://github.com/TheAlgorithms/Python/blob/master/other/detecting_english_programmatically.py) + * [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py) + * [Doomsday](https://github.com/TheAlgorithms/Python/blob/master/other/doomsday.py) + * [Euclidean Gcd](https://github.com/TheAlgorithms/Python/blob/master/other/euclidean_gcd.py) + * [Fischer Yates Shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py) + * [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py) + * [Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/other/game_of_life.py) + * [Gauss Easter](https://github.com/TheAlgorithms/Python/blob/master/other/gauss_easter.py) + * [Greedy](https://github.com/TheAlgorithms/Python/blob/master/other/greedy.py) + * [Integeration By Simpson Approx](https://github.com/TheAlgorithms/Python/blob/master/other/integeration_by_simpson_approx.py) + * [Largest Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/other/largest_subarray_sum.py) + * [Least Recently Used](https://github.com/TheAlgorithms/Python/blob/master/other/least_recently_used.py) + * [Lfu Cache](https://github.com/TheAlgorithms/Python/blob/master/other/lfu_cache.py) + * [Linear Congruential Generator](https://github.com/TheAlgorithms/Python/blob/master/other/linear_congruential_generator.py) + * [Lru Cache](https://github.com/TheAlgorithms/Python/blob/master/other/lru_cache.py) + * [Magicdiamondpattern](https://github.com/TheAlgorithms/Python/blob/master/other/magicdiamondpattern.py) + * [Markov Chain](https://github.com/TheAlgorithms/Python/blob/master/other/markov_chain.py) + * [Max Sum Sliding Window](https://github.com/TheAlgorithms/Python/blob/master/other/max_sum_sliding_window.py) + * [Median Of Two Arrays](https://github.com/TheAlgorithms/Python/blob/master/other/median_of_two_arrays.py) + * [Nested Brackets](https://github.com/TheAlgorithms/Python/blob/master/other/nested_brackets.py) + * [Palindrome](https://github.com/TheAlgorithms/Python/blob/master/other/palindrome.py) + * [Password Generator](https://github.com/TheAlgorithms/Python/blob/master/other/password_generator.py) + * [Primelib](https://github.com/TheAlgorithms/Python/blob/master/other/primelib.py) + * [Scoring Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/scoring_algorithm.py) + * [Sdes](https://github.com/TheAlgorithms/Python/blob/master/other/sdes.py) + * [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/other/sierpinski_triangle.py) + * [Tower Of Hanoi](https://github.com/TheAlgorithms/Python/blob/master/other/tower_of_hanoi.py) + * [Triplet Sum](https://github.com/TheAlgorithms/Python/blob/master/other/triplet_sum.py) + * [Two Pointer](https://github.com/TheAlgorithms/Python/blob/master/other/two_pointer.py) + * [Two Sum](https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py) + * [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/other/word_patterns.py) + +## Project Euler + * Problem 001 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_001/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_001/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_001/sol3.py) + * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_001/sol4.py) + * [Sol5](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_001/sol5.py) + * [Sol6](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_001/sol6.py) + * [Sol7](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_001/sol7.py) + * Problem 002 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_002/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_002/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_002/sol3.py) + * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_002/sol4.py) + * [Sol5](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_002/sol5.py) + * Problem 003 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_003/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_003/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_003/sol3.py) + * Problem 004 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_004/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_004/sol2.py) + * Problem 005 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_005/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_005/sol2.py) + * Problem 006 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_006/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_006/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_006/sol3.py) + * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_006/sol4.py) + * Problem 007 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_007/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_007/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_007/sol3.py) + * Problem 008 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_008/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_008/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_008/sol3.py) + * Problem 009 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_009/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_009/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_009/sol3.py) + * Problem 010 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_010/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_010/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_010/sol3.py) + * Problem 011 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_011/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_011/sol2.py) + * Problem 012 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_012/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_012/sol2.py) + * Problem 013 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_013/sol1.py) + * Problem 014 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_014/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_014/sol2.py) + * Problem 015 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_015/sol1.py) + * Problem 016 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_016/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_016/sol2.py) + * Problem 017 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_017/sol1.py) + * Problem 018 + * [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_018/solution.py) + * Problem 019 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_019/sol1.py) + * Problem 020 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_020/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_020/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_020/sol3.py) + * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_020/sol4.py) + * Problem 021 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_021/sol1.py) + * Problem 022 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_022/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_022/sol2.py) + * Problem 023 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_023/sol1.py) + * Problem 024 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_024/sol1.py) + * Problem 025 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_025/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_025/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_025/sol3.py) + * Problem 026 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_026/sol1.py) + * Problem 027 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_027/sol1.py) + * Problem 028 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_028/sol1.py) + * Problem 029 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_029/sol1.py) + * Problem 030 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_030/sol1.py) + * Problem 031 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_031/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_031/sol2.py) + * Problem 032 + * [Sol32](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_032/sol32.py) + * Problem 033 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_033/sol1.py) + * Problem 034 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_034/sol1.py) + * Problem 035 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_035/sol1.py) + * Problem 036 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_036/sol1.py) + * Problem 037 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_037/sol1.py) + * Problem 038 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_038/sol1.py) + * Problem 039 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_039/sol1.py) + * Problem 040 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_040/sol1.py) + * Problem 041 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_041/sol1.py) + * Problem 042 + * [Solution42](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_042/solution42.py) + * Problem 043 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_043/sol1.py) + * Problem 044 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_044/sol1.py) + * Problem 045 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_045/sol1.py) + * Problem 046 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_046/sol1.py) + * Problem 047 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_047/sol1.py) + * Problem 048 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_048/sol1.py) + * Problem 049 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_049/sol1.py) + * Problem 050 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_050/sol1.py) + * Problem 051 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_051/sol1.py) + * Problem 052 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_052/sol1.py) + * Problem 053 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_053/sol1.py) + * Problem 054 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_054/sol1.py) + * [Test Poker Hand](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_054/test_poker_hand.py) + * Problem 055 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_055/sol1.py) + * Problem 056 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_056/sol1.py) + * Problem 057 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_057/sol1.py) + * Problem 058 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_058/sol1.py) + * Problem 059 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_059/sol1.py) + * Problem 062 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_062/sol1.py) + * Problem 063 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_063/sol1.py) + * Problem 064 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_064/sol1.py) + * Problem 065 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_065/sol1.py) + * Problem 067 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_067/sol1.py) + * Problem 069 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_069/sol1.py) + * Problem 070 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_070/sol1.py) + * Problem 071 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_071/sol1.py) + * Problem 072 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_072/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_072/sol2.py) + * Problem 074 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol2.py) + * Problem 075 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_075/sol1.py) + * Problem 076 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_076/sol1.py) + * Problem 077 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_077/sol1.py) + * Problem 080 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_080/sol1.py) + * Problem 081 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_081/sol1.py) + * Problem 085 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_085/sol1.py) + * Problem 086 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_086/sol1.py) + * Problem 087 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_087/sol1.py) + * Problem 089 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_089/sol1.py) + * Problem 091 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_091/sol1.py) + * Problem 097 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_097/sol1.py) + * Problem 099 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_099/sol1.py) + * Problem 101 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_101/sol1.py) + * Problem 102 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_102/sol1.py) + * Problem 107 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_107/sol1.py) + * Problem 112 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_112/sol1.py) + * Problem 113 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_113/sol1.py) + * Problem 119 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_119/sol1.py) + * Problem 120 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_120/sol1.py) + * Problem 123 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_123/sol1.py) + * Problem 125 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_125/sol1.py) + * Problem 129 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_129/sol1.py) + * Problem 135 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_135/sol1.py) + * Problem 173 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py) + * Problem 174 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_174/sol1.py) + * Problem 180 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_180/sol1.py) + * Problem 188 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_188/sol1.py) + * Problem 191 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_191/sol1.py) + * Problem 203 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_203/sol1.py) + * Problem 206 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_206/sol1.py) + * Problem 207 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_207/sol1.py) + * Problem 234 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_234/sol1.py) + * Problem 301 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_301/sol1.py) + * Problem 551 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py) + +## Quantum + * [Deutsch Jozsa](https://github.com/TheAlgorithms/Python/blob/master/quantum/deutsch_jozsa.py) + * [Half Adder](https://github.com/TheAlgorithms/Python/blob/master/quantum/half_adder.py) + * [Not Gate](https://github.com/TheAlgorithms/Python/blob/master/quantum/not_gate.py) + * [Quantum Entanglement](https://github.com/TheAlgorithms/Python/blob/master/quantum/quantum_entanglement.py) + * [Ripple Adder Classic](https://github.com/TheAlgorithms/Python/blob/master/quantum/ripple_adder_classic.py) + * [Single Qubit Measure](https://github.com/TheAlgorithms/Python/blob/master/quantum/single_qubit_measure.py) + +## Scheduling + * [First Come First Served](https://github.com/TheAlgorithms/Python/blob/master/scheduling/first_come_first_served.py) + * [Round Robin](https://github.com/TheAlgorithms/Python/blob/master/scheduling/round_robin.py) + * [Shortest Job First](https://github.com/TheAlgorithms/Python/blob/master/scheduling/shortest_job_first.py) + +## Searches + * [Binary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/binary_search.py) + * [Double Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/double_linear_search.py) + * [Double Linear Search Recursion](https://github.com/TheAlgorithms/Python/blob/master/searches/double_linear_search_recursion.py) + * [Fibonacci Search](https://github.com/TheAlgorithms/Python/blob/master/searches/fibonacci_search.py) + * [Hill Climbing](https://github.com/TheAlgorithms/Python/blob/master/searches/hill_climbing.py) + * [Interpolation Search](https://github.com/TheAlgorithms/Python/blob/master/searches/interpolation_search.py) + * [Jump Search](https://github.com/TheAlgorithms/Python/blob/master/searches/jump_search.py) + * [Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/linear_search.py) + * [Quick Select](https://github.com/TheAlgorithms/Python/blob/master/searches/quick_select.py) + * [Sentinel Linear Search](https://github.com/TheAlgorithms/Python/blob/master/searches/sentinel_linear_search.py) + * [Simple Binary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/simple_binary_search.py) + * [Simulated Annealing](https://github.com/TheAlgorithms/Python/blob/master/searches/simulated_annealing.py) + * [Tabu Search](https://github.com/TheAlgorithms/Python/blob/master/searches/tabu_search.py) + * [Ternary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/ternary_search.py) + +## Sorts + * [Bead Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/bead_sort.py) + * [Bitonic Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/bitonic_sort.py) + * [Bogo Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/bogo_sort.py) + * [Bubble Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/bubble_sort.py) + * [Bucket Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/bucket_sort.py) + * [Cocktail Shaker Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/cocktail_shaker_sort.py) + * [Comb Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/comb_sort.py) + * [Counting Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/counting_sort.py) + * [Cycle Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/cycle_sort.py) + * [Double Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/double_sort.py) + * [External Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/external_sort.py) + * [Gnome Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/gnome_sort.py) + * [Heap Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/heap_sort.py) + * [Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/insertion_sort.py) + * [Intro Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/intro_sort.py) + * [Iterative Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/iterative_merge_sort.py) + * [Merge Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_insertion_sort.py) + * [Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_sort.py) + * [Natural Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/natural_sort.py) + * [Odd Even Transposition Parallel](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_parallel.py) + * [Odd Even Transposition Single Threaded](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_single_threaded.py) + * [Pancake Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pancake_sort.py) + * [Patience Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/patience_sort.py) + * [Pigeon Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pigeon_sort.py) + * [Pigeonhole Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pigeonhole_sort.py) + * [Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort.py) + * [Quick Sort 3 Partition](https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort_3_partition.py) + * [Radix Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/radix_sort.py) + * [Random Normal Distribution Quicksort](https://github.com/TheAlgorithms/Python/blob/master/sorts/random_normal_distribution_quicksort.py) + * [Random Pivot Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/random_pivot_quick_sort.py) + * [Recursive Bubble Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_bubble_sort.py) + * [Recursive Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_insertion_sort.py) + * [Recursive Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_quick_sort.py) + * [Selection Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/selection_sort.py) + * [Shell Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/shell_sort.py) + * [Stooge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/stooge_sort.py) + * [Strand Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/strand_sort.py) + * [Tim Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/tim_sort.py) + * [Topological Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/topological_sort.py) + * [Tree Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/tree_sort.py) + * [Unknown Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/unknown_sort.py) + * [Wiggle Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/wiggle_sort.py) + +## Strings + * [Aho Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho_corasick.py) + * [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py) + * [Can String Be Rearranged As Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/can_string_be_rearranged_as_palindrome.py) + * [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py) + * [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py) + * [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py) + * [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/is_palindrome.py) + * [Jaro Winkler](https://github.com/TheAlgorithms/Python/blob/master/strings/jaro_winkler.py) + * [Knuth Morris Pratt](https://github.com/TheAlgorithms/Python/blob/master/strings/knuth_morris_pratt.py) + * [Levenshtein Distance](https://github.com/TheAlgorithms/Python/blob/master/strings/levenshtein_distance.py) + * [Lower](https://github.com/TheAlgorithms/Python/blob/master/strings/lower.py) + * [Manacher](https://github.com/TheAlgorithms/Python/blob/master/strings/manacher.py) + * [Min Cost String Conversion](https://github.com/TheAlgorithms/Python/blob/master/strings/min_cost_string_conversion.py) + * [Naive String Search](https://github.com/TheAlgorithms/Python/blob/master/strings/naive_string_search.py) + * [Prefix Function](https://github.com/TheAlgorithms/Python/blob/master/strings/prefix_function.py) + * [Rabin Karp](https://github.com/TheAlgorithms/Python/blob/master/strings/rabin_karp.py) + * [Remove Duplicate](https://github.com/TheAlgorithms/Python/blob/master/strings/remove_duplicate.py) + * [Reverse Letters](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_letters.py) + * [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py) + * [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py) + * [Swap Case](https://github.com/TheAlgorithms/Python/blob/master/strings/swap_case.py) + * [Upper](https://github.com/TheAlgorithms/Python/blob/master/strings/upper.py) + * [Word Occurrence](https://github.com/TheAlgorithms/Python/blob/master/strings/word_occurrence.py) + * [Z Function](https://github.com/TheAlgorithms/Python/blob/master/strings/z_function.py) + +## Traversals + * [Binary Tree Traversals](https://github.com/TheAlgorithms/Python/blob/master/traversals/binary_tree_traversals.py) + +## Web Programming + * [Co2 Emission](https://github.com/TheAlgorithms/Python/blob/master/web_programming/co2_emission.py) + * [Covid Stats Via Xpath](https://github.com/TheAlgorithms/Python/blob/master/web_programming/covid_stats_via_xpath.py) + * [Crawl Google Results](https://github.com/TheAlgorithms/Python/blob/master/web_programming/crawl_google_results.py) + * [Crawl Google Scholar Citation](https://github.com/TheAlgorithms/Python/blob/master/web_programming/crawl_google_scholar_citation.py) + * [Currency Converter](https://github.com/TheAlgorithms/Python/blob/master/web_programming/currency_converter.py) + * [Current Stock Price](https://github.com/TheAlgorithms/Python/blob/master/web_programming/current_stock_price.py) + * [Current Weather](https://github.com/TheAlgorithms/Python/blob/master/web_programming/current_weather.py) + * [Daily Horoscope](https://github.com/TheAlgorithms/Python/blob/master/web_programming/daily_horoscope.py) + * [Emails From Url](https://github.com/TheAlgorithms/Python/blob/master/web_programming/emails_from_url.py) + * [Fetch Bbc News](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_bbc_news.py) + * [Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_github_info.py) + * [Fetch Jobs](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_jobs.py) + * [Get Imdb Top 250 Movies Csv](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdb_top_250_movies_csv.py) + * [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py) + * [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py) + * [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py) + * [Instagram Video](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_video.py) + * [Recaptcha Verification](https://github.com/TheAlgorithms/Python/blob/master/web_programming/recaptcha_verification.py) + * [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py) + * [Test Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/test_fetch_github_info.py) + * [World Covid19 Stats](https://github.com/TheAlgorithms/Python/blob/master/web_programming/world_covid19_stats.py) diff --git a/License b/LICENSE.md similarity index 96% rename from License rename to LICENSE.md index c84ae570c084..3b7951527ab3 100644 --- a/License +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2016 The Algorithms +Copyright (c) 2020 The Algorithms Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 1e43deb6bdef..f81031b53ebb 100644 --- a/README.md +++ b/README.md @@ -1,355 +1,27 @@ -# The Algorithms - Python -[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=JP3BLXA6KMDGW) - +# The Algorithms - Python +[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod&style=flat-square)](https://gitpod.io/#https://github.com/TheAlgorithms/Python)  +[![Gitter chat](https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square)](https://gitter.im/TheAlgorithms)  +[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/TheAlgorithms/Python/build?label=CI&logo=github&style=flat-square)](https://github.com/TheAlgorithms/Python/actions)  +[![LGTM](https://img.shields.io/lgtm/alerts/github/TheAlgorithms/Python.svg?label=LGTM&logo=LGTM&style=flat-square)](https://lgtm.com/projects/g/TheAlgorithms/Python/alerts)  +[![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md)  +[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg?logo=paypal&style=flat-square)](https://www.paypal.me/TheAlgorithms/100)  +![](https://img.shields.io/github/repo-size/TheAlgorithms/Python.svg?label=Repo%20size&style=flat-square)  +[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square)](https://github.com/pre-commit/pre-commit)  +[![code style: black](https://img.shields.io/static/v1?label=code%20style&message=black&color=black&style=flat-square)](https://github.com/psf/black)  + ### All algorithms implemented in Python (for education) -These implementations are for demonstration purposes. They are less efficient than the implementations in the Python standard library. - -## Sorting Algorithms - - -### Bubble Sort -![alt text][bubble-image] - -**Bubble sort**, sometimes referred to as *sinking sort*, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. - -__Properties__ -* Worst case performance O(n2) -* Best case performance O(n) -* Average case performance O(n2) - -###### Source: [Wikipedia][bubble-wiki] -###### View the algorithm in [action][bubble-toptal] - -### Bucket -![alt text][bucket-image-1] -![alt text][bucket-image-2] - -**Bucket sort**, or _bin sort_, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. - -__Properties__ -* Worst case performance O(n2) -* Best case performance O(n+k) -* Average case performance O(n+k) - -###### Source: [Wikipedia][bucket-wiki] - - -### Cocktail shaker -![alt text][cocktail-shaker-image] - -**Cocktail shaker sort**, also known as _bidirectional bubble sort_, _cocktail sort_, _shaker sort_ (which can also refer to a variant of _selection sort_), _ripple sort_, _shuffle sort_, or _shuttle sort_, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. - -__Properties__ -* Worst case performance O(n2) -* Best case performance O(n) -* Average case performance O(n2) - -###### Source: [Wikipedia][cocktail-shaker-wiki] - - -### Insertion Sort -![alt text][insertion-image] - -**Insertion sort** is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on *large* lists than more advanced algorithms such as quicksort, heapsort, or merge sort. - -__Properties__ -* Worst case performance O(n2) -* Best case performance O(n) -* Average case performance O(n2) - -###### Source: [Wikipedia][insertion-wiki] -###### View the algorithm in [action][insertion-toptal] - - -### Merge Sort -![alt text][merge-image] - -**Merge sort** (also commonly spelled *mergesort*) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945. - -__Properties__ -* Worst case performance O(n log n) -* Best case performance O(n log n) -* Average case performance O(n log n) - -###### Source: [Wikipedia][merge-wiki] -###### View the algorithm in [action][merge-toptal] - -### Quick -![alt text][quick-image] - -**Quicksort** (sometimes called *partition-exchange sort*) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. - -__Properties__ -* Worst case performance O(n2) -* Best case performance O(*n* log *n*) or O(n) with three-way partition -* Average case performance O(*n* log *n*) - -###### Source: [Wikipedia][quick-wiki] -###### View the algorithm in [action][quick-toptal] - - -### Heap -![alt text][heapsort-image] - -**Heapsort** is a _comparison-based_ sorting algorithm. It can be thought of as an improved selection sort. It divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. - -__Properties__ -* Worst case performance O(*n* log *n*) -* Best case performance O(*n* log *n*) -* Average case performance O(*n* log *n*) - -###### Source: [Wikipedia][heap-wiki] -###### View the algorithm in [action](https://www.toptal.com/developers/sorting-algorithms/heap-sort) - - -### Radix - -From [Wikipedia][radix-wiki]: Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. - -__Properties__ -* Worst case performance O(wn) -* Best case performance O(wn) -* Average case performance O(wn) - -###### Source: [Wikipedia][radix-wiki] - - -### Selection -![alt text][selection-image] - -**Selection sort** is an algorithm that divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. - -__Properties__ -* Worst case performance O(n2) -* Best case performance O(n2) -* Average case performance O(n2) - -###### Source: [Wikipedia][selection-wiki] -###### View the algorithm in [action][selection-toptal] - - -### Shell -![alt text][shell-image] - -**Shellsort** is a generalization of *insertion sort* that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted. - -__Properties__ -* Worst case performance O(*n*log2*n*) -* Best case performance O(*n* log *n*) -* Average case performance depends on gap sequence - -###### Source: [Wikipedia][shell-wiki] -###### View the algorithm in [action][shell-toptal] - - -### Topological - -From [Wikipedia][topological-wiki]: **Topological sort**, or _topological ordering of a directed graph_ is a linear ordering of its vertices such that for every directed edge _uv_ from vertex _u_ to vertex _v_, _u_ comes before _v_ in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the edges may represent constraints that one task must be performed before another; in this application, a topological ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a _directed acyclic graph_ (DAG). Any DAG has at least one topological ordering, and algorithms are known for constructing a topological ordering of any DAG in linear time. - -### Time-Complexity Graphs - -Comparing the complexity of sorting algorithms (*Bubble Sort*, *Insertion Sort*, *Selection Sort*) - -![Complexity Graphs](https://github.com/prateekiiest/Python/blob/master/sorts/sortinggraphs.png) - -Comparing the sorting algorithms: -
-Quicksort is a very fast algorithm but can be pretty tricky to implement -
-Bubble sort is a slow algorithm but is very easy to implement. To sort small sets of data, bubble sort may be a better option since it can be implemented quickly, but for larger datasets, the speedup from quicksort might be worth the trouble implementing the algorithm. - ----------------------------------------------------------------------------------- - -## Search Algorithms - -### Linear -![alt text][linear-image] - -**Linear search** or *sequential search* is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched. Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. - -__Properties__ -* Worst case performance O(n) -* Best case performance O(1) -* Average case performance O(n) -* Worst case space complexity O(1) iterative - -###### Source: [Wikipedia][linear-wiki] - - -### Binary -![alt text][binary-image] - -**Binary search**, also known as *half-interval search* or *logarithmic search*, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful. - -__Properties__ -* Worst case performance O(log n) -* Best case performance O(1) -* Average case performance O(log n) -* Worst case space complexity O(1) - -###### Source: [Wikipedia][binary-wiki] - - -## Interpolation -**Interpolation search** is an algorithm for searching for a key in an array that has been ordered by numerical values assigned to the keys (key values). It was first described by W. W. Peterson in 1957. Interpolation search resembles the method by which people search a telephone directory for a name (the key value by which the book's entries are ordered): in each step the algorithm calculates where in the remaining search space the sought item might be, based on the key values at the bounds of the search space and the value of the sought key, usually via a linear interpolation. The key value actually found at this estimated position is then compared to the key value being sought. If it is not equal, then depending on the comparison, the remaining search space is reduced to the part before or after the estimated position. This method will only work if calculations on the size of differences between key values are sensible. - -By comparison, binary search always chooses the middle of the remaining search space, discarding one half or the other, depending on the comparison between the key found at the estimated position and the key sought — it does not require numerical values for the keys, just a total order on them. The remaining search space is reduced to the part before or after the estimated position. The linear search uses equality only as it compares elements one-by-one from the start, ignoring any sorting. - -On average the interpolation search makes about log(log(n)) comparisons (if the elements are uniformly distributed), where n is the number of elements to be searched. In the worst case (for instance where the numerical values of the keys increase exponentially) it can make up to O(n) comparisons. - -In interpolation-sequential search, interpolation is used to find an item near the one being searched for, then linear search is used to find the exact item. - -###### Source: [Wikipedia][interpolation-wiki] - - -## Jump Search -**Jump search** or _block search_ refers to a search algorithm for ordered lists. It works by first checking all items Lkm, where {\displaystyle k\in \mathbb {N} } k\in \mathbb {N} and m is the block size, until an item is found that is larger than the search key. To find the exact position of the search key in the list a linear search is performed on the sublist L[(k-1)m, km]. - -The optimal value of m is √n, where n is the length of the list L. Because both steps of the algorithm look at, at most, √n items the algorithm runs in O(√n) time. This is better than a linear search, but worse than a binary search. The advantage over the latter is that a jump search only needs to jump backwards once, while a binary can jump backwards up to log n times. This can be important if a jumping backwards takes significantly more time than jumping forward. - -The algorithm can be modified by performing multiple levels of jump search on the sublists, before finally performing the linear search. For an k-level jump search the optimum block size ml for the lth level (counting from 1) is n(k-l)/k. The modified algorithm will perform k backward jumps and runs in O(kn1/(k+1)) time. - -###### Source: [Wikipedia][jump-wiki] - - -## Quick Select -![alt text][QuickSelect-image] - -**Quick Select** is a selection algorithm to find the kth smallest element in an unordered list. It is related to the quicksort sorting algorithm. Like quicksort, it was developed by Tony Hoare, and thus is also known as Hoare's selection algorithm.[1] Like quicksort, it is efficient in practice and has good average-case performance, but has poor worst-case performance. Quickselect and its variants are the selection algorithms most often used in efficient real-world implementations. - -Quickselect uses the same overall approach as quicksort, choosing one element as a pivot and partitioning the data in two based on the pivot, accordingly as less than or greater than the pivot. However, instead of recursing into both sides, as in quicksort, quickselect only recurses into one side – the side with the element it is searching for. This reduces the average complexity from O(n log n) to O(n), with a worst case of O(n2). - -As with quicksort, quickselect is generally implemented as an in-place algorithm, and beyond selecting the k'th element, it also partially sorts the data. See selection algorithm for further discussion of the connection with sorting. - -###### Source: [Wikipedia][quick-wiki] - - -## Tabu -**Tabu search** uses a local or neighborhood search procedure to iteratively move from one potential solution {\displaystyle x} x to an improved solution {\displaystyle x'} x' in the neighborhood of {\displaystyle x} x, until some stopping criterion has been satisfied (generally, an attempt limit or a score threshold). Local search procedures often become stuck in poor-scoring areas or areas where scores plateau. In order to avoid these pitfalls and explore regions of the search space that would be left unexplored by other local search procedures, tabu search carefully explores the neighborhood of each solution as the search progresses. The solutions admitted to the new neighborhood, {\displaystyle N^{*}(x)} N^*(x), are determined through the use of memory structures. Using these memory structures, the search progresses by iteratively moving from the current solution {\displaystyle x} x to an improved solution {\displaystyle x'} x' in {\displaystyle N^{*}(x)} N^*(x). - -These memory structures form what is known as the tabu list, a set of rules and banned solutions used to filter which solutions will be admitted to the neighborhood {\displaystyle N^{*}(x)} N^*(x) to be explored by the search. In its simplest form, a tabu list is a short-term set of the solutions that have been visited in the recent past (less than {\displaystyle n} n iterations ago, where {\displaystyle n} n is the number of previous solutions to be stored — is also called the tabu tenure). More commonly, a tabu list consists of solutions that have changed by the process of moving from one solution to another. It is convenient, for ease of description, to understand a “solution” to be coded and represented by such attributes. - -###### Source: [Wikipedia][tabu-wiki] - ----------------------------------------------------------------------------------------------------------------------- - -## Ciphers - -### Caesar -![alt text][caesar] - -**Caesar cipher**, also known as _Caesar's cipher_, the _shift cipher_, _Caesar's code_ or _Caesar shift_, is one of the simplest and most widely known encryption techniques.
-It is **a type of substitution cipher** in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.
-The method is named after **Julius Caesar**, who used it in his private correspondence.
-The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security. - -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Caesar_cipher) - - -### Vigenère - -**Vigenère cipher** is a method of encrypting alphabetic text by using a series of **interwoven Caesar ciphers** based on the letters of a keyword. It is **a form of polyalphabetic substitution**.
-The Vigenère cipher has been reinvented many times. The method was originally described by Giovan Battista Bellaso in his 1553 book La cifra del. Sig. Giovan Battista Bellaso; however, the scheme was later misattributed to Blaise de Vigenère in the 19th century, and is now widely known as the "Vigenère cipher".
-Though the cipher is easy to understand and implement, for three centuries it resisted all attempts to break it; this earned it the description **le chiffre indéchiffrable**(French for 'the indecipherable cipher'). -Many people have tried to implement encryption schemes that are essentially Vigenère ciphers. Friedrich Kasiski was the first to publish a general method of deciphering a Vigenère cipher in 1863. - -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) - - -### Transposition - -**Transposition cipher** is a method of encryption by which the positions held by units of *plaintext* (which are commonly characters or groups of characters) are shifted according to a regular system, so that the *ciphertext* constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered).
- -Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt. - -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher) - - -### RSA (Rivest–Shamir–Adleman) -**RSA** _(Rivest–Shamir–Adleman)_ is one of the first public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key is public and it is different from the decryption key which is kept secret (private). In RSA, this asymmetry is based on the practical difficulty of the factorization of the product of two large prime numbers, the "factoring problem". The acronym RSA is made of the initial letters of the surnames of Ron Rivest, Adi Shamir, and Leonard Adleman, who first publicly described the algorithm in 1978. Clifford Cocks, an English mathematician working for the British intelligence agency Government Communications Headquarters (GCHQ), had developed an equivalent system in 1973, but this was not declassified until 1997.[1] - -A user of RSA creates and then publishes a public key based on two large prime numbers, along with an auxiliary value. The prime numbers must be kept secret. Anyone can use the public key to encrypt a message, but with currently published methods, and if the public key is large enough, only someone with knowledge of the prime numbers can decode the message feasibly.[2] Breaking RSA encryption is known as the RSA problem. Whether it is as difficult as the factoring problem remains an open question. - -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/RSA_(cryptosystem)) - - -## ROT13 -![alt text][ROT13-image] - -**ROT13** ("rotate by 13 places", sometimes hyphenated _ROT-13_) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in the alphabet. ROT13 is a special case of the Caesar cipher which was developed in ancient Rome. - -Because there are 26 letters (2×13) in the basic Latin alphabet, ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption.[1] - -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/ROT13) - - -## XOR -**XOR cipher** is a simple type of additive cipher,[1] an encryption algorithm that operates according to the principles: - -A {\displaystyle \oplus } \oplus 0 = A, -A {\displaystyle \oplus } \oplus A = 0, -(A {\displaystyle \oplus } \oplus B) {\displaystyle \oplus } \oplus C = A {\displaystyle \oplus } \oplus (B {\displaystyle \oplus } \oplus C), -(B {\displaystyle \oplus } \oplus A) {\displaystyle \oplus } \oplus A = B {\displaystyle \oplus } \oplus 0 = B, -where {\displaystyle \oplus } \oplus denotes the exclusive disjunction (XOR) operation. This operation is sometimes called modulus 2 addition (or subtraction, which is identical).[2] With this logic, a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher. - -###### Source: [Wikipedia](https://en.wikipedia.org/wiki/XOR_cipher) - - -[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort -[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort -[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort" - -[bucket-wiki]: https://en.wikipedia.org/wiki/Bucket_sort -[bucket-image-1]: https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/Bucket_sort_1.svg/311px-Bucket_sort_1.svg.png "Bucket Sort" -[bucket-image-2]: https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Bucket_sort_2.svg/311px-Bucket_sort_2.svg.png "Bucket Sort" - -[cocktail-shaker-wiki]: https://en.wikipedia.org/wiki/Cocktail_shaker_sort -[cocktail-shaker-image]: https://upload.wikimedia.org/wikipedia/commons/e/ef/Sorting_shaker_sort_anim.gif "Cocktail Shaker Sort" - -[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort -[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort -[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort" - -[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort -[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort -[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort" - -[heapsort-image]: https://upload.wikimedia.org/wikipedia/commons/4/4d/Heapsort-example.gif "Heap Sort" -[heap-wiki]: https://en.wikipedia.org/wiki/Heapsort - -[radix-wiki]: https://en.wikipedia.org/wiki/Radix_sort - -[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort -[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort -[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort" - -[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort -[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort -[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort" - -[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort -[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort -[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort" - -[topological-wiki]: https://en.wikipedia.org/wiki/Topological_sorting - -[linear-wiki]: https://en.wikipedia.org/wiki/Linear_search -[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif "Linear Search" - -[binary-wiki]: https://en.wikipedia.org/wiki/Binary_search_algorithm -[binary-image]: https://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_search_into_array.png "Binary Search" - - -[interpolation-wiki]: https://en.wikipedia.org/wiki/Interpolation_search +These implementations are for learning purposes only. Therefore they may be less efficient than the implementations in the Python standard library. -[jump-wiki]: https://en.wikipedia.org/wiki/Jump_search +## Contribution Guidelines -[quick-wiki]: https://en.wikipedia.org/wiki/Quickselect +Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute. -[tabu-wiki]: https://en.wikipedia.org/wiki/Tabu_search +## Community Channel -[ROT13-image]: https://upload.wikimedia.org/wikipedia/commons/3/33/ROT13_table_with_example.svg "ROT13" +We're on [Gitter](https://gitter.im/TheAlgorithms)! Please join us. -[JumpSearch-image]: https://i1.wp.com/theoryofprogramming.com/wp-content/uploads/2016/11/jump-search-1.jpg "Jump Search" +## List of Algorithms -[QuickSelect-image]: https://upload.wikimedia.org/wikipedia/commons/0/04/Selecting_quickselect_frames.gif "Quick Select" +See our [directory](DIRECTORY.md). diff --git a/data_structures/union_find/__init__.py b/arithmetic_analysis/__init__.py similarity index 100% rename from data_structures/union_find/__init__.py rename to arithmetic_analysis/__init__.py diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index c81fa84f81e1..0ef691678702 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -1,33 +1,55 @@ -import math +from typing import Callable -def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano - - start = a - end = b +def bisection(function: Callable[[float], float], a: float, b: float) -> float: + """ + finds where function becomes 0 in [a,b] using bolzano + >>> bisection(lambda x: x ** 3 - 1, -5, 5) + 1.0000000149011612 + >>> bisection(lambda x: x ** 3 - 1, 2, 1000) + Traceback (most recent call last): + ... + ValueError: could not find root in given interval. + >>> bisection(lambda x: x ** 2 - 4 * x + 3, 0, 2) + 1.0 + >>> bisection(lambda x: x ** 2 - 4 * x + 3, 2, 4) + 3.0 + >>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000) + Traceback (most recent call last): + ... + ValueError: could not find root in given interval. + """ + start: float = a + end: float = b if function(a) == 0: # one of the a or b is a root for the function return a elif function(b) == 0: return b - elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative, - # then his algorithm can't find the root - print("couldn't find root in [a,b]") - return + elif ( + function(a) * function(b) > 0 + ): # if none of these are root and they are both positive or negative, + # then this algorithm can't find the root + raise ValueError("could not find root in given interval.") else: - mid = (start + end) / 2 - while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7 + mid: float = start + (end - start) / 2.0 + while abs(start - mid) > 10 ** -7: # until precisely equals to 10^-7 if function(mid) == 0: return mid elif function(mid) * function(start) < 0: end = mid else: start = mid - mid = (start + end) / 2 + mid = start + (end - start) / 2.0 return mid -def f(x): - return math.pow(x, 3) - 2*x - 5 +def f(x: float) -> float: + return x ** 3 - 2 * x - 5 + if __name__ == "__main__": print(bisection(f, 1, 1000)) + + import doctest + + doctest.testmod() diff --git a/arithmetic_analysis/gaussian_elimination.py b/arithmetic_analysis/gaussian_elimination.py new file mode 100644 index 000000000000..51207686c12a --- /dev/null +++ b/arithmetic_analysis/gaussian_elimination.py @@ -0,0 +1,83 @@ +""" +Gaussian elimination method for solving a system of linear equations. +Gaussian elimination - https://en.wikipedia.org/wiki/Gaussian_elimination +""" + + +import numpy as np + + +def retroactive_resolution(coefficients: np.matrix, vector: np.array) -> np.array: + """ + This function performs a retroactive linear system resolution + for triangular matrix + + Examples: + 2x1 + 2x2 - 1x3 = 5 2x1 + 2x2 = -1 + 0x1 - 2x2 - 1x3 = -7 0x1 - 2x2 = -1 + 0x1 + 0x2 + 5x3 = 15 + >>> gaussian_elimination([[2, 2, -1], [0, -2, -1], [0, 0, 5]], [[5], [-7], [15]]) + array([[2.], + [2.], + [3.]]) + >>> gaussian_elimination([[2, 2], [0, -2]], [[-1], [-1]]) + array([[-1. ], + [ 0.5]]) + """ + + rows, columns = np.shape(coefficients) + + x = np.zeros((rows, 1), dtype=float) + for row in reversed(range(rows)): + sum = 0 + for col in range(row + 1, columns): + sum += coefficients[row, col] * x[col] + + x[row, 0] = (vector[row] - sum) / coefficients[row, row] + + return x + + +def gaussian_elimination(coefficients: np.matrix, vector: np.array) -> np.array: + """ + This function performs Gaussian elimination method + + Examples: + 1x1 - 4x2 - 2x3 = -2 1x1 + 2x2 = 5 + 5x1 + 2x2 - 2x3 = -3 5x1 + 2x2 = 5 + 1x1 - 1x2 + 0x3 = 4 + >>> gaussian_elimination([[1, -4, -2], [5, 2, -2], [1, -1, 0]], [[-2], [-3], [4]]) + array([[ 2.3 ], + [-1.7 ], + [ 5.55]]) + >>> gaussian_elimination([[1, 2], [5, 2]], [[5], [5]]) + array([[0. ], + [2.5]]) + """ + # coefficients must to be a square matrix so we need to check first + rows, columns = np.shape(coefficients) + if rows != columns: + return [] + + # augmented matrix + augmented_mat = np.concatenate((coefficients, vector), axis=1) + augmented_mat = augmented_mat.astype("float64") + + # scale the matrix leaving it triangular + for row in range(rows - 1): + pivot = augmented_mat[row, row] + for col in range(row + 1, columns): + factor = augmented_mat[col, row] / pivot + augmented_mat[col, :] -= factor * augmented_mat[row, :] + + x = retroactive_resolution( + augmented_mat[:, 0:columns], augmented_mat[:, columns : columns + 1] + ) + + return x + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/arithmetic_analysis/image_data/2D_problems.jpg b/arithmetic_analysis/image_data/2D_problems.jpg new file mode 100644 index 000000000000..8887cf641685 Binary files /dev/null and b/arithmetic_analysis/image_data/2D_problems.jpg differ diff --git a/arithmetic_analysis/image_data/2D_problems_1.jpg b/arithmetic_analysis/image_data/2D_problems_1.jpg new file mode 100644 index 000000000000..aa9f45362014 Binary files /dev/null and b/arithmetic_analysis/image_data/2D_problems_1.jpg differ diff --git a/arithmetic_analysis/image_data/__init__.py b/arithmetic_analysis/image_data/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/arithmetic_analysis/in_static_equilibrium.py b/arithmetic_analysis/in_static_equilibrium.py new file mode 100644 index 000000000000..9b2892151850 --- /dev/null +++ b/arithmetic_analysis/in_static_equilibrium.py @@ -0,0 +1,82 @@ +""" +Checks if a system of forces is in static equilibrium. +""" +from typing import List + +from numpy import array, cos, cross, radians, sin + + +def polar_force( + magnitude: float, angle: float, radian_mode: bool = False +) -> List[float]: + """ + Resolves force along rectangular components. + (force, angle) => (force_x, force_y) + >>> polar_force(10, 45) + [7.0710678118654755, 7.071067811865475] + >>> polar_force(10, 3.14, radian_mode=True) + [-9.999987317275394, 0.01592652916486828] + """ + if radian_mode: + return [magnitude * cos(angle), magnitude * sin(angle)] + return [magnitude * cos(radians(angle)), magnitude * sin(radians(angle))] + + +def in_static_equilibrium( + forces: array, location: array, eps: float = 10 ** -1 +) -> bool: + """ + Check if a system is in equilibrium. + It takes two numpy.array objects. + forces ==> [ + [force1_x, force1_y], + [force2_x, force2_y], + ....] + location ==> [ + [x1, y1], + [x2, y2], + ....] + >>> force = array([[1, 1], [-1, 2]]) + >>> location = array([[1, 0], [10, 0]]) + >>> in_static_equilibrium(force, location) + False + """ + # summation of moments is zero + moments: array = cross(location, forces) + sum_moments: float = sum(moments) + return abs(sum_moments) < eps + + +if __name__ == "__main__": + # Test to check if it works + forces = array( + [polar_force(718.4, 180 - 30), polar_force(879.54, 45), polar_force(100, -90)] + ) + + location = array([[0, 0], [0, 0], [0, 0]]) + + assert in_static_equilibrium(forces, location) + + # Problem 1 in image_data/2D_problems.jpg + forces = array( + [ + polar_force(30 * 9.81, 15), + polar_force(215, 180 - 45), + polar_force(264, 90 - 30), + ] + ) + + location = array([[0, 0], [0, 0], [0, 0]]) + + assert in_static_equilibrium(forces, location) + + # Problem in image_data/2D_problems_1.jpg + forces = array([[0, -2000], [0, -1200], [0, 15600], [0, -12400]]) + + location = array([[0, 0], [6, 0], [10, 0], [12, 0]]) + + assert in_static_equilibrium(forces, location) + + import doctest + + doctest.testmod() diff --git a/arithmetic_analysis/intersection.py b/arithmetic_analysis/intersection.py index 2f25f76ebd96..204dd5d8a935 100644 --- a/arithmetic_analysis/intersection.py +++ b/arithmetic_analysis/intersection.py @@ -1,17 +1,49 @@ import math +from typing import Callable -def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points - x_n = x0 - x_n1 = x1 + +def intersection(function: Callable[[float], float], x0: float, x1: float) -> float: + """ + function is the f we want to find its root + x0 and x1 are two random starting points + >>> intersection(lambda x: x ** 3 - 1, -5, 5) + 0.9999999999954654 + >>> intersection(lambda x: x ** 3 - 1, 5, 5) + Traceback (most recent call last): + ... + ZeroDivisionError: float division by zero, could not find root + >>> intersection(lambda x: x ** 3 - 1, 100, 200) + 1.0000000000003888 + >>> intersection(lambda x: x ** 2 - 4 * x + 3, 0, 2) + 0.9999999998088019 + >>> intersection(lambda x: x ** 2 - 4 * x + 3, 2, 4) + 2.9999999998088023 + >>> intersection(lambda x: x ** 2 - 4 * x + 3, 4, 1000) + 3.0000000001786042 + >>> intersection(math.sin, -math.pi, math.pi) + 0.0 + >>> intersection(math.cos, -math.pi, math.pi) + Traceback (most recent call last): + ... + ZeroDivisionError: float division by zero, could not find root + """ + x_n: float = x0 + x_n1: float = x1 while True: - x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n))) - if abs(x_n2 - x_n1) < 10**-5: + if x_n == x_n1 or function(x_n1) == function(x_n): + raise ZeroDivisionError("float division by zero, could not find root") + x_n2: float = x_n1 - ( + function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n)) + ) + if abs(x_n2 - x_n1) < 10 ** -5: return x_n2 - x_n=x_n1 - x_n1=x_n2 + x_n = x_n1 + x_n1 = x_n2 + + +def f(x: float) -> float: + return math.pow(x, 3) - (2 * x) - 5 -def f(x): - return math.pow(x , 3) - (2 * x) -5 if __name__ == "__main__": - print(intersection(f,3,3.5)) + print(intersection(f, 3, 3.5)) diff --git a/arithmetic_analysis/lu_decomposition.py b/arithmetic_analysis/lu_decomposition.py index f291d2dfe003..ef37d1b7b4ef 100644 --- a/arithmetic_analysis/lu_decomposition.py +++ b/arithmetic_analysis/lu_decomposition.py @@ -1,32 +1,64 @@ -# lower–upper (LU) decomposition - https://en.wikipedia.org/wiki/LU_decomposition -import numpy +"""Lower-Upper (LU) Decomposition. -def LUDecompose (table): +Reference: +- https://en.wikipedia.org/wiki/LU_decomposition +""" +from typing import Tuple + +import numpy as np +from numpy import ndarray + + +def lower_upper_decomposition(table: ndarray) -> Tuple[ndarray, ndarray]: + """Lower-Upper (LU) Decomposition + + Example: + + >>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]]) + >>> outcome = lower_upper_decomposition(matrix) + >>> outcome[0] + array([[1. , 0. , 0. ], + [0. , 1. , 0. ], + [2.5, 8. , 1. ]]) + >>> outcome[1] + array([[ 2. , -2. , 1. ], + [ 0. , 1. , 2. ], + [ 0. , 0. , -17.5]]) + + >>> matrix = np.array([[2, -2, 1], [0, 1, 2]]) + >>> lower_upper_decomposition(matrix) + Traceback (most recent call last): + ... + ValueError: 'table' has to be of square shaped array but got a 2x3 array: + [[ 2 -2 1] + [ 0 1 2]] + """ # Table that contains our data # Table has to be a square array so we need to check first - rows,columns=numpy.shape(table) - L=numpy.zeros((rows,columns)) - U=numpy.zeros((rows,columns)) - if rows!=columns: - return [] - for i in range (columns): - for j in range(i-1): - sum=0 - for k in range (j-1): - sum+=L[i][k]*U[k][j] - L[i][j]=(table[i][j]-sum)/U[j][j] - L[i][i]=1 - for j in range(i-1,columns): - sum1=0 - for k in range(i-1): - sum1+=L[i][k]*U[k][j] - U[i][j]=table[i][j]-sum1 - return L,U + rows, columns = np.shape(table) + if rows != columns: + raise ValueError( + f"'table' has to be of square shaped array but got a {rows}x{columns} " + + f"array:\n{table}" + ) + lower = np.zeros((rows, columns)) + upper = np.zeros((rows, columns)) + for i in range(columns): + for j in range(i): + total = 0 + for k in range(j): + total += lower[i][k] * upper[k][j] + lower[i][j] = (table[i][j] - total) / upper[j][j] + lower[i][i] = 1 + for j in range(i, columns): + total = 0 + for k in range(i): + total += lower[i][k] * upper[k][j] + upper[i][j] = table[i][j] - total + return lower, upper + if __name__ == "__main__": - matrix =numpy.array([[2,-2,1], - [0,1,2], - [5,3,1]]) - L,U = LUDecompose(matrix) - print(L) - print(U) + import doctest + + doctest.testmod() diff --git a/arithmetic_analysis/newton_forward_interpolation.py b/arithmetic_analysis/newton_forward_interpolation.py new file mode 100644 index 000000000000..66cde4b73c4f --- /dev/null +++ b/arithmetic_analysis/newton_forward_interpolation.py @@ -0,0 +1,57 @@ +# https://www.geeksforgeeks.org/newton-forward-backward-interpolation/ + +import math +from typing import List + + +# for calculating u value +def ucal(u: float, p: int) -> float: + """ + >>> ucal(1, 2) + 0 + >>> ucal(1.1, 2) + 0.11000000000000011 + >>> ucal(1.2, 2) + 0.23999999999999994 + """ + temp = u + for i in range(1, p): + temp = temp * (u - i) + return temp + + +def main() -> None: + n = int(input("enter the numbers of values: ")) + y: List[List[float]] = [] + for i in range(n): + y.append([]) + for i in range(n): + for j in range(n): + y[i].append(j) + y[i][j] = 0 + + print("enter the values of parameters in a list: ") + x = list(map(int, input().split())) + + print("enter the values of corresponding parameters: ") + for i in range(n): + y[i][0] = float(input()) + + value = int(input("enter the value to interpolate: ")) + u = (value - x[0]) / (x[1] - x[0]) + + # for calculating forward difference table + + for i in range(1, n): + for j in range(n - i): + y[j][i] = y[j + 1][i - 1] - y[j][i - 1] + + summ = y[0][0] + for i in range(1, n): + summ += (ucal(u, i) * y[0][i]) / math.factorial(i) + + print(f"the value at {value} is {summ}") + + +if __name__ == "__main__": + main() diff --git a/arithmetic_analysis/newton_method.py b/arithmetic_analysis/newton_method.py index 2ed29502522e..a9a94372671e 100644 --- a/arithmetic_analysis/newton_method.py +++ b/arithmetic_analysis/newton_method.py @@ -1,18 +1,54 @@ +"""Newton's Method.""" + # Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method +from typing import Callable + +RealFunc = Callable[[float], float] # type alias for a real -> real function + + +# function is the f(x) and derivative is the f'(x) +def newton( + function: RealFunc, + derivative: RealFunc, + starting_int: int, +) -> float: + """ + >>> newton(lambda x: x ** 3 - 2 * x - 5, lambda x: 3 * x ** 2 - 2, 3) + 2.0945514815423474 + >>> newton(lambda x: x ** 3 - 1, lambda x: 3 * x ** 2, -2) + 1.0 + >>> newton(lambda x: x ** 3 - 1, lambda x: 3 * x ** 2, -4) + 1.0000000000000102 + >>> import math + >>> newton(math.sin, math.cos, 1) + 0.0 + >>> newton(math.sin, math.cos, 2) + 3.141592653589793 + >>> newton(math.cos, lambda x: -math.sin(x), 2) + 1.5707963267948966 + >>> newton(math.cos, lambda x: -math.sin(x), 0) + Traceback (most recent call last): + ... + ZeroDivisionError: Could not find root + """ + prev_guess = float(starting_int) + while True: + try: + next_guess = prev_guess - function(prev_guess) / derivative(prev_guess) + except ZeroDivisionError: + raise ZeroDivisionError("Could not find root") from None + if abs(prev_guess - next_guess) < 10 ** -5: + return next_guess + prev_guess = next_guess + + +def f(x: float) -> float: + return (x ** 3) - (2 * x) - 5 + -def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x) - x_n=startingInt - while True: - x_n1=x_n-function(x_n)/function1(x_n) - if abs(x_n-x_n1) < 10**-5: - return x_n1 - x_n=x_n1 - -def f(x): - return (x**3) - (2 * x) -5 +def f1(x: float) -> float: + return 3 * (x ** 2) - 2 -def f1(x): - return 3 * (x**2) -2 if __name__ == "__main__": - print(newton(f,f1,3)) + print(newton(f, f1, 3)) diff --git a/arithmetic_analysis/newton_raphson.py b/arithmetic_analysis/newton_raphson.py new file mode 100644 index 000000000000..146bb0aa5adf --- /dev/null +++ b/arithmetic_analysis/newton_raphson.py @@ -0,0 +1,43 @@ +# Implementing Newton Raphson method in Python +# Author: Syed Haseeb Shah (github.com/QuantumNovice) +# The Newton-Raphson method (also known as Newton's method) is a way to +# quickly find a good approximation for the root of a real-valued function +from decimal import Decimal +from math import * # noqa: F401, F403 +from typing import Union + +from sympy import diff + + +def newton_raphson( + func: str, a: Union[float, Decimal], precision: float = 10 ** -10 +) -> float: + """Finds root from the point 'a' onwards by Newton-Raphson method + >>> newton_raphson("sin(x)", 2) + 3.1415926536808043 + >>> newton_raphson("x**2 - 5*x +2", 0.4) + 0.4384471871911695 + >>> newton_raphson("x**2 - 5", 0.1) + 2.23606797749979 + >>> newton_raphson("log(x)- 1", 2) + 2.718281828458938 + """ + x = a + while True: + x = Decimal(x) - (Decimal(eval(func)) / Decimal(eval(str(diff(func))))) + # This number dictates the accuracy of the answer + if abs(eval(func)) < precision: + return float(x) + + +# Let's Execute +if __name__ == "__main__": + # Find root of trigonometric function + # Find value of pi + print(f"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}") + # Find root of polynomial + print(f"The root of x**2 - 5*x + 2 = 0 is {newton_raphson('x**2 - 5*x + 2', 0.4)}") + # Find Square Root of 5 + print(f"The root of log(x) - 1 = 0 is {newton_raphson('log(x) - 1', 2)}") + # Exponential Roots + print(f"The root of exp(x) - 1 = 0 is {newton_raphson('exp(x) - 1', 0)}") diff --git a/arithmetic_analysis/newton_raphson_method.py b/arithmetic_analysis/newton_raphson_method.py deleted file mode 100644 index 5e7e2f930abc..000000000000 --- a/arithmetic_analysis/newton_raphson_method.py +++ /dev/null @@ -1,36 +0,0 @@ -# Implementing Newton Raphson method in Python -# Author: Haseeb - -from sympy import diff -from decimal import Decimal - -def NewtonRaphson(func, a): - ''' Finds root from the point 'a' onwards by Newton-Raphson method ''' - while True: - c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) ) - - a = c - - # This number dictates the accuracy of the answer - if abs(eval(func)) < 10**-15: - return c - - -# Let's Execute -if __name__ == '__main__': - # Find root of trigonometric function - # Find value of pi - print ('sin(x) = 0', NewtonRaphson('sin(x)', 2)) - - # Find root of polynomial - print ('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4)) - - # Find Square Root of 5 - print ('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1)) - - # Exponential Roots - print ('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0)) - - - - diff --git a/arithmetic_analysis/secant_method.py b/arithmetic_analysis/secant_method.py new file mode 100644 index 000000000000..7eb1dd8f5c6b --- /dev/null +++ b/arithmetic_analysis/secant_method.py @@ -0,0 +1,29 @@ +""" +Implementing Secant method in Python +Author: dimgrichr +""" +from math import exp + + +def f(x: float) -> float: + """ + >>> f(5) + 39.98652410600183 + """ + return 8 * x - 2 * exp(-x) + + +def secant_method(lower_bound: float, upper_bound: float, repeats: int) -> float: + """ + >>> secant_method(1, 3, 2) + 0.2139409276214589 + """ + x0 = lower_bound + x1 = upper_bound + for i in range(0, repeats): + x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0)) + return x1 + + +if __name__ == "__main__": + print(f"Example: {secant_method(1, 3, 2) = }") diff --git a/backtracking/__init__.py b/backtracking/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py new file mode 100644 index 000000000000..76462837ce35 --- /dev/null +++ b/backtracking/all_combinations.py @@ -0,0 +1,46 @@ +""" + In this problem, we want to determine all possible combinations of k + numbers out of 1 ... n. We use backtracking to solve this problem. + Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))) +""" +from typing import List + + +def generate_all_combinations(n: int, k: int) -> List[List[int]]: + """ + >>> generate_all_combinations(n=4, k=2) + [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] + """ + + result: List[List[int]] = [] + create_all_state(1, n, k, [], result) + return result + + +def create_all_state( + increment: int, + total_number: int, + level: int, + current_list: List[int], + total_list: List[List[int]], +) -> None: + if level == 0: + total_list.append(current_list[:]) + return + + for i in range(increment, total_number - level + 2): + current_list.append(i) + create_all_state(i + 1, total_number, level - 1, current_list, total_list) + current_list.pop() + + +def print_all_state(total_list: List[List[int]]) -> None: + for i in total_list: + print(*i) + + +if __name__ == "__main__": + n = 4 + k = 2 + total_list = generate_all_combinations(n, k) + print_all_state(total_list) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py new file mode 100644 index 000000000000..a0032c5ca814 --- /dev/null +++ b/backtracking/all_permutations.py @@ -0,0 +1,51 @@ +""" + In this problem, we want to determine all possible permutations + of the given sequence. We use backtracking to solve this problem. + + Time complexity: O(n! * n), + where n denotes the length of the given sequence. +""" +from typing import List, Union + + +def generate_all_permutations(sequence: List[Union[int, str]]) -> None: + create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) + + +def create_state_space_tree( + sequence: List[Union[int, str]], + current_sequence: List[Union[int, str]], + index: int, + index_used: List[int], +) -> None: + """ + Creates a state space tree to iterate through each branch using DFS. + We know that each state has exactly len(sequence) - index children. + It terminates when it reaches the end of the given sequence. + """ + + if index == len(sequence): + print(current_sequence) + return + + for i in range(len(sequence)): + if not index_used[i]: + current_sequence.append(sequence[i]) + index_used[i] = True + create_state_space_tree(sequence, current_sequence, index + 1, index_used) + current_sequence.pop() + index_used[i] = False + + +""" +remove the comment to take an input from the user + +print("Enter the elements") +sequence = list(map(int, input().split())) +""" + +sequence: List[Union[int, str]] = [3, 1, 2, 4] +generate_all_permutations(sequence) + +sequence_2: List[Union[int, str]] = ["A", "B", "C"] +generate_all_permutations(sequence_2) diff --git a/backtracking/all_subsequences.py b/backtracking/all_subsequences.py new file mode 100644 index 000000000000..99db4ea46589 --- /dev/null +++ b/backtracking/all_subsequences.py @@ -0,0 +1,40 @@ +""" +In this problem, we want to determine all possible subsequences +of the given sequence. We use backtracking to solve this problem. + +Time complexity: O(2^n), +where n denotes the length of the given sequence. +""" +from typing import Any, List + + +def generate_all_subsequences(sequence: List[Any]) -> None: + create_state_space_tree(sequence, [], 0) + + +def create_state_space_tree( + sequence: List[Any], current_subsequence: List[Any], index: int +) -> None: + """ + Creates a state space tree to iterate through each branch using DFS. + We know that each state has exactly two children. + It terminates when it reaches the end of the given sequence. + """ + + if index == len(sequence): + print(current_subsequence) + return + + create_state_space_tree(sequence, current_subsequence, index + 1) + current_subsequence.append(sequence[index]) + create_state_space_tree(sequence, current_subsequence, index + 1) + current_subsequence.pop() + + +if __name__ == "__main__": + seq: List[Any] = [3, 1, 2, 4] + generate_all_subsequences(seq) + + seq.clear() + seq.extend(["A", "B", "C"]) + generate_all_subsequences(seq) diff --git a/backtracking/coloring.py b/backtracking/coloring.py new file mode 100644 index 000000000000..3956b21a9182 --- /dev/null +++ b/backtracking/coloring.py @@ -0,0 +1,114 @@ +""" + Graph Coloring also called "m coloring problem" + consists of coloring given graph with at most m colors + such that no adjacent vertices are assigned same color + + Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring +""" +from typing import List + + +def valid_coloring( + neighbours: List[int], colored_vertices: List[int], color: int +) -> bool: + """ + For each neighbour check if coloring constraint is satisfied + If any of the neighbours fail the constraint return False + If all neighbours validate constraint return True + + >>> neighbours = [0,1,0,1,0] + >>> colored_vertices = [0, 2, 1, 2, 0] + + >>> color = 1 + >>> valid_coloring(neighbours, colored_vertices, color) + True + + >>> color = 2 + >>> valid_coloring(neighbours, colored_vertices, color) + False + """ + # Does any neighbour not satisfy the constraints + return not any( + neighbour == 1 and colored_vertices[i] == color + for i, neighbour in enumerate(neighbours) + ) + + +def util_color( + graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int +) -> bool: + """ + Pseudo-Code + + Base Case: + 1. Check if coloring is complete + 1.1 If complete return True (meaning that we successfully colored graph) + + Recursive Step: + 2. Itterates over each color: + Check if current coloring is valid: + 2.1. Color given vertex + 2.2. Do recursive call check if this coloring leads to solving problem + 2.4. if current coloring leads to solution return + 2.5. Uncolor given vertex + + >>> graph = [[0, 1, 0, 0, 0], + ... [1, 0, 1, 0, 1], + ... [0, 1, 0, 1, 0], + ... [0, 1, 1, 0, 0], + ... [0, 1, 0, 0, 0]] + >>> max_colors = 3 + >>> colored_vertices = [0, 1, 0, 0, 0] + >>> index = 3 + + >>> util_color(graph, max_colors, colored_vertices, index) + True + + >>> max_colors = 2 + >>> util_color(graph, max_colors, colored_vertices, index) + False + """ + + # Base Case + if index == len(graph): + return True + + # Recursive Step + for i in range(max_colors): + if valid_coloring(graph[index], colored_vertices, i): + # Color current vertex + colored_vertices[index] = i + # Validate coloring + if util_color(graph, max_colors, colored_vertices, index + 1): + return True + # Backtrack + colored_vertices[index] = -1 + return False + + +def color(graph: List[List[int]], max_colors: int) -> List[int]: + """ + Wrapper function to call subroutine called util_color + which will either return True or False. + If True is returned colored_vertices list is filled with correct colorings + + >>> graph = [[0, 1, 0, 0, 0], + ... [1, 0, 1, 0, 1], + ... [0, 1, 0, 1, 0], + ... [0, 1, 1, 0, 0], + ... [0, 1, 0, 0, 0]] + + >>> max_colors = 3 + >>> color(graph, max_colors) + [0, 1, 0, 2, 0] + + >>> max_colors = 2 + >>> color(graph, max_colors) + [] + """ + colored_vertices = [-1] * len(graph) + + if util_color(graph, max_colors, colored_vertices, 0): + return colored_vertices + + return [] diff --git a/backtracking/hamiltonian_cycle.py b/backtracking/hamiltonian_cycle.py new file mode 100644 index 000000000000..7be1ea350d7c --- /dev/null +++ b/backtracking/hamiltonian_cycle.py @@ -0,0 +1,177 @@ +""" + A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle + through a graph that visits each node exactly once. + Determining whether such paths and cycles exist in graphs + is the 'Hamiltonian path problem', which is NP-complete. + + Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path +""" +from typing import List + + +def valid_connection( + graph: List[List[int]], next_ver: int, curr_ind: int, path: List[int] +) -> bool: + """ + Checks whether it is possible to add next into path by validating 2 statements + 1. There should be path between current and next vertex + 2. Next vertex should not be in path + If both validations succeeds we return True saying that it is possible to connect + this vertices either we return False + + Case 1:Use exact graph as in main function, with initialized values + >>> graph = [[0, 1, 0, 1, 0], + ... [1, 0, 1, 1, 1], + ... [0, 1, 0, 0, 1], + ... [1, 1, 0, 0, 1], + ... [0, 1, 1, 1, 0]] + >>> path = [0, -1, -1, -1, -1, 0] + >>> curr_ind = 1 + >>> next_ver = 1 + >>> valid_connection(graph, next_ver, curr_ind, path) + True + + Case 2: Same graph, but trying to connect to node that is already in path + >>> path = [0, 1, 2, 4, -1, 0] + >>> curr_ind = 4 + >>> next_ver = 1 + >>> valid_connection(graph, next_ver, curr_ind, path) + False + """ + + # 1. Validate that path exists between current and next vertices + if graph[path[curr_ind - 1]][next_ver] == 0: + return False + + # 2. Validate that next vertex is not already in path + return not any(vertex == next_ver for vertex in path) + + +def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) -> bool: + """ + Pseudo-Code + Base Case: + 1. Check if we visited all of vertices + 1.1 If last visited vertex has path to starting vertex return True either + return False + Recursive Step: + 2. Iterate over each vertex + Check if next vertex is valid for transiting from current vertex + 2.1 Remember next vertex as next transition + 2.2 Do recursive call and check if going to this vertex solves problem + 2.3 If next vertex leads to solution return True + 2.4 Else backtrack, delete remembered vertex + + Case 1: Use exact graph as in main function, with initialized values + >>> graph = [[0, 1, 0, 1, 0], + ... [1, 0, 1, 1, 1], + ... [0, 1, 0, 0, 1], + ... [1, 1, 0, 0, 1], + ... [0, 1, 1, 1, 0]] + >>> path = [0, -1, -1, -1, -1, 0] + >>> curr_ind = 1 + >>> util_hamilton_cycle(graph, path, curr_ind) + True + >>> print(path) + [0, 1, 2, 4, 3, 0] + + Case 2: Use exact graph as in previous case, but in the properties taken from + middle of calculation + >>> graph = [[0, 1, 0, 1, 0], + ... [1, 0, 1, 1, 1], + ... [0, 1, 0, 0, 1], + ... [1, 1, 0, 0, 1], + ... [0, 1, 1, 1, 0]] + >>> path = [0, 1, 2, -1, -1, 0] + >>> curr_ind = 3 + >>> util_hamilton_cycle(graph, path, curr_ind) + True + >>> print(path) + [0, 1, 2, 4, 3, 0] + """ + + # Base Case + if curr_ind == len(graph): + # return whether path exists between current and starting vertices + return graph[path[curr_ind - 1]][path[0]] == 1 + + # Recursive Step + for next in range(0, len(graph)): + if valid_connection(graph, next, curr_ind, path): + # Insert current vertex into path as next transition + path[curr_ind] = next + # Validate created path + if util_hamilton_cycle(graph, path, curr_ind + 1): + return True + # Backtrack + path[curr_ind] = -1 + return False + + +def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]: + r""" + Wrapper function to call subroutine called util_hamilton_cycle, + which will either return array of vertices indicating hamiltonian cycle + or an empty list indicating that hamiltonian cycle was not found. + Case 1: + Following graph consists of 5 edges. + If we look closely, we can see that there are multiple Hamiltonian cycles. + For example one result is when we iterate like: + (0)->(1)->(2)->(4)->(3)->(0) + + (0)---(1)---(2) + | / \ | + | / \ | + | / \ | + |/ \| + (3)---------(4) + >>> graph = [[0, 1, 0, 1, 0], + ... [1, 0, 1, 1, 1], + ... [0, 1, 0, 0, 1], + ... [1, 1, 0, 0, 1], + ... [0, 1, 1, 1, 0]] + >>> hamilton_cycle(graph) + [0, 1, 2, 4, 3, 0] + + Case 2: + Same Graph as it was in Case 1, changed starting index from default to 3 + + (0)---(1)---(2) + | / \ | + | / \ | + | / \ | + |/ \| + (3)---------(4) + >>> graph = [[0, 1, 0, 1, 0], + ... [1, 0, 1, 1, 1], + ... [0, 1, 0, 0, 1], + ... [1, 1, 0, 0, 1], + ... [0, 1, 1, 1, 0]] + >>> hamilton_cycle(graph, 3) + [3, 0, 1, 2, 4, 3] + + Case 3: + Following Graph is exactly what it was before, but edge 3-4 is removed. + Result is that there is no Hamiltonian Cycle anymore. + + (0)---(1)---(2) + | / \ | + | / \ | + | / \ | + |/ \| + (3) (4) + >>> graph = [[0, 1, 0, 1, 0], + ... [1, 0, 1, 1, 1], + ... [0, 1, 0, 0, 1], + ... [1, 1, 0, 0, 0], + ... [0, 1, 1, 0, 0]] + >>> hamilton_cycle(graph,4) + [] + """ + + # Initialize path with -1, indicating that we have not visited them yet + path = [-1] * (len(graph) + 1) + # initialize start and end of path with starting index + path[0] = path[-1] = start_index + # evaluate and if we find answer return path either return empty array + return path if util_hamilton_cycle(graph, path, 1) else [] diff --git a/backtracking/knight_tour.py b/backtracking/knight_tour.py new file mode 100644 index 000000000000..8e6613e07d8b --- /dev/null +++ b/backtracking/knight_tour.py @@ -0,0 +1,100 @@ +# Knight Tour Intro: https://www.youtube.com/watch?v=ab_dY3dZFHM + +from typing import List, Tuple + + +def get_valid_pos(position: Tuple[int, int], n: int) -> List[Tuple[int, int]]: + """ + Find all the valid positions a knight can move to from the current position. + + >>> get_valid_pos((1, 3), 4) + [(2, 1), (0, 1), (3, 2)] + """ + + y, x = position + positions = [ + (y + 1, x + 2), + (y - 1, x + 2), + (y + 1, x - 2), + (y - 1, x - 2), + (y + 2, x + 1), + (y + 2, x - 1), + (y - 2, x + 1), + (y - 2, x - 1), + ] + permissible_positions = [] + + for position in positions: + y_test, x_test = position + if 0 <= y_test < n and 0 <= x_test < n: + permissible_positions.append(position) + + return permissible_positions + + +def is_complete(board: List[List[int]]) -> bool: + """ + Check if the board (matrix) has been completely filled with non-zero values. + + >>> is_complete([[1]]) + True + + >>> is_complete([[1, 2], [3, 0]]) + False + """ + + return not any(elem == 0 for row in board for elem in row) + + +def open_knight_tour_helper( + board: List[List[int]], pos: Tuple[int, int], curr: int +) -> bool: + """ + Helper function to solve knight tour problem. + """ + + if is_complete(board): + return True + + for position in get_valid_pos(pos, len(board)): + y, x = position + + if board[y][x] == 0: + board[y][x] = curr + 1 + if open_knight_tour_helper(board, position, curr + 1): + return True + board[y][x] = 0 + + return False + + +def open_knight_tour(n: int) -> List[List[int]]: + """ + Find the solution for the knight tour problem for a board of size n. Raises + ValueError if the tour cannot be performed for the given size. + + >>> open_knight_tour(1) + [[1]] + + >>> open_knight_tour(2) + Traceback (most recent call last): + ... + ValueError: Open Kight Tour cannot be performed on a board of size 2 + """ + + board = [[0 for i in range(n)] for j in range(n)] + + for i in range(n): + for j in range(n): + board[i][j] = 1 + if open_knight_tour_helper(board, (i, j), 1): + return board + board[i][j] = 0 + + raise ValueError(f"Open Kight Tour cannot be performed on a board of size {n}") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/backtracking/minimax.py b/backtracking/minimax.py new file mode 100644 index 000000000000..dda29b47d6cc --- /dev/null +++ b/backtracking/minimax.py @@ -0,0 +1,69 @@ +""" +Minimax helps to achieve maximum score in a game by checking all possible moves +depth is current depth in game tree. + +nodeIndex is index of current node in scores[]. +if move is of maximizer return true else false +leaves of game tree is stored in scores[] +height is maximum height of Game tree +""" +import math +from typing import List + + +def minimax( + depth: int, node_index: int, is_max: bool, scores: List[int], height: float +) -> int: + """ + >>> import math + >>> scores = [90, 23, 6, 33, 21, 65, 123, 34423] + >>> height = math.log(len(scores), 2) + >>> minimax(0, 0, True, scores, height) + 65 + >>> minimax(-1, 0, True, scores, height) + Traceback (most recent call last): + ... + ValueError: Depth cannot be less than 0 + >>> minimax(0, 0, True, [], 2) + Traceback (most recent call last): + ... + ValueError: Scores cannot be empty + >>> scores = [3, 5, 2, 9, 12, 5, 23, 23] + >>> height = math.log(len(scores), 2) + >>> minimax(0, 0, True, scores, height) + 12 + """ + + if depth < 0: + raise ValueError("Depth cannot be less than 0") + + if len(scores) == 0: + raise ValueError("Scores cannot be empty") + + if depth == height: + return scores[node_index] + + if is_max: + return max( + minimax(depth + 1, node_index * 2, False, scores, height), + minimax(depth + 1, node_index * 2 + 1, False, scores, height), + ) + + return min( + minimax(depth + 1, node_index * 2, True, scores, height), + minimax(depth + 1, node_index * 2 + 1, True, scores, height), + ) + + +def main() -> None: + scores = [90, 23, 6, 33, 21, 65, 123, 34423] + height = math.log(len(scores), 2) + print("Optimal value : ", end="") + print(minimax(0, 0, True, scores, height)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/backtracking/n_queens.py b/backtracking/n_queens.py new file mode 100644 index 000000000000..29b8d819acf3 --- /dev/null +++ b/backtracking/n_queens.py @@ -0,0 +1,90 @@ +""" + + The nqueens problem is of placing N queens on a N * N + chess board such that no queen can attack any other queens placed + on that chess board. + This means that one queen cannot have any other queen on its horizontal, vertical and + diagonal lines. + +""" +from typing import List + +solution = [] + + +def isSafe(board: List[List[int]], row: int, column: int) -> bool: + """ + This function returns a boolean value True if it is safe to place a queen there + considering the current state of the board. + + Parameters : + board(2D matrix) : board + row ,column : coordinates of the cell on a board + + Returns : + Boolean Value + + """ + for i in range(len(board)): + if board[row][i] == 1: + return False + for i in range(len(board)): + if board[i][column] == 1: + return False + for i, j in zip(range(row, -1, -1), range(column, -1, -1)): + if board[i][j] == 1: + return False + for i, j in zip(range(row, -1, -1), range(column, len(board))): + if board[i][j] == 1: + return False + return True + + +def solve(board: List[List[int]], row: int) -> bool: + """ + It creates a state space tree and calls the safe function until it receives a + False Boolean and terminates that branch and backtracks to the next + possible solution branch. + """ + if row >= len(board): + """ + If the row number exceeds N we have board with a successful combination + and that combination is appended to the solution list and the board is printed. + + """ + solution.append(board) + printboard(board) + print() + return True + for i in range(len(board)): + """ + For every row it iterates through each column to check if it is feasible to + place a queen there. + If all the combinations for that particular branch are successful the board is + reinitialized for the next possible combination. + """ + if isSafe(board, row, i): + board[row][i] = 1 + solve(board, row + 1) + board[row][i] = 0 + return False + + +def printboard(board: List[List[int]]) -> None: + """ + Prints the boards that have a successful combination. + """ + for i in range(len(board)): + for j in range(len(board)): + if board[i][j] == 1: + print("Q", end=" ") + else: + print(".", end=" ") + print() + + +# n=int(input("The no. of queens")) +n = 8 +board = [[0 for i in range(n)] for j in range(n)] +solve(board, 0) +print("The total no. of solutions are :", len(solution)) diff --git a/backtracking/n_queens_math.py b/backtracking/n_queens_math.py new file mode 100644 index 000000000000..a8651c5c362e --- /dev/null +++ b/backtracking/n_queens_math.py @@ -0,0 +1,158 @@ +r""" +Problem: + +The n queens problem is of placing N queens on a N * N chess board such that no queen +can attack any other queens placed on that chess board. This means that one queen +cannot have any other queen on its horizontal, vertical and diagonal lines. + +Solution: + +To solve this problem we will use simple math. First we know the queen can move in all +the possible ways, we can simplify it in this: vertical, horizontal, diagonal left and + diagonal right. + +We can visualize it like this: + +left diagonal = \ +right diagonal = / + +On a chessboard vertical movement could be the rows and horizontal movement could be +the columns. + +In programming we can use an array, and in this array each index could be the rows and +each value in the array could be the column. For example: + + . Q . . We have this chessboard with one queen in each column and each queen + . . . Q can't attack to each other. + Q . . . The array for this example would look like this: [1, 3, 0, 2] + . . Q . + +So if we use an array and we verify that each value in the array is different to each +other we know that at least the queens can't attack each other in horizontal and +vertical. + +At this point we have that halfway completed and we will treat the chessboard as a +Cartesian plane. Hereinafter we are going to remember basic math, so in the school we +learned this formula: + + Slope of a line: + + y2 - y1 + m = ---------- + x2 - x1 + +This formula allow us to get the slope. For the angles 45º (right diagonal) and 135º +(left diagonal) this formula gives us m = 1, and m = -1 respectively. + +See:: +https://www.enotes.com/homework-help/write-equation-line-that-hits-origin-45-degree-1474860 + +Then we have this another formula: + +Slope intercept: + +y = mx + b + +b is where the line crosses the Y axis (to get more information see: +https://www.mathsisfun.com/y_intercept.html), if we change the formula to solve for b +we would have: + +y - mx = b + +And like we already have the m values for the angles 45º and 135º, this formula would +look like this: + +45º: y - (1)x = b +45º: y - x = b + +135º: y - (-1)x = b +135º: y + x = b + +y = row +x = column + +Applying this two formulas we can check if a queen in some position is being attacked +for another one or vice versa. + +""" +from typing import List + + +def depth_first_search( + possible_board: List[int], + diagonal_right_collisions: List[int], + diagonal_left_collisions: List[int], + boards: List[List[str]], + n: int, +) -> None: + """ + >>> boards = [] + >>> depth_first_search([], [], [], boards, 4) + >>> for board in boards: + ... print(board) + ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] + ['. . Q . ', 'Q . . . ', '. . . Q ', '. Q . . '] + """ + + # Get next row in the current board (possible_board) to fill it with a queen + row = len(possible_board) + + # If row is equal to the size of the board it means there are a queen in each row in + # the current board (possible_board) + if row == n: + # We convert the variable possible_board that looks like this: [1, 3, 0, 2] to + # this: ['. Q . . ', '. . . Q ', 'Q . . . ', '. . Q . '] + boards.append([". " * i + "Q " + ". " * (n - 1 - i) for i in possible_board]) + return + + # We iterate each column in the row to find all possible results in each row + for col in range(n): + + # We apply that we learned previously. First we check that in the current board + # (possible_board) there are not other same value because if there is it means + # that there are a collision in vertical. Then we apply the two formulas we + # learned before: + # + # 45º: y - x = b or 45: row - col = b + # 135º: y + x = b or row + col = b. + # + # And we verify if the results of this two formulas not exist in their variables + # respectively. (diagonal_right_collisions, diagonal_left_collisions) + # + # If any or these are True it means there is a collision so we continue to the + # next value in the for loop. + if ( + col in possible_board + or row - col in diagonal_right_collisions + or row + col in diagonal_left_collisions + ): + continue + + # If it is False we call dfs function again and we update the inputs + depth_first_search( + possible_board + [col], + diagonal_right_collisions + [row - col], + diagonal_left_collisions + [row + col], + boards, + n, + ) + + +def n_queens_solution(n: int) -> None: + boards: List[List[str]] = [] + depth_first_search([], [], [], boards, n) + + # Print all the boards + for board in boards: + for column in board: + print(column) + print("") + + print(len(boards), "solutions were found.") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + n_queens_solution(4) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py new file mode 100644 index 000000000000..cd2a8f41daa8 --- /dev/null +++ b/backtracking/rat_in_maze.py @@ -0,0 +1,118 @@ +from typing import List + + +def solve_maze(maze: List[List[int]]) -> bool: + """ + This method solves the "rat in maze" problem. + In this problem we have some n by n matrix, a start point and an end point. + We want to go from the start to the end. In this matrix zeroes represent walls + and ones paths we can use. + Parameters : + maze(2D matrix) : maze + Returns: + Return: True if the maze has a solution or False if it does not. + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [1, 0, 1, 0, 1], + ... [0, 0, 1, 0, 0], + ... [1, 0, 0, 1, 0]] + >>> solve_maze(maze) + [1, 0, 0, 0, 0] + [1, 1, 1, 1, 0] + [0, 0, 0, 1, 0] + [0, 0, 0, 1, 1] + [0, 0, 0, 0, 1] + True + + >>> maze = [[0, 1, 0, 1, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 1], + ... [0, 0, 0, 0, 0], + ... [0, 0, 0, 0, 0]] + >>> solve_maze(maze) + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 0, 0, 0, 0] + [1, 1, 1, 1, 1] + True + + >>> maze = [[0, 0, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze) + [1, 1, 1] + [0, 0, 1] + [0, 0, 1] + True + + >>> maze = [[0, 1, 0], + ... [0, 1, 0], + ... [1, 0, 0]] + >>> solve_maze(maze) + No solution exists! + False + + >>> maze = [[0, 1], + ... [1, 0]] + >>> solve_maze(maze) + No solution exists! + False + """ + size = len(maze) + # We need to create solution object to save path. + solutions = [[0 for _ in range(size)] for _ in range(size)] + solved = run_maze(maze, 0, 0, solutions) + if solved: + print("\n".join(str(row) for row in solutions)) + else: + print("No solution exists!") + return solved + + +def run_maze(maze: List[List[int]], i: int, j: int, solutions: List[List[int]]) -> bool: + """ + This method is recursive starting from (i, j) and going in one of four directions: + up, down, left, right. + If a path is found to destination it returns True otherwise it returns False. + Parameters: + maze(2D matrix) : maze + i, j : coordinates of matrix + solutions(2D matrix) : solutions + Returns: + Boolean if path is found True, Otherwise False. + """ + size = len(maze) + # Final check point. + if i == j == (size - 1): + solutions[i][j] = 1 + return True + + lower_flag = (not (i < 0)) and (not (j < 0)) # Check lower bounds + upper_flag = (i < size) and (j < size) # Check upper bounds + + if lower_flag and upper_flag: + # check for already visited and block points. + block_flag = (not (solutions[i][j])) and (not (maze[i][j])) + if block_flag: + # check visited + solutions[i][j] = 1 + + # check for directions + if ( + run_maze(maze, i + 1, j, solutions) + or run_maze(maze, i, j + 1, solutions) + or run_maze(maze, i - 1, j, solutions) + or run_maze(maze, i, j - 1, solutions) + ): + return True + + solutions[i][j] = 0 + return False + return False + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/backtracking/sudoku.py b/backtracking/sudoku.py new file mode 100644 index 000000000000..3bfaddd6e56f --- /dev/null +++ b/backtracking/sudoku.py @@ -0,0 +1,158 @@ +""" +Given a partially filled 9×9 2D array, the objective is to fill a 9×9 +square grid with digits numbered 1 to 9, so that every row, column, and +and each of the nine 3×3 sub-grids contains all of the digits. + +This can be solved using Backtracking and is similar to n-queens. +We check to see if a cell is safe or not and recursively call the +function on the next column to see if it returns True. if yes, we +have solved the puzzle. else, we backtrack and place another number +in that cell and repeat this process. +""" +from typing import List, Optional, Tuple + +Matrix = List[List[int]] + +# assigning initial values to the grid +initial_grid: Matrix = [ + [3, 0, 6, 5, 0, 8, 4, 0, 0], + [5, 2, 0, 0, 0, 0, 0, 0, 0], + [0, 8, 7, 0, 0, 0, 0, 3, 1], + [0, 0, 3, 0, 1, 0, 0, 8, 0], + [9, 0, 0, 8, 6, 3, 0, 0, 5], + [0, 5, 0, 0, 9, 0, 6, 0, 0], + [1, 3, 0, 0, 0, 0, 2, 5, 0], + [0, 0, 0, 0, 0, 0, 0, 7, 4], + [0, 0, 5, 2, 0, 6, 3, 0, 0], +] + +# a grid with no solution +no_solution: Matrix = [ + [5, 0, 6, 5, 0, 8, 4, 0, 3], + [5, 2, 0, 0, 0, 0, 0, 0, 2], + [1, 8, 7, 0, 0, 0, 0, 3, 1], + [0, 0, 3, 0, 1, 0, 0, 8, 0], + [9, 0, 0, 8, 6, 3, 0, 0, 5], + [0, 5, 0, 0, 9, 0, 6, 0, 0], + [1, 3, 0, 0, 0, 0, 2, 5, 0], + [0, 0, 0, 0, 0, 0, 0, 7, 4], + [0, 0, 5, 2, 0, 6, 3, 0, 0], +] + + +def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool: + """ + This function checks the grid to see if each row, + column, and the 3x3 subgrids contain the digit 'n'. + It returns False if it is not 'safe' (a duplicate digit + is found) else returns True if it is 'safe' + """ + for i in range(9): + if grid[row][i] == n or grid[i][column] == n: + return False + + for i in range(3): + for j in range(3): + if grid[(row - row % 3) + i][(column - column % 3) + j] == n: + return False + + return True + + +def is_completed(grid: Matrix) -> bool: + """ + This function checks if the puzzle is completed or not. + it is completed when all the cells are assigned with a non-zero number. + + >>> is_completed([[0]]) + False + >>> is_completed([[1]]) + True + >>> is_completed([[1, 2], [0, 4]]) + False + >>> is_completed([[1, 2], [3, 4]]) + True + >>> is_completed(initial_grid) + False + >>> is_completed(no_solution) + False + """ + return all(all(cell != 0 for cell in row) for row in grid) + + +def find_empty_location(grid: Matrix) -> Optional[Tuple[int, int]]: + """ + This function finds an empty location so that we can assign a number + for that particular row and column. + """ + for i in range(9): + for j in range(9): + if grid[i][j] == 0: + return i, j + return None + + +def sudoku(grid: Matrix) -> Optional[Matrix]: + """ + Takes a partially filled-in grid and attempts to assign values to + all unassigned locations in such a way to meet the requirements + for Sudoku solution (non-duplication across rows, columns, and boxes) + + >>> sudoku(initial_grid) # doctest: +NORMALIZE_WHITESPACE + [[3, 1, 6, 5, 7, 8, 4, 9, 2], + [5, 2, 9, 1, 3, 4, 7, 6, 8], + [4, 8, 7, 6, 2, 9, 5, 3, 1], + [2, 6, 3, 4, 1, 5, 9, 8, 7], + [9, 7, 4, 8, 6, 3, 1, 2, 5], + [8, 5, 1, 7, 9, 2, 6, 4, 3], + [1, 3, 8, 9, 4, 7, 2, 5, 6], + [6, 9, 2, 3, 5, 1, 8, 7, 4], + [7, 4, 5, 2, 8, 6, 3, 1, 9]] + >>> sudoku(no_solution) is None + True + """ + + if is_completed(grid): + return grid + + location = find_empty_location(grid) + if location is not None: + row, column = location + else: + # If the location is ``None``, then the grid is solved. + return grid + + for digit in range(1, 10): + if is_safe(grid, row, column, digit): + grid[row][column] = digit + + if sudoku(grid) is not None: + return grid + + grid[row][column] = 0 + + return None + + +def print_solution(grid: Matrix) -> None: + """ + A function to print the solution in the form + of a 9x9 grid + """ + for row in grid: + for cell in row: + print(cell, end=" ") + print() + + +if __name__ == "__main__": + # make a copy of grid so that you can compare with the unmodified grid + for example_grid in (initial_grid, no_solution): + print("\nExample grid:\n" + "=" * 20) + print_solution(example_grid) + print("\nExample grid solution:") + solution = sudoku(example_grid) + if solution is not None: + print_solution(solution) + else: + print("Cannot find a solution.") diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py new file mode 100644 index 000000000000..f695b8f7a80e --- /dev/null +++ b/backtracking/sum_of_subsets.py @@ -0,0 +1,65 @@ +""" + The sum-of-subsetsproblem states that a set of non-negative integers, and a + value M, determine all possible subsets of the given set whose summation sum + equal to given M. + + Summation of the chosen numbers must be equal to given number M and one number + can be used only once. +""" +from typing import List + + +def generate_sum_of_subsets_soln(nums: List[int], max_sum: int) -> List[List[int]]: + result: List[List[int]] = [] + path: List[int] = [] + num_index = 0 + remaining_nums_sum = sum(nums) + create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum) + return result + + +def create_state_space_tree( + nums: List[int], + max_sum: int, + num_index: int, + path: List[int], + result: List[List[int]], + remaining_nums_sum: int, +) -> None: + """ + Creates a state space tree to iterate through each branch using DFS. + It terminates the branching of a node when any of the two conditions + given below satisfy. + This algorithm follows depth-fist-search and backtracks when the node is not + branchable. + + """ + if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum: + return + if sum(path) == max_sum: + result.append(path) + return + for num_index in range(num_index, len(nums)): + create_state_space_tree( + nums, + max_sum, + num_index + 1, + path + [nums[num_index]], + result, + remaining_nums_sum - nums[num_index], + ) + + +""" +remove the comment to take an input from the user + +print("Enter the elements") +nums = list(map(int, input().split())) +print("Enter max_sum sum") +max_sum = int(input()) + +""" +nums = [3, 34, 4, 12, 5, 2] +max_sum = 9 +result = generate_sum_of_subsets_soln(nums, max_sum) +print(*result) diff --git a/binary_tree/basic_binary_tree.py b/binary_tree/basic_binary_tree.py deleted file mode 100644 index 7c6240fb4dd4..000000000000 --- a/binary_tree/basic_binary_tree.py +++ /dev/null @@ -1,63 +0,0 @@ -class Node: # This is the Class Node with constructor that contains data variable to type data and left,right pointers. - def __init__(self, data): - self.data = data - self.left = None - self.right = None - -def display(tree): #In Order traversal of the tree - - if tree is None: - return - - if tree.left is not None: - display(tree.left) - - print(tree.data) - - if tree.right is not None: - display(tree.right) - - return - -def depth_of_tree(tree): #This is the recursive function to find the depth of binary tree. - if tree is None: - return 0 - else: - depth_l_tree = depth_of_tree(tree.left) - depth_r_tree = depth_of_tree(tree.right) - if depth_l_tree > depth_r_tree: - return 1 + depth_l_tree - else: - return 1 + depth_r_tree - - -def is_full_binary_tree(tree): # This functions returns that is it full binary tree or not? - if tree is None: - return True - if (tree.left is None) and (tree.right is None): - return True - if (tree.left is not None) and (tree.right is not None): - return (is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right)) - else: - return False - - -def main(): # Main func for testing. - tree = Node(1) - tree.left = Node(2) - tree.right = Node(3) - tree.left.left = Node(4) - tree.left.right = Node(5) - tree.left.right.left = Node(6) - tree.right.left = Node(7) - tree.right.left.left = Node(8) - tree.right.left.left.right = Node(9) - - print(is_full_binary_tree(tree)) - print(depth_of_tree(tree)) - print("Tree is: ") - display(tree) - - -if __name__ == '__main__': - main() diff --git a/bit_manipulation/README.md b/bit_manipulation/README.md new file mode 100644 index 000000000000..2ef1661524f2 --- /dev/null +++ b/bit_manipulation/README.md @@ -0,0 +1,7 @@ +https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations +https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations +https://docs.python.org/3/library/stdtypes.html#bitwise-operations-on-integer-types + +https://wiki.python.org/moin/BitManipulation +https://wiki.python.org/moin/BitwiseOperators +https://www.tutorialspoint.com/python3/bitwise_operators_example.htm diff --git a/bit_manipulation/__init__.py b/bit_manipulation/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/bit_manipulation/binary_and_operator.py b/bit_manipulation/binary_and_operator.py new file mode 100644 index 000000000000..191ff8eb44a4 --- /dev/null +++ b/bit_manipulation/binary_and_operator.py @@ -0,0 +1,52 @@ +# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm + + +def binary_and(a: int, b: int) -> str: + """ + Take in 2 integers, convert them to binary, + return a binary number that is the + result of a binary and operation on the integers provided. + + >>> binary_and(25, 32) + '0b000000' + >>> binary_and(37, 50) + '0b100000' + >>> binary_and(21, 30) + '0b10100' + >>> binary_and(58, 73) + '0b0001000' + >>> binary_and(0, 255) + '0b00000000' + >>> binary_and(256, 256) + '0b100000000' + >>> binary_and(0, -1) + Traceback (most recent call last): + ... + ValueError: the value of both input must be positive + >>> binary_and(0, 1.1) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> binary_and("0", "1") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0 or b < 0: + raise ValueError("the value of both input must be positive") + + a_binary = str(bin(a))[2:] # remove the leading "0b" + b_binary = str(bin(b))[2:] # remove the leading "0b" + + max_len = max(len(a_binary), len(b_binary)) + + return "0b" + "".join( + str(int(char_a == "1" and char_b == "1")) + for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len)) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/bit_manipulation/binary_count_setbits.py b/bit_manipulation/binary_count_setbits.py new file mode 100644 index 000000000000..3c92694533aa --- /dev/null +++ b/bit_manipulation/binary_count_setbits.py @@ -0,0 +1,41 @@ +def binary_count_setbits(a: int) -> int: + """ + Take in 1 integer, return a number that is + the number of 1's in binary representation of that number. + + >>> binary_count_setbits(25) + 3 + >>> binary_count_setbits(36) + 2 + >>> binary_count_setbits(16) + 1 + >>> binary_count_setbits(58) + 4 + >>> binary_count_setbits(4294967295) + 32 + >>> binary_count_setbits(0) + 0 + >>> binary_count_setbits(-10) + Traceback (most recent call last): + ... + ValueError: Input value must be a positive integer + >>> binary_count_setbits(0.8) + Traceback (most recent call last): + ... + TypeError: Input value must be a 'int' type + >>> binary_count_setbits("0") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0: + raise ValueError("Input value must be a positive integer") + elif isinstance(a, float): + raise TypeError("Input value must be a 'int' type") + return bin(a).count("1") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/bit_manipulation/binary_count_trailing_zeros.py b/bit_manipulation/binary_count_trailing_zeros.py new file mode 100644 index 000000000000..f401c4ab9266 --- /dev/null +++ b/bit_manipulation/binary_count_trailing_zeros.py @@ -0,0 +1,44 @@ +from math import log2 + + +def binary_count_trailing_zeros(a: int) -> int: + """ + Take in 1 integer, return a number that is + the number of trailing zeros in binary representation of that number. + + >>> binary_count_trailing_zeros(25) + 0 + >>> binary_count_trailing_zeros(36) + 2 + >>> binary_count_trailing_zeros(16) + 4 + >>> binary_count_trailing_zeros(58) + 1 + >>> binary_count_trailing_zeros(4294967296) + 32 + >>> binary_count_trailing_zeros(0) + 0 + >>> binary_count_trailing_zeros(-10) + Traceback (most recent call last): + ... + ValueError: Input value must be a positive integer + >>> binary_count_trailing_zeros(0.8) + Traceback (most recent call last): + ... + TypeError: Input value must be a 'int' type + >>> binary_count_trailing_zeros("0") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0: + raise ValueError("Input value must be a positive integer") + elif isinstance(a, float): + raise TypeError("Input value must be a 'int' type") + return 0 if (a == 0) else int(log2(a & -a)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/bit_manipulation/binary_or_operator.py b/bit_manipulation/binary_or_operator.py new file mode 100644 index 000000000000..dabf5bcb09fd --- /dev/null +++ b/bit_manipulation/binary_or_operator.py @@ -0,0 +1,48 @@ +# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm + + +def binary_or(a: int, b: int) -> str: + """ + Take in 2 integers, convert them to binary, and return a binary number that is the + result of a binary or operation on the integers provided. + + >>> binary_or(25, 32) + '0b111001' + >>> binary_or(37, 50) + '0b110111' + >>> binary_or(21, 30) + '0b11111' + >>> binary_or(58, 73) + '0b1111011' + >>> binary_or(0, 255) + '0b11111111' + >>> binary_or(0, 256) + '0b100000000' + >>> binary_or(0, -1) + Traceback (most recent call last): + ... + ValueError: the value of both input must be positive + >>> binary_or(0, 1.1) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> binary_or("0", "1") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0 or b < 0: + raise ValueError("the value of both input must be positive") + a_binary = str(bin(a))[2:] # remove the leading "0b" + b_binary = str(bin(b))[2:] + max_len = max(len(a_binary), len(b_binary)) + return "0b" + "".join( + str(int("1" in (char_a, char_b))) + for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len)) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/bit_manipulation/binary_xor_operator.py b/bit_manipulation/binary_xor_operator.py new file mode 100644 index 000000000000..6f8962192ad8 --- /dev/null +++ b/bit_manipulation/binary_xor_operator.py @@ -0,0 +1,52 @@ +# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm + + +def binary_xor(a: int, b: int) -> str: + """ + Take in 2 integers, convert them to binary, + return a binary number that is the + result of a binary xor operation on the integers provided. + + >>> binary_xor(25, 32) + '0b111001' + >>> binary_xor(37, 50) + '0b010111' + >>> binary_xor(21, 30) + '0b01011' + >>> binary_xor(58, 73) + '0b1110011' + >>> binary_xor(0, 255) + '0b11111111' + >>> binary_xor(256, 256) + '0b000000000' + >>> binary_xor(0, -1) + Traceback (most recent call last): + ... + ValueError: the value of both input must be positive + >>> binary_xor(0, 1.1) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> binary_xor("0", "1") + Traceback (most recent call last): + ... + TypeError: '<' not supported between instances of 'str' and 'int' + """ + if a < 0 or b < 0: + raise ValueError("the value of both input must be positive") + + a_binary = str(bin(a))[2:] # remove the leading "0b" + b_binary = str(bin(b))[2:] # remove the leading "0b" + + max_len = max(len(a_binary), len(b_binary)) + + return "0b" + "".join( + str(int(char_a != char_b)) + for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len)) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/bit_manipulation/single_bit_manipulation_operations.py b/bit_manipulation/single_bit_manipulation_operations.py new file mode 100644 index 000000000000..e4a54028d9ee --- /dev/null +++ b/bit_manipulation/single_bit_manipulation_operations.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +"""Provide the functionality to manipulate a single bit.""" + + +def set_bit(number: int, position: int) -> int: + """ + Set the bit at position to 1. + + Details: perform bitwise or for given number and X. + Where X is a number with all the bits – zeroes and bit on given + position – one. + + >>> set_bit(0b1101, 1) # 0b1111 + 15 + >>> set_bit(0b0, 5) # 0b100000 + 32 + >>> set_bit(0b1111, 1) # 0b1111 + 15 + """ + return number | (1 << position) + + +def clear_bit(number: int, position: int) -> int: + """ + Set the bit at position to 0. + + Details: perform bitwise and for given number and X. + Where X is a number with all the bits – ones and bit on given + position – zero. + + >>> clear_bit(0b10010, 1) # 0b10000 + 16 + >>> clear_bit(0b0, 5) # 0b0 + 0 + """ + return number & ~(1 << position) + + +def flip_bit(number: int, position: int) -> int: + """ + Flip the bit at position. + + Details: perform bitwise xor for given number and X. + Where X is a number with all the bits – zeroes and bit on given + position – one. + + >>> flip_bit(0b101, 1) # 0b111 + 7 + >>> flip_bit(0b101, 0) # 0b100 + 4 + """ + return number ^ (1 << position) + + +def is_bit_set(number: int, position: int) -> bool: + """ + Is the bit at position set? + + Details: Shift the bit at position to be the first (smallest) bit. + Then check if the first bit is set by anding the shifted number with 1. + + >>> is_bit_set(0b1010, 0) + False + >>> is_bit_set(0b1010, 1) + True + >>> is_bit_set(0b1010, 2) + False + >>> is_bit_set(0b1010, 3) + True + >>> is_bit_set(0b0, 17) + False + """ + return ((number >> position) & 1) == 1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/blockchain/__init__.py b/blockchain/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/blockchain/chinese_remainder_theorem.py b/blockchain/chinese_remainder_theorem.py new file mode 100644 index 000000000000..b50147ac1215 --- /dev/null +++ b/blockchain/chinese_remainder_theorem.py @@ -0,0 +1,94 @@ +""" +Chinese Remainder Theorem: +GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor ) + +If GCD(a,b) = 1, then for any remainder ra modulo a and any remainder rb modulo b +there exists integer n, such that n = ra (mod a) and n = ra(mod b). If n1 and n2 are +two such integers, then n1=n2(mod ab) + +Algorithm : + +1. Use extended euclid algorithm to find x,y such that a*x + b*y = 1 +2. Take n = ra*by + rb*ax +""" +from typing import Tuple + + +# Extended Euclid +def extended_euclid(a: int, b: int) -> Tuple[int, int]: + """ + >>> extended_euclid(10, 6) + (-1, 2) + + >>> extended_euclid(7, 5) + (-2, 3) + + """ + if b == 0: + return (1, 0) + (x, y) = extended_euclid(b, a % b) + k = a // b + return (y, x - k * y) + + +# Uses ExtendedEuclid to find inverses +def chinese_remainder_theorem(n1: int, r1: int, n2: int, r2: int) -> int: + """ + >>> chinese_remainder_theorem(5,1,7,3) + 31 + + Explanation : 31 is the smallest number such that + (i) When we divide it by 5, we get remainder 1 + (ii) When we divide it by 7, we get remainder 3 + + >>> chinese_remainder_theorem(6,1,4,3) + 14 + + """ + (x, y) = extended_euclid(n1, n2) + m = n1 * n2 + n = r2 * x * n1 + r1 * y * n2 + return (n % m + m) % m + + +# ----------SAME SOLUTION USING InvertModulo instead ExtendedEuclid---------------- + +# This function find the inverses of a i.e., a^(-1) +def invert_modulo(a: int, n: int) -> int: + """ + >>> invert_modulo(2, 5) + 3 + + >>> invert_modulo(8,7) + 1 + + """ + (b, x) = extended_euclid(a, n) + if b < 0: + b = (b % n + n) % n + return b + + +# Same a above using InvertingModulo +def chinese_remainder_theorem2(n1: int, r1: int, n2: int, r2: int) -> int: + """ + >>> chinese_remainder_theorem2(5,1,7,3) + 31 + + >>> chinese_remainder_theorem2(6,1,4,3) + 14 + + """ + x, y = invert_modulo(n1, n2), invert_modulo(n2, n1) + m = n1 * n2 + n = r2 * x * n1 + r1 * y * n2 + return (n % m + m) % m + + +if __name__ == "__main__": + from doctest import testmod + + testmod(name="chinese_remainder_theorem", verbose=True) + testmod(name="chinese_remainder_theorem2", verbose=True) + testmod(name="invert_modulo", verbose=True) + testmod(name="extended_euclid", verbose=True) diff --git a/blockchain/diophantine_equation.py b/blockchain/diophantine_equation.py new file mode 100644 index 000000000000..7df674cb1438 --- /dev/null +++ b/blockchain/diophantine_equation.py @@ -0,0 +1,131 @@ +from typing import Tuple + + +def diophantine(a: int, b: int, c: int) -> Tuple[float, float]: + """ + Diophantine Equation : Given integers a,b,c ( at least one of a and b != 0), the + diophantine equation a*x + b*y = c has a solution (where x and y are integers) + iff gcd(a,b) divides c. + + GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor ) + + >>> diophantine(10,6,14) + (-7.0, 14.0) + + >>> diophantine(391,299,-69) + (9.0, -12.0) + + But above equation has one more solution i.e., x = -4, y = 5. + That's why we need diophantine all solution function. + + """ + + assert ( + c % greatest_common_divisor(a, b) == 0 + ) # greatest_common_divisor(a,b) function implemented below + (d, x, y) = extended_gcd(a, b) # extended_gcd(a,b) function implemented below + r = c / d + return (r * x, r * y) + + +def diophantine_all_soln(a: int, b: int, c: int, n: int = 2) -> None: + """ + Lemma : if n|ab and gcd(a,n) = 1, then n|b. + + Finding All solutions of Diophantine Equations: + + Theorem : Let gcd(a,b) = d, a = d*p, b = d*q. If (x0,y0) is a solution of + Diophantine Equation a*x + b*y = c. a*x0 + b*y0 = c, then all the + solutions have the form a(x0 + t*q) + b(y0 - t*p) = c, + where t is an arbitrary integer. + + n is the number of solution you want, n = 2 by default + + >>> diophantine_all_soln(10, 6, 14) + -7.0 14.0 + -4.0 9.0 + + >>> diophantine_all_soln(10, 6, 14, 4) + -7.0 14.0 + -4.0 9.0 + -1.0 4.0 + 2.0 -1.0 + + >>> diophantine_all_soln(391, 299, -69, n = 4) + 9.0 -12.0 + 22.0 -29.0 + 35.0 -46.0 + 48.0 -63.0 + + """ + (x0, y0) = diophantine(a, b, c) # Initial value + d = greatest_common_divisor(a, b) + p = a // d + q = b // d + + for i in range(n): + x = x0 + i * q + y = y0 - i * p + print(x, y) + + +def greatest_common_divisor(a: int, b: int) -> int: + """ + Euclid's Lemma : d divides a and b, if and only if d divides a-b and b + + Euclid's Algorithm + + >>> greatest_common_divisor(7,5) + 1 + + Note : In number theory, two integers a and b are said to be relatively prime, + mutually prime, or co-prime if the only positive integer (factor) that + divides both of them is 1 i.e., gcd(a,b) = 1. + + >>> greatest_common_divisor(121, 11) + 11 + + """ + if a < b: + a, b = b, a + + while a % b != 0: + a, b = b, a % b + + return b + + +def extended_gcd(a: int, b: int) -> Tuple[int, int, int]: + """ + Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers + x and y, then d = gcd(a,b) + + >>> extended_gcd(10, 6) + (2, -1, 2) + + >>> extended_gcd(7, 5) + (1, -2, 3) + + """ + assert a >= 0 and b >= 0 + + if b == 0: + d, x, y = a, 1, 0 + else: + (d, p, q) = extended_gcd(b, a % b) + x = q + y = p - q * (a // b) + + assert a % d == 0 and b % d == 0 + assert d == a * x + b * y + + return (d, x, y) + + +if __name__ == "__main__": + from doctest import testmod + + testmod(name="diophantine", verbose=True) + testmod(name="diophantine_all_soln", verbose=True) + testmod(name="extended_gcd", verbose=True) + testmod(name="greatest_common_divisor", verbose=True) diff --git a/blockchain/modular_division.py b/blockchain/modular_division.py new file mode 100644 index 000000000000..4f7f50a92ad0 --- /dev/null +++ b/blockchain/modular_division.py @@ -0,0 +1,154 @@ +from typing import Tuple + + +def modular_division(a: int, b: int, n: int) -> int: + """ + Modular Division : + An efficient algorithm for dividing b by a modulo n. + + GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor ) + + Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should + return an integer x such that 0≤x≤n−1, and b/a=x(modn) (that is, b=ax(modn)). + + Theorem: + a has a multiplicative inverse modulo n iff gcd(a,n) = 1 + + + This find x = b*a^(-1) mod n + Uses ExtendedEuclid to find the inverse of a + + >>> modular_division(4,8,5) + 2 + + >>> modular_division(3,8,5) + 1 + + >>> modular_division(4, 11, 5) + 4 + + """ + assert n > 1 and a > 0 and greatest_common_divisor(a, n) == 1 + (d, t, s) = extended_gcd(n, a) # Implemented below + x = (b * s) % n + return x + + +def invert_modulo(a: int, n: int) -> int: + """ + This function find the inverses of a i.e., a^(-1) + + >>> invert_modulo(2, 5) + 3 + + >>> invert_modulo(8,7) + 1 + + """ + (b, x) = extended_euclid(a, n) # Implemented below + if b < 0: + b = (b % n + n) % n + return b + + +# ------------------ Finding Modular division using invert_modulo ------------------- + + +def modular_division2(a: int, b: int, n: int) -> int: + """ + This function used the above inversion of a to find x = (b*a^(-1))mod n + + >>> modular_division2(4,8,5) + 2 + + >>> modular_division2(3,8,5) + 1 + + >>> modular_division2(4, 11, 5) + 4 + + """ + s = invert_modulo(a, n) + x = (b * s) % n + return x + + +def extended_gcd(a: int, b: int) -> Tuple[int, int, int]: + """ + Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x + and y, then d = gcd(a,b) + >>> extended_gcd(10, 6) + (2, -1, 2) + + >>> extended_gcd(7, 5) + (1, -2, 3) + + ** extended_gcd function is used when d = gcd(a,b) is required in output + + """ + assert a >= 0 and b >= 0 + + if b == 0: + d, x, y = a, 1, 0 + else: + (d, p, q) = extended_gcd(b, a % b) + x = q + y = p - q * (a // b) + + assert a % d == 0 and b % d == 0 + assert d == a * x + b * y + + return (d, x, y) + + +def extended_euclid(a: int, b: int) -> Tuple[int, int]: + """ + Extended Euclid + >>> extended_euclid(10, 6) + (-1, 2) + + >>> extended_euclid(7, 5) + (-2, 3) + + """ + if b == 0: + return (1, 0) + (x, y) = extended_euclid(b, a % b) + k = a // b + return (y, x - k * y) + + +def greatest_common_divisor(a: int, b: int) -> int: + """ + Euclid's Lemma : d divides a and b, if and only if d divides a-b and b + Euclid's Algorithm + + >>> greatest_common_divisor(7,5) + 1 + + Note : In number theory, two integers a and b are said to be relatively prime, + mutually prime, or co-prime if the only positive integer (factor) that divides + both of them is 1 i.e., gcd(a,b) = 1. + + >>> greatest_common_divisor(121, 11) + 11 + + """ + if a < b: + a, b = b, a + + while a % b != 0: + a, b = b, a % b + + return b + + +if __name__ == "__main__": + from doctest import testmod + + testmod(name="modular_division", verbose=True) + testmod(name="modular_division2", verbose=True) + testmod(name="invert_modulo", verbose=True) + testmod(name="extended_gcd", verbose=True) + testmod(name="extended_euclid", verbose=True) + testmod(name="greatest_common_divisor", verbose=True) diff --git a/boolean_algebra/__init__.py b/boolean_algebra/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/boolean_algebra/quine_mc_cluskey.py b/boolean_algebra/quine_mc_cluskey.py index db4d153cbfd7..19bac336f6c5 100644 --- a/boolean_algebra/quine_mc_cluskey.py +++ b/boolean_algebra/quine_mc_cluskey.py @@ -1,116 +1,168 @@ -def compare_string(string1, string2): - l1 = list(string1); l2 = list(string2) - count = 0 - for i in range(len(l1)): - if l1[i] != l2[i]: - count += 1 - l1[i] = '_' - if count > 1: - return -1 - else: - return("".join(l1)) - -def check(binary): - pi = [] - while 1: - check1 = ['$']*len(binary) - temp = [] - for i in range(len(binary)): - for j in range(i+1, len(binary)): - k=compare_string(binary[i], binary[j]) - if k != -1: - check1[i] = '*' - check1[j] = '*' - temp.append(k) - for i in range(len(binary)): - if check1[i] == '$': - pi.append(binary[i]) - if len(temp) == 0: - return pi - binary = list(set(temp)) - -def decimal_to_binary(no_of_variable, minterms): - temp = [] - s = '' - for m in minterms: - for i in range(no_of_variable): - s = str(m%2) + s - m //= 2 - temp.append(s) - s = '' - return temp - -def is_for_table(string1, string2, count): - l1 = list(string1);l2=list(string2) - count_n = 0 - for i in range(len(l1)): - if l1[i] != l2[i]: - count_n += 1 - if count_n == count: - return True - else: - return False - -def selection(chart, prime_implicants): - temp = [] - select = [0]*len(chart) - for i in range(len(chart[0])): - count = 0 - rem = -1 - for j in range(len(chart)): - if chart[j][i] == 1: - count += 1 - rem = j - if count == 1: - select[rem] = 1 - for i in range(len(select)): - if select[i] == 1: - for j in range(len(chart[0])): - if chart[i][j] == 1: - for k in range(len(chart)): - chart[k][j] = 0 - temp.append(prime_implicants[i]) - while 1: - max_n = 0; rem = -1; count_n = 0 - for i in range(len(chart)): - count_n = chart[i].count(1) - if count_n > max_n: - max_n = count_n - rem = i - - if max_n == 0: - return temp - - temp.append(prime_implicants[rem]) - - for i in range(len(chart[0])): - if chart[rem][i] == 1: - for j in range(len(chart)): - chart[j][i] = 0 - -def prime_implicant_chart(prime_implicants, binary): - chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))] - for i in range(len(prime_implicants)): - count = prime_implicants[i].count('_') - for j in range(len(binary)): - if(is_for_table(prime_implicants[i], binary[j], count)): - chart[i][j] = 1 - - return chart +def compare_string(string1: str, string2: str) -> str: + """ + >>> compare_string('0010','0110') + '0_10' + + >>> compare_string('0110','1101') + -1 + """ + l1 = list(string1) + l2 = list(string2) + count = 0 + for i in range(len(l1)): + if l1[i] != l2[i]: + count += 1 + l1[i] = "_" + if count > 1: + return -1 + else: + return "".join(l1) + + +def check(binary: [str]) -> [str]: + """ + >>> check(['0.00.01.5']) + ['0.00.01.5'] + """ + pi = [] + while 1: + check1 = ["$"] * len(binary) + temp = [] + for i in range(len(binary)): + for j in range(i + 1, len(binary)): + k = compare_string(binary[i], binary[j]) + if k != -1: + check1[i] = "*" + check1[j] = "*" + temp.append(k) + for i in range(len(binary)): + if check1[i] == "$": + pi.append(binary[i]) + if len(temp) == 0: + return pi + binary = list(set(temp)) + + +def decimal_to_binary(no_of_variable: int, minterms: [float]) -> [str]: + """ + >>> decimal_to_binary(3,[1.5]) + ['0.00.01.5'] + """ + temp = [] + s = "" + for m in minterms: + for i in range(no_of_variable): + s = str(m % 2) + s + m //= 2 + temp.append(s) + s = "" + return temp + + +def is_for_table(string1: str, string2: str, count: int) -> bool: + """ + >>> is_for_table('__1','011',2) + True + + >>> is_for_table('01_','001',1) + False + """ + l1 = list(string1) + l2 = list(string2) + count_n = 0 + for i in range(len(l1)): + if l1[i] != l2[i]: + count_n += 1 + if count_n == count: + return True + else: + return False + + +def selection(chart: [[int]], prime_implicants: [str]) -> [str]: + """ + >>> selection([[1]],['0.00.01.5']) + ['0.00.01.5'] + + >>> selection([[1]],['0.00.01.5']) + ['0.00.01.5'] + """ + temp = [] + select = [0] * len(chart) + for i in range(len(chart[0])): + count = 0 + rem = -1 + for j in range(len(chart)): + if chart[j][i] == 1: + count += 1 + rem = j + if count == 1: + select[rem] = 1 + for i in range(len(select)): + if select[i] == 1: + for j in range(len(chart[0])): + if chart[i][j] == 1: + for k in range(len(chart)): + chart[k][j] = 0 + temp.append(prime_implicants[i]) + while 1: + max_n = 0 + rem = -1 + count_n = 0 + for i in range(len(chart)): + count_n = chart[i].count(1) + if count_n > max_n: + max_n = count_n + rem = i + + if max_n == 0: + return temp + + temp.append(prime_implicants[rem]) + + for i in range(len(chart[0])): + if chart[rem][i] == 1: + for j in range(len(chart)): + chart[j][i] = 0 + + +def prime_implicant_chart(prime_implicants: [str], binary: [str]) -> [[int]]: + """ + >>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5']) + [[1]] + """ + chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))] + for i in range(len(prime_implicants)): + count = prime_implicants[i].count("_") + for j in range(len(binary)): + if is_for_table(prime_implicants[i], binary[j], count): + chart[i][j] = 1 + + return chart + def main(): - no_of_variable = int(input("Enter the no. of variables\n")) - minterms = [int(x) for x in input("Enter the decimal representation of Minterms 'Spaces Seprated'\n").split()] - binary = decimal_to_binary(no_of_variable, minterms) - - prime_implicants = check(binary) - print("Prime Implicants are:") - print(prime_implicants) - chart = prime_implicant_chart(prime_implicants, binary) - - essential_prime_implicants = selection(chart,prime_implicants) - print("Essential Prime Implicants are:") - print(essential_prime_implicants) - -if __name__ == '__main__': - main() + no_of_variable = int(input("Enter the no. of variables\n")) + minterms = [ + int(x) + for x in input( + "Enter the decimal representation of Minterms 'Spaces Separated'\n" + ).split() + ] + binary = decimal_to_binary(no_of_variable, minterms) + + prime_implicants = check(binary) + print("Prime Implicants are:") + print(prime_implicants) + chart = prime_implicant_chart(prime_implicants, binary) + + essential_prime_implicants = selection(chart, prime_implicants) + print("Essential Prime Implicants are:") + print(essential_prime_implicants) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/cellular_automata/README.md b/cellular_automata/README.md new file mode 100644 index 000000000000..c3fa0516f5dd --- /dev/null +++ b/cellular_automata/README.md @@ -0,0 +1,4 @@ +# Cellular Automata + +* https://en.wikipedia.org/wiki/Cellular_automaton +* https://mathworld.wolfram.com/ElementaryCellularAutomaton.html diff --git a/cellular_automata/__init__.py b/cellular_automata/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/cellular_automata/conways_game_of_life.py b/cellular_automata/conways_game_of_life.py new file mode 100644 index 000000000000..321baa3a3794 --- /dev/null +++ b/cellular_automata/conways_game_of_life.py @@ -0,0 +1,100 @@ +""" +Conway's Game of Life implemented in Python. +https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life +""" + +from __future__ import annotations + +from typing import List + +from PIL import Image + +# Define glider example +GLIDER = [ + [0, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0, 0], + [1, 1, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], +] + +# Define blinker example +BLINKER = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] + + +def new_generation(cells: List[List[int]]) -> List[List[int]]: + """ + Generates the next generation for a given state of Conway's Game of Life. + >>> new_generation(BLINKER) + [[0, 0, 0], [1, 1, 1], [0, 0, 0]] + """ + next_generation = [] + for i in range(len(cells)): + next_generation_row = [] + for j in range(len(cells[i])): + # Get the number of live neighbours + neighbour_count = 0 + if i > 0 and j > 0: + neighbour_count += cells[i - 1][j - 1] + if i > 0: + neighbour_count += cells[i - 1][j] + if i > 0 and j < len(cells[i]) - 1: + neighbour_count += cells[i - 1][j + 1] + if j > 0: + neighbour_count += cells[i][j - 1] + if j < len(cells[i]) - 1: + neighbour_count += cells[i][j + 1] + if i < len(cells) - 1 and j > 0: + neighbour_count += cells[i + 1][j - 1] + if i < len(cells) - 1: + neighbour_count += cells[i + 1][j] + if i < len(cells) - 1 and j < len(cells[i]) - 1: + neighbour_count += cells[i + 1][j + 1] + + # Rules of the game of life (excerpt from Wikipedia): + # 1. Any live cell with two or three live neighbours survives. + # 2. Any dead cell with three live neighbours becomes a live cell. + # 3. All other live cells die in the next generation. + # Similarly, all other dead cells stay dead. + alive = cells[i][j] == 1 + if ( + (alive and 2 <= neighbour_count <= 3) + or not alive + and neighbour_count == 3 + ): + next_generation_row.append(1) + else: + next_generation_row.append(0) + + next_generation.append(next_generation_row) + return next_generation + + +def generate_images(cells: list[list[int]], frames) -> list[Image.Image]: + """ + Generates a list of images of subsequent Game of Life states. + """ + images = [] + for _ in range(frames): + # Create output image + img = Image.new("RGB", (len(cells[0]), len(cells))) + pixels = img.load() + + # Save cells to image + for x in range(len(cells)): + for y in range(len(cells[0])): + colour = 255 - cells[y][x] * 255 + pixels[x, y] = (colour, colour, colour) + + # Save image + images.append(img) + cells = new_generation(cells) + return images + + +if __name__ == "__main__": + images = generate_images(GLIDER, 16) + images[0].save("out.gif", save_all=True, append_images=images[1:]) diff --git a/cellular_automata/one_dimensional.py b/cellular_automata/one_dimensional.py new file mode 100644 index 000000000000..da77e444502f --- /dev/null +++ b/cellular_automata/one_dimensional.py @@ -0,0 +1,74 @@ +""" +Return an image of 16 generations of one-dimensional cellular automata based on a given +ruleset number +https://mathworld.wolfram.com/ElementaryCellularAutomaton.html +""" + +from __future__ import annotations + +from PIL import Image + +# Define the first generation of cells +# fmt: off +CELLS = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] +# fmt: on + + +def format_ruleset(ruleset: int) -> list[int]: + """ + >>> format_ruleset(11100) + [0, 0, 0, 1, 1, 1, 0, 0] + >>> format_ruleset(0) + [0, 0, 0, 0, 0, 0, 0, 0] + >>> format_ruleset(11111111) + [1, 1, 1, 1, 1, 1, 1, 1] + """ + return [int(c) for c in f"{ruleset:08}"[:8]] + + +def new_generation(cells: list[list[int]], rule: list[int], time: int) -> list[int]: + population = len(cells[0]) # 31 + next_generation = [] + for i in range(population): + # Get the neighbors of each cell + # Handle neighbours outside bounds by using 0 as their value + left_neighbor = 0 if i == 0 else cells[time][i - 1] + right_neighbor = 0 if i == population - 1 else cells[time][i + 1] + # Define a new cell and add it to the new generation + situation = 7 - int(f"{left_neighbor}{cells[time][i]}{right_neighbor}", 2) + next_generation.append(rule[situation]) + return next_generation + + +def generate_image(cells: list[list[int]]) -> Image.Image: + """ + Convert the cells into a greyscale PIL.Image.Image and return it to the caller. + >>> from random import random + >>> cells = [[random() for w in range(31)] for h in range(16)] + >>> img = generate_image(cells) + >>> isinstance(img, Image.Image) + True + >>> img.width, img.height + (31, 16) + """ + # Create the output image + img = Image.new("RGB", (len(cells[0]), len(cells))) + pixels = img.load() + # Generates image + for w in range(img.width): + for h in range(img.height): + color = 255 - int(255 * cells[h][w]) + pixels[w, h] = (color, color, color) + return img + + +if __name__ == "__main__": + rule_num = bin(int(input("Rule:\n").strip()))[2:] + rule = format_ruleset(int(rule_num)) + for time in range(16): + CELLS.append(new_generation(CELLS, rule, time)) + img = generate_image(CELLS) + # Uncomment to save the image + # img.save(f"rule_{rule_num}.png") + img.show() diff --git a/ciphers/__init__.py b/ciphers/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/ciphers/a1z26.py b/ciphers/a1z26.py new file mode 100644 index 000000000000..92710ec44b0e --- /dev/null +++ b/ciphers/a1z26.py @@ -0,0 +1,33 @@ +""" +Convert a string of characters to a sequence of numbers +corresponding to the character's position in the alphabet. + +https://www.dcode.fr/letter-number-cipher +http://bestcodes.weebly.com/a1z26.html +""" + + +def encode(plain: str) -> list: + """ + >>> encode("myname") + [13, 25, 14, 1, 13, 5] + """ + return [ord(elem) - 96 for elem in plain] + + +def decode(encoded: list) -> str: + """ + >>> decode([13, 25, 14, 1, 13, 5]) + 'myname' + """ + return "".join(chr(elem + 96) for elem in encoded) + + +def main(): + encoded = encode(input("->").strip().lower()) + print("Encoded: ", encoded) + print("Decoded:", decode(encoded)) + + +if __name__ == "__main__": + main() diff --git a/ciphers/affine_cipher.py b/ciphers/affine_cipher.py index af5f4e0ff4c6..cf8c0d5f4c1d 100644 --- a/ciphers/affine_cipher.py +++ b/ciphers/affine_cipher.py @@ -1,44 +1,67 @@ -from __future__ import print_function -import sys, random, cryptomath_module as cryptoMath +import random +import sys + +from . import cryptomath_module as cryptomath + +SYMBOLS = ( + r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`""" + r"""abcdefghijklmnopqrstuvwxyz{|}~""" +) -SYMBOLS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~""" def main(): - message = input('Enter message: ') - key = int(input('Enter key [2000 - 9000]: ')) - mode = input('Encrypt/Decrypt [E/D]: ') - - if mode.lower().startswith('e'): - mode = 'encrypt' - translated = encryptMessage(key, message) - elif mode.lower().startswith('d'): - mode = 'decrypt' - translated = decryptMessage(key, message) - print('\n%sed text: \n%s' % (mode.title(), translated)) - -def getKeyParts(key): - keyA = key // len(SYMBOLS) - keyB = key % len(SYMBOLS) - return (keyA, keyB) - -def checkKeys(keyA, keyB, mode): - if keyA == 1 and mode == 'encrypt': - sys.exit('The affine cipher becomes weak when key A is set to 1. Choose different key') - if keyB == 0 and mode == 'encrypt': - sys.exit('The affine cipher becomes weak when key A is set to 1. Choose different key') + """ + >>> key = get_random_key() + >>> msg = "This is a test!" + >>> decrypt_message(key, encrypt_message(key, msg)) == msg + True + """ + message = input("Enter message: ").strip() + key = int(input("Enter key [2000 - 9000]: ").strip()) + mode = input("Encrypt/Decrypt [E/D]: ").strip().lower() + + if mode.startswith("e"): + mode = "encrypt" + translated = encrypt_message(key, message) + elif mode.startswith("d"): + mode = "decrypt" + translated = decrypt_message(key, message) + print(f"\n{mode.title()}ed text: \n{translated}") + + +def check_keys(keyA: int, keyB: int, mode: str) -> None: + if mode == "encrypt": + if keyA == 1: + sys.exit( + "The affine cipher becomes weak when key " + "A is set to 1. Choose different key" + ) + if keyB == 0: + sys.exit( + "The affine cipher becomes weak when key " + "B is set to 0. Choose different key" + ) if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1: - sys.exit('Key A must be greater than 0 and key B must be between 0 and %s.' % (len(SYMBOLS) - 1)) - if cryptoMath.gcd(keyA, len(SYMBOLS)) != 1: - sys.exit('Key A %s and the symbol set size %s are not relatively prime. Choose a different key.' % (keyA, len(SYMBOLS))) + sys.exit( + "Key A must be greater than 0 and key B must " + f"be between 0 and {len(SYMBOLS) - 1}." + ) + if cryptomath.gcd(keyA, len(SYMBOLS)) != 1: + sys.exit( + f"Key A {keyA} and the symbol set size {len(SYMBOLS)} " + "are not relatively prime. Choose a different key." + ) -def encryptMessage(key, message): - ''' - >>> encryptMessage(4545, 'The affine cipher is a type of monoalphabetic substitution cipher.') + +def encrypt_message(key: int, message: str) -> str: + """ + >>> encrypt_message(4545, 'The affine cipher is a type of monoalphabetic ' + ... 'substitution cipher.') 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi' - ''' - keyA, keyB = getKeyParts(key) - checkKeys(keyA, keyB, 'encrypt') - cipherText = '' + """ + keyA, keyB = divmod(key, len(SYMBOLS)) + check_keys(keyA, keyB, "encrypt") + cipherText = "" for symbol in message: if symbol in SYMBOLS: symIndex = SYMBOLS.find(symbol) @@ -47,15 +70,17 @@ def encryptMessage(key, message): cipherText += symbol return cipherText -def decryptMessage(key, message): - ''' - >>> decryptMessage(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi') + +def decrypt_message(key: int, message: str) -> str: + """ + >>> decrypt_message(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF' + ... '{xIp~{HL}Gi') 'The affine cipher is a type of monoalphabetic substitution cipher.' - ''' - keyA, keyB = getKeyParts(key) - checkKeys(keyA, keyB, 'decrypt') - plainText = '' - modInverseOfkeyA = cryptoMath.findModInverse(keyA, len(SYMBOLS)) + """ + keyA, keyB = divmod(key, len(SYMBOLS)) + check_keys(keyA, keyB, "decrypt") + plainText = "" + modInverseOfkeyA = cryptomath.findModInverse(keyA, len(SYMBOLS)) for symbol in message: if symbol in SYMBOLS: symIndex = SYMBOLS.find(symbol) @@ -64,14 +89,17 @@ def decryptMessage(key, message): plainText += symbol return plainText -def getRandomKey(): + +def get_random_key() -> int: while True: keyA = random.randint(2, len(SYMBOLS)) keyB = random.randint(2, len(SYMBOLS)) - if cryptoMath.gcd(keyA, len(SYMBOLS)) == 1: + if cryptomath.gcd(keyA, len(SYMBOLS)) == 1 and keyB % len(SYMBOLS) != 0: return keyA * len(SYMBOLS) + keyB -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() - main() + # main() diff --git a/ciphers/atbash.py b/ciphers/atbash.py new file mode 100644 index 000000000000..c17d1e34f37a --- /dev/null +++ b/ciphers/atbash.py @@ -0,0 +1,66 @@ +""" https://en.wikipedia.org/wiki/Atbash """ +import string + + +def atbash_slow(sequence: str) -> str: + """ + >>> atbash_slow("ABCDEFG") + 'ZYXWVUT' + + >>> atbash_slow("aW;;123BX") + 'zD;;123YC' + """ + output = "" + for i in sequence: + extract = ord(i) + if 65 <= extract <= 90: + output += chr(155 - extract) + elif 97 <= extract <= 122: + output += chr(219 - extract) + else: + output += i + return output + + +def atbash(sequence: str) -> str: + """ + >>> atbash("ABCDEFG") + 'ZYXWVUT' + + >>> atbash("aW;;123BX") + 'zD;;123YC' + """ + letters = string.ascii_letters + letters_reversed = string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1] + return "".join( + letters_reversed[letters.index(c)] if c in letters else c for c in sequence + ) + + +def benchmark() -> None: + """Let's benchmark them side-by-side...""" + from timeit import timeit + + print("Running performance benchmarks...") + print( + "> atbash_slow()", + timeit( + "atbash_slow(printable)", + setup="from string import printable ; from __main__ import atbash_slow", + ), + "seconds", + ) + print( + "> atbash()", + timeit( + "atbash(printable)", + setup="from string import printable ; from __main__ import atbash", + ), + "seconds", + ) + + +if __name__ == "__main__": + for sequence in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"): + print(f"{sequence} encrypted in atbash: {atbash(sequence)}") + benchmark() diff --git a/ciphers/base16.py b/ciphers/base16.py index 9bc0e5d8337a..f27ea4628e54 100644 --- a/ciphers/base16.py +++ b/ciphers/base16.py @@ -1,11 +1,22 @@ import base64 -def main(): - inp = input('->') - encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) - b16encoded = base64.b16encode(encoded) #b16encoded the encoded string - print(b16encoded) - print(base64.b16decode(b16encoded).decode('utf-8'))#decoded it - -if __name__ == '__main__': - main() + +def encode_to_b16(inp: str) -> bytes: + """ + Encodes a given utf-8 string into base-16. + >>> encode_to_b16('Hello World!') + b'48656C6C6F20576F726C6421' + >>> encode_to_b16('HELLO WORLD!') + b'48454C4C4F20574F524C4421' + >>> encode_to_b16('') + b'' + """ + encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) + b16encoded = base64.b16encode(encoded) # b16encoded the encoded string + return b16encoded + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/ciphers/base32.py b/ciphers/base32.py index 2ac29f441e94..5bba8c4dd685 100644 --- a/ciphers/base32.py +++ b/ciphers/base32.py @@ -1,11 +1,13 @@ import base64 + def main(): - inp = input('->') - encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) - b32encoded = base64.b32encode(encoded) #b32encoded the encoded string + inp = input("->") + encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) + b32encoded = base64.b32encode(encoded) # b32encoded the encoded string print(b32encoded) - print(base64.b32decode(b32encoded).decode('utf-8'))#decoded it + print(base64.b32decode(b32encoded).decode("utf-8")) # decoded it + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/base64_cipher.py b/ciphers/base64_cipher.py deleted file mode 100644 index fa3451c0cbae..000000000000 --- a/ciphers/base64_cipher.py +++ /dev/null @@ -1,64 +0,0 @@ -def encodeBase64(text): - base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" - - r = "" #the result - c = 3 - len(text) % 3 #the length of padding - p = "=" * c #the padding - s = text + "\0" * c #the text to encode - - i = 0 - while i < len(s): - if i > 0 and ((i / 3 * 4) % 76) == 0: - r = r + "\r\n" - - n = (ord(s[i]) << 16) + (ord(s[i+1]) << 8 ) + ord(s[i+2]) - - n1 = (n >> 18) & 63 - n2 = (n >> 12) & 63 - n3 = (n >> 6) & 63 - n4 = n & 63 - - r += base64chars[n1] + base64chars[n2] + base64chars[n3] + base64chars[n4] - i += 3 - - return r[0: len(r)-len(p)] + p - -def decodeBase64(text): - base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" - s = "" - - for i in text: - if i in base64chars: - s += i - c = "" - else: - if i == '=': - c += '=' - - p = "" - if c == "=": - p = 'A' - else: - if c == "==": - p = "AA" - - r = "" - s = s + p - - i = 0 - while i < len(s): - n = (base64chars.index(s[i]) << 18) + (base64chars.index(s[i+1]) << 12) + (base64chars.index(s[i+2]) << 6) +base64chars.index(s[i+3]) - - r += chr((n >> 16) & 255) + chr((n >> 8) & 255) + chr(n & 255) - - i += 4 - - return r[0: len(r) - len(p)] - -def main(): - print(encodeBase64("WELCOME to base64 encoding")) - print(decodeBase64(encodeBase64("WELCOME to base64 encoding"))) - - -if __name__ == '__main__': - main() diff --git a/ciphers/base64_encoding.py b/ciphers/base64_encoding.py new file mode 100644 index 000000000000..634afcb89873 --- /dev/null +++ b/ciphers/base64_encoding.py @@ -0,0 +1,142 @@ +B64_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + + +def base64_encode(data: bytes) -> bytes: + """Encodes data according to RFC4648. + + The data is first transformed to binary and appended with binary digits so that its + length becomes a multiple of 6, then each 6 binary digits will match a character in + the B64_CHARSET string. The number of appended binary digits would later determine + how many "=" sign should be added, the padding. + For every 2 binary digits added, a "=" sign is added in the output. + We can add any binary digits to make it a multiple of 6, for instance, consider the + following example: + "AA" -> 0010100100101001 -> 001010 010010 1001 + As can be seen above, 2 more binary digits should be added, so there's 4 + possibilities here: 00, 01, 10 or 11. + That being said, Base64 encoding can be used in Steganography to hide data in these + appended digits. + + >>> from base64 import b64encode + >>> a = b"This pull request is part of Hacktoberfest20!" + >>> b = b"https://tools.ietf.org/html/rfc4648" + >>> c = b"A" + >>> base64_encode(a) == b64encode(a) + True + >>> base64_encode(b) == b64encode(b) + True + >>> base64_encode(c) == b64encode(c) + True + >>> base64_encode("abc") + Traceback (most recent call last): + ... + TypeError: a bytes-like object is required, not 'str' + """ + # Make sure the supplied data is a bytes-like object + if not isinstance(data, bytes): + raise TypeError( + f"a bytes-like object is required, not '{data.__class__.__name__}'" + ) + + binary_stream = "".join(bin(byte)[2:].zfill(8) for byte in data) + + padding_needed = len(binary_stream) % 6 != 0 + + if padding_needed: + # The padding that will be added later + padding = b"=" * ((6 - len(binary_stream) % 6) // 2) + + # Append binary_stream with arbitrary binary digits (0's by default) to make its + # length a multiple of 6. + binary_stream += "0" * (6 - len(binary_stream) % 6) + else: + padding = b"" + + # Encode every 6 binary digits to their corresponding Base64 character + return ( + "".join( + B64_CHARSET[int(binary_stream[index : index + 6], 2)] + for index in range(0, len(binary_stream), 6) + ).encode() + + padding + ) + + +def base64_decode(encoded_data: str) -> bytes: + """Decodes data according to RFC4648. + + This does the reverse operation of base64_encode. + We first transform the encoded data back to a binary stream, take off the + previously appended binary digits according to the padding, at this point we + would have a binary stream whose length is multiple of 8, the last step is + to convert every 8 bits to a byte. + + >>> from base64 import b64decode + >>> a = "VGhpcyBwdWxsIHJlcXVlc3QgaXMgcGFydCBvZiBIYWNrdG9iZXJmZXN0MjAh" + >>> b = "aHR0cHM6Ly90b29scy5pZXRmLm9yZy9odG1sL3JmYzQ2NDg=" + >>> c = "QQ==" + >>> base64_decode(a) == b64decode(a) + True + >>> base64_decode(b) == b64decode(b) + True + >>> base64_decode(c) == b64decode(c) + True + >>> base64_decode("abc") + Traceback (most recent call last): + ... + AssertionError: Incorrect padding + """ + # Make sure encoded_data is either a string or a bytes-like object + if not isinstance(encoded_data, bytes) and not isinstance(encoded_data, str): + raise TypeError( + "argument should be a bytes-like object or ASCII string, not " + f"'{encoded_data.__class__.__name__}'" + ) + + # In case encoded_data is a bytes-like object, make sure it contains only + # ASCII characters so we convert it to a string object + if isinstance(encoded_data, bytes): + try: + encoded_data = encoded_data.decode("utf-8") + except UnicodeDecodeError: + raise ValueError("base64 encoded data should only contain ASCII characters") + + padding = encoded_data.count("=") + + # Check if the encoded string contains non base64 characters + if padding: + assert all( + char in B64_CHARSET for char in encoded_data[:-padding] + ), "Invalid base64 character(s) found." + else: + assert all( + char in B64_CHARSET for char in encoded_data + ), "Invalid base64 character(s) found." + + # Check the padding + assert len(encoded_data) % 4 == 0 and padding < 3, "Incorrect padding" + + if padding: + # Remove padding if there is one + encoded_data = encoded_data[:-padding] + + binary_stream = "".join( + bin(B64_CHARSET.index(char))[2:].zfill(6) for char in encoded_data + )[: -padding * 2] + else: + binary_stream = "".join( + bin(B64_CHARSET.index(char))[2:].zfill(6) for char in encoded_data + ) + + data = [ + int(binary_stream[index : index + 8], 2) + for index in range(0, len(binary_stream), 8) + ] + + return bytes(data) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/ciphers/base85.py b/ciphers/base85.py index 5fd13837f662..ebfd0480f794 100644 --- a/ciphers/base85.py +++ b/ciphers/base85.py @@ -1,11 +1,13 @@ import base64 + def main(): - inp = input('->') - encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object) - a85encoded = base64.a85encode(encoded) #a85encoded the encoded string + inp = input("->") + encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object) + a85encoded = base64.a85encode(encoded) # a85encoded the encoded string print(a85encoded) - print(base64.a85decode(a85encoded).decode('utf-8'))#decoded it + print(base64.a85decode(a85encoded).decode("utf-8")) # decoded it + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/beaufort_cipher.py b/ciphers/beaufort_cipher.py new file mode 100644 index 000000000000..c885dec74001 --- /dev/null +++ b/ciphers/beaufort_cipher.py @@ -0,0 +1,82 @@ +""" +Author: Mohit Radadiya +""" + +from string import ascii_uppercase + +dict1 = {char: i for i, char in enumerate(ascii_uppercase)} +dict2 = {i: char for i, char in enumerate(ascii_uppercase)} + + +# This function generates the key in +# a cyclic manner until it's length isn't +# equal to the length of original text +def generate_key(message: str, key: str) -> str: + """ + >>> generate_key("THE GERMAN ATTACK","SECRET") + 'SECRETSECRETSECRE' + """ + x = len(message) + i = 0 + while True: + if x == i: + i = 0 + if len(key) == len(message): + break + key += key[i] + i += 1 + return key + + +# This function returns the encrypted text +# generated with the help of the key +def cipher_text(message: str, key_new: str) -> str: + """ + >>> cipher_text("THE GERMAN ATTACK","SECRETSECRETSECRE") + 'BDC PAYUWL JPAIYI' + """ + cipher_text = "" + i = 0 + for letter in message: + if letter == " ": + cipher_text += " " + else: + x = (dict1[letter] - dict1[key_new[i]]) % 26 + i += 1 + cipher_text += dict2[x] + return cipher_text + + +# This function decrypts the encrypted text +# and returns the original text +def original_text(cipher_text: str, key_new: str) -> str: + """ + >>> original_text("BDC PAYUWL JPAIYI","SECRETSECRETSECRE") + 'THE GERMAN ATTACK' + """ + or_txt = "" + i = 0 + for letter in cipher_text: + if letter == " ": + or_txt += " " + else: + x = (dict1[letter] + dict1[key_new[i]] + 26) % 26 + i += 1 + or_txt += dict2[x] + return or_txt + + +def main(): + message = "THE GERMAN ATTACK" + key = "SECRET" + key_new = generate_key(message, key) + s = cipher_text(message, key_new) + print(f"Encrypted Text = {s}") + print(f"Original Text = {original_text(s, key_new)}") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/ciphers/brute_force_caesar_cipher.py b/ciphers/brute_force_caesar_cipher.py index 3b0716442fc5..13a165245403 100644 --- a/ciphers/brute_force_caesar_cipher.py +++ b/ciphers/brute_force_caesar_cipher.py @@ -1,5 +1,4 @@ -from __future__ import print_function -def decrypt(message): +def decrypt(message: str) -> None: """ >>> decrypt('TMDETUX PMDVU') Decryption using Key #0: TMDETUX PMDVU @@ -41,14 +40,17 @@ def decrypt(message): translated = translated + LETTERS[num] else: translated = translated + symbol - print("Decryption using Key #%s: %s" % (key, translated)) + print(f"Decryption using Key #{key}: {translated}") + def main(): message = input("Encrypted message: ") message = message.upper() decrypt(message) -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() main() diff --git a/ciphers/caesar_cipher.py b/ciphers/caesar_cipher.py index 39c069c95a7c..4b2f76c7d873 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,63 +1,238 @@ -import sys -def encrypt(strng, key): - encrypted = '' - for x in strng: - indx = (ord(x) + key) % 256 - if indx > 126: - indx = indx - 95 - encrypted = encrypted + chr(indx) - return encrypted - - -def decrypt(strng, key): - decrypted = '' - for x in strng: - indx = (ord(x) - key) % 256 - if indx < 32: - indx = indx + 95 - decrypted = decrypted + chr(indx) - return decrypted - -def brute_force(strng): - key = 1 - decrypted = '' - while key <= 94: - for x in strng: - indx = (ord(x) - key) % 256 - if indx < 32: - indx = indx + 95 - decrypted = decrypted + chr(indx) - print("Key: {}\t| Message: {}".format(key, decrypted)) - decrypted = '' - key += 1 - return None - - -def main(): +from string import ascii_letters +from typing import Dict, Optional + + +def encrypt(input_string: str, key: int, alphabet: Optional[str] = None) -> str: + """ + encrypt + ======= + Encodes a given string with the caesar cipher and returns the encoded + message + + Parameters: + ----------- + * input_string: the plain-text that needs to be encoded + * key: the number of letters to shift the message by + + Optional: + * alphabet (None): the alphabet used to encode the cipher, if not + specified, the standard english alphabet with upper and lowercase + letters is used + + Returns: + * A string containing the encoded cipher-text + + More on the caesar cipher + ========================= + The caesar cipher is named after Julius Caesar who used it when sending + secret military messages to his troops. This is a simple substitution cipher + where very character in the plain-text is shifted by a certain number known + as the "key" or "shift". + + Example: + Say we have the following message: + "Hello, captain" + + And our alphabet is made up of lower and uppercase letters: + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + + And our shift is "2" + + We can then encode the message, one letter at a time. "H" would become "J", + since "J" is two letters away, and so on. If the shift is ever two large, or + our letter is at the end of the alphabet, we just start at the beginning + ("Z" would shift to "a" then "b" and so on). + + Our final message would be "Jgnnq, ecrvckp" + + Further reading + =============== + * https://en.m.wikipedia.org/wiki/Caesar_cipher + + Doctests + ======== + >>> encrypt('The quick brown fox jumps over the lazy dog', 8) + 'bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo' + + >>> encrypt('A very large key', 8000) + 's nWjq dSjYW cWq' + + >>> encrypt('a lowercase alphabet', 5, 'abcdefghijklmnopqrstuvwxyz') + 'f qtbjwhfxj fqumfgjy' + """ + # Set default alphabet to lower and upper case english chars + alpha = alphabet or ascii_letters + + # The final result string + result = "" + + for character in input_string: + if character not in alpha: + # Append without encryption if character is not in the alphabet + result += character + else: + # Get the index of the new key and make sure it isn't too large + new_key = (alpha.index(character) + key) % len(alpha) + + # Append the encoded character to the alphabet + result += alpha[new_key] + + return result + + +def decrypt(input_string: str, key: int, alphabet: Optional[str] = None) -> str: + """ + decrypt + ======= + Decodes a given string of cipher-text and returns the decoded plain-text + + Parameters: + ----------- + * input_string: the cipher-text that needs to be decoded + * key: the number of letters to shift the message backwards by to decode + + Optional: + * alphabet (None): the alphabet used to decode the cipher, if not + specified, the standard english alphabet with upper and lowercase + letters is used + + Returns: + * A string containing the decoded plain-text + + More on the caesar cipher + ========================= + The caesar cipher is named after Julius Caesar who used it when sending + secret military messages to his troops. This is a simple substitution cipher + where very character in the plain-text is shifted by a certain number known + as the "key" or "shift". Please keep in mind, here we will be focused on + decryption. + + Example: + Say we have the following cipher-text: + "Jgnnq, ecrvckp" + + And our alphabet is made up of lower and uppercase letters: + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + + And our shift is "2" + + To decode the message, we would do the same thing as encoding, but in + reverse. The first letter, "J" would become "H" (remember: we are decoding) + because "H" is two letters in reverse (to the left) of "J". We would + continue doing this. A letter like "a" would shift back to the end of + the alphabet, and would become "Z" or "Y" and so on. + + Our final message would be "Hello, captain" + + Further reading + =============== + * https://en.m.wikipedia.org/wiki/Caesar_cipher + + Doctests + ======== + >>> decrypt('bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo', 8) + 'The quick brown fox jumps over the lazy dog' + + >>> decrypt('s nWjq dSjYW cWq', 8000) + 'A very large key' + + >>> decrypt('f qtbjwhfxj fqumfgjy', 5, 'abcdefghijklmnopqrstuvwxyz') + 'a lowercase alphabet' + """ + # Turn on decode mode by making the key negative + key *= -1 + + return encrypt(input_string, key, alphabet) + + +def brute_force(input_string: str, alphabet: Optional[str] = None) -> Dict[int, str]: + """ + brute_force + =========== + Returns all the possible combinations of keys and the decoded strings in the + form of a dictionary + + Parameters: + ----------- + * input_string: the cipher-text that needs to be used during brute-force + + Optional: + * alphabet: (None): the alphabet used to decode the cipher, if not + specified, the standard english alphabet with upper and lowercase + letters is used + + More about brute force + ====================== + Brute force is when a person intercepts a message or password, not knowing + the key and tries every single combination. This is easy with the caesar + cipher since there are only all the letters in the alphabet. The more + complex the cipher, the larger amount of time it will take to do brute force + + Ex: + Say we have a 5 letter alphabet (abcde), for simplicity and we intercepted the + following message: + + "dbc" + + we could then just write out every combination: + ecd... and so on, until we reach a combination that makes sense: + "cab" + + Further reading + =============== + * https://en.wikipedia.org/wiki/Brute_force + + Doctests + ======== + >>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20] + "Please don't brute force me!" + + >>> brute_force(1) + Traceback (most recent call last): + TypeError: 'int' object is not iterable + """ + # Set default alphabet to lower and upper case english chars + alpha = alphabet or ascii_letters + + # To store data on all the combinations + brute_force_data = {} + + # Cycle through each combination + for key in range(1, len(alpha) + 1): + # Decrypt the message and store the result in the data + brute_force_data[key] = decrypt(input_string, key, alpha) + + return brute_force_data + + +if __name__ == "__main__": while True: - print('-' * 10 + "\n**Menu**\n" + '-' * 10) - print("1.Encrpyt") - print("2.Decrypt") - print("3.BruteForce") - print("4.Quit") - choice = input("What would you like to do?: ") - if choice not in ['1', '2', '3', '4']: - print ("Invalid choice, please enter a valid choice") - elif choice == '1': - strng = input("Please enter the string to be encrypted: ") - key = int(input("Please enter off-set between 1-94: ")) - if key in range(1, 95): - print (encrypt(strng.lower(), key)) - elif choice == '2': - strng = input("Please enter the string to be decrypted: ") - key = int(input("Please enter off-set between 1-94: ")) - if key in range(1,95): - print(decrypt(strng, key)) - elif choice == '3': - strng = input("Please enter the string to be decrypted: ") - brute_force(strng) - main() - elif choice == '4': - print ("Goodbye.") + print(f'\n{"-" * 10}\n Menu\n{"-" * 10}') + print(*["1.Encrypt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep="\n") + + # get user input + choice = input("\nWhat would you like to do?: ").strip() or "4" + + # run functions based on what the user chose + if choice not in ("1", "2", "3", "4"): + print("Invalid choice, please enter a valid choice") + elif choice == "1": + input_string = input("Please enter the string to be encrypted: ") + key = int(input("Please enter off-set: ").strip()) + + print(encrypt(input_string, key)) + elif choice == "2": + input_string = input("Please enter the string to be decrypted: ") + key = int(input("Please enter off-set: ").strip()) + + print(decrypt(input_string, key)) + elif choice == "3": + input_string = input("Please enter the string to be decrypted: ") + brute_force_data = brute_force(input_string) + + for key, value in brute_force_data.items(): + print(f"Key: {key} | Message: {value}") + + elif choice == "4": + print("Goodbye.") break -main() diff --git a/ciphers/cryptomath_module.py b/ciphers/cryptomath_module.py index 3e8e71b117ed..ffeac1617f64 100644 --- a/ciphers/cryptomath_module.py +++ b/ciphers/cryptomath_module.py @@ -1,14 +1,15 @@ -def gcd(a, b): +def gcd(a: int, b: int) -> int: while a != 0: a, b = b % a, a return b -def findModInverse(a, m): + +def findModInverse(a: int, m: int) -> int: if gcd(a, m) != 1: return None u1, u2, u3 = 1, 0, a v1, v2, v3 = 0, 1, m while v3 != 0: q = u3 // v3 - v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q *v3), v1, v2, v3 - return u1 % m + v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3 + return u1 % m diff --git a/ciphers/decrypt_caesar_with_chi_squared.py b/ciphers/decrypt_caesar_with_chi_squared.py new file mode 100644 index 000000000000..41b4a12ba453 --- /dev/null +++ b/ciphers/decrypt_caesar_with_chi_squared.py @@ -0,0 +1,240 @@ +#!/usr/bin/env python3 + +from typing import Tuple + + +def decrypt_caesar_with_chi_squared( + ciphertext: str, + cipher_alphabet: str = None, + frequencies_dict: str = None, + case_sensetive: bool = False, +) -> Tuple[int, float, str]: + """ + Basic Usage + =========== + Arguments: + * ciphertext (str): the text to decode (encoded with the caesar cipher) + + Optional Arguments: + * cipher_alphabet (list): the alphabet used for the cipher (each letter is + a string separated by commas) + * frequencies_dict (dict): a dictionary of word frequencies where keys are + the letters and values are a percentage representation of the frequency as + a decimal/float + * case_sensetive (bool): a boolean value: True if the case matters during + decryption, False if it doesn't + + Returns: + * A tuple in the form of: + ( + most_likely_cipher, + most_likely_cipher_chi_squared_value, + decoded_most_likely_cipher + ) + + where... + - most_likely_cipher is an integer representing the shift of the smallest + chi-squared statistic (most likely key) + - most_likely_cipher_chi_squared_value is a float representing the + chi-squared statistic of the most likely shift + - decoded_most_likely_cipher is a string with the decoded cipher + (decoded by the most_likely_cipher key) + + + The Chi-squared test + ==================== + + The caesar cipher + ----------------- + The caesar cipher is a very insecure encryption algorithm, however it has + been used since Julius Caesar. The cipher is a simple substitution cipher + where each character in the plain text is replaced by a character in the + alphabet a certain number of characters after the original character. The + number of characters away is called the shift or key. For example: + + Plain text: hello + Key: 1 + Cipher text: ifmmp + (each letter in hello has been shifted one to the right in the eng. alphabet) + + As you can imagine, this doesn't provide lots of security. In fact + decrypting ciphertext by brute-force is extremely easy even by hand. However + one way to do that is the chi-squared test. + + The chi-squared test + ------------------- + Each letter in the english alphabet has a frequency, or the amount of times + it shows up compared to other letters (usually expressed as a decimal + representing the percentage likelihood). The most common letter in the + english language is "e" with a frequency of 0.11162 or 11.162%. The test is + completed in the following fashion. + + 1. The ciphertext is decoded in a brute force way (every combination of the + 26 possible combinations) + 2. For every combination, for each letter in the combination, the average + amount of times the letter should appear the message is calculated by + multiplying the total number of characters by the frequency of the letter + + For example: + In a message of 100 characters, e should appear around 11.162 times. + + 3. Then, to calculate the margin of error (the amount of times the letter + SHOULD appear with the amount of times the letter DOES appear), we use + the chi-squared test. The following formula is used: + + Let: + - n be the number of times the letter actually appears + - p be the predicted value of the number of times the letter should + appear (see #2) + - let v be the chi-squared test result (referred to here as chi-squared + value/statistic) + + (n - p)^2 + --------- = v + p + + 4. Each chi squared value for each letter is then added up to the total. + The total is the chi-squared statistic for that encryption key. + 5. The encryption key with the lowest chi-squared value is the most likely + to be the decoded answer. + + Further Reading + ================ + + * http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared- + statistic/ + * https://en.wikipedia.org/wiki/Letter_frequency + * https://en.wikipedia.org/wiki/Chi-squared_test + * https://en.m.wikipedia.org/wiki/Caesar_cipher + + Doctests + ======== + >>> decrypt_caesar_with_chi_squared( + ... 'dof pz aol jhlzhy jpwoly zv wvwbshy? pa pz avv lhzf av jyhjr!' + ... ) # doctest: +NORMALIZE_WHITESPACE + (7, 3129.228005747531, + 'why is the caesar cipher so popular? it is too easy to crack!') + + >>> decrypt_caesar_with_chi_squared('crybd cdbsxq') + (10, 233.35343938980898, 'short string') + + >>> decrypt_caesar_with_chi_squared(12) + Traceback (most recent call last): + AttributeError: 'int' object has no attribute 'lower' + """ + alphabet_letters = cipher_alphabet or [chr(i) for i in range(97, 123)] + frequencies_dict = frequencies_dict or {} + + if frequencies_dict == {}: + # Frequencies of letters in the english language (how much they show up) + frequencies = { + "a": 0.08497, + "b": 0.01492, + "c": 0.02202, + "d": 0.04253, + "e": 0.11162, + "f": 0.02228, + "g": 0.02015, + "h": 0.06094, + "i": 0.07546, + "j": 0.00153, + "k": 0.01292, + "l": 0.04025, + "m": 0.02406, + "n": 0.06749, + "o": 0.07507, + "p": 0.01929, + "q": 0.00095, + "r": 0.07587, + "s": 0.06327, + "t": 0.09356, + "u": 0.02758, + "v": 0.00978, + "w": 0.02560, + "x": 0.00150, + "y": 0.01994, + "z": 0.00077, + } + else: + # Custom frequencies dictionary + frequencies = frequencies_dict + + if not case_sensetive: + ciphertext = ciphertext.lower() + + # Chi squared statistic values + chi_squared_statistic_values = {} + + # cycle through all of the shifts + for shift in range(len(alphabet_letters)): + decrypted_with_shift = "" + + # decrypt the message with the shift + for letter in ciphertext: + try: + # Try to index the letter in the alphabet + new_key = (alphabet_letters.index(letter) - shift) % len( + alphabet_letters + ) + decrypted_with_shift += alphabet_letters[new_key] + except ValueError: + # Append the character if it isn't in the alphabet + decrypted_with_shift += letter + + chi_squared_statistic = 0.0 + + # Loop through each letter in the decoded message with the shift + for letter in decrypted_with_shift: + if case_sensetive: + if letter in frequencies: + # Get the amount of times the letter occurs in the message + occurrences = decrypted_with_shift.count(letter) + + # Get the excepcted amount of times the letter should appear based + # on letter frequencies + expected = frequencies[letter] * occurrences + + # Complete the chi squared statistic formula + chi_letter_value = ((occurrences - expected) ** 2) / expected + + # Add the margin of error to the total chi squared statistic + chi_squared_statistic += chi_letter_value + else: + if letter.lower() in frequencies: + # Get the amount of times the letter occurs in the message + occurrences = decrypted_with_shift.count(letter) + + # Get the excepcted amount of times the letter should appear based + # on letter frequencies + expected = frequencies[letter] * occurrences + + # Complete the chi squared statistic formula + chi_letter_value = ((occurrences - expected) ** 2) / expected + + # Add the margin of error to the total chi squared statistic + chi_squared_statistic += chi_letter_value + + # Add the data to the chi_squared_statistic_values dictionary + chi_squared_statistic_values[shift] = [ + chi_squared_statistic, + decrypted_with_shift, + ] + + # Get the most likely cipher by finding the cipher with the smallest chi squared + # statistic + most_likely_cipher = min( + chi_squared_statistic_values, key=chi_squared_statistic_values.get + ) + + # Get all the data from the most likely cipher (key, decoded message) + most_likely_cipher_chi_squared_value = chi_squared_statistic_values[ + most_likely_cipher + ][0] + decoded_most_likely_cipher = chi_squared_statistic_values[most_likely_cipher][1] + + # Return the data on the most likely shift + return ( + most_likely_cipher, + most_likely_cipher_chi_squared_value, + decoded_most_likely_cipher, + ) diff --git a/ciphers/deterministic_miller_rabin.py b/ciphers/deterministic_miller_rabin.py new file mode 100644 index 000000000000..d7fcb67e936c --- /dev/null +++ b/ciphers/deterministic_miller_rabin.py @@ -0,0 +1,137 @@ +"""Created by Nathan Damon, @bizzfitch on github +>>> test_miller_rabin() +""" + + +def miller_rabin(n: int, allow_probable: bool = False) -> bool: + """Deterministic Miller-Rabin algorithm for primes ~< 3.32e24. + + Uses numerical analysis results to return whether or not the passed number + is prime. If the passed number is above the upper limit, and + allow_probable is True, then a return value of True indicates that n is + probably prime. This test does not allow False negatives- a return value + of False is ALWAYS composite. + + Parameters + ---------- + n : int + The integer to be tested. Since we usually care if a number is prime, + n < 2 returns False instead of raising a ValueError. + allow_probable: bool, default False + Whether or not to test n above the upper bound of the deterministic test. + + Raises + ------ + ValueError + + Reference + --------- + https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test + """ + if n == 2: + return True + if not n % 2 or n < 2: + return False + if n > 5 and n % 10 not in (1, 3, 7, 9): # can quickly check last digit + return False + if n > 3_317_044_064_679_887_385_961_981 and not allow_probable: + raise ValueError( + "Warning: upper bound of deterministic test is exceeded. " + "Pass allow_probable=True to allow probabilistic test. " + "A return value of True indicates a probable prime." + ) + # array bounds provided by analysis + bounds = [ + 2_047, + 1_373_653, + 25_326_001, + 3_215_031_751, + 2_152_302_898_747, + 3_474_749_660_383, + 341_550_071_728_321, + 1, + 3_825_123_056_546_413_051, + 1, + 1, + 318_665_857_834_031_151_167_461, + 3_317_044_064_679_887_385_961_981, + ] + + primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41] + for idx, _p in enumerate(bounds, 1): + if n < _p: + # then we have our last prime to check + plist = primes[:idx] + break + d, s = n - 1, 0 + # break up n -1 into a power of 2 (s) and + # remaining odd component + # essentially, solve for d * 2 ** s == n - 1 + while d % 2 == 0: + d //= 2 + s += 1 + for prime in plist: + pr = False + for r in range(s): + m = pow(prime, d * 2 ** r, n) + # see article for analysis explanation for m + if (r == 0 and m == 1) or ((m + 1) % n == 0): + pr = True + # this loop will not determine compositeness + break + if pr: + continue + # if pr is False, then the above loop never evaluated to true, + # and the n MUST be composite + return False + return True + + +def test_miller_rabin() -> None: + """Testing a nontrivial (ends in 1, 3, 7, 9) composite + and a prime in each range. + """ + assert not miller_rabin(561) + assert miller_rabin(563) + # 2047 + + assert not miller_rabin(838_201) + assert miller_rabin(838_207) + # 1_373_653 + + assert not miller_rabin(17_316_001) + assert miller_rabin(17_316_017) + # 25_326_001 + + assert not miller_rabin(3_078_386_641) + assert miller_rabin(3_078_386_653) + # 3_215_031_751 + + assert not miller_rabin(1_713_045_574_801) + assert miller_rabin(1_713_045_574_819) + # 2_152_302_898_747 + + assert not miller_rabin(2_779_799_728_307) + assert miller_rabin(2_779_799_728_327) + # 3_474_749_660_383 + + assert not miller_rabin(113_850_023_909_441) + assert miller_rabin(113_850_023_909_527) + # 341_550_071_728_321 + + assert not miller_rabin(1_275_041_018_848_804_351) + assert miller_rabin(1_275_041_018_848_804_391) + # 3_825_123_056_546_413_051 + + assert not miller_rabin(79_666_464_458_507_787_791_867) + assert miller_rabin(79_666_464_458_507_787_791_951) + # 318_665_857_834_031_151_167_461 + + assert not miller_rabin(552_840_677_446_647_897_660_333) + assert miller_rabin(552_840_677_446_647_897_660_359) + # 3_317_044_064_679_887_385_961_981 + # upper limit for probabilistic test + + +if __name__ == "__main__": + test_miller_rabin() diff --git a/ciphers/diffie.py b/ciphers/diffie.py new file mode 100644 index 000000000000..44b12bf9d103 --- /dev/null +++ b/ciphers/diffie.py @@ -0,0 +1,25 @@ +def find_primitive(n: int) -> int: + for r in range(1, n): + li = [] + for x in range(n - 1): + val = pow(r, x, n) + if val in li: + break + li.append(val) + else: + return r + + +if __name__ == "__main__": + q = int(input("Enter a prime number q: ")) + a = find_primitive(q) + a_private = int(input("Enter private key of A: ")) + a_public = pow(a, a_private, q) + b_private = int(input("Enter private key of B: ")) + b_public = pow(a, b_private, q) + + a_secret = pow(b_public, a_private, q) + b_secret = pow(a_public, b_private, q) + + print("The key value generated by A is: ", a_secret) + print("The key value generated by B is: ", b_secret) diff --git a/ciphers/elgamal_key_generator.py b/ciphers/elgamal_key_generator.py index 6a8751f69524..52cf69074187 100644 --- a/ciphers/elgamal_key_generator.py +++ b/ciphers/elgamal_key_generator.py @@ -1,25 +1,28 @@ import os import random import sys -import rabin_miller as rabinMiller, cryptomath_module as cryptoMath + +from . import cryptomath_module as cryptoMath +from . import rabin_miller as rabinMiller min_primitive_root = 3 def main(): - print('Making key files...') - makeKeyFiles('elgamal', 2048) - print('Key files generation successful') + print("Making key files...") + makeKeyFiles("elgamal", 2048) + print("Key files generation successful") # I have written my code naively same as definition of primitive root # however every time I run this program, memory exceeded... -# so I used 4.80 Algorithm in Handbook of Applied Cryptography(CRC Press, ISBN : 0-8493-8523-7, October 1996) +# so I used 4.80 Algorithm in +# Handbook of Applied Cryptography(CRC Press, ISBN : 0-8493-8523-7, October 1996) # and it seems to run nicely! -def primitiveRoot(p_val): +def primitiveRoot(p_val: int) -> int: print("Generating primitive root of p") while True: - g = random.randrange(3,p_val) + g = random.randrange(3, p_val) if pow(g, 2, p_val) == 1: continue if pow(g, p_val, p_val) == 1: @@ -27,8 +30,8 @@ def primitiveRoot(p_val): return g -def generateKey(keySize): - print('Generating prime p...') +def generateKey(keySize: int) -> ((int, int, int, int), (int, int)): + print("Generating prime p...") p = rabinMiller.generateLargePrime(keySize) # select large prime number. e_1 = primitiveRoot(p) # one primitive root on modulo p. d = random.randrange(3, p) # private_key -> have to be greater than 2 for safety. @@ -40,24 +43,29 @@ def generateKey(keySize): return publicKey, privateKey -def makeKeyFiles(name, keySize): - if os.path.exists('%s_pubkey.txt' % name) or os.path.exists('%s_privkey.txt' % name): - print('\nWARNING:') - print('"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n' - 'Use a different name or delete these files and re-run this program.' % - (name, name)) +def makeKeyFiles(name: str, keySize: int): + if os.path.exists("%s_pubkey.txt" % name) or os.path.exists( + "%s_privkey.txt" % name + ): + print("\nWARNING:") + print( + '"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n' + "Use a different name or delete these files and re-run this program." + % (name, name) + ) sys.exit() publicKey, privateKey = generateKey(keySize) - print('\nWriting public key to file %s_pubkey.txt...' % name) - with open('%s_pubkey.txt' % name, 'w') as fo: - fo.write('%d,%d,%d,%d' % (publicKey[0], publicKey[1], publicKey[2], publicKey[3])) + print("\nWriting public key to file %s_pubkey.txt..." % name) + with open("%s_pubkey.txt" % name, "w") as fo: + fo.write( + "%d,%d,%d,%d" % (publicKey[0], publicKey[1], publicKey[2], publicKey[3]) + ) - print('Writing private key to file %s_privkey.txt...' % name) - with open('%s_privkey.txt' % name, 'w') as fo: - fo.write('%d,%d' % (privateKey[0], privateKey[1])) + print("Writing private key to file %s_privkey.txt..." % name) + with open("%s_privkey.txt" % name, "w") as fo: + fo.write("%d,%d" % (privateKey[0], privateKey[1])) -if __name__ == '__main__': +if __name__ == "__main__": main() - \ No newline at end of file diff --git a/ciphers/enigma_machine2.py b/ciphers/enigma_machine2.py new file mode 100644 index 000000000000..4344db0056fd --- /dev/null +++ b/ciphers/enigma_machine2.py @@ -0,0 +1,287 @@ +""" +Wikipedia: https://en.wikipedia.org/wiki/Enigma_machine +Video explanation: https://youtu.be/QwQVMqfoB2E +Also check out Numberphile's and Computerphile's videos on this topic + +This module contains function 'enigma' which emulates +the famous Enigma machine from WWII. +Module includes: +- enigma function +- showcase of function usage +- 9 randnomly generated rotors +- reflector (aka static rotor) +- original alphabet + +Created by TrapinchO +""" + +# used alphabet -------------------------- +# from string.ascii_uppercase +abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + +# -------------------------- default selection -------------------------- +# rotors -------------------------- +rotor1 = "EGZWVONAHDCLFQMSIPJBYUKXTR" +rotor2 = "FOBHMDKEXQNRAULPGSJVTYICZW" +rotor3 = "ZJXESIUQLHAVRMDOYGTNFWPBKC" +# reflector -------------------------- +reflector = { + "A": "N", + "N": "A", + "B": "O", + "O": "B", + "C": "P", + "P": "C", + "D": "Q", + "Q": "D", + "E": "R", + "R": "E", + "F": "S", + "S": "F", + "G": "T", + "T": "G", + "H": "U", + "U": "H", + "I": "V", + "V": "I", + "J": "W", + "W": "J", + "K": "X", + "X": "K", + "L": "Y", + "Y": "L", + "M": "Z", + "Z": "M", +} + +# -------------------------- extra rotors -------------------------- +rotor4 = "RMDJXFUWGISLHVTCQNKYPBEZOA" +rotor5 = "SGLCPQWZHKXAREONTFBVIYJUDM" +rotor6 = "HVSICLTYKQUBXDWAJZOMFGPREN" +rotor7 = "RZWQHFMVDBKICJLNTUXAGYPSOE" +rotor8 = "LFKIJODBEGAMQPXVUHYSTCZRWN" +rotor9 = "KOAEGVDHXPQZMLFTYWJNBRCIUS" + + +def _validator(rotpos: tuple, rotsel: tuple, pb: str) -> tuple: + """ + Checks if the values can be used for the 'enigma' function + + >>> _validator((1,1,1), (rotor1, rotor2, rotor3), 'POLAND') + ((1, 1, 1), ('EGZWVONAHDCLFQMSIPJBYUKXTR', 'FOBHMDKEXQNRAULPGSJVTYICZW', \ +'ZJXESIUQLHAVRMDOYGTNFWPBKC'), \ +{'P': 'O', 'O': 'P', 'L': 'A', 'A': 'L', 'N': 'D', 'D': 'N'}) + + :param rotpos: rotor_positon + :param rotsel: rotor_selection + :param pb: plugb -> validated and transformed + :return: (rotpos, rotsel, pb) + """ + # Checks if there are 3 unique rotors + + unique_rotsel = len(set(rotsel)) + if unique_rotsel < 3: + raise Exception(f"Please use 3 unique rotors (not {unique_rotsel})") + + # Checks if rotor positions are valid + rotorpos1, rotorpos2, rotorpos3 = rotpos + if not 0 < rotorpos1 <= len(abc): + raise ValueError( + f"First rotor position is not within range of 1..26 (" f"{rotorpos1}" + ) + if not 0 < rotorpos2 <= len(abc): + raise ValueError( + f"Second rotor position is not within range of 1..26 (" f"{rotorpos2})" + ) + if not 0 < rotorpos3 <= len(abc): + raise ValueError( + f"Third rotor position is not within range of 1..26 (" f"{rotorpos3})" + ) + + # Validates string and returns dict + pb = _plugboard(pb) + + return rotpos, rotsel, pb + + +def _plugboard(pbstring: str) -> dict: + """ + https://en.wikipedia.org/wiki/Enigma_machine#Plugboard + + >>> _plugboard('PICTURES') + {'P': 'I', 'I': 'P', 'C': 'T', 'T': 'C', 'U': 'R', 'R': 'U', 'E': 'S', 'S': 'E'} + >>> _plugboard('POLAND') + {'P': 'O', 'O': 'P', 'L': 'A', 'A': 'L', 'N': 'D', 'D': 'N'} + + In the code, 'pb' stands for 'plugboard' + + Pairs can be separated by spaces + :param pbstring: string containing plugboard setting for the Enigma machine + :return: dictionary containing converted pairs + """ + + # tests the input string if it + # a) is type string + # b) has even length (so pairs can be made) + if not isinstance(pbstring, str): + raise TypeError(f"Plugboard setting isn't type string ({type(pbstring)})") + elif len(pbstring) % 2 != 0: + raise Exception(f"Odd number of symbols ({len(pbstring)})") + elif pbstring == "": + return {} + + pbstring.replace(" ", "") + + # Checks if all characters are unique + tmppbl = set() + for i in pbstring: + if i not in abc: + raise Exception(f"'{i}' not in list of symbols") + elif i in tmppbl: + raise Exception(f"Duplicate symbol ({i})") + else: + tmppbl.add(i) + del tmppbl + + # Created the dictionary + pb = {} + for i in range(0, len(pbstring) - 1, 2): + pb[pbstring[i]] = pbstring[i + 1] + pb[pbstring[i + 1]] = pbstring[i] + + return pb + + +def enigma( + text: str, + rotor_position: tuple, + rotor_selection: tuple = (rotor1, rotor2, rotor3), + plugb: str = "", +) -> str: + """ + The only difference with real-world enigma is that I allowed string input. + All characters are converted to uppercase. (non-letter symbol are ignored) + How it works: + (for every letter in the message) + + - Input letter goes into the plugboard. + If it is connected to another one, switch it. + + - Letter goes through 3 rotors. + Each rotor can be represented as 2 sets of symbol, where one is shuffled. + Each symbol from the first set has corresponding symbol in + the second set and vice versa. + + example: + | ABCDEFGHIJKLMNOPQRSTUVWXYZ | e.g. F=D and D=F + | VKLEPDBGRNWTFCJOHQAMUZYIXS | + + - Symbol then goes through reflector (static rotor). + There it is switched with paired symbol + The reflector can be represented as2 sets, each with half of the alphanet. + There are usually 10 pairs of letters. + + Example: + | ABCDEFGHIJKLM | e.g. E is paired to X + | ZYXWVUTSRQPON | so when E goes in X goes out and vice versa + + - Letter then goes through the rotors again + + - If the letter is connected to plugboard, it is switched. + + - Return the letter + + >>> enigma('Hello World!', (1, 2, 1), plugb='pictures') + 'KORYH JUHHI!' + >>> enigma('KORYH, juhhi!', (1, 2, 1), plugb='pictures') + 'HELLO, WORLD!' + >>> enigma('hello world!', (1, 1, 1), plugb='pictures') + 'FPNCZ QWOBU!' + >>> enigma('FPNCZ QWOBU', (1, 1, 1), plugb='pictures') + 'HELLO WORLD' + + + :param text: input message + :param rotor_position: tuple with 3 values in range 1..26 + :param rotor_selection: tuple with 3 rotors () + :param plugb: string containing plugboard configuration (default '') + :return: en/decrypted string + """ + + text = text.upper() + rotor_position, rotor_selection, plugboard = _validator( + rotor_position, rotor_selection, plugb.upper() + ) + + rotorpos1, rotorpos2, rotorpos3 = rotor_position + rotor1, rotor2, rotor3 = rotor_selection + rotorpos1 -= 1 + rotorpos2 -= 1 + rotorpos3 -= 1 + + result = [] + + # encryption/decryption process -------------------------- + for symbol in text: + if symbol in abc: + + # 1st plugboard -------------------------- + if symbol in plugboard: + symbol = plugboard[symbol] + + # rotor ra -------------------------- + index = abc.index(symbol) + rotorpos1 + symbol = rotor1[index % len(abc)] + + # rotor rb -------------------------- + index = abc.index(symbol) + rotorpos2 + symbol = rotor2[index % len(abc)] + + # rotor rc -------------------------- + index = abc.index(symbol) + rotorpos3 + symbol = rotor3[index % len(abc)] + + # reflector -------------------------- + # this is the reason you don't need another machine to decipher + + symbol = reflector[symbol] + + # 2nd rotors + symbol = abc[rotor3.index(symbol) - rotorpos3] + symbol = abc[rotor2.index(symbol) - rotorpos2] + symbol = abc[rotor1.index(symbol) - rotorpos1] + + # 2nd plugboard + if symbol in plugboard: + symbol = plugboard[symbol] + + # moves/resets rotor positions + rotorpos1 += 1 + if rotorpos1 >= len(abc): + rotorpos1 = 0 + rotorpos2 += 1 + if rotorpos2 >= len(abc): + rotorpos2 = 0 + rotorpos3 += 1 + if rotorpos3 >= len(abc): + rotorpos3 = 0 + + # else: + # pass + # Error could be also raised + # raise ValueError( + # 'Invalid symbol('+repr(symbol)+')') + result.append(symbol) + + return "".join(result) + + +if __name__ == "__main__": + message = "This is my Python script that emulates the Enigma machine from WWII." + rotor_pos = (1, 1, 1) + pb = "pictures" + rotor_sel = (rotor2, rotor4, rotor8) + en = enigma(message, rotor_pos, rotor_sel, pb) + + print("Encrypted message:", en) + print("Decrypted message:", enigma(en, rotor_pos, rotor_sel, pb)) diff --git a/ciphers/hill_cipher.py b/ciphers/hill_cipher.py index 89b88beed17e..8237abf6aa5d 100644 --- a/ciphers/hill_cipher.py +++ b/ciphers/hill_cipher.py @@ -1,17 +1,16 @@ """ Hill Cipher: -The below defined class 'HillCipher' implements the Hill Cipher algorithm. -The Hill Cipher is an algorithm that implements modern linear algebra techniques -In this algortihm, you have an encryption key matrix. This is what will be used -in encoding and decoding your text. +The 'HillCipher' class below implements the Hill Cipher algorithm which uses +modern linear algebra techniques to encode and decode text using an encryption +key matrix. -Algortihm: +Algorithm: Let the order of the encryption key be N (as it is a square matrix). Your text is divided into batches of length N and converted to numerical vectors by a simple mapping starting with A=0 and so on. -The key is then mulitplied with the newly created batch vector to obtain the +The key is then multiplied with the newly created batch vector to obtain the encoded vector. After each multiplication modular 36 calculations are performed on the vectors so as to bring the numbers between 0 and 36 and then mapped with their corresponding alphanumerics. @@ -24,12 +23,11 @@ The determinant of the encryption key matrix must be relatively prime w.r.t 36. Note: -The algorithm implemented in this code considers only alphanumerics in the text. -If the length of the text to be encrypted is not a multiple of the -break key(the length of one batch of letters),the last character of the text -is added to the text until the length of the text reaches a multiple of -the break_key. So the text after decrypting might be a little different than -the original text. +This implementation only considers alphanumerics in the text. If the length of +the text to be encrypted is not a multiple of the break key(the length of one +batch of letters), the last character of the text is added to the text until the +length of the text reaches a multiple of the break_key. So the text after +decrypting might be a little different than the original text. References: https://apprendre-en-ligne.net/crypto/hill/Hillciph.pdf @@ -37,75 +35,131 @@ https://www.youtube.com/watch?v=4RhLNDqcjpA """ +import string import numpy -def gcd(a, b): - if a == 0: - return b - return gcd(b%a, a) +def greatest_common_divisor(a: int, b: int) -> int: + """ + >>> greatest_common_divisor(4, 8) + 4 + >>> greatest_common_divisor(8, 4) + 4 + >>> greatest_common_divisor(4, 7) + 1 + >>> greatest_common_divisor(0, 10) + 10 + """ + return b if a == 0 else greatest_common_divisor(b % a, a) class HillCipher: - key_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + key_string = string.ascii_uppercase + string.digits # This cipher takes alphanumerics into account # i.e. a total of 36 characters - replaceLetters = lambda self, letter: self.key_string.index(letter) - replaceNumbers = lambda self, num: self.key_string[round(num)] - # take x and return x % len(key_string) modulus = numpy.vectorize(lambda x: x % 36) - toInt = numpy.vectorize(lambda x: round(x)) - - def __init__(self, encrypt_key): + to_int = numpy.vectorize(lambda x: round(x)) + + def __init__(self, encrypt_key: int): """ - encrypt_key is an NxN numpy matrix + encrypt_key is an NxN numpy array """ - self.encrypt_key = self.modulus(encrypt_key) # mod36 calc's on the encrypt key - self.checkDeterminant() # validate the determinant of the encryption key + self.encrypt_key = self.modulus(encrypt_key) # mod36 calc's on the encrypt key + self.check_determinant() # validate the determinant of the encryption key self.decrypt_key = None self.break_key = encrypt_key.shape[0] - def checkDeterminant(self): + def replace_letters(self, letter: str) -> int: + """ + >>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]])) + >>> hill_cipher.replace_letters('T') + 19 + >>> hill_cipher.replace_letters('0') + 26 + """ + return self.key_string.index(letter) + + def replace_digits(self, num: int) -> str: + """ + >>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]])) + >>> hill_cipher.replace_digits(19) + 'T' + >>> hill_cipher.replace_digits(26) + '0' + """ + return self.key_string[round(num)] + + def check_determinant(self) -> None: + """ + >>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]])) + >>> hill_cipher.check_determinant() + """ det = round(numpy.linalg.det(self.encrypt_key)) - + if det < 0: det = det % len(self.key_string) req_l = len(self.key_string) - if gcd(det, len(self.key_string)) != 1: - raise ValueError("discriminant modular {0} of encryption key({1}) is not co prime w.r.t {2}.\nTry another key.".format(req_l, det, req_l)) + if greatest_common_divisor(det, len(self.key_string)) != 1: + raise ValueError( + f"determinant modular {req_l} of encryption key({det}) is not co prime " + f"w.r.t {req_l}.\nTry another key." + ) - def processText(self, text): - text = list(text.upper()) - text = [char for char in text if char in self.key_string] + def process_text(self, text: str) -> str: + """ + >>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]])) + >>> hill_cipher.process_text('Testing Hill Cipher') + 'TESTINGHILLCIPHERR' + >>> hill_cipher.process_text('hello') + 'HELLOO' + """ + chars = [char for char in text.upper() if char in self.key_string] - last = text[-1] - while len(text) % self.break_key != 0: - text.append(last) + last = chars[-1] + while len(chars) % self.break_key != 0: + chars.append(last) - return ''.join(text) - - def encrypt(self, text): - text = self.processText(text.upper()) - encrypted = '' + return "".join(chars) + + def encrypt(self, text: str) -> str: + """ + >>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]])) + >>> hill_cipher.encrypt('testing hill cipher') + 'WHXYJOLM9C6XT085LL' + >>> hill_cipher.encrypt('hello') + '85FF00' + """ + text = self.process_text(text.upper()) + encrypted = "" for i in range(0, len(text) - self.break_key + 1, self.break_key): - batch = text[i:i+self.break_key] - batch_vec = list(map(self.replaceLetters, batch)) - batch_vec = numpy.matrix([batch_vec]).T - batch_encrypted = self.modulus(self.encrypt_key.dot(batch_vec)).T.tolist()[0] - encrypted_batch = ''.join(list(map(self.replaceNumbers, batch_encrypted))) + batch = text[i : i + self.break_key] + batch_vec = [self.replace_letters(char) for char in batch] + batch_vec = numpy.array([batch_vec]).T + batch_encrypted = self.modulus(self.encrypt_key.dot(batch_vec)).T.tolist()[ + 0 + ] + encrypted_batch = "".join( + self.replace_digits(num) for num in batch_encrypted + ) encrypted += encrypted_batch return encrypted - def makeDecryptKey(self): + def make_decrypt_key(self): + """ + >>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]])) + >>> hill_cipher.make_decrypt_key() + array([[ 6, 25], + [ 5, 26]]) + """ det = round(numpy.linalg.det(self.encrypt_key)) - + if det < 0: det = det % len(self.key_string) det_inv = None @@ -114,22 +168,36 @@ def makeDecryptKey(self): det_inv = i break - inv_key = det_inv * numpy.linalg.det(self.encrypt_key) *\ - numpy.linalg.inv(self.encrypt_key) + inv_key = ( + det_inv + * numpy.linalg.det(self.encrypt_key) + * numpy.linalg.inv(self.encrypt_key) + ) - return self.toInt(self.modulus(inv_key)) - - def decrypt(self, text): - self.decrypt_key = self.makeDecryptKey() - text = self.processText(text.upper()) - decrypted = '' + return self.to_int(self.modulus(inv_key)) + + def decrypt(self, text: str) -> str: + """ + >>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]])) + >>> hill_cipher.decrypt('WHXYJOLM9C6XT085LL') + 'TESTINGHILLCIPHERR' + >>> hill_cipher.decrypt('85FF00') + 'HELLOO' + """ + self.decrypt_key = self.make_decrypt_key() + text = self.process_text(text.upper()) + decrypted = "" for i in range(0, len(text) - self.break_key + 1, self.break_key): - batch = text[i:i+self.break_key] - batch_vec = list(map(self.replaceLetters, batch)) - batch_vec = numpy.matrix([batch_vec]).T - batch_decrypted = self.modulus(self.decrypt_key.dot(batch_vec)).T.tolist()[0] - decrypted_batch = ''.join(list(map(self.replaceNumbers, batch_decrypted))) + batch = text[i : i + self.break_key] + batch_vec = [self.replace_letters(char) for char in batch] + batch_vec = numpy.array([batch_vec]).T + batch_decrypted = self.modulus(self.decrypt_key.dot(batch_vec)).T.tolist()[ + 0 + ] + decrypted_batch = "".join( + self.replace_digits(num) for num in batch_decrypted + ) decrypted += decrypted_batch return decrypted @@ -141,27 +209,26 @@ def main(): print("Enter each row of the encryption key with space separated integers") for i in range(N): - row = list(map(int, input().split())) + row = [int(x) for x in input().split()] hill_matrix.append(row) - hc = HillCipher(numpy.matrix(hill_matrix)) + hc = HillCipher(numpy.array(hill_matrix)) print("Would you like to encrypt or decrypt some text? (1 or 2)") - option = input(""" -1. Encrypt -2. Decrypt -""" - ) - - if option == '1': + option = input("\n1. Encrypt\n2. Decrypt\n") + if option == "1": text_e = input("What text would you like to encrypt?: ") print("Your encrypted text is:") print(hc.encrypt(text_e)) - elif option == '2': + elif option == "2": text_d = input("What text would you like to decrypt?: ") print("Your decrypted text is:") print(hc.decrypt(text_d)) - + if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/ciphers/mixed_keyword_cypher.py b/ciphers/mixed_keyword_cypher.py new file mode 100644 index 000000000000..59298d310ce0 --- /dev/null +++ b/ciphers/mixed_keyword_cypher.py @@ -0,0 +1,68 @@ +def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str: + """ + + For key:hello + + H E L O + A B C D + F G I J + K M N P + Q R S T + U V W X + Y Z + and map vertically + + >>> mixed_keyword("college", "UNIVERSITY") # doctest: +NORMALIZE_WHITESPACE + {'A': 'C', 'B': 'A', 'C': 'I', 'D': 'P', 'E': 'U', 'F': 'Z', 'G': 'O', 'H': 'B', + 'I': 'J', 'J': 'Q', 'K': 'V', 'L': 'L', 'M': 'D', 'N': 'K', 'O': 'R', 'P': 'W', + 'Q': 'E', 'R': 'F', 'S': 'M', 'T': 'S', 'U': 'X', 'V': 'G', 'W': 'H', 'X': 'N', + 'Y': 'T', 'Z': 'Y'} + 'XKJGUFMJST' + """ + key = key.upper() + pt = pt.upper() + temp = [] + for i in key: + if i not in temp: + temp.append(i) + len_temp = len(temp) + # print(temp) + alpha = [] + modalpha = [] + for i in range(65, 91): + t = chr(i) + alpha.append(t) + if t not in temp: + temp.append(t) + # print(temp) + r = int(26 / 4) + # print(r) + k = 0 + for i in range(r): + t = [] + for j in range(len_temp): + t.append(temp[k]) + if not (k < 25): + break + k += 1 + modalpha.append(t) + # print(modalpha) + d = {} + j = 0 + k = 0 + for j in range(len_temp): + for i in modalpha: + if not (len(i) - 1 >= j): + break + d[alpha[k]] = i[j] + if not k < 25: + break + k += 1 + print(d) + cypher = "" + for i in pt: + cypher += d[i] + return cypher + + +print(mixed_keyword("college", "UNIVERSITY")) diff --git a/ciphers/mono_alphabetic_ciphers.py b/ciphers/mono_alphabetic_ciphers.py new file mode 100644 index 000000000000..0a29d6442896 --- /dev/null +++ b/ciphers/mono_alphabetic_ciphers.py @@ -0,0 +1,59 @@ +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + +def translate_message(key, message, mode): + """ + >>> translate_message("QWERTYUIOPASDFGHJKLZXCVBNM","Hello World","encrypt") + 'Pcssi Bidsm' + """ + chars_a = LETTERS if mode == "decrypt" else key + chars_b = key if mode == "decrypt" else LETTERS + translated = "" + # loop through each symbol in the message + for symbol in message: + if symbol.upper() in chars_a: + # encrypt/decrypt the symbol + sym_index = chars_a.find(symbol.upper()) + if symbol.isupper(): + translated += chars_b[sym_index].upper() + else: + translated += chars_b[sym_index].lower() + else: + # symbol is not in LETTERS, just add it + translated += symbol + return translated + + +def encrypt_message(key: str, message: str) -> str: + """ + >>> encrypt_message("QWERTYUIOPASDFGHJKLZXCVBNM", "Hello World") + 'Pcssi Bidsm' + """ + return translate_message(key, message, "encrypt") + + +def decrypt_message(key: str, message: str) -> str: + """ + >>> decrypt_message("QWERTYUIOPASDFGHJKLZXCVBNM", "Hello World") + 'Itssg Vgksr' + """ + return translate_message(key, message, "decrypt") + + +def main(): + message = "Hello World" + key = "QWERTYUIOPASDFGHJKLZXCVBNM" + mode = "decrypt" # set to 'encrypt' or 'decrypt' + + if mode == "encrypt": + translated = encrypt_message(key, message) + elif mode == "decrypt": + translated = decrypt_message(key, message) + print(f"Using the key {key}, the {mode}ed message is: {translated}") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/ciphers/morse_code_implementation.py b/ciphers/morse_code_implementation.py new file mode 100644 index 000000000000..1cce2ef8b386 --- /dev/null +++ b/ciphers/morse_code_implementation.py @@ -0,0 +1,97 @@ +# Python program to implement Morse Code Translator + +# Dictionary representing the morse code chart +MORSE_CODE_DICT = { + "A": ".-", + "B": "-...", + "C": "-.-.", + "D": "-..", + "E": ".", + "F": "..-.", + "G": "--.", + "H": "....", + "I": "..", + "J": ".---", + "K": "-.-", + "L": ".-..", + "M": "--", + "N": "-.", + "O": "---", + "P": ".--.", + "Q": "--.-", + "R": ".-.", + "S": "...", + "T": "-", + "U": "..-", + "V": "...-", + "W": ".--", + "X": "-..-", + "Y": "-.--", + "Z": "--..", + "1": ".----", + "2": "..---", + "3": "...--", + "4": "....-", + "5": ".....", + "6": "-....", + "7": "--...", + "8": "---..", + "9": "----.", + "0": "-----", + "&": ".-...", + "@": ".--.-.", + ":": "---...", + ",": "--..--", + ".": ".-.-.-", + "'": ".----.", + '"': ".-..-.", + "?": "..--..", + "/": "-..-.", + "=": "-...-", + "+": ".-.-.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", + # Exclamation mark is not in ITU-R recommendation + "!": "-.-.--", +} + + +def encrypt(message: str) -> str: + cipher = "" + for letter in message: + if letter != " ": + cipher += MORSE_CODE_DICT[letter] + " " + else: + cipher += "/ " + + # Remove trailing space added on line 64 + return cipher[:-1] + + +def decrypt(message: str) -> str: + decipher = "" + letters = message.split(" ") + for letter in letters: + if letter != "/": + decipher += list(MORSE_CODE_DICT.keys())[ + list(MORSE_CODE_DICT.values()).index(letter) + ] + else: + decipher += " " + + return decipher + + +def main(): + message = "Morse code here" + result = encrypt(message.upper()) + print(result) + + message = result + result = decrypt(message) + print(result) + + +if __name__ == "__main__": + main() diff --git a/ciphers/onepad_cipher.py b/ciphers/onepad_cipher.py index 6afbd45249ec..a91f2b4d31c5 100644 --- a/ciphers/onepad_cipher.py +++ b/ciphers/onepad_cipher.py @@ -1,32 +1,30 @@ -from __future__ import print_function - import random class Onepad: - def encrypt(self, text): - '''Function to encrypt text using psedo-random numbers''' + def encrypt(self, text: str) -> ([str], [int]): + """Function to encrypt text using pseudo-random numbers""" plain = [ord(i) for i in text] key = [] cipher = [] for i in plain: k = random.randint(1, 300) - c = (i+k)*k + c = (i + k) * k cipher.append(c) key.append(k) return cipher, key - - def decrypt(self, cipher, key): - '''Function to decrypt text using psedo-random numbers.''' + + def decrypt(self, cipher: [str], key: [int]) -> str: + """Function to decrypt text using pseudo-random numbers.""" plain = [] for i in range(len(key)): - p = int((cipher[i]-(key[i])**2)/key[i]) + p = int((cipher[i] - (key[i]) ** 2) / key[i]) plain.append(chr(p)) - plain = ''.join([i for i in plain]) + plain = "".join([i for i in plain]) return plain -if __name__ == '__main__': - c, k = Onepad().encrypt('Hello') +if __name__ == "__main__": + c, k = Onepad().encrypt("Hello") print(c, k) print(Onepad().decrypt(c, k)) diff --git a/ciphers/playfair_cipher.py b/ciphers/playfair_cipher.py index 20449b161963..219437448e53 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -1,47 +1,48 @@ -import string import itertools +import string + def chunker(seq, size): it = iter(seq) while True: - chunk = tuple(itertools.islice(it, size)) - if not chunk: - return - yield chunk + chunk = tuple(itertools.islice(it, size)) + if not chunk: + return + yield chunk - -def prepare_input(dirty): +def prepare_input(dirty: str) -> str: """ Prepare the plaintext by up-casing it and separating repeated letters with X's """ - - dirty = ''.join([c.upper() for c in dirty if c in string.ascii_letters]) + + dirty = "".join([c.upper() for c in dirty if c in string.ascii_letters]) clean = "" - + if len(dirty) < 2: return dirty - for i in range(len(dirty)-1): + for i in range(len(dirty) - 1): clean += dirty[i] - - if dirty[i] == dirty[i+1]: - clean += 'X' - + + if dirty[i] == dirty[i + 1]: + clean += "X" + clean += dirty[-1] if len(clean) & 1: - clean += 'X' + clean += "X" return clean -def generate_table(key): + +def generate_table(key: str) -> [str]: # I and J are used interchangeably to allow # us to use a 5x5 table (25 letters) alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ" - # we're using a list instead of a '2d' array because it makes the math + # we're using a list instead of a '2d' array because it makes the math # for setting up the table and doing the actual encoding/decoding simpler table = [] @@ -57,7 +58,8 @@ def generate_table(key): return table -def encode(plaintext, key): + +def encode(plaintext: str, key: str) -> str: table = generate_table(key) plaintext = prepare_input(plaintext) ciphertext = "" @@ -68,19 +70,19 @@ def encode(plaintext, key): row2, col2 = divmod(table.index(char2), 5) if row1 == row2: - ciphertext += table[row1*5+(col1+1)%5] - ciphertext += table[row2*5+(col2+1)%5] + ciphertext += table[row1 * 5 + (col1 + 1) % 5] + ciphertext += table[row2 * 5 + (col2 + 1) % 5] elif col1 == col2: - ciphertext += table[((row1+1)%5)*5+col1] - ciphertext += table[((row2+1)%5)*5+col2] - else: # rectangle - ciphertext += table[row1*5+col2] - ciphertext += table[row2*5+col1] + ciphertext += table[((row1 + 1) % 5) * 5 + col1] + ciphertext += table[((row2 + 1) % 5) * 5 + col2] + else: # rectangle + ciphertext += table[row1 * 5 + col2] + ciphertext += table[row2 * 5 + col1] return ciphertext -def decode(ciphertext, key): +def decode(ciphertext: str, key: str) -> str: table = generate_table(key) plaintext = "" @@ -90,13 +92,13 @@ def decode(ciphertext, key): row2, col2 = divmod(table.index(char2), 5) if row1 == row2: - plaintext += table[row1*5+(col1-1)%5] - plaintext += table[row2*5+(col2-1)%5] + plaintext += table[row1 * 5 + (col1 - 1) % 5] + plaintext += table[row2 * 5 + (col2 - 1) % 5] elif col1 == col2: - plaintext += table[((row1-1)%5)*5+col1] - plaintext += table[((row2-1)%5)*5+col2] - else: # rectangle - plaintext += table[row1*5+col2] - plaintext += table[row2*5+col1] + plaintext += table[((row1 - 1) % 5) * 5 + col1] + plaintext += table[((row2 - 1) % 5) * 5 + col2] + else: # rectangle + plaintext += table[row1 * 5 + col2] + plaintext += table[row2 * 5 + col1] return plaintext diff --git a/ciphers/porta_cipher.py b/ciphers/porta_cipher.py new file mode 100644 index 000000000000..29043c4c9fac --- /dev/null +++ b/ciphers/porta_cipher.py @@ -0,0 +1,110 @@ +alphabet = { + "A": ("ABCDEFGHIJKLM", "NOPQRSTUVWXYZ"), + "B": ("ABCDEFGHIJKLM", "NOPQRSTUVWXYZ"), + "C": ("ABCDEFGHIJKLM", "ZNOPQRSTUVWXY"), + "D": ("ABCDEFGHIJKLM", "ZNOPQRSTUVWXY"), + "E": ("ABCDEFGHIJKLM", "YZNOPQRSTUVWX"), + "F": ("ABCDEFGHIJKLM", "YZNOPQRSTUVWX"), + "G": ("ABCDEFGHIJKLM", "XYZNOPQRSTUVW"), + "H": ("ABCDEFGHIJKLM", "XYZNOPQRSTUVW"), + "I": ("ABCDEFGHIJKLM", "WXYZNOPQRSTUV"), + "J": ("ABCDEFGHIJKLM", "WXYZNOPQRSTUV"), + "K": ("ABCDEFGHIJKLM", "VWXYZNOPQRSTU"), + "L": ("ABCDEFGHIJKLM", "VWXYZNOPQRSTU"), + "M": ("ABCDEFGHIJKLM", "UVWXYZNOPQRST"), + "N": ("ABCDEFGHIJKLM", "UVWXYZNOPQRST"), + "O": ("ABCDEFGHIJKLM", "TUVWXYZNOPQRS"), + "P": ("ABCDEFGHIJKLM", "TUVWXYZNOPQRS"), + "Q": ("ABCDEFGHIJKLM", "STUVWXYZNOPQR"), + "R": ("ABCDEFGHIJKLM", "STUVWXYZNOPQR"), + "S": ("ABCDEFGHIJKLM", "RSTUVWXYZNOPQ"), + "T": ("ABCDEFGHIJKLM", "RSTUVWXYZNOPQ"), + "U": ("ABCDEFGHIJKLM", "QRSTUVWXYZNOP"), + "V": ("ABCDEFGHIJKLM", "QRSTUVWXYZNOP"), + "W": ("ABCDEFGHIJKLM", "PQRSTUVWXYZNO"), + "X": ("ABCDEFGHIJKLM", "PQRSTUVWXYZNO"), + "Y": ("ABCDEFGHIJKLM", "OPQRSTUVWXYZN"), + "Z": ("ABCDEFGHIJKLM", "OPQRSTUVWXYZN"), +} + + +def generate_table(key: str) -> [(str, str)]: + """ + >>> generate_table('marvin') # doctest: +NORMALIZE_WHITESPACE + [('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'), + ('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'), + ('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')] + """ + return [alphabet[char] for char in key.upper()] + + +def encrypt(key: str, words: str) -> str: + """ + >>> encrypt('marvin', 'jessica') + 'QRACRWU' + """ + cipher = "" + count = 0 + table = generate_table(key) + for char in words.upper(): + cipher += get_opponent(table[count], char) + count = (count + 1) % len(table) + return cipher + + +def decrypt(key: str, words: str) -> str: + """ + >>> decrypt('marvin', 'QRACRWU') + 'JESSICA' + """ + return encrypt(key, words) + + +def get_position(table: [(str, str)], char: str) -> (int, int) or (None, None): + """ + >>> table = [ + ... ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'), + ... ('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'), + ... ('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')] + >>> get_position(table, 'A') + (None, None) + """ + if char in table[0]: + row = 0 + else: + row = 1 if char in table[1] else -1 + return (None, None) if row == -1 else (row, table[row].index(char)) + + +def get_opponent(table: [(str, str)], char: str) -> str: + """ + >>> table = [ + ... ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST'), ('ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ'), + ... ('ABCDEFGHIJKLM', 'STUVWXYZNOPQR'), ('ABCDEFGHIJKLM', 'QRSTUVWXYZNOP'), + ... ('ABCDEFGHIJKLM', 'WXYZNOPQRSTUV'), ('ABCDEFGHIJKLM', 'UVWXYZNOPQRST')] + >>> get_opponent(table, 'A') + 'A' + """ + row, col = get_position(table, char.upper()) + if row == 1: + return table[0][col] + else: + return table[1][col] if row == 0 else char + + +if __name__ == "__main__": + import doctest + + doctest.testmod() # Fist ensure that all our tests are passing... + """ + ENTER KEY: marvin + ENTER TEXT TO ENCRYPT: jessica + ENCRYPTED: QRACRWU + DECRYPTED WITH KEY: JESSICA + """ + key = input("ENTER KEY: ").strip() + text = input("ENTER TEXT TO ENCRYPT: ").strip() + cipher_text = encrypt(key, text) + + print(f"ENCRYPTED: {cipher_text}") + print(f"DECRYPTED WITH KEY: {decrypt(key, cipher_text)}") diff --git a/ciphers/prehistoric_men.txt b/ciphers/prehistoric_men.txt index 86c4de821bfc..a58e533a8405 100644 --- a/ciphers/prehistoric_men.txt +++ b/ciphers/prehistoric_men.txt @@ -3,9 +3,9 @@ Braidwood, Illustrated by Susan T. Richert This eBook is for the use of anyone anywhere in the United States and most -other parts of the world at no cost and with almost no restrictions +other parts of the world at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of -the Project Gutenberg License included with this eBook or online at +the Project Gutenberg License included with this eBook or online at www.gutenberg.org. If you are not located in the United States, you'll have to check the laws of the country where you are located before using this ebook. @@ -7109,9 +7109,9 @@ and permanent future for Project Gutenberg-tm and future generations. To learn more about the Project Gutenberg Literary Archive Foundation and how your efforts and donations can help, see Sections 3 and 4 and the Foundation information page at -www.gutenberg.org +www.gutenberg.org -Section 3. Information about the Project Gutenberg Literary +Section 3. Information about the Project Gutenberg Literary Archive Foundation The Project Gutenberg Literary Archive Foundation is a non profit diff --git a/ciphers/rabin_miller.py b/ciphers/rabin_miller.py index f71fb03c0051..65c162984ece 100644 --- a/ciphers/rabin_miller.py +++ b/ciphers/rabin_miller.py @@ -1,9 +1,9 @@ -from __future__ import print_function # Primality Testing with the Rabin-Miller Algorithm import random -def rabinMiller(num): + +def rabinMiller(num: int) -> bool: s = num - 1 t = 0 @@ -24,24 +24,181 @@ def rabinMiller(num): v = (v ** 2) % num return True -def isPrime(num): - if (num < 2): + +def isPrime(num: int) -> bool: + if num < 2: return False - lowPrimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, - 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, - 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, - 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, - 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, - 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, - 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, - 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, - 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, - 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, - 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, - 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, - 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, - 971, 977, 983, 991, 997] + lowPrimes = [ + 2, + 3, + 5, + 7, + 11, + 13, + 17, + 19, + 23, + 29, + 31, + 37, + 41, + 43, + 47, + 53, + 59, + 61, + 67, + 71, + 73, + 79, + 83, + 89, + 97, + 101, + 103, + 107, + 109, + 113, + 127, + 131, + 137, + 139, + 149, + 151, + 157, + 163, + 167, + 173, + 179, + 181, + 191, + 193, + 197, + 199, + 211, + 223, + 227, + 229, + 233, + 239, + 241, + 251, + 257, + 263, + 269, + 271, + 277, + 281, + 283, + 293, + 307, + 311, + 313, + 317, + 331, + 337, + 347, + 349, + 353, + 359, + 367, + 373, + 379, + 383, + 389, + 397, + 401, + 409, + 419, + 421, + 431, + 433, + 439, + 443, + 449, + 457, + 461, + 463, + 467, + 479, + 487, + 491, + 499, + 503, + 509, + 521, + 523, + 541, + 547, + 557, + 563, + 569, + 571, + 577, + 587, + 593, + 599, + 601, + 607, + 613, + 617, + 619, + 631, + 641, + 643, + 647, + 653, + 659, + 661, + 673, + 677, + 683, + 691, + 701, + 709, + 719, + 727, + 733, + 739, + 743, + 751, + 757, + 761, + 769, + 773, + 787, + 797, + 809, + 811, + 821, + 823, + 827, + 829, + 839, + 853, + 857, + 859, + 863, + 877, + 881, + 883, + 887, + 907, + 911, + 919, + 929, + 937, + 941, + 947, + 953, + 967, + 971, + 977, + 983, + 991, + 997, + ] if num in lowPrimes: return True @@ -52,13 +209,15 @@ def isPrime(num): return rabinMiller(num) -def generateLargePrime(keysize = 1024): + +def generateLargePrime(keysize: int = 1024) -> int: while True: num = random.randrange(2 ** (keysize - 1), 2 ** (keysize)) if isPrime(num): return num -if __name__ == '__main__': + +if __name__ == "__main__": num = generateLargePrime() - print(('Prime number:', num)) - print(('isPrime:', isPrime(num))) + print(("Prime number:", num)) + print(("isPrime:", isPrime(num))) diff --git a/ciphers/rail_fence_cipher.py b/ciphers/rail_fence_cipher.py new file mode 100644 index 000000000000..2596415207ae --- /dev/null +++ b/ciphers/rail_fence_cipher.py @@ -0,0 +1,102 @@ +""" https://en.wikipedia.org/wiki/Rail_fence_cipher """ + + +def encrypt(input_string: str, key: int) -> str: + """ + Shuffles the character of a string by placing each of them + in a grid (the height is dependent on the key) in a zigzag + formation and reading it left to right. + + >>> encrypt("Hello World", 4) + 'HWe olordll' + + >>> encrypt("This is a message", 0) + Traceback (most recent call last): + ... + ValueError: Height of grid can't be 0 or negative + + >>> encrypt(b"This is a byte string", 5) + Traceback (most recent call last): + ... + TypeError: sequence item 0: expected str instance, int found + """ + grid = [[] for _ in range(key)] + lowest = key - 1 + + if key <= 0: + raise ValueError("Height of grid can't be 0 or negative") + if key == 1 or len(input_string) <= key: + return input_string + + for position, character in enumerate(input_string): + num = position % (lowest * 2) # puts it in bounds + num = min(num, lowest * 2 - num) # creates zigzag pattern + grid[num].append(character) + grid = ["".join(row) for row in grid] + output_string = "".join(grid) + + return output_string + + +def decrypt(input_string: str, key: int) -> str: + """ + Generates a template based on the key and fills it in with + the characters of the input string and then reading it in + a zigzag formation. + + >>> decrypt("HWe olordll", 4) + 'Hello World' + + >>> decrypt("This is a message", -10) + Traceback (most recent call last): + ... + ValueError: Height of grid can't be 0 or negative + + >>> decrypt("My key is very big", 100) + 'My key is very big' + """ + grid = [] + lowest = key - 1 + + if key <= 0: + raise ValueError("Height of grid can't be 0 or negative") + if key == 1: + return input_string + + temp_grid = [[] for _ in range(key)] # generates template + for position in range(len(input_string)): + num = position % (lowest * 2) # puts it in bounds + num = min(num, lowest * 2 - num) # creates zigzag pattern + temp_grid[num].append("*") + + counter = 0 + for row in temp_grid: # fills in the characters + splice = input_string[counter : counter + len(row)] + grid.append([character for character in splice]) + counter += len(row) + + output_string = "" # reads as zigzag + for position in range(len(input_string)): + num = position % (lowest * 2) # puts it in bounds + num = min(num, lowest * 2 - num) # creates zigzag pattern + output_string += grid[num][0] + grid[num].pop(0) + return output_string + + +def bruteforce(input_string: str) -> dict: + """Uses decrypt function by guessing every key + + >>> bruteforce("HWe olordll")[4] + 'Hello World' + """ + results = {} + for key_guess in range(1, len(input_string)): # tries every key + results[key_guess] = decrypt(input_string, key_guess) + return results + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/ciphers/rot13.py b/ciphers/rot13.py index 2abf981e9d7d..21dbda98eecc 100644 --- a/ciphers/rot13.py +++ b/ciphers/rot13.py @@ -1,25 +1,37 @@ -from __future__ import print_function -def dencrypt(s, n): - out = '' +def dencrypt(s: str, n: int = 13) -> str: + """ + https://en.wikipedia.org/wiki/ROT13 + + >>> msg = "My secret bank account number is 173-52946 so don't tell anyone!!" + >>> s = dencrypt(msg) + >>> s + "Zl frperg onax nppbhag ahzore vf 173-52946 fb qba'g gryy nalbar!!" + >>> dencrypt(s) == msg + True + """ + out = "" for c in s: - if c >= 'A' and c <= 'Z': - out += chr(ord('A') + (ord(c) - ord('A') + n) % 26) - elif c >= 'a' and c <= 'z': - out += chr(ord('a') + (ord(c) - ord('a') + n) % 26) + if "A" <= c <= "Z": + out += chr(ord("A") + (ord(c) - ord("A") + n) % 26) + elif "a" <= c <= "z": + out += chr(ord("a") + (ord(c) - ord("a") + n) % 26) else: out += c return out def main(): - s0 = 'HELLO' + s0 = input("Enter message: ") s1 = dencrypt(s0, 13) - print(s1) # URYYB + print("Encryption:", s1) s2 = dencrypt(s1, 13) - print(s2) # HELLO + print("Decryption: ", s2) + +if __name__ == "__main__": + import doctest -if __name__ == '__main__': + doctest.testmod() main() diff --git a/ciphers/rsa_cipher.py b/ciphers/rsa_cipher.py index d81f1ffc1a1e..57c916a44d4b 100644 --- a/ciphers/rsa_cipher.py +++ b/ciphers/rsa_cipher.py @@ -1,44 +1,47 @@ -from __future__ import print_function -import sys, rsa_key_generator as rkg, os +import os +import sys + +from . import rsa_key_generator as rkg DEFAULT_BLOCK_SIZE = 128 BYTE_SIZE = 256 + def main(): - filename = 'encrypted_file.txt' - response = input(r'Encrypte\Decrypt [e\d]: ') - - if response.lower().startswith('e'): - mode = 'encrypt' - elif response.lower().startswith('d'): - mode = 'decrypt' - - if mode == 'encrypt': - if not os.path.exists('rsa_pubkey.txt'): - rkg.makeKeyFiles('rsa', 1024) - - message = input('\nEnter message: ') - pubKeyFilename = 'rsa_pubkey.txt' - print('Encrypting and writing to %s...' % (filename)) + filename = "encrypted_file.txt" + response = input(r"Encrypt\Decrypt [e\d]: ") + + if response.lower().startswith("e"): + mode = "encrypt" + elif response.lower().startswith("d"): + mode = "decrypt" + + if mode == "encrypt": + if not os.path.exists("rsa_pubkey.txt"): + rkg.makeKeyFiles("rsa", 1024) + + message = input("\nEnter message: ") + pubKeyFilename = "rsa_pubkey.txt" + print("Encrypting and writing to %s..." % (filename)) encryptedText = encryptAndWriteToFile(filename, pubKeyFilename, message) - print('\nEncrypted text:') + print("\nEncrypted text:") print(encryptedText) - elif mode == 'decrypt': - privKeyFilename = 'rsa_privkey.txt' - print('Reading from %s and decrypting...' % (filename)) + elif mode == "decrypt": + privKeyFilename = "rsa_privkey.txt" + print("Reading from %s and decrypting..." % (filename)) decryptedText = readFromFileAndDecrypt(filename, privKeyFilename) - print('writing decryption to rsa_decryption.txt...') - with open('rsa_decryption.txt', 'w') as dec: + print("writing decryption to rsa_decryption.txt...") + with open("rsa_decryption.txt", "w") as dec: dec.write(decryptedText) - print('\nDecryption:') + print("\nDecryption:") print(decryptedText) -def getBlocksFromText(message, blockSize=DEFAULT_BLOCK_SIZE): - messageBytes = message.encode('ascii') +def getBlocksFromText(message: int, blockSize: int = DEFAULT_BLOCK_SIZE) -> [int]: + messageBytes = message.encode("ascii") blockInts = [] for blockStart in range(0, len(messageBytes), blockSize): @@ -49,7 +52,9 @@ def getBlocksFromText(message, blockSize=DEFAULT_BLOCK_SIZE): return blockInts -def getTextFromBlocks(blockInts, messageLength, blockSize=DEFAULT_BLOCK_SIZE): +def getTextFromBlocks( + blockInts: [int], messageLength: int, blockSize: int = DEFAULT_BLOCK_SIZE +) -> str: message = [] for blockInt in blockInts: blockMessage = [] @@ -59,10 +64,12 @@ def getTextFromBlocks(blockInts, messageLength, blockSize=DEFAULT_BLOCK_SIZE): blockInt = blockInt % (BYTE_SIZE ** i) blockMessage.insert(0, chr(asciiNumber)) message.extend(blockMessage) - return ''.join(message) + return "".join(message) -def encryptMessage(message, key, blockSize=DEFAULT_BLOCK_SIZE): +def encryptMessage( + message: str, key: (int, int), blockSize: int = DEFAULT_BLOCK_SIZE +) -> [int]: encryptedBlocks = [] n, e = key @@ -71,7 +78,12 @@ def encryptMessage(message, key, blockSize=DEFAULT_BLOCK_SIZE): return encryptedBlocks -def decryptMessage(encryptedBlocks, messageLength, key, blockSize=DEFAULT_BLOCK_SIZE): +def decryptMessage( + encryptedBlocks: [int], + messageLength: int, + key: (int, int), + blockSize: int = DEFAULT_BLOCK_SIZE, +) -> str: decryptedBlocks = [] n, d = key for block in encryptedBlocks: @@ -79,45 +91,61 @@ def decryptMessage(encryptedBlocks, messageLength, key, blockSize=DEFAULT_BLOCK_ return getTextFromBlocks(decryptedBlocks, messageLength, blockSize) -def readKeyFile(keyFilename): +def readKeyFile(keyFilename: str) -> (int, int, int): with open(keyFilename) as fo: content = fo.read() - keySize, n, EorD = content.split(',') + keySize, n, EorD = content.split(",") return (int(keySize), int(n), int(EorD)) -def encryptAndWriteToFile(messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE): +def encryptAndWriteToFile( + messageFilename: str, + keyFilename: str, + message: str, + blockSize: int = DEFAULT_BLOCK_SIZE, +) -> str: keySize, n, e = readKeyFile(keyFilename) if keySize < blockSize * 8: - sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Either decrease the block size or use different keys.' % (blockSize * 8, keySize)) + sys.exit( + "ERROR: Block size is %s bits and key size is %s bits. The RSA cipher " + "requires the block size to be equal to or greater than the key size. " + "Either decrease the block size or use different keys." + % (blockSize * 8, keySize) + ) encryptedBlocks = encryptMessage(message, (n, e), blockSize) for i in range(len(encryptedBlocks)): encryptedBlocks[i] = str(encryptedBlocks[i]) - encryptedContent = ','.join(encryptedBlocks) - encryptedContent = '%s_%s_%s' % (len(message), blockSize, encryptedContent) - with open(messageFilename, 'w') as fo: + encryptedContent = ",".join(encryptedBlocks) + encryptedContent = "{}_{}_{}".format(len(message), blockSize, encryptedContent) + with open(messageFilename, "w") as fo: fo.write(encryptedContent) return encryptedContent -def readFromFileAndDecrypt(messageFilename, keyFilename): +def readFromFileAndDecrypt(messageFilename: str, keyFilename: str) -> str: keySize, n, d = readKeyFile(keyFilename) with open(messageFilename) as fo: content = fo.read() - messageLength, blockSize, encryptedMessage = content.split('_') + messageLength, blockSize, encryptedMessage = content.split("_") messageLength = int(messageLength) blockSize = int(blockSize) if keySize < blockSize * 8: - sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Did you specify the correct key file and encrypted file?' % (blockSize * 8, keySize)) + sys.exit( + "ERROR: Block size is %s bits and key size is %s bits. The RSA cipher " + "requires the block size to be equal to or greater than the key size. " + "Did you specify the correct key file and encrypted file?" + % (blockSize * 8, keySize) + ) encryptedBlocks = [] - for block in encryptedMessage.split(','): + for block in encryptedMessage.split(","): encryptedBlocks.append(int(block)) return decryptMessage(encryptedBlocks, messageLength, (n, d), blockSize) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/ciphers/rsa_factorization.py b/ciphers/rsa_factorization.py new file mode 100644 index 000000000000..b18aab609e2d --- /dev/null +++ b/ciphers/rsa_factorization.py @@ -0,0 +1,57 @@ +""" +An RSA prime factor algorithm. + +The program can efficiently factor RSA prime number given the private key d and +public key e. +Source: on page 3 of https://crypto.stanford.edu/~dabo/papers/RSA-survey.pdf +More readable source: https://www.di-mgt.com.au/rsa_factorize_n.html +large number can take minutes to factor, therefore are not included in doctest. +""" +from __future__ import annotations + +import math +import random + + +def rsafactor(d: int, e: int, N: int) -> [int]: + """ + This function returns the factors of N, where p*q=N + Return: [p, q] + + We call N the RSA modulus, e the encryption exponent, and d the decryption exponent. + The pair (N, e) is the public key. As its name suggests, it is public and is used to + encrypt messages. + The pair (N, d) is the secret key or private key and is known only to the recipient + of encrypted messages. + + >>> rsafactor(3, 16971, 25777) + [149, 173] + >>> rsafactor(7331, 11, 27233) + [113, 241] + >>> rsafactor(4021, 13, 17711) + [89, 199] + """ + k = d * e - 1 + p = 0 + q = 0 + while p == 0: + g = random.randint(2, N - 1) + t = k + while True: + if t % 2 == 0: + t = t // 2 + x = (g ** t) % N + y = math.gcd(x - 1, N) + if x > 1 and y > 1: + p = y + q = N // y + break # find the correct factors + else: + break # t is not divisible by 2, break and choose another g + return sorted([p, q]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/ciphers/rsa_key_generator.py b/ciphers/rsa_key_generator.py index 541e90d6e884..5693aa637ee9 100644 --- a/ciphers/rsa_key_generator.py +++ b/ciphers/rsa_key_generator.py @@ -1,46 +1,60 @@ -from __future__ import print_function -import random, sys, os -import rabin_miller as rabinMiller, cryptomath_module as cryptoMath +import os +import random +import sys +from typing import Tuple + +from . import cryptomath_module as cryptoMath +from . import rabin_miller as rabinMiller + def main(): - print('Making key files...') - makeKeyFiles('rsa', 1024) - print('Key files generation successful.') + print("Making key files...") + makeKeyFiles("rsa", 1024) + print("Key files generation successful.") + -def generateKey(keySize): - print('Generating prime p...') +def generateKey(keySize: int) -> Tuple[Tuple[int, int], Tuple[int, int]]: + print("Generating prime p...") p = rabinMiller.generateLargePrime(keySize) - print('Generating prime q...') + print("Generating prime q...") q = rabinMiller.generateLargePrime(keySize) n = p * q - print('Generating e that is relatively prime to (p - 1) * (q - 1)...') + print("Generating e that is relatively prime to (p - 1) * (q - 1)...") while True: e = random.randrange(2 ** (keySize - 1), 2 ** (keySize)) if cryptoMath.gcd(e, (p - 1) * (q - 1)) == 1: break - print('Calculating d that is mod inverse of e...') + print("Calculating d that is mod inverse of e...") d = cryptoMath.findModInverse(e, (p - 1) * (q - 1)) publicKey = (n, e) privateKey = (n, d) return (publicKey, privateKey) -def makeKeyFiles(name, keySize): - if os.path.exists('%s_pubkey.txt' % (name)) or os.path.exists('%s_privkey.txt' % (name)): - print('\nWARNING:') - print('"%s_pubkey.txt" or "%s_privkey.txt" already exists. \nUse a different name or delete these files and re-run this program.' % (name, name)) + +def makeKeyFiles(name: int, keySize: int) -> None: + if os.path.exists("%s_pubkey.txt" % (name)) or os.path.exists( + "%s_privkey.txt" % (name) + ): + print("\nWARNING:") + print( + '"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n' + "Use a different name or delete these files and re-run this program." + % (name, name) + ) sys.exit() publicKey, privateKey = generateKey(keySize) - print('\nWriting public key to file %s_pubkey.txt...' % name) - with open('%s_pubkey.txt' % name, 'w') as fo: - fo.write('%s,%s,%s' % (keySize, publicKey[0], publicKey[1])) + print("\nWriting public key to file %s_pubkey.txt..." % name) + with open("%s_pubkey.txt" % name, "w") as out_file: + out_file.write("{},{},{}".format(keySize, publicKey[0], publicKey[1])) + + print("Writing private key to file %s_privkey.txt..." % name) + with open("%s_privkey.txt" % name, "w") as out_file: + out_file.write("{},{},{}".format(keySize, privateKey[0], privateKey[1])) - print('Writing private key to file %s_privkey.txt...' % name) - with open('%s_privkey.txt' % name, 'w') as fo: - fo.write('%s,%s,%s' % (keySize, privateKey[0], privateKey[1])) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/shuffled_shift_cipher.py b/ciphers/shuffled_shift_cipher.py new file mode 100644 index 000000000000..22628f3c9d9e --- /dev/null +++ b/ciphers/shuffled_shift_cipher.py @@ -0,0 +1,182 @@ +import random +import string + + +class ShuffledShiftCipher: + """ + This algorithm uses the Caesar Cipher algorithm but removes the option to + use brute force to decrypt the message. + + The passcode is a a random password from the selection buffer of + 1. uppercase letters of the English alphabet + 2. lowercase letters of the English alphabet + 3. digits from 0 to 9 + + Using unique characters from the passcode, the normal list of characters, + that can be allowed in the plaintext, is pivoted and shuffled. Refer to docstring + of __make_key_list() to learn more about the shuffling. + + Then, using the passcode, a number is calculated which is used to encrypt the + plaintext message with the normal shift cipher method, only in this case, the + reference, to look back at while decrypting, is shuffled. + + Each cipher object can possess an optional argument as passcode, without which a + new passcode is generated for that object automatically. + cip1 = ShuffledShiftCipher('d4usr9TWxw9wMD') + cip2 = ShuffledShiftCipher() + """ + + def __init__(self, passcode: str = None): + """ + Initializes a cipher object with a passcode as it's entity + Note: No new passcode is generated if user provides a passcode + while creating the object + """ + self.__passcode = passcode or self.__passcode_creator() + self.__key_list = self.__make_key_list() + self.__shift_key = self.__make_shift_key() + + def __str__(self): + """ + :return: passcode of the cipher object + """ + return "Passcode is: " + "".join(self.__passcode) + + def __neg_pos(self, iterlist: list) -> list: + """ + Mutates the list by changing the sign of each alternate element + + :param iterlist: takes a list iterable + :return: the mutated list + + """ + for i in range(1, len(iterlist), 2): + iterlist[i] *= -1 + return iterlist + + def __passcode_creator(self) -> list: + """ + Creates a random password from the selection buffer of + 1. uppercase letters of the English alphabet + 2. lowercase letters of the English alphabet + 3. digits from 0 to 9 + + :rtype: list + :return: a password of a random length between 10 to 20 + """ + choices = string.ascii_letters + string.digits + password = [random.choice(choices) for i in range(random.randint(10, 20))] + return password + + def __make_key_list(self) -> list: + """ + Shuffles the ordered character choices by pivoting at breakpoints + Breakpoints are the set of characters in the passcode + + eg: + if, ABCDEFGHIJKLMNOPQRSTUVWXYZ are the possible characters + and CAMERA is the passcode + then, breakpoints = [A,C,E,M,R] # sorted set of characters from passcode + shuffled parts: [A,CB,ED,MLKJIHGF,RQPON,ZYXWVUTS] + shuffled __key_list : ACBEDMLKJIHGFRQPONZYXWVUTS + + Shuffling only 26 letters of the english alphabet can generate 26! + combinations for the shuffled list. In the program we consider, a set of + 97 characters (including letters, digits, punctuation and whitespaces), + thereby creating a possibility of 97! combinations (which is a 152 digit number + in itself), thus diminishing the possibility of a brute force approach. + Moreover, shift keys even introduce a multiple of 26 for a brute force approach + for each of the already 97! combinations. + """ + # key_list_options contain nearly all printable except few elements from + # string.whitespace + key_list_options = ( + string.ascii_letters + string.digits + string.punctuation + " \t\n" + ) + + keys_l = [] + + # creates points known as breakpoints to break the key_list_options at those + # points and pivot each substring + breakpoints = sorted(set(self.__passcode)) + temp_list = [] + + # algorithm for creating a new shuffled list, keys_l, out of key_list_options + for i in key_list_options: + temp_list.extend(i) + + # checking breakpoints at which to pivot temporary sublist and add it into + # keys_l + if i in breakpoints or i == key_list_options[-1]: + keys_l.extend(temp_list[::-1]) + temp_list = [] + + # returning a shuffled keys_l to prevent brute force guessing of shift key + return keys_l + + def __make_shift_key(self) -> int: + """ + sum() of the mutated list of ascii values of all characters where the + mutated list is the one returned by __neg_pos() + """ + num = sum(self.__neg_pos([ord(x) for x in self.__passcode])) + return num if num > 0 else len(self.__passcode) + + def decrypt(self, encoded_message: str) -> str: + """ + Performs shifting of the encoded_message w.r.t. the shuffled __key_list + to create the decoded_message + + >>> ssc = ShuffledShiftCipher('4PYIXyqeQZr44') + >>> ssc.decrypt("d>**-1z6&'5z'5z:z+-='$'>=zp:>5:#z<'.&>#") + 'Hello, this is a modified Caesar cipher' + + """ + decoded_message = "" + + # decoding shift like Caesar cipher algorithm implementing negative shift or + # reverse shift or left shift + for i in encoded_message: + position = self.__key_list.index(i) + decoded_message += self.__key_list[ + (position - self.__shift_key) % -len(self.__key_list) + ] + + return decoded_message + + def encrypt(self, plaintext: str) -> str: + """ + Performs shifting of the plaintext w.r.t. the shuffled __key_list + to create the encoded_message + + >>> ssc = ShuffledShiftCipher('4PYIXyqeQZr44') + >>> ssc.encrypt('Hello, this is a modified Caesar cipher') + "d>**-1z6&'5z'5z:z+-='$'>=zp:>5:#z<'.&>#" + + """ + encoded_message = "" + + # encoding shift like Caesar cipher algorithm implementing positive shift or + # forward shift or right shift + for i in plaintext: + position = self.__key_list.index(i) + encoded_message += self.__key_list[ + (position + self.__shift_key) % len(self.__key_list) + ] + + return encoded_message + + +def test_end_to_end(msg: str = "Hello, this is a modified Caesar cipher"): + """ + >>> test_end_to_end() + 'Hello, this is a modified Caesar cipher' + """ + cip1 = ShuffledShiftCipher() + return cip1.decrypt(cip1.encrypt(msg)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/ciphers/simple_keyword_cypher.py b/ciphers/simple_keyword_cypher.py new file mode 100644 index 000000000000..71c3083e9dfc --- /dev/null +++ b/ciphers/simple_keyword_cypher.py @@ -0,0 +1,90 @@ +def remove_duplicates(key: str) -> str: + """ + Removes duplicate alphabetic characters in a keyword (letter is ignored after its + first appearance). + :param key: Keyword to use + :return: String with duplicates removed + >>> remove_duplicates('Hello World!!') + 'Helo Wrd' + """ + + key_no_dups = "" + for ch in key: + if ch == " " or ch not in key_no_dups and ch.isalpha(): + key_no_dups += ch + return key_no_dups + + +def create_cipher_map(key: str) -> dict: + """ + Returns a cipher map given a keyword. + :param key: keyword to use + :return: dictionary cipher map + """ + # Create alphabet list + alphabet = [chr(i + 65) for i in range(26)] + # Remove duplicate characters from key + key = remove_duplicates(key.upper()) + offset = len(key) + # First fill cipher with key characters + cipher_alphabet = {alphabet[i]: char for i, char in enumerate(key)} + # Then map remaining characters in alphabet to + # the alphabet from the beginning + for i in range(len(cipher_alphabet), 26): + char = alphabet[i - offset] + # Ensure we are not mapping letters to letters previously mapped + while char in key: + offset -= 1 + char = alphabet[i - offset] + cipher_alphabet[alphabet[i]] = char + return cipher_alphabet + + +def encipher(message: str, cipher_map: dict) -> str: + """ + Enciphers a message given a cipher map. + :param message: Message to encipher + :param cipher_map: Cipher map + :return: enciphered string + >>> encipher('Hello World!!', create_cipher_map('Goodbye!!')) + 'CYJJM VMQJB!!' + """ + return "".join(cipher_map.get(ch, ch) for ch in message.upper()) + + +def decipher(message: str, cipher_map: dict) -> str: + """ + Deciphers a message given a cipher map + :param message: Message to decipher + :param cipher_map: Dictionary mapping to use + :return: Deciphered string + >>> cipher_map = create_cipher_map('Goodbye!!') + >>> decipher(encipher('Hello World!!', cipher_map), cipher_map) + 'HELLO WORLD!!' + """ + # Reverse our cipher mappings + rev_cipher_map = {v: k for k, v in cipher_map.items()} + return "".join(rev_cipher_map.get(ch, ch) for ch in message.upper()) + + +def main(): + """ + Handles I/O + :return: void + """ + message = input("Enter message to encode or decode: ").strip() + key = input("Enter keyword: ").strip() + option = input("Encipher or decipher? E/D:").strip()[0].lower() + try: + func = {"e": encipher, "d": decipher}[option] + except KeyError: + raise KeyError("invalid input option") + cipher_map = create_cipher_map(key) + print(func(message, cipher_map)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/ciphers/simple_substitution_cipher.py b/ciphers/simple_substitution_cipher.py index 1bdd7dc04a57..646ea449fc06 100644 --- a/ciphers/simple_substitution_cipher.py +++ b/ciphers/simple_substitution_cipher.py @@ -1,55 +1,60 @@ -from __future__ import print_function -import sys, random +import random +import sys + +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" -LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def main(): - message = input('Enter message: ') - key = 'LFWOAYUISVKMNXPBDCRJTQEGHZ' - resp = input('Encrypt/Decrypt [e/d]: ') + message = input("Enter message: ") + key = "LFWOAYUISVKMNXPBDCRJTQEGHZ" + resp = input("Encrypt/Decrypt [e/d]: ") checkValidKey(key) - if resp.lower().startswith('e'): - mode = 'encrypt' + if resp.lower().startswith("e"): + mode = "encrypt" translated = encryptMessage(key, message) - elif resp.lower().startswith('d'): - mode = 'decrypt' + elif resp.lower().startswith("d"): + mode = "decrypt" translated = decryptMessage(key, message) - print('\n%sion: \n%s' % (mode.title(), translated)) - -def checkValidKey(key): + print(f"\n{mode.title()}ion: \n{translated}") + + +def checkValidKey(key: str) -> None: keyList = list(key) lettersList = list(LETTERS) keyList.sort() lettersList.sort() if keyList != lettersList: - sys.exit('Error in the key or symbol set.') + sys.exit("Error in the key or symbol set.") -def encryptMessage(key, message): + +def encryptMessage(key: str, message: str) -> str: """ >>> encryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Harshil Darji') 'Ilcrism Olcvs' """ - return translateMessage(key, message, 'encrypt') + return translateMessage(key, message, "encrypt") + -def decryptMessage(key, message): +def decryptMessage(key: str, message: str) -> str: """ >>> decryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Ilcrism Olcvs') 'Harshil Darji' """ - return translateMessage(key, message, 'decrypt') + return translateMessage(key, message, "decrypt") -def translateMessage(key, message, mode): - translated = '' + +def translateMessage(key: str, message: str, mode: str) -> str: + translated = "" charsA = LETTERS charsB = key - if mode == 'decrypt': + if mode == "decrypt": charsA, charsB = charsB, charsA - + for symbol in message: if symbol.upper() in charsA: symIndex = charsA.find(symbol.upper()) @@ -62,10 +67,12 @@ def translateMessage(key, message, mode): return translated + def getRandomKey(): key = list(LETTERS) random.shuffle(key) - return ''.join(key) + return "".join(key) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/trafid_cipher.py b/ciphers/trafid_cipher.py index 0453272f26a0..328814f97744 100644 --- a/ciphers/trafid_cipher.py +++ b/ciphers/trafid_cipher.py @@ -1,9 +1,10 @@ -#https://en.wikipedia.org/wiki/Trifid_cipher +# https://en.wikipedia.org/wiki/Trifid_cipher -def __encryptPart(messagePart, character2Number): + +def __encryptPart(messagePart: str, character2Number: dict) -> str: one, two, three = "", "", "" tmp = [] - + for character in messagePart: tmp.append(character2Number[character]) @@ -11,10 +12,11 @@ def __encryptPart(messagePart, character2Number): one += each[0] two += each[1] three += each[2] - - return one+two+three -def __decryptPart(messagePart, character2Number): + return one + two + three + + +def __decryptPart(messagePart: str, character2Number: dict) -> (str, str, str): tmp, thisPart = "", "" result = [] @@ -25,62 +27,98 @@ def __decryptPart(messagePart, character2Number): tmp += digit if len(tmp) == len(messagePart): result.append(tmp) - tmp = "" + tmp = "" return result[0], result[1], result[2] -def __prepare(message, alphabet): - #Validate message and alphabet, set to upper and remove spaces + +def __prepare(message: str, alphabet: str) -> (str, str, dict, dict): + # Validate message and alphabet, set to upper and remove spaces alphabet = alphabet.replace(" ", "").upper() message = message.replace(" ", "").upper() - #Check length and characters + # Check length and characters if len(alphabet) != 27: raise KeyError("Length of alphabet has to be 27.") for each in message: if each not in alphabet: raise ValueError("Each message character has to be included in alphabet!") - #Generate dictionares - numbers = ("111","112","113","121","122","123","131","132","133","211","212","213","221","222","223","231","232","233","311","312","313","321","322","323","331","332","333") + # Generate dictionares + numbers = ( + "111", + "112", + "113", + "121", + "122", + "123", + "131", + "132", + "133", + "211", + "212", + "213", + "221", + "222", + "223", + "231", + "232", + "233", + "311", + "312", + "313", + "321", + "322", + "323", + "331", + "332", + "333", + ) character2Number = {} number2Character = {} for letter, number in zip(alphabet, numbers): character2Number[letter] = number number2Character[number] = letter - + return message, alphabet, character2Number, number2Character -def encryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): + +def encryptMessage( + message: str, alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period: int = 5 +) -> str: message, alphabet, character2Number, number2Character = __prepare(message, alphabet) encrypted, encrypted_numeric = "", "" - for i in range(0, len(message)+1, period): - encrypted_numeric += __encryptPart(message[i:i+period], character2Number) - + for i in range(0, len(message) + 1, period): + encrypted_numeric += __encryptPart(message[i : i + period], character2Number) + for i in range(0, len(encrypted_numeric), 3): - encrypted += number2Character[encrypted_numeric[i:i+3]] + encrypted += number2Character[encrypted_numeric[i : i + 3]] return encrypted -def decryptMessage(message, alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): + +def decryptMessage( + message: str, alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period: int = 5 +) -> str: message, alphabet, character2Number, number2Character = __prepare(message, alphabet) decrypted_numeric = [] decrypted = "" - for i in range(0, len(message)+1, period): - a,b,c = __decryptPart(message[i:i+period], character2Number) - + for i in range(0, len(message) + 1, period): + a, b, c = __decryptPart(message[i : i + period], character2Number) + for j in range(0, len(a)): - decrypted_numeric.append(a[j]+b[j]+c[j]) + decrypted_numeric.append(a[j] + b[j] + c[j]) for each in decrypted_numeric: decrypted += number2Character[each] return decrypted -if __name__ == '__main__': + +if __name__ == "__main__": msg = "DEFEND THE EAST WALL OF THE CASTLE." - encrypted = encryptMessage(msg,"EPSDUCVWYM.ZLKXNBTFGORIJHAQ") + encrypted = encryptMessage(msg, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") - print ("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted)) \ No newline at end of file + print(f"Encrypted: {encrypted}\nDecrypted: {decrypted}") diff --git a/ciphers/transposition_cipher.py b/ciphers/transposition_cipher.py index dbb358315d22..6a0a22d3e31d 100644 --- a/ciphers/transposition_cipher.py +++ b/ciphers/transposition_cipher.py @@ -1,33 +1,42 @@ -from __future__ import print_function import math +""" +In cryptography, the TRANSPOSITION cipher is a method of encryption where the +positions of plaintext are shifted a certain number(determined by the key) that +follows a regular system that results in the permuted text, known as the encrypted +text. The type of transposition cipher demonstrated under is the ROUTE cipher. +""" + + def main(): - message = input('Enter message: ') - key = int(input('Enter key [2-%s]: ' % (len(message) - 1))) - mode = input('Encryption/Decryption [e/d]: ') + message = input("Enter message: ") + key = int(input("Enter key [2-%s]: " % (len(message) - 1))) + mode = input("Encryption/Decryption [e/d]: ") - if mode.lower().startswith('e'): + if mode.lower().startswith("e"): text = encryptMessage(key, message) - elif mode.lower().startswith('d'): + elif mode.lower().startswith("d"): text = decryptMessage(key, message) # Append pipe symbol (vertical bar) to identify spaces at the end. - print('Output:\n%s' %(text + '|')) + print("Output:\n%s" % (text + "|")) + -def encryptMessage(key, message): +def encryptMessage(key: int, message: str) -> str: """ >>> encryptMessage(6, 'Harshil Darji') 'Hlia rDsahrij' """ - cipherText = [''] * key + cipherText = [""] * key for col in range(key): pointer = col while pointer < len(message): cipherText[col] += message[pointer] pointer += key - return ''.join(cipherText) + return "".join(cipherText) -def decryptMessage(key, message): + +def decryptMessage(key: int, message: str) -> str: """ >>> decryptMessage(6, 'Hlia rDsahrij') 'Harshil Darji' @@ -36,19 +45,26 @@ def decryptMessage(key, message): numRows = key numShadedBoxes = (numCols * numRows) - len(message) plainText = [""] * numCols - col = 0; row = 0; + col = 0 + row = 0 for symbol in message: plainText[col] += symbol col += 1 - if (col == numCols) or (col == numCols - 1) and (row >= numRows - numShadedBoxes): + if ( + (col == numCols) + or (col == numCols - 1) + and (row >= numRows - numShadedBoxes) + ): col = 0 row += 1 return "".join(plainText) -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() main() diff --git a/ciphers/transposition_cipher_encrypt_decrypt_file.py b/ciphers/transposition_cipher_encrypt_decrypt_file.py index a186cf81cde7..45aab056109a 100644 --- a/ciphers/transposition_cipher_encrypt_decrypt_file.py +++ b/ciphers/transposition_cipher_encrypt_decrypt_file.py @@ -1,37 +1,41 @@ -from __future__ import print_function -import time, os, sys -import transposition_cipher as transCipher +import os +import sys +import time + +from . import transposition_cipher as transCipher + def main(): - inputFile = 'Prehistoric Men.txt' - outputFile = 'Output.txt' - key = int(input('Enter key: ')) - mode = input('Encrypt/Decrypt [e/d]: ') + inputFile = "Prehistoric Men.txt" + outputFile = "Output.txt" + key = int(input("Enter key: ")) + mode = input("Encrypt/Decrypt [e/d]: ") if not os.path.exists(inputFile): - print('File %s does not exist. Quitting...' % inputFile) + print("File %s does not exist. Quitting..." % inputFile) sys.exit() if os.path.exists(outputFile): - print('Overwrite %s? [y/n]' % outputFile) - response = input('> ') - if not response.lower().startswith('y'): + print("Overwrite %s? [y/n]" % outputFile) + response = input("> ") + if not response.lower().startswith("y"): sys.exit() - + startTime = time.time() - if mode.lower().startswith('e'): + if mode.lower().startswith("e"): with open(inputFile) as f: content = f.read() translated = transCipher.encryptMessage(key, content) - elif mode.lower().startswith('d'): + elif mode.lower().startswith("d"): with open(outputFile) as f: content = f.read() - translated =transCipher .decryptMessage(key, content) + translated = transCipher.decryptMessage(key, content) - with open(outputFile, 'w') as outputObj: + with open(outputFile, "w") as outputObj: outputObj.write(translated) - + totalTime = round(time.time() - startTime, 2) - print(('Done (', totalTime, 'seconds )')) - -if __name__ == '__main__': + print(("Done (", totalTime, "seconds )")) + + +if __name__ == "__main__": main() diff --git a/ciphers/vigenere_cipher.py b/ciphers/vigenere_cipher.py index 5d5be0792835..eb523d078005 100644 --- a/ciphers/vigenere_cipher.py +++ b/ciphers/vigenere_cipher.py @@ -1,36 +1,39 @@ -from __future__ import print_function -LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + def main(): - message = input('Enter message: ') - key = input('Enter key [alphanumeric]: ') - mode = input('Encrypt/Decrypt [e/d]: ') + message = input("Enter message: ") + key = input("Enter key [alphanumeric]: ") + mode = input("Encrypt/Decrypt [e/d]: ") - if mode.lower().startswith('e'): - mode = 'encrypt' + if mode.lower().startswith("e"): + mode = "encrypt" translated = encryptMessage(key, message) - elif mode.lower().startswith('d'): - mode = 'decrypt' + elif mode.lower().startswith("d"): + mode = "decrypt" translated = decryptMessage(key, message) - print('\n%sed message:' % mode.title()) + print("\n%sed message:" % mode.title()) print(translated) -def encryptMessage(key, message): - ''' + +def encryptMessage(key: str, message: str) -> str: + """ >>> encryptMessage('HDarji', 'This is Harshil Darji from Dharmaj.') 'Akij ra Odrjqqs Gaisq muod Mphumrs.' - ''' - return translateMessage(key, message, 'encrypt') + """ + return translateMessage(key, message, "encrypt") + -def decryptMessage(key, message): - ''' +def decryptMessage(key: str, message: str) -> str: + """ >>> decryptMessage('HDarji', 'Akij ra Odrjqqs Gaisq muod Mphumrs.') 'This is Harshil Darji from Dharmaj.' - ''' - return translateMessage(key, message, 'decrypt') + """ + return translateMessage(key, message, "decrypt") -def translateMessage(key, message, mode): + +def translateMessage(key: str, message: str, mode: str) -> str: translated = [] keyIndex = 0 key = key.upper() @@ -38,9 +41,9 @@ def translateMessage(key, message, mode): for symbol in message: num = LETTERS.find(symbol.upper()) if num != -1: - if mode == 'encrypt': + if mode == "encrypt": num += LETTERS.find(key[keyIndex]) - elif mode == 'decrypt': + elif mode == "decrypt": num -= LETTERS.find(key[keyIndex]) num %= len(LETTERS) @@ -55,7 +58,8 @@ def translateMessage(key, message, mode): keyIndex = 0 else: translated.append(symbol) - return ''.join(translated) + return "".join(translated) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/ciphers/xor_cipher.py b/ciphers/xor_cipher.py index 727fac3b0703..32a350d4e61c 100644 --- a/ciphers/xor_cipher.py +++ b/ciphers/xor_cipher.py @@ -1,209 +1,205 @@ """ - author: Christian Bender - date: 21.12.2017 - class: XORCipher - - This class implements the XOR-cipher algorithm and provides - some useful methods for encrypting and decrypting strings and - files. - - Overview about methods - - - encrypt : list of char - - decrypt : list of char - - encrypt_string : str - - decrypt_string : str - - encrypt_file : boolean - - decrypt_file : boolean + author: Christian Bender + date: 21.12.2017 + class: XORCipher + + This class implements the XOR-cipher algorithm and provides + some useful methods for encrypting and decrypting strings and + files. + + Overview about methods + + - encrypt : list of char + - decrypt : list of char + - encrypt_string : str + - decrypt_string : str + - encrypt_file : boolean + - decrypt_file : boolean """ -class XORCipher(object): - def __init__(self, key = 0): - """ - simple constructor that receives a key or uses - default key = 0 - """ - #private field - self.__key = key +class XORCipher: + def __init__(self, key: int = 0): + """ + simple constructor that receives a key or uses + default key = 0 + """ - def encrypt(self, content, key): - """ - input: 'content' of type string and 'key' of type int - output: encrypted string 'content' as a list of chars - if key not passed the method uses the key by the constructor. - otherwise key = 1 - """ + # private field + self.__key = key - # precondition - assert (isinstance(key,int) and isinstance(content,str)) + def encrypt(self, content: str, key: int) -> [str]: + """ + input: 'content' of type string and 'key' of type int + output: encrypted string 'content' as a list of chars + if key not passed the method uses the key by the constructor. + otherwise key = 1 + """ - key = key or self.__key or 1 + # precondition + assert isinstance(key, int) and isinstance(content, str) - # make sure key can be any size - while (key > 255): - key -= 255 + key = key or self.__key or 1 - # This will be returned - ans = [] + # make sure key can be any size + while key > 255: + key -= 255 - for ch in content: - ans.append(chr(ord(ch) ^ key)) + # This will be returned + ans = [] - return ans + for ch in content: + ans.append(chr(ord(ch) ^ key)) - def decrypt(self,content,key): - """ - input: 'content' of type list and 'key' of type int - output: decrypted string 'content' as a list of chars - if key not passed the method uses the key by the constructor. - otherwise key = 1 - """ + return ans - # precondition - assert (isinstance(key,int) and isinstance(content,list)) + def decrypt(self, content: str, key: int) -> [str]: + """ + input: 'content' of type list and 'key' of type int + output: decrypted string 'content' as a list of chars + if key not passed the method uses the key by the constructor. + otherwise key = 1 + """ - key = key or self.__key or 1 + # precondition + assert isinstance(key, int) and isinstance(content, list) - # make sure key can be any size - while (key > 255): - key -= 255 + key = key or self.__key or 1 - # This will be returned - ans = [] + # make sure key can be any size + while key > 255: + key -= 255 - for ch in content: - ans.append(chr(ord(ch) ^ key)) + # This will be returned + ans = [] - return ans + for ch in content: + ans.append(chr(ord(ch) ^ key)) + return ans - def encrypt_string(self,content, key = 0): - """ - input: 'content' of type string and 'key' of type int - output: encrypted string 'content' - if key not passed the method uses the key by the constructor. - otherwise key = 1 - """ + def encrypt_string(self, content: str, key: int = 0) -> str: + """ + input: 'content' of type string and 'key' of type int + output: encrypted string 'content' + if key not passed the method uses the key by the constructor. + otherwise key = 1 + """ - # precondition - assert (isinstance(key,int) and isinstance(content,str)) + # precondition + assert isinstance(key, int) and isinstance(content, str) - key = key or self.__key or 1 + key = key or self.__key or 1 - # make sure key can be any size - while (key > 255): - key -= 255 + # make sure key can be any size + while key > 255: + key -= 255 - # This will be returned - ans = "" + # This will be returned + ans = "" - for ch in content: - ans += chr(ord(ch) ^ key) + for ch in content: + ans += chr(ord(ch) ^ key) - return ans + return ans - def decrypt_string(self,content,key = 0): - """ - input: 'content' of type string and 'key' of type int - output: decrypted string 'content' - if key not passed the method uses the key by the constructor. - otherwise key = 1 - """ + def decrypt_string(self, content: str, key: int = 0) -> str: + """ + input: 'content' of type string and 'key' of type int + output: decrypted string 'content' + if key not passed the method uses the key by the constructor. + otherwise key = 1 + """ - # precondition - assert (isinstance(key,int) and isinstance(content,str)) + # precondition + assert isinstance(key, int) and isinstance(content, str) - key = key or self.__key or 1 + key = key or self.__key or 1 - # make sure key can be any size - while (key > 255): - key -= 255 + # make sure key can be any size + while key > 255: + key -= 255 - # This will be returned - ans = "" - - for ch in content: - ans += chr(ord(ch) ^ key) + # This will be returned + ans = "" - return ans + for ch in content: + ans += chr(ord(ch) ^ key) + return ans - def encrypt_file(self, file, key = 0): - """ - input: filename (str) and a key (int) - output: returns true if encrypt process was - successful otherwise false - if key not passed the method uses the key by the constructor. - otherwise key = 1 - """ + def encrypt_file(self, file: str, key: int = 0) -> bool: + """ + input: filename (str) and a key (int) + output: returns true if encrypt process was + successful otherwise false + if key not passed the method uses the key by the constructor. + otherwise key = 1 + """ - #precondition - assert (isinstance(file,str) and isinstance(key,int)) + # precondition + assert isinstance(file, str) and isinstance(key, int) - try: - with open(file,"r") as fin: - with open("encrypt.out","w+") as fout: + try: + with open(file) as fin: + with open("encrypt.out", "w+") as fout: - # actual encrypt-process - for line in fin: - fout.write(self.encrypt_string(line,key)) + # actual encrypt-process + for line in fin: + fout.write(self.encrypt_string(line, key)) - except: - return False + except OSError: + return False - return True + return True + def decrypt_file(self, file: str, key: int) -> bool: + """ + input: filename (str) and a key (int) + output: returns true if decrypt process was + successful otherwise false + if key not passed the method uses the key by the constructor. + otherwise key = 1 + """ - def decrypt_file(self,file, key): - """ - input: filename (str) and a key (int) - output: returns true if decrypt process was - successful otherwise false - if key not passed the method uses the key by the constructor. - otherwise key = 1 - """ + # precondition + assert isinstance(file, str) and isinstance(key, int) - #precondition - assert (isinstance(file,str) and isinstance(key,int)) + try: + with open(file) as fin: + with open("decrypt.out", "w+") as fout: - try: - with open(file,"r") as fin: - with open("decrypt.out","w+") as fout: - - # actual encrypt-process - for line in fin: - fout.write(self.decrypt_string(line,key)) - - except: - return False - - return True + # actual encrypt-process + for line in fin: + fout.write(self.decrypt_string(line, key)) + except OSError: + return False + return True # Tests # crypt = XORCipher() # key = 67 -# # test enrcypt -# print crypt.encrypt("hallo welt",key) +# # test encrypt +# print(crypt.encrypt("hallo welt",key)) # # test decrypt -# print crypt.decrypt(crypt.encrypt("hallo welt",key), key) +# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) # # test encrypt_string -# print crypt.encrypt_string("hallo welt",key) +# print(crypt.encrypt_string("hallo welt",key)) # # test decrypt_string -# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key) +# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) # if (crypt.encrypt_file("test.txt",key)): -# print "encrypt successful" +# print("encrypt successful") # else: -# print "encrypt unsuccessful" +# print("encrypt unsuccessful") # if (crypt.decrypt_file("encrypt.out",key)): -# print "decrypt successful" +# print("decrypt successful") # else: -# print "decrypt unsuccessful" \ No newline at end of file +# print("decrypt unsuccessful") diff --git a/compression/__init__.py b/compression/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/compression/burrows_wheeler.py b/compression/burrows_wheeler.py new file mode 100644 index 000000000000..1a6610915e65 --- /dev/null +++ b/compression/burrows_wheeler.py @@ -0,0 +1,167 @@ +""" +https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform + +The Burrows–Wheeler transform (BWT, also called block-sorting compression) +rearranges a character string into runs of similar characters. This is useful +for compression, since it tends to be easy to compress a string that has runs +of repeated characters by techniques such as move-to-front transform and +run-length encoding. More importantly, the transformation is reversible, +without needing to store any additional data except the position of the first +original character. The BWT is thus a "free" method of improving the efficiency +of text compression algorithms, costing only some extra computation. +""" +from __future__ import annotations + + +def all_rotations(s: str) -> list[str]: + """ + :param s: The string that will be rotated len(s) times. + :return: A list with the rotations. + :raises TypeError: If s is not an instance of str. + Examples: + + >>> all_rotations("^BANANA|") # doctest: +NORMALIZE_WHITESPACE + ['^BANANA|', 'BANANA|^', 'ANANA|^B', 'NANA|^BA', 'ANA|^BAN', 'NA|^BANA', + 'A|^BANAN', '|^BANANA'] + >>> all_rotations("a_asa_da_casa") # doctest: +NORMALIZE_WHITESPACE + ['a_asa_da_casa', '_asa_da_casaa', 'asa_da_casaa_', 'sa_da_casaa_a', + 'a_da_casaa_as', '_da_casaa_asa', 'da_casaa_asa_', 'a_casaa_asa_d', + '_casaa_asa_da', 'casaa_asa_da_', 'asaa_asa_da_c', 'saa_asa_da_ca', + 'aa_asa_da_cas'] + >>> all_rotations("panamabanana") # doctest: +NORMALIZE_WHITESPACE + ['panamabanana', 'anamabananap', 'namabananapa', 'amabananapan', + 'mabananapana', 'abananapanam', 'bananapanama', 'ananapanamab', + 'nanapanamaba', 'anapanamaban', 'napanamabana', 'apanamabanan'] + >>> all_rotations(5) + Traceback (most recent call last): + ... + TypeError: The parameter s type must be str. + """ + if not isinstance(s, str): + raise TypeError("The parameter s type must be str.") + + return [s[i:] + s[:i] for i in range(len(s))] + + +def bwt_transform(s: str) -> dict: + """ + :param s: The string that will be used at bwt algorithm + :return: the string composed of the last char of each row of the ordered + rotations and the index of the original string at ordered rotations list + :raises TypeError: If the s parameter type is not str + :raises ValueError: If the s parameter is empty + Examples: + + >>> bwt_transform("^BANANA") + {'bwt_string': 'BNN^AAA', 'idx_original_string': 6} + >>> bwt_transform("a_asa_da_casa") + {'bwt_string': 'aaaadss_c__aa', 'idx_original_string': 3} + >>> bwt_transform("panamabanana") + {'bwt_string': 'mnpbnnaaaaaa', 'idx_original_string': 11} + >>> bwt_transform(4) + Traceback (most recent call last): + ... + TypeError: The parameter s type must be str. + >>> bwt_transform('') + Traceback (most recent call last): + ... + ValueError: The parameter s must not be empty. + """ + if not isinstance(s, str): + raise TypeError("The parameter s type must be str.") + if not s: + raise ValueError("The parameter s must not be empty.") + + rotations = all_rotations(s) + rotations.sort() # sort the list of rotations in alphabetically order + # make a string composed of the last char of each rotation + return { + "bwt_string": "".join([word[-1] for word in rotations]), + "idx_original_string": rotations.index(s), + } + + +def reverse_bwt(bwt_string: str, idx_original_string: int) -> str: + """ + :param bwt_string: The string returned from bwt algorithm execution + :param idx_original_string: A 0-based index of the string that was used to + generate bwt_string at ordered rotations list + :return: The string used to generate bwt_string when bwt was executed + :raises TypeError: If the bwt_string parameter type is not str + :raises ValueError: If the bwt_string parameter is empty + :raises TypeError: If the idx_original_string type is not int or if not + possible to cast it to int + :raises ValueError: If the idx_original_string value is lower than 0 or + greater than len(bwt_string) - 1 + + >>> reverse_bwt("BNN^AAA", 6) + '^BANANA' + >>> reverse_bwt("aaaadss_c__aa", 3) + 'a_asa_da_casa' + >>> reverse_bwt("mnpbnnaaaaaa", 11) + 'panamabanana' + >>> reverse_bwt(4, 11) + Traceback (most recent call last): + ... + TypeError: The parameter bwt_string type must be str. + >>> reverse_bwt("", 11) + Traceback (most recent call last): + ... + ValueError: The parameter bwt_string must not be empty. + >>> reverse_bwt("mnpbnnaaaaaa", "asd") # doctest: +NORMALIZE_WHITESPACE + Traceback (most recent call last): + ... + TypeError: The parameter idx_original_string type must be int or passive + of cast to int. + >>> reverse_bwt("mnpbnnaaaaaa", -1) + Traceback (most recent call last): + ... + ValueError: The parameter idx_original_string must not be lower than 0. + >>> reverse_bwt("mnpbnnaaaaaa", 12) # doctest: +NORMALIZE_WHITESPACE + Traceback (most recent call last): + ... + ValueError: The parameter idx_original_string must be lower than + len(bwt_string). + >>> reverse_bwt("mnpbnnaaaaaa", 11.0) + 'panamabanana' + >>> reverse_bwt("mnpbnnaaaaaa", 11.4) + 'panamabanana' + """ + if not isinstance(bwt_string, str): + raise TypeError("The parameter bwt_string type must be str.") + if not bwt_string: + raise ValueError("The parameter bwt_string must not be empty.") + try: + idx_original_string = int(idx_original_string) + except ValueError: + raise TypeError( + "The parameter idx_original_string type must be int or passive" + " of cast to int." + ) + if idx_original_string < 0: + raise ValueError("The parameter idx_original_string must not be lower than 0.") + if idx_original_string >= len(bwt_string): + raise ValueError( + "The parameter idx_original_string must be lower than" " len(bwt_string)." + ) + + ordered_rotations = [""] * len(bwt_string) + for x in range(len(bwt_string)): + for i in range(len(bwt_string)): + ordered_rotations[i] = bwt_string[i] + ordered_rotations[i] + ordered_rotations.sort() + return ordered_rotations[idx_original_string] + + +if __name__ == "__main__": + entry_msg = "Provide a string that I will generate its BWT transform: " + s = input(entry_msg).strip() + result = bwt_transform(s) + bwt_output_msg = "Burrows Wheeler transform for string '{}' results in '{}'" + print(bwt_output_msg.format(s, result["bwt_string"])) + original_string = reverse_bwt(result["bwt_string"], result["idx_original_string"]) + fmt = ( + "Reversing Burrows Wheeler transform for entry '{}' we get original" + " string '{}'" + ) + print(fmt.format(result["bwt_string"], original_string)) diff --git a/compression/huffman.py b/compression/huffman.py new file mode 100644 index 000000000000..3a3cbfa4b0c6 --- /dev/null +++ b/compression/huffman.py @@ -0,0 +1,87 @@ +import sys + + +class Letter: + def __init__(self, letter, freq): + self.letter = letter + self.freq = freq + self.bitstring = "" + + def __repr__(self): + return f"{self.letter}:{self.freq}" + + +class TreeNode: + def __init__(self, freq, left, right): + self.freq = freq + self.left = left + self.right = right + + +def parse_file(file_path): + """ + Read the file and build a dict of all letters and their + frequencies, then convert the dict into a list of Letters. + """ + chars = {} + with open(file_path) as f: + while True: + c = f.read(1) + if not c: + break + chars[c] = chars[c] + 1 if c in chars.keys() else 1 + return sorted([Letter(c, f) for c, f in chars.items()], key=lambda l: l.freq) + + +def build_tree(letters): + """ + Run through the list of Letters and build the min heap + for the Huffman Tree. + """ + while len(letters) > 1: + left = letters.pop(0) + right = letters.pop(0) + total_freq = left.freq + right.freq + node = TreeNode(total_freq, left, right) + letters.append(node) + letters.sort(key=lambda l: l.freq) + return letters[0] + + +def traverse_tree(root, bitstring): + """ + Recursively traverse the Huffman Tree to set each + Letter's bitstring, and return the list of Letters + """ + if type(root) is Letter: + root.bitstring = bitstring + return [root] + letters = [] + letters += traverse_tree(root.left, bitstring + "0") + letters += traverse_tree(root.right, bitstring + "1") + return letters + + +def huffman(file_path): + """ + Parse the file, build the tree, then run through the file + again, using the list of Letters to find and print out the + bitstring for each letter. + """ + letters_list = parse_file(file_path) + root = build_tree(letters_list) + letters = traverse_tree(root, "") + print(f"Huffman Coding of {file_path}: ") + with open(file_path) as f: + while True: + c = f.read(1) + if not c: + break + le = list(filter(lambda l: l.letter == c, letters))[0] + print(le.bitstring, end=" ") + print() + + +if __name__ == "__main__": + # pass the file path to the huffman function + huffman(sys.argv[1]) diff --git a/analysis/compression_analysis/PSNR-example-base.png b/compression/image_data/PSNR-example-base.png similarity index 100% rename from analysis/compression_analysis/PSNR-example-base.png rename to compression/image_data/PSNR-example-base.png diff --git a/analysis/compression_analysis/PSNR-example-comp-10.jpg b/compression/image_data/PSNR-example-comp-10.jpg similarity index 100% rename from analysis/compression_analysis/PSNR-example-comp-10.jpg rename to compression/image_data/PSNR-example-comp-10.jpg diff --git a/compression/image_data/__init__.py b/compression/image_data/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/analysis/compression_analysis/compressed_image.png b/compression/image_data/compressed_image.png similarity index 100% rename from analysis/compression_analysis/compressed_image.png rename to compression/image_data/compressed_image.png diff --git a/analysis/compression_analysis/example_image.jpg b/compression/image_data/example_image.jpg similarity index 100% rename from analysis/compression_analysis/example_image.jpg rename to compression/image_data/example_image.jpg diff --git a/analysis/compression_analysis/example_wikipedia_image.jpg b/compression/image_data/example_wikipedia_image.jpg similarity index 100% rename from analysis/compression_analysis/example_wikipedia_image.jpg rename to compression/image_data/example_wikipedia_image.jpg diff --git a/analysis/compression_analysis/original_image.png b/compression/image_data/original_image.png similarity index 100% rename from analysis/compression_analysis/original_image.png rename to compression/image_data/original_image.png diff --git a/compression/lempel_ziv.py b/compression/lempel_ziv.py new file mode 100644 index 000000000000..2d0601b27b34 --- /dev/null +++ b/compression/lempel_ziv.py @@ -0,0 +1,125 @@ +""" + One of the several implementations of Lempel–Ziv–Welch compression algorithm + https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch +""" + +import math +import os +import sys + + +def read_file_binary(file_path: str) -> str: + """ + Reads given file as bytes and returns them as a long string + """ + result = "" + try: + with open(file_path, "rb") as binary_file: + data = binary_file.read() + for dat in data: + curr_byte = f"{dat:08b}" + result += curr_byte + return result + except OSError: + print("File not accessible") + sys.exit() + + +def add_key_to_lexicon( + lexicon: dict, curr_string: str, index: int, last_match_id: int +) -> None: + """ + Adds new strings (curr_string + "0", curr_string + "1") to the lexicon + """ + lexicon.pop(curr_string) + lexicon[curr_string + "0"] = last_match_id + + if math.log2(index).is_integer(): + for curr_key in lexicon: + lexicon[curr_key] = "0" + lexicon[curr_key] + + lexicon[curr_string + "1"] = bin(index)[2:] + + +def compress_data(data_bits: str) -> str: + """ + Compresses given data_bits using Lempel–Ziv–Welch compression algorithm + and returns the result as a string + """ + lexicon = {"0": "0", "1": "1"} + result, curr_string = "", "" + index = len(lexicon) + + for i in range(len(data_bits)): + curr_string += data_bits[i] + if curr_string not in lexicon: + continue + + last_match_id = lexicon[curr_string] + result += last_match_id + add_key_to_lexicon(lexicon, curr_string, index, last_match_id) + index += 1 + curr_string = "" + + while curr_string != "" and curr_string not in lexicon: + curr_string += "0" + + if curr_string != "": + last_match_id = lexicon[curr_string] + result += last_match_id + + return result + + +def add_file_length(source_path: str, compressed: str) -> str: + """ + Adds given file's length in front (using Elias gamma coding) of the compressed + string + """ + file_length = os.path.getsize(source_path) + file_length_binary = bin(file_length)[2:] + length_length = len(file_length_binary) + + return "0" * (length_length - 1) + file_length_binary + compressed + + +def write_file_binary(file_path: str, to_write: str) -> None: + """ + Writes given to_write string (should only consist of 0's and 1's) as bytes in the + file + """ + byte_length = 8 + try: + with open(file_path, "wb") as opened_file: + result_byte_array = [ + to_write[i : i + byte_length] + for i in range(0, len(to_write), byte_length) + ] + + if len(result_byte_array[-1]) % byte_length == 0: + result_byte_array.append("10000000") + else: + result_byte_array[-1] += "1" + "0" * ( + byte_length - len(result_byte_array[-1]) - 1 + ) + + for elem in result_byte_array: + opened_file.write(int(elem, 2).to_bytes(1, byteorder="big")) + except OSError: + print("File not accessible") + sys.exit() + + +def compress(source_path, destination_path: str) -> None: + """ + Reads source file, compresses it and writes the compressed result in destination + file + """ + data_bits = read_file_binary(source_path) + compressed = compress_data(data_bits) + compressed = add_file_length(source_path, compressed) + write_file_binary(destination_path, compressed) + + +if __name__ == "__main__": + compress(sys.argv[1], sys.argv[2]) diff --git a/compression/lempel_ziv_decompress.py b/compression/lempel_ziv_decompress.py new file mode 100644 index 000000000000..4d3c2c0d2cf3 --- /dev/null +++ b/compression/lempel_ziv_decompress.py @@ -0,0 +1,111 @@ +""" + One of the several implementations of Lempel–Ziv–Welch decompression algorithm + https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch +""" + +import math +import sys + + +def read_file_binary(file_path: str) -> str: + """ + Reads given file as bytes and returns them as a long string + """ + result = "" + try: + with open(file_path, "rb") as binary_file: + data = binary_file.read() + for dat in data: + curr_byte = f"{dat:08b}" + result += curr_byte + return result + except OSError: + print("File not accessible") + sys.exit() + + +def decompress_data(data_bits: str) -> str: + """ + Decompresses given data_bits using Lempel–Ziv–Welch compression algorithm + and returns the result as a string + """ + lexicon = {"0": "0", "1": "1"} + result, curr_string = "", "" + index = len(lexicon) + + for i in range(len(data_bits)): + curr_string += data_bits[i] + if curr_string not in lexicon: + continue + + last_match_id = lexicon[curr_string] + result += last_match_id + lexicon[curr_string] = last_match_id + "0" + + if math.log2(index).is_integer(): + newLex = {} + for curr_key in list(lexicon): + newLex["0" + curr_key] = lexicon.pop(curr_key) + lexicon = newLex + + lexicon[bin(index)[2:]] = last_match_id + "1" + index += 1 + curr_string = "" + return result + + +def write_file_binary(file_path: str, to_write: str) -> None: + """ + Writes given to_write string (should only consist of 0's and 1's) as bytes in the + file + """ + byte_length = 8 + try: + with open(file_path, "wb") as opened_file: + result_byte_array = [ + to_write[i : i + byte_length] + for i in range(0, len(to_write), byte_length) + ] + + if len(result_byte_array[-1]) % byte_length == 0: + result_byte_array.append("10000000") + else: + result_byte_array[-1] += "1" + "0" * ( + byte_length - len(result_byte_array[-1]) - 1 + ) + + for elem in result_byte_array[:-1]: + opened_file.write(int(elem, 2).to_bytes(1, byteorder="big")) + except OSError: + print("File not accessible") + sys.exit() + + +def remove_prefix(data_bits: str) -> str: + """ + Removes size prefix, that compressed file should have + Returns the result + """ + counter = 0 + for letter in data_bits: + if letter == "1": + break + counter += 1 + + data_bits = data_bits[counter:] + data_bits = data_bits[counter + 1 :] + return data_bits + + +def compress(source_path: str, destination_path: str) -> None: + """ + Reads source file, decompresses it and writes the result in destination file + """ + data_bits = read_file_binary(source_path) + data_bits = remove_prefix(data_bits) + decompressed = decompress_data(data_bits) + write_file_binary(destination_path, decompressed) + + +if __name__ == "__main__": + compress(sys.argv[1], sys.argv[2]) diff --git a/analysis/compression_analysis/psnr.py b/compression/peak_signal_to_noise_ratio.py similarity index 53% rename from analysis/compression_analysis/psnr.py rename to compression/peak_signal_to_noise_ratio.py index 0f21aac07d34..6c6c4c38a12a 100644 --- a/analysis/compression_analysis/psnr.py +++ b/compression/peak_signal_to_noise_ratio.py @@ -1,6 +1,8 @@ """ - Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio - Soruce: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/ +Peak signal-to-noise ratio - PSNR + https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio +Source: +https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python """ import math @@ -9,6 +11,7 @@ import cv2 import numpy as np + def psnr(original, contrast): mse = np.mean((original - contrast) ** 2) if mse == 0: @@ -21,11 +24,13 @@ def psnr(original, contrast): def main(): dir_path = os.path.dirname(os.path.realpath(__file__)) # Loading images (original image and compressed image) - original = cv2.imread(os.path.join(dir_path, 'original_image.png')) - contrast = cv2.imread(os.path.join(dir_path, 'compressed_image.png'), 1) + original = cv2.imread(os.path.join(dir_path, "image_data/original_image.png")) + contrast = cv2.imread(os.path.join(dir_path, "image_data/compressed_image.png"), 1) - original2 = cv2.imread(os.path.join(dir_path, 'PSNR-example-base.png')) - contrast2 = cv2.imread(os.path.join(dir_path, 'PSNR-example-comp-10.jpg'), 1) + original2 = cv2.imread(os.path.join(dir_path, "image_data/PSNR-example-base.png")) + contrast2 = cv2.imread( + os.path.join(dir_path, "image_data/PSNR-example-comp-10.jpg"), 1 + ) # Value expected: 29.73dB print("-- First Test --") @@ -36,5 +41,5 @@ def main(): print(f"PSNR value is {psnr(original2, contrast2)} dB") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/computer_vision/README.md b/computer_vision/README.md new file mode 100644 index 000000000000..94ee493086cc --- /dev/null +++ b/computer_vision/README.md @@ -0,0 +1,7 @@ +### Computer Vision + +Computer vision is a field of computer science that works on enabling computers to see, +identify and process images in the same way that human vision does, and then provide appropriate output. +It is like imparting human intelligence and instincts to a computer. +Image processing and computer vision are a little different from each other. Image processing means applying some algorithms for transforming image from one form to the other like smoothing, contrasting, stretching, etc. +While computer vision comes from modelling image processing using the techniques of machine learning, computer vision applies machine learning to recognize patterns for interpretation of images (much like the process of visual reasoning of human vision). diff --git a/computer_vision/__init__.py b/computer_vision/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/computer_vision/harriscorner.py b/computer_vision/harriscorner.py new file mode 100644 index 000000000000..fb7f560f7873 --- /dev/null +++ b/computer_vision/harriscorner.py @@ -0,0 +1,75 @@ +import cv2 +import numpy as np + +""" +Harris Corner Detector +https://en.wikipedia.org/wiki/Harris_Corner_Detector +""" + + +class Harris_Corner: + def __init__(self, k: float, window_size: int): + + """ + k : is an empirically determined constant in [0.04,0.06] + window_size : neighbourhoods considered + """ + + if k in (0.04, 0.06): + self.k = k + self.window_size = window_size + else: + raise ValueError("invalid k value") + + def __str__(self): + + return f"Harris Corner detection with k : {self.k}" + + def detect(self, img_path: str): + + """ + Returns the image with corners identified + img_path : path of the image + output : list of the corner positions, image + """ + + img = cv2.imread(img_path, 0) + h, w = img.shape + corner_list = [] + color_img = img.copy() + color_img = cv2.cvtColor(color_img, cv2.COLOR_GRAY2RGB) + dy, dx = np.gradient(img) + ixx = dx ** 2 + iyy = dy ** 2 + ixy = dx * dy + k = 0.04 + offset = self.window_size // 2 + for y in range(offset, h - offset): + for x in range(offset, w - offset): + wxx = ixx[ + y - offset : y + offset + 1, x - offset : x + offset + 1 + ].sum() + wyy = iyy[ + y - offset : y + offset + 1, x - offset : x + offset + 1 + ].sum() + wxy = ixy[ + y - offset : y + offset + 1, x - offset : x + offset + 1 + ].sum() + + det = (wxx * wyy) - (wxy ** 2) + trace = wxx + wyy + r = det - k * (trace ** 2) + # Can change the value + if r > 0.5: + corner_list.append([x, y, r]) + color_img.itemset((y, x, 0), 0) + color_img.itemset((y, x, 1), 0) + color_img.itemset((y, x, 2), 255) + return color_img, corner_list + + +if __name__ == "__main__": + + edge_detect = Harris_Corner(0.04, 3) + color_img, _ = edge_detect.detect("path_to_image") + cv2.imwrite("detect.png", color_img) diff --git a/computer_vision/meanthreshold.py b/computer_vision/meanthreshold.py new file mode 100644 index 000000000000..76657933d6a9 --- /dev/null +++ b/computer_vision/meanthreshold.py @@ -0,0 +1,30 @@ +from PIL import Image + +""" +Mean thresholding algorithm for image processing +https://en.wikipedia.org/wiki/Thresholding_(image_processing) +""" + + +def mean_threshold(image: Image) -> Image: + """ + image: is a grayscale PIL image object + """ + height, width = image.size + mean = 0 + pixels = image.load() + for i in range(width): + for j in range(height): + pixel = pixels[j, i] + mean += pixel + mean //= width * height + + for j in range(width): + for i in range(height): + pixels[i, j] = 255 if pixels[i, j] > mean else 0 + return image + + +if __name__ == "__main__": + image = mean_threshold(Image.open("path_to_image").convert("L")) + image.save("output_image_path") diff --git a/conversions/__init__.py b/conversions/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/conversions/binary_to_decimal.py b/conversions/binary_to_decimal.py new file mode 100644 index 000000000000..a7625e475bdc --- /dev/null +++ b/conversions/binary_to_decimal.py @@ -0,0 +1,43 @@ +def bin_to_decimal(bin_string: str) -> int: + """ + Convert a binary value to its decimal equivalent + + >>> bin_to_decimal("101") + 5 + >>> bin_to_decimal(" 1010 ") + 10 + >>> bin_to_decimal("-11101") + -29 + >>> bin_to_decimal("0") + 0 + >>> bin_to_decimal("a") + Traceback (most recent call last): + ... + ValueError: Non-binary value was passed to the function + >>> bin_to_decimal("") + Traceback (most recent call last): + ... + ValueError: Empty string was passed to the function + >>> bin_to_decimal("39") + Traceback (most recent call last): + ... + ValueError: Non-binary value was passed to the function + """ + bin_string = str(bin_string).strip() + if not bin_string: + raise ValueError("Empty string was passed to the function") + is_negative = bin_string[0] == "-" + if is_negative: + bin_string = bin_string[1:] + if not all(char in "01" for char in bin_string): + raise ValueError("Non-binary value was passed to the function") + decimal_number = 0 + for char in bin_string: + decimal_number = 2 * decimal_number + int(char) + return -decimal_number if is_negative else decimal_number + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/conversions/binary_to_octal.py b/conversions/binary_to_octal.py new file mode 100644 index 000000000000..8b594887867e --- /dev/null +++ b/conversions/binary_to_octal.py @@ -0,0 +1,45 @@ +""" +The function below will convert any binary string to the octal equivalent. + +>>> bin_to_octal("1111") +'17' + +>>> bin_to_octal("101010101010011") +'52523' + +>>> bin_to_octal("") +Traceback (most recent call last): +... +ValueError: Empty string was passed to the function +>>> bin_to_octal("a-1") +Traceback (most recent call last): +... +ValueError: Non-binary value was passed to the function +""" + + +def bin_to_octal(bin_string: str) -> str: + if not all(char in "01" for char in bin_string): + raise ValueError("Non-binary value was passed to the function") + if not bin_string: + raise ValueError("Empty string was passed to the function") + oct_string = "" + while len(bin_string) % 3 != 0: + bin_string = "0" + bin_string + bin_string_in_3_list = [ + bin_string[index : index + 3] + for index, value in enumerate(bin_string) + if index % 3 == 0 + ] + for bin_group in bin_string_in_3_list: + oct_val = 0 + for index, val in enumerate(bin_group): + oct_val += int(2 ** (2 - index) * int(val)) + oct_string += str(oct_val) + return oct_string + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py new file mode 100644 index 000000000000..3c72a7732ac6 --- /dev/null +++ b/conversions/decimal_to_any.py @@ -0,0 +1,107 @@ +"""Convert a positive Decimal Number to Any Other Representation""" + + +def decimal_to_any(num: int, base: int) -> str: + """ + Convert a positive integer to another base as str. + >>> decimal_to_any(0, 2) + '0' + >>> decimal_to_any(5, 4) + '11' + >>> decimal_to_any(20, 3) + '202' + >>> decimal_to_any(58, 16) + '3A' + >>> decimal_to_any(243, 17) + 'E5' + >>> decimal_to_any(34923, 36) + 'QY3' + >>> decimal_to_any(10, 11) + 'A' + >>> decimal_to_any(16, 16) + '10' + >>> decimal_to_any(36, 36) + '10' + >>> # negatives will error + >>> decimal_to_any(-45, 8) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: parameter must be positive int + >>> # floats will error + >>> decimal_to_any(34.4, 6) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: int() can't convert non-string with explicit base + >>> # a float base will error + >>> decimal_to_any(5, 2.5) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> # a str base will error + >>> decimal_to_any(10, '16') # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: 'str' object cannot be interpreted as an integer + >>> # a base less than 2 will error + >>> decimal_to_any(7, 0) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: base must be >= 2 + >>> # a base greater than 36 will error + >>> decimal_to_any(34, 37) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: base must be <= 36 + """ + if isinstance(num, float): + raise TypeError("int() can't convert non-string with explicit base") + if num < 0: + raise ValueError("parameter must be positive int") + if isinstance(base, str): + raise TypeError("'str' object cannot be interpreted as an integer") + if isinstance(base, float): + raise TypeError("'float' object cannot be interpreted as an integer") + if base in (0, 1): + raise ValueError("base must be >= 2") + if base > 36: + raise ValueError("base must be <= 36") + # fmt: off + ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', + '16': 'G', '17': 'H', '18': 'I', '19': 'J', '20': 'K', '21': 'L', + '22': 'M', '23': 'N', '24': 'O', '25': 'P', '26': 'Q', '27': 'R', + '28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X', + '34': 'Y', '35': 'Z'} + # fmt: on + new_value = "" + mod = 0 + div = 0 + while div != 1: + div, mod = divmod(num, base) + if base >= 11 and 9 < mod < 36: + actual_value = ALPHABET_VALUES[str(mod)] + mod = actual_value + new_value += str(mod) + div = num // base + num = div + if div == 0: + return str(new_value[::-1]) + elif div == 1: + new_value += str(div) + return str(new_value[::-1]) + + return new_value[::-1] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + for base in range(2, 37): + for num in range(1000): + assert int(decimal_to_any(num, base), base) == num, ( + num, + base, + decimal_to_any(num, base), + int(decimal_to_any(num, base), base), + ) diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py new file mode 100644 index 000000000000..7e83aee4f7a5 --- /dev/null +++ b/conversions/decimal_to_binary.py @@ -0,0 +1,59 @@ +"""Convert a Decimal Number to a Binary Number.""" + + +def decimal_to_binary(num: int) -> str: + + """ + Convert an Integer Decimal Number to a Binary Number as str. + >>> decimal_to_binary(0) + '0b0' + >>> decimal_to_binary(2) + '0b10' + >>> decimal_to_binary(7) + '0b111' + >>> decimal_to_binary(35) + '0b100011' + >>> # negatives work too + >>> decimal_to_binary(-2) + '-0b10' + >>> # other floats will error + >>> decimal_to_binary(16.16) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> # strings will error as well + >>> decimal_to_binary('0xfffff') # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: 'str' object cannot be interpreted as an integer + """ + + if type(num) == float: + raise TypeError("'float' object cannot be interpreted as an integer") + if type(num) == str: + raise TypeError("'str' object cannot be interpreted as an integer") + + if num == 0: + return "0b0" + + negative = False + + if num < 0: + negative = True + num = -num + + binary = [] + while num > 0: + binary.insert(0, num % 2) + num >>= 1 + + if negative: + return "-0b" + "".join(str(e) for e in binary) + + return "0b" + "".join(str(e) for e in binary) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py new file mode 100644 index 000000000000..c149ea86592f --- /dev/null +++ b/conversions/decimal_to_binary_recursion.py @@ -0,0 +1,53 @@ +def binary_recursive(decimal: int) -> str: + """ + Take a positive integer value and return its binary equivalent. + >>> binary_recursive(1000) + '1111101000' + >>> binary_recursive("72") + '1001000' + >>> binary_recursive("number") + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: 'number' + """ + decimal = int(decimal) + if decimal in (0, 1): # Exit cases for the recursion + return str(decimal) + div, mod = divmod(decimal, 2) + return binary_recursive(div) + str(mod) + + +def main(number: str) -> str: + """ + Take an integer value and raise ValueError for wrong inputs, + call the function above and return the output with prefix "0b" & "-0b" + for positive and negative integers respectively. + >>> main(0) + '0b0' + >>> main(40) + '0b101000' + >>> main(-40) + '-0b101000' + >>> main(40.8) + Traceback (most recent call last): + ... + ValueError: Input value is not an integer + >>> main("forty") + Traceback (most recent call last): + ... + ValueError: Input value is not an integer + """ + number = str(number).strip() + if not number: + raise ValueError("No input value was provided") + negative = "-" if number.startswith("-") else "" + number = number.lstrip("-") + if not number.isnumeric(): + raise ValueError("Input value is not an integer") + return f"{negative}0b{binary_recursive(int(number))}" + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/conversions/decimal_to_hexadecimal.py b/conversions/decimal_to_hexadecimal.py new file mode 100644 index 000000000000..433f78dfecb7 --- /dev/null +++ b/conversions/decimal_to_hexadecimal.py @@ -0,0 +1,78 @@ +""" Convert Base 10 (Decimal) Values to Hexadecimal Representations """ + +# set decimal value for each hexadecimal digit +values = { + 0: "0", + 1: "1", + 2: "2", + 3: "3", + 4: "4", + 5: "5", + 6: "6", + 7: "7", + 8: "8", + 9: "9", + 10: "a", + 11: "b", + 12: "c", + 13: "d", + 14: "e", + 15: "f", +} + + +def decimal_to_hexadecimal(decimal): + """ + take integer decimal value, return hexadecimal representation as str beginning + with 0x + >>> decimal_to_hexadecimal(5) + '0x5' + >>> decimal_to_hexadecimal(15) + '0xf' + >>> decimal_to_hexadecimal(37) + '0x25' + >>> decimal_to_hexadecimal(255) + '0xff' + >>> decimal_to_hexadecimal(4096) + '0x1000' + >>> decimal_to_hexadecimal(999098) + '0xf3eba' + >>> # negatives work too + >>> decimal_to_hexadecimal(-256) + '-0x100' + >>> # floats are acceptable if equivalent to an int + >>> decimal_to_hexadecimal(17.0) + '0x11' + >>> # other floats will error + >>> decimal_to_hexadecimal(16.16) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + AssertionError + >>> # strings will error as well + >>> decimal_to_hexadecimal('0xfffff') # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + AssertionError + >>> # results are the same when compared to Python's default hex function + >>> decimal_to_hexadecimal(-256) == hex(-256) + True + """ + assert type(decimal) in (int, float) and decimal == int(decimal) + hexadecimal = "" + negative = False + if decimal < 0: + negative = True + decimal *= -1 + while decimal > 0: + decimal, remainder = divmod(decimal, 16) + hexadecimal = values[remainder] + hexadecimal + hexadecimal = "0x" + hexadecimal + if negative: + hexadecimal = "-" + hexadecimal + return hexadecimal + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/conversions/decimal_to_octal.py b/conversions/decimal_to_octal.py new file mode 100644 index 000000000000..8dc04830ad87 --- /dev/null +++ b/conversions/decimal_to_octal.py @@ -0,0 +1,43 @@ +"""Convert a Decimal Number to an Octal Number.""" + +import math + +# Modified from: +# https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToOctal.js + + +def decimal_to_octal(num: int) -> str: + """Convert a Decimal Number to an Octal Number. + + >>> all(decimal_to_octal(i) == oct(i) for i + ... in (0, 2, 8, 64, 65, 216, 255, 256, 512)) + True + """ + octal = 0 + counter = 0 + while num > 0: + remainder = num % 8 + octal = octal + (remainder * math.pow(10, counter)) + counter += 1 + num = math.floor(num / 8) # basically /= 8 without remainder if any + # This formatting removes trailing '.0' from `octal`. + return f"0o{int(octal)}" + + +def main(): + """Print octal equivalents of decimal numbers.""" + print("\n2 in octal is:") + print(decimal_to_octal(2)) # = 2 + print("\n8 in octal is:") + print(decimal_to_octal(8)) # = 10 + print("\n65 in octal is:") + print(decimal_to_octal(65)) # = 101 + print("\n216 in octal is:") + print(decimal_to_octal(216)) # = 330 + print("\n512 in octal is:") + print(decimal_to_octal(512)) # = 1000 + print("\n") + + +if __name__ == "__main__": + main() diff --git a/conversions/hexadecimal_to_decimal.py b/conversions/hexadecimal_to_decimal.py new file mode 100644 index 000000000000..beb1c2c3ded6 --- /dev/null +++ b/conversions/hexadecimal_to_decimal.py @@ -0,0 +1,49 @@ +hex_table = {hex(i)[2:]: i for i in range(16)} # Use [:2] to strip off the leading '0x' + + +def hex_to_decimal(hex_string: str) -> int: + """ + Convert a hexadecimal value to its decimal equivalent + #https://www.programiz.com/python-programming/methods/built-in/hex + + >>> hex_to_decimal("a") + 10 + >>> hex_to_decimal("12f") + 303 + >>> hex_to_decimal(" 12f ") + 303 + >>> hex_to_decimal("FfFf") + 65535 + >>> hex_to_decimal("-Ff") + -255 + >>> hex_to_decimal("F-f") + Traceback (most recent call last): + ... + ValueError: Non-hexadecimal value was passed to the function + >>> hex_to_decimal("") + Traceback (most recent call last): + ... + ValueError: Empty string was passed to the function + >>> hex_to_decimal("12m") + Traceback (most recent call last): + ... + ValueError: Non-hexadecimal value was passed to the function + """ + hex_string = hex_string.strip().lower() + if not hex_string: + raise ValueError("Empty string was passed to the function") + is_negative = hex_string[0] == "-" + if is_negative: + hex_string = hex_string[1:] + if not all(char in hex_table for char in hex_string): + raise ValueError("Non-hexadecimal value was passed to the function") + decimal_number = 0 + for char in hex_string: + decimal_number = 16 * decimal_number + hex_table[char] + return -decimal_number if is_negative else decimal_number + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/conversions/molecular_chemistry.py b/conversions/molecular_chemistry.py new file mode 100644 index 000000000000..8c68459965b0 --- /dev/null +++ b/conversions/molecular_chemistry.py @@ -0,0 +1,92 @@ +""" +Functions useful for doing molecular chemistry: +* molarity_to_normality +* moles_to_pressure +* moles_to_volume +* pressure_and_volume_to_temperature +""" + + +def molarity_to_normality(nfactor: int, moles: float, volume: float) -> float: + """ + Convert molarity to normality. + Volume is taken in litres. + + Wikipedia reference: https://en.wikipedia.org/wiki/Equivalent_concentration + Wikipedia reference: https://en.wikipedia.org/wiki/Molar_concentration + + >>> molarity_to_normality(2, 3.1, 0.31) + 20 + >>> molarity_to_normality(4, 11.4, 5.7) + 8 + """ + return round((float(moles / volume) * nfactor)) + + +def moles_to_pressure(volume: float, moles: float, temperature: float) -> float: + """ + Convert moles to pressure. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature + + >>> moles_to_pressure(0.82, 3, 300) + 90 + >>> moles_to_pressure(8.2, 5, 200) + 10 + """ + return round(float((moles * 0.0821 * temperature) / (volume))) + + +def moles_to_volume(pressure: float, moles: float, temperature: float) -> float: + """ + Convert moles to volume. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature + + >>> moles_to_volume(0.82, 3, 300) + 90 + >>> moles_to_volume(8.2, 5, 200) + 10 + """ + return round(float((moles * 0.0821 * temperature) / (pressure))) + + +def pressure_and_volume_to_temperature( + pressure: float, moles: float, volume: float +) -> float: + """ + Convert pressure and volume to temperature. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature + + >>> pressure_and_volume_to_temperature(0.82, 1, 2) + 20 + >>> pressure_and_volume_to_temperature(8.2, 5, 3) + 60 + """ + return round(float((pressure * volume) / (0.0821 * moles))) + + +if __name__ == "__main__": + + import doctest + + doctest.testmod() diff --git a/conversions/octal_to_decimal.py b/conversions/octal_to_decimal.py new file mode 100644 index 000000000000..5a7373fef7e3 --- /dev/null +++ b/conversions/octal_to_decimal.py @@ -0,0 +1,43 @@ +def oct_to_decimal(oct_string: str) -> int: + """ + Convert a octal value to its decimal equivalent + + >>> oct_to_decimal("12") + 10 + >>> oct_to_decimal(" 12 ") + 10 + >>> oct_to_decimal("-45") + -37 + >>> oct_to_decimal("2-0Fm") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("") + Traceback (most recent call last): + ... + ValueError: Empty string was passed to the function + >>> oct_to_decimal("19") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + """ + oct_string = str(oct_string).strip() + if not oct_string: + raise ValueError("Empty string was passed to the function") + is_negative = oct_string[0] == "-" + if is_negative: + oct_string = oct_string[1:] + if not oct_string.isdigit() or not all(0 <= int(char) <= 7 for char in oct_string): + raise ValueError("Non-octal value was passed to the function") + decimal_number = 0 + for char in oct_string: + decimal_number = 8 * decimal_number + int(char) + if is_negative: + decimal_number = -decimal_number + return decimal_number + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/conversions/prefix_conversions.py b/conversions/prefix_conversions.py new file mode 100644 index 000000000000..c2440d1cf886 --- /dev/null +++ b/conversions/prefix_conversions.py @@ -0,0 +1,100 @@ +""" +Convert International System of Units (SI) and Binary prefixes +""" +from enum import Enum +from typing import Union + + +class SI_Unit(Enum): + yotta = 24 + zetta = 21 + exa = 18 + peta = 15 + tera = 12 + giga = 9 + mega = 6 + kilo = 3 + hecto = 2 + deca = 1 + deci = -1 + centi = -2 + milli = -3 + micro = -6 + nano = -9 + pico = -12 + femto = -15 + atto = -18 + zepto = -21 + yocto = -24 + + +class Binary_Unit(Enum): + yotta = 8 + zetta = 7 + exa = 6 + peta = 5 + tera = 4 + giga = 3 + mega = 2 + kilo = 1 + + +def convert_si_prefix( + known_amount: float, + known_prefix: Union[str, SI_Unit], + unknown_prefix: Union[str, SI_Unit], +) -> float: + """ + Wikipedia reference: https://en.wikipedia.org/wiki/Binary_prefix + Wikipedia reference: https://en.wikipedia.org/wiki/International_System_of_Units + >>> convert_si_prefix(1, SI_Unit.giga, SI_Unit.mega) + 1000 + >>> convert_si_prefix(1, SI_Unit.mega, SI_Unit.giga) + 0.001 + >>> convert_si_prefix(1, SI_Unit.kilo, SI_Unit.kilo) + 1 + >>> convert_si_prefix(1, 'giga', 'mega') + 1000 + >>> convert_si_prefix(1, 'gIGa', 'mEGa') + 1000 + """ + if isinstance(known_prefix, str): + known_prefix: SI_Unit = SI_Unit[known_prefix.lower()] + if isinstance(unknown_prefix, str): + unknown_prefix: SI_Unit = SI_Unit[unknown_prefix.lower()] + unknown_amount = known_amount * (10 ** (known_prefix.value - unknown_prefix.value)) + return unknown_amount + + +def convert_binary_prefix( + known_amount: float, + known_prefix: Union[str, Binary_Unit], + unknown_prefix: Union[str, Binary_Unit], +) -> float: + """ + Wikipedia reference: https://en.wikipedia.org/wiki/Metric_prefix + >>> convert_binary_prefix(1, Binary_Unit.giga, Binary_Unit.mega) + 1024 + >>> convert_binary_prefix(1, Binary_Unit.mega, Binary_Unit.giga) + 0.0009765625 + >>> convert_binary_prefix(1, Binary_Unit.kilo, Binary_Unit.kilo) + 1 + >>> convert_binary_prefix(1, 'giga', 'mega') + 1024 + >>> convert_binary_prefix(1, 'gIGa', 'mEGa') + 1024 + """ + if isinstance(known_prefix, str): + known_prefix: Binary_Unit = Binary_Unit[known_prefix.lower()] + if isinstance(unknown_prefix, str): + unknown_prefix: Binary_Unit = Binary_Unit[unknown_prefix.lower()] + unknown_amount = known_amount * ( + 2 ** ((known_prefix.value - unknown_prefix.value) * 10) + ) + return unknown_amount + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py new file mode 100644 index 000000000000..9933e6a78a4d --- /dev/null +++ b/conversions/roman_numerals.py @@ -0,0 +1,59 @@ +def roman_to_int(roman: str) -> int: + """ + LeetCode No. 13 Roman to Integer + Given a roman numeral, convert it to an integer. + Input is guaranteed to be within the range from 1 to 3999. + https://en.wikipedia.org/wiki/Roman_numerals + >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} + >>> all(roman_to_int(key) == value for key, value in tests.items()) + True + """ + vals = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} + total = 0 + place = 0 + while place < len(roman): + if (place + 1 < len(roman)) and (vals[roman[place]] < vals[roman[place + 1]]): + total += vals[roman[place + 1]] - vals[roman[place]] + place += 2 + else: + total += vals[roman[place]] + place += 1 + return total + + +def int_to_roman(number: int) -> str: + """ + Given a integer, convert it to an roman numeral. + https://en.wikipedia.org/wiki/Roman_numerals + >>> tests = {"III": 3, "CLIV": 154, "MIX": 1009, "MMD": 2500, "MMMCMXCIX": 3999} + >>> all(int_to_roman(value) == key for key, value in tests.items()) + True + """ + ROMAN = [ + (1000, "M"), + (900, "CM"), + (500, "D"), + (400, "CD"), + (100, "C"), + (90, "XC"), + (50, "L"), + (40, "XL"), + (10, "X"), + (9, "IX"), + (5, "V"), + (4, "IV"), + (1, "I"), + ] + result = [] + for (arabic, roman) in ROMAN: + (factor, number) = divmod(number, arabic) + result.append(roman * factor) + if number == 0: + break + return "".join(result) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/conversions/temperature_conversions.py b/conversions/temperature_conversions.py new file mode 100644 index 000000000000..167c9dc64727 --- /dev/null +++ b/conversions/temperature_conversions.py @@ -0,0 +1,386 @@ +""" Convert between different units of temperature """ + + +def celsius_to_fahrenheit(celsius: float, ndigits: int = 2) -> float: + """ + Convert a given value from Celsius to Fahrenheit and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Celsius + Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit + + >>> celsius_to_fahrenheit(273.354, 3) + 524.037 + >>> celsius_to_fahrenheit(273.354, 0) + 524.0 + >>> celsius_to_fahrenheit(-40.0) + -40.0 + >>> celsius_to_fahrenheit(-20.0) + -4.0 + >>> celsius_to_fahrenheit(0) + 32.0 + >>> celsius_to_fahrenheit(20) + 68.0 + >>> celsius_to_fahrenheit("40") + 104.0 + >>> celsius_to_fahrenheit("celsius") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'celsius' + """ + return round((float(celsius) * 9 / 5) + 32, ndigits) + + +def celsius_to_kelvin(celsius: float, ndigits: int = 2) -> float: + """ + Convert a given value from Celsius to Kelvin and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Celsius + Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin + + >>> celsius_to_kelvin(273.354, 3) + 546.504 + >>> celsius_to_kelvin(273.354, 0) + 547.0 + >>> celsius_to_kelvin(0) + 273.15 + >>> celsius_to_kelvin(20.0) + 293.15 + >>> celsius_to_kelvin("40") + 313.15 + >>> celsius_to_kelvin("celsius") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'celsius' + """ + return round(float(celsius) + 273.15, ndigits) + + +def celsius_to_rankine(celsius: float, ndigits: int = 2) -> float: + """ + Convert a given value from Celsius to Rankine and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Celsius + Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale + + >>> celsius_to_rankine(273.354, 3) + 983.707 + >>> celsius_to_rankine(273.354, 0) + 984.0 + >>> celsius_to_rankine(0) + 491.67 + >>> celsius_to_rankine(20.0) + 527.67 + >>> celsius_to_rankine("40") + 563.67 + >>> celsius_to_rankine("celsius") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'celsius' + """ + return round((float(celsius) * 9 / 5) + 491.67, ndigits) + + +def fahrenheit_to_celsius(fahrenheit: float, ndigits: int = 2) -> float: + """ + Convert a given value from Fahrenheit to Celsius and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit + Wikipedia reference: https://en.wikipedia.org/wiki/Celsius + + >>> fahrenheit_to_celsius(273.354, 3) + 134.086 + >>> fahrenheit_to_celsius(273.354, 0) + 134.0 + >>> fahrenheit_to_celsius(0) + -17.78 + >>> fahrenheit_to_celsius(20.0) + -6.67 + >>> fahrenheit_to_celsius(40.0) + 4.44 + >>> fahrenheit_to_celsius(60) + 15.56 + >>> fahrenheit_to_celsius(80) + 26.67 + >>> fahrenheit_to_celsius("100") + 37.78 + >>> fahrenheit_to_celsius("fahrenheit") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'fahrenheit' + """ + return round((float(fahrenheit) - 32) * 5 / 9, ndigits) + + +def fahrenheit_to_kelvin(fahrenheit: float, ndigits: int = 2) -> float: + """ + Convert a given value from Fahrenheit to Kelvin and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit + Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin + + >>> fahrenheit_to_kelvin(273.354, 3) + 407.236 + >>> fahrenheit_to_kelvin(273.354, 0) + 407.0 + >>> fahrenheit_to_kelvin(0) + 255.37 + >>> fahrenheit_to_kelvin(20.0) + 266.48 + >>> fahrenheit_to_kelvin(40.0) + 277.59 + >>> fahrenheit_to_kelvin(60) + 288.71 + >>> fahrenheit_to_kelvin(80) + 299.82 + >>> fahrenheit_to_kelvin("100") + 310.93 + >>> fahrenheit_to_kelvin("fahrenheit") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'fahrenheit' + """ + return round(((float(fahrenheit) - 32) * 5 / 9) + 273.15, ndigits) + + +def fahrenheit_to_rankine(fahrenheit: float, ndigits: int = 2) -> float: + """ + Convert a given value from Fahrenheit to Rankine and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit + Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale + + >>> fahrenheit_to_rankine(273.354, 3) + 733.024 + >>> fahrenheit_to_rankine(273.354, 0) + 733.0 + >>> fahrenheit_to_rankine(0) + 459.67 + >>> fahrenheit_to_rankine(20.0) + 479.67 + >>> fahrenheit_to_rankine(40.0) + 499.67 + >>> fahrenheit_to_rankine(60) + 519.67 + >>> fahrenheit_to_rankine(80) + 539.67 + >>> fahrenheit_to_rankine("100") + 559.67 + >>> fahrenheit_to_rankine("fahrenheit") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'fahrenheit' + """ + return round(float(fahrenheit) + 459.67, ndigits) + + +def kelvin_to_celsius(kelvin: float, ndigits: int = 2) -> float: + """ + Convert a given value from Kelvin to Celsius and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin + Wikipedia reference: https://en.wikipedia.org/wiki/Celsius + + >>> kelvin_to_celsius(273.354, 3) + 0.204 + >>> kelvin_to_celsius(273.354, 0) + 0.0 + >>> kelvin_to_celsius(273.15) + 0.0 + >>> kelvin_to_celsius(300) + 26.85 + >>> kelvin_to_celsius("315.5") + 42.35 + >>> kelvin_to_celsius("kelvin") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'kelvin' + """ + return round(float(kelvin) - 273.15, ndigits) + + +def kelvin_to_fahrenheit(kelvin: float, ndigits: int = 2) -> float: + """ + Convert a given value from Kelvin to Fahrenheit and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin + Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit + + >>> kelvin_to_fahrenheit(273.354, 3) + 32.367 + >>> kelvin_to_fahrenheit(273.354, 0) + 32.0 + >>> kelvin_to_fahrenheit(273.15) + 32.0 + >>> kelvin_to_fahrenheit(300) + 80.33 + >>> kelvin_to_fahrenheit("315.5") + 108.23 + >>> kelvin_to_fahrenheit("kelvin") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'kelvin' + """ + return round(((float(kelvin) - 273.15) * 9 / 5) + 32, ndigits) + + +def kelvin_to_rankine(kelvin: float, ndigits: int = 2) -> float: + """ + Convert a given value from Kelvin to Rankine and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin + Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale + + >>> kelvin_to_rankine(273.354, 3) + 492.037 + >>> kelvin_to_rankine(273.354, 0) + 492.0 + >>> kelvin_to_rankine(0) + 0.0 + >>> kelvin_to_rankine(20.0) + 36.0 + >>> kelvin_to_rankine("40") + 72.0 + >>> kelvin_to_rankine("kelvin") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'kelvin' + """ + return round((float(kelvin) * 9 / 5), ndigits) + + +def rankine_to_celsius(rankine: float, ndigits: int = 2) -> float: + """ + Convert a given value from Rankine to Celsius and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale + Wikipedia reference: https://en.wikipedia.org/wiki/Celsius + + >>> rankine_to_celsius(273.354, 3) + -121.287 + >>> rankine_to_celsius(273.354, 0) + -121.0 + >>> rankine_to_celsius(273.15) + -121.4 + >>> rankine_to_celsius(300) + -106.48 + >>> rankine_to_celsius("315.5") + -97.87 + >>> rankine_to_celsius("rankine") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'rankine' + """ + return round((float(rankine) - 491.67) * 5 / 9, ndigits) + + +def rankine_to_fahrenheit(rankine: float, ndigits: int = 2) -> float: + """ + Convert a given value from Rankine to Fahrenheit and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale + Wikipedia reference: https://en.wikipedia.org/wiki/Fahrenheit + + >>> rankine_to_fahrenheit(273.15) + -186.52 + >>> rankine_to_fahrenheit(300) + -159.67 + >>> rankine_to_fahrenheit("315.5") + -144.17 + >>> rankine_to_fahrenheit("rankine") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'rankine' + """ + return round(float(rankine) - 459.67, ndigits) + + +def rankine_to_kelvin(rankine: float, ndigits: int = 2) -> float: + """ + Convert a given value from Rankine to Kelvin and round it to 2 decimal places. + Wikipedia reference: https://en.wikipedia.org/wiki/Rankine_scale + Wikipedia reference: https://en.wikipedia.org/wiki/Kelvin + + >>> rankine_to_kelvin(0) + 0.0 + >>> rankine_to_kelvin(20.0) + 11.11 + >>> rankine_to_kelvin("40") + 22.22 + >>> rankine_to_kelvin("rankine") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'rankine' + """ + return round((float(rankine) * 5 / 9), ndigits) + + +def reaumur_to_kelvin(reaumur: float, ndigits: int = 2) -> float: + """ + Convert a given value from reaumur to Kelvin and round it to 2 decimal places. + Reference:- http://www.csgnetwork.com/temp2conv.html + + >>> reaumur_to_kelvin(0) + 273.15 + >>> reaumur_to_kelvin(20.0) + 298.15 + >>> reaumur_to_kelvin(40) + 323.15 + >>> reaumur_to_kelvin("reaumur") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'reaumur' + """ + return round((float(reaumur) * 1.25 + 273.15), ndigits) + + +def reaumur_to_fahrenheit(reaumur: float, ndigits: int = 2) -> float: + """ + Convert a given value from reaumur to fahrenheit and round it to 2 decimal places. + Reference:- http://www.csgnetwork.com/temp2conv.html + + >>> reaumur_to_fahrenheit(0) + 32.0 + >>> reaumur_to_fahrenheit(20.0) + 77.0 + >>> reaumur_to_fahrenheit(40) + 122.0 + >>> reaumur_to_fahrenheit("reaumur") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'reaumur' + """ + return round((float(reaumur) * 2.25 + 32), ndigits) + + +def reaumur_to_celsius(reaumur: float, ndigits: int = 2) -> float: + """ + Convert a given value from reaumur to celsius and round it to 2 decimal places. + Reference:- http://www.csgnetwork.com/temp2conv.html + + >>> reaumur_to_celsius(0) + 0.0 + >>> reaumur_to_celsius(20.0) + 25.0 + >>> reaumur_to_celsius(40) + 50.0 + >>> reaumur_to_celsius("reaumur") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'reaumur' + """ + return round((float(reaumur) * 1.25), ndigits) + + +def reaumur_to_rankine(reaumur: float, ndigits: int = 2) -> float: + """ + Convert a given value from reaumur to rankine and round it to 2 decimal places. + Reference:- http://www.csgnetwork.com/temp2conv.html + + >>> reaumur_to_rankine(0) + 491.67 + >>> reaumur_to_rankine(20.0) + 536.67 + >>> reaumur_to_rankine(40) + 581.67 + >>> reaumur_to_rankine("reaumur") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'reaumur' + """ + return round((float(reaumur) * 2.25 + 32 + 459.67), ndigits) + + +if __name__ == "__main__": + + import doctest + + doctest.testmod() diff --git a/conversions/weight_conversion.py b/conversions/weight_conversion.py new file mode 100644 index 000000000000..85515f2f6f88 --- /dev/null +++ b/conversions/weight_conversion.py @@ -0,0 +1,287 @@ +""" +Conversion of weight units. + +__author__ = "Anubhav Solanki" +__license__ = "MIT" +__version__ = "1.0.0" +__maintainer__ = "Anubhav Solanki" +__email__ = "anubhavsolanki0@gmail.com" + +USAGE : +-> Import this file into their respective project. +-> Use the function weight_conversion() for conversion of weight units. +-> Parameters : + -> from_type : From which type you want to convert + -> to_type : To which type you want to convert + -> value : the value which you want to convert + +REFERENCES : + +-> Wikipedia reference: https://en.wikipedia.org/wiki/Kilogram +-> Wikipedia reference: https://en.wikipedia.org/wiki/Gram +-> Wikipedia reference: https://en.wikipedia.org/wiki/Millimetre +-> Wikipedia reference: https://en.wikipedia.org/wiki/Tonne +-> Wikipedia reference: https://en.wikipedia.org/wiki/Long_ton +-> Wikipedia reference: https://en.wikipedia.org/wiki/Short_ton +-> Wikipedia reference: https://en.wikipedia.org/wiki/Pound +-> Wikipedia reference: https://en.wikipedia.org/wiki/Ounce +-> Wikipedia reference: https://en.wikipedia.org/wiki/Fineness#Karat +-> Wikipedia reference: https://en.wikipedia.org/wiki/Dalton_(unit) +""" + +KILOGRAM_CHART = { + "kilogram": 1, + "gram": pow(10, 3), + "milligram": pow(10, 6), + "metric-ton": pow(10, -3), + "long-ton": 0.0009842073, + "short-ton": 0.0011023122, + "pound": 2.2046244202, + "ounce": 35.273990723, + "carrat": 5000, + "atomic-mass-unit": 6.022136652e26, +} + +WEIGHT_TYPE_CHART = { + "kilogram": 1, + "gram": pow(10, -3), + "milligram": pow(10, -6), + "metric-ton": pow(10, 3), + "long-ton": 1016.04608, + "short-ton": 907.184, + "pound": 0.453592, + "ounce": 0.0283495, + "carrat": 0.0002, + "atomic-mass-unit": 1.660540199e-27, +} + + +def weight_conversion(from_type: str, to_type: str, value: float) -> float: + """ + Conversion of weight unit with the help of KILOGRAM_CHART + + "kilogram" : 1, + "gram" : pow(10, 3), + "milligram" : pow(10, 6), + "metric-ton" : pow(10, -3), + "long-ton" : 0.0009842073, + "short-ton" : 0.0011023122, + "pound" : 2.2046244202, + "ounce" : 35.273990723, + "carrat" : 5000, + "atomic-mass-unit" : 6.022136652E+26 + + >>> weight_conversion("kilogram","kilogram",4) + 4 + >>> weight_conversion("kilogram","gram",1) + 1000 + >>> weight_conversion("kilogram","milligram",4) + 4000000 + >>> weight_conversion("kilogram","metric-ton",4) + 0.004 + >>> weight_conversion("kilogram","long-ton",3) + 0.0029526219 + >>> weight_conversion("kilogram","short-ton",1) + 0.0011023122 + >>> weight_conversion("kilogram","pound",4) + 8.8184976808 + >>> weight_conversion("kilogram","ounce",4) + 141.095962892 + >>> weight_conversion("kilogram","carrat",3) + 15000 + >>> weight_conversion("kilogram","atomic-mass-unit",1) + 6.022136652e+26 + >>> weight_conversion("gram","kilogram",1) + 0.001 + >>> weight_conversion("gram","gram",3) + 3.0 + >>> weight_conversion("gram","milligram",2) + 2000.0 + >>> weight_conversion("gram","metric-ton",4) + 4e-06 + >>> weight_conversion("gram","long-ton",3) + 2.9526219e-06 + >>> weight_conversion("gram","short-ton",3) + 3.3069366000000003e-06 + >>> weight_conversion("gram","pound",3) + 0.0066138732606 + >>> weight_conversion("gram","ounce",1) + 0.035273990723 + >>> weight_conversion("gram","carrat",2) + 10.0 + >>> weight_conversion("gram","atomic-mass-unit",1) + 6.022136652e+23 + >>> weight_conversion("milligram","kilogram",1) + 1e-06 + >>> weight_conversion("milligram","gram",2) + 0.002 + >>> weight_conversion("milligram","milligram",3) + 3.0 + >>> weight_conversion("milligram","metric-ton",3) + 3e-09 + >>> weight_conversion("milligram","long-ton",3) + 2.9526219e-09 + >>> weight_conversion("milligram","short-ton",1) + 1.1023122e-09 + >>> weight_conversion("milligram","pound",3) + 6.6138732605999995e-06 + >>> weight_conversion("milligram","ounce",2) + 7.054798144599999e-05 + >>> weight_conversion("milligram","carrat",1) + 0.005 + >>> weight_conversion("milligram","atomic-mass-unit",1) + 6.022136652e+20 + >>> weight_conversion("metric-ton","kilogram",2) + 2000 + >>> weight_conversion("metric-ton","gram",2) + 2000000 + >>> weight_conversion("metric-ton","milligram",3) + 3000000000 + >>> weight_conversion("metric-ton","metric-ton",2) + 2.0 + >>> weight_conversion("metric-ton","long-ton",3) + 2.9526219 + >>> weight_conversion("metric-ton","short-ton",2) + 2.2046244 + >>> weight_conversion("metric-ton","pound",3) + 6613.8732606 + >>> weight_conversion("metric-ton","ounce",4) + 141095.96289199998 + >>> weight_conversion("metric-ton","carrat",4) + 20000000 + >>> weight_conversion("metric-ton","atomic-mass-unit",1) + 6.022136652e+29 + >>> weight_conversion("long-ton","kilogram",4) + 4064.18432 + >>> weight_conversion("long-ton","gram",4) + 4064184.32 + >>> weight_conversion("long-ton","milligram",3) + 3048138240.0 + >>> weight_conversion("long-ton","metric-ton",4) + 4.06418432 + >>> weight_conversion("long-ton","long-ton",3) + 2.999999907217152 + >>> weight_conversion("long-ton","short-ton",1) + 1.119999989746176 + >>> weight_conversion("long-ton","pound",3) + 6720.000000049448 + >>> weight_conversion("long-ton","ounce",1) + 35840.000000060514 + >>> weight_conversion("long-ton","carrat",4) + 20320921.599999998 + >>> weight_conversion("long-ton","atomic-mass-unit",4) + 2.4475073353955697e+30 + >>> weight_conversion("short-ton","kilogram",3) + 2721.5519999999997 + >>> weight_conversion("short-ton","gram",3) + 2721552.0 + >>> weight_conversion("short-ton","milligram",1) + 907184000.0 + >>> weight_conversion("short-ton","metric-ton",4) + 3.628736 + >>> weight_conversion("short-ton","long-ton",3) + 2.6785713457296 + >>> weight_conversion("short-ton","short-ton",3) + 2.9999999725344 + >>> weight_conversion("short-ton","pound",2) + 4000.0000000294335 + >>> weight_conversion("short-ton","ounce",4) + 128000.00000021611 + >>> weight_conversion("short-ton","carrat",4) + 18143680.0 + >>> weight_conversion("short-ton","atomic-mass-unit",1) + 5.463186016507968e+29 + >>> weight_conversion("pound","kilogram",4) + 1.814368 + >>> weight_conversion("pound","gram",2) + 907.184 + >>> weight_conversion("pound","milligram",3) + 1360776.0 + >>> weight_conversion("pound","metric-ton",3) + 0.001360776 + >>> weight_conversion("pound","long-ton",2) + 0.0008928571152432 + >>> weight_conversion("pound","short-ton",1) + 0.0004999999954224 + >>> weight_conversion("pound","pound",3) + 3.0000000000220752 + >>> weight_conversion("pound","ounce",1) + 16.000000000027015 + >>> weight_conversion("pound","carrat",1) + 2267.96 + >>> weight_conversion("pound","atomic-mass-unit",4) + 1.0926372033015936e+27 + >>> weight_conversion("ounce","kilogram",3) + 0.0850485 + >>> weight_conversion("ounce","gram",3) + 85.0485 + >>> weight_conversion("ounce","milligram",4) + 113398.0 + >>> weight_conversion("ounce","metric-ton",4) + 0.000113398 + >>> weight_conversion("ounce","long-ton",4) + 0.0001116071394054 + >>> weight_conversion("ounce","short-ton",4) + 0.0001249999988556 + >>> weight_conversion("ounce","pound",1) + 0.0625000000004599 + >>> weight_conversion("ounce","ounce",2) + 2.000000000003377 + >>> weight_conversion("ounce","carrat",1) + 141.7475 + >>> weight_conversion("ounce","atomic-mass-unit",1) + 1.70724563015874e+25 + >>> weight_conversion("carrat","kilogram",1) + 0.0002 + >>> weight_conversion("carrat","gram",4) + 0.8 + >>> weight_conversion("carrat","milligram",2) + 400.0 + >>> weight_conversion("carrat","metric-ton",2) + 4.0000000000000003e-07 + >>> weight_conversion("carrat","long-ton",3) + 5.9052438e-07 + >>> weight_conversion("carrat","short-ton",4) + 8.818497600000002e-07 + >>> weight_conversion("carrat","pound",1) + 0.00044092488404000004 + >>> weight_conversion("carrat","ounce",2) + 0.0141095962892 + >>> weight_conversion("carrat","carrat",4) + 4.0 + >>> weight_conversion("carrat","atomic-mass-unit",4) + 4.8177093216e+23 + >>> weight_conversion("atomic-mass-unit","kilogram",4) + 6.642160796e-27 + >>> weight_conversion("atomic-mass-unit","gram",2) + 3.321080398e-24 + >>> weight_conversion("atomic-mass-unit","milligram",2) + 3.3210803980000002e-21 + >>> weight_conversion("atomic-mass-unit","metric-ton",3) + 4.9816205970000004e-30 + >>> weight_conversion("atomic-mass-unit","long-ton",3) + 4.9029473573977584e-30 + >>> weight_conversion("atomic-mass-unit","short-ton",1) + 1.830433719948128e-30 + >>> weight_conversion("atomic-mass-unit","pound",3) + 1.0982602420317504e-26 + >>> weight_conversion("atomic-mass-unit","ounce",2) + 1.1714775914938915e-25 + >>> weight_conversion("atomic-mass-unit","carrat",2) + 1.660540199e-23 + >>> weight_conversion("atomic-mass-unit","atomic-mass-unit",2) + 1.999999998903455 + """ + if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART: + raise ValueError( + f"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" + f"Supported values are: {', '.join(WEIGHT_TYPE_CHART)}" + ) + return value * KILOGRAM_CHART[to_type] * WEIGHT_TYPE_CHART[from_type] + + +if __name__ == "__main__": + + import doctest + + doctest.testmod() diff --git a/data_structures/arrays.py b/data_structures/arrays.py deleted file mode 100644 index feb061013556..000000000000 --- a/data_structures/arrays.py +++ /dev/null @@ -1,3 +0,0 @@ -arr = [10, 20, 30, 40] -arr[1] = 30 # set element 1 (20) of array to 30 -print(arr) diff --git a/data_structures/avl.py b/data_structures/avl.py deleted file mode 100644 index d01e8f825368..000000000000 --- a/data_structures/avl.py +++ /dev/null @@ -1,181 +0,0 @@ -""" -An AVL tree -""" -from __future__ import print_function - - -class Node: - - def __init__(self, label): - self.label = label - self._parent = None - self._left = None - self._right = None - self.height = 0 - - @property - def right(self): - return self._right - - @right.setter - def right(self, node): - if node is not None: - node._parent = self - self._right = node - - @property - def left(self): - return self._left - - @left.setter - def left(self, node): - if node is not None: - node._parent = self - self._left = node - - @property - def parent(self): - return self._parent - - @parent.setter - def parent(self, node): - if node is not None: - self._parent = node - self.height = self.parent.height + 1 - else: - self.height = 0 - - -class AVL: - - def __init__(self): - self.root = None - self.size = 0 - - def insert(self, value): - node = Node(value) - - if self.root is None: - self.root = node - self.root.height = 0 - self.size = 1 - else: - # Same as Binary Tree - dad_node = None - curr_node = self.root - - while True: - if curr_node is not None: - - dad_node = curr_node - - if node.label < curr_node.label: - curr_node = curr_node.left - else: - curr_node = curr_node.right - else: - node.height = dad_node.height - dad_node.height += 1 - if node.label < dad_node.label: - dad_node.left = node - else: - dad_node.right = node - self.rebalance(node) - self.size += 1 - break - - def rebalance(self, node): - n = node - - while n is not None: - height_right = n.height - height_left = n.height - - if n.right is not None: - height_right = n.right.height - - if n.left is not None: - height_left = n.left.height - - if abs(height_left - height_right) > 1: - if height_left > height_right: - left_child = n.left - if left_child is not None: - h_right = (left_child.right.height - if (left_child.right is not None) else 0) - h_left = (left_child.left.height - if (left_child.left is not None) else 0) - if (h_left > h_right): - self.rotate_left(n) - break - else: - self.double_rotate_right(n) - break - else: - right_child = n.right - if right_child is not None: - h_right = (right_child.right.height - if (right_child.right is not None) else 0) - h_left = (right_child.left.height - if (right_child.left is not None) else 0) - if (h_left > h_right): - self.double_rotate_left(n) - break - else: - self.rotate_right(n) - break - n = n.parent - - def rotate_left(self, node): - aux = node.parent.label - node.parent.label = node.label - node.parent.right = Node(aux) - node.parent.right.height = node.parent.height + 1 - node.parent.left = node.right - - - def rotate_right(self, node): - aux = node.parent.label - node.parent.label = node.label - node.parent.left = Node(aux) - node.parent.left.height = node.parent.height + 1 - node.parent.right = node.right - - def double_rotate_left(self, node): - self.rotate_right(node.getRight().getRight()) - self.rotate_left(node) - - def double_rotate_right(self, node): - self.rotate_left(node.getLeft().getLeft()) - self.rotate_right(node) - - def empty(self): - if self.root is None: - return True - return False - - def preShow(self, curr_node): - if curr_node is not None: - self.preShow(curr_node.left) - print(curr_node.label, end=" ") - self.preShow(curr_node.right) - - def preorder(self, curr_node): - if curr_node is not None: - self.preShow(curr_node.left) - self.preShow(curr_node.right) - print(curr_node.label, end=" ") - - def getRoot(self): - return self.root - -t = AVL() -t.insert(1) -t.insert(2) -t.insert(3) -# t.preShow(t.root) -# print("\n") -# t.insert(4) -# t.insert(5) -# t.preShow(t.root) -# t.preorden(t.root) diff --git a/data_structures/binary tree/AVLtree.py b/data_structures/binary tree/AVLtree.py deleted file mode 100644 index ff44963d1690..000000000000 --- a/data_structures/binary tree/AVLtree.py +++ /dev/null @@ -1,255 +0,0 @@ -# -*- coding: utf-8 -*- -''' -An auto-balanced binary tree! -''' -import math -import random -class my_queue: - def __init__(self): - self.data = [] - self.head = 0 - self.tail = 0 - def isEmpty(self): - return self.head == self.tail - def push(self,data): - self.data.append(data) - self.tail = self.tail + 1 - def pop(self): - ret = self.data[self.head] - self.head = self.head + 1 - return ret - def count(self): - return self.tail - self.head - def print(self): - print(self.data) - print("**************") - print(self.data[self.head:self.tail]) - -class my_node: - def __init__(self,data): - self.data = data - self.left = None - self.right = None - self.height = 1 - def getdata(self): - return self.data - def getleft(self): - return self.left - def getright(self): - return self.right - def getheight(self): - return self.height - def setdata(self,data): - self.data = data - return - def setleft(self,node): - self.left = node - return - def setright(self,node): - self.right = node - return - def setheight(self,height): - self.height = height - return - -def getheight(node): - if node is None: - return 0 - return node.getheight() - -def my_max(a,b): - if a > b: - return a - return b - - - -def leftrotation(node): - r''' - A B - / \ / \ - B C Bl A - / \ --> / / \ - Bl Br UB Br C - / - UB - - UB = unbalanced node - ''' - print("left rotation node:",node.getdata()) - ret = node.getleft() - node.setleft(ret.getright()) - ret.setright(node) - h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 - node.setheight(h1) - h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1 - ret.setheight(h2) - return ret - -def rightrotation(node): - ''' - a mirror symmetry rotation of the leftrotation - ''' - print("right rotation node:",node.getdata()) - ret = node.getright() - node.setright(ret.getleft()) - ret.setleft(node) - h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 - node.setheight(h1) - h2 = my_max(getheight(ret.getright()),getheight(ret.getleft())) + 1 - ret.setheight(h2) - return ret - -def rlrotation(node): - r''' - A A Br - / \ / \ / \ - B C RR Br C LR B A - / \ --> / \ --> / / \ - Bl Br B UB Bl UB C - \ / - UB Bl - RR = rightrotation LR = leftrotation - ''' - node.setleft(rightrotation(node.getleft())) - return leftrotation(node) - -def lrrotation(node): - node.setright(leftrotation(node.getright())) - return rightrotation(node) - - -def insert_node(node,data): - if node is None: - return my_node(data) - if data < node.getdata(): - node.setleft(insert_node(node.getleft(),data)) - if getheight(node.getleft()) - getheight(node.getright()) == 2: #an unbalance detected - if data < node.getleft().getdata(): #new node is the left child of the left child - node = leftrotation(node) - else: - node = rlrotation(node) #new node is the right child of the left child - else: - node.setright(insert_node(node.getright(),data)) - if getheight(node.getright()) - getheight(node.getleft()) == 2: - if data < node.getright().getdata(): - node = lrrotation(node) - else: - node = rightrotation(node) - h1 = my_max(getheight(node.getright()),getheight(node.getleft())) + 1 - node.setheight(h1) - return node - -def getRightMost(root): - while root.getright() is not None: - root = root.getright() - return root.getdata() -def getLeftMost(root): - while root.getleft() is not None: - root = root.getleft() - return root.getdata() - -def del_node(root,data): - if root.getdata() == data: - if root.getleft() is not None and root.getright() is not None: - temp_data = getLeftMost(root.getright()) - root.setdata(temp_data) - root.setright(del_node(root.getright(),temp_data)) - elif root.getleft() is not None: - root = root.getleft() - else: - root = root.getright() - elif root.getdata() > data: - if root.getleft() is None: - print("No such data") - return root - else: - root.setleft(del_node(root.getleft(),data)) - elif root.getdata() < data: - if root.getright() is None: - return root - else: - root.setright(del_node(root.getright(),data)) - if root is None: - return root - if getheight(root.getright()) - getheight(root.getleft()) == 2: - if getheight(root.getright().getright()) > getheight(root.getright().getleft()): - root = rightrotation(root) - else: - root = lrrotation(root) - elif getheight(root.getright()) - getheight(root.getleft()) == -2: - if getheight(root.getleft().getleft()) > getheight(root.getleft().getright()): - root = leftrotation(root) - else: - root = rlrotation(root) - height = my_max(getheight(root.getright()),getheight(root.getleft())) + 1 - root.setheight(height) - return root - -class AVLtree: - def __init__(self): - self.root = None - def getheight(self): -# print("yyy") - return getheight(self.root) - def insert(self,data): - print("insert:"+str(data)) - self.root = insert_node(self.root,data) - - def del_node(self,data): - print("delete:"+str(data)) - if self.root is None: - print("Tree is empty!") - return - self.root = del_node(self.root,data) - def traversale(self): #a level traversale, gives a more intuitive look on the tree - q = my_queue() - q.push(self.root) - layer = self.getheight() - if layer == 0: - return - cnt = 0 - while not q.isEmpty(): - node = q.pop() - space = " "*int(math.pow(2,layer-1)) - print(space,end = "") - if node is None: - print("*",end = "") - q.push(None) - q.push(None) - else: - print(node.getdata(),end = "") - q.push(node.getleft()) - q.push(node.getright()) - print(space,end = "") - cnt = cnt + 1 - for i in range(100): - if cnt == math.pow(2,i) - 1: - layer = layer -1 - if layer == 0: - print() - print("*************************************") - return - print() - break - print() - print("*************************************") - return - - def test(self): - getheight(None) - print("****") - self.getheight() -if __name__ == "__main__": - t = AVLtree() - t.traversale() - l = list(range(10)) - random.shuffle(l) - for i in l: - t.insert(i) - t.traversale() - - random.shuffle(l) - for i in l: - t.del_node(i) - t.traversale() diff --git a/data_structures/binary tree/binary_search_tree.py b/data_structures/binary tree/binary_search_tree.py deleted file mode 100644 index cef5b55f245d..000000000000 --- a/data_structures/binary tree/binary_search_tree.py +++ /dev/null @@ -1,258 +0,0 @@ -''' -A binary search Tree -''' -from __future__ import print_function -class Node: - - def __init__(self, label, parent): - self.label = label - self.left = None - self.right = None - #Added in order to delete a node easier - self.parent = parent - - def getLabel(self): - return self.label - - def setLabel(self, label): - self.label = label - - def getLeft(self): - return self.left - - def setLeft(self, left): - self.left = left - - def getRight(self): - return self.right - - def setRight(self, right): - self.right = right - - def getParent(self): - return self.parent - - def setParent(self, parent): - self.parent = parent - -class BinarySearchTree: - - def __init__(self): - self.root = None - - def insert(self, label): - # Create a new Node - new_node = Node(label, None) - # If Tree is empty - if self.empty(): - self.root = new_node - else: - #If Tree is not empty - curr_node = self.root - #While we don't get to a leaf - while curr_node is not None: - #We keep reference of the parent node - parent_node = curr_node - #If node label is less than current node - if new_node.getLabel() < curr_node.getLabel(): - #We go left - curr_node = curr_node.getLeft() - else: - #Else we go right - curr_node = curr_node.getRight() - #We insert the new node in a leaf - if new_node.getLabel() < parent_node.getLabel(): - parent_node.setLeft(new_node) - else: - parent_node.setRight(new_node) - #Set parent to the new node - new_node.setParent(parent_node) - - def delete(self, label): - if (not self.empty()): - #Look for the node with that label - node = self.getNode(label) - #If the node exists - if(node is not None): - #If it has no children - if(node.getLeft() is None and node.getRight() is None): - self.__reassignNodes(node, None) - node = None - #Has only right children - elif(node.getLeft() is None and node.getRight() is not None): - self.__reassignNodes(node, node.getRight()) - #Has only left children - elif(node.getLeft() is not None and node.getRight() is None): - self.__reassignNodes(node, node.getLeft()) - #Has two children - else: - #Gets the max value of the left branch - tmpNode = self.getMax(node.getLeft()) - #Deletes the tmpNode - self.delete(tmpNode.getLabel()) - #Assigns the value to the node to delete and keesp tree structure - node.setLabel(tmpNode.getLabel()) - - def getNode(self, label): - curr_node = None - #If the tree is not empty - if(not self.empty()): - #Get tree root - curr_node = self.getRoot() - #While we don't find the node we look for - #I am using lazy evaluation here to avoid NoneType Attribute error - while curr_node is not None and curr_node.getLabel() is not label: - #If node label is less than current node - if label < curr_node.getLabel(): - #We go left - curr_node = curr_node.getLeft() - else: - #Else we go right - curr_node = curr_node.getRight() - return curr_node - - def getMax(self, root = None): - if(root is not None): - curr_node = root - else: - #We go deep on the right branch - curr_node = self.getRoot() - if(not self.empty()): - while(curr_node.getRight() is not None): - curr_node = curr_node.getRight() - return curr_node - - def getMin(self, root = None): - if(root is not None): - curr_node = root - else: - #We go deep on the left branch - curr_node = self.getRoot() - if(not self.empty()): - curr_node = self.getRoot() - while(curr_node.getLeft() is not None): - curr_node = curr_node.getLeft() - return curr_node - - def empty(self): - if self.root is None: - return True - return False - - def __InOrderTraversal(self, curr_node): - nodeList = [] - if curr_node is not None: - nodeList.insert(0, curr_node) - nodeList = nodeList + self.__InOrderTraversal(curr_node.getLeft()) - nodeList = nodeList + self.__InOrderTraversal(curr_node.getRight()) - return nodeList - - def getRoot(self): - return self.root - - def __isRightChildren(self, node): - if(node == node.getParent().getRight()): - return True - return False - - def __reassignNodes(self, node, newChildren): - if(newChildren is not None): - newChildren.setParent(node.getParent()) - if(node.getParent() is not None): - #If it is the Right Children - if(self.__isRightChildren(node)): - node.getParent().setRight(newChildren) - else: - #Else it is the left children - node.getParent().setLeft(newChildren) - - #This function traversal the tree. By default it returns an - #In order traversal list. You can pass a function to traversal - #The tree as needed by client code - def traversalTree(self, traversalFunction = None, root = None): - if(traversalFunction is None): - #Returns a list of nodes in preOrder by default - return self.__InOrderTraversal(self.root) - else: - #Returns a list of nodes in the order that the users wants to - return traversalFunction(self.root) - - #Returns an string of all the nodes labels in the list - #In Order Traversal - def __str__(self): - list = self.__InOrderTraversal(self.root) - str = "" - for x in list: - str = str + " " + x.getLabel().__str__() - return str - -def InPreOrder(curr_node): - nodeList = [] - if curr_node is not None: - nodeList = nodeList + InPreOrder(curr_node.getLeft()) - nodeList.insert(0, curr_node.getLabel()) - nodeList = nodeList + InPreOrder(curr_node.getRight()) - return nodeList - -def testBinarySearchTree(): - r''' - Example - 8 - / \ - 3 10 - / \ \ - 1 6 14 - / \ / - 4 7 13 - ''' - - r''' - Example After Deletion - 7 - / \ - 1 4 - - ''' - t = BinarySearchTree() - t.insert(8) - t.insert(3) - t.insert(6) - t.insert(1) - t.insert(10) - t.insert(14) - t.insert(13) - t.insert(4) - t.insert(7) - - #Prints all the elements of the list in order traversal - print(t.__str__()) - - if(t.getNode(6) is not None): - print("The label 6 exists") - else: - print("The label 6 doesn't exist") - - if(t.getNode(-1) is not None): - print("The label -1 exists") - else: - print("The label -1 doesn't exist") - - if(not t.empty()): - print(("Max Value: ", t.getMax().getLabel())) - print(("Min Value: ", t.getMin().getLabel())) - - t.delete(13) - t.delete(10) - t.delete(8) - t.delete(3) - t.delete(6) - t.delete(14) - - #Gets all the elements of the tree In pre order - #And it prints them - list = t.traversalTree(InPreOrder, t.root) - for x in list: - print(x) - -if __name__ == "__main__": - testBinarySearchTree() diff --git a/data_structures/binary tree/fenwick_tree.py b/data_structures/binary tree/fenwick_tree.py deleted file mode 100644 index f429161c8c36..000000000000 --- a/data_structures/binary tree/fenwick_tree.py +++ /dev/null @@ -1,29 +0,0 @@ -from __future__ import print_function -class FenwickTree: - - def __init__(self, SIZE): # create fenwick tree with size SIZE - self.Size = SIZE - self.ft = [0 for i in range (0,SIZE)] - - def update(self, i, val): # update data (adding) in index i in O(lg N) - while (i < self.Size): - self.ft[i] += val - i += i & (-i) - - def query(self, i): # query cumulative data from index 0 to i in O(lg N) - ret = 0 - while (i > 0): - ret += self.ft[i] - i -= i & (-i) - return ret - -if __name__ == '__main__': - f = FenwickTree(100) - f.update(1,20) - f.update(4,4) - print (f.query(1)) - print (f.query(3)) - print (f.query(4)) - f.update(2,-5) - print (f.query(1)) - print (f.query(3)) diff --git a/data_structures/binary tree/lazy_segment_tree.py b/data_structures/binary tree/lazy_segment_tree.py deleted file mode 100644 index 9b14b24e81fa..000000000000 --- a/data_structures/binary tree/lazy_segment_tree.py +++ /dev/null @@ -1,91 +0,0 @@ -from __future__ import print_function -import math - -class SegmentTree: - - def __init__(self, N): - self.N = N - self.st = [0 for i in range(0,4*N)] # approximate the overall size of segment tree with array N - self.lazy = [0 for i in range(0,4*N)] # create array to store lazy update - self.flag = [0 for i in range(0,4*N)] # flag for lazy update - - def left(self, idx): - return idx*2 - - def right(self, idx): - return idx*2 + 1 - - def build(self, idx, l, r, A): - if l==r: - self.st[idx] = A[l-1] - else : - mid = (l+r)//2 - self.build(self.left(idx),l,mid, A) - self.build(self.right(idx),mid+1,r, A) - self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) - - # update with O(lg N) (Normal segment tree without lazy update will take O(Nlg N) for each update) - def update(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b] - if self.flag[idx] == True: - self.st[idx] = self.lazy[idx] - self.flag[idx] = False - if l!=r: - self.lazy[self.left(idx)] = self.lazy[idx] - self.lazy[self.right(idx)] = self.lazy[idx] - self.flag[self.left(idx)] = True - self.flag[self.right(idx)] = True - - if r < a or l > b: - return True - if l >= a and r <= b : - self.st[idx] = val - if l!=r: - self.lazy[self.left(idx)] = val - self.lazy[self.right(idx)] = val - self.flag[self.left(idx)] = True - self.flag[self.right(idx)] = True - return True - mid = (l+r)//2 - self.update(self.left(idx),l,mid,a,b,val) - self.update(self.right(idx),mid+1,r,a,b,val) - self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) - return True - - # query with O(lg N) - def query(self, idx, l, r, a, b): #query(1, 1, N, a, b) for query max of [a,b] - if self.flag[idx] == True: - self.st[idx] = self.lazy[idx] - self.flag[idx] = False - if l != r: - self.lazy[self.left(idx)] = self.lazy[idx] - self.lazy[self.right(idx)] = self.lazy[idx] - self.flag[self.left(idx)] = True - self.flag[self.right(idx)] = True - if r < a or l > b: - return -math.inf - if l >= a and r <= b: - return self.st[idx] - mid = (l+r)//2 - q1 = self.query(self.left(idx),l,mid,a,b) - q2 = self.query(self.right(idx),mid+1,r,a,b) - return max(q1,q2) - - def showData(self): - showList = [] - for i in range(1,N+1): - showList += [self.query(1, 1, self.N, i, i)] - print (showList) - - -if __name__ == '__main__': - A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8] - N = 15 - segt = SegmentTree(N) - segt.build(1,1,N,A) - print (segt.query(1,1,N,4,6)) - print (segt.query(1,1,N,7,11)) - print (segt.query(1,1,N,7,12)) - segt.update(1,1,N,1,3,111) - print (segt.query(1,1,N,1,15)) - segt.update(1,1,N,7,8,235) - segt.showData() diff --git a/data_structures/binary_tree/__init__.py b/data_structures/binary_tree/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py new file mode 100644 index 000000000000..3362610b9303 --- /dev/null +++ b/data_structures/binary_tree/avl_tree.py @@ -0,0 +1,328 @@ +""" +Implementation of an auto-balanced binary tree! +For doctests run following command: +python3 -m doctest -v avl_tree.py +For testing run: +python avl_tree.py +""" + +import math +import random + + +class my_queue: + def __init__(self): + self.data = [] + self.head = 0 + self.tail = 0 + + def is_empty(self): + return self.head == self.tail + + def push(self, data): + self.data.append(data) + self.tail = self.tail + 1 + + def pop(self): + ret = self.data[self.head] + self.head = self.head + 1 + return ret + + def count(self): + return self.tail - self.head + + def print(self): + print(self.data) + print("**************") + print(self.data[self.head : self.tail]) + + +class my_node: + def __init__(self, data): + self.data = data + self.left = None + self.right = None + self.height = 1 + + def get_data(self): + return self.data + + def get_left(self): + return self.left + + def get_right(self): + return self.right + + def get_height(self): + return self.height + + def set_data(self, data): + self.data = data + return + + def set_left(self, node): + self.left = node + return + + def set_right(self, node): + self.right = node + return + + def set_height(self, height): + self.height = height + return + + +def get_height(node): + if node is None: + return 0 + return node.get_height() + + +def my_max(a, b): + if a > b: + return a + return b + + +def right_rotation(node): + r""" + A B + / \ / \ + B C Bl A + / \ --> / / \ + Bl Br UB Br C + / + UB + UB = unbalanced node + """ + print("left rotation node:", node.get_data()) + ret = node.get_left() + node.set_left(ret.get_right()) + ret.set_right(node) + h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 + node.set_height(h1) + h2 = my_max(get_height(ret.get_right()), get_height(ret.get_left())) + 1 + ret.set_height(h2) + return ret + + +def left_rotation(node): + """ + a mirror symmetry rotation of the left_rotation + """ + print("right rotation node:", node.get_data()) + ret = node.get_right() + node.set_right(ret.get_left()) + ret.set_left(node) + h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 + node.set_height(h1) + h2 = my_max(get_height(ret.get_right()), get_height(ret.get_left())) + 1 + ret.set_height(h2) + return ret + + +def lr_rotation(node): + r""" + A A Br + / \ / \ / \ + B C LR Br C RR B A + / \ --> / \ --> / / \ + Bl Br B UB Bl UB C + \ / + UB Bl + RR = right_rotation LR = left_rotation + """ + node.set_left(left_rotation(node.get_left())) + return right_rotation(node) + + +def rl_rotation(node): + node.set_right(right_rotation(node.get_right())) + return left_rotation(node) + + +def insert_node(node, data): + if node is None: + return my_node(data) + if data < node.get_data(): + node.set_left(insert_node(node.get_left(), data)) + if ( + get_height(node.get_left()) - get_height(node.get_right()) == 2 + ): # an unbalance detected + if ( + data < node.get_left().get_data() + ): # new node is the left child of the left child + node = right_rotation(node) + else: + node = lr_rotation(node) + else: + node.set_right(insert_node(node.get_right(), data)) + if get_height(node.get_right()) - get_height(node.get_left()) == 2: + if data < node.get_right().get_data(): + node = rl_rotation(node) + else: + node = left_rotation(node) + h1 = my_max(get_height(node.get_right()), get_height(node.get_left())) + 1 + node.set_height(h1) + return node + + +def get_rightMost(root): + while root.get_right() is not None: + root = root.get_right() + return root.get_data() + + +def get_leftMost(root): + while root.get_left() is not None: + root = root.get_left() + return root.get_data() + + +def del_node(root, data): + if root.get_data() == data: + if root.get_left() is not None and root.get_right() is not None: + temp_data = get_leftMost(root.get_right()) + root.set_data(temp_data) + root.set_right(del_node(root.get_right(), temp_data)) + elif root.get_left() is not None: + root = root.get_left() + else: + root = root.get_right() + elif root.get_data() > data: + if root.get_left() is None: + print("No such data") + return root + else: + root.set_left(del_node(root.get_left(), data)) + elif root.get_data() < data: + if root.get_right() is None: + return root + else: + root.set_right(del_node(root.get_right(), data)) + if root is None: + return root + if get_height(root.get_right()) - get_height(root.get_left()) == 2: + if get_height(root.get_right().get_right()) > get_height( + root.get_right().get_left() + ): + root = left_rotation(root) + else: + root = rl_rotation(root) + elif get_height(root.get_right()) - get_height(root.get_left()) == -2: + if get_height(root.get_left().get_left()) > get_height( + root.get_left().get_right() + ): + root = right_rotation(root) + else: + root = lr_rotation(root) + height = my_max(get_height(root.get_right()), get_height(root.get_left())) + 1 + root.set_height(height) + return root + + +class AVLtree: + """ + An AVL tree doctest + Examples: + >>> t = AVLtree() + >>> t.insert(4) + insert:4 + >>> print(str(t).replace(" \\n","\\n")) + 4 + ************************************* + >>> t.insert(2) + insert:2 + >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) + 4 + 2 * + ************************************* + >>> t.insert(3) + insert:3 + right rotation node: 2 + left rotation node: 4 + >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) + 3 + 2 4 + ************************************* + >>> t.get_height() + 2 + >>> t.del_node(3) + delete:3 + >>> print(str(t).replace(" \\n","\\n").replace(" \\n","\\n")) + 4 + 2 * + ************************************* + """ + + def __init__(self): + self.root = None + + def get_height(self): + # print("yyy") + return get_height(self.root) + + def insert(self, data): + print("insert:" + str(data)) + self.root = insert_node(self.root, data) + + def del_node(self, data): + print("delete:" + str(data)) + if self.root is None: + print("Tree is empty!") + return + self.root = del_node(self.root, data) + + def __str__(self): # a level traversale, gives a more intuitive look on the tree + output = "" + q = my_queue() + q.push(self.root) + layer = self.get_height() + if layer == 0: + return output + cnt = 0 + while not q.is_empty(): + node = q.pop() + space = " " * int(math.pow(2, layer - 1)) + output += space + if node is None: + output += "*" + q.push(None) + q.push(None) + else: + output += str(node.get_data()) + q.push(node.get_left()) + q.push(node.get_right()) + output += space + cnt = cnt + 1 + for i in range(100): + if cnt == math.pow(2, i) - 1: + layer = layer - 1 + if layer == 0: + output += "\n*************************************" + return output + output += "\n" + break + output += "\n*************************************" + return output + + +def _test(): + import doctest + + doctest.testmod() + + +if __name__ == "__main__": + _test() + t = AVLtree() + lst = list(range(10)) + random.shuffle(lst) + for i in lst: + t.insert(i) + print(str(t)) + random.shuffle(lst) + for i in lst: + t.del_node(i) + print(str(t)) diff --git a/data_structures/binary_tree/basic_binary_tree.py b/data_structures/binary_tree/basic_binary_tree.py new file mode 100644 index 000000000000..575b157ee78a --- /dev/null +++ b/data_structures/binary_tree/basic_binary_tree.py @@ -0,0 +1,101 @@ +from typing import Optional + + +class Node: + """ + A Node has data variable and pointers to Nodes to its left and right. + """ + + def __init__(self, data: int) -> None: + self.data = data + self.left: Optional[Node] = None + self.right: Optional[Node] = None + + +def display(tree: Optional[Node]) -> None: # In Order traversal of the tree + """ + >>> root = Node(1) + >>> root.left = Node(0) + >>> root.right = Node(2) + >>> display(root) + 0 + 1 + 2 + >>> display(root.right) + 2 + """ + if tree: + display(tree.left) + print(tree.data) + display(tree.right) + + +def depth_of_tree(tree: Optional[Node]) -> int: + """ + Recursive function that returns the depth of a binary tree. + + >>> root = Node(0) + >>> depth_of_tree(root) + 1 + >>> root.left = Node(0) + >>> depth_of_tree(root) + 2 + >>> root.right = Node(0) + >>> depth_of_tree(root) + 2 + >>> root.left.right = Node(0) + >>> depth_of_tree(root) + 3 + >>> depth_of_tree(root.left) + 2 + """ + return 1 + max(depth_of_tree(tree.left), depth_of_tree(tree.right)) if tree else 0 + + +def is_full_binary_tree(tree: Node) -> bool: + """ + Returns True if this is a full binary tree + + >>> root = Node(0) + >>> is_full_binary_tree(root) + True + >>> root.left = Node(0) + >>> is_full_binary_tree(root) + False + >>> root.right = Node(0) + >>> is_full_binary_tree(root) + True + >>> root.left.left = Node(0) + >>> is_full_binary_tree(root) + False + >>> root.right.right = Node(0) + >>> is_full_binary_tree(root) + False + """ + if not tree: + return True + if tree.left and tree.right: + return is_full_binary_tree(tree.left) and is_full_binary_tree(tree.right) + else: + return not tree.left and not tree.right + + +def main() -> None: # Main function for testing. + tree = Node(1) + tree.left = Node(2) + tree.right = Node(3) + tree.left.left = Node(4) + tree.left.right = Node(5) + tree.left.right.left = Node(6) + tree.right.left = Node(7) + tree.right.left.left = Node(8) + tree.right.left.left.right = Node(9) + + print(is_full_binary_tree(tree)) + print(depth_of_tree(tree)) + print("Tree is: ") + display(tree) + + +if __name__ == "__main__": + main() diff --git a/data_structures/binary_tree/binary_search_tree.py b/data_structures/binary_tree/binary_search_tree.py new file mode 100644 index 000000000000..45c3933fe899 --- /dev/null +++ b/data_structures/binary_tree/binary_search_tree.py @@ -0,0 +1,221 @@ +""" +A binary search Tree +""" + + +class Node: + def __init__(self, value, parent): + self.value = value + self.parent = parent # Added in order to delete a node easier + self.left = None + self.right = None + + def __repr__(self): + from pprint import pformat + + if self.left is None and self.right is None: + return str(self.value) + return pformat({"%s" % (self.value): (self.left, self.right)}, indent=1) + + +class BinarySearchTree: + def __init__(self, root=None): + self.root = root + + def __str__(self): + """ + Return a string of all the Nodes using in order traversal + """ + return str(self.root) + + def __reassign_nodes(self, node, new_children): + if new_children is not None: # reset its kids + new_children.parent = node.parent + if node.parent is not None: # reset its parent + if self.is_right(node): # If it is the right children + node.parent.right = new_children + else: + node.parent.left = new_children + else: + self.root = new_children + + def is_right(self, node): + return node == node.parent.right + + def empty(self): + return self.root is None + + def __insert(self, value): + """ + Insert a new node in Binary Search Tree with value label + """ + new_node = Node(value, None) # create a new Node + if self.empty(): # if Tree is empty + self.root = new_node # set its root + else: # Tree is not empty + parent_node = self.root # from root + while True: # While we don't get to a leaf + if value < parent_node.value: # We go left + if parent_node.left is None: + parent_node.left = new_node # We insert the new node in a leaf + break + else: + parent_node = parent_node.left + else: + if parent_node.right is None: + parent_node.right = new_node + break + else: + parent_node = parent_node.right + new_node.parent = parent_node + + def insert(self, *values): + for value in values: + self.__insert(value) + return self + + def search(self, value): + if self.empty(): + raise IndexError("Warning: Tree is empty! please use another.") + else: + node = self.root + # use lazy evaluation here to avoid NoneType Attribute error + while node is not None and node.value is not value: + node = node.left if value < node.value else node.right + return node + + def get_max(self, node=None): + """ + We go deep on the right branch + """ + if node is None: + node = self.root + if not self.empty(): + while node.right is not None: + node = node.right + return node + + def get_min(self, node=None): + """ + We go deep on the left branch + """ + if node is None: + node = self.root + if not self.empty(): + node = self.root + while node.left is not None: + node = node.left + return node + + def remove(self, value): + node = self.search(value) # Look for the node with that label + if node is not None: + if node.left is None and node.right is None: # If it has no children + self.__reassign_nodes(node, None) + elif node.left is None: # Has only right children + self.__reassign_nodes(node, node.right) + elif node.right is None: # Has only left children + self.__reassign_nodes(node, node.left) + else: + tmp_node = self.get_max( + node.left + ) # Gets the max value of the left branch + self.remove(tmp_node.value) + node.value = ( + tmp_node.value + ) # Assigns the value to the node to delete and keep tree structure + + def preorder_traverse(self, node): + if node is not None: + yield node # Preorder Traversal + yield from self.preorder_traverse(node.left) + yield from self.preorder_traverse(node.right) + + def traversal_tree(self, traversal_function=None): + """ + This function traversal the tree. + You can pass a function to traversal the tree as needed by client code + """ + if traversal_function is None: + return self.preorder_traverse(self.root) + else: + return traversal_function(self.root) + + def inorder(self, arr: list, node: Node): + """Perform an inorder traversal and append values of the nodes to + a list named arr""" + if node: + self.inorder(arr, node.left) + arr.append(node.value) + self.inorder(arr, node.right) + + def find_kth_smallest(self, k: int, node: Node) -> int: + """Return the kth smallest element in a binary search tree """ + arr = [] + self.inorder(arr, node) # append all values to list using inorder traversal + return arr[k - 1] + + +def postorder(curr_node): + """ + postOrder (left, right, self) + """ + node_list = list() + if curr_node is not None: + node_list = postorder(curr_node.left) + postorder(curr_node.right) + [curr_node] + return node_list + + +def binary_search_tree(): + r""" + Example + 8 + / \ + 3 10 + / \ \ + 1 6 14 + / \ / + 4 7 13 + + >>> t = BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7) + >>> print(" ".join(repr(i.value) for i in t.traversal_tree())) + 8 3 1 6 4 7 10 14 13 + >>> print(" ".join(repr(i.value) for i in t.traversal_tree(postorder))) + 1 4 7 6 3 13 14 10 8 + >>> BinarySearchTree().search(6) + Traceback (most recent call last): + ... + IndexError: Warning: Tree is empty! please use another. + """ + testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7) + t = BinarySearchTree() + for i in testlist: + t.insert(i) + + # Prints all the elements of the list in order traversal + print(t) + + if t.search(6) is not None: + print("The value 6 exists") + else: + print("The value 6 doesn't exist") + + if t.search(-1) is not None: + print("The value -1 exists") + else: + print("The value -1 doesn't exist") + + if not t.empty(): + print("Max Value: ", t.get_max().value) + print("Min Value: ", t.get_min().value) + + for i in testlist: + t.remove(i) + print(t) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + # binary_search_tree() diff --git a/data_structures/binary_tree/binary_search_tree_recursive.py b/data_structures/binary_tree/binary_search_tree_recursive.py new file mode 100644 index 000000000000..f1e46e33cd24 --- /dev/null +++ b/data_structures/binary_tree/binary_search_tree_recursive.py @@ -0,0 +1,613 @@ +""" +This is a python3 implementation of binary search tree using recursion + +To run tests: +python -m unittest binary_search_tree_recursive.py + +To run an example: +python binary_search_tree_recursive.py +""" +import unittest + + +class Node: + def __init__(self, label: int, parent): + self.label = label + self.parent = parent + self.left = None + self.right = None + + +class BinarySearchTree: + def __init__(self): + self.root = None + + def empty(self): + """ + Empties the tree + + >>> t = BinarySearchTree() + >>> assert t.root is None + >>> t.put(8) + >>> assert t.root is not None + """ + self.root = None + + def is_empty(self) -> bool: + """ + Checks if the tree is empty + + >>> t = BinarySearchTree() + >>> t.is_empty() + True + >>> t.put(8) + >>> t.is_empty() + False + """ + return self.root is None + + def put(self, label: int): + """ + Put a new node in the tree + + >>> t = BinarySearchTree() + >>> t.put(8) + >>> assert t.root.parent is None + >>> assert t.root.label == 8 + + >>> t.put(10) + >>> assert t.root.right.parent == t.root + >>> assert t.root.right.label == 10 + + >>> t.put(3) + >>> assert t.root.left.parent == t.root + >>> assert t.root.left.label == 3 + """ + self.root = self._put(self.root, label) + + def _put(self, node: Node, label: int, parent: Node = None) -> Node: + if node is None: + node = Node(label, parent) + else: + if label < node.label: + node.left = self._put(node.left, label, node) + elif label > node.label: + node.right = self._put(node.right, label, node) + else: + raise Exception(f"Node with label {label} already exists") + + return node + + def search(self, label: int) -> Node: + """ + Searches a node in the tree + + >>> t = BinarySearchTree() + >>> t.put(8) + >>> t.put(10) + >>> node = t.search(8) + >>> assert node.label == 8 + + >>> node = t.search(3) + Traceback (most recent call last): + ... + Exception: Node with label 3 does not exist + """ + return self._search(self.root, label) + + def _search(self, node: Node, label: int) -> Node: + if node is None: + raise Exception(f"Node with label {label} does not exist") + else: + if label < node.label: + node = self._search(node.left, label) + elif label > node.label: + node = self._search(node.right, label) + + return node + + def remove(self, label: int): + """ + Removes a node in the tree + + >>> t = BinarySearchTree() + >>> t.put(8) + >>> t.put(10) + >>> t.remove(8) + >>> assert t.root.label == 10 + + >>> t.remove(3) + Traceback (most recent call last): + ... + Exception: Node with label 3 does not exist + """ + node = self.search(label) + if not node.right and not node.left: + self._reassign_nodes(node, None) + elif not node.right and node.left: + self._reassign_nodes(node, node.left) + elif node.right and not node.left: + self._reassign_nodes(node, node.right) + else: + lowest_node = self._get_lowest_node(node.right) + lowest_node.left = node.left + lowest_node.right = node.right + node.left.parent = lowest_node + if node.right: + node.right.parent = lowest_node + self._reassign_nodes(node, lowest_node) + + def _reassign_nodes(self, node: Node, new_children: Node): + if new_children: + new_children.parent = node.parent + + if node.parent: + if node.parent.right == node: + node.parent.right = new_children + else: + node.parent.left = new_children + else: + self.root = new_children + + def _get_lowest_node(self, node: Node) -> Node: + if node.left: + lowest_node = self._get_lowest_node(node.left) + else: + lowest_node = node + self._reassign_nodes(node, node.right) + + return lowest_node + + def exists(self, label: int) -> bool: + """ + Checks if a node exists in the tree + + >>> t = BinarySearchTree() + >>> t.put(8) + >>> t.put(10) + >>> t.exists(8) + True + + >>> t.exists(3) + False + """ + try: + self.search(label) + return True + except Exception: + return False + + def get_max_label(self) -> int: + """ + Gets the max label inserted in the tree + + >>> t = BinarySearchTree() + >>> t.get_max_label() + Traceback (most recent call last): + ... + Exception: Binary search tree is empty + + >>> t.put(8) + >>> t.put(10) + >>> t.get_max_label() + 10 + """ + if self.is_empty(): + raise Exception("Binary search tree is empty") + + node = self.root + while node.right is not None: + node = node.right + + return node.label + + def get_min_label(self) -> int: + """ + Gets the min label inserted in the tree + + >>> t = BinarySearchTree() + >>> t.get_min_label() + Traceback (most recent call last): + ... + Exception: Binary search tree is empty + + >>> t.put(8) + >>> t.put(10) + >>> t.get_min_label() + 8 + """ + if self.is_empty(): + raise Exception("Binary search tree is empty") + + node = self.root + while node.left is not None: + node = node.left + + return node.label + + def inorder_traversal(self) -> list: + """ + Return the inorder traversal of the tree + + >>> t = BinarySearchTree() + >>> [i.label for i in t.inorder_traversal()] + [] + + >>> t.put(8) + >>> t.put(10) + >>> t.put(9) + >>> [i.label for i in t.inorder_traversal()] + [8, 9, 10] + """ + return self._inorder_traversal(self.root) + + def _inorder_traversal(self, node: Node) -> list: + if node is not None: + yield from self._inorder_traversal(node.left) + yield node + yield from self._inorder_traversal(node.right) + + def preorder_traversal(self) -> list: + """ + Return the preorder traversal of the tree + + >>> t = BinarySearchTree() + >>> [i.label for i in t.preorder_traversal()] + [] + + >>> t.put(8) + >>> t.put(10) + >>> t.put(9) + >>> [i.label for i in t.preorder_traversal()] + [8, 10, 9] + """ + return self._preorder_traversal(self.root) + + def _preorder_traversal(self, node: Node) -> list: + if node is not None: + yield node + yield from self._preorder_traversal(node.left) + yield from self._preorder_traversal(node.right) + + +class BinarySearchTreeTest(unittest.TestCase): + @staticmethod + def _get_binary_search_tree(): + r""" + 8 + / \ + 3 10 + / \ \ + 1 6 14 + / \ / + 4 7 13 + \ + 5 + """ + t = BinarySearchTree() + t.put(8) + t.put(3) + t.put(6) + t.put(1) + t.put(10) + t.put(14) + t.put(13) + t.put(4) + t.put(7) + t.put(5) + + return t + + def test_put(self): + t = BinarySearchTree() + assert t.is_empty() + + t.put(8) + r""" + 8 + """ + assert t.root.parent is None + assert t.root.label == 8 + + t.put(10) + r""" + 8 + \ + 10 + """ + assert t.root.right.parent == t.root + assert t.root.right.label == 10 + + t.put(3) + r""" + 8 + / \ + 3 10 + """ + assert t.root.left.parent == t.root + assert t.root.left.label == 3 + + t.put(6) + r""" + 8 + / \ + 3 10 + \ + 6 + """ + assert t.root.left.right.parent == t.root.left + assert t.root.left.right.label == 6 + + t.put(1) + r""" + 8 + / \ + 3 10 + / \ + 1 6 + """ + assert t.root.left.left.parent == t.root.left + assert t.root.left.left.label == 1 + + with self.assertRaises(Exception): + t.put(1) + + def test_search(self): + t = self._get_binary_search_tree() + + node = t.search(6) + assert node.label == 6 + + node = t.search(13) + assert node.label == 13 + + with self.assertRaises(Exception): + t.search(2) + + def test_remove(self): + t = self._get_binary_search_tree() + + t.remove(13) + r""" + 8 + / \ + 3 10 + / \ \ + 1 6 14 + / \ + 4 7 + \ + 5 + """ + assert t.root.right.right.right is None + assert t.root.right.right.left is None + + t.remove(7) + r""" + 8 + / \ + 3 10 + / \ \ + 1 6 14 + / + 4 + \ + 5 + """ + assert t.root.left.right.right is None + assert t.root.left.right.left.label == 4 + + t.remove(6) + r""" + 8 + / \ + 3 10 + / \ \ + 1 4 14 + \ + 5 + """ + assert t.root.left.left.label == 1 + assert t.root.left.right.label == 4 + assert t.root.left.right.right.label == 5 + assert t.root.left.right.left is None + assert t.root.left.left.parent == t.root.left + assert t.root.left.right.parent == t.root.left + + t.remove(3) + r""" + 8 + / \ + 4 10 + / \ \ + 1 5 14 + """ + assert t.root.left.label == 4 + assert t.root.left.right.label == 5 + assert t.root.left.left.label == 1 + assert t.root.left.parent == t.root + assert t.root.left.left.parent == t.root.left + assert t.root.left.right.parent == t.root.left + + t.remove(4) + r""" + 8 + / \ + 5 10 + / \ + 1 14 + """ + assert t.root.left.label == 5 + assert t.root.left.right is None + assert t.root.left.left.label == 1 + assert t.root.left.parent == t.root + assert t.root.left.left.parent == t.root.left + + def test_remove_2(self): + t = self._get_binary_search_tree() + + t.remove(3) + r""" + 8 + / \ + 4 10 + / \ \ + 1 6 14 + / \ / + 5 7 13 + """ + assert t.root.left.label == 4 + assert t.root.left.right.label == 6 + assert t.root.left.left.label == 1 + assert t.root.left.right.right.label == 7 + assert t.root.left.right.left.label == 5 + assert t.root.left.parent == t.root + assert t.root.left.right.parent == t.root.left + assert t.root.left.left.parent == t.root.left + assert t.root.left.right.left.parent == t.root.left.right + + def test_empty(self): + t = self._get_binary_search_tree() + t.empty() + assert t.root is None + + def test_is_empty(self): + t = self._get_binary_search_tree() + assert not t.is_empty() + + t.empty() + assert t.is_empty() + + def test_exists(self): + t = self._get_binary_search_tree() + + assert t.exists(6) + assert not t.exists(-1) + + def test_get_max_label(self): + t = self._get_binary_search_tree() + + assert t.get_max_label() == 14 + + t.empty() + with self.assertRaises(Exception): + t.get_max_label() + + def test_get_min_label(self): + t = self._get_binary_search_tree() + + assert t.get_min_label() == 1 + + t.empty() + with self.assertRaises(Exception): + t.get_min_label() + + def test_inorder_traversal(self): + t = self._get_binary_search_tree() + + inorder_traversal_nodes = [i.label for i in t.inorder_traversal()] + assert inorder_traversal_nodes == [1, 3, 4, 5, 6, 7, 8, 10, 13, 14] + + def test_preorder_traversal(self): + t = self._get_binary_search_tree() + + preorder_traversal_nodes = [i.label for i in t.preorder_traversal()] + assert preorder_traversal_nodes == [8, 3, 1, 6, 4, 5, 7, 10, 14, 13] + + +def binary_search_tree_example(): + r""" + Example + 8 + / \ + 3 10 + / \ \ + 1 6 14 + / \ / + 4 7 13 + \ + 5 + + Example After Deletion + 4 + / \ + 1 7 + \ + 5 + + """ + + t = BinarySearchTree() + t.put(8) + t.put(3) + t.put(6) + t.put(1) + t.put(10) + t.put(14) + t.put(13) + t.put(4) + t.put(7) + t.put(5) + + print( + """ + 8 + / \\ + 3 10 + / \\ \\ + 1 6 14 + / \\ / + 4 7 13 + \\ + 5 + """ + ) + + print("Label 6 exists:", t.exists(6)) + print("Label 13 exists:", t.exists(13)) + print("Label -1 exists:", t.exists(-1)) + print("Label 12 exists:", t.exists(12)) + + # Prints all the elements of the list in inorder traversal + inorder_traversal_nodes = [i.label for i in t.inorder_traversal()] + print("Inorder traversal:", inorder_traversal_nodes) + + # Prints all the elements of the list in preorder traversal + preorder_traversal_nodes = [i.label for i in t.preorder_traversal()] + print("Preorder traversal:", preorder_traversal_nodes) + + print("Max. label:", t.get_max_label()) + print("Min. label:", t.get_min_label()) + + # Delete elements + print("\nDeleting elements 13, 10, 8, 3, 6, 14") + print( + """ + 4 + / \\ + 1 7 + \\ + 5 + """ + ) + t.remove(13) + t.remove(10) + t.remove(8) + t.remove(3) + t.remove(6) + t.remove(14) + + # Prints all the elements of the list in inorder traversal after delete + inorder_traversal_nodes = [i.label for i in t.inorder_traversal()] + print("Inorder traversal after delete:", inorder_traversal_nodes) + + # Prints all the elements of the list in preorder traversal after delete + preorder_traversal_nodes = [i.label for i in t.preorder_traversal()] + print("Preorder traversal after delete:", preorder_traversal_nodes) + + print("Max. label:", t.get_max_label()) + print("Min. label:", t.get_min_label()) + + +if __name__ == "__main__": + binary_search_tree_example() diff --git a/data_structures/binary_tree/binary_tree_mirror.py b/data_structures/binary_tree/binary_tree_mirror.py new file mode 100644 index 000000000000..dc7f657b37c7 --- /dev/null +++ b/data_structures/binary_tree/binary_tree_mirror.py @@ -0,0 +1,44 @@ +""" +Problem Description: +Given a binary tree, return it's mirror. +""" + + +def binary_tree_mirror_dict(binary_tree_mirror_dictionary: dict, root: int): + if not root or root not in binary_tree_mirror_dictionary: + return + left_child, right_child = binary_tree_mirror_dictionary[root][:2] + binary_tree_mirror_dictionary[root] = [right_child, left_child] + binary_tree_mirror_dict(binary_tree_mirror_dictionary, left_child) + binary_tree_mirror_dict(binary_tree_mirror_dictionary, right_child) + + +def binary_tree_mirror(binary_tree: dict, root: int = 1) -> dict: + """ + >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}, 1) + {1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]} + >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 1) + {1: [3, 2], 2: [5, 4], 3: [7, 6], 4: [11, 10]} + >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 5) + Traceback (most recent call last): + ... + ValueError: root 5 is not present in the binary_tree + >>> binary_tree_mirror({}, 5) + Traceback (most recent call last): + ... + ValueError: binary tree cannot be empty + """ + if not binary_tree: + raise ValueError("binary tree cannot be empty") + if root not in binary_tree: + raise ValueError(f"root {root} is not present in the binary_tree") + binary_tree_mirror_dictionary = dict(binary_tree) + binary_tree_mirror_dict(binary_tree_mirror_dictionary, root) + return binary_tree_mirror_dictionary + + +if __name__ == "__main__": + binary_tree = {1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]} + print(f"Binary tree: {binary_tree}") + binary_tree_mirror_dictionary = binary_tree_mirror(binary_tree, 5) + print(f"Binary tree mirror: {binary_tree_mirror_dictionary}") diff --git a/data_structures/binary_tree/binary_tree_traversals.py b/data_structures/binary_tree/binary_tree_traversals.py new file mode 100644 index 000000000000..7c0ee1dbbc2a --- /dev/null +++ b/data_structures/binary_tree/binary_tree_traversals.py @@ -0,0 +1,161 @@ +# https://en.wikipedia.org/wiki/Tree_traversal + + +class Node: + """ + A Node has data variable and pointers to its left and right nodes. + """ + + def __init__(self, data): + self.left = None + self.right = None + self.data = data + + +def make_tree() -> Node: + root = Node(1) + root.left = Node(2) + root.right = Node(3) + root.left.left = Node(4) + root.left.right = Node(5) + return root + + +def preorder(root: Node): + """ + Pre-order traversal visits root node, left subtree, right subtree. + >>> preorder(make_tree()) + [1, 2, 4, 5, 3] + """ + return [root.data] + preorder(root.left) + preorder(root.right) if root else [] + + +def postorder(root: Node): + """ + Post-order traversal visits left subtree, right subtree, root node. + >>> postorder(make_tree()) + [4, 5, 2, 3, 1] + """ + return postorder(root.left) + postorder(root.right) + [root.data] if root else [] + + +def inorder(root: Node): + """ + In-order traversal visits left subtree, root node, right subtree. + >>> inorder(make_tree()) + [4, 2, 5, 1, 3] + """ + return inorder(root.left) + [root.data] + inorder(root.right) if root else [] + + +def height(root: Node): + """ + Recursive function for calculating the height of the binary tree. + >>> height(None) + 0 + >>> height(make_tree()) + 3 + """ + return (max(height(root.left), height(root.right)) + 1) if root else 0 + + +def level_order_1(root: Node): + """ + Print whole binary tree in Level Order Traverse. + Level Order traverse: Visit nodes of the tree level-by-level. + """ + if not root: + return + temp = root + que = [temp] + while len(que) > 0: + print(que[0].data, end=" ") + temp = que.pop(0) + if temp.left: + que.append(temp.left) + if temp.right: + que.append(temp.right) + return que + + +def level_order_2(root: Node, level: int): + """ + Level-wise traversal: Print all nodes present at the given level of the binary tree + """ + if not root: + return root + if level == 1: + print(root.data, end=" ") + elif level > 1: + level_order_2(root.left, level - 1) + level_order_2(root.right, level - 1) + + +def print_left_to_right(root: Node, level: int): + """ + Print elements on particular level from left to right direction of the binary tree. + """ + if not root: + return + if level == 1: + print(root.data, end=" ") + elif level > 1: + print_left_to_right(root.left, level - 1) + print_left_to_right(root.right, level - 1) + + +def print_right_to_left(root: Node, level: int): + """ + Print elements on particular level from right to left direction of the binary tree. + """ + if not root: + return + if level == 1: + print(root.data, end=" ") + elif level > 1: + print_right_to_left(root.right, level - 1) + print_right_to_left(root.left, level - 1) + + +def zigzag(root: Node): + """ + ZigZag traverse: Print node left to right and right to left, alternatively. + """ + flag = 0 + height_tree = height(root) + for h in range(1, height_tree + 1): + if flag == 0: + print_left_to_right(root, h) + flag = 1 + else: + print_right_to_left(root, h) + flag = 0 + + +def main(): # Main function for testing. + """ + Create binary tree. + """ + root = make_tree() + """ + All Traversals of the binary are as follows: + """ + print(f" In-order Traversal is {inorder(root)}") + print(f" Pre-order Traversal is {preorder(root)}") + print(f"Post-order Traversal is {postorder(root)}") + print(f"Height of Tree is {height(root)}") + print("Complete Level Order Traversal is : ") + level_order_1(root) + print("\nLevel-wise order Traversal is : ") + for h in range(1, height(root) + 1): + level_order_2(root, h) + print("\nZigZag order Traversal is : ") + zigzag(root) + print() + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/data_structures/binary_tree/fenwick_tree.py b/data_structures/binary_tree/fenwick_tree.py new file mode 100644 index 000000000000..54f0f07ac68d --- /dev/null +++ b/data_structures/binary_tree/fenwick_tree.py @@ -0,0 +1,28 @@ +class FenwickTree: + def __init__(self, SIZE): # create fenwick tree with size SIZE + self.Size = SIZE + self.ft = [0 for i in range(0, SIZE)] + + def update(self, i, val): # update data (adding) in index i in O(lg N) + while i < self.Size: + self.ft[i] += val + i += i & (-i) + + def query(self, i): # query cumulative data from index 0 to i in O(lg N) + ret = 0 + while i > 0: + ret += self.ft[i] + i -= i & (-i) + return ret + + +if __name__ == "__main__": + f = FenwickTree(100) + f.update(1, 20) + f.update(4, 4) + print(f.query(1)) + print(f.query(3)) + print(f.query(4)) + f.update(2, -5) + print(f.query(1)) + print(f.query(3)) diff --git a/data_structures/binary_tree/lazy_segment_tree.py b/data_structures/binary_tree/lazy_segment_tree.py new file mode 100644 index 000000000000..5bc79e74efcd --- /dev/null +++ b/data_structures/binary_tree/lazy_segment_tree.py @@ -0,0 +1,136 @@ +from __future__ import annotations + +import math + + +class SegmentTree: + def __init__(self, size: int) -> None: + self.size = size + # approximate the overall size of segment tree with given value + self.segment_tree = [0 for i in range(0, 4 * size)] + # create array to store lazy update + self.lazy = [0 for i in range(0, 4 * size)] + self.flag = [0 for i in range(0, 4 * size)] # flag for lazy update + + def left(self, idx: int) -> int: + """ + >>> segment_tree = SegmentTree(15) + >>> segment_tree.left(1) + 2 + >>> segment_tree.left(2) + 4 + >>> segment_tree.left(12) + 24 + """ + return idx * 2 + + def right(self, idx: int) -> int: + """ + >>> segment_tree = SegmentTree(15) + >>> segment_tree.right(1) + 3 + >>> segment_tree.right(2) + 5 + >>> segment_tree.right(12) + 25 + """ + return idx * 2 + 1 + + def build( + self, idx: int, left_element: int, right_element: int, A: list[int] + ) -> None: + if left_element == right_element: + self.segment_tree[idx] = A[left_element - 1] + else: + mid = (left_element + right_element) // 2 + self.build(self.left(idx), left_element, mid, A) + self.build(self.right(idx), mid + 1, right_element, A) + self.segment_tree[idx] = max( + self.segment_tree[self.left(idx)], self.segment_tree[self.right(idx)] + ) + + def update( + self, idx: int, left_element: int, right_element: int, a: int, b: int, val: int + ) -> bool: + """ + update with O(lg n) (Normal segment tree without lazy update will take O(nlg n) + for each update) + + update(1, 1, size, a, b, v) for update val v to [a,b] + """ + if self.flag[idx] is True: + self.segment_tree[idx] = self.lazy[idx] + self.flag[idx] = False + if left_element != right_element: + self.lazy[self.left(idx)] = self.lazy[idx] + self.lazy[self.right(idx)] = self.lazy[idx] + self.flag[self.left(idx)] = True + self.flag[self.right(idx)] = True + + if right_element < a or left_element > b: + return True + if left_element >= a and right_element <= b: + self.segment_tree[idx] = val + if left_element != right_element: + self.lazy[self.left(idx)] = val + self.lazy[self.right(idx)] = val + self.flag[self.left(idx)] = True + self.flag[self.right(idx)] = True + return True + mid = (left_element + right_element) // 2 + self.update(self.left(idx), left_element, mid, a, b, val) + self.update(self.right(idx), mid + 1, right_element, a, b, val) + self.segment_tree[idx] = max( + self.segment_tree[self.left(idx)], self.segment_tree[self.right(idx)] + ) + return True + + # query with O(lg n) + def query( + self, idx: int, left_element: int, right_element: int, a: int, b: int + ) -> int: + """ + query(1, 1, size, a, b) for query max of [a,b] + >>> A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] + >>> segment_tree = SegmentTree(15) + >>> segment_tree.build(1, 1, 15, A) + >>> segment_tree.query(1, 1, 15, 4, 6) + 7 + >>> segment_tree.query(1, 1, 15, 7, 11) + 14 + >>> segment_tree.query(1, 1, 15, 7, 12) + 15 + """ + if self.flag[idx] is True: + self.segment_tree[idx] = self.lazy[idx] + self.flag[idx] = False + if left_element != right_element: + self.lazy[self.left(idx)] = self.lazy[idx] + self.lazy[self.right(idx)] = self.lazy[idx] + self.flag[self.left(idx)] = True + self.flag[self.right(idx)] = True + if right_element < a or left_element > b: + return -math.inf + if left_element >= a and right_element <= b: + return self.segment_tree[idx] + mid = (left_element + right_element) // 2 + q1 = self.query(self.left(idx), left_element, mid, a, b) + q2 = self.query(self.right(idx), mid + 1, right_element, a, b) + return max(q1, q2) + + def __str__(self) -> None: + return [self.query(1, 1, self.size, i, i) for i in range(1, self.size + 1)] + + +if __name__ == "__main__": + A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] + size = 15 + segt = SegmentTree(size) + segt.build(1, 1, size, A) + print(segt.query(1, 1, size, 4, 6)) + print(segt.query(1, 1, size, 7, 11)) + print(segt.query(1, 1, size, 7, 12)) + segt.update(1, 1, size, 1, 3, 111) + print(segt.query(1, 1, size, 1, 15)) + segt.update(1, 1, size, 7, 8, 235) + print(segt) diff --git a/data_structures/LCA.py b/data_structures/binary_tree/lowest_common_ancestor.py similarity index 51% rename from data_structures/LCA.py rename to data_structures/binary_tree/lowest_common_ancestor.py index 9c9d8ca629c7..2f1e893fcf99 100644 --- a/data_structures/LCA.py +++ b/data_structures/binary_tree/lowest_common_ancestor.py @@ -1,15 +1,31 @@ +# https://en.wikipedia.org/wiki/Lowest_common_ancestor +# https://en.wikipedia.org/wiki/Breadth-first_search + +from __future__ import annotations + import queue -def swap(a, b): +def swap(a: int, b: int) -> tuple[int, int]: + """ + Return a tuple (b, a) when given two integers a and b + >>> swap(2,3) + (3, 2) + >>> swap(3,4) + (4, 3) + >>> swap(67, 12) + (12, 67) + """ a ^= b b ^= a a ^= b return a, b -# creating sparse table which saves each nodes 2^ith parent -def creatSparse(max_node, parent): +def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]: + """ + creating sparse table which saves each nodes 2^i-th parent + """ j = 1 while (1 << j) < max_node: for i in range(1, max_node + 1): @@ -19,7 +35,9 @@ def creatSparse(max_node, parent): # returns lca of node u,v -def LCA(u, v, level, parent): +def lowest_common_ancestor( + u: int, v: int, level: list[int], parent: list[list[int]] +) -> list[list[int]]: # u must be deeper in the tree than v if level[u] < level[v]: u, v = swap(u, v) @@ -39,10 +57,18 @@ def LCA(u, v, level, parent): # runs a breadth first search from root node of the tree -# sets every nodes direct parent -# parent of root node is set to 0 -# calculates depth of each node from root node -def bfs(level, parent, max_node, graph, root=1): +def breadth_first_search( + level: list[int], + parent: list[list[int]], + max_node: int, + graph: dict[int, int], + root=1, +) -> tuple[list[int], list[list[int]]]: + """ + sets every nodes direct parent + parent of root node is set to 0 + calculates depth of each node from root node + """ level[root] = 0 q = queue.Queue(maxsize=max_node) q.put(root) @@ -56,7 +82,7 @@ def bfs(level, parent, max_node, graph, root=1): return level, parent -def main(): +def main() -> None: max_node = 13 # initializing with 0 parent = [[0 for _ in range(max_node + 10)] for _ in range(20)] @@ -75,16 +101,16 @@ def main(): 10: [], 11: [], 12: [], - 13: [] + 13: [], } - level, parent = bfs(level, parent, max_node, graph, 1) - parent = creatSparse(max_node, parent) - print("LCA of node 1 and 3 is: ", LCA(1, 3, level, parent)) - print("LCA of node 5 and 6 is: ", LCA(5, 6, level, parent)) - print("LCA of node 7 and 11 is: ", LCA(7, 11, level, parent)) - print("LCA of node 6 and 7 is: ", LCA(6, 7, level, parent)) - print("LCA of node 4 and 12 is: ", LCA(4, 12, level, parent)) - print("LCA of node 8 and 8 is: ", LCA(8, 8, level, parent)) + level, parent = breadth_first_search(level, parent, max_node, graph, 1) + parent = create_sparse(max_node, parent) + print("LCA of node 1 and 3 is: ", lowest_common_ancestor(1, 3, level, parent)) + print("LCA of node 5 and 6 is: ", lowest_common_ancestor(5, 6, level, parent)) + print("LCA of node 7 and 11 is: ", lowest_common_ancestor(7, 11, level, parent)) + print("LCA of node 6 and 7 is: ", lowest_common_ancestor(6, 7, level, parent)) + print("LCA of node 4 and 12 is: ", lowest_common_ancestor(4, 12, level, parent)) + print("LCA of node 8 and 8 is: ", lowest_common_ancestor(8, 8, level, parent)) if __name__ == "__main__": diff --git a/data_structures/binary_tree/non_recursive_segment_tree.py b/data_structures/binary_tree/non_recursive_segment_tree.py new file mode 100644 index 000000000000..064e5aded7b4 --- /dev/null +++ b/data_structures/binary_tree/non_recursive_segment_tree.py @@ -0,0 +1,158 @@ +""" +A non-recursive Segment Tree implementation with range query and single element update, +works virtually with any list of the same type of elements with a "commutative" +combiner. + +Explanation: +https://www.geeksforgeeks.org/iterative-segment-tree-range-minimum-query/ +https://www.geeksforgeeks.org/segment-tree-efficient-implementation/ + +>>> SegmentTree([1, 2, 3], lambda a, b: a + b).query(0, 2) +6 +>>> SegmentTree([3, 1, 2], min).query(0, 2) +1 +>>> SegmentTree([2, 3, 1], max).query(0, 2) +3 +>>> st = SegmentTree([1, 5, 7, -1, 6], lambda a, b: a + b) +>>> st.update(1, -1) +>>> st.update(2, 3) +>>> st.query(1, 2) +2 +>>> st.query(1, 1) +-1 +>>> st.update(4, 1) +>>> st.query(3, 4) +0 +>>> st = SegmentTree([[1, 2, 3], [3, 2, 1], [1, 1, 1]], lambda a, b: [a[i] + b[i] for i +... in range(len(a))]) +>>> st.query(0, 1) +[4, 4, 4] +>>> st.query(1, 2) +[4, 3, 2] +>>> st.update(1, [-1, -1, -1]) +>>> st.query(1, 2) +[0, 0, 0] +>>> st.query(0, 2) +[1, 2, 3] +""" +from __future__ import annotations + +from typing import Callable, TypeVar + +T = TypeVar("T") + + +class SegmentTree: + def __init__(self, arr: list[T], fnc: Callable[[T, T], T]) -> None: + """ + Segment Tree constructor, it works just with commutative combiner. + :param arr: list of elements for the segment tree + :param fnc: commutative function for combine two elements + + >>> SegmentTree(['a', 'b', 'c'], lambda a, b: '{}{}'.format(a, b)).query(0, 2) + 'abc' + >>> SegmentTree([(1, 2), (2, 3), (3, 4)], + ... lambda a, b: (a[0] + b[0], a[1] + b[1])).query(0, 2) + (6, 9) + """ + self.N = len(arr) + self.st = [None for _ in range(len(arr))] + arr + self.fn = fnc + self.build() + + def build(self) -> None: + for p in range(self.N - 1, 0, -1): + self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1]) + + def update(self, p: int, v: T) -> None: + """ + Update an element in log(N) time + :param p: position to be update + :param v: new value + + >>> st = SegmentTree([3, 1, 2, 4], min) + >>> st.query(0, 3) + 1 + >>> st.update(2, -1) + >>> st.query(0, 3) + -1 + """ + p += self.N + self.st[p] = v + while p > 1: + p = p // 2 + self.st[p] = self.fn(self.st[p * 2], self.st[p * 2 + 1]) + + def query(self, l: int, r: int) -> T: # noqa: E741 + """ + Get range query value in log(N) time + :param l: left element index + :param r: right element index + :return: element combined in the range [l, r] + + >>> st = SegmentTree([1, 2, 3, 4], lambda a, b: a + b) + >>> st.query(0, 2) + 6 + >>> st.query(1, 2) + 5 + >>> st.query(0, 3) + 10 + >>> st.query(2, 3) + 7 + """ + l, r = l + self.N, r + self.N # noqa: E741 + res = None + while l <= r: # noqa: E741 + if l % 2 == 1: + res = self.st[l] if res is None else self.fn(res, self.st[l]) + if r % 2 == 0: + res = self.st[r] if res is None else self.fn(res, self.st[r]) + l, r = (l + 1) // 2, (r - 1) // 2 + return res + + +if __name__ == "__main__": + from functools import reduce + + test_array = [1, 10, -2, 9, -3, 8, 4, -7, 5, 6, 11, -12] + + test_updates = { + 0: 7, + 1: 2, + 2: 6, + 3: -14, + 4: 5, + 5: 4, + 6: 7, + 7: -10, + 8: 9, + 9: 10, + 10: 12, + 11: 1, + } + + min_segment_tree = SegmentTree(test_array, min) + max_segment_tree = SegmentTree(test_array, max) + sum_segment_tree = SegmentTree(test_array, lambda a, b: a + b) + + def test_all_segments(): + """ + Test all possible segments + """ + for i in range(len(test_array)): + for j in range(i, len(test_array)): + min_range = reduce(min, test_array[i : j + 1]) + max_range = reduce(max, test_array[i : j + 1]) + sum_range = reduce(lambda a, b: a + b, test_array[i : j + 1]) + assert min_range == min_segment_tree.query(i, j) + assert max_range == max_segment_tree.query(i, j) + assert sum_range == sum_segment_tree.query(i, j) + + test_all_segments() + + for index, value in test_updates.items(): + test_array[index] = value + min_segment_tree.update(index, value) + max_segment_tree.update(index, value) + sum_segment_tree.update(index, value) + test_all_segments() diff --git a/data_structures/binary_tree/number_of_possible_binary_trees.py b/data_structures/binary_tree/number_of_possible_binary_trees.py new file mode 100644 index 000000000000..1ad8f2ed4287 --- /dev/null +++ b/data_structures/binary_tree/number_of_possible_binary_trees.py @@ -0,0 +1,102 @@ +""" +Hey, we are going to find an exciting number called Catalan number which is use to find +the number of possible binary search trees from tree of a given number of nodes. + +We will use the formula: t(n) = SUMMATION(i = 1 to n)t(i-1)t(n-i) + +Further details at Wikipedia: https://en.wikipedia.org/wiki/Catalan_number +""" +""" +Our Contribution: +Basically we Create the 2 function: + 1. catalan_number(node_count: int) -> int + Returns the number of possible binary search trees for n nodes. + 2. binary_tree_count(node_count: int) -> int + Returns the number of possible binary trees for n nodes. +""" + + +def binomial_coefficient(n: int, k: int) -> int: + """ + Since Here we Find the Binomial Coefficient: + https://en.wikipedia.org/wiki/Binomial_coefficient + C(n,k) = n! / k!(n-k)! + :param n: 2 times of Number of nodes + :param k: Number of nodes + :return: Integer Value + + >>> binomial_coefficient(4, 2) + 6 + """ + result = 1 # To kept the Calculated Value + # Since C(n, k) = C(n, n-k) + if k > (n - k): + k = n - k + # Calculate C(n,k) + for i in range(k): + result *= n - i + result //= i + 1 + return result + + +def catalan_number(node_count: int) -> int: + """ + We can find Catalan number many ways but here we use Binomial Coefficient because it + does the job in O(n) + + return the Catalan number of n using 2nCn/(n+1). + :param n: number of nodes + :return: Catalan number of n nodes + + >>> catalan_number(5) + 42 + >>> catalan_number(6) + 132 + """ + return binomial_coefficient(2 * node_count, node_count) // (node_count + 1) + + +def factorial(n: int) -> int: + """ + Return the factorial of a number. + :param n: Number to find the Factorial of. + :return: Factorial of n. + + >>> import math + >>> all(factorial(i) == math.factorial(i) for i in range(10)) + True + >>> factorial(-5) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: factorial() not defined for negative values + """ + if n < 0: + raise ValueError("factorial() not defined for negative values") + result = 1 + for i in range(1, n + 1): + result *= i + return result + + +def binary_tree_count(node_count: int) -> int: + """ + Return the number of possible of binary trees. + :param n: number of nodes + :return: Number of possible binary trees + + >>> binary_tree_count(5) + 5040 + >>> binary_tree_count(6) + 95040 + """ + return catalan_number(node_count) * factorial(node_count) + + +if __name__ == "__main__": + node_count = int(input("Enter the number of nodes: ").strip() or 0) + if node_count <= 0: + raise ValueError("We need some nodes to work with.") + print( + f"Given {node_count} nodes, there are {binary_tree_count(node_count)} " + f"binary trees and {catalan_number(node_count)} binary search trees." + ) diff --git a/data_structures/binary_tree/red_black_tree.py b/data_structures/binary_tree/red_black_tree.py new file mode 100644 index 000000000000..5d721edfa45b --- /dev/null +++ b/data_structures/binary_tree/red_black_tree.py @@ -0,0 +1,718 @@ +""" +python/black : true +flake8 : passed +""" +from typing import Iterator, Optional + + +class RedBlackTree: + """ + A Red-Black tree, which is a self-balancing BST (binary search + tree). + This tree has similar performance to AVL trees, but the balancing is + less strict, so it will perform faster for writing/deleting nodes + and slower for reading in the average case, though, because they're + both balanced binary search trees, both will get the same asymptotic + performance. + To read more about them, https://en.wikipedia.org/wiki/Red–black_tree + Unless otherwise specified, all asymptotic runtimes are specified in + terms of the size of the tree. + """ + + def __init__( + self, + label: Optional[int] = None, + color: int = 0, + parent: Optional["RedBlackTree"] = None, + left: Optional["RedBlackTree"] = None, + right: Optional["RedBlackTree"] = None, + ) -> None: + """Initialize a new Red-Black Tree node with the given values: + label: The value associated with this node + color: 0 if black, 1 if red + parent: The parent to this node + left: This node's left child + right: This node's right child + """ + self.label = label + self.parent = parent + self.left = left + self.right = right + self.color = color + + # Here are functions which are specific to red-black trees + + def rotate_left(self) -> "RedBlackTree": + """Rotate the subtree rooted at this node to the left and + returns the new root to this subtree. + Performing one rotation can be done in O(1). + """ + parent = self.parent + right = self.right + self.right = right.left + if self.right: + self.right.parent = self + self.parent = right + right.left = self + if parent is not None: + if parent.left == self: + parent.left = right + else: + parent.right = right + right.parent = parent + return right + + def rotate_right(self) -> "RedBlackTree": + """Rotate the subtree rooted at this node to the right and + returns the new root to this subtree. + Performing one rotation can be done in O(1). + """ + parent = self.parent + left = self.left + self.left = left.right + if self.left: + self.left.parent = self + self.parent = left + left.right = self + if parent is not None: + if parent.right is self: + parent.right = left + else: + parent.left = left + left.parent = parent + return left + + def insert(self, label: int) -> "RedBlackTree": + """Inserts label into the subtree rooted at self, performs any + rotations necessary to maintain balance, and then returns the + new root to this subtree (likely self). + This is guaranteed to run in O(log(n)) time. + """ + if self.label is None: + # Only possible with an empty tree + self.label = label + return self + if self.label == label: + return self + elif self.label > label: + if self.left: + self.left.insert(label) + else: + self.left = RedBlackTree(label, 1, self) + self.left._insert_repair() + else: + if self.right: + self.right.insert(label) + else: + self.right = RedBlackTree(label, 1, self) + self.right._insert_repair() + return self.parent or self + + def _insert_repair(self) -> None: + """Repair the coloring from inserting into a tree.""" + if self.parent is None: + # This node is the root, so it just needs to be black + self.color = 0 + elif color(self.parent) == 0: + # If the parent is black, then it just needs to be red + self.color = 1 + else: + uncle = self.parent.sibling + if color(uncle) == 0: + if self.is_left() and self.parent.is_right(): + self.parent.rotate_right() + self.right._insert_repair() + elif self.is_right() and self.parent.is_left(): + self.parent.rotate_left() + self.left._insert_repair() + elif self.is_left(): + self.grandparent.rotate_right() + self.parent.color = 0 + self.parent.right.color = 1 + else: + self.grandparent.rotate_left() + self.parent.color = 0 + self.parent.left.color = 1 + else: + self.parent.color = 0 + uncle.color = 0 + self.grandparent.color = 1 + self.grandparent._insert_repair() + + def remove(self, label: int) -> "RedBlackTree": + """Remove label from this tree.""" + if self.label == label: + if self.left and self.right: + # It's easier to balance a node with at most one child, + # so we replace this node with the greatest one less than + # it and remove that. + value = self.left.get_max() + self.label = value + self.left.remove(value) + else: + # This node has at most one non-None child, so we don't + # need to replace + child = self.left or self.right + if self.color == 1: + # This node is red, and its child is black + # The only way this happens to a node with one child + # is if both children are None leaves. + # We can just remove this node and call it a day. + if self.is_left(): + self.parent.left = None + else: + self.parent.right = None + else: + # The node is black + if child is None: + # This node and its child are black + if self.parent is None: + # The tree is now empty + return RedBlackTree(None) + else: + self._remove_repair() + if self.is_left(): + self.parent.left = None + else: + self.parent.right = None + self.parent = None + else: + # This node is black and its child is red + # Move the child node here and make it black + self.label = child.label + self.left = child.left + self.right = child.right + if self.left: + self.left.parent = self + if self.right: + self.right.parent = self + elif self.label > label: + if self.left: + self.left.remove(label) + else: + if self.right: + self.right.remove(label) + return self.parent or self + + def _remove_repair(self) -> None: + """Repair the coloring of the tree that may have been messed up.""" + if color(self.sibling) == 1: + self.sibling.color = 0 + self.parent.color = 1 + if self.is_left(): + self.parent.rotate_left() + else: + self.parent.rotate_right() + if ( + color(self.parent) == 0 + and color(self.sibling) == 0 + and color(self.sibling.left) == 0 + and color(self.sibling.right) == 0 + ): + self.sibling.color = 1 + self.parent._remove_repair() + return + if ( + color(self.parent) == 1 + and color(self.sibling) == 0 + and color(self.sibling.left) == 0 + and color(self.sibling.right) == 0 + ): + self.sibling.color = 1 + self.parent.color = 0 + return + if ( + self.is_left() + and color(self.sibling) == 0 + and color(self.sibling.right) == 0 + and color(self.sibling.left) == 1 + ): + self.sibling.rotate_right() + self.sibling.color = 0 + self.sibling.right.color = 1 + if ( + self.is_right() + and color(self.sibling) == 0 + and color(self.sibling.right) == 1 + and color(self.sibling.left) == 0 + ): + self.sibling.rotate_left() + self.sibling.color = 0 + self.sibling.left.color = 1 + if ( + self.is_left() + and color(self.sibling) == 0 + and color(self.sibling.right) == 1 + ): + self.parent.rotate_left() + self.grandparent.color = self.parent.color + self.parent.color = 0 + self.parent.sibling.color = 0 + if ( + self.is_right() + and color(self.sibling) == 0 + and color(self.sibling.left) == 1 + ): + self.parent.rotate_right() + self.grandparent.color = self.parent.color + self.parent.color = 0 + self.parent.sibling.color = 0 + + def check_color_properties(self) -> bool: + """Check the coloring of the tree, and return True iff the tree + is colored in a way which matches these five properties: + (wording stolen from wikipedia article) + 1. Each node is either red or black. + 2. The root node is black. + 3. All leaves are black. + 4. If a node is red, then both its children are black. + 5. Every path from any node to all of its descendent NIL nodes + has the same number of black nodes. + This function runs in O(n) time, because properties 4 and 5 take + that long to check. + """ + # I assume property 1 to hold because there is nothing that can + # make the color be anything other than 0 or 1. + + # Property 2 + if self.color: + # The root was red + print("Property 2") + return False + + # Property 3 does not need to be checked, because None is assumed + # to be black and is all the leaves. + + # Property 4 + if not self.check_coloring(): + print("Property 4") + return False + + # Property 5 + if self.black_height() is None: + print("Property 5") + return False + # All properties were met + return True + + def check_coloring(self) -> None: + """A helper function to recursively check Property 4 of a + Red-Black Tree. See check_color_properties for more info. + """ + if self.color == 1: + if color(self.left) == 1 or color(self.right) == 1: + return False + if self.left and not self.left.check_coloring(): + return False + if self.right and not self.right.check_coloring(): + return False + return True + + def black_height(self) -> int: + """Returns the number of black nodes from this node to the + leaves of the tree, or None if there isn't one such value (the + tree is color incorrectly). + """ + if self is None: + # If we're already at a leaf, there is no path + return 1 + left = RedBlackTree.black_height(self.left) + right = RedBlackTree.black_height(self.right) + if left is None or right is None: + # There are issues with coloring below children nodes + return None + if left != right: + # The two children have unequal depths + return None + # Return the black depth of children, plus one if this node is + # black + return left + (1 - self.color) + + # Here are functions which are general to all binary search trees + + def __contains__(self, label) -> bool: + """Search through the tree for label, returning True iff it is + found somewhere in the tree. + Guaranteed to run in O(log(n)) time. + """ + return self.search(label) is not None + + def search(self, label: int) -> "RedBlackTree": + """Search through the tree for label, returning its node if + it's found, and None otherwise. + This method is guaranteed to run in O(log(n)) time. + """ + if self.label == label: + return self + elif label > self.label: + if self.right is None: + return None + else: + return self.right.search(label) + else: + if self.left is None: + return None + else: + return self.left.search(label) + + def floor(self, label: int) -> int: + """Returns the largest element in this tree which is at most label. + This method is guaranteed to run in O(log(n)) time.""" + if self.label == label: + return self.label + elif self.label > label: + if self.left: + return self.left.floor(label) + else: + return None + else: + if self.right: + attempt = self.right.floor(label) + if attempt is not None: + return attempt + return self.label + + def ceil(self, label: int) -> int: + """Returns the smallest element in this tree which is at least label. + This method is guaranteed to run in O(log(n)) time. + """ + if self.label == label: + return self.label + elif self.label < label: + if self.right: + return self.right.ceil(label) + else: + return None + else: + if self.left: + attempt = self.left.ceil(label) + if attempt is not None: + return attempt + return self.label + + def get_max(self) -> int: + """Returns the largest element in this tree. + This method is guaranteed to run in O(log(n)) time. + """ + if self.right: + # Go as far right as possible + return self.right.get_max() + else: + return self.label + + def get_min(self) -> int: + """Returns the smallest element in this tree. + This method is guaranteed to run in O(log(n)) time. + """ + if self.left: + # Go as far left as possible + return self.left.get_min() + else: + return self.label + + @property + def grandparent(self) -> "RedBlackTree": + """Get the current node's grandparent, or None if it doesn't exist.""" + if self.parent is None: + return None + else: + return self.parent.parent + + @property + def sibling(self) -> "RedBlackTree": + """Get the current node's sibling, or None if it doesn't exist.""" + if self.parent is None: + return None + elif self.parent.left is self: + return self.parent.right + else: + return self.parent.left + + def is_left(self) -> bool: + """Returns true iff this node is the left child of its parent.""" + return self.parent and self.parent.left is self + + def is_right(self) -> bool: + """Returns true iff this node is the right child of its parent.""" + return self.parent and self.parent.right is self + + def __bool__(self) -> bool: + return True + + def __len__(self) -> int: + """ + Return the number of nodes in this tree. + """ + ln = 1 + if self.left: + ln += len(self.left) + if self.right: + ln += len(self.right) + return ln + + def preorder_traverse(self) -> Iterator[int]: + yield self.label + if self.left: + yield from self.left.preorder_traverse() + if self.right: + yield from self.right.preorder_traverse() + + def inorder_traverse(self) -> Iterator[int]: + if self.left: + yield from self.left.inorder_traverse() + yield self.label + if self.right: + yield from self.right.inorder_traverse() + + def postorder_traverse(self) -> Iterator[int]: + if self.left: + yield from self.left.postorder_traverse() + if self.right: + yield from self.right.postorder_traverse() + yield self.label + + def __repr__(self) -> str: + from pprint import pformat + + if self.left is None and self.right is None: + return "'{} {}'".format(self.label, (self.color and "red") or "blk") + return pformat( + { + "%s %s" + % (self.label, (self.color and "red") or "blk"): (self.left, self.right) + }, + indent=1, + ) + + def __eq__(self, other) -> bool: + """Test if two trees are equal.""" + if self.label == other.label: + return self.left == other.left and self.right == other.right + else: + return False + + +def color(node) -> int: + """Returns the color of a node, allowing for None leaves.""" + if node is None: + return 0 + else: + return node.color + + +""" +Code for testing the various +functions of the red-black tree. +""" + + +def test_rotations() -> bool: + """Test that the rotate_left and rotate_right functions work.""" + # Make a tree to test on + tree = RedBlackTree(0) + tree.left = RedBlackTree(-10, parent=tree) + tree.right = RedBlackTree(10, parent=tree) + tree.left.left = RedBlackTree(-20, parent=tree.left) + tree.left.right = RedBlackTree(-5, parent=tree.left) + tree.right.left = RedBlackTree(5, parent=tree.right) + tree.right.right = RedBlackTree(20, parent=tree.right) + # Make the right rotation + left_rot = RedBlackTree(10) + left_rot.left = RedBlackTree(0, parent=left_rot) + left_rot.left.left = RedBlackTree(-10, parent=left_rot.left) + left_rot.left.right = RedBlackTree(5, parent=left_rot.left) + left_rot.left.left.left = RedBlackTree(-20, parent=left_rot.left.left) + left_rot.left.left.right = RedBlackTree(-5, parent=left_rot.left.left) + left_rot.right = RedBlackTree(20, parent=left_rot) + tree = tree.rotate_left() + if tree != left_rot: + return False + tree = tree.rotate_right() + tree = tree.rotate_right() + # Make the left rotation + right_rot = RedBlackTree(-10) + right_rot.left = RedBlackTree(-20, parent=right_rot) + right_rot.right = RedBlackTree(0, parent=right_rot) + right_rot.right.left = RedBlackTree(-5, parent=right_rot.right) + right_rot.right.right = RedBlackTree(10, parent=right_rot.right) + right_rot.right.right.left = RedBlackTree(5, parent=right_rot.right.right) + right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right) + if tree != right_rot: + return False + return True + + +def test_insertion_speed() -> bool: + """Test that the tree balances inserts to O(log(n)) by doing a lot + of them. + """ + tree = RedBlackTree(-1) + for i in range(300000): + tree = tree.insert(i) + return True + + +def test_insert() -> bool: + """Test the insert() method of the tree correctly balances, colors, + and inserts. + """ + tree = RedBlackTree(0) + tree.insert(8) + tree.insert(-8) + tree.insert(4) + tree.insert(12) + tree.insert(10) + tree.insert(11) + ans = RedBlackTree(0, 0) + ans.left = RedBlackTree(-8, 0, ans) + ans.right = RedBlackTree(8, 1, ans) + ans.right.left = RedBlackTree(4, 0, ans.right) + ans.right.right = RedBlackTree(11, 0, ans.right) + ans.right.right.left = RedBlackTree(10, 1, ans.right.right) + ans.right.right.right = RedBlackTree(12, 1, ans.right.right) + return tree == ans + + +def test_insert_and_search() -> bool: + """Tests searching through the tree for values.""" + tree = RedBlackTree(0) + tree.insert(8) + tree.insert(-8) + tree.insert(4) + tree.insert(12) + tree.insert(10) + tree.insert(11) + if 5 in tree or -6 in tree or -10 in tree or 13 in tree: + # Found something not in there + return False + if not (11 in tree and 12 in tree and -8 in tree and 0 in tree): + # Didn't find something in there + return False + return True + + +def test_insert_delete() -> bool: + """Test the insert() and delete() method of the tree, verifying the + insertion and removal of elements, and the balancing of the tree. + """ + tree = RedBlackTree(0) + tree = tree.insert(-12) + tree = tree.insert(8) + tree = tree.insert(-8) + tree = tree.insert(15) + tree = tree.insert(4) + tree = tree.insert(12) + tree = tree.insert(10) + tree = tree.insert(9) + tree = tree.insert(11) + tree = tree.remove(15) + tree = tree.remove(-12) + tree = tree.remove(9) + if not tree.check_color_properties(): + return False + if list(tree.inorder_traverse()) != [-8, 0, 4, 8, 10, 11, 12]: + return False + return True + + +def test_floor_ceil() -> bool: + """Tests the floor and ceiling functions in the tree.""" + tree = RedBlackTree(0) + tree.insert(-16) + tree.insert(16) + tree.insert(8) + tree.insert(24) + tree.insert(20) + tree.insert(22) + tuples = [(-20, None, -16), (-10, -16, 0), (8, 8, 8), (50, 24, None)] + for val, floor, ceil in tuples: + if tree.floor(val) != floor or tree.ceil(val) != ceil: + return False + return True + + +def test_min_max() -> bool: + """Tests the min and max functions in the tree.""" + tree = RedBlackTree(0) + tree.insert(-16) + tree.insert(16) + tree.insert(8) + tree.insert(24) + tree.insert(20) + tree.insert(22) + if tree.get_max() != 22 or tree.get_min() != -16: + return False + return True + + +def test_tree_traversal() -> bool: + """Tests the three different tree traversal functions.""" + tree = RedBlackTree(0) + tree = tree.insert(-16) + tree.insert(16) + tree.insert(8) + tree.insert(24) + tree.insert(20) + tree.insert(22) + if list(tree.inorder_traverse()) != [-16, 0, 8, 16, 20, 22, 24]: + return False + if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]: + return False + if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]: + return False + return True + + +def test_tree_chaining() -> bool: + """Tests the three different tree chaining functions.""" + tree = RedBlackTree(0) + tree = tree.insert(-16).insert(16).insert(8).insert(24).insert(20).insert(22) + if list(tree.inorder_traverse()) != [-16, 0, 8, 16, 20, 22, 24]: + return False + if list(tree.preorder_traverse()) != [0, -16, 16, 8, 22, 20, 24]: + return False + if list(tree.postorder_traverse()) != [-16, 8, 20, 24, 22, 16, 0]: + return False + return True + + +def print_results(msg: str, passes: bool) -> None: + print(str(msg), "works!" if passes else "doesn't work :(") + + +def pytests() -> None: + assert test_rotations() + assert test_insert() + assert test_insert_and_search() + assert test_insert_delete() + assert test_floor_ceil() + assert test_tree_traversal() + assert test_tree_chaining() + + +def main() -> None: + """ + >>> pytests() + """ + print_results("Rotating right and left", test_rotations()) + + print_results("Inserting", test_insert()) + + print_results("Searching", test_insert_and_search()) + + print_results("Deleting", test_insert_delete()) + + print_results("Floor and ceil", test_floor_ceil()) + + print_results("Tree traversal", test_tree_traversal()) + + print_results("Tree traversal", test_tree_chaining()) + + print("Testing tree balancing...") + print("This should only be a few seconds.") + test_insertion_speed() + print("Done!") + + +if __name__ == "__main__": + main() diff --git a/data_structures/binary tree/segment_tree.py b/data_structures/binary_tree/segment_tree.py similarity index 50% rename from data_structures/binary tree/segment_tree.py rename to data_structures/binary_tree/segment_tree.py index 001bf999f391..10451ae68bb2 100644 --- a/data_structures/binary tree/segment_tree.py +++ b/data_structures/binary_tree/segment_tree.py @@ -1,71 +1,78 @@ -from __future__ import print_function import math + class SegmentTree: - def __init__(self, A): self.N = len(A) - self.st = [0] * (4 * self.N) # approximate the overall size of segment tree with array N + self.st = [0] * ( + 4 * self.N + ) # approximate the overall size of segment tree with array N self.build(1, 0, self.N - 1) - + def left(self, idx): return idx * 2 def right(self, idx): return idx * 2 + 1 - def build(self, idx, l, r): - if l == r: + def build(self, idx, l, r): # noqa: E741 + if l == r: # noqa: E741 self.st[idx] = A[l] else: mid = (l + r) // 2 self.build(self.left(idx), l, mid) self.build(self.right(idx), mid + 1, r) - self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) - + self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) + def update(self, a, b, val): return self.update_recursive(1, 0, self.N - 1, a - 1, b - 1, val) - - def update_recursive(self, idx, l, r, a, b, val): # update(1, 1, N, a, b, v) for update val v to [a,b] + + def update_recursive(self, idx, l, r, a, b, val): # noqa: E741 + """ + update(1, 1, N, a, b, v) for update val v to [a,b] + """ if r < a or l > b: return True - if l == r : + if l == r: # noqa: E741 self.st[idx] = val return True - mid = (l+r)//2 + mid = (l + r) // 2 self.update_recursive(self.left(idx), l, mid, a, b, val) - self.update_recursive(self.right(idx), mid+1, r, a, b, val) - self.st[idx] = max(self.st[self.left(idx)] , self.st[self.right(idx)]) + self.update_recursive(self.right(idx), mid + 1, r, a, b, val) + self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)]) return True def query(self, a, b): return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1) - def query_recursive(self, idx, l, r, a, b): #query(1, 1, N, a, b) for query max of [a,b] + def query_recursive(self, idx, l, r, a, b): # noqa: E741 + """ + query(1, 1, N, a, b) for query max of [a,b] + """ if r < a or l > b: return -math.inf - if l >= a and r <= b: + if l >= a and r <= b: # noqa: E741 return self.st[idx] - mid = (l+r)//2 + mid = (l + r) // 2 q1 = self.query_recursive(self.left(idx), l, mid, a, b) q2 = self.query_recursive(self.right(idx), mid + 1, r, a, b) return max(q1, q2) def showData(self): showList = [] - for i in range(1,N+1): + for i in range(1, N + 1): showList += [self.query(i, i)] - print (showList) - + print(showList) + -if __name__ == '__main__': - A = [1,2,-4,7,3,-5,6,11,-20,9,14,15,5,2,-8] +if __name__ == "__main__": + A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8] N = 15 segt = SegmentTree(A) - print (segt.query(4, 6)) - print (segt.query(7, 11)) - print (segt.query(7, 12)) - segt.update(1,3,111) - print (segt.query(1, 15)) - segt.update(7,8,235) + print(segt.query(4, 6)) + print(segt.query(7, 11)) + print(segt.query(7, 12)) + segt.update(1, 3, 111) + print(segt.query(1, 15)) + segt.update(7, 8, 235) segt.showData() diff --git a/data_structures/binary_tree/segment_tree_other.py b/data_structures/binary_tree/segment_tree_other.py new file mode 100644 index 000000000000..90afd7ca8b71 --- /dev/null +++ b/data_structures/binary_tree/segment_tree_other.py @@ -0,0 +1,241 @@ +""" +Segment_tree creates a segment tree with a given array and function, +allowing queries to be done later in log(N) time +function takes 2 values and returns a same type value +""" +from collections.abc import Sequence +from queue import Queue + + +class SegmentTreeNode: + def __init__(self, start, end, val, left=None, right=None): + self.start = start + self.end = end + self.val = val + self.mid = (start + end) // 2 + self.left = left + self.right = right + + def __str__(self): + return f"val: {self.val}, start: {self.start}, end: {self.end}" + + +class SegmentTree: + """ + >>> import operator + >>> num_arr = SegmentTree([2, 1, 5, 3, 4], operator.add) + >>> for node in num_arr.traverse(): + ... print(node) + ... + val: 15, start: 0, end: 4 + val: 8, start: 0, end: 2 + val: 7, start: 3, end: 4 + val: 3, start: 0, end: 1 + val: 5, start: 2, end: 2 + val: 3, start: 3, end: 3 + val: 4, start: 4, end: 4 + val: 2, start: 0, end: 0 + val: 1, start: 1, end: 1 + >>> + >>> num_arr.update(1, 5) + >>> for node in num_arr.traverse(): + ... print(node) + ... + val: 19, start: 0, end: 4 + val: 12, start: 0, end: 2 + val: 7, start: 3, end: 4 + val: 7, start: 0, end: 1 + val: 5, start: 2, end: 2 + val: 3, start: 3, end: 3 + val: 4, start: 4, end: 4 + val: 2, start: 0, end: 0 + val: 5, start: 1, end: 1 + >>> + >>> num_arr.query_range(3, 4) + 7 + >>> num_arr.query_range(2, 2) + 5 + >>> num_arr.query_range(1, 3) + 13 + >>> + >>> max_arr = SegmentTree([2, 1, 5, 3, 4], max) + >>> for node in max_arr.traverse(): + ... print(node) + ... + val: 5, start: 0, end: 4 + val: 5, start: 0, end: 2 + val: 4, start: 3, end: 4 + val: 2, start: 0, end: 1 + val: 5, start: 2, end: 2 + val: 3, start: 3, end: 3 + val: 4, start: 4, end: 4 + val: 2, start: 0, end: 0 + val: 1, start: 1, end: 1 + >>> + >>> max_arr.update(1, 5) + >>> for node in max_arr.traverse(): + ... print(node) + ... + val: 5, start: 0, end: 4 + val: 5, start: 0, end: 2 + val: 4, start: 3, end: 4 + val: 5, start: 0, end: 1 + val: 5, start: 2, end: 2 + val: 3, start: 3, end: 3 + val: 4, start: 4, end: 4 + val: 2, start: 0, end: 0 + val: 5, start: 1, end: 1 + >>> + >>> max_arr.query_range(3, 4) + 4 + >>> max_arr.query_range(2, 2) + 5 + >>> max_arr.query_range(1, 3) + 5 + >>> + >>> min_arr = SegmentTree([2, 1, 5, 3, 4], min) + >>> for node in min_arr.traverse(): + ... print(node) + ... + val: 1, start: 0, end: 4 + val: 1, start: 0, end: 2 + val: 3, start: 3, end: 4 + val: 1, start: 0, end: 1 + val: 5, start: 2, end: 2 + val: 3, start: 3, end: 3 + val: 4, start: 4, end: 4 + val: 2, start: 0, end: 0 + val: 1, start: 1, end: 1 + >>> + >>> min_arr.update(1, 5) + >>> for node in min_arr.traverse(): + ... print(node) + ... + val: 2, start: 0, end: 4 + val: 2, start: 0, end: 2 + val: 3, start: 3, end: 4 + val: 2, start: 0, end: 1 + val: 5, start: 2, end: 2 + val: 3, start: 3, end: 3 + val: 4, start: 4, end: 4 + val: 2, start: 0, end: 0 + val: 5, start: 1, end: 1 + >>> + >>> min_arr.query_range(3, 4) + 3 + >>> min_arr.query_range(2, 2) + 5 + >>> min_arr.query_range(1, 3) + 3 + >>> + + """ + + def __init__(self, collection: Sequence, function): + self.collection = collection + self.fn = function + if self.collection: + self.root = self._build_tree(0, len(collection) - 1) + + def update(self, i, val): + """ + Update an element in log(N) time + :param i: position to be update + :param val: new value + >>> import operator + >>> num_arr = SegmentTree([2, 1, 5, 3, 4], operator.add) + >>> num_arr.update(1, 5) + >>> num_arr.query_range(1, 3) + 13 + """ + self._update_tree(self.root, i, val) + + def query_range(self, i, j): + """ + Get range query value in log(N) time + :param i: left element index + :param j: right element index + :return: element combined in the range [i, j] + >>> import operator + >>> num_arr = SegmentTree([2, 1, 5, 3, 4], operator.add) + >>> num_arr.update(1, 5) + >>> num_arr.query_range(3, 4) + 7 + >>> num_arr.query_range(2, 2) + 5 + >>> num_arr.query_range(1, 3) + 13 + >>> + """ + return self._query_range(self.root, i, j) + + def _build_tree(self, start, end): + if start == end: + return SegmentTreeNode(start, end, self.collection[start]) + mid = (start + end) // 2 + left = self._build_tree(start, mid) + right = self._build_tree(mid + 1, end) + return SegmentTreeNode(start, end, self.fn(left.val, right.val), left, right) + + def _update_tree(self, node, i, val): + if node.start == i and node.end == i: + node.val = val + return + if i <= node.mid: + self._update_tree(node.left, i, val) + else: + self._update_tree(node.right, i, val) + node.val = self.fn(node.left.val, node.right.val) + + def _query_range(self, node, i, j): + if node.start == i and node.end == j: + return node.val + + if i <= node.mid: + if j <= node.mid: + # range in left child tree + return self._query_range(node.left, i, j) + else: + # range in left child tree and right child tree + return self.fn( + self._query_range(node.left, i, node.mid), + self._query_range(node.right, node.mid + 1, j), + ) + else: + # range in right child tree + return self._query_range(node.right, i, j) + + def traverse(self): + if self.root is not None: + queue = Queue() + queue.put(self.root) + while not queue.empty(): + node = queue.get() + yield node + + if node.left is not None: + queue.put(node.left) + + if node.right is not None: + queue.put(node.right) + + +if __name__ == "__main__": + import operator + + for fn in [operator.add, max, min]: + print("*" * 50) + arr = SegmentTree([2, 1, 5, 3, 4], fn) + for node in arr.traverse(): + print(node) + print() + + arr.update(1, 5) + for node in arr.traverse(): + print(node) + print() + + print(arr.query_range(3, 4)) # 7 + print(arr.query_range(2, 2)) # 5 + print(arr.query_range(1, 3)) # 13 + print() diff --git a/data_structures/binary_tree/treap.py b/data_structures/binary_tree/treap.py new file mode 100644 index 000000000000..26648f7aba61 --- /dev/null +++ b/data_structures/binary_tree/treap.py @@ -0,0 +1,184 @@ +# flake8: noqa + +from __future__ import annotations + +from random import random + + +class Node: + """ + Treap's node + Treap is a binary tree by value and heap by priority + """ + + def __init__(self, value: int = None): + self.value = value + self.prior = random() + self.left = None + self.right = None + + def __repr__(self): + from pprint import pformat + + if self.left is None and self.right is None: + return f"'{self.value}: {self.prior:.5}'" + else: + return pformat( + {f"{self.value}: {self.prior:.5}": (self.left, self.right)}, indent=1 + ) + + def __str__(self): + value = str(self.value) + " " + left = str(self.left or "") + right = str(self.right or "") + return value + left + right + + +def split(root: Node, value: int) -> tuple[Node, Node]: + """ + We split current tree into 2 trees with value: + + Left tree contains all values less than split value. + Right tree contains all values greater or equal, than split value + """ + if root is None: # None tree is split into 2 Nones + return (None, None) + elif root.value is None: + return (None, None) + else: + if value < root.value: + """ + Right tree's root will be current node. + Now we split(with the same value) current node's left son + Left tree: left part of that split + Right tree's left son: right part of that split + """ + left, root.left = split(root.left, value) + return (left, root) + else: + """ + Just symmetric to previous case + """ + root.right, right = split(root.right, value) + return (root, right) + + +def merge(left: Node, right: Node) -> Node: + """ + We merge 2 trees into one. + Note: all left tree's values must be less than all right tree's + """ + if (not left) or (not right): # If one node is None, return the other + return left or right + elif left.prior < right.prior: + """ + Left will be root because it has more priority + Now we need to merge left's right son and right tree + """ + left.right = merge(left.right, right) + return left + else: + """ + Symmetric as well + """ + right.left = merge(left, right.left) + return right + + +def insert(root: Node, value: int) -> Node: + """ + Insert element + + Split current tree with a value into left, right, + Insert new node into the middle + Merge left, node, right into root + """ + node = Node(value) + left, right = split(root, value) + return merge(merge(left, node), right) + + +def erase(root: Node, value: int) -> Node: + """ + Erase element + + Split all nodes with values less into left, + Split all nodes with values greater into right. + Merge left, right + """ + left, right = split(root, value - 1) + _, right = split(right, value) + return merge(left, right) + + +def inorder(root: Node): + """ + Just recursive print of a tree + """ + if not root: # None + return + else: + inorder(root.left) + print(root.value, end=",") + inorder(root.right) + + +def interactTreap(root, args): + """ + Commands: + + value to add value into treap + - value to erase all nodes with value + + >>> root = interactTreap(None, "+1") + >>> inorder(root) + 1, + >>> root = interactTreap(root, "+3 +5 +17 +19 +2 +16 +4 +0") + >>> inorder(root) + 0,1,2,3,4,5,16,17,19, + >>> root = interactTreap(root, "+4 +4 +4") + >>> inorder(root) + 0,1,2,3,4,4,4,4,5,16,17,19, + >>> root = interactTreap(root, "-0") + >>> inorder(root) + 1,2,3,4,4,4,4,5,16,17,19, + >>> root = interactTreap(root, "-4") + >>> inorder(root) + 1,2,3,5,16,17,19, + >>> root = interactTreap(root, "=0") + Unknown command + """ + for arg in args.split(): + if arg[0] == "+": + root = insert(root, int(arg[1:])) + + elif arg[0] == "-": + root = erase(root, int(arg[1:])) + + else: + print("Unknown command") + + return root + + +def main(): + """After each command, program prints treap""" + root = None + print( + "enter numbers to create a tree, + value to add value into treap, " + "- value to erase all nodes with value. 'q' to quit. " + ) + + args = input() + while args != "q": + root = interactTreap(root, args) + print(root) + args = input() + + print("good by!") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/data_structures/disjoint_set/__init__.py b/data_structures/disjoint_set/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/data_structures/disjoint_set/alternate_disjoint_set.py b/data_structures/disjoint_set/alternate_disjoint_set.py new file mode 100644 index 000000000000..5103335bc80a --- /dev/null +++ b/data_structures/disjoint_set/alternate_disjoint_set.py @@ -0,0 +1,68 @@ +""" +Implements a disjoint set using Lists and some added heuristics for efficiency +Union by Rank Heuristic and Path Compression +""" + + +class DisjointSet: + def __init__(self, set_counts: list) -> None: + """ + Initialize with a list of the number of items in each set + and with rank = 1 for each set + """ + self.set_counts = set_counts + self.max_set = max(set_counts) + num_sets = len(set_counts) + self.ranks = [1] * num_sets + self.parents = list(range(num_sets)) + + def merge(self, src: int, dst: int) -> bool: + """ + Merge two sets together using Union by rank heuristic + Return True if successful + Merge two disjoint sets + >>> A = DisjointSet([1, 1, 1]) + >>> A.merge(1, 2) + True + >>> A.merge(0, 2) + True + >>> A.merge(0, 1) + False + """ + src_parent = self.get_parent(src) + dst_parent = self.get_parent(dst) + + if src_parent == dst_parent: + return False + + if self.ranks[dst_parent] >= self.ranks[src_parent]: + self.set_counts[dst_parent] += self.set_counts[src_parent] + self.set_counts[src_parent] = 0 + self.parents[src_parent] = dst_parent + if self.ranks[dst_parent] == self.ranks[src_parent]: + self.ranks[dst_parent] += 1 + joined_set_size = self.set_counts[dst_parent] + else: + self.set_counts[src_parent] += self.set_counts[dst_parent] + self.set_counts[dst_parent] = 0 + self.parents[dst_parent] = src_parent + joined_set_size = self.set_counts[src_parent] + + self.max_set = max(self.max_set, joined_set_size) + return True + + def get_parent(self, disj_set: int) -> int: + """ + Find the Parent of a given set + >>> A = DisjointSet([1, 1, 1]) + >>> A.merge(1, 2) + True + >>> A.get_parent(0) + 0 + >>> A.get_parent(1) + 2 + """ + if self.parents[disj_set] == disj_set: + return disj_set + self.parents[disj_set] = self.get_parent(self.parents[disj_set]) + return self.parents[disj_set] diff --git a/data_structures/disjoint_set/disjoint_set.py b/data_structures/disjoint_set/disjoint_set.py new file mode 100644 index 000000000000..a93b89621c4a --- /dev/null +++ b/data_structures/disjoint_set/disjoint_set.py @@ -0,0 +1,79 @@ +""" + disjoint set + Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure +""" + + +class Node: + def __init__(self, data): + self.data = data + + +def make_set(x): + """ + make x as a set. + """ + # rank is the distance from x to its' parent + # root's rank is 0 + x.rank = 0 + x.parent = x + + +def union_set(x, y): + """ + union two sets. + set with bigger rank should be parent, so that the + disjoint set tree will be more flat. + """ + x, y = find_set(x), find_set(y) + if x.rank > y.rank: + y.parent = x + else: + x.parent = y + if x.rank == y.rank: + y.rank += 1 + + +def find_set(x): + """ + return the parent of x + """ + if x != x.parent: + x.parent = find_set(x.parent) + return x.parent + + +def find_python_set(node: Node) -> set: + """ + Return a Python Standard Library set that contains i. + """ + sets = ({0, 1, 2}, {3, 4, 5}) + for s in sets: + if node.data in s: + return s + raise ValueError(f"{node.data} is not in {sets}") + + +def test_disjoint_set(): + """ + >>> test_disjoint_set() + """ + vertex = [Node(i) for i in range(6)] + for v in vertex: + make_set(v) + + union_set(vertex[0], vertex[1]) + union_set(vertex[1], vertex[2]) + union_set(vertex[3], vertex[4]) + union_set(vertex[3], vertex[5]) + + for node0 in vertex: + for node1 in vertex: + if find_python_set(node0).isdisjoint(find_python_set(node1)): + assert find_set(node0) != find_set(node1) + else: + assert find_set(node0) == find_set(node1) + + +if __name__ == "__main__": + test_disjoint_set() diff --git a/data_structures/hashing/__init__.py b/data_structures/hashing/__init__.py index b96ddd478458..e69de29bb2d1 100644 --- a/data_structures/hashing/__init__.py +++ b/data_structures/hashing/__init__.py @@ -1,6 +0,0 @@ -from .hash_table import HashTable - -class QuadraticProbing(HashTable): - - def __init__(self): - super(self.__class__, self).__init__() diff --git a/data_structures/hashing/double_hash.py b/data_structures/hashing/double_hash.py index 60098cda0ce1..57b1ffff4770 100644 --- a/data_structures/hashing/double_hash.py +++ b/data_structures/hashing/double_hash.py @@ -1,33 +1,41 @@ #!/usr/bin/env python3 - from .hash_table import HashTable -from number_theory.prime_numbers import next_prime, check_prime +from .number_theory.prime_numbers import check_prime, next_prime class DoubleHash(HashTable): """ - Hash Table example with open addressing and Double Hash + Hash Table example with open addressing and Double Hash """ + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def __hash_function_2(self, value, data): - next_prime_gt = next_prime(value % self.size_table) \ - if not check_prime(value % self.size_table) else value % self.size_table #gt = bigger than + next_prime_gt = ( + next_prime(value % self.size_table) + if not check_prime(value % self.size_table) + else value % self.size_table + ) # gt = bigger than return next_prime_gt - (data % next_prime_gt) def __hash_double_function(self, key, data, increment): return (increment * self.__hash_function_2(key, data)) % self.size_table - def _colision_resolution(self, key, data=None): + def _collision_resolution(self, key, data=None): i = 1 new_key = self.hash_function(data) while self.values[new_key] is not None and self.values[new_key] != key: - new_key = self.__hash_double_function(key, data, i) if \ - self.balanced_factor() >= self.lim_charge else None - if new_key is None: break - else: i += 1 + new_key = ( + self.__hash_double_function(key, data, i) + if self.balanced_factor() >= self.lim_charge + else None + ) + if new_key is None: + break + else: + i += 1 return new_key diff --git a/data_structures/hashing/hash_table.py b/data_structures/hashing/hash_table.py index f0de128d1ad1..fd9e6eec134c 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -from number_theory.prime_numbers import next_prime +from .number_theory.prime_numbers import next_prime class HashTable: """ - Basic Hash Table example with open addressing and linear probing + Basic Hash Table example with open addressing and linear probing """ def __init__(self, size_table, charge_factor=None, lim_charge=None): @@ -19,15 +19,16 @@ def keys(self): return self._keys def balanced_factor(self): - return sum([1 for slot in self.values - if slot is not None]) / (self.size_table * self.charge_factor) + return sum([1 for slot in self.values if slot is not None]) / ( + self.size_table * self.charge_factor + ) def hash_function(self, key): return key % self.size_table def _step_by_step(self, step_ord): - print("step {0}".format(step_ord)) + print(f"step {step_ord}") print([i for i in range(len(self.values))]) print(self.values) @@ -43,11 +44,10 @@ def _set_value(self, key, data): self.values[key] = data self._keys[key] = data - def _colision_resolution(self, key, data=None): + def _collision_resolution(self, key, data=None): new_key = self.hash_function(key + 1) - while self.values[new_key] is not None \ - and self.values[new_key] != key: + while self.values[new_key] is not None and self.values[new_key] != key: if self.values.count(None) > 0: new_key = self.hash_function(new_key + 1) @@ -61,8 +61,9 @@ def rehashing(self): survivor_values = [value for value in self.values if value is not None] self.size_table = next_prime(self.size_table, factor=2) self._keys.clear() - self.values = [None] * self.size_table #hell's pointers D: don't DRY ;/ - map(self.insert_data, survivor_values) + self.values = [None] * self.size_table # hell's pointers D: don't DRY ;/ + for value in survivor_values: + self.insert_data(value) def insert_data(self, data): key = self.hash_function(data) @@ -74,11 +75,9 @@ def insert_data(self, data): pass else: - colision_resolution = self._colision_resolution(key, data) - if colision_resolution is not None: - self._set_value(colision_resolution, data) + collision_resolution = self._collision_resolution(key, data) + if collision_resolution is not None: + self._set_value(collision_resolution, data) else: self.rehashing() self.insert_data(data) - - diff --git a/data_structures/hashing/hash_table_with_linked_list.py b/data_structures/hashing/hash_table_with_linked_list.py index 9689e4fc9fcf..fe838268fce8 100644 --- a/data_structures/hashing/hash_table_with_linked_list.py +++ b/data_structures/hashing/hash_table_with_linked_list.py @@ -1,24 +1,27 @@ -from .hash_table import HashTable from collections import deque +from .hash_table import HashTable + class HashTableWithLinkedList(HashTable): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def _set_value(self, key, data): - self.values[key] = deque([]) if self.values[key] is None else self.values[key] + self.values[key] = deque([]) if self.values[key] is None else self.values[key] self.values[key].appendleft(data) self._keys[key] = self.values[key] def balanced_factor(self): - return sum([self.charge_factor - len(slot) for slot in self.values])\ - / self.size_table * self.charge_factor - - def _colision_resolution(self, key, data=None): - if not (len(self.values[key]) == self.charge_factor - and self.values.count(None) == 0): - return key - return super()._colision_resolution(key, data) - + return ( + sum([self.charge_factor - len(slot) for slot in self.values]) + / self.size_table + * self.charge_factor + ) + def _collision_resolution(self, key, data=None): + if not ( + len(self.values[key]) == self.charge_factor and self.values.count(None) == 0 + ): + return key + return super()._collision_resolution(key, data) diff --git a/data_structures/hashing/number_theory/prime_numbers.py b/data_structures/hashing/number_theory/prime_numbers.py index 8a521bc45758..db4d40f475b2 100644 --- a/data_structures/hashing/number_theory/prime_numbers.py +++ b/data_structures/hashing/number_theory/prime_numbers.py @@ -5,25 +5,25 @@ def check_prime(number): - """ - it's not the best solution - """ - special_non_primes = [0,1,2] - if number in special_non_primes[:2]: - return 2 - elif number == special_non_primes[-1]: - return 3 - - return all([number % i for i in range(2, number)]) + """ + it's not the best solution + """ + special_non_primes = [0, 1, 2] + if number in special_non_primes[:2]: + return 2 + elif number == special_non_primes[-1]: + return 3 + + return all([number % i for i in range(2, number)]) def next_prime(value, factor=1, **kwargs): value = factor * value first_value_val = value - + while not check_prime(value): value += 1 if not ("desc" in kwargs.keys() and kwargs["desc"] is True) else -1 - + if value == first_value_val: return next_prime(value + 1, **kwargs) return value diff --git a/data_structures/hashing/quadratic_probing.py b/data_structures/hashing/quadratic_probing.py index f7a9ac1ae347..0930340a347f 100644 --- a/data_structures/hashing/quadratic_probing.py +++ b/data_structures/hashing/quadratic_probing.py @@ -5,20 +5,23 @@ class QuadraticProbing(HashTable): """ - Basic Hash Table example with open addressing using Quadratic Probing + Basic Hash Table example with open addressing using Quadratic Probing """ + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - def _colision_resolution(self, key, data=None): + def _collision_resolution(self, key, data=None): i = 1 - new_key = self.hash_function(key + i*i) + new_key = self.hash_function(key + i * i) - while self.values[new_key] is not None \ - and self.values[new_key] != key: + while self.values[new_key] is not None and self.values[new_key] != key: i += 1 - new_key = self.hash_function(key + i*i) if not \ - self.balanced_factor() >= self.lim_charge else None + new_key = ( + self.hash_function(key + i * i) + if not self.balanced_factor() >= self.lim_charge + else None + ) if new_key is None: break diff --git a/data_structures/heap/__init__.py b/data_structures/heap/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/data_structures/heap/binomial_heap.py b/data_structures/heap/binomial_heap.py new file mode 100644 index 000000000000..334b444eaaff --- /dev/null +++ b/data_structures/heap/binomial_heap.py @@ -0,0 +1,403 @@ +# flake8: noqa + +""" +Binomial Heap +Reference: Advanced Data Structures, Peter Brass +""" + + +class Node: + """ + Node in a doubly-linked binomial tree, containing: + - value + - size of left subtree + - link to left, right and parent nodes + """ + + def __init__(self, val): + self.val = val + # Number of nodes in left subtree + self.left_tree_size = 0 + self.left = None + self.right = None + self.parent = None + + def mergeTrees(self, other): + """ + In-place merge of two binomial trees of equal size. + Returns the root of the resulting tree + """ + assert self.left_tree_size == other.left_tree_size, "Unequal Sizes of Blocks" + + if self.val < other.val: + other.left = self.right + other.parent = None + if self.right: + self.right.parent = other + self.right = other + self.left_tree_size = self.left_tree_size * 2 + 1 + return self + else: + self.left = other.right + self.parent = None + if other.right: + other.right.parent = self + other.right = self + other.left_tree_size = other.left_tree_size * 2 + 1 + return other + + +class BinomialHeap: + r""" + Min-oriented priority queue implemented with the Binomial Heap data + structure implemented with the BinomialHeap class. It supports: + - Insert element in a heap with n elements: Guaranteed logn, amoratized 1 + - Merge (meld) heaps of size m and n: O(logn + logm) + - Delete Min: O(logn) + - Peek (return min without deleting it): O(1) + + Example: + + Create a random permutation of 30 integers to be inserted and 19 of them deleted + >>> import numpy as np + >>> permutation = np.random.permutation(list(range(30))) + + Create a Heap and insert the 30 integers + __init__() test + >>> first_heap = BinomialHeap() + + 30 inserts - insert() test + >>> for number in permutation: + ... first_heap.insert(number) + + Size test + >>> print(first_heap.size) + 30 + + Deleting - delete() test + >>> for i in range(25): + ... print(first_heap.deleteMin(), end=" ") + 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 + + Create a new Heap + >>> second_heap = BinomialHeap() + >>> vals = [17, 20, 31, 34] + >>> for value in vals: + ... second_heap.insert(value) + + + The heap should have the following structure: + + 17 + / \ + # 31 + / \ + 20 34 + / \ / \ + # # # # + + preOrder() test + >>> print(second_heap.preOrder()) + [(17, 0), ('#', 1), (31, 1), (20, 2), ('#', 3), ('#', 3), (34, 2), ('#', 3), ('#', 3)] + + printing Heap - __str__() test + >>> print(second_heap) + 17 + -# + -31 + --20 + ---# + ---# + --34 + ---# + ---# + + mergeHeaps() test + >>> merged = second_heap.mergeHeaps(first_heap) + >>> merged.peek() + 17 + + values in merged heap; (merge is inplace) + >>> while not first_heap.isEmpty(): + ... print(first_heap.deleteMin(), end=" ") + 17 20 25 26 27 28 29 31 34 + """ + + def __init__(self, bottom_root=None, min_node=None, heap_size=0): + self.size = heap_size + self.bottom_root = bottom_root + self.min_node = min_node + + def mergeHeaps(self, other): + """ + In-place merge of two binomial heaps. + Both of them become the resulting merged heap + """ + + # Empty heaps corner cases + if other.size == 0: + return + if self.size == 0: + self.size = other.size + self.bottom_root = other.bottom_root + self.min_node = other.min_node + return + # Update size + self.size = self.size + other.size + + # Update min.node + if self.min_node.val > other.min_node.val: + self.min_node = other.min_node + # Merge + + # Order roots by left_subtree_size + combined_roots_list = [] + i, j = self.bottom_root, other.bottom_root + while i or j: + if i and ((not j) or i.left_tree_size < j.left_tree_size): + combined_roots_list.append((i, True)) + i = i.parent + else: + combined_roots_list.append((j, False)) + j = j.parent + # Insert links between them + for i in range(len(combined_roots_list) - 1): + if combined_roots_list[i][1] != combined_roots_list[i + 1][1]: + combined_roots_list[i][0].parent = combined_roots_list[i + 1][0] + combined_roots_list[i + 1][0].left = combined_roots_list[i][0] + # Consecutively merge roots with same left_tree_size + i = combined_roots_list[0][0] + while i.parent: + if ( + (i.left_tree_size == i.parent.left_tree_size) and (not i.parent.parent) + ) or ( + i.left_tree_size == i.parent.left_tree_size + and i.left_tree_size != i.parent.parent.left_tree_size + ): + + # Neighbouring Nodes + previous_node = i.left + next_node = i.parent.parent + + # Merging trees + i = i.mergeTrees(i.parent) + + # Updating links + i.left = previous_node + i.parent = next_node + if previous_node: + previous_node.parent = i + if next_node: + next_node.left = i + else: + i = i.parent + # Updating self.bottom_root + while i.left: + i = i.left + self.bottom_root = i + + # Update other + other.size = self.size + other.bottom_root = self.bottom_root + other.min_node = self.min_node + + # Return the merged heap + return self + + def insert(self, val): + """ + insert a value in the heap + """ + if self.size == 0: + self.bottom_root = Node(val) + self.size = 1 + self.min_node = self.bottom_root + else: + # Create new node + new_node = Node(val) + + # Update size + self.size += 1 + + # update min_node + if val < self.min_node.val: + self.min_node = new_node + # Put new_node as a bottom_root in heap + self.bottom_root.left = new_node + new_node.parent = self.bottom_root + self.bottom_root = new_node + + # Consecutively merge roots with same left_tree_size + while ( + self.bottom_root.parent + and self.bottom_root.left_tree_size + == self.bottom_root.parent.left_tree_size + ): + + # Next node + next_node = self.bottom_root.parent.parent + + # Merge + self.bottom_root = self.bottom_root.mergeTrees(self.bottom_root.parent) + + # Update Links + self.bottom_root.parent = next_node + self.bottom_root.left = None + if next_node: + next_node.left = self.bottom_root + + def peek(self): + """ + return min element without deleting it + """ + return self.min_node.val + + def isEmpty(self): + return self.size == 0 + + def deleteMin(self): + """ + delete min element and return it + """ + # assert not self.isEmpty(), "Empty Heap" + + # Save minimal value + min_value = self.min_node.val + + # Last element in heap corner case + if self.size == 1: + # Update size + self.size = 0 + + # Update bottom root + self.bottom_root = None + + # Update min_node + self.min_node = None + + return min_value + # No right subtree corner case + # The structure of the tree implies that this should be the bottom root + # and there is at least one other root + if self.min_node.right is None: + # Update size + self.size -= 1 + + # Update bottom root + self.bottom_root = self.bottom_root.parent + self.bottom_root.left = None + + # Update min_node + self.min_node = self.bottom_root + i = self.bottom_root.parent + while i: + if i.val < self.min_node.val: + self.min_node = i + i = i.parent + return min_value + # General case + # Find the BinomialHeap of the right subtree of min_node + bottom_of_new = self.min_node.right + bottom_of_new.parent = None + min_of_new = bottom_of_new + size_of_new = 1 + + # Size, min_node and bottom_root + while bottom_of_new.left: + size_of_new = size_of_new * 2 + 1 + bottom_of_new = bottom_of_new.left + if bottom_of_new.val < min_of_new.val: + min_of_new = bottom_of_new + # Corner case of single root on top left path + if (not self.min_node.left) and (not self.min_node.parent): + self.size = size_of_new + self.bottom_root = bottom_of_new + self.min_node = min_of_new + # print("Single root, multiple nodes case") + return min_value + # Remaining cases + # Construct heap of right subtree + newHeap = BinomialHeap( + bottom_root=bottom_of_new, min_node=min_of_new, heap_size=size_of_new + ) + + # Update size + self.size = self.size - 1 - size_of_new + + # Neighbour nodes + previous_node = self.min_node.left + next_node = self.min_node.parent + + # Initialize new bottom_root and min_node + self.min_node = previous_node or next_node + self.bottom_root = next_node + + # Update links of previous_node and search below for new min_node and + # bottom_root + if previous_node: + previous_node.parent = next_node + + # Update bottom_root and search for min_node below + self.bottom_root = previous_node + self.min_node = previous_node + while self.bottom_root.left: + self.bottom_root = self.bottom_root.left + if self.bottom_root.val < self.min_node.val: + self.min_node = self.bottom_root + if next_node: + next_node.left = previous_node + + # Search for new min_node above min_node + i = next_node + while i: + if i.val < self.min_node.val: + self.min_node = i + i = i.parent + # Merge heaps + self.mergeHeaps(newHeap) + + return min_value + + def preOrder(self): + """ + Returns the Pre-order representation of the heap including + values of nodes plus their level distance from the root; + Empty nodes appear as # + """ + # Find top root + top_root = self.bottom_root + while top_root.parent: + top_root = top_root.parent + # preorder + heap_preOrder = [] + self.__traversal(top_root, heap_preOrder) + return heap_preOrder + + def __traversal(self, curr_node, preorder, level=0): + """ + Pre-order traversal of nodes + """ + if curr_node: + preorder.append((curr_node.val, level)) + self.__traversal(curr_node.left, preorder, level + 1) + self.__traversal(curr_node.right, preorder, level + 1) + else: + preorder.append(("#", level)) + + def __str__(self): + """ + Overwriting str for a pre-order print of nodes in heap; + Performance is poor, so use only for small examples + """ + if self.isEmpty(): + return "" + preorder_heap = self.preOrder() + + return "\n".join(("-" * level + str(value)) for value, level in preorder_heap) + + +# Unit Tests +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/heap/heap.py b/data_structures/heap/heap.py index 39778f725c3a..8592362c23b9 100644 --- a/data_structures/heap/heap.py +++ b/data_structures/heap/heap.py @@ -1,91 +1,163 @@ -#!/usr/bin/python +from typing import Iterable, List, Optional -from __future__ import print_function, division -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 - -#This heap class start from here. class Heap: - def __init__(self): #Default constructor of heap class. - self.h = [] - self.currsize = 0 - - def leftChild(self,i): - if 2*i+1 < self.currsize: - return 2*i+1 - return None - - def rightChild(self,i): - if 2*i+2 < self.currsize: - return 2*i+2 - return None - - def maxHeapify(self,node): - if node < self.currsize: - m = node - lc = self.leftChild(node) - rc = self.rightChild(node) - if lc is not None and self.h[lc] > self.h[m]: - m = lc - if rc is not None and self.h[rc] > self.h[m]: - m = rc - if m!=node: - temp = self.h[node] - self.h[node] = self.h[m] - self.h[m] = temp - self.maxHeapify(m) - - def buildHeap(self,a): #This function is used to build the heap from the data container 'a'. - self.currsize = len(a) - self.h = list(a) - for i in range(self.currsize//2,-1,-1): - self.maxHeapify(i) - - def getMax(self): #This function is used to get maximum value from the heap. - if self.currsize >= 1: - me = self.h[0] - temp = self.h[0] - self.h[0] = self.h[self.currsize-1] - self.h[self.currsize-1] = temp - self.currsize -= 1 - self.maxHeapify(0) - return me - return None - - def heapSort(self): #This function is used to sort the heap. - size = self.currsize - while self.currsize-1 >= 0: - temp = self.h[0] - self.h[0] = self.h[self.currsize-1] - self.h[self.currsize-1] = temp - self.currsize -= 1 - self.maxHeapify(0) - self.currsize = size - - def insert(self,data): #This function is used to insert data in the heap. - self.h.append(data) - curr = self.currsize - self.currsize+=1 - while self.h[curr] > self.h[curr/2]: - temp = self.h[curr/2] - self.h[curr/2] = self.h[curr] - self.h[curr] = temp - curr = curr/2 - - def display(self): #This function is used to print the heap. - print(self.h) - -def main(): - l = list(map(int, raw_input().split())) - h = Heap() - h.buildHeap(l) - h.heapSort() - h.display() - -if __name__=='__main__': - main() + """A Max Heap Implementation + + >>> unsorted = [103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5] + >>> h = Heap() + >>> h.build_max_heap(unsorted) + >>> print(h) + [209, 201, 25, 103, 107, 15, 1, 9, 7, 11, 5] + >>> + >>> h.extract_max() + 209 + >>> print(h) + [201, 107, 25, 103, 11, 15, 1, 9, 7, 5] + >>> + >>> h.insert(100) + >>> print(h) + [201, 107, 25, 103, 100, 15, 1, 9, 7, 5, 11] + >>> + >>> h.heap_sort() + >>> print(h) + [1, 5, 7, 9, 11, 15, 25, 100, 103, 107, 201] + """ + + def __init__(self) -> None: + self.h: List[float] = [] + self.heap_size: int = 0 + + def __repr__(self) -> str: + return str(self.h) + + def parent_index(self, child_idx: int) -> Optional[int]: + """ return the parent index of given child """ + if child_idx > 0: + return (child_idx - 1) // 2 + return None + + def left_child_idx(self, parent_idx: int) -> Optional[int]: + """ + return the left child index if the left child exists. + if not, return None. + """ + left_child_index = 2 * parent_idx + 1 + if left_child_index < self.heap_size: + return left_child_index + return None + + def right_child_idx(self, parent_idx: int) -> Optional[int]: + """ + return the right child index if the right child exists. + if not, return None. + """ + right_child_index = 2 * parent_idx + 2 + if right_child_index < self.heap_size: + return right_child_index + return None + + def max_heapify(self, index: int) -> None: + """ + correct a single violation of the heap property in a subtree's root. + """ + if index < self.heap_size: + violation: int = index + left_child = self.left_child_idx(index) + right_child = self.right_child_idx(index) + # check which child is larger than its parent + if left_child is not None and self.h[left_child] > self.h[violation]: + violation = left_child + if right_child is not None and self.h[right_child] > self.h[violation]: + violation = right_child + # if violation indeed exists + if violation != index: + # swap to fix the violation + self.h[violation], self.h[index] = self.h[index], self.h[violation] + # fix the subsequent violation recursively if any + self.max_heapify(violation) + + def build_max_heap(self, collection: Iterable[float]) -> None: + """ build max heap from an unsorted array""" + self.h = list(collection) + self.heap_size = len(self.h) + if self.heap_size > 1: + # max_heapify from right to left but exclude leaves (last level) + for i in range(self.heap_size // 2 - 1, -1, -1): + self.max_heapify(i) + + def max(self) -> float: + """ return the max in the heap """ + if self.heap_size >= 1: + return self.h[0] + else: + raise Exception("Empty heap") + + def extract_max(self) -> float: + """ get and remove max from heap """ + if self.heap_size >= 2: + me = self.h[0] + self.h[0] = self.h.pop(-1) + self.heap_size -= 1 + self.max_heapify(0) + return me + elif self.heap_size == 1: + self.heap_size -= 1 + return self.h.pop(-1) + else: + raise Exception("Empty heap") + + def insert(self, value: float) -> None: + """ insert a new value into the max heap """ + self.h.append(value) + idx = (self.heap_size - 1) // 2 + self.heap_size += 1 + while idx >= 0: + self.max_heapify(idx) + idx = (idx - 1) // 2 + + def heap_sort(self) -> None: + size = self.heap_size + for j in range(size - 1, 0, -1): + self.h[0], self.h[j] = self.h[j], self.h[0] + self.heap_size -= 1 + self.max_heapify(0) + self.heap_size = size + + +if __name__ == "__main__": + import doctest + + # run doc test + doctest.testmod() + + # demo + for unsorted in [ + [0], + [2], + [3, 5], + [5, 3], + [5, 5], + [0, 0, 0, 0], + [1, 1, 1, 1], + [2, 2, 3, 5], + [0, 2, 2, 3, 5], + [2, 5, 3, 0, 2, 3, 0, 3], + [6, 1, 2, 7, 9, 3, 4, 5, 10, 8], + [103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5], + [-45, -2, -5], + ]: + print(f"unsorted array: {unsorted}") + + heap = Heap() + heap.build_max_heap(unsorted) + print(f"after build heap: {heap}") + + print(f"max value: {heap.extract_max()}") + print(f"after max value removed: {heap}") + heap.insert(100) + print(f"after new value 100 inserted: {heap}") + heap.heap_sort() + print(f"heap-sorted array: {heap}\n") diff --git a/data_structures/heap/heap_generic.py b/data_structures/heap/heap_generic.py new file mode 100644 index 000000000000..553cb94518c4 --- /dev/null +++ b/data_structures/heap/heap_generic.py @@ -0,0 +1,172 @@ +class Heap: + """ + A generic Heap class, can be used as min or max by passing the key function + accordingly. + """ + + def __init__(self, key=None): + # Stores actual heap items. + self.arr = list() + # Stores indexes of each item for supporting updates and deletion. + self.pos_map = {} + # Stores current size of heap. + self.size = 0 + # Stores function used to evaluate the score of an item on which basis ordering + # will be done. + self.key = key or (lambda x: x) + + def _parent(self, i): + """Returns parent index of given index if exists else None""" + return int((i - 1) / 2) if i > 0 else None + + def _left(self, i): + """Returns left-child-index of given index if exists else None""" + left = int(2 * i + 1) + return left if 0 < left < self.size else None + + def _right(self, i): + """Returns right-child-index of given index if exists else None""" + right = int(2 * i + 2) + return right if 0 < right < self.size else None + + def _swap(self, i, j): + """Performs changes required for swapping two elements in the heap""" + # First update the indexes of the items in index map. + self.pos_map[self.arr[i][0]], self.pos_map[self.arr[j][0]] = ( + self.pos_map[self.arr[j][0]], + self.pos_map[self.arr[i][0]], + ) + # Then swap the items in the list. + self.arr[i], self.arr[j] = self.arr[j], self.arr[i] + + def _cmp(self, i, j): + """Compares the two items using default comparison""" + return self.arr[i][1] < self.arr[j][1] + + def _get_valid_parent(self, i): + """ + Returns index of valid parent as per desired ordering among given index and + both it's children + """ + left = self._left(i) + right = self._right(i) + valid_parent = i + + if left is not None and not self._cmp(left, valid_parent): + valid_parent = left + if right is not None and not self._cmp(right, valid_parent): + valid_parent = right + + return valid_parent + + def _heapify_up(self, index): + """Fixes the heap in upward direction of given index""" + parent = self._parent(index) + while parent is not None and not self._cmp(index, parent): + self._swap(index, parent) + index, parent = parent, self._parent(parent) + + def _heapify_down(self, index): + """Fixes the heap in downward direction of given index""" + valid_parent = self._get_valid_parent(index) + while valid_parent != index: + self._swap(index, valid_parent) + index, valid_parent = valid_parent, self._get_valid_parent(valid_parent) + + def update_item(self, item, item_value): + """Updates given item value in heap if present""" + if item not in self.pos_map: + return + index = self.pos_map[item] + self.arr[index] = [item, self.key(item_value)] + # Make sure heap is right in both up and down direction. + # Ideally only one of them will make any change. + self._heapify_up(index) + self._heapify_down(index) + + def delete_item(self, item): + """Deletes given item from heap if present""" + if item not in self.pos_map: + return + index = self.pos_map[item] + del self.pos_map[item] + self.arr[index] = self.arr[self.size - 1] + self.pos_map[self.arr[self.size - 1][0]] = index + self.size -= 1 + # Make sure heap is right in both up and down direction. Ideally only one + # of them will make any change- so no performance loss in calling both. + if self.size > index: + self._heapify_up(index) + self._heapify_down(index) + + def insert_item(self, item, item_value): + """Inserts given item with given value in heap""" + arr_len = len(self.arr) + if arr_len == self.size: + self.arr.append([item, self.key(item_value)]) + else: + self.arr[self.size] = [item, self.key(item_value)] + self.pos_map[item] = self.size + self.size += 1 + self._heapify_up(self.size - 1) + + def get_top(self): + """Returns top item tuple (Calculated value, item) from heap if present""" + return self.arr[0] if self.size else None + + def extract_top(self): + """ + Return top item tuple (Calculated value, item) from heap and removes it as well + if present + """ + top_item_tuple = self.get_top() + if top_item_tuple: + self.delete_item(top_item_tuple[0]) + return top_item_tuple + + +def test_heap() -> None: + """ + >>> h = Heap() # Max-heap + >>> h.insert_item(5, 34) + >>> h.insert_item(6, 31) + >>> h.insert_item(7, 37) + >>> h.get_top() + [7, 37] + >>> h.extract_top() + [7, 37] + >>> h.extract_top() + [5, 34] + >>> h.extract_top() + [6, 31] + >>> h = Heap(key=lambda x: -x) # Min heap + >>> h.insert_item(5, 34) + >>> h.insert_item(6, 31) + >>> h.insert_item(7, 37) + >>> h.get_top() + [6, -31] + >>> h.extract_top() + [6, -31] + >>> h.extract_top() + [5, -34] + >>> h.extract_top() + [7, -37] + >>> h.insert_item(8, 45) + >>> h.insert_item(9, 40) + >>> h.insert_item(10, 50) + >>> h.get_top() + [9, -40] + >>> h.update_item(10, 30) + >>> h.get_top() + [10, -30] + >>> h.delete_item(10) + >>> h.get_top() + [9, -40] + """ + pass + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py new file mode 100644 index 000000000000..2a08f8fa2cd1 --- /dev/null +++ b/data_structures/heap/max_heap.py @@ -0,0 +1,87 @@ +class BinaryHeap: + """ + A max-heap implementation in Python + >>> binary_heap = BinaryHeap() + >>> binary_heap.insert(6) + >>> binary_heap.insert(10) + >>> binary_heap.insert(15) + >>> binary_heap.insert(12) + >>> binary_heap.pop() + 15 + >>> binary_heap.pop() + 12 + >>> binary_heap.get_list + [10, 6] + >>> len(binary_heap) + 2 + """ + + def __init__(self): + self.__heap = [0] + self.__size = 0 + + def __swap_up(self, i: int) -> None: + """ Swap the element up """ + temporary = self.__heap[i] + while i // 2 > 0: + if self.__heap[i] > self.__heap[i // 2]: + self.__heap[i] = self.__heap[i // 2] + self.__heap[i // 2] = temporary + i //= 2 + + def insert(self, value: int) -> None: + """ Insert new element """ + self.__heap.append(value) + self.__size += 1 + self.__swap_up(self.__size) + + def __swap_down(self, i: int) -> None: + """ Swap the element down """ + while self.__size >= 2 * i: + if 2 * i + 1 > self.__size: + bigger_child = 2 * i + else: + if self.__heap[2 * i] > self.__heap[2 * i + 1]: + bigger_child = 2 * i + else: + bigger_child = 2 * i + 1 + temporary = self.__heap[i] + if self.__heap[i] < self.__heap[bigger_child]: + self.__heap[i] = self.__heap[bigger_child] + self.__heap[bigger_child] = temporary + i = bigger_child + + def pop(self) -> int: + """ Pop the root element """ + max_value = self.__heap[1] + self.__heap[1] = self.__heap[self.__size] + self.__size -= 1 + self.__heap.pop() + self.__swap_down(1) + return max_value + + @property + def get_list(self): + return self.__heap[1:] + + def __len__(self): + """ Length of the array """ + return self.__size + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + # create an instance of BinaryHeap + binary_heap = BinaryHeap() + binary_heap.insert(6) + binary_heap.insert(10) + binary_heap.insert(15) + binary_heap.insert(12) + # pop root(max-values because it is max heap) + print(binary_heap.pop()) # 15 + print(binary_heap.pop()) # 12 + # get the list and size after operations + print(binary_heap.get_list) + print(len(binary_heap)) diff --git a/data_structures/heap/min_heap.py b/data_structures/heap/min_heap.py new file mode 100644 index 000000000000..9265c4839536 --- /dev/null +++ b/data_structures/heap/min_heap.py @@ -0,0 +1,170 @@ +# Min heap data structure +# with decrease key functionality - in O(log(n)) time + + +class Node: + def __init__(self, name, val): + self.name = name + self.val = val + + def __str__(self): + return f"{self.__class__.__name__}({self.name}, {self.val})" + + def __lt__(self, other): + return self.val < other.val + + +class MinHeap: + """ + >>> r = Node("R", -1) + >>> b = Node("B", 6) + >>> a = Node("A", 3) + >>> x = Node("X", 1) + >>> e = Node("E", 4) + >>> print(b) + Node(B, 6) + >>> myMinHeap = MinHeap([r, b, a, x, e]) + >>> myMinHeap.decrease_key(b, -17) + >>> print(b) + Node(B, -17) + >>> print(myMinHeap["B"]) + -17 + """ + + def __init__(self, array): + self.idx_of_element = {} + self.heap_dict = {} + self.heap = self.build_heap(array) + + def __getitem__(self, key): + return self.get_value(key) + + def get_parent_idx(self, idx): + return (idx - 1) // 2 + + def get_left_child_idx(self, idx): + return idx * 2 + 1 + + def get_right_child_idx(self, idx): + return idx * 2 + 2 + + def get_value(self, key): + return self.heap_dict[key] + + def build_heap(self, array): + lastIdx = len(array) - 1 + startFrom = self.get_parent_idx(lastIdx) + + for idx, i in enumerate(array): + self.idx_of_element[i] = idx + self.heap_dict[i.name] = i.val + + for i in range(startFrom, -1, -1): + self.sift_down(i, array) + return array + + # this is min-heapify method + def sift_down(self, idx, array): + while True: + l = self.get_left_child_idx(idx) # noqa: E741 + r = self.get_right_child_idx(idx) + + smallest = idx + if l < len(array) and array[l] < array[idx]: + smallest = l + if r < len(array) and array[r] < array[smallest]: + smallest = r + + if smallest != idx: + array[idx], array[smallest] = array[smallest], array[idx] + ( + self.idx_of_element[array[idx]], + self.idx_of_element[array[smallest]], + ) = ( + self.idx_of_element[array[smallest]], + self.idx_of_element[array[idx]], + ) + idx = smallest + else: + break + + def sift_up(self, idx): + p = self.get_parent_idx(idx) + while p >= 0 and self.heap[p] > self.heap[idx]: + self.heap[p], self.heap[idx] = self.heap[idx], self.heap[p] + self.idx_of_element[self.heap[p]], self.idx_of_element[self.heap[idx]] = ( + self.idx_of_element[self.heap[idx]], + self.idx_of_element[self.heap[p]], + ) + idx = p + p = self.get_parent_idx(idx) + + def peek(self): + return self.heap[0] + + def remove(self): + self.heap[0], self.heap[-1] = self.heap[-1], self.heap[0] + self.idx_of_element[self.heap[0]], self.idx_of_element[self.heap[-1]] = ( + self.idx_of_element[self.heap[-1]], + self.idx_of_element[self.heap[0]], + ) + + x = self.heap.pop() + del self.idx_of_element[x] + self.sift_down(0, self.heap) + return x + + def insert(self, node): + self.heap.append(node) + self.idx_of_element[node] = len(self.heap) - 1 + self.heap_dict[node.name] = node.val + self.sift_up(len(self.heap) - 1) + + def is_empty(self): + return True if len(self.heap) == 0 else False + + def decrease_key(self, node, newValue): + assert ( + self.heap[self.idx_of_element[node]].val > newValue + ), "newValue must be less that current value" + node.val = newValue + self.heap_dict[node.name] = newValue + self.sift_up(self.idx_of_element[node]) + + +# USAGE + +r = Node("R", -1) +b = Node("B", 6) +a = Node("A", 3) +x = Node("X", 1) +e = Node("E", 4) + +# Use one of these two ways to generate Min-Heap + +# Generating Min-Heap from array +myMinHeap = MinHeap([r, b, a, x, e]) + +# Generating Min-Heap by Insert method +# myMinHeap.insert(a) +# myMinHeap.insert(b) +# myMinHeap.insert(x) +# myMinHeap.insert(r) +# myMinHeap.insert(e) + +# Before +print("Min Heap - before decrease key") +for i in myMinHeap.heap: + print(i) + +print("Min Heap - After decrease key of node [B -> -17]") +myMinHeap.decrease_key(b, -17) + +# After +for i in myMinHeap.heap: + print(i) + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/heap/randomized_heap.py b/data_structures/heap/randomized_heap.py new file mode 100644 index 000000000000..0ddc2272efe8 --- /dev/null +++ b/data_structures/heap/randomized_heap.py @@ -0,0 +1,188 @@ +#!/usr/bin/env python3 + +from __future__ import annotations + +import random +from typing import Generic, Iterable, List, Optional, TypeVar + +T = TypeVar("T") + + +class RandomizedHeapNode(Generic[T]): + """ + One node of the randomized heap. Contains the value and references to + two children. + """ + + def __init__(self, value: T) -> None: + self._value: T = value + self.left: Optional[RandomizedHeapNode[T]] = None + self.right: Optional[RandomizedHeapNode[T]] = None + + @property + def value(self) -> T: + """Return the value of the node.""" + return self._value + + @staticmethod + def merge( + root1: Optional[RandomizedHeapNode[T]], root2: Optional[RandomizedHeapNode[T]] + ) -> Optional[RandomizedHeapNode[T]]: + """Merge 2 nodes together.""" + if not root1: + return root2 + + if not root2: + return root1 + + if root1.value > root2.value: + root1, root2 = root2, root1 + + if random.choice([True, False]): + root1.left, root1.right = root1.right, root1.left + + root1.left = RandomizedHeapNode.merge(root1.left, root2) + + return root1 + + +class RandomizedHeap(Generic[T]): + """ + A data structure that allows inserting a new value and to pop the smallest + values. Both operations take O(logN) time where N is the size of the + structure. + Wiki: https://en.wikipedia.org/wiki/Randomized_meldable_heap + + >>> RandomizedHeap([2, 3, 1, 5, 1, 7]).to_sorted_list() + [1, 1, 2, 3, 5, 7] + + >>> rh = RandomizedHeap() + >>> rh.pop() + Traceback (most recent call last): + ... + IndexError: Can't get top element for the empty heap. + + >>> rh.insert(1) + >>> rh.insert(-1) + >>> rh.insert(0) + >>> rh.to_sorted_list() + [-1, 0, 1] + """ + + def __init__(self, data: Optional[Iterable[T]] = ()) -> None: + """ + >>> rh = RandomizedHeap([3, 1, 3, 7]) + >>> rh.to_sorted_list() + [1, 3, 3, 7] + """ + self._root: Optional[RandomizedHeapNode[T]] = None + for item in data: + self.insert(item) + + def insert(self, value: T) -> None: + """ + Insert the value into the heap. + + >>> rh = RandomizedHeap() + >>> rh.insert(3) + >>> rh.insert(1) + >>> rh.insert(3) + >>> rh.insert(7) + >>> rh.to_sorted_list() + [1, 3, 3, 7] + """ + self._root = RandomizedHeapNode.merge(self._root, RandomizedHeapNode(value)) + + def pop(self) -> T: + """ + Pop the smallest value from the heap and return it. + + >>> rh = RandomizedHeap([3, 1, 3, 7]) + >>> rh.pop() + 1 + >>> rh.pop() + 3 + >>> rh.pop() + 3 + >>> rh.pop() + 7 + >>> rh.pop() + Traceback (most recent call last): + ... + IndexError: Can't get top element for the empty heap. + """ + result = self.top() + self._root = RandomizedHeapNode.merge(self._root.left, self._root.right) + + return result + + def top(self) -> T: + """ + Return the smallest value from the heap. + + >>> rh = RandomizedHeap() + >>> rh.insert(3) + >>> rh.top() + 3 + >>> rh.insert(1) + >>> rh.top() + 1 + >>> rh.insert(3) + >>> rh.top() + 1 + >>> rh.insert(7) + >>> rh.top() + 1 + """ + if not self._root: + raise IndexError("Can't get top element for the empty heap.") + return self._root.value + + def clear(self): + """ + Clear the heap. + + >>> rh = RandomizedHeap([3, 1, 3, 7]) + >>> rh.clear() + >>> rh.pop() + Traceback (most recent call last): + ... + IndexError: Can't get top element for the empty heap. + """ + self._root = None + + def to_sorted_list(self) -> List[T]: + """ + Returns sorted list containing all the values in the heap. + + >>> rh = RandomizedHeap([3, 1, 3, 7]) + >>> rh.to_sorted_list() + [1, 3, 3, 7] + """ + result = [] + while self: + result.append(self.pop()) + + return result + + def __bool__(self) -> bool: + """ + Check if the heap is not empty. + + >>> rh = RandomizedHeap() + >>> bool(rh) + False + >>> rh.insert(1) + >>> bool(rh) + True + >>> rh.clear() + >>> bool(rh) + False + """ + return self._root is not None + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/heap/skew_heap.py b/data_structures/heap/skew_heap.py new file mode 100644 index 000000000000..417a383f733e --- /dev/null +++ b/data_structures/heap/skew_heap.py @@ -0,0 +1,192 @@ +#!/usr/bin/env python3 + +from __future__ import annotations + +from typing import Generic, Iterable, Iterator, Optional, TypeVar + +T = TypeVar("T") + + +class SkewNode(Generic[T]): + """ + One node of the skew heap. Contains the value and references to + two children. + """ + + def __init__(self, value: T) -> None: + self._value: T = value + self.left: Optional[SkewNode[T]] = None + self.right: Optional[SkewNode[T]] = None + + @property + def value(self) -> T: + """Return the value of the node.""" + return self._value + + @staticmethod + def merge( + root1: Optional[SkewNode[T]], root2: Optional[SkewNode[T]] + ) -> Optional[SkewNode[T]]: + """Merge 2 nodes together.""" + if not root1: + return root2 + + if not root2: + return root1 + + if root1.value > root2.value: + root1, root2 = root2, root1 + + result = root1 + temp = root1.right + result.right = root1.left + result.left = SkewNode.merge(temp, root2) + + return result + + +class SkewHeap(Generic[T]): + """ + A data structure that allows inserting a new value and to pop the smallest + values. Both operations take O(logN) time where N is the size of the + structure. + Wiki: https://en.wikipedia.org/wiki/Skew_heap + Visualisation: https://www.cs.usfca.edu/~galles/visualization/SkewHeap.html + + >>> list(SkewHeap([2, 3, 1, 5, 1, 7])) + [1, 1, 2, 3, 5, 7] + + >>> sh = SkewHeap() + >>> sh.pop() + Traceback (most recent call last): + ... + IndexError: Can't get top element for the empty heap. + + >>> sh.insert(1) + >>> sh.insert(-1) + >>> sh.insert(0) + >>> list(sh) + [-1, 0, 1] + """ + + def __init__(self, data: Optional[Iterable[T]] = ()) -> None: + """ + >>> sh = SkewHeap([3, 1, 3, 7]) + >>> list(sh) + [1, 3, 3, 7] + """ + self._root: Optional[SkewNode[T]] = None + for item in data: + self.insert(item) + + def __bool__(self) -> bool: + """ + Check if the heap is not empty. + + >>> sh = SkewHeap() + >>> bool(sh) + False + >>> sh.insert(1) + >>> bool(sh) + True + >>> sh.clear() + >>> bool(sh) + False + """ + return self._root is not None + + def __iter__(self) -> Iterator[T]: + """ + Returns sorted list containing all the values in the heap. + + >>> sh = SkewHeap([3, 1, 3, 7]) + >>> list(sh) + [1, 3, 3, 7] + """ + result = [] + while self: + result.append(self.pop()) + + # Pushing items back to the heap not to clear it. + for item in result: + self.insert(item) + + return iter(result) + + def insert(self, value: T) -> None: + """ + Insert the value into the heap. + + >>> sh = SkewHeap() + >>> sh.insert(3) + >>> sh.insert(1) + >>> sh.insert(3) + >>> sh.insert(7) + >>> list(sh) + [1, 3, 3, 7] + """ + self._root = SkewNode.merge(self._root, SkewNode(value)) + + def pop(self) -> T: + """ + Pop the smallest value from the heap and return it. + + >>> sh = SkewHeap([3, 1, 3, 7]) + >>> sh.pop() + 1 + >>> sh.pop() + 3 + >>> sh.pop() + 3 + >>> sh.pop() + 7 + >>> sh.pop() + Traceback (most recent call last): + ... + IndexError: Can't get top element for the empty heap. + """ + result = self.top() + self._root = SkewNode.merge(self._root.left, self._root.right) + + return result + + def top(self) -> T: + """ + Return the smallest value from the heap. + + >>> sh = SkewHeap() + >>> sh.insert(3) + >>> sh.top() + 3 + >>> sh.insert(1) + >>> sh.top() + 1 + >>> sh.insert(3) + >>> sh.top() + 1 + >>> sh.insert(7) + >>> sh.top() + 1 + """ + if not self._root: + raise IndexError("Can't get top element for the empty heap.") + return self._root.value + + def clear(self): + """ + Clear the heap. + + >>> sh = SkewHeap([3, 1, 3, 7]) + >>> sh.clear() + >>> sh.pop() + Traceback (most recent call last): + ... + IndexError: Can't get top element for the empty heap. + """ + self._root = None + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/linked_list/__init__.py b/data_structures/linked_list/__init__.py index 6d50f23c1f1a..a5f5537b1d96 100644 --- a/data_structures/linked_list/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -1,22 +1,80 @@ +""" +Linked Lists consists of Nodes. +Nodes contain data and also may link to other nodes: + - Head Node: First node, the address of the + head node gives us access of the complete list + - Last node: points to null +""" + +from typing import Any + + class Node: - def __init__(self, item, next): + def __init__(self, item: Any, next: Any) -> None: self.item = item self.next = next + class LinkedList: - def __init__(self): + def __init__(self) -> None: self.head = None + self.size = 0 - def add(self, item): + def add(self, item: Any) -> None: self.head = Node(item, self.head) + self.size += 1 - def remove(self): + def remove(self) -> Any: if self.is_empty(): return None else: item = self.head.item self.head = self.head.next + self.size -= 1 return item - def is_empty(self): + def is_empty(self) -> bool: return self.head is None + + def __str__(self) -> str: + """ + >>> linked_list = LinkedList() + >>> linked_list.add(23) + >>> linked_list.add(14) + >>> linked_list.add(9) + >>> print(linked_list) + 9 --> 14 --> 23 + """ + if not self.is_empty: + return "" + else: + iterate = self.head + item_str = "" + item_list = [] + while iterate: + item_list.append(str(iterate.item)) + iterate = iterate.next + + item_str = " --> ".join(item_list) + + return item_str + + def __len__(self) -> int: + """ + >>> linked_list = LinkedList() + >>> len(linked_list) + 0 + >>> linked_list.add("a") + >>> len(linked_list) + 1 + >>> linked_list.add("b") + >>> len(linked_list) + 2 + >>> _ = linked_list.remove() + >>> len(linked_list) + 1 + >>> _ = linked_list.remove() + >>> len(linked_list) + 0 + """ + return self.size diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py new file mode 100644 index 000000000000..f67c1e8f2cf7 --- /dev/null +++ b/data_structures/linked_list/circular_linked_list.py @@ -0,0 +1,141 @@ +from typing import Any + + +class Node: + def __init__(self, data: Any): + self.data = data + self.next = None + + +class CircularLinkedList: + def __init__(self): + self.head = None + self.tail = None + + def __iter__(self): + node = self.head + while self.head: + yield node.data + node = node.next + if node == self.head: + break + + def __len__(self) -> int: + return len(tuple(iter(self))) + + def __repr__(self): + return "->".join(str(item) for item in iter(self)) + + def insert_tail(self, data: Any) -> None: + self.insert_nth(len(self), data) + + def insert_head(self, data: Any) -> None: + self.insert_nth(0, data) + + def insert_nth(self, index: int, data: Any) -> None: + if index < 0 or index > len(self): + raise IndexError("list index out of range.") + new_node = Node(data) + if self.head is None: + new_node.next = new_node # first node points itself + self.tail = self.head = new_node + elif index == 0: # insert at head + new_node.next = self.head + self.head = self.tail.next = new_node + else: + temp = self.head + for _ in range(index - 1): + temp = temp.next + new_node.next = temp.next + temp.next = new_node + if index == len(self) - 1: # insert at tail + self.tail = new_node + + def delete_front(self): + return self.delete_nth(0) + + def delete_tail(self) -> None: + return self.delete_nth(len(self) - 1) + + def delete_nth(self, index: int = 0): + if not 0 <= index < len(self): + raise IndexError("list index out of range.") + delete_node = self.head + if self.head == self.tail: # just one node + self.head = self.tail = None + elif index == 0: # delete head node + self.tail.next = self.tail.next.next + self.head = self.head.next + else: + temp = self.head + for _ in range(index - 1): + temp = temp.next + delete_node = temp.next + temp.next = temp.next.next + if index == len(self) - 1: # delete at tail + self.tail = temp + return delete_node.data + + def is_empty(self): + return len(self) == 0 + + +def test_circular_linked_list() -> None: + """ + >>> test_circular_linked_list() + """ + circular_linked_list = CircularLinkedList() + assert len(circular_linked_list) == 0 + assert circular_linked_list.is_empty() is True + assert str(circular_linked_list) == "" + + try: + circular_linked_list.delete_front() + assert False # This should not happen + except IndexError: + assert True # This should happen + + try: + circular_linked_list.delete_tail() + assert False # This should not happen + except IndexError: + assert True # This should happen + + try: + circular_linked_list.delete_nth(-1) + assert False + except IndexError: + assert True + + try: + circular_linked_list.delete_nth(0) + assert False + except IndexError: + assert True + + assert circular_linked_list.is_empty() is True + for i in range(5): + assert len(circular_linked_list) == i + circular_linked_list.insert_nth(i, i + 1) + assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) + + circular_linked_list.insert_tail(6) + assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 7)) + circular_linked_list.insert_head(0) + assert str(circular_linked_list) == "->".join(str(i) for i in range(0, 7)) + + assert circular_linked_list.delete_front() == 0 + assert circular_linked_list.delete_tail() == 6 + assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) + assert circular_linked_list.delete_nth(2) == 3 + + circular_linked_list.insert_nth(2, 3) + assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 6)) + + assert circular_linked_list.is_empty() is False + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/linked_list/deque_doubly.py b/data_structures/linked_list/deque_doubly.py new file mode 100644 index 000000000000..894f91d561cc --- /dev/null +++ b/data_structures/linked_list/deque_doubly.py @@ -0,0 +1,143 @@ +""" +Implementing Deque using DoublyLinkedList ... +Operations: + 1. insertion in the front -> O(1) + 2. insertion in the end -> O(1) + 3. remove from the front -> O(1) + 4. remove from the end -> O(1) +""" + + +class _DoublyLinkedBase: + """ A Private class (to be inherited) """ + + class _Node: + __slots__ = "_prev", "_data", "_next" + + def __init__(self, link_p, element, link_n): + self._prev = link_p + self._data = element + self._next = link_n + + def has_next_and_prev(self): + return " Prev -> {}, Next -> {}".format( + self._prev is not None, self._next is not None + ) + + def __init__(self): + self._header = self._Node(None, None, None) + self._trailer = self._Node(None, None, None) + self._header._next = self._trailer + self._trailer._prev = self._header + self._size = 0 + + def __len__(self): + return self._size + + def is_empty(self): + return self.__len__() == 0 + + def _insert(self, predecessor, e, successor): + # Create new_node by setting it's prev.link -> header + # setting it's next.link -> trailer + new_node = self._Node(predecessor, e, successor) + predecessor._next = new_node + successor._prev = new_node + self._size += 1 + return self + + def _delete(self, node): + predecessor = node._prev + successor = node._next + + predecessor._next = successor + successor._prev = predecessor + self._size -= 1 + temp = node._data + node._prev = node._next = node._data = None + del node + return temp + + +class LinkedDeque(_DoublyLinkedBase): + def first(self): + """return first element + >>> d = LinkedDeque() + >>> d.add_first('A').first() + 'A' + >>> d.add_first('B').first() + 'B' + """ + if self.is_empty(): + raise Exception("List is empty") + return self._header._next._data + + def last(self): + """return last element + >>> d = LinkedDeque() + >>> d.add_last('A').last() + 'A' + >>> d.add_last('B').last() + 'B' + """ + if self.is_empty(): + raise Exception("List is empty") + return self._trailer._prev._data + + # DEque Insert Operations (At the front, At the end) + + def add_first(self, element): + """insertion in the front + >>> LinkedDeque().add_first('AV').first() + 'AV' + """ + return self._insert(self._header, element, self._header._next) + + def add_last(self, element): + """insertion in the end + >>> LinkedDeque().add_last('B').last() + 'B' + """ + return self._insert(self._trailer._prev, element, self._trailer) + + # DEqueu Remove Operations (At the front, At the end) + + def remove_first(self): + """removal from the front + >>> d = LinkedDeque() + >>> d.is_empty() + True + >>> d.remove_first() + Traceback (most recent call last): + ... + IndexError: remove_first from empty list + >>> d.add_first('A') # doctest: +ELLIPSIS + >> d.remove_first() + 'A' + >>> d.is_empty() + True + """ + if self.is_empty(): + raise IndexError("remove_first from empty list") + return self._delete(self._header._next) + + def remove_last(self): + """removal in the end + >>> d = LinkedDeque() + >>> d.is_empty() + True + >>> d.remove_last() + Traceback (most recent call last): + ... + IndexError: remove_first from empty list + >>> d.add_first('A') # doctest: +ELLIPSIS + >> d.remove_last() + 'A' + >>> d.is_empty() + True + """ + if self.is_empty(): + raise IndexError("remove_first from empty list") + return self._delete(self._trailer._prev) diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 75b1f889dfc2..0eb3cf101a3e 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -1,77 +1,226 @@ -''' -- A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes. -- This is an example of a double ended, doubly linked list. -- Each link references the next link and the previous one. -- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list. - - Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent''' -from __future__ import print_function +""" +https://en.wikipedia.org/wiki/Doubly_linked_list +""" -class LinkedList: #making main class named linked list +class Node: + def __init__(self, data): + self.data = data + self.previous = None + self.next = None + + def __str__(self): + return f"{self.data}" + + +class DoublyLinkedList: def __init__(self): self.head = None self.tail = None - - def insertHead(self, x): - newLink = Link(x) #Create a new link with a value attached to it - if(self.isEmpty() == True): #Set the first element added to be the tail - self.tail = newLink + + def __iter__(self): + """ + >>> linked_list = DoublyLinkedList() + >>> linked_list.insert_at_head('b') + >>> linked_list.insert_at_head('a') + >>> linked_list.insert_at_tail('c') + >>> tuple(linked_list) + ('a', 'b', 'c') + """ + node = self.head + while node: + yield node.data + node = node.next + + def __str__(self): + """ + >>> linked_list = DoublyLinkedList() + >>> linked_list.insert_at_tail('a') + >>> linked_list.insert_at_tail('b') + >>> linked_list.insert_at_tail('c') + >>> str(linked_list) + 'a->b->c' + """ + return "->".join([str(item) for item in self]) + + def __len__(self): + """ + >>> linked_list = DoublyLinkedList() + >>> for i in range(0, 5): + ... linked_list.insert_at_nth(i, i + 1) + >>> len(linked_list) == 5 + True + """ + return len(tuple(iter(self))) + + def insert_at_head(self, data): + self.insert_at_nth(0, data) + + def insert_at_tail(self, data): + self.insert_at_nth(len(self), data) + + def insert_at_nth(self, index: int, data): + """ + >>> linked_list = DoublyLinkedList() + >>> linked_list.insert_at_nth(-1, 666) + Traceback (most recent call last): + .... + IndexError: list index out of range + >>> linked_list.insert_at_nth(1, 666) + Traceback (most recent call last): + .... + IndexError: list index out of range + >>> linked_list.insert_at_nth(0, 2) + >>> linked_list.insert_at_nth(0, 1) + >>> linked_list.insert_at_nth(2, 4) + >>> linked_list.insert_at_nth(2, 3) + >>> str(linked_list) + '1->2->3->4' + >>> linked_list.insert_at_nth(5, 5) + Traceback (most recent call last): + .... + IndexError: list index out of range + """ + if not 0 <= index <= len(self): + raise IndexError("list index out of range") + new_node = Node(data) + if self.head is None: + self.head = self.tail = new_node + elif index == 0: + self.head.previous = new_node + new_node.next = self.head + self.head = new_node + elif index == len(self): + self.tail.next = new_node + new_node.previous = self.tail + self.tail = new_node else: - self.head.previous = newLink # newLink <-- currenthead(head) - newLink.next = self.head # newLink <--> currenthead(head) - self.head = newLink # newLink(head) <--> oldhead - - def deleteHead(self): - temp = self.head - self.head = self.head.next # oldHead <--> 2ndElement(head) - self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed - if(self.head is None): - self.tail = None #if empty linked list - return temp - - def insertTail(self, x): - newLink = Link(x) - newLink.next = None # currentTail(tail) newLink --> - self.tail.next = newLink # currentTail(tail) --> newLink --> - newLink.previous = self.tail #currentTail(tail) <--> newLink --> - self.tail = newLink # oldTail <--> newLink(tail) --> - - def deleteTail(self): - temp = self.tail - self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None - self.tail.next = None # 2ndlast(tail) --> None - return temp - - def delete(self, x): - current = self.head - - while(current.value != x): # Find the position to delete - current = current.next - - if(current == self.head): - self.deleteHead() - - elif(current == self.tail): - self.deleteTail() - - else: #Before: 1 <--> 2(current) <--> 3 - current.previous.next = current.next # 1 --> 3 - current.next.previous = current.previous # 1 <--> 3 - - def isEmpty(self): #Will return True if the list is empty - return(self.head is None) - - def display(self): #Prints contents of the list + temp = self.head + for i in range(0, index): + temp = temp.next + temp.previous.next = new_node + new_node.previous = temp.previous + new_node.next = temp + temp.previous = new_node + + def delete_head(self): + return self.delete_at_nth(0) + + def delete_tail(self): + return self.delete_at_nth(len(self) - 1) + + def delete_at_nth(self, index: int): + """ + >>> linked_list = DoublyLinkedList() + >>> linked_list.delete_at_nth(0) + Traceback (most recent call last): + .... + IndexError: list index out of range + >>> for i in range(0, 5): + ... linked_list.insert_at_nth(i, i + 1) + >>> linked_list.delete_at_nth(0) == 1 + True + >>> linked_list.delete_at_nth(3) == 5 + True + >>> linked_list.delete_at_nth(1) == 3 + True + >>> str(linked_list) + '2->4' + >>> linked_list.delete_at_nth(2) + Traceback (most recent call last): + .... + IndexError: list index out of range + """ + if not 0 <= index <= len(self) - 1: + raise IndexError("list index out of range") + delete_node = self.head # default first node + if len(self) == 1: + self.head = self.tail = None + elif index == 0: + self.head = self.head.next + self.head.previous = None + elif index == len(self) - 1: + delete_node = self.tail + self.tail = self.tail.previous + self.tail.next = None + else: + temp = self.head + for i in range(0, index): + temp = temp.next + delete_node = temp + temp.next.previous = temp.previous + temp.previous.next = temp.next + return delete_node.data + + def delete(self, data) -> str: current = self.head - while(current != None): - current.displayLink() - current = current.next - print() - -class Link: - next = None #This points to the link in front of the new link - previous = None #This points to the link behind the new link - def __init__(self, x): - self.value = x - def displayLink(self): - print("{}".format(self.value), end=" ") + + while current.data != data: # Find the position to delete + if current.next: + current = current.next + else: # We have reached the end an no value matches + return "No data matching given value" + + if current == self.head: + self.delete_head() + + elif current == self.tail: + self.delete_tail() + + else: # Before: 1 <--> 2(current) <--> 3 + current.previous.next = current.next # 1 --> 3 + current.next.previous = current.previous # 1 <--> 3 + return data + + def is_empty(self): + """ + >>> linked_list = DoublyLinkedList() + >>> linked_list.is_empty() + True + >>> linked_list.insert_at_tail(1) + >>> linked_list.is_empty() + False + """ + return len(self) == 0 + + +def test_doubly_linked_list() -> None: + """ + >>> test_doubly_linked_list() + """ + linked_list = DoublyLinkedList() + assert linked_list.is_empty() is True + assert str(linked_list) == "" + + try: + linked_list.delete_head() + assert False # This should not happen. + except IndexError: + assert True # This should happen. + + try: + linked_list.delete_tail() + assert False # This should not happen. + except IndexError: + assert True # This should happen. + + for i in range(10): + assert len(linked_list) == i + linked_list.insert_at_nth(i, i + 1) + assert str(linked_list) == "->".join(str(i) for i in range(1, 11)) + + linked_list.insert_at_head(0) + linked_list.insert_at_tail(11) + assert str(linked_list) == "->".join(str(i) for i in range(0, 12)) + + assert linked_list.delete_head() == 0 + assert linked_list.delete_at_nth(9) == 10 + assert linked_list.delete_tail() == 11 + assert len(linked_list) == 9 + assert str(linked_list) == "->".join(str(i) for i in range(1, 10)) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py new file mode 100644 index 000000000000..184b6966b5a9 --- /dev/null +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -0,0 +1,253 @@ +""" +- A linked list is similar to an array, it holds values. However, links in a linked + list do not have indexes. +- This is an example of a double ended, doubly linked list. +- Each link references the next link and the previous one. +- A Doubly Linked List (DLL) contains an extra pointer, typically called previous + pointer, together with next pointer and data which are there in singly linked list. + - Advantages over SLL - It can be traversed in both forward and backward direction. + Delete operation is more efficient +""" + + +class Node: + def __init__(self, data: int, previous=None, next_node=None): + self.data = data + self.previous = previous + self.next = next_node + + def __str__(self) -> str: + return f"{self.data}" + + def get_data(self) -> int: + return self.data + + def get_next(self): + return self.next + + def get_previous(self): + return self.previous + + +class LinkedListIterator: + def __init__(self, head): + self.current = head + + def __iter__(self): + return self + + def __next__(self): + if not self.current: + raise StopIteration + else: + value = self.current.get_data() + self.current = self.current.get_next() + return value + + +class LinkedList: + def __init__(self): + self.head = None # First node in list + self.tail = None # Last node in list + + def __str__(self): + current = self.head + nodes = [] + while current is not None: + nodes.append(current.get_data()) + current = current.get_next() + return " ".join(str(node) for node in nodes) + + def __contains__(self, value: int): + current = self.head + while current: + if current.get_data() == value: + return True + current = current.get_next() + return False + + def __iter__(self): + return LinkedListIterator(self.head) + + def get_head_data(self): + if self.head: + return self.head.get_data() + return None + + def get_tail_data(self): + if self.tail: + return self.tail.get_data() + return None + + def set_head(self, node: Node) -> None: + + if self.head is None: + self.head = node + self.tail = node + else: + self.insert_before_node(self.head, node) + + def set_tail(self, node: Node) -> None: + if self.head is None: + self.set_head(node) + else: + self.insert_after_node(self.tail, node) + + def insert(self, value: int) -> None: + node = Node(value) + if self.head is None: + self.set_head(node) + else: + self.set_tail(node) + + def insert_before_node(self, node: Node, node_to_insert: Node) -> None: + node_to_insert.next = node + node_to_insert.previous = node.previous + + if node.get_previous() is None: + self.head = node_to_insert + else: + node.previous.next = node_to_insert + + node.previous = node_to_insert + + def insert_after_node(self, node: Node, node_to_insert: Node) -> None: + node_to_insert.previous = node + node_to_insert.next = node.next + + if node.get_next() is None: + self.tail = node_to_insert + else: + node.next.previous = node_to_insert + + node.next = node_to_insert + + def insert_at_position(self, position: int, value: int) -> None: + current_position = 1 + new_node = Node(value) + node = self.head + while node: + if current_position == position: + self.insert_before_node(node, new_node) + return None + current_position += 1 + node = node.next + self.insert_after_node(self.tail, new_node) + + def get_node(self, item: int) -> Node: + node = self.head + while node: + if node.get_data() == item: + return node + node = node.get_next() + raise Exception("Node not found") + + def delete_value(self, value): + node = self.get_node(value) + + if node is not None: + if node == self.head: + self.head = self.head.get_next() + + if node == self.tail: + self.tail = self.tail.get_previous() + + self.remove_node_pointers(node) + + @staticmethod + def remove_node_pointers(node: Node) -> None: + if node.get_next(): + node.next.previous = node.previous + + if node.get_previous(): + node.previous.next = node.next + + node.next = None + node.previous = None + + def is_empty(self): + return self.head is None + + +def create_linked_list() -> None: + """ + >>> new_linked_list = LinkedList() + >>> new_linked_list.get_head_data() is None + True + >>> new_linked_list.get_tail_data() is None + True + >>> new_linked_list.is_empty() + True + >>> new_linked_list.insert(10) + >>> new_linked_list.get_head_data() + 10 + >>> new_linked_list.get_tail_data() + 10 + >>> new_linked_list.insert_at_position(position=3, value=20) + >>> new_linked_list.get_head_data() + 10 + >>> new_linked_list.get_tail_data() + 20 + >>> new_linked_list.set_head(Node(1000)) + >>> new_linked_list.get_head_data() + 1000 + >>> new_linked_list.get_tail_data() + 20 + >>> new_linked_list.set_tail(Node(2000)) + >>> new_linked_list.get_head_data() + 1000 + >>> new_linked_list.get_tail_data() + 2000 + >>> for value in new_linked_list: + ... print(value) + 1000 + 10 + 20 + 2000 + >>> new_linked_list.is_empty() + False + >>> for value in new_linked_list: + ... print(value) + 1000 + 10 + 20 + 2000 + >>> 10 in new_linked_list + True + >>> new_linked_list.delete_value(value=10) + >>> 10 in new_linked_list + False + >>> new_linked_list.delete_value(value=2000) + >>> new_linked_list.get_tail_data() + 20 + >>> new_linked_list.delete_value(value=1000) + >>> new_linked_list.get_tail_data() + 20 + >>> new_linked_list.get_head_data() + 20 + >>> for value in new_linked_list: + ... print(value) + 20 + >>> new_linked_list.delete_value(value=20) + >>> for value in new_linked_list: + ... print(value) + >>> for value in range(1,10): + ... new_linked_list.insert(value=value) + >>> for value in new_linked_list: + ... print(value) + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + """ + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/linked_list/from_sequence.py b/data_structures/linked_list/from_sequence.py new file mode 100644 index 000000000000..94b44f15037f --- /dev/null +++ b/data_structures/linked_list/from_sequence.py @@ -0,0 +1,44 @@ +# Recursive Prorgam to create a Linked List from a sequence and +# print a string representation of it. + + +class Node: + def __init__(self, data=None): + self.data = data + self.next = None + + def __repr__(self): + """Returns a visual representation of the node and all its following nodes.""" + string_rep = "" + temp = self + while temp: + string_rep += f"<{temp.data}> ---> " + temp = temp.next + string_rep += "" + return string_rep + + +def make_linked_list(elements_list): + """Creates a Linked List from the elements of the given sequence + (list/tuple) and returns the head of the Linked List.""" + + # if elements_list is empty + if not elements_list: + raise Exception("The Elements List is empty") + + # Set first element as Head + head = Node(elements_list[0]) + current = head + # Loop through elements from position 1 + for data in elements_list[1:]: + current.next = Node(data) + current = current.next + return head + + +list_data = [1, 3, 5, 32, 44, 12, 43] +print(f"List: {list_data}") +print("Creating Linked List from List.") +linked_list = make_linked_list(list_data) +print("Linked List:") +print(linked_list) diff --git a/data_structures/linked_list/has_loop.py b/data_structures/linked_list/has_loop.py new file mode 100644 index 000000000000..405ece7e27c8 --- /dev/null +++ b/data_structures/linked_list/has_loop.py @@ -0,0 +1,60 @@ +from typing import Any + + +class ContainsLoopError(Exception): + pass + + +class Node: + def __init__(self, data: Any) -> None: + self.data = data + self.next_node = None + + def __iter__(self): + node = self + visited = [] + while node: + if node in visited: + raise ContainsLoopError + visited.append(node) + yield node.data + node = node.next_node + + @property + def has_loop(self) -> bool: + """ + A loop is when the exact same Node appears more than once in a linked list. + >>> root_node = Node(1) + >>> root_node.next_node = Node(2) + >>> root_node.next_node.next_node = Node(3) + >>> root_node.next_node.next_node.next_node = Node(4) + >>> root_node.has_loop + False + >>> root_node.next_node.next_node.next_node = root_node.next_node + >>> root_node.has_loop + True + """ + try: + list(self) + return False + except ContainsLoopError: + return True + + +if __name__ == "__main__": + root_node = Node(1) + root_node.next_node = Node(2) + root_node.next_node.next_node = Node(3) + root_node.next_node.next_node.next_node = Node(4) + print(root_node.has_loop) # False + root_node.next_node.next_node.next_node = root_node.next_node + print(root_node.has_loop) # True + + root_node = Node(5) + root_node.next_node = Node(6) + root_node.next_node.next_node = Node(5) + root_node.next_node.next_node.next_node = Node(6) + print(root_node.has_loop) # False + + root_node = Node(1) + print(root_node.has_loop) # False diff --git a/data_structures/linked_list/is_Palindrome.py b/data_structures/linked_list/is_palindrome.py similarity index 100% rename from data_structures/linked_list/is_Palindrome.py rename to data_structures/linked_list/is_palindrome.py diff --git a/data_structures/linked_list/merge_two_lists.py b/data_structures/linked_list/merge_two_lists.py new file mode 100644 index 000000000000..96ec6b8abc85 --- /dev/null +++ b/data_structures/linked_list/merge_two_lists.py @@ -0,0 +1,83 @@ +""" +Algorithm that merges two sorted linked lists into one sorted linked list. +""" +from __future__ import annotations + +from collections.abc import Iterable, Iterator +from dataclasses import dataclass +from typing import Optional + +test_data_odd = (3, 9, -11, 0, 7, 5, 1, -1) +test_data_even = (4, 6, 2, 0, 8, 10, 3, -2) + + +@dataclass +class Node: + data: int + next: Optional[Node] + + +class SortedLinkedList: + def __init__(self, ints: Iterable[int]) -> None: + self.head: Optional[Node] = None + for i in reversed(sorted(ints)): + self.head = Node(i, self.head) + + def __iter__(self) -> Iterator[int]: + """ + >>> tuple(SortedLinkedList(test_data_odd)) == tuple(sorted(test_data_odd)) + True + >>> tuple(SortedLinkedList(test_data_even)) == tuple(sorted(test_data_even)) + True + """ + node = self.head + while node: + yield node.data + node = node.next + + def __len__(self) -> int: + """ + >>> for i in range(3): + ... len(SortedLinkedList(range(i))) == i + True + True + True + >>> len(SortedLinkedList(test_data_odd)) + 8 + """ + return len(tuple(iter(self))) + + def __str__(self) -> str: + """ + >>> str(SortedLinkedList([])) + '' + >>> str(SortedLinkedList(test_data_odd)) + '-11 -> -1 -> 0 -> 1 -> 3 -> 5 -> 7 -> 9' + >>> str(SortedLinkedList(test_data_even)) + '-2 -> 0 -> 2 -> 3 -> 4 -> 6 -> 8 -> 10' + """ + return " -> ".join([str(node) for node in self]) + + +def merge_lists( + sll_one: SortedLinkedList, sll_two: SortedLinkedList +) -> SortedLinkedList: + """ + >>> SSL = SortedLinkedList + >>> merged = merge_lists(SSL(test_data_odd), SSL(test_data_even)) + >>> len(merged) + 16 + >>> str(merged) + '-11 -> -2 -> -1 -> 0 -> 0 -> 1 -> 2 -> 3 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10' + >>> list(merged) == list(sorted(test_data_odd + test_data_even)) + True + """ + return SortedLinkedList(list(sll_one) + list(sll_two)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + SSL = SortedLinkedList + print(merge_lists(SSL(test_data_odd), SSL(test_data_even))) diff --git a/data_structures/linked_list/middle_element_of_linked_list.py b/data_structures/linked_list/middle_element_of_linked_list.py new file mode 100644 index 000000000000..185c4ccbbb0a --- /dev/null +++ b/data_structures/linked_list/middle_element_of_linked_list.py @@ -0,0 +1,64 @@ +class Node: + def __init__(self, data: int) -> int: + self.data = data + self.next = None + + +class LinkedList: + def __init__(self): + self.head = None + + def push(self, new_data: int) -> int: + new_node = Node(new_data) + new_node.next = self.head + self.head = new_node + return self.head.data + + def middle_element(self) -> int: + """ + >>> link = LinkedList() + >>> link.middle_element() + No element found. + >>> link.push(5) + 5 + >>> link.push(6) + 6 + >>> link.push(8) + 8 + >>> link.push(8) + 8 + >>> link.push(10) + 10 + >>> link.push(12) + 12 + >>> link.push(17) + 17 + >>> link.push(7) + 7 + >>> link.push(3) + 3 + >>> link.push(20) + 20 + >>> link.push(-20) + -20 + >>> link.middle_element() + 12 + >>> + """ + slow_pointer = self.head + fast_pointer = self.head + if self.head: + while fast_pointer and fast_pointer.next: + fast_pointer = fast_pointer.next.next + slow_pointer = slow_pointer.next + return slow_pointer.data + else: + print("No element found.") + + +if __name__ == "__main__": + link = LinkedList() + for i in range(int(input().strip())): + data = int(input().strip()) + link.push(data) + print(link.middle_element()) diff --git a/data_structures/linked_list/print_reverse.py b/data_structures/linked_list/print_reverse.py new file mode 100644 index 000000000000..c46f228e7260 --- /dev/null +++ b/data_structures/linked_list/print_reverse.py @@ -0,0 +1,70 @@ +from typing import List + + +class Node: + def __init__(self, data=None): + self.data = data + self.next = None + + def __repr__(self): + """Returns a visual representation of the node and all its following nodes.""" + string_rep = [] + temp = self + while temp: + string_rep.append(f"{temp.data}") + temp = temp.next + return "->".join(string_rep) + + +def make_linked_list(elements_list: List): + """Creates a Linked List from the elements of the given sequence + (list/tuple) and returns the head of the Linked List. + >>> make_linked_list([]) + Traceback (most recent call last): + ... + Exception: The Elements List is empty + >>> make_linked_list([7]) + 7 + >>> make_linked_list(['abc']) + abc + >>> make_linked_list([7, 25]) + 7->25 + """ + if not elements_list: + raise Exception("The Elements List is empty") + + current = head = Node(elements_list[0]) + for i in range(1, len(elements_list)): + current.next = Node(elements_list[i]) + current = current.next + return head + + +def print_reverse(head_node: Node) -> None: + """Prints the elements of the given Linked List in reverse order + >>> print_reverse([]) + >>> linked_list = make_linked_list([69, 88, 73]) + >>> print_reverse(linked_list) + 73 + 88 + 69 + """ + if head_node is not None and isinstance(head_node, Node): + print_reverse(head_node.next) + print(head_node.data) + + +def main(): + from doctest import testmod + + testmod() + + linked_list = make_linked_list([14, 52, 14, 12, 43]) + print("Linked List:") + print(linked_list) + print("Elements in Reverse:") + print_reverse(linked_list) + + +if __name__ == "__main__": + main() diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 5ae97523b9a1..e45a210a1785 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -1,60 +1,151 @@ -from __future__ import print_function - - -class Node: # create a Node +class Node: def __init__(self, data): - self.data = data # given data - self.next = None # given next to None + self.data = data + self.next = None + def __repr__(self): + return f"Node({self.data})" -class Linked_List: + +class LinkedList: def __init__(self): - self.Head = None # Initialize Head to None - - def insert_tail(self, data): - if(self.Head is None): self.insert_head(data) #If this is first node, call insert_head + self.head = None + + def __iter__(self): + node = self.head + while node: + yield node.data + node = node.next + + def __len__(self) -> int: + """ + Return length of linked list i.e. number of nodes + >>> linked_list = LinkedList() + >>> len(linked_list) + 0 + >>> linked_list.insert_tail("head") + >>> len(linked_list) + 1 + >>> linked_list.insert_head("head") + >>> len(linked_list) + 2 + >>> _ = linked_list.delete_tail() + >>> len(linked_list) + 1 + >>> _ = linked_list.delete_head() + >>> len(linked_list) + 0 + """ + return len(tuple(iter(self))) + + def __repr__(self): + """ + String representation/visualization of a Linked Lists + """ + return "->".join([str(item) for item in self]) + + def __getitem__(self, index): + """ + Indexing Support. Used to get a node at particular position + >>> linked_list = LinkedList() + >>> for i in range(0, 10): + ... linked_list.insert_nth(i, i) + >>> all(str(linked_list[i]) == str(i) for i in range(0, 10)) + True + >>> linked_list[-10] + Traceback (most recent call last): + ... + ValueError: list index out of range. + >>> linked_list[len(linked_list)] + Traceback (most recent call last): + ... + ValueError: list index out of range. + """ + if not 0 <= index < len(self): + raise ValueError("list index out of range.") + for i, node in enumerate(self): + if i == index: + return node + + # Used to change the data of a particular node + def __setitem__(self, index, data): + """ + >>> linked_list = LinkedList() + >>> for i in range(0, 10): + ... linked_list.insert_nth(i, i) + >>> linked_list[0] = 666 + >>> linked_list[0] + 666 + >>> linked_list[5] = -666 + >>> linked_list[5] + -666 + >>> linked_list[-10] = 666 + Traceback (most recent call last): + ... + ValueError: list index out of range. + >>> linked_list[len(linked_list)] = 666 + Traceback (most recent call last): + ... + ValueError: list index out of range. + """ + if not 0 <= index < len(self): + raise ValueError("list index out of range.") + current = self.head + for i in range(index): + current = current.next + current.data = data + + def insert_tail(self, data) -> None: + self.insert_nth(len(self), data) + + def insert_head(self, data) -> None: + self.insert_nth(0, data) + + def insert_nth(self, index: int, data) -> None: + if not 0 <= index <= len(self): + raise IndexError("list index out of range") + new_node = Node(data) + if self.head is None: + self.head = new_node + elif index == 0: + new_node.next = self.head # link new_node to head + self.head = new_node else: - temp = self.Head - while(temp.next != None): #traverse to last node + temp = self.head + for _ in range(index - 1): temp = temp.next - temp.next = Node(data) #create node & link to tail - - def insert_head(self, data): - newNod = Node(data) # create a new node - if self.Head != None: - newNod.next = self.Head # link newNode to head - self.Head = newNod # make NewNode as Head - - def printList(self): # print every node data - tamp = self.Head - while tamp is not None: - print(tamp.data) - tamp = tamp.next - - def delete_head(self): # delete from head - temp = self.Head - if self.Head != None: - self.Head = self.Head.next - temp.next = None - return temp - + new_node.next = temp.next + temp.next = new_node + + def print_list(self) -> None: # print every node data + print(self) + + def delete_head(self): + return self.delete_nth(0) + def delete_tail(self): # delete from tail - tamp = self.Head - if self.Head != None: - if(self.Head.next is None): # if Head is the only Node in the Linked List - self.Head = None - else: - while tamp.next.next is not None: # find the 2nd last element - tamp = tamp.next - tamp.next, tamp = None, tamp.next #(2nd last element).next = None and tamp = last element - return tamp - - def isEmpty(self): - return self.Head is None # Return if Head is none + return self.delete_nth(len(self) - 1) + + def delete_nth(self, index: int = 0): + if not 0 <= index <= len(self) - 1: # test if index is valid + raise IndexError("list index out of range") + delete_node = self.head # default first node + if index == 0: + self.head = self.head.next + else: + temp = self.head + for _ in range(index - 1): + temp = temp.next + delete_node = temp.next + temp.next = temp.next.next + return delete_node.data + + def is_empty(self) -> bool: + return self.head is None def reverse(self): prev = None - current = self.Head + current = self.head while current: # Store the current node's next node. @@ -66,36 +157,84 @@ def reverse(self): # Make the current node the next node (to progress iteration) current = next_node # Return prev in order to put the head at the end - self.Head = prev + self.head = prev + + +def test_singly_linked_list() -> None: + """ + >>> test_singly_linked_list() + """ + linked_list = LinkedList() + assert linked_list.is_empty() is True + assert str(linked_list) == "" + + try: + linked_list.delete_head() + assert False # This should not happen. + except IndexError: + assert True # This should happen. + + try: + linked_list.delete_tail() + assert False # This should not happen. + except IndexError: + assert True # This should happen. + + for i in range(10): + assert len(linked_list) == i + linked_list.insert_nth(i, i + 1) + assert str(linked_list) == "->".join(str(i) for i in range(1, 11)) + + linked_list.insert_head(0) + linked_list.insert_tail(11) + assert str(linked_list) == "->".join(str(i) for i in range(0, 12)) + + assert linked_list.delete_head() == 0 + assert linked_list.delete_nth(9) == 10 + assert linked_list.delete_tail() == 11 + assert len(linked_list) == 9 + assert str(linked_list) == "->".join(str(i) for i in range(1, 10)) + + assert all(linked_list[i] == i + 1 for i in range(0, 9)) is True + + for i in range(0, 9): + linked_list[i] = -i + assert all(linked_list[i] == -i for i in range(0, 9)) is True + def main(): - A = Linked_List() - print("Inserting 1st at Head") - a1=input() - A.insert_head(a1) - print("Inserting 2nd at Head") - a2=input() - A.insert_head(a2) - print("\nPrint List : ") - A.printList() - print("\nInserting 1st at Tail") - a3=input() - A.insert_tail(a3) - print("Inserting 2nd at Tail") - a4=input() - A.insert_tail(a4) - print("\nPrint List : ") - A.printList() - print("\nDelete Head") - A.delete_head() - print("Delete Tail") - A.delete_tail() - print("\nPrint List : ") - A.printList() - print("\nReverse Linked List") - A.reverse() - print("\nPrint List : ") - A.printList() - -if __name__ == '__main__': - main() + from doctest import testmod + + testmod() + + linked_list = LinkedList() + linked_list.insert_head(input("Inserting 1st at head ").strip()) + linked_list.insert_head(input("Inserting 2nd at head ").strip()) + print("\nPrint list:") + linked_list.print_list() + linked_list.insert_tail(input("\nInserting 1st at tail ").strip()) + linked_list.insert_tail(input("Inserting 2nd at tail ").strip()) + print("\nPrint list:") + linked_list.print_list() + print("\nDelete head") + linked_list.delete_head() + print("Delete tail") + linked_list.delete_tail() + print("\nPrint list:") + linked_list.print_list() + print("\nReverse linked list") + linked_list.reverse() + print("\nPrint list:") + linked_list.print_list() + print("\nString representation of linked list:") + print(linked_list) + print("\nReading/changing Node data using indexing:") + print(f"Element at Position 1: {linked_list[1]}") + linked_list[1] = input("Enter New Value: ").strip() + print("New list:") + print(linked_list) + print(f"length of linked_list is : {len(linked_list)}") + + +if __name__ == "__main__": + main() diff --git a/data_structures/linked_list/skip_list.py b/data_structures/linked_list/skip_list.py new file mode 100644 index 000000000000..8f06e6193d52 --- /dev/null +++ b/data_structures/linked_list/skip_list.py @@ -0,0 +1,447 @@ +""" +Based on "Skip Lists: A Probabilistic Alternative to Balanced Trees" by William Pugh +https://epaperpress.com/sortsearch/download/skiplist.pdf +""" + +from __future__ import annotations + +from random import random +from typing import Generic, Optional, TypeVar + +KT = TypeVar("KT") +VT = TypeVar("VT") + + +class Node(Generic[KT, VT]): + def __init__(self, key: KT, value: VT): + self.key = key + self.value = value + self.forward: list[Node[KT, VT]] = [] + + def __repr__(self) -> str: + """ + :return: Visual representation of Node + + >>> node = Node("Key", 2) + >>> repr(node) + 'Node(Key: 2)' + """ + + return f"Node({self.key}: {self.value})" + + @property + def level(self) -> int: + """ + :return: Number of forward references + + >>> node = Node("Key", 2) + >>> node.level + 0 + >>> node.forward.append(Node("Key2", 4)) + >>> node.level + 1 + >>> node.forward.append(Node("Key3", 6)) + >>> node.level + 2 + """ + + return len(self.forward) + + +class SkipList(Generic[KT, VT]): + def __init__(self, p: float = 0.5, max_level: int = 16): + self.head = Node("root", None) + self.level = 0 + self.p = p + self.max_level = max_level + + def __str__(self) -> str: + """ + :return: Visual representation of SkipList + + >>> skip_list = SkipList() + >>> print(skip_list) + SkipList(level=0) + >>> skip_list.insert("Key1", "Value") + >>> print(skip_list) # doctest: +ELLIPSIS + SkipList(level=... + [root]--... + [Key1]--Key1... + None *... + >>> skip_list.insert("Key2", "OtherValue") + >>> print(skip_list) # doctest: +ELLIPSIS + SkipList(level=... + [root]--... + [Key1]--Key1... + [Key2]--Key2... + None *... + """ + + items = list(self) + + if len(items) == 0: + return f"SkipList(level={self.level})" + + label_size = max((len(str(item)) for item in items), default=4) + label_size = max(label_size, 4) + 4 + + node = self.head + lines = [] + + forwards = node.forward.copy() + lines.append(f"[{node.key}]".ljust(label_size, "-") + "* " * len(forwards)) + lines.append(" " * label_size + "| " * len(forwards)) + + while len(node.forward) != 0: + node = node.forward[0] + + lines.append( + f"[{node.key}]".ljust(label_size, "-") + + " ".join(str(n.key) if n.key == node.key else "|" for n in forwards) + ) + lines.append(" " * label_size + "| " * len(forwards)) + forwards[: node.level] = node.forward + + lines.append("None".ljust(label_size) + "* " * len(forwards)) + return f"SkipList(level={self.level})\n" + "\n".join(lines) + + def __iter__(self): + node = self.head + + while len(node.forward) != 0: + yield node.forward[0].key + node = node.forward[0] + + def random_level(self) -> int: + """ + :return: Random level from [1, self.max_level] interval. + Higher values are less likely. + """ + + level = 1 + while random() < self.p and level < self.max_level: + level += 1 + + return level + + def _locate_node(self, key) -> tuple[Optional[Node[KT, VT]], list[Node[KT, VT]]]: + """ + :param key: Searched key, + :return: Tuple with searched node (or None if given key is not present) + and list of nodes that refer (if key is present) of should refer to + given node. + """ + + # Nodes with refer or should refer to output node + update_vector = [] + + node = self.head + + for i in reversed(range(self.level)): + # i < node.level - When node level is lesser than `i` decrement `i`. + # node.forward[i].key < key - Jumping to node with key value higher + # or equal to searched key would result + # in skipping searched key. + while i < node.level and node.forward[i].key < key: + node = node.forward[i] + # Each leftmost node (relative to searched node) will potentially have to + # be updated. + update_vector.append(node) + + update_vector.reverse() # Note that we were inserting values in reverse order. + + # len(node.forward) != 0 - If current node doesn't contain any further + # references then searched key is not present. + # node.forward[0].key == key - Next node key should be equal to search key + # if key is present. + if len(node.forward) != 0 and node.forward[0].key == key: + return node.forward[0], update_vector + else: + return None, update_vector + + def delete(self, key: KT): + """ + :param key: Key to remove from list. + + >>> skip_list = SkipList() + >>> skip_list.insert(2, "Two") + >>> skip_list.insert(1, "One") + >>> skip_list.insert(3, "Three") + >>> list(skip_list) + [1, 2, 3] + >>> skip_list.delete(2) + >>> list(skip_list) + [1, 3] + """ + + node, update_vector = self._locate_node(key) + + if node is not None: + for i, update_node in enumerate(update_vector): + # Remove or replace all references to removed node. + if update_node.level > i and update_node.forward[i].key == key: + if node.level > i: + update_node.forward[i] = node.forward[i] + else: + update_node.forward = update_node.forward[:i] + + def insert(self, key: KT, value: VT): + """ + :param key: Key to insert. + :param value: Value associated with given key. + + >>> skip_list = SkipList() + >>> skip_list.insert(2, "Two") + >>> skip_list.find(2) + 'Two' + >>> list(skip_list) + [2] + """ + + node, update_vector = self._locate_node(key) + if node is not None: + node.value = value + else: + level = self.random_level() + + if level > self.level: + # After level increase we have to add additional nodes to head. + for i in range(self.level - 1, level): + update_vector.append(self.head) + self.level = level + + new_node = Node(key, value) + + for i, update_node in enumerate(update_vector[:level]): + # Change references to pass through new node. + if update_node.level > i: + new_node.forward.append(update_node.forward[i]) + + if update_node.level < i + 1: + update_node.forward.append(new_node) + else: + update_node.forward[i] = new_node + + def find(self, key: VT) -> Optional[VT]: + """ + :param key: Search key. + :return: Value associated with given key or None if given key is not present. + + >>> skip_list = SkipList() + >>> skip_list.find(2) + >>> skip_list.insert(2, "Two") + >>> skip_list.find(2) + 'Two' + >>> skip_list.insert(2, "Three") + >>> skip_list.find(2) + 'Three' + """ + + node, _ = self._locate_node(key) + + if node is not None: + return node.value + + return None + + +def test_insert(): + skip_list = SkipList() + skip_list.insert("Key1", 3) + skip_list.insert("Key2", 12) + skip_list.insert("Key3", 41) + skip_list.insert("Key4", -19) + + node = skip_list.head + all_values = {} + while node.level != 0: + node = node.forward[0] + all_values[node.key] = node.value + + assert len(all_values) == 4 + assert all_values["Key1"] == 3 + assert all_values["Key2"] == 12 + assert all_values["Key3"] == 41 + assert all_values["Key4"] == -19 + + +def test_insert_overrides_existing_value(): + skip_list = SkipList() + skip_list.insert("Key1", 10) + skip_list.insert("Key1", 12) + + skip_list.insert("Key5", 7) + skip_list.insert("Key7", 10) + skip_list.insert("Key10", 5) + + skip_list.insert("Key7", 7) + skip_list.insert("Key5", 5) + skip_list.insert("Key10", 10) + + node = skip_list.head + all_values = {} + while node.level != 0: + node = node.forward[0] + all_values[node.key] = node.value + + if len(all_values) != 4: + print() + assert len(all_values) == 4 + assert all_values["Key1"] == 12 + assert all_values["Key7"] == 7 + assert all_values["Key5"] == 5 + assert all_values["Key10"] == 10 + + +def test_searching_empty_list_returns_none(): + skip_list = SkipList() + assert skip_list.find("Some key") is None + + +def test_search(): + skip_list = SkipList() + + skip_list.insert("Key2", 20) + assert skip_list.find("Key2") == 20 + + skip_list.insert("Some Key", 10) + skip_list.insert("Key2", 8) + skip_list.insert("V", 13) + + assert skip_list.find("Y") is None + assert skip_list.find("Key2") == 8 + assert skip_list.find("Some Key") == 10 + assert skip_list.find("V") == 13 + + +def test_deleting_item_from_empty_list_do_nothing(): + skip_list = SkipList() + skip_list.delete("Some key") + + assert len(skip_list.head.forward) == 0 + + +def test_deleted_items_are_not_founded_by_find_method(): + skip_list = SkipList() + + skip_list.insert("Key1", 12) + skip_list.insert("V", 13) + skip_list.insert("X", 14) + skip_list.insert("Key2", 15) + + skip_list.delete("V") + skip_list.delete("Key2") + + assert skip_list.find("V") is None + assert skip_list.find("Key2") is None + + +def test_delete_removes_only_given_key(): + skip_list = SkipList() + + skip_list.insert("Key1", 12) + skip_list.insert("V", 13) + skip_list.insert("X", 14) + skip_list.insert("Key2", 15) + + skip_list.delete("V") + assert skip_list.find("V") is None + assert skip_list.find("X") == 14 + assert skip_list.find("Key1") == 12 + assert skip_list.find("Key2") == 15 + + skip_list.delete("X") + assert skip_list.find("V") is None + assert skip_list.find("X") is None + assert skip_list.find("Key1") == 12 + assert skip_list.find("Key2") == 15 + + skip_list.delete("Key1") + assert skip_list.find("V") is None + assert skip_list.find("X") is None + assert skip_list.find("Key1") is None + assert skip_list.find("Key2") == 15 + + skip_list.delete("Key2") + assert skip_list.find("V") is None + assert skip_list.find("X") is None + assert skip_list.find("Key1") is None + assert skip_list.find("Key2") is None + + +def test_delete_doesnt_leave_dead_nodes(): + skip_list = SkipList() + + skip_list.insert("Key1", 12) + skip_list.insert("V", 13) + skip_list.insert("X", 142) + skip_list.insert("Key2", 15) + + skip_list.delete("X") + + def traverse_keys(node): + yield node.key + for forward_node in node.forward: + yield from traverse_keys(forward_node) + + assert len(set(traverse_keys(skip_list.head))) == 4 + + +def test_iter_always_yields_sorted_values(): + def is_sorted(lst): + for item, next_item in zip(lst, lst[1:]): + if next_item < item: + return False + return True + + skip_list = SkipList() + for i in range(10): + skip_list.insert(i, i) + assert is_sorted(list(skip_list)) + skip_list.delete(5) + skip_list.delete(8) + skip_list.delete(2) + assert is_sorted(list(skip_list)) + skip_list.insert(-12, -12) + skip_list.insert(77, 77) + assert is_sorted(list(skip_list)) + + +def pytests(): + for i in range(100): + # Repeat test 100 times due to the probabilistic nature of skip list + # random values == random bugs + test_insert() + test_insert_overrides_existing_value() + + test_searching_empty_list_returns_none() + test_search() + + test_deleting_item_from_empty_list_do_nothing() + test_deleted_items_are_not_founded_by_find_method() + test_delete_removes_only_given_key() + test_delete_doesnt_leave_dead_nodes() + + test_iter_always_yields_sorted_values() + + +def main(): + """ + >>> pytests() + """ + + skip_list = SkipList() + skip_list.insert(2, "2") + skip_list.insert(4, "4") + skip_list.insert(6, "4") + skip_list.insert(4, "5") + skip_list.insert(8, "4") + skip_list.insert(9, "4") + + skip_list.delete(4) + + print(skip_list) + + +if __name__ == "__main__": + main() diff --git a/data_structures/linked_list/swapNodes.py b/data_structures/linked_list/swapNodes.py deleted file mode 100644 index ce2543bc46d8..000000000000 --- a/data_structures/linked_list/swapNodes.py +++ /dev/null @@ -1,75 +0,0 @@ -class Node: - def __init__(self, data): - self.data = data; - self.next = None - - -class Linkedlist: - def __init__(self): - self.head = None - - def print_list(self): - temp = self.head - while temp is not None: - print(temp.data) - temp = temp.next - -# adding nodes - def push(self, new_data): - new_node = Node(new_data) - new_node.next = self.head - self.head = new_node - -# swapping nodes - def swapNodes(self, d1, d2): - prevD1 = None - prevD2 = None - if d1 == d2: - return - else: - # find d1 - D1 = self.head - while D1 is not None and D1.data != d1: - prevD1 = D1 - D1 = D1.next - # find d2 - D2 = self.head - while D2 is not None and D2.data != d2: - prevD2 = D2 - D2 = D2.next - if D1 is None and D2 is None: - return - # if D1 is head - if prevD1 is not None: - prevD1.next = D2 - else: - self.head = D2 - # if D2 is head - if prevD2 is not None: - prevD2.next = D1 - else: - self.head = D1 - temp = D1.next - D1.next = D2.next - D2.next = temp - -# swapping code ends here - - - -if __name__ == '__main__': - list = Linkedlist() - list.push(5) - list.push(4) - list.push(3) - list.push(2) - list.push(1) - - list.print_list() - - list.swapNodes(1, 4) - print("After swapping") - list.print_list() - - - diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py new file mode 100644 index 000000000000..3f825756b3d2 --- /dev/null +++ b/data_structures/linked_list/swap_nodes.py @@ -0,0 +1,55 @@ +from typing import Any + + +class Node: + def __init__(self, data: Any): + self.data = data + self.next = None + + +class LinkedList: + def __init__(self): + self.head = None + + def print_list(self): + temp = self.head + while temp is not None: + print(temp.data, end=" ") + temp = temp.next + print() + + # adding nodes + def push(self, new_data: Any): + new_node = Node(new_data) + new_node.next = self.head + self.head = new_node + + # swapping nodes + def swap_nodes(self, node_data_1, node_data_2): + if node_data_1 == node_data_2: + return + else: + node_1 = self.head + while node_1 is not None and node_1.data != node_data_1: + node_1 = node_1.next + + node_2 = self.head + while node_2 is not None and node_2.data != node_data_2: + node_2 = node_2.next + + if node_1 is None or node_2 is None: + return + + node_1.data, node_2.data = node_2.data, node_1.data + + +if __name__ == "__main__": + ll = LinkedList() + for i in range(5, 0, -1): + ll.push(i) + + ll.print_list() + + ll.swap_nodes(1, 4) + print("After swapping") + ll.print_list() diff --git a/data_structures/queue/circular_queue.py b/data_structures/queue/circular_queue.py new file mode 100644 index 000000000000..93a6ef805c7c --- /dev/null +++ b/data_structures/queue/circular_queue.py @@ -0,0 +1,94 @@ +# Implementation of Circular Queue (using Python lists) + + +class CircularQueue: + """Circular FIFO queue with a fixed capacity""" + + def __init__(self, n: int): + self.n = n + self.array = [None] * self.n + self.front = 0 # index of the first element + self.rear = 0 + self.size = 0 + + def __len__(self) -> int: + """ + >>> cq = CircularQueue(5) + >>> len(cq) + 0 + >>> cq.enqueue("A") # doctest: +ELLIPSIS + >> len(cq) + 1 + """ + return self.size + + def is_empty(self) -> bool: + """ + >>> cq = CircularQueue(5) + >>> cq.is_empty() + True + >>> cq.enqueue("A").is_empty() + False + """ + return self.size == 0 + + def first(self): + """ + >>> cq = CircularQueue(5) + >>> cq.first() + False + >>> cq.enqueue("A").first() + 'A' + """ + return False if self.is_empty() else self.array[self.front] + + def enqueue(self, data): + """ + This function insert an element in the queue using self.rear value as an index + >>> cq = CircularQueue(5) + >>> cq.enqueue("A") # doctest: +ELLIPSIS + >> (cq.size, cq.first()) + (1, 'A') + >>> cq.enqueue("B") # doctest: +ELLIPSIS + >> (cq.size, cq.first()) + (2, 'A') + """ + if self.size >= self.n: + raise Exception("QUEUE IS FULL") + + self.array[self.rear] = data + self.rear = (self.rear + 1) % self.n + self.size += 1 + return self + + def dequeue(self): + """ + This function removes an element from the queue using on self.front value as an + index + >>> cq = CircularQueue(5) + >>> cq.dequeue() + Traceback (most recent call last): + ... + Exception: UNDERFLOW + >>> cq.enqueue("A").enqueue("B").dequeue() + 'A' + >>> (cq.size, cq.first()) + (1, 'B') + >>> cq.dequeue() + 'B' + >>> cq.dequeue() + Traceback (most recent call last): + ... + Exception: UNDERFLOW + """ + if self.size == 0: + raise Exception("UNDERFLOW") + + temp = self.array[self.front] + self.array[self.front] = None + self.front = (self.front + 1) % self.n + self.size -= 1 + return temp diff --git a/data_structures/queue/deqeue.py b/data_structures/queue/deqeue.py deleted file mode 100644 index fdee64eb6ae0..000000000000 --- a/data_structures/queue/deqeue.py +++ /dev/null @@ -1,40 +0,0 @@ -from __future__ import print_function -# Python code to demonstrate working of -# extend(), extendleft(), rotate(), reverse() - -# importing "collections" for deque operations -import collections - -# initializing deque -de = collections.deque([1, 2, 3,]) - -# using extend() to add numbers to right end -# adds 4,5,6 to right end -de.extend([4,5,6]) - -# printing modified deque -print ("The deque after extending deque at end is : ") -print (de) - -# using extendleft() to add numbers to left end -# adds 7,8,9 to right end -de.extendleft([7,8,9]) - -# printing modified deque -print ("The deque after extending deque at beginning is : ") -print (de) - -# using rotate() to rotate the deque -# rotates by 3 to left -de.rotate(-3) - -# printing modified deque -print ("The deque after rotating deque is : ") -print (de) - -# using reverse() to reverse the deque -de.reverse() - -# printing modified deque -print ("The deque after reversing deque is : ") -print (de) diff --git a/data_structures/queue/double_ended_queue.py b/data_structures/queue/double_ended_queue.py new file mode 100644 index 000000000000..dd003b7c98ac --- /dev/null +++ b/data_structures/queue/double_ended_queue.py @@ -0,0 +1,57 @@ +# Python code to demonstrate working of +# extend(), extendleft(), rotate(), reverse() + +# importing "collections" for deque operations +import collections + +# initializing deque +de = collections.deque([1, 2, 3]) + +# using extend() to add numbers to right end +# adds 4,5,6 to right end +de.extend([4, 5, 6]) + +# printing modified deque +print("The deque after extending deque at end is : ") +print(de) + +# using extendleft() to add numbers to left end +# adds 7,8,9 to right end +de.extendleft([7, 8, 9]) + +# printing modified deque +print("The deque after extending deque at beginning is : ") +print(de) + +# using rotate() to rotate the deque +# rotates by 3 to left +de.rotate(-3) + +# printing modified deque +print("The deque after rotating deque is : ") +print(de) + +# using reverse() to reverse the deque +de.reverse() + +# printing modified deque +print("The deque after reversing deque is : ") +print(de) + +# get right-end value and eliminate +startValue = de.pop() + +print("The deque after popping value at end is : ") +print(de) + +# get left-end value and eliminate +endValue = de.popleft() + +print("The deque after popping value at start is : ") +print(de) + +# eliminate element searched by value +de.remove(5) + +print("The deque after eliminating element searched by value : ") +print(de) diff --git a/data_structures/queue/linked_queue.py b/data_structures/queue/linked_queue.py new file mode 100644 index 000000000000..8526ad311ed0 --- /dev/null +++ b/data_structures/queue/linked_queue.py @@ -0,0 +1,151 @@ +""" A Queue using a linked list like structure """ +from typing import Any + + +class Node: + def __init__(self, data: Any) -> None: + self.data = data + self.next = None + + def __str__(self) -> str: + return f"{self.data}" + + +class LinkedQueue: + """ + >>> queue = LinkedQueue() + >>> queue.is_empty() + True + >>> queue.put(5) + >>> queue.put(9) + >>> queue.put('python') + >>> queue.is_empty(); + False + >>> queue.get() + 5 + >>> queue.put('algorithms') + >>> queue.get() + 9 + >>> queue.get() + 'python' + >>> queue.get() + 'algorithms' + >>> queue.is_empty() + True + >>> queue.get() + Traceback (most recent call last): + ... + IndexError: dequeue from empty queue + """ + + def __init__(self) -> None: + self.front = self.rear = None + + def __iter__(self): + node = self.front + while node: + yield node.data + node = node.next + + def __len__(self) -> int: + """ + >>> queue = LinkedQueue() + >>> for i in range(1, 6): + ... queue.put(i) + >>> len(queue) + 5 + >>> for i in range(1, 6): + ... assert len(queue) == 6 - i + ... _ = queue.get() + >>> len(queue) + 0 + """ + return len(tuple(iter(self))) + + def __str__(self) -> str: + """ + >>> queue = LinkedQueue() + >>> for i in range(1, 4): + ... queue.put(i) + >>> queue.put("Python") + >>> queue.put(3.14) + >>> queue.put(True) + >>> str(queue) + '1 <- 2 <- 3 <- Python <- 3.14 <- True' + """ + return " <- ".join(str(item) for item in self) + + def is_empty(self) -> bool: + """ + >>> queue = LinkedQueue() + >>> queue.is_empty() + True + >>> for i in range(1, 6): + ... queue.put(i) + >>> queue.is_empty() + False + """ + return len(self) == 0 + + def put(self, item) -> None: + """ + >>> queue = LinkedQueue() + >>> queue.get() + Traceback (most recent call last): + ... + IndexError: dequeue from empty queue + >>> for i in range(1, 6): + ... queue.put(i) + >>> str(queue) + '1 <- 2 <- 3 <- 4 <- 5' + """ + node = Node(item) + if self.is_empty(): + self.front = self.rear = node + else: + assert isinstance(self.rear, Node) + self.rear.next = node + self.rear = node + + def get(self) -> Any: + """ + >>> queue = LinkedQueue() + >>> queue.get() + Traceback (most recent call last): + ... + IndexError: dequeue from empty queue + >>> queue = LinkedQueue() + >>> for i in range(1, 6): + ... queue.put(i) + >>> for i in range(1, 6): + ... assert queue.get() == i + >>> len(queue) + 0 + """ + if self.is_empty(): + raise IndexError("dequeue from empty queue") + assert isinstance(self.front, Node) + node = self.front + self.front = self.front.next + if self.front is None: + self.rear = None + return node.data + + def clear(self) -> None: + """ + >>> queue = LinkedQueue() + >>> for i in range(1, 6): + ... queue.put(i) + >>> queue.clear() + >>> len(queue) + 0 + >>> str(queue) + '' + """ + self.front = self.rear = None + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/data_structures/queue/priority_queue_using_list.py b/data_structures/queue/priority_queue_using_list.py new file mode 100644 index 000000000000..c5cf26433fff --- /dev/null +++ b/data_structures/queue/priority_queue_using_list.py @@ -0,0 +1,232 @@ +""" +Pure Python implementations of a Fixed Priority Queue and an Element Priority Queue +using Python lists. +""" + + +class OverFlowError(Exception): + pass + + +class UnderFlowError(Exception): + pass + + +class FixedPriorityQueue: + """ + Tasks can be added to a Priority Queue at any time and in any order but when Tasks + are removed then the Task with the highest priority is removed in FIFO order. In + code we will use three levels of priority with priority zero Tasks being the most + urgent (high priority) and priority 2 tasks being the least urgent. + + Examples + >>> fpq = FixedPriorityQueue() + >>> fpq.enqueue(0, 10) + >>> fpq.enqueue(1, 70) + >>> fpq.enqueue(0, 100) + >>> fpq.enqueue(2, 1) + >>> fpq.enqueue(2, 5) + >>> fpq.enqueue(1, 7) + >>> fpq.enqueue(2, 4) + >>> fpq.enqueue(1, 64) + >>> fpq.enqueue(0, 128) + >>> print(fpq) + Priority 0: [10, 100, 128] + Priority 1: [70, 7, 64] + Priority 2: [1, 5, 4] + >>> fpq.dequeue() + 10 + >>> fpq.dequeue() + 100 + >>> fpq.dequeue() + 128 + >>> fpq.dequeue() + 70 + >>> fpq.dequeue() + 7 + >>> print(fpq) + Priority 0: [] + Priority 1: [64] + Priority 2: [1, 5, 4] + >>> fpq.dequeue() + 64 + >>> fpq.dequeue() + 1 + >>> fpq.dequeue() + 5 + >>> fpq.dequeue() + 4 + >>> fpq.dequeue() + Traceback (most recent call last): + ... + data_structures.queue.priority_queue_using_list.UnderFlowError: All queues are empty + >>> print(fpq) + Priority 0: [] + Priority 1: [] + Priority 2: [] + """ + + def __init__(self): + self.queues = [ + [], + [], + [], + ] + + def enqueue(self, priority: int, data: int) -> None: + """ + Add an element to a queue based on its priority. + If the priority is invalid ValueError is raised. + If the queue is full an OverFlowError is raised. + """ + try: + if len(self.queues[priority]) >= 100: + raise OverflowError("Maximum queue size is 100") + self.queues[priority].append(data) + except IndexError: + raise ValueError("Valid priorities are 0, 1, and 2") + + def dequeue(self) -> int: + """ + Return the highest priority element in FIFO order. + If the queue is empty then an under flow exception is raised. + """ + for queue in self.queues: + if queue: + return queue.pop(0) + raise UnderFlowError("All queues are empty") + + def __str__(self) -> str: + return "\n".join(f"Priority {i}: {q}" for i, q in enumerate(self.queues)) + + +class ElementPriorityQueue: + """ + Element Priority Queue is the same as Fixed Priority Queue except that the value of + the element itself is the priority. The rules for priorities are the same the as + Fixed Priority Queue. + + >>> epq = ElementPriorityQueue() + >>> epq.enqueue(10) + >>> epq.enqueue(70) + >>> epq.enqueue(4) + >>> epq.enqueue(1) + >>> epq.enqueue(5) + >>> epq.enqueue(7) + >>> epq.enqueue(4) + >>> epq.enqueue(64) + >>> epq.enqueue(128) + >>> print(epq) + [10, 70, 4, 1, 5, 7, 4, 64, 128] + >>> epq.dequeue() + 1 + >>> epq.dequeue() + 4 + >>> epq.dequeue() + 4 + >>> epq.dequeue() + 5 + >>> epq.dequeue() + 7 + >>> epq.dequeue() + 10 + >>> print(epq) + [70, 64, 128] + >>> epq.dequeue() + 64 + >>> epq.dequeue() + 70 + >>> epq.dequeue() + 128 + >>> epq.dequeue() + Traceback (most recent call last): + ... + data_structures.queue.priority_queue_using_list.UnderFlowError: The queue is empty + >>> print(epq) + [] + """ + + def __init__(self): + self.queue = [] + + def enqueue(self, data: int) -> None: + """ + This function enters the element into the queue + If the queue is full an Exception is raised saying Over Flow! + """ + if len(self.queue) == 100: + raise OverFlowError("Maximum queue size is 100") + self.queue.append(data) + + def dequeue(self) -> int: + """ + Return the highest priority element in FIFO order. + If the queue is empty then an under flow exception is raised. + """ + if not self.queue: + raise UnderFlowError("The queue is empty") + else: + data = min(self.queue) + self.queue.remove(data) + return data + + def __str__(self) -> str: + """ + Prints all the elements within the Element Priority Queue + """ + return str(self.queue) + + +def fixed_priority_queue(): + fpq = FixedPriorityQueue() + fpq.enqueue(0, 10) + fpq.enqueue(1, 70) + fpq.enqueue(0, 100) + fpq.enqueue(2, 1) + fpq.enqueue(2, 5) + fpq.enqueue(1, 7) + fpq.enqueue(2, 4) + fpq.enqueue(1, 64) + fpq.enqueue(0, 128) + print(fpq) + print(fpq.dequeue()) + print(fpq.dequeue()) + print(fpq.dequeue()) + print(fpq.dequeue()) + print(fpq.dequeue()) + print(fpq) + print(fpq.dequeue()) + print(fpq.dequeue()) + print(fpq.dequeue()) + print(fpq.dequeue()) + print(fpq.dequeue()) + + +def element_priority_queue(): + epq = ElementPriorityQueue() + epq.enqueue(10) + epq.enqueue(70) + epq.enqueue(100) + epq.enqueue(1) + epq.enqueue(5) + epq.enqueue(7) + epq.enqueue(4) + epq.enqueue(64) + epq.enqueue(128) + print(epq) + print(epq.dequeue()) + print(epq.dequeue()) + print(epq.dequeue()) + print(epq.dequeue()) + print(epq.dequeue()) + print(epq) + print(epq.dequeue()) + print(epq.dequeue()) + print(epq.dequeue()) + print(epq.dequeue()) + print(epq.dequeue()) + + +if __name__ == "__main__": + fixed_priority_queue() + element_priority_queue() diff --git a/data_structures/queue/queue_on_list.py b/data_structures/queue/queue_on_list.py index c8d0b41de5d5..485cf0b6f7a3 100644 --- a/data_structures/queue/queue_on_list.py +++ b/data_structures/queue/queue_on_list.py @@ -1,45 +1,52 @@ -"""Queue represented by a python list""" -class Queue(): +"""Queue represented by a Python list""" + + +class Queue: def __init__(self): self.entries = [] self.length = 0 - self.front=0 + self.front = 0 def __str__(self): - printed = '<' + str(self.entries)[1:-1] + '>' + printed = "<" + str(self.entries)[1:-1] + ">" return printed """Enqueues {@code item} @param item item to enqueue""" + def put(self, item): self.entries.append(item) self.length = self.length + 1 - """Dequeues {@code item} @requirement: |self.length| > 0 @return dequeued item that was dequeued""" + def get(self): self.length = self.length - 1 dequeued = self.entries[self.front] - self.front-=1 - self.entries = self.entries[self.front:] + # self.front-=1 + # self.entries = self.entries[self.front:] + self.entries = self.entries[1:] return dequeued """Rotates the queue {@code rotation} times @param rotation number of times to rotate queue""" + def rotate(self, rotation): for i in range(rotation): self.put(self.get()) """Enqueues {@code item} @return item at front of self.entries""" - def front(self): + + def get_front(self): return self.entries[0] """Returns the length of this.entries""" + def size(self): return self.length diff --git a/data_structures/queue/queue_on_pseudo_stack.py b/data_structures/queue/queue_on_pseudo_stack.py index b69fbcc988f7..7fa2fb2566af 100644 --- a/data_structures/queue/queue_on_pseudo_stack.py +++ b/data_structures/queue/queue_on_pseudo_stack.py @@ -1,16 +1,19 @@ """Queue represented by a pseudo stack (represented by a list with pop and append)""" -class Queue(): + + +class Queue: def __init__(self): self.stack = [] self.length = 0 def __str__(self): - printed = '<' + str(self.stack)[1:-1] + '>' + printed = "<" + str(self.stack)[1:-1] + ">" return printed """Enqueues {@code item} @param item item to enqueue""" + def put(self, item): self.stack.append(item) self.length = self.length + 1 @@ -19,17 +22,19 @@ def put(self, item): @requirement: |self.length| > 0 @return dequeued item that was dequeued""" + def get(self): self.rotate(1) - dequeued = self.stack[self.length-1] + dequeued = self.stack[self.length - 1] self.stack = self.stack[:-1] - self.rotate(self.length-1) - self.length = self.length -1 + self.rotate(self.length - 1) + self.length = self.length - 1 return dequeued """Rotates the queue {@code rotation} times @param rotation number of times to rotate queue""" + def rotate(self, rotation): for i in range(rotation): temp = self.stack[0] @@ -39,12 +44,14 @@ def rotate(self, rotation): """Reports item at the front of self @return item at front of self.stack""" + def front(self): front = self.get() self.put(front) - self.rotate(self.length-1) + self.rotate(self.length - 1) return front """Returns the length of this.stack""" + def size(self): return self.length diff --git a/data_structures/stacks/__init__.py b/data_structures/stacks/__init__.py index f7e92ae2d269..e69de29bb2d1 100644 --- a/data_structures/stacks/__init__.py +++ b/data_structures/stacks/__init__.py @@ -1,23 +0,0 @@ -class Stack: - - def __init__(self): - self.stack = [] - self.top = 0 - - def is_empty(self): - return (self.top == 0) - - def push(self, item): - if self.top < len(self.stack): - self.stack[self.top] = item - else: - self.stack.append(item) - - self.top += 1 - - def pop(self): - if self.is_empty(): - return None - else: - self.top -= 1 - return self.stack[self.top] diff --git a/data_structures/stacks/balanced_parentheses.py b/data_structures/stacks/balanced_parentheses.py index 3229d19c8621..674f7ea436ed 100644 --- a/data_structures/stacks/balanced_parentheses.py +++ b/data_structures/stacks/balanced_parentheses.py @@ -1,25 +1,37 @@ -from __future__ import print_function -from __future__ import absolute_import -from stack import Stack +from .stack import Stack -__author__ = 'Omkar Pathak' - -def balanced_parentheses(parentheses): - """ Use a stack to check if a string of parentheses is balanced.""" - stack = Stack(len(parentheses)) - for parenthesis in parentheses: - if parenthesis == '(': - stack.push(parenthesis) - elif parenthesis == ')': - if stack.is_empty(): +def balanced_parentheses(parentheses: str) -> bool: + """Use a stack to check if a string of parentheses is balanced. + >>> balanced_parentheses("([]{})") + True + >>> balanced_parentheses("[()]{}{[()()]()}") + True + >>> balanced_parentheses("[(])") + False + >>> balanced_parentheses("1+2*3-4") + True + >>> balanced_parentheses("") + True + """ + stack = Stack() + bracket_pairs = {"(": ")", "[": "]", "{": "}"} + for bracket in parentheses: + if bracket in bracket_pairs: + stack.push(bracket) + elif bracket in (")", "]", "}"): + if stack.is_empty() or bracket_pairs[stack.pop()] != bracket: return False - stack.pop() return stack.is_empty() -if __name__ == '__main__': - examples = ['((()))', '((())', '(()))'] - print('Balanced parentheses demonstration:\n') +if __name__ == "__main__": + from doctest import testmod + + testmod() + + examples = ["((()))", "((())", "(()))"] + print("Balanced parentheses demonstration:\n") for example in examples: - print(example + ': ' + str(balanced_parentheses(example))) + not_str = "" if balanced_parentheses(example) else "not " + print(f"{example} is {not_str}balanced") diff --git a/data_structures/stacks/dijkstras_two_stack_algorithm.py b/data_structures/stacks/dijkstras_two_stack_algorithm.py new file mode 100644 index 000000000000..8b4668f9f839 --- /dev/null +++ b/data_structures/stacks/dijkstras_two_stack_algorithm.py @@ -0,0 +1,83 @@ +""" +Author: Alexander Joslin +GitHub: github.com/echoaj + +Explanation: https://medium.com/@haleesammar/implemented-in-js-dijkstras-2-stack- + algorithm-for-evaluating-mathematical-expressions-fc0837dae1ea + +We can use Dijkstra's two stack algorithm to solve an equation +such as: (5 + ((4 * 2) * (2 + 3))) + +THESE ARE THE ALGORITHM'S RULES: +RULE 1: Scan the expression from left to right. When an operand is encountered, + push it onto the the operand stack. + +RULE 2: When an operator is encountered in the expression, + push it onto the operator stack. + +RULE 3: When a left parenthesis is encountered in the expression, ignore it. + +RULE 4: When a right parenthesis is encountered in the expression, + pop an operator off the operator stack. The two operands it must + operate on must be the last two operands pushed onto the operand stack. + We therefore pop the operand stack twice, perform the operation, + and push the result back onto the operand stack so it will be available + for use as an operand of the next operator popped off the operator stack. + +RULE 5: When the entire infix expression has been scanned, the value left on + the operand stack represents the value of the expression. + +NOTE: It only works with whole numbers. +""" +__author__ = "Alexander Joslin" + +import operator as op + +from .stack import Stack + + +def dijkstras_two_stack_algorithm(equation: str) -> int: + """ + DocTests + >>> dijkstras_two_stack_algorithm("(5 + 3)") + 8 + >>> dijkstras_two_stack_algorithm("((9 - (2 + 9)) + (8 - 1))") + 5 + >>> dijkstras_two_stack_algorithm("((((3 - 2) - (2 + 3)) + (2 - 4)) + 3)") + -3 + + :param equation: a string + :return: result: an integer + """ + operators = {"*": op.mul, "/": op.truediv, "+": op.add, "-": op.sub} + + operand_stack = Stack() + operator_stack = Stack() + + for i in equation: + if i.isdigit(): + # RULE 1 + operand_stack.push(int(i)) + elif i in operators: + # RULE 2 + operator_stack.push(i) + elif i == ")": + # RULE 4 + opr = operator_stack.peek() + operator_stack.pop() + num1 = operand_stack.peek() + operand_stack.pop() + num2 = operand_stack.peek() + operand_stack.pop() + + total = operators[opr](num2, num1) + operand_stack.push(total) + + # RULE 5 + return operand_stack.peek() + + +if __name__ == "__main__": + equation = "(5 + ((4 * 2) * (2 + 3)))" + # answer = 45 + print(f"{equation} = {dijkstras_two_stack_algorithm(equation)}") diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py new file mode 100644 index 000000000000..a03cb43bb020 --- /dev/null +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -0,0 +1,49 @@ +""" +The Reverse Polish Nation also known as Polish postfix notation +or simply postfix notation. +https://en.wikipedia.org/wiki/Reverse_Polish_notation +Classic examples of simple stack implementations +Valid operators are +, -, *, /. +Each operand may be an integer or another expression. +""" + + +def evaluate_postfix(postfix_notation: list) -> int: + """ + >>> evaluate_postfix(["2", "1", "+", "3", "*"]) + 9 + >>> evaluate_postfix(["4", "13", "5", "/", "+"]) + 6 + >>> evaluate_postfix([]) + 0 + """ + if not postfix_notation: + return 0 + + operations = {"+", "-", "*", "/"} + stack = [] + + for token in postfix_notation: + if token in operations: + b, a = stack.pop(), stack.pop() + if token == "+": + stack.append(a + b) + elif token == "-": + stack.append(a - b) + elif token == "*": + stack.append(a * b) + else: + if a * b < 0 and a % b != 0: + stack.append(a // b + 1) + else: + stack.append(a // b) + else: + stack.append(int(token)) + + return stack.pop() + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/stacks/infix_to_postfix_conversion.py b/data_structures/stacks/infix_to_postfix_conversion.py index 75211fed258d..dedba8479ac8 100644 --- a/data_structures/stacks/infix_to_postfix_conversion.py +++ b/data_structures/stacks/infix_to_postfix_conversion.py @@ -1,64 +1,69 @@ -from __future__ import print_function -from __future__ import absolute_import -import string +""" +https://en.wikipedia.org/wiki/Infix_notation +https://en.wikipedia.org/wiki/Reverse_Polish_notation +https://en.wikipedia.org/wiki/Shunting-yard_algorithm +""" -from .Stack import Stack +from .balanced_parentheses import balanced_parentheses +from .stack import Stack -__author__ = 'Omkar Pathak' - -def is_operand(char): - return char in string.ascii_letters or char in string.digits - - -def precedence(char): - """ Return integer value representing an operator's precedence, or +def precedence(char: str) -> int: + """ + Return integer value representing an operator's precedence, or order of operation. - https://en.wikipedia.org/wiki/Order_of_operations """ - dictionary = {'+': 1, '-': 1, - '*': 2, '/': 2, - '^': 3} - return dictionary.get(char, -1) - + return {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3}.get(char, -1) -def infix_to_postfix(expression): - """ Convert infix notation to postfix notation using the Shunting-yard - algorithm. - https://en.wikipedia.org/wiki/Shunting-yard_algorithm - https://en.wikipedia.org/wiki/Infix_notation - https://en.wikipedia.org/wiki/Reverse_Polish_notation +def infix_to_postfix(expression_str: str) -> str: """ - stack = Stack(len(expression)) + >>> infix_to_postfix("(1*(2+3)+4))") + Traceback (most recent call last): + ... + ValueError: Mismatched parentheses + >>> infix_to_postfix("") + '' + >>> infix_to_postfix("3+2") + '3 2 +' + >>> infix_to_postfix("(3+4)*5-6") + '3 4 + 5 * 6 -' + >>> infix_to_postfix("(1+2)*3/4-5") + '1 2 + 3 * 4 / 5 -' + >>> infix_to_postfix("a+b*c+(d*e+f)*g") + 'a b c * + d e * f + g * +' + >>> infix_to_postfix("x^y/(5*z)+2") + 'x y ^ 5 z * / 2 +' + """ + if not balanced_parentheses(expression_str): + raise ValueError("Mismatched parentheses") + stack = Stack() postfix = [] - for char in expression: - if is_operand(char): + for char in expression_str: + if char.isalpha() or char.isdigit(): postfix.append(char) - elif char not in {'(', ')'}: - while (not stack.is_empty() - and precedence(char) <= precedence(stack.peek())): - postfix.append(stack.pop()) - stack.push(char) - elif char == '(': + elif char == "(": stack.push(char) - elif char == ')': - while not stack.is_empty() and stack.peek() != '(': + elif char == ")": + while not stack.is_empty() and stack.peek() != "(": postfix.append(stack.pop()) - # Pop '(' from stack. If there is no '(', there is a mismatched - # parentheses. - if stack.peek() != '(': - raise ValueError('Mismatched parentheses') stack.pop() + else: + while not stack.is_empty() and precedence(char) <= precedence(stack.peek()): + postfix.append(stack.pop()) + stack.push(char) while not stack.is_empty(): postfix.append(stack.pop()) - return ' '.join(postfix) + return " ".join(postfix) + +if __name__ == "__main__": + from doctest import testmod -if __name__ == '__main__': - expression = 'a+b*(c^d-e)^(f+g*h)-i' + testmod() + expression = "a+b*(c^d-e)^(f+g*h)-i" - print('Infix to Postfix Notation demonstration:\n') - print('Infix notation: ' + expression) - print('Postfix notation: ' + infix_to_postfix(expression)) + print("Infix to Postfix Notation demonstration:\n") + print("Infix notation: " + expression) + print("Postfix notation: " + infix_to_postfix(expression)) diff --git a/data_structures/stacks/infix_to_prefix_conversion.py b/data_structures/stacks/infix_to_prefix_conversion.py index da5fc261fb9f..d3dc9e3e9c73 100644 --- a/data_structures/stacks/infix_to_prefix_conversion.py +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -11,51 +11,83 @@ a | + | cb^a | | cb^a+ - a+b^c (Infix) -> +a^bc (Prefix) + a+b^c (Infix) -> +a^bc (Prefix) """ + def infix_2_postfix(Infix): Stack = [] Postfix = [] - priority = {'^':3, '*':2, '/':2, '%':2, '+':1, '-':1} # Priority of each operator - print_width = len(Infix) if(len(Infix)>7) else 7 + priority = { + "^": 3, + "*": 2, + "/": 2, + "%": 2, + "+": 1, + "-": 1, + } # Priority of each operator + print_width = len(Infix) if (len(Infix) > 7) else 7 # Print table header for output - print('Symbol'.center(8), 'Stack'.center(print_width), 'Postfix'.center(print_width), sep = " | ") - print('-'*(print_width*3+7)) + print( + "Symbol".center(8), + "Stack".center(print_width), + "Postfix".center(print_width), + sep=" | ", + ) + print("-" * (print_width * 3 + 7)) for x in Infix: - if(x.isalpha() or x.isdigit()): Postfix.append(x) # if x is Alphabet / Digit, add it to Postfix - elif(x == '('): Stack.append(x) # if x is "(" push to Stack - elif(x == ')'): # if x is ")" pop stack until "(" is encountered - while(Stack[-1] != '('): - Postfix.append( Stack.pop() ) #Pop stack & add the content to Postfix + if x.isalpha() or x.isdigit(): + Postfix.append(x) # if x is Alphabet / Digit, add it to Postfix + elif x == "(": + Stack.append(x) # if x is "(" push to Stack + elif x == ")": # if x is ")" pop stack until "(" is encountered + while Stack[-1] != "(": + Postfix.append(Stack.pop()) # Pop stack & add the content to Postfix Stack.pop() else: - if(len(Stack)==0): Stack.append(x) #If stack is empty, push x to stack - else: - while( len(Stack) > 0 and priority[x] <= priority[Stack[-1]]): # while priority of x is not greater than priority of element in the stack - Postfix.append( Stack.pop() ) # pop stack & add to Postfix - Stack.append(x) # push x to stack + if len(Stack) == 0: + Stack.append(x) # If stack is empty, push x to stack + else: # while priority of x is not > priority of element in the stack + while len(Stack) > 0 and priority[x] <= priority[Stack[-1]]: + Postfix.append(Stack.pop()) # pop stack & add to Postfix + Stack.append(x) # push x to stack + + print( + x.center(8), + ("".join(Stack)).ljust(print_width), + ("".join(Postfix)).ljust(print_width), + sep=" | ", + ) # Output in tabular format - print(x.center(8), (''.join(Stack)).ljust(print_width), (''.join(Postfix)).ljust(print_width), sep = " | ") # Output in tabular format + while len(Stack) > 0: # while stack is not empty + Postfix.append(Stack.pop()) # pop stack & add to Postfix + print( + " ".center(8), + ("".join(Stack)).ljust(print_width), + ("".join(Postfix)).ljust(print_width), + sep=" | ", + ) # Output in tabular format - while(len(Stack) > 0): # while stack is not empty - Postfix.append( Stack.pop() ) # pop stack & add to Postfix - print(' '.center(8), (''.join(Stack)).ljust(print_width), (''.join(Postfix)).ljust(print_width), sep = " | ") # Output in tabular format + return "".join(Postfix) # return Postfix as str - return "".join(Postfix) # return Postfix as str def infix_2_prefix(Infix): - Infix = list(Infix[::-1]) # reverse the infix equation - + Infix = list(Infix[::-1]) # reverse the infix equation + for i in range(len(Infix)): - if(Infix[i] == '('): Infix[i] = ')' # change "(" to ")" - elif(Infix[i] == ')'): Infix[i] = '(' # change ")" to "(" - - return (infix_2_postfix("".join(Infix)))[::-1] # call infix_2_postfix on Infix, return reverse of Postfix + if Infix[i] == "(": + Infix[i] = ")" # change "(" to ")" + elif Infix[i] == ")": + Infix[i] = "(" # change ")" to "(" + + return (infix_2_postfix("".join(Infix)))[ + ::-1 + ] # call infix_2_postfix on Infix, return reverse of Postfix + if __name__ == "__main__": - Infix = input("\nEnter an Infix Equation = ") #Input an Infix equation - Infix = "".join(Infix.split()) #Remove spaces from the input + Infix = input("\nEnter an Infix Equation = ") # Input an Infix equation + Infix = "".join(Infix.split()) # Remove spaces from the input print("\n\t", Infix, "(Infix) -> ", infix_2_prefix(Infix), "(Prefix)") diff --git a/data_structures/stacks/linked_stack.py b/data_structures/stacks/linked_stack.py new file mode 100644 index 000000000000..1a2d07f20e7c --- /dev/null +++ b/data_structures/stacks/linked_stack.py @@ -0,0 +1,157 @@ +""" A Stack using a linked list like structure """ +from typing import Any + + +class Node: + def __init__(self, data): + self.data = data + self.next = None + + def __str__(self): + return f"{self.data}" + + +class LinkedStack: + """ + Linked List Stack implementing push (to top), + pop (from top) and is_empty + + >>> stack = LinkedStack() + >>> stack.is_empty() + True + >>> stack.push(5) + >>> stack.push(9) + >>> stack.push('python') + >>> stack.is_empty() + False + >>> stack.pop() + 'python' + >>> stack.push('algorithms') + >>> stack.pop() + 'algorithms' + >>> stack.pop() + 9 + >>> stack.pop() + 5 + >>> stack.is_empty() + True + >>> stack.pop() + Traceback (most recent call last): + ... + IndexError: pop from empty stack + """ + + def __init__(self) -> None: + self.top = None + + def __iter__(self): + node = self.top + while node: + yield node.data + node = node.next + + def __str__(self): + """ + >>> stack = LinkedStack() + >>> stack.push("c") + >>> stack.push("b") + >>> stack.push("a") + >>> str(stack) + 'a->b->c' + """ + return "->".join([str(item) for item in self]) + + def __len__(self): + """ + >>> stack = LinkedStack() + >>> len(stack) == 0 + True + >>> stack.push("c") + >>> stack.push("b") + >>> stack.push("a") + >>> len(stack) == 3 + True + """ + return len(tuple(iter(self))) + + def is_empty(self) -> bool: + """ + >>> stack = LinkedStack() + >>> stack.is_empty() + True + >>> stack.push(1) + >>> stack.is_empty() + False + """ + return self.top is None + + def push(self, item: Any) -> None: + """ + >>> stack = LinkedStack() + >>> stack.push("Python") + >>> stack.push("Java") + >>> stack.push("C") + >>> str(stack) + 'C->Java->Python' + """ + node = Node(item) + if not self.is_empty(): + node.next = self.top + self.top = node + + def pop(self) -> Any: + """ + >>> stack = LinkedStack() + >>> stack.pop() + Traceback (most recent call last): + ... + IndexError: pop from empty stack + >>> stack.push("c") + >>> stack.push("b") + >>> stack.push("a") + >>> stack.pop() == 'a' + True + >>> stack.pop() == 'b' + True + >>> stack.pop() == 'c' + True + """ + if self.is_empty(): + raise IndexError("pop from empty stack") + assert isinstance(self.top, Node) + pop_node = self.top + self.top = self.top.next + return pop_node.data + + def peek(self) -> Any: + """ + >>> stack = LinkedStack() + >>> stack.push("Java") + >>> stack.push("C") + >>> stack.push("Python") + >>> stack.peek() + 'Python' + """ + if self.is_empty(): + raise IndexError("peek from empty stack") + return self.top.data + + def clear(self) -> None: + """ + >>> stack = LinkedStack() + >>> stack.push("Java") + >>> stack.push("C") + >>> stack.push("Python") + >>> str(stack) + 'Python->C->Java' + >>> stack.clear() + >>> len(stack) == 0 + True + """ + self.top = None + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/data_structures/stacks/next.py b/data_structures/stacks/next.py deleted file mode 100644 index bca83339592c..000000000000 --- a/data_structures/stacks/next.py +++ /dev/null @@ -1,17 +0,0 @@ -from __future__ import print_function -# Function to print element and NGE pair for all elements of list -def printNGE(arr): - - for i in range(0, len(arr), 1): - - next = -1 - for j in range(i+1, len(arr), 1): - if arr[i] < arr[j]: - next = arr[j] - break - - print(str(arr[i]) + " -- " + str(next)) - -# Driver program to test above function -arr = [11,13,21,3] -printNGE(arr) diff --git a/data_structures/stacks/next_greater_element.py b/data_structures/stacks/next_greater_element.py new file mode 100644 index 000000000000..d8c7ed17317b --- /dev/null +++ b/data_structures/stacks/next_greater_element.py @@ -0,0 +1,98 @@ +arr = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0] +expect = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1] + + +def next_greatest_element_slow(arr: list) -> list: + """ + Get the Next Greatest Element (NGE) for all elements in a list. + Maximum element present after the current one which is also greater than the + current one. + >>> next_greatest_element_slow(arr) == expect + True + """ + result = [] + for i in range(0, len(arr), 1): + next = -1 + for j in range(i + 1, len(arr), 1): + if arr[i] < arr[j]: + next = arr[j] + break + result.append(next) + return result + + +def next_greatest_element_fast(arr: list) -> list: + """ + Like next_greatest_element_slow() but changes the loops to use + enumerate() instead of range(len()) for the outer loop and + for in a slice of arr for the inner loop. + >>> next_greatest_element_fast(arr) == expect + True + """ + result = [] + for i, outer in enumerate(arr): + next = -1 + for inner in arr[i + 1 :]: + if outer < inner: + next = inner + break + result.append(next) + return result + + +def next_greatest_element(arr: list) -> list: + """ + Get the Next Greatest Element (NGE) for all elements in a list. + Maximum element present after the current one which is also greater than the + current one. + + A naive way to solve this is to take two loops and check for the next bigger + number but that will make the time complexity as O(n^2). The better way to solve + this would be to use a stack to keep track of maximum number giving a linear time + solution. + >>> next_greatest_element(arr) == expect + True + """ + stack = [] + result = [-1] * len(arr) + + for index in reversed(range(len(arr))): + if len(stack): + while stack[-1] <= arr[index]: + stack.pop() + if len(stack) == 0: + break + + if len(stack) != 0: + result[index] = stack[-1] + + stack.append(arr[index]) + + return result + + +if __name__ == "__main__": + from doctest import testmod + from timeit import timeit + + testmod() + print(next_greatest_element_slow(arr)) + print(next_greatest_element_fast(arr)) + print(next_greatest_element(arr)) + + setup = ( + "from __main__ import arr, next_greatest_element_slow, " + "next_greatest_element_fast, next_greatest_element" + ) + print( + "next_greatest_element_slow():", + timeit("next_greatest_element_slow(arr)", setup=setup), + ) + print( + "next_greatest_element_fast():", + timeit("next_greatest_element_fast(arr)", setup=setup), + ) + print( + " next_greatest_element():", + timeit("next_greatest_element(arr)", setup=setup), + ) diff --git a/data_structures/stacks/postfix_evaluation.py b/data_structures/stacks/postfix_evaluation.py index 1786e71dd383..574acac71c43 100644 --- a/data_structures/stacks/postfix_evaluation.py +++ b/data_structures/stacks/postfix_evaluation.py @@ -14,37 +14,55 @@ | pop(5) | + | push(5+54) | 59 - Result = 59 + Result = 59 """ import operator as op + def Solve(Postfix): Stack = [] - Div = lambda x, y: int(x/y) # integer division operation - Opr = {'^':op.pow, '*':op.mul, '/':Div, '+':op.add, '-':op.sub} # operators & their respective operation + Div = lambda x, y: int(x / y) # noqa: E731 integer division operation + Opr = { + "^": op.pow, + "*": op.mul, + "/": Div, + "+": op.add, + "-": op.sub, + } # operators & their respective operation # print table header - print('Symbol'.center(8), 'Action'.center(12), 'Stack', sep = " | ") - print('-'*(30+len(Postfix))) + print("Symbol".center(8), "Action".center(12), "Stack", sep=" | ") + print("-" * (30 + len(Postfix))) for x in Postfix: - if( x.isdigit() ): # if x in digit - Stack.append(x) # append x to stack - print(x.rjust(8), ('push('+x+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format + if x.isdigit(): # if x in digit + Stack.append(x) # append x to stack + # output in tabular format + print(x.rjust(8), ("push(" + x + ")").ljust(12), ",".join(Stack), sep=" | ") else: - B = Stack.pop() # pop stack - print("".rjust(8), ('pop('+B+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format + B = Stack.pop() # pop stack + # output in tabular format + print("".rjust(8), ("pop(" + B + ")").ljust(12), ",".join(Stack), sep=" | ") - A = Stack.pop() # pop stack - print("".rjust(8), ('pop('+A+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format + A = Stack.pop() # pop stack + # output in tabular format + print("".rjust(8), ("pop(" + A + ")").ljust(12), ",".join(Stack), sep=" | ") - Stack.append( str(Opr[x](int(A), int(B))) ) # evaluate the 2 values poped from stack & push result to stack - print(x.rjust(8), ('push('+A+x+B+')').ljust(12), ','.join(Stack), sep = " | ") # output in tabular format + Stack.append( + str(Opr[x](int(A), int(B))) + ) # evaluate the 2 values popped from stack & push result to stack + # output in tabular format + print( + x.rjust(8), + ("push(" + A + x + B + ")").ljust(12), + ",".join(Stack), + sep=" | ", + ) return int(Stack[0]) if __name__ == "__main__": - Postfix = input("\n\nEnter a Postfix Equation (space separated) = ").split(' ') + Postfix = input("\n\nEnter a Postfix Equation (space separated) = ").split(" ") print("\n\tResult = ", Solve(Postfix)) diff --git a/data_structures/stacks/prefix_evaluation.py b/data_structures/stacks/prefix_evaluation.py new file mode 100644 index 000000000000..00df2c1e63b0 --- /dev/null +++ b/data_structures/stacks/prefix_evaluation.py @@ -0,0 +1,60 @@ +""" +Python3 program to evaluate a prefix expression. +""" + +calc = { + "+": lambda x, y: x + y, + "-": lambda x, y: x - y, + "*": lambda x, y: x * y, + "/": lambda x, y: x / y, +} + + +def is_operand(c): + """ + Return True if the given char c is an operand, e.g. it is a number + + >>> is_operand("1") + True + >>> is_operand("+") + False + """ + return c.isdigit() + + +def evaluate(expression): + """ + Evaluate a given expression in prefix notation. + Asserts that the given expression is valid. + + >>> evaluate("+ 9 * 2 6") + 21 + >>> evaluate("/ * 10 2 + 4 1 ") + 4.0 + """ + stack = [] + + # iterate over the string in reverse order + for c in expression.split()[::-1]: + + # push operand to stack + if is_operand(c): + stack.append(int(c)) + + else: + # pop values from stack can calculate the result + # push the result onto the stack again + o1 = stack.pop() + o2 = stack.pop() + stack.append(calc[c](o1, o2)) + + return stack.pop() + + +# Driver code +if __name__ == "__main__": + test_expression = "+ 9 * 2 6" + print(evaluate(test_expression)) + + test_expression = "/ * 10 2 + 4 1 " + print(evaluate(test_expression)) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 7f979d927d08..840cde099d38 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -1,25 +1,24 @@ -from __future__ import print_function -__author__ = 'Omkar Pathak' +class StackOverflowError(BaseException): + pass -class Stack(object): - """ A stack is an abstract data type that serves as a collection of +class Stack: + """A stack is an abstract data type that serves as a collection of elements with two principal operations: push() and pop(). push() adds an element to the top of the stack, and pop() removes an element from the top of a stack. The order in which elements come off of a stack are Last In, First Out (LIFO). - https://en.wikipedia.org/wiki/Stack_(abstract_data_type) """ - def __init__(self, limit=10): + def __init__(self, limit: int = 10): self.stack = [] self.limit = limit - def __bool__(self): + def __bool__(self) -> bool: return bool(self.stack) - def __str__(self): + def __str__(self) -> str: return str(self.stack) def push(self, data): @@ -30,40 +29,76 @@ def push(self, data): def pop(self): """ Pop an element off of the top of the stack.""" - if self.stack: - return self.stack.pop() - else: - raise IndexError('pop from an empty stack') + return self.stack.pop() def peek(self): """ Peek at the top-most element of the stack.""" - if self.stack: - return self.stack[-1] + return self.stack[-1] - def is_empty(self): + def is_empty(self) -> bool: """ Check if a stack is empty.""" return not bool(self.stack) - def size(self): + def is_full(self) -> bool: + return self.size() == self.limit + + def size(self) -> int: """ Return the size of the stack.""" return len(self.stack) + def __contains__(self, item) -> bool: + """Check if item is in stack""" + return item in self.stack -class StackOverflowError(BaseException): - pass +def test_stack() -> None: + """ + >>> test_stack() + """ + stack = Stack(10) + assert bool(stack) is False + assert stack.is_empty() is True + assert stack.is_full() is False + assert str(stack) == "[]" + + try: + _ = stack.pop() + assert False # This should not happen + except IndexError: + assert True # This should happen + + try: + _ = stack.peek() + assert False # This should not happen + except IndexError: + assert True # This should happen -if __name__ == '__main__': - stack = Stack() for i in range(10): + assert stack.size() == i stack.push(i) - print('Stack demonstration:\n') - print('Initial stack: ' + str(stack)) - print('pop(): ' + str(stack.pop())) - print('After pop(), the stack is now: ' + str(stack)) - print('peek(): ' + str(stack.peek())) + assert bool(stack) is True + assert stack.is_empty() is False + assert stack.is_full() is True + assert str(stack) == str(list(range(10))) + assert stack.pop() == 9 + assert stack.peek() == 8 + stack.push(100) - print('After push(100), the stack is now: ' + str(stack)) - print('is_empty(): ' + str(stack.is_empty())) - print('size(): ' + str(stack.size())) + assert str(stack) == str([0, 1, 2, 3, 4, 5, 6, 7, 8, 100]) + + try: + stack.push(200) + assert False # This should not happen + except StackOverflowError: + assert True # This should happen + + assert stack.is_empty() is False + assert stack.size() == 10 + + assert 5 in stack + assert 55 not in stack + + +if __name__ == "__main__": + test_stack() diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_using_dll.py new file mode 100644 index 000000000000..75e0cd20640d --- /dev/null +++ b/data_structures/stacks/stack_using_dll.py @@ -0,0 +1,123 @@ +# A complete working Python program to demonstrate all +# stack operations using a doubly linked list + + +class Node: + def __init__(self, data): + self.data = data # Assign data + self.next = None # Initialize next as null + self.prev = None # Initialize prev as null + + +class Stack: + """ + >>> stack = Stack() + >>> stack.is_empty() + True + >>> stack.print_stack() + stack elements are: + >>> for i in range(4): + ... stack.push(i) + ... + >>> stack.is_empty() + False + >>> stack.print_stack() + stack elements are: + 3->2->1->0-> + >>> stack.top() + 3 + >>> len(stack) + 4 + >>> stack.pop() + 3 + >>> stack.print_stack() + stack elements are: + 2->1->0-> + """ + + def __init__(self): + self.head = None + + def push(self, data): + """add a Node to the stack""" + if self.head is None: + self.head = Node(data) + else: + new_node = Node(data) + self.head.prev = new_node + new_node.next = self.head + new_node.prev = None + self.head = new_node + + def pop(self): + """pop the top element off the stack""" + if self.head is None: + return None + else: + temp = self.head.data + self.head = self.head.next + self.head.prev = None + return temp + + def top(self): + """return the top element of the stack""" + return self.head.data + + def __len__(self): + temp = self.head + count = 0 + while temp is not None: + count += 1 + temp = temp.next + return count + + def is_empty(self): + return self.head is None + + def print_stack(self): + print("stack elements are:") + temp = self.head + while temp is not None: + print(temp.data, end="->") + temp = temp.next + + +# Code execution starts here +if __name__ == "__main__": + + # Start with the empty stack + stack = Stack() + + # Insert 4 at the beginning. So stack becomes 4->None + print("Stack operations using Doubly LinkedList") + stack.push(4) + + # Insert 5 at the beginning. So stack becomes 4->5->None + stack.push(5) + + # Insert 6 at the beginning. So stack becomes 4->5->6->None + stack.push(6) + + # Insert 7 at the beginning. So stack becomes 4->5->6->7->None + stack.push(7) + + # Print the stack + stack.print_stack() + + # Print the top element + print("\nTop element is ", stack.top()) + + # Print the stack size + print("Size of the stack is ", len(stack)) + + # pop the top element + stack.pop() + + # pop the top element + stack.pop() + + # two elements have now been popped off + stack.print_stack() + + # Print True if the stack is empty else False + print("\nstack is empty:", stack.is_empty()) diff --git a/data_structures/stacks/stock_span_problem.py b/data_structures/stacks/stock_span_problem.py index 9628864edd10..cc2adfdd6c21 100644 --- a/data_structures/stacks/stock_span_problem.py +++ b/data_structures/stacks/stock_span_problem.py @@ -1,52 +1,53 @@ -''' -The stock span problem is a financial problem where we have a series of n daily +""" +The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock's price for all n days. -The span Si of the stock's price on a given day i is defined as the maximum -number of consecutive days just before the given day, for which the price of the stock +The span Si of the stock's price on a given day i is defined as the maximum +number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day. -''' -from __future__ import print_function -def calculateSpan(price, S): - - n = len(price) - # Create a stack and push index of fist element to it - st = [] - st.append(0) - - # Span value of first element is always 1 - S[0] = 1 - - # Calculate span values for rest of the elements - for i in range(1, n): - - # Pop elements from stack whlie stack is not - # empty and top of stack is smaller than price[i] - while( len(st) > 0 and price[st[0]] <= price[i]): - st.pop() - - # If stack becomes empty, then price[i] is greater - # than all elements on left of it, i.e. price[0], - # price[1], ..price[i-1]. Else the price[i] is - # greater than elements after top of stack - S[i] = i+1 if len(st) <= 0 else (i - st[0]) - - # Push this element to stack - st.append(i) - - -# A utility function to print elements of array -def printArray(arr, n): - for i in range(0,n): - print (arr[i],end =" ") - - -# Driver program to test above function -price = [10, 4, 5, 90, 120, 80] -S = [0 for i in range(len(price)+1)] - -# Fill the span values in array S[] -calculateSpan(price, S) - -# Print the calculated span values -printArray(S, len(price)) +""" + + +def calculateSpan(price, S): + + n = len(price) + # Create a stack and push index of fist element to it + st = [] + st.append(0) + + # Span value of first element is always 1 + S[0] = 1 + + # Calculate span values for rest of the elements + for i in range(1, n): + + # Pop elements from stack while stack is not + # empty and top of stack is smaller than price[i] + while len(st) > 0 and price[st[0]] <= price[i]: + st.pop() + + # If stack becomes empty, then price[i] is greater + # than all elements on left of it, i.e. price[0], + # price[1], ..price[i-1]. Else the price[i] is + # greater than elements after top of stack + S[i] = i + 1 if len(st) <= 0 else (i - st[0]) + + # Push this element to stack + st.append(i) + + +# A utility function to print elements of array +def printArray(arr, n): + for i in range(0, n): + print(arr[i], end=" ") + + +# Driver program to test above function +price = [10, 4, 5, 90, 120, 80] +S = [0 for i in range(len(price) + 1)] + +# Fill the span values in array S[] +calculateSpan(price, S) + +# Print the calculated span values +printArray(S, len(price)) diff --git a/data_structures/trie/__init__.py b/data_structures/trie/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/data_structures/trie/trie.py b/data_structures/trie/trie.py index b6234c6704c6..6582be24fd0c 100644 --- a/data_structures/trie/trie.py +++ b/data_structures/trie/trie.py @@ -1,9 +1,8 @@ """ A Trie/Prefix Tree is a kind of search tree used to provide quick lookup of words/patterns in a set of words. A basic Trie however has O(n^2) space complexity -making it impractical in practice. It however provides O(max(search_string, length of longest word)) lookup -time making it an optimal approach when space is not an issue. - +making it impractical in practice. It however provides O(max(search_string, length of +longest word)) lookup time making it an optimal approach when space is not an issue. """ @@ -12,7 +11,7 @@ def __init__(self): self.nodes = dict() # Mapping from char to TrieNode self.is_leaf = False - def insert_many(self, words: [str]): # noqa: E999 This syntax is Python 3 only + def insert_many(self, words: [str]): """ Inserts a list of words into the Trie :param words: list of string words @@ -21,7 +20,7 @@ def insert_many(self, words: [str]): # noqa: E999 This syntax is Python 3 only for word in words: self.insert(word) - def insert(self, word: str): # noqa: E999 This syntax is Python 3 only + def insert(self, word: str): """ Inserts a word into the Trie :param word: word to be inserted @@ -34,7 +33,7 @@ def insert(self, word: str): # noqa: E999 This syntax is Python 3 only curr = curr.nodes[char] curr.is_leaf = True - def find(self, word: str) -> bool: # noqa: E999 This syntax is Python 3 only + def find(self, word: str) -> bool: """ Tries to find word in a Trie :param word: word to look for @@ -47,8 +46,36 @@ def find(self, word: str) -> bool: # noqa: E999 This syntax is Python 3 only curr = curr.nodes[char] return curr.is_leaf + def delete(self, word: str): + """ + Deletes a word in a Trie + :param word: word to delete + :return: None + """ -def print_words(node: TrieNode, word: str): # noqa: E999 This syntax is Python 3 only + def _delete(curr: TrieNode, word: str, index: int): + if index == len(word): + # If word does not exist + if not curr.is_leaf: + return False + curr.is_leaf = False + return len(curr.nodes) == 0 + char = word[index] + char_node = curr.nodes.get(char) + # If char not in current trie node + if not char_node: + return False + # Flag to check if node can be deleted + delete_curr = _delete(char_node, word, index + 1) + if delete_curr: + del curr.nodes[char] + return len(curr.nodes) == 0 + return delete_curr + + _delete(self, word, 0) + + +def print_words(node: TrieNode, word: str): """ Prints all the words in a Trie :param node: root node of Trie @@ -56,20 +83,45 @@ def print_words(node: TrieNode, word: str): # noqa: E999 This syntax is Python :return: None """ if node.is_leaf: - print(word, end=' ') + print(word, end=" ") for key, value in node.nodes.items(): print_words(value, word + key) -def test(): - words = ['banana', 'bananas', 'bandana', 'band', 'apple', 'all', 'beast'] +def test_trie(): + words = "banana bananas bandana band apple all beast".split() root = TrieNode() root.insert_many(words) - # print_words(root, '') - assert root.find('banana') - assert not root.find('bandanas') - assert not root.find('apps') - assert root.find('apple') + # print_words(root, "") + assert all(root.find(word) for word in words) + assert root.find("banana") + assert not root.find("bandanas") + assert not root.find("apps") + assert root.find("apple") + assert root.find("all") + root.delete("all") + assert not root.find("all") + root.delete("banana") + assert not root.find("banana") + assert root.find("bananas") + return True + + +def print_results(msg: str, passes: bool) -> None: + print(str(msg), "works!" if passes else "doesn't work :(") + + +def pytests(): + assert test_trie() + + +def main(): + """ + >>> pytests() + """ + print_results("Testing trie functionality", test_trie()) + -test() +if __name__ == "__main__": + main() diff --git a/data_structures/union_find/tests_union_find.py b/data_structures/union_find/tests_union_find.py deleted file mode 100644 index b0708778ddbd..000000000000 --- a/data_structures/union_find/tests_union_find.py +++ /dev/null @@ -1,78 +0,0 @@ -from __future__ import absolute_import -from .union_find import UnionFind -import unittest - - -class TestUnionFind(unittest.TestCase): - def test_init_with_valid_size(self): - uf = UnionFind(5) - self.assertEqual(uf.size, 5) - - def test_init_with_invalid_size(self): - with self.assertRaises(ValueError): - uf = UnionFind(0) - - with self.assertRaises(ValueError): - uf = UnionFind(-5) - - def test_union_with_valid_values(self): - uf = UnionFind(10) - - for i in range(11): - for j in range(11): - uf.union(i, j) - - def test_union_with_invalid_values(self): - uf = UnionFind(10) - - with self.assertRaises(ValueError): - uf.union(-1, 1) - - with self.assertRaises(ValueError): - uf.union(11, 1) - - def test_same_set_with_valid_values(self): - uf = UnionFind(10) - - for i in range(11): - for j in range(11): - if i == j: - self.assertTrue(uf.same_set(i, j)) - else: - self.assertFalse(uf.same_set(i, j)) - - uf.union(1, 2) - self.assertTrue(uf.same_set(1, 2)) - - uf.union(3, 4) - self.assertTrue(uf.same_set(3, 4)) - - self.assertFalse(uf.same_set(1, 3)) - self.assertFalse(uf.same_set(1, 4)) - self.assertFalse(uf.same_set(2, 3)) - self.assertFalse(uf.same_set(2, 4)) - - uf.union(1, 3) - self.assertTrue(uf.same_set(1, 3)) - self.assertTrue(uf.same_set(1, 4)) - self.assertTrue(uf.same_set(2, 3)) - self.assertTrue(uf.same_set(2, 4)) - - uf.union(4, 10) - self.assertTrue(uf.same_set(1, 10)) - self.assertTrue(uf.same_set(2, 10)) - self.assertTrue(uf.same_set(3, 10)) - self.assertTrue(uf.same_set(4, 10)) - - def test_same_set_with_invalid_values(self): - uf = UnionFind(10) - - with self.assertRaises(ValueError): - uf.same_set(-1, 1) - - with self.assertRaises(ValueError): - uf.same_set(11, 0) - - -if __name__ == '__main__': - unittest.main() diff --git a/data_structures/union_find/union_find.py b/data_structures/union_find/union_find.py deleted file mode 100644 index 40eea67ac944..000000000000 --- a/data_structures/union_find/union_find.py +++ /dev/null @@ -1,87 +0,0 @@ -class UnionFind(): - """ - https://en.wikipedia.org/wiki/Disjoint-set_data_structure - - The union-find is a disjoint-set data structure - - You can merge two sets and tell if one set belongs to - another one. - - It's used on the Kruskal Algorithm - (https://en.wikipedia.org/wiki/Kruskal%27s_algorithm) - - The elements are in range [0, size] - """ - def __init__(self, size): - if size <= 0: - raise ValueError("size should be greater than 0") - - self.size = size - - # The below plus 1 is because we are using elements - # in range [0, size]. It makes more sense. - - # Every set begins with only itself - self.root = [i for i in range(size+1)] - - # This is used for heuristic union by rank - self.weight = [0 for i in range(size+1)] - - def union(self, u, v): - """ - Union of the sets u and v. - Complexity: log(n). - Amortized complexity: < 5 (it's very fast). - """ - - self._validate_element_range(u, "u") - self._validate_element_range(v, "v") - - if u == v: - return - - # Using union by rank will guarantee the - # log(n) complexity - rootu = self._root(u) - rootv = self._root(v) - weight_u = self.weight[rootu] - weight_v = self.weight[rootv] - if weight_u >= weight_v: - self.root[rootv] = rootu - if weight_u == weight_v: - self.weight[rootu] += 1 - else: - self.root[rootu] = rootv - - def same_set(self, u, v): - """ - Return true if the elements u and v belongs to - the same set - """ - - self._validate_element_range(u, "u") - self._validate_element_range(v, "v") - - return self._root(u) == self._root(v) - - def _root(self, u): - """ - Get the element set root. - This uses the heuristic path compression - See wikipedia article for more details. - """ - - if u != self.root[u]: - self.root[u] = self._root(self.root[u]) - - return self.root[u] - - def _validate_element_range(self, u, element_name): - """ - Raises ValueError if element is not in range - """ - if u < 0 or u > self.size: - msg = ("element {0} with value {1} " - "should be in range [0~{2}]")\ - .format(element_name, u, self.size) - raise ValueError(msg) diff --git a/digital_image_processing/change_brightness.py b/digital_image_processing/change_brightness.py new file mode 100644 index 000000000000..97493f1a399e --- /dev/null +++ b/digital_image_processing/change_brightness.py @@ -0,0 +1,26 @@ +from PIL import Image + + +def change_brightness(img: Image, level: float) -> Image: + """ + Change the brightness of a PIL Image to a given level. + """ + + def brightness(c: int) -> float: + """ + Fundamental Transformation/Operation that'll be performed on + every bit. + """ + return 128 + level + (c - 128) + + if not -255.0 <= level <= 255.0: + raise ValueError("level must be between -255.0 (black) and 255.0 (white)") + return img.point(brightness) + + +if __name__ == "__main__": + # Load image + with Image.open("image_data/lena.jpg") as img: + # Change brightness to 100 + brigt_img = change_brightness(img, 100) + brigt_img.save("image_data/lena_brightness.png", format="png") diff --git a/digital_image_processing/change_contrast.py b/digital_image_processing/change_contrast.py new file mode 100644 index 000000000000..6a150400249f --- /dev/null +++ b/digital_image_processing/change_contrast.py @@ -0,0 +1,35 @@ +""" +Changing contrast with PIL + +This algorithm is used in +https://noivce.pythonanywhere.com/ Python web app. + +python/black: True +flake8 : True +""" + +from PIL import Image + + +def change_contrast(img: Image, level: int) -> Image: + """ + Function to change contrast + """ + factor = (259 * (level + 255)) / (255 * (259 - level)) + + def contrast(c: int) -> int: + """ + Fundamental Transformation/Operation that'll be performed on + every bit. + """ + return int(128 + factor * (c - 128)) + + return img.point(contrast) + + +if __name__ == "__main__": + # Load image + with Image.open("image_data/lena.jpg") as img: + # Change contrast to 170 + cont_img = change_contrast(img, 170) + cont_img.save("image_data/lena_high_contrast.png", format="png") diff --git a/digital_image_processing/convert_to_negative.py b/digital_image_processing/convert_to_negative.py new file mode 100644 index 000000000000..7df44138973c --- /dev/null +++ b/digital_image_processing/convert_to_negative.py @@ -0,0 +1,29 @@ +""" + Implemented an algorithm using opencv to convert a colored image into its negative +""" +from cv2 import destroyAllWindows, imread, imshow, waitKey + + +def convert_to_negative(img): + # getting number of pixels in the image + pixel_h, pixel_v = img.shape[0], img.shape[1] + + # converting each pixel's color to its negative + for i in range(pixel_h): + for j in range(pixel_v): + img[i][j] = [255, 255, 255] - img[i][j] + + return img + + +if __name__ == "__main__": + # read original image + img = imread("image_data/lena.jpg", 1) + + # convert to its negative + neg = convert_to_negative(img) + + # show result image + imshow("negative of original image", img) + waitKey(0) + destroyAllWindows() diff --git a/digital_image_processing/dithering/__init__.py b/digital_image_processing/dithering/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/dithering/burkes.py b/digital_image_processing/dithering/burkes.py new file mode 100644 index 000000000000..2bf0bbe03225 --- /dev/null +++ b/digital_image_processing/dithering/burkes.py @@ -0,0 +1,87 @@ +""" +Implementation Burke's algorithm (dithering) +""" +import numpy as np +from cv2 import destroyAllWindows, imread, imshow, waitKey + + +class Burkes: + """ + Burke's algorithm is using for converting grayscale image to black and white version + Source: Source: https://en.wikipedia.org/wiki/Dither + + Note: + * Best results are given with threshold= ~1/2 * max greyscale value. + * This implementation get RGB image and converts it to greyscale in runtime. + """ + + def __init__(self, input_img, threshold: int): + self.min_threshold = 0 + # max greyscale value for #FFFFFF + self.max_threshold = int(self.get_greyscale(255, 255, 255)) + + if not self.min_threshold < threshold < self.max_threshold: + raise ValueError(f"Factor value should be from 0 to {self.max_threshold}") + + self.input_img = input_img + self.threshold = threshold + self.width, self.height = self.input_img.shape[1], self.input_img.shape[0] + + # error table size (+4 columns and +1 row) greater than input image because of + # lack of if statements + self.error_table = [ + [0 for _ in range(self.height + 4)] for __ in range(self.width + 1) + ] + self.output_img = np.ones((self.width, self.height, 3), np.uint8) * 255 + + @classmethod + def get_greyscale(cls, blue: int, green: int, red: int) -> float: + """ + >>> Burkes.get_greyscale(3, 4, 5) + 3.753 + """ + return 0.114 * blue + 0.587 * green + 0.2126 * red + + def process(self) -> None: + for y in range(self.height): + for x in range(self.width): + greyscale = int(self.get_greyscale(*self.input_img[y][x])) + if self.threshold > greyscale + self.error_table[y][x]: + self.output_img[y][x] = (0, 0, 0) + current_error = greyscale + self.error_table[x][y] + else: + self.output_img[y][x] = (255, 255, 255) + current_error = greyscale + self.error_table[x][y] - 255 + """ + Burkes error propagation (`*` is current pixel): + + * 8/32 4/32 + 2/32 4/32 8/32 4/32 2/32 + """ + self.error_table[y][x + 1] += int(8 / 32 * current_error) + self.error_table[y][x + 2] += int(4 / 32 * current_error) + self.error_table[y + 1][x] += int(8 / 32 * current_error) + self.error_table[y + 1][x + 1] += int(4 / 32 * current_error) + self.error_table[y + 1][x + 2] += int(2 / 32 * current_error) + self.error_table[y + 1][x - 1] += int(4 / 32 * current_error) + self.error_table[y + 1][x - 2] += int(2 / 32 * current_error) + + +if __name__ == "__main__": + # create Burke's instances with original images in greyscale + burkes_instances = [ + Burkes(imread("image_data/lena.jpg", 1), threshold) + for threshold in (1, 126, 130, 140) + ] + + for burkes in burkes_instances: + burkes.process() + + for burkes in burkes_instances: + imshow( + f"Original image with dithering threshold: {burkes.threshold}", + burkes.output_img, + ) + + waitKey(0) + destroyAllWindows() diff --git a/digital_image_processing/edge_detection/__init__.py b/digital_image_processing/edge_detection/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/edge_detection/canny.py b/digital_image_processing/edge_detection/canny.py new file mode 100644 index 000000000000..295b4d825c12 --- /dev/null +++ b/digital_image_processing/edge_detection/canny.py @@ -0,0 +1,122 @@ +import cv2 +import numpy as np + +from digital_image_processing.filters.convolve import img_convolve +from digital_image_processing.filters.sobel_filter import sobel_filter + +PI = 180 + + +def gen_gaussian_kernel(k_size, sigma): + center = k_size // 2 + x, y = np.mgrid[0 - center : k_size - center, 0 - center : k_size - center] + g = ( + 1 + / (2 * np.pi * sigma) + * np.exp(-(np.square(x) + np.square(y)) / (2 * np.square(sigma))) + ) + return g + + +def canny(image, threshold_low=15, threshold_high=30, weak=128, strong=255): + image_row, image_col = image.shape[0], image.shape[1] + # gaussian_filter + gaussian_out = img_convolve(image, gen_gaussian_kernel(9, sigma=1.4)) + # get the gradient and degree by sobel_filter + sobel_grad, sobel_theta = sobel_filter(gaussian_out) + gradient_direction = np.rad2deg(sobel_theta) + gradient_direction += PI + + dst = np.zeros((image_row, image_col)) + + """ + Non-maximum suppression. If the edge strength of the current pixel is the largest + compared to the other pixels in the mask with the same direction, the value will be + preserved. Otherwise, the value will be suppressed. + """ + for row in range(1, image_row - 1): + for col in range(1, image_col - 1): + direction = gradient_direction[row, col] + + if ( + 0 <= direction < 22.5 + or 15 * PI / 8 <= direction <= 2 * PI + or 7 * PI / 8 <= direction <= 9 * PI / 8 + ): + W = sobel_grad[row, col - 1] + E = sobel_grad[row, col + 1] + if sobel_grad[row, col] >= W and sobel_grad[row, col] >= E: + dst[row, col] = sobel_grad[row, col] + + elif (PI / 8 <= direction < 3 * PI / 8) or ( + 9 * PI / 8 <= direction < 11 * PI / 8 + ): + SW = sobel_grad[row + 1, col - 1] + NE = sobel_grad[row - 1, col + 1] + if sobel_grad[row, col] >= SW and sobel_grad[row, col] >= NE: + dst[row, col] = sobel_grad[row, col] + + elif (3 * PI / 8 <= direction < 5 * PI / 8) or ( + 11 * PI / 8 <= direction < 13 * PI / 8 + ): + N = sobel_grad[row - 1, col] + S = sobel_grad[row + 1, col] + if sobel_grad[row, col] >= N and sobel_grad[row, col] >= S: + dst[row, col] = sobel_grad[row, col] + + elif (5 * PI / 8 <= direction < 7 * PI / 8) or ( + 13 * PI / 8 <= direction < 15 * PI / 8 + ): + NW = sobel_grad[row - 1, col - 1] + SE = sobel_grad[row + 1, col + 1] + if sobel_grad[row, col] >= NW and sobel_grad[row, col] >= SE: + dst[row, col] = sobel_grad[row, col] + + """ + High-Low threshold detection. If an edge pixel’s gradient value is higher + than the high threshold value, it is marked as a strong edge pixel. If an + edge pixel’s gradient value is smaller than the high threshold value and + larger than the low threshold value, it is marked as a weak edge pixel. If + an edge pixel's value is smaller than the low threshold value, it will be + suppressed. + """ + if dst[row, col] >= threshold_high: + dst[row, col] = strong + elif dst[row, col] <= threshold_low: + dst[row, col] = 0 + else: + dst[row, col] = weak + + """ + Edge tracking. Usually a weak edge pixel caused from true edges will be connected + to a strong edge pixel while noise responses are unconnected. As long as there is + one strong edge pixel that is involved in its 8-connected neighborhood, that weak + edge point can be identified as one that should be preserved. + """ + for row in range(1, image_row): + for col in range(1, image_col): + if dst[row, col] == weak: + if 255 in ( + dst[row, col + 1], + dst[row, col - 1], + dst[row - 1, col], + dst[row + 1, col], + dst[row - 1, col - 1], + dst[row + 1, col - 1], + dst[row - 1, col + 1], + dst[row + 1, col + 1], + ): + dst[row, col] = strong + else: + dst[row, col] = 0 + + return dst + + +if __name__ == "__main__": + # read original image in gray mode + lena = cv2.imread(r"../image_data/lena.jpg", 0) + # canny edge detection + canny_dst = canny(lena) + cv2.imshow("canny", canny_dst) + cv2.waitKey(0) diff --git a/digital_image_processing/filters/bilateral_filter.py b/digital_image_processing/filters/bilateral_filter.py new file mode 100644 index 000000000000..76ae4dd20345 --- /dev/null +++ b/digital_image_processing/filters/bilateral_filter.py @@ -0,0 +1,88 @@ +""" +Implementation of Bilateral filter + +Inputs: + img: A 2d image with values in between 0 and 1 + varS: variance in space dimension. + varI: variance in Intensity. + N: Kernel size(Must be an odd number) +Output: + img:A 2d zero padded image with values in between 0 and 1 +""" +import math +import sys + +import cv2 +import numpy as np + + +def vec_gaussian(img: np.ndarray, variance: float) -> np.ndarray: + # For applying gaussian function for each element in matrix. + sigma = math.sqrt(variance) + cons = 1 / (sigma * math.sqrt(2 * math.pi)) + return cons * np.exp(-((img / sigma) ** 2) * 0.5) + + +def get_slice(img: np.ndarray, x: int, y: int, kernel_size: int) -> np.ndarray: + half = kernel_size // 2 + return img[x - half : x + half + 1, y - half : y + half + 1] + + +def get_gauss_kernel(kernel_size: int, spatial_variance: float) -> np.ndarray: + # Creates a gaussian kernel of given dimension. + arr = np.zeros((kernel_size, kernel_size)) + for i in range(0, kernel_size): + for j in range(0, kernel_size): + arr[i, j] = math.sqrt( + abs(i - kernel_size // 2) ** 2 + abs(j - kernel_size // 2) ** 2 + ) + return vec_gaussian(arr, spatial_variance) + + +def bilateral_filter( + img: np.ndarray, + spatial_variance: float, + intensity_variance: float, + kernel_size: int, +) -> np.ndarray: + img2 = np.zeros(img.shape) + gaussKer = get_gauss_kernel(kernel_size, spatial_variance) + sizeX, sizeY = img.shape + for i in range(kernel_size // 2, sizeX - kernel_size // 2): + for j in range(kernel_size // 2, sizeY - kernel_size // 2): + + imgS = get_slice(img, i, j, kernel_size) + imgI = imgS - imgS[kernel_size // 2, kernel_size // 2] + imgIG = vec_gaussian(imgI, intensity_variance) + weights = np.multiply(gaussKer, imgIG) + vals = np.multiply(imgS, weights) + val = np.sum(vals) / np.sum(weights) + img2[i, j] = val + return img2 + + +def parse_args(args: list) -> tuple: + filename = args[1] if args[1:] else "../image_data/lena.jpg" + spatial_variance = float(args[2]) if args[2:] else 1.0 + intensity_variance = float(args[3]) if args[3:] else 1.0 + if args[4:]: + kernel_size = int(args[4]) + kernel_size = kernel_size + abs(kernel_size % 2 - 1) + else: + kernel_size = 5 + return filename, spatial_variance, intensity_variance, kernel_size + + +if __name__ == "__main__": + filename, spatial_variance, intensity_variance, kernel_size = parse_args(sys.argv) + img = cv2.imread(filename, 0) + cv2.imshow("input image", img) + + out = img / 255 + out = out.astype("float32") + out = bilateral_filter(out, spatial_variance, intensity_variance, kernel_size) + out = out * 255 + out = np.uint8(out) + cv2.imshow("output image", out) + cv2.waitKey(0) + cv2.destroyAllWindows() diff --git a/digital_image_processing/filters/convolve.py b/digital_image_processing/filters/convolve.py new file mode 100644 index 000000000000..299682010da6 --- /dev/null +++ b/digital_image_processing/filters/convolve.py @@ -0,0 +1,49 @@ +# @Author : lightXu +# @File : convolve.py +# @Time : 2019/7/8 0008 下午 16:13 +from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey +from numpy import array, dot, pad, ravel, uint8, zeros + + +def im2col(image, block_size): + rows, cols = image.shape + dst_height = cols - block_size[1] + 1 + dst_width = rows - block_size[0] + 1 + image_array = zeros((dst_height * dst_width, block_size[1] * block_size[0])) + row = 0 + for i in range(0, dst_height): + for j in range(0, dst_width): + window = ravel(image[i : i + block_size[0], j : j + block_size[1]]) + image_array[row, :] = window + row += 1 + + return image_array + + +def img_convolve(image, filter_kernel): + height, width = image.shape[0], image.shape[1] + k_size = filter_kernel.shape[0] + pad_size = k_size // 2 + # Pads image with the edge values of array. + image_tmp = pad(image, pad_size, mode="edge") + + # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows + image_array = im2col(image_tmp, (k_size, k_size)) + + # turn the kernel into shape(k*k, 1) + kernel_array = ravel(filter_kernel) + # reshape and get the dst image + dst = dot(image_array, kernel_array).reshape(height, width) + return dst + + +if __name__ == "__main__": + # read original image + img = imread(r"../image_data/lena.jpg") + # turn image in gray scale value + gray = cvtColor(img, COLOR_BGR2GRAY) + # Laplace operator + Laplace_kernel = array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) + out = img_convolve(gray, Laplace_kernel).astype(uint8) + imshow("Laplacian", out) + waitKey(0) diff --git a/digital_image_processing/filters/gaussian_filter.py b/digital_image_processing/filters/gaussian_filter.py new file mode 100644 index 000000000000..87fa67fb65ea --- /dev/null +++ b/digital_image_processing/filters/gaussian_filter.py @@ -0,0 +1,54 @@ +""" +Implementation of gaussian filter algorithm +""" +from itertools import product + +from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey +from numpy import dot, exp, mgrid, pi, ravel, square, uint8, zeros + + +def gen_gaussian_kernel(k_size, sigma): + center = k_size // 2 + x, y = mgrid[0 - center : k_size - center, 0 - center : k_size - center] + g = 1 / (2 * pi * sigma) * exp(-(square(x) + square(y)) / (2 * square(sigma))) + return g + + +def gaussian_filter(image, k_size, sigma): + height, width = image.shape[0], image.shape[1] + # dst image height and width + dst_height = height - k_size + 1 + dst_width = width - k_size + 1 + + # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows + image_array = zeros((dst_height * dst_width, k_size * k_size)) + row = 0 + for i, j in product(range(dst_height), range(dst_width)): + window = ravel(image[i : i + k_size, j : j + k_size]) + image_array[row, :] = window + row += 1 + + # turn the kernel into shape(k*k, 1) + gaussian_kernel = gen_gaussian_kernel(k_size, sigma) + filter_array = ravel(gaussian_kernel) + + # reshape and get the dst image + dst = dot(image_array, filter_array).reshape(dst_height, dst_width).astype(uint8) + + return dst + + +if __name__ == "__main__": + # read original image + img = imread(r"../image_data/lena.jpg") + # turn image in gray scale value + gray = cvtColor(img, COLOR_BGR2GRAY) + + # get values with two different mask size + gaussian3x3 = gaussian_filter(gray, 3, sigma=1) + gaussian5x5 = gaussian_filter(gray, 5, sigma=0.8) + + # show result images + imshow("gaussian filter with 3x3 mask", gaussian3x3) + imshow("gaussian filter with 5x5 mask", gaussian5x5) + waitKey() diff --git a/digital_image_processing/filters/median_filter.py b/digital_image_processing/filters/median_filter.py index eea4295632a1..174018569d62 100644 --- a/digital_image_processing/filters/median_filter.py +++ b/digital_image_processing/filters/median_filter.py @@ -1,9 +1,8 @@ """ Implementation of median filter algorithm """ - -from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey -from numpy import zeros_like, ravel, sort, multiply, divide, int8 +from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey +from numpy import divide, int8, multiply, ravel, sort, zeros_like def median_filter(gray_img, mask=3): @@ -15,20 +14,20 @@ def median_filter(gray_img, mask=3): # set image borders bd = int(mask / 2) # copy image size - median_img = zeros_like(gray) + median_img = zeros_like(gray_img) for i in range(bd, gray_img.shape[0] - bd): for j in range(bd, gray_img.shape[1] - bd): # get mask according with mask - kernel = ravel(gray_img[i - bd:i + bd + 1, j - bd:j + bd + 1]) + kernel = ravel(gray_img[i - bd : i + bd + 1, j - bd : j + bd + 1]) # calculate mask median median = sort(kernel)[int8(divide((multiply(mask, mask)), 2) + 1)] median_img[i, j] = median return median_img -if __name__ == '__main__': +if __name__ == "__main__": # read original image - img = imread('lena.jpg') + img = imread("../image_data/lena.jpg") # turn image in gray scale value gray = cvtColor(img, COLOR_BGR2GRAY) @@ -37,6 +36,6 @@ def median_filter(gray_img, mask=3): median5x5 = median_filter(gray, 5) # show result images - imshow('median filter with 3x3 mask', median3x3) - imshow('median filter with 5x5 mask', median5x5) + imshow("median filter with 3x3 mask", median3x3) + imshow("median filter with 5x5 mask", median5x5) waitKey(0) diff --git a/digital_image_processing/filters/sobel_filter.py b/digital_image_processing/filters/sobel_filter.py new file mode 100644 index 000000000000..33284a32f424 --- /dev/null +++ b/digital_image_processing/filters/sobel_filter.py @@ -0,0 +1,39 @@ +# @Author : lightXu +# @File : sobel_filter.py +# @Time : 2019/7/8 0008 下午 16:26 +import numpy as np +from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey + +from digital_image_processing.filters.convolve import img_convolve + + +def sobel_filter(image): + kernel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) + kernel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) + + dst_x = np.abs(img_convolve(image, kernel_x)) + dst_y = np.abs(img_convolve(image, kernel_y)) + # modify the pix within [0, 255] + dst_x = dst_x * 255 / np.max(dst_x) + dst_y = dst_y * 255 / np.max(dst_y) + + dst_xy = np.sqrt((np.square(dst_x)) + (np.square(dst_y))) + dst_xy = dst_xy * 255 / np.max(dst_xy) + dst = dst_xy.astype(np.uint8) + + theta = np.arctan2(dst_y, dst_x) + return dst, theta + + +if __name__ == "__main__": + # read original image + img = imread("../image_data/lena.jpg") + # turn image in gray scale value + gray = cvtColor(img, COLOR_BGR2GRAY) + + sobel_grad, sobel_theta = sobel_filter(gray) + + # show result images + imshow("sobel filter", sobel_grad) + imshow("sobel theta", sobel_theta) + waitKey(0) diff --git a/digital_image_processing/histogram_equalization/__init__.py b/digital_image_processing/histogram_equalization/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/histogram_equalization/histogram_stretch.py b/digital_image_processing/histogram_equalization/histogram_stretch.py new file mode 100644 index 000000000000..0288a2c1fcf5 --- /dev/null +++ b/digital_image_processing/histogram_equalization/histogram_stretch.py @@ -0,0 +1,63 @@ +""" +Created on Fri Sep 28 15:22:29 2018 + +@author: Binish125 +""" +import copy +import os + +import cv2 +import numpy as np +from matplotlib import pyplot as plt + + +class contrastStretch: + def __init__(self): + self.img = "" + self.original_image = "" + self.last_list = [] + self.rem = 0 + self.L = 256 + self.sk = 0 + self.k = 0 + self.number_of_rows = 0 + self.number_of_cols = 0 + + def stretch(self, input_image): + self.img = cv2.imread(input_image, 0) + self.original_image = copy.deepcopy(self.img) + x, _, _ = plt.hist(self.img.ravel(), 256, [0, 256], label="x") + self.k = np.sum(x) + for i in range(len(x)): + prk = x[i] / self.k + self.sk += prk + last = (self.L - 1) * self.sk + if self.rem != 0: + self.rem = int(last % last) + last = int(last + 1 if self.rem >= 0.5 else last) + self.last_list.append(last) + self.number_of_rows = int(np.ma.count(self.img) / self.img[1].size) + self.number_of_cols = self.img[1].size + for i in range(self.number_of_cols): + for j in range(self.number_of_rows): + num = self.img[j][i] + if num != self.last_list[num]: + self.img[j][i] = self.last_list[num] + cv2.imwrite("output_data/output.jpg", self.img) + + def plotHistogram(self): + plt.hist(self.img.ravel(), 256, [0, 256]) + + def showImage(self): + cv2.imshow("Output-Image", self.img) + cv2.imshow("Input-Image", self.original_image) + cv2.waitKey(5000) + cv2.destroyAllWindows() + + +if __name__ == "__main__": + file_path = os.path.join(os.path.basename(__file__), "image_data/input.jpg") + stretcher = contrastStretch() + stretcher.stretch(file_path) + stretcher.plotHistogram() + stretcher.showImage() diff --git a/digital_image_processing/histogram_equalization/image_data/__init__.py b/digital_image_processing/histogram_equalization/image_data/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/histogram_equalization/image_data/input.jpg b/digital_image_processing/histogram_equalization/image_data/input.jpg new file mode 100644 index 000000000000..483da6fd97f4 Binary files /dev/null and b/digital_image_processing/histogram_equalization/image_data/input.jpg differ diff --git a/digital_image_processing/histogram_equalization/output_data/__init__.py b/digital_image_processing/histogram_equalization/output_data/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/histogram_equalization/output_data/output.jpg b/digital_image_processing/histogram_equalization/output_data/output.jpg new file mode 100644 index 000000000000..48c70d8ae17d Binary files /dev/null and b/digital_image_processing/histogram_equalization/output_data/output.jpg differ diff --git a/digital_image_processing/image_data/__init__.py b/digital_image_processing/image_data/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/image_data/lena.jpg b/digital_image_processing/image_data/lena.jpg new file mode 100644 index 000000000000..15c4d9764eff Binary files /dev/null and b/digital_image_processing/image_data/lena.jpg differ diff --git a/digital_image_processing/image_data/lena_small.jpg b/digital_image_processing/image_data/lena_small.jpg new file mode 100644 index 000000000000..b85144e9f65c Binary files /dev/null and b/digital_image_processing/image_data/lena_small.jpg differ diff --git a/digital_image_processing/index_calculation.py b/digital_image_processing/index_calculation.py new file mode 100644 index 000000000000..4350b8603390 --- /dev/null +++ b/digital_image_processing/index_calculation.py @@ -0,0 +1,576 @@ +# Author: João Gustavo A. Amorim +# Author email: joaogustavoamorim@gmail.com +# Coding date: jan 2019 +# python/black: True + +# Imports +import numpy as np + + +# Class implemented to calculus the index +class IndexCalculation: + """ + # Class Summary + This algorithm consists in calculating vegetation indices, these + indices can be used for precision agriculture for example (or remote + sensing). There are functions to define the data and to calculate the + implemented indices. + + # Vegetation index + https://en.wikipedia.org/wiki/Vegetation_Index + A Vegetation Index (VI) is a spectral transformation of two or more bands + designed to enhance the contribution of vegetation properties and allow + reliable spatial and temporal inter-comparisons of terrestrial + photosynthetic activity and canopy structural variations + + # Information about channels (Wavelength range for each) + * nir - near-infrared + https://www.malvernpanalytical.com/br/products/technology/near-infrared-spectroscopy + Wavelength Range 700 nm to 2500 nm + * Red Edge + https://en.wikipedia.org/wiki/Red_edge + Wavelength Range 680 nm to 730 nm + * red + https://en.wikipedia.org/wiki/Color + Wavelength Range 635 nm to 700 nm + * blue + https://en.wikipedia.org/wiki/Color + Wavelength Range 450 nm to 490 nm + * green + https://en.wikipedia.org/wiki/Color + Wavelength Range 520 nm to 560 nm + + + # Implemented index list + #"abbreviationOfIndexName" -- list of channels used + + #"ARVI2" -- red, nir + #"CCCI" -- red, redEdge, nir + #"CVI" -- red, green, nir + #"GLI" -- red, green, blue + #"NDVI" -- red, nir + #"BNDVI" -- blue, nir + #"redEdgeNDVI" -- red, redEdge + #"GNDVI" -- green, nir + #"GBNDVI" -- green, blue, nir + #"GRNDVI" -- red, green, nir + #"RBNDVI" -- red, blue, nir + #"PNDVI" -- red, green, blue, nir + #"ATSAVI" -- red, nir + #"BWDRVI" -- blue, nir + #"CIgreen" -- green, nir + #"CIrededge" -- redEdge, nir + #"CI" -- red, blue + #"CTVI" -- red, nir + #"GDVI" -- green, nir + #"EVI" -- red, blue, nir + #"GEMI" -- red, nir + #"GOSAVI" -- green, nir + #"GSAVI" -- green, nir + #"Hue" -- red, green, blue + #"IVI" -- red, nir + #"IPVI" -- red, nir + #"I" -- red, green, blue + #"RVI" -- red, nir + #"MRVI" -- red, nir + #"MSAVI" -- red, nir + #"NormG" -- red, green, nir + #"NormNIR" -- red, green, nir + #"NormR" -- red, green, nir + #"NGRDI" -- red, green + #"RI" -- red, green + #"S" -- red, green, blue + #"IF" -- red, green, blue + #"DVI" -- red, nir + #"TVI" -- red, nir + #"NDRE" -- redEdge, nir + + #list of all index implemented + #allIndex = ["ARVI2", "CCCI", "CVI", "GLI", "NDVI", "BNDVI", "redEdgeNDVI", + "GNDVI", "GBNDVI", "GRNDVI", "RBNDVI", "PNDVI", "ATSAVI", + "BWDRVI", "CIgreen", "CIrededge", "CI", "CTVI", "GDVI", "EVI", + "GEMI", "GOSAVI", "GSAVI", "Hue", "IVI", "IPVI", "I", "RVI", + "MRVI", "MSAVI", "NormG", "NormNIR", "NormR", "NGRDI", "RI", + "S", "IF", "DVI", "TVI", "NDRE"] + + #list of index with not blue channel + #notBlueIndex = ["ARVI2", "CCCI", "CVI", "NDVI", "redEdgeNDVI", "GNDVI", + "GRNDVI", "ATSAVI", "CIgreen", "CIrededge", "CTVI", "GDVI", + "GEMI", "GOSAVI", "GSAVI", "IVI", "IPVI", "RVI", "MRVI", + "MSAVI", "NormG", "NormNIR", "NormR", "NGRDI", "RI", "DVI", + "TVI", "NDRE"] + + #list of index just with RGB channels + #RGBIndex = ["GLI", "CI", "Hue", "I", "NGRDI", "RI", "S", "IF"] + """ + + def __init__(self, red=None, green=None, blue=None, redEdge=None, nir=None): + # print("Numpy version: " + np.__version__) + self.setMatrices(red=red, green=green, blue=blue, redEdge=redEdge, nir=nir) + + def setMatrices(self, red=None, green=None, blue=None, redEdge=None, nir=None): + if red is not None: + self.red = red + if green is not None: + self.green = green + if blue is not None: + self.blue = blue + if redEdge is not None: + self.redEdge = redEdge + if nir is not None: + self.nir = nir + return True + + def calculation( + self, index="", red=None, green=None, blue=None, redEdge=None, nir=None + ): + """ + performs the calculation of the index with the values instantiated in the class + :str index: abbreviation of index name to perform + """ + self.setMatrices(red=red, green=green, blue=blue, redEdge=redEdge, nir=nir) + funcs = { + "ARVI2": self.ARVI2, + "CCCI": self.CCCI, + "CVI": self.CVI, + "GLI": self.GLI, + "NDVI": self.NDVI, + "BNDVI": self.BNDVI, + "redEdgeNDVI": self.redEdgeNDVI, + "GNDVI": self.GNDVI, + "GBNDVI": self.GBNDVI, + "GRNDVI": self.GRNDVI, + "RBNDVI": self.RBNDVI, + "PNDVI": self.PNDVI, + "ATSAVI": self.ATSAVI, + "BWDRVI": self.BWDRVI, + "CIgreen": self.CIgreen, + "CIrededge": self.CIrededge, + "CI": self.CI, + "CTVI": self.CTVI, + "GDVI": self.GDVI, + "EVI": self.EVI, + "GEMI": self.GEMI, + "GOSAVI": self.GOSAVI, + "GSAVI": self.GSAVI, + "Hue": self.Hue, + "IVI": self.IVI, + "IPVI": self.IPVI, + "I": self.I, + "RVI": self.RVI, + "MRVI": self.MRVI, + "MSAVI": self.MSAVI, + "NormG": self.NormG, + "NormNIR": self.NormNIR, + "NormR": self.NormR, + "NGRDI": self.NGRDI, + "RI": self.RI, + "S": self.S, + "IF": self.IF, + "DVI": self.DVI, + "TVI": self.TVI, + "NDRE": self.NDRE, + } + + try: + return funcs[index]() + except KeyError: + print("Index not in the list!") + return False + + def ARVI2(self): + """ + Atmospherically Resistant Vegetation Index 2 + https://www.indexdatabase.de/db/i-single.php?id=396 + :return: index + −0.18+1.17*(self.nir−self.red)/(self.nir+self.red) + """ + return -0.18 + (1.17 * ((self.nir - self.red) / (self.nir + self.red))) + + def CCCI(self): + """ + Canopy Chlorophyll Content Index + https://www.indexdatabase.de/db/i-single.php?id=224 + :return: index + """ + return ((self.nir - self.redEdge) / (self.nir + self.redEdge)) / ( + (self.nir - self.red) / (self.nir + self.red) + ) + + def CVI(self): + """ + Chlorophyll vegetation index + https://www.indexdatabase.de/db/i-single.php?id=391 + :return: index + """ + return self.nir * (self.red / (self.green ** 2)) + + def GLI(self): + """ + self.green leaf index + https://www.indexdatabase.de/db/i-single.php?id=375 + :return: index + """ + return (2 * self.green - self.red - self.blue) / ( + 2 * self.green + self.red + self.blue + ) + + def NDVI(self): + """ + Normalized Difference self.nir/self.red Normalized Difference Vegetation + Index, Calibrated NDVI - CDVI + https://www.indexdatabase.de/db/i-single.php?id=58 + :return: index + """ + return (self.nir - self.red) / (self.nir + self.red) + + def BNDVI(self): + """ + Normalized Difference self.nir/self.blue self.blue-normalized difference + vegetation index + https://www.indexdatabase.de/db/i-single.php?id=135 + :return: index + """ + return (self.nir - self.blue) / (self.nir + self.blue) + + def redEdgeNDVI(self): + """ + Normalized Difference self.rededge/self.red + https://www.indexdatabase.de/db/i-single.php?id=235 + :return: index + """ + return (self.redEdge - self.red) / (self.redEdge + self.red) + + def GNDVI(self): + """ + Normalized Difference self.nir/self.green self.green NDVI + https://www.indexdatabase.de/db/i-single.php?id=401 + :return: index + """ + return (self.nir - self.green) / (self.nir + self.green) + + def GBNDVI(self): + """ + self.green-self.blue NDVI + https://www.indexdatabase.de/db/i-single.php?id=186 + :return: index + """ + return (self.nir - (self.green + self.blue)) / ( + self.nir + (self.green + self.blue) + ) + + def GRNDVI(self): + """ + self.green-self.red NDVI + https://www.indexdatabase.de/db/i-single.php?id=185 + :return: index + """ + return (self.nir - (self.green + self.red)) / ( + self.nir + (self.green + self.red) + ) + + def RBNDVI(self): + """ + self.red-self.blue NDVI + https://www.indexdatabase.de/db/i-single.php?id=187 + :return: index + """ + return (self.nir - (self.blue + self.red)) / (self.nir + (self.blue + self.red)) + + def PNDVI(self): + """ + Pan NDVI + https://www.indexdatabase.de/db/i-single.php?id=188 + :return: index + """ + return (self.nir - (self.green + self.red + self.blue)) / ( + self.nir + (self.green + self.red + self.blue) + ) + + def ATSAVI(self, X=0.08, a=1.22, b=0.03): + """ + Adjusted transformed soil-adjusted VI + https://www.indexdatabase.de/db/i-single.php?id=209 + :return: index + """ + return a * ( + (self.nir - a * self.red - b) + / (a * self.nir + self.red - a * b + X * (1 + a ** 2)) + ) + + def BWDRVI(self): + """ + self.blue-wide dynamic range vegetation index + https://www.indexdatabase.de/db/i-single.php?id=136 + :return: index + """ + return (0.1 * self.nir - self.blue) / (0.1 * self.nir + self.blue) + + def CIgreen(self): + """ + Chlorophyll Index self.green + https://www.indexdatabase.de/db/i-single.php?id=128 + :return: index + """ + return (self.nir / self.green) - 1 + + def CIrededge(self): + """ + Chlorophyll Index self.redEdge + https://www.indexdatabase.de/db/i-single.php?id=131 + :return: index + """ + return (self.nir / self.redEdge) - 1 + + def CI(self): + """ + Coloration Index + https://www.indexdatabase.de/db/i-single.php?id=11 + :return: index + """ + return (self.red - self.blue) / self.red + + def CTVI(self): + """ + Corrected Transformed Vegetation Index + https://www.indexdatabase.de/db/i-single.php?id=244 + :return: index + """ + ndvi = self.NDVI() + return ((ndvi + 0.5) / (abs(ndvi + 0.5))) * (abs(ndvi + 0.5) ** (1 / 2)) + + def GDVI(self): + """ + Difference self.nir/self.green self.green Difference Vegetation Index + https://www.indexdatabase.de/db/i-single.php?id=27 + :return: index + """ + return self.nir - self.green + + def EVI(self): + """ + Enhanced Vegetation Index + https://www.indexdatabase.de/db/i-single.php?id=16 + :return: index + """ + return 2.5 * ( + (self.nir - self.red) / (self.nir + 6 * self.red - 7.5 * self.blue + 1) + ) + + def GEMI(self): + """ + Global Environment Monitoring Index + https://www.indexdatabase.de/db/i-single.php?id=25 + :return: index + """ + n = (2 * (self.nir ** 2 - self.red ** 2) + 1.5 * self.nir + 0.5 * self.red) / ( + self.nir + self.red + 0.5 + ) + return n * (1 - 0.25 * n) - (self.red - 0.125) / (1 - self.red) + + def GOSAVI(self, Y=0.16): + """ + self.green Optimized Soil Adjusted Vegetation Index + https://www.indexdatabase.de/db/i-single.php?id=29 + mit Y = 0,16 + :return: index + """ + return (self.nir - self.green) / (self.nir + self.green + Y) + + def GSAVI(self, L=0.5): + """ + self.green Soil Adjusted Vegetation Index + https://www.indexdatabase.de/db/i-single.php?id=31 + mit L = 0,5 + :return: index + """ + return ((self.nir - self.green) / (self.nir + self.green + L)) * (1 + L) + + def Hue(self): + """ + Hue + https://www.indexdatabase.de/db/i-single.php?id=34 + :return: index + """ + return np.arctan( + ((2 * self.red - self.green - self.blue) / 30.5) * (self.green - self.blue) + ) + + def IVI(self, a=None, b=None): + """ + Ideal vegetation index + https://www.indexdatabase.de/db/i-single.php?id=276 + b=intercept of vegetation line + a=soil line slope + :return: index + """ + return (self.nir - b) / (a * self.red) + + def IPVI(self): + """ + Infraself.red percentage vegetation index + https://www.indexdatabase.de/db/i-single.php?id=35 + :return: index + """ + return (self.nir / ((self.nir + self.red) / 2)) * (self.NDVI() + 1) + + def I(self): # noqa: E741,E743 + """ + Intensity + https://www.indexdatabase.de/db/i-single.php?id=36 + :return: index + """ + return (self.red + self.green + self.blue) / 30.5 + + def RVI(self): + """ + Ratio-Vegetation-Index + http://www.seos-project.eu/modules/remotesensing/remotesensing-c03-s01-p01.html + :return: index + """ + return self.nir / self.red + + def MRVI(self): + """ + Modified Normalized Difference Vegetation Index RVI + https://www.indexdatabase.de/db/i-single.php?id=275 + :return: index + """ + return (self.RVI() - 1) / (self.RVI() + 1) + + def MSAVI(self): + """ + Modified Soil Adjusted Vegetation Index + https://www.indexdatabase.de/db/i-single.php?id=44 + :return: index + """ + return ( + (2 * self.nir + 1) + - ((2 * self.nir + 1) ** 2 - 8 * (self.nir - self.red)) ** (1 / 2) + ) / 2 + + def NormG(self): + """ + Norm G + https://www.indexdatabase.de/db/i-single.php?id=50 + :return: index + """ + return self.green / (self.nir + self.red + self.green) + + def NormNIR(self): + """ + Norm self.nir + https://www.indexdatabase.de/db/i-single.php?id=51 + :return: index + """ + return self.nir / (self.nir + self.red + self.green) + + def NormR(self): + """ + Norm R + https://www.indexdatabase.de/db/i-single.php?id=52 + :return: index + """ + return self.red / (self.nir + self.red + self.green) + + def NGRDI(self): + """ + Normalized Difference self.green/self.red Normalized self.green self.red + difference index, Visible Atmospherically Resistant Indices self.green + (VIself.green) + https://www.indexdatabase.de/db/i-single.php?id=390 + :return: index + """ + return (self.green - self.red) / (self.green + self.red) + + def RI(self): + """ + Normalized Difference self.red/self.green self.redness Index + https://www.indexdatabase.de/db/i-single.php?id=74 + :return: index + """ + return (self.red - self.green) / (self.red + self.green) + + def S(self): + """ + Saturation + https://www.indexdatabase.de/db/i-single.php?id=77 + :return: index + """ + max = np.max([np.max(self.red), np.max(self.green), np.max(self.blue)]) + min = np.min([np.min(self.red), np.min(self.green), np.min(self.blue)]) + return (max - min) / max + + def IF(self): + """ + Shape Index + https://www.indexdatabase.de/db/i-single.php?id=79 + :return: index + """ + return (2 * self.red - self.green - self.blue) / (self.green - self.blue) + + def DVI(self): + """ + Simple Ratio self.nir/self.red Difference Vegetation Index, Vegetation Index + Number (VIN) + https://www.indexdatabase.de/db/i-single.php?id=12 + :return: index + """ + return self.nir / self.red + + def TVI(self): + """ + Transformed Vegetation Index + https://www.indexdatabase.de/db/i-single.php?id=98 + :return: index + """ + return (self.NDVI() + 0.5) ** (1 / 2) + + def NDRE(self): + return (self.nir - self.redEdge) / (self.nir + self.redEdge) + + +""" +# genering a random matrices to test this class +red = np.ones((1000,1000, 1),dtype="float64") * 46787 +green = np.ones((1000,1000, 1),dtype="float64") * 23487 +blue = np.ones((1000,1000, 1),dtype="float64") * 14578 +redEdge = np.ones((1000,1000, 1),dtype="float64") * 51045 +nir = np.ones((1000,1000, 1),dtype="float64") * 52200 + +# Examples of how to use the class + +# instantiating the class +cl = IndexCalculation() + +# instantiating the class with the values +#cl = indexCalculation(red=red, green=green, blue=blue, redEdge=redEdge, nir=nir) + +# how set the values after instantiate the class cl, (for update the data or when don't +# instantiating the class with the values) +cl.setMatrices(red=red, green=green, blue=blue, redEdge=redEdge, nir=nir) + +# calculating the indices for the instantiated values in the class + # Note: the CCCI index can be changed to any index implemented in the class. +indexValue_form1 = cl.calculation("CCCI", red=red, green=green, blue=blue, + redEdge=redEdge, nir=nir).astype(np.float64) +indexValue_form2 = cl.CCCI() + +# calculating the index with the values directly -- you can set just the values +# preferred note: the *calculation* function performs the function *setMatrices* +indexValue_form3 = cl.calculation("CCCI", red=red, green=green, blue=blue, + redEdge=redEdge, nir=nir).astype(np.float64) + +print("Form 1: "+np.array2string(indexValue_form1, precision=20, separator=', ', + floatmode='maxprec_equal')) +print("Form 2: "+np.array2string(indexValue_form2, precision=20, separator=', ', + floatmode='maxprec_equal')) +print("Form 3: "+np.array2string(indexValue_form3, precision=20, separator=', ', + floatmode='maxprec_equal')) + +# A list of examples results for different type of data at NDVI +# float16 -> 0.31567383 #NDVI (red = 50, nir = 100) +# float32 -> 0.31578946 #NDVI (red = 50, nir = 100) +# float64 -> 0.3157894736842105 #NDVI (red = 50, nir = 100) +# longdouble -> 0.3157894736842105 #NDVI (red = 50, nir = 100) +""" diff --git a/digital_image_processing/resize/__init__.py b/digital_image_processing/resize/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/resize/resize.py b/digital_image_processing/resize/resize.py new file mode 100644 index 000000000000..4836521f9f58 --- /dev/null +++ b/digital_image_processing/resize/resize.py @@ -0,0 +1,71 @@ +""" Multiple image resizing techniques """ +import numpy as np +from cv2 import destroyAllWindows, imread, imshow, waitKey + + +class NearestNeighbour: + """ + Simplest and fastest version of image resizing. + Source: https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation + """ + + def __init__(self, img, dst_width: int, dst_height: int): + if dst_width < 0 or dst_height < 0: + raise ValueError("Destination width/height should be > 0") + + self.img = img + self.src_w = img.shape[1] + self.src_h = img.shape[0] + self.dst_w = dst_width + self.dst_h = dst_height + + self.ratio_x = self.src_w / self.dst_w + self.ratio_y = self.src_h / self.dst_h + + self.output = self.output_img = ( + np.ones((self.dst_h, self.dst_w, 3), np.uint8) * 255 + ) + + def process(self): + for i in range(self.dst_h): + for j in range(self.dst_w): + self.output[i][j] = self.img[self.get_y(i)][self.get_x(j)] + + def get_x(self, x: int) -> int: + """ + Get parent X coordinate for destination X + :param x: Destination X coordinate + :return: Parent X coordinate based on `x ratio` + >>> nn = NearestNeighbour(imread("digital_image_processing/image_data/lena.jpg", + ... 1), 100, 100) + >>> nn.ratio_x = 0.5 + >>> nn.get_x(4) + 2 + """ + return int(self.ratio_x * x) + + def get_y(self, y: int) -> int: + """ + Get parent Y coordinate for destination Y + :param y: Destination X coordinate + :return: Parent X coordinate based on `y ratio` + >>> nn = NearestNeighbour(imread("digital_image_processing/image_data/lena.jpg", + ... 1), 100, 100) + >>> nn.ratio_y = 0.5 + >>> nn.get_y(4) + 2 + """ + return int(self.ratio_y * y) + + +if __name__ == "__main__": + dst_w, dst_h = 800, 600 + im = imread("image_data/lena.jpg", 1) + n = NearestNeighbour(im, dst_w, dst_h) + n.process() + + imshow( + f"Image resized from: {im.shape[1]}x{im.shape[0]} to {dst_w}x{dst_h}", n.output + ) + waitKey(0) + destroyAllWindows() diff --git a/digital_image_processing/rotation/__init__.py b/digital_image_processing/rotation/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/rotation/rotation.py b/digital_image_processing/rotation/rotation.py new file mode 100644 index 000000000000..2951f18fc0ec --- /dev/null +++ b/digital_image_processing/rotation/rotation.py @@ -0,0 +1,52 @@ +import cv2 +import numpy as np +from matplotlib import pyplot as plt + + +def get_rotation( + img: np.array, pt1: np.float32, pt2: np.float32, rows: int, cols: int +) -> np.array: + """ + Get image rotation + :param img: np.array + :param pt1: 3x2 list + :param pt2: 3x2 list + :param rows: columns image shape + :param cols: rows image shape + :return: np.array + """ + matrix = cv2.getAffineTransform(pt1, pt2) + return cv2.warpAffine(img, matrix, (rows, cols)) + + +if __name__ == "__main__": + # read original image + image = cv2.imread("lena.jpg") + # turn image in gray scale value + gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + # get image shape + img_rows, img_cols = gray_img.shape + + # set different points to rotate image + pts1 = np.float32([[50, 50], [200, 50], [50, 200]]) + pts2 = np.float32([[10, 100], [200, 50], [100, 250]]) + pts3 = np.float32([[50, 50], [150, 50], [120, 200]]) + pts4 = np.float32([[10, 100], [80, 50], [180, 250]]) + + # add all rotated images in a list + images = [ + gray_img, + get_rotation(gray_img, pts1, pts2, img_rows, img_cols), + get_rotation(gray_img, pts2, pts3, img_rows, img_cols), + get_rotation(gray_img, pts2, pts4, img_rows, img_cols), + ] + + # plot different image rotations + fig = plt.figure(1) + titles = ["Original", "Rotation 1", "Rotation 2", "Rotation 3"] + for i, image in enumerate(images): + plt.subplot(2, 2, i + 1), plt.imshow(image, "gray") + plt.title(titles[i]) + plt.axis("off") + plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95) + plt.show() diff --git a/digital_image_processing/sepia.py b/digital_image_processing/sepia.py new file mode 100644 index 000000000000..dfb5951676aa --- /dev/null +++ b/digital_image_processing/sepia.py @@ -0,0 +1,50 @@ +""" + Implemented an algorithm using opencv to tone an image with sepia technique +""" +from cv2 import destroyAllWindows, imread, imshow, waitKey + + +def make_sepia(img, factor: int): + """ + Function create sepia tone. + Source: https://en.wikipedia.org/wiki/Sepia_(color) + """ + pixel_h, pixel_v = img.shape[0], img.shape[1] + + def to_grayscale(blue, green, red): + """ + Helper function to create pixel's greyscale representation + Src: https://pl.wikipedia.org/wiki/YUV + """ + return 0.2126 * red + 0.587 * green + 0.114 * blue + + def normalize(value): + """ Helper function to normalize R/G/B value -> return 255 if value > 255""" + return min(value, 255) + + for i in range(pixel_h): + for j in range(pixel_v): + greyscale = int(to_grayscale(*img[i][j])) + img[i][j] = [ + normalize(greyscale), + normalize(greyscale + factor), + normalize(greyscale + 2 * factor), + ] + + return img + + +if __name__ == "__main__": + # read original image + images = { + percentage: imread("image_data/lena.jpg", 1) for percentage in (10, 20, 30, 40) + } + + for percentage, img in images.items(): + make_sepia(img, percentage) + + for percentage, img in images.items(): + imshow(f"Original image with sepia (factor: {percentage})", img) + + waitKey(0) + destroyAllWindows() diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py new file mode 100644 index 000000000000..40f2f7b83b6d --- /dev/null +++ b/digital_image_processing/test_digital_image_processing.py @@ -0,0 +1,93 @@ +""" +PyTest's for Digital Image Processing +""" +from cv2 import COLOR_BGR2GRAY, cvtColor, imread +from numpy import array, uint8 +from PIL import Image + +from digital_image_processing import change_contrast as cc +from digital_image_processing import convert_to_negative as cn +from digital_image_processing import sepia as sp +from digital_image_processing.dithering import burkes as bs +from digital_image_processing.edge_detection import canny as canny +from digital_image_processing.filters import convolve as conv +from digital_image_processing.filters import gaussian_filter as gg +from digital_image_processing.filters import median_filter as med +from digital_image_processing.filters import sobel_filter as sob +from digital_image_processing.resize import resize as rs + +img = imread(r"digital_image_processing/image_data/lena_small.jpg") +gray = cvtColor(img, COLOR_BGR2GRAY) + + +# Test: convert_to_negative() +def test_convert_to_negative(): + negative_img = cn.convert_to_negative(img) + # assert negative_img array for at least one True + assert negative_img.any() + + +# Test: change_contrast() +def test_change_contrast(): + with Image.open("digital_image_processing/image_data/lena_small.jpg") as img: + # Work around assertion for response + assert str(cc.change_contrast(img, 110)).startswith( + " Divide and conquer +The points are sorted based on Xco-ords and +then based on Yco-ords separately. +And by applying divide and conquer approach, +minimum distance is obtained recursively. + +>> Closest points can lie on different sides of partition. +This case handled by forming a strip of points +whose Xco-ords distance is less than closest_pair_dis +from mid-point's Xco-ords. Points sorted based on Yco-ords +are used in this step to reduce sorting time. +Closest pair distance is found in the strip of points. (closest_in_strip) + +min(closest_pair_dis, closest_in_strip) would be the final answer. + +Time complexity: O(n * log n) +""" + + +def euclidean_distance_sqr(point1, point2): + """ + >>> euclidean_distance_sqr([1,2],[2,4]) + 5 + """ + return (point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2 + + +def column_based_sort(array, column=0): + """ + >>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1) + [(3, 0), (5, 1), (4, 2)] + """ + return sorted(array, key=lambda x: x[column]) + + +def dis_between_closest_pair(points, points_counts, min_dis=float("inf")): + """ + brute force approach to find distance between closest pair points + + Parameters : + points, points_count, min_dis (list(tuple(int, int)), int, int) + + Returns : + min_dis (float): distance between closest pair of points + + >>> dis_between_closest_pair([[1,2],[2,4],[5,7],[8,9],[11,0]],5) + 5 + + """ + + for i in range(points_counts - 1): + for j in range(i + 1, points_counts): + current_dis = euclidean_distance_sqr(points[i], points[j]) + if current_dis < min_dis: + min_dis = current_dis + return min_dis + + +def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")): + """ + closest pair of points in strip + + Parameters : + points, points_count, min_dis (list(tuple(int, int)), int, int) + + Returns : + min_dis (float): distance btw closest pair of points in the strip (< min_dis) + + >>> dis_between_closest_in_strip([[1,2],[2,4],[5,7],[8,9],[11,0]],5) + 85 + """ + + for i in range(min(6, points_counts - 1), points_counts): + for j in range(max(0, i - 6), i): + current_dis = euclidean_distance_sqr(points[i], points[j]) + if current_dis < min_dis: + min_dis = current_dis + return min_dis + + +def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_counts): + """divide and conquer approach + + Parameters : + points, points_count (list(tuple(int, int)), int) + + Returns : + (float): distance btw closest pair of points + + >>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(5, 6), (7, 8)], 2) + 8 + """ + + # base case + if points_counts <= 3: + return dis_between_closest_pair(points_sorted_on_x, points_counts) + + # recursion + mid = points_counts // 2 + closest_in_left = closest_pair_of_points_sqr( + points_sorted_on_x, points_sorted_on_y[:mid], mid + ) + closest_in_right = closest_pair_of_points_sqr( + points_sorted_on_y, points_sorted_on_y[mid:], points_counts - mid + ) + closest_pair_dis = min(closest_in_left, closest_in_right) + + """ + cross_strip contains the points, whose Xcoords are at a + distance(< closest_pair_dis) from mid's Xcoord + """ + + cross_strip = [] + for point in points_sorted_on_x: + if abs(point[0] - points_sorted_on_x[mid][0]) < closest_pair_dis: + cross_strip.append(point) + + closest_in_strip = dis_between_closest_in_strip( + cross_strip, len(cross_strip), closest_pair_dis + ) + return min(closest_pair_dis, closest_in_strip) + + +def closest_pair_of_points(points, points_counts): + """ + >>> closest_pair_of_points([(2, 3), (12, 30)], len([(2, 3), (12, 30)])) + 28.792360097775937 + """ + points_sorted_on_x = column_based_sort(points, column=0) + points_sorted_on_y = column_based_sort(points, column=1) + return ( + closest_pair_of_points_sqr( + points_sorted_on_x, points_sorted_on_y, points_counts + ) + ) ** 0.5 + + +if __name__ == "__main__": + points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)] + print("Distance:", closest_pair_of_points(points, len(points))) diff --git a/divide_and_conquer/convex_hull.py b/divide_and_conquer/convex_hull.py new file mode 100644 index 000000000000..9c096f671385 --- /dev/null +++ b/divide_and_conquer/convex_hull.py @@ -0,0 +1,506 @@ +""" +The convex hull problem is problem of finding all the vertices of convex polygon, P of +a set of points in a plane such that all the points are either on the vertices of P or +inside P. TH convex hull problem has several applications in geometrical problems, +computer graphics and game development. + +Two algorithms have been implemented for the convex hull problem here. +1. A brute-force algorithm which runs in O(n^3) +2. A divide-and-conquer algorithm which runs in O(n log(n)) + +There are other several other algorithms for the convex hull problem +which have not been implemented here, yet. + +""" + +from typing import Iterable, List, Set, Union + + +class Point: + """ + Defines a 2-d point for use by all convex-hull algorithms. + + Parameters + ---------- + x: an int or a float, the x-coordinate of the 2-d point + y: an int or a float, the y-coordinate of the 2-d point + + Examples + -------- + >>> Point(1, 2) + (1.0, 2.0) + >>> Point("1", "2") + (1.0, 2.0) + >>> Point(1, 2) > Point(0, 1) + True + >>> Point(1, 1) == Point(1, 1) + True + >>> Point(-0.5, 1) == Point(0.5, 1) + False + >>> Point("pi", "e") + Traceback (most recent call last): + ... + ValueError: could not convert string to float: 'pi' + """ + + def __init__(self, x, y): + self.x, self.y = float(x), float(y) + + def __eq__(self, other): + return self.x == other.x and self.y == other.y + + def __ne__(self, other): + return not self == other + + def __gt__(self, other): + if self.x > other.x: + return True + elif self.x == other.x: + return self.y > other.y + return False + + def __lt__(self, other): + return not self > other + + def __ge__(self, other): + if self.x > other.x: + return True + elif self.x == other.x: + return self.y >= other.y + return False + + def __le__(self, other): + if self.x < other.x: + return True + elif self.x == other.x: + return self.y <= other.y + return False + + def __repr__(self): + return f"({self.x}, {self.y})" + + def __hash__(self): + return hash(self.x) + + +def _construct_points( + list_of_tuples: Union[List[Point], List[List[float]], Iterable[List[float]]] +) -> List[Point]: + """ + constructs a list of points from an array-like object of numbers + + Arguments + --------- + + list_of_tuples: array-like object of type numbers. Acceptable types so far + are lists, tuples and sets. + + Returns + -------- + points: a list where each item is of type Point. This contains only objects + which can be converted into a Point. + + Examples + ------- + >>> _construct_points([[1, 1], [2, -1], [0.3, 4]]) + [(1.0, 1.0), (2.0, -1.0), (0.3, 4.0)] + >>> _construct_points([1, 2]) + Ignoring deformed point 1. All points must have at least 2 coordinates. + Ignoring deformed point 2. All points must have at least 2 coordinates. + [] + >>> _construct_points([]) + [] + >>> _construct_points(None) + [] + """ + + points: List[Point] = [] + if list_of_tuples: + for p in list_of_tuples: + if isinstance(p, Point): + points.append(p) + else: + try: + points.append(Point(p[0], p[1])) + except (IndexError, TypeError): + print( + f"Ignoring deformed point {p}. All points" + " must have at least 2 coordinates." + ) + return points + + +def _validate_input(points: Union[List[Point], List[List[float]]]) -> List[Point]: + """ + validates an input instance before a convex-hull algorithms uses it + + Parameters + --------- + points: array-like, the 2d points to validate before using with + a convex-hull algorithm. The elements of points must be either lists, tuples or + Points. + + Returns + ------- + points: array_like, an iterable of all well-defined Points constructed passed in. + + + Exception + --------- + ValueError: if points is empty or None, or if a wrong data structure like a scalar + is passed + + TypeError: if an iterable but non-indexable object (eg. dictionary) is passed. + The exception to this a set which we'll convert to a list before using + + + Examples + ------- + >>> _validate_input([[1, 2]]) + [(1.0, 2.0)] + >>> _validate_input([(1, 2)]) + [(1.0, 2.0)] + >>> _validate_input([Point(2, 1), Point(-1, 2)]) + [(2.0, 1.0), (-1.0, 2.0)] + >>> _validate_input([]) + Traceback (most recent call last): + ... + ValueError: Expecting a list of points but got [] + >>> _validate_input(1) + Traceback (most recent call last): + ... + ValueError: Expecting an iterable object but got an non-iterable type 1 + """ + + if not hasattr(points, "__iter__"): + raise ValueError( + f"Expecting an iterable object but got an non-iterable type {points}" + ) + + if not points: + raise ValueError(f"Expecting a list of points but got {points}") + + return _construct_points(points) + + +def _det(a: Point, b: Point, c: Point) -> float: + """ + Computes the sign perpendicular distance of a 2d point c from a line segment + ab. The sign indicates the direction of c relative to ab. + A Positive value means c is above ab (to the left), while a negative value + means c is below ab (to the right). 0 means all three points are on a straight line. + + As a side note, 0.5 * abs|det| is the area of triangle abc + + Parameters + ---------- + a: point, the point on the left end of line segment ab + b: point, the point on the right end of line segment ab + c: point, the point for which the direction and location is desired. + + Returns + -------- + det: float, abs(det) is the distance of c from ab. The sign + indicates which side of line segment ab c is. det is computed as + (a_xb_y + c_xa_y + b_xc_y) - (a_yb_x + c_ya_x + b_yc_x) + + Examples + ---------- + >>> _det(Point(1, 1), Point(1, 2), Point(1, 5)) + 0.0 + >>> _det(Point(0, 0), Point(10, 0), Point(0, 10)) + 100.0 + >>> _det(Point(0, 0), Point(10, 0), Point(0, -10)) + -100.0 + """ + + det = (a.x * b.y + b.x * c.y + c.x * a.y) - (a.y * b.x + b.y * c.x + c.y * a.x) + return det + + +def convex_hull_bf(points: List[Point]) -> List[Point]: + """ + Constructs the convex hull of a set of 2D points using a brute force algorithm. + The algorithm basically considers all combinations of points (i, j) and uses the + definition of convexity to determine whether (i, j) is part of the convex hull or + not. (i, j) is part of the convex hull if and only iff there are no points on both + sides of the line segment connecting the ij, and there is no point k such that k is + on either end of the ij. + + Runtime: O(n^3) - definitely horrible + + Parameters + --------- + points: array-like of object of Points, lists or tuples. + The set of 2d points for which the convex-hull is needed + + Returns + ------ + convex_set: list, the convex-hull of points sorted in non-decreasing order. + + See Also + -------- + convex_hull_recursive, + + Examples + --------- + >>> convex_hull_bf([[0, 0], [1, 0], [10, 1]]) + [(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)] + >>> convex_hull_bf([[0, 0], [1, 0], [10, 0]]) + [(0.0, 0.0), (10.0, 0.0)] + >>> convex_hull_bf([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1], + ... [-0.75, 1]]) + [(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)] + >>> convex_hull_bf([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3), + ... (2, -1), (2, -4), (1, -3)]) + [(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)] + """ + + points = sorted(_validate_input(points)) + n = len(points) + convex_set = set() + + for i in range(n - 1): + for j in range(i + 1, n): + points_left_of_ij = points_right_of_ij = False + ij_part_of_convex_hull = True + for k in range(n): + if k != i and k != j: + det_k = _det(points[i], points[j], points[k]) + + if det_k > 0: + points_left_of_ij = True + elif det_k < 0: + points_right_of_ij = True + else: + # point[i], point[j], point[k] all lie on a straight line + # if point[k] is to the left of point[i] or it's to the + # right of point[j], then point[i], point[j] cannot be + # part of the convex hull of A + if points[k] < points[i] or points[k] > points[j]: + ij_part_of_convex_hull = False + break + + if points_left_of_ij and points_right_of_ij: + ij_part_of_convex_hull = False + break + + if ij_part_of_convex_hull: + convex_set.update([points[i], points[j]]) + + return sorted(convex_set) + + +def convex_hull_recursive(points: List[Point]) -> List[Point]: + """ + Constructs the convex hull of a set of 2D points using a divide-and-conquer strategy + The algorithm exploits the geometric properties of the problem by repeatedly + partitioning the set of points into smaller hulls, and finding the convex hull of + these smaller hulls. The union of the convex hull from smaller hulls is the + solution to the convex hull of the larger problem. + + Parameter + --------- + points: array-like of object of Points, lists or tuples. + The set of 2d points for which the convex-hull is needed + + Runtime: O(n log n) + + Returns + ------- + convex_set: list, the convex-hull of points sorted in non-decreasing order. + + Examples + --------- + >>> convex_hull_recursive([[0, 0], [1, 0], [10, 1]]) + [(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)] + >>> convex_hull_recursive([[0, 0], [1, 0], [10, 0]]) + [(0.0, 0.0), (10.0, 0.0)] + >>> convex_hull_recursive([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1], + ... [-0.75, 1]]) + [(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)] + >>> convex_hull_recursive([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3), + ... (2, -1), (2, -4), (1, -3)]) + [(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)] + + """ + points = sorted(_validate_input(points)) + n = len(points) + + # divide all the points into an upper hull and a lower hull + # the left most point and the right most point are definitely + # members of the convex hull by definition. + # use these two anchors to divide all the points into two hulls, + # an upper hull and a lower hull. + + # all points to the left (above) the line joining the extreme points belong to the + # upper hull + # all points to the right (below) the line joining the extreme points below to the + # lower hull + # ignore all points on the line joining the extreme points since they cannot be + # part of the convex hull + + left_most_point = points[0] + right_most_point = points[n - 1] + + convex_set = {left_most_point, right_most_point} + upper_hull = [] + lower_hull = [] + + for i in range(1, n - 1): + det = _det(left_most_point, right_most_point, points[i]) + + if det > 0: + upper_hull.append(points[i]) + elif det < 0: + lower_hull.append(points[i]) + + _construct_hull(upper_hull, left_most_point, right_most_point, convex_set) + _construct_hull(lower_hull, right_most_point, left_most_point, convex_set) + + return sorted(convex_set) + + +def _construct_hull( + points: List[Point], left: Point, right: Point, convex_set: Set[Point] +) -> None: + """ + + Parameters + --------- + points: list or None, the hull of points from which to choose the next convex-hull + point + left: Point, the point to the left of line segment joining left and right + right: The point to the right of the line segment joining left and right + convex_set: set, the current convex-hull. The state of convex-set gets updated by + this function + + Note + ---- + For the line segment 'ab', 'a' is on the left and 'b' on the right. + but the reverse is true for the line segment 'ba'. + + Returns + ------- + Nothing, only updates the state of convex-set + """ + if points: + extreme_point = None + extreme_point_distance = float("-inf") + candidate_points = [] + + for p in points: + det = _det(left, right, p) + + if det > 0: + candidate_points.append(p) + + if det > extreme_point_distance: + extreme_point_distance = det + extreme_point = p + + if extreme_point: + _construct_hull(candidate_points, left, extreme_point, convex_set) + convex_set.add(extreme_point) + _construct_hull(candidate_points, extreme_point, right, convex_set) + + +def convex_hull_melkman(points: List[Point]) -> List[Point]: + """ + Constructs the convex hull of a set of 2D points using the melkman algorithm. + The algorithm works by iteratively inserting points of a simple polygonal chain + (meaning that no line segments between two consecutive points cross each other). + Sorting the points yields such a polygonal chain. + + For a detailed description, see http://cgm.cs.mcgill.ca/~athens/cs601/Melkman.html + + Runtime: O(n log n) - O(n) if points are already sorted in the input + + Parameters + --------- + points: array-like of object of Points, lists or tuples. + The set of 2d points for which the convex-hull is needed + + Returns + ------ + convex_set: list, the convex-hull of points sorted in non-decreasing order. + + See Also + -------- + + Examples + --------- + >>> convex_hull_melkman([[0, 0], [1, 0], [10, 1]]) + [(0.0, 0.0), (1.0, 0.0), (10.0, 1.0)] + >>> convex_hull_melkman([[0, 0], [1, 0], [10, 0]]) + [(0.0, 0.0), (10.0, 0.0)] + >>> convex_hull_melkman([[-1, 1],[-1, -1], [0, 0], [0.5, 0.5], [1, -1], [1, 1], + ... [-0.75, 1]]) + [(-1.0, -1.0), (-1.0, 1.0), (1.0, -1.0), (1.0, 1.0)] + >>> convex_hull_melkman([(0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3), + ... (2, -1), (2, -4), (1, -3)]) + [(0.0, 0.0), (0.0, 3.0), (1.0, -3.0), (2.0, -4.0), (3.0, 0.0), (3.0, 3.0)] + """ + points = sorted(_validate_input(points)) + n = len(points) + + convex_hull = points[:2] + for i in range(2, n): + det = _det(convex_hull[1], convex_hull[0], points[i]) + if det > 0: + convex_hull.insert(0, points[i]) + break + elif det < 0: + convex_hull.append(points[i]) + break + else: + convex_hull[1] = points[i] + i += 1 + + for i in range(i, n): + if ( + _det(convex_hull[0], convex_hull[-1], points[i]) > 0 + and _det(convex_hull[-1], convex_hull[0], points[1]) < 0 + ): + # The point lies within the convex hull + continue + + convex_hull.insert(0, points[i]) + convex_hull.append(points[i]) + while _det(convex_hull[0], convex_hull[1], convex_hull[2]) >= 0: + del convex_hull[1] + while _det(convex_hull[-1], convex_hull[-2], convex_hull[-3]) <= 0: + del convex_hull[-2] + + # `convex_hull` is contains the convex hull in circular order + return sorted(convex_hull[1:] if len(convex_hull) > 3 else convex_hull) + + +def main(): + points = [ + (0, 3), + (2, 2), + (1, 1), + (2, 1), + (3, 0), + (0, 0), + (3, 3), + (2, -1), + (2, -4), + (1, -3), + ] + # the convex set of points is + # [(0, 0), (0, 3), (1, -3), (2, -4), (3, 0), (3, 3)] + results_bf = convex_hull_bf(points) + + results_recursive = convex_hull_recursive(points) + assert results_bf == results_recursive + + results_melkman = convex_hull_melkman(points) + assert results_bf == results_melkman + + print(results_bf) + + +if __name__ == "__main__": + main() diff --git a/divide_and_conquer/heaps_algorithm.py b/divide_and_conquer/heaps_algorithm.py new file mode 100644 index 000000000000..af30ad664101 --- /dev/null +++ b/divide_and_conquer/heaps_algorithm.py @@ -0,0 +1,56 @@ +""" +Heap's algorithm returns the list of all permutations possible from a list. +It minimizes movement by generating each permutation from the previous one +by swapping only two elements. +More information: +https://en.wikipedia.org/wiki/Heap%27s_algorithm. +""" + + +def heaps(arr: list) -> list: + """ + Pure python implementation of the Heap's algorithm (recursive version), + returning all permutations of a list. + >>> heaps([]) + [()] + >>> heaps([0]) + [(0,)] + >>> heaps([-1, 1]) + [(-1, 1), (1, -1)] + >>> heaps([1, 2, 3]) + [(1, 2, 3), (2, 1, 3), (3, 1, 2), (1, 3, 2), (2, 3, 1), (3, 2, 1)] + >>> from itertools import permutations + >>> sorted(heaps([1,2,3])) == sorted(permutations([1,2,3])) + True + >>> all(sorted(heaps(x)) == sorted(permutations(x)) + ... for x in ([], [0], [-1, 1], [1, 2, 3])) + True + """ + + if len(arr) <= 1: + return [tuple(arr)] + + res = [] + + def generate(k: int, arr: list): + if k == 1: + res.append(tuple(arr[:])) + return + + generate(k - 1, arr) + + for i in range(k - 1): + if k % 2 == 0: # k is even + arr[i], arr[k - 1] = arr[k - 1], arr[i] + else: # k is odd + arr[0], arr[k - 1] = arr[k - 1], arr[0] + generate(k - 1, arr) + + generate(len(arr), arr) + return res + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + arr = [int(item) for item in user_input.split(",")] + print(heaps(arr)) diff --git a/divide_and_conquer/heaps_algorithm_iterative.py b/divide_and_conquer/heaps_algorithm_iterative.py new file mode 100644 index 000000000000..4dab41f539c0 --- /dev/null +++ b/divide_and_conquer/heaps_algorithm_iterative.py @@ -0,0 +1,60 @@ +""" +Heap's (iterative) algorithm returns the list of all permutations possible from a list. +It minimizes movement by generating each permutation from the previous one +by swapping only two elements. +More information: +https://en.wikipedia.org/wiki/Heap%27s_algorithm. +""" + + +def heaps(arr: list) -> list: + """ + Pure python implementation of the iterative Heap's algorithm, + returning all permutations of a list. + >>> heaps([]) + [()] + >>> heaps([0]) + [(0,)] + >>> heaps([-1, 1]) + [(-1, 1), (1, -1)] + >>> heaps([1, 2, 3]) + [(1, 2, 3), (2, 1, 3), (3, 1, 2), (1, 3, 2), (2, 3, 1), (3, 2, 1)] + >>> from itertools import permutations + >>> sorted(heaps([1,2,3])) == sorted(permutations([1,2,3])) + True + >>> all(sorted(heaps(x)) == sorted(permutations(x)) + ... for x in ([], [0], [-1, 1], [1, 2, 3])) + True + """ + + if len(arr) <= 1: + return [tuple(arr)] + + res = [] + + def generate(n: int, arr: list): + c = [0] * n + res.append(tuple(arr)) + + i = 0 + while i < n: + if c[i] < i: + if i % 2 == 0: + arr[0], arr[i] = arr[i], arr[0] + else: + arr[c[i]], arr[i] = arr[i], arr[c[i]] + res.append(tuple(arr)) + c[i] += 1 + i = 0 + else: + c[i] = 0 + i += 1 + + generate(len(arr), arr) + return res + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + arr = [int(item) for item in user_input.split(",")] + print(heaps(arr)) diff --git a/divide_and_conquer/inversions.py b/divide_and_conquer/inversions.py new file mode 100644 index 000000000000..9bb656229321 --- /dev/null +++ b/divide_and_conquer/inversions.py @@ -0,0 +1,169 @@ +""" +Given an array-like data structure A[1..n], how many pairs +(i, j) for all 1 <= i < j <= n such that A[i] > A[j]? These pairs are +called inversions. Counting the number of such inversions in an array-like +object is the important. Among other things, counting inversions can help +us determine how close a given array is to being sorted + +In this implementation, I provide two algorithms, a divide-and-conquer +algorithm which runs in nlogn and the brute-force n^2 algorithm. + +""" + + +def count_inversions_bf(arr): + """ + Counts the number of inversions using a a naive brute-force algorithm + + Parameters + ---------- + arr: arr: array-like, the list containing the items for which the number + of inversions is desired. The elements of `arr` must be comparable. + + Returns + ------- + num_inversions: The total number of inversions in `arr` + + Examples + --------- + + >>> count_inversions_bf([1, 4, 2, 4, 1]) + 4 + >>> count_inversions_bf([1, 1, 2, 4, 4]) + 0 + >>> count_inversions_bf([]) + 0 + """ + + num_inversions = 0 + n = len(arr) + + for i in range(n - 1): + for j in range(i + 1, n): + if arr[i] > arr[j]: + num_inversions += 1 + + return num_inversions + + +def count_inversions_recursive(arr): + """ + Counts the number of inversions using a divide-and-conquer algorithm + + Parameters + ----------- + arr: array-like, the list containing the items for which the number + of inversions is desired. The elements of `arr` must be comparable. + + Returns + ------- + C: a sorted copy of `arr`. + num_inversions: int, the total number of inversions in 'arr' + + Examples + -------- + + >>> count_inversions_recursive([1, 4, 2, 4, 1]) + ([1, 1, 2, 4, 4], 4) + >>> count_inversions_recursive([1, 1, 2, 4, 4]) + ([1, 1, 2, 4, 4], 0) + >>> count_inversions_recursive([]) + ([], 0) + """ + if len(arr) <= 1: + return arr, 0 + else: + mid = len(arr) // 2 + P = arr[0:mid] + Q = arr[mid:] + + A, inversion_p = count_inversions_recursive(P) + B, inversions_q = count_inversions_recursive(Q) + C, cross_inversions = _count_cross_inversions(A, B) + + num_inversions = inversion_p + inversions_q + cross_inversions + return C, num_inversions + + +def _count_cross_inversions(P, Q): + """ + Counts the inversions across two sorted arrays. + And combine the two arrays into one sorted array + + For all 1<= i<=len(P) and for all 1 <= j <= len(Q), + if P[i] > Q[j], then (i, j) is a cross inversion + + Parameters + ---------- + P: array-like, sorted in non-decreasing order + Q: array-like, sorted in non-decreasing order + + Returns + ------ + R: array-like, a sorted array of the elements of `P` and `Q` + num_inversion: int, the number of inversions across `P` and `Q` + + Examples + -------- + + >>> _count_cross_inversions([1, 2, 3], [0, 2, 5]) + ([0, 1, 2, 2, 3, 5], 4) + >>> _count_cross_inversions([1, 2, 3], [3, 4, 5]) + ([1, 2, 3, 3, 4, 5], 0) + """ + + R = [] + i = j = num_inversion = 0 + while i < len(P) and j < len(Q): + if P[i] > Q[j]: + # if P[1] > Q[j], then P[k] > Q[k] for all i < k <= len(P) + # These are all inversions. The claim emerges from the + # property that P is sorted. + num_inversion += len(P) - i + R.append(Q[j]) + j += 1 + else: + R.append(P[i]) + i += 1 + + if i < len(P): + R.extend(P[i:]) + else: + R.extend(Q[j:]) + + return R, num_inversion + + +def main(): + arr_1 = [10, 2, 1, 5, 5, 2, 11] + + # this arr has 8 inversions: + # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2) + + num_inversions_bf = count_inversions_bf(arr_1) + _, num_inversions_recursive = count_inversions_recursive(arr_1) + + assert num_inversions_bf == num_inversions_recursive == 8 + + print("number of inversions = ", num_inversions_bf) + + # testing an array with zero inversion (a sorted arr_1) + + arr_1.sort() + num_inversions_bf = count_inversions_bf(arr_1) + _, num_inversions_recursive = count_inversions_recursive(arr_1) + + assert num_inversions_bf == num_inversions_recursive == 0 + print("number of inversions = ", num_inversions_bf) + + # an empty list should also have zero inversions + arr_1 = [] + num_inversions_bf = count_inversions_bf(arr_1) + _, num_inversions_recursive = count_inversions_recursive(arr_1) + + assert num_inversions_bf == num_inversions_recursive == 0 + print("number of inversions = ", num_inversions_bf) + + +if __name__ == "__main__": + main() diff --git a/divide_and_conquer/kth_order_statistic.py b/divide_and_conquer/kth_order_statistic.py new file mode 100644 index 000000000000..f6e81a306bff --- /dev/null +++ b/divide_and_conquer/kth_order_statistic.py @@ -0,0 +1,64 @@ +""" +Find the kth smallest element in linear time using divide and conquer. +Recall we can do this trivially in O(nlogn) time. Sort the list and +access kth element in constant time. + +This is a divide and conquer algorithm that can find a solution in O(n) time. + +For more information of this algorithm: +https://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/08/Small08.pdf +""" +from random import choice +from typing import List + + +def random_pivot(lst): + """ + Choose a random pivot for the list. + We can use a more sophisticated algorithm here, such as the median-of-medians + algorithm. + """ + return choice(lst) + + +def kth_number(lst: List[int], k: int) -> int: + """ + Return the kth smallest number in lst. + >>> kth_number([2, 1, 3, 4, 5], 3) + 3 + >>> kth_number([2, 1, 3, 4, 5], 1) + 1 + >>> kth_number([2, 1, 3, 4, 5], 5) + 5 + >>> kth_number([3, 2, 5, 6, 7, 8], 2) + 3 + >>> kth_number([25, 21, 98, 100, 76, 22, 43, 60, 89, 87], 4) + 43 + """ + # pick a pivot and separate into list based on pivot. + pivot = random_pivot(lst) + + # partition based on pivot + # linear time + small = [e for e in lst if e < pivot] + big = [e for e in lst if e > pivot] + + # if we get lucky, pivot might be the element we want. + # we can easily see this: + # small (elements smaller than k) + # + pivot (kth element) + # + big (elements larger than k) + if len(small) == k - 1: + return pivot + # pivot is in elements bigger than k + elif len(small) < k - 1: + return kth_number(big, k - len(small) - 1) + # pivot is in elements smaller than k + else: + return kth_number(small, k) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/divide_and_conquer/max_difference_pair.py b/divide_and_conquer/max_difference_pair.py new file mode 100644 index 000000000000..b976aca43137 --- /dev/null +++ b/divide_and_conquer/max_difference_pair.py @@ -0,0 +1,47 @@ +from typing import List + + +def max_difference(a: List[int]) -> (int, int): + """ + We are given an array A[1..n] of integers, n >= 1. We want to + find a pair of indices (i, j) such that + 1 <= i <= j <= n and A[j] - A[i] is as large as possible. + + Explanation: + https://www.geeksforgeeks.org/maximum-difference-between-two-elements/ + + >>> max_difference([5, 11, 2, 1, 7, 9, 0, 7]) + (1, 9) + """ + # base case + if len(a) == 1: + return a[0], a[0] + else: + # split A into half. + first = a[: len(a) // 2] + second = a[len(a) // 2 :] + + # 2 sub problems, 1/2 of original size. + small1, big1 = max_difference(first) + small2, big2 = max_difference(second) + + # get min of first and max of second + # linear time + min_first = min(first) + max_second = max(second) + + # 3 cases, either (small1, big1), + # (min_first, max_second), (small2, big2) + # constant comparisons + if big2 - small2 > max_second - min_first and big2 - small2 > big1 - small1: + return small2, big2 + elif big1 - small1 > max_second - min_first: + return small1, big1 + else: + return min_first, max_second + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/divide_and_conquer/max_subarray_sum.py b/divide_and_conquer/max_subarray_sum.py new file mode 100644 index 000000000000..43f58086e078 --- /dev/null +++ b/divide_and_conquer/max_subarray_sum.py @@ -0,0 +1,76 @@ +""" +Given a array of length n, max_subarray_sum() finds +the maximum of sum of contiguous sub-array using divide and conquer method. + +Time complexity : O(n log n) + +Ref : INTRODUCTION TO ALGORITHMS THIRD EDITION +(section : 4, sub-section : 4.1, page : 70) + +""" + + +def max_sum_from_start(array): + """This function finds the maximum contiguous sum of array from 0 index + + Parameters : + array (list[int]) : given array + + Returns : + max_sum (int) : maximum contiguous sum of array from 0 index + + """ + array_sum = 0 + max_sum = float("-inf") + for num in array: + array_sum += num + if array_sum > max_sum: + max_sum = array_sum + return max_sum + + +def max_cross_array_sum(array, left, mid, right): + """This function finds the maximum contiguous sum of left and right arrays + + Parameters : + array, left, mid, right (list[int], int, int, int) + + Returns : + (int) : maximum of sum of contiguous sum of left and right arrays + + """ + + max_sum_of_left = max_sum_from_start(array[left : mid + 1][::-1]) + max_sum_of_right = max_sum_from_start(array[mid + 1 : right + 1]) + return max_sum_of_left + max_sum_of_right + + +def max_subarray_sum(array, left, right): + """Maximum contiguous sub-array sum, using divide and conquer method + + Parameters : + array, left, right (list[int], int, int) : + given array, current left index and current right index + + Returns : + int : maximum of sum of contiguous sub-array + + """ + + # base case: array has only one element + if left == right: + return array[right] + + # Recursion + mid = (left + right) // 2 + left_half_sum = max_subarray_sum(array, left, mid) + right_half_sum = max_subarray_sum(array, mid + 1, right) + cross_sum = max_cross_array_sum(array, left, mid, right) + return max(left_half_sum, right_half_sum, cross_sum) + + +array = [-2, -5, 6, -2, -3, 1, 5, -6] +array_length = len(array) +print( + "Maximum sum of contiguous subarray:", max_subarray_sum(array, 0, array_length - 1) +) diff --git a/divide_and_conquer/mergesort.py b/divide_and_conquer/mergesort.py new file mode 100644 index 000000000000..46a46941cab3 --- /dev/null +++ b/divide_and_conquer/mergesort.py @@ -0,0 +1,112 @@ +from typing import List + + +def merge(left_half: List, right_half: List) -> List: + """Helper function for mergesort. + + >>> left_half = [-2] + >>> right_half = [-1] + >>> merge(left_half, right_half) + [-2, -1] + + >>> left_half = [1,2,3] + >>> right_half = [4,5,6] + >>> merge(left_half, right_half) + [1, 2, 3, 4, 5, 6] + + >>> left_half = [-2] + >>> right_half = [-1] + >>> merge(left_half, right_half) + [-2, -1] + + >>> left_half = [12, 15] + >>> right_half = [13, 14] + >>> merge(left_half, right_half) + [12, 13, 14, 15] + + >>> left_half = [] + >>> right_half = [] + >>> merge(left_half, right_half) + [] + """ + sorted_array = [None] * (len(right_half) + len(left_half)) + + pointer1 = 0 # pointer to current index for left Half + pointer2 = 0 # pointer to current index for the right Half + index = 0 # pointer to current index for the sorted array Half + + while pointer1 < len(left_half) and pointer2 < len(right_half): + if left_half[pointer1] < right_half[pointer2]: + sorted_array[index] = left_half[pointer1] + pointer1 += 1 + index += 1 + else: + sorted_array[index] = right_half[pointer2] + pointer2 += 1 + index += 1 + while pointer1 < len(left_half): + sorted_array[index] = left_half[pointer1] + pointer1 += 1 + index += 1 + + while pointer2 < len(right_half): + sorted_array[index] = right_half[pointer2] + pointer2 += 1 + index += 1 + + return sorted_array + + +def merge_sort(array: List) -> List: + """Returns a list of sorted array elements using merge sort. + + >>> from random import shuffle + >>> array = [-2, 3, -10, 11, 99, 100000, 100, -200] + >>> shuffle(array) + >>> merge_sort(array) + [-200, -10, -2, 3, 11, 99, 100, 100000] + + >>> shuffle(array) + >>> merge_sort(array) + [-200, -10, -2, 3, 11, 99, 100, 100000] + + >>> array = [-200] + >>> merge_sort(array) + [-200] + + >>> array = [-2, 3, -10, 11, 99, 100000, 100, -200] + >>> shuffle(array) + >>> sorted(array) == merge_sort(array) + True + + >>> array = [-2] + >>> merge_sort(array) + [-2] + + >>> array = [] + >>> merge_sort(array) + [] + + >>> array = [10000000, 1, -1111111111, 101111111112, 9000002] + >>> sorted(array) == merge_sort(array) + True + """ + if len(array) <= 1: + return array + # the actual formula to calculate the middle element = left + (right - left) // 2 + # this avoids integer overflow in case of large N + middle = 0 + (len(array) - 0) // 2 + + # Split the array into halves till the array length becomes equal to One + # merge the arrays of single length returned by mergeSort function and + # pass them into the merge arrays function which merges the array + left_half = array[:middle] + right_half = array[middle:] + + return merge(merge_sort(left_half), merge_sort(right_half)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/divide_and_conquer/peak.py b/divide_and_conquer/peak.py new file mode 100644 index 000000000000..f94f83ed3fcb --- /dev/null +++ b/divide_and_conquer/peak.py @@ -0,0 +1,53 @@ +""" +Finding the peak of a unimodal list using divide and conquer. +A unimodal array is defined as follows: array is increasing up to index p, +then decreasing afterwards. (for p >= 1) +An obvious solution can be performed in O(n), +to find the maximum of the array. +(From Kleinberg and Tardos. Algorithm Design. +Addison Wesley 2006: Chapter 5 Solved Exercise 1) +""" +from typing import List + + +def peak(lst: List[int]) -> int: + """ + Return the peak value of `lst`. + >>> peak([1, 2, 3, 4, 5, 4, 3, 2, 1]) + 5 + >>> peak([1, 10, 9, 8, 7, 6, 5, 4]) + 10 + >>> peak([1, 9, 8, 7]) + 9 + >>> peak([1, 2, 3, 4, 5, 6, 7, 0]) + 7 + >>> peak([1, 2, 3, 4, 3, 2, 1, 0, -1, -2]) + 4 + """ + # middle index + m = len(lst) // 2 + + # choose the middle 3 elements + three = lst[m - 1 : m + 2] + + # if middle element is peak + if three[1] > three[0] and three[1] > three[2]: + return three[1] + + # if increasing, recurse on right + elif three[0] < three[2]: + if len(lst[:m]) == 2: + m -= 1 + return peak(lst[m:]) + + # decreasing + else: + if len(lst[:m]) == 2: + m += 1 + return peak(lst[:m]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py new file mode 100644 index 000000000000..f2e023afd536 --- /dev/null +++ b/divide_and_conquer/power.py @@ -0,0 +1,33 @@ +def actual_power(a: int, b: int): + """ + Function using divide and conquer to calculate a^b. + It only works for integer a,b. + """ + if b == 0: + return 1 + if (b % 2) == 0: + return actual_power(a, int(b / 2)) * actual_power(a, int(b / 2)) + else: + return a * actual_power(a, int(b / 2)) * actual_power(a, int(b / 2)) + + +def power(a: int, b: int) -> float: + """ + >>> power(4,6) + 4096 + >>> power(2,3) + 8 + >>> power(-2,3) + -8 + >>> power(2,-3) + 0.125 + >>> power(-2,-3) + -0.125 + """ + if b < 0: + return 1 / actual_power(a, b) + return actual_power(a, b) + + +if __name__ == "__main__": + print(power(-2, -3)) diff --git a/divide_and_conquer/strassen_matrix_multiplication.py b/divide_and_conquer/strassen_matrix_multiplication.py new file mode 100644 index 000000000000..29a174daebf9 --- /dev/null +++ b/divide_and_conquer/strassen_matrix_multiplication.py @@ -0,0 +1,171 @@ +from __future__ import annotations + +import math + + +def default_matrix_multiplication(a: list, b: list) -> list: + """ + Multiplication only for 2x2 matrices + """ + if len(a) != 2 or len(a[0]) != 2 or len(b) != 2 or len(b[0]) != 2: + raise Exception("Matrices are not 2x2") + new_matrix = [ + [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]], + [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]], + ] + return new_matrix + + +def matrix_addition(matrix_a: list, matrix_b: list): + return [ + [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row]))] + for row in range(len(matrix_a)) + ] + + +def matrix_subtraction(matrix_a: list, matrix_b: list): + return [ + [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row]))] + for row in range(len(matrix_a)) + ] + + +def split_matrix(a: list) -> tuple[list, list, list, list]: + """ + Given an even length matrix, returns the top_left, top_right, bot_left, bot_right + quadrant. + + >>> split_matrix([[4,3,2,4],[2,3,1,1],[6,5,4,3],[8,4,1,6]]) + ([[4, 3], [2, 3]], [[2, 4], [1, 1]], [[6, 5], [8, 4]], [[4, 3], [1, 6]]) + >>> split_matrix([ + ... [4,3,2,4,4,3,2,4],[2,3,1,1,2,3,1,1],[6,5,4,3,6,5,4,3],[8,4,1,6,8,4,1,6], + ... [4,3,2,4,4,3,2,4],[2,3,1,1,2,3,1,1],[6,5,4,3,6,5,4,3],[8,4,1,6,8,4,1,6] + ... ]) # doctest: +NORMALIZE_WHITESPACE + ([[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], + [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1], + [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], + [8, 4, 1, 6]]) + """ + if len(a) % 2 != 0 or len(a[0]) % 2 != 0: + raise Exception("Odd matrices are not supported!") + + matrix_length = len(a) + mid = matrix_length // 2 + + top_right = [[a[i][j] for j in range(mid, matrix_length)] for i in range(mid)] + bot_right = [ + [a[i][j] for j in range(mid, matrix_length)] for i in range(mid, matrix_length) + ] + + top_left = [[a[i][j] for j in range(mid)] for i in range(mid)] + bot_left = [[a[i][j] for j in range(mid)] for i in range(mid, matrix_length)] + + return top_left, top_right, bot_left, bot_right + + +def matrix_dimensions(matrix: list) -> tuple[int, int]: + return len(matrix), len(matrix[0]) + + +def print_matrix(matrix: list) -> None: + for i in range(len(matrix)): + print(matrix[i]) + + +def actual_strassen(matrix_a: list, matrix_b: list) -> list: + """ + Recursive function to calculate the product of two matrices, using the Strassen + Algorithm. It only supports even length matrices. + """ + if matrix_dimensions(matrix_a) == (2, 2): + return default_matrix_multiplication(matrix_a, matrix_b) + + a, b, c, d = split_matrix(matrix_a) + e, f, g, h = split_matrix(matrix_b) + + t1 = actual_strassen(a, matrix_subtraction(f, h)) + t2 = actual_strassen(matrix_addition(a, b), h) + t3 = actual_strassen(matrix_addition(c, d), e) + t4 = actual_strassen(d, matrix_subtraction(g, e)) + t5 = actual_strassen(matrix_addition(a, d), matrix_addition(e, h)) + t6 = actual_strassen(matrix_subtraction(b, d), matrix_addition(g, h)) + t7 = actual_strassen(matrix_subtraction(a, c), matrix_addition(e, f)) + + top_left = matrix_addition(matrix_subtraction(matrix_addition(t5, t4), t2), t6) + top_right = matrix_addition(t1, t2) + bot_left = matrix_addition(t3, t4) + bot_right = matrix_subtraction(matrix_subtraction(matrix_addition(t1, t5), t3), t7) + + # construct the new matrix from our 4 quadrants + new_matrix = [] + for i in range(len(top_right)): + new_matrix.append(top_left[i] + top_right[i]) + for i in range(len(bot_right)): + new_matrix.append(bot_left[i] + bot_right[i]) + return new_matrix + + +def strassen(matrix1: list, matrix2: list) -> list: + """ + >>> strassen([[2,1,3],[3,4,6],[1,4,2],[7,6,7]], [[4,2,3,4],[2,1,1,1],[8,6,4,2]]) + [[34, 23, 19, 15], [68, 46, 37, 28], [28, 18, 15, 12], [96, 62, 55, 48]] + >>> strassen([[3,7,5,6,9],[1,5,3,7,8],[1,4,4,5,7]], [[2,4],[5,2],[1,7],[5,5],[7,8]]) + [[139, 163], [121, 134], [100, 121]] + """ + if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]: + raise Exception( + f"Unable to multiply these matrices, please check the dimensions. \n" + f"Matrix A:{matrix1} \nMatrix B:{matrix2}" + ) + dimension1 = matrix_dimensions(matrix1) + dimension2 = matrix_dimensions(matrix2) + + if dimension1[0] == dimension1[1] and dimension2[0] == dimension2[1]: + return matrix1, matrix2 + + maximum = max(max(dimension1), max(dimension2)) + maxim = int(math.pow(2, math.ceil(math.log2(maximum)))) + new_matrix1 = matrix1 + new_matrix2 = matrix2 + + # Adding zeros to the matrices so that the arrays dimensions are the same and also + # power of 2 + for i in range(0, maxim): + if i < dimension1[0]: + for j in range(dimension1[1], maxim): + new_matrix1[i].append(0) + else: + new_matrix1.append([0] * maxim) + if i < dimension2[0]: + for j in range(dimension2[1], maxim): + new_matrix2[i].append(0) + else: + new_matrix2.append([0] * maxim) + + final_matrix = actual_strassen(new_matrix1, new_matrix2) + + # Removing the additional zeros + for i in range(0, maxim): + if i < dimension1[0]: + for j in range(dimension2[1], maxim): + final_matrix[i].pop() + else: + final_matrix.pop() + return final_matrix + + +if __name__ == "__main__": + matrix1 = [ + [2, 3, 4, 5], + [6, 4, 3, 1], + [2, 3, 6, 7], + [3, 1, 2, 4], + [2, 3, 4, 5], + [6, 4, 3, 1], + [2, 3, 6, 7], + [3, 1, 2, 4], + [2, 3, 4, 5], + [6, 2, 3, 1], + ] + matrix2 = [[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]] + print(strassen(matrix1, matrix2)) diff --git a/dynamic_programming/FractionalKnapsack.py b/dynamic_programming/FractionalKnapsack.py deleted file mode 100644 index 74e85b4b4708..000000000000 --- a/dynamic_programming/FractionalKnapsack.py +++ /dev/null @@ -1,12 +0,0 @@ -from itertools import accumulate -from bisect import bisect - -def fracKnapsack(vl, wt, W, n): - - r = list(sorted(zip(vl,wt), key=lambda x:x[0]/x[1],reverse=True)) - vl , wt = [i[0] for i in r],[i[1] for i in r] - acc=list(accumulate(wt)) - k = bisect(acc,W) - return 0 if k == 0 else sum(vl[:k])+(W-acc[k-1])*(vl[k])/(wt[k]) if k!=n else sum(vl[:k]) - -print("%.0f"%fracKnapsack([60, 100, 120],[10, 20, 30],50,3)) diff --git a/dynamic_programming/__init__.py b/dynamic_programming/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/dynamic_programming/abbreviation.py b/dynamic_programming/abbreviation.py index f4d07e402925..5175aa9ed92f 100644 --- a/dynamic_programming/abbreviation.py +++ b/dynamic_programming/abbreviation.py @@ -12,7 +12,13 @@ """ -def abbr(a, b): +def abbr(a: str, b: str) -> bool: + """ + >>> abbr("daBcd", "ABC") + True + >>> abbr("dBcd", "ABC") + False + """ n = len(a) m = len(b) dp = [[False for _ in range(m + 1)] for _ in range(n + 1)] @@ -28,4 +34,6 @@ def abbr(a, b): if __name__ == "__main__": - print(abbr("daBcd", "ABC")) # expect True + import doctest + + doctest.testmod() diff --git a/dynamic_programming/bitmask.py b/dynamic_programming/bitmask.py index 213b22fe9051..2994db5b5e1e 100644 --- a/dynamic_programming/bitmask.py +++ b/dynamic_programming/bitmask.py @@ -1,90 +1,93 @@ """ -This is a python implementation for questions involving task assignments between people. +This is a Python implementation for questions involving task assignments between people. Here Bitmasking and DP are used for solving this. Question :- -We have N tasks and M people. Each person in M can do only certain of these tasks. Also a person can do only one task and a task is performed only by one person. +We have N tasks and M people. Each person in M can do only certain of these tasks. Also +a person can do only one task and a task is performed only by one person. Find the total no of ways in which the tasks can be distributed. - - """ -from __future__ import print_function from collections import defaultdict class AssignmentUsingBitmask: - def __init__(self,task_performed,total): - - self.total_tasks = total #total no of tasks (N) - + def __init__(self, task_performed, total): + + self.total_tasks = total # total no of tasks (N) + # DP table will have a dimension of (2^M)*N # initially all values are set to -1 - self.dp = [[-1 for i in range(total+1)] for j in range(2**len(task_performed))] - - self.task = defaultdict(list) #stores the list of persons for each task - - #finalmask is used to check if all persons are included by setting all bits to 1 - self.finalmask = (1< self.total_tasks: + # if not everyone gets the task and no more tasks are available, return 0 + if task_no > self.total_tasks: return 0 - #if case already considered - if self.dp[mask][taskno]!=-1: - return self.dp[mask][taskno] + # if case already considered + if self.dp[mask][task_no] != -1: + return self.dp[mask][task_no] + + # Number of ways when we don't this task in the arrangement + total_ways_util = self.CountWaysUtil(mask, task_no + 1) - # Number of ways when we dont this task in the arrangement - total_ways_util = self.CountWaysUtil(mask,taskno+1) + # now assign the tasks one by one to all possible persons and recursively + # assign for the remaining tasks. + if task_no in self.task: + for p in self.task[task_no]: - # now assign the tasks one by one to all possible persons and recursively assign for the remaining tasks. - if taskno in self.task: - for p in self.task[taskno]: - # if p is already given a task - if mask & (1< int: + """ + LeetCdoe No.70: Climbing Stairs + Distinct ways to climb a n step staircase where + each time you can either climb 1 or 2 steps. + + Args: + n: number of steps of staircase + + Returns: + Distinct ways to climb a n step staircase + + Raises: + AssertionError: n not positive integer + + >>> climb_stairs(3) + 3 + >>> climb_stairs(1) + 1 + >>> climb_stairs(-7) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + AssertionError: n needs to be positive integer, your input -7 + """ + fmt = "n needs to be positive integer, your input {}" + assert isinstance(n, int) and n > 0, fmt.format(n) + if n == 1: + return 1 + dp = [0] * (n + 1) + dp[0], dp[1] = (1, 1) + for i in range(2, n + 1): + dp[i] = dp[i - 1] + dp[i - 2] + return dp[n] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/coin_change.py b/dynamic_programming/coin_change.py index 74d86661f52d..2869b5857be1 100644 --- a/dynamic_programming/coin_change.py +++ b/dynamic_programming/coin_change.py @@ -5,11 +5,25 @@ the given types of coins? https://www.hackerrank.com/challenges/coin-change/problem """ -from __future__ import print_function -def dp_count(S, m, n): - +def dp_count(S, n): + """ + >>> dp_count([1, 2, 3], 4) + 4 + >>> dp_count([1, 2, 3], 7) + 8 + >>> dp_count([2, 5, 3, 6], 10) + 5 + >>> dp_count([10], 99) + 0 + >>> dp_count([4, 5, 6], 0) + 1 + >>> dp_count([1, 2, 3], -5) + 0 + """ + if n < 0: + return 0 # table[i] represents the number of ways to get to amount i table = [0] * (n + 1) @@ -26,6 +40,7 @@ def dp_count(S, m, n): return table[n] -if __name__ == '__main__': - print(dp_count([1, 2, 3], 3, 4)) # answer 4 - print(dp_count([2, 5, 3, 6], 4, 10)) # answer 5 +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/edit_distance.py b/dynamic_programming/edit_distance.py index 335e5196ed53..56877e0c50a2 100644 --- a/dynamic_programming/edit_distance.py +++ b/dynamic_programming/edit_distance.py @@ -2,12 +2,13 @@ Author : Turfa Auliarachman Date : October 12, 2016 -This is a pure Python implementation of Dynamic Programming solution to the edit distance problem. +This is a pure Python implementation of Dynamic Programming solution to the edit +distance problem. The problem is : -Given two strings A and B. Find the minimum number of operations to string B such that A = B. The permitted operations are removal, insertion, and substitution. +Given two strings A and B. Find the minimum number of operations to string B such that +A = B. The permitted operations are removal, insertion, and substitution. """ -from __future__ import print_function class EditDistance: @@ -20,56 +21,85 @@ class EditDistance: def __init__(self): self.__prepare__() - def __prepare__(self, N = 0, M = 0): - self.dp = [[-1 for y in range(0,M)] for x in range(0,N)] + def __prepare__(self, N=0, M=0): + self.dp = [[-1 for y in range(0, M)] for x in range(0, N)] def __solveDP(self, x, y): - if (x==-1): - return y+1 - elif (y==-1): - return x+1 - elif (self.dp[x][y]>-1): + if x == -1: + return y + 1 + elif y == -1: + return x + 1 + elif self.dp[x][y] > -1: return self.dp[x][y] else: - if (self.A[x]==self.B[y]): - self.dp[x][y] = self.__solveDP(x-1,y-1) + if self.A[x] == self.B[y]: + self.dp[x][y] = self.__solveDP(x - 1, y - 1) else: - self.dp[x][y] = 1+min(self.__solveDP(x,y-1), self.__solveDP(x-1,y), self.__solveDP(x-1,y-1)) + self.dp[x][y] = 1 + min( + self.__solveDP(x, y - 1), + self.__solveDP(x - 1, y), + self.__solveDP(x - 1, y - 1), + ) return self.dp[x][y] def solve(self, A, B): - if isinstance(A,bytes): - A = A.decode('ascii') + if isinstance(A, bytes): + A = A.decode("ascii") - if isinstance(B,bytes): - B = B.decode('ascii') + if isinstance(B, bytes): + B = B.decode("ascii") self.A = str(A) self.B = str(B) self.__prepare__(len(A), len(B)) - return self.__solveDP(len(A)-1, len(B)-1) + return self.__solveDP(len(A) - 1, len(B) - 1) -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - solver = EditDistance() +def min_distance_bottom_up(word1: str, word2: str) -> int: + """ + >>> min_distance_bottom_up("intention", "execution") + 5 + >>> min_distance_bottom_up("intention", "") + 9 + >>> min_distance_bottom_up("", "") + 0 + """ + m = len(word1) + n = len(word2) + dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)] + for i in range(m + 1): + for j in range(n + 1): + + if i == 0: # first string is empty + dp[i][j] = j + elif j == 0: # second string is empty + dp[i][j] = i + elif ( + word1[i - 1] == word2[j - 1] + ): # last character of both substing is equal + dp[i][j] = dp[i - 1][j - 1] + else: + insert = dp[i][j - 1] + delete = dp[i - 1][j] + replace = dp[i - 1][j - 1] + dp[i][j] = 1 + min(insert, delete, replace) + return dp[m][n] + - print("****************** Testing Edit Distance DP Algorithm ******************") - print() +if __name__ == "__main__": + solver = EditDistance() - print("Enter the first string: ", end="") - S1 = raw_input().strip() + print("****************** Testing Edit Distance DP Algorithm ******************") + print() - print("Enter the second string: ", end="") - S2 = raw_input().strip() + S1 = input("Enter the first string: ").strip() + S2 = input("Enter the second string: ").strip() - print() - print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2))) - print() - print("*************** End of Testing Edit Distance DP Algorithm ***************") + print() + print("The minimum Edit Distance is: %d" % (solver.solve(S1, S2))) + print("The minimum Edit Distance is: %d" % (min_distance_bottom_up(S1, S2))) + print() + print("*************** End of Testing Edit Distance DP Algorithm ***************") diff --git a/dynamic_programming/factorial.py b/dynamic_programming/factorial.py new file mode 100644 index 000000000000..1c9c927f5af3 --- /dev/null +++ b/dynamic_programming/factorial.py @@ -0,0 +1,27 @@ +# Factorial of a number using memoization + +from functools import lru_cache + + +@lru_cache +def factorial(num: int) -> int: + """ + >>> factorial(7) + 5040 + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: Number should not be negative. + >>> [factorial(i) for i in range(10)] + [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] + """ + if num < 0: + raise ValueError("Number should not be negative.") + + return 1 if num in (0, 1) else num * factorial(num - 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/fast_fibonacci.py b/dynamic_programming/fast_fibonacci.py new file mode 100644 index 000000000000..f48186a34c25 --- /dev/null +++ b/dynamic_programming/fast_fibonacci.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +""" +This program calculates the nth Fibonacci number in O(log(n)). +It's possible to calculate F(1_000_000) in less than a second. +""" +from __future__ import annotations + +import sys + + +def fibonacci(n: int) -> int: + """ + return F(n) + >>> [fibonacci(i) for i in range(13)] + [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144] + """ + if n < 0: + raise ValueError("Negative arguments are not supported") + return _fib(n)[0] + + +# returns (F(n), F(n-1)) +def _fib(n: int) -> tuple[int, int]: + if n == 0: # (F(0), F(1)) + return (0, 1) + + # F(2n) = F(n)[2F(n+1) − F(n)] + # F(2n+1) = F(n+1)^2+F(n)^2 + a, b = _fib(n // 2) + c = a * (b * 2 - a) + d = a * a + b * b + return (d, c + d) if n % 2 else (c, d) + + +if __name__ == "__main__": + n = int(sys.argv[1]) + print(f"fibonacci({n}) is {fibonacci(n)}") diff --git a/dynamic_programming/fastfibonacci.py b/dynamic_programming/fastfibonacci.py deleted file mode 100644 index cbc118467b3c..000000000000 --- a/dynamic_programming/fastfibonacci.py +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/python -# encoding=utf8 - -""" -This program calculates the nth Fibonacci number in O(log(n)). -It's possible to calculate F(1000000) in less than a second. -""" -from __future__ import print_function -import sys - - -# returns F(n) -def fibonacci(n: int): # noqa: E999 This syntax is Python 3 only - if n < 0: - raise ValueError("Negative arguments are not supported") - return _fib(n)[0] - - -# returns (F(n), F(n-1)) -def _fib(n: int): # noqa: E999 This syntax is Python 3 only - if n == 0: - # (F(0), F(1)) - return (0, 1) - else: - # F(2n) = F(n)[2F(n+1) − F(n)] - # F(2n+1) = F(n+1)^2+F(n)^2 - a, b = _fib(n // 2) - c = a * (b * 2 - a) - d = a * a + b * b - if n % 2 == 0: - return (c, d) - else: - return (d, c + d) - - -if __name__ == "__main__": - args = sys.argv[1:] - if len(args) != 1: - print("Too few or too much parameters given.") - exit(1) - try: - n = int(args[0]) - except ValueError: - print("Could not convert data to an integer.") - exit(1) - print("F(%d) = %d" % (n, fibonacci(n))) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index b453ce255853..cab1358ddea1 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -1,11 +1,10 @@ """ -This is a pure Python implementation of Dynamic Programming solution to the fibonacci sequence problem. +This is a pure Python implementation of Dynamic Programming solution to the fibonacci +sequence problem. """ -from __future__ import print_function class Fibonacci: - def __init__(self, N=None): self.fib_array = [] if N: @@ -16,34 +15,42 @@ def __init__(self, N=None): self.fib_array.append(self.fib_array[i - 1] + self.fib_array[i - 2]) elif N == 0: self.fib_array.append(0) + print(self.fib_array) def get(self, sequence_no=None): - if sequence_no != None: + """ + >>> Fibonacci(5).get(3) + [0, 1, 1, 2, 3, 5] + [0, 1, 1, 2] + >>> Fibonacci(5).get(6) + [0, 1, 1, 2, 3, 5] + Out of bound. + >>> Fibonacci(5).get(-1) + [0, 1, 1, 2, 3, 5] + [] + """ + if sequence_no is not None: if sequence_no < len(self.fib_array): - return print(self.fib_array[:sequence_no + 1]) + return print(self.fib_array[: sequence_no + 1]) else: print("Out of bound.") else: print("Please specify a value") -if __name__ == '__main__': +if __name__ == "__main__": print("\n********* Fibonacci Series Using Dynamic Programming ************\n") - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - print("\n Enter the upper limit for the fibonacci sequence: ", end="") try: - N = eval(raw_input().strip()) + N = int(input().strip()) fib = Fibonacci(N) print( - "\n********* Enter different values to get the corresponding fibonacci sequence, enter any negative number to exit. ************\n") + "\n********* Enter different values to get the corresponding fibonacci " + "sequence, enter any negative number to exit. ************\n" + ) while True: - print("Enter value: ", end=" ") try: - i = eval(raw_input().strip()) + i = int(input("Enter value: ").strip()) if i < 0: print("\n********* Good Bye!! ************\n") break @@ -52,3 +59,7 @@ def get(self, sequence_no=None): print("\nInvalid input, please try again.") except NameError: print("\n********* Invalid input, good bye!! ************\n") + + import doctest + + doctest.testmod() diff --git a/dynamic_programming/floyd_warshall.py b/dynamic_programming/floyd_warshall.py index 038499ca03b6..a4b6c6a82568 100644 --- a/dynamic_programming/floyd_warshall.py +++ b/dynamic_programming/floyd_warshall.py @@ -1,37 +1,42 @@ import math + class Graph: - - def __init__(self, N = 0): # a graph with Node 0,1,...,N-1 + def __init__(self, N=0): # a graph with Node 0,1,...,N-1 self.N = N - self.W = [[math.inf for j in range(0,N)] for i in range(0,N)] # adjacency matrix for weight - self.dp = [[math.inf for j in range(0,N)] for i in range(0,N)] # dp[i][j] stores minimum distance from i to j + self.W = [ + [math.inf for j in range(0, N)] for i in range(0, N) + ] # adjacency matrix for weight + self.dp = [ + [math.inf for j in range(0, N)] for i in range(0, N) + ] # dp[i][j] stores minimum distance from i to j def addEdge(self, u, v, w): self.dp[u][v] = w def floyd_warshall(self): - for k in range(0,self.N): - for i in range(0,self.N): - for j in range(0,self.N): + for k in range(0, self.N): + for i in range(0, self.N): + for j in range(0, self.N): self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j]) def showMin(self, u, v): return self.dp[u][v] - -if __name__ == '__main__': + + +if __name__ == "__main__": graph = Graph(5) - graph.addEdge(0,2,9) - graph.addEdge(0,4,10) - graph.addEdge(1,3,5) - graph.addEdge(2,3,7) - graph.addEdge(3,0,10) - graph.addEdge(3,1,2) - graph.addEdge(3,2,1) - graph.addEdge(3,4,6) - graph.addEdge(4,1,3) - graph.addEdge(4,2,4) - graph.addEdge(4,3,9) + graph.addEdge(0, 2, 9) + graph.addEdge(0, 4, 10) + graph.addEdge(1, 3, 5) + graph.addEdge(2, 3, 7) + graph.addEdge(3, 0, 10) + graph.addEdge(3, 1, 2) + graph.addEdge(3, 2, 1) + graph.addEdge(3, 4, 6) + graph.addEdge(4, 1, 3) + graph.addEdge(4, 2, 4) + graph.addEdge(4, 3, 9) graph.floyd_warshall() - graph.showMin(1,4) - graph.showMin(0,3) + graph.showMin(1, 4) + graph.showMin(0, 3) diff --git a/dynamic_programming/fractional_knapsack.py b/dynamic_programming/fractional_knapsack.py new file mode 100644 index 000000000000..c74af7ef8fc5 --- /dev/null +++ b/dynamic_programming/fractional_knapsack.py @@ -0,0 +1,27 @@ +from bisect import bisect +from itertools import accumulate + + +def fracKnapsack(vl, wt, W, n): + """ + >>> fracKnapsack([60, 100, 120], [10, 20, 30], 50, 3) + 240.0 + """ + + r = list(sorted(zip(vl, wt), key=lambda x: x[0] / x[1], reverse=True)) + vl, wt = [i[0] for i in r], [i[1] for i in r] + acc = list(accumulate(wt)) + k = bisect(acc, W) + return ( + 0 + if k == 0 + else sum(vl[:k]) + (W - acc[k - 1]) * (vl[k]) / (wt[k]) + if k != n + else sum(vl[:k]) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/fractional_knapsack_2.py b/dynamic_programming/fractional_knapsack_2.py new file mode 100644 index 000000000000..cae57738311b --- /dev/null +++ b/dynamic_programming/fractional_knapsack_2.py @@ -0,0 +1,60 @@ +# https://en.wikipedia.org/wiki/Continuous_knapsack_problem +# https://www.guru99.com/fractional-knapsack-problem-greedy.html +# https://medium.com/walkinthecode/greedy-algorithm-fractional-knapsack-problem-9aba1daecc93 + +from __future__ import annotations + + +def fractional_knapsack( + value: list[int], weight: list[int], capacity: int +) -> tuple[int, list[int]]: + """ + >>> value = [1, 3, 5, 7, 9] + >>> weight = [0.9, 0.7, 0.5, 0.3, 0.1] + >>> fractional_knapsack(value, weight, 5) + (25, [1, 1, 1, 1, 1]) + >>> fractional_knapsack(value, weight, 15) + (25, [1, 1, 1, 1, 1]) + >>> fractional_knapsack(value, weight, 25) + (25, [1, 1, 1, 1, 1]) + >>> fractional_knapsack(value, weight, 26) + (25, [1, 1, 1, 1, 1]) + >>> fractional_knapsack(value, weight, -1) + (-90.0, [0, 0, 0, 0, -10.0]) + >>> fractional_knapsack([1, 3, 5, 7], weight, 30) + (16, [1, 1, 1, 1]) + >>> fractional_knapsack(value, [0.9, 0.7, 0.5, 0.3, 0.1], 30) + (25, [1, 1, 1, 1, 1]) + >>> fractional_knapsack([], [], 30) + (0, []) + """ + index = list(range(len(value))) + ratio = [v / w for v, w in zip(value, weight)] + index.sort(key=lambda i: ratio[i], reverse=True) + + max_value = 0 + fractions = [0] * len(value) + for i in index: + if weight[i] <= capacity: + fractions[i] = 1 + max_value += value[i] + capacity -= weight[i] + else: + fractions[i] = capacity / weight[i] + max_value += value[i] * capacity / weight[i] + break + + return max_value, fractions + + +if __name__ == "__main__": + n = int(input("Enter number of items: ")) + value = input(f"Enter the values of the {n} item(s) in order: ").split() + value = [int(v) for v in value] + weight = input(f"Enter the positive weights of the {n} item(s) in order: ".split()) + weight = [int(w) for w in weight] + capacity = int(input("Enter maximum weight: ")) + + max_value, fractions = fractional_knapsack(value, weight, capacity) + print("The maximum value of items that can be carried:", max_value) + print("The fractions in which the items should be taken:", fractions) diff --git a/dynamic_programming/integer_partition.py b/dynamic_programming/integer_partition.py index 7b27afebaa6c..4eb06348ce84 100644 --- a/dynamic_programming/integer_partition.py +++ b/dynamic_programming/integer_partition.py @@ -1,45 +1,37 @@ -from __future__ import print_function +""" +The number of partitions of a number n into at least k parts equals the number of +partitions into exactly k parts plus the number of partitions into at least k-1 parts. +Subtracting 1 from each part of a partition of n into k parts gives a partition of n-k +into k parts. These two facts together are used for this algorithm. +""" -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 -try: - raw_input #Python 2 -except NameError: - raw_input = input #Python 3 - -''' -The number of partitions of a number n into at least k parts equals the number of partitions into exactly k parts -plus the number of partitions into at least k-1 parts. Subtracting 1 from each part of a partition of n into k parts -gives a partition of n-k into k parts. These two facts together are used for this algorithm. -''' def partition(m): - memo = [[0 for _ in xrange(m)] for _ in xrange(m+1)] - for i in xrange(m+1): - memo[i][0] = 1 + memo = [[0 for _ in range(m)] for _ in range(m + 1)] + for i in range(m + 1): + memo[i][0] = 1 + + for n in range(m + 1): + for k in range(1, m): + memo[n][k] += memo[n][k - 1] + if n - k > 0: + memo[n][k] += memo[n - k - 1][k] - for n in xrange(m+1): - for k in xrange(1, m): - memo[n][k] += memo[n][k-1] - if n-k > 0: - memo[n][k] += memo[n-k-1][k] + return memo[m][m - 1] - return memo[m][m-1] -if __name__ == '__main__': - import sys +if __name__ == "__main__": + import sys - if len(sys.argv) == 1: - try: - n = int(raw_input('Enter a number: ')) - print(partition(n)) - except ValueError: - print('Please enter a number.') - else: - try: - n = int(sys.argv[1]) - print(partition(n)) - except ValueError: - print('Please pass a number.') \ No newline at end of file + if len(sys.argv) == 1: + try: + n = int(input("Enter a number: ").strip()) + print(partition(n)) + except ValueError: + print("Please enter a number.") + else: + try: + n = int(sys.argv[1]) + print(partition(n)) + except ValueError: + print("Please pass a number.") diff --git a/dynamic_programming/iterating_through_submasks.py b/dynamic_programming/iterating_through_submasks.py new file mode 100644 index 000000000000..855af61d6707 --- /dev/null +++ b/dynamic_programming/iterating_through_submasks.py @@ -0,0 +1,61 @@ +""" +Author : Syed Faizan (3rd Year Student IIIT Pune) +github : faizan2700 +You are given a bitmask m and you want to efficiently iterate through all of +its submasks. The mask s is submask of m if only bits that were included in +bitmask are set +""" +from __future__ import annotations + + +def list_of_submasks(mask: int) -> list[int]: + + """ + Args: + mask : number which shows mask ( always integer > 0, zero does not have any + submasks ) + + Returns: + all_submasks : the list of submasks of mask (mask s is called submask of mask + m if only bits that were included in original mask are set + + Raises: + AssertionError: mask not positive integer + + >>> list_of_submasks(15) + [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] + >>> list_of_submasks(13) + [13, 12, 9, 8, 5, 4, 1] + >>> list_of_submasks(-7) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + AssertionError: mask needs to be positive integer, your input -7 + >>> list_of_submasks(0) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + AssertionError: mask needs to be positive integer, your input 0 + + """ + + fmt = "mask needs to be positive integer, your input {}" + assert isinstance(mask, int) and mask > 0, fmt.format(mask) + + """ + first submask iterated will be mask itself then operation will be performed + to get other submasks till we reach empty submask that is zero ( zero is not + included in final submasks list ) + """ + all_submasks = [] + submask = mask + + while submask: + all_submasks.append(submask) + submask = (submask - 1) & mask + + return all_submasks + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/k_means_clustering_tensorflow.py b/dynamic_programming/k_means_clustering_tensorflow.py_tf similarity index 51% rename from dynamic_programming/k_means_clustering_tensorflow.py rename to dynamic_programming/k_means_clustering_tensorflow.py_tf index b6813c6a22b3..4fbcedeaa0dc 100644 --- a/dynamic_programming/k_means_clustering_tensorflow.py +++ b/dynamic_programming/k_means_clustering_tensorflow.py_tf @@ -14,24 +14,24 @@ def TFKMeansCluster(vectors, noofclusters): noofclusters = int(noofclusters) assert noofclusters < len(vectors) - #Find out the dimensionality + # Find out the dimensionality dim = len(vectors[0]) - #Will help select random centroids from among the available vectors + # Will help select random centroids from among the available vectors vector_indices = list(range(len(vectors))) shuffle(vector_indices) - #GRAPH OF COMPUTATION - #We initialize a new graph and set it as the default during each run - #of this algorithm. This ensures that as this function is called - #multiple times, the default graph doesn't keep getting crowded with - #unused ops and Variables from previous function calls. + # GRAPH OF COMPUTATION + # We initialize a new graph and set it as the default during each run + # of this algorithm. This ensures that as this function is called + # multiple times, the default graph doesn't keep getting crowded with + # unused ops and Variables from previous function calls. graph = tf.Graph() with graph.as_default(): - #SESSION OF COMPUTATION + # SESSION OF COMPUTATION sess = tf.Session() @@ -39,8 +39,9 @@ def TFKMeansCluster(vectors, noofclusters): ##First lets ensure we have a Variable vector for each centroid, ##initialized to one of the vectors from the available data points - centroids = [tf.Variable((vectors[vector_indices[i]])) - for i in range(noofclusters)] + centroids = [ + tf.Variable(vectors[vector_indices[i]]) for i in range(noofclusters) + ] ##These nodes will assign the centroid Variables the appropriate ##values centroid_value = tf.placeholder("float64", [dim]) @@ -56,26 +57,24 @@ def TFKMeansCluster(vectors, noofclusters): assignment_value = tf.placeholder("int32") cluster_assigns = [] for assignment in assignments: - cluster_assigns.append(tf.assign(assignment, - assignment_value)) + cluster_assigns.append(tf.assign(assignment, assignment_value)) ##Now lets construct the node that will compute the mean - #The placeholder for the input + # The placeholder for the input mean_input = tf.placeholder("float", [None, dim]) - #The Node/op takes the input and computes a mean along the 0th - #dimension, i.e. the list of input vectors + # The Node/op takes the input and computes a mean along the 0th + # dimension, i.e. the list of input vectors mean_op = tf.reduce_mean(mean_input, 0) ##Node for computing Euclidean distances - #Placeholders for input + # Placeholders for input v1 = tf.placeholder("float", [dim]) v2 = tf.placeholder("float", [dim]) - euclid_dist = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub( - v1, v2), 2))) + euclid_dist = tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(v1, v2), 2))) ##This node will figure out which cluster to assign a vector to, ##based on Euclidean distances of the vector from the centroids. - #Placeholder for input + # Placeholder for input centroid_distances = tf.placeholder("float", [noofclusters]) cluster_assignment = tf.argmin(centroid_distances, 0) @@ -87,55 +86,62 @@ def TFKMeansCluster(vectors, noofclusters): ##will be included in the initialization. init_op = tf.initialize_all_variables() - #Initialize all variables + # Initialize all variables sess.run(init_op) ##CLUSTERING ITERATIONS - #Now perform the Expectation-Maximization steps of K-Means clustering - #iterations. To keep things simple, we will only do a set number of - #iterations, instead of using a Stopping Criterion. + # Now perform the Expectation-Maximization steps of K-Means clustering + # iterations. To keep things simple, we will only do a set number of + # iterations, instead of using a Stopping Criterion. noofiterations = 100 for iteration_n in range(noofiterations): ##EXPECTATION STEP ##Based on the centroid locations till last iteration, compute ##the _expected_ centroid assignments. - #Iterate over each vector + # Iterate over each vector for vector_n in range(len(vectors)): vect = vectors[vector_n] - #Compute Euclidean distance between this vector and each - #centroid. Remember that this list cannot be named + # Compute Euclidean distance between this vector and each + # centroid. Remember that this list cannot be named #'centroid_distances', since that is the input to the - #cluster assignment node. - distances = [sess.run(euclid_dist, feed_dict={ - v1: vect, v2: sess.run(centroid)}) - for centroid in centroids] - #Now use the cluster assignment node, with the distances - #as the input - assignment = sess.run(cluster_assignment, feed_dict = { - centroid_distances: distances}) - #Now assign the value to the appropriate state variable - sess.run(cluster_assigns[vector_n], feed_dict={ - assignment_value: assignment}) + # cluster assignment node. + distances = [ + sess.run(euclid_dist, feed_dict={v1: vect, v2: sess.run(centroid)}) + for centroid in centroids + ] + # Now use the cluster assignment node, with the distances + # as the input + assignment = sess.run( + cluster_assignment, feed_dict={centroid_distances: distances} + ) + # Now assign the value to the appropriate state variable + sess.run( + cluster_assigns[vector_n], feed_dict={assignment_value: assignment} + ) ##MAXIMIZATION STEP - #Based on the expected state computed from the Expectation Step, - #compute the locations of the centroids so as to maximize the - #overall objective of minimizing within-cluster Sum-of-Squares + # Based on the expected state computed from the Expectation Step, + # compute the locations of the centroids so as to maximize the + # overall objective of minimizing within-cluster Sum-of-Squares for cluster_n in range(noofclusters): - #Collect all the vectors assigned to this cluster - assigned_vects = [vectors[i] for i in range(len(vectors)) - if sess.run(assignments[i]) == cluster_n] - #Compute new centroid location - new_location = sess.run(mean_op, feed_dict={ - mean_input: array(assigned_vects)}) - #Assign value to appropriate variable - sess.run(cent_assigns[cluster_n], feed_dict={ - centroid_value: new_location}) - - #Return centroids and assignments + # Collect all the vectors assigned to this cluster + assigned_vects = [ + vectors[i] + for i in range(len(vectors)) + if sess.run(assignments[i]) == cluster_n + ] + # Compute new centroid location + new_location = sess.run( + mean_op, feed_dict={mean_input: array(assigned_vects)} + ) + # Assign value to appropriate variable + sess.run( + cent_assigns[cluster_n], feed_dict={centroid_value: new_location} + ) + + # Return centroids and assignments centroids = sess.run(centroids) assignments = sess.run(assignments) return centroids, assignments - diff --git a/dynamic_programming/knapsack.py b/dynamic_programming/knapsack.py index 27d1cfed799b..69e54c00aa4e 100644 --- a/dynamic_programming/knapsack.py +++ b/dynamic_programming/knapsack.py @@ -1,42 +1,150 @@ """ -Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. +Given weights and values of n items, put these items in a knapsack of + capacity W to get the maximum total value in the knapsack. + +Note that only the integer weights 0-1 knapsack problem is solvable + using dynamic programming. """ -def MF_knapsack(i,wt,val,j): - ''' - This code involves the concept of memory functions. Here we solve the subproblems which are needed - unlike the below example + + +def MF_knapsack(i, wt, val, j): + """ + This code involves the concept of memory functions. Here we solve the subproblems + which are needed unlike the below example F is a 2D array with -1s filled up - ''' + """ global F # a global dp table for knapsack if F[i][j] < 0: if j < wt[i - 1]: - val = MF_knapsack(i - 1,wt,val,j) + val = MF_knapsack(i - 1, wt, val, j) else: - val = max(MF_knapsack(i - 1,wt,val,j),MF_knapsack(i - 1,wt,val,j - wt[i - 1]) + val[i - 1]) + val = max( + MF_knapsack(i - 1, wt, val, j), + MF_knapsack(i - 1, wt, val, j - wt[i - 1]) + val[i - 1], + ) F[i][j] = val return F[i][j] + def knapsack(W, wt, val, n): - dp = [[0 for i in range(W+1)]for j in range(n+1)] + dp = [[0 for i in range(W + 1)] for j in range(n + 1)] - for i in range(1,n+1): - for w in range(1,W+1): - if(wt[i-1]<=w): - dp[i][w] = max(val[i-1]+dp[i-1][w-wt[i-1]],dp[i-1][w]) + for i in range(1, n + 1): + for w in range(1, W + 1): + if wt[i - 1] <= w: + dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]) else: - dp[i][w] = dp[i-1][w] + dp[i][w] = dp[i - 1][w] + + return dp[n][W], dp + + +def knapsack_with_example_solution(W: int, wt: list, val: list): + """ + Solves the integer weights knapsack problem returns one of + the several possible optimal subsets. + + Parameters + --------- + + W: int, the total maximum weight for the given knapsack problem. + wt: list, the vector of weights for all items where wt[i] is the weight + of the i-th item. + val: list, the vector of values for all items where val[i] is the value + of the i-th item + + Returns + ------- + optimal_val: float, the optimal value for the given knapsack problem + example_optional_set: set, the indices of one of the optimal subsets + which gave rise to the optimal value. + + Examples + ------- + >>> knapsack_with_example_solution(10, [1, 3, 5, 2], [10, 20, 100, 22]) + (142, {2, 3, 4}) + >>> knapsack_with_example_solution(6, [4, 3, 2, 3], [3, 2, 4, 4]) + (8, {3, 4}) + >>> knapsack_with_example_solution(6, [4, 3, 2, 3], [3, 2, 4]) + Traceback (most recent call last): + ... + ValueError: The number of weights must be the same as the number of values. + But got 4 weights and 3 values + """ + if not (isinstance(wt, (list, tuple)) and isinstance(val, (list, tuple))): + raise ValueError( + "Both the weights and values vectors must be either lists or tuples" + ) + + num_items = len(wt) + if num_items != len(val): + raise ValueError( + "The number of weights must be the " + "same as the number of values.\nBut " + f"got {num_items} weights and {len(val)} values" + ) + for i in range(num_items): + if not isinstance(wt[i], int): + raise TypeError( + "All weights must be integers but " + f"got weight of type {type(wt[i])} at index {i}" + ) - return dp[n][w] + optimal_val, dp_table = knapsack(W, wt, val, num_items) + example_optional_set = set() + _construct_solution(dp_table, wt, num_items, W, example_optional_set) -if __name__ == '__main__': - ''' + return optimal_val, example_optional_set + + +def _construct_solution(dp: list, wt: list, i: int, j: int, optimal_set: set): + """ + Recursively reconstructs one of the optimal subsets given + a filled DP table and the vector of weights + + Parameters + --------- + + dp: list of list, the table of a solved integer weight dynamic programming problem + + wt: list or tuple, the vector of weights of the items + i: int, the index of the item under consideration + j: int, the current possible maximum weight + optimal_set: set, the optimal subset so far. This gets modified by the function. + + Returns + ------- + None + + """ + # for the current item i at a maximum weight j to be part of an optimal subset, + # the optimal value at (i, j) must be greater than the optimal value at (i-1, j). + # where i - 1 means considering only the previous items at the given maximum weight + if i > 0 and j > 0: + if dp[i - 1][j] == dp[i][j]: + _construct_solution(dp, wt, i - 1, j, optimal_set) + else: + optimal_set.add(i) + _construct_solution(dp, wt, i - 1, j - wt[i - 1], optimal_set) + + +if __name__ == "__main__": + """ Adding test case for knapsack - ''' - val = [3,2,4,4] - wt = [4,3,2,3] + """ + val = [3, 2, 4, 4] + wt = [4, 3, 2, 3] n = 4 w = 6 - F = [[0]*(w + 1)] + [[0] + [-1 for i in range(w + 1)] for j in range(n + 1)] - print(knapsack(w,wt,val,n)) - print(MF_knapsack(n,wt,val,w)) # switched the n and w - + F = [[0] * (w + 1)] + [[0] + [-1 for i in range(w + 1)] for j in range(n + 1)] + optimal_solution, _ = knapsack(w, wt, val, n) + print(optimal_solution) + print(MF_knapsack(n, wt, val, w)) # switched the n and w + + # testing the dynamic programming problem with example + # the optimal subset for the above example are items 3 and 4 + optimal_solution, optimal_subset = knapsack_with_example_solution(w, wt, val) + assert optimal_solution == 8 + assert optimal_subset == {3, 4} + print("optimal_value = ", optimal_solution) + print("An optimal subset corresponding to the optimal value", optimal_subset) diff --git a/dynamic_programming/longest_common_subsequence.py b/dynamic_programming/longest_common_subsequence.py index 0a4771cb2efd..fdcf3311a017 100644 --- a/dynamic_programming/longest_common_subsequence.py +++ b/dynamic_programming/longest_common_subsequence.py @@ -1,37 +1,83 @@ """ -LCS Problem Statement: Given two sequences, find the length of longest subsequence present in both of them. -A subsequence is a sequence that appears in the same relative order, but not necessarily continious. +LCS Problem Statement: Given two sequences, find the length of longest subsequence +present in both of them. A subsequence is a sequence that appears in the same relative +order, but not necessarily continuous. Example:"abc", "abg" are subsequences of "abcdefgh". """ -from __future__ import print_function -try: - xrange # Python 2 -except NameError: - xrange = range # Python 3 -def lcs_dp(x, y): +def longest_common_subsequence(x: str, y: str): + """ + Finds the longest common subsequence between two strings. Also returns the + The subsequence found + + Parameters + ---------- + + x: str, one of the strings + y: str, the other string + + Returns + ------- + L[m][n]: int, the length of the longest subsequence. Also equal to len(seq) + Seq: str, the subsequence found + + >>> longest_common_subsequence("programming", "gaming") + (6, 'gaming') + >>> longest_common_subsequence("physics", "smartphone") + (2, 'ph') + >>> longest_common_subsequence("computer", "food") + (1, 'o') + """ # find the length of strings + + assert x is not None + assert y is not None + m = len(x) n = len(y) # declaring the array for storing the dp values - L = [[None] * (n + 1) for i in xrange(m + 1)] - seq = [] - - for i in range(m + 1): - for j in range(n + 1): - if i == 0 or j == 0: - L[i][j] = 0 - elif x[i - 1] == y[ j - 1]: - L[i][j] = L[i - 1][j - 1] + 1 - seq.append(x[i -1]) + L = [[0] * (n + 1) for _ in range(m + 1)] + + for i in range(1, m + 1): + for j in range(1, n + 1): + if x[i - 1] == y[j - 1]: + match = 1 else: - L[i][j] = max(L[i - 1][j], L[i][j - 1]) - # L[m][n] contains the length of LCS of X[0..n-1] & Y[0..m-1] + match = 0 + + L[i][j] = max(L[i - 1][j], L[i][j - 1], L[i - 1][j - 1] + match) + + seq = "" + i, j = m, n + while i > 0 and j > 0: + if x[i - 1] == y[j - 1]: + match = 1 + else: + match = 0 + + if L[i][j] == L[i - 1][j - 1] + match: + if match == 1: + seq = x[i - 1] + seq + i -= 1 + j -= 1 + elif L[i][j] == L[i - 1][j]: + i -= 1 + else: + j -= 1 + return L[m][n], seq -if __name__=='__main__': - x = 'AGGTAB' - y = 'GXTXAYB' - print(lcs_dp(x, y)) + +if __name__ == "__main__": + a = "AGGTAB" + b = "GXTXAYB" + expected_ln = 4 + expected_subseq = "GTAB" + + ln, subseq = longest_common_subsequence(a, b) + print("len =", ln, ", sub-sequence =", subseq) + import doctest + + doctest.testmod() diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index b6d165909e70..f5ca8a2b5cdc 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -1,42 +1,61 @@ -''' +""" Author : Mehdi ALAOUI -This is a pure Python implementation of Dynamic Programming solution to the longest increasing subsequence of a given sequence. +This is a pure Python implementation of Dynamic Programming solution to the longest +increasing subsequence of a given sequence. The problem is : -Given an ARRAY, to find the longest and increasing sub ARRAY in that given ARRAY and return it. -Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return [10, 22, 33, 41, 60, 80] as output -''' -from __future__ import print_function - -def longestSub(ARRAY): #This function is recursive - - ARRAY_LENGTH = len(ARRAY) - if(ARRAY_LENGTH <= 1): #If the array contains only one element, we return it (it's the stop condition of recursion) - return ARRAY - #Else - PIVOT=ARRAY[0] - isFound=False - i=1 - LONGEST_SUB=[] - while(not isFound and i= ARRAY[i] ] - TEMPORARY_ARRAY = longestSub(TEMPORARY_ARRAY) - if ( len(TEMPORARY_ARRAY) > len(LONGEST_SUB) ): - LONGEST_SUB = TEMPORARY_ARRAY - else: - i+=1 - - TEMPORARY_ARRAY = [ element for element in ARRAY[1:] if element >= PIVOT ] - TEMPORARY_ARRAY = [PIVOT] + longestSub(TEMPORARY_ARRAY) - if ( len(TEMPORARY_ARRAY) > len(LONGEST_SUB) ): - return TEMPORARY_ARRAY - else: - return LONGEST_SUB - -#Some examples - -print(longestSub([4,8,7,5,1,12,2,3,9])) -print(longestSub([9,8,7,6,5,7])) \ No newline at end of file +Given an array, to find the longest and increasing sub-array in that given array and +return it. +Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return + [10, 22, 33, 41, 60, 80] as output +""" +from __future__ import annotations + + +def longest_subsequence(array: list[int]) -> list[int]: # This function is recursive + """ + Some examples + >>> longest_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80]) + [10, 22, 33, 41, 60, 80] + >>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9]) + [1, 2, 3, 9] + >>> longest_subsequence([9, 8, 7, 6, 5, 7]) + [8] + >>> longest_subsequence([1, 1, 1]) + [1, 1, 1] + >>> longest_subsequence([]) + [] + """ + array_length = len(array) + # If the array contains only one element, we return it (it's the stop condition of + # recursion) + if array_length <= 1: + return array + # Else + pivot = array[0] + isFound = False + i = 1 + longest_subseq = [] + while not isFound and i < array_length: + if array[i] < pivot: + isFound = True + temp_array = [element for element in array[i:] if element >= array[i]] + temp_array = longest_subsequence(temp_array) + if len(temp_array) > len(longest_subseq): + longest_subseq = temp_array + else: + i += 1 + + temp_array = [element for element in array[1:] if element >= pivot] + temp_array = [pivot] + longest_subsequence(temp_array) + if len(temp_array) > len(longest_subseq): + return temp_array + else: + return longest_subseq + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/longest_increasing_subsequence_O(nlogn).py b/dynamic_programming/longest_increasing_subsequence_O(nlogn).py deleted file mode 100644 index 21122a04d69f..000000000000 --- a/dynamic_programming/longest_increasing_subsequence_O(nlogn).py +++ /dev/null @@ -1,41 +0,0 @@ -from __future__ import print_function -############################# -# Author: Aravind Kashyap -# File: lis.py -# comments: This programme outputs the Longest Strictly Increasing Subsequence in O(NLogN) -# Where N is the Number of elements in the list -############################# -def CeilIndex(v,l,r,key): - while r-l > 1: - m = (l + r)/2 - if v[m] >= key: - r = m - else: - l = m - - return r - - -def LongestIncreasingSubsequenceLength(v): - if(len(v) == 0): - return 0 - - tail = [0]*len(v) - length = 1 - - tail[0] = v[0] - - for i in range(1,len(v)): - if v[i] < tail[0]: - tail[0] = v[i] - elif v[i] > tail[length-1]: - tail[length] = v[i] - length += 1 - else: - tail[CeilIndex(tail,-1,length-1,v[i])] = v[i] - - return length - - -v = [2, 5, 3, 7, 11, 8, 10, 13, 6] -print(LongestIncreasingSubsequenceLength(v)) diff --git a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py new file mode 100644 index 000000000000..af536f8bbd01 --- /dev/null +++ b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py @@ -0,0 +1,55 @@ +############################# +# Author: Aravind Kashyap +# File: lis.py +# comments: This programme outputs the Longest Strictly Increasing Subsequence in +# O(NLogN) Where N is the Number of elements in the list +############################# +from __future__ import annotations + + +def CeilIndex(v, l, r, key): # noqa: E741 + while r - l > 1: + m = (l + r) // 2 + if v[m] >= key: + r = m + else: + l = m # noqa: E741 + return r + + +def LongestIncreasingSubsequenceLength(v: list[int]) -> int: + """ + >>> LongestIncreasingSubsequenceLength([2, 5, 3, 7, 11, 8, 10, 13, 6]) + 6 + >>> LongestIncreasingSubsequenceLength([]) + 0 + >>> LongestIncreasingSubsequenceLength([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, + ... 11, 7, 15]) + 6 + >>> LongestIncreasingSubsequenceLength([5, 4, 3, 2, 1]) + 1 + """ + if len(v) == 0: + return 0 + + tail = [0] * len(v) + length = 1 + + tail[0] = v[0] + + for i in range(1, len(v)): + if v[i] < tail[0]: + tail[0] = v[i] + elif v[i] > tail[length - 1]: + tail[length] = v[i] + length += 1 + else: + tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i] + + return length + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py index de2c88a8b525..30159a1386c3 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -1,33 +1,34 @@ -''' -Auther : Yvonne +""" +Author : Yvonne -This is a pure Python implementation of Dynamic Programming solution to the longest_sub_array problem. +This is a pure Python implementation of Dynamic Programming solution to the + longest_sub_array problem. The problem is : -Given an array, to find the longest and continuous sub array and get the max sum of the sub array in the given array. -''' -from __future__ import print_function +Given an array, to find the longest and continuous sub array and get the max sum of the + sub array in the given array. +""" class SubArray: - def __init__(self, arr): # we need a list not a string, so do something to change the type - self.array = arr.split(',') + self.array = arr.split(",") print(("the input array is:", self.array)) def solve_sub_array(self): - rear = [int(self.array[0])]*len(self.array) - sum_value = [int(self.array[0])]*len(self.array) + rear = [int(self.array[0])] * len(self.array) + sum_value = [int(self.array[0])] * len(self.array) for i in range(1, len(self.array)): - sum_value[i] = max(int(self.array[i]) + sum_value[i-1], int(self.array[i])) - rear[i] = max(sum_value[i], rear[i-1]) - return rear[len(self.array)-1] + sum_value[i] = max( + int(self.array[i]) + sum_value[i - 1], int(self.array[i]) + ) + rear[i] = max(sum_value[i], rear[i - 1]) + return rear[len(self.array) - 1] -if __name__ == '__main__': +if __name__ == "__main__": whole_array = input("please input some numbers:") array = SubArray(whole_array) re = array.solve_sub_array() print(("the results is:", re)) - diff --git a/dynamic_programming/matrix_chain_order.py b/dynamic_programming/matrix_chain_order.py index b8234a65acbe..9411bc704f1c 100644 --- a/dynamic_programming/matrix_chain_order.py +++ b/dynamic_programming/matrix_chain_order.py @@ -1,46 +1,54 @@ -from __future__ import print_function - import sys -''' + +""" Dynamic Programming Implementation of Matrix Chain Multiplication Time Complexity: O(n^3) Space Complexity: O(n^2) -''' +""" + + def MatrixChainOrder(array): - N=len(array) - Matrix=[[0 for x in range(N)] for x in range(N)] - Sol=[[0 for x in range(N)] for x in range(N)] + N = len(array) + Matrix = [[0 for x in range(N)] for x in range(N)] + Sol = [[0 for x in range(N)] for x in range(N)] - for ChainLength in range(2,N): - for a in range(1,N-ChainLength+1): - b = a+ChainLength-1 + for ChainLength in range(2, N): + for a in range(1, N - ChainLength + 1): + b = a + ChainLength - 1 Matrix[a][b] = sys.maxsize - for c in range(a , b): - cost = Matrix[a][c] + Matrix[c+1][b] + array[a-1]*array[c]*array[b] + for c in range(a, b): + cost = ( + Matrix[a][c] + Matrix[c + 1][b] + array[a - 1] * array[c] * array[b] + ) if cost < Matrix[a][b]: Matrix[a][b] = cost Sol[a][b] = c - return Matrix , Sol -#Print order of matrix with Ai as Matrix -def PrintOptimalSolution(OptimalSolution,i,j): - if i==j: - print("A" + str(i),end = " ") + return Matrix, Sol + + +# Print order of matrix with Ai as Matrix +def PrintOptimalSolution(OptimalSolution, i, j): + if i == j: + print("A" + str(i), end=" ") else: - print("(",end = " ") - PrintOptimalSolution(OptimalSolution,i,OptimalSolution[i][j]) - PrintOptimalSolution(OptimalSolution,OptimalSolution[i][j]+1,j) - print(")",end = " ") + print("(", end=" ") + PrintOptimalSolution(OptimalSolution, i, OptimalSolution[i][j]) + PrintOptimalSolution(OptimalSolution, OptimalSolution[i][j] + 1, j) + print(")", end=" ") + def main(): - array=[30,35,15,5,10,20,25] - n=len(array) - #Size of matrix created from above array will be + array = [30, 35, 15, 5, 10, 20, 25] + n = len(array) + # Size of matrix created from above array will be # 30*35 35*15 15*5 5*10 10*20 20*25 - Matrix , OptimalSolution = MatrixChainOrder(array) + Matrix, OptimalSolution = MatrixChainOrder(array) + + print("No. of Operation required: " + str(Matrix[1][n - 1])) + PrintOptimalSolution(OptimalSolution, 1, n - 1) + - print("No. of Operation required: "+str((Matrix[1][n-1]))) - PrintOptimalSolution(OptimalSolution,1,n-1) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/dynamic_programming/max_non_adjacent_sum.py b/dynamic_programming/max_non_adjacent_sum.py new file mode 100644 index 000000000000..5362b22ca9dc --- /dev/null +++ b/dynamic_programming/max_non_adjacent_sum.py @@ -0,0 +1,34 @@ +# Video Explanation: https://www.youtube.com/watch?v=6w60Zi1NtL8&feature=emb_logo + +from __future__ import annotations + + +def maximum_non_adjacent_sum(nums: list[int]) -> int: + """ + Find the maximum non-adjacent sum of the integers in the nums input list + + >>> print(maximum_non_adjacent_sum([1, 2, 3])) + 4 + >>> maximum_non_adjacent_sum([1, 5, 3, 7, 2, 2, 6]) + 18 + >>> maximum_non_adjacent_sum([-1, -5, -3, -7, -2, -2, -6]) + 0 + >>> maximum_non_adjacent_sum([499, 500, -3, -7, -2, -2, -6]) + 500 + """ + if not nums: + return 0 + max_including = nums[0] + max_excluding = 0 + for num in nums[1:]: + max_including, max_excluding = ( + max_excluding + num, + max(max_including, max_excluding), + ) + return max(max_excluding, max_including) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/max_sub_array.py b/dynamic_programming/max_sub_array.py index 5d48882427c0..3060010ef7c6 100644 --- a/dynamic_programming/max_sub_array.py +++ b/dynamic_programming/max_sub_array.py @@ -1,60 +1,94 @@ """ author : Mayank Kumar Jha (mk9440) """ -from __future__ import print_function - -import time -import matplotlib.pyplot as plt -from random import randint -def find_max_sub_array(A,low,high): - if low==high: - return low,high,A[low] - else : - mid=(low+high)//2 - left_low,left_high,left_sum=find_max_sub_array(A,low,mid) - right_low,right_high,right_sum=find_max_sub_array(A,mid+1,high) - cross_left,cross_right,cross_sum=find_max_cross_sum(A,low,mid,high) - if left_sum>=right_sum and left_sum>=cross_sum: - return left_low,left_high,left_sum - elif right_sum>=left_sum and right_sum>=cross_sum : - return right_low,right_high,right_sum +from __future__ import annotations + + +def find_max_sub_array(A, low, high): + if low == high: + return low, high, A[low] + else: + mid = (low + high) // 2 + left_low, left_high, left_sum = find_max_sub_array(A, low, mid) + right_low, right_high, right_sum = find_max_sub_array(A, mid + 1, high) + cross_left, cross_right, cross_sum = find_max_cross_sum(A, low, mid, high) + if left_sum >= right_sum and left_sum >= cross_sum: + return left_low, left_high, left_sum + elif right_sum >= left_sum and right_sum >= cross_sum: + return right_low, right_high, right_sum else: - return cross_left,cross_right,cross_sum - -def find_max_cross_sum(A,low,mid,high): - left_sum,max_left=-999999999,-1 - right_sum,max_right=-999999999,-1 - summ=0 - for i in range(mid,low-1,-1): - summ+=A[i] + return cross_left, cross_right, cross_sum + + +def find_max_cross_sum(A, low, mid, high): + left_sum, max_left = -999999999, -1 + right_sum, max_right = -999999999, -1 + summ = 0 + for i in range(mid, low - 1, -1): + summ += A[i] if summ > left_sum: - left_sum=summ - max_left=i - summ=0 - for i in range(mid+1,high+1): - summ+=A[i] + left_sum = summ + max_left = i + summ = 0 + for i in range(mid + 1, high + 1): + summ += A[i] if summ > right_sum: - right_sum=summ - max_right=i - return max_left,max_right,(left_sum+right_sum) - - -if __name__=='__main__': - inputs=[10,100,1000,10000,50000,100000,200000,300000,400000,500000] - tim=[] - for i in inputs: - li=[randint(1,i) for j in range(i)] - strt=time.time() - (find_max_sub_array(li,0,len(li)-1)) - end=time.time() - tim.append(end-strt) - print("No of Inputs Time Taken") - for i in range(len(inputs)): - print(inputs[i],'\t\t',tim[i]) - plt.plot(inputs,tim) - plt.xlabel("Number of Inputs");plt.ylabel("Time taken in seconds ") - plt.show() + right_sum = summ + max_right = i + return max_left, max_right, (left_sum + right_sum) + +def max_sub_array(nums: list[int]) -> int: + """ + Finds the contiguous subarray which has the largest sum and return its sum. + >>> max_sub_array([-2, 1, -3, 4, -1, 2, 1, -5, 4]) + 6 - + An empty (sub)array has sum 0. + >>> max_sub_array([]) + 0 + + If all elements are negative, the largest subarray would be the empty array, + having the sum 0. + >>> max_sub_array([-1, -2, -3]) + 0 + >>> max_sub_array([5, -2, -3]) + 5 + >>> max_sub_array([31, -41, 59, 26, -53, 58, 97, -93, -23, 84]) + 187 + """ + best = 0 + current = 0 + for i in nums: + current += i + if current < 0: + current = 0 + best = max(best, current) + return best + + +if __name__ == "__main__": + """ + A random simulation of this algorithm. + """ + import time + from random import randint + + from matplotlib import pyplot as plt + + inputs = [10, 100, 1000, 10000, 50000, 100000, 200000, 300000, 400000, 500000] + tim = [] + for i in inputs: + li = [randint(1, i) for j in range(i)] + strt = time.time() + (find_max_sub_array(li, 0, len(li) - 1)) + end = time.time() + tim.append(end - strt) + print("No of Inputs Time Taken") + for i in range(len(inputs)): + print(inputs[i], "\t\t", tim[i]) + plt.plot(inputs, tim) + plt.xlabel("Number of Inputs") + plt.ylabel("Time taken in seconds ") + plt.show() diff --git a/dynamic_programming/max_sum_contiguous_subsequence.py b/dynamic_programming/max_sum_contiguous_subsequence.py new file mode 100644 index 000000000000..bac592370c5d --- /dev/null +++ b/dynamic_programming/max_sum_contiguous_subsequence.py @@ -0,0 +1,20 @@ +def max_subarray_sum(nums: list) -> int: + """ + >>> max_subarray_sum([6 , 9, -1, 3, -7, -5, 10]) + 17 + """ + if not nums: + return 0 + n = len(nums) + + res, s, s_pre = nums[0], nums[0], nums[0] + for i in range(1, n): + s = max(nums[i], s_pre + nums[i]) + s_pre = s + res = max(res, s) + return res + + +if __name__ == "__main__": + nums = [6, 9, -1, 3, -7, -5, 10] + print(max_subarray_sum(nums)) diff --git a/dynamic_programming/minimum_cost_path.py b/dynamic_programming/minimum_cost_path.py new file mode 100644 index 000000000000..3ad24b5528d1 --- /dev/null +++ b/dynamic_programming/minimum_cost_path.py @@ -0,0 +1,37 @@ +# Youtube Explanation: https://www.youtube.com/watch?v=lBRtnuxg-gU + +from __future__ import annotations + + +def minimum_cost_path(matrix: list[list[int]]) -> int: + """ + Find the minimum cost traced by all possible paths from top left to bottom right in + a given matrix + + >>> minimum_cost_path([[2, 1], [3, 1], [4, 2]]) + 6 + + >>> minimum_cost_path([[2, 1, 4], [2, 1, 3], [3, 2, 1]]) + 7 + """ + + # preprocessing the first row + for i in range(1, len(matrix[0])): + matrix[0][i] += matrix[0][i - 1] + + # preprocessing the first column + for i in range(1, len(matrix)): + matrix[i][0] += matrix[i - 1][0] + + # updating the path cost for current position + for i in range(1, len(matrix)): + for j in range(1, len(matrix[0])): + matrix[i][j] += min(matrix[i - 1][j], matrix[i][j - 1]) + + return matrix[-1][-1] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/minimum_partition.py b/dynamic_programming/minimum_partition.py index 18aa1faa2fa6..8fad4ef3072f 100644 --- a/dynamic_programming/minimum_partition.py +++ b/dynamic_programming/minimum_partition.py @@ -1,28 +1,30 @@ """ Partition a set into two subsets such that the difference of subset sums is minimum """ + + def findMin(arr): n = len(arr) s = sum(arr) - dp = [[False for x in range(s+1)]for y in range(n+1)] + dp = [[False for x in range(s + 1)] for y in range(n + 1)] - for i in range(1, n+1): + for i in range(1, n + 1): dp[i][0] = True - for i in range(1, s+1): + for i in range(1, s + 1): dp[0][i] = False - for i in range(1, n+1): - for j in range(1, s+1): - dp[i][j]= dp[i][j-1] + for i in range(1, n + 1): + for j in range(1, s + 1): + dp[i][j] = dp[i][j - 1] - if (arr[i-1] <= j): - dp[i][j] = dp[i][j] or dp[i-1][j-arr[i-1]] + if arr[i - 1] <= j: + dp[i][j] = dp[i][j] or dp[i - 1][j - arr[i - 1]] - for j in range(int(s/2), -1, -1): - if dp[n][j] == True: - diff = s-2*j - break; + for j in range(int(s / 2), -1, -1): + if dp[n][j] is True: + diff = s - 2 * j + break return diff diff --git a/dynamic_programming/minimum_steps_to_one.py b/dynamic_programming/minimum_steps_to_one.py new file mode 100644 index 000000000000..f4eb7033dd20 --- /dev/null +++ b/dynamic_programming/minimum_steps_to_one.py @@ -0,0 +1,65 @@ +""" +YouTube Explanation: https://www.youtube.com/watch?v=f2xi3c1S95M + +Given an integer n, return the minimum steps to 1 + +AVAILABLE STEPS: + * Decrement by 1 + * if n is divisible by 2, divide by 2 + * if n is divisible by 3, divide by 3 + + +Example 1: n = 10 +10 -> 9 -> 3 -> 1 +Result: 3 steps + +Example 2: n = 15 +15 -> 5 -> 4 -> 2 -> 1 +Result: 4 steps + +Example 3: n = 6 +6 -> 2 -> 1 +Result: 2 step +""" + +from __future__ import annotations + +__author__ = "Alexander Joslin" + + +def min_steps_to_one(number: int) -> int: + """ + Minimum steps to 1 implemented using tabulation. + >>> min_steps_to_one(10) + 3 + >>> min_steps_to_one(15) + 4 + >>> min_steps_to_one(6) + 2 + + :param number: + :return int: + """ + + if number <= 0: + raise ValueError(f"n must be greater than 0. Got n = {number}") + + table = [number + 1] * (number + 1) + + # starting position + table[1] = 0 + for i in range(1, number): + table[i + 1] = min(table[i + 1], table[i] + 1) + # check if out of bounds + if i * 2 <= number: + table[i * 2] = min(table[i * 2], table[i] + 1) + # check if out of bounds + if i * 3 <= number: + table[i * 3] = min(table[i * 3], table[i] + 1) + return table[number] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/optimal_binary_search_tree.py b/dynamic_programming/optimal_binary_search_tree.py new file mode 100644 index 000000000000..0d94c1b61d39 --- /dev/null +++ b/dynamic_programming/optimal_binary_search_tree.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python3 + +# This Python program implements an optimal binary search tree (abbreviated BST) +# building dynamic programming algorithm that delivers O(n^2) performance. +# +# The goal of the optimal BST problem is to build a low-cost BST for a +# given set of nodes, each with its own key and frequency. The frequency +# of the node is defined as how many time the node is being searched. +# The search cost of binary search tree is given by this formula: +# +# cost(1, n) = sum{i = 1 to n}((depth(node_i) + 1) * node_i_freq) +# +# where n is number of nodes in the BST. The characteristic of low-cost +# BSTs is having a faster overall search time than other implementations. +# The reason for their fast search time is that the nodes with high +# frequencies will be placed near the root of the tree while the nodes +# with low frequencies will be placed near the leaves of the tree thus +# reducing search time in the most frequent instances. +import sys +from random import randint + + +class Node: + """Binary Search Tree Node""" + + def __init__(self, key, freq): + self.key = key + self.freq = freq + + def __str__(self): + """ + >>> str(Node(1, 2)) + 'Node(key=1, freq=2)' + """ + return f"Node(key={self.key}, freq={self.freq})" + + +def print_binary_search_tree(root, key, i, j, parent, is_left): + """ + Recursive function to print a BST from a root table. + + >>> key = [3, 8, 9, 10, 17, 21] + >>> root = [[0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 3], [0, 0, 2, 3, 3, 3], \ + [0, 0, 0, 3, 3, 3], [0, 0, 0, 0, 4, 5], [0, 0, 0, 0, 0, 5]] + >>> print_binary_search_tree(root, key, 0, 5, -1, False) + 8 is the root of the binary search tree. + 3 is the left child of key 8. + 10 is the right child of key 8. + 9 is the left child of key 10. + 21 is the right child of key 10. + 17 is the left child of key 21. + """ + if i > j or i < 0 or j > len(root) - 1: + return + + node = root[i][j] + if parent == -1: # root does not have a parent + print(f"{key[node]} is the root of the binary search tree.") + elif is_left: + print(f"{key[node]} is the left child of key {parent}.") + else: + print(f"{key[node]} is the right child of key {parent}.") + + print_binary_search_tree(root, key, i, node - 1, key[node], True) + print_binary_search_tree(root, key, node + 1, j, key[node], False) + + +def find_optimal_binary_search_tree(nodes): + """ + This function calculates and prints the optimal binary search tree. + The dynamic programming algorithm below runs in O(n^2) time. + Implemented from CLRS (Introduction to Algorithms) book. + https://en.wikipedia.org/wiki/Introduction_to_Algorithms + + >>> find_optimal_binary_search_tree([Node(12, 8), Node(10, 34), Node(20, 50), \ + Node(42, 3), Node(25, 40), Node(37, 30)]) + Binary search tree nodes: + Node(key=10, freq=34) + Node(key=12, freq=8) + Node(key=20, freq=50) + Node(key=25, freq=40) + Node(key=37, freq=30) + Node(key=42, freq=3) + + The cost of optimal BST for given tree nodes is 324. + 20 is the root of the binary search tree. + 10 is the left child of key 20. + 12 is the right child of key 10. + 25 is the right child of key 20. + 37 is the right child of key 25. + 42 is the right child of key 37. + """ + # Tree nodes must be sorted first, the code below sorts the keys in + # increasing order and rearrange its frequencies accordingly. + nodes.sort(key=lambda node: node.key) + + n = len(nodes) + + keys = [nodes[i].key for i in range(n)] + freqs = [nodes[i].freq for i in range(n)] + + # This 2D array stores the overall tree cost (which's as minimized as possible); + # for a single key, cost is equal to frequency of the key. + dp = [[freqs[i] if i == j else 0 for j in range(n)] for i in range(n)] + # sum[i][j] stores the sum of key frequencies between i and j inclusive in nodes + # array + sum = [[freqs[i] if i == j else 0 for j in range(n)] for i in range(n)] + # stores tree roots that will be used later for constructing binary search tree + root = [[i if i == j else 0 for j in range(n)] for i in range(n)] + + for interval_length in range(2, n + 1): + for i in range(n - interval_length + 1): + j = i + interval_length - 1 + + dp[i][j] = sys.maxsize # set the value to "infinity" + sum[i][j] = sum[i][j - 1] + freqs[j] + + # Apply Knuth's optimization + # Loop without optimization: for r in range(i, j + 1): + for r in range(root[i][j - 1], root[i + 1][j] + 1): # r is a temporal root + left = dp[i][r - 1] if r != i else 0 # optimal cost for left subtree + right = dp[r + 1][j] if r != j else 0 # optimal cost for right subtree + cost = left + sum[i][j] + right + + if dp[i][j] > cost: + dp[i][j] = cost + root[i][j] = r + + print("Binary search tree nodes:") + for node in nodes: + print(node) + + print(f"\nThe cost of optimal BST for given tree nodes is {dp[0][n - 1]}.") + print_binary_search_tree(root, keys, 0, n - 1, -1, False) + + +def main(): + # A sample binary search tree + nodes = [Node(i, randint(1, 50)) for i in range(10, 0, -1)] + find_optimal_binary_search_tree(nodes) + + +if __name__ == "__main__": + main() diff --git a/dynamic_programming/rod_cutting.py b/dynamic_programming/rod_cutting.py index 34350cb8202b..442a39cb1616 100644 --- a/dynamic_programming/rod_cutting.py +++ b/dynamic_programming/rod_cutting.py @@ -1,58 +1,207 @@ -### PROBLEM ### -""" -We are given a rod of length n and we are given the array of prices, also of -length n. This array contains the price for selling a rod at a certain length. -For example, prices[5] shows the price we can sell a rod of length 5. -Generalising, prices[x] shows the price a rod of length x can be sold. -We are tasked to find the optimal solution to sell the given rod. """ +This module provides two implementations for the rod-cutting problem: +1. A naive recursive implementation which has an exponential runtime +2. Two dynamic programming implementations which have quadratic runtime -### SOLUTION ### -""" -Profit(n) = max(1>> naive_cut_rod_recursive(4, [1, 5, 8, 9]) + 10 + >>> naive_cut_rod_recursive(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]) + 30 + """ + + _enforce_args(n, prices) + if n == 0: + return 0 + max_revue = float("-inf") + for i in range(1, n + 1): + max_revue = max( + max_revue, prices[i - 1] + naive_cut_rod_recursive(n - i, prices) + ) + + return max_revue + + +def top_down_cut_rod(n: int, prices: list): + """ + Constructs a top-down dynamic programming solution for the rod-cutting + problem via memoization. This function serves as a wrapper for + _top_down_cut_rod_recursive + + Runtime: O(n^2) + + Arguments + -------- + n: int, the length of the rod + prices: list, the prices for each piece of rod. ``p[i-i]`` is the + price for a rod of length ``i`` + + Note + ---- + For convenience and because Python's lists using 0-indexing, length(max_rev) = + n + 1, to accommodate for the revenue obtainable from a rod of length 0. + + Returns + ------- + The maximum revenue obtainable for a rod of length n given the list of prices + for each piece. + + Examples + ------- + >>> top_down_cut_rod(4, [1, 5, 8, 9]) + 10 + >>> top_down_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]) + 30 + """ + _enforce_args(n, prices) + max_rev = [float("-inf") for _ in range(n + 1)] + return _top_down_cut_rod_recursive(n, prices, max_rev) + + +def _top_down_cut_rod_recursive(n: int, prices: list, max_rev: list): + """ + Constructs a top-down dynamic programming solution for the rod-cutting problem + via memoization. + + Runtime: O(n^2) + + Arguments + -------- + n: int, the length of the rod + prices: list, the prices for each piece of rod. ``p[i-i]`` is the + price for a rod of length ``i`` + max_rev: list, the computed maximum revenue for a piece of rod. + ``max_rev[i]`` is the maximum revenue obtainable for a rod of length ``i`` + + Returns + ------- + The maximum revenue obtainable for a rod of length n given the list of prices + for each piece. + """ + if max_rev[n] >= 0: + return max_rev[n] + elif n == 0: + return 0 + else: + max_revenue = float("-inf") + for i in range(1, n + 1): + max_revenue = max( + max_revenue, + prices[i - 1] + _top_down_cut_rod_recursive(n - i, prices, max_rev), + ) + + max_rev[n] = max_revenue + + return max_rev[n] + + +def bottom_up_cut_rod(n: int, prices: list): + """ + Constructs a bottom-up dynamic programming solution for the rod-cutting problem + + Runtime: O(n^2) + + Arguments + ---------- + n: int, the maximum length of the rod. + prices: list, the prices for each piece of rod. ``p[i-i]`` is the + price for a rod of length ``i`` + + Returns + ------- + The maximum revenue obtainable from cutting a rod of length n given + the prices for each piece of rod p. + + Examples + ------- + >>> bottom_up_cut_rod(4, [1, 5, 8, 9]) + 10 + >>> bottom_up_cut_rod(10, [1, 5, 8, 9, 10, 17, 17, 20, 24, 30]) + 30 + """ + _enforce_args(n, prices) + + # length(max_rev) = n + 1, to accommodate for the revenue obtainable from a rod of + # length 0. + max_rev = [float("-inf") for _ in range(n + 1)] + max_rev[0] = 0 + + for i in range(1, n + 1): + max_revenue_i = max_rev[i] + for j in range(1, i + 1): + max_revenue_i = max(max_revenue_i, prices[j - 1] + max_rev[i - j]) + + max_rev[i] = max_revenue_i + + return max_rev[n] + + +def _enforce_args(n: int, prices: list): + """ + Basic checks on the arguments to the rod-cutting algorithms + + n: int, the length of the rod + prices: list, the price list for each piece of rod. + + Throws ValueError: + + if n is negative or there are fewer items in the price list than the length of + the rod + """ + if n < 0: + raise ValueError(f"n must be greater than or equal to 0. Got n = {n}") + + if n > len(prices): + raise ValueError( + f"Each integral piece of rod must have a corresponding " + f"price. Got n = {n} but length of prices = {len(prices)}" + ) - for i in range(1,n): - if(solutions[i] == -1): - #We haven't calulated solution for length i yet. - #We know we sell the part of length i so we get prices[i]. - #We just need to know how to sell rod of length n-i - yesCut[i] = prices[i] + CutRod(n-i) - else: - #We have calculated solution for length i. - #We add the two prices. - yesCut[i] = prices[i] + solutions[n-i] - #We need to find the highest price in order to sell more efficiently. - #We have to choose between noCut and the prices in yesCut. - m = noCut #Initialize max to noCut - for i in range(n): - if(yesCut[i] > m): - m = yesCut[i] +def main(): + prices = [6, 10, 12, 15, 20, 23] + n = len(prices) - solutions[n] = m - return m + # the best revenue comes from cutting the rod into 6 pieces, each + # of length 1 resulting in a revenue of 6 * 6 = 36. + expected_max_revenue = 36 + max_rev_top_down = top_down_cut_rod(n, prices) + max_rev_bottom_up = bottom_up_cut_rod(n, prices) + max_rev_naive = naive_cut_rod_recursive(n, prices) + assert expected_max_revenue == max_rev_top_down + assert max_rev_top_down == max_rev_bottom_up + assert max_rev_bottom_up == max_rev_naive -### EXAMPLE ### -length = 5 -#The first price, 0, is for when we have no rod. -prices = [0, 1, 3, 7, 9, 11, 13, 17, 21, 21, 30] -solutions = [-1 for x in range(length+1)] -print(CutRod(length)) +if __name__ == "__main__": + main() diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py new file mode 100644 index 000000000000..4781b23b32eb --- /dev/null +++ b/dynamic_programming/subset_generation.py @@ -0,0 +1,43 @@ +# Print all subset combinations of n element in given set of r element. + + +def combination_util(arr, n, r, index, data, i): + """ + Current combination is ready to be printed, print it + arr[] ---> Input Array + data[] ---> Temporary array to store current combination + start & end ---> Staring and Ending indexes in arr[] + index ---> Current index in data[] + r ---> Size of a combination to be printed + """ + if index == r: + for j in range(r): + print(data[j], end=" ") + print(" ") + return + # When no more elements are there to put in data[] + if i >= n: + return + # current is included, put next at next location + data[index] = arr[i] + combination_util(arr, n, r, index + 1, data, i + 1) + # current is excluded, replace it with + # next (Note that i+1 is passed, but + # index is not changed) + combination_util(arr, n, r, index, data, i + 1) + # The main function that prints all combinations + # of size r in arr[] of size n. This function + # mainly uses combinationUtil() + + +def print_combination(arr, n, r): + # A temporary array to store all combination one by one + data = [0] * r + # Print all combination using temporary array 'data[]' + combination_util(arr, n, r, 0, data, 0) + + +# Driver function to check for above function +arr = [10, 20, 30, 40, 50] +print_combination(arr, len(arr), 3) +# This code is contributed by Ambuj sahu diff --git a/dynamic_programming/sum_of_subset.py b/dynamic_programming/sum_of_subset.py new file mode 100644 index 000000000000..a12177b57c74 --- /dev/null +++ b/dynamic_programming/sum_of_subset.py @@ -0,0 +1,37 @@ +def isSumSubset(arr, arrLen, requiredSum): + """ + >>> isSumSubset([2, 4, 6, 8], 4, 5) + False + >>> isSumSubset([2, 4, 6, 8], 4, 14) + True + """ + # a subset value says 1 if that subset sum can be formed else 0 + # initially no subsets can be formed hence False/0 + subset = [[False for i in range(requiredSum + 1)] for i in range(arrLen + 1)] + + # for each arr value, a sum of zero(0) can be formed by not taking any element + # hence True/1 + for i in range(arrLen + 1): + subset[i][0] = True + + # sum is not zero and set is empty then false + for i in range(1, requiredSum + 1): + subset[0][i] = False + + for i in range(1, arrLen + 1): + for j in range(1, requiredSum + 1): + if arr[i - 1] > j: + subset[i][j] = subset[i - 1][j] + if arr[i - 1] <= j: + subset[i][j] = subset[i - 1][j] or subset[i - 1][j - arr[i - 1]] + + # uncomment to print the subset + # for i in range(arrLen+1): + # print(subset[i]) + print(subset[arrLen][requiredSum]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/electronics/electric_power.py b/electronics/electric_power.py new file mode 100644 index 000000000000..768c3d5c7232 --- /dev/null +++ b/electronics/electric_power.py @@ -0,0 +1,49 @@ +# https://en.m.wikipedia.org/wiki/Electric_power +from collections import namedtuple + + +def electric_power(voltage: float, current: float, power: float) -> float: + """ + This function can calculate any one of the three (voltage, current, power), + fundamental value of electrical system. + examples are below: + >>> electric_power(voltage=0, current=2, power=5) + result(name='voltage', value=2.5) + >>> electric_power(voltage=2, current=2, power=0) + result(name='power', value=4.0) + >>> electric_power(voltage=-2, current=3, power=0) + result(name='power', value=6.0) + >>> electric_power(voltage=2, current=4, power=2) + Traceback (most recent call last): + File "", line 15, in + ValueError: Only one argument must be 0 + >>> electric_power(voltage=0, current=0, power=2) + Traceback (most recent call last): + File "", line 19, in + ValueError: Only one argument must be 0 + >>> electric_power(voltage=0, current=2, power=-4) + Traceback (most recent call last): + File "", line 23, in >> electric_power(voltage=2.2, current=2.2, power=0) + result(name='power', value=4.84) + """ + result = namedtuple("result", "name value") + if (voltage, current, power).count(0) != 1: + raise ValueError("Only one argument must be 0") + elif power < 0: + raise ValueError( + "Power cannot be negative in any electrical/electronics system" + ) + elif voltage == 0: + return result("voltage", power / current) + elif current == 0: + return result("current", power / voltage) + elif power == 0: + return result("power", float(round(abs(voltage * current), 2))) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py new file mode 100644 index 000000000000..a7b37b635397 --- /dev/null +++ b/electronics/ohms_law.py @@ -0,0 +1,39 @@ +# https://en.wikipedia.org/wiki/Ohm%27s_law + + +def ohms_law(voltage: float, current: float, resistance: float) -> float: + """ + Apply Ohm's Law, on any two given electrical values, which can be voltage, current, + and resistance, and then in a Python dict return name/value pair of the zero value. + + >>> ohms_law(voltage=10, resistance=5, current=0) + {'current': 2.0} + >>> ohms_law(voltage=0, current=0, resistance=10) + Traceback (most recent call last): + ... + ValueError: One and only one argument must be 0 + >>> ohms_law(voltage=0, current=1, resistance=-2) + Traceback (most recent call last): + ... + ValueError: Resistance cannot be negative + >>> ohms_law(resistance=0, voltage=-10, current=1) + {'resistance': -10.0} + >>> ohms_law(voltage=0, current=-1.5, resistance=2) + {'voltage': -3.0} + """ + if (voltage, current, resistance).count(0) != 1: + raise ValueError("One and only one argument must be 0") + if resistance < 0: + raise ValueError("Resistance cannot be negative") + if voltage == 0: + return {"voltage": float(current * resistance)} + elif current == 0: + return {"current": voltage / resistance} + elif resistance == 0: + return {"resistance": voltage / current} + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/file_transfer/__init__.py b/file_transfer/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/file_transfer/mytext.txt b/file_transfer/mytext.txt new file mode 100644 index 000000000000..54cfa7f766c7 --- /dev/null +++ b/file_transfer/mytext.txt @@ -0,0 +1,6 @@ +Hello +This is sample data +«küßî» +“ЌύБЇ” +😀😉 +😋 diff --git a/file_transfer/receive_file.py b/file_transfer/receive_file.py new file mode 100644 index 000000000000..cfba6ed88484 --- /dev/null +++ b/file_transfer/receive_file.py @@ -0,0 +1,23 @@ +if __name__ == "__main__": + import socket # Import socket module + + sock = socket.socket() # Create a socket object + host = socket.gethostname() # Get local machine name + port = 12312 + + sock.connect((host, port)) + sock.send(b"Hello server!") + + with open("Received_file", "wb") as out_file: + print("File opened") + print("Receiving data...") + while True: + data = sock.recv(1024) + print(f"data={data}") + if not data: + break + out_file.write(data) # Write data to a file + + print("Successfully got the file") + sock.close() + print("Connection closed") diff --git a/file_transfer/send_file.py b/file_transfer/send_file.py new file mode 100644 index 000000000000..5b53471dfb50 --- /dev/null +++ b/file_transfer/send_file.py @@ -0,0 +1,35 @@ +def send_file(filename: str = "mytext.txt", testing: bool = False) -> None: + import socket + + port = 12312 # Reserve a port for your service. + sock = socket.socket() # Create a socket object + host = socket.gethostname() # Get local machine name + sock.bind((host, port)) # Bind to the port + sock.listen(5) # Now wait for client connection. + + print("Server listening....") + + while True: + conn, addr = sock.accept() # Establish connection with client. + print(f"Got connection from {addr}") + data = conn.recv(1024) + print(f"Server received {data}") + + with open(filename, "rb") as in_file: + data = in_file.read(1024) + while data: + conn.send(data) + print(f"Sent {data!r}") + data = in_file.read(1024) + + print("Done sending") + conn.close() + if testing: # Allow the test to complete + break + + sock.shutdown(1) + sock.close() + + +if __name__ == "__main__": + send_file() diff --git a/file_transfer/tests/__init__.py b/file_transfer/tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/file_transfer/tests/test_send_file.py b/file_transfer/tests/test_send_file.py new file mode 100644 index 000000000000..2a6008448362 --- /dev/null +++ b/file_transfer/tests/test_send_file.py @@ -0,0 +1,31 @@ +from unittest.mock import Mock, patch + +from file_transfer.send_file import send_file + + +@patch("socket.socket") +@patch("builtins.open") +def test_send_file_running_as_expected(file, sock): + # ===== initialization ===== + conn = Mock() + sock.return_value.accept.return_value = conn, Mock() + f = iter([1, None]) + file.return_value.__enter__.return_value.read.side_effect = lambda _: next(f) + + # ===== invoke ===== + send_file(filename="mytext.txt", testing=True) + + # ===== ensurance ===== + sock.assert_called_once() + sock.return_value.bind.assert_called_once() + sock.return_value.listen.assert_called_once() + sock.return_value.accept.assert_called_once() + conn.recv.assert_called_once() + + file.return_value.__enter__.assert_called_once() + file.return_value.__enter__.return_value.read.assert_called() + + conn.send.assert_called_once() + conn.close.assert_called_once() + sock.return_value.shutdown.assert_called_once() + sock.return_value.close.assert_called_once() diff --git a/file_transfer_protocol/ftp_client_server.py b/file_transfer_protocol/ftp_client_server.py deleted file mode 100644 index 414c336dee9f..000000000000 --- a/file_transfer_protocol/ftp_client_server.py +++ /dev/null @@ -1,57 +0,0 @@ -# server - -import socket # Import socket module - -port = 60000 # Reserve a port for your service. -s = socket.socket() # Create a socket object -host = socket.gethostname() # Get local machine name -s.bind((host, port)) # Bind to the port -s.listen(5) # Now wait for client connection. - -print('Server listening....') - -while True: - conn, addr = s.accept() # Establish connection with client. - print('Got connection from', addr) - data = conn.recv(1024) - print('Server received', repr(data)) - - filename = 'mytext.txt' - with open(filename, 'rb') as f: - in_data = f.read(1024) - while in_data: - conn.send(in_data) - print('Sent ', repr(in_data)) - in_data = f.read(1024) - - print('Done sending') - conn.send('Thank you for connecting') - conn.close() - - -# client side server - -import socket # Import socket module - -s = socket.socket() # Create a socket object -host = socket.gethostname() # Get local machine name -port = 60000 # Reserve a port for your service. - -s.connect((host, port)) -s.send("Hello server!") - -with open('received_file', 'wb') as f: - print('file opened') - while True: - print('receiving data...') - data = s.recv(1024) - print('data=%s', (data)) - if not data: - break - # write data to a file - f.write(data) - -f.close() -print('Successfully get the file') -s.close() -print('connection closed') diff --git a/file_transfer_protocol/ftp_send_receive.py b/file_transfer_protocol/ftp_send_receive.py deleted file mode 100644 index 6a9819ef3f21..000000000000 --- a/file_transfer_protocol/ftp_send_receive.py +++ /dev/null @@ -1,36 +0,0 @@ -""" -File transfer protocol used to send and receive files using FTP server. -Use credentials to provide access to the FTP client - -Note: Do not use root username & password for security reasons -Create a seperate user and provide access to a home directory of the user -Use login id and password of the user created -cwd here stands for current working directory -""" - -from ftplib import FTP -ftp = FTP('xxx.xxx.x.x') # Enter the ip address or the domain name here -ftp.login(user='username', passwd='password') -ftp.cwd('/Enter the directory here/') - -""" -The file which will be received via the FTP server -Enter the location of the file where the file is received -""" - -def ReceiveFile(): - FileName = 'example.txt' """ Enter the location of the file """ - with open(FileName, 'wb') as LocalFile: - ftp.retrbinary('RETR ' + FileName, LocalFile.write, 1024) - ftp.quit() - -""" -The file which will be sent via the FTP server -The file send will be send to the current working directory -""" - -def SendFile(): - FileName = 'example.txt' """ Enter the name of the file """ - with open(FileName, 'rb') as LocalFile: - ftp.storbinary('STOR ' + FileName, LocalFile) - ftp.quit() diff --git a/fuzzy_logic/__init__.py b/fuzzy_logic/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py new file mode 100644 index 000000000000..0f573f158663 --- /dev/null +++ b/fuzzy_logic/fuzzy_operations.py @@ -0,0 +1,102 @@ +"""README, Author - Jigyasa Gandhi(mailto:jigsgandhi97@gmail.com) +Requirements: + - scikit-fuzzy + - numpy + - matplotlib +Python: + - 3.5 +""" +import numpy as np +import skfuzzy as fuzz + +if __name__ == "__main__": + # Create universe of discourse in Python using linspace () + X = np.linspace(start=0, stop=75, num=75, endpoint=True, retstep=False) + + # Create two fuzzy sets by defining any membership function + # (trapmf(), gbellmf(), gaussmf(), etc). + abc1 = [0, 25, 50] + abc2 = [25, 50, 75] + young = fuzz.membership.trimf(X, abc1) + middle_aged = fuzz.membership.trimf(X, abc2) + + # Compute the different operations using inbuilt functions. + one = np.ones(75) + zero = np.zeros((75,)) + # 1. Union = max(µA(x), µB(x)) + union = fuzz.fuzzy_or(X, young, X, middle_aged)[1] + # 2. Intersection = min(µA(x), µB(x)) + intersection = fuzz.fuzzy_and(X, young, X, middle_aged)[1] + # 3. Complement (A) = (1- min(µA(x)) + complement_a = fuzz.fuzzy_not(young) + # 4. Difference (A/B) = min(µA(x),(1- µB(x))) + difference = fuzz.fuzzy_and(X, young, X, fuzz.fuzzy_not(middle_aged)[1])[1] + # 5. Algebraic Sum = [µA(x) + µB(x) – (µA(x) * µB(x))] + alg_sum = young + middle_aged - (young * middle_aged) + # 6. Algebraic Product = (µA(x) * µB(x)) + alg_product = young * middle_aged + # 7. Bounded Sum = min[1,(µA(x), µB(x))] + bdd_sum = fuzz.fuzzy_and(X, one, X, young + middle_aged)[1] + # 8. Bounded difference = min[0,(µA(x), µB(x))] + bdd_difference = fuzz.fuzzy_or(X, zero, X, young - middle_aged)[1] + + # max-min composition + # max-product composition + + # Plot each set A, set B and each operation result using plot() and subplot(). + from matplotlib import pyplot as plt + + plt.figure() + + plt.subplot(4, 3, 1) + plt.plot(X, young) + plt.title("Young") + plt.grid(True) + + plt.subplot(4, 3, 2) + plt.plot(X, middle_aged) + plt.title("Middle aged") + plt.grid(True) + + plt.subplot(4, 3, 3) + plt.plot(X, union) + plt.title("union") + plt.grid(True) + + plt.subplot(4, 3, 4) + plt.plot(X, intersection) + plt.title("intersection") + plt.grid(True) + + plt.subplot(4, 3, 5) + plt.plot(X, complement_a) + plt.title("complement_a") + plt.grid(True) + + plt.subplot(4, 3, 6) + plt.plot(X, difference) + plt.title("difference a/b") + plt.grid(True) + + plt.subplot(4, 3, 7) + plt.plot(X, alg_sum) + plt.title("alg_sum") + plt.grid(True) + + plt.subplot(4, 3, 8) + plt.plot(X, alg_product) + plt.title("alg_product") + plt.grid(True) + + plt.subplot(4, 3, 9) + plt.plot(X, bdd_sum) + plt.title("bdd_sum") + plt.grid(True) + + plt.subplot(4, 3, 10) + plt.plot(X, bdd_difference) + plt.title("bdd_difference") + plt.grid(True) + + plt.subplots_adjust(hspace=0.5) + plt.show() diff --git a/genetic_algorithm/__init__.py b/genetic_algorithm/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py new file mode 100644 index 000000000000..97dbe182bc82 --- /dev/null +++ b/genetic_algorithm/basic_string.py @@ -0,0 +1,175 @@ +""" +Simple multithreaded algorithm to show how the 4 phases of a genetic algorithm works +(Evaluation, Selection, Crossover and Mutation) +https://en.wikipedia.org/wiki/Genetic_algorithm +Author: D4rkia +""" + +from __future__ import annotations + +import random + +# Maximum size of the population. bigger could be faster but is more memory expensive +N_POPULATION = 200 +# Number of elements selected in every generation for evolution the selection takes +# place from the best to the worst of that generation must be smaller than N_POPULATION +N_SELECTED = 50 +# Probability that an element of a generation can mutate changing one of its genes this +# guarantees that all genes will be used during evolution +MUTATION_PROBABILITY = 0.4 +# just a seed to improve randomness required by the algorithm +random.seed(random.randint(0, 1000)) + + +def basic(target: str, genes: list[str], debug: bool = True) -> tuple[int, int, str]: + """ + Verify that the target contains no genes besides the ones inside genes variable. + + >>> from string import ascii_lowercase + >>> basic("doctest", ascii_lowercase, debug=False)[2] + 'doctest' + >>> genes = list(ascii_lowercase) + >>> genes.remove("e") + >>> basic("test", genes) + Traceback (most recent call last): + ... + ValueError: ['e'] is not in genes list, evolution cannot converge + >>> genes.remove("s") + >>> basic("test", genes) + Traceback (most recent call last): + ... + ValueError: ['e', 's'] is not in genes list, evolution cannot converge + >>> genes.remove("t") + >>> basic("test", genes) + Traceback (most recent call last): + ... + ValueError: ['e', 's', 't'] is not in genes list, evolution cannot converge + """ + + # Verify if N_POPULATION is bigger than N_SELECTED + if N_POPULATION < N_SELECTED: + raise ValueError(f"{N_POPULATION} must be bigger than {N_SELECTED}") + # Verify that the target contains no genes besides the ones inside genes variable. + not_in_genes_list = sorted({c for c in target if c not in genes}) + if not_in_genes_list: + raise ValueError( + f"{not_in_genes_list} is not in genes list, evolution cannot converge" + ) + + # Generate random starting population + population = [] + for _ in range(N_POPULATION): + population.append("".join([random.choice(genes) for i in range(len(target))])) + + # Just some logs to know what the algorithms is doing + generation, total_population = 0, 0 + + # This loop will end when we will find a perfect match for our target + while True: + generation += 1 + total_population += len(population) + + # Random population created now it's time to evaluate + def evaluate(item: str, main_target: str = target) -> tuple[str, float]: + """ + Evaluate how similar the item is with the target by just + counting each char in the right position + >>> evaluate("Helxo Worlx", Hello World) + ["Helxo Worlx", 9] + """ + score = len( + [g for position, g in enumerate(item) if g == main_target[position]] + ) + return (item, float(score)) + + # Adding a bit of concurrency can make everything faster, + # + # import concurrent.futures + # population_score: list[tuple[str, float]] = [] + # with concurrent.futures.ThreadPoolExecutor( + # max_workers=NUM_WORKERS) as executor: + # futures = {executor.submit(evaluate, item) for item in population} + # concurrent.futures.wait(futures) + # population_score = [item.result() for item in futures] + # + # but with a simple algorithm like this will probably be slower + # we just need to call evaluate for every item inside population + population_score = [evaluate(item) for item in population] + + # Check if there is a matching evolution + population_score = sorted(population_score, key=lambda x: x[1], reverse=True) + if population_score[0][0] == target: + return (generation, total_population, population_score[0][0]) + + # Print the Best result every 10 generation + # just to know that the algorithm is working + if debug and generation % 10 == 0: + print( + f"\nGeneration: {generation}" + f"\nTotal Population:{total_population}" + f"\nBest score: {population_score[0][1]}" + f"\nBest string: {population_score[0][0]}" + ) + + # Flush the old population keeping some of the best evolutions + # Keeping this avoid regression of evolution + population_best = population[: int(N_POPULATION / 3)] + population.clear() + population.extend(population_best) + # Normalize population score from 0 to 1 + population_score = [ + (item, score / len(target)) for item, score in population_score + ] + + # Select, Crossover and Mutate a new population + def select(parent_1: tuple[str, float]) -> list[str]: + """Select the second parent and generate new population""" + pop = [] + # Generate more child proportionally to the fitness score + child_n = int(parent_1[1] * 100) + 1 + child_n = 10 if child_n >= 10 else child_n + for _ in range(child_n): + parent_2 = population_score[random.randint(0, N_SELECTED)][0] + child_1, child_2 = crossover(parent_1[0], parent_2) + # Append new string to the population list + pop.append(mutate(child_1)) + pop.append(mutate(child_2)) + return pop + + def crossover(parent_1: str, parent_2: str) -> tuple[str, str]: + """Slice and combine two string in a random point""" + random_slice = random.randint(0, len(parent_1) - 1) + child_1 = parent_1[:random_slice] + parent_2[random_slice:] + child_2 = parent_2[:random_slice] + parent_1[random_slice:] + return (child_1, child_2) + + def mutate(child: str) -> str: + """Mutate a random gene of a child with another one from the list""" + child_list = list(child) + if random.uniform(0, 1) < MUTATION_PROBABILITY: + child_list[random.randint(0, len(child)) - 1] = random.choice(genes) + return "".join(child_list) + + # This is Selection + for i in range(N_SELECTED): + population.extend(select(population_score[int(i)])) + # Check if the population has already reached the maximum value and if so, + # break the cycle. if this check is disabled the algorithm will take + # forever to compute large strings but will also calculate small string in + # a lot fewer generations + if len(population) > N_POPULATION: + break + + +if __name__ == "__main__": + target_str = ( + "This is a genetic algorithm to evaluate, combine, evolve, and mutate a string!" + ) + genes_list = list( + " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm" + "nopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\" + ) + print( + "\nGeneration: %s\nTotal Population: %s\nTarget: %s" + % basic(target_str, genes_list) + ) diff --git a/geodesy/__init__.py b/geodesy/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/geodesy/haversine_distance.py b/geodesy/haversine_distance.py new file mode 100644 index 000000000000..de8ac7f88302 --- /dev/null +++ b/geodesy/haversine_distance.py @@ -0,0 +1,56 @@ +from math import asin, atan, cos, radians, sin, sqrt, tan + + +def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float: + """ + Calculate great circle distance between two points in a sphere, + given longitudes and latitudes https://en.wikipedia.org/wiki/Haversine_formula + + We know that the globe is "sort of" spherical, so a path between two points + isn't exactly a straight line. We need to account for the Earth's curvature + when calculating distance from point A to B. This effect is negligible for + small distances but adds up as distance increases. The Haversine method treats + the earth as a sphere which allows us to "project" the two points A and B + onto the surface of that sphere and approximate the spherical distance between + them. Since the Earth is not a perfect sphere, other methods which model the + Earth's ellipsoidal nature are more accurate but a quick and modifiable + computation like Haversine can be handy for shorter range distances. + + Args: + lat1, lon1: latitude and longitude of coordinate 1 + lat2, lon2: latitude and longitude of coordinate 2 + Returns: + geographical distance between two points in metres + >>> from collections import namedtuple + >>> point_2d = namedtuple("point_2d", "lat lon") + >>> SAN_FRANCISCO = point_2d(37.774856, -122.424227) + >>> YOSEMITE = point_2d(37.864742, -119.537521) + >>> f"{haversine_distance(*SAN_FRANCISCO, *YOSEMITE):0,.0f} meters" + '254,352 meters' + """ + # CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System + # Distance in metres(m) + AXIS_A = 6378137.0 + AXIS_B = 6356752.314245 + RADIUS = 6378137 + # Equation parameters + # Equation https://en.wikipedia.org/wiki/Haversine_formula#Formulation + flattening = (AXIS_A - AXIS_B) / AXIS_A + phi_1 = atan((1 - flattening) * tan(radians(lat1))) + phi_2 = atan((1 - flattening) * tan(radians(lat2))) + lambda_1 = radians(lon1) + lambda_2 = radians(lon2) + # Equation + sin_sq_phi = sin((phi_2 - phi_1) / 2) + sin_sq_lambda = sin((lambda_2 - lambda_1) / 2) + # Square both values + sin_sq_phi *= sin_sq_phi + sin_sq_lambda *= sin_sq_lambda + h_value = sqrt(sin_sq_phi + (cos(phi_1) * cos(phi_2) * sin_sq_lambda)) + return 2 * RADIUS * asin(h_value) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/geodesy/lamberts_ellipsoidal_distance.py b/geodesy/lamberts_ellipsoidal_distance.py new file mode 100644 index 000000000000..bf8f1b9a5080 --- /dev/null +++ b/geodesy/lamberts_ellipsoidal_distance.py @@ -0,0 +1,86 @@ +from math import atan, cos, radians, sin, tan + +from .haversine_distance import haversine_distance + + +def lamberts_ellipsoidal_distance( + lat1: float, lon1: float, lat2: float, lon2: float +) -> float: + + """ + Calculate the shortest distance along the surface of an ellipsoid between + two points on the surface of earth given longitudes and latitudes + https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines + + NOTE: This algorithm uses geodesy/haversine_distance.py to compute central angle, + sigma + + Representing the earth as an ellipsoid allows us to approximate distances between + points on the surface much better than a sphere. Ellipsoidal formulas treat the + Earth as an oblate ellipsoid which means accounting for the flattening that happens + at the North and South poles. Lambert's formulae provide accuracy on the order of + 10 meteres over thousands of kilometeres. Other methods can provide + millimeter-level accuracy but this is a simpler method to calculate long range + distances without increasing computational intensity. + + Args: + lat1, lon1: latitude and longitude of coordinate 1 + lat2, lon2: latitude and longitude of coordinate 2 + Returns: + geographical distance between two points in metres + + >>> from collections import namedtuple + >>> point_2d = namedtuple("point_2d", "lat lon") + >>> SAN_FRANCISCO = point_2d(37.774856, -122.424227) + >>> YOSEMITE = point_2d(37.864742, -119.537521) + >>> NEW_YORK = point_2d(40.713019, -74.012647) + >>> VENICE = point_2d(45.443012, 12.313071) + >>> f"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *YOSEMITE):0,.0f} meters" + '254,351 meters' + >>> f"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *NEW_YORK):0,.0f} meters" + '4,138,992 meters' + >>> f"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *VENICE):0,.0f} meters" + '9,737,326 meters' + """ + + # CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System + # Distance in metres(m) + AXIS_A = 6378137.0 + AXIS_B = 6356752.314245 + EQUATORIAL_RADIUS = 6378137 + + # Equation Parameters + # https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines + flattening = (AXIS_A - AXIS_B) / AXIS_A + # Parametric latitudes + # https://en.wikipedia.org/wiki/Latitude#Parametric_(or_reduced)_latitude + b_lat1 = atan((1 - flattening) * tan(radians(lat1))) + b_lat2 = atan((1 - flattening) * tan(radians(lat2))) + + # Compute central angle between two points + # using haversine theta. sigma = haversine_distance / equatorial radius + sigma = haversine_distance(lat1, lon1, lat2, lon2) / EQUATORIAL_RADIUS + + # Intermediate P and Q values + P_value = (b_lat1 + b_lat2) / 2 + Q_value = (b_lat2 - b_lat1) / 2 + + # Intermediate X value + # X = (sigma - sin(sigma)) * sin^2Pcos^2Q / cos^2(sigma/2) + X_numerator = (sin(P_value) ** 2) * (cos(Q_value) ** 2) + X_demonimator = cos(sigma / 2) ** 2 + X_value = (sigma - sin(sigma)) * (X_numerator / X_demonimator) + + # Intermediate Y value + # Y = (sigma + sin(sigma)) * cos^2Psin^2Q / sin^2(sigma/2) + Y_numerator = (cos(P_value) ** 2) * (sin(Q_value) ** 2) + Y_denominator = sin(sigma / 2) ** 2 + Y_value = (sigma + sin(sigma)) * (Y_numerator / Y_denominator) + + return EQUATORIAL_RADIUS * (sigma - ((flattening / 2) * (X_value + Y_value))) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/graphics/__init__.py b/graphics/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/graphics/bezier_curve.py b/graphics/bezier_curve.py new file mode 100644 index 000000000000..295ff47e8cdc --- /dev/null +++ b/graphics/bezier_curve.py @@ -0,0 +1,114 @@ +# https://en.wikipedia.org/wiki/B%C3%A9zier_curve +# https://www.tutorialspoint.com/computer_graphics/computer_graphics_curves.htm +from __future__ import annotations + +from scipy.special import comb + + +class BezierCurve: + """ + Bezier curve is a weighted sum of a set of control points. + Generate Bezier curves from a given set of control points. + This implementation works only for 2d coordinates in the xy plane. + """ + + def __init__(self, list_of_points: list[tuple[float, float]]): + """ + list_of_points: Control points in the xy plane on which to interpolate. These + points control the behavior (shape) of the Bezier curve. + """ + self.list_of_points = list_of_points + # Degree determines the flexibility of the curve. + # Degree = 1 will produce a straight line. + self.degree = len(list_of_points) - 1 + + def basis_function(self, t: float) -> list[float]: + """ + The basis function determines the weight of each control point at time t. + t: time value between 0 and 1 inclusive at which to evaluate the basis of + the curve. + returns the x, y values of basis function at time t + + >>> curve = BezierCurve([(1,1), (1,2)]) + >>> curve.basis_function(0) + [1.0, 0.0] + >>> curve.basis_function(1) + [0.0, 1.0] + """ + assert 0 <= t <= 1, "Time t must be between 0 and 1." + output_values: list[float] = [] + for i in range(len(self.list_of_points)): + # basis function for each i + output_values.append( + comb(self.degree, i) * ((1 - t) ** (self.degree - i)) * (t ** i) + ) + # the basis must sum up to 1 for it to produce a valid Bezier curve. + assert round(sum(output_values), 5) == 1 + return output_values + + def bezier_curve_function(self, t: float) -> tuple[float, float]: + """ + The function to produce the values of the Bezier curve at time t. + t: the value of time t at which to evaluate the Bezier function + Returns the x, y coordinates of the Bezier curve at time t. + The first point in the curve is when t = 0. + The last point in the curve is when t = 1. + + >>> curve = BezierCurve([(1,1), (1,2)]) + >>> curve.bezier_curve_function(0) + (1.0, 1.0) + >>> curve.bezier_curve_function(1) + (1.0, 2.0) + """ + + assert 0 <= t <= 1, "Time t must be between 0 and 1." + + basis_function = self.basis_function(t) + x = 0.0 + y = 0.0 + for i in range(len(self.list_of_points)): + # For all points, sum up the product of i-th basis function and i-th point. + x += basis_function[i] * self.list_of_points[i][0] + y += basis_function[i] * self.list_of_points[i][1] + return (x, y) + + def plot_curve(self, step_size: float = 0.01): + """ + Plots the Bezier curve using matplotlib plotting capabilities. + step_size: defines the step(s) at which to evaluate the Bezier curve. + The smaller the step size, the finer the curve produced. + """ + from matplotlib import pyplot as plt + + to_plot_x: list[float] = [] # x coordinates of points to plot + to_plot_y: list[float] = [] # y coordinates of points to plot + + t = 0.0 + while t <= 1: + value = self.bezier_curve_function(t) + to_plot_x.append(value[0]) + to_plot_y.append(value[1]) + t += step_size + + x = [i[0] for i in self.list_of_points] + y = [i[1] for i in self.list_of_points] + + plt.plot( + to_plot_x, + to_plot_y, + color="blue", + label="Curve of Degree " + str(self.degree), + ) + plt.scatter(x, y, color="red", label="Control Points") + plt.legend() + plt.show() + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + BezierCurve([(1, 2), (3, 5)]).plot_curve() # degree 1 + BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve() # degree 2 + BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve() # degree 3 diff --git a/graphics/vector3_for_2d_rendering.py b/graphics/vector3_for_2d_rendering.py new file mode 100644 index 000000000000..dfa22262a8d8 --- /dev/null +++ b/graphics/vector3_for_2d_rendering.py @@ -0,0 +1,102 @@ +""" +render 3d points for 2d surfaces. +""" + +from __future__ import annotations + +import math + +__version__ = "2020.9.26" +__author__ = "xcodz-dot, cclaus, dhruvmanila" + + +def convert_to_2d( + x: float, y: float, z: float, scale: float, distance: float +) -> tuple[float, float]: + """ + Converts 3d point to a 2d drawable point + + >>> convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0) + (7.6923076923076925, 15.384615384615385) + + >>> convert_to_2d(1, 2, 3, 10, 10) + (7.6923076923076925, 15.384615384615385) + + >>> convert_to_2d("1", 2, 3, 10, 10) # '1' is str + Traceback (most recent call last): + ... + TypeError: Input values must either be float or int: ['1', 2, 3, 10, 10] + """ + if not all(isinstance(val, (float, int)) for val in locals().values()): + raise TypeError( + "Input values must either be float or int: " f"{list(locals().values())}" + ) + projected_x = ((x * distance) / (z + distance)) * scale + projected_y = ((y * distance) / (z + distance)) * scale + return projected_x, projected_y + + +def rotate( + x: float, y: float, z: float, axis: str, angle: float +) -> tuple[float, float, float]: + """ + rotate a point around a certain axis with a certain angle + angle can be any integer between 1, 360 and axis can be any one of + 'x', 'y', 'z' + + >>> rotate(1.0, 2.0, 3.0, 'y', 90.0) + (3.130524675073759, 2.0, 0.4470070007889556) + + >>> rotate(1, 2, 3, "z", 180) + (0.999736015495891, -2.0001319704760485, 3) + + >>> rotate('1', 2, 3, "z", 90.0) # '1' is str + Traceback (most recent call last): + ... + TypeError: Input values except axis must either be float or int: ['1', 2, 3, 90.0] + + >>> rotate(1, 2, 3, "n", 90) # 'n' is not a valid axis + Traceback (most recent call last): + ... + ValueError: not a valid axis, choose one of 'x', 'y', 'z' + + >>> rotate(1, 2, 3, "x", -90) + (1, -2.5049096187183877, -2.5933429780983657) + + >>> rotate(1, 2, 3, "x", 450) # 450 wrap around to 90 + (1, 3.5776792428178217, -0.44744970165427644) + """ + if not isinstance(axis, str): + raise TypeError("Axis must be a str") + input_variables = locals() + del input_variables["axis"] + if not all(isinstance(val, (float, int)) for val in input_variables.values()): + raise TypeError( + "Input values except axis must either be float or int: " + f"{list(input_variables.values())}" + ) + angle = (angle % 360) / 450 * 180 / math.pi + if axis == "z": + new_x = x * math.cos(angle) - y * math.sin(angle) + new_y = y * math.cos(angle) + x * math.sin(angle) + new_z = z + elif axis == "x": + new_y = y * math.cos(angle) - z * math.sin(angle) + new_z = z * math.cos(angle) + y * math.sin(angle) + new_x = x + elif axis == "y": + new_x = x * math.cos(angle) - z * math.sin(angle) + new_z = z * math.cos(angle) + x * math.sin(angle) + new_y = y + else: + raise ValueError("not a valid axis, choose one of 'x', 'y', 'z'") + + return new_x, new_y, new_z + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print(f"{convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0) = }") + print(f"{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }") diff --git a/graphs/BFS.py b/graphs/BFS.py deleted file mode 100644 index bf9b572cec50..000000000000 --- a/graphs/BFS.py +++ /dev/null @@ -1,39 +0,0 @@ -"""pseudo-code""" - -""" -BFS(graph G, start vertex s): -// all nodes initially unexplored -mark s as explored -let Q = queue data structure, initialized with s -while Q is non-empty: - remove the first node of Q, call it v - for each edge(v, w): // for w in graph[v] - if w unexplored: - mark w as explored - add w to Q (at the end) - -""" - -import collections - - -def bfs(graph, start): - explored, queue = set(), [start] # collections.deque([start]) - explored.add(start) - while queue: - v = queue.pop(0) # queue.popleft() - for w in graph[v]: - if w not in explored: - explored.add(w) - queue.append(w) - return explored - - -G = {'A': ['B', 'C'], - 'B': ['A', 'D', 'E'], - 'C': ['A', 'F'], - 'D': ['B'], - 'E': ['B', 'F'], - 'F': ['C', 'E']} - -print(bfs(G, 'A')) diff --git a/graphs/DFS.py b/graphs/DFS.py deleted file mode 100644 index d3c34fabb7b3..000000000000 --- a/graphs/DFS.py +++ /dev/null @@ -1,36 +0,0 @@ -"""pseudo-code""" - -""" -DFS(graph G, start vertex s): -// all nodes initially unexplored -mark s as explored -for every edge (s, v): - if v unexplored: - DFS(G, v) -""" - - -def dfs(graph, start): - """The DFS function simply calls itself recursively for every unvisited child of its argument. We can emulate that - behaviour precisely using a stack of iterators. Instead of recursively calling with a node, we'll push an iterator - to the node's children onto the iterator stack. When the iterator at the top of the stack terminates, we'll pop - it off the stack.""" - explored, stack = set(), [start] - explored.add(start) - while stack: - v = stack.pop() # the only difference from BFS is to pop last element here instead of first one - for w in graph[v]: - if w not in explored: - explored.add(w) - stack.append(w) - return explored - - -G = {'A': ['B', 'C'], - 'B': ['A', 'D', 'E'], - 'C': ['A', 'F'], - 'D': ['B'], - 'E': ['B', 'F'], - 'F': ['C', 'E']} - -print(dfs(G, 'A')) diff --git a/graphs/Directed and Undirected (Weighted) Graph.py b/graphs/Directed and Undirected (Weighted) Graph.py deleted file mode 100644 index a31a4a96d6d0..000000000000 --- a/graphs/Directed and Undirected (Weighted) Graph.py +++ /dev/null @@ -1,472 +0,0 @@ -from collections import deque -import random as rand -import math as math -import time - -# the dfault weight is 1 if not assigend but all the implementation is weighted - -class DirectedGraph: - def __init__(self): - self.graph = {} - - # adding vertices and edges - # adding the weight is optional - # handels repetition - def add_pair(self, u, v, w = 1): - if self.graph.get(u): - if self.graph[u].count([w,v]) == 0: - self.graph[u].append([w, v]) - else: - self.graph[u] = [[w, v]] - if not self.graph.get(v): - self.graph[v] = [] - - def all_nodes(self): - return list(self.graph) - - # handels if the input does not exist - def remove_pair(self, u, v): - if self.graph.get(u): - for _ in self.graph[u]: - if _[1] == v: - self.graph[u].remove(_) - - # if no destination is meant the defaut value is -1 - def dfs(self, s = -2, d = -1): - if s == d: - return [] - stack = [] - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - ss = s - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - if __[1] == d: - visited.append(d) - return visited - else: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return visited - - # c is the count of nodes you want and if you leave it or pass -1 to the funtion the count - # will be random from 10 to 10000 - def fill_graph_randomly(self, c = -1): - if c == -1: - c = (math.floor(rand.random() * 10000)) + 10 - for _ in range(c): - # every vertex has max 100 edges - e = math.floor(rand.random() * 102) + 1 - for __ in range(e): - n = math.floor(rand.random() * (c)) + 1 - if n == _: - continue - self.add_pair(_, n, 1) - - def bfs(self, s = -2): - d = deque() - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - d.append(s) - visited.append(s) - while d: - s = d.popleft() - if len(self.graph[s]) != 0: - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - d.append(__[1]) - visited.append(__[1]) - return visited - def in_degree(self, u): - count = 0 - for _ in self.graph: - for __ in self.graph[_]: - if __[1] == u: - count += 1 - return count - - def out_degree(self, u): - return len(self.graph[u]) - - def topological_sort(self, s = -2): - stack = [] - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - ss = s - sorted_nodes = [] - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - sorted_nodes.append(stack.pop()) - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return sorted_nodes - - def cycle_nodes(self): - stack = [] - visited = [] - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - parent = -2 - indirect_parents = [] - ss = s - on_the_way_back = False - anticipating_nodes = set() - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back: - l = len(stack) - 1 - while True and l >= 0: - if stack[l] == __[1]: - anticipating_nodes.add(__[1]) - break - else: - anticipating_nodes.add(stack[l]) - l -= 1 - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - on_the_way_back = True - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - on_the_way_back = False - indirect_parents.append(parent) - parent = s - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return list(anticipating_nodes) - - def has_cycle(self): - stack = [] - visited = [] - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - parent = -2 - indirect_parents = [] - ss = s - on_the_way_back = False - anticipating_nodes = set() - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back: - l = len(stack) - 1 - while True and l >= 0: - if stack[l] == __[1]: - anticipating_nodes.add(__[1]) - break - else: - return True - anticipating_nodes.add(stack[l]) - l -= 1 - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - on_the_way_back = True - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - on_the_way_back = False - indirect_parents.append(parent) - parent = s - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return False - - def dfs_time(self, s = -2, e = -1): - begin = time.time() - self.dfs(s,e) - end = time.time() - return end - begin - - def bfs_time(self, s = -2): - begin = time.time() - self.bfs(s) - end = time.time() - return end - begin - -class Graph: - def __init__(self): - self.graph = {} - - # adding vertices and edges - # adding the weight is optional - # handels repetition - def add_pair(self, u, v, w = 1): - # check if the u exists - if self.graph.get(u): - # if there already is a edge - if self.graph[u].count([w,v]) == 0: - self.graph[u].append([w, v]) - else: - # if u does not exist - self.graph[u] = [[w, v]] - # add the other way - if self.graph.get(v): - # if there already is a edge - if self.graph[v].count([w,u]) == 0: - self.graph[v].append([w, u]) - else: - # if u does not exist - self.graph[v] = [[w, u]] - - # handels if the input does not exist - def remove_pair(self, u, v): - if self.graph.get(u): - for _ in self.graph[u]: - if _[1] == v: - self.graph[u].remove(_) - # the other way round - if self.graph.get(v): - for _ in self.graph[v]: - if _[1] == u: - self.graph[v].remove(_) - - # if no destination is meant the defaut value is -1 - def dfs(self, s = -2, d = -1): - if s == d: - return [] - stack = [] - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - ss = s - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - if __[1] == d: - visited.append(d) - return visited - else: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return visited - - # c is the count of nodes you want and if you leave it or pass -1 to the funtion the count - # will be random from 10 to 10000 - def fill_graph_randomly(self, c = -1): - if c == -1: - c = (math.floor(rand.random() * 10000)) + 10 - for _ in range(c): - # every vertex has max 100 edges - e = math.floor(rand.random() * 102) + 1 - for __ in range(e): - n = math.floor(rand.random() * (c)) + 1 - if n == _: - continue - self.add_pair(_, n, 1) - - def bfs(self, s = -2): - d = deque() - visited = [] - if s == -2: - s = list(self.graph.keys())[0] - d.append(s) - visited.append(s) - while d: - s = d.popleft() - if len(self.graph[s]) != 0: - for __ in self.graph[s]: - if visited.count(__[1]) < 1: - d.append(__[1]) - visited.append(__[1]) - return visited - def degree(self, u): - return len(self.graph[u]) - - def cycle_nodes(self): - stack = [] - visited = [] - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - parent = -2 - indirect_parents = [] - ss = s - on_the_way_back = False - anticipating_nodes = set() - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back: - l = len(stack) - 1 - while True and l >= 0: - if stack[l] == __[1]: - anticipating_nodes.add(__[1]) - break - else: - anticipating_nodes.add(stack[l]) - l -= 1 - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - on_the_way_back = True - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - on_the_way_back = False - indirect_parents.append(parent) - parent = s - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return list(anticipating_nodes) - - def has_cycle(self): - stack = [] - visited = [] - s = list(self.graph.keys())[0] - stack.append(s) - visited.append(s) - parent = -2 - indirect_parents = [] - ss = s - on_the_way_back = False - anticipating_nodes = set() - - while True: - # check if there is any non isolated nodes - if len(self.graph[s]) != 0: - ss = s - for __ in self.graph[s]: - if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back: - l = len(stack) - 1 - while True and l >= 0: - if stack[l] == __[1]: - anticipating_nodes.add(__[1]) - break - else: - return True - anticipating_nodes.add(stack[l]) - l -= 1 - if visited.count(__[1]) < 1: - stack.append(__[1]) - visited.append(__[1]) - ss =__[1] - break - - # check if all the children are visited - if s == ss : - stack.pop() - on_the_way_back = True - if len(stack) != 0: - s = stack[len(stack) - 1] - else: - on_the_way_back = False - indirect_parents.append(parent) - parent = s - s = ss - - # check if se have reached the starting point - if len(stack) == 0: - return False - def all_nodes(self): - return list(self.graph) - - def dfs_time(self, s = -2, e = -1): - begin = time.time() - self.dfs(s,e) - end = time.time() - return end - begin - - def bfs_time(self, s = -2): - begin = time.time() - self.bfs(s) - end = time.time() - return end - begin diff --git a/graphs/__init__.py b/graphs/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/graphs/a_star.py b/graphs/a_star.py index 584222e6f62b..cb5b2fcd16e8 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -1,44 +1,45 @@ -from __future__ import print_function +grid = [ + [0, 1, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles + [0, 1, 0, 0, 0, 0], + [0, 1, 0, 0, 1, 0], + [0, 0, 0, 0, 1, 0], +] -grid = [[0, 1, 0, 0, 0, 0], - [0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles - [0, 1, 0, 0, 0, 0], - [0, 1, 0, 0, 1, 0], - [0, 0, 0, 0, 1, 0]] - -''' +""" heuristic = [[9, 8, 7, 6, 5, 4], [8, 7, 6, 5, 4, 3], [7, 6, 5, 4, 3, 2], [6, 5, 4, 3, 2, 1], - [5, 4, 3, 2, 1, 0]]''' + [5, 4, 3, 2, 1, 0]]""" init = [0, 0] -goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x] +goal = [len(grid) - 1, len(grid[0]) - 1] # all coordinates are given in format [y,x] cost = 1 -#the cost map which pushes the path closer to the goal +# the cost map which pushes the path closer to the goal heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] -for i in range(len(grid)): - for j in range(len(grid[0])): +for i in range(len(grid)): + for j in range(len(grid[0])): heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1]) if grid[i][j] == 1: - heuristic[i][j] = 99 #added extra penalty in the heuristic map + heuristic[i][j] = 99 # added extra penalty in the heuristic map -#the actions we can take -delta = [[-1, 0 ], # go up - [ 0, -1], # go left - [ 1, 0 ], # go down - [ 0, 1 ]] # go right +# the actions we can take +delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] # go up # go left # go down # go right -#function to search the path -def search(grid,init,goal,cost,heuristic): +# function to search the path +def search(grid, init, goal, cost, heuristic): - closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid + closed = [ + [0 for col in range(len(grid[0]))] for row in range(len(grid)) + ] # the reference grid closed[init[0]][init[1]] = 1 - action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid + action = [ + [0 for col in range(len(grid[0]))] for row in range(len(grid)) + ] # the action grid x = init[0] y = init[1] @@ -47,29 +48,26 @@ def search(grid,init,goal,cost,heuristic): cell = [[f, g, x, y]] found = False # flag that is set when search is complete - resign = False # flag set if we can't find expand + resign = False # flag set if we can't find expand while not found and not resign: if len(cell) == 0: - resign = True return "FAIL" - else: - cell.sort()#to choose the least costliest action so as to move closer to the goal + else: # to choose the least costliest action so as to move closer to the goal + cell.sort() cell.reverse() next = cell.pop() x = next[2] y = next[3] g = next[1] - f = next[0] - if x == goal[0] and y == goal[1]: found = True else: - for i in range(len(delta)):#to try out different valid actions + for i in range(len(delta)): # to try out different valid actions x2 = x + delta[i][0] y2 = y + delta[i][1] - if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]): + if x2 >= 0 and x2 < len(grid) and y2 >= 0 and y2 < len(grid[0]): if closed[x2][y2] == 0 and grid[x2][y2] == 0: g2 = g + cost f2 = g2 + heuristic[x2][y2] @@ -79,7 +77,7 @@ def search(grid,init,goal,cost,heuristic): invpath = [] x = goal[0] y = goal[1] - invpath.append([x, y])#we get the reverse path from here + invpath.append([x, y]) # we get the reverse path from here while x != init[0] or y != init[1]: x2 = x - delta[action[x][y]][0] y2 = y - delta[action[x][y]][1] @@ -89,14 +87,14 @@ def search(grid,init,goal,cost,heuristic): path = [] for i in range(len(invpath)): - path.append(invpath[len(invpath) - 1 - i]) + path.append(invpath[len(invpath) - 1 - i]) print("ACTION MAP") for i in range(len(action)): print(action[i]) - + return path - -a = search(grid,init,goal,cost,heuristic) -for i in range(len(a)): - print(a[i]) + +a = search(grid, init, goal, cost, heuristic) +for i in range(len(a)): + print(a[i]) diff --git a/graphs/articulation_points.py b/graphs/articulation_points.py index 1173c4ea373c..7197369de090 100644 --- a/graphs/articulation_points.py +++ b/graphs/articulation_points.py @@ -1,5 +1,5 @@ # Finding Articulation Points in Undirected Graph -def computeAP(l): +def computeAP(l): # noqa: E741 n = len(l) outEdgeCount = 0 low = [0] * n @@ -33,12 +33,23 @@ def dfs(root, at, parent, outEdgeCount): if not visited[i]: outEdgeCount = 0 outEdgeCount = dfs(i, i, -1, outEdgeCount) - isArt[i] = (outEdgeCount > 1) + isArt[i] = outEdgeCount > 1 for x in range(len(isArt)): - if isArt[x] == True: + if isArt[x] is True: print(x) + # Adjacency list of graph -l = {0:[1,2], 1:[0,2], 2:[0,1,3,5], 3:[2,4], 4:[3], 5:[2,6,8], 6:[5,7], 7:[6,8], 8:[5,7]} -computeAP(l) +data = { + 0: [1, 2], + 1: [0, 2], + 2: [0, 1, 3, 5], + 3: [2, 4], + 4: [3], + 5: [2, 6, 8], + 6: [5, 7], + 7: [6, 8], + 8: [5, 7], +} +computeAP(data) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index 3b3abeb1720d..0f73d8d07b2a 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -1,51 +1,42 @@ -from __future__ import print_function - -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 - -try: - xrange # Python 2 -except NameError: - xrange = range # Python 3 - -# Accept No. of Nodes and edges -n, m = map(int, raw_input().split(" ")) - -# Initialising Dictionary of edges -g = {} -for i in xrange(n): - g[i + 1] = [] - -""" --------------------------------------------------------------------------------- - Accepting edges of Unweighted Directed Graphs --------------------------------------------------------------------------------- -""" -for _ in xrange(m): - x, y = map(int, raw_input().split(" ")) - g[x].append(y) - -""" --------------------------------------------------------------------------------- - Accepting edges of Unweighted Undirected Graphs --------------------------------------------------------------------------------- -""" -for _ in xrange(m): - x, y = map(int, raw_input().split(" ")) - g[x].append(y) - g[y].append(x) +from collections import deque -""" --------------------------------------------------------------------------------- - Accepting edges of Weighted Undirected Graphs --------------------------------------------------------------------------------- -""" -for _ in xrange(m): - x, y, r = map(int, raw_input().split(" ")) - g[x].append([y, r]) - g[y].append([x, r]) +if __name__ == "__main__": + # Accept No. of Nodes and edges + n, m = map(int, input().split(" ")) + + # Initialising Dictionary of edges + g = {} + for i in range(n): + g[i + 1] = [] + + """ + ---------------------------------------------------------------------------- + Accepting edges of Unweighted Directed Graphs + ---------------------------------------------------------------------------- + """ + for _ in range(m): + x, y = map(int, input().strip().split(" ")) + g[x].append(y) + + """ + ---------------------------------------------------------------------------- + Accepting edges of Unweighted Undirected Graphs + ---------------------------------------------------------------------------- + """ + for _ in range(m): + x, y = map(int, input().strip().split(" ")) + g[x].append(y) + g[y].append(x) + + """ + ---------------------------------------------------------------------------- + Accepting edges of Weighted Undirected Graphs + ---------------------------------------------------------------------------- + """ + for _ in range(m): + x, y, r = map(int, input().strip().split(" ")) + g[x].append([y, r]) + g[y].append([x, r]) """ -------------------------------------------------------------------------------- @@ -59,7 +50,7 @@ def dfs(G, s): - vis, S = set([s]), [s] + vis, S = {s}, [s] print(s) while S: flag = 0 @@ -80,14 +71,13 @@ def dfs(G, s): Args : G - Dictionary of edges s - Starting Node Vars : vis - Set of visited nodes - Q - Traveral Stack + Q - Traversal Stack -------------------------------------------------------------------------------- """ -from collections import deque def bfs(G, s): - vis, Q = set([s]), deque([s]) + vis, Q = {s}, deque([s]) print(s) while Q: u = Q.popleft() @@ -136,10 +126,11 @@ def dijk(G, s): Topological Sort -------------------------------------------------------------------------------- """ -from collections import deque -def topo(G, ind=None, Q=[1]): +def topo(G, ind=None, Q=None): + if Q is None: + Q = [1] if ind is None: ind = [0] * (len(G) + 1) # SInce oth Index is ignored for u in G: @@ -168,9 +159,10 @@ def topo(G, ind=None, Q=[1]): def adjm(): - n, a = raw_input(), [] - for i in xrange(n): - a.append(map(int, raw_input().split())) + n = input().strip() + a = [] + for i in range(n): + a.append(map(int, input().strip().split())) return a, n @@ -190,10 +182,10 @@ def adjm(): def floy(A_and_n): (A, n) = A_and_n dist = list(A) - path = [[0] * n for i in xrange(n)] - for k in xrange(n): - for i in xrange(n): - for j in xrange(n): + path = [[0] * n for i in range(n)] + for k in range(n): + for i in range(n): + for j in range(n): if dist[i][j] > dist[i][k] + dist[k][j]: dist[i][j] = dist[i][k] + dist[k][j] path[i][k] = k @@ -228,6 +220,7 @@ def prim(G, s): if v[1] < dist.get(v[0], 100000): dist[v[0]] = v[1] path[v[0]] = u + return dist """ @@ -242,11 +235,11 @@ def prim(G, s): def edglist(): - n, m = map(int, raw_input().split(" ")) - l = [] - for i in xrange(m): - l.append(map(int, raw_input().split(' '))) - return l, n + n, m = map(int, input().split(" ")) + edges = [] + for i in range(m): + edges.append(map(int, input().split(" "))) + return edges, n """ @@ -263,16 +256,16 @@ def krusk(E_and_n): # Sort edges on the basis of distance (E, n) = E_and_n E.sort(reverse=True, key=lambda x: x[2]) - s = [set([i]) for i in range(1, n + 1)] + s = [{i} for i in range(1, n + 1)] while True: if len(s) == 1: break print(s) x = E.pop() - for i in xrange(len(s)): + for i in range(len(s)): if x[0] in s[i]: break - for j in xrange(len(s)): + for j in range(len(s)): if x[1] in s[j]: if i == j: break diff --git a/graphs/bellman_ford.py b/graphs/bellman_ford.py index 82db80546b94..ace7985647bb 100644 --- a/graphs/bellman_ford.py +++ b/graphs/bellman_ford.py @@ -1,54 +1,56 @@ -from __future__ import print_function +from __future__ import annotations + def printDist(dist, V): - print("\nVertex Distance") - for i in range(V): - if dist[i] != float('inf') : - print(i,"\t",int(dist[i]),end = "\t") - else: - print(i,"\t","INF",end="\t") - print() - -def BellmanFord(graph, V, E, src): - mdist=[float('inf') for i in range(V)] - mdist[src] = 0.0 - - for i in range(V-1): - for j in range(V): - u = graph[j]["src"] - v = graph[j]["dst"] - w = graph[j]["weight"] - - if mdist[u] != float('inf') and mdist[u] + w < mdist[v]: - mdist[v] = mdist[u] + w - for j in range(V): - u = graph[j]["src"] - v = graph[j]["dst"] - w = graph[j]["weight"] - - if mdist[u] != float('inf') and mdist[u] + w < mdist[v]: - print("Negative cycle found. Solution not possible.") - return - - printDist(mdist, V) - - - -#MAIN -V = int(input("Enter number of vertices: ")) -E = int(input("Enter number of edges: ")) - -graph = [dict() for j in range(E)] - -for i in range(V): - graph[i][i] = 0.0 - -for i in range(E): - print("\nEdge ",i+1) - src = int(input("Enter source:")) - dst = int(input("Enter destination:")) - weight = float(input("Enter weight:")) - graph[i] = {"src": src,"dst": dst, "weight": weight} - -gsrc = int(input("\nEnter shortest path source:")) -BellmanFord(graph, V, E, gsrc) + print("Vertex Distance") + distances = ("INF" if d == float("inf") else d for d in dist) + print("\t".join(f"{i}\t{d}" for i, d in enumerate(distances))) + + +def BellmanFord(graph: list[dict[str, int]], V: int, E: int, src: int) -> int: + """ + Returns shortest paths from a vertex src to all + other vertices. + """ + mdist = [float("inf") for i in range(V)] + mdist[src] = 0.0 + + for i in range(V - 1): + for j in range(E): + u = graph[j]["src"] + v = graph[j]["dst"] + w = graph[j]["weight"] + + if mdist[u] != float("inf") and mdist[u] + w < mdist[v]: + mdist[v] = mdist[u] + w + for j in range(E): + u = graph[j]["src"] + v = graph[j]["dst"] + w = graph[j]["weight"] + + if mdist[u] != float("inf") and mdist[u] + w < mdist[v]: + print("Negative cycle found. Solution not possible.") + return + + printDist(mdist, V) + return src + + +if __name__ == "__main__": + V = int(input("Enter number of vertices: ").strip()) + E = int(input("Enter number of edges: ").strip()) + + graph = [dict() for j in range(E)] + + for i in range(E): + graph[i][i] = 0.0 + + for i in range(E): + print("\nEdge ", i + 1) + src = int(input("Enter source:").strip()) + dst = int(input("Enter destination:").strip()) + weight = float(input("Enter weight:").strip()) + graph[i] = {"src": src, "dst": dst, "weight": weight} + + gsrc = int(input("\nEnter shortest path source:").strip()) + BellmanFord(graph, V, E, gsrc) diff --git a/graphs/bfs_shortest_path.py b/graphs/bfs_shortest_path.py new file mode 100644 index 000000000000..754ba403537e --- /dev/null +++ b/graphs/bfs_shortest_path.py @@ -0,0 +1,106 @@ +"""Breadth-first search shortest path implementations. + doctest: + python -m doctest -v bfs_shortest_path.py + Manual test: + python bfs_shortest_path.py +""" +graph = { + "A": ["B", "C", "E"], + "B": ["A", "D", "E"], + "C": ["A", "F", "G"], + "D": ["B"], + "E": ["A", "B", "D"], + "F": ["C"], + "G": ["C"], +} + + +def bfs_shortest_path(graph: dict, start, goal) -> str: + """Find shortest path between `start` and `goal` nodes. + Args: + graph (dict): node/list of neighboring nodes key/value pairs. + start: start node. + goal: target node. + Returns: + Shortest path between `start` and `goal` nodes as a string of nodes. + 'Not found' string if no path found. + Example: + >>> bfs_shortest_path(graph, "G", "D") + ['G', 'C', 'A', 'B', 'D'] + """ + # keep track of explored nodes + explored = set() + # keep track of all the paths to be checked + queue = [[start]] + + # return path if start is goal + if start == goal: + return "That was easy! Start = goal" + + # keeps looping until all possible paths have been checked + while queue: + # pop the first path from the queue + path = queue.pop(0) + # get the last node from the path + node = path[-1] + if node not in explored: + neighbours = graph[node] + # go through all neighbour nodes, construct a new path and + # push it into the queue + for neighbour in neighbours: + new_path = list(path) + new_path.append(neighbour) + queue.append(new_path) + # return path if neighbour is goal + if neighbour == goal: + return new_path + + # mark node as explored + explored.add(node) + + # in case there's no path between the 2 nodes + return "So sorry, but a connecting path doesn't exist :(" + + +def bfs_shortest_path_distance(graph: dict, start, target) -> int: + """Find shortest path distance between `start` and `target` nodes. + Args: + graph: node/list of neighboring nodes key/value pairs. + start: node to start search from. + target: node to search for. + Returns: + Number of edges in shortest path between `start` and `target` nodes. + -1 if no path exists. + Example: + >>> bfs_shortest_path_distance(graph, "G", "D") + 4 + >>> bfs_shortest_path_distance(graph, "A", "A") + 0 + >>> bfs_shortest_path_distance(graph, "A", "H") + -1 + """ + if not graph or start not in graph or target not in graph: + return -1 + if start == target: + return 0 + queue = [start] + visited = set(start) + # Keep tab on distances from `start` node. + dist = {start: 0, target: -1} + while queue: + node = queue.pop(0) + if node == target: + dist[target] = ( + dist[node] if dist[target] == -1 else min(dist[target], dist[node]) + ) + for adjacent in graph[node]: + if adjacent not in visited: + visited.add(adjacent) + queue.append(adjacent) + dist[adjacent] = dist[node] + 1 + return dist[target] + + +if __name__ == "__main__": + print(bfs_shortest_path(graph, "G", "D")) # returns ['G', 'C', 'A', 'B', 'D'] + print(bfs_shortest_path_distance(graph, "G", "D")) # returns 4 diff --git a/graphs/bfs_zero_one_shortest_path.py b/graphs/bfs_zero_one_shortest_path.py new file mode 100644 index 000000000000..a725fae7e48f --- /dev/null +++ b/graphs/bfs_zero_one_shortest_path.py @@ -0,0 +1,138 @@ +from collections import deque +from dataclasses import dataclass +from typing import Iterator, List + +""" +Finding the shortest path in 0-1-graph in O(E + V) which is faster than dijkstra. +0-1-graph is the weighted graph with the weights equal to 0 or 1. +Link: https://codeforces.com/blog/entry/22276 +""" + + +@dataclass +class Edge: + """Weighted directed graph edge.""" + + destination_vertex: int + weight: int + + +class AdjacencyList: + """Graph adjacency list.""" + + def __init__(self, size: int): + self._graph: List[List[Edge]] = [[] for _ in range(size)] + self._size = size + + def __getitem__(self, vertex: int) -> Iterator[Edge]: + """Get all the vertices adjacent to the given one.""" + return iter(self._graph[vertex]) + + @property + def size(self): + return self._size + + def add_edge(self, from_vertex: int, to_vertex: int, weight: int): + """ + >>> g = AdjacencyList(2) + >>> g.add_edge(0, 1, 0) + >>> g.add_edge(1, 0, 1) + >>> list(g[0]) + [Edge(destination_vertex=1, weight=0)] + >>> list(g[1]) + [Edge(destination_vertex=0, weight=1)] + >>> g.add_edge(0, 1, 2) + Traceback (most recent call last): + ... + ValueError: Edge weight must be either 0 or 1. + >>> g.add_edge(0, 2, 1) + Traceback (most recent call last): + ... + ValueError: Vertex indexes must be in [0; size). + """ + if weight not in (0, 1): + raise ValueError("Edge weight must be either 0 or 1.") + + if to_vertex < 0 or to_vertex >= self.size: + raise ValueError("Vertex indexes must be in [0; size).") + + self._graph[from_vertex].append(Edge(to_vertex, weight)) + + def get_shortest_path(self, start_vertex: int, finish_vertex: int) -> int: + """ + Return the shortest distance from start_vertex to finish_vertex in 0-1-graph. + 1 1 1 + 0--------->3 6--------7>------->8 + | ^ ^ ^ |1 + | | | |0 v + 0| |0 1| 9-------->10 + | | | ^ 1 + v | | |0 + 1--------->2<-------4------->5 + 0 1 1 + >>> g = AdjacencyList(11) + >>> g.add_edge(0, 1, 0) + >>> g.add_edge(0, 3, 1) + >>> g.add_edge(1, 2, 0) + >>> g.add_edge(2, 3, 0) + >>> g.add_edge(4, 2, 1) + >>> g.add_edge(4, 5, 1) + >>> g.add_edge(4, 6, 1) + >>> g.add_edge(5, 9, 0) + >>> g.add_edge(6, 7, 1) + >>> g.add_edge(7, 8, 1) + >>> g.add_edge(8, 10, 1) + >>> g.add_edge(9, 7, 0) + >>> g.add_edge(9, 10, 1) + >>> g.add_edge(1, 2, 2) + Traceback (most recent call last): + ... + ValueError: Edge weight must be either 0 or 1. + >>> g.get_shortest_path(0, 3) + 0 + >>> g.get_shortest_path(0, 4) + Traceback (most recent call last): + ... + ValueError: No path from start_vertex to finish_vertex. + >>> g.get_shortest_path(4, 10) + 2 + >>> g.get_shortest_path(4, 8) + 2 + >>> g.get_shortest_path(0, 1) + 0 + >>> g.get_shortest_path(1, 0) + Traceback (most recent call last): + ... + ValueError: No path from start_vertex to finish_vertex. + """ + queue = deque([start_vertex]) + distances = [None for i in range(self.size)] + distances[start_vertex] = 0 + + while queue: + current_vertex = queue.popleft() + current_distance = distances[current_vertex] + + for edge in self[current_vertex]: + new_distance = current_distance + edge.weight + if ( + distances[edge.destination_vertex] is not None + and new_distance >= distances[edge.destination_vertex] + ): + continue + distances[edge.destination_vertex] = new_distance + if edge.weight == 0: + queue.appendleft(edge.destination_vertex) + else: + queue.append(edge.destination_vertex) + + if distances[finish_vertex] is None: + raise ValueError("No path from start_vertex to finish_vertex.") + + return distances[finish_vertex] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/graphs/bidirectional_a_star.py b/graphs/bidirectional_a_star.py new file mode 100644 index 000000000000..72ff4fa65ff0 --- /dev/null +++ b/graphs/bidirectional_a_star.py @@ -0,0 +1,257 @@ +""" +https://en.wikipedia.org/wiki/Bidirectional_search +""" + +from __future__ import annotations + +import time +from math import sqrt + +# 1 for manhattan, 0 for euclidean +HEURISTIC = 0 + +grid = [ + [0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0], + [1, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0], +] + +delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right + + +class Node: + """ + >>> k = Node(0, 0, 4, 3, 0, None) + >>> k.calculate_heuristic() + 5.0 + >>> n = Node(1, 4, 3, 4, 2, None) + >>> n.calculate_heuristic() + 2.0 + >>> l = [k, n] + >>> n == l[0] + False + >>> l.sort() + >>> n == l[0] + True + """ + + def __init__(self, pos_x, pos_y, goal_x, goal_y, g_cost, parent): + self.pos_x = pos_x + self.pos_y = pos_y + self.pos = (pos_y, pos_x) + self.goal_x = goal_x + self.goal_y = goal_y + self.g_cost = g_cost + self.parent = parent + self.h_cost = self.calculate_heuristic() + self.f_cost = self.g_cost + self.h_cost + + def calculate_heuristic(self) -> float: + """ + Heuristic for the A* + """ + dy = self.pos_x - self.goal_x + dx = self.pos_y - self.goal_y + if HEURISTIC == 1: + return abs(dx) + abs(dy) + else: + return sqrt(dy ** 2 + dx ** 2) + + def __lt__(self, other) -> bool: + return self.f_cost < other.f_cost + + +class AStar: + """ + >>> astar = AStar((0, 0), (len(grid) - 1, len(grid[0]) - 1)) + >>> (astar.start.pos_y + delta[3][0], astar.start.pos_x + delta[3][1]) + (0, 1) + >>> [x.pos for x in astar.get_successors(astar.start)] + [(1, 0), (0, 1)] + >>> (astar.start.pos_y + delta[2][0], astar.start.pos_x + delta[2][1]) + (1, 0) + >>> astar.retrace_path(astar.start) + [(0, 0)] + >>> astar.search() # doctest: +NORMALIZE_WHITESPACE + [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3), (3, 3), + (4, 3), (4, 4), (5, 4), (5, 5), (6, 5), (6, 6)] + """ + + def __init__(self, start, goal): + self.start = Node(start[1], start[0], goal[1], goal[0], 0, None) + self.target = Node(goal[1], goal[0], goal[1], goal[0], 99999, None) + + self.open_nodes = [self.start] + self.closed_nodes = [] + + self.reached = False + + def search(self) -> list[tuple[int]]: + while self.open_nodes: + # Open Nodes are sorted using __lt__ + self.open_nodes.sort() + current_node = self.open_nodes.pop(0) + + if current_node.pos == self.target.pos: + self.reached = True + return self.retrace_path(current_node) + + self.closed_nodes.append(current_node) + successors = self.get_successors(current_node) + + for child_node in successors: + if child_node in self.closed_nodes: + continue + + if child_node not in self.open_nodes: + self.open_nodes.append(child_node) + else: + # retrieve the best current path + better_node = self.open_nodes.pop(self.open_nodes.index(child_node)) + + if child_node.g_cost < better_node.g_cost: + self.open_nodes.append(child_node) + else: + self.open_nodes.append(better_node) + + if not (self.reached): + return [(self.start.pos)] + + def get_successors(self, parent: Node) -> list[Node]: + """ + Returns a list of successors (both in the grid and free spaces) + """ + successors = [] + for action in delta: + pos_x = parent.pos_x + action[1] + pos_y = parent.pos_y + action[0] + if not (0 <= pos_x <= len(grid[0]) - 1 and 0 <= pos_y <= len(grid) - 1): + continue + + if grid[pos_y][pos_x] != 0: + continue + + successors.append( + Node( + pos_x, + pos_y, + self.target.pos_y, + self.target.pos_x, + parent.g_cost + 1, + parent, + ) + ) + return successors + + def retrace_path(self, node: Node) -> list[tuple[int]]: + """ + Retrace the path from parents to parents until start node + """ + current_node = node + path = [] + while current_node is not None: + path.append((current_node.pos_y, current_node.pos_x)) + current_node = current_node.parent + path.reverse() + return path + + +class BidirectionalAStar: + """ + >>> bd_astar = BidirectionalAStar((0, 0), (len(grid) - 1, len(grid[0]) - 1)) + >>> bd_astar.fwd_astar.start.pos == bd_astar.bwd_astar.target.pos + True + >>> bd_astar.retrace_bidirectional_path(bd_astar.fwd_astar.start, + ... bd_astar.bwd_astar.start) + [(0, 0)] + >>> bd_astar.search() # doctest: +NORMALIZE_WHITESPACE + [(0, 0), (0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (2, 4), + (2, 5), (3, 5), (4, 5), (5, 5), (5, 6), (6, 6)] + """ + + def __init__(self, start, goal): + self.fwd_astar = AStar(start, goal) + self.bwd_astar = AStar(goal, start) + self.reached = False + + def search(self) -> list[tuple[int]]: + while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes: + self.fwd_astar.open_nodes.sort() + self.bwd_astar.open_nodes.sort() + current_fwd_node = self.fwd_astar.open_nodes.pop(0) + current_bwd_node = self.bwd_astar.open_nodes.pop(0) + + if current_bwd_node.pos == current_fwd_node.pos: + self.reached = True + return self.retrace_bidirectional_path( + current_fwd_node, current_bwd_node + ) + + self.fwd_astar.closed_nodes.append(current_fwd_node) + self.bwd_astar.closed_nodes.append(current_bwd_node) + + self.fwd_astar.target = current_bwd_node + self.bwd_astar.target = current_fwd_node + + successors = { + self.fwd_astar: self.fwd_astar.get_successors(current_fwd_node), + self.bwd_astar: self.bwd_astar.get_successors(current_bwd_node), + } + + for astar in [self.fwd_astar, self.bwd_astar]: + for child_node in successors[astar]: + if child_node in astar.closed_nodes: + continue + + if child_node not in astar.open_nodes: + astar.open_nodes.append(child_node) + else: + # retrieve the best current path + better_node = astar.open_nodes.pop( + astar.open_nodes.index(child_node) + ) + + if child_node.g_cost < better_node.g_cost: + astar.open_nodes.append(child_node) + else: + astar.open_nodes.append(better_node) + + if not self.reached: + return [self.fwd_astar.start.pos] + + def retrace_bidirectional_path( + self, fwd_node: Node, bwd_node: Node + ) -> list[tuple[int]]: + fwd_path = self.fwd_astar.retrace_path(fwd_node) + bwd_path = self.bwd_astar.retrace_path(bwd_node) + bwd_path.pop() + bwd_path.reverse() + path = fwd_path + bwd_path + return path + + +if __name__ == "__main__": + # all coordinates are given in format [y,x] + import doctest + + doctest.testmod() + init = (0, 0) + goal = (len(grid) - 1, len(grid[0]) - 1) + for elem in grid: + print(elem) + + start_time = time.time() + a_star = AStar(init, goal) + path = a_star.search() + end_time = time.time() - start_time + print(f"AStar execution time = {end_time:f} seconds") + + bd_start_time = time.time() + bidir_astar = BidirectionalAStar(init, goal) + path = bidir_astar.search() + bd_end_time = time.time() - bd_start_time + print(f"BidirectionalAStar execution time = {bd_end_time:f} seconds") diff --git a/graphs/bidirectional_breadth_first_search.py b/graphs/bidirectional_breadth_first_search.py new file mode 100644 index 000000000000..39d8dc7d4187 --- /dev/null +++ b/graphs/bidirectional_breadth_first_search.py @@ -0,0 +1,181 @@ +""" +https://en.wikipedia.org/wiki/Bidirectional_search +""" + +from __future__ import annotations + +import time + +grid = [ + [0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0], + [1, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0], +] + +delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] # up, left, down, right + + +class Node: + def __init__(self, pos_x, pos_y, goal_x, goal_y, parent): + self.pos_x = pos_x + self.pos_y = pos_y + self.pos = (pos_y, pos_x) + self.goal_x = goal_x + self.goal_y = goal_y + self.parent = parent + + +class BreadthFirstSearch: + """ + >>> bfs = BreadthFirstSearch((0, 0), (len(grid) - 1, len(grid[0]) - 1)) + >>> (bfs.start.pos_y + delta[3][0], bfs.start.pos_x + delta[3][1]) + (0, 1) + >>> [x.pos for x in bfs.get_successors(bfs.start)] + [(1, 0), (0, 1)] + >>> (bfs.start.pos_y + delta[2][0], bfs.start.pos_x + delta[2][1]) + (1, 0) + >>> bfs.retrace_path(bfs.start) + [(0, 0)] + >>> bfs.search() # doctest: +NORMALIZE_WHITESPACE + [(0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (4, 1), + (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (6, 5), (6, 6)] + """ + + def __init__(self, start, goal): + self.start = Node(start[1], start[0], goal[1], goal[0], None) + self.target = Node(goal[1], goal[0], goal[1], goal[0], None) + + self.node_queue = [self.start] + self.reached = False + + def search(self) -> list[tuple[int]]: + while self.node_queue: + current_node = self.node_queue.pop(0) + + if current_node.pos == self.target.pos: + self.reached = True + return self.retrace_path(current_node) + + successors = self.get_successors(current_node) + + for node in successors: + self.node_queue.append(node) + + if not (self.reached): + return [(self.start.pos)] + + def get_successors(self, parent: Node) -> list[Node]: + """ + Returns a list of successors (both in the grid and free spaces) + """ + successors = [] + for action in delta: + pos_x = parent.pos_x + action[1] + pos_y = parent.pos_y + action[0] + if not (0 <= pos_x <= len(grid[0]) - 1 and 0 <= pos_y <= len(grid) - 1): + continue + + if grid[pos_y][pos_x] != 0: + continue + + successors.append( + Node(pos_x, pos_y, self.target.pos_y, self.target.pos_x, parent) + ) + return successors + + def retrace_path(self, node: Node) -> list[tuple[int]]: + """ + Retrace the path from parents to parents until start node + """ + current_node = node + path = [] + while current_node is not None: + path.append((current_node.pos_y, current_node.pos_x)) + current_node = current_node.parent + path.reverse() + return path + + +class BidirectionalBreadthFirstSearch: + """ + >>> bd_bfs = BidirectionalBreadthFirstSearch((0, 0), (len(grid) - 1, + ... len(grid[0]) - 1)) + >>> bd_bfs.fwd_bfs.start.pos == bd_bfs.bwd_bfs.target.pos + True + >>> bd_bfs.retrace_bidirectional_path(bd_bfs.fwd_bfs.start, + ... bd_bfs.bwd_bfs.start) + [(0, 0)] + >>> bd_bfs.search() # doctest: +NORMALIZE_WHITESPACE + [(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (2, 3), + (2, 4), (3, 4), (3, 5), (3, 6), (4, 6), (5, 6), (6, 6)] + """ + + def __init__(self, start, goal): + self.fwd_bfs = BreadthFirstSearch(start, goal) + self.bwd_bfs = BreadthFirstSearch(goal, start) + self.reached = False + + def search(self) -> list[tuple[int]]: + while self.fwd_bfs.node_queue or self.bwd_bfs.node_queue: + current_fwd_node = self.fwd_bfs.node_queue.pop(0) + current_bwd_node = self.bwd_bfs.node_queue.pop(0) + + if current_bwd_node.pos == current_fwd_node.pos: + self.reached = True + return self.retrace_bidirectional_path( + current_fwd_node, current_bwd_node + ) + + self.fwd_bfs.target = current_bwd_node + self.bwd_bfs.target = current_fwd_node + + successors = { + self.fwd_bfs: self.fwd_bfs.get_successors(current_fwd_node), + self.bwd_bfs: self.bwd_bfs.get_successors(current_bwd_node), + } + + for bfs in [self.fwd_bfs, self.bwd_bfs]: + for node in successors[bfs]: + bfs.node_queue.append(node) + + if not self.reached: + return [self.fwd_bfs.start.pos] + + def retrace_bidirectional_path( + self, fwd_node: Node, bwd_node: Node + ) -> list[tuple[int]]: + fwd_path = self.fwd_bfs.retrace_path(fwd_node) + bwd_path = self.bwd_bfs.retrace_path(bwd_node) + bwd_path.pop() + bwd_path.reverse() + path = fwd_path + bwd_path + return path + + +if __name__ == "__main__": + # all coordinates are given in format [y,x] + import doctest + + doctest.testmod() + init = (0, 0) + goal = (len(grid) - 1, len(grid[0]) - 1) + for elem in grid: + print(elem) + + start_bfs_time = time.time() + bfs = BreadthFirstSearch(init, goal) + path = bfs.search() + bfs_time = time.time() - start_bfs_time + + print("Unidirectional BFS computation time : ", bfs_time) + + start_bd_bfs_time = time.time() + bd_bfs = BidirectionalBreadthFirstSearch(init, goal) + bd_path = bd_bfs.search() + bd_bfs_time = time.time() - start_bd_bfs_time + + print("Bidirectional BFS computation time : ", bd_bfs_time) diff --git a/graphs/breadth_first_search.py b/graphs/breadth_first_search.py index 3992e2d4d892..ee9855bd0c2d 100644 --- a/graphs/breadth_first_search.py +++ b/graphs/breadth_first_search.py @@ -1,67 +1,91 @@ #!/usr/bin/python -# encoding=utf8 """ Author: OMKAR PATHAK """ -from __future__ import print_function +from typing import Set -class Graph(): - def __init__(self): - self.vertex = {} +class Graph: + def __init__(self) -> None: + self.vertices = {} - # for printing the Graph vertexes - def printGraph(self): - for i in self.vertex.keys(): - print(i,' -> ', ' -> '.join([str(j) for j in self.vertex[i]])) + def print_graph(self) -> None: + """ + prints adjacency list representation of graaph + >>> g = Graph() + >>> g.print_graph() + >>> g.add_edge(0, 1) + >>> g.print_graph() + 0 : 1 + """ + for i in self.vertices: + print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]])) - # for adding the edge beween two vertexes - def addEdge(self, fromVertex, toVertex): - # check if vertex is already present, - if fromVertex in self.vertex.keys(): - self.vertex[fromVertex].append(toVertex) + def add_edge(self, from_vertex: int, to_vertex: int) -> None: + """ + adding the edge between two vertices + >>> g = Graph() + >>> g.print_graph() + >>> g.add_edge(0, 1) + >>> g.print_graph() + 0 : 1 + """ + if from_vertex in self.vertices: + self.vertices[from_vertex].append(to_vertex) else: - # else make a new vertex - self.vertex[fromVertex] = [toVertex] + self.vertices[from_vertex] = [to_vertex] - def BFS(self, startVertex): - # Take a list for stoting already visited vertexes - visited = [False] * len(self.vertex) + def bfs(self, start_vertex: int) -> Set[int]: + """ + >>> g = Graph() + >>> g.add_edge(0, 1) + >>> g.add_edge(0, 1) + >>> g.add_edge(0, 2) + >>> g.add_edge(1, 2) + >>> g.add_edge(2, 0) + >>> g.add_edge(2, 3) + >>> g.add_edge(3, 3) + >>> sorted(g.bfs(2)) + [0, 1, 2, 3] + """ + # initialize set for storing already visited vertices + visited = set() - # create a list to store all the vertexes for BFS + # create a first in first out queue to store all the vertices for BFS queue = [] # mark the source node as visited and enqueue it - visited[startVertex] = True - queue.append(startVertex) + visited.add(start_vertex) + queue.append(start_vertex) while queue: - startVertex = queue.pop(0) - print(startVertex, end = ' ') + vertex = queue.pop(0) - # mark all adjacent nodes as visited and print them - for i in self.vertex[startVertex]: - if visited[i] == False: - queue.append(i) - visited[i] = True + # loop through all adjacent vertex and enqueue it if not yet visited + for adjacent_vertex in self.vertices[vertex]: + if adjacent_vertex not in visited: + queue.append(adjacent_vertex) + visited.add(adjacent_vertex) + return visited + + +if __name__ == "__main__": + from doctest import testmod + + testmod(verbose=True) -if __name__ == '__main__': g = Graph() - g.addEdge(0, 1) - g.addEdge(0, 2) - g.addEdge(1, 2) - g.addEdge(2, 0) - g.addEdge(2, 3) - g.addEdge(3, 3) - - g.printGraph() - print('BFS:') - g.BFS(2) - - # OUTPUT: - # 0  ->  1 -> 2 - # 1  ->  2 - # 2  ->  0 -> 3 - # 3  ->  3 - # BFS: - # 2 0 3 1 + g.add_edge(0, 1) + g.add_edge(0, 2) + g.add_edge(1, 2) + g.add_edge(2, 0) + g.add_edge(2, 3) + g.add_edge(3, 3) + + g.print_graph() + # 0 : 1 -> 2 + # 1 : 2 + # 2 : 0 -> 3 + # 3 : 3 + + assert sorted(g.bfs(2)) == [0, 1, 2, 3] diff --git a/graphs/breadth_first_search_2.py b/graphs/breadth_first_search_2.py new file mode 100644 index 000000000000..a90e963a4043 --- /dev/null +++ b/graphs/breadth_first_search_2.py @@ -0,0 +1,44 @@ +""" +https://en.wikipedia.org/wiki/Breadth-first_search +pseudo-code: +breadth_first_search(graph G, start vertex s): +// all nodes initially unexplored +mark s as explored +let Q = queue data structure, initialized with s +while Q is non-empty: + remove the first node of Q, call it v + for each edge(v, w): // for w in graph[v] + if w unexplored: + mark w as explored + add w to Q (at the end) +""" +from __future__ import annotations + +G = { + "A": ["B", "C"], + "B": ["A", "D", "E"], + "C": ["A", "F"], + "D": ["B"], + "E": ["B", "F"], + "F": ["C", "E"], +} + + +def breadth_first_search(graph: dict, start: str) -> set[str]: + """ + >>> ''.join(sorted(breadth_first_search(G, 'A'))) + 'ABCDEF' + """ + explored = {start} + queue = [start] + while queue: + v = queue.pop(0) # queue.popleft() + for w in graph[v]: + if w not in explored: + explored.add(w) + queue.append(w) + return explored + + +if __name__ == "__main__": + print(breadth_first_search(G, "A")) diff --git a/graphs/breadth_first_search_shortest_path.py b/graphs/breadth_first_search_shortest_path.py new file mode 100644 index 000000000000..b43479d4659c --- /dev/null +++ b/graphs/breadth_first_search_shortest_path.py @@ -0,0 +1,82 @@ +"""Breath First Search (BFS) can be used when finding the shortest path +from a given source node to a target node in an unweighted graph. +""" +from __future__ import annotations + +graph = { + "A": ["B", "C", "E"], + "B": ["A", "D", "E"], + "C": ["A", "F", "G"], + "D": ["B"], + "E": ["A", "B", "D"], + "F": ["C"], + "G": ["C"], +} + + +class Graph: + def __init__(self, graph: dict[str, str], source_vertex: str) -> None: + """Graph is implemented as dictionary of adjacency lists. Also, + Source vertex have to be defined upon initialization. + """ + self.graph = graph + # mapping node to its parent in resulting breadth first tree + self.parent = {} + self.source_vertex = source_vertex + + def breath_first_search(self) -> None: + """This function is a helper for running breath first search on this graph. + >>> g = Graph(graph, "G") + >>> g.breath_first_search() + >>> g.parent + {'G': None, 'C': 'G', 'A': 'C', 'F': 'C', 'B': 'A', 'E': 'A', 'D': 'B'} + """ + visited = {self.source_vertex} + self.parent[self.source_vertex] = None + queue = [self.source_vertex] # first in first out queue + + while queue: + vertex = queue.pop(0) + for adjacent_vertex in self.graph[vertex]: + if adjacent_vertex not in visited: + visited.add(adjacent_vertex) + self.parent[adjacent_vertex] = vertex + queue.append(adjacent_vertex) + + def shortest_path(self, target_vertex: str) -> str: + """This shortest path function returns a string, describing the result: + 1.) No path is found. The string is a human readable message to indicate this. + 2.) The shortest path is found. The string is in the form + `v1(->v2->v3->...->vn)`, where v1 is the source vertex and vn is the target + vertex, if it exists separately. + + >>> g = Graph(graph, "G") + >>> g.breath_first_search() + + Case 1 - No path is found. + >>> g.shortest_path("Foo") + 'No path from vertex:G to vertex:Foo' + + Case 2 - The path is found. + >>> g.shortest_path("D") + 'G->C->A->B->D' + >>> g.shortest_path("G") + 'G' + """ + if target_vertex == self.source_vertex: + return f"{self.source_vertex}" + elif not self.parent.get(target_vertex): + return f"No path from vertex:{self.source_vertex} to vertex:{target_vertex}" + else: + return self.shortest_path(self.parent[target_vertex]) + f"->{target_vertex}" + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + g = Graph(graph, "G") + g.breath_first_search() + print(g.shortest_path("D")) + print(g.shortest_path("G")) + print(g.shortest_path("Foo")) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 1b9c32c6ccc4..00b771649b5d 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -1,21 +1,22 @@ # Check whether Graph is Bipartite or Not using BFS + # A Bipartite Graph is a graph whose vertices can be divided into two independent sets, # U and V such that every edge (u, v) either connects a vertex from U to V or a vertex # from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, # or u belongs to V and v to U. We can also say that there is no edge that connects # vertices of same set. -def checkBipartite(l): +def checkBipartite(graph): queue = [] - visited = [False] * len(l) - color = [-1] * len(l) + visited = [False] * len(graph) + color = [-1] * len(graph) def bfs(): - while(queue): + while queue: u = queue.pop(0) visited[u] = True - for neighbour in l[u]: + for neighbour in graph[u]: if neighbour == u: return False @@ -29,15 +30,16 @@ def bfs(): return True - for i in range(len(l)): + for i in range(len(graph)): if not visited[i]: queue.append(i) color[i] = 0 - if bfs() == False: + if bfs() is False: return False return True -# Adjacency List of graph -l = {0:[1,3], 1:[0,2], 2:[1,3], 3:[0,2]} -print(checkBipartite(l)) + +if __name__ == "__main__": + # Adjacency List of graph + print(checkBipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]})) diff --git a/graphs/check_bipartite_graph_dfs.py b/graphs/check_bipartite_graph_dfs.py new file mode 100644 index 000000000000..fd644230449c --- /dev/null +++ b/graphs/check_bipartite_graph_dfs.py @@ -0,0 +1,34 @@ +# Check whether Graph is Bipartite or Not using DFS + + +# A Bipartite Graph is a graph whose vertices can be divided into two independent sets, +# U and V such that every edge (u, v) either connects a vertex from U to V or a vertex +# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, +# or u belongs to V and v to U. We can also say that there is no edge that connects +# vertices of same set. +def check_bipartite_dfs(graph): + visited = [False] * len(graph) + color = [-1] * len(graph) + + def dfs(v, c): + visited[v] = True + color[v] = c + for u in graph[v]: + if not visited[u]: + dfs(u, 1 - c) + + for i in range(len(graph)): + if not visited[i]: + dfs(i, 0) + + for i in range(len(graph)): + for j in graph[i]: + if color[i] == color[j]: + return False + + return True + + +# Adjacency list of graph +graph = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} +print(check_bipartite_dfs(graph)) diff --git a/graphs/connected_components.py b/graphs/connected_components.py new file mode 100644 index 000000000000..4af7803d74a7 --- /dev/null +++ b/graphs/connected_components.py @@ -0,0 +1,58 @@ +""" +https://en.wikipedia.org/wiki/Component_(graph_theory) + +Finding connected components in graph + +""" + +test_graph_1 = {0: [1, 2], 1: [0, 3], 2: [0], 3: [1], 4: [5, 6], 5: [4, 6], 6: [4, 5]} + +test_graph_2 = {0: [1, 2, 3], 1: [0, 3], 2: [0], 3: [0, 1], 4: [], 5: []} + + +def dfs(graph: dict, vert: int, visited: list) -> list: + """ + Use depth first search to find all vertices + being in the same component as initial vertex + >>> dfs(test_graph_1, 0, 5 * [False]) + [0, 1, 3, 2] + >>> dfs(test_graph_2, 0, 6 * [False]) + [0, 1, 3, 2] + """ + + visited[vert] = True + connected_verts = [] + + for neighbour in graph[vert]: + if not visited[neighbour]: + connected_verts += dfs(graph, neighbour, visited) + + return [vert] + connected_verts + + +def connected_components(graph: dict) -> list: + """ + This function takes graph as a parameter + and then returns the list of connected components + >>> connected_components(test_graph_1) + [[0, 1, 3, 2], [4, 5, 6]] + >>> connected_components(test_graph_2) + [[0, 1, 3, 2], [4], [5]] + """ + + graph_size = len(graph) + visited = graph_size * [False] + components_list = [] + + for i in range(graph_size): + if not visited[i]: + i_connected = dfs(graph, i, visited) + components_list.append(i_connected) + + return components_list + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/graphs/depth_first_search.py b/graphs/depth_first_search.py index 98faf61354f9..907cc172f253 100644 --- a/graphs/depth_first_search.py +++ b/graphs/depth_first_search.py @@ -1,66 +1,49 @@ -#!/usr/bin/python -# encoding=utf8 - -""" Author: OMKAR PATHAK """ -from __future__ import print_function - - -class Graph(): - def __init__(self): - self.vertex = {} - - # for printing the Graph vertexes - def printGraph(self): - print(self.vertex) - for i in self.vertex.keys(): - print(i,' -> ', ' -> '.join([str(j) for j in self.vertex[i]])) - - # for adding the edge beween two vertexes - def addEdge(self, fromVertex, toVertex): - # check if vertex is already present, - if fromVertex in self.vertex.keys(): - self.vertex[fromVertex].append(toVertex) - else: - # else make a new vertex - self.vertex[fromVertex] = [toVertex] - - def DFS(self): - # visited array for storing already visited nodes - visited = [False] * len(self.vertex) - - # call the recursive helper function - for i in range(len(self.vertex)): - if visited[i] == False: - self.DFSRec(i, visited) - - def DFSRec(self, startVertex, visited): - # mark start vertex as visited - visited[startVertex] = True - - print(startVertex, end = ' ') - - # Recur for all the vertexes that are adjacent to this node - for i in self.vertex.keys(): - if visited[i] == False: - self.DFSRec(i, visited) - -if __name__ == '__main__': - g = Graph() - g.addEdge(0, 1) - g.addEdge(0, 2) - g.addEdge(1, 2) - g.addEdge(2, 0) - g.addEdge(2, 3) - g.addEdge(3, 3) - - g.printGraph() - print('DFS:') - g.DFS() - - # OUTPUT: - # 0  ->  1 -> 2 - # 1  ->  2 - # 2  ->  0 -> 3 - # 3  ->  3 - # DFS: - # 0 1 2 3 +"""Non recursive implementation of a DFS algorithm.""" + +from __future__ import annotations + + +def depth_first_search(graph: dict, start: str) -> set[int]: + """Depth First Search on Graph + :param graph: directed graph in dictionary format + :param vertex: starting vertex as a string + :returns: the trace of the search + >>> G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"], + ... "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"], + ... "F": ["C", "E", "G"], "G": ["F"] } + >>> start = "A" + >>> output_G = list({'A', 'B', 'C', 'D', 'E', 'F', 'G'}) + >>> all(x in output_G for x in list(depth_first_search(G, "A"))) + True + >>> all(x in output_G for x in list(depth_first_search(G, "G"))) + True + """ + explored, stack = set(start), [start] + + while stack: + v = stack.pop() + explored.add(v) + # Differences from BFS: + # 1) pop last element instead of first one + # 2) add adjacent elements to stack without exploring them + for adj in reversed(graph[v]): + if adj not in explored: + stack.append(adj) + return explored + + +G = { + "A": ["B", "C", "D"], + "B": ["A", "D", "E"], + "C": ["A", "F"], + "D": ["B", "D"], + "E": ["B", "F"], + "F": ["C", "E", "G"], + "G": ["F"], +} + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print(depth_first_search(G, "A")) diff --git a/graphs/depth_first_search_2.py b/graphs/depth_first_search_2.py new file mode 100644 index 000000000000..3072d527c1c7 --- /dev/null +++ b/graphs/depth_first_search_2.py @@ -0,0 +1,65 @@ +#!/usr/bin/python + +""" Author: OMKAR PATHAK """ + + +class Graph: + def __init__(self): + self.vertex = {} + + # for printing the Graph vertices + def print_graph(self) -> None: + print(self.vertex) + for i in self.vertex: + print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]])) + + # for adding the edge between two vertices + def add_edge(self, from_vertex: int, to_vertex: int) -> None: + # check if vertex is already present, + if from_vertex in self.vertex: + self.vertex[from_vertex].append(to_vertex) + else: + # else make a new vertex + self.vertex[from_vertex] = [to_vertex] + + def dfs(self) -> None: + # visited array for storing already visited nodes + visited = [False] * len(self.vertex) + + # call the recursive helper function + for i in range(len(self.vertex)): + if not visited[i]: + self.dfs_recursive(i, visited) + + def dfs_recursive(self, start_vertex: int, visited: list) -> None: + # mark start vertex as visited + visited[start_vertex] = True + + print(start_vertex, end=" ") + + # Recur for all the vertices that are adjacent to this node + for i in self.vertex: + if not visited[i]: + self.dfs_recursive(i, visited) + + +if __name__ == "__main__": + g = Graph() + g.add_edge(0, 1) + g.add_edge(0, 2) + g.add_edge(1, 2) + g.add_edge(2, 0) + g.add_edge(2, 3) + g.add_edge(3, 3) + + g.print_graph() + print("DFS:") + g.dfs() + + # OUTPUT: + # 0 -> 1 -> 2 + # 1 -> 2 + # 2 -> 0 -> 3 + # 3 -> 3 + # DFS: + # 0 1 2 3 diff --git a/graphs/dijkstra.py b/graphs/dijkstra.py index 6b08b28fcfd3..d15fcbbfeef0 100644 --- a/graphs/dijkstra.py +++ b/graphs/dijkstra.py @@ -1,47 +1,118 @@ -"""pseudo-code""" - """ -DIJKSTRA(graph G, start vertex s,destination vertex d): -// all nodes initially unexplored -let H = min heap data structure, initialized with 0 and s [here 0 indicates the distance from start vertex] -while H is non-empty: - remove the first node and cost of H, call it U and cost - if U is not explored - mark U as explored - if U is d: - return cost // total cost from start to destination vertex - for each edge(U, V): c=cost of edge(u,V) // for V in graph[U] - if V unexplored: - next=cost+c - add next,V to H (at the end) +pseudo-code + +DIJKSTRA(graph G, start vertex s, destination vertex d): + +//all nodes initially unexplored + +1 - let H = min heap data structure, initialized with 0 and s [here 0 indicates + the distance from start vertex s] +2 - while H is non-empty: +3 - remove the first node and cost of H, call it U and cost +4 - if U has been previously explored: +5 - go to the while loop, line 2 //Once a node is explored there is no need + to make it again +6 - mark U as explored +7 - if U is d: +8 - return cost // total cost from start to destination vertex +9 - for each edge(U, V): c=cost of edge(U,V) // for V in graph[U] +10 - if V explored: +11 - go to next V in line 9 +12 - total_cost = cost + c +13 - add (total_cost,V) to H + +You can think at cost as a distance where Dijkstra finds the shortest distance +between vertices s and v in a graph G. The use of a min heap as H guarantees +that if a vertex has already been explored there will be no other path with +shortest distance, that happens because heapq.heappop will always return the +next vertex with the shortest distance, considering that the heap stores not +only the distance between previous vertex and current vertex but the entire +distance between each vertex that makes up the path from start vertex to target +vertex. """ import heapq def dijkstra(graph, start, end): + """Return the cost of the shortest path between vertices start and end. + + >>> dijkstra(G, "E", "C") + 6 + >>> dijkstra(G2, "E", "F") + 3 + >>> dijkstra(G3, "E", "F") + 3 + """ + heap = [(0, start)] # cost from start node,end node - visited = [] + visited = set() while heap: (cost, u) = heapq.heappop(heap) if u in visited: continue - visited.append(u) + visited.add(u) if u == end: return cost - for v, c in G[u]: + for v, c in graph[u]: if v in visited: continue next = cost + c heapq.heappush(heap, (next, v)) - return (-1, -1) + return -1 + + +G = { + "A": [["B", 2], ["C", 5]], + "B": [["A", 2], ["D", 3], ["E", 1], ["F", 1]], + "C": [["A", 5], ["F", 3]], + "D": [["B", 3]], + "E": [["B", 4], ["F", 3]], + "F": [["C", 3], ["E", 3]], +} + +r""" +Layout of G2: + +E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F + \ /\ + \ || + ----------------- 3 -------------------- +""" +G2 = { + "B": [["C", 1]], + "C": [["D", 1]], + "D": [["F", 1]], + "E": [["B", 1], ["F", 3]], + "F": [], +} + +r""" +Layout of G3: + +E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F + \ /\ + \ || + -------- 2 ---------> G ------- 1 ------ +""" +G3 = { + "B": [["C", 1]], + "C": [["D", 1]], + "D": [["F", 1]], + "E": [["B", 1], ["G", 2]], + "F": [], + "G": [["F", 1]], +} + +shortDistance = dijkstra(G, "E", "C") +print(shortDistance) # E -- 3 --> F -- 3 --> C == 6 + +shortDistance = dijkstra(G2, "E", "F") +print(shortDistance) # E -- 3 --> F == 3 +shortDistance = dijkstra(G3, "E", "F") +print(shortDistance) # E -- 2 --> G -- 1 --> F == 3 -G = {'A': [['B', 2], ['C', 5]], - 'B': [['A', 2], ['D', 3], ['E', 1]], - 'C': [['A', 5], ['F', 3]], - 'D': [['B', 3]], - 'E': [['B', 1], ['F', 3]], - 'F': [['C', 3], ['E', 3]]} +if __name__ == "__main__": + import doctest -shortDistance = dijkstra(G, 'E', 'C') -print(shortDistance) + doctest.testmod() diff --git a/graphs/dijkstra_2.py b/graphs/dijkstra_2.py index a6c340e8a68d..762884136e4a 100644 --- a/graphs/dijkstra_2.py +++ b/graphs/dijkstra_2.py @@ -1,57 +1,58 @@ -from __future__ import print_function - def printDist(dist, V): - print("\nVertex Distance") - for i in range(V): - if dist[i] != float('inf') : - print(i,"\t",int(dist[i]),end = "\t") - else: - print(i,"\t","INF",end="\t") - print() + print("\nVertex Distance") + for i in range(V): + if dist[i] != float("inf"): + print(i, "\t", int(dist[i]), end="\t") + else: + print(i, "\t", "INF", end="\t") + print() + def minDist(mdist, vset, V): - minVal = float('inf') - minInd = -1 - for i in range(V): - if (not vset[i]) and mdist[i] < minVal : - minInd = i - minVal = mdist[i] - return minInd + minVal = float("inf") + minInd = -1 + for i in range(V): + if (not vset[i]) and mdist[i] < minVal: + minInd = i + minVal = mdist[i] + return minInd + def Dijkstra(graph, V, src): - mdist=[float('inf') for i in range(V)] - vset = [False for i in range(V)] - mdist[src] = 0.0 - - for i in range(V-1): - u = minDist(mdist, vset, V) - vset[u] = True - - for v in range(V): - if (not vset[v]) and graph[u][v]!=float('inf') and mdist[u] + graph[u][v] < mdist[v]: - mdist[v] = mdist[u] + graph[u][v] - - - - printDist(mdist, V) - - - -#MAIN -V = int(input("Enter number of vertices: ")) -E = int(input("Enter number of edges: ")) - -graph = [[float('inf') for i in range(V)] for j in range(V)] - -for i in range(V): - graph[i][i] = 0.0 - -for i in range(E): - print("\nEdge ",i+1) - src = int(input("Enter source:")) - dst = int(input("Enter destination:")) - weight = float(input("Enter weight:")) - graph[src][dst] = weight - -gsrc = int(input("\nEnter shortest path source:")) -Dijkstra(graph, V, gsrc) + mdist = [float("inf") for i in range(V)] + vset = [False for i in range(V)] + mdist[src] = 0.0 + + for i in range(V - 1): + u = minDist(mdist, vset, V) + vset[u] = True + + for v in range(V): + if ( + (not vset[v]) + and graph[u][v] != float("inf") + and mdist[u] + graph[u][v] < mdist[v] + ): + mdist[v] = mdist[u] + graph[u][v] + + printDist(mdist, V) + + +if __name__ == "__main__": + V = int(input("Enter number of vertices: ").strip()) + E = int(input("Enter number of edges: ").strip()) + + graph = [[float("inf") for i in range(V)] for j in range(V)] + + for i in range(V): + graph[i][i] = 0.0 + + for i in range(E): + print("\nEdge ", i + 1) + src = int(input("Enter source:").strip()) + dst = int(input("Enter destination:").strip()) + weight = float(input("Enter weight:").strip()) + graph[src][dst] = weight + + gsrc = int(input("\nEnter shortest path source:").strip()) + Dijkstra(graph, V, gsrc) diff --git a/graphs/dijkstra_algorithm.py b/graphs/dijkstra_algorithm.py index 985c7f6c1301..6b64834acd81 100644 --- a/graphs/dijkstra_algorithm.py +++ b/graphs/dijkstra_algorithm.py @@ -2,10 +2,10 @@ # Author: Shubham Malik # References: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm -from __future__ import print_function import math import sys -# For storing the vertex set to retreive node with the lowest distance + +# For storing the vertex set to retrieve node with the lowest distance class PriorityQueue: @@ -13,7 +13,7 @@ class PriorityQueue: def __init__(self): self.cur_size = 0 self.array = [] - self.pos = {} # To store the pos of node in array + self.pos = {} # To store the pos of node in array def isEmpty(self): return self.cur_size == 0 @@ -79,8 +79,8 @@ def decrease_key(self, tup, new_d): class Graph: def __init__(self, num): - self.adjList = {} # To store graph: u -> (v,w) - self.num_nodes = num # Number of nodes in graph + self.adjList = {} # To store graph: u -> (v,w) + self.num_nodes = num # Number of nodes in graph # To store the distance from source vertex self.dist = [0] * self.num_nodes self.par = [-1] * self.num_nodes # To store the path @@ -103,8 +103,7 @@ def add_edge(self, u, v, w): def show_graph(self): # u -> v(w) for u in self.adjList: - print(u, '->', ' -> '.join(str("{}({})".format(v, w)) - for v, w in self.adjList[u])) + print(u, "->", " -> ".join(str(f"{v}({w})") for v, w in self.adjList[u])) def dijkstra(self, src): # Flush old junk values in par[] @@ -136,9 +135,9 @@ def dijkstra(self, src): self.show_distances(src) def show_distances(self, src): - print("Distance from node: {}".format(src)) + print(f"Distance from node: {src}") for u in range(self.num_nodes): - print('Node {} has distance: {}'.format(u, self.dist[u])) + print(f"Node {u} has distance: {self.dist[u]}") def show_path(self, src, dest): # To show the shortest path from src to dest @@ -158,16 +157,16 @@ def show_path(self, src, dest): path.append(src) path.reverse() - print('----Path to reach {} from {}----'.format(dest, src)) + print(f"----Path to reach {dest} from {src}----") for u in path: - print('{}'.format(u), end=' ') + print(f"{u}", end=" ") if u != dest: - print('-> ', end='') + print("-> ", end="") - print('\nTotal cost of path: ', cost) + print("\nTotal cost of path: ", cost) -if __name__ == '__main__': +if __name__ == "__main__": graph = Graph(9) graph.add_edge(0, 1, 4) graph.add_edge(0, 7, 8) diff --git a/graphs/dinic.py b/graphs/dinic.py new file mode 100644 index 000000000000..aaf3a119525c --- /dev/null +++ b/graphs/dinic.py @@ -0,0 +1,94 @@ +INF = float("inf") + + +class Dinic: + def __init__(self, n): + self.lvl = [0] * n + self.ptr = [0] * n + self.q = [0] * n + self.adj = [[] for _ in range(n)] + + """ + Here we will add our edges containing with the following parameters: + vertex closest to source, vertex closest to sink and flow capacity + through that edge ... + """ + + def add_edge(self, a, b, c, rcap=0): + self.adj[a].append([b, len(self.adj[b]), c, 0]) + self.adj[b].append([a, len(self.adj[a]) - 1, rcap, 0]) + + # This is a sample depth first search to be used at max_flow + def depth_first_search(self, vertex, sink, flow): + if vertex == sink or not flow: + return flow + + for i in range(self.ptr[vertex], len(self.adj[vertex])): + e = self.adj[vertex][i] + if self.lvl[e[0]] == self.lvl[vertex] + 1: + p = self.depth_first_search(e[0], sink, min(flow, e[2] - e[3])) + if p: + self.adj[vertex][i][3] += p + self.adj[e[0]][e[1]][3] -= p + return p + self.ptr[vertex] = self.ptr[vertex] + 1 + return 0 + + # Here we calculate the flow that reaches the sink + def max_flow(self, source, sink): + flow, self.q[0] = 0, source + for l in range(31): # noqa: E741 l = 30 maybe faster for random data + while True: + self.lvl, self.ptr = [0] * len(self.q), [0] * len(self.q) + qi, qe, self.lvl[source] = 0, 1, 1 + while qi < qe and not self.lvl[sink]: + v = self.q[qi] + qi += 1 + for e in self.adj[v]: + if not self.lvl[e[0]] and (e[2] - e[3]) >> (30 - l): + self.q[qe] = e[0] + qe += 1 + self.lvl[e[0]] = self.lvl[v] + 1 + + p = self.depth_first_search(source, sink, INF) + while p: + flow += p + p = self.depth_first_search(source, sink, INF) + + if not self.lvl[sink]: + break + + return flow + + +# Example to use + +""" +Will be a bipartite graph, than it has the vertices near the source(4) +and the vertices near the sink(4) +""" +# Here we make a graphs with 10 vertex(source and sink includes) +graph = Dinic(10) +source = 0 +sink = 9 +""" +Now we add the vertices next to the font in the font with 1 capacity in this edge +(source -> source vertices) +""" +for vertex in range(1, 5): + graph.add_edge(source, vertex, 1) +""" +We will do the same thing for the vertices near the sink, but from vertex to sink +(sink vertices -> sink) +""" +for vertex in range(5, 9): + graph.add_edge(vertex, sink, 1) +""" +Finally we add the verices near the sink to the vertices near the source. +(source vertices -> sink vertices) +""" +for vertex in range(1, 5): + graph.add_edge(vertex, vertex + 4, 1) + +# Now we can know that is the maximum flow(source -> sink) +print(graph.max_flow(source, sink)) diff --git a/graphs/directed_and_undirected_(weighted)_graph.py b/graphs/directed_and_undirected_(weighted)_graph.py new file mode 100644 index 000000000000..5cfa9e13edd9 --- /dev/null +++ b/graphs/directed_and_undirected_(weighted)_graph.py @@ -0,0 +1,496 @@ +from collections import deque +from math import floor +from random import random +from time import time + +# the default weight is 1 if not assigned but all the implementation is weighted + + +class DirectedGraph: + def __init__(self): + self.graph = {} + + # adding vertices and edges + # adding the weight is optional + # handles repetition + def add_pair(self, u, v, w=1): + if self.graph.get(u): + if self.graph[u].count([w, v]) == 0: + self.graph[u].append([w, v]) + else: + self.graph[u] = [[w, v]] + if not self.graph.get(v): + self.graph[v] = [] + + def all_nodes(self): + return list(self.graph) + + # handles if the input does not exist + def remove_pair(self, u, v): + if self.graph.get(u): + for _ in self.graph[u]: + if _[1] == v: + self.graph[u].remove(_) + + # if no destination is meant the default value is -1 + def dfs(self, s=-2, d=-1): + if s == d: + return [] + stack = [] + visited = [] + if s == -2: + s = list(self.graph)[0] + stack.append(s) + visited.append(s) + ss = s + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for node in self.graph[s]: + if visited.count(node[1]) < 1: + if node[1] == d: + visited.append(d) + return visited + else: + stack.append(node[1]) + visited.append(node[1]) + ss = node[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return visited + + # c is the count of nodes you want and if you leave it or pass -1 to the function + # the count will be random from 10 to 10000 + def fill_graph_randomly(self, c=-1): + if c == -1: + c = floor(random() * 10000) + 10 + for i in range(c): + # every vertex has max 100 edges + for _ in range(floor(random() * 102) + 1): + n = floor(random() * c) + 1 + if n != i: + self.add_pair(i, n, 1) + + def bfs(self, s=-2): + d = deque() + visited = [] + if s == -2: + s = list(self.graph)[0] + d.append(s) + visited.append(s) + while d: + s = d.popleft() + if len(self.graph[s]) != 0: + for node in self.graph[s]: + if visited.count(node[1]) < 1: + d.append(node[1]) + visited.append(node[1]) + return visited + + def in_degree(self, u): + count = 0 + for x in self.graph: + for y in self.graph[x]: + if y[1] == u: + count += 1 + return count + + def out_degree(self, u): + return len(self.graph[u]) + + def topological_sort(self, s=-2): + stack = [] + visited = [] + if s == -2: + s = list(self.graph)[0] + stack.append(s) + visited.append(s) + ss = s + sorted_nodes = [] + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for node in self.graph[s]: + if visited.count(node[1]) < 1: + stack.append(node[1]) + visited.append(node[1]) + ss = node[1] + break + + # check if all the children are visited + if s == ss: + sorted_nodes.append(stack.pop()) + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return sorted_nodes + + def cycle_nodes(self): + stack = [] + visited = [] + s = list(self.graph)[0] + stack.append(s) + visited.append(s) + parent = -2 + indirect_parents = [] + ss = s + on_the_way_back = False + anticipating_nodes = set() + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for node in self.graph[s]: + if ( + visited.count(node[1]) > 0 + and node[1] != parent + and indirect_parents.count(node[1]) > 0 + and not on_the_way_back + ): + len_stack = len(stack) - 1 + while True and len_stack >= 0: + if stack[len_stack] == node[1]: + anticipating_nodes.add(node[1]) + break + else: + anticipating_nodes.add(stack[len_stack]) + len_stack -= 1 + if visited.count(node[1]) < 1: + stack.append(node[1]) + visited.append(node[1]) + ss = node[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + on_the_way_back = True + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + on_the_way_back = False + indirect_parents.append(parent) + parent = s + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return list(anticipating_nodes) + + def has_cycle(self): + stack = [] + visited = [] + s = list(self.graph)[0] + stack.append(s) + visited.append(s) + parent = -2 + indirect_parents = [] + ss = s + on_the_way_back = False + anticipating_nodes = set() + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for node in self.graph[s]: + if ( + visited.count(node[1]) > 0 + and node[1] != parent + and indirect_parents.count(node[1]) > 0 + and not on_the_way_back + ): + len_stack_minus_one = len(stack) - 1 + while True and len_stack_minus_one >= 0: + if stack[len_stack_minus_one] == node[1]: + anticipating_nodes.add(node[1]) + break + else: + return True + # TODO:The following code is unreachable. + anticipating_nodes.add(stack[len_stack_minus_one]) + len_stack_minus_one -= 1 + if visited.count(node[1]) < 1: + stack.append(node[1]) + visited.append(node[1]) + ss = node[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + on_the_way_back = True + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + on_the_way_back = False + indirect_parents.append(parent) + parent = s + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return False + + def dfs_time(self, s=-2, e=-1): + begin = time() + self.dfs(s, e) + end = time() + return end - begin + + def bfs_time(self, s=-2): + begin = time() + self.bfs(s) + end = time() + return end - begin + + +class Graph: + def __init__(self): + self.graph = {} + + # adding vertices and edges + # adding the weight is optional + # handles repetition + def add_pair(self, u, v, w=1): + # check if the u exists + if self.graph.get(u): + # if there already is a edge + if self.graph[u].count([w, v]) == 0: + self.graph[u].append([w, v]) + else: + # if u does not exist + self.graph[u] = [[w, v]] + # add the other way + if self.graph.get(v): + # if there already is a edge + if self.graph[v].count([w, u]) == 0: + self.graph[v].append([w, u]) + else: + # if u does not exist + self.graph[v] = [[w, u]] + + # handles if the input does not exist + def remove_pair(self, u, v): + if self.graph.get(u): + for _ in self.graph[u]: + if _[1] == v: + self.graph[u].remove(_) + # the other way round + if self.graph.get(v): + for _ in self.graph[v]: + if _[1] == u: + self.graph[v].remove(_) + + # if no destination is meant the default value is -1 + def dfs(self, s=-2, d=-1): + if s == d: + return [] + stack = [] + visited = [] + if s == -2: + s = list(self.graph)[0] + stack.append(s) + visited.append(s) + ss = s + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for node in self.graph[s]: + if visited.count(node[1]) < 1: + if node[1] == d: + visited.append(d) + return visited + else: + stack.append(node[1]) + visited.append(node[1]) + ss = node[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return visited + + # c is the count of nodes you want and if you leave it or pass -1 to the function + # the count will be random from 10 to 10000 + def fill_graph_randomly(self, c=-1): + if c == -1: + c = floor(random() * 10000) + 10 + for i in range(c): + # every vertex has max 100 edges + for _ in range(floor(random() * 102) + 1): + n = floor(random() * c) + 1 + if n != i: + self.add_pair(i, n, 1) + + def bfs(self, s=-2): + d = deque() + visited = [] + if s == -2: + s = list(self.graph)[0] + d.append(s) + visited.append(s) + while d: + s = d.popleft() + if len(self.graph[s]) != 0: + for node in self.graph[s]: + if visited.count(node[1]) < 1: + d.append(node[1]) + visited.append(node[1]) + return visited + + def degree(self, u): + return len(self.graph[u]) + + def cycle_nodes(self): + stack = [] + visited = [] + s = list(self.graph)[0] + stack.append(s) + visited.append(s) + parent = -2 + indirect_parents = [] + ss = s + on_the_way_back = False + anticipating_nodes = set() + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for node in self.graph[s]: + if ( + visited.count(node[1]) > 0 + and node[1] != parent + and indirect_parents.count(node[1]) > 0 + and not on_the_way_back + ): + len_stack = len(stack) - 1 + while True and len_stack >= 0: + if stack[len_stack] == node[1]: + anticipating_nodes.add(node[1]) + break + else: + anticipating_nodes.add(stack[len_stack]) + len_stack -= 1 + if visited.count(node[1]) < 1: + stack.append(node[1]) + visited.append(node[1]) + ss = node[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + on_the_way_back = True + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + on_the_way_back = False + indirect_parents.append(parent) + parent = s + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return list(anticipating_nodes) + + def has_cycle(self): + stack = [] + visited = [] + s = list(self.graph)[0] + stack.append(s) + visited.append(s) + parent = -2 + indirect_parents = [] + ss = s + on_the_way_back = False + anticipating_nodes = set() + + while True: + # check if there is any non isolated nodes + if len(self.graph[s]) != 0: + ss = s + for node in self.graph[s]: + if ( + visited.count(node[1]) > 0 + and node[1] != parent + and indirect_parents.count(node[1]) > 0 + and not on_the_way_back + ): + len_stack_minus_one = len(stack) - 1 + while True and len_stack_minus_one >= 0: + if stack[len_stack_minus_one] == node[1]: + anticipating_nodes.add(node[1]) + break + else: + return True + # TODO: the following code is unreachable + # is this meant to be called in the else ? + anticipating_nodes.add(stack[len_stack_minus_one]) + len_stack_minus_one -= 1 + if visited.count(node[1]) < 1: + stack.append(node[1]) + visited.append(node[1]) + ss = node[1] + break + + # check if all the children are visited + if s == ss: + stack.pop() + on_the_way_back = True + if len(stack) != 0: + s = stack[len(stack) - 1] + else: + on_the_way_back = False + indirect_parents.append(parent) + parent = s + s = ss + + # check if se have reached the starting point + if len(stack) == 0: + return False + + def all_nodes(self): + return list(self.graph) + + def dfs_time(self, s=-2, e=-1): + begin = time() + self.dfs(s, e) + end = time() + return end - begin + + def bfs_time(self, s=-2): + begin = time() + self.bfs(s) + end = time() + return end - begin diff --git a/Graphs/edmonds_karp_Multiple_SourceAndSink.py b/graphs/edmonds_karp_multiple_source_and_sink.py similarity index 85% rename from Graphs/edmonds_karp_Multiple_SourceAndSink.py rename to graphs/edmonds_karp_multiple_source_and_sink.py index d231ac2c4cc3..0f359ff1aea3 100644 --- a/Graphs/edmonds_karp_Multiple_SourceAndSink.py +++ b/graphs/edmonds_karp_multiple_source_and_sink.py @@ -28,14 +28,13 @@ def _normalizeGraph(self, sources, sinks): for i in sources: maxInputFlow += sum(self.graph[i]) - size = len(self.graph) + 1 for room in self.graph: room.insert(0, 0) self.graph.insert(0, [0] * size) for i in sources: self.graph[0][i + 1] = maxInputFlow - self.sourceIndex = 0 + self.sourceIndex = 0 size = len(self.graph) + 1 for room in self.graph: @@ -45,7 +44,6 @@ def _normalizeGraph(self, sources, sinks): self.graph[i + 1][size - 1] = maxInputFlow self.sinkIndex = size - 1 - def findMaximumFlow(self): if self.maximumFlowAlgorithm is None: raise Exception("You need to set maximum flow algorithm before.") @@ -59,7 +57,7 @@ def setMaximumFlowAlgorithm(self, Algorithm): self.maximumFlowAlgorithm = Algorithm(self) -class FlowNetworkAlgorithmExecutor(object): +class FlowNetworkAlgorithmExecutor: def __init__(self, flowNetwork): self.flowNetwork = flowNetwork self.verticesCount = flowNetwork.verticesCount @@ -80,10 +78,9 @@ def _algorithm(self): pass - class MaximumFlowAlgorithmExecutor(FlowNetworkAlgorithmExecutor): def __init__(self, flowNetwork): - super(MaximumFlowAlgorithmExecutor, self).__init__(flowNetwork) + super().__init__(flowNetwork) # use this to save your result self.maximumFlow = -1 @@ -93,9 +90,10 @@ def getMaximumFlow(self): return self.maximumFlow + class PushRelabelExecutor(MaximumFlowAlgorithmExecutor): def __init__(self, flowNetwork): - super(PushRelabelExecutor, self).__init__(flowNetwork) + super().__init__(flowNetwork) self.preflow = [[0] * self.verticesCount for i in range(self.verticesCount)] @@ -112,8 +110,11 @@ def _algorithm(self): self.excesses[nextVertexIndex] += bandwidth # Relabel-to-front selection rule - verticesList = [i for i in range(self.verticesCount) - if i != self.sourceIndex and i != self.sinkIndex] + verticesList = [ + i + for i in range(self.verticesCount) + if i != self.sourceIndex and i != self.sinkIndex + ] # move through list i = 0 @@ -135,15 +136,21 @@ def processVertex(self, vertexIndex): while self.excesses[vertexIndex] > 0: for neighbourIndex in range(self.verticesCount): # if it's neighbour and current vertex is higher - if self.graph[vertexIndex][neighbourIndex] - self.preflow[vertexIndex][neighbourIndex] > 0\ - and self.heights[vertexIndex] > self.heights[neighbourIndex]: + if ( + self.graph[vertexIndex][neighbourIndex] + - self.preflow[vertexIndex][neighbourIndex] + > 0 + and self.heights[vertexIndex] > self.heights[neighbourIndex] + ): self.push(vertexIndex, neighbourIndex) self.relabel(vertexIndex) def push(self, fromIndex, toIndex): - preflowDelta = min(self.excesses[fromIndex], - self.graph[fromIndex][toIndex] - self.preflow[fromIndex][toIndex]) + preflowDelta = min( + self.excesses[fromIndex], + self.graph[fromIndex][toIndex] - self.preflow[fromIndex][toIndex], + ) self.preflow[fromIndex][toIndex] += preflowDelta self.preflow[toIndex][fromIndex] -= preflowDelta self.excesses[fromIndex] -= preflowDelta @@ -152,14 +159,18 @@ def push(self, fromIndex, toIndex): def relabel(self, vertexIndex): minHeight = None for toIndex in range(self.verticesCount): - if self.graph[vertexIndex][toIndex] - self.preflow[vertexIndex][toIndex] > 0: + if ( + self.graph[vertexIndex][toIndex] - self.preflow[vertexIndex][toIndex] + > 0 + ): if minHeight is None or self.heights[toIndex] < minHeight: minHeight = self.heights[toIndex] if minHeight is not None: self.heights[vertexIndex] = minHeight + 1 -if __name__ == '__main__': + +if __name__ == "__main__": entrances = [0] exits = [3] # graph = [ @@ -179,4 +190,4 @@ def relabel(self, vertexIndex): # and calculate maximumFlow = flowNetwork.findMaximumFlow() - print("maximum flow is {}".format(maximumFlow)) + print(f"maximum flow is {maximumFlow}") diff --git a/graphs/eulerian_path_and_circuit_for_undirected_graph.py b/graphs/eulerian_path_and_circuit_for_undirected_graph.py new file mode 100644 index 000000000000..7850933b0201 --- /dev/null +++ b/graphs/eulerian_path_and_circuit_for_undirected_graph.py @@ -0,0 +1,71 @@ +# Eulerian Path is a path in graph that visits every edge exactly once. +# Eulerian Circuit is an Eulerian Path which starts and ends on the same +# vertex. +# time complexity is O(V+E) +# space complexity is O(VE) + + +# using dfs for finding eulerian path traversal +def dfs(u, graph, visited_edge, path=[]): + path = path + [u] + for v in graph[u]: + if visited_edge[u][v] is False: + visited_edge[u][v], visited_edge[v][u] = True, True + path = dfs(v, graph, visited_edge, path) + return path + + +# for checking in graph has euler path or circuit +def check_circuit_or_path(graph, max_node): + odd_degree_nodes = 0 + odd_node = -1 + for i in range(max_node): + if i not in graph.keys(): + continue + if len(graph[i]) % 2 == 1: + odd_degree_nodes += 1 + odd_node = i + if odd_degree_nodes == 0: + return 1, odd_node + if odd_degree_nodes == 2: + return 2, odd_node + return 3, odd_node + + +def check_euler(graph, max_node): + visited_edge = [[False for _ in range(max_node + 1)] for _ in range(max_node + 1)] + check, odd_node = check_circuit_or_path(graph, max_node) + if check == 3: + print("graph is not Eulerian") + print("no path") + return + start_node = 1 + if check == 2: + start_node = odd_node + print("graph has a Euler path") + if check == 1: + print("graph has a Euler cycle") + path = dfs(start_node, graph, visited_edge) + print(path) + + +def main(): + G1 = {1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]} + G2 = {1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]} + G3 = {1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]} + G4 = {1: [2, 3], 2: [1, 3], 3: [1, 2]} + G5 = { + 1: [], + 2: [] + # all degree is zero + } + max_node = 10 + check_euler(G1, max_node) + check_euler(G2, max_node) + check_euler(G3, max_node) + check_euler(G4, max_node) + check_euler(G5, max_node) + + +if __name__ == "__main__": + main() diff --git a/graphs/even_tree.py b/graphs/even_tree.py index 9383ea9a13c1..c9aef6e7861f 100644 --- a/graphs/even_tree.py +++ b/graphs/even_tree.py @@ -12,7 +12,6 @@ Note: The tree input will be such that it can always be decomposed into components containing an even number of nodes. """ -from __future__ import print_function # pylint: disable=invalid-name from collections import defaultdict @@ -46,23 +45,13 @@ def even_tree(): dfs(1) -if __name__ == '__main__': +if __name__ == "__main__": n, m = 10, 9 tree = defaultdict(list) visited = {} cuts = [] count = 0 - edges = [ - (2, 1), - (3, 1), - (4, 3), - (5, 2), - (6, 1), - (7, 2), - (8, 6), - (9, 8), - (10, 8), - ] + edges = [(2, 1), (3, 1), (4, 3), (5, 2), (6, 1), (7, 2), (8, 6), (9, 8), (10, 8)] for u, v in edges: tree[u].append(v) tree[v].append(u) diff --git a/graphs/finding_bridges.py b/graphs/finding_bridges.py index 56533dd48bde..6555dd7bc29e 100644 --- a/graphs/finding_bridges.py +++ b/graphs/finding_bridges.py @@ -1,7 +1,7 @@ # Finding Bridges in Undirected Graph -def computeBridges(l): +def computeBridges(graph): id = 0 - n = len(l) # No of vertices in graph + n = len(graph) # No of vertices in graph low = [0] * n visited = [False] * n @@ -9,7 +9,7 @@ def dfs(at, parent, bridges, id): visited[at] = True low[at] = id id += 1 - for to in l[at]: + for to in graph[at]: if to == parent: pass elif not visited[to]: @@ -23,9 +23,20 @@ def dfs(at, parent, bridges, id): bridges = [] for i in range(n): - if (not visited[i]): + if not visited[i]: dfs(i, -1, bridges, id) print(bridges) - -l = {0:[1,2], 1:[0,2], 2:[0,1,3,5], 3:[2,4], 4:[3], 5:[2,6,8], 6:[5,7], 7:[6,8], 8:[5,7]} -computeBridges(l) + + +graph = { + 0: [1, 2], + 1: [0, 2], + 2: [0, 1, 3, 5], + 3: [2, 4], + 4: [3], + 5: [2, 6, 8], + 6: [5, 7], + 7: [6, 8], + 8: [5, 7], +} +computeBridges(graph) diff --git a/graphs/floyd_warshall.py b/graphs/floyd_warshall.py deleted file mode 100644 index fae8b19b351a..000000000000 --- a/graphs/floyd_warshall.py +++ /dev/null @@ -1,48 +0,0 @@ -from __future__ import print_function - -def printDist(dist, V): - print("\nThe shortest path matrix using Floyd Warshall algorithm\n") - for i in range(V): - for j in range(V): - if dist[i][j] != float('inf') : - print(int(dist[i][j]),end = "\t") - else: - print("INF",end="\t") - print() - - - -def FloydWarshall(graph, V): - dist=[[float('inf') for i in range(V)] for j in range(V)] - - for i in range(V): - for j in range(V): - dist[i][j] = graph[i][j] - - for k in range(V): - for i in range(V): - for j in range(V): - if dist[i][k]!=float('inf') and dist[k][j]!=float('inf') and dist[i][k]+dist[k][j] < dist[i][j]: - dist[i][j] = dist[i][k] + dist[k][j] - - printDist(dist, V) - - - -#MAIN -V = int(input("Enter number of vertices: ")) -E = int(input("Enter number of edges: ")) - -graph = [[float('inf') for i in range(V)] for j in range(V)] - -for i in range(V): - graph[i][i] = 0.0 - -for i in range(E): - print("\nEdge ",i+1) - src = int(input("Enter source:")) - dst = int(input("Enter destination:")) - weight = float(input("Enter weight:")) - graph[src][dst] = weight - -FloydWarshall(graph, V) diff --git a/graphs/frequent_pattern_graph_miner.py b/graphs/frequent_pattern_graph_miner.py new file mode 100644 index 000000000000..ff7063082267 --- /dev/null +++ b/graphs/frequent_pattern_graph_miner.py @@ -0,0 +1,232 @@ +""" +FP-GraphMiner - A Fast Frequent Pattern Mining Algorithm for Network Graphs + +A novel Frequent Pattern Graph Mining algorithm, FP-GraphMiner, that compactly +represents a set of network graphs as a Frequent Pattern Graph (or FP-Graph). +This graph can be used to efficiently mine frequent subgraphs including maximal +frequent subgraphs and maximum common subgraphs. + +URL: https://www.researchgate.net/publication/235255851 +""" +# fmt: off +edge_array = [ + ['ab-e1', 'ac-e3', 'ad-e5', 'bc-e4', 'bd-e2', 'be-e6', 'bh-e12', 'cd-e2', 'ce-e4', + 'de-e1', 'df-e8', 'dg-e5', 'dh-e10', 'ef-e3', 'eg-e2', 'fg-e6', 'gh-e6', 'hi-e3'], + ['ab-e1', 'ac-e3', 'ad-e5', 'bc-e4', 'bd-e2', 'be-e6', 'cd-e2', 'de-e1', 'df-e8', + 'ef-e3', 'eg-e2', 'fg-e6'], + ['ab-e1', 'ac-e3', 'bc-e4', 'bd-e2', 'de-e1', 'df-e8', 'dg-e5', 'ef-e3', 'eg-e2', + 'eh-e12', 'fg-e6', 'fh-e10', 'gh-e6'], + ['ab-e1', 'ac-e3', 'bc-e4', 'bd-e2', 'bh-e12', 'cd-e2', 'df-e8', 'dh-e10'], + ['ab-e1', 'ac-e3', 'ad-e5', 'bc-e4', 'bd-e2', 'cd-e2', 'ce-e4', 'de-e1', 'df-e8', + 'dg-e5', 'ef-e3', 'eg-e2', 'fg-e6'] +] +# fmt: on + + +def get_distinct_edge(edge_array): + """ + Return Distinct edges from edge array of multiple graphs + >>> sorted(get_distinct_edge(edge_array)) + ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] + """ + distinct_edge = set() + for row in edge_array: + for item in row: + distinct_edge.add(item[0]) + return list(distinct_edge) + + +def get_bitcode(edge_array, distinct_edge): + """ + Return bitcode of distinct_edge + """ + bitcode = ["0"] * len(edge_array) + for i, row in enumerate(edge_array): + for item in row: + if distinct_edge in item[0]: + bitcode[i] = "1" + break + return "".join(bitcode) + + +def get_frequency_table(edge_array): + """ + Returns Frequency Table + """ + distinct_edge = get_distinct_edge(edge_array) + frequency_table = dict() + + for item in distinct_edge: + bit = get_bitcode(edge_array, item) + # print('bit',bit) + # bt=''.join(bit) + s = bit.count("1") + frequency_table[item] = [s, bit] + # Store [Distinct edge, WT(Bitcode), Bitcode] in descending order + sorted_frequency_table = [ + [k, v[0], v[1]] + for k, v in sorted(frequency_table.items(), key=lambda v: v[1][0], reverse=True) + ] + return sorted_frequency_table + + +def get_nodes(frequency_table): + """ + Returns nodes + format nodes={bitcode:edges that represent the bitcode} + >>> get_nodes([['ab', 5, '11111'], ['ac', 5, '11111'], ['df', 5, '11111'], + ... ['bd', 5, '11111'], ['bc', 5, '11111']]) + {'11111': ['ab', 'ac', 'df', 'bd', 'bc']} + """ + nodes = {} + for i, item in enumerate(frequency_table): + nodes.setdefault(item[2], []).append(item[0]) + return nodes + + +def get_cluster(nodes): + """ + Returns cluster + format cluster:{WT(bitcode):nodes with same WT} + """ + cluster = {} + for key, value in nodes.items(): + cluster.setdefault(key.count("1"), {})[key] = value + return cluster + + +def get_support(cluster): + """ + Returns support + >>> get_support({5: {'11111': ['ab', 'ac', 'df', 'bd', 'bc']}, + ... 4: {'11101': ['ef', 'eg', 'de', 'fg'], '11011': ['cd']}, + ... 3: {'11001': ['ad'], '10101': ['dg']}, + ... 2: {'10010': ['dh', 'bh'], '11000': ['be'], '10100': ['gh'], + ... '10001': ['ce']}, + ... 1: {'00100': ['fh', 'eh'], '10000': ['hi']}}) + [100.0, 80.0, 60.0, 40.0, 20.0] + """ + return [i * 100 / len(cluster) for i in cluster] + + +def print_all() -> None: + print("\nNodes\n") + for key, value in nodes.items(): + print(key, value) + print("\nSupport\n") + print(support) + print("\n Cluster \n") + for key, value in sorted(cluster.items(), reverse=True): + print(key, value) + print("\n Graph\n") + for key, value in graph.items(): + print(key, value) + print("\n Edge List of Frequent subgraphs \n") + for edge_list in freq_subgraph_edge_list: + print(edge_list) + + +def create_edge(nodes, graph, cluster, c1): + """ + create edge between the nodes + """ + for i in cluster[c1].keys(): + count = 0 + c2 = c1 + 1 + while c2 < max(cluster.keys()): + for j in cluster[c2].keys(): + """ + creates edge only if the condition satisfies + """ + if int(i, 2) & int(j, 2) == int(i, 2): + if tuple(nodes[i]) in graph: + graph[tuple(nodes[i])].append(nodes[j]) + else: + graph[tuple(nodes[i])] = [nodes[j]] + count += 1 + if count == 0: + c2 = c2 + 1 + else: + break + + +def construct_graph(cluster, nodes): + X = cluster[max(cluster.keys())] + cluster[max(cluster.keys()) + 1] = "Header" + graph = {} + for i in X: + if tuple(["Header"]) in graph: + graph[tuple(["Header"])].append(X[i]) + else: + graph[tuple(["Header"])] = [X[i]] + for i in X: + graph[tuple(X[i])] = [["Header"]] + i = 1 + while i < max(cluster) - 1: + create_edge(nodes, graph, cluster, i) + i = i + 1 + return graph + + +def myDFS(graph, start, end, path=[]): + """ + find different DFS walk from given node to Header node + """ + path = path + [start] + if start == end: + paths.append(path) + for node in graph[start]: + if tuple(node) not in path: + myDFS(graph, tuple(node), end, path) + + +def find_freq_subgraph_given_support(s, cluster, graph): + """ + find edges of multiple frequent subgraphs + """ + k = int(s / 100 * (len(cluster) - 1)) + for i in cluster[k].keys(): + myDFS(graph, tuple(cluster[k][i]), tuple(["Header"])) + + +def freq_subgraphs_edge_list(paths): + """ + returns Edge list for frequent subgraphs + """ + freq_sub_EL = [] + for edges in paths: + EL = [] + for j in range(len(edges) - 1): + temp = list(edges[j]) + for e in temp: + edge = (e[0], e[1]) + EL.append(edge) + freq_sub_EL.append(EL) + return freq_sub_EL + + +def preprocess(edge_array): + """ + Preprocess the edge array + >>> preprocess([['ab-e1', 'ac-e3', 'ad-e5', 'bc-e4', 'bd-e2', 'be-e6', 'bh-e12', + ... 'cd-e2', 'ce-e4', 'de-e1', 'df-e8', 'dg-e5', 'dh-e10', 'ef-e3', + ... 'eg-e2', 'fg-e6', 'gh-e6', 'hi-e3']]) + + """ + for i in range(len(edge_array)): + for j in range(len(edge_array[i])): + t = edge_array[i][j].split("-") + edge_array[i][j] = t + + +if __name__ == "__main__": + preprocess(edge_array) + frequency_table = get_frequency_table(edge_array) + nodes = get_nodes(frequency_table) + cluster = get_cluster(nodes) + support = get_support(cluster) + graph = construct_graph(cluster, nodes) + find_freq_subgraph_given_support(60, cluster, graph) + paths = [] + freq_subgraph_edge_list = freq_subgraphs_edge_list(paths) + print_all() diff --git a/graphs/g_topological_sort.py b/graphs/g_topological_sort.py new file mode 100644 index 000000000000..77543d51f61d --- /dev/null +++ b/graphs/g_topological_sort.py @@ -0,0 +1,47 @@ +# Author: Phyllipe Bezerra (https://github.com/pmba) + +clothes = { + 0: "underwear", + 1: "pants", + 2: "belt", + 3: "suit", + 4: "shoe", + 5: "socks", + 6: "shirt", + 7: "tie", + 8: "watch", +} + +graph = [[1, 4], [2, 4], [3], [], [], [4], [2, 7], [3], []] + +visited = [0 for x in range(len(graph))] +stack = [] + + +def print_stack(stack, clothes): + order = 1 + while stack: + current_clothing = stack.pop() + print(order, clothes[current_clothing]) + order += 1 + + +def depth_first_search(u, visited, graph): + visited[u] = 1 + for v in graph[u]: + if not visited[v]: + depth_first_search(v, visited, graph) + + stack.append(u) + + +def topological_sort(graph, visited): + for v in range(len(graph)): + if not visited[v]: + depth_first_search(v, visited, graph) + + +if __name__ == "__main__": + topological_sort(graph, visited) + print(stack) + print_stack(stack, clothes) diff --git a/graphs/gale_shapley_bigraph.py b/graphs/gale_shapley_bigraph.py new file mode 100644 index 000000000000..59baf8296ea6 --- /dev/null +++ b/graphs/gale_shapley_bigraph.py @@ -0,0 +1,44 @@ +from __future__ import annotations + + +def stable_matching(donor_pref: list[int], recipient_pref: list[int]) -> list[int]: + """ + Finds the stable match in any bipartite graph, i.e a pairing where no 2 objects + prefer each other over their partner. The function accepts the preferences of + oegan donors and recipients (where both are assigned numbers from 0 to n-1) and + returns a list where the index position corresponds to the donor and value at the + index is the organ recipient. + + To better understand the algorithm, see also: + https://github.com/akashvshroff/Gale_Shapley_Stable_Matching (README). + https://www.youtube.com/watch?v=Qcv1IqHWAzg&t=13s (Numberphile YouTube). + + >>> donor_pref = [[0, 1, 3, 2], [0, 2, 3, 1], [1, 0, 2, 3], [0, 3, 1, 2]] + >>> recipient_pref = [[3, 1, 2, 0], [3, 1, 0, 2], [0, 3, 1, 2], [1, 0, 3, 2]] + >>> print(stable_matching(donor_pref, recipient_pref)) + [1, 2, 3, 0] + """ + assert len(donor_pref) == len(recipient_pref) + n = len(donor_pref) + unmatched_donors = list(range(n)) + donor_record = [-1] * n # who the donor has donated to + rec_record = [-1] * n # who the recipient has received from + num_donations = [0] * n + while unmatched_donors: + donor = unmatched_donors[0] + donor_preference = donor_pref[donor] + recipient = donor_preference[num_donations[donor]] + num_donations[donor] += 1 + rec_preference = recipient_pref[recipient] + prev_donor = rec_record[recipient] + if prev_donor != -1: + if rec_preference.index(prev_donor) > rec_preference.index(donor): + rec_record[recipient] = donor + donor_record[donor] = recipient + unmatched_donors.append(prev_donor) + unmatched_donors.remove(donor) + else: + rec_record[recipient] = donor + donor_record[donor] = recipient + unmatched_donors.remove(donor) + return donor_record diff --git a/graphs/graph.py b/graphs/graph.py deleted file mode 100644 index 9bd61559dcbf..000000000000 --- a/graphs/graph.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/python -# encoding=utf8 - -from __future__ import print_function -# Author: OMKAR PATHAK - -# We can use Python's dictionary for constructing the graph - -class AdjacencyList(object): - def __init__(self): - self.List = {} - - def addEdge(self, fromVertex, toVertex): - # check if vertex is already present - if fromVertex in self.List.keys(): - self.List[fromVertex].append(toVertex) - else: - self.List[fromVertex] = [toVertex] - - def printList(self): - for i in self.List: - print((i,'->',' -> '.join([str(j) for j in self.List[i]]))) - -if __name__ == '__main__': - al = AdjacencyList() - al.addEdge(0, 1) - al.addEdge(0, 4) - al.addEdge(4, 1) - al.addEdge(4, 3) - al.addEdge(1, 0) - al.addEdge(1, 4) - al.addEdge(1, 3) - al.addEdge(1, 2) - al.addEdge(2, 3) - al.addEdge(3, 4) - - al.printList() - - # OUTPUT: - # 0 -> 1 -> 4 - # 1 -> 0 -> 4 -> 3 -> 2 - # 2 -> 3 - # 3 -> 4 - # 4 -> 1 -> 3 diff --git a/graphs/graph_list.py b/graphs/graph_list.py index d67bc96c4a81..a812fecd961e 100644 --- a/graphs/graph_list.py +++ b/graphs/graph_list.py @@ -1,31 +1,44 @@ -from __future__ import print_function - - -class Graph: - def __init__(self, vertex): - self.vertex = vertex - self.graph = [[0] for i in range(vertex)] - - def add_edge(self, u, v): - self.graph[u - 1].append(v - 1) - - def show(self): - for i in range(self.vertex): - print('%d: '% (i + 1), end=' ') - for j in self.graph[i]: - print('%d-> '% (j + 1), end=' ') - print(' ') - - - -g = Graph(100) - -g.add_edge(1,3) -g.add_edge(2,3) -g.add_edge(3,4) -g.add_edge(3,5) -g.add_edge(4,5) - - -g.show() - +#!/usr/bin/python + +# Author: OMKAR PATHAK + +# We can use Python's dictionary for constructing the graph. + + +class AdjacencyList: + def __init__(self): + self.adj_list = {} + + def add_edge(self, from_vertex: int, to_vertex: int) -> None: + # check if vertex is already present + if from_vertex in self.adj_list: + self.adj_list[from_vertex].append(to_vertex) + else: + self.adj_list[from_vertex] = [to_vertex] + + def print_list(self) -> None: + for i in self.adj_list: + print((i, "->", " -> ".join([str(j) for j in self.adj_list[i]]))) + + +if __name__ == "__main__": + al = AdjacencyList() + al.add_edge(0, 1) + al.add_edge(0, 4) + al.add_edge(4, 1) + al.add_edge(4, 3) + al.add_edge(1, 0) + al.add_edge(1, 4) + al.add_edge(1, 3) + al.add_edge(1, 2) + al.add_edge(2, 3) + al.add_edge(3, 4) + + al.print_list() + + # OUTPUT: + # 0 -> 1 -> 4 + # 1 -> 0 -> 4 -> 3 -> 2 + # 2 -> 3 + # 3 -> 4 + # 4 -> 1 -> 3 diff --git a/graphs/graph_matrix.py b/graphs/graph_matrix.py index de25301d6dd1..987168426ba5 100644 --- a/graphs/graph_matrix.py +++ b/graphs/graph_matrix.py @@ -1,11 +1,7 @@ -from __future__ import print_function - - class Graph: - def __init__(self, vertex): self.vertex = vertex - self.graph = [[0] * vertex for i in range(vertex) ] + self.graph = [[0] * vertex for i in range(vertex)] def add_edge(self, u, v): self.graph[u - 1][v - 1] = 1 @@ -15,18 +11,15 @@ def show(self): for i in self.graph: for j in i: - print(j, end=' ') - print(' ') - - + print(j, end=" ") + print(" ") g = Graph(100) -g.add_edge(1,4) -g.add_edge(4,2) -g.add_edge(4,5) -g.add_edge(2,5) -g.add_edge(5,3) +g.add_edge(1, 4) +g.add_edge(4, 2) +g.add_edge(4, 5) +g.add_edge(2, 5) +g.add_edge(5, 3) g.show() - diff --git a/graphs/graphs_floyd_warshall.py b/graphs/graphs_floyd_warshall.py new file mode 100644 index 000000000000..56cf8b9e382b --- /dev/null +++ b/graphs/graphs_floyd_warshall.py @@ -0,0 +1,102 @@ +# floyd_warshall.py +""" + The problem is to find the shortest distance between all pairs of vertices in a + weighted directed graph that can have negative edge weights. +""" + + +def _print_dist(dist, v): + print("\nThe shortest path matrix using Floyd Warshall algorithm\n") + for i in range(v): + for j in range(v): + if dist[i][j] != float("inf"): + print(int(dist[i][j]), end="\t") + else: + print("INF", end="\t") + print() + + +def floyd_warshall(graph, v): + """ + :param graph: 2D array calculated from weight[edge[i, j]] + :type graph: List[List[float]] + :param v: number of vertices + :type v: int + :return: shortest distance between all vertex pairs + distance[u][v] will contain the shortest distance from vertex u to v. + + 1. For all edges from v to n, distance[i][j] = weight(edge(i, j)). + 3. The algorithm then performs distance[i][j] = min(distance[i][j], distance[i][k] + + distance[k][j]) for each possible pair i, j of vertices. + 4. The above is repeated for each vertex k in the graph. + 5. Whenever distance[i][j] is given a new minimum value, next vertex[i][j] is + updated to the next vertex[i][k]. + """ + + dist = [[float("inf") for _ in range(v)] for _ in range(v)] + + for i in range(v): + for j in range(v): + dist[i][j] = graph[i][j] + + # check vertex k against all other vertices (i, j) + for k in range(v): + # looping through rows of graph array + for i in range(v): + # looping through columns of graph array + for j in range(v): + if ( + dist[i][k] != float("inf") + and dist[k][j] != float("inf") + and dist[i][k] + dist[k][j] < dist[i][j] + ): + dist[i][j] = dist[i][k] + dist[k][j] + + _print_dist(dist, v) + return dist, v + + +if __name__ == "__main__": + v = int(input("Enter number of vertices: ")) + e = int(input("Enter number of edges: ")) + + graph = [[float("inf") for i in range(v)] for j in range(v)] + + for i in range(v): + graph[i][i] = 0.0 + + # src and dst are indices that must be within the array size graph[e][v] + # failure to follow this will result in an error + for i in range(e): + print("\nEdge ", i + 1) + src = int(input("Enter source:")) + dst = int(input("Enter destination:")) + weight = float(input("Enter weight:")) + graph[src][dst] = weight + + floyd_warshall(graph, v) + + # Example Input + # Enter number of vertices: 3 + # Enter number of edges: 2 + + # # generated graph from vertex and edge inputs + # [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]] + # [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]] + + # specify source, destination and weight for edge #1 + # Edge 1 + # Enter source:1 + # Enter destination:2 + # Enter weight:2 + + # specify source, destination and weight for edge #2 + # Edge 2 + # Enter source:2 + # Enter destination:1 + # Enter weight:1 + + # # Expected Output from the vertice, edge and src, dst, weight inputs!! + # 0 INF INF + # INF 0 2 + # INF 1 0 diff --git a/graphs/greedy_best_first.py b/graphs/greedy_best_first.py new file mode 100644 index 000000000000..4b80a6853d3f --- /dev/null +++ b/graphs/greedy_best_first.py @@ -0,0 +1,174 @@ +""" +https://en.wikipedia.org/wiki/Best-first_search#Greedy_BFS +""" + +from __future__ import annotations + +grid = [ + [0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0], + [1, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0], +] + +delta = ([-1, 0], [0, -1], [1, 0], [0, 1]) # up, left, down, right + + +class Node: + """ + >>> k = Node(0, 0, 4, 5, 0, None) + >>> k.calculate_heuristic() + 9 + >>> n = Node(1, 4, 3, 4, 2, None) + >>> n.calculate_heuristic() + 2 + >>> l = [k, n] + >>> n == l[0] + False + >>> l.sort() + >>> n == l[0] + True + """ + + def __init__(self, pos_x, pos_y, goal_x, goal_y, g_cost, parent): + self.pos_x = pos_x + self.pos_y = pos_y + self.pos = (pos_y, pos_x) + self.goal_x = goal_x + self.goal_y = goal_y + self.g_cost = g_cost + self.parent = parent + self.f_cost = self.calculate_heuristic() + + def calculate_heuristic(self) -> float: + """ + The heuristic here is the Manhattan Distance + Could elaborate to offer more than one choice + """ + dy = abs(self.pos_x - self.goal_x) + dx = abs(self.pos_y - self.goal_y) + return dx + dy + + def __lt__(self, other) -> bool: + return self.f_cost < other.f_cost + + +class GreedyBestFirst: + """ + >>> gbf = GreedyBestFirst((0, 0), (len(grid) - 1, len(grid[0]) - 1)) + >>> [x.pos for x in gbf.get_successors(gbf.start)] + [(1, 0), (0, 1)] + >>> (gbf.start.pos_y + delta[3][0], gbf.start.pos_x + delta[3][1]) + (0, 1) + >>> (gbf.start.pos_y + delta[2][0], gbf.start.pos_x + delta[2][1]) + (1, 0) + >>> gbf.retrace_path(gbf.start) + [(0, 0)] + >>> gbf.search() # doctest: +NORMALIZE_WHITESPACE + [(0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (4, 1), (5, 1), (6, 1), + (6, 2), (6, 3), (5, 3), (5, 4), (5, 5), (6, 5), (6, 6)] + """ + + def __init__(self, start, goal): + self.start = Node(start[1], start[0], goal[1], goal[0], 0, None) + self.target = Node(goal[1], goal[0], goal[1], goal[0], 99999, None) + + self.open_nodes = [self.start] + self.closed_nodes = [] + + self.reached = False + + def search(self) -> list[tuple[int]]: + """ + Search for the path, + if a path is not found, only the starting position is returned + """ + while self.open_nodes: + # Open Nodes are sorted using __lt__ + self.open_nodes.sort() + current_node = self.open_nodes.pop(0) + + if current_node.pos == self.target.pos: + self.reached = True + return self.retrace_path(current_node) + + self.closed_nodes.append(current_node) + successors = self.get_successors(current_node) + + for child_node in successors: + if child_node in self.closed_nodes: + continue + + if child_node not in self.open_nodes: + self.open_nodes.append(child_node) + else: + # retrieve the best current path + better_node = self.open_nodes.pop(self.open_nodes.index(child_node)) + + if child_node.g_cost < better_node.g_cost: + self.open_nodes.append(child_node) + else: + self.open_nodes.append(better_node) + + if not (self.reached): + return [self.start.pos] + + def get_successors(self, parent: Node) -> list[Node]: + """ + Returns a list of successors (both in the grid and free spaces) + """ + successors = [] + for action in delta: + pos_x = parent.pos_x + action[1] + pos_y = parent.pos_y + action[0] + + if not (0 <= pos_x <= len(grid[0]) - 1 and 0 <= pos_y <= len(grid) - 1): + continue + + if grid[pos_y][pos_x] != 0: + continue + + successors.append( + Node( + pos_x, + pos_y, + self.target.pos_y, + self.target.pos_x, + parent.g_cost + 1, + parent, + ) + ) + return successors + + def retrace_path(self, node: Node) -> list[tuple[int]]: + """ + Retrace the path from parents to parents until start node + """ + current_node = node + path = [] + while current_node is not None: + path.append((current_node.pos_y, current_node.pos_x)) + current_node = current_node.parent + path.reverse() + return path + + +if __name__ == "__main__": + init = (0, 0) + goal = (len(grid) - 1, len(grid[0]) - 1) + for elem in grid: + print(elem) + + print("------") + + greedy_bf = GreedyBestFirst(init, goal) + path = greedy_bf.search() + + for elem in path: + grid[elem[0]][elem[1]] = 2 + + for elem in grid: + print(elem) diff --git a/graphs/kahns_algorithm_long.py b/graphs/kahns_algorithm_long.py index 453b5706f6da..fed7517a21e2 100644 --- a/graphs/kahns_algorithm_long.py +++ b/graphs/kahns_algorithm_long.py @@ -1,10 +1,10 @@ # Finding longest distance in Directed Acyclic Graph using KahnsAlgorithm -def longestDistance(l): - indegree = [0] * len(l) +def longestDistance(graph): + indegree = [0] * len(graph) queue = [] - longDist = [1] * len(l) + longDist = [1] * len(graph) - for key, values in l.items(): + for key, values in graph.items(): for i in values: indegree[i] += 1 @@ -12,19 +12,20 @@ def longestDistance(l): if indegree[i] == 0: queue.append(i) - while(queue): + while queue: vertex = queue.pop(0) - for x in l[vertex]: + for x in graph[vertex]: indegree[x] -= 1 if longDist[vertex] + 1 > longDist[x]: - longDist[x] = longDist[vertex] + 1 + longDist[x] = longDist[vertex] + 1 if indegree[x] == 0: queue.append(x) print(max(longDist)) + # Adjacency list of Graph -l = {0:[2,3,4], 1:[2,7], 2:[5], 3:[5,7], 4:[7], 5:[6], 6:[7], 7:[]} -longestDistance(l) +graph = {0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []} +longestDistance(graph) diff --git a/graphs/kahns_algorithm_topo.py b/graphs/kahns_algorithm_topo.py index 8c182c4e902c..bf9f90299361 100644 --- a/graphs/kahns_algorithm_topo.py +++ b/graphs/kahns_algorithm_topo.py @@ -1,11 +1,14 @@ -# Kahn's Algorithm is used to find Topological ordering of Directed Acyclic Graph using BFS -def topologicalSort(l): - indegree = [0] * len(l) +def topologicalSort(graph): + """ + Kahn's Algorithm is used to find Topological ordering of Directed Acyclic Graph + using BFS + """ + indegree = [0] * len(graph) queue = [] topo = [] cnt = 0 - for key, values in l.items(): + for key, values in graph.items(): for i in values: indegree[i] += 1 @@ -13,20 +16,21 @@ def topologicalSort(l): if indegree[i] == 0: queue.append(i) - while(queue): + while queue: vertex = queue.pop(0) cnt += 1 topo.append(vertex) - for x in l[vertex]: + for x in graph[vertex]: indegree[x] -= 1 if indegree[x] == 0: queue.append(x) - if cnt != len(l): + if cnt != len(graph): print("Cycle exists") else: print(topo) + # Adjacency List of Graph -l = {0:[1,2], 1:[3], 2:[3], 3:[4,5], 4:[], 5:[]} -topologicalSort(l) +graph = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} +topologicalSort(graph) diff --git a/graphs/karger.py b/graphs/karger.py new file mode 100644 index 000000000000..f72128c8178a --- /dev/null +++ b/graphs/karger.py @@ -0,0 +1,86 @@ +""" +An implementation of Karger's Algorithm for partitioning a graph. +""" + +from __future__ import annotations + +import random + +# Adjacency list representation of this graph: +# https://en.wikipedia.org/wiki/File:Single_run_of_Karger%E2%80%99s_Mincut_algorithm.svg +TEST_GRAPH = { + "1": ["2", "3", "4", "5"], + "2": ["1", "3", "4", "5"], + "3": ["1", "2", "4", "5", "10"], + "4": ["1", "2", "3", "5", "6"], + "5": ["1", "2", "3", "4", "7"], + "6": ["7", "8", "9", "10", "4"], + "7": ["6", "8", "9", "10", "5"], + "8": ["6", "7", "9", "10"], + "9": ["6", "7", "8", "10"], + "10": ["6", "7", "8", "9", "3"], +} + + +def partition_graph(graph: dict[str, list[str]]) -> set[tuple[str, str]]: + """ + Partitions a graph using Karger's Algorithm. Implemented from + pseudocode found here: + https://en.wikipedia.org/wiki/Karger%27s_algorithm. + This function involves random choices, meaning it will not give + consistent outputs. + + Args: + graph: A dictionary containing adacency lists for the graph. + Nodes must be strings. + + Returns: + The cutset of the cut found by Karger's Algorithm. + + >>> graph = {'0':['1'], '1':['0']} + >>> partition_graph(graph) + {('0', '1')} + """ + # Dict that maps contracted nodes to a list of all the nodes it "contains." + contracted_nodes = {node: {node} for node in graph} + + graph_copy = {node: graph[node][:] for node in graph} + + while len(graph_copy) > 2: + + # Choose a random edge. + u = random.choice(list(graph_copy.keys())) + v = random.choice(graph_copy[u]) + + # Contract edge (u, v) to new node uv + uv = u + v + uv_neighbors = list(set(graph_copy[u] + graph_copy[v])) + uv_neighbors.remove(u) + uv_neighbors.remove(v) + graph_copy[uv] = uv_neighbors + for neighbor in uv_neighbors: + graph_copy[neighbor].append(uv) + + contracted_nodes[uv] = set(contracted_nodes[u].union(contracted_nodes[v])) + + # Remove nodes u and v. + del graph_copy[u] + del graph_copy[v] + for neighbor in uv_neighbors: + if u in graph_copy[neighbor]: + graph_copy[neighbor].remove(u) + if v in graph_copy[neighbor]: + graph_copy[neighbor].remove(v) + + # Find cutset. + groups = [contracted_nodes[node] for node in graph_copy] + return { + (node, neighbor) + for node in groups[0] + for neighbor in graph[node] + if neighbor in groups[1] + } + + +if __name__ == "__main__": + print(partition_graph(TEST_GRAPH)) diff --git a/graphs/minimum_spanning_tree_boruvka.py b/graphs/minimum_spanning_tree_boruvka.py new file mode 100644 index 000000000000..32548b2ecb6c --- /dev/null +++ b/graphs/minimum_spanning_tree_boruvka.py @@ -0,0 +1,196 @@ +class Graph: + """ + Data structure to store graphs (based on adjacency lists) + """ + + def __init__(self): + + self.num_vertices = 0 + self.num_edges = 0 + self.adjacency = {} + + def add_vertex(self, vertex): + """ + Adds a vertex to the graph + + """ + if vertex not in self.adjacency: + self.adjacency[vertex] = {} + self.num_vertices += 1 + + def add_edge(self, head, tail, weight): + """ + Adds an edge to the graph + + """ + + self.add_vertex(head) + self.add_vertex(tail) + + if head == tail: + return + + self.adjacency[head][tail] = weight + self.adjacency[tail][head] = weight + + def distinct_weight(self): + """ + For Boruvks's algorithm the weights should be distinct + Converts the weights to be distinct + + """ + edges = self.get_edges() + for edge in edges: + head, tail, weight = edge + edges.remove((tail, head, weight)) + for i in range(len(edges)): + edges[i] = list(edges[i]) + + edges.sort(key=lambda e: e[2]) + for i in range(len(edges) - 1): + if edges[i][2] >= edges[i + 1][2]: + edges[i + 1][2] = edges[i][2] + 1 + for edge in edges: + head, tail, weight = edge + self.adjacency[head][tail] = weight + self.adjacency[tail][head] = weight + + def __str__(self): + """ + Returns string representation of the graph + """ + string = "" + for tail in self.adjacency: + for head in self.adjacency[tail]: + weight = self.adjacency[head][tail] + string += "%d -> %d == %d\n" % (head, tail, weight) + return string.rstrip("\n") + + def get_edges(self): + """ + Returna all edges in the graph + """ + output = [] + for tail in self.adjacency: + for head in self.adjacency[tail]: + output.append((tail, head, self.adjacency[head][tail])) + return output + + def get_vertices(self): + """ + Returns all vertices in the graph + """ + return self.adjacency.keys() + + @staticmethod + def build(vertices=None, edges=None): + """ + Builds a graph from the given set of vertices and edges + + """ + g = Graph() + if vertices is None: + vertices = [] + if edges is None: + edge = [] + for vertex in vertices: + g.add_vertex(vertex) + for edge in edges: + g.add_edge(*edge) + return g + + class UnionFind: + """ + Disjoint set Union and Find for Boruvka's algorithm + """ + + def __init__(self): + self.parent = {} + self.rank = {} + + def __len__(self): + return len(self.parent) + + def make_set(self, item): + if item in self.parent: + return self.find(item) + + self.parent[item] = item + self.rank[item] = 0 + return item + + def find(self, item): + if item not in self.parent: + return self.make_set(item) + if item != self.parent[item]: + self.parent[item] = self.find(self.parent[item]) + return self.parent[item] + + def union(self, item1, item2): + root1 = self.find(item1) + root2 = self.find(item2) + + if root1 == root2: + return root1 + + if self.rank[root1] > self.rank[root2]: + self.parent[root2] = root1 + return root1 + + if self.rank[root1] < self.rank[root2]: + self.parent[root1] = root2 + return root2 + + if self.rank[root1] == self.rank[root2]: + self.rank[root1] += 1 + self.parent[root2] = root1 + return root1 + + @staticmethod + def boruvka_mst(graph): + """ + Implementation of Boruvka's algorithm + >>> g = Graph() + >>> g = Graph.build([0, 1, 2, 3], [[0, 1, 1], [0, 2, 1],[2, 3, 1]]) + >>> g.distinct_weight() + >>> bg = Graph.boruvka_mst(g) + >>> print(bg) + 1 -> 0 == 1 + 2 -> 0 == 2 + 0 -> 1 == 1 + 0 -> 2 == 2 + 3 -> 2 == 3 + 2 -> 3 == 3 + """ + num_components = graph.num_vertices + + union_find = Graph.UnionFind() + mst_edges = [] + while num_components > 1: + cheap_edge = {} + for vertex in graph.get_vertices(): + cheap_edge[vertex] = -1 + + edges = graph.get_edges() + for edge in edges: + head, tail, weight = edge + edges.remove((tail, head, weight)) + for edge in edges: + head, tail, weight = edge + set1 = union_find.find(head) + set2 = union_find.find(tail) + if set1 != set2: + if cheap_edge[set1] == -1 or cheap_edge[set1][2] > weight: + cheap_edge[set1] = [head, tail, weight] + + if cheap_edge[set2] == -1 or cheap_edge[set2][2] > weight: + cheap_edge[set2] = [head, tail, weight] + for vertex in cheap_edge: + if cheap_edge[vertex] != -1: + head, tail, weight = cheap_edge[vertex] + if union_find.find(head) != union_find.find(tail): + union_find.union(head, tail) + mst_edges.append(cheap_edge[vertex]) + num_components = num_components - 1 + mst = Graph.build(edges=mst_edges) + return mst diff --git a/graphs/minimum_spanning_tree_kruskal.py b/graphs/minimum_spanning_tree_kruskal.py index 81d64f421a31..a51f970341f7 100644 --- a/graphs/minimum_spanning_tree_kruskal.py +++ b/graphs/minimum_spanning_tree_kruskal.py @@ -1,32 +1,47 @@ -from __future__ import print_function -num_nodes, num_edges = list(map(int,input().split())) +from typing import List, Tuple -edges = [] -for i in range(num_edges): - node1, node2, cost = list(map(int,input().split())) - edges.append((i,node1,node2,cost)) +def kruskal(num_nodes: int, num_edges: int, edges: List[Tuple[int, int, int]]) -> int: + """ + >>> kruskal(4, 3, [(0, 1, 3), (1, 2, 5), (2, 3, 1)]) + [(2, 3, 1), (0, 1, 3), (1, 2, 5)] -edges = sorted(edges, key=lambda edge: edge[3]) + >>> kruskal(4, 5, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2)]) + [(2, 3, 1), (0, 2, 1), (0, 1, 3)] -parent = [i for i in range(num_nodes)] + >>> kruskal(4, 6, [(0, 1, 3), (1, 2, 5), (2, 3, 1), (0, 2, 1), (0, 3, 2), + ... (2, 1, 1)]) + [(2, 3, 1), (0, 2, 1), (2, 1, 1)] + """ + edges = sorted(edges, key=lambda edge: edge[2]) -def find_parent(i): - if(i != parent[i]): - parent[i] = find_parent(parent[i]) - return parent[i] + parent = list(range(num_nodes)) -minimum_spanning_tree_cost = 0 -minimum_spanning_tree = [] + def find_parent(i): + if i != parent[i]: + parent[i] = find_parent(parent[i]) + return parent[i] -for edge in edges: - parent_a = find_parent(edge[1]) - parent_b = find_parent(edge[2]) - if(parent_a != parent_b): - minimum_spanning_tree_cost += edge[3] - minimum_spanning_tree.append(edge) - parent[parent_a] = parent_b + minimum_spanning_tree_cost = 0 + minimum_spanning_tree = [] -print(minimum_spanning_tree_cost) -for edge in minimum_spanning_tree: - print(edge) + for edge in edges: + parent_a = find_parent(edge[0]) + parent_b = find_parent(edge[1]) + if parent_a != parent_b: + minimum_spanning_tree_cost += edge[2] + minimum_spanning_tree.append(edge) + parent[parent_a] = parent_b + + return minimum_spanning_tree + + +if __name__ == "__main__": # pragma: no cover + num_nodes, num_edges = list(map(int, input().strip().split())) + edges = [] + + for _ in range(num_edges): + node1, node2, cost = [int(x) for x in input().strip().split()] + edges.append((node1, node2, cost)) + + kruskal(num_nodes, num_edges, edges) diff --git a/graphs/minimum_spanning_tree_kruskal2.py b/graphs/minimum_spanning_tree_kruskal2.py new file mode 100644 index 000000000000..dfb87efeb89a --- /dev/null +++ b/graphs/minimum_spanning_tree_kruskal2.py @@ -0,0 +1,109 @@ +from __future__ import annotations + + +class DisjointSetTreeNode: + # Disjoint Set Node to store the parent and rank + def __init__(self, key: int) -> None: + self.key = key + self.parent = self + self.rank = 0 + + +class DisjointSetTree: + # Disjoint Set DataStructure + def __init__(self): + # map from node name to the node object + self.map = {} + + def make_set(self, x: int) -> None: + # create a new set with x as its member + self.map[x] = DisjointSetTreeNode(x) + + def find_set(self, x: int) -> DisjointSetTreeNode: + # find the set x belongs to (with path-compression) + elem_ref = self.map[x] + if elem_ref != elem_ref.parent: + elem_ref.parent = self.find_set(elem_ref.parent.key) + return elem_ref.parent + + def link(self, x: int, y: int) -> None: + # helper function for union operation + if x.rank > y.rank: + y.parent = x + else: + x.parent = y + if x.rank == y.rank: + y.rank += 1 + + def union(self, x: int, y: int) -> None: + # merge 2 disjoint sets + self.link(self.find_set(x), self.find_set(y)) + + +class GraphUndirectedWeighted: + def __init__(self): + # connections: map from the node to the neighbouring nodes (with weights) + self.connections = {} + + def add_node(self, node: int) -> None: + # add a node ONLY if its not present in the graph + if node not in self.connections: + self.connections[node] = {} + + def add_edge(self, node1: int, node2: int, weight: int) -> None: + # add an edge with the given weight + self.add_node(node1) + self.add_node(node2) + self.connections[node1][node2] = weight + self.connections[node2][node1] = weight + + def kruskal(self) -> GraphUndirectedWeighted: + # Kruskal's Algorithm to generate a Minimum Spanning Tree (MST) of a graph + """ + Details: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm + + Example: + + >>> graph = GraphUndirectedWeighted() + >>> graph.add_edge(1, 2, 1) + >>> graph.add_edge(2, 3, 2) + >>> graph.add_edge(3, 4, 1) + >>> graph.add_edge(3, 5, 100) # Removed in MST + >>> graph.add_edge(4, 5, 5) + >>> assert 5 in graph.connections[3] + >>> mst = graph.kruskal() + >>> assert 5 not in mst.connections[3] + """ + + # getting the edges in ascending order of weights + edges = [] + seen = set() + for start in self.connections: + for end in self.connections[start]: + if (start, end) not in seen: + seen.add((end, start)) + edges.append((start, end, self.connections[start][end])) + edges.sort(key=lambda x: x[2]) + # creating the disjoint set + disjoint_set = DisjointSetTree() + [disjoint_set.make_set(node) for node in self.connections] + # MST generation + num_edges = 0 + index = 0 + graph = GraphUndirectedWeighted() + while num_edges < len(self.connections) - 1: + u, v, w = edges[index] + index += 1 + parentu = disjoint_set.find_set(u) + parentv = disjoint_set.find_set(v) + if parentu != parentv: + num_edges += 1 + graph.add_edge(u, v, w) + disjoint_set.union(u, v) + return graph + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/graphs/minimum_spanning_tree_prims.py b/graphs/minimum_spanning_tree_prims.py index 7b1ad0e743f7..16b4286140ec 100644 --- a/graphs/minimum_spanning_tree_prims.py +++ b/graphs/minimum_spanning_tree_prims.py @@ -1,16 +1,18 @@ import sys from collections import defaultdict -def PrimsAlgorithm(l): + +def PrimsAlgorithm(l): # noqa: E741 nodePosition = [] - def getPosition(vertex): + + def get_position(vertex): return nodePosition[vertex] - def setPosition(vertex, pos): + def set_position(vertex, pos): nodePosition[vertex] = pos - def topToBottom(heap, start, size, positions): + def top_to_bottom(heap, start, size, positions): if start > size // 2 - 1: return else: @@ -26,52 +28,53 @@ def topToBottom(heap, start, size, positions): heap[m], positions[m] = heap[start], positions[start] heap[start], positions[start] = temp, temp1 - temp = getPosition(positions[m]) - setPosition(positions[m], getPosition(positions[start])) - setPosition(positions[start], temp) + temp = get_position(positions[m]) + set_position(positions[m], get_position(positions[start])) + set_position(positions[start], temp) - topToBottom(heap, m, size, positions) + top_to_bottom(heap, m, size, positions) # Update function if value of any node in min-heap decreases - def bottomToTop(val, index, heap, position): + def bottom_to_top(val, index, heap, position): temp = position[index] - while(index != 0): + while index != 0: if index % 2 == 0: - parent = int( (index-2) / 2 ) + parent = int((index - 2) / 2) else: - parent = int( (index-1) / 2 ) + parent = int((index - 1) / 2) if val < heap[parent]: heap[index] = heap[parent] position[index] = position[parent] - setPosition(position[parent], index) + set_position(position[parent], index) else: heap[index] = val position[index] = temp - setPosition(temp, index) + set_position(temp, index) break index = parent else: heap[0] = val position[0] = temp - setPosition(temp, 0) + set_position(temp, 0) def heapify(heap, positions): start = len(heap) // 2 - 1 for i in range(start, -1, -1): - topToBottom(heap, i, len(heap), positions) + top_to_bottom(heap, i, len(heap), positions) def deleteMinimum(heap, positions): temp = positions[0] heap[0] = sys.maxsize - topToBottom(heap, 0, len(heap), positions) + top_to_bottom(heap, 0, len(heap), positions) return temp visited = [0 for i in range(len(l))] - Nbr_TV = [-1 for i in range(len(l))] # Neighboring Tree Vertex of selected vertex - # Minimum Distance of explored vertex with neighboring vertex of partial tree formed in graph - Distance_TV = [] # Heap of Distance of vertices from their neighboring vertex + Nbr_TV = [-1 for i in range(len(l))] # Neighboring Tree Vertex of selected vertex + # Minimum Distance of explored vertex with neighboring vertex of partial tree + # formed in graph + Distance_TV = [] # Heap of Distance of vertices from their neighboring vertex Positions = [] for x in range(len(l)): @@ -84,8 +87,8 @@ def deleteMinimum(heap, positions): visited[0] = 1 Distance_TV[0] = sys.maxsize for x in l[0]: - Nbr_TV[ x[0] ] = 0 - Distance_TV[ x[0] ] = x[1] + Nbr_TV[x[0]] = 0 + Distance_TV[x[0]] = x[1] heapify(Distance_TV, Positions) for i in range(1, len(l)): @@ -94,18 +97,20 @@ def deleteMinimum(heap, positions): TreeEdges.append((Nbr_TV[vertex], vertex)) visited[vertex] = 1 for v in l[vertex]: - if visited[v[0]] == 0 and v[1] < Distance_TV[ getPosition(v[0]) ]: - Distance_TV[ getPosition(v[0]) ] = v[1] - bottomToTop(v[1], getPosition(v[0]), Distance_TV, Positions) - Nbr_TV[ v[0] ] = vertex + if visited[v[0]] == 0 and v[1] < Distance_TV[get_position(v[0])]: + Distance_TV[get_position(v[0])] = v[1] + bottom_to_top(v[1], get_position(v[0]), Distance_TV, Positions) + Nbr_TV[v[0]] = vertex return TreeEdges -# < --------- Prims Algorithm --------- > -n = int(input("Enter number of vertices: ")) -e = int(input("Enter number of edges: ")) -adjlist = defaultdict(list) -for x in range(e): - l = [int(x) for x in input().split()] - adjlist[l[0]].append([ l[1], l[2] ]) - adjlist[l[1]].append([ l[0], l[2] ]) -print(PrimsAlgorithm(adjlist)) + +if __name__ == "__main__": # pragma: no cover + # < --------- Prims Algorithm --------- > + n = int(input("Enter number of vertices: ").strip()) + e = int(input("Enter number of edges: ").strip()) + adjlist = defaultdict(list) + for x in range(e): + l = [int(x) for x in input().strip().split()] # noqa: E741 + adjlist[l[0]].append([l[1], l[2]]) + adjlist[l[1]].append([l[0], l[2]]) + print(PrimsAlgorithm(adjlist)) diff --git a/graphs/minimum_spanning_tree_prims2.py b/graphs/minimum_spanning_tree_prims2.py new file mode 100644 index 000000000000..10ed736c9d17 --- /dev/null +++ b/graphs/minimum_spanning_tree_prims2.py @@ -0,0 +1,271 @@ +""" +Prim's (also known as Jarník's) algorithm is a greedy algorithm that finds a minimum +spanning tree for a weighted undirected graph. This means it finds a subset of the +edges that forms a tree that includes every vertex, where the total weight of all the +edges in the tree is minimized. The algorithm operates by building this tree one vertex +at a time, from an arbitrary starting vertex, at each step adding the cheapest possible +connection from the tree to another vertex. +""" + +from sys import maxsize +from typing import Dict, Optional, Tuple, Union + + +def get_parent_position(position: int) -> int: + """ + heap helper function get the position of the parent of the current node + + >>> get_parent_position(1) + 0 + >>> get_parent_position(2) + 0 + """ + return (position - 1) // 2 + + +def get_child_left_position(position: int) -> int: + """ + heap helper function get the position of the left child of the current node + + >>> get_child_left_position(0) + 1 + """ + return (2 * position) + 1 + + +def get_child_right_position(position: int) -> int: + """ + heap helper function get the position of the right child of the current node + + >>> get_child_right_position(0) + 2 + """ + return (2 * position) + 2 + + +class MinPriorityQueue: + """ + Minimum Priority Queue Class + + Functions: + is_empty: function to check if the priority queue is empty + push: function to add an element with given priority to the queue + extract_min: function to remove and return the element with lowest weight (highest + priority) + update_key: function to update the weight of the given key + _bubble_up: helper function to place a node at the proper position (upward + movement) + _bubble_down: helper function to place a node at the proper position (downward + movement) + _swap_nodes: helper function to swap the nodes at the given positions + + >>> queue = MinPriorityQueue() + + >>> queue.push(1, 1000) + >>> queue.push(2, 100) + >>> queue.push(3, 4000) + >>> queue.push(4, 3000) + + >>> print(queue.extract_min()) + 2 + + >>> queue.update_key(4, 50) + + >>> print(queue.extract_min()) + 4 + >>> print(queue.extract_min()) + 1 + >>> print(queue.extract_min()) + 3 + """ + + def __init__(self) -> None: + self.heap = [] + self.position_map = {} + self.elements = 0 + + def __len__(self) -> int: + return self.elements + + def __repr__(self) -> str: + return str(self.heap) + + def is_empty(self) -> bool: + # Check if the priority queue is empty + return self.elements == 0 + + def push(self, elem: Union[int, str], weight: int) -> None: + # Add an element with given priority to the queue + self.heap.append((elem, weight)) + self.position_map[elem] = self.elements + self.elements += 1 + self._bubble_up(elem) + + def extract_min(self) -> Union[int, str]: + # Remove and return the element with lowest weight (highest priority) + if self.elements > 1: + self._swap_nodes(0, self.elements - 1) + elem, _ = self.heap.pop() + del self.position_map[elem] + self.elements -= 1 + if self.elements > 0: + bubble_down_elem, _ = self.heap[0] + self._bubble_down(bubble_down_elem) + return elem + + def update_key(self, elem: Union[int, str], weight: int) -> None: + # Update the weight of the given key + position = self.position_map[elem] + self.heap[position] = (elem, weight) + if position > 0: + parent_position = get_parent_position(position) + _, parent_weight = self.heap[parent_position] + if parent_weight > weight: + self._bubble_up(elem) + else: + self._bubble_down(elem) + else: + self._bubble_down(elem) + + def _bubble_up(self, elem: Union[int, str]) -> None: + # Place a node at the proper position (upward movement) [to be used internally + # only] + curr_pos = self.position_map[elem] + if curr_pos == 0: + return + parent_position = get_parent_position(curr_pos) + _, weight = self.heap[curr_pos] + _, parent_weight = self.heap[parent_position] + if parent_weight > weight: + self._swap_nodes(parent_position, curr_pos) + return self._bubble_up(elem) + return + + def _bubble_down(self, elem: Union[int, str]) -> None: + # Place a node at the proper position (downward movement) [to be used + # internally only] + curr_pos = self.position_map[elem] + _, weight = self.heap[curr_pos] + child_left_position = get_child_left_position(curr_pos) + child_right_position = get_child_right_position(curr_pos) + if child_left_position < self.elements and child_right_position < self.elements: + _, child_left_weight = self.heap[child_left_position] + _, child_right_weight = self.heap[child_right_position] + if child_right_weight < child_left_weight: + if child_right_weight < weight: + self._swap_nodes(child_right_position, curr_pos) + return self._bubble_down(elem) + if child_left_position < self.elements: + _, child_left_weight = self.heap[child_left_position] + if child_left_weight < weight: + self._swap_nodes(child_left_position, curr_pos) + return self._bubble_down(elem) + else: + return + if child_right_position < self.elements: + _, child_right_weight = self.heap[child_right_position] + if child_right_weight < weight: + self._swap_nodes(child_right_position, curr_pos) + return self._bubble_down(elem) + else: + return + + def _swap_nodes(self, node1_pos: int, node2_pos: int) -> None: + # Swap the nodes at the given positions + node1_elem = self.heap[node1_pos][0] + node2_elem = self.heap[node2_pos][0] + self.heap[node1_pos], self.heap[node2_pos] = ( + self.heap[node2_pos], + self.heap[node1_pos], + ) + self.position_map[node1_elem] = node2_pos + self.position_map[node2_elem] = node1_pos + + +class GraphUndirectedWeighted: + """ + Graph Undirected Weighted Class + + Functions: + add_node: function to add a node in the graph + add_edge: function to add an edge between 2 nodes in the graph + """ + + def __init__(self) -> None: + self.connections = {} + self.nodes = 0 + + def __repr__(self) -> str: + return str(self.connections) + + def __len__(self) -> int: + return self.nodes + + def add_node(self, node: Union[int, str]) -> None: + # Add a node in the graph if it is not in the graph + if node not in self.connections: + self.connections[node] = {} + self.nodes += 1 + + def add_edge( + self, node1: Union[int, str], node2: Union[int, str], weight: int + ) -> None: + # Add an edge between 2 nodes in the graph + self.add_node(node1) + self.add_node(node2) + self.connections[node1][node2] = weight + self.connections[node2][node1] = weight + + +def prims_algo( + graph: GraphUndirectedWeighted, +) -> Tuple[Dict[str, int], Dict[str, Optional[str]]]: + """ + >>> graph = GraphUndirectedWeighted() + + >>> graph.add_edge("a", "b", 3) + >>> graph.add_edge("b", "c", 10) + >>> graph.add_edge("c", "d", 5) + >>> graph.add_edge("a", "c", 15) + >>> graph.add_edge("b", "d", 100) + + >>> dist, parent = prims_algo(graph) + + >>> abs(dist["a"] - dist["b"]) + 3 + >>> abs(dist["d"] - dist["b"]) + 15 + >>> abs(dist["a"] - dist["c"]) + 13 + """ + # prim's algorithm for minimum spanning tree + dist = {node: maxsize for node in graph.connections} + parent = {node: None for node in graph.connections} + priority_queue = MinPriorityQueue() + [priority_queue.push(node, weight) for node, weight in dist.items()] + if priority_queue.is_empty(): + return dist, parent + + # initialization + node = priority_queue.extract_min() + dist[node] = 0 + for neighbour in graph.connections[node]: + if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: + dist[neighbour] = dist[node] + graph.connections[node][neighbour] + priority_queue.update_key(neighbour, dist[neighbour]) + parent[neighbour] = node + # running prim's algorithm + while not priority_queue.is_empty(): + node = priority_queue.extract_min() + for neighbour in graph.connections[node]: + if dist[neighbour] > dist[node] + graph.connections[node][neighbour]: + dist[neighbour] = dist[node] + graph.connections[node][neighbour] + priority_queue.update_key(neighbour, dist[neighbour]) + parent[neighbour] = node + return dist, parent + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/graphs/multi_heuristic_astar.py b/graphs/multi_heuristic_astar.py new file mode 100644 index 000000000000..77ca5760d5f0 --- /dev/null +++ b/graphs/multi_heuristic_astar.py @@ -0,0 +1,313 @@ +import heapq + +import numpy as np + + +class PriorityQueue: + def __init__(self): + self.elements = [] + self.set = set() + + def minkey(self): + if not self.empty(): + return self.elements[0][0] + else: + return float("inf") + + def empty(self): + return len(self.elements) == 0 + + def put(self, item, priority): + if item not in self.set: + heapq.heappush(self.elements, (priority, item)) + self.set.add(item) + else: + # update + # print("update", item) + temp = [] + (pri, x) = heapq.heappop(self.elements) + while x != item: + temp.append((pri, x)) + (pri, x) = heapq.heappop(self.elements) + temp.append((priority, item)) + for (pro, xxx) in temp: + heapq.heappush(self.elements, (pro, xxx)) + + def remove_element(self, item): + if item in self.set: + self.set.remove(item) + temp = [] + (pro, x) = heapq.heappop(self.elements) + while x != item: + temp.append((pro, x)) + (pro, x) = heapq.heappop(self.elements) + for (prito, yyy) in temp: + heapq.heappush(self.elements, (prito, yyy)) + + def top_show(self): + return self.elements[0][1] + + def get(self): + (priority, item) = heapq.heappop(self.elements) + self.set.remove(item) + return (priority, item) + + +def consistent_heuristic(P, goal): + # euclidean distance + a = np.array(P) + b = np.array(goal) + return np.linalg.norm(a - b) + + +def heuristic_2(P, goal): + # integer division by time variable + return consistent_heuristic(P, goal) // t + + +def heuristic_1(P, goal): + # manhattan distance + return abs(P[0] - goal[0]) + abs(P[1] - goal[1]) + + +def key(start, i, goal, g_function): + ans = g_function[start] + W1 * heuristics[i](start, goal) + return ans + + +def do_something(back_pointer, goal, start): + grid = np.chararray((n, n)) + for i in range(n): + for j in range(n): + grid[i][j] = "*" + + for i in range(n): + for j in range(n): + if (j, (n - 1) - i) in blocks: + grid[i][j] = "#" + + grid[0][(n - 1)] = "-" + x = back_pointer[goal] + while x != start: + (x_c, y_c) = x + # print(x) + grid[(n - 1) - y_c][x_c] = "-" + x = back_pointer[x] + grid[(n - 1)][0] = "-" + + for i in range(n): + for j in range(n): + if (i, j) == (0, n - 1): + print(grid[i][j], end=" ") + print("<-- End position", end=" ") + else: + print(grid[i][j], end=" ") + print() + print("^") + print("Start position") + print() + print("# is an obstacle") + print("- is the path taken by algorithm") + print("PATH TAKEN BY THE ALGORITHM IS:-") + x = back_pointer[goal] + while x != start: + print(x, end=" ") + x = back_pointer[x] + print(x) + quit() + + +def valid(p): + if p[0] < 0 or p[0] > n - 1: + return False + if p[1] < 0 or p[1] > n - 1: + return False + return True + + +def expand_state( + s, + j, + visited, + g_function, + close_list_anchor, + close_list_inad, + open_list, + back_pointer, +): + for itera in range(n_heuristic): + open_list[itera].remove_element(s) + # print("s", s) + # print("j", j) + (x, y) = s + left = (x - 1, y) + right = (x + 1, y) + up = (x, y + 1) + down = (x, y - 1) + + for neighbours in [left, right, up, down]: + if neighbours not in blocks: + if valid(neighbours) and neighbours not in visited: + # print("neighbour", neighbours) + visited.add(neighbours) + back_pointer[neighbours] = -1 + g_function[neighbours] = float("inf") + + if valid(neighbours) and g_function[neighbours] > g_function[s] + 1: + g_function[neighbours] = g_function[s] + 1 + back_pointer[neighbours] = s + if neighbours not in close_list_anchor: + open_list[0].put(neighbours, key(neighbours, 0, goal, g_function)) + if neighbours not in close_list_inad: + for var in range(1, n_heuristic): + if key(neighbours, var, goal, g_function) <= W2 * key( + neighbours, 0, goal, g_function + ): + open_list[j].put( + neighbours, key(neighbours, var, goal, g_function) + ) + + +def make_common_ground(): + some_list = [] + for x in range(1, 5): + for y in range(1, 6): + some_list.append((x, y)) + + for x in range(15, 20): + some_list.append((x, 17)) + + for x in range(10, 19): + for y in range(1, 15): + some_list.append((x, y)) + + # L block + for x in range(1, 4): + for y in range(12, 19): + some_list.append((x, y)) + for x in range(3, 13): + for y in range(16, 19): + some_list.append((x, y)) + return some_list + + +heuristics = {0: consistent_heuristic, 1: heuristic_1, 2: heuristic_2} + +blocks_blk = [ + (0, 1), + (1, 1), + (2, 1), + (3, 1), + (4, 1), + (5, 1), + (6, 1), + (7, 1), + (8, 1), + (9, 1), + (10, 1), + (11, 1), + (12, 1), + (13, 1), + (14, 1), + (15, 1), + (16, 1), + (17, 1), + (18, 1), + (19, 1), +] +blocks_no = [] +blocks_all = make_common_ground() + + +blocks = blocks_blk +# hyper parameters +W1 = 1 +W2 = 1 +n = 20 +n_heuristic = 3 # one consistent and two other inconsistent + +# start and end destination +start = (0, 0) +goal = (n - 1, n - 1) + +t = 1 + + +def multi_a_star(start, goal, n_heuristic): + g_function = {start: 0, goal: float("inf")} + back_pointer = {start: -1, goal: -1} + open_list = [] + visited = set() + + for i in range(n_heuristic): + open_list.append(PriorityQueue()) + open_list[i].put(start, key(start, i, goal, g_function)) + + close_list_anchor = [] + close_list_inad = [] + while open_list[0].minkey() < float("inf"): + for i in range(1, n_heuristic): + # print(open_list[0].minkey(), open_list[i].minkey()) + if open_list[i].minkey() <= W2 * open_list[0].minkey(): + global t + t += 1 + if g_function[goal] <= open_list[i].minkey(): + if g_function[goal] < float("inf"): + do_something(back_pointer, goal, start) + else: + _, get_s = open_list[i].top_show() + visited.add(get_s) + expand_state( + get_s, + i, + visited, + g_function, + close_list_anchor, + close_list_inad, + open_list, + back_pointer, + ) + close_list_inad.append(get_s) + else: + if g_function[goal] <= open_list[0].minkey(): + if g_function[goal] < float("inf"): + do_something(back_pointer, goal, start) + else: + get_s = open_list[0].top_show() + visited.add(get_s) + expand_state( + get_s, + 0, + visited, + g_function, + close_list_anchor, + close_list_inad, + open_list, + back_pointer, + ) + close_list_anchor.append(get_s) + print("No path found to goal") + print() + for i in range(n - 1, -1, -1): + for j in range(n): + if (j, i) in blocks: + print("#", end=" ") + elif (j, i) in back_pointer: + if (j, i) == (n - 1, n - 1): + print("*", end=" ") + else: + print("-", end=" ") + else: + print("*", end=" ") + if (j, i) == (n - 1, n - 1): + print("<-- End position", end=" ") + print() + print("^") + print("Start position") + print() + print("# is an obstacle") + print("- is the path taken by algorithm") + + +if __name__ == "__main__": + multi_a_star(start, goal, n_heuristic) diff --git a/graphs/multi_hueristic_astar.py b/graphs/multi_hueristic_astar.py deleted file mode 100644 index 1acd098f327d..000000000000 --- a/graphs/multi_hueristic_astar.py +++ /dev/null @@ -1,266 +0,0 @@ -from __future__ import print_function -import heapq -import numpy as np - -try: - xrange # Python 2 -except NameError: - xrange = range # Python 3 - - -class PriorityQueue: - def __init__(self): - self.elements = [] - self.set = set() - - def minkey(self): - if not self.empty(): - return self.elements[0][0] - else: - return float('inf') - - def empty(self): - return len(self.elements) == 0 - - def put(self, item, priority): - if item not in self.set: - heapq.heappush(self.elements, (priority, item)) - self.set.add(item) - else: - # update - # print("update", item) - temp = [] - (pri, x) = heapq.heappop(self.elements) - while x != item: - temp.append((pri, x)) - (pri, x) = heapq.heappop(self.elements) - temp.append((priority, item)) - for (pro, xxx) in temp: - heapq.heappush(self.elements, (pro, xxx)) - - def remove_element(self, item): - if item in self.set: - self.set.remove(item) - temp = [] - (pro, x) = heapq.heappop(self.elements) - while x != item: - temp.append((pro, x)) - (pro, x) = heapq.heappop(self.elements) - for (prito, yyy) in temp: - heapq.heappush(self.elements, (prito, yyy)) - - def top_show(self): - return self.elements[0][1] - - def get(self): - (priority, item) = heapq.heappop(self.elements) - self.set.remove(item) - return (priority, item) - -def consistent_hueristic(P, goal): - # euclidean distance - a = np.array(P) - b = np.array(goal) - return np.linalg.norm(a - b) - -def hueristic_2(P, goal): - # integer division by time variable - return consistent_hueristic(P, goal) // t - -def hueristic_1(P, goal): - # manhattan distance - return abs(P[0] - goal[0]) + abs(P[1] - goal[1]) - -def key(start, i, goal, g_function): - ans = g_function[start] + W1 * hueristics[i](start, goal) - return ans - -def do_something(back_pointer, goal, start): - grid = np.chararray((n, n)) - for i in range(n): - for j in range(n): - grid[i][j] = '*' - - for i in range(n): - for j in range(n): - if (j, (n-1)-i) in blocks: - grid[i][j] = "#" - - grid[0][(n-1)] = "-" - x = back_pointer[goal] - while x != start: - (x_c, y_c) = x - # print(x) - grid[(n-1)-y_c][x_c] = "-" - x = back_pointer[x] - grid[(n-1)][0] = "-" - - - for i in xrange(n): - for j in range(n): - if (i, j) == (0, n-1): - print(grid[i][j], end=' ') - print("<-- End position", end=' ') - else: - print(grid[i][j], end=' ') - print() - print("^") - print("Start position") - print() - print("# is an obstacle") - print("- is the path taken by algorithm") - print("PATH TAKEN BY THE ALGORITHM IS:-") - x = back_pointer[goal] - while x != start: - print(x, end=' ') - x = back_pointer[x] - print(x) - quit() - -def valid(p): - if p[0] < 0 or p[0] > n-1: - return False - if p[1] < 0 or p[1] > n-1: - return False - return True - -def expand_state(s, j, visited, g_function, close_list_anchor, close_list_inad, open_list, back_pointer): - for itera in range(n_hueristic): - open_list[itera].remove_element(s) - # print("s", s) - # print("j", j) - (x, y) = s - left = (x-1, y) - right = (x+1, y) - up = (x, y+1) - down = (x, y-1) - - for neighbours in [left, right, up, down]: - if neighbours not in blocks: - if valid(neighbours) and neighbours not in visited: - # print("neighbour", neighbours) - visited.add(neighbours) - back_pointer[neighbours] = -1 - g_function[neighbours] = float('inf') - - if valid(neighbours) and g_function[neighbours] > g_function[s] + 1: - g_function[neighbours] = g_function[s] + 1 - back_pointer[neighbours] = s - if neighbours not in close_list_anchor: - open_list[0].put(neighbours, key(neighbours, 0, goal, g_function)) - if neighbours not in close_list_inad: - for var in range(1,n_hueristic): - if key(neighbours, var, goal, g_function) <= W2 * key(neighbours, 0, goal, g_function): - # print("why not plssssssssss") - open_list[j].put(neighbours, key(neighbours, var, goal, g_function)) - - - # print - -def make_common_ground(): - some_list = [] - # block 1 - for x in range(1, 5): - for y in range(1, 6): - some_list.append((x, y)) - - # line - for x in range(15, 20): - some_list.append((x, 17)) - - # block 2 big - for x in range(10, 19): - for y in range(1, 15): - some_list.append((x, y)) - - # L block - for x in range(1, 4): - for y in range(12, 19): - some_list.append((x, y)) - for x in range(3, 13): - for y in range(16, 19): - some_list.append((x, y)) - return some_list - -hueristics = {0: consistent_hueristic, 1: hueristic_1, 2: hueristic_2} - -blocks_blk = [(0, 1),(1, 1),(2, 1),(3, 1),(4, 1),(5, 1),(6, 1),(7, 1),(8, 1),(9, 1),(10, 1),(11, 1),(12, 1),(13, 1),(14, 1),(15, 1),(16, 1),(17, 1),(18, 1), (19, 1)] -blocks_no = [] -blocks_all = make_common_ground() - - - - -blocks = blocks_blk -# hyper parameters -W1 = 1 -W2 = 1 -n = 20 -n_hueristic = 3 # one consistent and two other inconsistent - -# start and end destination -start = (0, 0) -goal = (n-1, n-1) - -t = 1 -def multi_a_star(start, goal, n_hueristic): - g_function = {start: 0, goal: float('inf')} - back_pointer = {start:-1, goal:-1} - open_list = [] - visited = set() - - for i in range(n_hueristic): - open_list.append(PriorityQueue()) - open_list[i].put(start, key(start, i, goal, g_function)) - - close_list_anchor = [] - close_list_inad = [] - while open_list[0].minkey() < float('inf'): - for i in range(1, n_hueristic): - # print("i", i) - # print(open_list[0].minkey(), open_list[i].minkey()) - if open_list[i].minkey() <= W2 * open_list[0].minkey(): - global t - t += 1 - # print("less prio") - if g_function[goal] <= open_list[i].minkey(): - if g_function[goal] < float('inf'): - do_something(back_pointer, goal, start) - else: - _, get_s = open_list[i].top_show() - visited.add(get_s) - expand_state(get_s, i, visited, g_function, close_list_anchor, close_list_inad, open_list, back_pointer) - close_list_inad.append(get_s) - else: - # print("more prio") - if g_function[goal] <= open_list[0].minkey(): - if g_function[goal] < float('inf'): - do_something(back_pointer, goal, start) - else: - # print("hoolla") - get_s = open_list[0].top_show() - visited.add(get_s) - expand_state(get_s, 0, visited, g_function, close_list_anchor, close_list_inad, open_list, back_pointer) - close_list_anchor.append(get_s) - print("No path found to goal") - print() - for i in range(n-1,-1, -1): - for j in range(n): - if (j, i) in blocks: - print('#', end=' ') - elif (j, i) in back_pointer: - if (j, i) == (n-1, n-1): - print('*', end=' ') - else: - print('-', end=' ') - else: - print('*', end=' ') - if (j, i) == (n-1, n-1): - print('<-- End position', end=' ') - print() - print("^") - print("Start position") - print() - print("# is an obstacle") - print("- is the path taken by algorithm") -multi_a_star(start, goal, n_hueristic) diff --git a/graphs/page_rank.py b/graphs/page_rank.py new file mode 100644 index 000000000000..0f5129146ddf --- /dev/null +++ b/graphs/page_rank.py @@ -0,0 +1,70 @@ +""" +Author: https://github.com/bhushan-borole +""" +""" +The input graph for the algorithm is: + + A B C +A 0 1 1 +B 0 0 1 +C 1 0 0 + +""" + +graph = [[0, 1, 1], [0, 0, 1], [1, 0, 0]] + + +class Node: + def __init__(self, name): + self.name = name + self.inbound = [] + self.outbound = [] + + def add_inbound(self, node): + self.inbound.append(node) + + def add_outbound(self, node): + self.outbound.append(node) + + def __repr__(self): + return f"Node {self.name}: Inbound: {self.inbound} ; Outbound: {self.outbound}" + + +def page_rank(nodes, limit=3, d=0.85): + ranks = {} + for node in nodes: + ranks[node.name] = 1 + + outbounds = {} + for node in nodes: + outbounds[node.name] = len(node.outbound) + + for i in range(limit): + print(f"======= Iteration {i + 1} =======") + for j, node in enumerate(nodes): + ranks[node.name] = (1 - d) + d * sum( + [ranks[ib] / outbounds[ib] for ib in node.inbound] + ) + print(ranks) + + +def main(): + names = list(input("Enter Names of the Nodes: ").split()) + + nodes = [Node(name) for name in names] + + for ri, row in enumerate(graph): + for ci, col in enumerate(row): + if col == 1: + nodes[ci].add_inbound(names[ri]) + nodes[ri].add_outbound(names[ci]) + + print("======= Nodes =======") + for node in nodes: + print(node) + + page_rank(nodes) + + +if __name__ == "__main__": + main() diff --git a/graphs/prim.py b/graphs/prim.py new file mode 100644 index 000000000000..70329da7e8e2 --- /dev/null +++ b/graphs/prim.py @@ -0,0 +1,152 @@ +"""Prim's Algorithm. + + Determines the minimum spanning tree(MST) of a graph using the Prim's Algorithm. + + Details: https://en.wikipedia.org/wiki/Prim%27s_algorithm +""" + +import heapq as hq +import math +from typing import Iterator + + +class Vertex: + """Class Vertex.""" + + def __init__(self, id): + """ + Arguments: + id - input an id to identify the vertex + Attributes: + neighbors - a list of the vertices it is linked to + edges - a dict to store the edges's weight + """ + self.id = str(id) + self.key = None + self.pi = None + self.neighbors = [] + self.edges = {} # {vertex:distance} + + def __lt__(self, other): + """Comparison rule to < operator.""" + return self.key < other.key + + def __repr__(self): + """Return the vertex id.""" + return self.id + + def add_neighbor(self, vertex): + """Add a pointer to a vertex at neighbor's list.""" + self.neighbors.append(vertex) + + def add_edge(self, vertex, weight): + """Destination vertex and weight.""" + self.edges[vertex.id] = weight + + +def connect(graph, a, b, edge): + # add the neighbors: + graph[a - 1].add_neighbor(graph[b - 1]) + graph[b - 1].add_neighbor(graph[a - 1]) + # add the edges: + graph[a - 1].add_edge(graph[b - 1], edge) + graph[b - 1].add_edge(graph[a - 1], edge) + + +def prim(graph: list, root: Vertex) -> list: + """Prim's Algorithm. + + Runtime: + O(mn) with `m` edges and `n` vertices + + Return: + List with the edges of a Minimum Spanning Tree + + Usage: + prim(graph, graph[0]) + """ + a = [] + for u in graph: + u.key = math.inf + u.pi = None + root.key = 0 + q = graph[:] + while q: + u = min(q) + q.remove(u) + for v in u.neighbors: + if (v in q) and (u.edges[v.id] < v.key): + v.pi = u + v.key = u.edges[v.id] + for i in range(1, len(graph)): + a.append((int(graph[i].id) + 1, int(graph[i].pi.id) + 1)) + return a + + +def prim_heap(graph: list, root: Vertex) -> Iterator[tuple]: + """Prim's Algorithm with min heap. + + Runtime: + O((m + n)log n) with `m` edges and `n` vertices + + Yield: + Edges of a Minimum Spanning Tree + + Usage: + prim(graph, graph[0]) + """ + for u in graph: + u.key = math.inf + u.pi = None + root.key = 0 + + h = list(graph) + hq.heapify(h) + + while h: + u = hq.heappop(h) + for v in u.neighbors: + if (v in h) and (u.edges[v.id] < v.key): + v.pi = u + v.key = u.edges[v.id] + hq.heapify(h) + + for i in range(1, len(graph)): + yield (int(graph[i].id) + 1, int(graph[i].pi.id) + 1) + + +def test_vector() -> None: + """ + # Creates a list to store x vertices. + >>> x = 5 + >>> G = [Vertex(n) for n in range(x)] + + >>> connect(G, 1, 2, 15) + >>> connect(G, 1, 3, 12) + >>> connect(G, 2, 4, 13) + >>> connect(G, 2, 5, 5) + >>> connect(G, 3, 2, 6) + >>> connect(G, 3, 4, 6) + >>> connect(G, 0, 0, 0) # Generate the minimum spanning tree: + >>> G_heap = G[:] + >>> MST = prim(G, G[0]) + >>> MST_heap = prim_heap(G, G[0]) + >>> for i in MST: + ... print(i) + (2, 3) + (3, 1) + (4, 3) + (5, 2) + >>> for i in MST_heap: + ... print(i) + (2, 3) + (3, 1) + (4, 3) + (5, 2) + """ + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/graphs/scc_kosaraju.py b/graphs/scc_kosaraju.py index 1f13ebaba36b..573c1bf5e363 100644 --- a/graphs/scc_kosaraju.py +++ b/graphs/scc_kosaraju.py @@ -1,46 +1,51 @@ -from __future__ import print_function -# n - no of nodes, m - no of edges -n, m = list(map(int,input().split())) - -g = [[] for i in range(n)] #graph -r = [[] for i in range(n)] #reversed graph -# input graph data (edges) -for i in range(m): - u, v = list(map(int,input().split())) - g[u].append(v) - r[v].append(u) - -stack = [] -visit = [False]*n -scc = [] -component = [] - def dfs(u): global g, r, scc, component, visit, stack - if visit[u]: return + if visit[u]: + return visit[u] = True for v in g[u]: dfs(v) stack.append(u) + def dfs2(u): global g, r, scc, component, visit, stack - if visit[u]: return + if visit[u]: + return visit[u] = True component.append(u) for v in r[u]: dfs2(v) + def kosaraju(): global g, r, scc, component, visit, stack for i in range(n): dfs(i) - visit = [False]*n + visit = [False] * n for i in stack[::-1]: - if visit[i]: continue + if visit[i]: + continue component = [] dfs2(i) scc.append(component) return scc -print(kosaraju()) + +if __name__ == "__main__": + # n - no of nodes, m - no of edges + n, m = list(map(int, input().strip().split())) + + g = [[] for i in range(n)] # graph + r = [[] for i in range(n)] # reversed graph + # input graph data (edges) + for i in range(m): + u, v = list(map(int, input().strip().split())) + g[u].append(v) + r[v].append(u) + + stack = [] + visit = [False] * n + scc = [] + component = [] + print(kosaraju()) diff --git a/graphs/strongly_connected_components.py b/graphs/strongly_connected_components.py new file mode 100644 index 000000000000..d469df0c625b --- /dev/null +++ b/graphs/strongly_connected_components.py @@ -0,0 +1,92 @@ +""" +https://en.wikipedia.org/wiki/Strongly_connected_component + +Finding strongly connected components in directed graph + +""" + +test_graph_1 = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []} + +test_graph_2 = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} + + +def topology_sort(graph: dict, vert: int, visited: list) -> list: + """ + Use depth first search to sort graph + At this time graph is the same as input + >>> topology_sort(test_graph_1, 0, 5 * [False]) + [1, 2, 4, 3, 0] + >>> topology_sort(test_graph_2, 0, 6 * [False]) + [2, 1, 5, 4, 3, 0] + """ + + visited[vert] = True + order = [] + + for neighbour in graph[vert]: + if not visited[neighbour]: + order += topology_sort(graph, neighbour, visited) + + order.append(vert) + + return order + + +def find_components(reversed_graph: dict, vert: int, visited: list) -> list: + """ + Use depth first search to find strongliy connected + vertices. Now graph is reversed + >>> find_components({0: [1], 1: [2], 2: [0]}, 0, 5 * [False]) + [0, 1, 2] + >>> find_components({0: [2], 1: [0], 2: [0, 1]}, 0, 6 * [False]) + [0, 2, 1] + """ + + visited[vert] = True + component = [vert] + + for neighbour in reversed_graph[vert]: + if not visited[neighbour]: + component += find_components(reversed_graph, neighbour, visited) + + return component + + +def strongly_connected_components(graph: dict) -> list: + """ + This function takes graph as a parameter + and then returns the list of strongly connected components + >>> strongly_connected_components(test_graph_1) + [[0, 1, 2], [3], [4]] + >>> strongly_connected_components(test_graph_2) + [[0, 2, 1], [3, 5, 4]] + """ + + visited = len(graph) * [False] + reversed_graph = {vert: [] for vert in range(len(graph))} + + for vert, neighbours in graph.items(): + for neighbour in neighbours: + reversed_graph[neighbour].append(vert) + + order = [] + for i, was_visited in enumerate(visited): + if not was_visited: + order += topology_sort(graph, i, visited) + + components_list = [] + visited = len(graph) * [False] + + for i in range(len(graph)): + vert = order[len(graph) - i - 1] + if not visited[vert]: + component = find_components(reversed_graph, vert, visited) + components_list.append(component) + + return components_list + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/graphs/tarjans_scc.py b/graphs/tarjans_scc.py index 89754e593508..30f8ca8a204f 100644 --- a/graphs/tarjans_scc.py +++ b/graphs/tarjans_scc.py @@ -5,19 +5,20 @@ def tarjan(g): """ Tarjan's algo for finding strongly connected components in a directed graph - Uses two main attributes of each node to track reachability, the index of that node within a component(index), - and the lowest index reachable from that node(lowlink). + Uses two main attributes of each node to track reachability, the index of that node + within a component(index), and the lowest index reachable from that node(lowlink). - We then perform a dfs of the each component making sure to update these parameters for each node and saving the - nodes we visit on the way. + We then perform a dfs of the each component making sure to update these parameters + for each node and saving the nodes we visit on the way. - If ever we find that the lowest reachable node from a current node is equal to the index of the current node then it - must be the root of a strongly connected component and so we save it and it's equireachable vertices as a strongly + If ever we find that the lowest reachable node from a current node is equal to the + index of the current node then it must be the root of a strongly connected + component and so we save it and it's equireachable vertices as a strongly connected component. - Complexity: strong_connect() is called at most once for each node and has a complexity of O(|E|) as it is DFS. + Complexity: strong_connect() is called at most once for each node and has a + complexity of O(|E|) as it is DFS. Therefore this has complexity O(|V| + |E|) for a graph G = (V, E) - """ n = len(g) @@ -36,9 +37,13 @@ def strong_connect(v, index, components): for w in g[v]: if index_of[w] == -1: index = strong_connect(w, index, components) - lowlink_of[v] = lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] + lowlink_of[v] = ( + lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] + ) elif on_stack[w]: - lowlink_of[v] = lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] + lowlink_of[v] = ( + lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v] + ) if lowlink_of[v] == index_of[v]: component = [] @@ -67,7 +72,7 @@ def create_graph(n, edges): return g -if __name__ == '__main__': +if __name__ == "__main__": # Test n_vertices = 7 source = [0, 0, 1, 2, 3, 3, 4, 4, 6] diff --git a/graphs/tests/test_min_spanning_tree_kruskal.py b/graphs/tests/test_min_spanning_tree_kruskal.py new file mode 100644 index 000000000000..3a527aef384f --- /dev/null +++ b/graphs/tests/test_min_spanning_tree_kruskal.py @@ -0,0 +1,36 @@ +from graphs.minimum_spanning_tree_kruskal import kruskal + + +def test_kruskal_successful_result(): + num_nodes, num_edges = 9, 14 + edges = [ + [0, 1, 4], + [0, 7, 8], + [1, 2, 8], + [7, 8, 7], + [7, 6, 1], + [2, 8, 2], + [8, 6, 6], + [2, 3, 7], + [2, 5, 4], + [6, 5, 2], + [3, 5, 14], + [3, 4, 9], + [5, 4, 10], + [1, 7, 11], + ] + + result = kruskal(num_nodes, num_edges, edges) + + expected = [ + [7, 6, 1], + [2, 8, 2], + [6, 5, 2], + [0, 1, 4], + [2, 5, 4], + [2, 3, 7], + [0, 7, 8], + [3, 4, 9], + ] + + assert sorted(expected) == sorted(result) diff --git a/graphs/tests/test_min_spanning_tree_prim.py b/graphs/tests/test_min_spanning_tree_prim.py new file mode 100644 index 000000000000..048fbf595fa6 --- /dev/null +++ b/graphs/tests/test_min_spanning_tree_prim.py @@ -0,0 +1,46 @@ +from collections import defaultdict + +from graphs.minimum_spanning_tree_prims import PrimsAlgorithm as mst + + +def test_prim_successful_result(): + num_nodes, num_edges = 9, 14 # noqa: F841 + edges = [ + [0, 1, 4], + [0, 7, 8], + [1, 2, 8], + [7, 8, 7], + [7, 6, 1], + [2, 8, 2], + [8, 6, 6], + [2, 3, 7], + [2, 5, 4], + [6, 5, 2], + [3, 5, 14], + [3, 4, 9], + [5, 4, 10], + [1, 7, 11], + ] + + adjancency = defaultdict(list) + for node1, node2, cost in edges: + adjancency[node1].append([node2, cost]) + adjancency[node2].append([node1, cost]) + + result = mst(adjancency) + + expected = [ + [7, 6, 1], + [2, 8, 2], + [6, 5, 2], + [0, 1, 4], + [2, 5, 4], + [2, 3, 7], + [0, 7, 8], + [3, 4, 9], + ] + + for answer in expected: + edge = tuple(answer[:2]) + reverse = tuple(edge[::-1]) + assert edge in result or reverse in result diff --git a/hashes/__init__.py b/hashes/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/hashes/adler32.py b/hashes/adler32.py new file mode 100644 index 000000000000..fad747abe3c3 --- /dev/null +++ b/hashes/adler32.py @@ -0,0 +1,29 @@ +""" + Adler-32 is a checksum algorithm which was invented by Mark Adler in 1995. + Compared to a cyclic redundancy check of the same length, it trades reliability for + speed (preferring the latter). + Adler-32 is more reliable than Fletcher-16, and slightly less reliable than + Fletcher-32.[2] + + source: https://en.wikipedia.org/wiki/Adler-32 +""" + + +def adler32(plain_text: str) -> str: + """ + Function implements adler-32 hash. + Itterates and evaluates new value for each character + + >>> adler32('Algorithms') + 363791387 + + >>> adler32('go adler em all') + 708642122 + """ + MOD_ADLER = 65521 + a = 1 + b = 0 + for plain_chr in plain_text: + a = (a + ord(plain_chr)) % MOD_ADLER + b = (b + a) % MOD_ADLER + return (b << 16) | a diff --git a/hashes/chaos_machine.py b/hashes/chaos_machine.py index f0a305bfeade..1bdf984b68de 100644 --- a/hashes/chaos_machine.py +++ b/hashes/chaos_machine.py @@ -1,13 +1,9 @@ """example of simple chaos machine""" -from __future__ import print_function - -try: - input = raw_input # Python 2 -except NameError: - pass # Python 3 # Chaos Machine (K, t, m) -K = [0.33, 0.44, 0.55, 0.44, 0.33]; t = 3; m = 5 +K = [0.33, 0.44, 0.55, 0.44, 0.33] +t = 3 +m = 5 # Buffer Space (with Parameters Space) buffer_space, params_space = [], [] @@ -15,92 +11,91 @@ # Machine Time machine_time = 0 + def push(seed): - global buffer_space, params_space, machine_time, \ - K, m, t + global buffer_space, params_space, machine_time, K, m, t + + # Choosing Dynamical Systems (All) + for key, value in enumerate(buffer_space): + # Evolution Parameter + e = float(seed / value) - # Choosing Dynamical Systems (All) - for key, value in enumerate(buffer_space): - # Evolution Parameter - e = float(seed / value) + # Control Theory: Orbit Change + value = (buffer_space[(key + 1) % m] + e) % 1 - # Control Theory: Orbit Change - value = (buffer_space[(key + 1) % m] + e) % 1 + # Control Theory: Trajectory Change + r = (params_space[key] + e) % 1 + 3 - # Control Theory: Trajectory Change - r = (params_space[key] + e) % 1 + 3 + # Modification (Transition Function) - Jumps + buffer_space[key] = round(float(r * value * (1 - value)), 10) + params_space[key] = r # Saving to Parameters Space - # Modification (Transition Function) - Jumps - buffer_space[key] = \ - round(float(r * value * (1 - value)), 10) - params_space[key] = \ - r # Saving to Parameters Space + # Logistic Map + assert max(buffer_space) < 1 + assert max(params_space) < 4 - # Logistic Map - assert max(buffer_space) < 1 - assert max(params_space) < 4 + # Machine Time + machine_time += 1 - # Machine Time - machine_time += 1 def pull(): - global buffer_space, params_space, machine_time, \ - K, m, t + global buffer_space, params_space, machine_time, K, m, t - # PRNG (Xorshift by George Marsaglia) - def xorshift(X, Y): - X ^= Y >> 13 - Y ^= X << 17 - X ^= Y >> 5 - return X + # PRNG (Xorshift by George Marsaglia) + def xorshift(X, Y): + X ^= Y >> 13 + Y ^= X << 17 + X ^= Y >> 5 + return X - # Choosing Dynamical Systems (Increment) - key = machine_time % m + # Choosing Dynamical Systems (Increment) + key = machine_time % m - # Evolution (Time Length) - for i in range(0, t): - # Variables (Position + Parameters) - r = params_space[key] - value = buffer_space[key] + # Evolution (Time Length) + for i in range(0, t): + # Variables (Position + Parameters) + r = params_space[key] + value = buffer_space[key] - # Modification (Transition Function) - Flow - buffer_space[key] = \ - round(float(r * value * (1 - value)), 10) - params_space[key] = \ - (machine_time * 0.01 + r * 1.01) % 1 + 3 + # Modification (Transition Function) - Flow + buffer_space[key] = round(float(r * value * (1 - value)), 10) + params_space[key] = (machine_time * 0.01 + r * 1.01) % 1 + 3 - # Choosing Chaotic Data - X = int(buffer_space[(key + 2) % m] * (10 ** 10)) - Y = int(buffer_space[(key - 2) % m] * (10 ** 10)) + # Choosing Chaotic Data + X = int(buffer_space[(key + 2) % m] * (10 ** 10)) + Y = int(buffer_space[(key - 2) % m] * (10 ** 10)) - # Machine Time - machine_time += 1 + # Machine Time + machine_time += 1 + + return xorshift(X, Y) % 0xFFFFFFFF - return xorshift(X, Y) % 0xFFFFFFFF def reset(): - global buffer_space, params_space, machine_time, \ - K, m, t + global buffer_space, params_space, machine_time, K, m, t + + buffer_space = K + params_space = [0] * m + machine_time = 0 - buffer_space = K; params_space = [0] * m - machine_time = 0 -####################################### +if __name__ == "__main__": + # Initialization + reset() -# Initialization -reset() + # Pushing Data (Input) + import random -# Pushing Data (Input) -import random -message = random.sample(range(0xFFFFFFFF), 100) -for chunk in message: - push(chunk) + message = random.sample(range(0xFFFFFFFF), 100) + for chunk in message: + push(chunk) -# for controlling -inp = "" + # for controlling + inp = "" -# Pulling Data (Output) -while inp in ("e", "E"): - print("%s" % format(pull(), '#04x')) - print(buffer_space); print(params_space) - inp = input("(e)exit? ").strip() + # Pulling Data (Output) + while inp in ("e", "E"): + print("%s" % format(pull(), "#04x")) + print(buffer_space) + print(params_space) + inp = input("(e)exit? ").strip() diff --git a/hashes/djb2.py b/hashes/djb2.py new file mode 100644 index 000000000000..2d1c9aabb1fb --- /dev/null +++ b/hashes/djb2.py @@ -0,0 +1,35 @@ +""" +This algorithm (k=33) was first reported by Dan Bernstein many years ago in comp.lang.c +Another version of this algorithm (now favored by Bernstein) uses xor: + hash(i) = hash(i - 1) * 33 ^ str[i]; + + First Magic constant 33: + It has never been adequately explained. + It's magic because it works better than many other constants, prime or not. + + Second Magic Constant 5381: + + 1. odd number + 2. prime number + 3. deficient number + 4. 001/010/100/000/101 b + + source: http://www.cse.yorku.ca/~oz/hash.html +""" + + +def djb2(s: str) -> int: + """ + Implementation of djb2 hash algorithm that + is popular because of it's magic constants. + + >>> djb2('Algorithms') + 3782405311 + + >>> djb2('scramble bits') + 1609059040 + """ + hash = 5381 + for x in s: + hash = ((hash << 5) + hash) + ord(x) + return hash & 0xFFFFFFFF diff --git a/hashes/enigma_machine.py b/hashes/enigma_machine.py new file mode 100644 index 000000000000..5420bacc1409 --- /dev/null +++ b/hashes/enigma_machine.py @@ -0,0 +1,60 @@ +alphabets = [chr(i) for i in range(32, 126)] +gear_one = [i for i in range(len(alphabets))] +gear_two = [i for i in range(len(alphabets))] +gear_three = [i for i in range(len(alphabets))] +reflector = [i for i in reversed(range(len(alphabets)))] +code = [] +gear_one_pos = gear_two_pos = gear_three_pos = 0 + + +def rotator(): + global gear_one_pos + global gear_two_pos + global gear_three_pos + i = gear_one[0] + gear_one.append(i) + del gear_one[0] + gear_one_pos += 1 + if gear_one_pos % int(len(alphabets)) == 0: + i = gear_two[0] + gear_two.append(i) + del gear_two[0] + gear_two_pos += 1 + if gear_two_pos % int(len(alphabets)) == 0: + i = gear_three[0] + gear_three.append(i) + del gear_three[0] + gear_three_pos += 1 + + +def engine(input_character): + target = alphabets.index(input_character) + target = gear_one[target] + target = gear_two[target] + target = gear_three[target] + target = reflector[target] + target = gear_three.index(target) + target = gear_two.index(target) + target = gear_one.index(target) + code.append(alphabets[target]) + rotator() + + +if __name__ == "__main__": + decode = input("Type your message:\n") + decode = list(decode) + while True: + try: + token = int(input("Please set token:(must be only digits)\n")) + break + except Exception as error: + print(error) + for i in range(token): + rotator() + for i in decode: + engine(i) + print("\n" + "".join(code)) + print( + f"\nYour Token is {token} please write it down.\nIf you want to decode " + f"this message again you should input same digits as token!" + ) diff --git a/hashes/hamming_code.py b/hashes/hamming_code.py new file mode 100644 index 000000000000..4a32bae1a51c --- /dev/null +++ b/hashes/hamming_code.py @@ -0,0 +1,296 @@ +# Author: João Gustavo A. Amorim & Gabriel Kunz +# Author email: joaogustavoamorim@gmail.com and gabriel-kunz@uergs.edu.br +# Coding date: apr 2019 +# Black: True + +""" + * This code implement the Hamming code: + https://en.wikipedia.org/wiki/Hamming_code - In telecommunication, + Hamming codes are a family of linear error-correcting codes. Hamming + codes can detect up to two-bit errors or correct one-bit errors + without detection of uncorrected errors. By contrast, the simple + parity code cannot correct errors, and can detect only an odd number + of bits in error. Hamming codes are perfect codes, that is, they + achieve the highest possible rate for codes with their block length + and minimum distance of three. + + * the implemented code consists of: + * a function responsible for encoding the message (emitterConverter) + * return the encoded message + * a function responsible for decoding the message (receptorConverter) + * return the decoded message and a ack of data integrity + + * how to use: + to be used you must declare how many parity bits (sizePari) + you want to include in the message. + it is desired (for test purposes) to select a bit to be set + as an error. This serves to check whether the code is working correctly. + Lastly, the variable of the message/word that must be desired to be + encoded (text). + + * how this work: + declaration of variables (sizePari, be, text) + + converts the message/word (text) to binary using the + text_to_bits function + encodes the message using the rules of hamming encoding + decodes the message using the rules of hamming encoding + print the original message, the encoded message and the + decoded message + + forces an error in the coded text variable + decodes the message that was forced the error + print the original message, the encoded message, the bit changed + message and the decoded message +""" + +# Imports +import numpy as np + + +# Functions of binary conversion-------------------------------------- +def text_to_bits(text, encoding="utf-8", errors="surrogatepass"): + """ + >>> text_to_bits("msg") + '011011010111001101100111' + """ + bits = bin(int.from_bytes(text.encode(encoding, errors), "big"))[2:] + return bits.zfill(8 * ((len(bits) + 7) // 8)) + + +def text_from_bits(bits, encoding="utf-8", errors="surrogatepass"): + """ + >>> text_from_bits('011011010111001101100111') + 'msg' + """ + n = int(bits, 2) + return n.to_bytes((n.bit_length() + 7) // 8, "big").decode(encoding, errors) or "\0" + + +# Functions of hamming code------------------------------------------- +def emitterConverter(sizePar, data): + """ + :param sizePar: how many parity bits the message must have + :param data: information bits + :return: message to be transmitted by unreliable medium + - bits of information merged with parity bits + + >>> emitterConverter(4, "101010111111") + ['1', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1'] + """ + if sizePar + len(data) <= 2 ** sizePar - (len(data) - 1): + print("ERROR - size of parity don't match with size of data") + exit(0) + + dataOut = [] + parity = [] + binPos = [bin(x)[2:] for x in range(1, sizePar + len(data) + 1)] + + # sorted information data for the size of the output data + dataOrd = [] + # data position template + parity + dataOutGab = [] + # parity bit counter + qtdBP = 0 + # counter position of data bits + contData = 0 + + for x in range(1, sizePar + len(data) + 1): + # Performs a template of bit positions - who should be given, + # and who should be parity + if qtdBP < sizePar: + if (np.log(x) / np.log(2)).is_integer(): + dataOutGab.append("P") + qtdBP = qtdBP + 1 + else: + dataOutGab.append("D") + else: + dataOutGab.append("D") + + # Sorts the data to the new output size + if dataOutGab[-1] == "D": + dataOrd.append(data[contData]) + contData += 1 + else: + dataOrd.append(None) + + # Calculates parity + qtdBP = 0 # parity bit counter + for bp in range(1, sizePar + 1): + # Bit counter one for a given parity + contBO = 0 + # counter to control the loop reading + contLoop = 0 + for x in dataOrd: + if x is not None: + try: + aux = (binPos[contLoop])[-1 * (bp)] + except IndexError: + aux = "0" + if aux == "1": + if x == "1": + contBO += 1 + contLoop += 1 + parity.append(contBO % 2) + + qtdBP += 1 + + # Mount the message + ContBP = 0 # parity bit counter + for x in range(0, sizePar + len(data)): + if dataOrd[x] is None: + dataOut.append(str(parity[ContBP])) + ContBP += 1 + else: + dataOut.append(dataOrd[x]) + + return dataOut + + +def receptorConverter(sizePar, data): + """ + >>> receptorConverter(4, "1111010010111111") + (['1', '0', '1', '0', '1', '0', '1', '1', '1', '1', '1', '1'], True) + """ + # data position template + parity + dataOutGab = [] + # Parity bit counter + qtdBP = 0 + # Counter p data bit reading + contData = 0 + # list of parity received + parityReceived = [] + dataOutput = [] + + for x in range(1, len(data) + 1): + # Performs a template of bit positions - who should be given, + # and who should be parity + if qtdBP < sizePar and (np.log(x) / np.log(2)).is_integer(): + dataOutGab.append("P") + qtdBP = qtdBP + 1 + else: + dataOutGab.append("D") + + # Sorts the data to the new output size + if dataOutGab[-1] == "D": + dataOutput.append(data[contData]) + else: + parityReceived.append(data[contData]) + contData += 1 + + # -----------calculates the parity with the data + dataOut = [] + parity = [] + binPos = [bin(x)[2:] for x in range(1, sizePar + len(dataOutput) + 1)] + + # sorted information data for the size of the output data + dataOrd = [] + # Data position feedback + parity + dataOutGab = [] + # Parity bit counter + qtdBP = 0 + # Counter p data bit reading + contData = 0 + + for x in range(1, sizePar + len(dataOutput) + 1): + # Performs a template position of bits - who should be given, + # and who should be parity + if qtdBP < sizePar and (np.log(x) / np.log(2)).is_integer(): + dataOutGab.append("P") + qtdBP = qtdBP + 1 + else: + dataOutGab.append("D") + + # Sorts the data to the new output size + if dataOutGab[-1] == "D": + dataOrd.append(dataOutput[contData]) + contData += 1 + else: + dataOrd.append(None) + + # Calculates parity + qtdBP = 0 # parity bit counter + for bp in range(1, sizePar + 1): + # Bit counter one for a certain parity + contBO = 0 + # Counter to control loop reading + contLoop = 0 + for x in dataOrd: + if x is not None: + try: + aux = (binPos[contLoop])[-1 * (bp)] + except IndexError: + aux = "0" + if aux == "1" and x == "1": + contBO += 1 + contLoop += 1 + parity.append(str(contBO % 2)) + + qtdBP += 1 + + # Mount the message + ContBP = 0 # Parity bit counter + for x in range(0, sizePar + len(dataOutput)): + if dataOrd[x] is None: + dataOut.append(str(parity[ContBP])) + ContBP += 1 + else: + dataOut.append(dataOrd[x]) + + ack = parityReceived == parity + return dataOutput, ack + + +# --------------------------------------------------------------------- +""" +# Example how to use + +# number of parity bits +sizePari = 4 + +# location of the bit that will be forced an error +be = 2 + +# Message/word to be encoded and decoded with hamming +# text = input("Enter the word to be read: ") +text = "Message01" + +# Convert the message to binary +binaryText = text_to_bits(text) + +# Prints the binary of the string +print("Text input in binary is '" + binaryText + "'") + +# total transmitted bits +totalBits = len(binaryText) + sizePari +print("Size of data is " + str(totalBits)) + +print("\n --Message exchange--") +print("Data to send ------------> " + binaryText) +dataOut = emitterConverter(sizePari, binaryText) +print("Data converted ----------> " + "".join(dataOut)) +dataReceiv, ack = receptorConverter(sizePari, dataOut) +print( + "Data receive ------------> " + + "".join(dataReceiv) + + "\t\t -- Data integrity: " + + str(ack) +) + + +print("\n --Force error--") +print("Data to send ------------> " + binaryText) +dataOut = emitterConverter(sizePari, binaryText) +print("Data converted ----------> " + "".join(dataOut)) + +# forces error +dataOut[-be] = "1" * (dataOut[-be] == "0") + "0" * (dataOut[-be] == "1") +print("Data after transmission -> " + "".join(dataOut)) +dataReceiv, ack = receptorConverter(sizePari, dataOut) +print( + "Data receive ------------> " + + "".join(dataReceiv) + + "\t\t -- Data integrity: " + + str(ack) +) +""" diff --git a/hashes/md5.py b/hashes/md5.py index d3f15510874e..b7888fb610ac 100644 --- a/hashes/md5.py +++ b/hashes/md5.py @@ -1,155 +1,239 @@ -from __future__ import print_function import math + def rearrange(bitString32): - """[summary] - Regroups the given binary string. - - Arguments: - bitString32 {[string]} -- [32 bit binary] - - Raises: - ValueError -- [if the given string not are 32 bit binary string] - - Returns: - [string] -- [32 bit binary string] - """ - - if len(bitString32) != 32: - raise ValueError("Need length 32") - newString = "" - for i in [3,2,1,0]: - newString += bitString32[8*i:8*i+8] - return newString + """[summary] + Regroups the given binary string. + + Arguments: + bitString32 {[string]} -- [32 bit binary] + + Raises: + ValueError -- [if the given string not are 32 bit binary string] + + Returns: + [string] -- [32 bit binary string] + >>> rearrange('1234567890abcdfghijklmnopqrstuvw') + 'pqrstuvwhijklmno90abcdfg12345678' + """ + + if len(bitString32) != 32: + raise ValueError("Need length 32") + newString = "" + for i in [3, 2, 1, 0]: + newString += bitString32[8 * i : 8 * i + 8] + return newString + def reformatHex(i): - """[summary] - Converts the given integer into 8-digit hex number. + """[summary] + Converts the given integer into 8-digit hex number. + + Arguments: + i {[int]} -- [integer] + >>> reformatHex(666) + '9a020000' + """ - Arguments: - i {[int]} -- [integer] - """ + hexrep = format(i, "08x") + thing = "" + for i in [3, 2, 1, 0]: + thing += hexrep[2 * i : 2 * i + 2] + return thing - hexrep = format(i,'08x') - thing = "" - for i in [3,2,1,0]: - thing += hexrep[2*i:2*i+2] - return thing def pad(bitString): - """[summary] - Fills up the binary string to a 512 bit binary string - - Arguments: - bitString {[string]} -- [binary string] - - Returns: - [string] -- [binary string] - """ - - startLength = len(bitString) - bitString += '1' - while len(bitString) % 512 != 448: - bitString += '0' - lastPart = format(startLength,'064b') - bitString += rearrange(lastPart[32:]) + rearrange(lastPart[:32]) - return bitString + """[summary] + Fills up the binary string to a 512 bit binary string + + Arguments: + bitString {[string]} -- [binary string] + + Returns: + [string] -- [binary string] + """ + startLength = len(bitString) + bitString += "1" + while len(bitString) % 512 != 448: + bitString += "0" + lastPart = format(startLength, "064b") + bitString += rearrange(lastPart[32:]) + rearrange(lastPart[:32]) + return bitString + def getBlock(bitString): - """[summary] - Iterator: - Returns by each call a list of length 16 with the 32 bit - integer blocks. - - Arguments: - bitString {[string]} -- [binary string >= 512] - """ - - currPos = 0 - while currPos < len(bitString): - currPart = bitString[currPos:currPos+512] - mySplits = [] - for i in range(16): - mySplits.append(int(rearrange(currPart[32*i:32*i+32]),2)) - yield mySplits - currPos += 512 + """[summary] + Iterator: + Returns by each call a list of length 16 with the 32 bit + integer blocks. + + Arguments: + bitString {[string]} -- [binary string >= 512] + """ + + currPos = 0 + while currPos < len(bitString): + currPart = bitString[currPos : currPos + 512] + mySplits = [] + for i in range(16): + mySplits.append(int(rearrange(currPart[32 * i : 32 * i + 32]), 2)) + yield mySplits + currPos += 512 + def not32(i): - i_str = format(i,'032b') - new_str = '' - for c in i_str: - new_str += '1' if c=='0' else '0' - return int(new_str,2) + """ + >>> not32(34) + 4294967261 + """ + i_str = format(i, "032b") + new_str = "" + for c in i_str: + new_str += "1" if c == "0" else "0" + return int(new_str, 2) + -def sum32(a,b): - return (a + b) % 2**32 +def sum32(a, b): + """""" + return (a + b) % 2 ** 32 + + +def leftrot32(i, s): + return (i << s) ^ (i >> (32 - s)) -def leftrot32(i,s): - return (i << s) ^ (i >> (32-s)) def md5me(testString): - """[summary] - Returns a 32-bit hash code of the string 'testString' - - Arguments: - testString {[string]} -- [message] - """ - - bs ='' - for i in testString: - bs += format(ord(i),'08b') - bs = pad(bs) - - tvals = [int(2**32 * abs(math.sin(i+1))) for i in range(64)] - - a0 = 0x67452301 - b0 = 0xefcdab89 - c0 = 0x98badcfe - d0 = 0x10325476 - - s = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, \ - 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, \ - 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, \ - 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 ] - - for m in getBlock(bs): - A = a0 - B = b0 - C = c0 - D = d0 - for i in range(64): - if i <= 15: - #f = (B & C) | (not32(B) & D) - f = D ^ (B & (C ^ D)) - g = i - elif i<= 31: - #f = (D & B) | (not32(D) & C) - f = C ^ (D & (B ^ C)) - g = (5*i+1) % 16 - elif i <= 47: - f = B ^ C ^ D - g = (3*i+5) % 16 - else: - f = C ^ (B | not32(D)) - g = (7*i) % 16 - dtemp = D - D = C - C = B - B = sum32(B,leftrot32((A + f + tvals[i] + m[g]) % 2**32, s[i])) - A = dtemp - a0 = sum32(a0, A) - b0 = sum32(b0, B) - c0 = sum32(c0, C) - d0 = sum32(d0, D) - - digest = reformatHex(a0) + reformatHex(b0) + reformatHex(c0) + reformatHex(d0) - return digest + """[summary] + Returns a 32-bit hash code of the string 'testString' + + Arguments: + testString {[string]} -- [message] + """ + + bs = "" + for i in testString: + bs += format(ord(i), "08b") + bs = pad(bs) + + tvals = [int(2 ** 32 * abs(math.sin(i + 1))) for i in range(64)] + + a0 = 0x67452301 + b0 = 0xEFCDAB89 + c0 = 0x98BADCFE + d0 = 0x10325476 + + s = [ + 7, + 12, + 17, + 22, + 7, + 12, + 17, + 22, + 7, + 12, + 17, + 22, + 7, + 12, + 17, + 22, + 5, + 9, + 14, + 20, + 5, + 9, + 14, + 20, + 5, + 9, + 14, + 20, + 5, + 9, + 14, + 20, + 4, + 11, + 16, + 23, + 4, + 11, + 16, + 23, + 4, + 11, + 16, + 23, + 4, + 11, + 16, + 23, + 6, + 10, + 15, + 21, + 6, + 10, + 15, + 21, + 6, + 10, + 15, + 21, + 6, + 10, + 15, + 21, + ] + + for m in getBlock(bs): + A = a0 + B = b0 + C = c0 + D = d0 + for i in range(64): + if i <= 15: + # f = (B & C) | (not32(B) & D) + f = D ^ (B & (C ^ D)) + g = i + elif i <= 31: + # f = (D & B) | (not32(D) & C) + f = C ^ (D & (B ^ C)) + g = (5 * i + 1) % 16 + elif i <= 47: + f = B ^ C ^ D + g = (3 * i + 5) % 16 + else: + f = C ^ (B | not32(D)) + g = (7 * i) % 16 + dtemp = D + D = C + C = B + B = sum32(B, leftrot32((A + f + tvals[i] + m[g]) % 2 ** 32, s[i])) + A = dtemp + a0 = sum32(a0, A) + b0 = sum32(b0, B) + c0 = sum32(c0, C) + d0 = sum32(d0, D) + + digest = reformatHex(a0) + reformatHex(b0) + reformatHex(c0) + reformatHex(d0) + return digest + def test(): - assert md5me("") == "d41d8cd98f00b204e9800998ecf8427e" - assert md5me("The quick brown fox jumps over the lazy dog") == "9e107d9d372bb6826bd81d3542a419d6" - print("Success.") + assert md5me("") == "d41d8cd98f00b204e9800998ecf8427e" + assert ( + md5me("The quick brown fox jumps over the lazy dog") + == "9e107d9d372bb6826bd81d3542a419d6" + ) + print("Success.") if __name__ == "__main__": - test() + test() + import doctest + + doctest.testmod() diff --git a/hashes/sdbm.py b/hashes/sdbm.py new file mode 100644 index 000000000000..86d47a1d9967 --- /dev/null +++ b/hashes/sdbm.py @@ -0,0 +1,37 @@ +""" + This algorithm was created for sdbm (a public-domain reimplementation of ndbm) + database library. + It was found to do well in scrambling bits, causing better distribution of the keys + and fewer splits. + It also happens to be a good general hashing function with good distribution. + The actual function (pseudo code) is: + for i in i..len(str): + hash(i) = hash(i - 1) * 65599 + str[i]; + + What is included below is the faster version used in gawk. [there is even a faster, + duff-device version] + The magic constant 65599 was picked out of thin air while experimenting with + different constants. + It turns out to be a prime. + This is one of the algorithms used in berkeley db (see sleepycat) and elsewhere. + + source: http://www.cse.yorku.ca/~oz/hash.html +""" + + +def sdbm(plain_text: str) -> str: + """ + Function implements sdbm hash, easy to use, great for bits scrambling. + iterates over each character in the given string and applies function to each of + them. + + >>> sdbm('Algorithms') + 1462174910723540325254304520539387479031000036 + + >>> sdbm('scramble bits') + 730247649148944819640658295400555317318720608290373040936089 + """ + hash = 0 + for plain_chr in plain_text: + hash = ord(plain_chr) + (hash << 6) + (hash << 16) - hash + return hash diff --git a/hashes/sha1.py b/hashes/sha1.py index 4c78ad3a89e5..cca38b7c3fdc 100644 --- a/hashes/sha1.py +++ b/hashes/sha1.py @@ -2,13 +2,14 @@ Demonstrates implementation of SHA1 Hash function in a Python class and gives utilities to find hash of string or hash of text from a file. Usage: python sha1.py --string "Hello World!!" - pyhton sha1.py --file "hello_world.txt" - When run without any arguments, it prints the hash of the string "Hello World!! Welcome to Cryptography" + python sha1.py --file "hello_world.txt" + When run without any arguments, it prints the hash of the string "Hello World!! + Welcome to Cryptography" Also contains a Test class to verify that the generated Hash is same as that returned by the hashlib library -SHA1 hash or SHA1 sum of a string is a crytpographic function which means it is easy -to calculate forwards but extemely difficult to calculate backwards. What this means +SHA1 hash or SHA1 sum of a string is a cryptographic function which means it is easy +to calculate forwards but extremely difficult to calculate backwards. What this means is, you can easily calculate the hash of a string, but it is extremely difficult to know the original string if you have its hash. This property is useful to communicate securely, send encrypted messages and is very useful in payment systems, blockchain @@ -22,21 +23,24 @@ the final hash. Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/ """ - import argparse +import hashlib # hashlib is only used inside the Test class import struct -import hashlib #hashlib is only used inside the Test class import unittest class SHA1Hash: """ Class to contain the entire pipeline for SHA1 Hashing Algorithm + >>> SHA1Hash(bytes('Allan', 'utf-8')).final_hash() + '872af2d8ac3d8695387e7c804bf0e02c18df9e6e' """ + def __init__(self, data): """ Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal - numbers corresponding to (1732584193, 4023233417, 2562383102, 271733878, 3285377520) + numbers corresponding to + (1732584193, 4023233417, 2562383102, 271733878, 3285377520) respectively. We will start with this as a message digest. 0x is how you write Hexadecimal numbers in Python """ @@ -47,42 +51,47 @@ def __init__(self, data): def rotate(n, b): """ Static method to be used inside other methods. Left rotates n by b. + >>> SHA1Hash('').rotate(12,2) + 48 """ - return ((n << b) | (n >> (32 - b))) & 0xffffffff + return ((n << b) | (n >> (32 - b))) & 0xFFFFFFFF def padding(self): """ Pads the input message with zeros so that padded_data has 64 bytes or 512 bits """ - padding = b'\x80' + b'\x00'*(63 - (len(self.data) + 8) % 64) - padded_data = self.data + padding + struct.pack('>Q', 8 * len(self.data)) + padding = b"\x80" + b"\x00" * (63 - (len(self.data) + 8) % 64) + padded_data = self.data + padding + struct.pack(">Q", 8 * len(self.data)) return padded_data def split_blocks(self): """ Returns a list of bytestrings each of length 64 """ - return [self.padded_data[i:i+64] for i in range(0, len(self.padded_data), 64)] + return [ + self.padded_data[i : i + 64] for i in range(0, len(self.padded_data), 64) + ] # @staticmethod def expand_block(self, block): """ - Takes a bytestring-block of length 64, unpacks it to a list of integers and returns a - list of 80 integers pafter some bit operations + Takes a bytestring-block of length 64, unpacks it to a list of integers and + returns a list of 80 integers after some bit operations """ - w = list(struct.unpack('>16L', block)) + [0] * 64 + w = list(struct.unpack(">16L", block)) + [0] * 64 for i in range(16, 80): - w[i] = self.rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1) + w[i] = self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1) return w def final_hash(self): """ - Calls all the other methods to process the input. Pads the data, then splits into - blocks and then does a series of operations for each block (including expansion). + Calls all the other methods to process the input. Pads the data, then splits + into blocks and then does a series of operations for each block (including + expansion). For each block, the variable h that was initialized is copied to a,b,c,d,e - and these 5 variables a,b,c,d,e undergo several changes. After all the blocks are - processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] and so on. - This h becomes our final hash which is returned. + and these 5 variables a,b,c,d,e undergo several changes. After all the blocks + are processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] + and so on. This h becomes our final hash which is returned. """ self.padded_data = self.padding() self.blocks = self.split_blocks() @@ -102,47 +111,61 @@ def final_hash(self): elif 60 <= i < 80: f = b ^ c ^ d k = 0xCA62C1D6 - a, b, c, d, e = self.rotate(a, 5) + f + e + k + expanded_block[i] & 0xffffffff,\ - a, self.rotate(b, 30), c, d - self.h = self.h[0] + a & 0xffffffff,\ - self.h[1] + b & 0xffffffff,\ - self.h[2] + c & 0xffffffff,\ - self.h[3] + d & 0xffffffff,\ - self.h[4] + e & 0xffffffff - return '%08x%08x%08x%08x%08x' %tuple(self.h) + a, b, c, d, e = ( + self.rotate(a, 5) + f + e + k + expanded_block[i] & 0xFFFFFFFF, + a, + self.rotate(b, 30), + c, + d, + ) + self.h = ( + self.h[0] + a & 0xFFFFFFFF, + self.h[1] + b & 0xFFFFFFFF, + self.h[2] + c & 0xFFFFFFFF, + self.h[3] + d & 0xFFFFFFFF, + self.h[4] + e & 0xFFFFFFFF, + ) + return "%08x%08x%08x%08x%08x" % tuple(self.h) class SHA1HashTest(unittest.TestCase): """ Test class for the SHA1Hash class. Inherits the TestCase class from unittest """ + def testMatchHashes(self): - msg = bytes('Test String', 'utf-8') + msg = bytes("Test String", "utf-8") self.assertEqual(SHA1Hash(msg).final_hash(), hashlib.sha1(msg).hexdigest()) def main(): """ - Provides option 'string' or 'file' to take input and prints the calculated SHA1 hash. - unittest.main() has been commented because we probably dont want to run + Provides option 'string' or 'file' to take input and prints the calculated SHA1 + hash. unittest.main() has been commented because we probably don't want to run the test each time. """ # unittest.main() - parser = argparse.ArgumentParser(description='Process some strings or files') - parser.add_argument('--string', dest='input_string', - default='Hello World!! Welcome to Cryptography', - help='Hash the string') - parser.add_argument('--file', dest='input_file', help='Hash contents of a file') + parser = argparse.ArgumentParser(description="Process some strings or files") + parser.add_argument( + "--string", + dest="input_string", + default="Hello World!! Welcome to Cryptography", + help="Hash the string", + ) + parser.add_argument("--file", dest="input_file", help="Hash contents of a file") args = parser.parse_args() input_string = args.input_string - #In any case hash input should be a bytestring + # In any case hash input should be a bytestring if args.input_file: - with open(args.input_file, 'rb') as f: + with open(args.input_file, "rb") as f: hash_input = f.read() else: - hash_input = bytes(input_string, 'utf-8') + hash_input = bytes(input_string, "utf-8") print(SHA1Hash(hash_input).final_hash()) -if __name__ == '__main__': +if __name__ == "__main__": main() + import doctest + + doctest.testmod() diff --git a/knapsack/README.md b/knapsack/README.md new file mode 100644 index 000000000000..6041c1e48eb8 --- /dev/null +++ b/knapsack/README.md @@ -0,0 +1,32 @@ +# A naive recursive implementation of 0-1 Knapsack Problem + +This overview is taken from: + + https://en.wikipedia.org/wiki/Knapsack_problem + +--- + +## Overview + +The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. The problem often arises in resource allocation where the decision makers have to choose from a set of non-divisible projects or tasks under a fixed budget or time constraint, respectively. + +The knapsack problem has been studied for more than a century, with early works dating as far back as 1897 The name "knapsack problem" dates back to the early works of mathematician Tobias Dantzig (1884–1956), and refers to the commonplace problem of packing the most valuable or useful items without overloading the luggage. + +--- + +## Documentation + +This module uses docstrings to enable the use of Python's in-built `help(...)` function. +For instance, try `help(Vector)`, `help(unitBasisVector)`, and `help(CLASSNAME.METHODNAME)`. + +--- + +## Usage + +Import the module `knapsack.py` from the **.** directory into your project. + +--- + +## Tests + +`.` contains Python unit tests which can be run with `python3 -m unittest -v`. diff --git a/knapsack/__init__.py b/knapsack/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/knapsack/greedy_knapsack.py b/knapsack/greedy_knapsack.py new file mode 100644 index 000000000000..ed6399c740c8 --- /dev/null +++ b/knapsack/greedy_knapsack.py @@ -0,0 +1,98 @@ +# To get an insight into Greedy Algorithm through the Knapsack problem + + +""" +A shopkeeper has bags of wheat that each have different weights and different profits. +eg. +profit 5 8 7 1 12 3 4 +weight 2 7 1 6 4 2 5 +max_weight 100 + +Constraints: +max_weight > 0 +profit[i] >= 0 +weight[i] >= 0 +Calculate the maximum profit that the shopkeeper can make given maxmum weight that can +be carried. +""" + + +def calc_profit(profit: list, weight: list, max_weight: int) -> int: + """ + Function description is as follows- + :param profit: Take a list of profits + :param weight: Take a list of weight if bags corresponding to the profits + :param max_weight: Maximum weight that could be carried + :return: Maximum expected gain + + >>> calc_profit([1, 2, 3], [3, 4, 5], 15) + 6 + >>> calc_profit([10, 9 , 8], [3 ,4 , 5], 25) + 27 + """ + if len(profit) != len(weight): + raise ValueError("The length of profit and weight must be same.") + if max_weight <= 0: + raise ValueError("max_weight must greater than zero.") + if any(p < 0 for p in profit): + raise ValueError("Profit can not be negative.") + if any(w < 0 for w in weight): + raise ValueError("Weight can not be negative.") + + # List created to store profit gained for the 1kg in case of each weight + # respectively. Calculate and append profit/weight for each element. + profit_by_weight = [p / w for p, w in zip(profit, weight)] + + # Creating a copy of the list and sorting profit/weight in ascending order + sorted_profit_by_weight = sorted(profit_by_weight) + + # declaring useful variables + length = len(sorted_profit_by_weight) + limit = 0 + gain = 0 + i = 0 + + # loop till the total weight do not reach max limit e.g. 15 kg and till i= weight[index]: + limit += weight[index] + # Adding profit gained for the given weight 1 === + # weight[index]/weight[index] + gain += 1 * profit[index] + else: + # Since the weight encountered is greater than limit, therefore take the + # required number of remaining kgs and calculate profit for it. + # weight remaining / weight[index] + gain += (max_weight - limit) / weight[index] * profit[index] + break + i += 1 + return gain + + +if __name__ == "__main__": + print( + "Input profits, weights, and then max_weight (all positive ints) separated by " + "spaces." + ) + + profit = [int(x) for x in input("Input profits separated by spaces: ").split()] + weight = [int(x) for x in input("Input weights separated by spaces: ").split()] + max_weight = int(input("Max weight allowed: ")) + + # Function Call + calc_profit(profit, weight, max_weight) diff --git a/knapsack/knapsack.py b/knapsack/knapsack.py new file mode 100644 index 000000000000..756443ea6163 --- /dev/null +++ b/knapsack/knapsack.py @@ -0,0 +1,47 @@ +from typing import List + +""" A naive recursive implementation of 0-1 Knapsack Problem + https://en.wikipedia.org/wiki/Knapsack_problem +""" + + +def knapsack(capacity: int, weights: List[int], values: List[int], counter: int) -> int: + """ + Returns the maximum value that can be put in a knapsack of a capacity cap, + whereby each weight w has a specific value val. + + >>> cap = 50 + >>> val = [60, 100, 120] + >>> w = [10, 20, 30] + >>> c = len(val) + >>> knapsack(cap, w, val, c) + 220 + + The result is 220 cause the values of 100 and 120 got the weight of 50 + which is the limit of the capacity. + """ + + # Base Case + if counter == 0 or capacity == 0: + return 0 + + # If weight of the nth item is more than Knapsack of capacity, + # then this item cannot be included in the optimal solution, + # else return the maximum of two cases: + # (1) nth item included + # (2) not included + if weights[counter - 1] > capacity: + return knapsack(capacity, weights, values, counter - 1) + else: + left_capacity = capacity - weights[counter - 1] + new_value_included = values[counter - 1] + knapsack( + left_capacity, weights, values, counter - 1 + ) + without_new_value = knapsack(capacity, weights, values, counter - 1) + return max(new_value_included, without_new_value) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/knapsack/tests/__init__.py b/knapsack/tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/knapsack/tests/test_greedy_knapsack.py b/knapsack/tests/test_greedy_knapsack.py new file mode 100644 index 000000000000..b7b62d5d80b4 --- /dev/null +++ b/knapsack/tests/test_greedy_knapsack.py @@ -0,0 +1,75 @@ +import unittest + +from knapsack import greedy_knapsack as kp + + +class TestClass(unittest.TestCase): + """ + Test cases for knapsack + """ + + def test_sorted(self): + """ + kp.calc_profit takes the required argument (profit, weight, max_weight) + and returns whether the answer matches to the expected ones + """ + profit = [10, 20, 30, 40, 50, 60] + weight = [2, 4, 6, 8, 10, 12] + max_weight = 100 + self.assertEqual(kp.calc_profit(profit, weight, max_weight), 210) + + def test_negative_max_weight(self): + """ + Returns ValueError for any negative max_weight value + :return: ValueError + """ + # profit = [10, 20, 30, 40, 50, 60] + # weight = [2, 4, 6, 8, 10, 12] + # max_weight = -15 + self.assertRaisesRegex(ValueError, "max_weight must greater than zero.") + + def test_negative_profit_value(self): + """ + Returns ValueError for any negative profit value in the list + :return: ValueError + """ + # profit = [10, -20, 30, 40, 50, 60] + # weight = [2, 4, 6, 8, 10, 12] + # max_weight = 15 + self.assertRaisesRegex(ValueError, "Weight can not be negative.") + + def test_negative_weight_value(self): + """ + Returns ValueError for any negative weight value in the list + :return: ValueError + """ + # profit = [10, 20, 30, 40, 50, 60] + # weight = [2, -4, 6, -8, 10, 12] + # max_weight = 15 + self.assertRaisesRegex(ValueError, "Profit can not be negative.") + + def test_null_max_weight(self): + """ + Returns ValueError for any zero max_weight value + :return: ValueError + """ + # profit = [10, 20, 30, 40, 50, 60] + # weight = [2, 4, 6, 8, 10, 12] + # max_weight = null + self.assertRaisesRegex(ValueError, "max_weight must greater than zero.") + + def test_unequal_list_length(self): + """ + Returns IndexError if length of lists (profit and weight) are unequal. + :return: IndexError + """ + # profit = [10, 20, 30, 40, 50] + # weight = [2, 4, 6, 8, 10, 12] + # max_weight = 100 + self.assertRaisesRegex( + IndexError, "The length of profit and weight must be same." + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/knapsack/tests/test_knapsack.py b/knapsack/tests/test_knapsack.py new file mode 100644 index 000000000000..248855fbce53 --- /dev/null +++ b/knapsack/tests/test_knapsack.py @@ -0,0 +1,52 @@ +""" +Created on Fri Oct 16 09:31:07 2020 + +@author: Dr. Tobias Schröder +@license: MIT-license + +This file contains the test-suite for the knapsack problem. +""" +import unittest + +from knapsack import knapsack as k + + +class Test(unittest.TestCase): + def test_base_case(self): + """ + test for the base case + """ + cap = 0 + val = [0] + w = [0] + c = len(val) + self.assertEqual(k.knapsack(cap, w, val, c), 0) + + val = [60] + w = [10] + c = len(val) + self.assertEqual(k.knapsack(cap, w, val, c), 0) + + def test_easy_case(self): + """ + test for the base case + """ + cap = 3 + val = [1, 2, 3] + w = [3, 2, 1] + c = len(val) + self.assertEqual(k.knapsack(cap, w, val, c), 5) + + def test_knapsack(self): + """ + test for the knapsack + """ + cap = 50 + val = [60, 100, 120] + w = [10, 20, 30] + c = len(val) + self.assertEqual(k.knapsack(cap, w, val, c), 220) + + +if __name__ == "__main__": + unittest.main() diff --git a/linear_algebra/README.md b/linear_algebra/README.md new file mode 100644 index 000000000000..dc6085090d02 --- /dev/null +++ b/linear_algebra/README.md @@ -0,0 +1,75 @@ +# Linear algebra library for Python + +This module contains classes and functions for doing linear algebra. + +--- + +## Overview + +### class Vector +- + - This class represents a vector of arbitrary size and related operations. + + **Overview about the methods:** + + - constructor(components : list) : init the vector + - set(components : list) : changes the vector components. + - \_\_str\_\_() : toString method + - component(i : int): gets the i-th component (start by 0) + - \_\_len\_\_() : gets the size / length of the vector (number of components) + - euclidLength() : returns the eulidean length of the vector. + - operator + : vector addition + - operator - : vector subtraction + - operator * : scalar multiplication and dot product + - copy() : copies this vector and returns it. + - changeComponent(pos,value) : changes the specified component. + +- function zeroVector(dimension) + - returns a zero vector of 'dimension' +- function unitBasisVector(dimension,pos) + - returns a unit basis vector with a One at index 'pos' (indexing at 0) +- function axpy(scalar,vector1,vector2) + - computes the axpy operation +- function randomVector(N,a,b) + - returns a random vector of size N, with random integer components between 'a' and 'b'. + +### class Matrix +- + - This class represents a matrix of arbitrary size and operations on it. + + **Overview about the methods:** + + - \_\_str\_\_() : returns a string representation + - operator * : implements the matrix vector multiplication + implements the matrix-scalar multiplication. + - changeComponent(x,y,value) : changes the specified component. + - component(x,y) : returns the specified component. + - width() : returns the width of the matrix + - height() : returns the height of the matrix + - determinate() : returns the determinate of the matrix if it is square + - operator + : implements the matrix-addition. + - operator - _ implements the matrix-subtraction + +- function squareZeroMatrix(N) + - returns a square zero-matrix of dimension NxN +- function randomMatrix(W,H,a,b) + - returns a random matrix WxH with integer components between 'a' and 'b' +--- + +## Documentation + +This module uses docstrings to enable the use of Python's in-built `help(...)` function. +For instance, try `help(Vector)`, `help(unitBasisVector)`, and `help(CLASSNAME.METHODNAME)`. + +--- + +## Usage + +Import the module `lib.py` from the **src** directory into your project. +Alternatively, you can directly use the Python bytecode file `lib.pyc`. + +--- + +## Tests + +`src/tests.py` contains Python unit tests which can be run with `python3 -m unittest -v`. diff --git a/linear_algebra/__init__.py b/linear_algebra/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/linear_algebra/src/__init__.py b/linear_algebra/src/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/linear_algebra/src/conjugate_gradient.py b/linear_algebra/src/conjugate_gradient.py new file mode 100644 index 000000000000..1a65b8ccf019 --- /dev/null +++ b/linear_algebra/src/conjugate_gradient.py @@ -0,0 +1,173 @@ +""" +Resources: +- https://en.wikipedia.org/wiki/Conjugate_gradient_method +- https://en.wikipedia.org/wiki/Definite_symmetric_matrix +""" +import numpy as np + + +def _is_matrix_spd(matrix: np.array) -> bool: + """ + Returns True if input matrix is symmetric positive definite. + Returns False otherwise. + + For a matrix to be SPD, all eigenvalues must be positive. + + >>> import numpy as np + >>> matrix = np.array([ + ... [4.12401784, -5.01453636, -0.63865857], + ... [-5.01453636, 12.33347422, -3.40493586], + ... [-0.63865857, -3.40493586, 5.78591885]]) + >>> _is_matrix_spd(matrix) + True + >>> matrix = np.array([ + ... [0.34634879, 1.96165514, 2.18277744], + ... [0.74074469, -1.19648894, -1.34223498], + ... [-0.7687067 , 0.06018373, -1.16315631]]) + >>> _is_matrix_spd(matrix) + False + """ + # Ensure matrix is square. + assert np.shape(matrix)[0] == np.shape(matrix)[1] + + # If matrix not symmetric, exit right away. + if np.allclose(matrix, matrix.T) is False: + return False + + # Get eigenvalues and eignevectors for a symmetric matrix. + eigen_values, _ = np.linalg.eigh(matrix) + + # Check sign of all eigenvalues. + return np.all(eigen_values > 0) + + +def _create_spd_matrix(dimension: np.int64) -> np.array: + """ + Returns a symmetric positive definite matrix given a dimension. + + Input: + dimension gives the square matrix dimension. + + Output: + spd_matrix is an diminesion x dimensions symmetric positive definite (SPD) matrix. + + >>> import numpy as np + >>> dimension = 3 + >>> spd_matrix = _create_spd_matrix(dimension) + >>> _is_matrix_spd(spd_matrix) + True + """ + random_matrix = np.random.randn(dimension, dimension) + spd_matrix = np.dot(random_matrix, random_matrix.T) + assert _is_matrix_spd(spd_matrix) + return spd_matrix + + +def conjugate_gradient( + spd_matrix: np.array, + load_vector: np.array, + max_iterations: int = 1000, + tol: float = 1e-8, +) -> np.array: + """ + Returns solution to the linear system np.dot(spd_matrix, x) = b. + + Input: + spd_matrix is an NxN Symmetric Positive Definite (SPD) matrix. + load_vector is an Nx1 vector. + + Output: + x is an Nx1 vector that is the solution vector. + + >>> import numpy as np + >>> spd_matrix = np.array([ + ... [8.73256573, -5.02034289, -2.68709226], + ... [-5.02034289, 3.78188322, 0.91980451], + ... [-2.68709226, 0.91980451, 1.94746467]]) + >>> b = np.array([ + ... [-5.80872761], + ... [ 3.23807431], + ... [ 1.95381422]]) + >>> conjugate_gradient(spd_matrix, b) + array([[-0.63114139], + [-0.01561498], + [ 0.13979294]]) + """ + # Ensure proper dimensionality. + assert np.shape(spd_matrix)[0] == np.shape(spd_matrix)[1] + assert np.shape(load_vector)[0] == np.shape(spd_matrix)[0] + assert _is_matrix_spd(spd_matrix) + + # Initialize solution guess, residual, search direction. + x0 = np.zeros((np.shape(load_vector)[0], 1)) + r0 = np.copy(load_vector) + p0 = np.copy(r0) + + # Set initial errors in solution guess and residual. + error_residual = 1e9 + error_x_solution = 1e9 + error = 1e9 + + # Set iteration counter to threshold number of iterations. + iterations = 0 + + while error > tol: + + # Save this value so we only calculate the matrix-vector product once. + w = np.dot(spd_matrix, p0) + + # The main algorithm. + + # Update search direction magnitude. + alpha = np.dot(r0.T, r0) / np.dot(p0.T, w) + # Update solution guess. + x = x0 + alpha * p0 + # Calculate new residual. + r = r0 - alpha * w + # Calculate new Krylov subspace scale. + beta = np.dot(r.T, r) / np.dot(r0.T, r0) + # Calculate new A conjuage search direction. + p = r + beta * p0 + + # Calculate errors. + error_residual = np.linalg.norm(r - r0) + error_x_solution = np.linalg.norm(x - x0) + error = np.maximum(error_residual, error_x_solution) + + # Update variables. + x0 = np.copy(x) + r0 = np.copy(r) + p0 = np.copy(p) + + # Update number of iterations. + iterations += 1 + + return x + + +def test_conjugate_gradient() -> None: + """ + >>> test_conjugate_gradient() # self running tests + """ + # Create linear system with SPD matrix and known solution x_true. + dimension = 3 + spd_matrix = _create_spd_matrix(dimension) + x_true = np.random.randn(dimension, 1) + b = np.dot(spd_matrix, x_true) + + # Numpy solution. + x_numpy = np.linalg.solve(spd_matrix, b) + + # Our implementation. + x_conjugate_gradient = conjugate_gradient(spd_matrix, b) + + # Ensure both solutions are close to x_true (and therefore one another). + assert np.linalg.norm(x_numpy - x_true) <= 1e-6 + assert np.linalg.norm(x_conjugate_gradient - x_true) <= 1e-6 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + test_conjugate_gradient() diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py new file mode 100644 index 000000000000..353c8334093b --- /dev/null +++ b/linear_algebra/src/lib.py @@ -0,0 +1,379 @@ +""" +Created on Mon Feb 26 14:29:11 2018 + +@author: Christian Bender +@license: MIT-license + +This module contains some useful classes and functions for dealing +with linear algebra in python. + +Overview: + +- class Vector +- function zeroVector(dimension) +- function unitBasisVector(dimension,pos) +- function axpy(scalar,vector1,vector2) +- function randomVector(N,a,b) +- class Matrix +- function squareZeroMatrix(N) +- function randomMatrix(W,H,a,b) +""" + + +import math +import random + + +class Vector: + """ + This class represents a vector of arbitrary size. + You need to give the vector components. + + Overview about the methods: + + constructor(components : list) : init the vector + set(components : list) : changes the vector components. + __str__() : toString method + component(i : int): gets the i-th component (start by 0) + __len__() : gets the size of the vector (number of components) + euclidLength() : returns the euclidean length of the vector. + operator + : vector addition + operator - : vector subtraction + operator * : scalar multiplication and dot product + copy() : copies this vector and returns it. + changeComponent(pos,value) : changes the specified component. + TODO: compare-operator + """ + + def __init__(self, components=None): + """ + input: components or nothing + simple constructor for init the vector + """ + if components is None: + components = [] + self.__components = list(components) + + def set(self, components): + """ + input: new components + changes the components of the vector. + replace the components with newer one. + """ + if len(components) > 0: + self.__components = list(components) + else: + raise Exception("please give any vector") + + def __str__(self): + """ + returns a string representation of the vector + """ + return "(" + ",".join(map(str, self.__components)) + ")" + + def component(self, i): + """ + input: index (start at 0) + output: the i-th component of the vector. + """ + if type(i) is int and -len(self.__components) <= i < len(self.__components): + return self.__components[i] + else: + raise Exception("index out of range") + + def __len__(self): + """ + returns the size of the vector + """ + return len(self.__components) + + def euclidLength(self): + """ + returns the euclidean length of the vector + """ + summe = 0 + for c in self.__components: + summe += c ** 2 + return math.sqrt(summe) + + def __add__(self, other): + """ + input: other vector + assumes: other vector has the same size + returns a new vector that represents the sum. + """ + size = len(self) + if size == len(other): + result = [self.__components[i] + other.component(i) for i in range(size)] + return Vector(result) + else: + raise Exception("must have the same size") + + def __sub__(self, other): + """ + input: other vector + assumes: other vector has the same size + returns a new vector that represents the difference. + """ + size = len(self) + if size == len(other): + result = [self.__components[i] - other.component(i) for i in range(size)] + return Vector(result) + else: # error case + raise Exception("must have the same size") + + def __mul__(self, other): + """ + mul implements the scalar multiplication + and the dot-product + """ + if isinstance(other, float) or isinstance(other, int): + ans = [c * other for c in self.__components] + return Vector(ans) + elif isinstance(other, Vector) and (len(self) == len(other)): + size = len(self) + summe = 0 + for i in range(size): + summe += self.__components[i] * other.component(i) + return summe + else: # error case + raise Exception("invalid operand!") + + def copy(self): + """ + copies this vector and returns it. + """ + return Vector(self.__components) + + def changeComponent(self, pos, value): + """ + input: an index (pos) and a value + changes the specified component (pos) with the + 'value' + """ + # precondition + assert -len(self.__components) <= pos < len(self.__components) + self.__components[pos] = value + + +def zeroVector(dimension): + """ + returns a zero-vector of size 'dimension' + """ + # precondition + assert isinstance(dimension, int) + return Vector([0] * dimension) + + +def unitBasisVector(dimension, pos): + """ + returns a unit basis vector with a One + at index 'pos' (indexing at 0) + """ + # precondition + assert isinstance(dimension, int) and (isinstance(pos, int)) + ans = [0] * dimension + ans[pos] = 1 + return Vector(ans) + + +def axpy(scalar, x, y): + """ + input: a 'scalar' and two vectors 'x' and 'y' + output: a vector + computes the axpy operation + """ + # precondition + assert ( + isinstance(x, Vector) + and (isinstance(y, Vector)) + and (isinstance(scalar, int) or isinstance(scalar, float)) + ) + return x * scalar + y + + +def randomVector(N, a, b): + """ + input: size (N) of the vector. + random range (a,b) + output: returns a random vector of size N, with + random integer components between 'a' and 'b'. + """ + random.seed(None) + ans = [random.randint(a, b) for i in range(N)] + return Vector(ans) + + +class Matrix: + """ + class: Matrix + This class represents a arbitrary matrix. + + Overview about the methods: + + __str__() : returns a string representation + operator * : implements the matrix vector multiplication + implements the matrix-scalar multiplication. + changeComponent(x,y,value) : changes the specified component. + component(x,y) : returns the specified component. + width() : returns the width of the matrix + height() : returns the height of the matrix + operator + : implements the matrix-addition. + operator - _ implements the matrix-subtraction + """ + + def __init__(self, matrix, w, h): + """ + simple constructor for initializing + the matrix with components. + """ + self.__matrix = matrix + self.__width = w + self.__height = h + + def __str__(self): + """ + returns a string representation of this + matrix. + """ + ans = "" + for i in range(self.__height): + ans += "|" + for j in range(self.__width): + if j < self.__width - 1: + ans += str(self.__matrix[i][j]) + "," + else: + ans += str(self.__matrix[i][j]) + "|\n" + return ans + + def changeComponent(self, x, y, value): + """ + changes the x-y component of this matrix + """ + if 0 <= x < self.__height and 0 <= y < self.__width: + self.__matrix[x][y] = value + else: + raise Exception("changeComponent: indices out of bounds") + + def component(self, x, y): + """ + returns the specified (x,y) component + """ + if 0 <= x < self.__height and 0 <= y < self.__width: + return self.__matrix[x][y] + else: + raise Exception("changeComponent: indices out of bounds") + + def width(self): + """ + getter for the width + """ + return self.__width + + def height(self): + """ + getter for the height + """ + return self.__height + + def determinate(self) -> float: + """ + returns the determinate of an nxn matrix using Laplace expansion + """ + if self.__height == self.__width and self.__width >= 2: + total = 0 + if self.__width > 2: + for x in range(0, self.__width): + for y in range(0, self.__height): + total += ( + self.__matrix[x][y] + * (-1) ** (x + y) + * Matrix( + self.__matrix[0:x] + self.__matrix[x + 1 :], + self.__width - 1, + self.__height - 1, + ).determinate() + ) + else: + return ( + self.__matrix[0][0] * self.__matrix[1][1] + - self.__matrix[0][1] * self.__matrix[1][0] + ) + return total + else: + raise Exception("matrix is not square") + + def __mul__(self, other): + """ + implements the matrix-vector multiplication. + implements the matrix-scalar multiplication + """ + if isinstance(other, Vector): # vector-matrix + if len(other) == self.__width: + ans = zeroVector(self.__height) + for i in range(self.__height): + summe = 0 + for j in range(self.__width): + summe += other.component(j) * self.__matrix[i][j] + ans.changeComponent(i, summe) + summe = 0 + return ans + else: + raise Exception( + "vector must have the same size as the " + + "number of columns of the matrix!" + ) + elif isinstance(other, int) or isinstance(other, float): # matrix-scalar + matrix = [ + [self.__matrix[i][j] * other for j in range(self.__width)] + for i in range(self.__height) + ] + return Matrix(matrix, self.__width, self.__height) + + def __add__(self, other): + """ + implements the matrix-addition. + """ + if self.__width == other.width() and self.__height == other.height(): + matrix = [] + for i in range(self.__height): + row = [] + for j in range(self.__width): + row.append(self.__matrix[i][j] + other.component(i, j)) + matrix.append(row) + return Matrix(matrix, self.__width, self.__height) + else: + raise Exception("matrix must have the same dimension!") + + def __sub__(self, other): + """ + implements the matrix-subtraction. + """ + if self.__width == other.width() and self.__height == other.height(): + matrix = [] + for i in range(self.__height): + row = [] + for j in range(self.__width): + row.append(self.__matrix[i][j] - other.component(i, j)) + matrix.append(row) + return Matrix(matrix, self.__width, self.__height) + else: + raise Exception("matrix must have the same dimension!") + + +def squareZeroMatrix(N): + """ + returns a square zero-matrix of dimension NxN + """ + ans = [[0] * N for i in range(N)] + return Matrix(ans, N, N) + + +def randomMatrix(W, H, a, b): + """ + returns a random matrix WxH with integer components + between 'a' and 'b' + """ + random.seed(None) + matrix = [[random.randint(a, b) for j in range(W)] for i in range(H)] + return Matrix(matrix, W, H) diff --git a/linear_algebra/src/polynom_for_points.py b/linear_algebra/src/polynom_for_points.py new file mode 100644 index 000000000000..7a363723d9d2 --- /dev/null +++ b/linear_algebra/src/polynom_for_points.py @@ -0,0 +1,132 @@ +from __future__ import annotations + + +def points_to_polynomial(coordinates: list[list[int]]) -> str: + """ + coordinates is a two dimensional matrix: [[x, y], [x, y], ...] + number of points you want to use + + >>> print(points_to_polynomial([])) + The program cannot work out a fitting polynomial. + >>> print(points_to_polynomial([[]])) + The program cannot work out a fitting polynomial. + >>> print(points_to_polynomial([[1, 0], [2, 0], [3, 0]])) + f(x)=x^2*0.0+x^1*-0.0+x^0*0.0 + >>> print(points_to_polynomial([[1, 1], [2, 1], [3, 1]])) + f(x)=x^2*0.0+x^1*-0.0+x^0*1.0 + >>> print(points_to_polynomial([[1, 3], [2, 3], [3, 3]])) + f(x)=x^2*0.0+x^1*-0.0+x^0*3.0 + >>> print(points_to_polynomial([[1, 1], [2, 2], [3, 3]])) + f(x)=x^2*0.0+x^1*1.0+x^0*0.0 + >>> print(points_to_polynomial([[1, 1], [2, 4], [3, 9]])) + f(x)=x^2*1.0+x^1*-0.0+x^0*0.0 + >>> print(points_to_polynomial([[1, 3], [2, 6], [3, 11]])) + f(x)=x^2*1.0+x^1*-0.0+x^0*2.0 + >>> print(points_to_polynomial([[1, -3], [2, -6], [3, -11]])) + f(x)=x^2*-1.0+x^1*-0.0+x^0*-2.0 + >>> print(points_to_polynomial([[1, 5], [2, 2], [3, 9]])) + f(x)=x^2*5.0+x^1*-18.0+x^0*18.0 + """ + try: + check = 1 + more_check = 0 + d = coordinates[0][0] + for j in range(len(coordinates)): + if j == 0: + continue + if d == coordinates[j][0]: + more_check += 1 + solved = "x=" + str(coordinates[j][0]) + if more_check == len(coordinates) - 1: + check = 2 + break + elif more_check > 0 and more_check != len(coordinates) - 1: + check = 3 + else: + check = 1 + + if len(coordinates) == 1 and coordinates[0][0] == 0: + check = 2 + solved = "x=0" + except Exception: + check = 3 + + x = len(coordinates) + + if check == 1: + count_of_line = 0 + matrix = [] + # put the x and x to the power values in a matrix + while count_of_line < x: + count_in_line = 0 + a = coordinates[count_of_line][0] + count_line: list[int] = [] + while count_in_line < x: + count_line.append(a ** (x - (count_in_line + 1))) + count_in_line += 1 + matrix.append(count_line) + count_of_line += 1 + + count_of_line = 0 + # put the y values into a vector + vector: list[int] = [] + while count_of_line < x: + vector.append(coordinates[count_of_line][1]) + count_of_line += 1 + + count = 0 + + while count < x: + zahlen = 0 + while zahlen < x: + if count == zahlen: + zahlen += 1 + if zahlen == x: + break + bruch = matrix[zahlen][count] / matrix[count][count] + for counting_columns, item in enumerate(matrix[count]): + # manipulating all the values in the matrix + matrix[zahlen][counting_columns] -= item * bruch + # manipulating the values in the vector + vector[zahlen] -= vector[count] * bruch + zahlen += 1 + count += 1 + + count = 0 + # make solutions + solution: list[str] = [] + while count < x: + solution.append(vector[count] / matrix[count][count]) + count += 1 + + count = 0 + solved = "f(x)=" + + while count < x: + remove_e: list[str] = str(solution[count]).split("E") + if len(remove_e) > 1: + solution[count] = remove_e[0] + "*10^" + remove_e[1] + solved += "x^" + str(x - (count + 1)) + "*" + str(solution[count]) + if count + 1 != x: + solved += "+" + count += 1 + + return solved + + elif check == 2: + return solved + else: + return "The program cannot work out a fitting polynomial." + + +if __name__ == "__main__": + print(points_to_polynomial([])) + print(points_to_polynomial([[]])) + print(points_to_polynomial([[1, 0], [2, 0], [3, 0]])) + print(points_to_polynomial([[1, 1], [2, 1], [3, 1]])) + print(points_to_polynomial([[1, 3], [2, 3], [3, 3]])) + print(points_to_polynomial([[1, 1], [2, 2], [3, 3]])) + print(points_to_polynomial([[1, 1], [2, 4], [3, 9]])) + print(points_to_polynomial([[1, 3], [2, 6], [3, 11]])) + print(points_to_polynomial([[1, -3], [2, -6], [3, -11]])) + print(points_to_polynomial([[1, 5], [2, 2], [3, 9]])) diff --git a/linear_algebra/src/power_iteration.py b/linear_algebra/src/power_iteration.py new file mode 100644 index 000000000000..476361e0d433 --- /dev/null +++ b/linear_algebra/src/power_iteration.py @@ -0,0 +1,101 @@ +import numpy as np + + +def power_iteration( + input_matrix: np.array, vector: np.array, error_tol=1e-12, max_iterations=100 +) -> [float, np.array]: + """ + Power Iteration. + Find the largest eignevalue and corresponding eigenvector + of matrix input_matrix given a random vector in the same space. + Will work so long as vector has component of largest eigenvector. + input_matrix must be symmetric. + + Input + input_matrix: input matrix whose largest eigenvalue we will find. + Numpy array. np.shape(input_matrix) == (N,N). + vector: random initial vector in same space as matrix. + Numpy array. np.shape(vector) == (N,) or (N,1) + + Output + largest_eigenvalue: largest eigenvalue of the matrix input_matrix. + Float. Scalar. + largest_eigenvector: eigenvector corresponding to largest_eigenvalue. + Numpy array. np.shape(largest_eigenvector) == (N,) or (N,1). + + >>> import numpy as np + >>> input_matrix = np.array([ + ... [41, 4, 20], + ... [ 4, 26, 30], + ... [20, 30, 50] + ... ]) + >>> vector = np.array([41,4,20]) + >>> power_iteration(input_matrix,vector) + (79.66086378788381, array([0.44472726, 0.46209842, 0.76725662])) + """ + + # Ensure matrix is square. + assert np.shape(input_matrix)[0] == np.shape(input_matrix)[1] + # Ensure proper dimensionality. + assert np.shape(input_matrix)[0] == np.shape(vector)[0] + + # Set convergence to False. Will define convergence when we exceed max_iterations + # or when we have small changes from one iteration to next. + + convergence = False + lamda_previous = 0 + iterations = 0 + error = 1e12 + + while not convergence: + # Multiple matrix by the vector. + w = np.dot(input_matrix, vector) + # Normalize the resulting output vector. + vector = w / np.linalg.norm(w) + # Find rayleigh quotient + # (faster than usual b/c we know vector is normalized already) + lamda = np.dot(vector.T, np.dot(input_matrix, vector)) + + # Check convergence. + error = np.abs(lamda - lamda_previous) / lamda + iterations += 1 + + if error <= error_tol or iterations >= max_iterations: + convergence = True + + lamda_previous = lamda + + return lamda, vector + + +def test_power_iteration() -> None: + """ + >>> test_power_iteration() # self running tests + """ + # Our implementation. + input_matrix = np.array([[41, 4, 20], [4, 26, 30], [20, 30, 50]]) + vector = np.array([41, 4, 20]) + eigen_value, eigen_vector = power_iteration(input_matrix, vector) + + # Numpy implementation. + + # Get eigen values and eigen vectors using built in numpy + # eigh (eigh used for symmetric or hermetian matrices). + eigen_values, eigen_vectors = np.linalg.eigh(input_matrix) + # Last eigen value is the maximum one. + eigen_value_max = eigen_values[-1] + # Last column in this matrix is eigen vector corresponding to largest eigen value. + eigen_vector_max = eigen_vectors[:, -1] + + # Check our implementation and numpy gives close answers. + assert np.abs(eigen_value - eigen_value_max) <= 1e-6 + # Take absolute values element wise of each eigenvector. + # as they are only unique to a minus sign. + assert np.linalg.norm(np.abs(eigen_vector) - np.abs(eigen_vector_max)) <= 1e-6 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + test_power_iteration() diff --git a/linear_algebra/src/rayleigh_quotient.py b/linear_algebra/src/rayleigh_quotient.py new file mode 100644 index 000000000000..69bbbac119e8 --- /dev/null +++ b/linear_algebra/src/rayleigh_quotient.py @@ -0,0 +1,64 @@ +""" +https://en.wikipedia.org/wiki/Rayleigh_quotient +""" +import numpy as np + + +def is_hermitian(matrix: np.array) -> bool: + """ + Checks if a matrix is Hermitian. + >>> import numpy as np + >>> A = np.array([ + ... [2, 2+1j, 4], + ... [2-1j, 3, 1j], + ... [4, -1j, 1]]) + >>> is_hermitian(A) + True + >>> A = np.array([ + ... [2, 2+1j, 4+1j], + ... [2-1j, 3, 1j], + ... [4, -1j, 1]]) + >>> is_hermitian(A) + False + """ + return np.array_equal(matrix, matrix.conjugate().T) + + +def rayleigh_quotient(A: np.array, v: np.array) -> float: + """ + Returns the Rayleigh quotient of a Hermitian matrix A and + vector v. + >>> import numpy as np + >>> A = np.array([ + ... [1, 2, 4], + ... [2, 3, -1], + ... [4, -1, 1] + ... ]) + >>> v = np.array([ + ... [1], + ... [2], + ... [3] + ... ]) + >>> rayleigh_quotient(A, v) + array([[3.]]) + """ + v_star = v.conjugate().T + return (v_star.dot(A).dot(v)) / (v_star.dot(v)) + + +def tests() -> None: + A = np.array([[2, 2 + 1j, 4], [2 - 1j, 3, 1j], [4, -1j, 1]]) + v = np.array([[1], [2], [3]]) + assert is_hermitian(A), f"{A} is not hermitian." + print(rayleigh_quotient(A, v)) + + A = np.array([[1, 2, 4], [2, 3, -1], [4, -1, 1]]) + assert is_hermitian(A), f"{A} is not hermitian." + assert rayleigh_quotient(A, v) == float(3) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + tests() diff --git a/linear_algebra/src/test_linear_algebra.py b/linear_algebra/src/test_linear_algebra.py new file mode 100644 index 000000000000..6eba3a1638bd --- /dev/null +++ b/linear_algebra/src/test_linear_algebra.py @@ -0,0 +1,156 @@ +""" +Created on Mon Feb 26 15:40:07 2018 + +@author: Christian Bender +@license: MIT-license + +This file contains the test-suite for the linear algebra library. +""" +import unittest + +from .lib import Matrix, Vector, axpy, squareZeroMatrix, unitBasisVector, zeroVector + + +class Test(unittest.TestCase): + def test_component(self): + """ + test for method component + """ + x = Vector([1, 2, 3]) + self.assertEqual(x.component(0), 1) + self.assertEqual(x.component(2), 3) + _ = Vector() + + def test_str(self): + """ + test for toString() method + """ + x = Vector([0, 0, 0, 0, 0, 1]) + self.assertEqual(str(x), "(0,0,0,0,0,1)") + + def test_size(self): + """ + test for size()-method + """ + x = Vector([1, 2, 3, 4]) + self.assertEqual(len(x), 4) + + def test_euclidLength(self): + """ + test for the eulidean length + """ + x = Vector([1, 2]) + self.assertAlmostEqual(x.euclidLength(), 2.236, 3) + + def test_add(self): + """ + test for + operator + """ + x = Vector([1, 2, 3]) + y = Vector([1, 1, 1]) + self.assertEqual((x + y).component(0), 2) + self.assertEqual((x + y).component(1), 3) + self.assertEqual((x + y).component(2), 4) + + def test_sub(self): + """ + test for - operator + """ + x = Vector([1, 2, 3]) + y = Vector([1, 1, 1]) + self.assertEqual((x - y).component(0), 0) + self.assertEqual((x - y).component(1), 1) + self.assertEqual((x - y).component(2), 2) + + def test_mul(self): + """ + test for * operator + """ + x = Vector([1, 2, 3]) + a = Vector([2, -1, 4]) # for test of dot-product + b = Vector([1, -2, -1]) + self.assertEqual(str(x * 3.0), "(3.0,6.0,9.0)") + self.assertEqual((a * b), 0) + + def test_zeroVector(self): + """ + test for the global function zeroVector(...) + """ + self.assertTrue(str(zeroVector(10)).count("0") == 10) + + def test_unitBasisVector(self): + """ + test for the global function unitBasisVector(...) + """ + self.assertEqual(str(unitBasisVector(3, 1)), "(0,1,0)") + + def test_axpy(self): + """ + test for the global function axpy(...) (operation) + """ + x = Vector([1, 2, 3]) + y = Vector([1, 0, 1]) + self.assertEqual(str(axpy(2, x, y)), "(3,4,7)") + + def test_copy(self): + """ + test for the copy()-method + """ + x = Vector([1, 0, 0, 0, 0, 0]) + y = x.copy() + self.assertEqual(str(x), str(y)) + + def test_changeComponent(self): + """ + test for the changeComponent(...)-method + """ + x = Vector([1, 0, 0]) + x.changeComponent(0, 0) + x.changeComponent(1, 1) + self.assertEqual(str(x), "(0,1,0)") + + def test_str_matrix(self): + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(A)) + + def test_determinate(self): + """ + test for determinate() + """ + A = Matrix([[1, 1, 4, 5], [3, 3, 3, 2], [5, 1, 9, 0], [9, 7, 7, 9]], 4, 4) + self.assertEqual(-376, A.determinate()) + + def test__mul__matrix(self): + A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3) + x = Vector([1, 2, 3]) + self.assertEqual("(14,32,50)", str(A * x)) + self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n", str(A * 2)) + + def test_changeComponent_matrix(self): + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + A.changeComponent(0, 2, 5) + self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n", str(A)) + + def test_component_matrix(self): + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + self.assertEqual(7, A.component(2, 1), 0.01) + + def test__add__matrix(self): + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + B = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3) + self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n", str(A + B)) + + def test__sub__matrix(self): + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + B = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3) + self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n", str(A - B)) + + def test_squareZeroMatrix(self): + self.assertEqual( + "|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|" + "\n|0,0,0,0,0|\n", + str(squareZeroMatrix(5)), + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/linear_algebra/src/transformations_2d.py b/linear_algebra/src/transformations_2d.py new file mode 100644 index 000000000000..6a15189c5676 --- /dev/null +++ b/linear_algebra/src/transformations_2d.py @@ -0,0 +1,63 @@ +""" +2D Transformations are regularly used in Linear Algebra. + +I have added the codes for reflection, projection, scaling and rotation 2D matrices. + + scaling(5) = [[5.0, 0.0], [0.0, 5.0]] + rotation(45) = [[0.5253219888177297, -0.8509035245341184], + [0.8509035245341184, 0.5253219888177297]] +projection(45) = [[0.27596319193541496, 0.446998331800279], + [0.446998331800279, 0.7240368080645851]] +reflection(45) = [[0.05064397763545947, 0.893996663600558], + [0.893996663600558, 0.7018070490682369]] +""" +from __future__ import annotations + +from math import cos, sin + + +def scaling(scaling_factor: float) -> list[list[float]]: + """ + >>> scaling(5) + [[5.0, 0.0], [0.0, 5.0]] + """ + scaling_factor = float(scaling_factor) + return [[scaling_factor * int(x == y) for x in range(2)] for y in range(2)] + + +def rotation(angle: float) -> list[list[float]]: + """ + >>> rotation(45) # doctest: +NORMALIZE_WHITESPACE + [[0.5253219888177297, -0.8509035245341184], + [0.8509035245341184, 0.5253219888177297]] + """ + c, s = cos(angle), sin(angle) + return [[c, -s], [s, c]] + + +def projection(angle: float) -> list[list[float]]: + """ + >>> projection(45) # doctest: +NORMALIZE_WHITESPACE + [[0.27596319193541496, 0.446998331800279], + [0.446998331800279, 0.7240368080645851]] + """ + c, s = cos(angle), sin(angle) + cs = c * s + return [[c * c, cs], [cs, s * s]] + + +def reflection(angle: float) -> list[list[float]]: + """ + >>> reflection(45) # doctest: +NORMALIZE_WHITESPACE + [[0.05064397763545947, 0.893996663600558], + [0.893996663600558, 0.7018070490682369]] + """ + c, s = cos(angle), sin(angle) + cs = c * s + return [[2 * c - 1, 2 * cs], [2 * cs, 2 * s - 1]] + + +print(f" {scaling(5) = }") +print(f" {rotation(45) = }") +print(f"{projection(45) = }") +print(f"{reflection(45) = }") diff --git a/linear_algebra_python/README.md b/linear_algebra_python/README.md deleted file mode 100644 index 1e34d0bd7805..000000000000 --- a/linear_algebra_python/README.md +++ /dev/null @@ -1,76 +0,0 @@ -# Linear algebra library for Python - -This module contains some useful classes and functions for dealing with linear algebra in python 2. - ---- - -## Overview - -- class Vector - - This class represents a vector of arbitray size and operations on it. - - **Overview about the methods:** - - - constructor(components : list) : init the vector - - set(components : list) : changes the vector components. - - \_\_str\_\_() : toString method - - component(i : int): gets the i-th component (start by 0) - - \_\_len\_\_() : gets the size / length of the vector (number of components) - - euclidLength() : returns the eulidean length of the vector. - - operator + : vector addition - - operator - : vector subtraction - - operator * : scalar multiplication and dot product - - copy() : copies this vector and returns it. - - changeComponent(pos,value) : changes the specified component. - -- function zeroVector(dimension) - - returns a zero vector of 'dimension' -- function unitBasisVector(dimension,pos) - - returns a unit basis vector with a One at index 'pos' (indexing at 0) -- function axpy(scalar,vector1,vector2) - - computes the axpy operation -- function randomVector(N,a,b) - - returns a random vector of size N, with random integer components between 'a' and 'b'. - -- class Matrix - - This class represents a matrix of arbitrary size and operations on it. - - **Overview about the methods:** - - - \_\_str\_\_() : returns a string representation - - operator * : implements the matrix vector multiplication - implements the matrix-scalar multiplication. - - changeComponent(x,y,value) : changes the specified component. - - component(x,y) : returns the specified component. - - width() : returns the width of the matrix - - height() : returns the height of the matrix - - operator + : implements the matrix-addition. - - operator - _ implements the matrix-subtraction - -- function squareZeroMatrix(N) - - returns a square zero-matrix of dimension NxN -- function randomMatrix(W,H,a,b) - - returns a random matrix WxH with integer components between 'a' and 'b' ---- - -## Documentation - -The module is well documented. You can use the python in-built ```help(...)``` function. -For instance: ```help(Vector)``` gives you all information about the Vector-class. -Or ```help(unitBasisVector)``` gives you all information you needed about the -global function ```unitBasisVector(...)```. If you need informations about a certain -method you type ```help(CLASSNAME.METHODNAME)```. - ---- - -## Usage - -You will find the module in the **src** directory its called ```lib.py```. You need to -import this module in your project. Alternative you can also use the file ```lib.pyc``` in python-bytecode. - ---- - -## Tests - -In the **src** directory you also find the test-suite, its called ```tests.py```. -The test-suite uses the built-in python-test-framework **unittest**. diff --git a/linear_algebra_python/src/lib.py b/linear_algebra_python/src/lib.py deleted file mode 100644 index 281991a93b2d..000000000000 --- a/linear_algebra_python/src/lib.py +++ /dev/null @@ -1,323 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Mon Feb 26 14:29:11 2018 - -@author: Christian Bender -@license: MIT-license - -This module contains some useful classes and functions for dealing -with linear algebra in python. - -Overview: - -- class Vector -- function zeroVector(dimension) -- function unitBasisVector(dimension,pos) -- function axpy(scalar,vector1,vector2) -- function randomVector(N,a,b) -- class Matrix -- function squareZeroMatrix(N) -- function randomMatrix(W,H,a,b) -""" - - -import math -import random - - -class Vector(object): - """ - This class represents a vector of arbitray size. - You need to give the vector components. - - Overview about the methods: - - constructor(components : list) : init the vector - set(components : list) : changes the vector components. - __str__() : toString method - component(i : int): gets the i-th component (start by 0) - __len__() : gets the size of the vector (number of components) - euclidLength() : returns the eulidean length of the vector. - operator + : vector addition - operator - : vector subtraction - operator * : scalar multiplication and dot product - copy() : copies this vector and returns it. - changeComponent(pos,value) : changes the specified component. - TODO: compare-operator - """ - def __init__(self,components=[]): - """ - input: components or nothing - simple constructor for init the vector - """ - self.__components = list(components) - def set(self,components): - """ - input: new components - changes the components of the vector. - replace the components with newer one. - """ - if len(components) > 0: - self.__components = list(components) - else: - raise Exception("please give any vector") - def __str__(self): - """ - returns a string representation of the vector - """ - return "(" + ",".join(map(str, self.__components)) + ")" - def component(self,i): - """ - input: index (start at 0) - output: the i-th component of the vector. - """ - if type(i) is int and -len(self.__components) <= i < len(self.__components) : - return self.__components[i] - else: - raise Exception("index out of range") - def __len__(self): - """ - returns the size of the vector - """ - return len(self.__components) - def eulidLength(self): - """ - returns the eulidean length of the vector - """ - summe = 0 - for c in self.__components: - summe += c**2 - return math.sqrt(summe) - def __add__(self,other): - """ - input: other vector - assumes: other vector has the same size - returns a new vector that represents the sum. - """ - size = len(self) - if size == len(other): - result = [self.__components[i] + other.component(i) for i in range(size)] - return Vector(result) - else: - raise Exception("must have the same size") - def __sub__(self,other): - """ - input: other vector - assumes: other vector has the same size - returns a new vector that represents the differenz. - """ - size = len(self) - if size == len(other): - result = [self.__components[i] - other.component(i) for i in range(size)] - return result - else: # error case - raise Exception("must have the same size") - def __mul__(self,other): - """ - mul implements the scalar multiplication - and the dot-product - """ - if isinstance(other,float) or isinstance(other,int): - ans = [c*other for c in self.__components] - return ans - elif (isinstance(other,Vector) and (len(self) == len(other))): - size = len(self) - summe = 0 - for i in range(size): - summe += self.__components[i] * other.component(i) - return summe - else: # error case - raise Exception("invalide operand!") - def copy(self): - """ - copies this vector and returns it. - """ - return Vector(self.__components) - def changeComponent(self,pos,value): - """ - input: an index (pos) and a value - changes the specified component (pos) with the - 'value' - """ - #precondition - assert (-len(self.__components) <= pos < len(self.__components)) - self.__components[pos] = value - -def zeroVector(dimension): - """ - returns a zero-vector of size 'dimension' - """ - #precondition - assert(isinstance(dimension,int)) - return Vector([0]*dimension) - - -def unitBasisVector(dimension,pos): - """ - returns a unit basis vector with a One - at index 'pos' (indexing at 0) - """ - #precondition - assert(isinstance(dimension,int) and (isinstance(pos,int))) - ans = [0]*dimension - ans[pos] = 1 - return Vector(ans) - - -def axpy(scalar,x,y): - """ - input: a 'scalar' and two vectors 'x' and 'y' - output: a vector - computes the axpy operation - """ - # precondition - assert(isinstance(x,Vector) and (isinstance(y,Vector)) \ - and (isinstance(scalar,int) or isinstance(scalar,float))) - return (x*scalar + y) - - -def randomVector(N,a,b): - """ - input: size (N) of the vector. - random range (a,b) - output: returns a random vector of size N, with - random integer components between 'a' and 'b'. - """ - random.seed(None) - ans = [random.randint(a,b) for i in range(N)] - return Vector(ans) - - -class Matrix(object): - """ - class: Matrix - This class represents a arbitrary matrix. - - Overview about the methods: - - __str__() : returns a string representation - operator * : implements the matrix vector multiplication - implements the matrix-scalar multiplication. - changeComponent(x,y,value) : changes the specified component. - component(x,y) : returns the specified component. - width() : returns the width of the matrix - height() : returns the height of the matrix - operator + : implements the matrix-addition. - operator - _ implements the matrix-subtraction - """ - def __init__(self,matrix,w,h): - """ - simple constructor for initialzes - the matrix with components. - """ - self.__matrix = matrix - self.__width = w - self.__height = h - def __str__(self): - """ - returns a string representation of this - matrix. - """ - ans = "" - for i in range(self.__height): - ans += "|" - for j in range(self.__width): - if j < self.__width -1: - ans += str(self.__matrix[i][j]) + "," - else: - ans += str(self.__matrix[i][j]) + "|\n" - return ans - def changeComponent(self,x,y, value): - """ - changes the x-y component of this matrix - """ - if x >= 0 and x < self.__height and y >= 0 and y < self.__width: - self.__matrix[x][y] = value - else: - raise Exception ("changeComponent: indices out of bounds") - def component(self,x,y): - """ - returns the specified (x,y) component - """ - if x >= 0 and x < self.__height and y >= 0 and y < self.__width: - return self.__matrix[x][y] - else: - raise Exception ("changeComponent: indices out of bounds") - def width(self): - """ - getter for the width - """ - return self.__width - def height(self): - """ - getter for the height - """ - return self.__height - def __mul__(self,other): - """ - implements the matrix-vector multiplication. - implements the matrix-scalar multiplication - """ - if isinstance(other, Vector): # vector-matrix - if (len(other) == self.__width): - ans = zeroVector(self.__height) - for i in range(self.__height): - summe = 0 - for j in range(self.__width): - summe += other.component(j) * self.__matrix[i][j] - ans.changeComponent(i,summe) - summe = 0 - return ans - else: - raise Exception("vector must have the same size as the " + "number of columns of the matrix!") - elif isinstance(other,int) or isinstance(other,float): # matrix-scalar - matrix = [[self.__matrix[i][j] * other for j in range(self.__width)] for i in range(self.__height)] - return Matrix(matrix,self.__width,self.__height) - def __add__(self,other): - """ - implements the matrix-addition. - """ - if (self.__width == other.width() and self.__height == other.height()): - matrix = [] - for i in range(self.__height): - row = [] - for j in range(self.__width): - row.append(self.__matrix[i][j] + other.component(i,j)) - matrix.append(row) - return Matrix(matrix,self.__width,self.__height) - else: - raise Exception("matrix must have the same dimension!") - def __sub__(self,other): - """ - implements the matrix-subtraction. - """ - if (self.__width == other.width() and self.__height == other.height()): - matrix = [] - for i in range(self.__height): - row = [] - for j in range(self.__width): - row.append(self.__matrix[i][j] - other.component(i,j)) - matrix.append(row) - return Matrix(matrix,self.__width,self.__height) - else: - raise Exception("matrix must have the same dimension!") - - -def squareZeroMatrix(N): - """ - returns a square zero-matrix of dimension NxN - """ - ans = [[0]*N for i in range(N)] - return Matrix(ans,N,N) - - -def randomMatrix(W,H,a,b): - """ - returns a random matrix WxH with integer components - between 'a' and 'b' - """ - random.seed(None) - matrix = [[random.randint(a,b) for j in range(W)] for i in range(H)] - return Matrix(matrix,W,H) - - diff --git a/linear_algebra_python/src/tests.py b/linear_algebra_python/src/tests.py deleted file mode 100644 index a26eb92653e2..000000000000 --- a/linear_algebra_python/src/tests.py +++ /dev/null @@ -1,133 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Mon Feb 26 15:40:07 2018 - -@author: Christian Bender -@license: MIT-license - -This file contains the test-suite for the linear algebra library. -""" - -import unittest -from lib import * - -class Test(unittest.TestCase): - def test_component(self): - """ - test for method component - """ - x = Vector([1,2,3]) - self.assertEqual(x.component(0),1) - self.assertEqual(x.component(2),3) - try: - y = Vector() - self.assertTrue(False) - except: - self.assertTrue(True) - def test_str(self): - """ - test for toString() method - """ - x = Vector([0,0,0,0,0,1]) - self.assertEqual(str(x),"(0,0,0,0,0,1)") - def test_size(self): - """ - test for size()-method - """ - x = Vector([1,2,3,4]) - self.assertEqual(len(x),4) - def test_euclidLength(self): - """ - test for the eulidean length - """ - x = Vector([1,2]) - self.assertAlmostEqual(x.eulidLength(),2.236,3) - def test_add(self): - """ - test for + operator - """ - x = Vector([1,2,3]) - y = Vector([1,1,1]) - self.assertEqual((x+y).component(0),2) - self.assertEqual((x+y).component(1),3) - self.assertEqual((x+y).component(2),4) - def test_sub(self): - """ - test for - operator - """ - x = Vector([1,2,3]) - y = Vector([1,1,1]) - self.assertEqual((x-y).component(0),0) - self.assertEqual((x-y).component(1),1) - self.assertEqual((x-y).component(2),2) - def test_mul(self): - """ - test for * operator - """ - x = Vector([1,2,3]) - a = Vector([2,-1,4]) # for test of dot-product - b = Vector([1,-2,-1]) - self.assertEqual(str(x*3.0),"(3.0,6.0,9.0)") - self.assertEqual((a*b),0) - def test_zeroVector(self): - """ - test for the global function zeroVector(...) - """ - self.assertTrue(str(zeroVector(10)).count("0") == 10) - def test_unitBasisVector(self): - """ - test for the global function unitBasisVector(...) - """ - self.assertEqual(str(unitBasisVector(3,1)),"(0,1,0)") - def test_axpy(self): - """ - test for the global function axpy(...) (operation) - """ - x = Vector([1,2,3]) - y = Vector([1,0,1]) - self.assertEqual(str(axpy(2,x,y)),"(3,4,7)") - def test_copy(self): - """ - test for the copy()-method - """ - x = Vector([1,0,0,0,0,0]) - y = x.copy() - self.assertEqual(str(x),str(y)) - def test_changeComponent(self): - """ - test for the changeComponent(...)-method - """ - x = Vector([1,0,0]) - x.changeComponent(0,0) - x.changeComponent(1,1) - self.assertEqual(str(x),"(0,1,0)") - def test_str_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n",str(A)) - def test__mul__matrix(self): - A = Matrix([[1,2,3],[4,5,6],[7,8,9]],3,3) - x = Vector([1,2,3]) - self.assertEqual("(14,32,50)",str(A*x)) - self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n",str(A*2)) - def test_changeComponent_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - A.changeComponent(0,2,5) - self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n",str(A)) - def test_component_matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - self.assertEqual(7,A.component(2,1),0.01) - def test__add__matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - B = Matrix([[1,2,7],[2,4,5],[6,7,10]],3,3) - self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n",str(A+B)) - def test__sub__matrix(self): - A = Matrix([[1,2,3],[2,4,5],[6,7,8]],3,3) - B = Matrix([[1,2,7],[2,4,5],[6,7,10]],3,3) - self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n",str(A-B)) - def test_squareZeroMatrix(self): - self.assertEqual('|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|' - +'\n|0,0,0,0,0|\n',str(squareZeroMatrix(5))) - - -if __name__ == "__main__": - unittest.main() diff --git a/machine_learning/NaiveBayes.ipynb b/machine_learning/NaiveBayes.ipynb deleted file mode 100644 index 5a427c5cb965..000000000000 --- a/machine_learning/NaiveBayes.ipynb +++ /dev/null @@ -1,1659 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "from sklearn import datasets\n", - "import pandas as pd" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "iris = datasets.load_iris()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "df = pd.DataFrame(iris.data)\n", - "df.columns = [\"sl\", \"sw\", 'pl', 'pw']" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def abc(k, *val):\n", - " if k < val[0]:\n", - " return 0\n", - " else:\n", - " return 1" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0 1\n", - "1 0\n", - "2 0\n", - "3 0\n", - "4 1\n", - "5 1\n", - "6 0\n", - "7 1\n", - "8 0\n", - "9 0\n", - "10 1\n", - "11 0\n", - "12 0\n", - "13 0\n", - "14 1\n", - "15 1\n", - "16 1\n", - "17 1\n", - "18 1\n", - "19 1\n", - "20 1\n", - "21 1\n", - "22 0\n", - "23 1\n", - "24 0\n", - "25 1\n", - "26 1\n", - "27 1\n", - "28 1\n", - "29 0\n", - " ..\n", - "120 1\n", - "121 1\n", - "122 1\n", - "123 1\n", - "124 1\n", - "125 1\n", - "126 1\n", - "127 1\n", - "128 1\n", - "129 1\n", - "130 1\n", - "131 1\n", - "132 1\n", - "133 1\n", - "134 1\n", - "135 1\n", - "136 1\n", - "137 1\n", - "138 1\n", - "139 1\n", - "140 1\n", - "141 1\n", - "142 1\n", - "143 1\n", - "144 1\n", - "145 1\n", - "146 1\n", - "147 1\n", - "148 1\n", - "149 1\n", - "Name: sl, dtype: int64" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "df.sl.apply(abc, args=(5,))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "def label(val, *boundaries):\n", - " if (val < boundaries[0]):\n", - " return 'a'\n", - " elif (val < boundaries[1]):\n", - " return 'b'\n", - " elif (val < boundaries[2]):\n", - " return 'c'\n", - " else:\n", - " return 'd'\n", - "\n", - "def toLabel(df, old_feature_name):\n", - " second = df[old_feature_name].mean()\n", - " minimum = df[old_feature_name].min()\n", - " first = (minimum + second)/2\n", - " maximum = df[old_feature_name].max()\n", - " third = (maximum + second)/2\n", - " return df[old_feature_name].apply(label, args= (first, second, third))" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
slswplpwsl_labeledsw_labeledpl_labeledpw_labeled
05.13.51.40.2bcaa
14.93.01.40.2abaa
24.73.21.30.2acaa
34.63.11.50.2acaa
45.03.61.40.2acaa
55.43.91.70.4bdaa
64.63.41.40.3acaa
75.03.41.50.2acaa
84.42.91.40.2abaa
94.93.11.50.1acaa
105.43.71.50.2bcaa
114.83.41.60.2acaa
124.83.01.40.1abaa
134.33.01.10.1abaa
145.84.01.20.2bdaa
155.74.41.50.4bdaa
165.43.91.30.4bdaa
175.13.51.40.3bcaa
185.73.81.70.3bdaa
195.13.81.50.3bdaa
205.43.41.70.2bcaa
215.13.71.50.4bcaa
224.63.61.00.2acaa
235.13.31.70.5bcaa
244.83.41.90.2acaa
255.03.01.60.2abaa
265.03.41.60.4acaa
275.23.51.50.2bcaa
285.23.41.40.2bcaa
294.73.21.60.2acaa
...........................
1206.93.25.72.3dcdd
1215.62.84.92.0bbcd
1227.72.86.72.0dbdd
1236.32.74.91.8cbcc
1246.73.35.72.1ccdd
1257.23.26.01.8dcdc
1266.22.84.81.8cbcc
1276.13.04.91.8cbcc
1286.42.85.62.1cbdd
1297.23.05.81.6dbdc
1307.42.86.11.9dbdd
1317.93.86.42.0dddd
1326.42.85.62.2cbdd
1336.32.85.11.5cbcc
1346.12.65.61.4cbdc
1357.73.06.12.3dbdd
1366.33.45.62.4ccdd
1376.43.15.51.8ccdc
1386.03.04.81.8cbcc
1396.93.15.42.1dcdd
1406.73.15.62.4ccdd
1416.93.15.12.3dccd
1425.82.75.11.9bbcd
1436.83.25.92.3ccdd
1446.73.35.72.5ccdd
1456.73.05.22.3cbcd
1466.32.55.01.9cacd
1476.53.05.22.0cbcd
1486.23.45.42.3ccdd
1495.93.05.11.8cbcc
\n", - "

150 rows × 8 columns

\n", - "
" - ], - "text/plain": [ - " sl sw pl pw sl_labeled sw_labeled pl_labeled pw_labeled\n", - "0 5.1 3.5 1.4 0.2 b c a a\n", - "1 4.9 3.0 1.4 0.2 a b a a\n", - "2 4.7 3.2 1.3 0.2 a c a a\n", - "3 4.6 3.1 1.5 0.2 a c a a\n", - "4 5.0 3.6 1.4 0.2 a c a a\n", - "5 5.4 3.9 1.7 0.4 b d a a\n", - "6 4.6 3.4 1.4 0.3 a c a a\n", - "7 5.0 3.4 1.5 0.2 a c a a\n", - "8 4.4 2.9 1.4 0.2 a b a a\n", - "9 4.9 3.1 1.5 0.1 a c a a\n", - "10 5.4 3.7 1.5 0.2 b c a a\n", - "11 4.8 3.4 1.6 0.2 a c a a\n", - "12 4.8 3.0 1.4 0.1 a b a a\n", - "13 4.3 3.0 1.1 0.1 a b a a\n", - "14 5.8 4.0 1.2 0.2 b d a a\n", - "15 5.7 4.4 1.5 0.4 b d a a\n", - "16 5.4 3.9 1.3 0.4 b d a a\n", - "17 5.1 3.5 1.4 0.3 b c a a\n", - "18 5.7 3.8 1.7 0.3 b d a a\n", - "19 5.1 3.8 1.5 0.3 b d a a\n", - "20 5.4 3.4 1.7 0.2 b c a a\n", - "21 5.1 3.7 1.5 0.4 b c a a\n", - "22 4.6 3.6 1.0 0.2 a c a a\n", - "23 5.1 3.3 1.7 0.5 b c a a\n", - "24 4.8 3.4 1.9 0.2 a c a a\n", - "25 5.0 3.0 1.6 0.2 a b a a\n", - "26 5.0 3.4 1.6 0.4 a c a a\n", - "27 5.2 3.5 1.5 0.2 b c a a\n", - "28 5.2 3.4 1.4 0.2 b c a a\n", - "29 4.7 3.2 1.6 0.2 a c a a\n", - ".. ... ... ... ... ... ... ... ...\n", - "120 6.9 3.2 5.7 2.3 d c d d\n", - "121 5.6 2.8 4.9 2.0 b b c d\n", - "122 7.7 2.8 6.7 2.0 d b d d\n", - "123 6.3 2.7 4.9 1.8 c b c c\n", - "124 6.7 3.3 5.7 2.1 c c d d\n", - "125 7.2 3.2 6.0 1.8 d c d c\n", - "126 6.2 2.8 4.8 1.8 c b c c\n", - "127 6.1 3.0 4.9 1.8 c b c c\n", - "128 6.4 2.8 5.6 2.1 c b d d\n", - "129 7.2 3.0 5.8 1.6 d b d c\n", - "130 7.4 2.8 6.1 1.9 d b d d\n", - "131 7.9 3.8 6.4 2.0 d d d d\n", - "132 6.4 2.8 5.6 2.2 c b d d\n", - "133 6.3 2.8 5.1 1.5 c b c c\n", - "134 6.1 2.6 5.6 1.4 c b d c\n", - "135 7.7 3.0 6.1 2.3 d b d d\n", - "136 6.3 3.4 5.6 2.4 c c d d\n", - "137 6.4 3.1 5.5 1.8 c c d c\n", - "138 6.0 3.0 4.8 1.8 c b c c\n", - "139 6.9 3.1 5.4 2.1 d c d d\n", - "140 6.7 3.1 5.6 2.4 c c d d\n", - "141 6.9 3.1 5.1 2.3 d c c d\n", - "142 5.8 2.7 5.1 1.9 b b c d\n", - "143 6.8 3.2 5.9 2.3 c c d d\n", - "144 6.7 3.3 5.7 2.5 c c d d\n", - "145 6.7 3.0 5.2 2.3 c b c d\n", - "146 6.3 2.5 5.0 1.9 c a c d\n", - "147 6.5 3.0 5.2 2.0 c b c d\n", - "148 6.2 3.4 5.4 2.3 c c d d\n", - "149 5.9 3.0 5.1 1.8 c b c c\n", - "\n", - "[150 rows x 8 columns]" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "df['sl_labeled'] = toLabel(df, 'sl')\n", - "df['sw_labeled'] = toLabel(df, 'sw')\n", - "df['pl_labeled'] = toLabel(df, 'pl')\n", - "df['pw_labeled'] = toLabel(df, 'pw')\n", - "df" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "df.drop(['sl', 'sw', 'pl', 'pw'], axis = 1, inplace = True)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'a', 'b', 'c', 'd'}" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "set(df['sl_labeled'])" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "df[\"output\"] = iris.target" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
sl_labeledsw_labeledpl_labeledpw_labeledoutput
0bcaa0
1abaa0
2acaa0
3acaa0
4acaa0
5bdaa0
6acaa0
7acaa0
8abaa0
9acaa0
10bcaa0
11acaa0
12abaa0
13abaa0
14bdaa0
15bdaa0
16bdaa0
17bcaa0
18bdaa0
19bdaa0
20bcaa0
21bcaa0
22acaa0
23bcaa0
24acaa0
25abaa0
26acaa0
27bcaa0
28bcaa0
29acaa0
..................
120dcdd2
121bbcd2
122dbdd2
123cbcc2
124ccdd2
125dcdc2
126cbcc2
127cbcc2
128cbdd2
129dbdc2
130dbdd2
131dddd2
132cbdd2
133cbcc2
134cbdc2
135dbdd2
136ccdd2
137ccdc2
138cbcc2
139dcdd2
140ccdd2
141dccd2
142bbcd2
143ccdd2
144ccdd2
145cbcd2
146cacd2
147cbcd2
148ccdd2
149cbcc2
\n", - "

150 rows × 5 columns

\n", - "
" - ], - "text/plain": [ - " sl_labeled sw_labeled pl_labeled pw_labeled output\n", - "0 b c a a 0\n", - "1 a b a a 0\n", - "2 a c a a 0\n", - "3 a c a a 0\n", - "4 a c a a 0\n", - "5 b d a a 0\n", - "6 a c a a 0\n", - "7 a c a a 0\n", - "8 a b a a 0\n", - "9 a c a a 0\n", - "10 b c a a 0\n", - "11 a c a a 0\n", - "12 a b a a 0\n", - "13 a b a a 0\n", - "14 b d a a 0\n", - "15 b d a a 0\n", - "16 b d a a 0\n", - "17 b c a a 0\n", - "18 b d a a 0\n", - "19 b d a a 0\n", - "20 b c a a 0\n", - "21 b c a a 0\n", - "22 a c a a 0\n", - "23 b c a a 0\n", - "24 a c a a 0\n", - "25 a b a a 0\n", - "26 a c a a 0\n", - "27 b c a a 0\n", - "28 b c a a 0\n", - "29 a c a a 0\n", - ".. ... ... ... ... ...\n", - "120 d c d d 2\n", - "121 b b c d 2\n", - "122 d b d d 2\n", - "123 c b c c 2\n", - "124 c c d d 2\n", - "125 d c d c 2\n", - "126 c b c c 2\n", - "127 c b c c 2\n", - "128 c b d d 2\n", - "129 d b d c 2\n", - "130 d b d d 2\n", - "131 d d d d 2\n", - "132 c b d d 2\n", - "133 c b c c 2\n", - "134 c b d c 2\n", - "135 d b d d 2\n", - "136 c c d d 2\n", - "137 c c d c 2\n", - "138 c b c c 2\n", - "139 d c d d 2\n", - "140 c c d d 2\n", - "141 d c c d 2\n", - "142 b b c d 2\n", - "143 c c d d 2\n", - "144 c c d d 2\n", - "145 c b c d 2\n", - "146 c a c d 2\n", - "147 c b c d 2\n", - "148 c c d d 2\n", - "149 c b c c 2\n", - "\n", - "[150 rows x 5 columns]" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "df" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def fit(data):\n", - " output_name = data.columns[-1]\n", - " features = data.columns[0:-1]\n", - " counts = {}\n", - " possible_outputs = set(data[output_name])\n", - " for output in possible_outputs:\n", - " counts[output] = {}\n", - " smallData = data[data[output_name] == output]\n", - " counts[output][\"total_count\"] = len(smallData)\n", - " for f in features:\n", - " counts[output][f] = {}\n", - " possible_values = set(smallData[f])\n", - " for value in possible_values:\n", - " val_count = len(smallData[smallData[f] == value])\n", - " counts[output][f][value] = val_count\n", - " return counts" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{0: {'pl_labeled': {'a': 50},\n", - " 'pw_labeled': {'a': 50},\n", - " 'sl_labeled': {'a': 28, 'b': 22},\n", - " 'sw_labeled': {'a': 1, 'b': 7, 'c': 32, 'd': 10},\n", - " 'total_count': 50},\n", - " 1: {'pl_labeled': {'b': 7, 'c': 43},\n", - " 'pw_labeled': {'b': 10, 'c': 40},\n", - " 'sl_labeled': {'a': 3, 'b': 21, 'c': 24, 'd': 2},\n", - " 'sw_labeled': {'a': 13, 'b': 29, 'c': 8},\n", - " 'total_count': 50},\n", - " 2: {'pl_labeled': {'c': 20, 'd': 30},\n", - " 'pw_labeled': {'c': 16, 'd': 34},\n", - " 'sl_labeled': {'a': 1, 'b': 5, 'c': 29, 'd': 15},\n", - " 'sw_labeled': {'a': 5, 'b': 28, 'c': 15, 'd': 2},\n", - " 'total_count': 50}}" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "fit(df)" - ] - } - ], - "metadata": { - "anaconda-cloud": {}, - "kernelspec": { - "display_name": "Python [default]", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.5" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/machine_learning/Random Forest Classification/Random Forest Classifier.ipynb b/machine_learning/Random Forest Classification/Random Forest Classifier.ipynb deleted file mode 100644 index 7ee66124c371..000000000000 --- a/machine_learning/Random Forest Classification/Random Forest Classifier.ipynb +++ /dev/null @@ -1,196 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Users\\Satyam\\AppData\\Roaming\\Python\\Python35\\site-packages\\sklearn\\ensemble\\weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.\n", - " from numpy.core.umath_tests import inner1d\n" - ] - } - ], - "source": [ - "# Importing the libraries\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "import pandas as pd\n", - "from sklearn.model_selection import train_test_split\n", - "from sklearn.preprocessing import StandardScaler\n", - "from sklearn.metrics import confusion_matrix\n", - "from matplotlib.colors import ListedColormap\n", - "from sklearn.ensemble import RandomForestClassifier" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Importing the dataset\n", - "dataset = pd.read_csv('Social_Network_Ads.csv')\n", - "X = dataset.iloc[:, [2, 3]].values\n", - "y = dataset.iloc[:, 4].values" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Splitting the dataset into the Training set and Test set\n", - "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Users\\Satyam\\AppData\\Roaming\\Python\\Python35\\site-packages\\sklearn\\utils\\validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n", - " warnings.warn(msg, DataConversionWarning)\n" - ] - } - ], - "source": [ - "# Feature Scaling\n", - "sc = StandardScaler()\n", - "X_train = sc.fit_transform(X_train)\n", - "X_test = sc.transform(X_test)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[63 5]\n", - " [ 3 29]]\n" - ] - } - ], - "source": [ - "# Fitting classifier to the Training set\n", - "# Create your classifier here\n", - "classifier = RandomForestClassifier(n_estimators=10,criterion='entropy',random_state=0)\n", - "classifier.fit(X_train,y_train)\n", - "# Predicting the Test set results\n", - "y_pred = classifier.predict(X_test)\n", - "\n", - "# Making the Confusion Matrix\n", - "cm = confusion_matrix(y_test, y_pred)\n", - "print(cm)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEWCAYAAABmE+CbAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztnXuYHGWV8H+nZ5JJSGISBsgFCMl8kiEKGgTRIHyJIIgX\nFhV1wairLkbddVXQ9ZZlvaxZddeV9bJ+bgR1lSwoImoQVIhMBI0gYDRiQsAAAZJMyECGTEg6mZnz\n/VHVmb681VM1VdVVPXN+z5Mn3dXVVeft7jnnfc857zmiqhiGYRhGIWsBDMMwjHxgBsEwDMMAzCAY\nhmEYPmYQDMMwDMAMgmEYhuFjBsEwDMMAzCCMCURkiYg8lrUczULan5eIfF1ELi97/h4R6RaRPhFp\n9//vSPB+R4rIJhGZmNQ1q65/v4icmfS5WSAed4vICVnLkgVmEDJCRB4WkX3+H/8OEfm2iEzOWq64\niIiKyF5/XH0isrvB9w+lzEXkNBG5SUR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PMonCYisdx9uatlpKovC3nq\nKuAm4BMpipMow41NRN4GvBo4W5souBPhO2t2HgeOLXt+jH/MyDG+f/16YJWq/jBredJAVXeLyG3A\necCIEwNyt0Koh4gcX/b0AmBTVrIkjYicB3wY+CtVfSZreQwnvwOOF5F5IjIeuAj4ScYyGXXwA69X\nARtV9YtZy5MkInJkKRtRRCbiJTvE0onNlmV0PdCJl7XyCPBuVR0VMzQReRBoA3r8Q78dDRlUIvJa\n4CvAkcBuYL2qvjxbqUaOiLwS+E+gBfimqq7IWKREEJFrgCV4pZS7gU+o6lWZCpUAInIGcDuwAU9v\nAHxcVW/KTqpkEJHnAf+D91ssAN9X1U/HumYzGQTDMAwjPZrKZWQYhmGkhxkEwzAMAzCDYBiGYfiY\nQTAMwzAAMwiGYRiGjxkEwwiJiLxGRFRETshaFsNIAzMIhhGei/EqSl6ctSCGkQZmEAwjBH4tnDOA\nv4Q1nyMAAAFOSURBVMXboYyIFETka34t+htF5CYReb3/2ikislZE7hGRn/tlmA0j15hBMIxwXAD8\nTFU3Az0icgrwOmAucBJwCbAIDtXO+QrwelU9BfgmMCp2NBujm9wVtzOMnHIx8CX/8bX+81bgOlUd\nBHb4xcXAK69yInCLV0qHFmB7Y8U1jOiYQTCMYRCRw4GzgJNERPEUvAI3BL0FuE9VFzVIRMNIBHMZ\nGcbwvB74rqoep6pz/X4cD+F1GLvQjyXMwCsOB3A/cKSIHHIhichzsxDcMKJgBsEwhudialcD1wMz\n8bqm/Qn4Ol4nrl6/vebrgc+LyB+A9cDpjRPXMEaGVTs1jBiIyGS/yXk7cBfwElXdkbVchjESLIZg\nGPG40W9SMh74FzMGRjNjKwTDMAwDsBiCYRiG4WMGwTAMwwDMIBiGYRg+ZhAMwzAMwAyCYRiG4fP/\nAfyzKuSV3NT5AAAAAElFTkSuQmCC\n", - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEWCA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b7kLCGb2azP7S8Sf0wDcfZm7HwKsAN6XbWqrM9q9hccsAwYI7q8hxLkvkSyZ2RTgKuADJTUN\nDc3dB919AUGNwsvMrKbqvtzNZeTur4x56ArgOuCiFJOTqNHuzczOBl4HnOQN1LhTxW/W6B4GDil6\nf3C4TXIsrF+/Cljh7j/OOj1pcPdtZnYzcAow5o4BuSshVGJmhxW9PQ1Ym1VakmZmpwDnA3/j7k9n\nnR6JdBtwmJk928wmAmcCP8s4TVJB2PD6LWCNu38x6/QkycwOKPRGNLPJBJ0dasoTG62X0VXAfIJe\nKw8A73H3cfGEZmb3Ae3A1nDTH8ZDDyozOx34MnAAsA3odvdXZ5uqsTOz1wL/CbQCl7n78oyTlAgz\n+z6wmGAq5V7gInf/VqaJSoCZLQR+C9xFkG8AfMzdr8suVckwsxcC3yH4t9gC/MDdP13TORspIIiI\nSHoaqspIRETSo4AgIiKAAoKIiIQUEEREBFBAEBGRkAKCSExm9nozczN7btZpEUmDAoJIfGcRzCh5\nVtYJEUmDAoJIDOFcOAuBdxKMUMbMWszsa+Fc9Nea2XVm9sZw30vMbKWZ/dHMfhlOwyySawoIIvGc\nBvzC3dcDW83sJcAbgHnAC4BzgONg79w5Xwbe6O4vAS4DxsWIZhnfcje5nUhOnQV8KXx9Zfi+Dfih\nuw8Bm8PJxSCYXuVI4IZgKh1agU31Ta5I9RQQREZhZs8CTgReYGZOkME7cHW5jwB3u/txdUqiSCJU\nZSQyujcC33P3Q919XrgexwaCFcbOCNsSOggmhwNYBxxgZnurkMzs+VkkXKQaCggiozuLkaWBq4DZ\nBKum/QX4OsFKXNvD5TXfCHzezP4MdAPH1y+5ImOj2U5FamBmU8JFzmcCtwIvd/fNWadLZCzUhiBS\nm2vDRUomAv+iYCCNTCUEEREB1IYgIiIhBQQREQEUEEREJKSAICIigAKCiIiE/j8wn8IRk+gohgAA\nAABJRU5ErkJggg==\n", - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# Visualising the Training set results\n", - "X_set, y_set = X_train, y_train\n", - "X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),\n", - " np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))\n", - "plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),\n", - " alpha = 0.75, cmap = ListedColormap(('red', 'green')))\n", - "plt.xlim(X1.min(), X1.max())\n", - "plt.ylim(X2.min(), X2.max())\n", - "for i, j in enumerate(np.unique(y_set)):\n", - " plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],\n", - " c = ListedColormap(('red', 'green'))(i), label = j)\n", - "plt.title('Random Forest Classifier (Training set)')\n", - "plt.xlabel('Age')\n", - "plt.ylabel('Estimated Salary')\n", - "plt.legend()\n", - "plt.show()\n", - "\n", - "# Visualising the Test set results\n", - "X_set, y_set = X_test, y_test\n", - "X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),\n", - " np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))\n", - "plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),\n", - " alpha = 0.75, cmap = ListedColormap(('red', 'green')))\n", - "plt.xlim(X1.min(), X1.max())\n", - "plt.ylim(X2.min(), X2.max())\n", - "for i, j in enumerate(np.unique(y_set)):\n", - " plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],\n", - " c = ListedColormap(('red', 'green'))(i), label = j)\n", - "plt.title('Random Forest Classifier (Test set)')\n", - "plt.xlabel('Age')\n", - "plt.ylabel('Estimated Salary')\n", - "plt.legend()\n", - "plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.1" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/machine_learning/Random Forest Classification/Social_Network_Ads.csv b/machine_learning/Random Forest Classification/Social_Network_Ads.csv deleted file mode 100644 index 4a53849c2baf..000000000000 --- a/machine_learning/Random Forest Classification/Social_Network_Ads.csv +++ /dev/null @@ -1,401 +0,0 @@ -User ID,Gender,Age,EstimatedSalary,Purchased -15624510,Male,19,19000,0 -15810944,Male,35,20000,0 -15668575,Female,26,43000,0 -15603246,Female,27,57000,0 -15804002,Male,19,76000,0 -15728773,Male,27,58000,0 -15598044,Female,27,84000,0 -15694829,Female,32,150000,1 -15600575,Male,25,33000,0 -15727311,Female,35,65000,0 -15570769,Female,26,80000,0 -15606274,Female,26,52000,0 -15746139,Male,20,86000,0 -15704987,Male,32,18000,0 -15628972,Male,18,82000,0 -15697686,Male,29,80000,0 -15733883,Male,47,25000,1 -15617482,Male,45,26000,1 -15704583,Male,46,28000,1 -15621083,Female,48,29000,1 -15649487,Male,45,22000,1 -15736760,Female,47,49000,1 -15714658,Male,48,41000,1 -15599081,Female,45,22000,1 -15705113,Male,46,23000,1 -15631159,Male,47,20000,1 -15792818,Male,49,28000,1 -15633531,Female,47,30000,1 -15744529,Male,29,43000,0 -15669656,Male,31,18000,0 -15581198,Male,31,74000,0 -15729054,Female,27,137000,1 -15573452,Female,21,16000,0 -15776733,Female,28,44000,0 -15724858,Male,27,90000,0 -15713144,Male,35,27000,0 -15690188,Female,33,28000,0 -15689425,Male,30,49000,0 -15671766,Female,26,72000,0 -15782806,Female,27,31000,0 -15764419,Female,27,17000,0 -15591915,Female,33,51000,0 -15772798,Male,35,108000,0 -15792008,Male,30,15000,0 -15715541,Female,28,84000,0 -15639277,Male,23,20000,0 -15798850,Male,25,79000,0 -15776348,Female,27,54000,0 -15727696,Male,30,135000,1 -15793813,Female,31,89000,0 -15694395,Female,24,32000,0 -15764195,Female,18,44000,0 -15744919,Female,29,83000,0 -15671655,Female,35,23000,0 -15654901,Female,27,58000,0 -15649136,Female,24,55000,0 -15775562,Female,23,48000,0 -15807481,Male,28,79000,0 -15642885,Male,22,18000,0 -15789109,Female,32,117000,0 -15814004,Male,27,20000,0 -15673619,Male,25,87000,0 -15595135,Female,23,66000,0 -15583681,Male,32,120000,1 -15605000,Female,59,83000,0 -15718071,Male,24,58000,0 -15679760,Male,24,19000,0 -15654574,Female,23,82000,0 -15577178,Female,22,63000,0 -15595324,Female,31,68000,0 -15756932,Male,25,80000,0 -15726358,Female,24,27000,0 -15595228,Female,20,23000,0 -15782530,Female,33,113000,0 -15592877,Male,32,18000,0 -15651983,Male,34,112000,1 -15746737,Male,18,52000,0 -15774179,Female,22,27000,0 -15667265,Female,28,87000,0 -15655123,Female,26,17000,0 -15595917,Male,30,80000,0 -15668385,Male,39,42000,0 -15709476,Male,20,49000,0 -15711218,Male,35,88000,0 -15798659,Female,30,62000,0 -15663939,Female,31,118000,1 -15694946,Male,24,55000,0 -15631912,Female,28,85000,0 -15768816,Male,26,81000,0 -15682268,Male,35,50000,0 -15684801,Male,22,81000,0 -15636428,Female,30,116000,0 -15809823,Male,26,15000,0 -15699284,Female,29,28000,0 -15786993,Female,29,83000,0 -15709441,Female,35,44000,0 -15710257,Female,35,25000,0 -15582492,Male,28,123000,1 -15575694,Male,35,73000,0 -15756820,Female,28,37000,0 -15766289,Male,27,88000,0 -15593014,Male,28,59000,0 -15584545,Female,32,86000,0 -15675949,Female,33,149000,1 -15672091,Female,19,21000,0 -15801658,Male,21,72000,0 -15706185,Female,26,35000,0 -15789863,Male,27,89000,0 -15720943,Male,26,86000,0 -15697997,Female,38,80000,0 -15665416,Female,39,71000,0 -15660200,Female,37,71000,0 -15619653,Male,38,61000,0 -15773447,Male,37,55000,0 -15739160,Male,42,80000,0 -15689237,Male,40,57000,0 -15679297,Male,35,75000,0 -15591433,Male,36,52000,0 -15642725,Male,40,59000,0 -15701962,Male,41,59000,0 -15811613,Female,36,75000,0 -15741049,Male,37,72000,0 -15724423,Female,40,75000,0 -15574305,Male,35,53000,0 -15678168,Female,41,51000,0 -15697020,Female,39,61000,0 -15610801,Male,42,65000,0 -15745232,Male,26,32000,0 -15722758,Male,30,17000,0 -15792102,Female,26,84000,0 -15675185,Male,31,58000,0 -15801247,Male,33,31000,0 -15725660,Male,30,87000,0 -15638963,Female,21,68000,0 -15800061,Female,28,55000,0 -15578006,Male,23,63000,0 -15668504,Female,20,82000,0 -15687491,Male,30,107000,1 -15610403,Female,28,59000,0 -15741094,Male,19,25000,0 -15807909,Male,19,85000,0 -15666141,Female,18,68000,0 -15617134,Male,35,59000,0 -15783029,Male,30,89000,0 -15622833,Female,34,25000,0 -15746422,Female,24,89000,0 -15750839,Female,27,96000,1 -15749130,Female,41,30000,0 -15779862,Male,29,61000,0 -15767871,Male,20,74000,0 -15679651,Female,26,15000,0 -15576219,Male,41,45000,0 -15699247,Male,31,76000,0 -15619087,Female,36,50000,0 -15605327,Male,40,47000,0 -15610140,Female,31,15000,0 -15791174,Male,46,59000,0 -15602373,Male,29,75000,0 -15762605,Male,26,30000,0 -15598840,Female,32,135000,1 -15744279,Male,32,100000,1 -15670619,Male,25,90000,0 -15599533,Female,37,33000,0 -15757837,Male,35,38000,0 -15697574,Female,33,69000,0 -15578738,Female,18,86000,0 -15762228,Female,22,55000,0 -15614827,Female,35,71000,0 -15789815,Male,29,148000,1 -15579781,Female,29,47000,0 -15587013,Male,21,88000,0 -15570932,Male,34,115000,0 -15794661,Female,26,118000,0 -15581654,Female,34,43000,0 -15644296,Female,34,72000,0 -15614420,Female,23,28000,0 -15609653,Female,35,47000,0 -15594577,Male,25,22000,0 -15584114,Male,24,23000,0 -15673367,Female,31,34000,0 -15685576,Male,26,16000,0 -15774727,Female,31,71000,0 -15694288,Female,32,117000,1 -15603319,Male,33,43000,0 -15759066,Female,33,60000,0 -15814816,Male,31,66000,0 -15724402,Female,20,82000,0 -15571059,Female,33,41000,0 -15674206,Male,35,72000,0 -15715160,Male,28,32000,0 -15730448,Male,24,84000,0 -15662067,Female,19,26000,0 -15779581,Male,29,43000,0 -15662901,Male,19,70000,0 -15689751,Male,28,89000,0 -15667742,Male,34,43000,0 -15738448,Female,30,79000,0 -15680243,Female,20,36000,0 -15745083,Male,26,80000,0 -15708228,Male,35,22000,0 -15628523,Male,35,39000,0 -15708196,Male,49,74000,0 -15735549,Female,39,134000,1 -15809347,Female,41,71000,0 -15660866,Female,58,101000,1 -15766609,Female,47,47000,0 -15654230,Female,55,130000,1 -15794566,Female,52,114000,0 -15800890,Female,40,142000,1 -15697424,Female,46,22000,0 -15724536,Female,48,96000,1 -15735878,Male,52,150000,1 -15707596,Female,59,42000,0 -15657163,Male,35,58000,0 -15622478,Male,47,43000,0 -15779529,Female,60,108000,1 -15636023,Male,49,65000,0 -15582066,Male,40,78000,0 -15666675,Female,46,96000,0 -15732987,Male,59,143000,1 -15789432,Female,41,80000,0 -15663161,Male,35,91000,1 -15694879,Male,37,144000,1 -15593715,Male,60,102000,1 -15575002,Female,35,60000,0 -15622171,Male,37,53000,0 -15795224,Female,36,126000,1 -15685346,Male,56,133000,1 -15691808,Female,40,72000,0 -15721007,Female,42,80000,1 -15794253,Female,35,147000,1 -15694453,Male,39,42000,0 -15813113,Male,40,107000,1 -15614187,Male,49,86000,1 -15619407,Female,38,112000,0 -15646227,Male,46,79000,1 -15660541,Male,40,57000,0 -15753874,Female,37,80000,0 -15617877,Female,46,82000,0 -15772073,Female,53,143000,1 -15701537,Male,42,149000,1 -15736228,Male,38,59000,0 -15780572,Female,50,88000,1 -15769596,Female,56,104000,1 -15586996,Female,41,72000,0 -15722061,Female,51,146000,1 -15638003,Female,35,50000,0 -15775590,Female,57,122000,1 -15730688,Male,41,52000,0 -15753102,Female,35,97000,1 -15810075,Female,44,39000,0 -15723373,Male,37,52000,0 -15795298,Female,48,134000,1 -15584320,Female,37,146000,1 -15724161,Female,50,44000,0 -15750056,Female,52,90000,1 -15609637,Female,41,72000,0 -15794493,Male,40,57000,0 -15569641,Female,58,95000,1 -15815236,Female,45,131000,1 -15811177,Female,35,77000,0 -15680587,Male,36,144000,1 -15672821,Female,55,125000,1 -15767681,Female,35,72000,0 -15600379,Male,48,90000,1 -15801336,Female,42,108000,1 -15721592,Male,40,75000,0 -15581282,Male,37,74000,0 -15746203,Female,47,144000,1 -15583137,Male,40,61000,0 -15680752,Female,43,133000,0 -15688172,Female,59,76000,1 -15791373,Male,60,42000,1 -15589449,Male,39,106000,1 -15692819,Female,57,26000,1 -15727467,Male,57,74000,1 -15734312,Male,38,71000,0 -15764604,Male,49,88000,1 -15613014,Female,52,38000,1 -15759684,Female,50,36000,1 -15609669,Female,59,88000,1 -15685536,Male,35,61000,0 -15750447,Male,37,70000,1 -15663249,Female,52,21000,1 -15638646,Male,48,141000,0 -15734161,Female,37,93000,1 -15631070,Female,37,62000,0 -15761950,Female,48,138000,1 -15649668,Male,41,79000,0 -15713912,Female,37,78000,1 -15586757,Male,39,134000,1 -15596522,Male,49,89000,1 -15625395,Male,55,39000,1 -15760570,Male,37,77000,0 -15566689,Female,35,57000,0 -15725794,Female,36,63000,0 -15673539,Male,42,73000,1 -15705298,Female,43,112000,1 -15675791,Male,45,79000,0 -15747043,Male,46,117000,1 -15736397,Female,58,38000,1 -15678201,Male,48,74000,1 -15720745,Female,37,137000,1 -15637593,Male,37,79000,1 -15598070,Female,40,60000,0 -15787550,Male,42,54000,0 -15603942,Female,51,134000,0 -15733973,Female,47,113000,1 -15596761,Male,36,125000,1 -15652400,Female,38,50000,0 -15717893,Female,42,70000,0 -15622585,Male,39,96000,1 -15733964,Female,38,50000,0 -15753861,Female,49,141000,1 -15747097,Female,39,79000,0 -15594762,Female,39,75000,1 -15667417,Female,54,104000,1 -15684861,Male,35,55000,0 -15742204,Male,45,32000,1 -15623502,Male,36,60000,0 -15774872,Female,52,138000,1 -15611191,Female,53,82000,1 -15674331,Male,41,52000,0 -15619465,Female,48,30000,1 -15575247,Female,48,131000,1 -15695679,Female,41,60000,0 -15713463,Male,41,72000,0 -15785170,Female,42,75000,0 -15796351,Male,36,118000,1 -15639576,Female,47,107000,1 -15693264,Male,38,51000,0 -15589715,Female,48,119000,1 -15769902,Male,42,65000,0 -15587177,Male,40,65000,0 -15814553,Male,57,60000,1 -15601550,Female,36,54000,0 -15664907,Male,58,144000,1 -15612465,Male,35,79000,0 -15810800,Female,38,55000,0 -15665760,Male,39,122000,1 -15588080,Female,53,104000,1 -15776844,Male,35,75000,0 -15717560,Female,38,65000,0 -15629739,Female,47,51000,1 -15729908,Male,47,105000,1 -15716781,Female,41,63000,0 -15646936,Male,53,72000,1 -15768151,Female,54,108000,1 -15579212,Male,39,77000,0 -15721835,Male,38,61000,0 -15800515,Female,38,113000,1 -15591279,Male,37,75000,0 -15587419,Female,42,90000,1 -15750335,Female,37,57000,0 -15699619,Male,36,99000,1 -15606472,Male,60,34000,1 -15778368,Male,54,70000,1 -15671387,Female,41,72000,0 -15573926,Male,40,71000,1 -15709183,Male,42,54000,0 -15577514,Male,43,129000,1 -15778830,Female,53,34000,1 -15768072,Female,47,50000,1 -15768293,Female,42,79000,0 -15654456,Male,42,104000,1 -15807525,Female,59,29000,1 -15574372,Female,58,47000,1 -15671249,Male,46,88000,1 -15779744,Male,38,71000,0 -15624755,Female,54,26000,1 -15611430,Female,60,46000,1 -15774744,Male,60,83000,1 -15629885,Female,39,73000,0 -15708791,Male,59,130000,1 -15793890,Female,37,80000,0 -15646091,Female,46,32000,1 -15596984,Female,46,74000,0 -15800215,Female,42,53000,0 -15577806,Male,41,87000,1 -15749381,Female,58,23000,1 -15683758,Male,42,64000,0 -15670615,Male,48,33000,1 -15715622,Female,44,139000,1 -15707634,Male,49,28000,1 -15806901,Female,57,33000,1 -15775335,Male,56,60000,1 -15724150,Female,49,39000,1 -15627220,Male,39,71000,0 -15672330,Male,47,34000,1 -15668521,Female,48,35000,1 -15807837,Male,48,33000,1 -15592570,Male,47,23000,1 -15748589,Female,45,45000,1 -15635893,Male,60,42000,1 -15757632,Female,39,59000,0 -15691863,Female,46,41000,1 -15706071,Male,51,23000,1 -15654296,Female,50,20000,1 -15755018,Male,36,33000,0 -15594041,Female,49,36000,1 \ No newline at end of file diff --git a/machine_learning/Random Forest Classification/random_forest_classification.py b/machine_learning/Random Forest Classification/random_forest_classification.py deleted file mode 100644 index d5dde4b13822..000000000000 --- a/machine_learning/Random Forest Classification/random_forest_classification.py +++ /dev/null @@ -1,69 +0,0 @@ -# Random Forest Classification - -# Importing the libraries -import numpy as np -import matplotlib.pyplot as plt -import pandas as pd - -# Importing the dataset -dataset = pd.read_csv('Social_Network_Ads.csv') -X = dataset.iloc[:, [2, 3]].values -y = dataset.iloc[:, 4].values - -# Splitting the dataset into the Training set and Test set -from sklearn.cross_validation import train_test_split -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0) - -# Feature Scaling -from sklearn.preprocessing import StandardScaler -sc = StandardScaler() -X_train = sc.fit_transform(X_train) -X_test = sc.transform(X_test) - -# Fitting Random Forest Classification to the Training set -from sklearn.ensemble import RandomForestClassifier -classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0) -classifier.fit(X_train, y_train) - -# Predicting the Test set results -y_pred = classifier.predict(X_test) - -# Making the Confusion Matrix -from sklearn.metrics import confusion_matrix -cm = confusion_matrix(y_test, y_pred) - -# Visualising the Training set results -from matplotlib.colors import ListedColormap -X_set, y_set = X_train, y_train -X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), - np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) -plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), - alpha = 0.75, cmap = ListedColormap(('red', 'green'))) -plt.xlim(X1.min(), X1.max()) -plt.ylim(X2.min(), X2.max()) -for i, j in enumerate(np.unique(y_set)): - plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], - c = ListedColormap(('red', 'green'))(i), label = j) -plt.title('Random Forest Classification (Training set)') -plt.xlabel('Age') -plt.ylabel('Estimated Salary') -plt.legend() -plt.show() - -# Visualising the Test set results -from matplotlib.colors import ListedColormap -X_set, y_set = X_test, y_test -X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01), - np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01)) -plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), - alpha = 0.75, cmap = ListedColormap(('red', 'green'))) -plt.xlim(X1.min(), X1.max()) -plt.ylim(X2.min(), X2.max()) -for i, j in enumerate(np.unique(y_set)): - plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], - c = ListedColormap(('red', 'green'))(i), label = j) -plt.title('Random Forest Classification (Test set)') -plt.xlabel('Age') -plt.ylabel('Estimated Salary') -plt.legend() -plt.show() \ No newline at end of file diff --git a/machine_learning/Random Forest Regression/Position_Salaries.csv b/machine_learning/Random Forest Regression/Position_Salaries.csv deleted file mode 100644 index 0c752c72a1d1..000000000000 --- a/machine_learning/Random Forest Regression/Position_Salaries.csv +++ /dev/null @@ -1,11 +0,0 @@ -Position,Level,Salary -Business Analyst,1,45000 -Junior Consultant,2,50000 -Senior Consultant,3,60000 -Manager,4,80000 -Country Manager,5,110000 -Region Manager,6,150000 -Partner,7,200000 -Senior Partner,8,300000 -C-level,9,500000 -CEO,10,1000000 \ No newline at end of file diff --git a/machine_learning/Random Forest Regression/Random Forest Regression.ipynb b/machine_learning/Random Forest Regression/Random Forest Regression.ipynb deleted file mode 100644 index 17f4d42bfb0d..000000000000 --- a/machine_learning/Random Forest Regression/Random Forest Regression.ipynb +++ /dev/null @@ -1,147 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Importing the libraries\n", - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "import pandas as pd\n", - "from sklearn.ensemble import RandomForestRegressor" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Importing the dataset\n", - "dataset = pd.read_csv('Position_Salaries.csv')\n", - "X = dataset.iloc[:, 1:2].values\n", - "y = dataset.iloc[:, 2].values" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,\n", - " max_features='auto', max_leaf_nodes=None,\n", - " min_impurity_split=1e-07, min_samples_leaf=1,\n", - " min_samples_split=2, min_weight_fraction_leaf=0.0,\n", - " n_estimators=300, n_jobs=1, oob_score=False, random_state=0,\n", - " verbose=0, warm_start=False)" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Fitting Random Forest Regression to the dataset\n", - "regressor = RandomForestRegressor(n_estimators = 300, random_state = 0)\n", - "regressor.fit(X, y)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Predicting a new result\n", - "y_pred = regressor.predict(6.5)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[ 160333.33333333]\n" - ] - } - ], - "source": [ - "print(y_pred)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAEWCAYAAADPZygPAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XmcXFWd9/H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jZFaMk5BZ+baR9GSafohsnL6vAL+IiNUAkm4F/gG4C/ihpO8Dt0fE\nQyXU8/fAjIhoJRtU8gHg74BVwGMRsTjV9SQwkg6SEPAe4P3AvanB1EB26wKAG8mSz8Xp+bguljer\nCCchs/Ktad9y6ehsWET8r6R9gCOA70m6JyLOr0AMawumW+n8f1rA3IgodhvvG4CbUtKMiHhB0gc6\nWd6sInxNyKyyHgKOTiMybwt8GnhI0u5Ac0T8jOwGasVum7A+nQIrts3jJDVIGkx2R9THiizXleeB\nwZL2h+z0nKT3AUTEn8mS2P8lS0idLm9WKW4JmVVQRDwh6Vo2JonpEfEHSYcBl0jaAKwnO23X3jTg\nKUlPRMTEgvJfAPsDfyS7sdi3ImKJpD1LjG1d6lb943RNqi/Z3WbnpkVuAC4hu7lbd5Y322weRdvM\nzHLj03FmZpYbJyEzM8uNk5CZmeXGScjMzHLjJGRmZrlxEjIzs9w4CZmZWW7+P0PNi1lCP0XzAAAA\nAElFTkSuQmCC\n", - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# Visualising the Random Forest Regression results (higher resolution)\n", - "X_grid = np.arange(min(X), max(X), 0.01)\n", - "X_grid = X_grid.reshape((len(X_grid), 1))\n", - "plt.scatter(X, y, color = 'red')\n", - "plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')\n", - "plt.title('Truth or Bluff (Random Forest Regression)')\n", - "plt.xlabel('Position level')\n", - "plt.ylabel('Salary')\n", - "plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.1" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/machine_learning/Random Forest Regression/random_forest_regression.py b/machine_learning/Random Forest Regression/random_forest_regression.py deleted file mode 100644 index fce58b1fe283..000000000000 --- a/machine_learning/Random Forest Regression/random_forest_regression.py +++ /dev/null @@ -1,41 +0,0 @@ -# Random Forest Regression - -# Importing the libraries -import numpy as np -import matplotlib.pyplot as plt -import pandas as pd - -# Importing the dataset -dataset = pd.read_csv('Position_Salaries.csv') -X = dataset.iloc[:, 1:2].values -y = dataset.iloc[:, 2].values - -# Splitting the dataset into the Training set and Test set -"""from sklearn.cross_validation import train_test_split -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)""" - -# Feature Scaling -"""from sklearn.preprocessing import StandardScaler -sc_X = StandardScaler() -X_train = sc_X.fit_transform(X_train) -X_test = sc_X.transform(X_test) -sc_y = StandardScaler() -y_train = sc_y.fit_transform(y_train)""" - -# Fitting Random Forest Regression to the dataset -from sklearn.ensemble import RandomForestRegressor -regressor = RandomForestRegressor(n_estimators = 10, random_state = 0) -regressor.fit(X, y) - -# Predicting a new result -y_pred = regressor.predict(6.5) - -# Visualising the Random Forest Regression results (higher resolution) -X_grid = np.arange(min(X), max(X), 0.01) -X_grid = X_grid.reshape((len(X_grid), 1)) -plt.scatter(X, y, color = 'red') -plt.plot(X_grid, regressor.predict(X_grid), color = 'blue') -plt.title('Truth or Bluff (Random Forest Regression)') -plt.xlabel('Position level') -plt.ylabel('Salary') -plt.show() \ No newline at end of file diff --git a/machine_learning/__init__.py b/machine_learning/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/machine_learning/astar.py b/machine_learning/astar.py new file mode 100644 index 000000000000..ee3fcff0b7bf --- /dev/null +++ b/machine_learning/astar.py @@ -0,0 +1,150 @@ +""" +The A* algorithm combines features of uniform-cost search and pure +heuristic search to efficiently compute optimal solutions. +A* algorithm is a best-first search algorithm in which the cost +associated with a node is f(n) = g(n) + h(n), +where g(n) is the cost of the path from the initial state to node n and +h(n) is the heuristic estimate or the cost or a path +from node n to a goal.A* algorithm introduces a heuristic into a +regular graph-searching algorithm, +essentially planning ahead at each step so a more optimal decision +is made.A* also known as the algorithm with brains +""" +import numpy as np + + +class Cell: + """ + Class cell represents a cell in the world which have the property + position : The position of the represented by tupleof x and y + coordinates initially set to (0,0) + parent : This contains the parent cell object which we visited + before arrinving this cell + g,h,f : The parameters for constructing the heuristic function + which can be any function. for simplicity used line + distance + """ + + def __init__(self): + self.position = (0, 0) + self.parent = None + + self.g = 0 + self.h = 0 + self.f = 0 + + """ + overrides equals method because otherwise cell assign will give + wrong results + """ + + def __eq__(self, cell): + return self.position == cell.position + + def showcell(self): + print(self.position) + + +class Gridworld: + """ + Gridworld class represents the external world here a grid M*M + matrix + world_size: create a numpy array with the given world_size default is 5 + """ + + def __init__(self, world_size=(5, 5)): + self.w = np.zeros(world_size) + self.world_x_limit = world_size[0] + self.world_y_limit = world_size[1] + + def show(self): + print(self.w) + + def get_neigbours(self, cell): + """ + Return the neighbours of cell + """ + neughbour_cord = [ + (-1, -1), + (-1, 0), + (-1, 1), + (0, -1), + (0, 1), + (1, -1), + (1, 0), + (1, 1), + ] + current_x = cell.position[0] + current_y = cell.position[1] + neighbours = [] + for n in neughbour_cord: + x = current_x + n[0] + y = current_y + n[1] + if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit: + c = Cell() + c.position = (x, y) + c.parent = cell + neighbours.append(c) + return neighbours + + +def astar(world, start, goal): + """ + Implementation of a start algorithm + world : Object of the world object + start : Object of the cell as start position + stop : Object of the cell as goal position + + >>> p = Gridworld() + >>> start = Cell() + >>> start.position = (0,0) + >>> goal = Cell() + >>> goal.position = (4,4) + >>> astar(p, start, goal) + [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] + """ + _open = [] + _closed = [] + _open.append(start) + + while _open: + min_f = np.argmin([n.f for n in _open]) + current = _open[min_f] + _closed.append(_open.pop(min_f)) + if current == goal: + break + for n in world.get_neigbours(current): + for c in _closed: + if c == n: + continue + n.g = current.g + 1 + x1, y1 = n.position + x2, y2 = goal.position + n.h = (y2 - y1) ** 2 + (x2 - x1) ** 2 + n.f = n.h + n.g + + for c in _open: + if c == n and c.f < n.f: + continue + _open.append(n) + path = [] + while current.parent is not None: + path.append(current.position) + current = current.parent + path.append(current.position) + return path[::-1] + + +if __name__ == "__main__": + world = Gridworld() + # stat position and Goal + start = Cell() + start.position = (0, 0) + goal = Cell() + goal.position = (4, 4) + print(f"path from {start.position} to {goal.position}") + s = astar(world, start, goal) + # Just for visual reasons + for i in s: + world.w[i] = 1 + print(world.w) diff --git a/machine_learning/data_transformations.py b/machine_learning/data_transformations.py new file mode 100644 index 000000000000..9e0d747e93fa --- /dev/null +++ b/machine_learning/data_transformations.py @@ -0,0 +1,62 @@ +""" +Normalization Wikipedia: https://en.wikipedia.org/wiki/Normalization +Normalization is the process of converting numerical data to a standard range of values. +This range is typically between [0, 1] or [-1, 1]. The equation for normalization is +x_norm = (x - x_min)/(x_max - x_min) where x_norm is the normalized value, x is the +value, x_min is the minimum value within the column or list of data, and x_max is the +maximum value within the column or list of data. Normalization is used to speed up the +training of data and put all of the data on a similar scale. This is useful because +variance in the range of values of a dataset can heavily impact optimization +(particularly Gradient Descent). + +Standardization Wikipedia: https://en.wikipedia.org/wiki/Standardization +Standardization is the process of converting numerical data to a normally distributed +range of values. This range will have a mean of 0 and standard deviation of 1. This is +also known as z-score normalization. The equation for standardization is +x_std = (x - mu)/(sigma) where mu is the mean of the column or list of values and sigma +is the standard deviation of the column or list of values. + +Choosing between Normalization & Standardization is more of an art of a science, but it +is often recommended to run experiments with both to see which performs better. +Additionally, a few rules of thumb are: + 1. gaussian (normal) distributions work better with standardization + 2. non-gaussian (non-normal) distributions work better with normalization + 3. If a column or list of values has extreme values / outliers, use standardization +""" +from statistics import mean, stdev + + +def normalization(data: list, ndigits: int = 3) -> list: + """ + Returns a normalized list of values + @params: data, a list of values to normalize + @returns: a list of normalized values (rounded to ndigits decimal places) + @examples: + >>> normalization([2, 7, 10, 20, 30, 50]) + [0.0, 0.104, 0.167, 0.375, 0.583, 1.0] + >>> normalization([5, 10, 15, 20, 25]) + [0.0, 0.25, 0.5, 0.75, 1.0] + """ + # variables for calculation + x_min = min(data) + x_max = max(data) + # normalize data + return [round((x - x_min) / (x_max - x_min), ndigits) for x in data] + + +def standardization(data: list, ndigits: int = 3) -> list: + """ + Returns a standardized list of values + @params: data, a list of values to standardize + @returns: a list of standardized values (rounded to ndigits decimal places) + @examples: + >>> standardization([2, 7, 10, 20, 30, 50]) + [-0.999, -0.719, -0.551, 0.009, 0.57, 1.69] + >>> standardization([5, 10, 15, 20, 25]) + [-1.265, -0.632, 0.0, 0.632, 1.265] + """ + # variables for calculation + mu = mean(data) + sigma = stdev(data) + # standardize data + return [round((x - mu) / (sigma), ndigits) for x in data] diff --git a/machine_learning/decision_tree.py b/machine_learning/decision_tree.py index 71849904ccf2..ace6fb0fa883 100644 --- a/machine_learning/decision_tree.py +++ b/machine_learning/decision_tree.py @@ -1,14 +1,13 @@ """ Implementation of a basic regression decision tree. Input data set: The input data set must be 1-dimensional with continuous labels. -Output: The decision tree maps a real number input to a real number output. +Output: The decision tree maps a real number input to a real number output. """ -from __future__ import print_function - import numpy as np + class Decision_Tree: - def __init__(self, depth = 5, min_leaf_size = 5): + def __init__(self, depth=5, min_leaf_size=5): self.depth = depth self.decision_boundary = 0 self.left = None @@ -19,9 +18,23 @@ def __init__(self, depth = 5, min_leaf_size = 5): def mean_squared_error(self, labels, prediction): """ mean_squared_error: - @param labels: a one dimensional numpy array + @param labels: a one dimensional numpy array @param prediction: a floating point value - return value: mean_squared_error calculates the error if prediction is used to estimate the labels + return value: mean_squared_error calculates the error if prediction is used to + estimate the labels + >>> tester = Decision_Tree() + >>> test_labels = np.array([1,2,3,4,5,6,7,8,9,10]) + >>> test_prediction = np.float(6) + >>> tester.mean_squared_error(test_labels, test_prediction) == ( + ... Test_Decision_Tree.helper_mean_squared_error_test(test_labels, + ... test_prediction)) + True + >>> test_labels = np.array([1,2,3]) + >>> test_prediction = np.float(2) + >>> tester.mean_squared_error(test_labels, test_prediction) == ( + ... Test_Decision_Tree.helper_mean_squared_error_test(test_labels, + ... test_prediction)) + True """ if labels.ndim != 1: print("Error: Input labels must be one dimensional") @@ -32,14 +45,15 @@ def train(self, X, y): """ train: @param X: a one dimensional numpy array - @param y: a one dimensional numpy array. + @param y: a one dimensional numpy array. The contents of y are the labels for the corresponding X values train does not have a return value """ """ - this section is to check that the inputs conform to our dimensionality constraints + this section is to check that the inputs conform to our dimensionality + constraints """ if X.ndim != 1: print("Error: Input data set must be one dimensional") @@ -60,13 +74,13 @@ def train(self, X, y): return best_split = 0 - min_error = self.mean_squared_error(X,np.mean(y)) * 2 - + min_error = self.mean_squared_error(X, np.mean(y)) * 2 """ loop over all possible splits for the decision tree. find the best split. if no split exists that is less than 2 * error for the entire array - then the data set is not split and the average for the entire array is used as the predictor + then the data set is not split and the average for the entire array is used as + the predictor """ for i in range(len(X)): if len(X[:i]) < self.min_leaf_size: @@ -88,8 +102,12 @@ def train(self, X, y): right_y = y[best_split:] self.decision_boundary = X[best_split] - self.left = Decision_Tree(depth = self.depth - 1, min_leaf_size = self.min_leaf_size) - self.right = Decision_Tree(depth = self.depth - 1, min_leaf_size = self.min_leaf_size) + self.left = Decision_Tree( + depth=self.depth - 1, min_leaf_size=self.min_leaf_size + ) + self.right = Decision_Tree( + depth=self.depth - 1, min_leaf_size=self.min_leaf_size + ) self.left.train(left_X, left_y) self.right.train(right_X, right_y) else: @@ -115,17 +133,37 @@ def predict(self, x): print("Error: Decision tree not yet trained") return None + +class Test_Decision_Tree: + """Decision Tres test class""" + + @staticmethod + def helper_mean_squared_error_test(labels, prediction): + """ + helper_mean_squared_error_test: + @param labels: a one dimensional numpy array + @param prediction: a floating point value + return value: helper_mean_squared_error_test calculates the mean squared error + """ + squared_error_sum = np.float(0) + for label in labels: + squared_error_sum += (label - prediction) ** 2 + + return np.float(squared_error_sum / labels.size) + + def main(): """ - In this demonstration we're generating a sample data set from the sin function in numpy. - We then train a decision tree on the data set and use the decision tree to predict the - label of 10 different test values. Then the mean squared error over this test is displayed. + In this demonstration we're generating a sample data set from the sin function in + numpy. We then train a decision tree on the data set and use the decision tree to + predict the label of 10 different test values. Then the mean squared error over + this test is displayed. """ - X = np.arange(-1., 1., 0.005) + X = np.arange(-1.0, 1.0, 0.005) y = np.sin(X) - tree = Decision_Tree(depth = 10, min_leaf_size = 10) - tree.train(X,y) + tree = Decision_Tree(depth=10, min_leaf_size=10) + tree.train(X, y) test_cases = (np.random.rand(10) * 2) - 1 predictions = np.array([tree.predict(x) for x in test_cases]) @@ -135,6 +173,9 @@ def main(): print("Predictions: " + str(predictions)) print("Average error: " + str(avg_error)) - -if __name__ == '__main__': - main() \ No newline at end of file + +if __name__ == "__main__": + main() + import doctest + + doctest.testmod(name="mean_squarred_error", verbose=True) diff --git a/machine_learning/forecasting/__init__.py b/machine_learning/forecasting/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/machine_learning/forecasting/ex_data.csv b/machine_learning/forecasting/ex_data.csv new file mode 100644 index 000000000000..1c429e649755 --- /dev/null +++ b/machine_learning/forecasting/ex_data.csv @@ -0,0 +1,114 @@ +total_user,total_events,days +18231,0.0,1 +22621,1.0,2 +15675,0.0,3 +23583,1.0,4 +68351,5.0,5 +34338,3.0,6 +19238,0.0,0 +24192,0.0,1 +70349,0.0,2 +103510,0.0,3 +128355,1.0,4 +148484,6.0,5 +153489,3.0,6 +162667,1.0,0 +311430,3.0,1 +435663,7.0,2 +273526,0.0,3 +628588,2.0,4 +454989,13.0,5 +539040,3.0,6 +52974,1.0,0 +103451,2.0,1 +810020,5.0,2 +580982,3.0,3 +216515,0.0,4 +134694,10.0,5 +93563,1.0,6 +55432,1.0,0 +169634,1.0,1 +254908,4.0,2 +315285,3.0,3 +191764,0.0,4 +514284,7.0,5 +181214,4.0,6 +78459,2.0,0 +161620,3.0,1 +245610,4.0,2 +326722,5.0,3 +214578,0.0,4 +312365,5.0,5 +232454,4.0,6 +178368,1.0,0 +97152,1.0,1 +222813,4.0,2 +285852,4.0,3 +192149,1.0,4 +142241,1.0,5 +173011,2.0,6 +56488,3.0,0 +89572,2.0,1 +356082,2.0,2 +172799,0.0,3 +142300,1.0,4 +78432,2.0,5 +539023,9.0,6 +62389,1.0,0 +70247,1.0,1 +89229,0.0,2 +94583,1.0,3 +102455,0.0,4 +129270,0.0,5 +311409,1.0,6 +1837026,0.0,0 +361824,0.0,1 +111379,2.0,2 +76337,2.0,3 +96747,0.0,4 +92058,0.0,5 +81929,2.0,6 +143423,0.0,0 +82939,0.0,1 +74403,1.0,2 +68234,0.0,3 +94556,1.0,4 +80311,0.0,5 +75283,3.0,6 +77724,0.0,0 +49229,2.0,1 +65708,2.0,2 +273864,1.0,3 +1711281,0.0,4 +1900253,5.0,5 +343071,1.0,6 +1551326,0.0,0 +56636,1.0,1 +272782,2.0,2 +1785678,0.0,3 +241866,0.0,4 +461904,0.0,5 +2191901,2.0,6 +102925,0.0,0 +242778,1.0,1 +298608,0.0,2 +322458,10.0,3 +216027,9.0,4 +916052,12.0,5 +193278,12.0,6 +263207,8.0,0 +672948,10.0,1 +281909,1.0,2 +384562,1.0,3 +1027375,2.0,4 +828905,9.0,5 +624188,22.0,6 +392218,8.0,0 +292581,10.0,1 +299869,12.0,2 +769455,20.0,3 +316443,8.0,4 +1212864,24.0,5 +1397338,28.0,6 +223249,8.0,0 +191264,14.0,1 diff --git a/machine_learning/forecasting/run.py b/machine_learning/forecasting/run.py new file mode 100644 index 000000000000..0e11f958825f --- /dev/null +++ b/machine_learning/forecasting/run.py @@ -0,0 +1,157 @@ +""" +this is code for forecasting +but i modified it and used it for safety checker of data +for ex: you have a online shop and for some reason some data are +missing (the amount of data that u expected are not supposed to be) + then we can use it +*ps : 1. ofc we can use normal statistic method but in this case + the data is quite absurd and only a little^^ + 2. ofc u can use this and modified it for forecasting purpose + for the next 3 months sales or something, + u can just adjust it for ur own purpose +""" + +import numpy as np +import pandas as pd +from sklearn.preprocessing import Normalizer +from sklearn.svm import SVR +from statsmodels.tsa.statespace.sarimax import SARIMAX + + +def linear_regression_prediction( + train_dt: list, train_usr: list, train_mtch: list, test_dt: list, test_mtch: list +) -> float: + """ + First method: linear regression + input : training data (date, total_user, total_event) in list of float + output : list of total user prediction in float + >>> n = linear_regression_prediction([2,3,4,5], [5,3,4,6], [3,1,2,4], [2,1], [2,2]) + >>> abs(n - 5.0) < 1e-6 # Checking precision because of floating point errors + True + """ + x = [[1, item, train_mtch[i]] for i, item in enumerate(train_dt)] + x = np.array(x) + y = np.array(train_usr) + beta = np.dot(np.dot(np.linalg.inv(np.dot(x.transpose(), x)), x.transpose()), y) + return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2]) + + +def sarimax_predictor(train_user: list, train_match: list, test_match: list) -> float: + """ + second method: Sarimax + sarimax is a statistic method which using previous input + and learn its pattern to predict future data + input : training data (total_user, with exog data = total_event) in list of float + output : list of total user prediction in float + >>> sarimax_predictor([4,2,6,8], [3,1,2,4], [2]) + 6.6666671111109626 + """ + order = (1, 2, 1) + seasonal_order = (1, 1, 0, 7) + model = SARIMAX( + train_user, exog=train_match, order=order, seasonal_order=seasonal_order + ) + model_fit = model.fit(disp=False, maxiter=600, method="nm") + result = model_fit.predict(1, len(test_match), exog=[test_match]) + return result[0] + + +def support_vector_regressor(x_train: list, x_test: list, train_user: list) -> float: + """ + Third method: Support vector regressor + svr is quite the same with svm(support vector machine) + it uses the same principles as the SVM for classification, + with only a few minor differences and the only different is that + it suits better for regression purpose + input : training data (date, total_user, total_event) in list of float + where x = list of set (date and total event) + output : list of total user prediction in float + >>> support_vector_regressor([[5,2],[1,5],[6,2]], [[3,2]], [2,1,4]) + 1.634932078116079 + """ + regressor = SVR(kernel="rbf", C=1, gamma=0.1, epsilon=0.1) + regressor.fit(x_train, train_user) + y_pred = regressor.predict(x_test) + return y_pred[0] + + +def interquartile_range_checker(train_user: list) -> float: + """ + Optional method: interquatile range + input : list of total user in float + output : low limit of input in float + this method can be used to check whether some data is outlier or not + >>> interquartile_range_checker([1,2,3,4,5,6,7,8,9,10]) + 2.8 + """ + train_user.sort() + q1 = np.percentile(train_user, 25) + q3 = np.percentile(train_user, 75) + iqr = q3 - q1 + low_lim = q1 - (iqr * 0.1) + return low_lim + + +def data_safety_checker(list_vote: list, actual_result: float) -> None: + """ + Used to review all the votes (list result prediction) + and compare it to the actual result. + input : list of predictions + output : print whether it's safe or not + >>> data_safety_checker([2,3,4],5.0) + Today's data is not safe. + """ + safe = 0 + not_safe = 0 + for i in list_vote: + if i > actual_result: + safe = not_safe + 1 + else: + if abs(abs(i) - abs(actual_result)) <= 0.1: + safe = safe + 1 + else: + not_safe = not_safe + 1 + print(f"Today's data is {'not ' if safe <= not_safe else ''}safe.") + + +# data_input_df = pd.read_csv("ex_data.csv", header=None) +data_input = [[18231, 0.0, 1], [22621, 1.0, 2], [15675, 0.0, 3], [23583, 1.0, 4]] +data_input_df = pd.DataFrame(data_input, columns=["total_user", "total_even", "days"]) + +""" +data column = total user in a day, how much online event held in one day, +what day is that(sunday-saturday) +""" + +# start normalization +normalize_df = Normalizer().fit_transform(data_input_df.values) +# split data +total_date = normalize_df[:, 2].tolist() +total_user = normalize_df[:, 0].tolist() +total_match = normalize_df[:, 1].tolist() + +# for svr (input variable = total date and total match) +x = normalize_df[:, [1, 2]].tolist() +x_train = x[: len(x) - 1] +x_test = x[len(x) - 1 :] + +# for linear reression & sarimax +trn_date = total_date[: len(total_date) - 1] +trn_user = total_user[: len(total_user) - 1] +trn_match = total_match[: len(total_match) - 1] + +tst_date = total_date[len(total_date) - 1 :] +tst_user = total_user[len(total_user) - 1 :] +tst_match = total_match[len(total_match) - 1 :] + + +# voting system with forecasting +res_vote = [] +res_vote.append( + linear_regression_prediction(trn_date, trn_user, trn_match, tst_date, tst_match) +) +res_vote.append(sarimax_predictor(trn_user, trn_match, tst_match)) +res_vote.append(support_vector_regressor(x_train, x_test, trn_user)) + +# check the safety of todays'data^^ +data_safety_checker(res_vote, tst_user) diff --git a/machine_learning/gaussian_naive_bayes.py b/machine_learning/gaussian_naive_bayes.py new file mode 100644 index 000000000000..c200aa5a4d2d --- /dev/null +++ b/machine_learning/gaussian_naive_bayes.py @@ -0,0 +1,44 @@ +# Gaussian Naive Bayes Example +from matplotlib import pyplot as plt +from sklearn.datasets import load_iris +from sklearn.metrics import plot_confusion_matrix +from sklearn.model_selection import train_test_split +from sklearn.naive_bayes import GaussianNB + + +def main(): + + """ + Gaussian Naive Bayes Example using sklearn function. + Iris type dataset is used to demonstrate algorithm. + """ + + # Load Iris dataset + iris = load_iris() + + # Split dataset into train and test data + X = iris["data"] # features + Y = iris["target"] + x_train, x_test, y_train, y_test = train_test_split( + X, Y, test_size=0.3, random_state=1 + ) + + # Gaussian Naive Bayes + NB_model = GaussianNB() + NB_model.fit(x_train, y_train) + + # Display Confusion Matrix + plot_confusion_matrix( + NB_model, + x_test, + y_test, + display_labels=iris["target_names"], + cmap="Blues", + normalize="true", + ) + plt.title("Normalized Confusion Matrix - IRIS Dataset") + plt.show() + + +if __name__ == "__main__": + main() diff --git a/machine_learning/gradient_boosting_regressor.py b/machine_learning/gradient_boosting_regressor.py new file mode 100644 index 000000000000..0aa0e7a10ac5 --- /dev/null +++ b/machine_learning/gradient_boosting_regressor.py @@ -0,0 +1,66 @@ +"""Implementation of GradientBoostingRegressor in sklearn using the + boston dataset which is very popular for regression problem to + predict house price. +""" + +import matplotlib.pyplot as plt +import pandas as pd +from sklearn.datasets import load_boston +from sklearn.ensemble import GradientBoostingRegressor +from sklearn.metrics import mean_squared_error, r2_score +from sklearn.model_selection import train_test_split + + +def main(): + + # loading the dataset from the sklearn + df = load_boston() + print(df.keys()) + # now let construct a data frame + df_boston = pd.DataFrame(df.data, columns=df.feature_names) + # let add the target to the dataframe + df_boston["Price"] = df.target + # print the first five rows using the head function + print(df_boston.head()) + # Summary statistics + print(df_boston.describe().T) + # Feature selection + + X = df_boston.iloc[:, :-1] + y = df_boston.iloc[:, -1] # target variable + # split the data with 75% train and 25% test sets. + X_train, X_test, y_train, y_test = train_test_split( + X, y, random_state=0, test_size=0.25 + ) + + model = GradientBoostingRegressor( + n_estimators=500, max_depth=5, min_samples_split=4, learning_rate=0.01 + ) + # training the model + model.fit(X_train, y_train) + # to see how good the model fit the data + training_score = model.score(X_train, y_train).round(3) + test_score = model.score(X_test, y_test).round(3) + print("Training score of GradientBoosting is :", training_score) + print("The test score of GradientBoosting is :", test_score) + # Let us evaluation the model by finding the errors + y_pred = model.predict(X_test) + + # The mean squared error + print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred)) + # Explained variance score: 1 is perfect prediction + print("Test Variance score: %.2f" % r2_score(y_test, y_pred)) + + # So let's run the model against the test data + fig, ax = plt.subplots() + ax.scatter(y_test, y_pred, edgecolors=(0, 0, 0)) + ax.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], "k--", lw=4) + ax.set_xlabel("Actual") + ax.set_ylabel("Predicted") + ax.set_title("Truth vs Predicted") + # this show function will display the plotting + plt.show() + + +if __name__ == "__main__": + main() diff --git a/machine_learning/gradient_descent.py b/machine_learning/gradient_descent.py index 6387d4939205..9fa460a07562 100644 --- a/machine_learning/gradient_descent.py +++ b/machine_learning/gradient_descent.py @@ -1,25 +1,32 @@ """ -Implementation of gradient descent algorithm for minimizing cost of a linear hypothesis function. +Implementation of gradient descent algorithm for minimizing cost of a linear hypothesis +function. """ -from __future__ import print_function, division import numpy # List of input, output pairs -train_data = (((5, 2, 3), 15), ((6, 5, 9), 25), - ((11, 12, 13), 41), ((1, 1, 1), 8), ((11, 12, 13), 41)) +train_data = ( + ((5, 2, 3), 15), + ((6, 5, 9), 25), + ((11, 12, 13), 41), + ((1, 1, 1), 8), + ((11, 12, 13), 41), +) test_data = (((515, 22, 13), 555), ((61, 35, 49), 150)) parameter_vector = [2, 4, 1, 5] m = len(train_data) LEARNING_RATE = 0.009 -def _error(example_no, data_set='train'): +def _error(example_no, data_set="train"): """ :param data_set: train data or test data :param example_no: example number whose error has to be checked :return: error in example pointed by example number. """ - return calculate_hypothesis_value(example_no, data_set) - output(example_no, data_set) + return calculate_hypothesis_value(example_no, data_set) - output( + example_no, data_set + ) def _hypothesis_value(data_input_tuple): @@ -33,7 +40,7 @@ def _hypothesis_value(data_input_tuple): """ hyp_val = 0 for i in range(len(parameter_vector) - 1): - hyp_val += data_input_tuple[i]*parameter_vector[i+1] + hyp_val += data_input_tuple[i] * parameter_vector[i + 1] hyp_val += parameter_vector[0] return hyp_val @@ -44,9 +51,9 @@ def output(example_no, data_set): :param example_no: example whose output is to be fetched :return: output for that example """ - if data_set == 'train': + if data_set == "train": return train_data[example_no][1] - elif data_set == 'test': + elif data_set == "test": return test_data[example_no][1] @@ -69,14 +76,15 @@ def summation_of_cost_derivative(index, end=m): :param index: index wrt derivative is being calculated :param end: value where summation ends, default is m, number of examples :return: Returns the summation of cost derivative - Note: If index is -1, this means we are calculating summation wrt to biased parameter. + Note: If index is -1, this means we are calculating summation wrt to biased + parameter. """ summation_value = 0 for i in range(end): if index == -1: summation_value += _error(i) else: - summation_value += _error(i)*train_data[i][0][index] + summation_value += _error(i) * train_data[i][0][index] return summation_value @@ -84,9 +92,10 @@ def get_cost_derivative(index): """ :param index: index of the parameter vector wrt to derivative is to be calculated :return: derivative wrt to that index - Note: If index is -1, this means we are calculating summation wrt to biased parameter. + Note: If index is -1, this means we are calculating summation wrt to biased + parameter. """ - cost_derivative_value = summation_of_cost_derivative(index, m)/m + cost_derivative_value = summation_of_cost_derivative(index, m) / m return cost_derivative_value @@ -100,11 +109,16 @@ def run_gradient_descent(): j += 1 temp_parameter_vector = [0, 0, 0, 0] for i in range(0, len(parameter_vector)): - cost_derivative = get_cost_derivative(i-1) - temp_parameter_vector[i] = parameter_vector[i] - \ - LEARNING_RATE*cost_derivative - if numpy.allclose(parameter_vector, temp_parameter_vector, - atol=absolute_error_limit, rtol=relative_error_limit): + cost_derivative = get_cost_derivative(i - 1) + temp_parameter_vector[i] = ( + parameter_vector[i] - LEARNING_RATE * cost_derivative + ) + if numpy.allclose( + parameter_vector, + temp_parameter_vector, + atol=absolute_error_limit, + rtol=relative_error_limit, + ): break parameter_vector = temp_parameter_vector print(("Number of iterations:", j)) @@ -112,11 +126,11 @@ def run_gradient_descent(): def test_gradient_descent(): for i in range(len(test_data)): - print(("Actual output value:", output(i, 'test'))) - print(("Hypothesis output:", calculate_hypothesis_value(i, 'test'))) + print(("Actual output value:", output(i, "test"))) + print(("Hypothesis output:", calculate_hypothesis_value(i, "test"))) -if __name__ == '__main__': +if __name__ == "__main__": run_gradient_descent() print("\nTesting gradient descent for a linear hypothesis function.\n") test_gradient_descent() diff --git a/machine_learning/k_means_clust.py b/machine_learning/k_means_clust.py index 368739a45fe9..f155d4845f41 100644 --- a/machine_learning/k_means_clust.py +++ b/machine_learning/k_means_clust.py @@ -1,173 +1,353 @@ -'''README, Author - Anurag Kumar(mailto:anuragkumarak95@gmail.com) - +"""README, Author - Anurag Kumar(mailto:anuragkumarak95@gmail.com) Requirements: - sklearn - numpy - matplotlib - Python: - 3.5 - Inputs: - X , a 2D numpy array of features. - k , number of clusters to create. - - initial_centroids , initial centroid values generated by utility function(mentioned in usage). + - initial_centroids , initial centroid values generated by utility function(mentioned + in usage). - maxiter , maximum number of iterations to process. - - heterogeneity , empty list that will be filled with hetrogeneity values if passed to kmeans func. - + - heterogeneity , empty list that will be filled with hetrogeneity values if passed + to kmeans func. Usage: 1. define 'k' value, 'X' features array and 'hetrogeneity' empty list - 2. create initial_centroids, initial_centroids = get_initial_centroids( - X, - k, - seed=0 # seed value for initial centroid generation, None for randomness(default=None) + X, + k, + seed=0 # seed value for initial centroid generation, + # None for randomness(default=None) ) - 3. find centroids and clusters using kmeans function. - centroids, cluster_assignment = kmeans( - X, - k, - initial_centroids, + X, + k, + initial_centroids, maxiter=400, - record_heterogeneity=heterogeneity, + record_heterogeneity=heterogeneity, verbose=True # whether to print logs in console or not.(default=False) ) - - - 4. Plot the loss function, hetrogeneity values for every iteration saved in hetrogeneity list. + 4. Plot the loss function, hetrogeneity values for every iteration saved in + hetrogeneity list. plot_heterogeneity( - heterogeneity, + heterogeneity, k ) - - 5. Have fun.. - -''' -from __future__ import print_function -from sklearn.metrics import pairwise_distances + 5. Transfers Dataframe into excel format it must have feature called + 'Clust' with k means clustering numbers in it. +""" +import warnings + import numpy as np +import pandas as pd +from matplotlib import pyplot as plt +from sklearn.metrics import pairwise_distances + +warnings.filterwarnings("ignore") + +TAG = "K-MEANS-CLUST/ " -TAG = 'K-MEANS-CLUST/ ' def get_initial_centroids(data, k, seed=None): - '''Randomly choose k data points as initial centroids''' - if seed is not None: # useful for obtaining consistent results + """Randomly choose k data points as initial centroids""" + if seed is not None: # useful for obtaining consistent results np.random.seed(seed) - n = data.shape[0] # number of data points - + n = data.shape[0] # number of data points + # Pick K indices from range [0, N). rand_indices = np.random.randint(0, n, k) - + # Keep centroids as dense format, as many entries will be nonzero due to averaging. # As long as at least one document in a cluster contains a word, # it will carry a nonzero weight in the TF-IDF vector of the centroid. - centroids = data[rand_indices,:] - + centroids = data[rand_indices, :] + return centroids -def centroid_pairwise_dist(X,centroids): - return pairwise_distances(X,centroids,metric='euclidean') + +def centroid_pairwise_dist(X, centroids): + return pairwise_distances(X, centroids, metric="euclidean") + def assign_clusters(data, centroids): - + # Compute distances between each data point and the set of centroids: # Fill in the blank (RHS only) - distances_from_centroids = centroid_pairwise_dist(data,centroids) - + distances_from_centroids = centroid_pairwise_dist(data, centroids) + # Compute cluster assignments for each data point: # Fill in the blank (RHS only) - cluster_assignment = np.argmin(distances_from_centroids,axis=1) - + cluster_assignment = np.argmin(distances_from_centroids, axis=1) + return cluster_assignment + def revise_centroids(data, k, cluster_assignment): new_centroids = [] for i in range(k): # Select all data points that belong to cluster i. Fill in the blank (RHS only) - member_data_points = data[cluster_assignment==i] + member_data_points = data[cluster_assignment == i] # Compute the mean of the data points. Fill in the blank (RHS only) centroid = member_data_points.mean(axis=0) new_centroids.append(centroid) new_centroids = np.array(new_centroids) - + return new_centroids + def compute_heterogeneity(data, k, centroids, cluster_assignment): - + heterogeneity = 0.0 for i in range(k): - + # Select all data points that belong to cluster i. Fill in the blank (RHS only) - member_data_points = data[cluster_assignment==i, :] - - if member_data_points.shape[0] > 0: # check if i-th cluster is non-empty + member_data_points = data[cluster_assignment == i, :] + + if member_data_points.shape[0] > 0: # check if i-th cluster is non-empty # Compute distances from centroid to data points (RHS only) - distances = pairwise_distances(member_data_points, [centroids[i]], metric='euclidean') - squared_distances = distances**2 + distances = pairwise_distances( + member_data_points, [centroids[i]], metric="euclidean" + ) + squared_distances = distances ** 2 heterogeneity += np.sum(squared_distances) - + return heterogeneity -from matplotlib import pyplot as plt + def plot_heterogeneity(heterogeneity, k): - plt.figure(figsize=(7,4)) + plt.figure(figsize=(7, 4)) plt.plot(heterogeneity, linewidth=4) - plt.xlabel('# Iterations') - plt.ylabel('Heterogeneity') - plt.title('Heterogeneity of clustering over time, K={0:d}'.format(k)) - plt.rcParams.update({'font.size': 16}) + plt.xlabel("# Iterations") + plt.ylabel("Heterogeneity") + plt.title(f"Heterogeneity of clustering over time, K={k:d}") + plt.rcParams.update({"font.size": 16}) plt.show() -def kmeans(data, k, initial_centroids, maxiter=500, record_heterogeneity=None, verbose=False): - '''This function runs k-means on given data and initial set of centroids. - maxiter: maximum number of iterations to run.(default=500) - record_heterogeneity: (optional) a list, to store the history of heterogeneity as function of iterations - if None, do not store the history. - verbose: if True, print how many data points changed their cluster labels in each iteration''' + +def kmeans( + data, k, initial_centroids, maxiter=500, record_heterogeneity=None, verbose=False +): + """This function runs k-means on given data and initial set of centroids. + maxiter: maximum number of iterations to run.(default=500) + record_heterogeneity: (optional) a list, to store the history of heterogeneity + as function of iterations + if None, do not store the history. + verbose: if True, print how many data points changed their cluster labels in + each iteration""" centroids = initial_centroids[:] prev_cluster_assignment = None - - for itr in range(maxiter): + + for itr in range(maxiter): if verbose: - print(itr, end='') - + print(itr, end="") + # 1. Make cluster assignments using nearest centroids - cluster_assignment = assign_clusters(data,centroids) - - # 2. Compute a new centroid for each of the k clusters, averaging all data points assigned to that cluster. - centroids = revise_centroids(data,k, cluster_assignment) - + cluster_assignment = assign_clusters(data, centroids) + + # 2. Compute a new centroid for each of the k clusters, averaging all data + # points assigned to that cluster. + centroids = revise_centroids(data, k, cluster_assignment) + # Check for convergence: if none of the assignments changed, stop - if prev_cluster_assignment is not None and \ - (prev_cluster_assignment==cluster_assignment).all(): + if ( + prev_cluster_assignment is not None + and (prev_cluster_assignment == cluster_assignment).all() + ): break - - # Print number of new assignments + + # Print number of new assignments if prev_cluster_assignment is not None: - num_changed = np.sum(prev_cluster_assignment!=cluster_assignment) + num_changed = np.sum(prev_cluster_assignment != cluster_assignment) if verbose: - print(' {0:5d} elements changed their cluster assignment.'.format(num_changed)) - + print( + " {:5d} elements changed their cluster assignment.".format( + num_changed + ) + ) + # Record heterogeneity convergence metric if record_heterogeneity is not None: # YOUR CODE HERE - score = compute_heterogeneity(data,k,centroids,cluster_assignment) + score = compute_heterogeneity(data, k, centroids, cluster_assignment) record_heterogeneity.append(score) - + prev_cluster_assignment = cluster_assignment[:] - + return centroids, cluster_assignment + # Mock test below -if False: # change to true to run this test case. - import sklearn.datasets as ds +if False: # change to true to run this test case. + from sklearn import datasets as ds + dataset = ds.load_iris() k = 3 heterogeneity = [] - initial_centroids = get_initial_centroids(dataset['data'], k, seed=0) - centroids, cluster_assignment = kmeans(dataset['data'], k, initial_centroids, maxiter=400, - record_heterogeneity=heterogeneity, verbose=True) + initial_centroids = get_initial_centroids(dataset["data"], k, seed=0) + centroids, cluster_assignment = kmeans( + dataset["data"], + k, + initial_centroids, + maxiter=400, + record_heterogeneity=heterogeneity, + verbose=True, + ) plot_heterogeneity(heterogeneity, k) + + +def ReportGenerator( + df: pd.DataFrame, ClusteringVariables: np.array, FillMissingReport=None +) -> pd.DataFrame: + """ + Function generates easy-erading clustering report. It takes 2 arguments as an input: + DataFrame - dataframe with predicted cluester column; + FillMissingReport - dictionary of rules how we are going to fill missing + values of for final report generate (not included in modeling); + in order to run the function following libraries must be imported: + import pandas as pd + import numpy as np + >>> data = pd.DataFrame() + >>> data['numbers'] = [1, 2, 3] + >>> data['col1'] = [0.5, 2.5, 4.5] + >>> data['col2'] = [100, 200, 300] + >>> data['col3'] = [10, 20, 30] + >>> data['Cluster'] = [1, 1, 2] + >>> ReportGenerator(data, ['col1', 'col2'], 0) + Features Type Mark 1 2 + 0 # of Customers ClusterSize False 2.000000 1.000000 + 1 % of Customers ClusterProportion False 0.666667 0.333333 + 2 col1 mean_with_zeros True 1.500000 4.500000 + 3 col2 mean_with_zeros True 150.000000 300.000000 + 4 numbers mean_with_zeros False 1.500000 3.000000 + .. ... ... ... ... ... + 99 dummy 5% False 1.000000 1.000000 + 100 dummy 95% False 1.000000 1.000000 + 101 dummy stdev False 0.000000 NaN + 102 dummy mode False 1.000000 1.000000 + 103 dummy median False 1.000000 1.000000 + + [104 rows x 5 columns] + """ + # Fill missing values with given rules + if FillMissingReport: + df.fillna(value=FillMissingReport, inplace=True) + df["dummy"] = 1 + numeric_cols = df.select_dtypes(np.number).columns + report = ( + df.groupby(["Cluster"])[ # construct report dataframe + numeric_cols + ] # group by cluster number + .agg( + [ + ("sum", np.sum), + ("mean_with_zeros", lambda x: np.mean(np.nan_to_num(x))), + ("mean_without_zeros", lambda x: x.replace(0, np.NaN).mean()), + ( + "mean_25-75", + lambda x: np.mean( + np.nan_to_num( + sorted(x)[ + round(len(x) * 25 / 100) : round(len(x) * 75 / 100) + ] + ) + ), + ), + ("mean_with_na", np.mean), + ("min", lambda x: x.min()), + ("5%", lambda x: x.quantile(0.05)), + ("25%", lambda x: x.quantile(0.25)), + ("50%", lambda x: x.quantile(0.50)), + ("75%", lambda x: x.quantile(0.75)), + ("95%", lambda x: x.quantile(0.95)), + ("max", lambda x: x.max()), + ("count", lambda x: x.count()), + ("stdev", lambda x: x.std()), + ("mode", lambda x: x.mode()[0]), + ("median", lambda x: x.median()), + ("# > 0", lambda x: (x > 0).sum()), + ] + ) + .T.reset_index() + .rename(index=str, columns={"level_0": "Features", "level_1": "Type"}) + ) # rename columns + # calculate the size of cluster(count of clientID's) + clustersize = report[ + (report["Features"] == "dummy") & (report["Type"] == "count") + ].copy() # avoid SettingWithCopyWarning + clustersize.Type = ( + "ClusterSize" # rename created cluster df to match report column names + ) + clustersize.Features = "# of Customers" + clusterproportion = pd.DataFrame( + clustersize.iloc[:, 2:].values + / clustersize.iloc[:, 2:].values.sum() # calculating the proportion of cluster + ) + clusterproportion[ + "Type" + ] = "% of Customers" # rename created cluster df to match report column names + clusterproportion["Features"] = "ClusterProportion" + cols = clusterproportion.columns.tolist() + cols = cols[-2:] + cols[:-2] + clusterproportion = clusterproportion[cols] # rearrange columns to match report + clusterproportion.columns = report.columns + a = pd.DataFrame( + abs( + report[report["Type"] == "count"].iloc[:, 2:].values + - clustersize.iloc[:, 2:].values + ) + ) # generating df with count of nan values + a["Features"] = 0 + a["Type"] = "# of nan" + a.Features = report[ + report["Type"] == "count" + ].Features.tolist() # filling values in order to match report + cols = a.columns.tolist() + cols = cols[-2:] + cols[:-2] + a = a[cols] # rearrange columns to match report + a.columns = report.columns # rename columns to match report + report = report.drop( + report[report.Type == "count"].index + ) # drop count values except cluster size + report = pd.concat( + [report, a, clustersize, clusterproportion], axis=0 + ) # concat report with clustert size and nan values + report["Mark"] = report["Features"].isin(ClusteringVariables) + cols = report.columns.tolist() + cols = cols[0:2] + cols[-1:] + cols[2:-1] + report = report[cols] + sorter1 = { + "ClusterSize": 9, + "ClusterProportion": 8, + "mean_with_zeros": 7, + "mean_with_na": 6, + "max": 5, + "50%": 4, + "min": 3, + "25%": 2, + "75%": 1, + "# of nan": 0, + "# > 0": -1, + "sum_with_na": -2, + } + report = ( + report.assign( + Sorter1=lambda x: x.Type.map(sorter1), + Sorter2=lambda x: list(reversed(range(len(x)))), + ) + .sort_values(["Sorter1", "Mark", "Sorter2"], ascending=False) + .drop(["Sorter1", "Sorter2"], axis=1) + ) + report.columns.name = "" + report = report.reset_index() + report.drop(columns=["index"], inplace=True) + return report + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/machine_learning/k_nearest_neighbours.py b/machine_learning/k_nearest_neighbours.py new file mode 100644 index 000000000000..e90ea09a58c1 --- /dev/null +++ b/machine_learning/k_nearest_neighbours.py @@ -0,0 +1,58 @@ +from collections import Counter + +import numpy as np +from sklearn import datasets +from sklearn.model_selection import train_test_split + +data = datasets.load_iris() + +X = np.array(data["data"]) +y = np.array(data["target"]) +classes = data["target_names"] + +X_train, X_test, y_train, y_test = train_test_split(X, y) + + +def euclidean_distance(a, b): + """ + Gives the euclidean distance between two points + >>> euclidean_distance([0, 0], [3, 4]) + 5.0 + >>> euclidean_distance([1, 2, 3], [1, 8, 11]) + 10.0 + """ + return np.linalg.norm(np.array(a) - np.array(b)) + + +def classifier(train_data, train_target, classes, point, k=5): + """ + Classifies the point using the KNN algorithm + k closest points are found (ranked in ascending order of euclidean distance) + Params: + :train_data: Set of points that are classified into two or more classes + :train_target: List of classes in the order of train_data points + :classes: Labels of the classes + :point: The data point that needs to be classifed + + >>> X_train = [[0, 0], [1, 0], [0, 1], [0.5, 0.5], [3, 3], [2, 3], [3, 2]] + >>> y_train = [0, 0, 0, 0, 1, 1, 1] + >>> classes = ['A','B']; point = [1.2,1.2] + >>> classifier(X_train, y_train, classes,point) + 'A' + """ + data = zip(train_data, train_target) + # List of distances of all points from the point to be classified + distances = [] + for data_point in data: + distance = euclidean_distance(data_point[0], point) + distances.append((distance, data_point[1])) + # Choosing 'k' points with the least distances. + votes = [i[1] for i in sorted(distances)[:k]] + # Most commonly occurring class among them + # is the class into which the point is classified + result = Counter(votes).most_common(1)[0][0] + return classes[result] + + +if __name__ == "__main__": + print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4])) diff --git a/machine_learning/knn_sklearn.py b/machine_learning/knn_sklearn.py new file mode 100644 index 000000000000..9a9114102ff3 --- /dev/null +++ b/machine_learning/knn_sklearn.py @@ -0,0 +1,31 @@ +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +from sklearn.neighbors import KNeighborsClassifier + +# Load iris file +iris = load_iris() +iris.keys() + + +print(f"Target names: \n {iris.target_names} ") +print(f"\n Features: \n {iris.feature_names}") + +# Train set e Test set +X_train, X_test, y_train, y_test = train_test_split( + iris["data"], iris["target"], random_state=4 +) + +# KNN + +knn = KNeighborsClassifier(n_neighbors=1) +knn.fit(X_train, y_train) + +# new array to test +X_new = [[1, 2, 1, 4], [2, 3, 4, 5]] + +prediction = knn.predict(X_new) + +print( + "\nNew array: \n {}" + "\n\nTarget Names Prediction: \n {}".format(X_new, iris["target_names"][prediction]) +) diff --git a/machine_learning/linear_discriminant_analysis.py b/machine_learning/linear_discriminant_analysis.py new file mode 100644 index 000000000000..0d19e970e973 --- /dev/null +++ b/machine_learning/linear_discriminant_analysis.py @@ -0,0 +1,405 @@ +""" + Linear Discriminant Analysis + + + + Assumptions About Data : + 1. The input variables has a gaussian distribution. + 2. The variance calculated for each input variables by class grouping is the + same. + 3. The mix of classes in your training set is representative of the problem. + + + Learning The Model : + The LDA model requires the estimation of statistics from the training data : + 1. Mean of each input value for each class. + 2. Probability of an instance belong to each class. + 3. Covariance for the input data for each class + + Calculate the class means : + mean(x) = 1/n ( for i = 1 to i = n --> sum(xi)) + + Calculate the class probabilities : + P(y = 0) = count(y = 0) / (count(y = 0) + count(y = 1)) + P(y = 1) = count(y = 1) / (count(y = 0) + count(y = 1)) + + Calculate the variance : + We can calculate the variance for dataset in two steps : + 1. Calculate the squared difference for each input variable from the + group mean. + 2. Calculate the mean of the squared difference. + ------------------------------------------------ + Squared_Difference = (x - mean(k)) ** 2 + Variance = (1 / (count(x) - count(classes))) * + (for i = 1 to i = n --> sum(Squared_Difference(xi))) + + Making Predictions : + discriminant(x) = x * (mean / variance) - + ((mean ** 2) / (2 * variance)) + Ln(probability) + --------------------------------------------------------------------------- + After calculating the discriminant value for each class, the class with the + largest discriminant value is taken as the prediction. + + Author: @EverLookNeverSee +""" +from math import log +from os import name, system +from random import gauss, seed +from typing import Callable, TypeVar + + +# Make a training dataset drawn from a gaussian distribution +def gaussian_distribution(mean: float, std_dev: float, instance_count: int) -> list: + """ + Generate gaussian distribution instances based-on given mean and standard deviation + :param mean: mean value of class + :param std_dev: value of standard deviation entered by usr or default value of it + :param instance_count: instance number of class + :return: a list containing generated values based-on given mean, std_dev and + instance_count + + >>> gaussian_distribution(5.0, 1.0, 20) # doctest: +NORMALIZE_WHITESPACE + [6.288184753155463, 6.4494456086997705, 5.066335808938262, 4.235456349028368, + 3.9078267848958586, 5.031334516831717, 3.977896829989127, 3.56317055489747, + 5.199311976483754, 5.133374604658605, 5.546468300338232, 4.086029056264687, + 5.005005283626573, 4.935258239627312, 3.494170998739258, 5.537997178661033, + 5.320711100998849, 7.3891120432406865, 5.202969177309964, 4.855297691835079] + """ + seed(1) + return [gauss(mean, std_dev) for _ in range(instance_count)] + + +# Make corresponding Y flags to detecting classes +def y_generator(class_count: int, instance_count: list) -> list: + """ + Generate y values for corresponding classes + :param class_count: Number of classes(data groupings) in dataset + :param instance_count: number of instances in class + :return: corresponding values for data groupings in dataset + + >>> y_generator(1, [10]) + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + >>> y_generator(2, [5, 10]) + [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] + >>> y_generator(4, [10, 5, 15, 20]) # doctest: +NORMALIZE_WHITESPACE + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3] + """ + + return [k for k in range(class_count) for _ in range(instance_count[k])] + + +# Calculate the class means +def calculate_mean(instance_count: int, items: list) -> float: + """ + Calculate given class mean + :param instance_count: Number of instances in class + :param items: items that related to specific class(data grouping) + :return: calculated actual mean of considered class + + >>> items = gaussian_distribution(5.0, 1.0, 20) + >>> calculate_mean(len(items), items) + 5.011267842911003 + """ + # the sum of all items divided by number of instances + return sum(items) / instance_count + + +# Calculate the class probabilities +def calculate_probabilities(instance_count: int, total_count: int) -> float: + """ + Calculate the probability that a given instance will belong to which class + :param instance_count: number of instances in class + :param total_count: the number of all instances + :return: value of probability for considered class + + >>> calculate_probabilities(20, 60) + 0.3333333333333333 + >>> calculate_probabilities(30, 100) + 0.3 + """ + # number of instances in specific class divided by number of all instances + return instance_count / total_count + + +# Calculate the variance +def calculate_variance(items: list, means: list, total_count: int) -> float: + """ + Calculate the variance + :param items: a list containing all items(gaussian distribution of all classes) + :param means: a list containing real mean values of each class + :param total_count: the number of all instances + :return: calculated variance for considered dataset + + >>> items = gaussian_distribution(5.0, 1.0, 20) + >>> means = [5.011267842911003] + >>> total_count = 20 + >>> calculate_variance([items], means, total_count) + 0.9618530973487491 + """ + squared_diff = [] # An empty list to store all squared differences + # iterate over number of elements in items + for i in range(len(items)): + # for loop iterates over number of elements in inner layer of items + for j in range(len(items[i])): + # appending squared differences to 'squared_diff' list + squared_diff.append((items[i][j] - means[i]) ** 2) + + # one divided by (the number of all instances - number of classes) multiplied by + # sum of all squared differences + n_classes = len(means) # Number of classes in dataset + return 1 / (total_count - n_classes) * sum(squared_diff) + + +# Making predictions +def predict_y_values( + x_items: list, means: list, variance: float, probabilities: list +) -> list: + """This function predicts new indexes(groups for our data) + :param x_items: a list containing all items(gaussian distribution of all classes) + :param means: a list containing real mean values of each class + :param variance: calculated value of variance by calculate_variance function + :param probabilities: a list containing all probabilities of classes + :return: a list containing predicted Y values + + >>> x_items = [[6.288184753155463, 6.4494456086997705, 5.066335808938262, + ... 4.235456349028368, 3.9078267848958586, 5.031334516831717, + ... 3.977896829989127, 3.56317055489747, 5.199311976483754, + ... 5.133374604658605, 5.546468300338232, 4.086029056264687, + ... 5.005005283626573, 4.935258239627312, 3.494170998739258, + ... 5.537997178661033, 5.320711100998849, 7.3891120432406865, + ... 5.202969177309964, 4.855297691835079], [11.288184753155463, + ... 11.44944560869977, 10.066335808938263, 9.235456349028368, + ... 8.907826784895859, 10.031334516831716, 8.977896829989128, + ... 8.56317055489747, 10.199311976483754, 10.133374604658606, + ... 10.546468300338232, 9.086029056264687, 10.005005283626572, + ... 9.935258239627313, 8.494170998739259, 10.537997178661033, + ... 10.320711100998848, 12.389112043240686, 10.202969177309964, + ... 9.85529769183508], [16.288184753155463, 16.449445608699772, + ... 15.066335808938263, 14.235456349028368, 13.907826784895859, + ... 15.031334516831716, 13.977896829989128, 13.56317055489747, + ... 15.199311976483754, 15.133374604658606, 15.546468300338232, + ... 14.086029056264687, 15.005005283626572, 14.935258239627313, + ... 13.494170998739259, 15.537997178661033, 15.320711100998848, + ... 17.389112043240686, 15.202969177309964, 14.85529769183508]] + + >>> means = [5.011267842911003, 10.011267842911003, 15.011267842911002] + >>> variance = 0.9618530973487494 + >>> probabilities = [0.3333333333333333, 0.3333333333333333, 0.3333333333333333] + >>> predict_y_values(x_items, means, variance, + ... probabilities) # doctest: +NORMALIZE_WHITESPACE + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2] + + """ + # An empty list to store generated discriminant values of all items in dataset for + # each class + results = [] + # for loop iterates over number of elements in list + for i in range(len(x_items)): + # for loop iterates over number of inner items of each element + for j in range(len(x_items[i])): + temp = [] # to store all discriminant values of each item as a list + # for loop iterates over number of classes we have in our dataset + for k in range(len(x_items)): + # appending values of discriminants for each class to 'temp' list + temp.append( + x_items[i][j] * (means[k] / variance) + - (means[k] ** 2 / (2 * variance)) + + log(probabilities[k]) + ) + # appending discriminant values of each item to 'results' list + results.append(temp) + + return [result.index(max(result)) for result in results] + + +# Calculating Accuracy +def accuracy(actual_y: list, predicted_y: list) -> float: + """ + Calculate the value of accuracy based-on predictions + :param actual_y:a list containing initial Y values generated by 'y_generator' + function + :param predicted_y: a list containing predicted Y values generated by + 'predict_y_values' function + :return: percentage of accuracy + + >>> actual_y = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, + ... 1, 1 ,1 ,1 ,1 ,1 ,1] + >>> predicted_y = [0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, + ... 0, 0, 1, 1, 1, 0, 1, 1, 1] + >>> accuracy(actual_y, predicted_y) + 50.0 + + >>> actual_y = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, + ... 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] + >>> predicted_y = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + ... 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] + >>> accuracy(actual_y, predicted_y) + 100.0 + """ + # iterate over one element of each list at a time (zip mode) + # prediction is correct if actual Y value equals to predicted Y value + correct = sum(1 for i, j in zip(actual_y, predicted_y) if i == j) + # percentage of accuracy equals to number of correct predictions divided by number + # of all data and multiplied by 100 + return (correct / len(actual_y)) * 100 + + +num = TypeVar("num") + + +def valid_input( + input_type: Callable[[object], num], # Usually float or int + input_msg: str, + err_msg: str, + condition: Callable[[num], bool] = lambda x: True, + default: str = None, +) -> num: + """ + Ask for user value and validate that it fulfill a condition. + + :input_type: user input expected type of value + :input_msg: message to show user in the screen + :err_msg: message to show in the screen in case of error + :condition: function that represents the condition that user input is valid. + :default: Default value in case the user does not type anything + :return: user's input + """ + while True: + try: + user_input = input_type(input(input_msg).strip() or default) + if condition(user_input): + return user_input + else: + print(f"{user_input}: {err_msg}") + continue + except ValueError: + print( + f"{user_input}: Incorrect input type, expected {input_type.__name__!r}" + ) + + +# Main Function +def main(): + """ This function starts execution phase """ + while True: + print(" Linear Discriminant Analysis ".center(50, "*")) + print("*" * 50, "\n") + print("First of all we should specify the number of classes that") + print("we want to generate as training dataset") + # Trying to get number of classes + n_classes = valid_input( + input_type=int, + condition=lambda x: x > 0, + input_msg="Enter the number of classes (Data Groupings): ", + err_msg="Number of classes should be positive!", + ) + + print("-" * 100) + + # Trying to get the value of standard deviation + std_dev = valid_input( + input_type=float, + condition=lambda x: x >= 0, + input_msg=( + "Enter the value of standard deviation" + "(Default value is 1.0 for all classes): " + ), + err_msg="Standard deviation should not be negative!", + default="1.0", + ) + + print("-" * 100) + + # Trying to get number of instances in classes and theirs means to generate + # dataset + counts = [] # An empty list to store instance counts of classes in dataset + for i in range(n_classes): + user_count = valid_input( + input_type=int, + condition=lambda x: x > 0, + input_msg=(f"Enter The number of instances for class_{i+1}: "), + err_msg="Number of instances should be positive!", + ) + counts.append(user_count) + print("-" * 100) + + # An empty list to store values of user-entered means of classes + user_means = [] + for a in range(n_classes): + user_mean = valid_input( + input_type=float, + input_msg=(f"Enter the value of mean for class_{a+1}: "), + err_msg="This is an invalid value.", + ) + user_means.append(user_mean) + print("-" * 100) + + print("Standard deviation: ", std_dev) + # print out the number of instances in classes in separated line + for i, count in enumerate(counts, 1): + print(f"Number of instances in class_{i} is: {count}") + print("-" * 100) + + # print out mean values of classes separated line + for i, user_mean in enumerate(user_means, 1): + print(f"Mean of class_{i} is: {user_mean}") + print("-" * 100) + + # Generating training dataset drawn from gaussian distribution + x = [ + gaussian_distribution(user_means[j], std_dev, counts[j]) + for j in range(n_classes) + ] + print("Generated Normal Distribution: \n", x) + print("-" * 100) + + # Generating Ys to detecting corresponding classes + y = y_generator(n_classes, counts) + print("Generated Corresponding Ys: \n", y) + print("-" * 100) + + # Calculating the value of actual mean for each class + actual_means = [calculate_mean(counts[k], x[k]) for k in range(n_classes)] + # for loop iterates over number of elements in 'actual_means' list and print + # out them in separated line + for i, actual_mean in enumerate(actual_means, 1): + print(f"Actual(Real) mean of class_{i} is: {actual_mean}") + print("-" * 100) + + # Calculating the value of probabilities for each class + probabilities = [ + calculate_probabilities(counts[i], sum(counts)) for i in range(n_classes) + ] + + # for loop iterates over number of elements in 'probabilities' list and print + # out them in separated line + for i, probability in enumerate(probabilities, 1): + print(f"Probability of class_{i} is: {probability}") + print("-" * 100) + + # Calculating the values of variance for each class + variance = calculate_variance(x, actual_means, sum(counts)) + print("Variance: ", variance) + print("-" * 100) + + # Predicting Y values + # storing predicted Y values in 'pre_indexes' variable + pre_indexes = predict_y_values(x, actual_means, variance, probabilities) + print("-" * 100) + + # Calculating Accuracy of the model + print(f"Accuracy: {accuracy(y, pre_indexes)}") + print("-" * 100) + print(" DONE ".center(100, "+")) + + if input("Press any key to restart or 'q' for quit: ").strip().lower() == "q": + print("\n" + "GoodBye!".center(100, "-") + "\n") + break + system("cls" if name == "nt" else "clear") + + +if __name__ == "__main__": + main() diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index 8c23f1f77908..a726629efe00 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -1,39 +1,38 @@ """ Linear regression is the most basic type of regression commonly used for -predictive analysis. The idea is preety simple, we have a dataset and we have -a feature's associated with it. The Features should be choose very cautiously -as they determine, how much our model will be able to make future predictions. -We try to set these Feature weights, over many iterations, so that they best -fits our dataset. In this particular code, i had used a CSGO dataset (ADR vs +predictive analysis. The idea is pretty simple: we have a dataset and we have +features associated with it. Features should be chosen very cautiously +as they determine how much our model will be able to make future predictions. +We try to set the weight of these features, over many iterations, so that they best +fit our dataset. In this particular code, I had used a CSGO dataset (ADR vs Rating). We try to best fit a line through dataset and estimate the parameters. """ -from __future__ import print_function - -import requests import numpy as np +import requests def collect_dataset(): - """ Collect dataset of CSGO + """Collect dataset of CSGO The dataset contains ADR vs Rating of a Player :return : dataset obtained from the link, as matrix """ - response = requests.get('https://raw.githubusercontent.com/yashLadha/' + - 'The_Math_of_Intelligence/master/Week1/ADRvs' + - 'Rating.csv') + response = requests.get( + "https://raw.githubusercontent.com/yashLadha/" + + "The_Math_of_Intelligence/master/Week1/ADRvs" + + "Rating.csv" + ) lines = response.text.splitlines() data = [] for item in lines: - item = item.split(',') + item = item.split(",") data.append(item) data.pop(0) # This is for removing the labels from the list dataset = np.matrix(data) return dataset -def run_steep_gradient_descent(data_x, data_y, - len_data, alpha, theta): - """ Run steep gradient descent and updates the Feature vector accordingly_ +def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta): + """Run steep gradient descent and updates the Feature vector accordingly_ :param data_x : contains the dataset :param data_y : contains the output associated with each data-entry :param len_data : length of the data_ @@ -52,7 +51,7 @@ def run_steep_gradient_descent(data_x, data_y, def sum_of_square_error(data_x, data_y, len_data, theta): - """ Return sum of square error for error calculation + """Return sum of square error for error calculation :param data_x : contains our dataset :param data_y : contains the output (result vector) :param len_data : len of the dataset @@ -67,7 +66,7 @@ def sum_of_square_error(data_x, data_y, len_data, theta): def run_linear_regression(data_x, data_y): - """ Implement Linear regression over the dataset + """Implement Linear regression over the dataset :param data_x : contains our dataset :param data_y : contains the output (result vector) :return : feature for line of best fit (Feature vector) @@ -81,10 +80,9 @@ def run_linear_regression(data_x, data_y): theta = np.zeros((1, no_features)) for i in range(0, iterations): - theta = run_steep_gradient_descent(data_x, data_y, - len_data, alpha, theta) + theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta) error = sum_of_square_error(data_x, data_y, len_data, theta) - print('At Iteration %d - Error is %.5f ' % (i + 1, error)) + print("At Iteration %d - Error is %.5f " % (i + 1, error)) return theta @@ -99,10 +97,10 @@ def main(): theta = run_linear_regression(data_x, data_y) len_result = theta.shape[1] - print('Resultant Feature vector : ') + print("Resultant Feature vector : ") for i in range(0, len_result): - print('%.5f' % (theta[0, i])) + print("%.5f" % (theta[0, i])) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/machine_learning/logistic_regression.py b/machine_learning/logistic_regression.py index 71952e792e81..48d88ef61185 100644 --- a/machine_learning/logistic_regression.py +++ b/machine_learning/logistic_regression.py @@ -1,7 +1,6 @@ #!/usr/bin/python -# -*- coding: utf-8 -*- -## Logistic Regression from scratch +# Logistic Regression from scratch # In[62]: @@ -9,20 +8,24 @@ # importing all the required libraries -''' Implementing logistic regression for classification problem - Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac''' - +""" +Implementing logistic regression for classification problem +Helpful resources: +Coursera ML course +https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac +""" import numpy as np -import matplotlib.pyplot as plt +from matplotlib import pyplot as plt +from sklearn import datasets # get_ipython().run_line_magic('matplotlib', 'inline') -from sklearn import datasets - # In[67]: -# sigmoid function or logistic function is used as a hypothesis function in classification problems +# sigmoid function or logistic function is used as a hypothesis function in +# classification problems + def sigmoid_function(z): return 1 / (1 + np.exp(-z)) @@ -32,70 +35,53 @@ def cost_function(h, y): return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() -# here alpha is the learning rate, X is the feature matrix,y is the target matrix +def log_likelihood(X, Y, weights): + scores = np.dot(X, weights) + return np.sum(Y * scores - np.log(1 + np.exp(scores))) + -def logistic_reg( - alpha, - X, - y, - max_iterations=70000, - ): - converged = False - iterations = 0 +# here alpha is the learning rate, X is the feature matrix,y is the target matrix +def logistic_reg(alpha, X, y, max_iterations=70000): theta = np.zeros(X.shape[1]) - while not converged: + for iterations in range(max_iterations): z = np.dot(X, theta) h = sigmoid_function(z) gradient = np.dot(X.T, h - y) / y.size - theta = theta - alpha * gradient - + theta = theta - alpha * gradient # updating the weights z = np.dot(X, theta) h = sigmoid_function(z) J = cost_function(h, y) - - iterations += 1 # update iterations - - if iterations == max_iterations: - print ('Maximum iterations exceeded!') - print ('Minimal cost function J=', J) - converged = True - + if iterations % 100 == 0: + print(f"loss: {J} \t") # printing the loss after every 100 iterations return theta # In[68]: -if __name__ == '__main__': +if __name__ == "__main__": iris = datasets.load_iris() X = iris.data[:, :2] y = (iris.target != 0) * 1 alpha = 0.1 theta = logistic_reg(alpha, X, y, max_iterations=70000) - print (theta) - + print("theta: ", theta) # printing the theta i.e our weights vector def predict_prob(X): - return sigmoid_function(np.dot(X, theta)) # predicting the value of probability from the logistic regression algorithm - + return sigmoid_function( + np.dot(X, theta) + ) # predicting the value of probability from the logistic regression algorithm plt.figure(figsize=(10, 6)) - plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='b', label='0') - plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='r', label='1') + plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color="b", label="0") + plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color="r", label="1") (x1_min, x1_max) = (X[:, 0].min(), X[:, 0].max()) (x2_min, x2_max) = (X[:, 1].min(), X[:, 1].max()) - (xx1, xx2) = np.meshgrid(np.linspace(x1_min, x1_max), - np.linspace(x2_min, x2_max)) + (xx1, xx2) = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max)) grid = np.c_[xx1.ravel(), xx2.ravel()] probs = predict_prob(grid).reshape(xx1.shape) - plt.contour( - xx1, - xx2, - probs, - [0.5], - linewidths=1, - colors='black', - ) + plt.contour(xx1, xx2, probs, [0.5], linewidths=1, colors="black") plt.legend() + plt.show() diff --git a/machine_learning/lstm/__init__.py b/machine_learning/lstm/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/machine_learning/lstm/lstm_prediction.py_tf b/machine_learning/lstm/lstm_prediction.py_tf new file mode 100644 index 000000000000..5452f0443f62 --- /dev/null +++ b/machine_learning/lstm/lstm_prediction.py_tf @@ -0,0 +1,54 @@ +""" + Create a Long Short Term Memory (LSTM) network model + An LSTM is a type of Recurrent Neural Network (RNN) as discussed at: + * http://colah.github.io/posts/2015-08-Understanding-LSTMs + * https://en.wikipedia.org/wiki/Long_short-term_memory +""" +import numpy as np +import pandas as pd +from keras.layers import LSTM, Dense +from keras.models import Sequential +from sklearn.preprocessing import MinMaxScaler + +if __name__ == "__main__": + """ + First part of building a model is to get the data and prepare + it for our model. You can use any dataset for stock prediction + make sure you set the price column on line number 21. Here we + use a dataset which have the price on 3rd column. + """ + df = pd.read_csv("sample_data.csv", header=None) + len_data = df.shape[:1][0] + # If you're using some other dataset input the target column + actual_data = df.iloc[:, 1:2] + actual_data = actual_data.values.reshape(len_data, 1) + actual_data = MinMaxScaler().fit_transform(actual_data) + look_back = 10 + forward_days = 5 + periods = 20 + division = len_data - periods * look_back + train_data = actual_data[:division] + test_data = actual_data[division - look_back :] + train_x, train_y = [], [] + test_x, test_y = [], [] + + for i in range(0, len(train_data) - forward_days - look_back + 1): + train_x.append(train_data[i : i + look_back]) + train_y.append(train_data[i + look_back : i + look_back + forward_days]) + for i in range(0, len(test_data) - forward_days - look_back + 1): + test_x.append(test_data[i : i + look_back]) + test_y.append(test_data[i + look_back : i + look_back + forward_days]) + x_train = np.array(train_x) + x_test = np.array(test_x) + y_train = np.array([list(i.ravel()) for i in train_y]) + y_test = np.array([list(i.ravel()) for i in test_y]) + + model = Sequential() + model.add(LSTM(128, input_shape=(look_back, 1), return_sequences=True)) + model.add(LSTM(64, input_shape=(128, 1))) + model.add(Dense(forward_days)) + model.compile(loss="mean_squared_error", optimizer="adam") + history = model.fit( + x_train, y_train, epochs=150, verbose=1, shuffle=True, batch_size=4 + ) + pred = model.predict(x_test) diff --git a/machine_learning/lstm/sample_data.csv b/machine_learning/lstm/sample_data.csv new file mode 100644 index 000000000000..f94db621f619 --- /dev/null +++ b/machine_learning/lstm/sample_data.csv @@ -0,0 +1,1259 @@ +04/24/2020, 1279.31, 1640394, 1261.17, 1280.4, 1249.45 +04/23/2020, 1276.31, 1566203, 1271.55, 1293.31, 1265.67 +04/22/2020, 1263.21, 2093140, 1245.54, 1285.6133, 1242 +04/21/2020, 1216.34, 2153003, 1247, 1254.27, 1209.71 +04/20/2020, 1266.61, 1695488, 1271, 1281.6, 1261.37 +04/17/2020, 1283.25, 1949042, 1284.85, 1294.43, 1271.23 +04/16/2020, 1263.47, 2518099, 1274.1, 1279, 1242.62 +04/15/2020, 1262.47, 1671703, 1245.61, 1280.46, 1240.4 +04/14/2020, 1269.23, 2470353, 1245.09, 1282.07, 1236.93 +04/13/2020, 1217.56, 1739828, 1209.18, 1220.51, 1187.5984 +04/09/2020, 1211.45, 2175421, 1224.08, 1225.57, 1196.7351 +04/08/2020, 1210.28, 1975135, 1206.5, 1219.07, 1188.16 +04/07/2020, 1186.51, 2387329, 1221, 1225, 1182.23 +04/06/2020, 1186.92, 2664723, 1138, 1194.66, 1130.94 +04/03/2020, 1097.88, 2313400, 1119.015, 1123.54, 1079.81 +04/02/2020, 1120.84, 1964881, 1098.26, 1126.86, 1096.4 +04/01/2020, 1105.62, 2344173, 1122, 1129.69, 1097.45 +03/31/2020, 1162.81, 2487983, 1147.3, 1175.31, 1138.14 +03/30/2020, 1146.82, 2574061, 1125.04, 1151.63, 1096.48 +03/27/2020, 1110.71, 3208495, 1125.67, 1150.6702, 1105.91 +03/26/2020, 1161.75, 3573755, 1111.8, 1169.97, 1093.53 +03/25/2020, 1102.49, 4081528, 1126.47, 1148.9, 1086.01 +03/24/2020, 1134.46, 3344450, 1103.77, 1135, 1090.62 +03/23/2020, 1056.62, 4044137, 1061.32, 1071.32, 1013.5361 +03/20/2020, 1072.32, 3601750, 1135.72, 1143.99, 1065.49 +03/19/2020, 1115.29, 3651106, 1093.05, 1157.9699, 1060.1075 +03/18/2020, 1096.8, 4233435, 1056.51, 1106.5, 1037.28 +03/17/2020, 1119.8, 3861489, 1093.11, 1130.86, 1056.01 +03/16/2020, 1084.33, 4252365, 1096, 1152.2665, 1074.44 +03/13/2020, 1219.73, 3700125, 1179, 1219.76, 1117.1432 +03/12/2020, 1114.91, 4226748, 1126, 1193.87, 1113.3 +03/11/2020, 1215.41, 2611229, 1249.7, 1260.96, 1196.07 +03/10/2020, 1280.39, 2611373, 1260, 1281.15, 1218.77 +03/09/2020, 1215.56, 3365365, 1205.3, 1254.7599, 1200 +03/06/2020, 1298.41, 2660628, 1277.06, 1306.22, 1261.05 +03/05/2020, 1319.04, 2561288, 1350.2, 1358.91, 1305.1 +03/04/2020, 1386.52, 1913315, 1359.23, 1388.09, 1343.11 +03/03/2020, 1341.39, 2402326, 1399.42, 1410.15, 1332 +03/02/2020, 1389.11, 2431468, 1351.61, 1390.87, 1326.815 +02/28/2020, 1339.33, 3790618, 1277.5, 1341.14, 1271 +02/27/2020, 1318.09, 2978300, 1362.06, 1371.7037, 1317.17 +02/26/2020, 1393.18, 2204037, 1396.14, 1415.7, 1379 +02/25/2020, 1388.45, 2478278, 1433, 1438.14, 1382.4 +02/24/2020, 1421.59, 2867053, 1426.11, 1436.97, 1411.39 +02/21/2020, 1485.11, 1732273, 1508.03, 1512.215, 1480.44 +02/20/2020, 1518.15, 1096552, 1522, 1529.64, 1506.82 +02/19/2020, 1526.69, 949268, 1525.07, 1532.1063, 1521.4 +02/18/2020, 1519.67, 1121140, 1515, 1531.63, 1512.59 +02/14/2020, 1520.74, 1197836, 1515.6, 1520.74, 1507.34 +02/13/2020, 1514.66, 929730, 1512.69, 1527.18, 1504.6 +02/12/2020, 1518.27, 1167565, 1514.48, 1520.695, 1508.11 +02/11/2020, 1508.79, 1344633, 1511.81, 1529.63, 1505.6378 +02/10/2020, 1508.68, 1419876, 1474.32, 1509.5, 1474.32 +02/07/2020, 1479.23, 1172270, 1467.3, 1485.84, 1466.35 +02/06/2020, 1476.23, 1679384, 1450.33, 1481.9997, 1449.57 +02/05/2020, 1448.23, 1986157, 1462.42, 1463.84, 1430.56 +02/04/2020, 1447.07, 3932954, 1457.07, 1469.5, 1426.3 +02/03/2020, 1485.94, 3055216, 1462, 1490, 1458.99 +01/31/2020, 1434.23, 2417214, 1468.9, 1470.13, 1428.53 +01/30/2020, 1455.84, 1339421, 1439.96, 1457.28, 1436.4 +01/29/2020, 1458.63, 1078667, 1458.8, 1465.43, 1446.74 +01/28/2020, 1452.56, 1577422, 1443, 1456, 1432.47 +01/27/2020, 1433.9, 1755201, 1431, 1438.07, 1421.2 +01/24/2020, 1466.71, 1784644, 1493.59, 1495.495, 1465.25 +01/23/2020, 1486.65, 1351354, 1487.64, 1495.52, 1482.1 +01/22/2020, 1485.95, 1610846, 1491, 1503.2143, 1484.93 +01/21/2020, 1484.4, 2036780, 1479.12, 1491.85, 1471.2 +01/17/2020, 1480.39, 2396215, 1462.91, 1481.2954, 1458.22 +01/16/2020, 1451.7, 1173688, 1447.44, 1451.99, 1440.92 +01/15/2020, 1439.2, 1282685, 1430.21, 1441.395, 1430.21 +01/14/2020, 1430.88, 1560453, 1439.01, 1441.8, 1428.37 +01/13/2020, 1439.23, 1653482, 1436.13, 1440.52, 1426.02 +01/10/2020, 1429.73, 1821566, 1427.56, 1434.9292, 1418.35 +01/09/2020, 1419.83, 1502664, 1420.57, 1427.33, 1410.27 +01/08/2020, 1404.32, 1529177, 1392.08, 1411.58, 1390.84 +01/07/2020, 1393.34, 1511693, 1397.94, 1402.99, 1390.38 +01/06/2020, 1394.21, 1733149, 1350, 1396.5, 1350 +01/03/2020, 1360.66, 1187006, 1347.86, 1372.5, 1345.5436 +01/02/2020, 1367.37, 1406731, 1341.55, 1368.14, 1341.55 +12/31/2019, 1337.02, 962468, 1330.11, 1338, 1329.085 +12/30/2019, 1336.14, 1051323, 1350, 1353, 1334.02 +12/27/2019, 1351.89, 1038718, 1362.99, 1364.53, 1349.31 +12/26/2019, 1360.4, 667754, 1346.17, 1361.3269, 1344.47 +12/24/2019, 1343.56, 347518, 1348.5, 1350.26, 1342.78 +12/23/2019, 1348.84, 883200, 1355.87, 1359.7999, 1346.51 +12/20/2019, 1349.59, 3316905, 1363.35, 1363.64, 1349 +12/19/2019, 1356.04, 1470112, 1351.82, 1358.1, 1348.985 +12/18/2019, 1352.62, 1657069, 1356.6, 1360.47, 1351 +12/17/2019, 1355.12, 1855259, 1362.89, 1365, 1351.3231 +12/16/2019, 1361.17, 1397451, 1356.5, 1364.68, 1352.67 +12/13/2019, 1347.83, 1550028, 1347.95, 1353.0931, 1343.87 +12/12/2019, 1350.27, 1281722, 1345.94, 1355.775, 1340.5 +12/11/2019, 1345.02, 850796, 1350.84, 1351.2, 1342.67 +12/10/2019, 1344.66, 1094653, 1341.5, 1349.975, 1336.04 +12/09/2019, 1343.56, 1355795, 1338.04, 1359.45, 1337.84 +12/06/2019, 1340.62, 1315510, 1333.44, 1344, 1333.44 +12/05/2019, 1328.13, 1212818, 1328, 1329.3579, 1316.44 +12/04/2019, 1320.54, 1538110, 1307.01, 1325.8, 1304.87 +12/03/2019, 1295.28, 1268647, 1279.57, 1298.461, 1279 +12/02/2019, 1289.92, 1511851, 1301, 1305.83, 1281 +11/29/2019, 1304.96, 586981, 1307.12, 1310.205, 1303.97 +11/27/2019, 1312.99, 996329, 1315, 1318.36, 1309.63 +11/26/2019, 1313.55, 1069795, 1309.86, 1314.8, 1305.09 +11/25/2019, 1306.69, 1036487, 1299.18, 1311.31, 1298.13 +11/22/2019, 1295.34, 1386506, 1305.62, 1308.73, 1291.41 +11/21/2019, 1301.35, 995499, 1301.48, 1312.59, 1293 +11/20/2019, 1303.05, 1309835, 1311.74, 1315, 1291.15 +11/19/2019, 1315.46, 1269372, 1327.7, 1327.7, 1312.8 +11/18/2019, 1320.7, 1488083, 1332.22, 1335.5288, 1317.5 +11/15/2019, 1334.87, 1782955, 1318.94, 1334.88, 1314.2796 +11/14/2019, 1311.46, 1194305, 1297.5, 1317, 1295.65 +11/13/2019, 1298, 853861, 1294.07, 1304.3, 1293.51 +11/12/2019, 1298.8, 1085859, 1300, 1310, 1295.77 +11/11/2019, 1299.19, 1012429, 1303.18, 1306.425, 1297.41 +11/08/2019, 1311.37, 1251916, 1305.28, 1318, 1304.365 +11/07/2019, 1308.86, 2029970, 1294.28, 1323.74, 1294.245 +11/06/2019, 1291.8, 1152977, 1289.46, 1293.73, 1282.5 +11/05/2019, 1292.03, 1282711, 1292.89, 1298.93, 1291.2289 +11/04/2019, 1291.37, 1500964, 1276.45, 1294.13, 1276.355 +11/01/2019, 1273.74, 1670072, 1265, 1274.62, 1260.5 +10/31/2019, 1260.11, 1455651, 1261.28, 1267.67, 1250.8428 +10/30/2019, 1261.29, 1408851, 1252.97, 1269.36, 1252 +10/29/2019, 1262.62, 1886380, 1276.23, 1281.59, 1257.2119 +10/28/2019, 1290, 2613237, 1275.45, 1299.31, 1272.54 +10/25/2019, 1265.13, 1213051, 1251.03, 1269.6, 1250.01 +10/24/2019, 1260.99, 1039868, 1260.9, 1264, 1253.715 +10/23/2019, 1259.13, 928595, 1242.36, 1259.89, 1242.36 +10/22/2019, 1242.8, 1047851, 1247.85, 1250.6, 1241.38 +10/21/2019, 1246.15, 1038042, 1252.26, 1254.6287, 1240.6 +10/18/2019, 1245.49, 1352839, 1253.46, 1258.89, 1241.08 +10/17/2019, 1253.07, 980510, 1250.93, 1263.325, 1249.94 +10/16/2019, 1243.64, 1168174, 1241.17, 1254.74, 1238.45 +10/15/2019, 1243.01, 1395259, 1220.4, 1247.33, 1220.4 +10/14/2019, 1217.14, 882039, 1212.34, 1226.33, 1211.76 +10/11/2019, 1215.45, 1277144, 1222.21, 1228.39, 1213.74 +10/10/2019, 1208.67, 932531, 1198.58, 1215, 1197.34 +10/09/2019, 1202.31, 876632, 1199.35, 1208.35, 1197.63 +10/08/2019, 1189.13, 1141784, 1197.59, 1206.08, 1189.01 +10/07/2019, 1207.68, 867149, 1204.4, 1218.2036, 1203.75 +10/04/2019, 1209, 1183264, 1191.89, 1211.44, 1189.17 +10/03/2019, 1187.83, 1663656, 1180, 1189.06, 1162.43 +10/02/2019, 1176.63, 1639237, 1196.98, 1196.99, 1171.29 +10/01/2019, 1205.1, 1358279, 1219, 1231.23, 1203.58 +09/30/2019, 1219, 1419676, 1220.97, 1226, 1212.3 +09/27/2019, 1225.09, 1354432, 1243.01, 1244.02, 1214.45 +09/26/2019, 1241.39, 1561882, 1241.96, 1245, 1232.268 +09/25/2019, 1246.52, 1593875, 1215.82, 1248.3, 1210.09 +09/24/2019, 1218.76, 1591786, 1240, 1246.74, 1210.68 +09/23/2019, 1234.03, 1075253, 1226, 1239.09, 1224.17 +09/20/2019, 1229.93, 2337269, 1233.12, 1243.32, 1223.08 +09/19/2019, 1238.71, 1000155, 1232.06, 1244.44, 1232.02 +09/18/2019, 1232.41, 1144333, 1227.51, 1235.61, 1216.53 +09/17/2019, 1229.15, 958112, 1230.4, 1235, 1223.69 +09/16/2019, 1231.3, 1053299, 1229.52, 1239.56, 1225.61 +09/13/2019, 1239.56, 1301350, 1231.35, 1240.88, 1227.01 +09/12/2019, 1234.25, 1725908, 1224.3, 1241.86, 1223.02 +09/11/2019, 1220.17, 1307033, 1203.41, 1222.6, 1202.2 +09/10/2019, 1206, 1260115, 1195.15, 1210, 1194.58 +09/09/2019, 1204.41, 1471880, 1204, 1220, 1192.62 +09/06/2019, 1204.93, 1072143, 1208.13, 1212.015, 1202.5222 +09/05/2019, 1211.38, 1408601, 1191.53, 1213.04, 1191.53 +09/04/2019, 1181.41, 1068968, 1176.71, 1183.48, 1171 +09/03/2019, 1168.39, 1480420, 1177.03, 1186.89, 1163.2 +08/30/2019, 1188.1, 1129959, 1198.5, 1198.5, 1183.8026 +08/29/2019, 1192.85, 1088858, 1181.12, 1196.06, 1181.12 +08/28/2019, 1171.02, 802243, 1161.71, 1176.4199, 1157.3 +08/27/2019, 1167.84, 1077452, 1180.53, 1182.4, 1161.45 +08/26/2019, 1168.89, 1226441, 1157.26, 1169.47, 1152.96 +08/23/2019, 1151.29, 1688271, 1181.99, 1194.08, 1147.75 +08/22/2019, 1189.53, 947906, 1194.07, 1198.0115, 1178.58 +08/21/2019, 1191.25, 741053, 1193.15, 1199, 1187.43 +08/20/2019, 1182.69, 915605, 1195.25, 1196.06, 1182.11 +08/19/2019, 1198.45, 1232517, 1190.09, 1206.99, 1190.09 +08/16/2019, 1177.6, 1349436, 1179.55, 1182.72, 1171.81 +08/15/2019, 1167.26, 1224739, 1163.5, 1175.84, 1162.11 +08/14/2019, 1164.29, 1578668, 1176.31, 1182.3, 1160.54 +08/13/2019, 1197.27, 1318009, 1171.46, 1204.78, 1171.46 +08/12/2019, 1174.71, 1003187, 1179.21, 1184.96, 1167.6723 +08/09/2019, 1188.01, 1065658, 1197.99, 1203.88, 1183.603 +08/08/2019, 1204.8, 1467997, 1182.83, 1205.01, 1173.02 +08/07/2019, 1173.99, 1444324, 1156, 1178.4451, 1149.6239 +08/06/2019, 1169.95, 1709374, 1163.31, 1179.96, 1160 +08/05/2019, 1152.32, 2597455, 1170.04, 1175.24, 1140.14 +08/02/2019, 1193.99, 1645067, 1200.74, 1206.9, 1188.94 +08/01/2019, 1209.01, 1698510, 1214.03, 1234.11, 1205.72 +07/31/2019, 1216.68, 1725454, 1223, 1234, 1207.7635 +07/30/2019, 1225.14, 1453263, 1225.41, 1234.87, 1223.3 +07/29/2019, 1239.41, 2223731, 1241.05, 1247.37, 1228.23 +07/26/2019, 1250.41, 4805752, 1224.04, 1265.5499, 1224 +07/25/2019, 1132.12, 2209823, 1137.82, 1141.7, 1120.92 +07/24/2019, 1137.81, 1590101, 1131.9, 1144, 1126.99 +07/23/2019, 1146.21, 1093688, 1144, 1146.9, 1131.8 +07/22/2019, 1138.07, 1301846, 1133.45, 1139.25, 1124.24 +07/19/2019, 1130.1, 1647245, 1148.19, 1151.14, 1129.62 +07/18/2019, 1146.33, 1291281, 1141.74, 1147.605, 1132.73 +07/17/2019, 1146.35, 1170047, 1150.97, 1158.36, 1145.77 +07/16/2019, 1153.58, 1238807, 1146, 1158.58, 1145 +07/15/2019, 1150.34, 903780, 1146.86, 1150.82, 1139.4 +07/12/2019, 1144.9, 863973, 1143.99, 1147.34, 1138.78 +07/11/2019, 1144.21, 1195569, 1143.25, 1153.07, 1139.58 +07/10/2019, 1140.48, 1209466, 1131.22, 1142.05, 1130.97 +07/09/2019, 1124.83, 1330370, 1111.8, 1128.025, 1107.17 +07/08/2019, 1116.35, 1236419, 1125.17, 1125.98, 1111.21 +07/05/2019, 1131.59, 1264540, 1117.8, 1132.88, 1116.14 +07/03/2019, 1121.58, 767011, 1117.41, 1126.76, 1113.86 +07/02/2019, 1111.25, 991755, 1102.24, 1111.77, 1098.17 +07/01/2019, 1097.95, 1438504, 1098, 1107.58, 1093.703 +06/28/2019, 1080.91, 1693450, 1076.39, 1081, 1073.37 +06/27/2019, 1076.01, 1004477, 1084, 1087.1, 1075.29 +06/26/2019, 1079.8, 1810869, 1086.5, 1092.97, 1072.24 +06/25/2019, 1086.35, 1546913, 1112.66, 1114.35, 1083.8 +06/24/2019, 1115.52, 1395696, 1119.61, 1122, 1111.01 +06/21/2019, 1121.88, 1947591, 1109.24, 1124.11, 1108.08 +06/20/2019, 1111.42, 1262011, 1119.99, 1120.12, 1104.74 +06/19/2019, 1102.33, 1339218, 1105.6, 1107, 1093.48 +06/18/2019, 1103.6, 1386684, 1109.69, 1116.39, 1098.99 +06/17/2019, 1092.5, 941602, 1086.28, 1099.18, 1086.28 +06/14/2019, 1085.35, 1111643, 1086.42, 1092.69, 1080.1721 +06/13/2019, 1088.77, 1058000, 1083.64, 1094.17, 1080.15 +06/12/2019, 1077.03, 1061255, 1078, 1080.93, 1067.54 +06/11/2019, 1078.72, 1437063, 1093.98, 1101.99, 1077.6025 +06/10/2019, 1080.38, 1464248, 1072.98, 1092.66, 1072.3216 +06/07/2019, 1066.04, 1802370, 1050.63, 1070.92, 1048.4 +06/06/2019, 1044.34, 1703244, 1044.99, 1047.49, 1033.7 +06/05/2019, 1042.22, 2168439, 1051.54, 1053.55, 1030.49 +06/04/2019, 1053.05, 2833483, 1042.9, 1056.05, 1033.69 +06/03/2019, 1036.23, 5130576, 1065.5, 1065.5, 1025 +05/31/2019, 1103.63, 1508203, 1101.29, 1109.6, 1100.18 +05/30/2019, 1117.95, 951873, 1115.54, 1123.13, 1112.12 +05/29/2019, 1116.46, 1538212, 1127.52, 1129.1, 1108.2201 +05/28/2019, 1134.15, 1365166, 1134, 1151.5871, 1133.12 +05/24/2019, 1133.47, 1112341, 1147.36, 1149.765, 1131.66 +05/23/2019, 1140.77, 1199300, 1140.5, 1145.9725, 1129.224 +05/22/2019, 1151.42, 914839, 1146.75, 1158.52, 1145.89 +05/21/2019, 1149.63, 1160158, 1148.49, 1152.7077, 1137.94 +05/20/2019, 1138.85, 1353292, 1144.5, 1146.7967, 1131.4425 +05/17/2019, 1162.3, 1208623, 1168.47, 1180.15, 1160.01 +05/16/2019, 1178.98, 1531404, 1164.51, 1188.16, 1162.84 +05/15/2019, 1164.21, 2289302, 1117.87, 1171.33, 1116.6657 +05/14/2019, 1120.44, 1836604, 1137.21, 1140.42, 1119.55 +05/13/2019, 1132.03, 1860648, 1141.96, 1147.94, 1122.11 +05/10/2019, 1164.27, 1314546, 1163.59, 1172.6, 1142.5 +05/09/2019, 1162.38, 1185973, 1159.03, 1169.66, 1150.85 +05/08/2019, 1166.27, 1309514, 1172.01, 1180.4243, 1165.74 +05/07/2019, 1174.1, 1551368, 1180.47, 1190.44, 1161.04 +05/06/2019, 1189.39, 1563943, 1166.26, 1190.85, 1166.26 +05/03/2019, 1185.4, 1980653, 1173.65, 1186.8, 1169 +05/02/2019, 1162.61, 1944817, 1167.76, 1174.1895, 1155.0018 +05/01/2019, 1168.08, 2642983, 1188.05, 1188.05, 1167.18 +04/30/2019, 1188.48, 6194691, 1185, 1192.81, 1175 +04/29/2019, 1287.58, 2412788, 1274, 1289.27, 1266.2949 +04/26/2019, 1272.18, 1228276, 1269, 1273.07, 1260.32 +04/25/2019, 1263.45, 1099614, 1264.77, 1267.4083, 1252.03 +04/24/2019, 1256, 1015006, 1264.12, 1268.01, 1255 +04/23/2019, 1264.55, 1271195, 1250.69, 1269, 1246.38 +04/22/2019, 1248.84, 806577, 1235.99, 1249.09, 1228.31 +04/18/2019, 1236.37, 1315676, 1239.18, 1242, 1234.61 +04/17/2019, 1236.34, 1211866, 1233, 1240.56, 1227.82 +04/16/2019, 1227.13, 855258, 1225, 1230.82, 1220.12 +04/15/2019, 1221.1, 1187353, 1218, 1224.2, 1209.1101 +04/12/2019, 1217.87, 926799, 1210, 1218.35, 1208.11 +04/11/2019, 1204.62, 709417, 1203.96, 1207.96, 1200.13 +04/10/2019, 1202.16, 724524, 1200.68, 1203.785, 1196.435 +04/09/2019, 1197.25, 865416, 1196, 1202.29, 1193.08 +04/08/2019, 1203.84, 859969, 1207.89, 1208.69, 1199.86 +04/05/2019, 1207.15, 900950, 1214.99, 1216.22, 1205.03 +04/04/2019, 1215, 949962, 1205.94, 1215.67, 1204.13 +04/03/2019, 1205.92, 1014195, 1207.48, 1216.3, 1200.5 +04/02/2019, 1200.49, 800820, 1195.32, 1201.35, 1185.71 +04/01/2019, 1194.43, 1188235, 1184.1, 1196.66, 1182 +03/29/2019, 1173.31, 1269573, 1174.9, 1178.99, 1162.88 +03/28/2019, 1168.49, 966843, 1171.54, 1171.565, 1159.4312 +03/27/2019, 1173.02, 1362217, 1185.5, 1187.559, 1159.37 +03/26/2019, 1184.62, 1894639, 1198.53, 1202.83, 1176.72 +03/25/2019, 1193, 1493841, 1196.93, 1206.3975, 1187.04 +03/22/2019, 1205.5, 1668910, 1226.32, 1230, 1202.825 +03/21/2019, 1231.54, 1195899, 1216, 1231.79, 1213.15 +03/20/2019, 1223.97, 2089367, 1197.35, 1227.14, 1196.17 +03/19/2019, 1198.85, 1404863, 1188.81, 1200, 1185.87 +03/18/2019, 1184.26, 1212506, 1183.3, 1190, 1177.4211 +03/15/2019, 1184.46, 2457597, 1193.38, 1196.57, 1182.61 +03/14/2019, 1185.55, 1150950, 1194.51, 1197.88, 1184.48 +03/13/2019, 1193.32, 1434816, 1200.645, 1200.93, 1191.94 +03/12/2019, 1193.2, 2012306, 1178.26, 1200, 1178.26 +03/11/2019, 1175.76, 1569332, 1144.45, 1176.19, 1144.45 +03/08/2019, 1142.32, 1212271, 1126.73, 1147.08, 1123.3 +03/07/2019, 1143.3, 1166076, 1155.72, 1156.755, 1134.91 +03/06/2019, 1157.86, 1094100, 1162.49, 1167.5658, 1155.49 +03/05/2019, 1162.03, 1422357, 1150.06, 1169.61, 1146.195 +03/04/2019, 1147.8, 1444774, 1146.99, 1158.2804, 1130.69 +03/01/2019, 1140.99, 1447454, 1124.9, 1142.97, 1124.75 +02/28/2019, 1119.92, 1541068, 1111.3, 1127.65, 1111.01 +02/27/2019, 1116.05, 968362, 1106.95, 1117.98, 1101 +02/26/2019, 1115.13, 1469761, 1105.75, 1119.51, 1099.92 +02/25/2019, 1109.4, 1395281, 1116, 1118.54, 1107.27 +02/22/2019, 1110.37, 1048361, 1100.9, 1111.24, 1095.6 +02/21/2019, 1096.97, 1414744, 1110.84, 1111.94, 1092.52 +02/20/2019, 1113.8, 1080144, 1119.99, 1123.41, 1105.28 +02/19/2019, 1118.56, 1046315, 1110, 1121.89, 1110 +02/15/2019, 1113.65, 1442461, 1130.08, 1131.67, 1110.65 +02/14/2019, 1121.67, 941678, 1118.05, 1128.23, 1110.445 +02/13/2019, 1120.16, 1048630, 1124.99, 1134.73, 1118.5 +02/12/2019, 1121.37, 1608658, 1106.8, 1125.295, 1105.85 +02/11/2019, 1095.01, 1063825, 1096.95, 1105.945, 1092.86 +02/08/2019, 1095.06, 1072031, 1087, 1098.91, 1086.55 +02/07/2019, 1098.71, 2040615, 1104.16, 1104.84, 1086 +02/06/2019, 1115.23, 2101674, 1139.57, 1147, 1112.77 +02/05/2019, 1145.99, 3529974, 1124.84, 1146.85, 1117.248 +02/04/2019, 1132.8, 2518184, 1112.66, 1132.8, 1109.02 +02/01/2019, 1110.75, 1455609, 1112.4, 1125, 1104.89 +01/31/2019, 1116.37, 1531463, 1103, 1117.33, 1095.41 +01/30/2019, 1089.06, 1241760, 1068.43, 1091, 1066.85 +01/29/2019, 1060.62, 1006731, 1072.68, 1075.15, 1055.8647 +01/28/2019, 1070.08, 1277745, 1080.11, 1083, 1063.8 +01/25/2019, 1090.99, 1114785, 1085, 1094, 1081.82 +01/24/2019, 1073.9, 1317718, 1076.48, 1079.475, 1060.7 +01/23/2019, 1075.57, 956526, 1077.35, 1084.93, 1059.75 +01/22/2019, 1070.52, 1607398, 1088, 1091.51, 1063.47 +01/18/2019, 1098.26, 1933754, 1100, 1108.352, 1090.9 +01/17/2019, 1089.9, 1223674, 1079.47, 1091.8, 1073.5 +01/16/2019, 1080.97, 1320530, 1080, 1092.375, 1079.34 +01/15/2019, 1077.15, 1452238, 1050.17, 1080.05, 1047.34 +01/14/2019, 1044.69, 1127417, 1046.92, 1051.53, 1041.255 +01/11/2019, 1057.19, 1512651, 1063.18, 1063.775, 1048.48 +01/10/2019, 1070.33, 1444976, 1067.66, 1071.15, 1057.71 +01/09/2019, 1074.66, 1198369, 1081.65, 1082.63, 1066.4 +01/08/2019, 1076.28, 1748371, 1076.11, 1084.56, 1060.53 +01/07/2019, 1068.39, 1978077, 1071.5, 1073.9999, 1054.76 +01/04/2019, 1070.71, 2080144, 1032.59, 1070.84, 1027.4179 +01/03/2019, 1016.06, 1829379, 1041, 1056.98, 1014.07 +01/02/2019, 1045.85, 1516681, 1016.57, 1052.32, 1015.71 +12/31/2018, 1035.61, 1492541, 1050.96, 1052.7, 1023.59 +12/28/2018, 1037.08, 1399218, 1049.62, 1055.56, 1033.1 +12/27/2018, 1043.88, 2102069, 1017.15, 1043.89, 997 +12/26/2018, 1039.46, 2337212, 989.01, 1040, 983 +12/24/2018, 976.22, 1590328, 973.9, 1003.54, 970.11 +12/21/2018, 979.54, 4560424, 1015.3, 1024.02, 973.69 +12/20/2018, 1009.41, 2659047, 1018.13, 1034.22, 996.36 +12/19/2018, 1023.01, 2419322, 1033.99, 1062, 1008.05 +12/18/2018, 1028.71, 2101854, 1026.09, 1049.48, 1021.44 +12/17/2018, 1016.53, 2337631, 1037.51, 1053.15, 1007.9 +12/14/2018, 1042.1, 1685802, 1049.98, 1062.6, 1040.79 +12/13/2018, 1061.9, 1329198, 1068.07, 1079.7597, 1053.93 +12/12/2018, 1063.68, 1523276, 1068, 1081.65, 1062.79 +12/11/2018, 1051.75, 1354751, 1056.49, 1060.6, 1039.84 +12/10/2018, 1039.55, 1793465, 1035.05, 1048.45, 1023.29 +12/07/2018, 1036.58, 2098526, 1060.01, 1075.26, 1028.5 +12/06/2018, 1068.73, 2758098, 1034.26, 1071.2, 1030.7701 +12/04/2018, 1050.82, 2278200, 1103.12, 1104.42, 1049.98 +12/03/2018, 1106.43, 1900355, 1123.14, 1124.65, 1103.6645 +11/30/2018, 1094.43, 2554416, 1089.07, 1095.57, 1077.88 +11/29/2018, 1088.3, 1403540, 1076.08, 1094.245, 1076 +11/28/2018, 1086.23, 2399374, 1048.76, 1086.84, 1035.76 +11/27/2018, 1044.41, 1801334, 1041, 1057.58, 1038.49 +11/26/2018, 1048.62, 1846430, 1038.35, 1049.31, 1033.91 +11/23/2018, 1023.88, 691462, 1030, 1037.59, 1022.3992 +11/21/2018, 1037.61, 1531676, 1036.76, 1048.56, 1033.47 +11/20/2018, 1025.76, 2447254, 1000, 1031.74, 996.02 +11/19/2018, 1020, 1837207, 1057.2, 1060.79, 1016.2601 +11/16/2018, 1061.49, 1641232, 1059.41, 1067, 1048.98 +11/15/2018, 1064.71, 1819132, 1044.71, 1071.85, 1031.78 +11/14/2018, 1043.66, 1561656, 1050, 1054.5643, 1031 +11/13/2018, 1036.05, 1496534, 1043.29, 1056.605, 1031.15 +11/12/2018, 1038.63, 1429319, 1061.39, 1062.12, 1031 +11/09/2018, 1066.15, 1343154, 1073.99, 1075.56, 1053.11 +11/08/2018, 1082.4, 1463022, 1091.38, 1093.27, 1072.2048 +11/07/2018, 1093.39, 2057155, 1069, 1095.46, 1065.9 +11/06/2018, 1055.81, 1225197, 1039.48, 1064.345, 1038.07 +11/05/2018, 1040.09, 2436742, 1055, 1058.47, 1021.24 +11/02/2018, 1057.79, 1829295, 1073.73, 1082.975, 1054.61 +11/01/2018, 1070, 1456222, 1075.8, 1083.975, 1062.46 +10/31/2018, 1076.77, 2528584, 1059.81, 1091.94, 1057 +10/30/2018, 1036.21, 3209126, 1008.46, 1037.49, 1000.75 +10/29/2018, 1020.08, 3873644, 1082.47, 1097.04, 995.83 +10/26/2018, 1071.47, 4185201, 1037.03, 1106.53, 1034.09 +10/25/2018, 1095.57, 2511884, 1071.79, 1110.98, 1069.55 +10/24/2018, 1050.71, 1910060, 1104.25, 1106.12, 1048.74 +10/23/2018, 1103.69, 1847798, 1080.89, 1107.89, 1070 +10/22/2018, 1101.16, 1494285, 1103.06, 1112.23, 1091 +10/19/2018, 1096.46, 1264605, 1093.37, 1110.36, 1087.75 +10/18/2018, 1087.97, 2056606, 1121.84, 1121.84, 1077.09 +10/17/2018, 1115.69, 1397613, 1126.46, 1128.99, 1102.19 +10/16/2018, 1121.28, 1845491, 1104.59, 1124.22, 1102.5 +10/15/2018, 1092.25, 1343231, 1108.91, 1113.4464, 1089 +10/12/2018, 1110.08, 2029872, 1108, 1115, 1086.402 +10/11/2018, 1079.32, 2939514, 1072.94, 1106.4, 1068.27 +10/10/2018, 1081.22, 2574985, 1131.08, 1132.17, 1081.13 +10/09/2018, 1138.82, 1308706, 1146.15, 1154.35, 1137.572 +10/08/2018, 1148.97, 1877142, 1150.11, 1168, 1127.3636 +10/05/2018, 1157.35, 1184245, 1167.5, 1173.4999, 1145.12 +10/04/2018, 1168.19, 2151762, 1195.33, 1197.51, 1155.576 +10/03/2018, 1202.95, 1207280, 1205, 1206.41, 1193.83 +10/02/2018, 1200.11, 1655602, 1190.96, 1209.96, 1186.63 +10/01/2018, 1195.31, 1345250, 1199.89, 1209.9, 1190.3 +09/28/2018, 1193.47, 1306822, 1191.87, 1195.41, 1184.5 +09/27/2018, 1194.64, 1244278, 1186.73, 1202.1, 1183.63 +09/26/2018, 1180.49, 1346434, 1185.15, 1194.23, 1174.765 +09/25/2018, 1184.65, 937577, 1176.15, 1186.88, 1168 +09/24/2018, 1173.37, 1218532, 1157.17, 1178, 1146.91 +09/21/2018, 1166.09, 4363929, 1192, 1192.21, 1166.04 +09/20/2018, 1186.87, 1209855, 1179.99, 1189.89, 1173.36 +09/19/2018, 1171.09, 1185321, 1164.98, 1173.21, 1154.58 +09/18/2018, 1161.22, 1184407, 1157.09, 1176.08, 1157.09 +09/17/2018, 1156.05, 1279147, 1170.14, 1177.24, 1154.03 +09/14/2018, 1172.53, 934300, 1179.1, 1180.425, 1168.3295 +09/13/2018, 1175.33, 1402005, 1170.74, 1178.61, 1162.85 +09/12/2018, 1162.82, 1291304, 1172.72, 1178.61, 1158.36 +09/11/2018, 1177.36, 1209171, 1161.63, 1178.68, 1156.24 +09/10/2018, 1164.64, 1115259, 1172.19, 1174.54, 1160.11 +09/07/2018, 1164.83, 1401034, 1158.67, 1175.26, 1157.215 +09/06/2018, 1171.44, 1886690, 1186.3, 1186.3, 1152 +09/05/2018, 1186.48, 2043732, 1193.8, 1199.0096, 1162 +09/04/2018, 1197, 1800509, 1204.27, 1212.99, 1192.5 +08/31/2018, 1218.19, 1812366, 1234.98, 1238.66, 1211.2854 +08/30/2018, 1239.12, 1320261, 1244.23, 1253.635, 1232.59 +08/29/2018, 1249.3, 1295939, 1237.45, 1250.66, 1236.3588 +08/28/2018, 1231.15, 1296532, 1241.29, 1242.545, 1228.69 +08/27/2018, 1241.82, 1154962, 1227.6, 1243.09, 1225.716 +08/24/2018, 1220.65, 946529, 1208.82, 1221.65, 1206.3588 +08/23/2018, 1205.38, 988509, 1207.14, 1221.28, 1204.24 +08/22/2018, 1207.33, 881463, 1200, 1211.84, 1199 +08/21/2018, 1201.62, 1187884, 1208, 1217.26, 1200.3537 +08/20/2018, 1207.77, 864462, 1205.02, 1211, 1194.6264 +08/17/2018, 1200.96, 1381724, 1202.03, 1209.02, 1188.24 +08/16/2018, 1206.49, 1319985, 1224.73, 1225.9999, 1202.55 +08/15/2018, 1214.38, 1815642, 1229.26, 1235.24, 1209.51 +08/14/2018, 1242.1, 1342534, 1235.19, 1245.8695, 1225.11 +08/13/2018, 1235.01, 957153, 1236.98, 1249.2728, 1233.6405 +08/10/2018, 1237.61, 1107323, 1243, 1245.695, 1232 +08/09/2018, 1249.1, 805227, 1249.9, 1255.542, 1246.01 +08/08/2018, 1245.61, 1369650, 1240.47, 1256.5, 1238.0083 +08/07/2018, 1242.22, 1493073, 1237, 1251.17, 1236.17 +08/06/2018, 1224.77, 1080923, 1225, 1226.0876, 1215.7965 +08/03/2018, 1223.71, 1072524, 1229.62, 1230, 1215.06 +08/02/2018, 1226.15, 1520488, 1205.9, 1229.88, 1204.79 +08/01/2018, 1220.01, 1567142, 1228, 1233.47, 1210.21 +07/31/2018, 1217.26, 1632823, 1220.01, 1227.5877, 1205.6 +07/30/2018, 1219.74, 1822782, 1228.01, 1234.916, 1211.47 +07/27/2018, 1238.5, 2115802, 1271, 1273.89, 1231 +07/26/2018, 1268.33, 2334881, 1251, 1269.7707, 1249.02 +07/25/2018, 1263.7, 2115890, 1239.13, 1265.86, 1239.13 +07/24/2018, 1248.08, 3303268, 1262.59, 1266, 1235.56 +07/23/2018, 1205.5, 2584034, 1181.01, 1206.49, 1181 +07/20/2018, 1184.91, 1246898, 1186.96, 1196.86, 1184.22 +07/19/2018, 1186.96, 1256113, 1191, 1200, 1183.32 +07/18/2018, 1195.88, 1391232, 1196.56, 1204.5, 1190.34 +07/17/2018, 1198.8, 1585091, 1172.22, 1203.04, 1170.6 +07/16/2018, 1183.86, 1049560, 1189.39, 1191, 1179.28 +07/13/2018, 1188.82, 1221687, 1185, 1195.4173, 1180 +07/12/2018, 1183.48, 1251083, 1159.89, 1184.41, 1155.935 +07/11/2018, 1153.9, 1094301, 1144.59, 1164.29, 1141.0003 +07/10/2018, 1152.84, 789249, 1156.98, 1159.59, 1149.59 +07/09/2018, 1154.05, 906073, 1148.48, 1154.67, 1143.42 +07/06/2018, 1140.17, 966155, 1123.58, 1140.93, 1120.7371 +07/05/2018, 1124.27, 1060752, 1110.53, 1127.5, 1108.48 +07/03/2018, 1102.89, 679034, 1135.82, 1135.82, 1100.02 +07/02/2018, 1127.46, 1188616, 1099, 1128, 1093.8 +06/29/2018, 1115.65, 1275979, 1120, 1128.2265, 1115 +06/28/2018, 1114.22, 1072438, 1102.09, 1122.31, 1096.01 +06/27/2018, 1103.98, 1287698, 1121.34, 1131.8362, 1103.62 +06/26/2018, 1118.46, 1559791, 1128, 1133.21, 1116.6589 +06/25/2018, 1124.81, 2155276, 1143.6, 1143.91, 1112.78 +06/22/2018, 1155.48, 1310164, 1159.14, 1162.4965, 1147.26 +06/21/2018, 1157.66, 1232352, 1174.85, 1177.295, 1152.232 +06/20/2018, 1169.84, 1648248, 1175.31, 1186.2856, 1169.16 +06/19/2018, 1168.06, 1616125, 1158.5, 1171.27, 1154.01 +06/18/2018, 1173.46, 1400641, 1143.65, 1174.31, 1143.59 +06/15/2018, 1152.26, 2119134, 1148.86, 1153.42, 1143.485 +06/14/2018, 1152.12, 1350085, 1143.85, 1155.47, 1140.64 +06/13/2018, 1134.79, 1490017, 1141.12, 1146.5, 1133.38 +06/12/2018, 1139.32, 899231, 1131.07, 1139.79, 1130.735 +06/11/2018, 1129.99, 1071114, 1118.6, 1137.26, 1118.6 +06/08/2018, 1120.87, 1289859, 1118.18, 1126.67, 1112.15 +06/07/2018, 1123.86, 1519860, 1131.32, 1135.82, 1116.52 +06/06/2018, 1136.88, 1697489, 1142.17, 1143, 1125.7429 +06/05/2018, 1139.66, 1538169, 1140.99, 1145.738, 1133.19 +06/04/2018, 1139.29, 1881046, 1122.33, 1141.89, 1122.005 +06/01/2018, 1119.5, 2416755, 1099.35, 1120, 1098.5 +05/31/2018, 1084.99, 3085325, 1067.56, 1097.19, 1067.56 +05/30/2018, 1067.8, 1129958, 1063.03, 1069.21, 1056.83 +05/29/2018, 1060.32, 1858676, 1064.89, 1073.37, 1055.22 +05/25/2018, 1075.66, 878903, 1079.02, 1082.56, 1073.775 +05/24/2018, 1079.24, 757752, 1079, 1080.47, 1066.15 +05/23/2018, 1079.69, 1057712, 1065.13, 1080.78, 1061.71 +05/22/2018, 1069.73, 1088700, 1083.56, 1086.59, 1066.69 +05/21/2018, 1079.58, 1012258, 1074.06, 1088, 1073.65 +05/18/2018, 1066.36, 1496448, 1061.86, 1069.94, 1060.68 +05/17/2018, 1078.59, 1031190, 1079.89, 1086.87, 1073.5 +05/16/2018, 1081.77, 989819, 1077.31, 1089.27, 1076.26 +05/15/2018, 1079.23, 1494306, 1090, 1090.05, 1073.47 +05/14/2018, 1100.2, 1450140, 1100, 1110.75, 1099.11 +05/11/2018, 1098.26, 1253205, 1093.6, 1101.3295, 1090.91 +05/10/2018, 1097.57, 1441456, 1086.03, 1100.44, 1085.64 +05/09/2018, 1082.76, 2032319, 1058.1, 1085.44, 1056.365 +05/08/2018, 1053.91, 1217260, 1058.54, 1060.55, 1047.145 +05/07/2018, 1054.79, 1464008, 1049.23, 1061.68, 1047.1 +05/04/2018, 1048.21, 1936797, 1016.9, 1048.51, 1016.9 +05/03/2018, 1023.72, 1813623, 1019, 1029.675, 1006.29 +05/02/2018, 1024.38, 1534094, 1028.1, 1040.389, 1022.87 +05/01/2018, 1037.31, 1427171, 1013.66, 1038.47, 1008.21 +04/30/2018, 1017.33, 1664084, 1030.01, 1037, 1016.85 +04/27/2018, 1030.05, 1617452, 1046, 1049.5, 1025.59 +04/26/2018, 1040.04, 1984448, 1029.51, 1047.98, 1018.19 +04/25/2018, 1021.18, 2225495, 1025.52, 1032.49, 1015.31 +04/24/2018, 1019.98, 4750851, 1052, 1057, 1010.59 +04/23/2018, 1067.45, 2278846, 1077.86, 1082.72, 1060.7 +04/20/2018, 1072.96, 1887698, 1082, 1092.35, 1069.57 +04/19/2018, 1087.7, 1741907, 1069.4, 1094.165, 1068.18 +04/18/2018, 1072.08, 1336678, 1077.43, 1077.43, 1066.225 +04/17/2018, 1074.16, 2311903, 1051.37, 1077.88, 1048.26 +04/16/2018, 1037.98, 1194144, 1037, 1043.24, 1026.74 +04/13/2018, 1029.27, 1175754, 1040.88, 1046.42, 1022.98 +04/12/2018, 1032.51, 1357599, 1025.04, 1040.69, 1021.4347 +04/11/2018, 1019.97, 1476133, 1027.99, 1031.3641, 1015.87 +04/10/2018, 1031.64, 1983510, 1026.44, 1036.28, 1011.34 +04/09/2018, 1015.45, 1738682, 1016.8, 1039.6, 1014.08 +04/06/2018, 1007.04, 1740896, 1020, 1031.42, 1003.03 +04/05/2018, 1027.81, 1345681, 1041.33, 1042.79, 1020.1311 +04/04/2018, 1025.14, 2464418, 993.41, 1028.7175, 993 +04/03/2018, 1013.41, 2271858, 1013.91, 1020.99, 994.07 +04/02/2018, 1006.47, 2679214, 1022.82, 1034.8, 990.37 +03/29/2018, 1031.79, 2714402, 1011.63, 1043, 1002.9 +03/28/2018, 1004.56, 3345046, 998, 1024.23, 980.64 +03/27/2018, 1005.1, 3081612, 1063, 1064.8393, 996.92 +03/26/2018, 1053.21, 2593808, 1046, 1055.63, 1008.4 +03/23/2018, 1021.57, 2147097, 1047.03, 1063.36, 1021.22 +03/22/2018, 1049.08, 2584639, 1081.88, 1082.9, 1045.91 +03/21/2018, 1090.88, 1878294, 1092.74, 1106.2999, 1085.15 +03/20/2018, 1097.71, 1802209, 1099, 1105.2, 1083.46 +03/19/2018, 1099.82, 2355186, 1120.01, 1121.99, 1089.01 +03/16/2018, 1135.73, 2614871, 1154.14, 1155.88, 1131.96 +03/15/2018, 1149.58, 1397767, 1149.96, 1161.08, 1134.54 +03/14/2018, 1149.49, 1290638, 1145.21, 1158.59, 1141.44 +03/13/2018, 1138.17, 1874176, 1170, 1176.76, 1133.33 +03/12/2018, 1164.5, 2106548, 1163.85, 1177.05, 1157.42 +03/09/2018, 1160.04, 2121425, 1136, 1160.8, 1132.4606 +03/08/2018, 1126, 1393529, 1115.32, 1127.6, 1112.8 +03/07/2018, 1109.64, 1277439, 1089.19, 1112.22, 1085.4823 +03/06/2018, 1095.06, 1497087, 1099.22, 1101.85, 1089.775 +03/05/2018, 1090.93, 1141932, 1075.14, 1097.1, 1069.0001 +03/02/2018, 1078.92, 2271394, 1053.08, 1081.9986, 1048.115 +03/01/2018, 1069.52, 2511872, 1107.87, 1110.12, 1067.001 +02/28/2018, 1104.73, 1873737, 1123.03, 1127.53, 1103.24 +02/27/2018, 1118.29, 1772866, 1141.24, 1144.04, 1118 +02/26/2018, 1143.75, 1514920, 1127.8, 1143.96, 1126.695 +02/23/2018, 1126.79, 1190432, 1112.64, 1127.28, 1104.7135 +02/22/2018, 1106.63, 1309536, 1116.19, 1122.82, 1102.59 +02/21/2018, 1111.34, 1507152, 1106.47, 1133.97, 1106.33 +02/20/2018, 1102.46, 1389491, 1090.57, 1113.95, 1088.52 +02/16/2018, 1094.8, 1680283, 1088.41, 1104.67, 1088.3134 +02/15/2018, 1089.52, 1785552, 1079.07, 1091.4794, 1064.34 +02/14/2018, 1069.7, 1547665, 1048.95, 1071.72, 1046.75 +02/13/2018, 1052.1, 1213800, 1045, 1058.37, 1044.0872 +02/12/2018, 1051.94, 2054002, 1048, 1061.5, 1040.928 +02/09/2018, 1037.78, 3503970, 1017.25, 1043.97, 992.56 +02/08/2018, 1001.52, 2809890, 1055.41, 1058.62, 1000.66 +02/07/2018, 1048.58, 2353003, 1081.54, 1081.78, 1048.26 +02/06/2018, 1080.6, 3432313, 1027.18, 1081.71, 1023.1367 +02/05/2018, 1055.8, 3769453, 1090.6, 1110, 1052.03 +02/02/2018, 1111.9, 4837979, 1122, 1123.07, 1107.2779 +02/01/2018, 1167.7, 2380221, 1162.61, 1174, 1157.52 +01/31/2018, 1169.94, 1523820, 1170.57, 1173, 1159.13 +01/30/2018, 1163.69, 1541771, 1167.83, 1176.52, 1163.52 +01/29/2018, 1175.58, 1337324, 1176.48, 1186.89, 1171.98 +01/26/2018, 1175.84, 1981173, 1175.08, 1175.84, 1158.11 +01/25/2018, 1170.37, 1461518, 1172.53, 1175.94, 1162.76 +01/24/2018, 1164.24, 1382904, 1177.33, 1179.86, 1161.05 +01/23/2018, 1169.97, 1309862, 1159.85, 1171.6266, 1158.75 +01/22/2018, 1155.81, 1616120, 1137.49, 1159.88, 1135.1101 +01/19/2018, 1137.51, 1390118, 1131.83, 1137.86, 1128.3 +01/18/2018, 1129.79, 1194943, 1131.41, 1132.51, 1117.5 +01/17/2018, 1131.98, 1200476, 1126.22, 1132.6, 1117.01 +01/16/2018, 1121.76, 1566662, 1132.51, 1139.91, 1117.8316 +01/12/2018, 1122.26, 1718491, 1102.41, 1124.29, 1101.15 +01/11/2018, 1105.52, 977727, 1106.3, 1106.525, 1099.59 +01/10/2018, 1102.61, 1042273, 1097.1, 1104.6, 1096.11 +01/09/2018, 1106.26, 900089, 1109.4, 1110.57, 1101.2307 +01/08/2018, 1106.94, 1046767, 1102.23, 1111.27, 1101.62 +01/05/2018, 1102.23, 1279990, 1094, 1104.25, 1092 +01/04/2018, 1086.4, 1002945, 1088, 1093.5699, 1084.0017 +01/03/2018, 1082.48, 1429757, 1064.31, 1086.29, 1063.21 +01/02/2018, 1065, 1236401, 1048.34, 1066.94, 1045.23 +12/29/2017, 1046.4, 886845, 1046.72, 1049.7, 1044.9 +12/28/2017, 1048.14, 833011, 1051.6, 1054.75, 1044.77 +12/27/2017, 1049.37, 1271780, 1057.39, 1058.37, 1048.05 +12/26/2017, 1056.74, 761097, 1058.07, 1060.12, 1050.2 +12/22/2017, 1060.12, 755089, 1061.11, 1064.2, 1059.44 +12/21/2017, 1063.63, 986548, 1064.95, 1069.33, 1061.7938 +12/20/2017, 1064.95, 1268285, 1071.78, 1073.38, 1061.52 +12/19/2017, 1070.68, 1307894, 1075.2, 1076.84, 1063.55 +12/18/2017, 1077.14, 1552016, 1066.08, 1078.49, 1062 +12/15/2017, 1064.19, 3275091, 1054.61, 1067.62, 1049.5 +12/14/2017, 1049.15, 1558684, 1045, 1058.5, 1043.11 +12/13/2017, 1040.61, 1220364, 1046.12, 1046.665, 1038.38 +12/12/2017, 1040.48, 1279511, 1039.63, 1050.31, 1033.6897 +12/11/2017, 1041.1, 1190527, 1035.5, 1043.8, 1032.0504 +12/08/2017, 1037.05, 1288419, 1037.49, 1042.05, 1032.5222 +12/07/2017, 1030.93, 1458145, 1020.43, 1034.24, 1018.071 +12/06/2017, 1018.38, 1258496, 1001.5, 1024.97, 1001.14 +12/05/2017, 1005.15, 2066247, 995.94, 1020.61, 988.28 +12/04/2017, 998.68, 1906058, 1012.66, 1016.1, 995.57 +12/01/2017, 1010.17, 1908962, 1015.8, 1022.4897, 1002.02 +11/30/2017, 1021.41, 1723003, 1022.37, 1028.4899, 1015 +11/29/2017, 1021.66, 2442974, 1042.68, 1044.08, 1015.65 +11/28/2017, 1047.41, 1421027, 1055.09, 1062.375, 1040 +11/27/2017, 1054.21, 1307471, 1040, 1055.46, 1038.44 +11/24/2017, 1040.61, 536996, 1035.87, 1043.178, 1035 +11/22/2017, 1035.96, 746351, 1035, 1039.706, 1031.43 +11/21/2017, 1034.49, 1096161, 1023.31, 1035.11, 1022.655 +11/20/2017, 1018.38, 898389, 1020.26, 1022.61, 1017.5 +11/17/2017, 1019.09, 1366936, 1034.01, 1034.42, 1017.75 +11/16/2017, 1032.5, 1129424, 1022.52, 1035.92, 1022.52 +11/15/2017, 1020.91, 847932, 1019.21, 1024.09, 1015.42 +11/14/2017, 1026, 958708, 1022.59, 1026.81, 1014.15 +11/13/2017, 1025.75, 885565, 1023.42, 1031.58, 1022.57 +11/10/2017, 1028.07, 720674, 1026.46, 1030.76, 1025.28 +11/09/2017, 1031.26, 1244701, 1033.99, 1033.99, 1019.6656 +11/08/2017, 1039.85, 1088395, 1030.52, 1043.522, 1028.45 +11/07/2017, 1033.33, 1112123, 1027.27, 1033.97, 1025.13 +11/06/2017, 1025.9, 1124757, 1028.99, 1034.87, 1025 +11/03/2017, 1032.48, 1075134, 1022.11, 1032.65, 1020.31 +11/02/2017, 1025.58, 1048584, 1021.76, 1028.09, 1013.01 +11/01/2017, 1025.5, 1371619, 1017.21, 1029.67, 1016.95 +10/31/2017, 1016.64, 1331265, 1015.22, 1024, 1010.42 +10/30/2017, 1017.11, 2083490, 1014, 1024.97, 1007.5 +10/27/2017, 1019.27, 5165922, 1009.19, 1048.39, 1008.2 +10/26/2017, 972.56, 2027218, 980, 987.6, 972.2 +10/25/2017, 973.33, 1210368, 968.37, 976.09, 960.5201 +10/24/2017, 970.54, 1206074, 970, 972.23, 961 +10/23/2017, 968.45, 1471544, 989.52, 989.52, 966.12 +10/20/2017, 988.2, 1176177, 989.44, 991, 984.58 +10/19/2017, 984.45, 1312706, 986, 988.88, 978.39 +10/18/2017, 992.81, 1057285, 991.77, 996.72, 986.9747 +10/17/2017, 992.18, 1290152, 990.29, 996.44, 988.59 +10/16/2017, 992, 910246, 992.1, 993.9065, 984 +10/13/2017, 989.68, 1169584, 992, 997.21, 989 +10/12/2017, 987.83, 1278357, 987.45, 994.12, 985 +10/11/2017, 989.25, 1692843, 973.72, 990.71, 972.25 +10/10/2017, 972.6, 968113, 980, 981.57, 966.0801 +10/09/2017, 977, 890620, 980, 985.425, 976.11 +10/06/2017, 978.89, 1146207, 966.7, 979.46, 963.36 +10/05/2017, 969.96, 1210427, 955.49, 970.91, 955.18 +10/04/2017, 951.68, 951766, 957, 960.39, 950.69 +10/03/2017, 957.79, 888303, 954, 958, 949.14 +10/02/2017, 953.27, 1282850, 959.98, 962.54, 947.84 +09/29/2017, 959.11, 1576365, 952, 959.7864, 951.51 +09/28/2017, 949.5, 997036, 941.36, 950.69, 940.55 +09/27/2017, 944.49, 2237538, 927.74, 949.9, 927.74 +09/26/2017, 924.86, 1666749, 923.72, 930.82, 921.14 +09/25/2017, 920.97, 1855742, 925.45, 926.4, 909.7 +09/22/2017, 928.53, 1052170, 927.75, 934.73, 926.48 +09/21/2017, 932.45, 1227059, 933, 936.53, 923.83 +09/20/2017, 931.58, 1535626, 922.98, 933.88, 922 +09/19/2017, 921.81, 912967, 917.42, 922.4199, 912.55 +09/18/2017, 915, 1300759, 920.01, 922.08, 910.6 +09/15/2017, 920.29, 2499466, 924.66, 926.49, 916.36 +09/14/2017, 925.11, 1395497, 931.25, 932.77, 924 +09/13/2017, 935.09, 1101145, 930.66, 937.25, 929.86 +09/12/2017, 932.07, 1133638, 932.59, 933.48, 923.861 +09/11/2017, 929.08, 1266020, 934.25, 938.38, 926.92 +09/08/2017, 926.5, 997699, 936.49, 936.99, 924.88 +09/07/2017, 935.95, 1211472, 931.73, 936.41, 923.62 +09/06/2017, 927.81, 1526209, 930.15, 930.915, 919.27 +09/05/2017, 928.45, 1346791, 933.08, 937, 921.96 +09/01/2017, 937.34, 943657, 941.13, 942.48, 935.15 +08/31/2017, 939.33, 1566888, 931.76, 941.98, 931.76 +08/30/2017, 929.57, 1300616, 920.05, 930.819, 919.65 +08/29/2017, 921.29, 1181391, 905.1, 923.33, 905 +08/28/2017, 913.81, 1085014, 916, 919.245, 911.87 +08/25/2017, 915.89, 1052764, 923.49, 925.555, 915.5 +08/24/2017, 921.28, 1266191, 928.66, 930.84, 915.5 +08/23/2017, 927, 1088575, 921.93, 929.93, 919.36 +08/22/2017, 924.69, 1166320, 912.72, 925.86, 911.4751 +08/21/2017, 906.66, 942328, 910, 913, 903.4 +08/18/2017, 910.67, 1341990, 910.31, 915.275, 907.1543 +08/17/2017, 910.98, 1241782, 925.78, 926.86, 910.98 +08/16/2017, 926.96, 1005261, 925.29, 932.7, 923.445 +08/15/2017, 922.22, 882479, 924.23, 926.5499, 919.82 +08/14/2017, 922.67, 1063404, 922.53, 924.668, 918.19 +08/11/2017, 914.39, 1205652, 907.97, 917.78, 905.58 +08/10/2017, 907.24, 1755521, 917.55, 919.26, 906.13 +08/09/2017, 922.9, 1191332, 920.61, 925.98, 917.2501 +08/08/2017, 926.79, 1057351, 927.09, 935.814, 925.6095 +08/07/2017, 929.36, 1031710, 929.06, 931.7, 926.5 +08/04/2017, 927.96, 1081814, 926.75, 930.3068, 923.03 +08/03/2017, 923.65, 1201519, 930.34, 932.24, 922.24 +08/02/2017, 930.39, 1822272, 928.61, 932.6, 916.68 +08/01/2017, 930.83, 1234612, 932.38, 937.447, 929.26 +07/31/2017, 930.5, 1964748, 941.89, 943.59, 926.04 +07/28/2017, 941.53, 1802343, 929.4, 943.83, 927.5 +07/27/2017, 934.09, 3128819, 951.78, 951.78, 920 +07/26/2017, 947.8, 2069349, 954.68, 955, 942.2788 +07/25/2017, 950.7, 4656609, 953.81, 959.7, 945.4 +07/24/2017, 980.34, 3205374, 972.22, 986.2, 970.77 +07/21/2017, 972.92, 1697190, 962.25, 973.23, 960.15 +07/20/2017, 968.15, 1620636, 975, 975.9, 961.51 +07/19/2017, 970.89, 1221155, 967.84, 973.04, 964.03 +07/18/2017, 965.4, 1152741, 953, 968.04, 950.6 +07/17/2017, 953.42, 1164141, 957, 960.74, 949.2407 +07/14/2017, 955.99, 1052855, 952, 956.91, 948.005 +07/13/2017, 947.16, 1294674, 946.29, 954.45, 943.01 +07/12/2017, 943.83, 1517168, 938.68, 946.3, 934.47 +07/11/2017, 930.09, 1112417, 929.54, 931.43, 922 +07/10/2017, 928.8, 1190237, 921.77, 930.38, 919.59 +07/07/2017, 918.59, 1590456, 908.85, 921.54, 908.85 +07/06/2017, 906.69, 1424290, 904.12, 914.9444, 899.7 +07/05/2017, 911.71, 1813309, 901.76, 914.51, 898.5 +07/03/2017, 898.7, 1710373, 912.18, 913.94, 894.79 +06/30/2017, 908.73, 2086340, 926.05, 926.05, 908.31 +06/29/2017, 917.79, 3287991, 929.92, 931.26, 910.62 +06/28/2017, 940.49, 2719213, 929, 942.75, 916 +06/27/2017, 927.33, 2566047, 942.46, 948.29, 926.85 +06/26/2017, 952.27, 1596664, 969.9, 973.31, 950.79 +06/23/2017, 965.59, 1527513, 956.83, 966, 954.2 +06/22/2017, 957.09, 941639, 958.7, 960.72, 954.55 +06/21/2017, 959.45, 1201971, 953.64, 960.1, 950.76 +06/20/2017, 950.63, 1125520, 957.52, 961.62, 950.01 +06/19/2017, 957.37, 1520715, 949.96, 959.99, 949.05 +06/16/2017, 939.78, 3061794, 940, 942.04, 931.595 +06/15/2017, 942.31, 2065271, 933.97, 943.339, 924.44 +06/14/2017, 950.76, 1487378, 959.92, 961.15, 942.25 +06/13/2017, 953.4, 2012980, 951.91, 959.98, 944.09 +06/12/2017, 942.9, 3762434, 939.56, 949.355, 915.2328 +06/09/2017, 949.83, 3305545, 984.5, 984.5, 935.63 +06/08/2017, 983.41, 1477151, 982.35, 984.57, 977.2 +06/07/2017, 981.08, 1447172, 979.65, 984.15, 975.77 +06/06/2017, 976.57, 1814323, 983.16, 988.25, 975.14 +06/05/2017, 983.68, 1251903, 976.55, 986.91, 975.1 +06/02/2017, 975.6, 1750723, 969.46, 975.88, 966 +06/01/2017, 966.95, 1408958, 968.95, 971.5, 960.01 +05/31/2017, 964.86, 2447176, 975.02, 979.27, 960.18 +05/30/2017, 975.88, 1466288, 970.31, 976.2, 969.49 +05/26/2017, 971.47, 1251425, 969.7, 974.98, 965.03 +05/25/2017, 969.54, 1659422, 957.33, 972.629, 955.47 +05/24/2017, 954.96, 1031408, 952.98, 955.09, 949.5 +05/23/2017, 948.82, 1269438, 947.92, 951.4666, 942.575 +05/22/2017, 941.86, 1118456, 935, 941.8828, 935 +05/19/2017, 934.01, 1389848, 931.47, 937.755, 931 +05/18/2017, 930.24, 1596058, 921, 933.17, 918.75 +05/17/2017, 919.62, 2357922, 935.67, 939.3325, 918.14 +05/16/2017, 943, 968288, 940, 943.11, 937.58 +05/15/2017, 937.08, 1104595, 932.95, 938.25, 929.34 +05/12/2017, 932.22, 1050377, 931.53, 933.44, 927.85 +05/11/2017, 930.6, 834997, 925.32, 932.53, 923.0301 +05/10/2017, 928.78, 1173887, 931.98, 932, 925.16 +05/09/2017, 932.17, 1581236, 936.95, 937.5, 929.53 +05/08/2017, 934.3, 1328885, 926.12, 936.925, 925.26 +05/05/2017, 927.13, 1910317, 933.54, 934.9, 925.2 +05/04/2017, 931.66, 1421938, 926.07, 935.93, 924.59 +05/03/2017, 927.04, 1497565, 914.86, 928.1, 912.5426 +05/02/2017, 916.44, 1543696, 909.62, 920.77, 909.4526 +05/01/2017, 912.57, 2114629, 901.94, 915.68, 901.45 +04/28/2017, 905.96, 3223850, 910.66, 916.85, 905.77 +04/27/2017, 874.25, 2009509, 873.6, 875.4, 870.38 +04/26/2017, 871.73, 1233724, 874.23, 876.05, 867.7481 +04/25/2017, 872.3, 1670095, 865, 875, 862.81 +04/24/2017, 862.76, 1371722, 851.2, 863.45, 849.86 +04/21/2017, 843.19, 1323364, 842.88, 843.88, 840.6 +04/20/2017, 841.65, 957994, 841.44, 845.2, 839.32 +04/19/2017, 838.21, 954324, 839.79, 842.22, 836.29 +04/18/2017, 836.82, 835433, 834.22, 838.93, 832.71 +04/17/2017, 837.17, 894540, 825.01, 837.75, 824.47 +04/13/2017, 823.56, 1118221, 822.14, 826.38, 821.44 +04/12/2017, 824.32, 900059, 821.93, 826.66, 821.02 +04/11/2017, 823.35, 1078951, 824.71, 827.4267, 817.0201 +04/10/2017, 824.73, 978825, 825.39, 829.35, 823.77 +04/07/2017, 824.67, 1056692, 827.96, 828.485, 820.5127 +04/06/2017, 827.88, 1254235, 832.4, 836.39, 826.46 +04/05/2017, 831.41, 1553163, 835.51, 842.45, 830.72 +04/04/2017, 834.57, 1044455, 831.36, 835.18, 829.0363 +04/03/2017, 838.55, 1670349, 829.22, 840.85, 829.22 +03/31/2017, 829.56, 1401756, 828.97, 831.64, 827.39 +03/30/2017, 831.5, 1055263, 833.5, 833.68, 829 +03/29/2017, 831.41, 1785006, 825, 832.765, 822.3801 +03/28/2017, 820.92, 1620532, 820.41, 825.99, 814.027 +03/27/2017, 819.51, 1894735, 806.95, 821.63, 803.37 +03/24/2017, 814.43, 1980415, 820.08, 821.93, 808.89 +03/23/2017, 817.58, 3485390, 821, 822.57, 812.257 +03/22/2017, 829.59, 1399409, 831.91, 835.55, 827.1801 +03/21/2017, 830.46, 2461375, 851.4, 853.5, 829.02 +03/20/2017, 848.4, 1217560, 850.01, 850.22, 845.15 +03/17/2017, 852.12, 1712397, 851.61, 853.4, 847.11 +03/16/2017, 848.78, 977384, 849.03, 850.85, 846.13 +03/15/2017, 847.2, 1381328, 847.59, 848.63, 840.77 +03/14/2017, 845.62, 779920, 843.64, 847.24, 840.8 +03/13/2017, 845.54, 1149928, 844, 848.685, 843.25 +03/10/2017, 843.25, 1702731, 843.28, 844.91, 839.5 +03/09/2017, 838.68, 1261393, 836, 842, 834.21 +03/08/2017, 835.37, 988900, 833.51, 838.15, 831.79 +03/07/2017, 831.91, 1037573, 827.4, 833.41, 826.52 +03/06/2017, 827.78, 1108799, 826.95, 828.88, 822.4 +03/03/2017, 829.08, 890640, 830.56, 831.36, 825.751 +03/02/2017, 830.63, 937824, 833.85, 834.51, 829.64 +03/01/2017, 835.24, 1495934, 828.85, 836.255, 827.26 +02/28/2017, 823.21, 2258695, 825.61, 828.54, 820.2 +02/27/2017, 829.28, 1101120, 824.55, 830.5, 824 +02/24/2017, 828.64, 1392039, 827.73, 829, 824.2 +02/23/2017, 831.33, 1471342, 830.12, 832.46, 822.88 +02/22/2017, 830.76, 983058, 828.66, 833.25, 828.64 +02/21/2017, 831.66, 1259841, 828.66, 833.45, 828.35 +02/17/2017, 828.07, 1602549, 823.02, 828.07, 821.655 +02/16/2017, 824.16, 1285919, 819.93, 824.4, 818.98 +02/15/2017, 818.98, 1311316, 819.36, 823, 818.47 +02/14/2017, 820.45, 1054472, 819, 823, 816 +02/13/2017, 819.24, 1205835, 816, 820.959, 815.49 +02/10/2017, 813.67, 1134701, 811.7, 815.25, 809.78 +02/09/2017, 809.56, 990260, 809.51, 810.66, 804.54 +02/08/2017, 808.38, 1155892, 807, 811.84, 803.1903 +02/07/2017, 806.97, 1240257, 803.99, 810.5, 801.78 +02/06/2017, 801.34, 1182882, 799.7, 801.67, 795.2501 +02/03/2017, 801.49, 1461217, 802.99, 806, 800.37 +02/02/2017, 798.53, 1530827, 793.8, 802.7, 792 +02/01/2017, 795.695, 2027708, 799.68, 801.19, 791.19 +01/31/2017, 796.79, 2153957, 796.86, 801.25, 790.52 +01/30/2017, 802.32, 3243568, 814.66, 815.84, 799.8 +01/27/2017, 823.31, 2964989, 834.71, 841.95, 820.44 +01/26/2017, 832.15, 2944642, 837.81, 838, 827.01 +01/25/2017, 835.67, 1612854, 829.62, 835.77, 825.06 +01/24/2017, 823.87, 1472228, 822.3, 825.9, 817.821 +01/23/2017, 819.31, 1962506, 807.25, 820.87, 803.74 +01/20/2017, 805.02, 1668638, 806.91, 806.91, 801.69 +01/19/2017, 802.175, 917085, 805.12, 809.48, 801.8 +01/18/2017, 806.07, 1293893, 805.81, 806.205, 800.99 +01/17/2017, 804.61, 1361935, 807.08, 807.14, 800.37 +01/13/2017, 807.88, 1098154, 807.48, 811.2244, 806.69 +01/12/2017, 806.36, 1352872, 807.14, 807.39, 799.17 +01/11/2017, 807.91, 1065360, 805, 808.15, 801.37 +01/10/2017, 804.79, 1176637, 807.86, 809.1299, 803.51 +01/09/2017, 806.65, 1274318, 806.4, 809.9664, 802.83 +01/06/2017, 806.15, 1639246, 795.26, 807.9, 792.2041 +01/05/2017, 794.02, 1334028, 786.08, 794.48, 785.02 +01/04/2017, 786.9, 1071198, 788.36, 791.34, 783.16 +01/03/2017, 786.14, 1657291, 778.81, 789.63, 775.8 +12/30/2016, 771.82, 1769809, 782.75, 782.78, 770.41 +12/29/2016, 782.79, 743808, 783.33, 785.93, 778.92 +12/28/2016, 785.05, 1142148, 793.7, 794.23, 783.2 +12/27/2016, 791.55, 789151, 790.68, 797.86, 787.657 +12/23/2016, 789.91, 623682, 790.9, 792.74, 787.28 +12/22/2016, 791.26, 972147, 792.36, 793.32, 788.58 +12/21/2016, 794.56, 1208770, 795.84, 796.6757, 787.1 +12/20/2016, 796.42, 950345, 796.76, 798.65, 793.27 +12/19/2016, 794.2, 1231966, 790.22, 797.66, 786.27 +12/16/2016, 790.8, 2435100, 800.4, 800.8558, 790.29 +12/15/2016, 797.85, 1623709, 797.34, 803, 792.92 +12/14/2016, 797.07, 1700875, 797.4, 804, 794.01 +12/13/2016, 796.1, 2122735, 793.9, 804.3799, 793.34 +12/12/2016, 789.27, 2102288, 785.04, 791.25, 784.3554 +12/09/2016, 789.29, 1821146, 780, 789.43, 779.021 +12/08/2016, 776.42, 1487517, 772.48, 778.18, 767.23 +12/07/2016, 771.19, 1757710, 761, 771.36, 755.8 +12/06/2016, 759.11, 1690365, 764.73, 768.83, 757.34 +12/05/2016, 762.52, 1393566, 757.71, 763.9, 752.9 +12/02/2016, 750.5, 1452181, 744.59, 754, 743.1 +12/01/2016, 747.92, 3017001, 757.44, 759.85, 737.0245 +11/30/2016, 758.04, 2386628, 770.07, 772.99, 754.83 +11/29/2016, 770.84, 1616427, 771.53, 778.5, 768.24 +11/28/2016, 768.24, 2177039, 760, 779.53, 759.8 +11/25/2016, 761.68, 587421, 764.26, 765, 760.52 +11/23/2016, 760.99, 1477501, 767.73, 768.2825, 755.25 +11/22/2016, 768.27, 1592372, 772.63, 776.96, 767 +11/21/2016, 769.2, 1324431, 762.61, 769.7, 760.6 +11/18/2016, 760.54, 1528555, 771.37, 775, 760 +11/17/2016, 771.23, 1298484, 766.92, 772.7, 764.23 +11/16/2016, 764.48, 1468196, 755.2, 766.36, 750.51 +11/15/2016, 758.49, 2375056, 746.97, 764.4162, 746.97 +11/14/2016, 736.08, 3644965, 755.6, 757.85, 727.54 +11/11/2016, 754.02, 2421889, 756.54, 760.78, 750.38 +11/10/2016, 762.56, 4733916, 791.17, 791.17, 752.18 +11/09/2016, 785.31, 2603860, 779.94, 791.2265, 771.67 +11/08/2016, 790.51, 1361472, 783.4, 795.633, 780.19 +11/07/2016, 782.52, 1574426, 774.5, 785.19, 772.55 +11/04/2016, 762.02, 2131948, 750.66, 770.36, 750.5611 +11/03/2016, 762.13, 1933937, 767.25, 769.95, 759.03 +11/02/2016, 768.7, 1905814, 778.2, 781.65, 763.4496 +11/01/2016, 783.61, 2404898, 782.89, 789.49, 775.54 +10/31/2016, 784.54, 2420892, 795.47, 796.86, 784 +10/28/2016, 795.37, 4261912, 808.35, 815.49, 793.59 +10/27/2016, 795.35, 2723097, 801, 803.49, 791.5 +10/26/2016, 799.07, 1645403, 806.34, 806.98, 796.32 +10/25/2016, 807.67, 1575020, 816.68, 816.68, 805.14 +10/24/2016, 813.11, 1693162, 804.9, 815.18, 804.82 +10/21/2016, 799.37, 1262042, 795, 799.5, 794 +10/20/2016, 796.97, 1755546, 803.3, 803.97, 796.03 +10/19/2016, 801.56, 1762990, 798.86, 804.63, 797.635 +10/18/2016, 795.26, 2046338, 787.85, 801.61, 785.565 +10/17/2016, 779.96, 1091524, 779.8, 785.85, 777.5 +10/14/2016, 778.53, 851512, 781.65, 783.95, 776 +10/13/2016, 778.19, 1360619, 781.22, 781.22, 773 +10/12/2016, 786.14, 935138, 783.76, 788.13, 782.06 +10/11/2016, 783.07, 1371461, 786.66, 792.28, 780.58 +10/10/2016, 785.94, 1161410, 777.71, 789.38, 775.87 +10/07/2016, 775.08, 932444, 779.66, 779.66, 770.75 +10/06/2016, 776.86, 1066910, 779, 780.48, 775.54 +10/05/2016, 776.47, 1457661, 779.31, 782.07, 775.65 +10/04/2016, 776.43, 1198361, 776.03, 778.71, 772.89 +10/03/2016, 772.56, 1276614, 774.25, 776.065, 769.5 +09/30/2016, 777.29, 1583293, 776.33, 780.94, 774.09 +09/29/2016, 775.01, 1310252, 781.44, 785.8, 774.232 +09/28/2016, 781.56, 1108249, 777.85, 781.81, 774.97 +09/27/2016, 783.01, 1152760, 775.5, 785.9899, 774.308 +09/26/2016, 774.21, 1531788, 782.74, 782.74, 773.07 +09/23/2016, 786.9, 1411439, 786.59, 788.93, 784.15 +09/22/2016, 787.21, 1483899, 780, 789.85, 778.44 +09/21/2016, 776.22, 1166290, 772.66, 777.16, 768.301 +09/20/2016, 771.41, 975434, 769, 773.33, 768.53 +09/19/2016, 765.7, 1171969, 772.42, 774, 764.4406 +09/16/2016, 768.88, 2047036, 769.75, 769.75, 764.66 +09/15/2016, 771.76, 1344945, 762.89, 773.8, 759.96 +09/14/2016, 762.49, 1093723, 759.61, 767.68, 759.11 +09/13/2016, 759.69, 1394158, 764.48, 766.2195, 755.8 +09/12/2016, 769.02, 1310493, 755.13, 770.29, 754.0001 +09/09/2016, 759.66, 1879903, 770.1, 773.245, 759.66 +09/08/2016, 775.32, 1268663, 778.59, 780.35, 773.58 +09/07/2016, 780.35, 893874, 780, 782.73, 776.2 +09/06/2016, 780.08, 1441864, 773.45, 782, 771 +09/02/2016, 771.46, 1070725, 773.01, 773.9199, 768.41 +09/01/2016, 768.78, 925019, 769.25, 771.02, 764.3 +08/31/2016, 767.05, 1247937, 767.01, 769.09, 765.38 +08/30/2016, 769.09, 1129932, 769.33, 774.466, 766.84 +08/29/2016, 772.15, 847537, 768.74, 774.99, 766.615 +08/26/2016, 769.54, 1164713, 769, 776.0799, 765.85 +08/25/2016, 769.41, 926856, 767, 771.89, 763.1846 +08/24/2016, 769.64, 1071569, 770.58, 774.5, 767.07 +08/23/2016, 772.08, 925356, 775.48, 776.44, 771.785 +08/22/2016, 772.15, 950417, 773.27, 774.54, 770.0502 +08/19/2016, 775.42, 860899, 775, 777.1, 773.13 +08/18/2016, 777.5, 718882, 780.01, 782.86, 777 +08/17/2016, 779.91, 921666, 777.32, 780.81, 773.53 +08/16/2016, 777.14, 1027836, 780.3, 780.98, 773.444 +08/15/2016, 782.44, 938183, 783.75, 787.49, 780.11 +08/12/2016, 783.22, 739761, 781.5, 783.395, 780.4 +08/11/2016, 784.85, 971742, 785, 789.75, 782.97 +08/10/2016, 784.68, 784559, 783.75, 786.8123, 782.778 +08/09/2016, 784.26, 1318457, 781.1, 788.94, 780.57 +08/08/2016, 781.76, 1106693, 782, 782.63, 778.091 +08/05/2016, 782.22, 1799478, 773.78, 783.04, 772.34 +08/04/2016, 771.61, 1139972, 772.22, 774.07, 768.795 +08/03/2016, 773.18, 1283186, 767.18, 773.21, 766.82 +08/02/2016, 771.07, 1782822, 768.69, 775.84, 767.85 +08/01/2016, 772.88, 2697699, 761.09, 780.43, 761.09 +07/29/2016, 768.79, 3830103, 772.71, 778.55, 766.77 +07/28/2016, 745.91, 3473040, 747.04, 748.65, 739.3 +07/27/2016, 741.77, 1509133, 738.28, 744.46, 737 +07/26/2016, 738.42, 1182993, 739.04, 741.69, 734.27 +07/25/2016, 739.77, 1031643, 740.67, 742.61, 737.5 +07/22/2016, 742.74, 1256741, 741.86, 743.24, 736.56 +07/21/2016, 738.63, 1022229, 740.36, 741.69, 735.831 +07/20/2016, 741.19, 1283931, 737.33, 742.13, 737.1 +07/19/2016, 736.96, 1225467, 729.89, 736.99, 729 +07/18/2016, 733.78, 1284740, 722.71, 736.13, 721.19 +07/15/2016, 719.85, 1277514, 725.73, 725.74, 719.055 +07/14/2016, 720.95, 949456, 721.58, 722.21, 718.03 +07/13/2016, 716.98, 933352, 723.62, 724, 716.85 +07/12/2016, 720.64, 1336112, 719.12, 722.94, 715.91 +07/11/2016, 715.09, 1107039, 708.05, 716.51, 707.24 +07/08/2016, 705.63, 1573909, 699.5, 705.71, 696.435 +07/07/2016, 695.36, 1303661, 698.08, 698.2, 688.215 +07/06/2016, 697.77, 1411080, 689.98, 701.68, 689.09 +07/05/2016, 694.49, 1462879, 696.06, 696.94, 688.88 +07/01/2016, 699.21, 1344387, 692.2, 700.65, 692.1301 +06/30/2016, 692.1, 1597298, 685.47, 692.32, 683.65 +06/29/2016, 684.11, 1931436, 683, 687.4292, 681.41 +06/28/2016, 680.04, 2169704, 678.97, 680.33, 673 +06/27/2016, 668.26, 2632011, 671, 672.3, 663.284 +06/24/2016, 675.22, 4442943, 675.17, 689.4, 673.45 +06/23/2016, 701.87, 2166183, 697.45, 701.95, 687 +06/22/2016, 697.46, 1182161, 699.06, 700.86, 693.0819 +06/21/2016, 695.94, 1464836, 698.4, 702.77, 692.01 +06/20/2016, 693.71, 2080645, 698.77, 702.48, 693.41 +06/17/2016, 691.72, 3397720, 708.65, 708.82, 688.4515 +06/16/2016, 710.36, 1981657, 714.91, 716.65, 703.26 +06/15/2016, 718.92, 1213386, 719, 722.98, 717.31 +06/14/2016, 718.27, 1303808, 716.48, 722.47, 713.12 +06/13/2016, 718.36, 1255199, 716.51, 725.44, 716.51 +06/10/2016, 719.41, 1213989, 719.47, 725.89, 716.43 +06/09/2016, 728.58, 987635, 722.87, 729.54, 722.3361 +06/08/2016, 728.28, 1583325, 723.96, 728.57, 720.58 +06/07/2016, 716.65, 1336348, 719.84, 721.98, 716.55 +06/06/2016, 716.55, 1565955, 724.91, 724.91, 714.61 +06/03/2016, 722.34, 1225924, 729.27, 729.49, 720.56 +06/02/2016, 730.4, 1340664, 732.5, 733.02, 724.17 +06/01/2016, 734.15, 1251468, 734.53, 737.21, 730.66 +05/31/2016, 735.72, 2128358, 731.74, 739.73, 731.26 +05/27/2016, 732.66, 1974425, 724.01, 733.936, 724 +05/26/2016, 724.12, 1573635, 722.87, 728.33, 720.28 +05/25/2016, 725.27, 1629790, 720.76, 727.51, 719.7047 +05/24/2016, 720.09, 1926828, 706.86, 720.97, 706.86 +05/23/2016, 704.24, 1326386, 706.53, 711.4781, 704.18 +05/20/2016, 709.74, 1825830, 701.62, 714.58, 700.52 +05/19/2016, 700.32, 1668887, 702.36, 706, 696.8 +05/18/2016, 706.63, 1765632, 703.67, 711.6, 700.63 +05/17/2016, 706.23, 1999883, 715.99, 721.52, 704.11 +05/16/2016, 716.49, 1316719, 709.13, 718.48, 705.65 +05/13/2016, 710.83, 1307559, 711.93, 716.6619, 709.26 +05/12/2016, 713.31, 1361170, 717.06, 719.25, 709 +05/11/2016, 715.29, 1690862, 723.41, 724.48, 712.8 +05/10/2016, 723.18, 1568621, 716.75, 723.5, 715.72 +05/09/2016, 712.9, 1509892, 712, 718.71, 710 +05/06/2016, 711.12, 1828508, 698.38, 711.86, 698.1067 +05/05/2016, 701.43, 1680220, 697.7, 702.3199, 695.72 +05/04/2016, 695.7, 1692757, 690.49, 699.75, 689.01 +05/03/2016, 692.36, 1541297, 696.87, 697.84, 692 +05/02/2016, 698.21, 1645013, 697.63, 700.64, 691 +04/29/2016, 693.01, 2486584, 690.7, 697.62, 689 +04/28/2016, 691.02, 2859790, 708.26, 714.17, 689.55 +04/27/2016, 705.84, 3094905, 707.29, 708.98, 692.3651 +04/26/2016, 708.14, 2739133, 725.42, 725.766, 703.0264 +04/25/2016, 723.15, 1956956, 716.1, 723.93, 715.59 +04/22/2016, 718.77, 5949699, 726.3, 736.12, 713.61 +04/21/2016, 759.14, 2995094, 755.38, 760.45, 749.55 +04/20/2016, 752.67, 1526776, 758, 758.1315, 750.01 +04/19/2016, 753.93, 2027962, 769.51, 769.9, 749.33 +04/18/2016, 766.61, 1557199, 760.46, 768.05, 757.3 +04/15/2016, 759, 1807062, 753.98, 761, 752.6938 +04/14/2016, 753.2, 1134056, 754.01, 757.31, 752.705 +04/13/2016, 751.72, 1707397, 749.16, 754.38, 744.261 +04/12/2016, 743.09, 1349780, 738, 743.83, 731.01 +04/11/2016, 736.1, 1218789, 743.02, 745, 736.05 +04/08/2016, 739.15, 1289869, 743.97, 745.45, 735.55 +04/07/2016, 740.28, 1452369, 745.37, 746.9999, 736.28 +04/06/2016, 745.69, 1052171, 735.77, 746.24, 735.56 +04/05/2016, 737.8, 1130817, 738, 742.8, 735.37 +04/04/2016, 745.29, 1134214, 750.06, 752.8, 742.43 +04/01/2016, 749.91, 1576240, 738.6, 750.34, 737 +03/31/2016, 744.95, 1718638, 749.25, 750.85, 740.94 +03/30/2016, 750.53, 1782278, 750.1, 757.88, 748.74 +03/29/2016, 744.77, 1902254, 734.59, 747.25, 728.76 +03/28/2016, 733.53, 1300817, 736.79, 738.99, 732.5 +03/24/2016, 735.3, 1570474, 732.01, 737.747, 731 +03/23/2016, 738.06, 1431130, 742.36, 745.7199, 736.15 +03/22/2016, 740.75, 1269263, 737.46, 745, 737.46 +03/21/2016, 742.09, 1835963, 736.5, 742.5, 733.5157 +03/18/2016, 737.6, 2982194, 741.86, 742, 731.83 +03/17/2016, 737.78, 1859562, 736.45, 743.07, 736 +03/16/2016, 736.09, 1621412, 726.37, 737.47, 724.51 +03/15/2016, 728.33, 1720790, 726.92, 732.29, 724.77 +03/14/2016, 730.49, 1717002, 726.81, 735.5, 725.15 +03/11/2016, 726.82, 1968164, 720, 726.92, 717.125 +03/10/2016, 712.82, 2830630, 708.12, 716.44, 703.36 +03/09/2016, 705.24, 1419661, 698.47, 705.68, 694 +03/08/2016, 693.97, 2075305, 688.59, 703.79, 685.34 +03/07/2016, 695.16, 2986064, 706.9, 708.0912, 686.9 +03/04/2016, 710.89, 1971379, 714.99, 716.49, 706.02 +03/03/2016, 712.42, 1956958, 718.68, 719.45, 706.02 +03/02/2016, 718.85, 1629501, 719, 720, 712 +03/01/2016, 718.81, 2148608, 703.62, 718.81, 699.77 +02/29/2016, 697.77, 2478214, 700.32, 710.89, 697.68 +02/26/2016, 705.07, 2241785, 708.58, 713.43, 700.86 +02/25/2016, 705.75, 1640430, 700.01, 705.98, 690.585 +02/24/2016, 699.56, 1961258, 688.92, 700, 680.78 +02/23/2016, 695.85, 2006572, 701.45, 708.4, 693.58 +02/22/2016, 706.46, 1949046, 707.45, 713.24, 702.51 +02/19/2016, 700.91, 1585152, 695.03, 703.0805, 694.05 +02/18/2016, 697.35, 1880306, 710, 712.35, 696.03 +02/17/2016, 708.4, 2490021, 699, 709.75, 691.38 +02/16/2016, 691, 2517324, 692.98, 698, 685.05 +02/12/2016, 682.4, 2138937, 690.26, 693.75, 678.6 +02/11/2016, 683.11, 3021587, 675, 689.35, 668.8675 +02/10/2016, 684.12, 2629130, 686.86, 701.31, 682.13 +02/09/2016, 678.11, 3605792, 672.32, 699.9, 668.77 +02/08/2016, 682.74, 4241416, 667.85, 684.03, 663.06 +02/05/2016, 683.57, 5098357, 703.87, 703.99, 680.15 +02/04/2016, 708.01, 5157988, 722.81, 727, 701.86 +02/03/2016, 726.95, 6166731, 770.22, 774.5, 720.5 +02/02/2016, 764.65, 6340548, 784.5, 789.8699, 764.65 +02/01/2016, 752, 5065235, 750.46, 757.86, 743.27 +01/29/2016, 742.95, 3464432, 731.53, 744.9899, 726.8 +01/28/2016, 730.96, 2664956, 722.22, 733.69, 712.35 +01/27/2016, 699.99, 2175913, 713.67, 718.235, 694.39 +01/26/2016, 713.04, 1329141, 713.85, 718.28, 706.48 +01/25/2016, 711.67, 1709777, 723.58, 729.68, 710.01 +01/22/2016, 725.25, 2009951, 723.6, 728.13, 720.121 +01/21/2016, 706.59, 2411079, 702.18, 719.19, 694.46 +01/20/2016, 698.45, 3441642, 688.61, 706.85, 673.26 +01/19/2016, 701.79, 2264747, 703.3, 709.98, 693.4101 +01/15/2016, 694.45, 3604137, 692.29, 706.74, 685.37 +01/14/2016, 714.72, 2225495, 705.38, 721.925, 689.1 +01/13/2016, 700.56, 2497086, 730.85, 734.74, 698.61 +01/12/2016, 726.07, 2010026, 721.68, 728.75, 717.3165 +01/11/2016, 716.03, 2089495, 716.61, 718.855, 703.54 +01/08/2016, 714.47, 2449420, 731.45, 733.23, 713 +01/07/2016, 726.39, 2960578, 730.31, 738.5, 719.06 +01/06/2016, 743.62, 1943685, 730, 747.18, 728.92 +01/05/2016, 742.58, 1949386, 746.45, 752, 738.64 +01/04/2016, 741.84, 3271348, 743, 744.06, 731.2577 +12/31/2015, 758.88, 1500129, 769.5, 769.5, 758.34 +12/30/2015, 771, 1293514, 776.6, 777.6, 766.9 +12/29/2015, 776.6, 1764044, 766.69, 779.98, 766.43 +12/28/2015, 762.51, 1515574, 752.92, 762.99, 749.52 +12/24/2015, 748.4, 527223, 749.55, 751.35, 746.62 +12/23/2015, 750.31, 1566723, 753.47, 754.21, 744 +12/22/2015, 750, 1365420, 751.65, 754.85, 745.53 +12/21/2015, 747.77, 1524535, 746.13, 750, 740 +12/18/2015, 739.31, 3140906, 746.51, 754.13, 738.15 +12/17/2015, 749.43, 1551087, 762.42, 762.68, 749 +12/16/2015, 758.09, 1986319, 750, 760.59, 739.435 +12/15/2015, 743.4, 2661199, 753, 758.08, 743.01 +12/14/2015, 747.77, 2417778, 741.79, 748.73, 724.17 +12/11/2015, 738.87, 2223284, 741.16, 745.71, 736.75 +12/10/2015, 749.46, 1988035, 752.85, 755.85, 743.83 +12/09/2015, 751.61, 2697978, 759.17, 764.23, 737.001 +12/08/2015, 762.37, 1829004, 757.89, 764.8, 754.2 +12/07/2015, 763.25, 1811336, 767.77, 768.73, 755.09 +12/04/2015, 766.81, 2756194, 753.1, 768.49, 750 +12/03/2015, 752.54, 2589641, 766.01, 768.995, 745.63 +12/02/2015, 762.38, 2196721, 768.9, 775.955, 758.96 +12/01/2015, 767.04, 2131827, 747.11, 768.95, 746.7 +11/30/2015, 742.6, 2045584, 748.81, 754.93, 741.27 +11/27/2015, 750.26, 838528, 748.46, 753.41, 747.49 +11/25/2015, 748.15, 1122224, 748.14, 752, 746.06 +11/24/2015, 748.28, 2333700, 752, 755.279, 737.63 +11/23/2015, 755.98, 1414640, 757.45, 762.7075, 751.82 +11/20/2015, 756.6, 2212934, 746.53, 757.92, 743 +11/19/2015, 738.41, 1327265, 738.74, 742, 737.43 +11/18/2015, 740, 1683978, 727.58, 741.41, 727 +11/17/2015, 725.3, 1507449, 729.29, 731.845, 723.027 +11/16/2015, 728.96, 1904395, 715.6, 729.49, 711.33 +11/13/2015, 717, 2072392, 729.17, 731.15, 716.73 +11/12/2015, 731.23, 1836567, 731, 737.8, 728.645 +11/11/2015, 735.4, 1366611, 732.46, 741, 730.23 +11/10/2015, 728.32, 1606499, 724.4, 730.59, 718.5001 +11/09/2015, 724.89, 2068920, 730.2, 734.71, 719.43 +11/06/2015, 733.76, 1510586, 731.5, 735.41, 727.01 +11/05/2015, 731.25, 1861100, 729.47, 739.48, 729.47 +11/04/2015, 728.11, 1705745, 722, 733.1, 721.9 +11/03/2015, 722.16, 1565355, 718.86, 724.65, 714.72 +11/02/2015, 721.11, 1885155, 711.06, 721.62, 705.85 +10/30/2015, 710.81, 1907732, 715.73, 718, 710.05 +10/29/2015, 716.92, 1455508, 710.5, 718.26, 710.01 +10/28/2015, 712.95, 2178841, 707.33, 712.98, 703.08 +10/27/2015, 708.49, 2232183, 707.38, 713.62, 704.55 +10/26/2015, 712.78, 2709292, 701.55, 719.15, 701.26 +10/23/2015, 702, 6651909, 727.5, 730, 701.5 +10/22/2015, 651.79, 3994360, 646.7, 657.8, 644.01 +10/21/2015, 642.61, 1792869, 654.15, 655.87, 641.73 +10/20/2015, 650.28, 2498077, 664.04, 664.7197, 644.195 +10/19/2015, 666.1, 1465691, 661.18, 666.82, 659.58 +10/16/2015, 662.2, 1610712, 664.11, 664.97, 657.2 +10/15/2015, 661.74, 1832832, 654.66, 663.13, 654.46 +10/14/2015, 651.16, 1413798, 653.21, 659.39, 648.85 +10/13/2015, 652.3, 1806003, 643.15, 657.8125, 643.15 +10/12/2015, 646.67, 1275565, 642.09, 648.5, 639.01 +10/09/2015, 643.61, 1648656, 640, 645.99, 635.318 +10/08/2015, 639.16, 2181990, 641.36, 644.45, 625.56 +10/07/2015, 642.36, 2092536, 649.24, 650.609, 632.15 +10/06/2015, 645.44, 2235078, 638.84, 649.25, 636.5295 +10/05/2015, 641.47, 1802263, 632, 643.01, 627 +10/02/2015, 626.91, 2681241, 607.2, 627.34, 603.13 +10/01/2015, 611.29, 1866223, 608.37, 612.09, 599.85 +09/30/2015, 608.42, 2412754, 603.28, 608.76, 600.73 +09/29/2015, 594.97, 2310065, 597.28, 605, 590.22 +09/28/2015, 594.89, 3118693, 610.34, 614.605, 589.38 +09/25/2015, 611.97, 2173134, 629.77, 629.77, 611 +09/24/2015, 625.8, 2238097, 616.64, 627.32, 612.4 +09/23/2015, 622.36, 1470633, 622.05, 628.93, 620 +09/22/2015, 622.69, 2561551, 627, 627.55, 615.43 +09/21/2015, 635.44, 1786543, 634.4, 636.49, 625.94 +09/18/2015, 629.25, 5123314, 636.79, 640, 627.02 +09/17/2015, 642.9, 2259404, 637.79, 650.9, 635.02 +09/16/2015, 635.98, 1276250, 635.47, 637.95, 632.32 +09/15/2015, 635.14, 2082426, 626.7, 638.7, 623.78 +09/14/2015, 623.24, 1701618, 625.7, 625.86, 619.43 +09/11/2015, 625.77, 1372803, 619.75, 625.78, 617.42 +09/10/2015, 621.35, 1903334, 613.1, 624.16, 611.43 +09/09/2015, 612.72, 1699686, 621.22, 626.52, 609.6 +09/08/2015, 614.66, 2277487, 612.49, 616.31, 604.12 +09/04/2015, 600.7, 2087028, 600, 603.47, 595.25 +09/03/2015, 606.25, 1757851, 617, 619.71, 602.8213 +09/02/2015, 614.34, 2573982, 605.59, 614.34, 599.71 +09/01/2015, 597.79, 3699844, 602.36, 612.86, 594.1 +08/31/2015, 618.25, 2172168, 627.54, 635.8, 617.68 +08/28/2015, 630.38, 1975818, 632.82, 636.88, 624.56 +08/27/2015, 637.61, 3485906, 639.4, 643.59, 622 +08/26/2015, 628.62, 4187276, 610.35, 631.71, 599.05 +08/25/2015, 582.06, 3521916, 614.91, 617.45, 581.11 +08/24/2015, 589.61, 5727282, 573, 614, 565.05 +08/21/2015, 612.48, 4261666, 639.78, 640.05, 612.33 +08/20/2015, 646.83, 2854028, 655.46, 662.99, 642.9 +08/19/2015, 660.9, 2132265, 656.6, 667, 654.19 +08/18/2015, 656.13, 1455664, 661.9, 664, 653.46 +08/17/2015, 660.87, 1050553, 656.8, 661.38, 651.24 +08/14/2015, 657.12, 1071333, 655.01, 659.855, 652.66 +08/13/2015, 656.45, 1807182, 659.323, 664.5, 651.661 +08/12/2015, 659.56, 2938651, 663.08, 665, 652.29 +08/11/2015, 660.78, 5016425, 669.2, 674.9, 654.27 +08/10/2015, 633.73, 1653836, 639.48, 643.44, 631.249 +08/07/2015, 635.3, 1403441, 640.23, 642.68, 629.71 +08/06/2015, 642.68, 1572150, 645, 645.379, 632.25 +08/05/2015, 643.78, 2331720, 634.33, 647.86, 633.16 +08/04/2015, 629.25, 1486858, 628.42, 634.81, 627.16 +08/03/2015, 631.21, 1301439, 625.34, 633.0556, 625.34 +07/31/2015, 625.61, 1705286, 631.38, 632.91, 625.5 +07/30/2015, 632.59, 1472286, 630, 635.22, 622.05 +07/29/2015, 631.93, 1573146, 628.8, 633.36, 622.65 +07/28/2015, 628, 1713684, 632.83, 632.83, 623.31 +07/27/2015, 627.26, 2673801, 621, 634.3, 620.5 +07/24/2015, 623.56, 3622089, 647, 648.17, 622.52 +07/23/2015, 644.28, 3014035, 661.27, 663.63, 641 +07/22/2015, 662.1, 3707818, 660.89, 678.64, 659 +07/21/2015, 662.3, 3363342, 655.21, 673, 654.3 +07/20/2015, 663.02, 5857092, 659.24, 668.88, 653.01 +07/17/2015, 672.93, 11153500, 649, 674.468, 645 +07/16/2015, 579.85, 4559712, 565.12, 580.68, 565 +07/15/2015, 560.22, 1782264, 560.13, 566.5029, 556.79 +07/14/2015, 561.1, 3231284, 546.76, 565.8487, 546.71 +07/13/2015, 546.55, 2204610, 532.88, 547.11, 532.4001 +07/10/2015, 530.13, 1954951, 526.29, 532.56, 525.55 +07/09/2015, 520.68, 1840155, 523.12, 523.77, 520.35 +07/08/2015, 516.83, 1293372, 521.05, 522.734, 516.11 +07/07/2015, 525.02, 1595672, 523.13, 526.18, 515.18 +07/06/2015, 522.86, 1278587, 519.5, 525.25, 519 +07/02/2015, 523.4, 1235773, 521.08, 524.65, 521.08 +07/01/2015, 521.84, 1961197, 524.73, 525.69, 518.2305 +06/30/2015, 520.51, 2234284, 526.02, 526.25, 520.5 +06/29/2015, 521.52, 1935361, 525.01, 528.61, 520.54 +06/26/2015, 531.69, 2108629, 537.26, 537.76, 531.35 +06/25/2015, 535.23, 1332412, 538.87, 540.9, 535.23 +06/24/2015, 537.84, 1286576, 540, 540, 535.66 +06/23/2015, 540.48, 1196115, 539.64, 541.499, 535.25 +06/22/2015, 538.19, 1243535, 539.59, 543.74, 537.53 +06/19/2015, 536.69, 1890916, 537.21, 538.25, 533.01 +06/18/2015, 536.73, 1832450, 531, 538.15, 530.79 +06/17/2015, 529.26, 1269113, 529.37, 530.98, 525.1 +06/16/2015, 528.15, 1071728, 528.4, 529.6399, 525.56 +06/15/2015, 527.2, 1632675, 528, 528.3, 524 +06/12/2015, 532.33, 955489, 531.6, 533.12, 530.16 +06/11/2015, 534.61, 1208632, 538.425, 538.98, 533.02 +06/10/2015, 536.69, 1813775, 529.36, 538.36, 529.35 +06/09/2015, 526.69, 1454172, 527.56, 529.2, 523.01 +06/08/2015, 526.83, 1523960, 533.31, 534.12, 526.24 +06/05/2015, 533.33, 1375008, 536.35, 537.2, 532.52 +06/04/2015, 536.7, 1346044, 537.76, 540.59, 534.32 +06/03/2015, 540.31, 1716836, 539.91, 543.5, 537.11 +06/02/2015, 539.18, 1936721, 532.93, 543, 531.33 +06/01/2015, 533.99, 1900257, 536.79, 536.79, 529.76 +05/29/2015, 532.11, 2590445, 537.37, 538.63, 531.45 +05/28/2015, 539.78, 1029764, 538.01, 540.61, 536.25 +05/27/2015, 539.79, 1524783, 532.8, 540.55, 531.71 +05/26/2015, 532.32, 2404462, 538.12, 539, 529.88 +05/22/2015, 540.11, 1175065, 540.15, 544.19, 539.51 +05/21/2015, 542.51, 1461431, 537.95, 543.8399, 535.98 +05/20/2015, 539.27, 1430565, 538.49, 542.92, 532.972 +05/19/2015, 537.36, 1964037, 533.98, 540.66, 533.04 +05/18/2015, 532.3, 2001117, 532.01, 534.82, 528.85 +05/15/2015, 533.85, 1965088, 539.18, 539.2743, 530.38 +05/14/2015, 538.4, 1401005, 533.77, 539, 532.41 +05/13/2015, 529.62, 1253005, 530.56, 534.3215, 528.655 +05/12/2015, 529.04, 1633180, 531.6, 533.2089, 525.26 +05/11/2015, 535.7, 904465, 538.37, 541.98, 535.4 +05/08/2015, 538.22, 1527181, 536.65, 541.15, 536 +05/07/2015, 530.7, 1543986, 523.99, 533.46, 521.75 +05/06/2015, 524.22, 1566865, 531.24, 532.38, 521.085 +05/05/2015, 530.8, 1380519, 538.21, 539.74, 530.3906 +05/04/2015, 540.78, 1303830, 538.53, 544.07, 535.06 +05/01/2015, 537.9, 1758085, 538.43, 539.54, 532.1 +04/30/2015, 537.34, 2080834, 547.87, 548.59, 535.05 +04/29/2015, 549.08, 1696886, 550.47, 553.68, 546.905 +04/28/2015, 553.68, 1490735, 554.64, 556.02, 550.366 +04/27/2015, 555.37, 2390696, 563.39, 565.95, 553.2001 diff --git a/machine_learning/multilayer_perceptron_classifier.py b/machine_learning/multilayer_perceptron_classifier.py new file mode 100644 index 000000000000..604185cef677 --- /dev/null +++ b/machine_learning/multilayer_perceptron_classifier.py @@ -0,0 +1,29 @@ +from sklearn.neural_network import MLPClassifier + +X = [[0.0, 0.0], [1.0, 1.0], [1.0, 0.0], [0.0, 1.0]] +y = [0, 1, 0, 0] + + +clf = MLPClassifier( + solver="lbfgs", alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1 +) + +clf.fit(X, y) + + +test = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0]] +Y = clf.predict(test) + + +def wrapper(Y): + """ + >>> wrapper(Y) + [0, 0, 1] + """ + return list(Y) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/machine_learning/perceptron.py b/machine_learning/perceptron.py deleted file mode 100644 index fe1032aff4af..000000000000 --- a/machine_learning/perceptron.py +++ /dev/null @@ -1,124 +0,0 @@ -''' - - Perceptron - w = w + N * (d(k) - y) * x(k) - - Using perceptron network for oil analysis, - with Measuring of 3 parameters that represent chemical characteristics we can classify the oil, in p1 or p2 - p1 = -1 - p2 = 1 - -''' -from __future__ import print_function - -import random - - -class Perceptron: - def __init__(self, sample, exit, learn_rate=0.01, epoch_number=1000, bias=-1): - self.sample = sample - self.exit = exit - self.learn_rate = learn_rate - self.epoch_number = epoch_number - self.bias = bias - self.number_sample = len(sample) - self.col_sample = len(sample[0]) - self.weight = [] - - def trannig(self): - for sample in self.sample: - sample.insert(0, self.bias) - - for i in range(self.col_sample): - self.weight.append(random.random()) - - self.weight.insert(0, self.bias) - - epoch_count = 0 - - while True: - erro = False - for i in range(self.number_sample): - u = 0 - for j in range(self.col_sample + 1): - u = u + self.weight[j] * self.sample[i][j] - y = self.sign(u) - if y != self.exit[i]: - - for j in range(self.col_sample + 1): - - self.weight[j] = self.weight[j] + self.learn_rate * (self.exit[i] - y) * self.sample[i][j] - erro = True - #print('Epoch: \n',epoch_count) - epoch_count = epoch_count + 1 - # if you want controle the epoch or just by erro - if erro == False: - print(('\nEpoch:\n',epoch_count)) - print('------------------------\n') - #if epoch_count > self.epoch_number or not erro: - break - - def sort(self, sample): - sample.insert(0, self.bias) - u = 0 - for i in range(self.col_sample + 1): - u = u + self.weight[i] * sample[i] - - y = self.sign(u) - - if y == -1: - print(('Sample: ', sample)) - print('classification: P1') - else: - print(('Sample: ', sample)) - print('classification: P2') - - def sign(self, u): - return 1 if u >= 0 else -1 - - -samples = [ - [-0.6508, 0.1097, 4.0009], - [-1.4492, 0.8896, 4.4005], - [2.0850, 0.6876, 12.0710], - [0.2626, 1.1476, 7.7985], - [0.6418, 1.0234, 7.0427], - [0.2569, 0.6730, 8.3265], - [1.1155, 0.6043, 7.4446], - [0.0914, 0.3399, 7.0677], - [0.0121, 0.5256, 4.6316], - [-0.0429, 0.4660, 5.4323], - [0.4340, 0.6870, 8.2287], - [0.2735, 1.0287, 7.1934], - [0.4839, 0.4851, 7.4850], - [0.4089, -0.1267, 5.5019], - [1.4391, 0.1614, 8.5843], - [-0.9115, -0.1973, 2.1962], - [0.3654, 1.0475, 7.4858], - [0.2144, 0.7515, 7.1699], - [0.2013, 1.0014, 6.5489], - [0.6483, 0.2183, 5.8991], - [-0.1147, 0.2242, 7.2435], - [-0.7970, 0.8795, 3.8762], - [-1.0625, 0.6366, 2.4707], - [0.5307, 0.1285, 5.6883], - [-1.2200, 0.7777, 1.7252], - [0.3957, 0.1076, 5.6623], - [-0.1013, 0.5989, 7.1812], - [2.4482, 0.9455, 11.2095], - [2.0149, 0.6192, 10.9263], - [0.2012, 0.2611, 5.4631] - -] - -exit = [-1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1] - -network = Perceptron(sample=samples, exit = exit, learn_rate=0.01, epoch_number=1000, bias=-1) - -network.trannig() - -while True: - sample = [] - for i in range(3): - sample.insert(i, float(input('value: '))) - network.sort(sample) diff --git a/machine_learning/polymonial_regression.py b/machine_learning/polymonial_regression.py new file mode 100644 index 000000000000..374c35f7f905 --- /dev/null +++ b/machine_learning/polymonial_regression.py @@ -0,0 +1,45 @@ +import pandas as pd +from matplotlib import pyplot as plt +from sklearn.linear_model import LinearRegression + +# Splitting the dataset into the Training set and Test set +from sklearn.model_selection import train_test_split + +# Fitting Polynomial Regression to the dataset +from sklearn.preprocessing import PolynomialFeatures + +# Importing the dataset +dataset = pd.read_csv( + "https://s3.us-west-2.amazonaws.com/public.gamelab.fun/dataset/" + "position_salaries.csv" +) +X = dataset.iloc[:, 1:2].values +y = dataset.iloc[:, 2].values + + +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) + + +poly_reg = PolynomialFeatures(degree=4) +X_poly = poly_reg.fit_transform(X) +pol_reg = LinearRegression() +pol_reg.fit(X_poly, y) + + +# Visualizing the Polymonial Regression results +def viz_polymonial(): + plt.scatter(X, y, color="red") + plt.plot(X, pol_reg.predict(poly_reg.fit_transform(X)), color="blue") + plt.title("Truth or Bluff (Linear Regression)") + plt.xlabel("Position level") + plt.ylabel("Salary") + plt.show() + return + + +if __name__ == "__main__": + viz_polymonial() + + # Predicting a new result with Polymonial Regression + pol_reg.predict(poly_reg.fit_transform([[5.5]])) + # output should be 132148.43750003 diff --git a/machine_learning/random_forest_classifier.py b/machine_learning/random_forest_classifier.py new file mode 100644 index 000000000000..6370254090f7 --- /dev/null +++ b/machine_learning/random_forest_classifier.py @@ -0,0 +1,44 @@ +# Random Forest Classifier Example +from matplotlib import pyplot as plt +from sklearn.datasets import load_iris +from sklearn.ensemble import RandomForestClassifier +from sklearn.metrics import plot_confusion_matrix +from sklearn.model_selection import train_test_split + + +def main(): + + """ + Random Forest Classifier Example using sklearn function. + Iris type dataset is used to demonstrate algorithm. + """ + + # Load Iris dataset + iris = load_iris() + + # Split dataset into train and test data + X = iris["data"] # features + Y = iris["target"] + x_train, x_test, y_train, y_test = train_test_split( + X, Y, test_size=0.3, random_state=1 + ) + + # Random Forest Classifier + rand_for = RandomForestClassifier(random_state=42, n_estimators=100) + rand_for.fit(x_train, y_train) + + # Display Confusion Matrix of Classifier + plot_confusion_matrix( + rand_for, + x_test, + y_test, + display_labels=iris["target_names"], + cmap="Blues", + normalize="true", + ) + plt.title("Normalized Confusion Matrix - IRIS Dataset") + plt.show() + + +if __name__ == "__main__": + main() diff --git a/machine_learning/random_forest_regressor.py b/machine_learning/random_forest_regressor.py new file mode 100644 index 000000000000..0aade626b038 --- /dev/null +++ b/machine_learning/random_forest_regressor.py @@ -0,0 +1,40 @@ +# Random Forest Regressor Example +from sklearn.datasets import load_boston +from sklearn.ensemble import RandomForestRegressor +from sklearn.metrics import mean_absolute_error, mean_squared_error +from sklearn.model_selection import train_test_split + + +def main(): + + """ + Random Forest Regressor Example using sklearn function. + Boston house price dataset is used to demonstrate the algorithm. + """ + + # Load Boston house price dataset + boston = load_boston() + print(boston.keys()) + + # Split dataset into train and test data + X = boston["data"] # features + Y = boston["target"] + x_train, x_test, y_train, y_test = train_test_split( + X, Y, test_size=0.3, random_state=1 + ) + + # Random Forest Regressor + rand_for = RandomForestRegressor(random_state=42, n_estimators=300) + rand_for.fit(x_train, y_train) + + # Predict target for test data + predictions = rand_for.predict(x_test) + predictions = predictions.reshape(len(predictions), 1) + + # Error printing + print(f"Mean Absolute Error:\t {mean_absolute_error(y_test, predictions)}") + print(f"Mean Square Error :\t {mean_squared_error(y_test, predictions)}") + + +if __name__ == "__main__": + main() diff --git a/machine_learning/reuters_one_vs_rest_classifier.ipynb b/machine_learning/reuters_one_vs_rest_classifier.ipynb deleted file mode 100644 index 968130a6053a..000000000000 --- a/machine_learning/reuters_one_vs_rest_classifier.ipynb +++ /dev/null @@ -1,405 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " import nltk\n", - "except ModuleNotFoundError:\n", - " !pip install nltk" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "## This code downloads the required packages.\n", - "## You can run `nltk.download('all')` to download everything.\n", - "\n", - "nltk_packages = [\n", - " (\"reuters\", \"corpora/reuters.zip\")\n", - "]\n", - "\n", - "for pid, fid in nltk_packages:\n", - " try:\n", - " nltk.data.find(fid)\n", - " except LookupError:\n", - " nltk.download(pid)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Setting up corpus" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "from nltk.corpus import reuters" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Setting up train/test data" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "train_documents, train_categories = zip(*[(reuters.raw(i), reuters.categories(i)) for i in reuters.fileids() if i.startswith('training/')])\n", - "test_documents, test_categories = zip(*[(reuters.raw(i), reuters.categories(i)) for i in reuters.fileids() if i.startswith('test/')])" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "all_categories = sorted(list(set(reuters.categories())))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The following cell defines a function **tokenize** that performs following actions:\n", - "- Receive a document as an argument to the function\n", - "- Tokenize the document using `nltk.word_tokenize()`\n", - "- Use `PorterStemmer` provided by the `nltk` to remove morphological affixes from each token\n", - "- Append stemmed token to an already defined list `stems`\n", - "- Return the list `stems`" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "from nltk.stem.porter import PorterStemmer\n", - "def tokenize(text):\n", - " tokens = nltk.word_tokenize(text)\n", - " stems = []\n", - " for item in tokens:\n", - " stems.append(PorterStemmer().stem(item))\n", - " return stems" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To begin, I first used TF-IDF for feature selection on both train as well as test data using `TfidfVectorizer`.\n", - "\n", - "But first, What `TfidfVectorizer` actually does?\n", - "- `TfidfVectorizer` converts a collection of raw documents to a matrix of **TF-IDF** features.\n", - "\n", - "**TF-IDF**?\n", - "- TFIDF (abbreviation of the term *frequency–inverse document frequency*) is a numerical statistic that is intended to reflect how important a word is to a document in a collection or corpus. [tf–idf](https://en.wikipedia.org/wiki/Tf%E2%80%93idf)\n", - "\n", - "**Why `TfidfVectorizer`**?\n", - "- `TfidfVectorizer` scale down the impact of tokens that occur very frequently (e.g., “a”, “the”, and “of”) in a given corpus. [Feature Extraction and Transformation](https://spark.apache.org/docs/latest/mllib-feature-extraction.html#tf-idf)\n", - "\n", - "I gave following two arguments to `TfidfVectorizer`:\n", - "- tokenizer: `tokenize` function\n", - "- stop_words\n", - "\n", - "Then I used `fit_transform` and `transform` on the train and test documents repectively.\n", - "\n", - "**Why `fit_transform` for training data while `transform` for test data**?\n", - "\n", - "To avoid data leakage during cross-validation, imputer computes the statistic on the train data during the `fit`, **stores it** and uses the same on the test data, during the `transform`. This also prevents the test data from appearing in `fit` operation." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "from sklearn.feature_extraction.text import TfidfVectorizer\n", - "\n", - "vectorizer = TfidfVectorizer(tokenizer = tokenize, stop_words = 'english')\n", - "\n", - "vectorised_train_documents = vectorizer.fit_transform(train_documents)\n", - "vectorised_test_documents = vectorizer.transform(test_documents)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For the **efficient implementation** of machine learning algorithms, many machine learning algorithms **requires all input variables and output variables to be numeric**. This means that categorical data must be converted to a numerical form.\n", - "\n", - "For this purpose, I used `MultiLabelBinarizer` from `sklearn.preprocessing`." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "from sklearn.preprocessing import MultiLabelBinarizer\n", - "\n", - "mlb = MultiLabelBinarizer()\n", - "train_labels = mlb.fit_transform(train_categories)\n", - "test_labels = mlb.transform(test_categories)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now, To **train** the classifier, I used `LinearSVC` in combination with the `OneVsRestClassifier` function in the scikit-learn package.\n", - "\n", - "The strategy of `OneVsRestClassifier` is of **fitting one classifier per label** and the `OneVsRestClassifier` can efficiently do this task and also outputs are easy to interpret. Since each label is represented by **one and only one classifier**, it is possible to gain knowledge about the label by inspecting its corresponding classifier. [OneVsRestClassifier](http://scikit-learn.org/stable/modules/multiclass.html#one-vs-the-rest)\n", - "\n", - "The reason I combined `LinearSVC` with `OneVsRestClassifier` is because `LinearSVC` supports **Multi-class**, while we want to perform **Multi-label** classification." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "%%capture\n", - "from sklearn.multiclass import OneVsRestClassifier\n", - "from sklearn.svm import LinearSVC\n", - "\n", - "classifier = OneVsRestClassifier(LinearSVC())\n", - "classifier.fit(vectorised_train_documents, train_labels)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "After fitting the classifier, I decided to use `cross_val_score` to **measure score** of the classifier by **cross validation** on the training data. But the only problem was, I wanted to **shuffle** data to use with `cross_val_score`, but it does not support shuffle argument.\n", - "\n", - "So, I decided to use `KFold` with `cross_val_score` as `KFold` supports shuffling the data.\n", - "\n", - "I also enabled `random_state`, because `random_state` will guarantee the same output in each run. By setting the `random_state`, it is guaranteed that the pseudorandom number generator will generate the same sequence of random integers each time, which in turn will affect the split.\n", - "\n", - "Why **42**?\n", - "- [Why '42' is the preferred number when indicating something random?](https://softwareengineering.stackexchange.com/questions/507/why-42-is-the-preferred-number-when-indicating-something-random)" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "%%capture\n", - "from sklearn.model_selection import KFold, cross_val_score\n", - "\n", - "kf = KFold(n_splits=10, random_state = 42, shuffle = True)\n", - "scores = cross_val_score(classifier, vectorised_train_documents, train_labels, cv = kf)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Cross-validation scores: [0.83655084 0.86743887 0.8043758 0.83011583 0.83655084 0.81724582\n", - " 0.82754183 0.8030888 0.80694981 0.82731959]\n", - "Cross-validation accuracy: 0.8257 (+/- 0.0368)\n" - ] - } - ], - "source": [ - "print('Cross-validation scores:', scores)\n", - "print('Cross-validation accuracy: {:.4f} (+/- {:.4f})'.format(scores.mean(), scores.std() * 2))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the end, I used different methods (`accuracy_score`, `precision_score`, `recall_score`, `f1_score` and `confusion_matrix`) provided by scikit-learn **to evaluate** the classifier. (both *Macro-* and *Micro-averages*)" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "%%capture\n", - "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\n", - "\n", - "predictions = classifier.predict(vectorised_test_documents)\n", - "\n", - "accuracy = accuracy_score(test_labels, predictions)\n", - "\n", - "macro_precision = precision_score(test_labels, predictions, average='macro')\n", - "macro_recall = recall_score(test_labels, predictions, average='macro')\n", - "macro_f1 = f1_score(test_labels, predictions, average='macro')\n", - "\n", - "micro_precision = precision_score(test_labels, predictions, average='micro')\n", - "micro_recall = recall_score(test_labels, predictions, average='micro')\n", - "micro_f1 = f1_score(test_labels, predictions, average='micro')\n", - "\n", - "cm = confusion_matrix(test_labels.argmax(axis = 1), predictions.argmax(axis = 1))" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Accuracy: 0.8099\n", - "Precision:\n", - "- Macro: 0.6076\n", - "- Micro: 0.9471\n", - "Recall:\n", - "- Macro: 0.3708\n", - "- Micro: 0.7981\n", - "F1-measure:\n", - "- Macro: 0.4410\n", - "- Micro: 0.8662\n" - ] - } - ], - "source": [ - "print(\"Accuracy: {:.4f}\\nPrecision:\\n- Macro: {:.4f}\\n- Micro: {:.4f}\\nRecall:\\n- Macro: {:.4f}\\n- Micro: {:.4f}\\nF1-measure:\\n- Macro: {:.4f}\\n- Micro: {:.4f}\".format(accuracy, macro_precision, micro_precision, macro_recall, micro_recall, macro_f1, micro_f1))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In below cell, I used `matplotlib.pyplot` to **plot the confusion matrix** (of first *few results only* to keep the readings readable) using `heatmap` of `seaborn`." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAABSUAAAV0CAYAAAAhI3i0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzs3Xl8lOW5//HvPUlYVRRRIQkVW1xarYUWUKsiFQvUqnSlP0+1ttXD6XGptlW7aGu1p9upnurpplgFl8qiPXUFi2AtUBGIEiAQQBCKCRFXVHAhJPfvjxnoCDPPMpPMM3fuz/v1mhfJJN9c1/XMTeaZJ8/MGGutAAAAAAAAAKBUUkk3AAAAAAAAAMAvHJQEAAAAAAAAUFIclAQAAAAAAABQUhyUBAAAAAAAAFBSHJQEAAAAAAAAUFIclAQAAAAAAABQUokdlDTGjDPGrDHGrDPGfC9m9nZjzIvGmIYC6g40xvzNGNNojFlpjLk0RraHMWaxMWZZJnttAfUrjDFLjTEPF5DdaIxZYYypN8bUxczub4y5zxizOjP7CRFzR2bq7bq8YYy5LGbtb2W2V4MxZqoxpkeM7KWZ3MoodXOtDW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dInrbXXShoj6cv5QsaYicaYOmNMXXv79g5oc6+fv9d1UZ+G7mI2ydr0XdrazBwvm2RtZo6XTbI2M8fLJlmbmeNlk6zNzPGySdZm5njZJGszc7xskrWZOV42ydo+zgy4pDLk6yljzAFKH7w01tqXJMlau90YszNfyFo7SdIkKf9rShajualFA2urd39eWzNALS1bumw2ydr0XdrazOxH3z7OnGRtZvajbx9nTrI2M/vRt48zJ1mbmf3o28eZk6zt48yAS8LOlOwj6WlJdZL6GmP6S5IxZh9Jib1Q5pK6eg0efJgGDRqoqqoqTZgwXg89PLvLZumbvjs7S9/uZOnbnSx9u5Olb3ey9O1Olr7dydK3O1n6diebdG3AFYFnSlprB+X5UrukzxZT+O67fqdTRp6gfv36auNzdbr2uus1ecq0SNm2tjZdetnVmvnIPapIpTTljulatWptl83SN313dpa+3cnStztZ+nYnS9/uZOnbnSx9u5Olb3ey9O1ONunagCtMZ78uQWc8fRsAAAAAAMBnO3c0J/YM1nL2zjMPchwqS4+PnlW26yTs6dsAAAAAAAAA0KE4KAkAAAAAAACgpDgoCQAAAAAAAKCkOCgJAAAAAAAAoKQC330bbqlIFXeMua29vYM6AQAAAAAAAPLjoCQAAAAAAAC6BssJV67g6dsAAAAAAAAASoqDkgAAAAAAAABKioOSAAAAAAAAAEoqsYOSt066QZublql+6dyC8mPHjNLKhnlavWqBrrzioi6fjZu/5Zbr9fympXrm6Tnvuf7C//yqVix/QkufmaOf/fQHZdd3uWSTrM3MfvTt48xJ1mZmP/r2ceYkazOzH337OHOStZnZj759nDnJ2j7ODLjCWGs7tUBlt5qcBU4+6Tht27ZdkyffpCFDR8f6malUSo0r52vc6WerqalFTy2cqXPOvVCNjc92yWzUfPa7b5+U2b6333ajPvqx0yRJp5xygr733Us0/jNf1Y4dO3TQQQfqpZde2Z3J9e7bpei73LKu9u3jzK727ePMrvbt48yu9u3jzK727ePMrvbt48yu9u3jzK727ePMrvbtwsw7dzSbSM145p0lf+7cA12O6TH882W7ThI7U3L+gkV69bWt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J3F93IcKkuPEV8s23USdqakJMla+6akZZ3cCwAAAAAAAAAPFHdqHQAAAAAAAADExEFJAAAAAAAAACUV6enbAAAAAAAAQNnj/TKcwZmSAAAAAAAAAEqKMyW7EN49GwAAAAAAAC7gTEkAAAAAAAAAJcVBSQAAAAAAAAAlxdO3AQAAAAAA0DVYXtrOFZwpCQAAAAAAAKCkEjsoeeukG7S5aZnql84tKD92zCitbJin1asW6MorLury2SRr+3hbJVmbmf3o28eZk6zNzH707ePMSdZmZj/69nHmJGszsx99+zhzkrV9nBlwhbHWdmqBym41OQucfNJx2rZtuyZPvklDho6O9TNTqZQaV87XuNPPVlNTi55aOFPnnHuhGhuf7ZLZpGv7dlu52rePM7vat48zu9q3jzO72rePM7vat48zu9q3jzO72rePM7vat48zu9q3CzPv3NFsIjXjmXcWTu3cA12O6XHC2WW7TgLPlDTGfNMYM7AzCs9fsEivvra1oOyI4UO1fv1GbdiwSa2trZox4wGddebYLptNurZvt5Wrffs4s6t9+zizq337OLOrffs4s6t9+zizq337OLOrffs4s6t9+zizq327OjPgkrCnb/9E0iJjzHxjzIXGmINK0VSY6pr+er5p8+7Pm5pbVF3dv8tmk65dDLa3G9kka/vYt48zJ1mbmf3o28eZk6zNzH707ePMSdZmZj/69nHmJGv7ODMktbdzyb6UsbCDks9JqlX64OTHJK0yxjxqjDnPGLNvvpAxZqIxps4YU9fevr0D29398/e6LurT0F3MJl27GGxvN7JJ1vaxbx9nTrI2M8fLJlmbmeNlk6zNzPGySdZm5njZJGszc7xskrWZOV42ydo+zgy4JOygpLXWtltrZ1trz5dULen3ksYpfcAyX2iStXaYtXZYKtW7A9tNa25q0cDa6t2f19YMUEvLli6bTbp2MdjebmSTrO1j3z7OnGRtZvajbx9nTrI2M/vRt48zJ1mbmf3o28eZk6zt48yAS8IOSr7n8Ly1ttVa+6C19mxJ7+u8toItqavX4MGHadCggaqqqtKECeP10MOzu2w26drFYHu7kaVvd7L07U6Wvt3J0rc7Wfp2J0vf7mTp250sfbu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j6D3E1nP3L72DnI3XT2I7ePnYPcTWc/cvvYOcjdPnYGXGGsTe3TWjt1+lzTgqFDj1ZNzUd68MG7dNRR32zxc++44wb95z+7dNtt90hq/erboVBIleuWa9Tp5ykcrtaKlxdp7LgJqqx8M2YeF2fJTe50dM7P76U++b20qnytunXrqpUli3XOuT/MyNydOoUUiew9SVhV/UmL+e+P/bE2bdysrl1z1dBgtX39C3F9DySpqnqHrv/NFM344+8k/ffq2wcflKsOHXL07nufqFvXXB3ULU9Llz6n0844T1u37t17wYWXqHZXWJu3fJSy71ey8/y6onO25vaxs6u5fezsam4fO7ua28fOrub2sbOruV3oXL+nysQVxjO1t3yf12830/VXf83Yx0lanylZXLxSO3d+0Or95557pubMmRfz6xw9ZJA2bNisTZu2qK6uTnPnztPZZ42MK4OLs+Qmd6pnJWn79re1qnytJKmmplbr17+pwoL8jMz98ccRRRrfJ+RA86PPPl2f7IkO+TCkAAAgAElEQVToQP/mMv8fL+h7P7pC51x4iSb97l41NBz4Hz/217VLrnbV1EmSamrrdeyxR2rDhs3auLF57hGK508/Hx+frub2sbOruX3s7GpuHzu7mtvHzq7m9rGzq7l97Oxqblc7Ay7JmPeUHDr0aO3Y8a42bNgc8+cWFOZra3hb0+fhqmoVxHnyxMXZIHeTO727g+zcXL9+RRr49f9RycpVKd+dzmO1YfMWLX7+n5p1/xQ9OfM+hUIhLVjyYlx7cnON6uv/e8qxV6/eClft3duxY0i7P3pPh3+xr95995N2zdze864cq2yYDXK3j7l97Bzkbjr7kdvHzkHuprMfuX3sHORuHztDUiTCR/OPDJYxV98eM2a05s6N/SxJSTKm5TNP430ZuouzQe4md3p3B9l5n65du2junGm6+pqbtGtXTcp3p/NYlZSVq2L9W/rexVdIkj755BP1+MwhkqTLf3GLqrbtUF19nap3vKNzLtz7vi0HdcvVrpr6qBk++SSi93buUU1NnQ45pIM+2r37gM/SbEvm9p535Vhlw2yQu33M7WPnIHfTObHZIHfTObHZIHfTObHZIHfTObHZIHf72BlwSdSTks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Hj1rKdJTe5iz1L7nhmyR3PLLnjmSV3PLPkjmeW3PHMkjueWXLHM9sX80AKrLvXl5vZv0ta5u6PvOnnF0r6T3d/W64FlQNq+/cL2AEAAAAAACJz+NWWY9+4EspyDvUGbf3470muZ0o2S9ry5h+6+0OSch5IAgAAAAAAAKXCiWQ8cr2n5FclrTKzx8zs02Z2cilCAQAAAAAAACg+M7vQzLaY2TYz+3yp9uY6lNwhqU6vHU6+S9JGM3vIzD5hZkOKng4AAAAAAABAUZhZhaTvSvqQpHdI+isze0cpduc6lHR373D3Ze7+KUk1km6VdKFeO7AEAAAAAAAAEKezJW1z9x3u/qqk+ZIuKcXiXO8p+YY3w3T3Nkk/lfRTMxtYtFQAAAAAAAAAiq1W0p6jvm+W9O5SLM51KHnp8W5w99/ns4BPgwIAAAAAAEApcA71RmZ2paQrj/rRHHefc/RduhgryecFdXso6e5bSxECAAAAAAAAQN/qPICc081dmiWNOur7Okl7ixqqU673lAQAAAAAAABQntZIepuZjTOzAZL+Uq+9dWPR5Xr5NgAAAAAAAIAy5O6Hzewzkn4mqULSXHffUIrd5l6Sl4kDAAAAAAAAgCRevg0AAAAAAACgxDiUBAAAAAAAAFBSHEoCAAAAAAAAKCkOJQEAAAAAAACUFIeSAAAAAAAAAEqKQ0kAAAAAAAAAJcWhJAAAAAAAAICS4lASAAAAAAAAQEn9fymVlo281J4BAAAAAElFTkSuQmCC\n", - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import matplotlib.pyplot as plt\n", - "import seaborn as sb\n", - "import pandas as pd\n", - "\n", - "cm_plt = pd.DataFrame(cm[:73])\n", - "\n", - "plt.figure(figsize = (25, 25))\n", - "ax = plt.axes()\n", - "\n", - "sb.heatmap(cm_plt, annot=True)\n", - "\n", - "ax.xaxis.set_ticks_position('top')\n", - "\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Pipeline" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now, I took the data from [Coconut - Wikipedia](https://en.wikipedia.org/wiki/Coconut) to check if the classifier is able to **correctly** predict the label(s) or not.\n", - "\n", - "And here is the output:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Example labels: [('coconut', 'oilseed')]\n" - ] - } - ], - "source": [ - "example_text = '''The coconut tree (Cocos nucifera) is a member of the family Arecaceae (palm family) and the only species of the genus Cocos.\n", - "The term coconut can refer to the whole coconut palm or the seed, or the fruit, which, botanically, is a drupe, not a nut.\n", - "The spelling cocoanut is an archaic form of the word.\n", - "The term is derived from the 16th-century Portuguese and Spanish word coco meaning \"head\" or \"skull\", from the three indentations on the coconut shell that resemble facial features.\n", - "Coconuts are known for their versatility ranging from food to cosmetics.\n", - "They form a regular part of the diets of many people in the tropics and subtropics.\n", - "Coconuts are distinct from other fruits for their endosperm containing a large quantity of water (also called \"milk\"), and when immature, may be harvested for the potable coconut water.\n", - "When mature, they can be used as seed nuts or processed for oil, charcoal from the hard shell, and coir from the fibrous husk.\n", - "When dried, the coconut flesh is called copra.\n", - "The oil and milk derived from it are commonly used in cooking and frying, as well as in soaps and cosmetics.\n", - "The husks and leaves can be used as material to make a variety of products for furnishing and decorating.\n", - "The coconut also has cultural and religious significance in certain societies, particularly in India, where it is used in Hindu rituals.'''\n", - "\n", - "example_preds = classifier.predict(vectorizer.transform([example_text]))\n", - "example_labels = mlb.inverse_transform(example_preds)\n", - "print(\"Example labels: {}\".format(example_labels))" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/machine_learning/scoring_functions.py b/machine_learning/scoring_functions.py old mode 100755 new mode 100644 index a2d97b09ded2..08b969a95c3b --- a/machine_learning/scoring_functions.py +++ b/machine_learning/scoring_functions.py @@ -10,12 +10,23 @@ even log is used. Using log and roots can be perceived as tools for penalizing big - erors. However, using appropriate metrics depends on the situations, + errors. However, using appropriate metrics depends on the situations, and types of data """ -#Mean Absolute Error + +# Mean Absolute Error def mae(predict, actual): + """ + Examples(rounded for precision): + >>> actual = [1,2,3];predict = [1,4,3] + >>> np.around(mae(predict,actual),decimals = 2) + 0.67 + + >>> actual = [1,1,1];predict = [1,1,1] + >>> mae(predict,actual) + 0.0 + """ predict = np.array(predict) actual = np.array(actual) @@ -24,8 +35,19 @@ def mae(predict, actual): return score -#Mean Squared Error + +# Mean Squared Error def mse(predict, actual): + """ + Examples(rounded for precision): + >>> actual = [1,2,3];predict = [1,4,3] + >>> np.around(mse(predict,actual),decimals = 2) + 1.33 + + >>> actual = [1,1,1];predict = [1,1,1] + >>> mse(predict,actual) + 0.0 + """ predict = np.array(predict) actual = np.array(actual) @@ -35,8 +57,19 @@ def mse(predict, actual): score = square_diff.mean() return score -#Root Mean Squared Error + +# Root Mean Squared Error def rmse(predict, actual): + """ + Examples(rounded for precision): + >>> actual = [1,2,3];predict = [1,4,3] + >>> np.around(rmse(predict,actual),decimals = 2) + 1.15 + + >>> actual = [1,1,1];predict = [1,1,1] + >>> rmse(predict,actual) + 0.0 + """ predict = np.array(predict) actual = np.array(actual) @@ -46,13 +79,24 @@ def rmse(predict, actual): score = np.sqrt(mean_square_diff) return score -#Root Mean Square Logarithmic Error + +# Root Mean Square Logarithmic Error def rmsle(predict, actual): + """ + Examples(rounded for precision): + >>> actual = [10,10,30];predict = [10,2,30] + >>> np.around(rmsle(predict,actual),decimals = 2) + 0.75 + + >>> actual = [1,1,1];predict = [1,1,1] + >>> rmsle(predict,actual) + 0.0 + """ predict = np.array(predict) actual = np.array(actual) - log_predict = np.log(predict+1) - log_actual = np.log(actual+1) + log_predict = np.log(predict + 1) + log_actual = np.log(actual + 1) difference = log_predict - log_actual square_diff = np.square(difference) @@ -62,17 +106,36 @@ def rmsle(predict, actual): return score -#Mean Bias Deviation + +# Mean Bias Deviation def mbd(predict, actual): + """ + This value is Negative, if the model underpredicts, + positive, if it overpredicts. + + Example(rounded for precision): + + Here the model overpredicts + >>> actual = [1,2,3];predict = [2,3,4] + >>> np.around(mbd(predict,actual),decimals = 2) + 50.0 + + Here the model underpredicts + >>> actual = [1,2,3];predict = [0,1,1] + >>> np.around(mbd(predict,actual),decimals = 2) + -66.67 + """ predict = np.array(predict) actual = np.array(actual) difference = predict - actual - numerator = np.sum(difference) / len(predict) - denumerator = np.sum(actual) / len(predict) - print(numerator) - print(denumerator) - + numerator = np.sum(difference) / len(predict) + denumerator = np.sum(actual) / len(predict) + # print(numerator, denumerator) score = float(numerator) / denumerator * 100 return score + + +def manual_accuracy(predict, actual): + return np.mean(np.array(actual) == np.array(predict)) diff --git a/machine_learning/sequential_minimum_optimization.py b/machine_learning/sequential_minimum_optimization.py new file mode 100644 index 000000000000..98ce05c46cff --- /dev/null +++ b/machine_learning/sequential_minimum_optimization.py @@ -0,0 +1,632 @@ +""" + Implementation of sequential minimal optimization (SMO) for support vector machines + (SVM). + + Sequential minimal optimization (SMO) is an algorithm for solving the quadratic + programming (QP) problem that arises during the training of support vector + machines. + It was invented by John Platt in 1998. + +Input: + 0: type: numpy.ndarray. + 1: first column of ndarray must be tags of samples, must be 1 or -1. + 2: rows of ndarray represent samples. + +Usage: + Command: + python3 sequential_minimum_optimization.py + Code: + from sequential_minimum_optimization import SmoSVM, Kernel + + kernel = Kernel(kernel='poly', degree=3., coef0=1., gamma=0.5) + init_alphas = np.zeros(train.shape[0]) + SVM = SmoSVM(train=train, alpha_list=init_alphas, kernel_func=kernel, cost=0.4, + b=0.0, tolerance=0.001) + SVM.fit() + predict = SVM.predict(test_samples) + +Reference: + https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/smo-book.pdf + https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-98-14.pdf + http://web.cs.iastate.edu/~honavar/smo-svm.pdf +""" + + +import os +import sys +import urllib.request + +import numpy as np +import pandas as pd +from matplotlib import pyplot as plt +from sklearn.datasets import make_blobs, make_circles +from sklearn.preprocessing import StandardScaler + +CANCER_DATASET_URL = ( + "http://archive.ics.uci.edu/ml/machine-learning-databases/" + "breast-cancer-wisconsin/wdbc.data" +) + + +class SmoSVM: + def __init__( + self, + train, + kernel_func, + alpha_list=None, + cost=0.4, + b=0.0, + tolerance=0.001, + auto_norm=True, + ): + self._init = True + self._auto_norm = auto_norm + self._c = np.float64(cost) + self._b = np.float64(b) + self._tol = np.float64(tolerance) if tolerance > 0.0001 else np.float64(0.001) + + self.tags = train[:, 0] + self.samples = self._norm(train[:, 1:]) if self._auto_norm else train[:, 1:] + self.alphas = alpha_list if alpha_list is not None else np.zeros(train.shape[0]) + self.Kernel = kernel_func + + self._eps = 0.001 + self._all_samples = list(range(self.length)) + self._K_matrix = self._calculate_k_matrix() + self._error = np.zeros(self.length) + self._unbound = [] + + self.choose_alpha = self._choose_alphas() + + # Calculate alphas using SMO algorithm + def fit(self): + K = self._k + state = None + while True: + + # 1: Find alpha1, alpha2 + try: + i1, i2 = self.choose_alpha.send(state) + state = None + except StopIteration: + print("Optimization done!\nEvery sample satisfy the KKT condition!") + break + + # 2: calculate new alpha2 and new alpha1 + y1, y2 = self.tags[i1], self.tags[i2] + a1, a2 = self.alphas[i1].copy(), self.alphas[i2].copy() + e1, e2 = self._e(i1), self._e(i2) + args = (i1, i2, a1, a2, e1, e2, y1, y2) + a1_new, a2_new = self._get_new_alpha(*args) + if not a1_new and not a2_new: + state = False + continue + self.alphas[i1], self.alphas[i2] = a1_new, a2_new + + # 3: update threshold(b) + b1_new = np.float64( + -e1 + - y1 * K(i1, i1) * (a1_new - a1) + - y2 * K(i2, i1) * (a2_new - a2) + + self._b + ) + b2_new = np.float64( + -e2 + - y2 * K(i2, i2) * (a2_new - a2) + - y1 * K(i1, i2) * (a1_new - a1) + + self._b + ) + if 0.0 < a1_new < self._c: + b = b1_new + if 0.0 < a2_new < self._c: + b = b2_new + if not (np.float64(0) < a2_new < self._c) and not ( + np.float64(0) < a1_new < self._c + ): + b = (b1_new + b2_new) / 2.0 + b_old = self._b + self._b = b + + # 4: update error value,here we only calculate those non-bound samples' + # error + self._unbound = [i for i in self._all_samples if self._is_unbound(i)] + for s in self.unbound: + if s == i1 or s == i2: + continue + self._error[s] += ( + y1 * (a1_new - a1) * K(i1, s) + + y2 * (a2_new - a2) * K(i2, s) + + (self._b - b_old) + ) + + # if i1 or i2 is non-bound,update there error value to zero + if self._is_unbound(i1): + self._error[i1] = 0 + if self._is_unbound(i2): + self._error[i2] = 0 + + # Predict test samles + def predict(self, test_samples, classify=True): + + if test_samples.shape[1] > self.samples.shape[1]: + raise ValueError( + "Test samples' feature length does not equal to that of train samples" + ) + + if self._auto_norm: + test_samples = self._norm(test_samples) + + results = [] + for test_sample in test_samples: + result = self._predict(test_sample) + if classify: + results.append(1 if result > 0 else -1) + else: + results.append(result) + return np.array(results) + + # Check if alpha violate KKT condition + def _check_obey_kkt(self, index): + alphas = self.alphas + tol = self._tol + r = self._e(index) * self.tags[index] + c = self._c + + return (r < -tol and alphas[index] < c) or (r > tol and alphas[index] > 0.0) + + # Get value calculated from kernel function + def _k(self, i1, i2): + # for test samples,use Kernel function + if isinstance(i2, np.ndarray): + return self.Kernel(self.samples[i1], i2) + # for train samples,Kernel values have been saved in matrix + else: + return self._K_matrix[i1, i2] + + # Get sample's error + def _e(self, index): + """ + Two cases: + 1:Sample[index] is non-bound,Fetch error from list: _error + 2:sample[index] is bound,Use predicted value deduct true value: g(xi) - yi + + """ + # get from error data + if self._is_unbound(index): + return self._error[index] + # get by g(xi) - yi + else: + gx = np.dot(self.alphas * self.tags, self._K_matrix[:, index]) + self._b + yi = self.tags[index] + return gx - yi + + # Calculate Kernel matrix of all possible i1,i2 ,saving time + def _calculate_k_matrix(self): + k_matrix = np.zeros([self.length, self.length]) + for i in self._all_samples: + for j in self._all_samples: + k_matrix[i, j] = np.float64( + self.Kernel(self.samples[i, :], self.samples[j, :]) + ) + return k_matrix + + # Predict test sample's tag + def _predict(self, sample): + k = self._k + predicted_value = ( + np.sum( + [ + self.alphas[i1] * self.tags[i1] * k(i1, sample) + for i1 in self._all_samples + ] + ) + + self._b + ) + return predicted_value + + # Choose alpha1 and alpha2 + def _choose_alphas(self): + locis = yield from self._choose_a1() + if not locis: + return + return locis + + def _choose_a1(self): + """ + Choose first alpha ;steps: + 1:First loop over all sample + 2:Second loop over all non-bound samples till all non-bound samples does not + voilate kkt condition. + 3:Repeat this two process endlessly,till all samples does not voilate kkt + condition samples after first loop. + """ + while True: + all_not_obey = True + # all sample + print("scanning all sample!") + for i1 in [i for i in self._all_samples if self._check_obey_kkt(i)]: + all_not_obey = False + yield from self._choose_a2(i1) + + # non-bound sample + print("scanning non-bound sample!") + while True: + not_obey = True + for i1 in [ + i + for i in self._all_samples + if self._check_obey_kkt(i) and self._is_unbound(i) + ]: + not_obey = False + yield from self._choose_a2(i1) + if not_obey: + print("all non-bound samples fit the KKT condition!") + break + if all_not_obey: + print("all samples fit the KKT condition! Optimization done!") + break + return False + + def _choose_a2(self, i1): + """ + Choose the second alpha by using heuristic algorithm ;steps: + 1: Choose alpha2 which gets the maximum step size (|E1 - E2|). + 2: Start in a random point,loop over all non-bound samples till alpha1 and + alpha2 are optimized. + 3: Start in a random point,loop over all samples till alpha1 and alpha2 are + optimized. + """ + self._unbound = [i for i in self._all_samples if self._is_unbound(i)] + + if len(self.unbound) > 0: + tmp_error = self._error.copy().tolist() + tmp_error_dict = { + index: value + for index, value in enumerate(tmp_error) + if self._is_unbound(index) + } + if self._e(i1) >= 0: + i2 = min(tmp_error_dict, key=lambda index: tmp_error_dict[index]) + else: + i2 = max(tmp_error_dict, key=lambda index: tmp_error_dict[index]) + cmd = yield i1, i2 + if cmd is None: + return + + for i2 in np.roll(self.unbound, np.random.choice(self.length)): + cmd = yield i1, i2 + if cmd is None: + return + + for i2 in np.roll(self._all_samples, np.random.choice(self.length)): + cmd = yield i1, i2 + if cmd is None: + return + + # Get the new alpha2 and new alpha1 + def _get_new_alpha(self, i1, i2, a1, a2, e1, e2, y1, y2): + K = self._k + if i1 == i2: + return None, None + + # calculate L and H which bound the new alpha2 + s = y1 * y2 + if s == -1: + L, H = max(0.0, a2 - a1), min(self._c, self._c + a2 - a1) + else: + L, H = max(0.0, a2 + a1 - self._c), min(self._c, a2 + a1) + if L == H: + return None, None + + # calculate eta + k11 = K(i1, i1) + k22 = K(i2, i2) + k12 = K(i1, i2) + eta = k11 + k22 - 2.0 * k12 + + # select the new alpha2 which could get the minimal objectives + if eta > 0.0: + a2_new_unc = a2 + (y2 * (e1 - e2)) / eta + # a2_new has a boundary + if a2_new_unc >= H: + a2_new = H + elif a2_new_unc <= L: + a2_new = L + else: + a2_new = a2_new_unc + else: + b = self._b + l1 = a1 + s * (a2 - L) + h1 = a1 + s * (a2 - H) + + # way 1 + f1 = y1 * (e1 + b) - a1 * K(i1, i1) - s * a2 * K(i1, i2) + f2 = y2 * (e2 + b) - a2 * K(i2, i2) - s * a1 * K(i1, i2) + ol = ( + l1 * f1 + + L * f2 + + 1 / 2 * l1 ** 2 * K(i1, i1) + + 1 / 2 * L ** 2 * K(i2, i2) + + s * L * l1 * K(i1, i2) + ) + oh = ( + h1 * f1 + + H * f2 + + 1 / 2 * h1 ** 2 * K(i1, i1) + + 1 / 2 * H ** 2 * K(i2, i2) + + s * H * h1 * K(i1, i2) + ) + """ + # way 2 + Use objective function check which alpha2 new could get the minimal + objectives + """ + if ol < (oh - self._eps): + a2_new = L + elif ol > oh + self._eps: + a2_new = H + else: + a2_new = a2 + + # a1_new has a boundary too + a1_new = a1 + s * (a2 - a2_new) + if a1_new < 0: + a2_new += s * a1_new + a1_new = 0 + if a1_new > self._c: + a2_new += s * (a1_new - self._c) + a1_new = self._c + + return a1_new, a2_new + + # Normalise data using min_max way + def _norm(self, data): + if self._init: + self._min = np.min(data, axis=0) + self._max = np.max(data, axis=0) + self._init = False + return (data - self._min) / (self._max - self._min) + else: + return (data - self._min) / (self._max - self._min) + + def _is_unbound(self, index): + if 0.0 < self.alphas[index] < self._c: + return True + else: + return False + + def _is_support(self, index): + if self.alphas[index] > 0: + return True + else: + return False + + @property + def unbound(self): + return self._unbound + + @property + def support(self): + return [i for i in range(self.length) if self._is_support(i)] + + @property + def length(self): + return self.samples.shape[0] + + +class Kernel: + def __init__(self, kernel, degree=1.0, coef0=0.0, gamma=1.0): + self.degree = np.float64(degree) + self.coef0 = np.float64(coef0) + self.gamma = np.float64(gamma) + self._kernel_name = kernel + self._kernel = self._get_kernel(kernel_name=kernel) + self._check() + + def _polynomial(self, v1, v2): + return (self.gamma * np.inner(v1, v2) + self.coef0) ** self.degree + + def _linear(self, v1, v2): + return np.inner(v1, v2) + self.coef0 + + def _rbf(self, v1, v2): + return np.exp(-1 * (self.gamma * np.linalg.norm(v1 - v2) ** 2)) + + def _check(self): + if self._kernel == self._rbf: + if self.gamma < 0: + raise ValueError("gamma value must greater than 0") + + def _get_kernel(self, kernel_name): + maps = {"linear": self._linear, "poly": self._polynomial, "rbf": self._rbf} + return maps[kernel_name] + + def __call__(self, v1, v2): + return self._kernel(v1, v2) + + def __repr__(self): + return self._kernel_name + + +def count_time(func): + def call_func(*args, **kwargs): + import time + + start_time = time.time() + func(*args, **kwargs) + end_time = time.time() + print(f"smo algorithm cost {end_time - start_time} seconds") + + return call_func + + +@count_time +def test_cancel_data(): + print("Hello!\nStart test svm by smo algorithm!") + # 0: download dataset and load into pandas' dataframe + if not os.path.exists(r"cancel_data.csv"): + request = urllib.request.Request( + CANCER_DATASET_URL, + headers={"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"}, + ) + response = urllib.request.urlopen(request) + content = response.read().decode("utf-8") + with open(r"cancel_data.csv", "w") as f: + f.write(content) + + data = pd.read_csv(r"cancel_data.csv", header=None) + + # 1: pre-processing data + del data[data.columns.tolist()[0]] + data = data.dropna(axis=0) + data = data.replace({"M": np.float64(1), "B": np.float64(-1)}) + samples = np.array(data)[:, :] + + # 2: dividing data into train_data data and test_data data + train_data, test_data = samples[:328, :], samples[328:, :] + test_tags, test_samples = test_data[:, 0], test_data[:, 1:] + + # 3: choose kernel function,and set initial alphas to zero(optional) + mykernel = Kernel(kernel="rbf", degree=5, coef0=1, gamma=0.5) + al = np.zeros(train_data.shape[0]) + + # 4: calculating best alphas using SMO algorithm and predict test_data samples + mysvm = SmoSVM( + train=train_data, + kernel_func=mykernel, + alpha_list=al, + cost=0.4, + b=0.0, + tolerance=0.001, + ) + mysvm.fit() + predict = mysvm.predict(test_samples) + + # 5: check accuracy + score = 0 + test_num = test_tags.shape[0] + for i in range(test_tags.shape[0]): + if test_tags[i] == predict[i]: + score += 1 + print(f"\nall: {test_num}\nright: {score}\nfalse: {test_num - score}") + print(f"Rough Accuracy: {score / test_tags.shape[0]}") + + +def test_demonstration(): + # change stdout + print("\nStart plot,please wait!!!") + sys.stdout = open(os.devnull, "w") + + ax1 = plt.subplot2grid((2, 2), (0, 0)) + ax2 = plt.subplot2grid((2, 2), (0, 1)) + ax3 = plt.subplot2grid((2, 2), (1, 0)) + ax4 = plt.subplot2grid((2, 2), (1, 1)) + ax1.set_title("linear svm,cost:0.1") + test_linear_kernel(ax1, cost=0.1) + ax2.set_title("linear svm,cost:500") + test_linear_kernel(ax2, cost=500) + ax3.set_title("rbf kernel svm,cost:0.1") + test_rbf_kernel(ax3, cost=0.1) + ax4.set_title("rbf kernel svm,cost:500") + test_rbf_kernel(ax4, cost=500) + + sys.stdout = sys.__stdout__ + print("Plot done!!!") + + +def test_linear_kernel(ax, cost): + train_x, train_y = make_blobs( + n_samples=500, centers=2, n_features=2, random_state=1 + ) + train_y[train_y == 0] = -1 + scaler = StandardScaler() + train_x_scaled = scaler.fit_transform(train_x, train_y) + train_data = np.hstack((train_y.reshape(500, 1), train_x_scaled)) + mykernel = Kernel(kernel="linear", degree=5, coef0=1, gamma=0.5) + mysvm = SmoSVM( + train=train_data, + kernel_func=mykernel, + cost=cost, + tolerance=0.001, + auto_norm=False, + ) + mysvm.fit() + plot_partition_boundary(mysvm, train_data, ax=ax) + + +def test_rbf_kernel(ax, cost): + train_x, train_y = make_circles( + n_samples=500, noise=0.1, factor=0.1, random_state=1 + ) + train_y[train_y == 0] = -1 + scaler = StandardScaler() + train_x_scaled = scaler.fit_transform(train_x, train_y) + train_data = np.hstack((train_y.reshape(500, 1), train_x_scaled)) + mykernel = Kernel(kernel="rbf", degree=5, coef0=1, gamma=0.5) + mysvm = SmoSVM( + train=train_data, + kernel_func=mykernel, + cost=cost, + tolerance=0.001, + auto_norm=False, + ) + mysvm.fit() + plot_partition_boundary(mysvm, train_data, ax=ax) + + +def plot_partition_boundary( + model, train_data, ax, resolution=100, colors=("b", "k", "r") +): + """ + We can not get the optimum w of our kernel svm model which is different from linear + svm. For this reason, we generate randomly distributed points with high desity and + prediced values of these points are calculated by using our tained model. Then we + could use this prediced values to draw contour map. + And this contour map can represent svm's partition boundary. + """ + train_data_x = train_data[:, 1] + train_data_y = train_data[:, 2] + train_data_tags = train_data[:, 0] + xrange = np.linspace(train_data_x.min(), train_data_x.max(), resolution) + yrange = np.linspace(train_data_y.min(), train_data_y.max(), resolution) + test_samples = np.array([(x, y) for x in xrange for y in yrange]).reshape( + resolution * resolution, 2 + ) + + test_tags = model.predict(test_samples, classify=False) + grid = test_tags.reshape((len(xrange), len(yrange))) + + # Plot contour map which represents the partition boundary + ax.contour( + xrange, + yrange, + np.mat(grid).T, + levels=(-1, 0, 1), + linestyles=("--", "-", "--"), + linewidths=(1, 1, 1), + colors=colors, + ) + # Plot all train samples + ax.scatter( + train_data_x, + train_data_y, + c=train_data_tags, + cmap=plt.cm.Dark2, + lw=0, + alpha=0.5, + ) + + # Plot support vectors + support = model.support + ax.scatter( + train_data_x[support], + train_data_y[support], + c=train_data_tags[support], + cmap=plt.cm.Dark2, + ) + + +if __name__ == "__main__": + test_cancel_data() + test_demonstration() + plt.show() diff --git a/machine_learning/similarity_search.py b/machine_learning/similarity_search.py new file mode 100644 index 000000000000..6bfb12ed88cb --- /dev/null +++ b/machine_learning/similarity_search.py @@ -0,0 +1,137 @@ +""" +Similarity Search : https://en.wikipedia.org/wiki/Similarity_search +Similarity search is a search algorithm for finding the nearest vector from +vectors, used in natural language processing. +In this algorithm, it calculates distance with euclidean distance and +returns a list containing two data for each vector: + 1. the nearest vector + 2. distance between the vector and the nearest vector (float) +""" +import math + +import numpy as np + + +def euclidean(input_a: np.ndarray, input_b: np.ndarray) -> float: + """ + Calculates euclidean distance between two data. + :param input_a: ndarray of first vector. + :param input_b: ndarray of second vector. + :return: Euclidean distance of input_a and input_b. By using math.sqrt(), + result will be float. + + >>> euclidean(np.array([0]), np.array([1])) + 1.0 + >>> euclidean(np.array([0, 1]), np.array([1, 1])) + 1.0 + >>> euclidean(np.array([0, 0, 0]), np.array([0, 0, 1])) + 1.0 + """ + return math.sqrt(sum(pow(a - b, 2) for a, b in zip(input_a, input_b))) + + +def similarity_search(dataset: np.ndarray, value_array: np.ndarray) -> list: + """ + :param dataset: Set containing the vectors. Should be ndarray. + :param value_array: vector/vectors we want to know the nearest vector from dataset. + :return: Result will be a list containing + 1. the nearest vector + 2. distance from the vector + + >>> dataset = np.array([[0], [1], [2]]) + >>> value_array = np.array([[0]]) + >>> similarity_search(dataset, value_array) + [[[0], 0.0]] + + >>> dataset = np.array([[0, 0], [1, 1], [2, 2]]) + >>> value_array = np.array([[0, 1]]) + >>> similarity_search(dataset, value_array) + [[[0, 0], 1.0]] + + >>> dataset = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]]) + >>> value_array = np.array([[0, 0, 1]]) + >>> similarity_search(dataset, value_array) + [[[0, 0, 0], 1.0]] + + >>> dataset = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]]) + >>> value_array = np.array([[0, 0, 0], [0, 0, 1]]) + >>> similarity_search(dataset, value_array) + [[[0, 0, 0], 0.0], [[0, 0, 0], 1.0]] + + These are the errors that might occur: + + 1. If dimensions are different. + For example, dataset has 2d array and value_array has 1d array: + >>> dataset = np.array([[1]]) + >>> value_array = np.array([1]) + >>> similarity_search(dataset, value_array) + Traceback (most recent call last): + ... + ValueError: Wrong input data's dimensions... dataset : 2, value_array : 1 + + 2. If data's shapes are different. + For example, dataset has shape of (3, 2) and value_array has (2, 3). + We are expecting same shapes of two arrays, so it is wrong. + >>> dataset = np.array([[0, 0], [1, 1], [2, 2]]) + >>> value_array = np.array([[0, 0, 0], [0, 0, 1]]) + >>> similarity_search(dataset, value_array) + Traceback (most recent call last): + ... + ValueError: Wrong input data's shape... dataset : 2, value_array : 3 + + 3. If data types are different. + When trying to compare, we are expecting same types so they should be same. + If not, it'll come up with errors. + >>> dataset = np.array([[0, 0], [1, 1], [2, 2]], dtype=np.float32) + >>> value_array = np.array([[0, 0], [0, 1]], dtype=np.int32) + >>> similarity_search(dataset, value_array) # doctest: +NORMALIZE_WHITESPACE + Traceback (most recent call last): + ... + TypeError: Input data have different datatype... + dataset : float32, value_array : int32 + """ + + if dataset.ndim != value_array.ndim: + raise ValueError( + f"Wrong input data's dimensions... dataset : {dataset.ndim}, " + f"value_array : {value_array.ndim}" + ) + + try: + if dataset.shape[1] != value_array.shape[1]: + raise ValueError( + f"Wrong input data's shape... dataset : {dataset.shape[1]}, " + f"value_array : {value_array.shape[1]}" + ) + except IndexError: + if dataset.ndim != value_array.ndim: + raise TypeError("Wrong shape") + + if dataset.dtype != value_array.dtype: + raise TypeError( + f"Input data have different datatype... dataset : {dataset.dtype}, " + f"value_array : {value_array.dtype}" + ) + + answer = [] + + for value in value_array: + dist = euclidean(value, dataset[0]) + vector = dataset[0].tolist() + + for dataset_value in dataset[1:]: + temp_dist = euclidean(value, dataset_value) + + if dist > temp_dist: + dist = temp_dist + vector = dataset_value.tolist() + + answer.append([vector, dist]) + + return answer + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/machine_learning/support_vector_machines.py b/machine_learning/support_vector_machines.py new file mode 100644 index 000000000000..c5e5085d8748 --- /dev/null +++ b/machine_learning/support_vector_machines.py @@ -0,0 +1,58 @@ +from sklearn import svm +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split + + +# different functions implementing different types of SVM's +def NuSVC(train_x, train_y): + svc_NuSVC = svm.NuSVC() + svc_NuSVC.fit(train_x, train_y) + return svc_NuSVC + + +def Linearsvc(train_x, train_y): + svc_linear = svm.LinearSVC(tol=10e-2) + svc_linear.fit(train_x, train_y) + return svc_linear + + +def SVC(train_x, train_y): + # svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True, + # probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False, + # max_iter=1000, random_state=None) + # various parameters like "kernel","gamma","C" can effectively tuned for a given + # machine learning model. + SVC = svm.SVC(gamma="auto") + SVC.fit(train_x, train_y) + return SVC + + +def test(X_new): + """ + 3 test cases to be passed + an array containing the sepal length (cm), sepal width (cm), petal length (cm), + petal width (cm) based on which the target name will be predicted + >>> test([1,2,1,4]) + 'virginica' + >>> test([5, 2, 4, 1]) + 'versicolor' + >>> test([6,3,4,1]) + 'versicolor' + """ + iris = load_iris() + # splitting the dataset to test and train + train_x, test_x, train_y, test_y = train_test_split( + iris["data"], iris["target"], random_state=4 + ) + # any of the 3 types of SVM can be used + # current_model=SVC(train_x, train_y) + # current_model=NuSVC(train_x, train_y) + current_model = Linearsvc(train_x, train_y) + prediction = current_model.predict([X_new]) + return iris["target_names"][prediction][0] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/machine_learning/word_frequency_functions.py b/machine_learning/word_frequency_functions.py new file mode 100644 index 000000000000..9cf7b694c6be --- /dev/null +++ b/machine_learning/word_frequency_functions.py @@ -0,0 +1,137 @@ +import string +from math import log10 + +""" + tf-idf Wikipedia: https://en.wikipedia.org/wiki/Tf%E2%80%93idf + tf-idf and other word frequency algorithms are often used + as a weighting factor in information retrieval and text + mining. 83% of text-based recommender systems use + tf-idf for term weighting. In Layman's terms, tf-idf + is a statistic intended to reflect how important a word + is to a document in a corpus (a collection of documents) + + + Here I've implemented several word frequency algorithms + that are commonly used in information retrieval: Term Frequency, + Document Frequency, and TF-IDF (Term-Frequency*Inverse-Document-Frequency) + are included. + + Term Frequency is a statistical function that + returns a number representing how frequently + an expression occurs in a document. This + indicates how significant a particular term is in + a given document. + + Document Frequency is a statistical function that returns + an integer representing the number of documents in a + corpus that a term occurs in (where the max number returned + would be the number of documents in the corpus). + + Inverse Document Frequency is mathematically written as + log10(N/df), where N is the number of documents in your + corpus and df is the Document Frequency. If df is 0, a + ZeroDivisionError will be thrown. + + Term-Frequency*Inverse-Document-Frequency is a measure + of the originality of a term. It is mathematically written + as tf*log10(N/df). It compares the number of times + a term appears in a document with the number of documents + the term appears in. If df is 0, a ZeroDivisionError will be thrown. +""" + + +def term_frequency(term: str, document: str) -> int: + """ + Return the number of times a term occurs within + a given document. + @params: term, the term to search a document for, and document, + the document to search within + @returns: an integer representing the number of times a term is + found within the document + + @examples: + >>> term_frequency("to", "To be, or not to be") + 2 + """ + # strip all punctuation and newlines and replace it with '' + document_without_punctuation = document.translate( + str.maketrans("", "", string.punctuation) + ).replace("\n", "") + tokenize_document = document_without_punctuation.split(" ") # word tokenization + return len([word for word in tokenize_document if word.lower() == term.lower()]) + + +def document_frequency(term: str, corpus: str) -> int: + """ + Calculate the number of documents in a corpus that contain a + given term + @params : term, the term to search each document for, and corpus, a collection of + documents. Each document should be separated by a newline. + @returns : the number of documents in the corpus that contain the term you are + searching for and the number of documents in the corpus + @examples : + >>> document_frequency("first", "This is the first document in the corpus.\\nThIs\ +is the second document in the corpus.\\nTHIS is \ +the third document in the corpus.") + (1, 3) + """ + corpus_without_punctuation = corpus.lower().translate( + str.maketrans("", "", string.punctuation) + ) # strip all punctuation and replace it with '' + docs = corpus_without_punctuation.split("\n") + term = term.lower() + return (len([doc for doc in docs if term in doc]), len(docs)) + + +def inverse_document_frequency(df: int, N: int, smoothing=False) -> float: + """ + Return an integer denoting the importance + of a word. This measure of importance is + calculated by log10(N/df), where N is the + number of documents and df is + the Document Frequency. + @params : df, the Document Frequency, N, + the number of documents in the corpus and + smoothing, if True return the idf-smooth + @returns : log10(N/df) or 1+log10(N/1+df) + @examples : + >>> inverse_document_frequency(3, 0) + Traceback (most recent call last): + ... + ValueError: log10(0) is undefined. + >>> inverse_document_frequency(1, 3) + 0.477 + >>> inverse_document_frequency(0, 3) + Traceback (most recent call last): + ... + ZeroDivisionError: df must be > 0 + >>> inverse_document_frequency(0, 3,True) + 1.477 + """ + if smoothing: + if N == 0: + raise ValueError("log10(0) is undefined.") + return round(1 + log10(N / (1 + df)), 3) + + if df == 0: + raise ZeroDivisionError("df must be > 0") + elif N == 0: + raise ValueError("log10(0) is undefined.") + return round(log10(N / df), 3) + + +def tf_idf(tf: int, idf: int) -> float: + """ + Combine the term frequency + and inverse document frequency functions to + calculate the originality of a term. This + 'originality' is calculated by multiplying + the term frequency and the inverse document + frequency : tf-idf = TF * IDF + @params : tf, the term frequency, and idf, the inverse document + frequency + @examples : + >>> tf_idf(2, 0.477) + 0.954 + """ + return round(tf * idf, 3) diff --git a/maths/3n+1.py b/maths/3n+1.py deleted file mode 100644 index 6424fe0d8f15..000000000000 --- a/maths/3n+1.py +++ /dev/null @@ -1,19 +0,0 @@ -def main(): - def n31(a):# a = initial number - c = 0 - l = [a] - while a != 1: - if a % 2 == 0:#if even divide it by 2 - a = a // 2 - elif a % 2 == 1:#if odd 3n+1 - a = 3*a +1 - c += 1#counter - l += [a] - - return l , c - print(n31(43)) - print(n31(98)[0][-1])# = a - print("It took {0} steps.".format(n31(13)[1]))#optional finish - -if __name__ == '__main__': - main() diff --git a/maths/3n_plus_1.py b/maths/3n_plus_1.py new file mode 100644 index 000000000000..28c9fd7b426f --- /dev/null +++ b/maths/3n_plus_1.py @@ -0,0 +1,149 @@ +from __future__ import annotations + + +def n31(a: int) -> tuple[list[int], int]: + """ + Returns the Collatz sequence and its length of any positive integer. + >>> n31(4) + ([4, 2, 1], 3) + """ + + if not isinstance(a, int): + raise TypeError("Must be int, not {}".format(type(a).__name__)) + if a < 1: + raise ValueError(f"Given integer must be greater than 1, not {a}") + + path = [a] + while a != 1: + if a % 2 == 0: + a = a // 2 + else: + a = 3 * a + 1 + path += [a] + return path, len(path) + + +def test_n31(): + """ + >>> test_n31() + """ + assert n31(4) == ([4, 2, 1], 3) + assert n31(11) == ([11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1], 15) + assert n31(31) == ( + [ + 31, + 94, + 47, + 142, + 71, + 214, + 107, + 322, + 161, + 484, + 242, + 121, + 364, + 182, + 91, + 274, + 137, + 412, + 206, + 103, + 310, + 155, + 466, + 233, + 700, + 350, + 175, + 526, + 263, + 790, + 395, + 1186, + 593, + 1780, + 890, + 445, + 1336, + 668, + 334, + 167, + 502, + 251, + 754, + 377, + 1132, + 566, + 283, + 850, + 425, + 1276, + 638, + 319, + 958, + 479, + 1438, + 719, + 2158, + 1079, + 3238, + 1619, + 4858, + 2429, + 7288, + 3644, + 1822, + 911, + 2734, + 1367, + 4102, + 2051, + 6154, + 3077, + 9232, + 4616, + 2308, + 1154, + 577, + 1732, + 866, + 433, + 1300, + 650, + 325, + 976, + 488, + 244, + 122, + 61, + 184, + 92, + 46, + 23, + 70, + 35, + 106, + 53, + 160, + 80, + 40, + 20, + 10, + 5, + 16, + 8, + 4, + 2, + 1, + ], + 107, + ) + + +if __name__ == "__main__": + num = 4 + path, length = n31(num) + print(f"The Collatz sequence of {num} took {length} steps. \nPath: {path}") diff --git a/maths/FindMax.py b/maths/FindMax.py deleted file mode 100644 index 0ce49a68c348..000000000000 --- a/maths/FindMax.py +++ /dev/null @@ -1,14 +0,0 @@ -# NguyenU - -def find_max(nums): - max = nums[0] - for x in nums: - if x > max: - max = x - print(max) - -def main(): - find_max([2, 4, 9, 7, 19, 94, 5]) - -if __name__ == '__main__': - main() diff --git a/maths/FindMin.py b/maths/FindMin.py deleted file mode 100644 index 86207984e3da..000000000000 --- a/maths/FindMin.py +++ /dev/null @@ -1,12 +0,0 @@ -def main(): - def findMin(x): - minNum = x[0] - for i in x: - if minNum > i: - minNum = i - return minNum - - print(findMin([0,1,2,3,4,5,-3,24,-56])) # = -56 - -if __name__ == '__main__': - main() diff --git a/maths/Hanoi.py b/maths/Hanoi.py deleted file mode 100644 index dd04d0fa58d8..000000000000 --- a/maths/Hanoi.py +++ /dev/null @@ -1,24 +0,0 @@ -# @author willx75 -# Tower of Hanoi recursion game algorithm is a game, it consists of three rods and a number of disks of different sizes, which can slide onto any rod - -import logging - -log = logging.getLogger() -logging.basicConfig(level=logging.DEBUG) - - -def Tower_Of_Hanoi(n, source, dest, by, mouvement): - if n == 0: - return n - elif n == 1: - mouvement += 1 - # no print statement (you could make it an optional flag for printing logs) - logging.debug('Move the plate from', source, 'to', dest) - return mouvement - else: - - mouvement = mouvement + Tower_Of_Hanoi(n-1, source, by, dest, 0) - logging.debug('Move the plate from', source, 'to', dest) - - mouvement = mouvement + 1 + Tower_Of_Hanoi(n-1, by, dest, source, 0) - return mouvement diff --git a/maths/PrimeCheck.py b/maths/PrimeCheck.py deleted file mode 100644 index e0c51d77a038..000000000000 --- a/maths/PrimeCheck.py +++ /dev/null @@ -1,13 +0,0 @@ -import math -def primeCheck(number): - if number % 2 == 0 and number > 2: - return False - return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) - -def main(): - print(primeCheck(37)) - print(primeCheck(100)) - print(primeCheck(77)) - -if __name__ == '__main__': - main() diff --git a/maths/__init__.py b/maths/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/maths/abs.py b/maths/abs.py index 6d0596478d5f..68c99a1d51d8 100644 --- a/maths/abs.py +++ b/maths/abs.py @@ -1,18 +1,28 @@ -def absVal(num): +"""Absolute Value.""" + + +def abs_val(num): """ - Function to fins absolute value of numbers. - >>absVal(-5) - 5 - >>absVal(0) + Find the absolute value of a number. + + >>> abs_val(-5.1) + 5.1 + >>> abs_val(-5) == abs_val(5) + True + >>> abs_val(0) 0 """ - if num < 0: - return -num - else: - return num + return -num if num < 0 else num + + +def test_abs_val(): + """ + >>> test_abs_val() + """ + assert 0 == abs_val(0) + assert 34 == abs_val(34) + assert 100000000000 == abs_val(-100000000000) -def main(): - print(absVal(-34)) # = 34 -if __name__ == '__main__': - main() +if __name__ == "__main__": + print(abs_val(-34)) # --> 34 diff --git a/maths/absMax.py b/maths/absMax.py deleted file mode 100644 index 7ff9e4d3ca09..000000000000 --- a/maths/absMax.py +++ /dev/null @@ -1,25 +0,0 @@ -def absMax(x): - """ - #>>>absMax([0,5,1,11]) - 11 - >>absMax([3,-10,-2]) - -10 - """ - j =x[0] - for i in x: - if abs(i) > abs(j): - j = i - return j - - -def main(): - a = [1,2,-11] - print(absMax(a)) # = -11 - - -if __name__ == '__main__': - main() - -""" -print abs Max -""" diff --git a/maths/absMin.py b/maths/absMin.py deleted file mode 100644 index 67d510551907..000000000000 --- a/maths/absMin.py +++ /dev/null @@ -1,20 +0,0 @@ -from Maths.abs import absVal -def absMin(x): - """ - # >>>absMin([0,5,1,11]) - 0 - # >>absMin([3,-10,-2]) - -2 - """ - j = x[0] - for i in x: - if absVal(i) < absVal(j): - j = i - return j - -def main(): - a = [-3,-1,2,-11] - print(absMin(a)) # = -1 - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/maths/abs_max.py b/maths/abs_max.py new file mode 100644 index 000000000000..e5a8219657ac --- /dev/null +++ b/maths/abs_max.py @@ -0,0 +1,35 @@ +from __future__ import annotations + + +def abs_max(x: list[int]) -> int: + """ + >>> abs_max([0,5,1,11]) + 11 + >>> abs_max([3,-10,-2]) + -10 + """ + j = x[0] + for i in x: + if abs(i) > abs(j): + j = i + return j + + +def abs_max_sort(x): + """ + >>> abs_max_sort([0,5,1,11]) + 11 + >>> abs_max_sort([3,-10,-2]) + -10 + """ + return sorted(x, key=abs)[-1] + + +def main(): + a = [1, 2, -11] + assert abs_max(a) == -11 + assert abs_max_sort(a) == -11 + + +if __name__ == "__main__": + main() diff --git a/maths/abs_min.py b/maths/abs_min.py new file mode 100644 index 000000000000..eb84de37ce23 --- /dev/null +++ b/maths/abs_min.py @@ -0,0 +1,24 @@ +from .abs import abs_val + + +def absMin(x): + """ + >>> absMin([0,5,1,11]) + 0 + >>> absMin([3,-10,-2]) + -2 + """ + j = x[0] + for i in x: + if abs_val(i) < abs_val(j): + j = i + return j + + +def main(): + a = [-3, -1, 2, -11] + print(absMin(a)) # = -1 + + +if __name__ == "__main__": + main() diff --git a/maths/add.py b/maths/add.py new file mode 100644 index 000000000000..0bc7da9697d3 --- /dev/null +++ b/maths/add.py @@ -0,0 +1,19 @@ +""" +Just to check +""" + + +def add(a, b): + """ + >>> add(2, 2) + 4 + >>> add(2, -2) + 0 + """ + return a + b + + +if __name__ == "__main__": + a = 5 + b = 6 + print(f"The sum of {a} + {b} is {add(a, b)}") diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py new file mode 100644 index 000000000000..9c58aa61d19e --- /dev/null +++ b/maths/aliquot_sum.py @@ -0,0 +1,48 @@ +def aliquot_sum(input_num: int) -> int: + """ + Finds the aliquot sum of an input integer, where the + aliquot sum of a number n is defined as the sum of all + natural numbers less than n that divide n evenly. For + example, the aliquot sum of 15 is 1 + 3 + 5 = 9. This is + a simple O(n) implementation. + @param input_num: a positive integer whose aliquot sum is to be found + @return: the aliquot sum of input_num, if input_num is positive. + Otherwise, raise a ValueError + Wikipedia Explanation: https://en.wikipedia.org/wiki/Aliquot_sum + + >>> aliquot_sum(15) + 9 + >>> aliquot_sum(6) + 6 + >>> aliquot_sum(-1) + Traceback (most recent call last): + ... + ValueError: Input must be positive + >>> aliquot_sum(0) + Traceback (most recent call last): + ... + ValueError: Input must be positive + >>> aliquot_sum(1.6) + Traceback (most recent call last): + ... + ValueError: Input must be an integer + >>> aliquot_sum(12) + 16 + >>> aliquot_sum(1) + 0 + >>> aliquot_sum(19) + 1 + """ + if not isinstance(input_num, int): + raise ValueError("Input must be an integer") + if input_num <= 0: + raise ValueError("Input must be positive") + return sum( + divisor for divisor in range(1, input_num // 2 + 1) if input_num % divisor == 0 + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/allocation_number.py b/maths/allocation_number.py new file mode 100644 index 000000000000..d419e74d01ff --- /dev/null +++ b/maths/allocation_number.py @@ -0,0 +1,49 @@ +""" +In a multi-threaded download, this algorithm could be used to provide +each worker thread with a block of non-overlapping bytes to download. +For example: + for i in allocation_list: + requests.get(url,headers={'Range':f'bytes={i}'}) +""" +from __future__ import annotations + + +def allocation_num(number_of_bytes: int, partitions: int) -> list[str]: + """ + Divide a number of bytes into x partitions. + :param number_of_bytes: the total of bytes. + :param partitions: the number of partition need to be allocated. + :return: list of bytes to be assigned to each worker thread + + >>> allocation_num(16647, 4) + ['1-4161', '4162-8322', '8323-12483', '12484-16647'] + >>> allocation_num(50000, 5) + ['1-10000', '10001-20000', '20001-30000', '30001-40000', '40001-50000'] + >>> allocation_num(888, 999) + Traceback (most recent call last): + ... + ValueError: partitions can not > number_of_bytes! + >>> allocation_num(888, -4) + Traceback (most recent call last): + ... + ValueError: partitions must be a positive number! + """ + if partitions <= 0: + raise ValueError("partitions must be a positive number!") + if partitions > number_of_bytes: + raise ValueError("partitions can not > number_of_bytes!") + bytes_per_partition = number_of_bytes // partitions + allocation_list = [] + for i in range(partitions): + start_bytes = i * bytes_per_partition + 1 + end_bytes = ( + number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition + ) + allocation_list.append(f"{start_bytes}-{end_bytes}") + return allocation_list + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/area.py b/maths/area.py new file mode 100644 index 000000000000..8689f323cc9a --- /dev/null +++ b/maths/area.py @@ -0,0 +1,288 @@ +""" +Find the area of various geometric shapes +""" +from math import pi, sqrt + + +def surface_area_cube(side_length: float) -> float: + """ + Calculate the Surface Area of a Cube. + + >>> surface_area_cube(1) + 6 + >>> surface_area_cube(3) + 54 + >>> surface_area_cube(-1) + Traceback (most recent call last): + ... + ValueError: surface_area_cube() only accepts non-negative values + """ + if side_length < 0: + raise ValueError("surface_area_cube() only accepts non-negative values") + return 6 * side_length ** 2 + + +def surface_area_sphere(radius: float) -> float: + """ + Calculate the Surface Area of a Sphere. + Wikipedia reference: https://en.wikipedia.org/wiki/Sphere + Formula: 4 * pi * r^2 + + >>> surface_area_sphere(5) + 314.1592653589793 + >>> surface_area_sphere(1) + 12.566370614359172 + >>> surface_area_sphere(-1) + Traceback (most recent call last): + ... + ValueError: surface_area_sphere() only accepts non-negative values + """ + if radius < 0: + raise ValueError("surface_area_sphere() only accepts non-negative values") + return 4 * pi * radius ** 2 + + +def area_rectangle(length: float, width: float) -> float: + """ + Calculate the area of a rectangle. + + >>> area_rectangle(10, 20) + 200 + >>> area_rectangle(-1, -2) + Traceback (most recent call last): + ... + ValueError: area_rectangle() only accepts non-negative values + >>> area_rectangle(1, -2) + Traceback (most recent call last): + ... + ValueError: area_rectangle() only accepts non-negative values + >>> area_rectangle(-1, 2) + Traceback (most recent call last): + ... + ValueError: area_rectangle() only accepts non-negative values + """ + if length < 0 or width < 0: + raise ValueError("area_rectangle() only accepts non-negative values") + return length * width + + +def area_square(side_length: float) -> float: + """ + Calculate the area of a square. + + >>> area_square(10) + 100 + >>> area_square(-1) + Traceback (most recent call last): + ... + ValueError: area_square() only accepts non-negative values + """ + if side_length < 0: + raise ValueError("area_square() only accepts non-negative values") + return side_length ** 2 + + +def area_triangle(base: float, height: float) -> float: + """ + Calculate the area of a triangle given the base and height. + + >>> area_triangle(10, 10) + 50.0 + >>> area_triangle(-1, -2) + Traceback (most recent call last): + ... + ValueError: area_triangle() only accepts non-negative values + >>> area_triangle(1, -2) + Traceback (most recent call last): + ... + ValueError: area_triangle() only accepts non-negative values + >>> area_triangle(-1, 2) + Traceback (most recent call last): + ... + ValueError: area_triangle() only accepts non-negative values + """ + if base < 0 or height < 0: + raise ValueError("area_triangle() only accepts non-negative values") + return (base * height) / 2 + + +def area_triangle_three_sides(side1: float, side2: float, side3: float) -> float: + """ + Calculate area of triangle when the length of 3 sides are known. + + This function uses Heron's formula: https://en.wikipedia.org/wiki/Heron%27s_formula + + >>> area_triangle_three_sides(5, 12, 13) + 30.0 + >>> area_triangle_three_sides(10, 11, 12) + 51.521233486786784 + >>> area_triangle_three_sides(-1, -2, -1) + Traceback (most recent call last): + ... + ValueError: area_triangle_three_sides() only accepts non-negative values + >>> area_triangle_three_sides(1, -2, 1) + Traceback (most recent call last): + ... + ValueError: area_triangle_three_sides() only accepts non-negative values + """ + if side1 < 0 or side2 < 0 or side3 < 0: + raise ValueError("area_triangle_three_sides() only accepts non-negative values") + elif side1 + side2 < side3 or side1 + side3 < side2 or side2 + side3 < side1: + raise ValueError("Given three sides do not form a triangle") + semi_perimeter = (side1 + side2 + side3) / 2 + area = sqrt( + semi_perimeter + * (semi_perimeter - side1) + * (semi_perimeter - side2) + * (semi_perimeter - side3) + ) + return area + + +def area_parallelogram(base: float, height: float) -> float: + """ + Calculate the area of a parallelogram. + + >>> area_parallelogram(10, 20) + 200 + >>> area_parallelogram(-1, -2) + Traceback (most recent call last): + ... + ValueError: area_parallelogram() only accepts non-negative values + >>> area_parallelogram(1, -2) + Traceback (most recent call last): + ... + ValueError: area_parallelogram() only accepts non-negative values + >>> area_parallelogram(-1, 2) + Traceback (most recent call last): + ... + ValueError: area_parallelogram() only accepts non-negative values + """ + if base < 0 or height < 0: + raise ValueError("area_parallelogram() only accepts non-negative values") + return base * height + + +def area_trapezium(base1: float, base2: float, height: float) -> float: + """ + Calculate the area of a trapezium. + + >>> area_trapezium(10, 20, 30) + 450.0 + >>> area_trapezium(-1, -2, -3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(-1, 2, 3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(1, -2, 3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(1, 2, -3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(-1, -2, 3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(1, -2, -3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(-1, 2, -3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + """ + if base1 < 0 or base2 < 0 or height < 0: + raise ValueError("area_trapezium() only accepts non-negative values") + return 1 / 2 * (base1 + base2) * height + + +def area_circle(radius: float) -> float: + """ + Calculate the area of a circle. + + >>> area_circle(20) + 1256.6370614359173 + >>> area_circle(-1) + Traceback (most recent call last): + ... + ValueError: area_circle() only accepts non-negative values + """ + if radius < 0: + raise ValueError("area_circle() only accepts non-negative values") + return pi * radius ** 2 + + +def area_ellipse(radius_x: float, radius_y: float) -> float: + """ + Calculate the area of a ellipse. + + >>> area_ellipse(10, 10) + 314.1592653589793 + >>> area_ellipse(10, 20) + 628.3185307179587 + >>> area_ellipse(-10, 20) + Traceback (most recent call last): + ... + ValueError: area_ellipse() only accepts non-negative values + >>> area_ellipse(10, -20) + Traceback (most recent call last): + ... + ValueError: area_ellipse() only accepts non-negative values + >>> area_ellipse(-10, -20) + Traceback (most recent call last): + ... + ValueError: area_ellipse() only accepts non-negative values + """ + if radius_x < 0 or radius_y < 0: + raise ValueError("area_ellipse() only accepts non-negative values") + return pi * radius_x * radius_y + + +def area_rhombus(diagonal_1: float, diagonal_2: float) -> float: + """ + Calculate the area of a rhombus. + + >>> area_rhombus(10, 20) + 100.0 + >>> area_rhombus(-1, -2) + Traceback (most recent call last): + ... + ValueError: area_rhombus() only accepts non-negative values + >>> area_rhombus(1, -2) + Traceback (most recent call last): + ... + ValueError: area_rhombus() only accepts non-negative values + >>> area_rhombus(-1, 2) + Traceback (most recent call last): + ... + ValueError: area_rhombus() only accepts non-negative values + """ + if diagonal_1 < 0 or diagonal_2 < 0: + raise ValueError("area_rhombus() only accepts non-negative values") + return 1 / 2 * diagonal_1 * diagonal_2 + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) # verbose so we can see methods missing tests + + print("[DEMO] Areas of various geometric shapes: \n") + print(f"Rectangle: {area_rectangle(10, 20) = }") + print(f"Square: {area_square(10) = }") + print(f"Triangle: {area_triangle(10, 10) = }") + print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }") + print(f"Parallelogram: {area_parallelogram(10, 20) = }") + print(f"Trapezium: {area_trapezium(10, 20, 30) = }") + print(f"Circle: {area_circle(20) = }") + print("\nSurface Areas of various geometric shapes: \n") + print(f"Cube: {surface_area_cube(20) = }") + print(f"Sphere: {surface_area_sphere(20) = }") + print(f"Rhombus: {area_rhombus(10, 20) = }") diff --git a/maths/area_under_curve.py b/maths/area_under_curve.py new file mode 100644 index 000000000000..2d01e414b63b --- /dev/null +++ b/maths/area_under_curve.py @@ -0,0 +1,59 @@ +""" +Approximates the area under the curve using the trapezoidal rule +""" + +from typing import Callable, Union + + +def trapezoidal_area( + fnc: Callable[[Union[int, float]], Union[int, float]], + x_start: Union[int, float], + x_end: Union[int, float], + steps: int = 100, +) -> float: + """ + Treats curve as a collection of linear lines and sums the area of the + trapezium shape they form + :param fnc: a function which defines a curve + :param x_start: left end point to indicate the start of line segment + :param x_end: right end point to indicate end of line segment + :param steps: an accuracy gauge; more steps increases the accuracy + :return: a float representing the length of the curve + + >>> def f(x): + ... return 5 + >>> f"{trapezoidal_area(f, 12.0, 14.0, 1000):.3f}" + '10.000' + >>> def f(x): + ... return 9*x**2 + >>> f"{trapezoidal_area(f, -4.0, 0, 10000):.4f}" + '192.0000' + >>> f"{trapezoidal_area(f, -4.0, 4.0, 10000):.4f}" + '384.0000' + """ + x1 = x_start + fx1 = fnc(x_start) + area = 0.0 + for i in range(steps): + # Approximates small segments of curve as linear and solve + # for trapezoidal area + x2 = (x_end - x_start) / steps + x1 + fx2 = fnc(x2) + area += abs(fx2 + fx1) * (x2 - x1) / 2 + # Increment step + x1 = x2 + fx1 = fx2 + return area + + +if __name__ == "__main__": + + def f(x): + return x ** 3 + x ** 2 + + print("f(x) = x^3 + x^2") + print("The area between the curve, x = -5, x = 5 and the x axis is:") + i = 10 + while i <= 100000: + print(f"with {i} steps: {trapezoidal_area(f, -5, 5, i)}") + i *= 10 diff --git a/maths/armstrong_numbers.py b/maths/armstrong_numbers.py new file mode 100644 index 000000000000..d30ed2e430a0 --- /dev/null +++ b/maths/armstrong_numbers.py @@ -0,0 +1,69 @@ +""" +An Armstrong number is equal to the sum of the cubes of its digits. +For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370. +An Armstrong number is often called Narcissistic number. +""" + + +def armstrong_number(n: int) -> bool: + """ + Return True if n is an Armstrong number or False if it is not. + + >>> armstrong_number(153) + True + >>> armstrong_number(200) + False + >>> armstrong_number(1634) + True + >>> armstrong_number(0) + False + >>> armstrong_number(-1) + False + >>> armstrong_number(1.2) + False + """ + if not isinstance(n, int) or n < 1: + return False + + # Initialization of sum and number of digits. + sum = 0 + number_of_digits = 0 + temp = n + # Calculation of digits of the number + while temp > 0: + number_of_digits += 1 + temp //= 10 + # Dividing number into separate digits and find Armstrong number + temp = n + while temp > 0: + rem = temp % 10 + sum += rem ** number_of_digits + temp //= 10 + return n == sum + + +def narcissistic_number(n: int) -> bool: + """Return True if n is a narcissistic number or False if it is not""" + + expo = len(str(n)) # power, all number will be raised to + # each digit will be multiplied expo times + temp = [(int(i) ** expo) for i in str(n)] + + # check if sum of cube of each digit is equal to number + return n == sum(temp) + + +def main(): + """ + Request that user input an integer and tell them if it is Armstrong number. + """ + num = int(input("Enter an integer to see if it is an Armstrong number: ").strip()) + print(f"{num} is {'' if armstrong_number(num) else 'not '}an Armstrong number.") + print(f"{num} is {'' if narcissistic_number(num) else 'not '}an Armstrong number.") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/maths/average.py b/maths/average.py deleted file mode 100644 index dc70836b5e83..000000000000 --- a/maths/average.py +++ /dev/null @@ -1,14 +0,0 @@ -def average(nums): - sum = 0 - n = 0 - for x in nums: - sum += x - n += 1 - avg = sum / n - print(avg) - -def main(): - average([2, 4, 6, 8, 20, 50, 70]) - -if __name__ == '__main__': - main() diff --git a/maths/average_mean.py b/maths/average_mean.py new file mode 100644 index 000000000000..4beca1f741a0 --- /dev/null +++ b/maths/average_mean.py @@ -0,0 +1,20 @@ +"""Find mean of a list of numbers.""" + + +def average(nums): + """Find mean of a list of numbers.""" + return sum(nums) / len(nums) + + +def test_average(): + """ + >>> test_average() + """ + assert 12.0 == average([3, 6, 9, 12, 15, 18, 21]) + assert 20 == average([5, 10, 15, 20, 25, 30, 35]) + assert 4.5 == average([1, 2, 3, 4, 5, 6, 7, 8]) + + +if __name__ == "__main__": + """Call average module to find mean of a specific list of numbers.""" + print(average([2, 4, 6, 8, 20, 50, 70])) diff --git a/maths/average_median.py b/maths/average_median.py new file mode 100644 index 000000000000..0257e3f76f1a --- /dev/null +++ b/maths/average_median.py @@ -0,0 +1,35 @@ +def median(nums): + """ + Find median of a list of numbers. + + >>> median([0]) + 0 + >>> median([4,1,3,2]) + 2.5 + >>> median([2, 70, 6, 50, 20, 8, 4]) + 8 + + Args: + nums: List of nums + + Returns: + Median. + """ + sorted_list = sorted(nums) + length = len(sorted_list) + mid_index = length >> 1 + return ( + (sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2 + if length % 2 == 0 + else sorted_list[mid_index] + ) + + +def main(): + import doctest + + doctest.testmod() + + +if __name__ == "__main__": + main() diff --git a/maths/average_mode.py b/maths/average_mode.py new file mode 100644 index 000000000000..d472dc04d4bf --- /dev/null +++ b/maths/average_mode.py @@ -0,0 +1,31 @@ +import statistics + + +def mode(input_list): # Defining function "mode." + """This function returns the mode(Mode as in the measures of + central tendency) of the input data. + + The input list may contain any Datastructure or any Datatype. + + >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] + >>> mode(input_list) + 2 + >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] + >>> mode(input_list) == statistics.mode(input_list) + True + """ + # Copying input_list to check with the index number later. + check_list = input_list.copy() + result = list() # Empty list to store the counts of elements in input_list + for x in input_list: + result.append(input_list.count(x)) + input_list.remove(x) + y = max(result) # Gets the maximum value in the result list. + # Returns the value with the maximum number of repetitions. + return check_list[result.index(y)] + + +if __name__ == "__main__": + data = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] + print(mode(data)) + print(statistics.mode(data)) diff --git a/maths/bailey_borwein_plouffe.py b/maths/bailey_borwein_plouffe.py new file mode 100644 index 000000000000..febf7e975516 --- /dev/null +++ b/maths/bailey_borwein_plouffe.py @@ -0,0 +1,90 @@ +def bailey_borwein_plouffe(digit_position: int, precision: int = 1000) -> str: + """ + Implement a popular pi-digit-extraction algorithm known as the + Bailey-Borwein-Plouffe (BBP) formula to calculate the nth hex digit of pi. + Wikipedia page: + https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula + @param digit_position: a positive integer representing the position of the digit to + extract. + The digit immediately after the decimal point is located at position 1. + @param precision: number of terms in the second summation to calculate. + A higher number reduces the chance of an error but increases the runtime. + @return: a hexadecimal digit representing the digit at the nth position + in pi's decimal expansion. + + >>> "".join(bailey_borwein_plouffe(i) for i in range(1, 11)) + '243f6a8885' + >>> bailey_borwein_plouffe(5, 10000) + '6' + >>> bailey_borwein_plouffe(-10) + Traceback (most recent call last): + ... + ValueError: Digit position must be a positive integer + >>> bailey_borwein_plouffe(0) + Traceback (most recent call last): + ... + ValueError: Digit position must be a positive integer + >>> bailey_borwein_plouffe(1.7) + Traceback (most recent call last): + ... + ValueError: Digit position must be a positive integer + >>> bailey_borwein_plouffe(2, -10) + Traceback (most recent call last): + ... + ValueError: Precision must be a nonnegative integer + >>> bailey_borwein_plouffe(2, 1.6) + Traceback (most recent call last): + ... + ValueError: Precision must be a nonnegative integer + """ + if (not isinstance(digit_position, int)) or (digit_position <= 0): + raise ValueError("Digit position must be a positive integer") + elif (not isinstance(precision, int)) or (precision < 0): + raise ValueError("Precision must be a nonnegative integer") + + # compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly + # accurate + sum_result = ( + 4 * _subsum(digit_position, 1, precision) + - 2 * _subsum(digit_position, 4, precision) + - _subsum(digit_position, 5, precision) + - _subsum(digit_position, 6, precision) + ) + + # return the first hex digit of the fractional part of the result + return hex(int((sum_result % 1) * 16))[2:] + + +def _subsum( + digit_pos_to_extract: int, denominator_addend: int, precision: int +) -> float: + # only care about first digit of fractional part; don't need decimal + """ + Private helper function to implement the summation + functionality. + @param digit_pos_to_extract: digit position to extract + @param denominator_addend: added to denominator of fractions in the formula + @param precision: same as precision in main function + @return: floating-point number whose integer part is not important + """ + sum = 0.0 + for sum_index in range(digit_pos_to_extract + precision): + denominator = 8 * sum_index + denominator_addend + exponential_term = 0.0 + if sum_index < digit_pos_to_extract: + # if the exponential term is an integer and we mod it by the denominator + # before dividing, only the integer part of the sum will change; + # the fractional part will not + exponential_term = pow( + 16, digit_pos_to_extract - 1 - sum_index, denominator + ) + else: + exponential_term = pow(16, digit_pos_to_extract - 1 - sum_index) + sum += exponential_term / denominator + return sum + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 6e8c919a001d..07ee3b3df296 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -1,74 +1,80 @@ +"""Implementation of Basic Math in Python.""" import math -def primeFactors(n): + +def prime_factors(n: int) -> list: + """Find Prime Factors. + >>> prime_factors(100) + [2, 2, 5, 5] + """ pf = [] while n % 2 == 0: pf.append(2) n = int(n / 2) - - for i in range(3, int(math.sqrt(n))+1, 2): + for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: pf.append(i) n = int(n / i) - if n > 2: pf.append(n) - return pf -def numberOfDivisors(n): + +def number_of_divisors(n: int) -> int: + """Calculate Number of Divisors of an Integer. + >>> number_of_divisors(100) + 9 + """ div = 1 - temp = 1 while n % 2 == 0: temp += 1 n = int(n / 2) - div = div * (temp) - - for i in range(3, int(math.sqrt(n))+1, 2): + div *= temp + for i in range(3, int(math.sqrt(n)) + 1, 2): temp = 1 while n % i == 0: temp += 1 n = int(n / i) - div = div * (temp) - + div *= temp return div -def sumOfDivisors(n): + +def sum_of_divisors(n: int) -> int: + """Calculate Sum of Divisors. + >>> sum_of_divisors(100) + 217 + """ s = 1 - temp = 1 while n % 2 == 0: temp += 1 n = int(n / 2) if temp > 1: - s *= (2**temp - 1) / (2 - 1) - - for i in range(3, int(math.sqrt(n))+1, 2): + s *= (2 ** temp - 1) / (2 - 1) + for i in range(3, int(math.sqrt(n)) + 1, 2): temp = 1 while n % i == 0: temp += 1 n = int(n / i) if temp > 1: - s *= (i**temp - 1) / (i - 1) - - return s + s *= (i ** temp - 1) / (i - 1) + return int(s) -def eulerPhi(n): - l = primeFactors(n) - l = set(l) + +def euler_phi(n: int) -> int: + """Calculate Euler's Phi Function. + >>> euler_phi(100) + 40 + """ s = n - for x in l: - s *= (x - 1)/x - return s + for x in set(prime_factors(n)): + s *= (x - 1) / x + return int(s) + -def main(): - print(primeFactors(100)) - print(numberOfDivisors(100)) - print(sumOfDivisors(100)) - print(eulerPhi(100)) - -if __name__ == '__main__': - main() - - \ No newline at end of file +if __name__ == "__main__": + print(prime_factors(100)) + print(number_of_divisors(100)) + print(sum_of_divisors(100)) + print(euler_phi(100)) diff --git a/maths/binary_exp_mod.py b/maths/binary_exp_mod.py new file mode 100644 index 000000000000..67dd1e728b18 --- /dev/null +++ b/maths/binary_exp_mod.py @@ -0,0 +1,28 @@ +def bin_exp_mod(a, n, b): + """ + >>> bin_exp_mod(3, 4, 5) + 1 + >>> bin_exp_mod(7, 13, 10) + 7 + """ + # mod b + assert not (b == 0), "This cannot accept modulo that is == 0" + if n == 0: + return 1 + + if n % 2 == 1: + return (bin_exp_mod(a, n - 1, b) * a) % b + + r = bin_exp_mod(a, n / 2, b) + return (r * r) % b + + +if __name__ == "__main__": + try: + BASE = int(input("Enter Base : ").strip()) + POWER = int(input("Enter Power : ").strip()) + MODULO = int(input("Enter Modulo : ").strip()) + except ValueError: + print("Invalid literal for integer") + + print(bin_exp_mod(BASE, POWER, MODULO)) diff --git a/maths/binary_exponentiation.py b/maths/binary_exponentiation.py new file mode 100644 index 000000000000..8dda5245cf44 --- /dev/null +++ b/maths/binary_exponentiation.py @@ -0,0 +1,28 @@ +"""Binary Exponentiation.""" + +# Author : Junth Basnet +# Time Complexity : O(logn) + + +def binary_exponentiation(a, n): + + if n == 0: + return 1 + + elif n % 2 == 1: + return binary_exponentiation(a, n - 1) * a + + else: + b = binary_exponentiation(a, n / 2) + return b * b + + +if __name__ == "__main__": + try: + BASE = int(input("Enter Base : ").strip()) + POWER = int(input("Enter Power : ").strip()) + except ValueError: + print("Invalid literal for integer") + + RESULT = binary_exponentiation(BASE, POWER) + print(f"{BASE}^({POWER}) : {RESULT}") diff --git a/maths/binomial_coefficient.py b/maths/binomial_coefficient.py new file mode 100644 index 000000000000..4def041492f3 --- /dev/null +++ b/maths/binomial_coefficient.py @@ -0,0 +1,20 @@ +def binomial_coefficient(n, r): + """ + Find binomial coefficient using pascals triangle. + + >>> binomial_coefficient(10, 5) + 252 + """ + C = [0 for i in range(r + 1)] + # nc0 = 1 + C[0] = 1 + for i in range(1, n + 1): + # to compute current row from previous row. + j = min(i, r) + while j > 0: + C[j] += C[j - 1] + j -= 1 + return C[r] + + +print(binomial_coefficient(n=10, r=5)) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py new file mode 100644 index 000000000000..a74a5a7ed994 --- /dev/null +++ b/maths/binomial_distribution.py @@ -0,0 +1,40 @@ +"""For more information about the Binomial Distribution - + https://en.wikipedia.org/wiki/Binomial_distribution""" +from math import factorial + + +def binomial_distribution(successes: int, trials: int, prob: float) -> float: + """ + Return probability of k successes out of n tries, with p probability for one + success + + The function uses the factorial function in order to calculate the binomial + coefficient + + >>> binomial_distribution(3, 5, 0.7) + 0.30870000000000003 + >>> binomial_distribution (2, 4, 0.5) + 0.375 + """ + if successes > trials: + raise ValueError("""successes must be lower or equal to trials""") + if trials < 0 or successes < 0: + raise ValueError("the function is defined for non-negative integers") + if not isinstance(successes, int) or not isinstance(trials, int): + raise ValueError("the function is defined for non-negative integers") + if not 0 < prob < 1: + raise ValueError("prob has to be in range of 1 - 0") + probability = (prob ** successes) * ((1 - prob) ** (trials - successes)) + # Calculate the binomial coefficient: n! / k!(n-k)! + coefficient = float(factorial(trials)) + coefficient /= factorial(successes) * factorial(trials - successes) + return probability * coefficient + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + print("Probability of 2 successes out of 4 trails") + print("with probability of 0.75 is:", end=" ") + print(binomial_distribution(2, 4, 0.75)) diff --git a/maths/bisection.py b/maths/bisection.py new file mode 100644 index 000000000000..93cc2247b64e --- /dev/null +++ b/maths/bisection.py @@ -0,0 +1,63 @@ +""" +Given a function on floating number f(x) and two floating numbers ‘a’ and ‘b’ such that +f(a) * f(b) < 0 and f(x) is continuous in [a, b]. +Here f(x) represents algebraic or transcendental equation. +Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0) + +https://en.wikipedia.org/wiki/Bisection_method +""" + + +def equation(x: float) -> float: + """ + >>> equation(5) + -15 + >>> equation(0) + 10 + >>> equation(-5) + -15 + >>> equation(0.1) + 9.99 + >>> equation(-0.1) + 9.99 + """ + return 10 - x * x + + +def bisection(a: float, b: float) -> float: + """ + >>> bisection(-2, 5) + 3.1611328125 + >>> bisection(0, 6) + 3.158203125 + >>> bisection(2, 3) + Traceback (most recent call last): + ... + ValueError: Wrong space! + """ + # Bolzano theory in order to find if there is a root between a and b + if equation(a) * equation(b) >= 0: + raise ValueError("Wrong space!") + + c = a + while (b - a) >= 0.01: + # Find middle point + c = (a + b) / 2 + # Check if middle point is root + if equation(c) == 0.0: + break + # Decide the side to repeat the steps + if equation(c) * equation(a) < 0: + b = c + else: + a = c + return c + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + print(bisection(-2, 5)) + print(bisection(0, 6)) diff --git a/maths/ceil.py b/maths/ceil.py new file mode 100644 index 000000000000..97578265c1a9 --- /dev/null +++ b/maths/ceil.py @@ -0,0 +1,24 @@ +""" +https://en.wikipedia.org/wiki/Floor_and_ceiling_functions +""" + + +def ceil(x) -> int: + """ + Return the ceiling of x as an Integral. + + :param x: the number + :return: the smallest integer >= x. + + >>> import math + >>> all(ceil(n) == math.ceil(n) for n + ... in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000)) + True + """ + return int(x) if x - int(x) <= 0 else int(x) + 1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/chudnovsky_algorithm.py b/maths/chudnovsky_algorithm.py new file mode 100644 index 000000000000..aaee7462822e --- /dev/null +++ b/maths/chudnovsky_algorithm.py @@ -0,0 +1,60 @@ +from decimal import Decimal, getcontext +from math import ceil, factorial + + +def pi(precision: int) -> str: + """ + The Chudnovsky algorithm is a fast method for calculating the digits of PI, + based on Ramanujan’s PI formulae. + + https://en.wikipedia.org/wiki/Chudnovsky_algorithm + + PI = constant_term / ((multinomial_term * linear_term) / exponential_term) + where constant_term = 426880 * sqrt(10005) + + The linear_term and the exponential_term can be defined iteratively as follows: + L_k+1 = L_k + 545140134 where L_0 = 13591409 + X_k+1 = X_k * -262537412640768000 where X_0 = 1 + + The multinomial_term is defined as follows: + 6k! / ((3k)! * (k!) ^ 3) + where k is the k_th iteration. + + This algorithm correctly calculates around 14 digits of PI per iteration + + >>> pi(10) + '3.14159265' + >>> pi(100) + '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706' + >>> pi('hello') + Traceback (most recent call last): + ... + TypeError: Undefined for non-integers + >>> pi(-1) + Traceback (most recent call last): + ... + ValueError: Undefined for non-natural numbers + """ + + if not isinstance(precision, int): + raise TypeError("Undefined for non-integers") + elif precision < 1: + raise ValueError("Undefined for non-natural numbers") + + getcontext().prec = precision + num_iterations = ceil(precision / 14) + constant_term = 426880 * Decimal(10005).sqrt() + exponential_term = 1 + linear_term = 13591409 + partial_sum = Decimal(linear_term) + for k in range(1, num_iterations): + multinomial_term = factorial(6 * k) // (factorial(3 * k) * factorial(k) ** 3) + linear_term += 545140134 + exponential_term *= -262537412640768000 + partial_sum += Decimal(multinomial_term * linear_term) / exponential_term + return str(constant_term / partial_sum)[:-1] + + +if __name__ == "__main__": + n = 50 + print(f"The first {n} digits of pi is: {pi(n)}") diff --git a/maths/collatz_sequence.py b/maths/collatz_sequence.py new file mode 100644 index 000000000000..7b3636de69f4 --- /dev/null +++ b/maths/collatz_sequence.py @@ -0,0 +1,44 @@ +from __future__ import annotations + + +def collatz_sequence(n: int) -> list[int]: + """ + Collatz conjecture: start with any positive integer n. The next term is + obtained as follows: + If n term is even, the next term is: n / 2 . + If n is odd, the next term is: 3 * n + 1. + + The conjecture states the sequence will always reach 1 for any starting value n. + Example: + >>> collatz_sequence(2.1) + Traceback (most recent call last): + ... + Exception: Sequence only defined for natural numbers + >>> collatz_sequence(0) + Traceback (most recent call last): + ... + Exception: Sequence only defined for natural numbers + >>> collatz_sequence(43) # doctest: +NORMALIZE_WHITESPACE + [43, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56, 28, 14, 7, + 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] + """ + + if not isinstance(n, int) or n < 1: + raise Exception("Sequence only defined for natural numbers") + + sequence = [n] + while n != 1: + n = 3 * n + 1 if n & 1 else n // 2 + sequence.append(n) + return sequence + + +def main(): + n = 43 + sequence = collatz_sequence(n) + print(sequence) + print(f"collatz sequence from {n} took {len(sequence)} steps.") + + +if __name__ == "__main__": + main() diff --git a/maths/combinations.py b/maths/combinations.py new file mode 100644 index 000000000000..40f4f7a9f850 --- /dev/null +++ b/maths/combinations.py @@ -0,0 +1,58 @@ +""" +https://en.wikipedia.org/wiki/Combination +""" +from math import factorial + + +def combinations(n: int, k: int) -> int: + """ + Returns the number of different combinations of k length which can + be made from n values, where n >= k. + + Examples: + >>> combinations(10,5) + 252 + + >>> combinations(6,3) + 20 + + >>> combinations(20,5) + 15504 + + >>> combinations(52, 5) + 2598960 + + >>> combinations(0, 0) + 1 + + >>> combinations(-4, -5) + ... + Traceback (most recent call last): + ValueError: Please enter positive integers for n and k where n >= k + """ + + # If either of the conditions are true, the function is being asked + # to calculate a factorial of a negative number, which is not possible + if n < k or k < 0: + raise ValueError("Please enter positive integers for n and k where n >= k") + return int(factorial(n) / ((factorial(k)) * (factorial(n - k)))) + + +if __name__ == "__main__": + + print( + "\nThe number of five-card hands possible from a standard", + f"fifty-two card deck is: {combinations(52, 5)}", + ) + + print( + "\nIf a class of 40 students must be arranged into groups of", + f"4 for group projects, there are {combinations(40, 4)} ways", + "to arrange them.\n", + ) + + print( + "If 10 teams are competing in a Formula One race, there", + f"are {combinations(10, 3)} ways that first, second and", + "third place can be awarded.\n", + ) diff --git a/maths/decimal_isolate.py b/maths/decimal_isolate.py new file mode 100644 index 000000000000..0e3967a4671d --- /dev/null +++ b/maths/decimal_isolate.py @@ -0,0 +1,45 @@ +""" +Isolate the Decimal part of a Number +https://stackoverflow.com/questions/3886402/how-to-get-numbers-after-decimal-point +""" + + +def decimal_isolate(number, digitAmount): + + """ + Isolates the decimal part of a number. + If digitAmount > 0 round to that decimal place, else print the entire decimal. + >>> decimal_isolate(1.53, 0) + 0.53 + >>> decimal_isolate(35.345, 1) + 0.3 + >>> decimal_isolate(35.345, 2) + 0.34 + >>> decimal_isolate(35.345, 3) + 0.345 + >>> decimal_isolate(-14.789, 3) + -0.789 + >>> decimal_isolate(0, 2) + 0 + >>> decimal_isolate(-14.123, 1) + -0.1 + >>> decimal_isolate(-14.123, 2) + -0.12 + >>> decimal_isolate(-14.123, 3) + -0.123 + """ + if digitAmount > 0: + return round(number - int(number), digitAmount) + return number - int(number) + + +if __name__ == "__main__": + print(decimal_isolate(1.53, 0)) + print(decimal_isolate(35.345, 1)) + print(decimal_isolate(35.345, 2)) + print(decimal_isolate(35.345, 3)) + print(decimal_isolate(-14.789, 3)) + print(decimal_isolate(0, 2)) + print(decimal_isolate(-14.123, 1)) + print(decimal_isolate(-14.123, 2)) + print(decimal_isolate(-14.123, 3)) diff --git a/maths/entropy.py b/maths/entropy.py new file mode 100644 index 000000000000..43bb3860fc12 --- /dev/null +++ b/maths/entropy.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python3 + +""" +Implementation of entropy of information +https://en.wikipedia.org/wiki/Entropy_(information_theory) +""" +from __future__ import annotations + +import math +from collections import Counter +from string import ascii_lowercase + + +def calculate_prob(text: str) -> None: + """ + This method takes path and two dict as argument + and than calculates entropy of them. + :param dict: + :param dict: + :return: Prints + 1) Entropy of information based on 1 alphabet + 2) Entropy of information based on couples of 2 alphabet + 3) print Entropy of H(X n∣Xn−1) + + Text from random books. Also, random quotes. + >>> text = ("Behind Winston’s back the voice " + ... "from the telescreen was still " + ... "babbling and the overfulfilment") + >>> calculate_prob(text) + 4.0 + 6.0 + 2.0 + + >>> text = ("The Ministry of Truth—Minitrue, in Newspeak [Newspeak was the official" + ... "face in elegant lettering, the three") + >>> calculate_prob(text) + 4.0 + 5.0 + 1.0 + >>> text = ("Had repulsive dashwoods suspicion sincerity but advantage now him. " + ... "Remark easily garret nor nay. Civil those mrs enjoy shy fat merry. " + ... "You greatest jointure saw horrible. He private he on be imagine " + ... "suppose. Fertile beloved evident through no service elderly is. Blind " + ... "there if every no so at. Own neglected you preferred way sincerity " + ... "delivered his attempted. To of message cottage windows do besides " + ... "against uncivil. Delightful unreserved impossible few estimating " + ... "men favourable see entreaties. She propriety immediate was improving. " + ... "He or entrance humoured likewise moderate. Much nor game son say " + ... "feel. Fat make met can must form into gate. Me we offending prevailed " + ... "discovery.") + >>> calculate_prob(text) + 4.0 + 7.0 + 3.0 + """ + single_char_strings, two_char_strings = analyze_text(text) + my_alphas = list(" " + ascii_lowercase) + # what is our total sum of probabilities. + all_sum = sum(single_char_strings.values()) + + # one length string + my_fir_sum = 0 + # for each alpha we go in our dict and if it is in it we calculate entropy + for ch in my_alphas: + if ch in single_char_strings: + my_str = single_char_strings[ch] + prob = my_str / all_sum + my_fir_sum += prob * math.log2(prob) # entropy formula. + + # print entropy + print("{:.1f}".format(round(-1 * my_fir_sum))) + + # two len string + all_sum = sum(two_char_strings.values()) + my_sec_sum = 0 + # for each alpha (two in size) calculate entropy. + for ch0 in my_alphas: + for ch1 in my_alphas: + sequence = ch0 + ch1 + if sequence in two_char_strings: + my_str = two_char_strings[sequence] + prob = int(my_str) / all_sum + my_sec_sum += prob * math.log2(prob) + + # print second entropy + print("{:.1f}".format(round(-1 * my_sec_sum))) + + # print the difference between them + print("{:.1f}".format(round((-1 * my_sec_sum) - (-1 * my_fir_sum)))) + + +def analyze_text(text: str) -> tuple[dict, dict]: + """ + Convert text input into two dicts of counts. + The first dictionary stores the frequency of single character strings. + The second dictionary stores the frequency of two character strings. + """ + single_char_strings = Counter() # type: ignore + two_char_strings = Counter() # type: ignore + single_char_strings[text[-1]] += 1 + + # first case when we have space at start. + two_char_strings[" " + text[0]] += 1 + for i in range(0, len(text) - 1): + single_char_strings[text[i]] += 1 + two_char_strings[text[i : i + 2]] += 1 + return single_char_strings, two_char_strings + + +def main(): + import doctest + + doctest.testmod() + # text = ( + # "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark " + # "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest " + # "jointure saw horrible. He private he on be imagine suppose. Fertile " + # "beloved evident through no service elderly is. Blind there if every no so " + # "at. Own neglected you preferred way sincerity delivered his attempted. To " + # "of message cottage windows do besides against uncivil. Delightful " + # "unreserved impossible few estimating men favourable see entreaties. She " + # "propriety immediate was improving. He or entrance humoured likewise " + # "moderate. Much nor game son say feel. Fat make met can must form into " + # "gate. Me we offending prevailed discovery. " + # ) + + # calculate_prob(text) + + +if __name__ == "__main__": + main() diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py new file mode 100644 index 000000000000..6e0da6370219 --- /dev/null +++ b/maths/euclidean_distance.py @@ -0,0 +1,62 @@ +from typing import Iterable, Union + +import numpy as np + +Vector = Union[Iterable[float], Iterable[int], np.ndarray] +VectorOut = Union[np.float64, int, float] + + +def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: + """ + Calculate the distance between the two endpoints of two vectors. + A vector is defined as a list, tuple, or numpy 1D array. + >>> euclidean_distance((0, 0), (2, 2)) + 2.8284271247461903 + >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) + 3.4641016151377544 + >>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])) + 8.0 + >>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]) + 8.0 + """ + return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2)) + + +def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: + """ + Calculate the distance between the two endpoints of two vectors without numpy. + A vector is defined as a list, tuple, or numpy 1D array. + >>> euclidean_distance_no_np((0, 0), (2, 2)) + 2.8284271247461903 + >>> euclidean_distance_no_np([1, 2, 3, 4], [5, 6, 7, 8]) + 8.0 + """ + return sum((v1 - v2) ** 2 for v1, v2 in zip(vector_1, vector_2)) ** (1 / 2) + + +if __name__ == "__main__": + + def benchmark() -> None: + """ + Benchmarks + """ + from timeit import timeit + + print("Without Numpy") + print( + timeit( + "euclidean_distance_no_np([1, 2, 3], [4, 5, 6])", + number=10000, + globals=globals(), + ) + ) + print("With Numpy") + print( + timeit( + "euclidean_distance([1, 2, 3], [4, 5, 6])", + number=10000, + globals=globals(), + ) + ) + + benchmark() diff --git a/maths/eulers_totient.py b/maths/eulers_totient.py new file mode 100644 index 000000000000..6a35e69bde0b --- /dev/null +++ b/maths/eulers_totient.py @@ -0,0 +1,45 @@ +# Eulers Totient function finds the number of relative primes of a number n from 1 to n +def totient(n: int) -> list: + is_prime = [True for i in range(n + 1)] + totients = [i - 1 for i in range(n + 1)] + primes = [] + for i in range(2, n + 1): + if is_prime[i]: + primes.append(i) + for j in range(0, len(primes)): + if i * primes[j] >= n: + break + is_prime[i * primes[j]] = False + + if i % primes[j] == 0: + totients[i * primes[j]] = totients[i] * primes[j] + break + + totients[i * primes[j]] = totients[i] * (primes[j] - 1) + + return totients + + +def test_totient() -> None: + """ + >>> n = 10 + >>> totient_calculation = totient(n) + >>> for i in range(1, n): + ... print(f"{i} has {totient_calculation[i]} relative primes.") + 1 has 0 relative primes. + 2 has 1 relative primes. + 3 has 2 relative primes. + 4 has 2 relative primes. + 5 has 4 relative primes. + 6 has 2 relative primes. + 7 has 6 relative primes. + 8 has 4 relative primes. + 9 has 6 relative primes. + """ + pass + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/explicit_euler.py b/maths/explicit_euler.py new file mode 100644 index 000000000000..7c780198602b --- /dev/null +++ b/maths/explicit_euler.py @@ -0,0 +1,40 @@ +import numpy as np + + +def explicit_euler(ode_func, y0, x0, step_size, x_end): + """ + Calculate numeric solution at each step to an ODE using Euler's Method + + https://en.wikipedia.org/wiki/Euler_method + + Arguments: + ode_func -- The ode as a function of x and y + y0 -- the initial value for y + x0 -- the initial value for x + stepsize -- the increment value for x + x_end -- the end value for x + + >>> # the exact solution is math.exp(x) + >>> def f(x, y): + ... return y + >>> y0 = 1 + >>> y = explicit_euler(f, y0, 0.0, 0.01, 5) + >>> y[-1] + 144.77277243257308 + """ + N = int(np.ceil((x_end - x0) / step_size)) + y = np.zeros((N + 1,)) + y[0] = y0 + x = x0 + + for k in range(N): + y[k + 1] = y[k] + step_size * ode_func(x, y[k]) + x += step_size + + return y + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/extended_euclidean_algorithm.py b/maths/extended_euclidean_algorithm.py index f5a3cc88e474..e7087636ce09 100644 --- a/maths/extended_euclidean_algorithm.py +++ b/maths/extended_euclidean_algorithm.py @@ -1,51 +1,85 @@ +""" +Extended Euclidean Algorithm. + +Finds 2 numbers a and b such that it satisfies +the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity) + +https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm +""" + # @Author: S. Sharma # @Date: 2019-02-25T12:08:53-06:00 # @Email: silentcat@protonmail.com -# @Last modified by: silentcat -# @Last modified time: 2019-02-26T07:07:38-06:00 +# @Last modified by: pikulet +# @Last modified time: 2020-10-02 import sys +from typing import Tuple + + +def extended_euclidean_algorithm(a: int, b: int) -> Tuple[int, int]: + """ + Extended Euclidean Algorithm. + + Finds 2 numbers a and b such that it satisfies + the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity) + + >>> extended_euclidean_algorithm(1, 24) + (1, 0) + + >>> extended_euclidean_algorithm(8, 14) + (2, -1) + + >>> extended_euclidean_algorithm(240, 46) + (-9, 47) + + >>> extended_euclidean_algorithm(1, -4) + (1, 0) + + >>> extended_euclidean_algorithm(-2, -4) + (-1, 0) + + >>> extended_euclidean_algorithm(0, -4) + (0, -1) + + >>> extended_euclidean_algorithm(2, 0) + (1, 0) + + """ + # base cases + if abs(a) == 1: + return a, 0 + elif abs(b) == 1: + return 0, b + + old_remainder, remainder = a, b + old_coeff_a, coeff_a = 1, 0 + old_coeff_b, coeff_b = 0, 1 + + while remainder != 0: + quotient = old_remainder // remainder + old_remainder, remainder = remainder, old_remainder - quotient * remainder + old_coeff_a, coeff_a = coeff_a, old_coeff_a - quotient * coeff_a + old_coeff_b, coeff_b = coeff_b, old_coeff_b - quotient * coeff_b + + # sign correction for negative numbers + if a < 0: + old_coeff_a = -old_coeff_a + if b < 0: + old_coeff_b = -old_coeff_b + + return old_coeff_a, old_coeff_b -# Finds 2 numbers a and b such that it satisfies -# the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity) -def extended_euclidean_algorithm(m, n): - a = 0; aprime = 1; b = 1; bprime = 0 - q = 0; r = 0 - if m > n: - c = m; d = n - else: - c = n; d = m - - while True: - q = int(c / d) - r = c % d - if r == 0: - break - c = d - d = r - - t = aprime - aprime = a - a = t - q*a - - t = bprime - bprime = b - b = t - q*b - - pair = None - if m > n: - pair = (a,b) - else: - pair = (b,a) - return pair def main(): + """Call Extended Euclidean Algorithm.""" if len(sys.argv) < 3: - print('2 integer arguments required') + print("2 integer arguments required") exit(1) - m = int(sys.argv[1]) - n = int(sys.argv[2]) - print(extended_euclidean_algorithm(m, n)) + a = int(sys.argv[1]) + b = int(sys.argv[2]) + print(extended_euclidean_algorithm(a, b)) + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py new file mode 100644 index 000000000000..64314790c11c --- /dev/null +++ b/maths/factorial_iterative.py @@ -0,0 +1,30 @@ +# factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial + + +def factorial(n: int) -> int: + """ + >>> import math + >>> all(factorial(i) == math.factorial(i) for i in range(20)) + True + >>> factorial(0.1) + Traceback (most recent call last): + ... + ValueError: factorial() only accepts integral values + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: factorial() not defined for negative values + """ + if n != int(n): + raise ValueError("factorial() only accepts integral values") + if n < 0: + raise ValueError("factorial() not defined for negative values") + value = 1 + for i in range(1, n + 1): + value *= i + return value + + +if __name__ == "__main__": + n = int(input("Enter a positive integer: ").strip() or 0) + print(f"factorial{n} is {factorial(n)}") diff --git a/maths/factorial_python.py b/maths/factorial_python.py index 376983e08dab..46688261af56 100644 --- a/maths/factorial_python.py +++ b/maths/factorial_python.py @@ -1,19 +1,34 @@ -# Python program to find the factorial of a number provided by the user. +def factorial(input_number: int) -> int: + """ + Calculate the factorial of specified number -# change the value for a different result -num = 10 + >>> factorial(1) + 1 + >>> factorial(6) + 720 + >>> factorial(0) + 1 + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: factorial() not defined for negative values + >>> factorial(0.1) + Traceback (most recent call last): + ... + ValueError: factorial() only accepts integral values + """ -# uncomment to take input from the user -#num = int(input("Enter a number: ")) + if input_number < 0: + raise ValueError("factorial() not defined for negative values") + if not isinstance(input_number, int): + raise ValueError("factorial() only accepts integral values") + result = 1 + for i in range(1, input_number): + result = result * (i + 1) + return result -factorial = 1 -# check if the number is negative, positive or zero -if num < 0: - print("Sorry, factorial does not exist for negative numbers") -elif num == 0: - print("The factorial of 0 is 1") -else: - for i in range(1,num + 1): - factorial = factorial*i - print("The factorial of",num,"is",factorial) +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py index 41391a2718f6..137112738905 100644 --- a/maths/factorial_recursive.py +++ b/maths/factorial_recursive.py @@ -1,13 +1,28 @@ -def fact(n): - """ - Return 1, if n is 1 or below, - otherwise, return n * fact(n-1). - """ - return 1 if n <= 1 else n * fact(n-1) - -""" -Shown factorial for i, -where i ranges from 1 to 20. -""" -for i in range(1,21): - print(i, ": ", fact(i), sep='') +def factorial(n: int) -> int: + """ + Calculate the factorial of a positive integer + https://en.wikipedia.org/wiki/Factorial + + >>> import math + >>> all(factorial(i) == math.factorial(i) for i in range(20)) + True + >>> factorial(0.1) + Traceback (most recent call last): + ... + ValueError: factorial() only accepts integral values + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: factorial() not defined for negative values + """ + if not isinstance(n, int): + raise ValueError("factorial() only accepts integral values") + if n < 0: + raise ValueError("factorial() not defined for negative values") + return 1 if n == 0 or n == 1 else n * factorial(n - 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/factors.py b/maths/factors.py new file mode 100644 index 000000000000..e2fdc4063a13 --- /dev/null +++ b/maths/factors.py @@ -0,0 +1,18 @@ +def factors_of_a_number(num: int) -> list: + """ + >>> factors_of_a_number(1) + [1] + >>> factors_of_a_number(5) + [1, 5] + >>> factors_of_a_number(24) + [1, 2, 3, 4, 6, 8, 12, 24] + >>> factors_of_a_number(-24) + [] + """ + return [i for i in range(1, num + 1) if num % i == 0] + + +if __name__ == "__main__": + num = int(input("Enter a number to find its factors: ")) + factors = factors_of_a_number(num) + print(f"{num} has {len(factors)} factors: {', '.join(str(f) for f in factors)}") diff --git a/maths/fermat_little_theorem.py b/maths/fermat_little_theorem.py new file mode 100644 index 000000000000..73af3e28c618 --- /dev/null +++ b/maths/fermat_little_theorem.py @@ -0,0 +1,31 @@ +# Python program to show the usage of Fermat's little theorem in a division +# According to Fermat's little theorem, (a / b) mod p always equals +# a * (b ^ (p - 2)) mod p +# Here we assume that p is a prime number, b divides a, and p doesn't divide b +# Wikipedia reference: https://en.wikipedia.org/wiki/Fermat%27s_little_theorem + + +def binary_exponentiation(a, n, mod): + + if n == 0: + return 1 + + elif n % 2 == 1: + return (binary_exponentiation(a, n - 1, mod) * a) % mod + + else: + b = binary_exponentiation(a, n / 2, mod) + return (b * b) % mod + + +# a prime number +p = 701 + +a = 1000000000 +b = 10 + +# using binary exponentiation function, O(log(p)): +print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) + +# using Python operators: +print((a / b) % p == (a * b ** (p - 2)) % p) diff --git a/maths/fibonacci.py b/maths/fibonacci.py new file mode 100644 index 000000000000..e6519035401e --- /dev/null +++ b/maths/fibonacci.py @@ -0,0 +1,130 @@ +# fibonacci.py +""" +1. Calculates the iterative fibonacci sequence + +2. Calculates the fibonacci sequence with a formula + an = [ Phin - (phi)n ]/Sqrt[5] + reference-->Su, Francis E., et al. "Fibonacci Number Formula." Math Fun Facts. + +""" +import functools +import math +import time +from decimal import Decimal, getcontext + +getcontext().prec = 100 + + +def timer_decorator(func): + @functools.wraps(func) + def timer_wrapper(*args, **kwargs): + start = time.time() + func(*args, **kwargs) + end = time.time() + if int(end - start) > 0: + print(f"Run time for {func.__name__}: {(end - start):0.2f}s") + else: + print(f"Run time for {func.__name__}: {(end - start)*1000:0.2f}ms") + return func(*args, **kwargs) + + return timer_wrapper + + +# define Python user-defined exceptions +class Error(Exception): + """Base class for other exceptions""" + + +class ValueTooLargeError(Error): + """Raised when the input value is too large""" + + +class ValueTooSmallError(Error): + """Raised when the input value is not greater than one""" + + +class ValueLessThanZero(Error): + """Raised when the input value is less than zero""" + + +def _check_number_input(n, min_thresh, max_thresh=None): + """ + :param n: single integer + :type n: int + :param min_thresh: min threshold, single integer + :type min_thresh: int + :param max_thresh: max threshold, single integer + :type max_thresh: int + :return: boolean + """ + try: + if n >= min_thresh and max_thresh is None: + return True + elif min_thresh <= n <= max_thresh: + return True + elif n < 0: + raise ValueLessThanZero + elif n < min_thresh: + raise ValueTooSmallError + elif n > max_thresh: + raise ValueTooLargeError + except ValueLessThanZero: + print("Incorrect Input: number must not be less than 0") + except ValueTooSmallError: + print( + f"Incorrect Input: input number must be > {min_thresh} for the recursive " + "calculation" + ) + except ValueTooLargeError: + print( + f"Incorrect Input: input number must be < {max_thresh} for the recursive " + "calculation" + ) + return False + + +@timer_decorator +def fib_iterative(n): + """ + :param n: calculate Fibonacci to the nth integer + :type n:int + :return: Fibonacci sequence as a list + """ + n = int(n) + if _check_number_input(n, 2): + seq_out = [0, 1] + a, b = 0, 1 + for _ in range(n - len(seq_out)): + a, b = b, a + b + seq_out.append(b) + return seq_out + + +@timer_decorator +def fib_formula(n): + """ + :param n: calculate Fibonacci to the nth integer + :type n:int + :return: Fibonacci sequence as a list + """ + seq_out = [0, 1] + n = int(n) + if _check_number_input(n, 2, 1000000): + sqrt = Decimal(math.sqrt(5)) + phi_1 = Decimal(1 + sqrt) / Decimal(2) + phi_2 = Decimal(1 - sqrt) / Decimal(2) + for i in range(2, n): + temp_out = ((phi_1 ** Decimal(i)) - (phi_2 ** Decimal(i))) * ( + Decimal(sqrt) ** Decimal(-1) + ) + seq_out.append(int(temp_out)) + return seq_out + + +if __name__ == "__main__": + num = 20 + # print(f'{fib_recursive(num)}\n') + # print(f'{fib_iterative(num)}\n') + # print(f'{fib_formula(num)}\n') + fib_iterative(num) + fib_formula(num) diff --git a/maths/fibonacci_sequence_recursion.py b/maths/fibonacci_sequence_recursion.py index 9190e7fc7a40..794b9fc0bd3a 100644 --- a/maths/fibonacci_sequence_recursion.py +++ b/maths/fibonacci_sequence_recursion.py @@ -1,21 +1,22 @@ # Fibonacci Sequence Using Recursion -def recur_fibo(n): - if n <= 1: - return n - else: - (recur_fibo(n-1) + recur_fibo(n-2)) -def isPositiveInteger(limit): - return limit >= 0 +def recur_fibo(n: int) -> int: + """ + >>> [recur_fibo(i) for i in range(12)] + [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] + """ + return n if n <= 1 else recur_fibo(n - 1) + recur_fibo(n - 2) + -def main(): +def main() -> None: limit = int(input("How many terms to include in fibonacci series: ")) - if isPositiveInteger(limit): - print("The first {limit} terms of the fibonacci series are as follows:") + if limit > 0: + print(f"The first {limit} terms of the fibonacci series are as follows:") print([recur_fibo(n) for n in range(limit)]) else: print("Please enter a positive integer: ") -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/maths/find_lcm.py b/maths/find_lcm.py deleted file mode 100644 index 126242699ab7..000000000000 --- a/maths/find_lcm.py +++ /dev/null @@ -1,18 +0,0 @@ -def find_lcm(num_1, num_2): - max = num_1 if num_1 > num_2 else num_2 - lcm = max - while (True): - if ((lcm % num_1 == 0) and (lcm % num_2 == 0)): - break - lcm += max - return lcm - - -def main(): - num_1 = 12 - num_2 = 76 - print(find_lcm(num_1, num_2)) - - -if __name__ == '__main__': - main() diff --git a/maths/find_max.py b/maths/find_max.py new file mode 100644 index 000000000000..4d92e37eb2e1 --- /dev/null +++ b/maths/find_max.py @@ -0,0 +1,25 @@ +# NguyenU + + +def find_max(nums): + """ + >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]): + ... find_max(nums) == max(nums) + True + True + True + True + """ + max_num = nums[0] + for x in nums: + if x > max_num: + max_num = x + return max_num + + +def main(): + print(find_max([2, 4, 9, 7, 19, 94, 5])) # 94 + + +if __name__ == "__main__": + main() diff --git a/maths/find_max_recursion.py b/maths/find_max_recursion.py new file mode 100644 index 000000000000..03fb81950dcb --- /dev/null +++ b/maths/find_max_recursion.py @@ -0,0 +1,25 @@ +# Divide and Conquer algorithm +def find_max(nums, left, right): + """ + find max value in list + :param nums: contains elements + :param left: index of first element + :param right: index of last element + :return: max in nums + + >>> nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] + >>> find_max(nums, 0, len(nums) - 1) == max(nums) + True + """ + if left == right: + return nums[left] + mid = (left + right) >> 1 # the middle + left_max = find_max(nums, left, mid) # find max in range[left, mid] + right_max = find_max(nums, mid + 1, right) # find max in range[mid + 1, right] + + return left_max if left_max >= right_max else right_max + + +if __name__ == "__main__": + nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] + assert find_max(nums, 0, len(nums) - 1) == 10 diff --git a/maths/find_min.py b/maths/find_min.py new file mode 100644 index 000000000000..2af2e44ba353 --- /dev/null +++ b/maths/find_min.py @@ -0,0 +1,26 @@ +def find_min(nums): + """ + Find Minimum Number in a List + :param nums: contains elements + :return: min number in list + + >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]): + ... find_min(nums) == min(nums) + True + True + True + True + """ + min_num = nums[0] + for num in nums: + if min_num > num: + min_num = num + return min_num + + +def main(): + assert find_min([0, 1, 2, 3, 4, 5, -3, 24, -56]) == -56 + + +if __name__ == "__main__": + main() diff --git a/maths/find_min_recursion.py b/maths/find_min_recursion.py new file mode 100644 index 000000000000..4488967cc57a --- /dev/null +++ b/maths/find_min_recursion.py @@ -0,0 +1,25 @@ +# Divide and Conquer algorithm +def find_min(nums, left, right): + """ + find min value in list + :param nums: contains elements + :param left: index of first element + :param right: index of last element + :return: min in nums + + >>> nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] + >>> find_min(nums, 0, len(nums) - 1) == min(nums) + True + """ + if left == right: + return nums[left] + mid = (left + right) >> 1 # the middle + left_min = find_min(nums, left, mid) # find min in range[left, mid] + right_min = find_min(nums, mid + 1, right) # find min in range[mid + 1, right] + + return left_min if left_min <= right_min else right_min + + +if __name__ == "__main__": + nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] + assert find_min(nums, 0, len(nums) - 1) == 1 diff --git a/maths/floor.py b/maths/floor.py new file mode 100644 index 000000000000..482250f5e59e --- /dev/null +++ b/maths/floor.py @@ -0,0 +1,22 @@ +""" +https://en.wikipedia.org/wiki/Floor_and_ceiling_functions +""" + + +def floor(x) -> int: + """ + Return the floor of x as an Integral. + :param x: the number + :return: the largest integer <= x. + >>> import math + >>> all(floor(n) == math.floor(n) for n + ... in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000)) + True + """ + return int(x) if x - int(x) >= 0 else int(x) - 1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/gamma.py b/maths/gamma.py new file mode 100644 index 000000000000..69cd819ef186 --- /dev/null +++ b/maths/gamma.py @@ -0,0 +1,63 @@ +import math + +from numpy import inf +from scipy.integrate import quad + + +def gamma(num: float) -> float: + """ + https://en.wikipedia.org/wiki/Gamma_function + In mathematics, the gamma function is one commonly + used extension of the factorial function to complex numbers. + The gamma function is defined for all complex numbers except the non-positive + integers + + + >>> gamma(-1) + Traceback (most recent call last): + ... + ValueError: math domain error + + + + >>> gamma(0) + Traceback (most recent call last): + ... + ValueError: math domain error + + + >>> gamma(9) + 40320.0 + + >>> from math import gamma as math_gamma + >>> all(.99999999 < gamma(i) / math_gamma(i) <= 1.000000001 + ... for i in range(1, 50)) + True + + + >>> from math import gamma as math_gamma + >>> gamma(-1)/math_gamma(-1) <= 1.000000001 + Traceback (most recent call last): + ... + ValueError: math domain error + + + >>> from math import gamma as math_gamma + >>> gamma(3.3) - math_gamma(3.3) <= 0.00000001 + True + """ + + if num <= 0: + raise ValueError("math domain error") + + return quad(integrand, 0, inf, args=(num))[0] + + +def integrand(x: float, z: float) -> float: + return math.pow(x, z - 1) * math.exp(-x) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/gaussian.py b/maths/gaussian.py new file mode 100644 index 000000000000..a5dba50a927d --- /dev/null +++ b/maths/gaussian.py @@ -0,0 +1,61 @@ +""" +Reference: https://en.wikipedia.org/wiki/Gaussian_function +""" +from numpy import exp, pi, sqrt + + +def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int: + """ + >>> gaussian(1) + 0.24197072451914337 + + >>> gaussian(24) + 3.342714441794458e-126 + + >>> gaussian(1, 4, 2) + 0.06475879783294587 + + >>> gaussian(1, 5, 3) + 0.05467002489199788 + + Supports NumPy Arrays + Use numpy.meshgrid with this to generate gaussian blur on images. + >>> import numpy as np + >>> x = np.arange(15) + >>> gaussian(x) + array([3.98942280e-01, 2.41970725e-01, 5.39909665e-02, 4.43184841e-03, + 1.33830226e-04, 1.48671951e-06, 6.07588285e-09, 9.13472041e-12, + 5.05227108e-15, 1.02797736e-18, 7.69459863e-23, 2.11881925e-27, + 2.14638374e-32, 7.99882776e-38, 1.09660656e-43]) + + >>> gaussian(15) + 5.530709549844416e-50 + + >>> gaussian([1,2, 'string']) + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for -: 'list' and 'float' + + >>> gaussian('hello world') + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for -: 'str' and 'float' + + >>> gaussian(10**234) # doctest: +IGNORE_EXCEPTION_DETAIL + Traceback (most recent call last): + ... + OverflowError: (34, 'Result too large') + + >>> gaussian(10**-326) + 0.3989422804014327 + + >>> gaussian(2523, mu=234234, sigma=3425) + 0.0 + """ + return 1 / sqrt(2 * pi * sigma ** 2) * exp(-((x - mu) ** 2) / (2 * sigma ** 2)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/greater_common_divisor.py b/maths/greater_common_divisor.py deleted file mode 100644 index 15adaca1fb8d..000000000000 --- a/maths/greater_common_divisor.py +++ /dev/null @@ -1,15 +0,0 @@ -# Greater Common Divisor - https://en.wikipedia.org/wiki/Greatest_common_divisor -def gcd(a, b): - return b if a == 0 else gcd(b % a, a) - -def main(): - try: - nums = input("Enter two Integers separated by comma (,): ").split(',') - num1 = int(nums[0]); num2 = int(nums[1]) - except (IndexError, UnboundLocalError, ValueError): - print("Wrong Input") - print(f"gcd({num1}, {num2}) = {gcd(num1, num2)}") - -if __name__ == '__main__': - main() - diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py new file mode 100644 index 000000000000..a2174a8eb74a --- /dev/null +++ b/maths/greatest_common_divisor.py @@ -0,0 +1,77 @@ +""" +Greatest Common Divisor. + +Wikipedia reference: https://en.wikipedia.org/wiki/Greatest_common_divisor + +gcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b) by definition of divisibility +""" + + +def greatest_common_divisor(a: int, b: int) -> int: + """ + Calculate Greatest Common Divisor (GCD). + >>> greatest_common_divisor(24, 40) + 8 + >>> greatest_common_divisor(1, 1) + 1 + >>> greatest_common_divisor(1, 800) + 1 + >>> greatest_common_divisor(11, 37) + 1 + >>> greatest_common_divisor(3, 5) + 1 + >>> greatest_common_divisor(16, 4) + 4 + >>> greatest_common_divisor(-3, 9) + 3 + >>> greatest_common_divisor(9, -3) + 3 + >>> greatest_common_divisor(3, -9) + 3 + >>> greatest_common_divisor(-3, -9) + 3 + """ + return abs(b) if a == 0 else greatest_common_divisor(b % a, a) + + +def gcd_by_iterative(x: int, y: int) -> int: + """ + Below method is more memory efficient because it does not create additional + stack frames for recursive functions calls (as done in the above method). + >>> gcd_by_iterative(24, 40) + 8 + >>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40) + True + >>> gcd_by_iterative(-3, -9) + 3 + >>> gcd_by_iterative(3, -9) + 3 + >>> gcd_by_iterative(1, -800) + 1 + >>> gcd_by_iterative(11, 37) + 1 + """ + while y: # --> when y=0 then loop will terminate and return x as final GCD. + x, y = y, x % y + return abs(x) + + +def main(): + """ + Call Greatest Common Divisor function. + """ + try: + nums = input("Enter two integers separated by comma (,): ").split(",") + num_1 = int(nums[0]) + num_2 = int(nums[1]) + print( + f"greatest_common_divisor({num_1}, {num_2}) = " + f"{greatest_common_divisor(num_1, num_2)}" + ) + print(f"By iterative gcd({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}") + except (IndexError, UnboundLocalError, ValueError): + print("Wrong input") + + +if __name__ == "__main__": + main() diff --git a/maths/hardy_ramanujanalgo.py b/maths/hardy_ramanujanalgo.py new file mode 100644 index 000000000000..90e4913c70a7 --- /dev/null +++ b/maths/hardy_ramanujanalgo.py @@ -0,0 +1,45 @@ +# This theorem states that the number of prime factors of n +# will be approximately log(log(n)) for most natural numbers n + +import math + + +def exactPrimeFactorCount(n): + """ + >>> exactPrimeFactorCount(51242183) + 3 + """ + count = 0 + if n % 2 == 0: + count += 1 + while n % 2 == 0: + n = int(n / 2) + # the n input value must be odd so that + # we can skip one element (ie i += 2) + + i = 3 + + while i <= int(math.sqrt(n)): + if n % i == 0: + count += 1 + while n % i == 0: + n = int(n / i) + i = i + 2 + + # this condition checks the prime + # number n is greater than 2 + + if n > 2: + count += 1 + return count + + +if __name__ == "__main__": + n = 51242183 + print(f"The number of distinct prime factors is/are {exactPrimeFactorCount(n)}") + print("The value of log(log(n)) is {:.4f}".format(math.log(math.log(n)))) + + """ + The number of distinct prime factors is/are 3 + The value of log(log(n)) is 2.8765 + """ diff --git a/maths/images/__init__.py b/maths/images/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/maths/images/gaussian.png b/maths/images/gaussian.png new file mode 100644 index 000000000000..eb007c7e21b2 Binary files /dev/null and b/maths/images/gaussian.png differ diff --git a/maths/is_square_free.py b/maths/is_square_free.py new file mode 100644 index 000000000000..8d83d95ffb67 --- /dev/null +++ b/maths/is_square_free.py @@ -0,0 +1,39 @@ +""" +References: wikipedia:square free number +python/black : True +flake8 : True +""" +from __future__ import annotations + + +def is_square_free(factors: list[int]) -> bool: + """ + # doctest: +NORMALIZE_WHITESPACE + This functions takes a list of prime factors as input. + returns True if the factors are square free. + >>> is_square_free([1, 1, 2, 3, 4]) + False + + These are wrong but should return some value + it simply checks for repition in the numbers. + >>> is_square_free([1, 3, 4, 'sd', 0.0]) + True + + >>> is_square_free([1, 0.5, 2, 0.0]) + True + >>> is_square_free([1, 2, 2, 5]) + False + >>> is_square_free('asd') + True + >>> is_square_free(24) + Traceback (most recent call last): + ... + TypeError: 'int' object is not iterable + """ + return len(set(factors)) == len(factors) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/jaccard_similarity.py b/maths/jaccard_similarity.py new file mode 100644 index 000000000000..4f24d308f340 --- /dev/null +++ b/maths/jaccard_similarity.py @@ -0,0 +1,80 @@ +""" +The Jaccard similarity coefficient is a commonly used indicator of the +similarity between two sets. Let U be a set and A and B be subsets of U, +then the Jaccard index/similarity is defined to be the ratio of the number +of elements of their intersection and the number of elements of their union. + +Inspired from Wikipedia and +the book Mining of Massive Datasets [MMDS 2nd Edition, Chapter 3] + +https://en.wikipedia.org/wiki/Jaccard_index +https://mmds.org + +Jaccard similarity is widely used with MinHashing. +""" + + +def jaccard_similariy(setA, setB, alternativeUnion=False): + """ + Finds the jaccard similarity between two sets. + Essentially, its intersection over union. + + The alternative way to calculate this is to take union as sum of the + number of items in the two sets. This will lead to jaccard similarity + of a set with itself be 1/2 instead of 1. [MMDS 2nd Edition, Page 77] + + Parameters: + :setA (set,list,tuple): A non-empty set/list + :setB (set,list,tuple): A non-empty set/list + :alternativeUnion (boolean): If True, use sum of number of + items as union + + Output: + (float) The jaccard similarity between the two sets. + + Examples: + >>> setA = {'a', 'b', 'c', 'd', 'e'} + >>> setB = {'c', 'd', 'e', 'f', 'h', 'i'} + >>> jaccard_similariy(setA,setB) + 0.375 + + >>> jaccard_similariy(setA,setA) + 1.0 + + >>> jaccard_similariy(setA,setA,True) + 0.5 + + >>> setA = ['a', 'b', 'c', 'd', 'e'] + >>> setB = ('c', 'd', 'e', 'f', 'h', 'i') + >>> jaccard_similariy(setA,setB) + 0.375 + """ + + if isinstance(setA, set) and isinstance(setB, set): + + intersection = len(setA.intersection(setB)) + + if alternativeUnion: + union = len(setA) + len(setB) + else: + union = len(setA.union(setB)) + + return intersection / union + + if isinstance(setA, (list, tuple)) and isinstance(setB, (list, tuple)): + + intersection = [element for element in setA if element in setB] + + if alternativeUnion: + union = len(setA) + len(setB) + else: + union = setA + [element for element in setB if element not in setA] + + return len(intersection) / len(union) + + +if __name__ == "__main__": + + setA = {"a", "b", "c", "d", "e"} + setB = {"c", "d", "e", "f", "h", "i"} + print(jaccard_similariy(setA, setB)) diff --git a/maths/kadanes.py b/maths/kadanes.py new file mode 100644 index 000000000000..d239d4a2589b --- /dev/null +++ b/maths/kadanes.py @@ -0,0 +1,65 @@ +""" +Kadane's algorithm to get maximum subarray sum +https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d +https://en.wikipedia.org/wiki/Maximum_subarray_problem +""" +test_data: tuple = ([-2, -8, -9], [2, 8, 9], [-1, 0, 1], [0, 0], []) + + +def negative_exist(arr: list) -> int: + """ + >>> negative_exist([-2,-8,-9]) + -2 + >>> [negative_exist(arr) for arr in test_data] + [-2, 0, 0, 0, 0] + """ + arr = arr or [0] + max = arr[0] + for i in arr: + if i >= 0: + return 0 + elif max <= i: + max = i + return max + + +def kadanes(arr: list) -> int: + """ + If negative_exist() returns 0 than this function will execute + else it will return the value return by negative_exist function + + For example: arr = [2, 3, -9, 8, -2] + Initially we set value of max_sum to 0 and max_till_element to 0 than when + max_sum is less than max_till particular element it will assign that value to + max_sum and when value of max_till_sum is less than 0 it will assign 0 to i + and after that whole process, return the max_sum + So the output for above arr is 8 + + >>> kadanes([2, 3, -9, 8, -2]) + 8 + >>> [kadanes(arr) for arr in test_data] + [-2, 19, 1, 0, 0] + """ + max_sum = negative_exist(arr) + if max_sum < 0: + return max_sum + + max_sum = 0 + max_till_element = 0 + + for i in arr: + max_till_element += i + if max_sum <= max_till_element: + max_sum = max_till_element + if max_till_element < 0: + max_till_element = 0 + return max_sum + + +if __name__ == "__main__": + try: + print("Enter integer values sepatated by spaces") + arr = [int(x) for x in input().split()] + print(f"Maximum subarray sum of {arr} is {kadanes(arr)}") + except ValueError: + print("Please enter integer values.") diff --git a/maths/karatsuba.py b/maths/karatsuba.py new file mode 100644 index 000000000000..df29c77a5cf2 --- /dev/null +++ b/maths/karatsuba.py @@ -0,0 +1,32 @@ +""" Multiply two numbers using Karatsuba algorithm """ + + +def karatsuba(a, b): + """ + >>> karatsuba(15463, 23489) == 15463 * 23489 + True + >>> karatsuba(3, 9) == 3 * 9 + True + """ + if len(str(a)) == 1 or len(str(b)) == 1: + return a * b + else: + m1 = max(len(str(a)), len(str(b))) + m2 = m1 // 2 + + a1, a2 = divmod(a, 10 ** m2) + b1, b2 = divmod(b, 10 ** m2) + + x = karatsuba(a2, b2) + y = karatsuba((a1 + a2), (b1 + b2)) + z = karatsuba(a1, b1) + + return (z * 10 ** (2 * m2)) + ((y - z - x) * 10 ** (m2)) + (x) + + +def main(): + print(karatsuba(15463, 23489)) + + +if __name__ == "__main__": + main() diff --git a/maths/krishnamurthy_number.py b/maths/krishnamurthy_number.py new file mode 100644 index 000000000000..c88f68a07f27 --- /dev/null +++ b/maths/krishnamurthy_number.py @@ -0,0 +1,49 @@ +""" + == Krishnamurthy Number == +It is also known as Peterson Number +A Krishnamurthy Number is a number whose sum of the +factorial of the digits equals to the original +number itself. + +For example: 145 = 1! + 4! + 5! + So, 145 is a Krishnamurthy Number +""" + + +def factorial(digit: int) -> int: + """ + >>> factorial(3) + 6 + >>> factorial(0) + 1 + >>> factorial(5) + 120 + """ + + return 1 if digit in (0, 1) else (digit * factorial(digit - 1)) + + +def krishnamurthy(number: int) -> bool: + """ + >>> krishnamurthy(145) + True + >>> krishnamurthy(240) + False + >>> krishnamurthy(1) + True + """ + + factSum = 0 + duplicate = number + while duplicate > 0: + duplicate, digit = divmod(duplicate, 10) + factSum += factorial(digit) + return factSum == number + + +if __name__ == "__main__": + print("Program to check whether a number is a Krisnamurthy Number or not.") + number = int(input("Enter number: ").strip()) + print( + f"{number} is {'' if krishnamurthy(number) else 'not '}a Krishnamurthy Number." + ) diff --git a/maths/kth_lexicographic_permutation.py b/maths/kth_lexicographic_permutation.py new file mode 100644 index 000000000000..23eab626fbf8 --- /dev/null +++ b/maths/kth_lexicographic_permutation.py @@ -0,0 +1,40 @@ +def kthPermutation(k, n): + """ + Finds k'th lexicographic permutation (in increasing order) of + 0,1,2,...n-1 in O(n^2) time. + + Examples: + First permutation is always 0,1,2,...n + >>> kthPermutation(0,5) + [0, 1, 2, 3, 4] + + The order of permutation of 0,1,2,3 is [0,1,2,3], [0,1,3,2], [0,2,1,3], + [0,2,3,1], [0,3,1,2], [0,3,2,1], [1,0,2,3], [1,0,3,2], [1,2,0,3], + [1,2,3,0], [1,3,0,2] + >>> kthPermutation(10,4) + [1, 3, 0, 2] + """ + # Factorails from 1! to (n-1)! + factorials = [1] + for i in range(2, n): + factorials.append(factorials[-1] * i) + assert 0 <= k < factorials[-1] * n, "k out of bounds" + + permutation = [] + elements = list(range(n)) + + # Find permutation + while factorials: + factorial = factorials.pop() + number, k = divmod(k, factorial) + permutation.append(elements[number]) + elements.remove(elements[number]) + permutation.append(elements[0]) + + return permutation + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/largest_of_very_large_numbers.py b/maths/largest_of_very_large_numbers.py new file mode 100644 index 000000000000..d2dc0af18126 --- /dev/null +++ b/maths/largest_of_very_large_numbers.py @@ -0,0 +1,35 @@ +# Author: Abhijeeth S + +import math + + +def res(x, y): + if 0 not in (x, y): + # We use the relation x^y = y*log10(x), where 10 is the base. + return y * math.log10(x) + else: + if x == 0: # 0 raised to any number is 0 + return 0 + elif y == 0: + return 1 # any number raised to 0 is 1 + + +if __name__ == "__main__": # Main function + # Read two numbers from input and typecast them to int using map function. + # Here x is the base and y is the power. + prompt = "Enter the base and the power separated by a comma: " + x1, y1 = map(int, input(prompt).split(",")) + x2, y2 = map(int, input(prompt).split(",")) + + # We find the log of each number, using the function res(), which takes two + # arguments. + res1 = res(x1, y1) + res2 = res(x2, y2) + + # We check for the largest number + if res1 > res2: + print("Largest number is", x1, "^", y1) + elif res2 > res1: + print("Largest number is", x2, "^", y2) + else: + print("Both are equal") diff --git a/maths/least_common_multiple.py b/maths/least_common_multiple.py new file mode 100644 index 000000000000..0d087643e869 --- /dev/null +++ b/maths/least_common_multiple.py @@ -0,0 +1,95 @@ +import unittest +from timeit import timeit + + +def least_common_multiple_slow(first_num: int, second_num: int) -> int: + """ + Find the least common multiple of two numbers. + + Learn more: https://en.wikipedia.org/wiki/Least_common_multiple + + >>> least_common_multiple_slow(5, 2) + 10 + >>> least_common_multiple_slow(12, 76) + 228 + """ + max_num = first_num if first_num >= second_num else second_num + common_mult = max_num + while (common_mult % first_num > 0) or (common_mult % second_num > 0): + common_mult += max_num + return common_mult + + +def greatest_common_divisor(a: int, b: int) -> int: + """ + Calculate Greatest Common Divisor (GCD). + see greatest_common_divisor.py + >>> greatest_common_divisor(24, 40) + 8 + >>> greatest_common_divisor(1, 1) + 1 + >>> greatest_common_divisor(1, 800) + 1 + >>> greatest_common_divisor(11, 37) + 1 + >>> greatest_common_divisor(3, 5) + 1 + >>> greatest_common_divisor(16, 4) + 4 + """ + return b if a == 0 else greatest_common_divisor(b % a, a) + + +def least_common_multiple_fast(first_num: int, second_num: int) -> int: + """ + Find the least common multiple of two numbers. + https://en.wikipedia.org/wiki/Least_common_multiple#Using_the_greatest_common_divisor + >>> least_common_multiple_fast(5,2) + 10 + >>> least_common_multiple_fast(12,76) + 228 + """ + return first_num // greatest_common_divisor(first_num, second_num) * second_num + + +def benchmark(): + setup = ( + "from __main__ import least_common_multiple_slow, least_common_multiple_fast" + ) + print( + "least_common_multiple_slow():", + timeit("least_common_multiple_slow(1000, 999)", setup=setup), + ) + print( + "least_common_multiple_fast():", + timeit("least_common_multiple_fast(1000, 999)", setup=setup), + ) + + +class TestLeastCommonMultiple(unittest.TestCase): + + test_inputs = [ + (10, 20), + (13, 15), + (4, 31), + (10, 42), + (43, 34), + (5, 12), + (12, 25), + (10, 25), + (6, 9), + ] + expected_results = [20, 195, 124, 210, 1462, 60, 300, 50, 18] + + def test_lcm_function(self): + for i, (first_num, second_num) in enumerate(self.test_inputs): + slow_result = least_common_multiple_slow(first_num, second_num) + fast_result = least_common_multiple_fast(first_num, second_num) + with self.subTest(i=i): + self.assertEqual(slow_result, self.expected_results[i]) + self.assertEqual(fast_result, self.expected_results[i]) + + +if __name__ == "__main__": + benchmark() + unittest.main() diff --git a/maths/line_length.py b/maths/line_length.py new file mode 100644 index 000000000000..1d386b44b50d --- /dev/null +++ b/maths/line_length.py @@ -0,0 +1,65 @@ +import math +from typing import Callable, Union + + +def line_length( + fnc: Callable[[Union[int, float]], Union[int, float]], + x_start: Union[int, float], + x_end: Union[int, float], + steps: int = 100, +) -> float: + + """ + Approximates the arc length of a line segment by treating the curve as a + sequence of linear lines and summing their lengths + :param fnc: a function which defines a curve + :param x_start: left end point to indicate the start of line segment + :param x_end: right end point to indicate end of line segment + :param steps: an accuracy gauge; more steps increases accuracy + :return: a float representing the length of the curve + + >>> def f(x): + ... return x + >>> f"{line_length(f, 0, 1, 10):.6f}" + '1.414214' + + >>> def f(x): + ... return 1 + >>> f"{line_length(f, -5.5, 4.5):.6f}" + '10.000000' + + >>> def f(x): + ... return math.sin(5 * x) + math.cos(10 * x) + x * x/10 + >>> f"{line_length(f, 0.0, 10.0, 10000):.6f}" + '69.534930' + """ + + x1 = x_start + fx1 = fnc(x_start) + length = 0.0 + + for i in range(steps): + + # Approximates curve as a sequence of linear lines and sums their length + x2 = (x_end - x_start) / steps + x1 + fx2 = fnc(x2) + length += math.hypot(x2 - x1, fx2 - fx1) + + # Increment step + x1 = x2 + fx1 = fx2 + + return length + + +if __name__ == "__main__": + + def f(x): + return math.sin(10 * x) + + print("f(x) = sin(10 * x)") + print("The length of the curve from x = -10 to x = 10 is:") + i = 10 + while i <= 100000: + print(f"With {i} steps: {line_length(f, -10, 10, i)}") + i *= 10 diff --git a/maths/lucas_lehmer_primality_test.py b/maths/lucas_lehmer_primality_test.py new file mode 100644 index 000000000000..15e25cbfe996 --- /dev/null +++ b/maths/lucas_lehmer_primality_test.py @@ -0,0 +1,41 @@ +""" + In mathematics, the Lucas–Lehmer test (LLT) is a primality test for Mersenne + numbers. https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test + + A Mersenne number is a number that is one less than a power of two. + That is M_p = 2^p - 1 + https://en.wikipedia.org/wiki/Mersenne_prime + + The Lucas–Lehmer test is the primality test used by the + Great Internet Mersenne Prime Search (GIMPS) to locate large primes. +""" + + +# Primality test 2^p - 1 +# Return true if 2^p - 1 is prime +def lucas_lehmer_test(p: int) -> bool: + """ + >>> lucas_lehmer_test(p=7) + True + + >>> lucas_lehmer_test(p=11) + False + + # M_11 = 2^11 - 1 = 2047 = 23 * 89 + """ + + if p < 2: + raise ValueError("p should not be less than 2!") + elif p == 2: + return True + + s = 4 + M = (1 << p) - 1 + for i in range(p - 2): + s = ((s * s) - 2) % M + return s == 0 + + +if __name__ == "__main__": + print(lucas_lehmer_test(7)) + print(lucas_lehmer_test(11)) diff --git a/maths/lucas_series.py b/maths/lucas_series.py new file mode 100644 index 000000000000..6b32c2022e13 --- /dev/null +++ b/maths/lucas_series.py @@ -0,0 +1,66 @@ +""" +https://en.wikipedia.org/wiki/Lucas_number +""" + + +def recursive_lucas_number(n_th_number: int) -> int: + """ + Returns the nth lucas number + >>> recursive_lucas_number(1) + 1 + >>> recursive_lucas_number(20) + 15127 + >>> recursive_lucas_number(0) + 2 + >>> recursive_lucas_number(25) + 167761 + >>> recursive_lucas_number(-1.5) + Traceback (most recent call last): + ... + TypeError: recursive_lucas_number accepts only integer arguments. + """ + if not isinstance(n_th_number, int): + raise TypeError("recursive_lucas_number accepts only integer arguments.") + if n_th_number == 0: + return 2 + if n_th_number == 1: + return 1 + + return recursive_lucas_number(n_th_number - 1) + recursive_lucas_number( + n_th_number - 2 + ) + + +def dynamic_lucas_number(n_th_number: int) -> int: + """ + Returns the nth lucas number + >>> dynamic_lucas_number(1) + 1 + >>> dynamic_lucas_number(20) + 15127 + >>> dynamic_lucas_number(0) + 2 + >>> dynamic_lucas_number(25) + 167761 + >>> dynamic_lucas_number(-1.5) + Traceback (most recent call last): + ... + TypeError: dynamic_lucas_number accepts only integer arguments. + """ + if not isinstance(n_th_number, int): + raise TypeError("dynamic_lucas_number accepts only integer arguments.") + a, b = 2, 1 + for i in range(n_th_number): + a, b = b, a + b + return a + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + n = int(input("Enter the number of terms in lucas series:\n").strip()) + print("Using recursive function to calculate lucas series:") + print(" ".join(str(recursive_lucas_number(i)) for i in range(n))) + print("\nUsing dynamic function to calculate lucas series:") + print(" ".join(str(dynamic_lucas_number(i)) for i in range(n))) diff --git a/maths/matrix_exponentiation.py b/maths/matrix_exponentiation.py new file mode 100644 index 000000000000..033ceb3f28a0 --- /dev/null +++ b/maths/matrix_exponentiation.py @@ -0,0 +1,100 @@ +"""Matrix Exponentiation""" + +import timeit + +""" +Matrix Exponentiation is a technique to solve linear recurrences in logarithmic time. +You read more about it here: +http://zobayer.blogspot.com/2010/11/matrix-exponentiation.html +https://www.hackerearth.com/practice/notes/matrix-exponentiation-1/ +""" + + +class Matrix: + def __init__(self, arg): + if isinstance(arg, list): # Initializes a matrix identical to the one provided. + self.t = arg + self.n = len(arg) + else: # Initializes a square matrix of the given size and set values to zero. + self.n = arg + self.t = [[0 for _ in range(self.n)] for _ in range(self.n)] + + def __mul__(self, b): + matrix = Matrix(self.n) + for i in range(self.n): + for j in range(self.n): + for k in range(self.n): + matrix.t[i][j] += self.t[i][k] * b.t[k][j] + return matrix + + +def modular_exponentiation(a, b): + matrix = Matrix([[1, 0], [0, 1]]) + while b > 0: + if b & 1: + matrix *= a + a *= a + b >>= 1 + return matrix + + +def fibonacci_with_matrix_exponentiation(n, f1, f2): + # Trivial Cases + if n == 1: + return f1 + elif n == 2: + return f2 + matrix = Matrix([[1, 1], [1, 0]]) + matrix = modular_exponentiation(matrix, n - 2) + return f2 * matrix.t[0][0] + f1 * matrix.t[0][1] + + +def simple_fibonacci(n, f1, f2): + # Trivial Cases + if n == 1: + return f1 + elif n == 2: + return f2 + + fn_1 = f1 + fn_2 = f2 + n -= 2 + + while n > 0: + fn_1, fn_2 = fn_1 + fn_2, fn_1 + n -= 1 + + return fn_1 + + +def matrix_exponentiation_time(): + setup = """ +from random import randint +from __main__ import fibonacci_with_matrix_exponentiation +""" + code = "fibonacci_with_matrix_exponentiation(randint(1,70000), 1, 1)" + exec_time = timeit.timeit(setup=setup, stmt=code, number=100) + print("With matrix exponentiation the average execution time is ", exec_time / 100) + return exec_time + + +def simple_fibonacci_time(): + setup = """ +from random import randint +from __main__ import simple_fibonacci +""" + code = "simple_fibonacci(randint(1,70000), 1, 1)" + exec_time = timeit.timeit(setup=setup, stmt=code, number=100) + print( + "Without matrix exponentiation the average execution time is ", exec_time / 100 + ) + return exec_time + + +def main(): + matrix_exponentiation_time() + simple_fibonacci_time() + + +if __name__ == "__main__": + main() diff --git a/maths/miller_rabin.py b/maths/miller_rabin.py new file mode 100644 index 000000000000..fe992027190b --- /dev/null +++ b/maths/miller_rabin.py @@ -0,0 +1,50 @@ +import random + +from .binary_exp_mod import bin_exp_mod + + +# This is a probabilistic check to test primality, useful for big numbers! +# if it's a prime, it will return true +# if it's not a prime, the chance of it returning true is at most 1/4**prec +def is_prime(n, prec=1000): + """ + >>> from .prime_check import prime_check + >>> all(is_prime(i) == prime_check(i) for i in range(1000)) + True + """ + if n < 2: + return False + + if n % 2 == 0: + return n == 2 + + # this means n is odd + d = n - 1 + exp = 0 + while d % 2 == 0: + d /= 2 + exp += 1 + + # n - 1=d*(2**exp) + count = 0 + while count < prec: + a = random.randint(2, n - 1) + b = bin_exp_mod(a, d, n) + if b != 1: + flag = True + for i in range(exp): + if b == n - 1: + flag = False + break + b = b * b + b %= n + if flag: + return False + count += 1 + return True + + +if __name__ == "__main__": + n = abs(int(input("Enter bound : ").strip())) + print("Here's the list of primes:") + print(", ".join(str(i) for i in range(n + 1) if is_prime(i))) diff --git a/maths/mobius_function.py b/maths/mobius_function.py new file mode 100644 index 000000000000..4fcf35f21813 --- /dev/null +++ b/maths/mobius_function.py @@ -0,0 +1,43 @@ +""" +References: https://en.wikipedia.org/wiki/M%C3%B6bius_function +References: wikipedia:square free number +python/black : True +flake8 : True +""" + +from maths.is_square_free import is_square_free +from maths.prime_factors import prime_factors + + +def mobius(n: int) -> int: + """ + Mobius function + >>> mobius(24) + 0 + >>> mobius(-1) + 1 + >>> mobius('asd') + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'str' + >>> mobius(10**400) + 0 + >>> mobius(10**-400) + 1 + >>> mobius(-1424) + 1 + >>> mobius([1, '2', 2.0]) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'list' + """ + factors = prime_factors(n) + if is_square_free(factors): + return -1 if len(factors) % 2 else 1 + return 0 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/modular_exponential.py b/maths/modular_exponential.py index b3f4c00bd5d8..42987dbf3a24 100644 --- a/maths/modular_exponential.py +++ b/maths/modular_exponential.py @@ -1,20 +1,45 @@ -def modularExponential(base, power, mod): - if power < 0: - return -1 - base %= mod - result = 1 +""" + Modular Exponential. + Modular exponentiation is a type of exponentiation performed over a modulus. + For more explanation, please check + https://en.wikipedia.org/wiki/Modular_exponentiation +""" - while power > 0: - if power & 1: - result = (result * base) % mod - power = power >> 1 - base = (base * base) % mod - return result +"""Calculate Modular Exponential.""" + + +def modular_exponential(base: int, power: int, mod: int): + """ + >>> modular_exponential(5, 0, 10) + 1 + >>> modular_exponential(2, 8, 7) + 4 + >>> modular_exponential(3, -2, 9) + -1 + """ + + if power < 0: + return -1 + base %= mod + result = 1 + + while power > 0: + if power & 1: + result = (result * base) % mod + power = power >> 1 + base = (base * base) % mod + + return result def main(): - print(modularExponential(3, 200, 13)) + """Call Modular Exponential Function.""" + print(modular_exponential(3, 200, 13)) + + +if __name__ == "__main__": + import doctest + doctest.testmod() -if __name__ == '__main__': - main() + main() diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py new file mode 100644 index 000000000000..28027cbe4178 --- /dev/null +++ b/maths/monte_carlo.py @@ -0,0 +1,129 @@ +""" +@author: MatteoRaso +""" +from math import pi, sqrt +from random import uniform +from statistics import mean +from typing import Callable + + +def pi_estimator(iterations: int): + """ + An implementation of the Monte Carlo method used to find pi. + 1. Draw a 2x2 square centred at (0,0). + 2. Inscribe a circle within the square. + 3. For each iteration, place a dot anywhere in the square. + a. Record the number of dots within the circle. + 4. After all the dots are placed, divide the dots in the circle by the total. + 5. Multiply this value by 4 to get your estimate of pi. + 6. Print the estimated and numpy value of pi + """ + # A local function to see if a dot lands in the circle. + def is_in_circle(x: float, y: float) -> bool: + distance_from_centre = sqrt((x ** 2) + (y ** 2)) + # Our circle has a radius of 1, so a distance + # greater than 1 would land outside the circle. + return distance_from_centre <= 1 + + # The proportion of guesses that landed in the circle + proportion = mean( + int(is_in_circle(uniform(-1.0, 1.0), uniform(-1.0, 1.0))) + for _ in range(iterations) + ) + # The ratio of the area for circle to square is pi/4. + pi_estimate = proportion * 4 + print(f"The estimated value of pi is {pi_estimate}") + print(f"The numpy value of pi is {pi}") + print(f"The total error is {abs(pi - pi_estimate)}") + + +def area_under_curve_estimator( + iterations: int, + function_to_integrate: Callable[[float], float], + min_value: float = 0.0, + max_value: float = 1.0, +) -> float: + """ + An implementation of the Monte Carlo method to find area under + a single variable non-negative real-valued continuous function, + say f(x), where x lies within a continuous bounded interval, + say [min_value, max_value], where min_value and max_value are + finite numbers + 1. Let x be a uniformly distributed random variable between min_value to + max_value + 2. Expected value of f(x) = + (integrate f(x) from min_value to max_value)/(max_value - min_value) + 3. Finding expected value of f(x): + a. Repeatedly draw x from uniform distribution + b. Evaluate f(x) at each of the drawn x values + c. Expected value = average of the function evaluations + 4. Estimated value of integral = Expected value * (max_value - min_value) + 5. Returns estimated value + """ + + return mean( + function_to_integrate(uniform(min_value, max_value)) for _ in range(iterations) + ) * (max_value - min_value) + + +def area_under_line_estimator_check( + iterations: int, min_value: float = 0.0, max_value: float = 1.0 +) -> None: + """ + Checks estimation error for area_under_curve_estimator function + for f(x) = x where x lies within min_value to max_value + 1. Calls "area_under_curve_estimator" function + 2. Compares with the expected value + 3. Prints estimated, expected and error value + """ + + def identity_function(x: float) -> float: + """ + Represents identity function + >>> [function_to_integrate(x) for x in [-2.0, -1.0, 0.0, 1.0, 2.0]] + [-2.0, -1.0, 0.0, 1.0, 2.0] + """ + return x + + estimated_value = area_under_curve_estimator( + iterations, identity_function, min_value, max_value + ) + expected_value = (max_value * max_value - min_value * min_value) / 2 + + print("******************") + print(f"Estimating area under y=x where x varies from {min_value} to {max_value}") + print(f"Estimated value is {estimated_value}") + print(f"Expected value is {expected_value}") + print(f"Total error is {abs(estimated_value - expected_value)}") + print("******************") + + +def pi_estimator_using_area_under_curve(iterations: int) -> None: + """ + Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi + """ + + def function_to_integrate(x: float) -> float: + """ + Represents semi-circle with radius 2 + >>> [function_to_integrate(x) for x in [-2.0, 0.0, 2.0]] + [0.0, 2.0, 0.0] + """ + return sqrt(4.0 - x * x) + + estimated_value = area_under_curve_estimator( + iterations, function_to_integrate, 0.0, 2.0 + ) + + print("******************") + print("Estimating pi using area_under_curve_estimator") + print(f"Estimated value is {estimated_value}") + print(f"Expected value is {pi}") + print(f"Total error is {abs(estimated_value - pi)}") + print("******************") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/monte_carlo_dice.py b/maths/monte_carlo_dice.py new file mode 100644 index 000000000000..e8e3abe83a99 --- /dev/null +++ b/maths/monte_carlo_dice.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +import random + + +class Dice: + NUM_SIDES = 6 + + def __init__(self): + """ Initialize a six sided dice """ + self.sides = list(range(1, Dice.NUM_SIDES + 1)) + + def roll(self): + return random.choice(self.sides) + + def _str_(self): + return "Fair Dice" + + +def throw_dice(num_throws: int, num_dice: int = 2) -> list[float]: + """ + Return probability list of all possible sums when throwing dice. + + >>> random.seed(0) + >>> throw_dice(10, 1) + [10.0, 0.0, 30.0, 50.0, 10.0, 0.0] + >>> throw_dice(100, 1) + [19.0, 17.0, 17.0, 11.0, 23.0, 13.0] + >>> throw_dice(1000, 1) + [18.8, 15.5, 16.3, 17.6, 14.2, 17.6] + >>> throw_dice(10000, 1) + [16.35, 16.89, 16.93, 16.6, 16.52, 16.71] + >>> throw_dice(10000, 2) + [2.74, 5.6, 7.99, 11.26, 13.92, 16.7, 14.44, 10.63, 8.05, 5.92, 2.75] + """ + dices = [Dice() for i in range(num_dice)] + count_of_sum = [0] * (len(dices) * Dice.NUM_SIDES + 1) + for i in range(num_throws): + count_of_sum[sum(dice.roll() for dice in dices)] += 1 + probability = [round((count * 100) / num_throws, 2) for count in count_of_sum] + return probability[num_dice:] # remove probability of sums that never appear + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/newton_raphson.py b/maths/newton_raphson.py index c08bcedc9a4d..f2b7cb9766d2 100644 --- a/maths/newton_raphson.py +++ b/maths/newton_raphson.py @@ -1,4 +1,4 @@ -''' +""" Author: P Shreyas Shetty Implementation of Newton-Raphson method for solving equations of kind f(x) = 0. It is an iterative method where solution is found by the expression @@ -6,45 +6,49 @@ If no solution exists, then either the solution will not be found when iteration limit is reached or the gradient f'(x[n]) approaches zero. In both cases, exception is raised. If iteration limit is reached, try increasing maxiter. - ''' - + """ import math as m + def calc_derivative(f, a, h=0.001): - ''' - Calculates derivative at point a for function f using finite difference - method - ''' - return (f(a+h)-f(a-h))/(2*h) - -def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6,logsteps=False): - - a = x0 #set the initial guess + """ + Calculates derivative at point a for function f using finite difference + method + """ + return (f(a + h) - f(a - h)) / (2 * h) + + +def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=False): + + a = x0 # set the initial guess steps = [a] error = abs(f(a)) - f1 = lambda x:calc_derivative(f, x, h=step) #Derivative of f(x) + f1 = lambda x: calc_derivative(f, x, h=step) # noqa: E731 Derivative of f(x) for _ in range(maxiter): if f1(a) == 0: raise ValueError("No converging solution found") - a = a - f(a)/f1(a) #Calculate the next estimate + a = a - f(a) / f1(a) # Calculate the next estimate if logsteps: steps.append(a) - error = abs(f(a)) if error < maxerror: break else: - raise ValueError("Itheration limit reached, no converging solution found") + raise ValueError("Iteration limit reached, no converging solution found") if logsteps: - #If logstep is true, then log intermediate steps + # If logstep is true, then log intermediate steps return a, error, steps return a, error - -if __name__ == '__main__': - import matplotlib.pyplot as plt - f = lambda x:m.tanh(x)**2-m.exp(3*x) - solution, error, steps = newton_raphson(f, x0=10, maxiter=1000, step=1e-6, logsteps=True) + + +if __name__ == "__main__": + from matplotlib import pyplot as plt + + f = lambda x: m.tanh(x) ** 2 - m.exp(3 * x) # noqa: E731 + solution, error, steps = newton_raphson( + f, x0=10, maxiter=1000, step=1e-6, logsteps=True + ) plt.plot([abs(f(x)) for x in steps]) plt.xlabel("step") plt.ylabel("error") plt.show() - print("solution = {%f}, error = {%f}" % (solution, error)) \ No newline at end of file + print(f"solution = {{{solution:f}}}, error = {{{error:f}}}") diff --git a/maths/number_of_digits.py b/maths/number_of_digits.py new file mode 100644 index 000000000000..3c0eb7b3863f --- /dev/null +++ b/maths/number_of_digits.py @@ -0,0 +1,159 @@ +import math +from timeit import timeit + + +def num_digits(n: int) -> int: + """ + Find the number of digits in a number. + + >>> num_digits(12345) + 5 + >>> num_digits(123) + 3 + >>> num_digits(0) + 1 + >>> num_digits(-1) + 1 + >>> num_digits(-123456) + 6 + """ + digits = 0 + n = abs(n) + while True: + n = n // 10 + digits += 1 + if n == 0: + break + return digits + + +def num_digits_fast(n: int) -> int: + """ + Find the number of digits in a number. + abs() is used as logarithm for negative numbers is not defined. + + >>> num_digits_fast(12345) + 5 + >>> num_digits_fast(123) + 3 + >>> num_digits_fast(0) + 1 + >>> num_digits_fast(-1) + 1 + >>> num_digits_fast(-123456) + 6 + """ + return 1 if n == 0 else math.floor(math.log(abs(n), 10) + 1) + + +def num_digits_faster(n: int) -> int: + """ + Find the number of digits in a number. + abs() is used for negative numbers + + >>> num_digits_faster(12345) + 5 + >>> num_digits_faster(123) + 3 + >>> num_digits_faster(0) + 1 + >>> num_digits_faster(-1) + 1 + >>> num_digits_faster(-123456) + 6 + """ + return len(str(abs(n))) + + +def benchmark() -> None: + """ + Benchmark code for comparing 3 functions, + with 3 different length int values. + """ + print("\nFor small_num = ", small_num, ":") + print( + "> num_digits()", + "\t\tans =", + num_digits(small_num), + "\ttime =", + timeit("z.num_digits(z.small_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> num_digits_fast()", + "\tans =", + num_digits_fast(small_num), + "\ttime =", + timeit("z.num_digits_fast(z.small_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> num_digits_faster()", + "\tans =", + num_digits_faster(small_num), + "\ttime =", + timeit("z.num_digits_faster(z.small_num)", setup="import __main__ as z"), + "seconds", + ) + + print("\nFor medium_num = ", medium_num, ":") + print( + "> num_digits()", + "\t\tans =", + num_digits(medium_num), + "\ttime =", + timeit("z.num_digits(z.medium_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> num_digits_fast()", + "\tans =", + num_digits_fast(medium_num), + "\ttime =", + timeit("z.num_digits_fast(z.medium_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> num_digits_faster()", + "\tans =", + num_digits_faster(medium_num), + "\ttime =", + timeit("z.num_digits_faster(z.medium_num)", setup="import __main__ as z"), + "seconds", + ) + + print("\nFor large_num = ", large_num, ":") + print( + "> num_digits()", + "\t\tans =", + num_digits(large_num), + "\ttime =", + timeit("z.num_digits(z.large_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> num_digits_fast()", + "\tans =", + num_digits_fast(large_num), + "\ttime =", + timeit("z.num_digits_fast(z.large_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> num_digits_faster()", + "\tans =", + num_digits_faster(large_num), + "\ttime =", + timeit("z.num_digits_faster(z.large_num)", setup="import __main__ as z"), + "seconds", + ) + + +if __name__ == "__main__": + small_num = 262144 + medium_num = 1125899906842624 + large_num = 1267650600228229401496703205376 + benchmark() + import doctest + + doctest.testmod() diff --git a/maths/numerical_integration.py b/maths/numerical_integration.py new file mode 100644 index 000000000000..87184a76b740 --- /dev/null +++ b/maths/numerical_integration.py @@ -0,0 +1,66 @@ +""" +Approximates the area under the curve using the trapezoidal rule +""" + +from typing import Callable, Union + + +def trapezoidal_area( + fnc: Callable[[Union[int, float]], Union[int, float]], + x_start: Union[int, float], + x_end: Union[int, float], + steps: int = 100, +) -> float: + + """ + Treats curve as a collection of linear lines and sums the area of the + trapezium shape they form + :param fnc: a function which defines a curve + :param x_start: left end point to indicate the start of line segment + :param x_end: right end point to indicate end of line segment + :param steps: an accuracy gauge; more steps increases the accuracy + :return: a float representing the length of the curve + + >>> def f(x): + ... return 5 + >>> '%.3f' % trapezoidal_area(f, 12.0, 14.0, 1000) + '10.000' + + >>> def f(x): + ... return 9*x**2 + >>> '%.4f' % trapezoidal_area(f, -4.0, 0, 10000) + '192.0000' + + >>> '%.4f' % trapezoidal_area(f, -4.0, 4.0, 10000) + '384.0000' + """ + x1 = x_start + fx1 = fnc(x_start) + area = 0.0 + + for i in range(steps): + + # Approximates small segments of curve as linear and solve + # for trapezoidal area + x2 = (x_end - x_start) / steps + x1 + fx2 = fnc(x2) + area += abs(fx2 + fx1) * (x2 - x1) / 2 + + # Increment step + x1 = x2 + fx1 = fx2 + return area + + +if __name__ == "__main__": + + def f(x): + return x ** 3 + + print("f(x) = x^3") + print("The area between the curve, x = -10, x = 10 and the x axis is:") + i = 10 + while i <= 100000: + area = trapezoidal_area(f, -5, 5, i) + print(f"with {i} steps: {area}") + i *= 10 diff --git a/maths/perfect_cube.py b/maths/perfect_cube.py new file mode 100644 index 000000000000..9ad287e41e75 --- /dev/null +++ b/maths/perfect_cube.py @@ -0,0 +1,16 @@ +def perfect_cube(n: int) -> bool: + """ + Check if a number is a perfect cube or not. + + >>> perfect_cube(27) + True + >>> perfect_cube(4) + False + """ + val = n ** (1 / 3) + return (val * val * val) == n + + +if __name__ == "__main__": + print(perfect_cube(27)) + print(perfect_cube(4)) diff --git a/maths/perfect_number.py b/maths/perfect_number.py new file mode 100644 index 000000000000..148e988fb4c5 --- /dev/null +++ b/maths/perfect_number.py @@ -0,0 +1,34 @@ +""" +== Perfect Number == +In number theory, a perfect number is a positive integer that is equal to the sum of +its positive divisors, excluding the number itself. +For example: 6 ==> divisors[1, 2, 3, 6] + Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6 + So, 6 is a Perfect Number + +Other examples of Perfect Numbers: 28, 486, ... + +https://en.wikipedia.org/wiki/Perfect_number +""" + + +def perfect(number: int) -> bool: + """ + >>> perfect(27) + False + >>> perfect(28) + True + >>> perfect(29) + False + + Start from 1 because dividing by 0 will raise ZeroDivisionError. + A number at most can be divisible by the half of the number except the number + itself. For example, 6 is at most can be divisible by 3 except by 6 itself. + """ + return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number + + +if __name__ == "__main__": + print("Program to check whether a number is a Perfect number or not...") + number = int(input("Enter number: ").strip()) + print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") diff --git a/maths/perfect_square.py b/maths/perfect_square.py new file mode 100644 index 000000000000..4393dcfbc774 --- /dev/null +++ b/maths/perfect_square.py @@ -0,0 +1,73 @@ +import math + + +def perfect_square(num: int) -> bool: + """ + Check if a number is perfect square number or not + :param num: the number to be checked + :return: True if number is square number, otherwise False + + >>> perfect_square(9) + True + >>> perfect_square(16) + True + >>> perfect_square(1) + True + >>> perfect_square(0) + True + >>> perfect_square(10) + False + """ + return math.sqrt(num) * math.sqrt(num) == num + + +def perfect_square_binary_search(n: int) -> bool: + """ + Check if a number is perfect square using binary search. + Time complexity : O(Log(n)) + Space complexity: O(1) + + >>> perfect_square_binary_search(9) + True + >>> perfect_square_binary_search(16) + True + >>> perfect_square_binary_search(1) + True + >>> perfect_square_binary_search(0) + True + >>> perfect_square_binary_search(10) + False + >>> perfect_square_binary_search(-1) + False + >>> perfect_square_binary_search(1.1) + False + >>> perfect_square_binary_search("a") + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'str' + >>> perfect_square_binary_search(None) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'NoneType' + >>> perfect_square_binary_search([]) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'list' + """ + left = 0 + right = n + while left <= right: + mid = (left + right) // 2 + if mid ** 2 == n: + return True + elif mid ** 2 > n: + right = mid - 1 + else: + left = mid + 1 + return False + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/pi_monte_carlo_estimation.py b/maths/pi_monte_carlo_estimation.py new file mode 100644 index 000000000000..20b46dddc6e5 --- /dev/null +++ b/maths/pi_monte_carlo_estimation.py @@ -0,0 +1,67 @@ +import random + + +class Point: + def __init__(self, x: float, y: float) -> None: + self.x = x + self.y = y + + def is_in_unit_circle(self) -> bool: + """ + True, if the point lies in the unit circle + False, otherwise + """ + return (self.x ** 2 + self.y ** 2) <= 1 + + @classmethod + def random_unit_square(cls): + """ + Generates a point randomly drawn from the unit square [0, 1) x [0, 1). + """ + return cls(x=random.random(), y=random.random()) + + +def estimate_pi(number_of_simulations: int) -> float: + """ + Generates an estimate of the mathematical constant PI. + See https://en.wikipedia.org/wiki/Monte_Carlo_method#Overview + + The estimate is generated by Monte Carlo simulations. Let U be uniformly drawn from + the unit square [0, 1) x [0, 1). The probability that U lies in the unit circle is: + + P[U in unit circle] = 1/4 PI + + and therefore + + PI = 4 * P[U in unit circle] + + We can get an estimate of the probability P[U in unit circle]. + See https://en.wikipedia.org/wiki/Empirical_probability by: + + 1. Draw a point uniformly from the unit square. + 2. Repeat the first step n times and count the number of points in the unit + circle, which is called m. + 3. An estimate of P[U in unit circle] is m/n + """ + if number_of_simulations < 1: + raise ValueError("At least one simulation is necessary to estimate PI.") + + number_in_unit_circle = 0 + for simulation_index in range(number_of_simulations): + random_point = Point.random_unit_square() + + if random_point.is_in_unit_circle(): + number_in_unit_circle += 1 + + return 4 * number_in_unit_circle / number_of_simulations + + +if __name__ == "__main__": + # import doctest + + # doctest.testmod() + from math import pi + + prompt = "Please enter the desired number of Monte Carlo simulations: " + my_pi = estimate_pi(int(input(prompt).strip())) + print(f"An estimate of PI is {my_pi} with an error of {abs(my_pi - pi)}") diff --git a/maths/polynomial_evaluation.py b/maths/polynomial_evaluation.py new file mode 100644 index 000000000000..e929a2d02972 --- /dev/null +++ b/maths/polynomial_evaluation.py @@ -0,0 +1,54 @@ +from typing import Sequence + + +def evaluate_poly(poly: Sequence[float], x: float) -> float: + """Evaluate a polynomial f(x) at specified point x and return the value. + + Arguments: + poly -- the coeffiecients of a polynomial as an iterable in order of + ascending degree + x -- the point at which to evaluate the polynomial + + >>> evaluate_poly((0.0, 0.0, 5.0, 9.3, 7.0), 10.0) + 79800.0 + """ + return sum(c * (x ** i) for i, c in enumerate(poly)) + + +def horner(poly: Sequence[float], x: float) -> float: + """Evaluate a polynomial at specified point using Horner's method. + + In terms of computational complexity, Horner's method is an efficient method + of evaluating a polynomial. It avoids the use of expensive exponentiation, + and instead uses only multiplication and addition to evaluate the polynomial + in O(n), where n is the degree of the polynomial. + + https://en.wikipedia.org/wiki/Horner's_method + + Arguments: + poly -- the coeffiecients of a polynomial as an iterable in order of + ascending degree + x -- the point at which to evaluate the polynomial + + >>> horner((0.0, 0.0, 5.0, 9.3, 7.0), 10.0) + 79800.0 + """ + result = 0.0 + for coeff in reversed(poly): + result = result * x + coeff + return result + + +if __name__ == "__main__": + """ + Example: + >>> poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7.0x^4 + 9.3x^3 + 5.0x^2 + >>> x = -13.0 + >>> # f(-13) = 7.0(-13)^4 + 9.3(-13)^3 + 5.0(-13)^2 = 180339.9 + >>> print(evaluate_poly(poly, x)) + 180339.9 + """ + poly = (0.0, 0.0, 5.0, 9.3, 7.0) + x = 10.0 + print(evaluate_poly(poly, x)) + print(horner(poly, x)) diff --git a/maths/power_using_recursion.py b/maths/power_using_recursion.py new file mode 100644 index 000000000000..f82097f6d8ec --- /dev/null +++ b/maths/power_using_recursion.py @@ -0,0 +1,36 @@ +""" +== Raise base to the power of exponent using recursion == + Input --> + Enter the base: 3 + Enter the exponent: 4 + Output --> + 3 to the power of 4 is 81 + Input --> + Enter the base: 2 + Enter the exponent: 0 + Output --> + 2 to the power of 0 is 1 +""" + + +def power(base: int, exponent: int) -> float: + """ + power(3, 4) + 81 + >>> power(2, 0) + 1 + >>> all(power(base, exponent) == pow(base, exponent) + ... for base in range(-10, 10) for exponent in range(10)) + True + """ + return base * power(base, (exponent - 1)) if exponent else 1 + + +if __name__ == "__main__": + print("Raise base to the power of exponent using recursion...") + base = int(input("Enter the base: ").strip()) + exponent = int(input("Enter the exponent: ").strip()) + result = power(base, abs(exponent)) + if exponent < 0: # power() does not properly deal w/ negative exponents + result = 1 / result + print(f"{base} to the power of {exponent} is {result}") diff --git a/maths/prime_check.py b/maths/prime_check.py new file mode 100644 index 000000000000..e2bcb7b8f151 --- /dev/null +++ b/maths/prime_check.py @@ -0,0 +1,58 @@ +"""Prime Check.""" + +import math +import unittest + + +def prime_check(number: int) -> bool: + """Checks to see if a number is a prime. + + A number is prime if it has exactly two factors: 1 and itself. + """ + + if 1 < number < 4: + # 2 and 3 are primes + return True + elif number < 2 or not number % 2: + # Negatives, 0, 1 and all even numbers are not primes + return False + + odd_numbers = range(3, int(math.sqrt(number) + 1), 2) + return not any(not number % i for i in odd_numbers) + + +class Test(unittest.TestCase): + def test_primes(self): + self.assertTrue(prime_check(2)) + self.assertTrue(prime_check(3)) + self.assertTrue(prime_check(5)) + self.assertTrue(prime_check(7)) + self.assertTrue(prime_check(11)) + self.assertTrue(prime_check(13)) + self.assertTrue(prime_check(17)) + self.assertTrue(prime_check(19)) + self.assertTrue(prime_check(23)) + self.assertTrue(prime_check(29)) + + def test_not_primes(self): + self.assertFalse( + prime_check(-19), + "Negative numbers are excluded by definition of prime numbers.", + ) + self.assertFalse( + prime_check(0), + "Zero doesn't have any positive factors, primes must have exactly two.", + ) + self.assertFalse( + prime_check(1), + "One only has 1 positive factor, primes must have exactly two.", + ) + self.assertFalse(prime_check(2 * 2)) + self.assertFalse(prime_check(2 * 3)) + self.assertFalse(prime_check(3 * 3)) + self.assertFalse(prime_check(3 * 5)) + self.assertFalse(prime_check(3 * 5 * 7)) + + +if __name__ == "__main__": + unittest.main() diff --git a/maths/prime_factors.py b/maths/prime_factors.py new file mode 100644 index 000000000000..e520ae3a6d04 --- /dev/null +++ b/maths/prime_factors.py @@ -0,0 +1,52 @@ +""" +python/black : True +""" +from __future__ import annotations + + +def prime_factors(n: int) -> list[int]: + """ + Returns prime factors of n as a list. + + >>> prime_factors(0) + [] + >>> prime_factors(100) + [2, 2, 5, 5] + >>> prime_factors(2560) + [2, 2, 2, 2, 2, 2, 2, 2, 2, 5] + >>> prime_factors(10**-2) + [] + >>> prime_factors(0.02) + [] + >>> x = prime_factors(10**241) # doctest: +NORMALIZE_WHITESPACE + >>> x == [2]*241 + [5]*241 + True + >>> prime_factors(10**-354) + [] + >>> prime_factors('hello') + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'str' + >>> prime_factors([1,2,'hello']) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'list' + + """ + i = 2 + factors = [] + while i * i <= n: + if n % i: + i += 1 + else: + n //= i + factors.append(i) + if n > 1: + factors.append(n) + return factors + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/prime_numbers.py b/maths/prime_numbers.py new file mode 100644 index 000000000000..38bebddeee41 --- /dev/null +++ b/maths/prime_numbers.py @@ -0,0 +1,70 @@ +import math +from typing import Generator + + +def slow_primes(max: int) -> Generator[int, None, None]: + """ + Return a list of all primes numbers up to max. + >>> list(slow_primes(0)) + [] + >>> list(slow_primes(-1)) + [] + >>> list(slow_primes(-10)) + [] + >>> list(slow_primes(25)) + [2, 3, 5, 7, 11, 13, 17, 19, 23] + >>> list(slow_primes(11)) + [2, 3, 5, 7, 11] + >>> list(slow_primes(33)) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] + >>> list(slow_primes(10000))[-1] + 9973 + """ + numbers: Generator = (i for i in range(1, (max + 1))) + for i in (n for n in numbers if n > 1): + for j in range(2, i): + if (i % j) == 0: + break + else: + yield i + + +def primes(max: int) -> Generator[int, None, None]: + """ + Return a list of all primes numbers up to max. + >>> list(primes(0)) + [] + >>> list(primes(-1)) + [] + >>> list(primes(-10)) + [] + >>> list(primes(25)) + [2, 3, 5, 7, 11, 13, 17, 19, 23] + >>> list(primes(11)) + [2, 3, 5, 7, 11] + >>> list(primes(33)) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] + >>> list(primes(10000))[-1] + 9973 + """ + numbers: Generator = (i for i in range(1, (max + 1))) + for i in (n for n in numbers if n > 1): + # only need to check for factors up to sqrt(i) + bound = int(math.sqrt(i)) + 1 + for j in range(2, bound): + if (i % j) == 0: + break + else: + yield i + + +if __name__ == "__main__": + number = int(input("Calculate primes up to:\n>> ").strip()) + for ret in primes(number): + print(ret) + + # Let's benchmark them side-by-side... + from timeit import timeit + + print(timeit("slow_primes(1_000_000)", setup="from __main__ import slow_primes")) + print(timeit("primes(1_000_000)", setup="from __main__ import primes")) diff --git a/maths/prime_sieve_eratosthenes.py b/maths/prime_sieve_eratosthenes.py new file mode 100644 index 000000000000..8d60e48c2140 --- /dev/null +++ b/maths/prime_sieve_eratosthenes.py @@ -0,0 +1,47 @@ +# flake8: noqa + +""" +Sieve of Eratosthenes + +Input : n =10 +Output: 2 3 5 7 + +Input : n = 20 +Output: 2 3 5 7 11 13 17 19 + +you can read in detail about this at +https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes +""" + + +def prime_sieve_eratosthenes(num): + """ + print the prime numbers up to n + + >>> prime_sieve_eratosthenes(10) + 2,3,5,7, + >>> prime_sieve_eratosthenes(20) + 2,3,5,7,11,13,17,19, + """ + + primes = [True for i in range(num + 1)] + p = 2 + + while p * p <= num: + if primes[p]: + for i in range(p * p, num + 1, p): + primes[i] = False + p += 1 + + for prime in range(2, num + 1): + if primes[prime]: + print(prime, end=",") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + num = int(input()) + + prime_sieve_eratosthenes(num) diff --git a/maths/pythagoras.py b/maths/pythagoras.py new file mode 100644 index 000000000000..69a17731a0fd --- /dev/null +++ b/maths/pythagoras.py @@ -0,0 +1,33 @@ +"""Uses Pythagoras theorem to calculate the distance between two points in space.""" + +import math + + +class Point: + def __init__(self, x, y, z): + self.x = x + self.y = y + self.z = z + + def __repr__(self) -> str: + return f"Point({self.x}, {self.y}, {self.z})" + + +def distance(a: Point, b: Point) -> float: + return math.sqrt(abs((b.x - a.x) ** 2 + (b.y - a.y) ** 2 + (b.z - a.z) ** 2)) + + +def test_distance() -> None: + """ + >>> point1 = Point(2, -1, 7) + >>> point2 = Point(1, -3, 5) + >>> print(f"Distance from {point1} to {point2} is {distance(point1, point2)}") + Distance from Point(2, -1, 7) to Point(1, -3, 5) is 3.0 + """ + pass + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/qr_decomposition.py b/maths/qr_decomposition.py new file mode 100644 index 000000000000..5e15fede4f2a --- /dev/null +++ b/maths/qr_decomposition.py @@ -0,0 +1,71 @@ +import numpy as np + + +def qr_householder(A): + """Return a QR-decomposition of the matrix A using Householder reflection. + + The QR-decomposition decomposes the matrix A of shape (m, n) into an + orthogonal matrix Q of shape (m, m) and an upper triangular matrix R of + shape (m, n). Note that the matrix A does not have to be square. This + method of decomposing A uses the Householder reflection, which is + numerically stable and of complexity O(n^3). + + https://en.wikipedia.org/wiki/QR_decomposition#Using_Householder_reflections + + Arguments: + A -- a numpy.ndarray of shape (m, n) + + Note: several optimizations can be made for numeric efficiency, but this is + intended to demonstrate how it would be represented in a mathematics + textbook. In cases where efficiency is particularly important, an optimized + version from BLAS should be used. + + >>> A = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=float) + >>> Q, R = qr_householder(A) + + >>> # check that the decomposition is correct + >>> np.allclose(Q@R, A) + True + + >>> # check that Q is orthogonal + >>> np.allclose(Q@Q.T, np.eye(A.shape[0])) + True + >>> np.allclose(Q.T@Q, np.eye(A.shape[0])) + True + + >>> # check that R is upper triangular + >>> np.allclose(np.triu(R), R) + True + """ + m, n = A.shape + t = min(m, n) + Q = np.eye(m) + R = A.copy() + + for k in range(t - 1): + # select a column of modified matrix A': + x = R[k:, [k]] + # construct first basis vector + e1 = np.zeros_like(x) + e1[0] = 1.0 + # determine scaling factor + alpha = np.linalg.norm(x) + # construct vector v for Householder reflection + v = x + np.sign(x[0]) * alpha * e1 + v /= np.linalg.norm(v) + + # construct the Householder matrix + Q_k = np.eye(m - k) - 2.0 * v @ v.T + # pad with ones and zeros as necessary + Q_k = np.block([[np.eye(k), np.zeros((k, m - k))], [np.zeros((m - k, k)), Q_k]]) + + Q = Q @ Q_k.T + R = Q_k @ R + + return Q, R + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/quadratic_equations_complex_numbers.py b/maths/quadratic_equations_complex_numbers.py new file mode 100644 index 000000000000..01a411bc560d --- /dev/null +++ b/maths/quadratic_equations_complex_numbers.py @@ -0,0 +1,38 @@ +from __future__ import annotations + +from cmath import sqrt + + +def quadratic_roots(a: int, b: int, c: int) -> tuple[complex, complex]: + """ + Given the numerical coefficients a, b and c, + calculates the roots for any quadratic equation of the form ax^2 + bx + c + + >>> quadratic_roots(a=1, b=3, c=-4) + (1.0, -4.0) + >>> quadratic_roots(5, 6, 1) + (-0.2, -1.0) + >>> quadratic_roots(1, -6, 25) + ((3+4j), (3-4j)) + """ + + if a == 0: + raise ValueError("Coefficient 'a' must not be zero.") + delta = b * b - 4 * a * c + + root_1 = (-b + sqrt(delta)) / (2 * a) + root_2 = (-b - sqrt(delta)) / (2 * a) + + return ( + root_1.real if not root_1.imag else root_1, + root_2.real if not root_2.imag else root_2, + ) + + +def main(): + solutions = quadratic_roots(a=5, b=6, c=1) + print("The solutions are: {} and {}".format(*solutions)) + + +if __name__ == "__main__": + main() diff --git a/maths/radians.py b/maths/radians.py new file mode 100644 index 000000000000..465467a3ba08 --- /dev/null +++ b/maths/radians.py @@ -0,0 +1,29 @@ +from math import pi + + +def radians(degree: float) -> float: + """ + Coverts the given angle from degrees to radians + https://en.wikipedia.org/wiki/Radian + + >>> radians(180) + 3.141592653589793 + >>> radians(92) + 1.6057029118347832 + >>> radians(274) + 4.782202150464463 + >>> radians(109.82) + 1.9167205845401725 + + >>> from math import radians as math_radians + >>> all(abs(radians(i)-math_radians(i)) <= 0.00000001 for i in range(-2, 361)) + True + """ + + return degree / (180 / pi) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/radix2_fft.py b/maths/radix2_fft.py new file mode 100644 index 000000000000..de87071e5440 --- /dev/null +++ b/maths/radix2_fft.py @@ -0,0 +1,180 @@ +""" +Fast Polynomial Multiplication using radix-2 fast Fourier Transform. +""" + +import mpmath # for roots of unity +import numpy as np + + +class FFT: + """ + Fast Polynomial Multiplication using radix-2 fast Fourier Transform. + + Reference: + https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm#The_radix-2_DIT_case + + For polynomials of degree m and n the algorithms has complexity + O(n*logn + m*logm) + + The main part of the algorithm is split in two parts: + 1) __DFT: We compute the discrete fourier transform (DFT) of A and B using a + bottom-up dynamic approach - + 2) __multiply: Once we obtain the DFT of A*B, we can similarly + invert it to obtain A*B + + The class FFT takes two polynomials A and B with complex coefficients as arguments; + The two polynomials should be represented as a sequence of coefficients starting + from the free term. Thus, for instance x + 2*x^3 could be represented as + [0,1,0,2] or (0,1,0,2). The constructor adds some zeros at the end so that the + polynomials have the same length which is a power of 2 at least the length of + their product. + + Example: + + Create two polynomials as sequences + >>> A = [0, 1, 0, 2] # x+2x^3 + >>> B = (2, 3, 4, 0) # 2+3x+4x^2 + + Create an FFT object with them + >>> x = FFT(A, B) + + Print product + >>> print(x.product) # 2x + 3x^2 + 8x^3 + 4x^4 + 6x^5 + [(-0+0j), (2+0j), (3+0j), (8+0j), (6+0j), (8+0j)] + + __str__ test + >>> print(x) + A = 0*x^0 + 1*x^1 + 2*x^0 + 3*x^2 + B = 0*x^2 + 1*x^3 + 2*x^4 + A*B = 0*x^(-0+0j) + 1*x^(2+0j) + 2*x^(3+0j) + 3*x^(8+0j) + 4*x^(6+0j) + 5*x^(8+0j) + """ + + def __init__(self, polyA=[0], polyB=[0]): + # Input as list + self.polyA = list(polyA)[:] + self.polyB = list(polyB)[:] + + # Remove leading zero coefficients + while self.polyA[-1] == 0: + self.polyA.pop() + self.len_A = len(self.polyA) + + while self.polyB[-1] == 0: + self.polyB.pop() + self.len_B = len(self.polyB) + + # Add 0 to make lengths equal a power of 2 + self.C_max_length = int( + 2 ** np.ceil(np.log2(len(self.polyA) + len(self.polyB) - 1)) + ) + + while len(self.polyA) < self.C_max_length: + self.polyA.append(0) + while len(self.polyB) < self.C_max_length: + self.polyB.append(0) + # A complex root used for the fourier transform + self.root = complex(mpmath.root(x=1, n=self.C_max_length, k=1)) + + # The product + self.product = self.__multiply() + + # Discrete fourier transform of A and B + def __DFT(self, which): + if which == "A": + dft = [[x] for x in self.polyA] + else: + dft = [[x] for x in self.polyB] + # Corner case + if len(dft) <= 1: + return dft[0] + # + next_ncol = self.C_max_length // 2 + while next_ncol > 0: + new_dft = [[] for i in range(next_ncol)] + root = self.root ** next_ncol + + # First half of next step + current_root = 1 + for j in range(self.C_max_length // (next_ncol * 2)): + for i in range(next_ncol): + new_dft[i].append(dft[i][j] + current_root * dft[i + next_ncol][j]) + current_root *= root + # Second half of next step + current_root = 1 + for j in range(self.C_max_length // (next_ncol * 2)): + for i in range(next_ncol): + new_dft[i].append(dft[i][j] - current_root * dft[i + next_ncol][j]) + current_root *= root + # Update + dft = new_dft + next_ncol = next_ncol // 2 + return dft[0] + + # multiply the DFTs of A and B and find A*B + def __multiply(self): + dftA = self.__DFT("A") + dftB = self.__DFT("B") + inverseC = [[dftA[i] * dftB[i] for i in range(self.C_max_length)]] + del dftA + del dftB + + # Corner Case + if len(inverseC[0]) <= 1: + return inverseC[0] + # Inverse DFT + next_ncol = 2 + while next_ncol <= self.C_max_length: + new_inverseC = [[] for i in range(next_ncol)] + root = self.root ** (next_ncol // 2) + current_root = 1 + # First half of next step + for j in range(self.C_max_length // next_ncol): + for i in range(next_ncol // 2): + # Even positions + new_inverseC[i].append( + ( + inverseC[i][j] + + inverseC[i][j + self.C_max_length // next_ncol] + ) + / 2 + ) + # Odd positions + new_inverseC[i + next_ncol // 2].append( + ( + inverseC[i][j] + - inverseC[i][j + self.C_max_length // next_ncol] + ) + / (2 * current_root) + ) + current_root *= root + # Update + inverseC = new_inverseC + next_ncol *= 2 + # Unpack + inverseC = [round(x[0].real, 8) + round(x[0].imag, 8) * 1j for x in inverseC] + + # Remove leading 0's + while inverseC[-1] == 0: + inverseC.pop() + return inverseC + + # Overwrite __str__ for print(); Shows A, B and A*B + def __str__(self): + A = "A = " + " + ".join( + f"{coef}*x^{i}" for coef, i in enumerate(self.polyA[: self.len_A]) + ) + B = "B = " + " + ".join( + f"{coef}*x^{i}" for coef, i in enumerate(self.polyB[: self.len_B]) + ) + C = "A*B = " + " + ".join( + f"{coef}*x^{i}" for coef, i in enumerate(self.product) + ) + + return "\n".join((A, B, C)) + + +# Unit tests +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/relu.py b/maths/relu.py new file mode 100644 index 000000000000..458c6bd5c391 --- /dev/null +++ b/maths/relu.py @@ -0,0 +1,40 @@ +""" +This script demonstrates the implementation of the ReLU function. + +It's a kind of activation function defined as the positive part of its argument in the +context of neural network. +The function takes a vector of K real numbers as input and then argmax(x, 0). +After through ReLU, the element of the vector always 0 or real number. + +Script inspired from its corresponding Wikipedia article +https://en.wikipedia.org/wiki/Rectifier_(neural_networks) +""" +from __future__ import annotations + +import numpy as np + + +def relu(vector: list[float]): + """ + Implements the relu function + + Parameters: + vector (np.array,list,tuple): A numpy array of shape (1,n) + consisting of real values or a similar list,tuple + + + Returns: + relu_vec (np.array): The input numpy array, after applying + relu. + + >>> vec = np.array([-1, 0, 5]) + >>> relu(vec) + array([0, 0, 5]) + """ + + # compare two arrays and then return element-wise maxima. + return np.maximum(0, vector) + + +if __name__ == "__main__": + print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5] diff --git a/maths/runge_kutta.py b/maths/runge_kutta.py new file mode 100644 index 000000000000..383797daa5ac --- /dev/null +++ b/maths/runge_kutta.py @@ -0,0 +1,44 @@ +import numpy as np + + +def runge_kutta(f, y0, x0, h, x_end): + """ + Calculate the numeric solution at each step to the ODE f(x, y) using RK4 + + https://en.wikipedia.org/wiki/Runge-Kutta_methods + + Arguments: + f -- The ode as a function of x and y + y0 -- the initial value for y + x0 -- the initial value for x + h -- the stepsize + x_end -- the end value for x + + >>> # the exact solution is math.exp(x) + >>> def f(x, y): + ... return y + >>> y0 = 1 + >>> y = runge_kutta(f, y0, 0.0, 0.01, 5) + >>> y[-1] + 148.41315904125113 + """ + N = int(np.ceil((x_end - x0) / h)) + y = np.zeros((N + 1,)) + y[0] = y0 + x = x0 + + for k in range(N): + k1 = f(x, y[k]) + k2 = f(x + 0.5 * h, y[k] + 0.5 * h * k1) + k3 = f(x + 0.5 * h, y[k] + 0.5 * h * k2) + k4 = f(x + h, y[k] + h * k3) + y[k + 1] = y[k] + (1 / 6) * h * (k1 + 2 * k2 + 2 * k3 + k4) + x += h + + return y + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/segmented_sieve.py b/maths/segmented_sieve.py index 52ca6fbe601d..c1cc497ad33e 100644 --- a/maths/segmented_sieve.py +++ b/maths/segmented_sieve.py @@ -1,46 +1,51 @@ +"""Segmented Sieve.""" + import math + def sieve(n): + """Segmented Sieve.""" in_prime = [] start = 2 - end = int(math.sqrt(n)) # Size of every segment + end = int(math.sqrt(n)) # Size of every segment temp = [True] * (end + 1) prime = [] - - while(start <= end): - if temp[start] == True: + + while start <= end: + if temp[start] is True: in_prime.append(start) - for i in range(start*start, end+1, start): - if temp[i] == True: + for i in range(start * start, end + 1, start): + if temp[i] is True: temp[i] = False start += 1 prime += in_prime - + low = end + 1 high = low + end - 1 if high > n: high = n - - while(low <= n): - temp = [True] * (high-low+1) + + while low <= n: + temp = [True] * (high - low + 1) for each in in_prime: - + t = math.floor(low / each) * each if t < low: t += each - - for j in range(t, high+1, each): + + for j in range(t, high + 1, each): temp[j - low] = False - + for j in range(len(temp)): - if temp[j] == True: - prime.append(j+low) - + if temp[j] is True: + prime.append(j + low) + low = high + 1 high = low + end - 1 if high > n: high = n - + return prime -print(sieve(10**6)) \ No newline at end of file + +print(sieve(10 ** 6)) diff --git a/maths/series/__init__.py b/maths/series/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py new file mode 100644 index 000000000000..d12382e6d8c4 --- /dev/null +++ b/maths/series/geometric_series.py @@ -0,0 +1,63 @@ +""" +This is a pure Python implementation of the Geometric Series algorithm +https://en.wikipedia.org/wiki/Geometric_series + +Run the doctests with the following command: +python3 -m doctest -v geometric_series.py +or +python -m doctest -v geometric_series.py +For manual testing run: +python3 geometric_series.py +""" + + +def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> list: + """Pure Python implementation of Geometric Series algorithm + :param nth_term: The last term (nth term of Geometric Series) + :param start_term_a : The first term of Geometric Series + :param common_ratio_r : The common ratio between all the terms + :return: The Geometric Series starting from first term a and multiple of common + ration with first term with increase in power till last term (nth term) + Examples: + >>> geometric_series(4, 2, 2) + [2, '4.0', '8.0', '16.0'] + >>> geometric_series(4.0, 2.0, 2.0) + [2.0, '4.0', '8.0', '16.0'] + >>> geometric_series(4.1, 2.1, 2.1) + [2.1, '4.41', '9.261000000000001', '19.448100000000004'] + >>> geometric_series(4, 2, -2) + [2, '-4.0', '8.0', '-16.0'] + >>> geometric_series(4, -2, 2) + [-2, '-4.0', '-8.0', '-16.0'] + >>> geometric_series(-4, 2, 2) + [] + >>> geometric_series(0, 100, 500) + [] + >>> geometric_series(1, 1, 1) + [1] + >>> geometric_series(0, 0, 0) + [] + """ + if "" in (nth_term, start_term_a, common_ratio_r): + return "" + series = [] + power = 1 + multiple = common_ratio_r + for _ in range(int(nth_term)): + if series == []: + series.append(start_term_a) + else: + power += 1 + series.append(str(float(start_term_a) * float(multiple))) + multiple = pow(float(common_ratio_r), power) + return series + + +if __name__ == "__main__": + nth_term = input("Enter the last number (n term) of the Geometric Series") + start_term_a = input("Enter the starting term (a) of the Geometric Series") + common_ratio_r = input( + "Enter the common ratio between two terms (r) of the Geometric Series" + ) + print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n") + print(geometric_series(nth_term, start_term_a, common_ratio_r)) diff --git a/maths/series/harmonic_series.py b/maths/series/harmonic_series.py new file mode 100644 index 000000000000..91b5944583e4 --- /dev/null +++ b/maths/series/harmonic_series.py @@ -0,0 +1,46 @@ +""" +This is a pure Python implementation of the Harmonic Series algorithm +https://en.wikipedia.org/wiki/Harmonic_series_(mathematics) + +For doctests run following command: +python -m doctest -v harmonic_series.py +or +python3 -m doctest -v harmonic_series.py + +For manual testing run: +python3 harmonic_series.py +""" + + +def harmonic_series(n_term: str) -> list: + """Pure Python implementation of Harmonic Series algorithm + + :param n_term: The last (nth) term of Harmonic Series + :return: The Harmonic Series starting from 1 to last (nth) term + + Examples: + >>> harmonic_series(5) + ['1', '1/2', '1/3', '1/4', '1/5'] + >>> harmonic_series(5.0) + ['1', '1/2', '1/3', '1/4', '1/5'] + >>> harmonic_series(5.1) + ['1', '1/2', '1/3', '1/4', '1/5'] + >>> harmonic_series(-5) + [] + >>> harmonic_series(0) + [] + >>> harmonic_series(1) + ['1'] + """ + if n_term == "": + return n_term + series = [] + for temp in range(int(n_term)): + series.append(f"1/{temp + 1}" if series else "1") + return series + + +if __name__ == "__main__": + nth_term = input("Enter the last number (nth term) of the Harmonic Series") + print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") + print(harmonic_series(nth_term)) diff --git a/maths/series/p_series.py b/maths/series/p_series.py new file mode 100644 index 000000000000..04019aed5a85 --- /dev/null +++ b/maths/series/p_series.py @@ -0,0 +1,48 @@ +""" +This is a pure Python implementation of the P-Series algorithm +https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#P-series + +For doctests run following command: +python -m doctest -v p_series.py +or +python3 -m doctest -v p_series.py + +For manual testing run: +python3 p_series.py +""" + + +def p_series(nth_term: int, power: int) -> list: + """Pure Python implementation of P-Series algorithm + + :return: The P-Series starting from 1 to last (nth) term + + Examples: + >>> p_series(5, 2) + [1, '1/4', '1/9', '1/16', '1/25'] + >>> p_series(-5, 2) + [] + >>> p_series(5, -2) + [1, '1/0.25', '1/0.1111111111111111', '1/0.0625', '1/0.04'] + >>> p_series("", 1000) + '' + >>> p_series(0, 0) + [] + >>> p_series(1, 1) + [1] + """ + if nth_term == "": + return nth_term + nth_term = int(nth_term) + power = int(power) + series = [] + for temp in range(int(nth_term)): + series.append(f"1/{pow(temp + 1, int(power))}" if series else 1) + return series + + +if __name__ == "__main__": + nth_term = input("Enter the last number (nth term) of the P-Series") + power = input("Enter the power for P-Series") + print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") + print(p_series(nth_term, power)) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index 26c17fa6ffec..47a086546900 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -1,24 +1,65 @@ +""" +Sieve of Eratosthones + +The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or +equal to a given value. +Illustration: +https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif +Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + +doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich) +Also thanks to Dmitry (https://github.com/LizardWizzard) for finding the problem +""" + + import math -n = int(input("Enter n: ")) +from typing import List + + +def prime_sieve(num: int) -> List[int]: + """ + Returns a list with all prime numbers up to n. + + >>> prime_sieve(50) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] + >>> prime_sieve(25) + [2, 3, 5, 7, 11, 13, 17, 19, 23] + >>> prime_sieve(10) + [2, 3, 5, 7] + >>> prime_sieve(9) + [2, 3, 5, 7] + >>> prime_sieve(2) + [2] + >>> prime_sieve(1) + [] + """ -def sieve(n): - l = [True] * (n+1) + if num <= 0: + raise ValueError(f"{num}: Invalid input, please enter a positive integer.") + + sieve = [True] * (num + 1) prime = [] start = 2 - end = int(math.sqrt(n)) - while(start <= end): - if l[start] == True: + end = int(math.sqrt(num)) + + while start <= end: + # If start is a prime + if sieve[start] is True: prime.append(start) - for i in range(start*start, n+1, start): - if l[i] == True: - l[i] = False + + # Set multiples of start be False + for i in range(start * start, num + 1, start): + if sieve[i] is True: + sieve[i] = False + start += 1 - - for j in range(end+1,n+1): - if l[j] == True: + + for j in range(end + 1, num + 1): + if sieve[j] is True: prime.append(j) - + return prime -print(sieve(n)) - + +if __name__ == "__main__": + print(prime_sieve(int(input("Enter a positive integer: ").strip()))) diff --git a/maths/sigmoid.py b/maths/sigmoid.py new file mode 100644 index 000000000000..147588e8871f --- /dev/null +++ b/maths/sigmoid.py @@ -0,0 +1,39 @@ +""" +This script demonstrates the implementation of the Sigmoid function. + +The function takes a vector of K real numbers as input and then 1 / (1 + exp(-x)). +After through Sigmoid, the element of the vector mostly 0 between 1. or 1 between -1. + +Script inspired from its corresponding Wikipedia article +https://en.wikipedia.org/wiki/Sigmoid_function +""" + +import numpy as np + + +def sigmoid(vector: np.array) -> np.array: + """ + Implements the sigmoid function + + Parameters: + vector (np.array): A numpy array of shape (1,n) + consisting of real values + + Returns: + sigmoid_vec (np.array): The input numpy array, after applying + sigmoid. + + Examples: + >>> sigmoid(np.array([-1.0, 1.0, 2.0])) + array([0.26894142, 0.73105858, 0.88079708]) + + >>> sigmoid(np.array([0.0])) + array([0.5]) + """ + return 1 / (1 + np.exp(-vector)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py index 091c86c17f1b..d66dc39a7171 100644 --- a/maths/simpson_rule.py +++ b/maths/simpson_rule.py @@ -1,49 +1,51 @@ - -''' +""" Numerical integration or quadrature for a smooth function f with known values at x_i -This method is the classical approch of suming 'Equally Spaced Abscissas' +This method is the classical approach of suming 'Equally Spaced Abscissas' -method 2: +method 2: "Simpson Rule" -''' -from __future__ import print_function +""" def method_2(boundary, steps): -# "Simpson Rule" -# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) - h = (boundary[1] - boundary[0]) / steps - a = boundary[0] - b = boundary[1] - x_i = makePoints(a,b,h) - y = 0.0 - y += (h/3.0)*f(a) - cnt = 2 - for i in x_i: - y += (h/3)*(4-2*(cnt%2))*f(i) - cnt += 1 - y += (h/3.0)*f(b) - return y - -def makePoints(a,b,h): - x = a + h - while x < (b-h): - yield x - x = x + h - -def f(x): #enter your function here - y = (x-0)*(x-0) - return y + # "Simpson Rule" + # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) + h = (boundary[1] - boundary[0]) / steps + a = boundary[0] + b = boundary[1] + x_i = make_points(a, b, h) + y = 0.0 + y += (h / 3.0) * f(a) + cnt = 2 + for i in x_i: + y += (h / 3) * (4 - 2 * (cnt % 2)) * f(i) + cnt += 1 + y += (h / 3.0) * f(b) + return y + + +def make_points(a, b, h): + x = a + h + while x < (b - h): + yield x + x = x + h + + +def f(x): # enter your function here + y = (x - 0) * (x - 0) + return y + def main(): - a = 0.0 #Lower bound of integration - b = 1.0 #Upper bound of integration - steps = 10.0 #define number of steps or resolution - boundary = [a, b] #define boundary of integration - y = method_2(boundary, steps) - print('y = {0}'.format(y)) - -if __name__ == '__main__': - main() + a = 0.0 # Lower bound of integration + b = 1.0 # Upper bound of integration + steps = 10.0 # define number of steps or resolution + boundary = [a, b] # define boundary of integration + y = method_2(boundary, steps) + print(f"y = {y}") + + +if __name__ == "__main__": + main() diff --git a/maths/softmax.py b/maths/softmax.py new file mode 100644 index 000000000000..e021a7f8a6fe --- /dev/null +++ b/maths/softmax.py @@ -0,0 +1,56 @@ +""" +This script demonstrates the implementation of the Softmax function. + +Its a function that takes as input a vector of K real numbers, and normalizes +it into a probability distribution consisting of K probabilities proportional +to the exponentials of the input numbers. After softmax, the elements of the +vector always sum up to 1. + +Script inspired from its corresponding Wikipedia article +https://en.wikipedia.org/wiki/Softmax_function +""" + +import numpy as np + + +def softmax(vector): + """ + Implements the softmax function + + Parameters: + vector (np.array,list,tuple): A numpy array of shape (1,n) + consisting of real values or a similar list,tuple + + + Returns: + softmax_vec (np.array): The input numpy array after applying + softmax. + + The softmax vector adds up to one. We need to ceil to mitigate for + precision + >>> np.ceil(np.sum(softmax([1,2,3,4]))) + 1.0 + + >>> vec = np.array([5,5]) + >>> softmax(vec) + array([0.5, 0.5]) + + >>> softmax([0]) + array([1.]) + """ + + # Calculate e^x for each x in your vector where e is Euler's + # number (approximately 2.718) + exponentVector = np.exp(vector) + + # Add up the all the exponentials + sumOfExponents = np.sum(exponentVector) + + # Divide every exponent by the sum of all exponents + softmax_vector = exponentVector / sumOfExponents + + return softmax_vector + + +if __name__ == "__main__": + print(softmax((0,))) diff --git a/maths/square_root.py b/maths/square_root.py new file mode 100644 index 000000000000..b324c723037c --- /dev/null +++ b/maths/square_root.py @@ -0,0 +1,64 @@ +import math + + +def fx(x: float, a: float) -> float: + return math.pow(x, 2) - a + + +def fx_derivative(x: float) -> float: + return 2 * x + + +def get_initial_point(a: float) -> float: + start = 2.0 + + while start <= a: + start = math.pow(start, 2) + + return start + + +def square_root_iterative( + a: float, max_iter: int = 9999, tolerance: float = 0.00000000000001 +) -> float: + """ + Square root is aproximated using Newtons method. + https://en.wikipedia.org/wiki/Newton%27s_method + + >>> all(abs(square_root_iterative(i)-math.sqrt(i)) <= .00000000000001 + ... for i in range(500)) + True + + >>> square_root_iterative(-1) + Traceback (most recent call last): + ... + ValueError: math domain error + + >>> square_root_iterative(4) + 2.0 + + >>> square_root_iterative(3.2) + 1.788854381999832 + + >>> square_root_iterative(140) + 11.832159566199232 + """ + + if a < 0: + raise ValueError("math domain error") + + value = get_initial_point(a) + + for i in range(max_iter): + prev_value = value + value = value - fx(value, a) / fx_derivative(value) + if abs(prev_value - value) < tolerance: + return value + + return value + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/sum_of_arithmetic_series.py b/maths/sum_of_arithmetic_series.py new file mode 100644 index 000000000000..74eef0f18a12 --- /dev/null +++ b/maths/sum_of_arithmetic_series.py @@ -0,0 +1,23 @@ +# DarkCoder +def sum_of_series(first_term, common_diff, num_of_terms): + """ + Find the sum of n terms in an arithmetic progression. + + >>> sum_of_series(1, 1, 10) + 55.0 + >>> sum_of_series(1, 10, 100) + 49600.0 + """ + sum = (num_of_terms / 2) * (2 * first_term + (num_of_terms - 1) * common_diff) + # formula for sum of series + return sum + + +def main(): + print(sum_of_series(1, 1, 10)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/sum_of_digits.py b/maths/sum_of_digits.py new file mode 100644 index 000000000000..64da00d4634c --- /dev/null +++ b/maths/sum_of_digits.py @@ -0,0 +1,149 @@ +from timeit import timeit + + +def sum_of_digits(n: int) -> int: + """ + Find the sum of digits of a number. + + >>> sum_of_digits(12345) + 15 + >>> sum_of_digits(123) + 6 + >>> sum_of_digits(-123) + 6 + >>> sum_of_digits(0) + 0 + """ + n = -n if n < 0 else n + res = 0 + while n > 0: + res += n % 10 + n = n // 10 + return res + + +def sum_of_digits_recursion(n: int) -> int: + """ + Find the sum of digits of a number using recursion + + >>> sum_of_digits_recursion(12345) + 15 + >>> sum_of_digits_recursion(123) + 6 + >>> sum_of_digits_recursion(-123) + 6 + >>> sum_of_digits_recursion(0) + 0 + """ + n = -n if n < 0 else n + return n if n < 10 else n % 10 + sum_of_digits(n // 10) + + +def sum_of_digits_compact(n: int) -> int: + """ + Find the sum of digits of a number + + >>> sum_of_digits_compact(12345) + 15 + >>> sum_of_digits_compact(123) + 6 + >>> sum_of_digits_compact(-123) + 6 + >>> sum_of_digits_compact(0) + 0 + """ + return sum(int(c) for c in str(abs(n))) + + +def benchmark() -> None: + """ + Benchmark code for comparing 3 functions, + with 3 different length int values. + """ + print("\nFor small_num = ", small_num, ":") + print( + "> sum_of_digits()", + "\t\tans =", + sum_of_digits(small_num), + "\ttime =", + timeit("z.sum_of_digits(z.small_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> sum_of_digits_recursion()", + "\tans =", + sum_of_digits_recursion(small_num), + "\ttime =", + timeit("z.sum_of_digits_recursion(z.small_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> sum_of_digits_compact()", + "\tans =", + sum_of_digits_compact(small_num), + "\ttime =", + timeit("z.sum_of_digits_compact(z.small_num)", setup="import __main__ as z"), + "seconds", + ) + + print("\nFor medium_num = ", medium_num, ":") + print( + "> sum_of_digits()", + "\t\tans =", + sum_of_digits(medium_num), + "\ttime =", + timeit("z.sum_of_digits(z.medium_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> sum_of_digits_recursion()", + "\tans =", + sum_of_digits_recursion(medium_num), + "\ttime =", + timeit("z.sum_of_digits_recursion(z.medium_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> sum_of_digits_compact()", + "\tans =", + sum_of_digits_compact(medium_num), + "\ttime =", + timeit("z.sum_of_digits_compact(z.medium_num)", setup="import __main__ as z"), + "seconds", + ) + + print("\nFor large_num = ", large_num, ":") + print( + "> sum_of_digits()", + "\t\tans =", + sum_of_digits(large_num), + "\ttime =", + timeit("z.sum_of_digits(z.large_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> sum_of_digits_recursion()", + "\tans =", + sum_of_digits_recursion(large_num), + "\ttime =", + timeit("z.sum_of_digits_recursion(z.large_num)", setup="import __main__ as z"), + "seconds", + ) + print( + "> sum_of_digits_compact()", + "\tans =", + sum_of_digits_compact(large_num), + "\ttime =", + timeit("z.sum_of_digits_compact(z.large_num)", setup="import __main__ as z"), + "seconds", + ) + + +if __name__ == "__main__": + small_num = 262144 + medium_num = 1125899906842624 + large_num = 1267650600228229401496703205376 + benchmark() + import doctest + + doctest.testmod() diff --git a/maths/sum_of_geometric_progression.py b/maths/sum_of_geometric_progression.py new file mode 100644 index 000000000000..f29dd8005cff --- /dev/null +++ b/maths/sum_of_geometric_progression.py @@ -0,0 +1,28 @@ +def sum_of_geometric_progression( + first_term: int, common_ratio: int, num_of_terms: int +) -> float: + """ " + Return the sum of n terms in a geometric progression. + >>> sum_of_geometric_progression(1, 2, 10) + 1023.0 + >>> sum_of_geometric_progression(1, 10, 5) + 11111.0 + >>> sum_of_geometric_progression(0, 2, 10) + 0.0 + >>> sum_of_geometric_progression(1, 0, 10) + 1.0 + >>> sum_of_geometric_progression(1, 2, 0) + -0.0 + >>> sum_of_geometric_progression(-1, 2, 10) + -1023.0 + >>> sum_of_geometric_progression(1, -2, 10) + -341.0 + >>> sum_of_geometric_progression(1, 2, -10) + -0.9990234375 + """ + if common_ratio == 1: + # Formula for sum if common ratio is 1 + return num_of_terms * first_term + + # Formula for finding sum of n terms of a GeometricProgression + return (first_term / (1 - common_ratio)) * (1 - common_ratio ** num_of_terms) diff --git a/maths/test_prime_check.py b/maths/test_prime_check.py new file mode 100644 index 000000000000..b6389684af9e --- /dev/null +++ b/maths/test_prime_check.py @@ -0,0 +1,8 @@ +""" +Minimalist file that allows pytest to find and run the Test unittest. For details, see: +http://doc.pytest.org/en/latest/goodpractices.html#conventions-for-python-test-discovery +""" + +from .prime_check import Test + +Test() diff --git a/maths/trapezoidal_rule.py b/maths/trapezoidal_rule.py index 52310c1ed3b0..9a4ddc8af66b 100644 --- a/maths/trapezoidal_rule.py +++ b/maths/trapezoidal_rule.py @@ -1,46 +1,50 @@ -''' +""" Numerical integration or quadrature for a smooth function f with known values at x_i -This method is the classical approch of suming 'Equally Spaced Abscissas' +This method is the classical approach of suming 'Equally Spaced Abscissas' -method 1: +method 1: "extended trapezoidal rule" -''' -from __future__ import print_function +""" + def method_1(boundary, steps): -# "extended trapezoidal rule" -# int(f) = dx/2 * (f1 + 2f2 + ... + fn) - h = (boundary[1] - boundary[0]) / steps - a = boundary[0] - b = boundary[1] - x_i = makePoints(a,b,h) - y = 0.0 - y += (h/2.0)*f(a) - for i in x_i: - #print(i) - y += h*f(i) - y += (h/2.0)*f(b) - return y - -def makePoints(a,b,h): - x = a + h - while x < (b-h): - yield x - x = x + h - -def f(x): #enter your function here - y = (x-0)*(x-0) - return y + # "extended trapezoidal rule" + # int(f) = dx/2 * (f1 + 2f2 + ... + fn) + h = (boundary[1] - boundary[0]) / steps + a = boundary[0] + b = boundary[1] + x_i = make_points(a, b, h) + y = 0.0 + y += (h / 2.0) * f(a) + for i in x_i: + # print(i) + y += h * f(i) + y += (h / 2.0) * f(b) + return y + + +def make_points(a, b, h): + x = a + h + while x < (b - h): + yield x + x = x + h + + +def f(x): # enter your function here + y = (x - 0) * (x - 0) + return y + def main(): - a = 0.0 #Lower bound of integration - b = 1.0 #Upper bound of integration - steps = 10.0 #define number of steps or resolution - boundary = [a, b] #define boundary of integration - y = method_1(boundary, steps) - print('y = {0}'.format(y)) - -if __name__ == '__main__': - main() + a = 0.0 # Lower bound of integration + b = 1.0 # Upper bound of integration + steps = 10.0 # define number of steps or resolution + boundary = [a, b] # define boundary of integration + y = method_1(boundary, steps) + print(f"y = {y}") + + +if __name__ == "__main__": + main() diff --git a/maths/ugly_numbers.py b/maths/ugly_numbers.py new file mode 100644 index 000000000000..4451a68cdaad --- /dev/null +++ b/maths/ugly_numbers.py @@ -0,0 +1,54 @@ +""" +Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence +1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention, +1 is included. +Given an integer n, we have to find the nth ugly number. + +For more details, refer this article +https://www.geeksforgeeks.org/ugly-numbers/ +""" + + +def ugly_numbers(n: int) -> int: + """ + Returns the nth ugly number. + >>> ugly_numbers(100) + 1536 + >>> ugly_numbers(0) + 1 + >>> ugly_numbers(20) + 36 + >>> ugly_numbers(-5) + 1 + >>> ugly_numbers(-5.5) + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + """ + ugly_nums = [1] + + i2, i3, i5 = 0, 0, 0 + next_2 = ugly_nums[i2] * 2 + next_3 = ugly_nums[i3] * 3 + next_5 = ugly_nums[i5] * 5 + + for i in range(1, n): + next_num = min(next_2, next_3, next_5) + ugly_nums.append(next_num) + if next_num == next_2: + i2 += 1 + next_2 = ugly_nums[i2] * 2 + if next_num == next_3: + i3 += 1 + next_3 = ugly_nums[i3] * 3 + if next_num == next_5: + i5 += 1 + next_5 = ugly_nums[i5] * 5 + return ugly_nums[-1] + + +if __name__ == "__main__": + from doctest import testmod + + testmod(verbose=True) + print(f"{ugly_numbers(200) = }") diff --git a/maths/volume.py b/maths/volume.py new file mode 100644 index 000000000000..41d2331db3cb --- /dev/null +++ b/maths/volume.py @@ -0,0 +1,132 @@ +""" +Find Volumes of Various Shapes. + +Wikipedia reference: https://en.wikipedia.org/wiki/Volume +""" +from math import pi, pow +from typing import Union + + +def vol_cube(side_length: Union[int, float]) -> float: + """ + Calculate the Volume of a Cube. + + >>> vol_cube(1) + 1.0 + >>> vol_cube(3) + 27.0 + """ + return pow(side_length, 3) + + +def vol_cuboid(width: float, height: float, length: float) -> float: + """ + Calculate the Volume of a Cuboid. + :return multiple of width, length and height + + >>> vol_cuboid(1, 1, 1) + 1.0 + >>> vol_cuboid(1, 2, 3) + 6.0 + """ + return float(width * height * length) + + +def vol_cone(area_of_base: float, height: float) -> float: + """ + Calculate the Volume of a Cone. + + Wikipedia reference: https://en.wikipedia.org/wiki/Cone + :return (1/3) * area_of_base * height + + >>> vol_cone(10, 3) + 10.0 + >>> vol_cone(1, 1) + 0.3333333333333333 + """ + return area_of_base * height / 3.0 + + +def vol_right_circ_cone(radius: float, height: float) -> float: + """ + Calculate the Volume of a Right Circular Cone. + + Wikipedia reference: https://en.wikipedia.org/wiki/Cone + :return (1/3) * pi * radius^2 * height + + >>> vol_right_circ_cone(2, 3) + 12.566370614359172 + """ + return pi * pow(radius, 2) * height / 3.0 + + +def vol_prism(area_of_base: float, height: float) -> float: + """ + Calculate the Volume of a Prism. + Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry) + :return V = Bh + + >>> vol_prism(10, 2) + 20.0 + >>> vol_prism(11, 1) + 11.0 + """ + return float(area_of_base * height) + + +def vol_pyramid(area_of_base: float, height: float) -> float: + """ + Calculate the Volume of a Pyramid. + Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry) + :return (1/3) * Bh + + >>> vol_pyramid(10, 3) + 10.0 + >>> vol_pyramid(1.5, 3) + 1.5 + """ + return area_of_base * height / 3.0 + + +def vol_sphere(radius: float) -> float: + """ + Calculate the Volume of a Sphere. + Wikipedia reference: https://en.wikipedia.org/wiki/Sphere + :return (4/3) * pi * r^3 + + >>> vol_sphere(5) + 523.5987755982989 + >>> vol_sphere(1) + 4.1887902047863905 + """ + return 4 / 3 * pi * pow(radius, 3) + + +def vol_circular_cylinder(radius: float, height: float) -> float: + """Calculate the Volume of a Circular Cylinder. + Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder + :return pi * radius^2 * height + + >>> vol_circular_cylinder(1, 1) + 3.141592653589793 + >>> vol_circular_cylinder(4, 3) + 150.79644737231007 + """ + return pi * pow(radius, 2) * height + + +def main(): + """Print the Results of Various Volume Calculations.""" + print("Volumes:") + print("Cube: " + str(vol_cube(2))) # = 8 + print("Cuboid: " + str(vol_cuboid(2, 2, 2))) # = 8 + print("Cone: " + str(vol_cone(2, 2))) # ~= 1.33 + print("Right Circular Cone: " + str(vol_right_circ_cone(2, 2))) # ~= 8.38 + print("Prism: " + str(vol_prism(2, 2))) # = 4 + print("Pyramid: " + str(vol_pyramid(2, 2))) # ~= 1.33 + print("Sphere: " + str(vol_sphere(2))) # ~= 33.5 + print("Circular Cylinder: " + str(vol_circular_cylinder(2, 2))) # ~= 25.1 + + +if __name__ == "__main__": + main() diff --git a/maths/zellers_congruence.py b/maths/zellers_congruence.py new file mode 100644 index 000000000000..2d4a22a0a5ba --- /dev/null +++ b/maths/zellers_congruence.py @@ -0,0 +1,158 @@ +import argparse +import datetime + + +def zeller(date_input: str) -> str: + + """ + Zellers Congruence Algorithm + Find the day of the week for nearly any Gregorian or Julian calendar date + + >>> zeller('01-31-2010') + 'Your date 01-31-2010, is a Sunday!' + + Validate out of range month + >>> zeller('13-31-2010') + Traceback (most recent call last): + ... + ValueError: Month must be between 1 - 12 + >>> zeller('.2-31-2010') + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: '.2' + + Validate out of range date: + >>> zeller('01-33-2010') + Traceback (most recent call last): + ... + ValueError: Date must be between 1 - 31 + >>> zeller('01-.4-2010') + Traceback (most recent call last): + ... + ValueError: invalid literal for int() with base 10: '.4' + + Validate second separator: + >>> zeller('01-31*2010') + Traceback (most recent call last): + ... + ValueError: Date separator must be '-' or '/' + + Validate first separator: + >>> zeller('01^31-2010') + Traceback (most recent call last): + ... + ValueError: Date separator must be '-' or '/' + + Validate out of range year: + >>> zeller('01-31-8999') + Traceback (most recent call last): + ... + ValueError: Year out of range. There has to be some sort of limit...right? + + Test null input: + >>> zeller() + Traceback (most recent call last): + ... + TypeError: zeller() missing 1 required positional argument: 'date_input' + + Test length of date_input: + >>> zeller('') + Traceback (most recent call last): + ... + ValueError: Must be 10 characters long + >>> zeller('01-31-19082939') + Traceback (most recent call last): + ... + ValueError: Must be 10 characters long""" + + # Days of the week for response + days = { + "0": "Sunday", + "1": "Monday", + "2": "Tuesday", + "3": "Wednesday", + "4": "Thursday", + "5": "Friday", + "6": "Saturday", + } + + convert_datetime_days = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 0} + + # Validate + if not 0 < len(date_input) < 11: + raise ValueError("Must be 10 characters long") + + # Get month + m: int = int(date_input[0] + date_input[1]) + # Validate + if not 0 < m < 13: + raise ValueError("Month must be between 1 - 12") + + sep_1: str = date_input[2] + # Validate + if sep_1 not in ["-", "/"]: + raise ValueError("Date separator must be '-' or '/'") + + # Get day + d: int = int(date_input[3] + date_input[4]) + # Validate + if not 0 < d < 32: + raise ValueError("Date must be between 1 - 31") + + # Get second separator + sep_2: str = date_input[5] + # Validate + if sep_2 not in ["-", "/"]: + raise ValueError("Date separator must be '-' or '/'") + + # Get year + y: int = int(date_input[6] + date_input[7] + date_input[8] + date_input[9]) + # Arbitrary year range + if not 45 < y < 8500: + raise ValueError( + "Year out of range. There has to be some sort of limit...right?" + ) + + # Get datetime obj for validation + dt_ck = datetime.date(int(y), int(m), int(d)) + + # Start math + if m <= 2: + y = y - 1 + m = m + 12 + # maths var + c: int = int(str(y)[:2]) + k: int = int(str(y)[2:]) + t: int = int(2.6 * m - 5.39) + u: int = int(c / 4) + v: int = int(k / 4) + x: int = int(d + k) + z: int = int(t + u + v + x) + w: int = int(z - (2 * c)) + f: int = round(w % 7) + # End math + + # Validate math + if f != convert_datetime_days[dt_ck.weekday()]: + raise AssertionError("The date was evaluated incorrectly. Contact developer.") + + # Response + response: str = f"Your date {date_input}, is a {days[str(f)]}!" + return response + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + parser = argparse.ArgumentParser( + description=( + "Find out what day of the week nearly any date is or was. Enter " + "date as a string in the mm-dd-yyyy or mm/dd/yyyy format" + ) + ) + parser.add_argument( + "date_input", type=str, help="Date as a string (mm-dd-yyyy or mm/dd/yyyy)" + ) + args = parser.parse_args() + zeller(args.date_input) diff --git a/matrix/__init__.py b/matrix/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py new file mode 100644 index 000000000000..ad9c67fb8c1b --- /dev/null +++ b/matrix/count_islands_in_matrix.py @@ -0,0 +1,36 @@ +# An island in matrix is a group of linked areas, all having the same value. +# This code counts number of islands in a given matrix, with including diagonal +# connections. + + +class matrix: # Public class to implement a graph + def __init__(self, row: int, col: int, graph: list): + self.ROW = row + self.COL = col + self.graph = graph + + def is_safe(self, i, j, visited) -> bool: + return ( + 0 <= i < self.ROW + and 0 <= j < self.COL + and not visited[i][j] + and self.graph[i][j] + ) + + def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element + rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order + colNbr = [-1, 0, 1, -1, 1, -1, 0, 1] + visited[i][j] = True # Make those cells visited + for k in range(8): + if self.is_safe(i + rowNbr[k], j + colNbr[k], visited): + self.diffs(i + rowNbr[k], j + colNbr[k], visited) + + def count_islands(self) -> int: # And finally, count all islands. + visited = [[False for j in range(self.COL)] for i in range(self.ROW)] + count = 0 + for i in range(self.ROW): + for j in range(self.COL): + if visited[i][j] is False and self.graph[i][j] == 1: + self.diffs(i, j, visited) + count += 1 + return count diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py new file mode 100644 index 000000000000..9deca6c3c08e --- /dev/null +++ b/matrix/inverse_of_matrix.py @@ -0,0 +1,40 @@ +from __future__ import annotations + +from decimal import Decimal + + +def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: + """ + A matrix multiplied with its inverse gives the identity matrix. + This function finds the inverse of a 2x2 matrix. + If the determinant of a matrix is 0, its inverse does not exist. + + Sources for fixing inaccurate float arithmetic: + https://stackoverflow.com/questions/6563058/how-do-i-use-accurate-float-arithmetic-in-python + https://docs.python.org/3/library/decimal.html + + >>> inverse_of_matrix([[2, 5], [2, 0]]) + [[0.0, 0.5], [0.2, -0.2]] + >>> inverse_of_matrix([[2.5, 5], [1, 2]]) + Traceback (most recent call last): + ... + ValueError: This matrix has no inverse. + >>> inverse_of_matrix([[12, -16], [-9, 0]]) + [[0.0, -0.1111111111111111], [-0.0625, -0.08333333333333333]] + >>> inverse_of_matrix([[12, 3], [16, 8]]) + [[0.16666666666666666, -0.0625], [-0.3333333333333333, 0.25]] + >>> inverse_of_matrix([[10, 5], [3, 2.5]]) + [[0.25, -0.5], [-0.3, 1.0]] + """ + + D = Decimal # An abbreviation to be conciseness + # Calculate the determinant of the matrix + determinant = D(matrix[0][0]) * D(matrix[1][1]) - D(matrix[1][0]) * D(matrix[0][1]) + if determinant == 0: + raise ValueError("This matrix has no inverse.") + # Creates a copy of the matrix with swapped positions of the elements + swapped_matrix = [[0.0, 0.0], [0.0, 0.0]] + swapped_matrix[0][0], swapped_matrix[1][1] = matrix[1][1], matrix[0][0] + swapped_matrix[1][0], swapped_matrix[0][1] = -matrix[1][0], -matrix[0][1] + # Calculate the inverse of the matrix + return [[float(D(n) / determinant) or 0.0 for n in row] for row in swapped_matrix] diff --git a/matrix/matrix_class.py b/matrix/matrix_class.py new file mode 100644 index 000000000000..57a2fc45ffd1 --- /dev/null +++ b/matrix/matrix_class.py @@ -0,0 +1,358 @@ +# An OOP approach to representing and manipulating matrices + + +class Matrix: + """ + Matrix object generated from a 2D array where each element is an array representing + a row. + Rows can contain type int or float. + Common operations and information available. + >>> rows = [ + ... [1, 2, 3], + ... [4, 5, 6], + ... [7, 8, 9] + ... ] + >>> matrix = Matrix(rows) + >>> print(matrix) + [[1. 2. 3.] + [4. 5. 6.] + [7. 8. 9.]] + + Matrix rows and columns are available as 2D arrays + >>> print(matrix.rows) + [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + >>> print(matrix.columns()) + [[1, 4, 7], [2, 5, 8], [3, 6, 9]] + + Order is returned as a tuple + >>> matrix.order + (3, 3) + + Squareness and invertability are represented as bool + >>> matrix.is_square + True + >>> matrix.is_invertable() + False + + Identity, Minors, Cofactors and Adjugate are returned as Matrices. Inverse can be + a Matrix or Nonetype + >>> print(matrix.identity()) + [[1. 0. 0.] + [0. 1. 0.] + [0. 0. 1.]] + >>> print(matrix.minors()) + [[-3. -6. -3.] + [-6. -12. -6.] + [-3. -6. -3.]] + >>> print(matrix.cofactors()) + [[-3. 6. -3.] + [6. -12. 6.] + [-3. 6. -3.]] + >>> # won't be apparent due to the nature of the cofactor matrix + >>> print(matrix.adjugate()) + [[-3. 6. -3.] + [6. -12. 6.] + [-3. 6. -3.]] + >>> print(matrix.inverse()) + None + + Determinant is an int, float, or Nonetype + >>> matrix.determinant() + 0 + + Negation, scalar multiplication, addition, subtraction, multiplication and + exponentiation are available and all return a Matrix + >>> print(-matrix) + [[-1. -2. -3.] + [-4. -5. -6.] + [-7. -8. -9.]] + >>> matrix2 = matrix * 3 + >>> print(matrix2) + [[3. 6. 9.] + [12. 15. 18.] + [21. 24. 27.]] + >>> print(matrix + matrix2) + [[4. 8. 12.] + [16. 20. 24.] + [28. 32. 36.]] + >>> print(matrix - matrix2) + [[-2. -4. -6.] + [-8. -10. -12.] + [-14. -16. -18.]] + >>> print(matrix ** 3) + [[468. 576. 684.] + [1062. 1305. 1548.] + [1656. 2034. 2412.]] + + Matrices can also be modified + >>> matrix.add_row([10, 11, 12]) + >>> print(matrix) + [[1. 2. 3.] + [4. 5. 6.] + [7. 8. 9.] + [10. 11. 12.]] + >>> matrix2.add_column([8, 16, 32]) + >>> print(matrix2) + [[3. 6. 9. 8.] + [12. 15. 18. 16.] + [21. 24. 27. 32.]] + >>> print(matrix * matrix2) + [[90. 108. 126. 136.] + [198. 243. 288. 304.] + [306. 378. 450. 472.] + [414. 513. 612. 640.]] + + """ + + def __init__(self, rows): + error = TypeError( + "Matrices must be formed from a list of zero or more lists containing at " + "least one and the same number of values, each of which must be of type " + "int or float." + ) + if len(rows) != 0: + cols = len(rows[0]) + if cols == 0: + raise error + for row in rows: + if len(row) != cols: + raise error + for value in row: + if not isinstance(value, (int, float)): + raise error + self.rows = rows + else: + self.rows = [] + + # MATRIX INFORMATION + def columns(self): + return [[row[i] for row in self.rows] for i in range(len(self.rows[0]))] + + @property + def num_rows(self): + return len(self.rows) + + @property + def num_columns(self): + return len(self.rows[0]) + + @property + def order(self): + return (self.num_rows, self.num_columns) + + @property + def is_square(self): + return self.order[0] == self.order[1] + + def identity(self): + values = [ + [0 if column_num != row_num else 1 for column_num in range(self.num_rows)] + for row_num in range(self.num_rows) + ] + return Matrix(values) + + def determinant(self): + if not self.is_square: + return None + if self.order == (0, 0): + return 1 + if self.order == (1, 1): + return self.rows[0][0] + if self.order == (2, 2): + return (self.rows[0][0] * self.rows[1][1]) - ( + self.rows[0][1] * self.rows[1][0] + ) + else: + return sum( + self.rows[0][column] * self.cofactors().rows[0][column] + for column in range(self.num_columns) + ) + + def is_invertable(self): + return bool(self.determinant()) + + def get_minor(self, row, column): + values = [ + [ + self.rows[other_row][other_column] + for other_column in range(self.num_columns) + if other_column != column + ] + for other_row in range(self.num_rows) + if other_row != row + ] + return Matrix(values).determinant() + + def get_cofactor(self, row, column): + if (row + column) % 2 == 0: + return self.get_minor(row, column) + return -1 * self.get_minor(row, column) + + def minors(self): + return Matrix( + [ + [self.get_minor(row, column) for column in range(self.num_columns)] + for row in range(self.num_rows) + ] + ) + + def cofactors(self): + return Matrix( + [ + [ + self.minors().rows[row][column] + if (row + column) % 2 == 0 + else self.minors().rows[row][column] * -1 + for column in range(self.minors().num_columns) + ] + for row in range(self.minors().num_rows) + ] + ) + + def adjugate(self): + values = [ + [self.cofactors().rows[column][row] for column in range(self.num_columns)] + for row in range(self.num_rows) + ] + return Matrix(values) + + def inverse(self): + determinant = self.determinant() + return None if not determinant else self.adjugate() * (1 / determinant) + + def __repr__(self): + return str(self.rows) + + def __str__(self): + if self.num_rows == 0: + return "[]" + if self.num_rows == 1: + return "[[" + ". ".join(self.rows[0]) + "]]" + return ( + "[" + + "\n ".join( + [ + "[" + ". ".join([str(value) for value in row]) + ".]" + for row in self.rows + ] + ) + + "]" + ) + + # MATRIX MANIPULATION + def add_row(self, row, position=None): + type_error = TypeError("Row must be a list containing all ints and/or floats") + if not isinstance(row, list): + raise type_error + for value in row: + if not isinstance(value, (int, float)): + raise type_error + if len(row) != self.num_columns: + raise ValueError( + "Row must be equal in length to the other rows in the matrix" + ) + if position is None: + self.rows.append(row) + else: + self.rows = self.rows[0:position] + [row] + self.rows[position:] + + def add_column(self, column, position=None): + type_error = TypeError( + "Column must be a list containing all ints and/or floats" + ) + if not isinstance(column, list): + raise type_error + for value in column: + if not isinstance(value, (int, float)): + raise type_error + if len(column) != self.num_rows: + raise ValueError( + "Column must be equal in length to the other columns in the matrix" + ) + if position is None: + self.rows = [self.rows[i] + [column[i]] for i in range(self.num_rows)] + else: + self.rows = [ + self.rows[i][0:position] + [column[i]] + self.rows[i][position:] + for i in range(self.num_rows) + ] + + # MATRIX OPERATIONS + def __eq__(self, other): + if not isinstance(other, Matrix): + raise TypeError("A Matrix can only be compared with another Matrix") + return self.rows == other.rows + + def __ne__(self, other): + return not self == other + + def __neg__(self): + return self * -1 + + def __add__(self, other): + if self.order != other.order: + raise ValueError("Addition requires matrices of the same order") + return Matrix( + [ + [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns)] + for i in range(self.num_rows) + ] + ) + + def __sub__(self, other): + if self.order != other.order: + raise ValueError("Subtraction requires matrices of the same order") + return Matrix( + [ + [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns)] + for i in range(self.num_rows) + ] + ) + + def __mul__(self, other): + if isinstance(other, (int, float)): + return Matrix([[element * other for element in row] for row in self.rows]) + elif isinstance(other, Matrix): + if self.num_columns != other.num_rows: + raise ValueError( + "The number of columns in the first matrix must " + "be equal to the number of rows in the second" + ) + return Matrix( + [ + [Matrix.dot_product(row, column) for column in other.columns()] + for row in self.rows + ] + ) + else: + raise TypeError( + "A Matrix can only be multiplied by an int, float, or another matrix" + ) + + def __pow__(self, other): + if not isinstance(other, int): + raise TypeError("A Matrix can only be raised to the power of an int") + if not self.is_square: + raise ValueError("Only square matrices can be raised to a power") + if other == 0: + return self.identity() + if other < 0: + if self.is_invertable: + return self.inverse() ** (-other) + raise ValueError( + "Only invertable matrices can be raised to a negative power" + ) + result = self + for i in range(other - 1): + result *= self + return result + + @classmethod + def dot_product(cls, row, column): + return sum(row[i] * column[i] for i in range(len(row))) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/matrix/matrix_multiplication_addition.py b/matrix/matrix_multiplication_addition.py deleted file mode 100644 index dd50db729e43..000000000000 --- a/matrix/matrix_multiplication_addition.py +++ /dev/null @@ -1,75 +0,0 @@ -def add(matrix_a, matrix_b): - rows = len(matrix_a) - columns = len(matrix_a[0]) - matrix_c = [] - for i in range(rows): - list_1 = [] - for j in range(columns): - val = matrix_a[i][j] + matrix_b[i][j] - list_1.append(val) - matrix_c.append(list_1) - return matrix_c - -def scalarMultiply(matrix , n): - return [[x * n for x in row] for row in matrix] - -def multiply(matrix_a, matrix_b): - matrix_c = [] - n = len(matrix_a) - for i in range(n): - list_1 = [] - for j in range(n): - val = 0 - for k in range(n): - val = val + matrix_a[i][k] * matrix_b[k][j] - list_1.append(val) - matrix_c.append(list_1) - return matrix_c - -def identity(n): - return [[int(row == column) for column in range(n)] for row in range(n)] - -def transpose(matrix): - return map(list , zip(*matrix)) - -def minor(matrix, row, column): - minor = matrix[:row] + matrix[row + 1:] - minor = [row[:column] + row[column + 1:] for row in minor] - return minor - -def determinant(matrix): - if len(matrix) == 1: return matrix[0][0] - - res = 0 - for x in range(len(matrix)): - res += matrix[0][x] * determinant(minor(matrix , 0 , x)) * (-1) ** x - return res - -def inverse(matrix): - det = determinant(matrix) - if det == 0: return None - - matrixMinor = [[] for _ in range(len(matrix))] - for i in range(len(matrix)): - for j in range(len(matrix)): - matrixMinor[i].append(determinant(minor(matrix , i , j))) - - cofactors = [[x * (-1) ** (row + col) for col, x in enumerate(matrixMinor[row])] for row in range(len(matrix))] - adjugate = transpose(cofactors) - return scalarMultiply(adjugate , 1/det) - -def main(): - matrix_a = [[12, 10], [3, 9]] - matrix_b = [[3, 4], [7, 4]] - matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]] - matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] - - print(add(matrix_a, matrix_b)) - print(multiply(matrix_a, matrix_b)) - print(identity(5)) - print(minor(matrix_c , 1 , 2)) - print(determinant(matrix_b)) - print(inverse(matrix_d)) - -if __name__ == '__main__': - main() diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py new file mode 100644 index 000000000000..dca01f9c3183 --- /dev/null +++ b/matrix/matrix_operation.py @@ -0,0 +1,184 @@ +""" +Functions for 2D matrix operations +""" + +from __future__ import annotations + + +def add(*matrix_s: list[list]) -> list[list]: + """ + >>> add([[1,2],[3,4]],[[2,3],[4,5]]) + [[3, 5], [7, 9]] + >>> add([[1.2,2.4],[3,4]],[[2,3],[4,5]]) + [[3.2, 5.4], [7, 9]] + >>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]]) + [[7, 14], [12, 16]] + """ + if all(_check_not_integer(m) for m in matrix_s): + for i in matrix_s[1:]: + _verify_matrix_sizes(matrix_s[0], i) + return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)] + + +def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]: + """ + >>> subtract([[1,2],[3,4]],[[2,3],[4,5]]) + [[-1, -1], [-1, -1]] + >>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]]) + [[-1, -0.5], [-1, -1.5]] + """ + if ( + _check_not_integer(matrix_a) + and _check_not_integer(matrix_b) + and _verify_matrix_sizes(matrix_a, matrix_b) + ): + return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)] + + +def scalar_multiply(matrix: list[list], n: int) -> list[list]: + """ + >>> scalar_multiply([[1,2],[3,4]],5) + [[5, 10], [15, 20]] + >>> scalar_multiply([[1.4,2.3],[3,4]],5) + [[7.0, 11.5], [15, 20]] + """ + return [[x * n for x in row] for row in matrix] + + +def multiply(matrix_a: list[list], matrix_b: list[list]) -> list[list]: + """ + >>> multiply([[1,2],[3,4]],[[5,5],[7,5]]) + [[19, 15], [43, 35]] + >>> multiply([[1,2.5],[3,4.5]],[[5,5],[7,5]]) + [[22.5, 17.5], [46.5, 37.5]] + >>> multiply([[1, 2, 3]], [[2], [3], [4]]) + [[20]] + """ + if _check_not_integer(matrix_a) and _check_not_integer(matrix_b): + rows, cols = _verify_matrix_sizes(matrix_a, matrix_b) + + if cols[0] != rows[1]: + raise ValueError( + f"Cannot multiply matrix of dimensions ({rows[0]},{cols[0]}) " + f"and ({rows[1]},{cols[1]})" + ) + return [ + [sum(m * n for m, n in zip(i, j)) for j in zip(*matrix_b)] for i in matrix_a + ] + + +def identity(n: int) -> list[list]: + """ + :param n: dimension for nxn matrix + :type n: int + :return: Identity matrix of shape [n, n] + >>> identity(3) + [[1, 0, 0], [0, 1, 0], [0, 0, 1]] + """ + n = int(n) + return [[int(row == column) for column in range(n)] for row in range(n)] + + +def transpose(matrix: list[list], return_map: bool = True) -> list[list]: + """ + >>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS + >> transpose([[1,2],[3,4]], return_map=False) + [[1, 3], [2, 4]] + """ + if _check_not_integer(matrix): + if return_map: + return map(list, zip(*matrix)) + else: + return list(map(list, zip(*matrix))) + + +def minor(matrix: list[list], row: int, column: int) -> list[list]: + """ + >>> minor([[1, 2], [3, 4]], 1, 1) + [[1]] + """ + minor = matrix[:row] + matrix[row + 1 :] + return [row[:column] + row[column + 1 :] for row in minor] + + +def determinant(matrix: list[list]) -> int: + """ + >>> determinant([[1, 2], [3, 4]]) + -2 + >>> determinant([[1.5, 2.5], [3, 4]]) + -1.5 + """ + if len(matrix) == 1: + return matrix[0][0] + + return sum( + x * determinant(minor(matrix, 0, i)) * (-1) ** i + for i, x in enumerate(matrix[0]) + ) + + +def inverse(matrix: list[list]) -> list[list]: + """ + >>> inverse([[1, 2], [3, 4]]) + [[-2.0, 1.0], [1.5, -0.5]] + >>> inverse([[1, 1], [1, 1]]) + """ + # https://stackoverflow.com/questions/20047519/python-doctests-test-for-none + det = determinant(matrix) + if det == 0: + return None + + matrix_minor = [ + [determinant(minor(matrix, i, j)) for j in range(len(matrix))] + for i in range(len(matrix)) + ] + + cofactors = [ + [x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])] + for row in range(len(matrix)) + ] + adjugate = transpose(cofactors) + return scalar_multiply(adjugate, 1 / det) + + +def _check_not_integer(matrix: list[list]) -> bool: + if not isinstance(matrix, int) and not isinstance(matrix[0], int): + return True + raise TypeError("Expected a matrix, got int/list instead") + + +def _shape(matrix: list[list]) -> list: + return len(matrix), len(matrix[0]) + + +def _verify_matrix_sizes(matrix_a: list[list], matrix_b: list[list]) -> tuple[list]: + shape = _shape(matrix_a) + _shape(matrix_b) + if shape[0] != shape[3] or shape[1] != shape[2]: + raise ValueError( + f"operands could not be broadcast together with shape " + f"({shape[0], shape[1]}), ({shape[2], shape[3]})" + ) + return (shape[0], shape[2]), (shape[1], shape[3]) + + +def main(): + matrix_a = [[12, 10], [3, 9]] + matrix_b = [[3, 4], [7, 4]] + matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]] + matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] + print(f"Add Operation, {add(matrix_a, matrix_b) = } \n") + print( + f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n", + ) + print(f"Identity: {identity(5)}\n") + print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n") + print(f"Determinant of {matrix_b} = {determinant(matrix_b)} \n") + print(f"Inverse of {matrix_d} = {inverse(matrix_d)}\n") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + main() diff --git a/matrix/nth_fibonacci_using_matrix_exponentiation.py b/matrix/nth_fibonacci_using_matrix_exponentiation.py new file mode 100644 index 000000000000..296c36e88691 --- /dev/null +++ b/matrix/nth_fibonacci_using_matrix_exponentiation.py @@ -0,0 +1,91 @@ +""" +Implementation of finding nth fibonacci number using matrix exponentiation. +Time Complexity is about O(log(n)*8), where 8 is the complexity of matrix +multiplication of size 2 by 2. +And on the other hand complexity of bruteforce solution is O(n). +As we know + f[n] = f[n-1] + f[n-1] +Converting to matrix, + [f(n),f(n-1)] = [[1,1],[1,0]] * [f(n-1),f(n-2)] +-> [f(n),f(n-1)] = [[1,1],[1,0]]^2 * [f(n-2),f(n-3)] + ... + ... +-> [f(n),f(n-1)] = [[1,1],[1,0]]^(n-1) * [f(1),f(0)] +So we just need the n times multiplication of the matrix [1,1],[1,0]]. +We can decrease the n times multiplication by following the divide and conquer approach. +""" + + +def multiply(matrix_a, matrix_b): + matrix_c = [] + n = len(matrix_a) + for i in range(n): + list_1 = [] + for j in range(n): + val = 0 + for k in range(n): + val = val + matrix_a[i][k] * matrix_b[k][j] + list_1.append(val) + matrix_c.append(list_1) + return matrix_c + + +def identity(n): + return [[int(row == column) for column in range(n)] for row in range(n)] + + +def nth_fibonacci_matrix(n): + """ + >>> nth_fibonacci_matrix(100) + 354224848179261915075 + >>> nth_fibonacci_matrix(-100) + -100 + """ + if n <= 1: + return n + res_matrix = identity(2) + fibonacci_matrix = [[1, 1], [1, 0]] + n = n - 1 + while n > 0: + if n % 2 == 1: + res_matrix = multiply(res_matrix, fibonacci_matrix) + fibonacci_matrix = multiply(fibonacci_matrix, fibonacci_matrix) + n = int(n / 2) + return res_matrix[0][0] + + +def nth_fibonacci_bruteforce(n): + """ + >>> nth_fibonacci_bruteforce(100) + 354224848179261915075 + >>> nth_fibonacci_bruteforce(-100) + -100 + """ + if n <= 1: + return n + fib0 = 0 + fib1 = 1 + for i in range(2, n + 1): + fib0, fib1 = fib1, fib0 + fib1 + return fib1 + + +def main(): + fmt = ( + "{} fibonacci number using matrix exponentiation is {} and using bruteforce " + "is {}\n" + ) + for ordinal in "0th 1st 2nd 3rd 10th 100th 1000th".split(): + n = int("".join(c for c in ordinal if c in "0123456789")) # 1000th --> 1000 + print(fmt.format(ordinal, nth_fibonacci_matrix(n), nth_fibonacci_bruteforce(n))) + # from timeit import timeit + # print(timeit("nth_fibonacci_matrix(1000000)", + # "from main import nth_fibonacci_matrix", number=5)) + # print(timeit("nth_fibonacci_bruteforce(1000000)", + # "from main import nth_fibonacci_bruteforce", number=5)) + # 2.3342058970001744 + # 57.256506615000035 + + +if __name__ == "__main__": + main() diff --git a/matrix/rotate_matrix.py b/matrix/rotate_matrix.py new file mode 100644 index 000000000000..6daf7e0cf2c5 --- /dev/null +++ b/matrix/rotate_matrix.py @@ -0,0 +1,99 @@ +""" +In this problem, we want to rotate the matrix elements by 90, 180, 270 +(counterclockwise) +Discussion in stackoverflow: +https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array +""" + + +def make_matrix(row_size: int = 4) -> [[int]]: + """ + >>> make_matrix() + [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] + >>> make_matrix(1) + [[1]] + >>> make_matrix(-2) + [[1, 2], [3, 4]] + >>> make_matrix(3) + [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + >>> make_matrix() == make_matrix(4) + True + """ + row_size = abs(row_size) or 4 + return [[1 + x + y * row_size for x in range(row_size)] for y in range(row_size)] + + +def rotate_90(matrix: [[]]) -> [[]]: + """ + >>> rotate_90(make_matrix()) + [[4, 8, 12, 16], [3, 7, 11, 15], [2, 6, 10, 14], [1, 5, 9, 13]] + >>> rotate_90(make_matrix()) == transpose(reverse_column(make_matrix())) + True + """ + + return reverse_row(transpose(matrix)) + # OR.. transpose(reverse_column(matrix)) + + +def rotate_180(matrix: [[]]) -> [[]]: + """ + >>> rotate_180(make_matrix()) + [[16, 15, 14, 13], [12, 11, 10, 9], [8, 7, 6, 5], [4, 3, 2, 1]] + >>> rotate_180(make_matrix()) == reverse_column(reverse_row(make_matrix())) + True + """ + + return reverse_row(reverse_column(matrix)) + # OR.. reverse_column(reverse_row(matrix)) + + +def rotate_270(matrix: [[]]) -> [[]]: + """ + >>> rotate_270(make_matrix()) + [[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]] + >>> rotate_270(make_matrix()) == transpose(reverse_row(make_matrix())) + True + """ + + return reverse_column(transpose(matrix)) + # OR.. transpose(reverse_row(matrix)) + + +def transpose(matrix: [[]]) -> [[]]: + matrix[:] = [list(x) for x in zip(*matrix)] + return matrix + + +def reverse_row(matrix: [[]]) -> [[]]: + matrix[:] = matrix[::-1] + return matrix + + +def reverse_column(matrix: [[]]) -> [[]]: + matrix[:] = [x[::-1] for x in matrix] + return matrix + + +def print_matrix(matrix: [[]]) -> [[]]: + for i in matrix: + print(*i) + + +if __name__ == "__main__": + matrix = make_matrix() + print("\norigin:\n") + print_matrix(matrix) + print("\nrotate 90 counterclockwise:\n") + print_matrix(rotate_90(matrix)) + + matrix = make_matrix() + print("\norigin:\n") + print_matrix(matrix) + print("\nrotate 180:\n") + print_matrix(rotate_180(matrix)) + + matrix = make_matrix() + print("\norigin:\n") + print_matrix(matrix) + print("\nrotate 270 counterclockwise:\n") + print_matrix(rotate_270(matrix)) diff --git a/matrix/searching_in_sorted_matrix.py b/matrix/searching_in_sorted_matrix.py index 54913b350803..ca6263a32f50 100644 --- a/matrix/searching_in_sorted_matrix.py +++ b/matrix/searching_in_sorted_matrix.py @@ -1,27 +1,46 @@ -def search_in_a_sorted_matrix(mat, m, n, key): +from __future__ import annotations + +from typing import Union + + +def search_in_a_sorted_matrix( + mat: list[list], m: int, n: int, key: Union[int, float] +) -> None: + """ + >>> search_in_a_sorted_matrix( + ... [[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]], 3, 3, 5) + Key 5 found at row- 1 column- 2 + >>> search_in_a_sorted_matrix( + ... [[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]], 3, 3, 21) + Key 21 not found + >>> search_in_a_sorted_matrix( + ... [[2.1, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]], 3, 3, 2.1) + Key 2.1 found at row- 1 column- 1 + >>> search_in_a_sorted_matrix( + ... [[2.1, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]], 3, 3, 2.2) + Key 2.2 not found + """ i, j = m - 1, 0 while i >= 0 and j < n: if key == mat[i][j]: - print('Key %s found at row- %s column- %s' % (key, i + 1, j + 1)) + print(f"Key {key} found at row- {i + 1} column- {j + 1}") return if key < mat[i][j]: i -= 1 else: j += 1 - print('Key %s not found' % (key)) + print(f"Key {key} not found") def main(): - mat = [ - [2, 5, 7], - [4, 8, 13], - [9, 11, 15], - [12, 17, 20] - ] + mat = [[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]] x = int(input("Enter the element to be searched:")) print(mat) search_in_a_sorted_matrix(mat, len(mat), len(mat[0]), x) -if __name__ == '__main__': +if __name__ == "__main__": + import doctest + + doctest.testmod() main() diff --git a/matrix/sherman_morrison.py b/matrix/sherman_morrison.py new file mode 100644 index 000000000000..4920ec6c13db --- /dev/null +++ b/matrix/sherman_morrison.py @@ -0,0 +1,270 @@ +class Matrix: + """ + + Matrix structure. + """ + + def __init__(self, row: int, column: int, default_value: float = 0): + """ + + Initialize matrix with given size and default value. + + Example: + >>> a = Matrix(2, 3, 1) + >>> a + Matrix consist of 2 rows and 3 columns + [1, 1, 1] + [1, 1, 1] + """ + + self.row, self.column = row, column + self.array = [[default_value for c in range(column)] for r in range(row)] + + def __str__(self): + """ + + Return string representation of this matrix. + """ + + # Prefix + s = "Matrix consist of %d rows and %d columns\n" % (self.row, self.column) + + # Make string identifier + max_element_length = 0 + for row_vector in self.array: + for obj in row_vector: + max_element_length = max(max_element_length, len(str(obj))) + string_format_identifier = "%%%ds" % (max_element_length,) + + # Make string and return + def single_line(row_vector): + nonlocal string_format_identifier + line = "[" + line += ", ".join(string_format_identifier % (obj,) for obj in row_vector) + line += "]" + return line + + s += "\n".join(single_line(row_vector) for row_vector in self.array) + return s + + def __repr__(self): + return str(self) + + def validateIndices(self, loc: tuple): + """ + + Check if given indices are valid to pick element from matrix. + + Example: + >>> a = Matrix(2, 6, 0) + >>> a.validateIndices((2, 7)) + False + >>> a.validateIndices((0, 0)) + True + """ + if not (isinstance(loc, (list, tuple)) and len(loc) == 2): + return False + elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): + return False + else: + return True + + def __getitem__(self, loc: tuple): + """ + + Return array[row][column] where loc = (row, column). + + Example: + >>> a = Matrix(3, 2, 7) + >>> a[1, 0] + 7 + """ + assert self.validateIndices(loc) + return self.array[loc[0]][loc[1]] + + def __setitem__(self, loc: tuple, value: float): + """ + + Set array[row][column] = value where loc = (row, column). + + Example: + >>> a = Matrix(2, 3, 1) + >>> a[1, 2] = 51 + >>> a + Matrix consist of 2 rows and 3 columns + [ 1, 1, 1] + [ 1, 1, 51] + """ + assert self.validateIndices(loc) + self.array[loc[0]][loc[1]] = value + + def __add__(self, another): + """ + + Return self + another. + + Example: + >>> a = Matrix(2, 1, -4) + >>> b = Matrix(2, 1, 3) + >>> a+b + Matrix consist of 2 rows and 1 columns + [-1] + [-1] + """ + + # Validation + assert isinstance(another, Matrix) + assert self.row == another.row and self.column == another.column + + # Add + result = Matrix(self.row, self.column) + for r in range(self.row): + for c in range(self.column): + result[r, c] = self[r, c] + another[r, c] + return result + + def __neg__(self): + """ + + Return -self. + + Example: + >>> a = Matrix(2, 2, 3) + >>> a[0, 1] = a[1, 0] = -2 + >>> -a + Matrix consist of 2 rows and 2 columns + [-3, 2] + [ 2, -3] + """ + + result = Matrix(self.row, self.column) + for r in range(self.row): + for c in range(self.column): + result[r, c] = -self[r, c] + return result + + def __sub__(self, another): + return self + (-another) + + def __mul__(self, another): + """ + + Return self * another. + + Example: + >>> a = Matrix(2, 3, 1) + >>> a[0,2] = a[1,2] = 3 + >>> a * -2 + Matrix consist of 2 rows and 3 columns + [-2, -2, -6] + [-2, -2, -6] + """ + + if isinstance(another, (int, float)): # Scalar multiplication + result = Matrix(self.row, self.column) + for r in range(self.row): + for c in range(self.column): + result[r, c] = self[r, c] * another + return result + elif isinstance(another, Matrix): # Matrix multiplication + assert self.column == another.row + result = Matrix(self.row, another.column) + for r in range(self.row): + for c in range(another.column): + for i in range(self.column): + result[r, c] += self[r, i] * another[i, c] + return result + else: + raise TypeError( + "Unsupported type given for another ({})".format(type(another)) + ) + + def transpose(self): + """ + + Return self^T. + + Example: + >>> a = Matrix(2, 3) + >>> for r in range(2): + ... for c in range(3): + ... a[r,c] = r*c + ... + >>> a.transpose() + Matrix consist of 3 rows and 2 columns + [0, 0] + [0, 1] + [0, 2] + """ + + result = Matrix(self.column, self.row) + for r in range(self.row): + for c in range(self.column): + result[c, r] = self[r, c] + return result + + def ShermanMorrison(self, u, v): + """ + + Apply Sherman-Morrison formula in O(n^2). + To learn this formula, please look this: + https://en.wikipedia.org/wiki/Sherman%E2%80%93Morrison_formula + This method returns (A + uv^T)^(-1) where A^(-1) is self. Returns None if it's + impossible to calculate. + Warning: This method doesn't check if self is invertible. + Make sure self is invertible before execute this method. + + Example: + >>> ainv = Matrix(3, 3, 0) + >>> for i in range(3): ainv[i,i] = 1 + ... + >>> u = Matrix(3, 1, 0) + >>> u[0,0], u[1,0], u[2,0] = 1, 2, -3 + >>> v = Matrix(3, 1, 0) + >>> v[0,0], v[1,0], v[2,0] = 4, -2, 5 + >>> ainv.ShermanMorrison(u, v) + Matrix consist of 3 rows and 3 columns + [ 1.2857142857142856, -0.14285714285714285, 0.3571428571428571] + [ 0.5714285714285714, 0.7142857142857143, 0.7142857142857142] + [ -0.8571428571428571, 0.42857142857142855, -0.0714285714285714] + """ + + # Size validation + assert isinstance(u, Matrix) and isinstance(v, Matrix) + assert self.row == self.column == u.row == v.row # u, v should be column vector + assert u.column == v.column == 1 # u, v should be column vector + + # Calculate + vT = v.transpose() + numerator_factor = (vT * self * u)[0, 0] + 1 + if numerator_factor == 0: + return None # It's not invertable + return self - ((self * u) * (vT * self) * (1.0 / numerator_factor)) + + +# Testing +if __name__ == "__main__": + + def test1(): + # a^(-1) + ainv = Matrix(3, 3, 0) + for i in range(3): + ainv[i, i] = 1 + print(f"a^(-1) is {ainv}") + # u, v + u = Matrix(3, 1, 0) + u[0, 0], u[1, 0], u[2, 0] = 1, 2, -3 + v = Matrix(3, 1, 0) + v[0, 0], v[1, 0], v[2, 0] = 4, -2, 5 + print(f"u is {u}") + print(f"v is {v}") + print("uv^T is %s" % (u * v.transpose())) + # Sherman Morrison + print("(a + uv^T)^(-1) is {}".format(ainv.ShermanMorrison(u, v))) + + def test2(): + import doctest + + doctest.testmod() + + test2() diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py new file mode 100644 index 000000000000..21dab76156e9 --- /dev/null +++ b/matrix/spiral_print.py @@ -0,0 +1,68 @@ +""" +This program print the matrix in spiral form. +This problem has been solved through recursive way. + + Matrix must satisfy below conditions + i) matrix should be only one or two dimensional + ii)column of all the row should be equal +""" + + +def checkMatrix(a): + # must be + if type(a) == list and len(a) > 0: + if type(a[0]) == list: + prevLen = 0 + for i in a: + if prevLen == 0: + prevLen = len(i) + result = True + elif prevLen == len(i): + result = True + else: + result = False + else: + result = True + else: + result = False + return result + + +def spiralPrint(a): + + if checkMatrix(a) and len(a) > 0: + + matRow = len(a) + if type(a[0]) == list: + matCol = len(a[0]) + else: + for dat in a: + print(dat), + return + + # horizotal printing increasing + for i in range(0, matCol): + print(a[0][i]), + # vertical printing down + for i in range(1, matRow): + print(a[i][matCol - 1]), + # horizotal printing decreasing + if matRow > 1: + for i in range(matCol - 2, -1, -1): + print(a[matRow - 1][i]), + # vertical printing up + for i in range(matRow - 2, 0, -1): + print(a[i][0]), + remainMat = [row[1 : matCol - 1] for row in a[1 : matRow - 1]] + if len(remainMat) > 0: + spiralPrint(remainMat) + else: + return + else: + print("Not a valid matrix") + return + + +# driver code +a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] +spiralPrint(a) diff --git a/matrix/tests/__init__.py b/matrix/tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/matrix/tests/pytest.ini b/matrix/tests/pytest.ini new file mode 100644 index 000000000000..8a978b56ef8b --- /dev/null +++ b/matrix/tests/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +markers = + mat_ops: tests for matrix operations diff --git a/matrix/tests/test_matrix_operation.py b/matrix/tests/test_matrix_operation.py new file mode 100644 index 000000000000..3500dfeb0641 --- /dev/null +++ b/matrix/tests/test_matrix_operation.py @@ -0,0 +1,120 @@ +""" +Testing here assumes that numpy and linalg is ALWAYS correct!!!! + +If running from PyCharm you can place the following line in "Additional Arguments" for +the pytest run configuration +-vv -m mat_ops -p no:cacheprovider +""" + +import logging + +# standard libraries +import sys + +import numpy as np +import pytest + +# Custom/local libraries +from matrix import matrix_operation as matop + +mat_a = [[12, 10], [3, 9]] +mat_b = [[3, 4], [7, 4]] +mat_c = [[3, 0, 2], [2, 0, -2], [0, 1, 1]] +mat_d = [[3, 0, -2], [2, 0, 2], [0, 1, 1]] +mat_e = [[3, 0, 2], [2, 0, -2], [0, 1, 1], [2, 0, -2]] +mat_f = [1] +mat_h = [2] + +logger = logging.getLogger() +logger.level = logging.DEBUG +stream_handler = logging.StreamHandler(sys.stdout) +logger.addHandler(stream_handler) + + +@pytest.mark.mat_ops +@pytest.mark.parametrize( + ("mat1", "mat2"), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)] +) +def test_addition(mat1, mat2): + if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): + with pytest.raises(TypeError): + logger.info(f"\n\t{test_addition.__name__} returned integer") + matop.add(mat1, mat2) + elif (np.array(mat1)).shape == (np.array(mat2)).shape: + logger.info(f"\n\t{test_addition.__name__} with same matrix dims") + act = (np.array(mat1) + np.array(mat2)).tolist() + theo = matop.add(mat1, mat2) + assert theo == act + else: + with pytest.raises(ValueError): + logger.info(f"\n\t{test_addition.__name__} with different matrix dims") + matop.add(mat1, mat2) + + +@pytest.mark.mat_ops +@pytest.mark.parametrize( + ("mat1", "mat2"), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)] +) +def test_subtraction(mat1, mat2): + if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): + with pytest.raises(TypeError): + logger.info(f"\n\t{test_subtraction.__name__} returned integer") + matop.subtract(mat1, mat2) + elif (np.array(mat1)).shape == (np.array(mat2)).shape: + logger.info(f"\n\t{test_subtraction.__name__} with same matrix dims") + act = (np.array(mat1) - np.array(mat2)).tolist() + theo = matop.subtract(mat1, mat2) + assert theo == act + else: + with pytest.raises(ValueError): + logger.info(f"\n\t{test_subtraction.__name__} with different matrix dims") + assert matop.subtract(mat1, mat2) + + +@pytest.mark.mat_ops +@pytest.mark.parametrize( + ("mat1", "mat2"), [(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)] +) +def test_multiplication(mat1, mat2): + if (np.array(mat1)).shape < (2, 2) or (np.array(mat2)).shape < (2, 2): + logger.info(f"\n\t{test_multiplication.__name__} returned integer") + with pytest.raises(TypeError): + matop.add(mat1, mat2) + elif (np.array(mat1)).shape == (np.array(mat2)).shape: + logger.info(f"\n\t{test_multiplication.__name__} meets dim requirements") + act = (np.matmul(mat1, mat2)).tolist() + theo = matop.multiply(mat1, mat2) + assert theo == act + else: + with pytest.raises(ValueError): + logger.info( + f"\n\t{test_multiplication.__name__} does not meet dim requirements" + ) + assert matop.subtract(mat1, mat2) + + +@pytest.mark.mat_ops +def test_scalar_multiply(): + act = (3.5 * np.array(mat_a)).tolist() + theo = matop.scalar_multiply(mat_a, 3.5) + assert theo == act + + +@pytest.mark.mat_ops +def test_identity(): + act = (np.identity(5)).tolist() + theo = matop.identity(5) + assert theo == act + + +@pytest.mark.mat_ops +@pytest.mark.parametrize("mat", [mat_a, mat_b, mat_c, mat_d, mat_e, mat_f]) +def test_transpose(mat): + if (np.array(mat)).shape < (2, 2): + with pytest.raises(TypeError): + logger.info(f"\n\t{test_transpose.__name__} returned integer") + matop.transpose(mat) + else: + act = (np.transpose(mat)).tolist() + theo = matop.transpose(mat, return_map=False) + assert theo == act diff --git a/networking_flow/__init__.py b/networking_flow/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index d51f1f0661b3..96b782649774 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -4,53 +4,58 @@ (1) Start with initial flow as 0; (2) Choose augmenting path from source to sink and add path to flow; """ - + + def BFS(graph, s, t, parent): # Return True if there is node that has not iterated. - visited = [False]*len(graph) - queue=[] + visited = [False] * len(graph) + queue = [] queue.append(s) visited[s] = True - + while queue: u = queue.pop(0) for ind in range(len(graph[u])): - if visited[ind] == False and graph[u][ind] > 0: + if visited[ind] is False and graph[u][ind] > 0: queue.append(ind) visited[ind] = True parent[ind] = u return True if visited[t] else False - + + def FordFulkerson(graph, source, sink): # This array is filled by BFS and to store path - parent = [-1]*(len(graph)) - max_flow = 0 - while BFS(graph, source, sink, parent) : + parent = [-1] * (len(graph)) + max_flow = 0 + while BFS(graph, source, sink, parent): path_flow = float("Inf") s = sink - while(s != source): + while s != source: # Find the minimum value in select path - path_flow = min (path_flow, graph[parent[s]][s]) + path_flow = min(path_flow, graph[parent[s]][s]) s = parent[s] - max_flow += path_flow + max_flow += path_flow v = sink - while(v != source): + while v != source: u = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow v = parent[v] return max_flow -graph = [[0, 16, 13, 0, 0, 0], - [0, 0, 10 ,12, 0, 0], - [0, 4, 0, 0, 14, 0], - [0, 0, 9, 0, 0, 20], - [0, 0, 0, 7, 0, 4], - [0, 0, 0, 0, 0, 0]] + +graph = [ + [0, 16, 13, 0, 0, 0], + [0, 0, 10, 12, 0, 0], + [0, 4, 0, 0, 14, 0], + [0, 0, 9, 0, 0, 20], + [0, 0, 0, 7, 0, 4], + [0, 0, 0, 0, 0, 0], +] source, sink = 0, 5 -print(FordFulkerson(graph, source, sink)) \ No newline at end of file +print(FordFulkerson(graph, source, sink)) diff --git a/networking_flow/minimum_cut.py b/networking_flow/minimum_cut.py index 8ad6e03b00c6..d79f3619caf1 100644 --- a/networking_flow/minimum_cut.py +++ b/networking_flow/minimum_cut.py @@ -1,41 +1,54 @@ # Minimum cut on Ford_Fulkerson algorithm. - + +test_graph = [ + [0, 16, 13, 0, 0, 0], + [0, 0, 10, 12, 0, 0], + [0, 4, 0, 0, 14, 0], + [0, 0, 9, 0, 0, 20], + [0, 0, 0, 7, 0, 4], + [0, 0, 0, 0, 0, 0], +] + + def BFS(graph, s, t, parent): # Return True if there is node that has not iterated. - visited = [False]*len(graph) - queue=[] - queue.append(s) + visited = [False] * len(graph) + queue = [s] visited[s] = True - + while queue: u = queue.pop(0) for ind in range(len(graph[u])): - if visited[ind] == False and graph[u][ind] > 0: + if visited[ind] is False and graph[u][ind] > 0: queue.append(ind) visited[ind] = True parent[ind] = u return True if visited[t] else False - + + def mincut(graph, source, sink): - # This array is filled by BFS and to store path - parent = [-1]*(len(graph)) - max_flow = 0 + """This array is filled by BFS and to store path + >>> mincut(test_graph, source=0, sink=5) + [(1, 3), (4, 3), (4, 5)] + """ + parent = [-1] * (len(graph)) + max_flow = 0 res = [] - temp = [i[:] for i in graph] # Record orignial cut, copy. - while BFS(graph, source, sink, parent) : + temp = [i[:] for i in graph] # Record original cut, copy. + while BFS(graph, source, sink, parent): path_flow = float("Inf") s = sink - while(s != source): + while s != source: # Find the minimum value in select path - path_flow = min (path_flow, graph[parent[s]][s]) + path_flow = min(path_flow, graph[parent[s]][s]) s = parent[s] - max_flow += path_flow + max_flow += path_flow v = sink - - while(v != source): + + while v != source: u = parent[v] graph[u][v] -= path_flow graph[v][u] += path_flow @@ -44,16 +57,10 @@ def mincut(graph, source, sink): for i in range(len(graph)): for j in range(len(graph[0])): if graph[i][j] == 0 and temp[i][j] > 0: - res.append((i,j)) + res.append((i, j)) return res -graph = [[0, 16, 13, 0, 0, 0], - [0, 0, 10 ,12, 0, 0], - [0, 4, 0, 0, 14, 0], - [0, 0, 9, 0, 0, 20], - [0, 0, 0, 7, 0, 4], - [0, 0, 0, 0, 0, 0]] -source, sink = 0, 5 -print(mincut(graph, source, sink)) \ No newline at end of file +if __name__ == "__main__": + print(mincut(test_graph, source=0, sink=5)) diff --git a/neural_network/2_hidden_layers_neural_network.py b/neural_network/2_hidden_layers_neural_network.py new file mode 100644 index 000000000000..baa4316200d9 --- /dev/null +++ b/neural_network/2_hidden_layers_neural_network.py @@ -0,0 +1,295 @@ +""" +References: + - http://neuralnetworksanddeeplearning.com/chap2.html (Backpropagation) + - https://en.wikipedia.org/wiki/Sigmoid_function (Sigmoid activation function) + - https://en.wikipedia.org/wiki/Feedforward_neural_network (Feedforward) +""" + +import numpy + + +class TwoHiddenLayerNeuralNetwork: + def __init__(self, input_array: numpy.ndarray, output_array: numpy.ndarray) -> None: + """ + This function initializes the TwoHiddenLayerNeuralNetwork class with random + weights for every layer and initializes predicted output with zeroes. + + input_array : input values for training the neural network (i.e training data) . + output_array : expected output values of the given inputs. + """ + + # Input values provided for training the model. + self.input_array = input_array + + # Random initial weights are assigned where first argument is the + # number of nodes in previous layer and second argument is the + # number of nodes in the next layer. + + # Random initial weights are assigned. + # self.input_array.shape[1] is used to represent number of nodes in input layer. + # First hidden layer consists of 4 nodes. + self.input_layer_and_first_hidden_layer_weights = numpy.random.rand( + self.input_array.shape[1], 4 + ) + + # Random initial values for the first hidden layer. + # First hidden layer has 4 nodes. + # Second hidden layer has 3 nodes. + self.first_hidden_layer_and_second_hidden_layer_weights = numpy.random.rand( + 4, 3 + ) + + # Random initial values for the second hidden layer. + # Second hidden layer has 3 nodes. + # Output layer has 1 node. + self.second_hidden_layer_and_output_layer_weights = numpy.random.rand(3, 1) + + # Real output values provided. + self.output_array = output_array + + # Predicted output values by the neural network. + # Predicted_output array initially consists of zeroes. + self.predicted_output = numpy.zeros(output_array.shape) + + def feedforward(self) -> numpy.ndarray: + """ + The information moves in only one direction i.e. forward from the input nodes, + through the two hidden nodes and to the output nodes. + There are no cycles or loops in the network. + + Return layer_between_second_hidden_layer_and_output + (i.e the last layer of the neural network). + + >>> input_val = numpy.array(([0, 0, 0], [0, 0, 0], [0, 0, 0]), dtype=float) + >>> output_val = numpy.array(([0], [0], [0]), dtype=float) + >>> nn = TwoHiddenLayerNeuralNetwork(input_val, output_val) + >>> res = nn.feedforward() + >>> array_sum = numpy.sum(res) + >>> numpy.isnan(array_sum) + False + """ + # Layer_between_input_and_first_hidden_layer is the layer connecting the + # input nodes with the first hidden layer nodes. + self.layer_between_input_and_first_hidden_layer = sigmoid( + numpy.dot(self.input_array, self.input_layer_and_first_hidden_layer_weights) + ) + + # layer_between_first_hidden_layer_and_second_hidden_layer is the layer + # connecting the first hidden set of nodes with the second hidden set of nodes. + self.layer_between_first_hidden_layer_and_second_hidden_layer = sigmoid( + numpy.dot( + self.layer_between_input_and_first_hidden_layer, + self.first_hidden_layer_and_second_hidden_layer_weights, + ) + ) + + # layer_between_second_hidden_layer_and_output is the layer connecting + # second hidden layer with the output node. + self.layer_between_second_hidden_layer_and_output = sigmoid( + numpy.dot( + self.layer_between_first_hidden_layer_and_second_hidden_layer, + self.second_hidden_layer_and_output_layer_weights, + ) + ) + + return self.layer_between_second_hidden_layer_and_output + + def back_propagation(self) -> None: + """ + Function for fine-tuning the weights of the neural net based on the + error rate obtained in the previous epoch (i.e., iteration). + Updation is done using derivative of sogmoid activation function. + + >>> input_val = numpy.array(([0, 0, 0], [0, 0, 0], [0, 0, 0]), dtype=float) + >>> output_val = numpy.array(([0], [0], [0]), dtype=float) + >>> nn = TwoHiddenLayerNeuralNetwork(input_val, output_val) + >>> res = nn.feedforward() + >>> nn.back_propagation() + >>> updated_weights = nn.second_hidden_layer_and_output_layer_weights + >>> (res == updated_weights).all() + False + """ + + updated_second_hidden_layer_and_output_layer_weights = numpy.dot( + self.layer_between_first_hidden_layer_and_second_hidden_layer.T, + 2 + * (self.output_array - self.predicted_output) + * sigmoid_derivative(self.predicted_output), + ) + updated_first_hidden_layer_and_second_hidden_layer_weights = numpy.dot( + self.layer_between_input_and_first_hidden_layer.T, + numpy.dot( + 2 + * (self.output_array - self.predicted_output) + * sigmoid_derivative(self.predicted_output), + self.second_hidden_layer_and_output_layer_weights.T, + ) + * sigmoid_derivative( + self.layer_between_first_hidden_layer_and_second_hidden_layer + ), + ) + updated_input_layer_and_first_hidden_layer_weights = numpy.dot( + self.input_array.T, + numpy.dot( + numpy.dot( + 2 + * (self.output_array - self.predicted_output) + * sigmoid_derivative(self.predicted_output), + self.second_hidden_layer_and_output_layer_weights.T, + ) + * sigmoid_derivative( + self.layer_between_first_hidden_layer_and_second_hidden_layer + ), + self.first_hidden_layer_and_second_hidden_layer_weights.T, + ) + * sigmoid_derivative(self.layer_between_input_and_first_hidden_layer), + ) + + self.input_layer_and_first_hidden_layer_weights += ( + updated_input_layer_and_first_hidden_layer_weights + ) + self.first_hidden_layer_and_second_hidden_layer_weights += ( + updated_first_hidden_layer_and_second_hidden_layer_weights + ) + self.second_hidden_layer_and_output_layer_weights += ( + updated_second_hidden_layer_and_output_layer_weights + ) + + def train(self, output: numpy.ndarray, iterations: int, give_loss: bool) -> None: + """ + Performs the feedforwarding and back propagation process for the + given number of iterations. + Every iteration will update the weights of neural network. + + output : real output values,required for calculating loss. + iterations : number of times the weights are to be updated. + give_loss : boolean value, If True then prints loss for each iteration, + If False then nothing is printed + + >>> input_val = numpy.array(([0, 0, 0], [0, 1, 0], [0, 0, 1]), dtype=float) + >>> output_val = numpy.array(([0], [1], [1]), dtype=float) + >>> nn = TwoHiddenLayerNeuralNetwork(input_val, output_val) + >>> first_iteration_weights = nn.feedforward() + >>> nn.back_propagation() + >>> updated_weights = nn.second_hidden_layer_and_output_layer_weights + >>> (first_iteration_weights == updated_weights).all() + False + """ + for iteration in range(1, iterations + 1): + self.output = self.feedforward() + self.back_propagation() + if give_loss: + loss = numpy.mean(numpy.square(output - self.feedforward())) + print(f"Iteration {iteration} Loss: {loss}") + + def predict(self, input: numpy.ndarray) -> int: + """ + Predict's the output for the given input values using + the trained neural network. + + The output value given by the model ranges in-between 0 and 1. + The predict function returns 1 if the model value is greater + than the threshold value else returns 0, + as the real output values are in binary. + + >>> input_val = numpy.array(([0, 0, 0], [0, 1, 0], [0, 0, 1]), dtype=float) + >>> output_val = numpy.array(([0], [1], [1]), dtype=float) + >>> nn = TwoHiddenLayerNeuralNetwork(input_val, output_val) + >>> nn.train(output_val, 1000, False) + >>> nn.predict([0,1,0]) + 1 + """ + + # Input values for which the predictions are to be made. + self.array = input + + self.layer_between_input_and_first_hidden_layer = sigmoid( + numpy.dot(self.array, self.input_layer_and_first_hidden_layer_weights) + ) + + self.layer_between_first_hidden_layer_and_second_hidden_layer = sigmoid( + numpy.dot( + self.layer_between_input_and_first_hidden_layer, + self.first_hidden_layer_and_second_hidden_layer_weights, + ) + ) + + self.layer_between_second_hidden_layer_and_output = sigmoid( + numpy.dot( + self.layer_between_first_hidden_layer_and_second_hidden_layer, + self.second_hidden_layer_and_output_layer_weights, + ) + ) + + return int(self.layer_between_second_hidden_layer_and_output > 0.6) + + +def sigmoid(value: numpy.ndarray) -> numpy.ndarray: + """ + Applies sigmoid activation function. + + return normalized values + + >>> sigmoid(numpy.array(([1, 0, 2], [1, 0, 0]), dtype=numpy.float64)) + array([[0.73105858, 0.5 , 0.88079708], + [0.73105858, 0.5 , 0.5 ]]) + """ + return 1 / (1 + numpy.exp(-value)) + + +def sigmoid_derivative(value: numpy.ndarray) -> numpy.ndarray: + """ + Provides the derivative value of the sigmoid function. + + returns derivative of the sigmoid value + + >>> sigmoid_derivative(numpy.array(([1, 0, 2], [1, 0, 0]), dtype=numpy.float64)) + array([[ 0., 0., -2.], + [ 0., 0., 0.]]) + """ + return (value) * (1 - (value)) + + +def example() -> int: + """ + Example for "how to use the neural network class and use the + respected methods for the desired output". + Calls the TwoHiddenLayerNeuralNetwork class and + provides the fixed input output values to the model. + Model is trained for a fixed amount of iterations then the predict method is called. + In this example the output is divided into 2 classes i.e. binary classification, + the two classes are represented by '0' and '1'. + + >>> example() + 1 + """ + # Input values. + input = numpy.array( + ( + [0, 0, 0], + [0, 0, 1], + [0, 1, 0], + [0, 1, 1], + [1, 0, 0], + [1, 0, 1], + [1, 1, 0], + [1, 1, 1], + ), + dtype=numpy.float64, + ) + + # True output values for the given input values. + output = numpy.array(([0], [1], [1], [0], [1], [0], [0], [1]), dtype=numpy.float64) + + # Calling neural network class. + neural_network = TwoHiddenLayerNeuralNetwork(input_array=input, output_array=output) + + # Calling training function. + # Set give_loss to True if you want to see loss in every iteration. + neural_network.train(output=output, iterations=10, give_loss=False) + + return neural_network.predict(numpy.array(([1, 1, 1]), dtype=numpy.float64)) + + +if __name__ == "__main__": + example() diff --git a/neural_network/__init__.py b/neural_network/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/neural_network/bpnn.py b/neural_network/back_propagation_neural_network.py similarity index 56% rename from neural_network/bpnn.py rename to neural_network/back_propagation_neural_network.py index 92deaee19c6e..43e796e77be3 100644 --- a/neural_network/bpnn.py +++ b/neural_network/back_propagation_neural_network.py @@ -1,7 +1,6 @@ #!/usr/bin/python -# encoding=utf8 -''' +""" A Framework of Back Propagation Neural Network(BP) model @@ -17,27 +16,30 @@ Github : https://github.com/RiptideBo Date: 2017.11.23 -''' - +""" import numpy as np -import matplotlib.pyplot as plt +from matplotlib import pyplot as plt def sigmoid(x): return 1 / (1 + np.exp(-1 * x)) -class DenseLayer(): - ''' + +class DenseLayer: + """ Layers of BP neural network - ''' - def __init__(self,units,activation=None,learning_rate=None,is_input_layer=False): - ''' + """ + + def __init__( + self, units, activation=None, learning_rate=None, is_input_layer=False + ): + """ common connected layer of bp network :param units: numbers of neural units :param activation: activation function :param learning_rate: learning rate for paras :param is_input_layer: whether it is input layer or not - ''' + """ self.units = units self.weight = None self.bias = None @@ -47,21 +49,22 @@ def __init__(self,units,activation=None,learning_rate=None,is_input_layer=False) self.learn_rate = learning_rate self.is_input_layer = is_input_layer - def initializer(self,back_units): - self.weight = np.asmatrix(np.random.normal(0,0.5,(self.units,back_units))) - self.bias = np.asmatrix(np.random.normal(0,0.5,self.units)).T + def initializer(self, back_units): + self.weight = np.asmatrix(np.random.normal(0, 0.5, (self.units, back_units))) + self.bias = np.asmatrix(np.random.normal(0, 0.5, self.units)).T if self.activation is None: self.activation = sigmoid def cal_gradient(self): + # activation function may be sigmoid or linear if self.activation == sigmoid: - gradient_mat = np.dot(self.output ,(1- self.output).T) + gradient_mat = np.dot(self.output, (1 - self.output).T) gradient_activation = np.diag(np.diag(gradient_mat)) else: gradient_activation = 1 return gradient_activation - def forward_propagation(self,xdata): + def forward_propagation(self, xdata): self.xdata = xdata if self.is_input_layer: # input layer @@ -69,57 +72,56 @@ def forward_propagation(self,xdata): self.output = xdata return xdata else: - self.wx_plus_b = np.dot(self.weight,self.xdata) - self.bias + self.wx_plus_b = np.dot(self.weight, self.xdata) - self.bias self.output = self.activation(self.wx_plus_b) return self.output - def back_propagation(self,gradient): - - gradient_activation = self.cal_gradient() # i * i 维 - gradient = np.asmatrix(np.dot(gradient.T,gradient_activation)) + def back_propagation(self, gradient): + gradient_activation = self.cal_gradient() # i * i 维 + gradient = np.asmatrix(np.dot(gradient.T, gradient_activation)) self._gradient_weight = np.asmatrix(self.xdata) self._gradient_bias = -1 self._gradient_x = self.weight - self.gradient_weight = np.dot(gradient.T,self._gradient_weight.T) + self.gradient_weight = np.dot(gradient.T, self._gradient_weight.T) self.gradient_bias = gradient * self._gradient_bias - self.gradient = np.dot(gradient,self._gradient_x).T - # ----------------------upgrade - # -----------the Negative gradient direction -------- + self.gradient = np.dot(gradient, self._gradient_x).T + # upgrade: the Negative gradient direction self.weight = self.weight - self.learn_rate * self.gradient_weight self.bias = self.bias - self.learn_rate * self.gradient_bias.T - + # updates the weights and bias according to learning rate (0.3 if undefined) return self.gradient -class BPNN(): - ''' +class BPNN: + """ Back Propagation Neural Network model - ''' + """ + def __init__(self): self.layers = [] self.train_mse = [] self.fig_loss = plt.figure() - self.ax_loss = self.fig_loss.add_subplot(1,1,1) + self.ax_loss = self.fig_loss.add_subplot(1, 1, 1) - def add_layer(self,layer): + def add_layer(self, layer): self.layers.append(layer) def build(self): - for i,layer in enumerate(self.layers[:]): + for i, layer in enumerate(self.layers[:]): if i < 1: layer.is_input_layer = True else: - layer.initializer(self.layers[i-1].units) + layer.initializer(self.layers[i - 1].units) def summary(self): - for i,layer in enumerate(self.layers[:]): - print('------- layer %d -------'%i) - print('weight.shape ',np.shape(layer.weight)) - print('bias.shape ',np.shape(layer.bias)) + for i, layer in enumerate(self.layers[:]): + print("------- layer %d -------" % i) + print("weight.shape ", np.shape(layer.weight)) + print("bias.shape ", np.shape(layer.bias)) - def train(self,xdata,ydata,train_round,accuracy): + def train(self, xdata, ydata, train_round, accuracy): self.train_round = train_round self.accuracy = accuracy @@ -129,8 +131,8 @@ def train(self,xdata,ydata,train_round,accuracy): for round_i in range(train_round): all_loss = 0 for row in range(x_shape[0]): - _xdata = np.asmatrix(xdata[row,:]).T - _ydata = np.asmatrix(ydata[row,:]).T + _xdata = np.asmatrix(xdata[row, :]).T + _ydata = np.asmatrix(ydata[row, :]).T # forward propagation for layer in self.layers: @@ -139,57 +141,59 @@ def train(self,xdata,ydata,train_round,accuracy): loss, gradient = self.cal_loss(_ydata, _xdata) all_loss = all_loss + loss - # back propagation - # the input_layer does not upgrade + # back propagation: the input_layer does not upgrade for layer in self.layers[:0:-1]: gradient = layer.back_propagation(gradient) - mse = all_loss/x_shape[0] + mse = all_loss / x_shape[0] self.train_mse.append(mse) self.plot_loss() if mse < self.accuracy: - print('----达到精度----') + print("----达到精度----") return mse - def cal_loss(self,ydata,ydata_): - self.loss = np.sum(np.power((ydata - ydata_),2)) + def cal_loss(self, ydata, ydata_): + self.loss = np.sum(np.power((ydata - ydata_), 2)) self.loss_gradient = 2 * (ydata_ - ydata) # vector (shape is the same as _ydata.shape) - return self.loss,self.loss_gradient + return self.loss, self.loss_gradient def plot_loss(self): if self.ax_loss.lines: self.ax_loss.lines.remove(self.ax_loss.lines[0]) - self.ax_loss.plot(self.train_mse, 'r-') + self.ax_loss.plot(self.train_mse, "r-") plt.ion() - plt.xlabel('step') - plt.ylabel('loss') + plt.xlabel("step") + plt.ylabel("loss") plt.show() plt.pause(0.1) - - def example(): - - x = np.random.randn(10,10) - y = np.asarray([[0.8,0.4],[0.4,0.3],[0.34,0.45],[0.67,0.32], - [0.88,0.67],[0.78,0.77],[0.55,0.66],[0.55,0.43],[0.54,0.1], - [0.1,0.5]]) - + x = np.random.randn(10, 10) + y = np.asarray( + [ + [0.8, 0.4], + [0.4, 0.3], + [0.34, 0.45], + [0.67, 0.32], + [0.88, 0.67], + [0.78, 0.77], + [0.55, 0.66], + [0.55, 0.43], + [0.54, 0.1], + [0.1, 0.5], + ] + ) model = BPNN() - model.add_layer(DenseLayer(10)) - model.add_layer(DenseLayer(20)) - model.add_layer(DenseLayer(30)) - model.add_layer(DenseLayer(2)) - + for i in (10, 20, 30, 2): + model.add_layer(DenseLayer(i)) model.build() - model.summary() + model.train(xdata=x, ydata=y, train_round=100, accuracy=0.01) - model.train(xdata=x,ydata=y,train_round=100,accuracy=0.01) -if __name__ == '__main__': +if __name__ == "__main__": example() diff --git a/neural_network/convolution_neural_network.py b/neural_network/convolution_neural_network.py index 0dca2bc485d1..d821488025ef 100644 --- a/neural_network/convolution_neural_network.py +++ b/neural_network/convolution_neural_network.py @@ -1,29 +1,29 @@ -#-*- coding: utf-8 -*- - -''' +""" - - - - - -- - - - - - - - - - - - - - - - - - - - - - - Name - - CNN - Convolution Neural Network For Photo Recognizing - Goal - - Recognize Handing Writting Word Photo + Goal - - Recognize Handing Writing Word Photo Detail:Total 5 layers neural network * Convolution layer * Pooling layer * Input layer layer of BP - * Hiden layer of BP + * Hidden layer of BP * Output layer of BP Author: Stephen Lee Github: 245885195@qq.com Date: 2017.9.20 - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - ''' -from __future__ import print_function +""" +import pickle import numpy as np -import matplotlib.pyplot as plt +from matplotlib import pyplot as plt -class CNN(): - def __init__(self,conv1_get,size_p1,bp_num1,bp_num2,bp_num3,rate_w=0.2,rate_t=0.2): - ''' +class CNN: + def __init__( + self, conv1_get, size_p1, bp_num1, bp_num2, bp_num3, rate_w=0.2, rate_t=0.2 + ): + """ :param conv1_get: [a,c,d],size, number, step of convolution kernel :param size_p1: pooling size :param bp_num1: units number of flatten layer @@ -31,7 +31,7 @@ def __init__(self,conv1_get,size_p1,bp_num1,bp_num2,bp_num3,rate_w=0.2,rate_t=0. :param bp_num3: units number of output layer :param rate_w: rate of weight learning :param rate_t: rate of threshold learning - ''' + """ self.num_bp1 = bp_num1 self.num_bp2 = bp_num2 self.num_bp3 = bp_num3 @@ -40,243 +40,288 @@ def __init__(self,conv1_get,size_p1,bp_num1,bp_num2,bp_num3,rate_w=0.2,rate_t=0. self.size_pooling1 = size_p1 self.rate_weight = rate_w self.rate_thre = rate_t - self.w_conv1 = [np.mat(-1*np.random.rand(self.conv1[0],self.conv1[0])+0.5) for i in range(self.conv1[1])] + self.w_conv1 = [ + np.mat(-1 * np.random.rand(self.conv1[0], self.conv1[0]) + 0.5) + for i in range(self.conv1[1]) + ] self.wkj = np.mat(-1 * np.random.rand(self.num_bp3, self.num_bp2) + 0.5) - self.vji = np.mat(-1*np.random.rand(self.num_bp2, self.num_bp1)+0.5) - self.thre_conv1 = -2*np.random.rand(self.conv1[1])+1 - self.thre_bp2 = -2*np.random.rand(self.num_bp2)+1 - self.thre_bp3 = -2*np.random.rand(self.num_bp3)+1 - + self.vji = np.mat(-1 * np.random.rand(self.num_bp2, self.num_bp1) + 0.5) + self.thre_conv1 = -2 * np.random.rand(self.conv1[1]) + 1 + self.thre_bp2 = -2 * np.random.rand(self.num_bp2) + 1 + self.thre_bp3 = -2 * np.random.rand(self.num_bp3) + 1 - def save_model(self,save_path): - #save model dict with pickle - import pickle - model_dic = {'num_bp1':self.num_bp1, - 'num_bp2':self.num_bp2, - 'num_bp3':self.num_bp3, - 'conv1':self.conv1, - 'step_conv1':self.step_conv1, - 'size_pooling1':self.size_pooling1, - 'rate_weight':self.rate_weight, - 'rate_thre':self.rate_thre, - 'w_conv1':self.w_conv1, - 'wkj':self.wkj, - 'vji':self.vji, - 'thre_conv1':self.thre_conv1, - 'thre_bp2':self.thre_bp2, - 'thre_bp3':self.thre_bp3} - with open(save_path, 'wb') as f: + def save_model(self, save_path): + # save model dict with pickle + model_dic = { + "num_bp1": self.num_bp1, + "num_bp2": self.num_bp2, + "num_bp3": self.num_bp3, + "conv1": self.conv1, + "step_conv1": self.step_conv1, + "size_pooling1": self.size_pooling1, + "rate_weight": self.rate_weight, + "rate_thre": self.rate_thre, + "w_conv1": self.w_conv1, + "wkj": self.wkj, + "vji": self.vji, + "thre_conv1": self.thre_conv1, + "thre_bp2": self.thre_bp2, + "thre_bp3": self.thre_bp3, + } + with open(save_path, "wb") as f: pickle.dump(model_dic, f) - print('Model saved: %s'% save_path) + print("Model saved: %s" % save_path) @classmethod - def ReadModel(cls,model_path): - #read saved model - import pickle - with open(model_path, 'rb') as f: + def ReadModel(cls, model_path): + # read saved model + with open(model_path, "rb") as f: model_dic = pickle.load(f) - conv_get= model_dic.get('conv1') - conv_get.append(model_dic.get('step_conv1')) - size_p1 = model_dic.get('size_pooling1') - bp1 = model_dic.get('num_bp1') - bp2 = model_dic.get('num_bp2') - bp3 = model_dic.get('num_bp3') - r_w = model_dic.get('rate_weight') - r_t = model_dic.get('rate_thre') - #create model instance - conv_ins = CNN(conv_get,size_p1,bp1,bp2,bp3,r_w,r_t) - #modify model parameter - conv_ins.w_conv1 = model_dic.get('w_conv1') - conv_ins.wkj = model_dic.get('wkj') - conv_ins.vji = model_dic.get('vji') - conv_ins.thre_conv1 = model_dic.get('thre_conv1') - conv_ins.thre_bp2 = model_dic.get('thre_bp2') - conv_ins.thre_bp3 = model_dic.get('thre_bp3') + conv_get = model_dic.get("conv1") + conv_get.append(model_dic.get("step_conv1")) + size_p1 = model_dic.get("size_pooling1") + bp1 = model_dic.get("num_bp1") + bp2 = model_dic.get("num_bp2") + bp3 = model_dic.get("num_bp3") + r_w = model_dic.get("rate_weight") + r_t = model_dic.get("rate_thre") + # create model instance + conv_ins = CNN(conv_get, size_p1, bp1, bp2, bp3, r_w, r_t) + # modify model parameter + conv_ins.w_conv1 = model_dic.get("w_conv1") + conv_ins.wkj = model_dic.get("wkj") + conv_ins.vji = model_dic.get("vji") + conv_ins.thre_conv1 = model_dic.get("thre_conv1") + conv_ins.thre_bp2 = model_dic.get("thre_bp2") + conv_ins.thre_bp3 = model_dic.get("thre_bp3") return conv_ins + def sig(self, x): + return 1 / (1 + np.exp(-1 * x)) - def sig(self,x): - return 1 / (1 + np.exp(-1*x)) - - def do_round(self,x): + def do_round(self, x): return round(x, 3) - def convolute(self,data,convs,w_convs,thre_convs,conv_step): - #convolution process + def convolute(self, data, convs, w_convs, thre_convs, conv_step): + # convolution process size_conv = convs[0] - num_conv =convs[1] + num_conv = convs[1] size_data = np.shape(data)[0] - #get the data slice of original image data, data_focus + # get the data slice of original image data, data_focus data_focus = [] for i_focus in range(0, size_data - size_conv + 1, conv_step): for j_focus in range(0, size_data - size_conv + 1, conv_step): - focus = data[i_focus:i_focus + size_conv, j_focus:j_focus + size_conv] + focus = data[ + i_focus : i_focus + size_conv, j_focus : j_focus + size_conv + ] data_focus.append(focus) - #caculate the feature map of every single kernel, and saved as list of matrix + # calculate the feature map of every single kernel, and saved as list of matrix data_featuremap = [] Size_FeatureMap = int((size_data - size_conv) / conv_step + 1) for i_map in range(num_conv): featuremap = [] for i_focus in range(len(data_focus)): - net_focus = np.sum(np.multiply(data_focus[i_focus], w_convs[i_map])) - thre_convs[i_map] + net_focus = ( + np.sum(np.multiply(data_focus[i_focus], w_convs[i_map])) + - thre_convs[i_map] + ) featuremap.append(self.sig(net_focus)) - featuremap = np.asmatrix(featuremap).reshape(Size_FeatureMap, Size_FeatureMap) + featuremap = np.asmatrix(featuremap).reshape( + Size_FeatureMap, Size_FeatureMap + ) data_featuremap.append(featuremap) - #expanding the data slice to One dimenssion + # expanding the data slice to One dimenssion focus1_list = [] for each_focus in data_focus: focus1_list.extend(self.Expand_Mat(each_focus)) focus_list = np.asarray(focus1_list) - return focus_list,data_featuremap + return focus_list, data_featuremap - def pooling(self,featuremaps,size_pooling,type='average_pool'): - #pooling process + def pooling(self, featuremaps, size_pooling, type="average_pool"): + # pooling process size_map = len(featuremaps[0]) - size_pooled = int(size_map/size_pooling) + size_pooled = int(size_map / size_pooling) featuremap_pooled = [] for i_map in range(len(featuremaps)): map = featuremaps[i_map] map_pooled = [] - for i_focus in range(0,size_map,size_pooling): + for i_focus in range(0, size_map, size_pooling): for j_focus in range(0, size_map, size_pooling): - focus = map[i_focus:i_focus + size_pooling, j_focus:j_focus + size_pooling] - if type == 'average_pool': - #average pooling + focus = map[ + i_focus : i_focus + size_pooling, + j_focus : j_focus + size_pooling, + ] + if type == "average_pool": + # average pooling map_pooled.append(np.average(focus)) - elif type == 'max_pooling': - #max pooling + elif type == "max_pooling": + # max pooling map_pooled.append(np.max(focus)) - map_pooled = np.asmatrix(map_pooled).reshape(size_pooled,size_pooled) + map_pooled = np.asmatrix(map_pooled).reshape(size_pooled, size_pooled) featuremap_pooled.append(map_pooled) return featuremap_pooled - def _expand(self,datas): - #expanding three dimension data to one dimension list + def _expand(self, data): + # expanding three dimension data to one dimension list data_expanded = [] - for i in range(len(datas)): - shapes = np.shape(datas[i]) - data_listed = datas[i].reshape(1,shapes[0]*shapes[1]) + for i in range(len(data)): + shapes = np.shape(data[i]) + data_listed = data[i].reshape(1, shapes[0] * shapes[1]) data_listed = data_listed.getA().tolist()[0] data_expanded.extend(data_listed) data_expanded = np.asarray(data_expanded) return data_expanded - def _expand_mat(self,data_mat): - #expanding matrix to one dimension list + def _expand_mat(self, data_mat): + # expanding matrix to one dimension list data_mat = np.asarray(data_mat) shapes = np.shape(data_mat) - data_expanded = data_mat.reshape(1,shapes[0]*shapes[1]) + data_expanded = data_mat.reshape(1, shapes[0] * shapes[1]) return data_expanded - def _calculate_gradient_from_pool(self,out_map,pd_pool,num_map,size_map,size_pooling): - ''' - calcluate the gradient from the data slice of pool layer + def _calculate_gradient_from_pool( + self, out_map, pd_pool, num_map, size_map, size_pooling + ): + """ + calculate the gradient from the data slice of pool layer pd_pool: list of matrix out_map: the shape of data slice(size_map*size_map) return: pd_all: list of matrix, [num, size_map, size_map] - ''' + """ pd_all = [] i_pool = 0 for i_map in range(num_map): pd_conv1 = np.ones((size_map, size_map)) for i in range(0, size_map, size_pooling): for j in range(0, size_map, size_pooling): - pd_conv1[i:i + size_pooling, j:j + size_pooling] = pd_pool[i_pool] + pd_conv1[i : i + size_pooling, j : j + size_pooling] = pd_pool[ + i_pool + ] i_pool = i_pool + 1 - pd_conv2 = np.multiply(pd_conv1,np.multiply(out_map[i_map],(1-out_map[i_map]))) + pd_conv2 = np.multiply( + pd_conv1, np.multiply(out_map[i_map], (1 - out_map[i_map])) + ) pd_all.append(pd_conv2) return pd_all - def trian(self,patterns,datas_train, datas_teach, n_repeat, error_accuracy,draw_e = bool): - #model traning - print('----------------------Start Training-------------------------') - print((' - - Shape: Train_Data ',np.shape(datas_train))) - print((' - - Shape: Teach_Data ',np.shape(datas_teach))) + def train( + self, patterns, datas_train, datas_teach, n_repeat, error_accuracy, draw_e=bool + ): + # model traning + print("----------------------Start Training-------------------------") + print((" - - Shape: Train_Data ", np.shape(datas_train))) + print((" - - Shape: Teach_Data ", np.shape(datas_teach))) rp = 0 all_mse = [] - mse = 10000 + mse = 10000 while rp < n_repeat and mse >= error_accuracy: - alle = 0 - print('-------------Learning Time %d--------------'%rp) + error_count = 0 + print("-------------Learning Time %d--------------" % rp) for p in range(len(datas_train)): - #print('------------Learning Image: %d--------------'%p) + # print('------------Learning Image: %d--------------'%p) data_train = np.asmatrix(datas_train[p]) data_teach = np.asarray(datas_teach[p]) - data_focus1,data_conved1 = self.convolute(data_train,self.conv1,self.w_conv1, - self.thre_conv1,conv_step=self.step_conv1) - data_pooled1 = self.pooling(data_conved1,self.size_pooling1) + data_focus1, data_conved1 = self.convolute( + data_train, + self.conv1, + self.w_conv1, + self.thre_conv1, + conv_step=self.step_conv1, + ) + data_pooled1 = self.pooling(data_conved1, self.size_pooling1) shape_featuremap1 = np.shape(data_conved1) - ''' + """ print(' -----original shape ', np.shape(data_train)) print(' ---- after convolution ',np.shape(data_conv1)) print(' -----after pooling ',np.shape(data_pooled1)) - ''' + """ data_bp_input = self._expand(data_pooled1) bp_out1 = data_bp_input - bp_net_j = np.dot(bp_out1,self.vji.T) - self.thre_bp2 + bp_net_j = np.dot(bp_out1, self.vji.T) - self.thre_bp2 bp_out2 = self.sig(bp_net_j) - bp_net_k = np.dot(bp_out2 ,self.wkj.T) - self.thre_bp3 + bp_net_k = np.dot(bp_out2, self.wkj.T) - self.thre_bp3 bp_out3 = self.sig(bp_net_k) - #--------------Model Leaning ------------------------ - # calcluate error and gradient--------------- - pd_k_all = np.multiply((data_teach - bp_out3), np.multiply(bp_out3, (1 - bp_out3))) - pd_j_all = np.multiply(np.dot(pd_k_all,self.wkj), np.multiply(bp_out2, (1 - bp_out2))) - pd_i_all = np.dot(pd_j_all,self.vji) + # --------------Model Leaning ------------------------ + # calculate error and gradient--------------- + pd_k_all = np.multiply( + (data_teach - bp_out3), np.multiply(bp_out3, (1 - bp_out3)) + ) + pd_j_all = np.multiply( + np.dot(pd_k_all, self.wkj), np.multiply(bp_out2, (1 - bp_out2)) + ) + pd_i_all = np.dot(pd_j_all, self.vji) - pd_conv1_pooled = pd_i_all / (self.size_pooling1*self.size_pooling1) + pd_conv1_pooled = pd_i_all / (self.size_pooling1 * self.size_pooling1) pd_conv1_pooled = pd_conv1_pooled.T.getA().tolist() - pd_conv1_all = self._calculate_gradient_from_pool(data_conved1,pd_conv1_pooled,shape_featuremap1[0], - shape_featuremap1[1],self.size_pooling1) - #weight and threshold learning process--------- - #convolution layer + pd_conv1_all = self._calculate_gradient_from_pool( + data_conved1, + pd_conv1_pooled, + shape_featuremap1[0], + shape_featuremap1[1], + self.size_pooling1, + ) + # weight and threshold learning process--------- + # convolution layer for k_conv in range(self.conv1[1]): pd_conv_list = self._expand_mat(pd_conv1_all[k_conv]) - delta_w = self.rate_weight * np.dot(pd_conv_list,data_focus1) + delta_w = self.rate_weight * np.dot(pd_conv_list, data_focus1) - self.w_conv1[k_conv] = self.w_conv1[k_conv] + delta_w.reshape((self.conv1[0],self.conv1[0])) + self.w_conv1[k_conv] = self.w_conv1[k_conv] + delta_w.reshape( + (self.conv1[0], self.conv1[0]) + ) - self.thre_conv1[k_conv] = self.thre_conv1[k_conv] - np.sum(pd_conv1_all[k_conv]) * self.rate_thre - #all connected layer + self.thre_conv1[k_conv] = ( + self.thre_conv1[k_conv] + - np.sum(pd_conv1_all[k_conv]) * self.rate_thre + ) + # all connected layer self.wkj = self.wkj + pd_k_all.T * bp_out2 * self.rate_weight self.vji = self.vji + pd_j_all.T * bp_out1 * self.rate_weight self.thre_bp3 = self.thre_bp3 - pd_k_all * self.rate_thre self.thre_bp2 = self.thre_bp2 - pd_j_all * self.rate_thre # calculate the sum error of all single image - errors = np.sum(abs((data_teach - bp_out3))) - alle = alle + errors - #print(' ----Teach ',data_teach) - #print(' ----BP_output ',bp_out3) + errors = np.sum(abs(data_teach - bp_out3)) + error_count += errors + # print(' ----Teach ',data_teach) + # print(' ----BP_output ',bp_out3) rp = rp + 1 - mse = alle/patterns + mse = error_count / patterns all_mse.append(mse) + def draw_error(): yplot = [error_accuracy for i in range(int(n_repeat * 1.2))] - plt.plot(all_mse, '+-') - plt.plot(yplot, 'r--') - plt.xlabel('Learning Times') - plt.ylabel('All_mse') + plt.plot(all_mse, "+-") + plt.plot(yplot, "r--") + plt.xlabel("Learning Times") + plt.ylabel("All_mse") plt.grid(True, alpha=0.5) plt.show() - print('------------------Training Complished---------------------') - print((' - - Training epoch: ', rp, ' - - Mse: %.6f' % mse)) + + print("------------------Training Complished---------------------") + print((" - - Training epoch: ", rp, " - - Mse: %.6f" % mse)) if draw_e: draw_error() return mse - def predict(self,datas_test): - #model predict + def predict(self, datas_test): + # model predict produce_out = [] - print('-------------------Start Testing-------------------------') - print((' - - Shape: Test_Data ',np.shape(datas_test))) + print("-------------------Start Testing-------------------------") + print((" - - Shape: Test_Data ", np.shape(datas_test))) for p in range(len(datas_test)): data_test = np.asmatrix(datas_test[p]) - data_focus1, data_conved1 = self.convolute(data_test, self.conv1, self.w_conv1, - self.thre_conv1, conv_step=self.step_conv1) + data_focus1, data_conved1 = self.convolute( + data_test, + self.conv1, + self.w_conv1, + self.thre_conv1, + conv_step=self.step_conv1, + ) data_pooled1 = self.pooling(data_conved1, self.size_pooling1) data_bp_input = self._expand(data_pooled1) @@ -286,21 +331,25 @@ def predict(self,datas_test): bp_net_k = bp_out2 * self.wkj.T - self.thre_bp3 bp_out3 = self.sig(bp_net_k) produce_out.extend(bp_out3.getA().tolist()) - res = [list(map(self.do_round,each)) for each in produce_out] + res = [list(map(self.do_round, each)) for each in produce_out] return np.asarray(res) - def convolution(self,data): - #return the data of image after convoluting process so we can check it out + def convolution(self, data): + # return the data of image after convoluting process so we can check it out data_test = np.asmatrix(data) - data_focus1, data_conved1 = self.convolute(data_test, self.conv1, self.w_conv1, - self.thre_conv1, conv_step=self.step_conv1) + data_focus1, data_conved1 = self.convolute( + data_test, + self.conv1, + self.w_conv1, + self.thre_conv1, + conv_step=self.step_conv1, + ) data_pooled1 = self.pooling(data_conved1, self.size_pooling1) - return data_conved1,data_pooled1 + return data_conved1, data_pooled1 -if __name__ == '__main__': - pass - ''' +if __name__ == "__main__": + """ I will put the example on other file - ''' \ No newline at end of file + """ diff --git a/neural_network/fcn.ipynb b/neural_network/fcn.ipynb deleted file mode 100644 index a8bcf4beeea1..000000000000 --- a/neural_network/fcn.ipynb +++ /dev/null @@ -1,327 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Standard (Fully Connected) Neural Network" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "#Use in Markup cell type\n", - "#![alt text](imagename.png \"Title\") " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Implementing Fully connected Neural Net" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Loading Required packages and Data" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using TensorFlow backend.\n" - ] - } - ], - "source": [ - "###1. Load Data and Splot Data\n", - "from keras.datasets import mnist\n", - "from keras.models import Sequential \n", - "from keras.layers.core import Dense, Activation\n", - "from keras.utils import np_utils\n", - "(X_train, Y_train), (X_test, Y_test) = mnist.load_data()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Preprocessing" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import matplotlib.pyplot as plt\n", - "n = 10 # how many digits we will display\n", - "plt.figure(figsize=(20, 4))\n", - "for i in range(n):\n", - " # display original\n", - " ax = plt.subplot(2, n, i + 1)\n", - " plt.imshow(X_test[i].reshape(28, 28))\n", - " plt.gray()\n", - " ax.get_xaxis().set_visible(False)\n", - " ax.get_yaxis().set_visible(False)\n", - "plt.show()\n", - "plt.close()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Previous X_train shape: (60000, 28, 28) \n", - "Previous Y_train shape:(60000,)\n", - "New X_train shape: (60000, 784) \n", - "New Y_train shape:(60000, 10)\n" - ] - } - ], - "source": [ - "print(\"Previous X_train shape: {} \\nPrevious Y_train shape:{}\".format(X_train.shape, Y_train.shape))\n", - "X_train = X_train.reshape(60000, 784) \n", - "X_test = X_test.reshape(10000, 784)\n", - "X_train = X_train.astype('float32') \n", - "X_test = X_test.astype('float32') \n", - "X_train /= 255 \n", - "X_test /= 255\n", - "classes = 10\n", - "Y_train = np_utils.to_categorical(Y_train, classes) \n", - "Y_test = np_utils.to_categorical(Y_test, classes)\n", - "print(\"New X_train shape: {} \\nNew Y_train shape:{}\".format(X_train.shape, Y_train.shape))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Setting up parameters" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "input_size = 784\n", - "batch_size = 200 \n", - "hidden1 = 400\n", - "hidden2 = 20\n", - "epochs = 2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Building the FCN Model" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "_________________________________________________________________\n", - "Layer (type) Output Shape Param # \n", - "=================================================================\n", - "dense_1 (Dense) (None, 400) 314000 \n", - "_________________________________________________________________\n", - "dense_2 (Dense) (None, 20) 8020 \n", - "_________________________________________________________________\n", - "dense_3 (Dense) (None, 10) 210 \n", - "=================================================================\n", - "Total params: 322,230\n", - "Trainable params: 322,230\n", - "Non-trainable params: 0\n", - "_________________________________________________________________\n" - ] - } - ], - "source": [ - "###4.Build the model\n", - "model = Sequential() \n", - "model.add(Dense(hidden1, input_dim=input_size, activation='relu'))\n", - "# output = relu (dot (W, input) + bias)\n", - "model.add(Dense(hidden2, activation='relu'))\n", - "model.add(Dense(classes, activation='softmax')) \n", - "\n", - "# Compilation\n", - "model.compile(loss='categorical_crossentropy', \n", - " metrics=['accuracy'], optimizer='sgd')\n", - "model.summary()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Training The Model" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Epoch 1/10\n", - " - 12s - loss: 1.4482 - acc: 0.6251\n", - "Epoch 2/10\n", - " - 3s - loss: 0.6239 - acc: 0.8482\n", - "Epoch 3/10\n", - " - 3s - loss: 0.4582 - acc: 0.8798\n", - "Epoch 4/10\n", - " - 3s - loss: 0.3941 - acc: 0.8936\n", - "Epoch 5/10\n", - " - 3s - loss: 0.3579 - acc: 0.9011\n", - "Epoch 6/10\n", - " - 4s - loss: 0.3328 - acc: 0.9070\n", - "Epoch 7/10\n", - " - 3s - loss: 0.3138 - acc: 0.9118\n", - "Epoch 8/10\n", - " - 3s - loss: 0.2980 - acc: 0.9157\n", - "Epoch 9/10\n", - " - 3s - loss: 0.2849 - acc: 0.9191\n", - "Epoch 10/10\n", - " - 3s - loss: 0.2733 - acc: 0.9223\n" - ] - }, - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Fitting on Data\n", - "model.fit(X_train, Y_train, batch_size=batch_size, epochs=10, verbose=2)\n", - "###5.Test " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "#### Testing The Model" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "10000/10000 [==============================] - 1s 121us/step\n", - "\n", - "Test accuracy: 0.9257\n", - "[0 6 9 0 1 5 9 7 3 4]\n" - ] - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAABHEAAABzCAYAAAAfb55ZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cG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kydbY4sXL5b8r3/9K8wVA0DO07+36e8YxhgzcODAsM7RpUsXyfPmzbPGunfvLvnEiRORLBEJ5h//+IfkZcuWWWP63kO84EkcAAAAAACAAOAmDgAAAAAAQAAEppxq1KhRknVnKVf+/Pkl644Lx44ds+adPn3a8xz68WT9WvrcxhgzdepUyXv37rXGtm/f7nn+ZFWiRIlM//nbb79tHVNCBeSc6667TnKvXr2ssd///veSS5Ys6XkOXUKlyzmM+XH3nKuKFy9uHbdv3/7ai0W2uWVS4c6jnMofEydOtI719aJLtt2uUOPHj890zP1uo0umEHvuddSkSRPJ+rPx1ltvtebp7mHz58+3xl544QUfVwjEp379+knu27dvROfInTu35NatW1tjejuOp556SjKlpsklV64fnmfRfyd27twZi+VkCU/iAAAAAAAABAA3cQAAAAAAAAKAmzgAAAAAAAABELd74txzzz3WcbNmzTKdd+jQIeu4bdu2kj/99FPJp06dsuadOXPG87V1fZxud+22tNNtz4cMGWKNdezYUfLJkyc9XyuZFCpUSPKlS5cku3viIDHoltQjRoyQXL9+fWuevt5CtaceMGCA5KNHj1rz6tSpI3nlypXW2Pnz57Oy7KTTuXNnyc8991xE51i9erXkhx56KKw/oz+rjWFPnHgzdOjQWC8hIS1atMg6btSokWS9102NGjVybE3IPr3nn36P77vvPmue3nNRf3/ds2ePNa9UqVKS3c/lAwcOSJ47d26EK04s6enpkt944w3Jes+3a9HfFZYuXeo5T//313tV3X///da848ePS167dm3Y68AV+/fv9xzTe4lNmDDBGtuxY4dk/f4PHz7cmqev2SVLlkjWe7AaY8yLL74oWe9bhsRQpUoVye5ejfGOJ3EAAAAAAAACgJs4AAAAAAAAARC35VS69MYYY4oVKyZZP0bnPvbmRxtUXdKhHynPmzevNe/ZZ5+V3LhxY2ts2rRpkpcvX57tNQWR2zKzQ4cOktevXy9Zt9JEsOhHVdPS0qyx1157TbJuT+22oA63PbV+1PmOO+6w5uk2ru3atbPGZs2a5bn+ZKXLVQcNGpTlP++2+9SPlLuPLPfu3TvL5wcSVdeuX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ND9PNXXlVv6pNehueXH586dy3Qeoufmm2/2HHPfj5deeinay4GitwIwxi4RDmXUqFGSKZ+KPzNmzMg0lyhRwppXsGBBybr81Rhj3n33Xcm6lHzv3r3WvI8++kjywYMHrbGLFy9mZdmIgN7Gwf2ZefLkyZxejm94EgcAAAAAACAAuIkDAAAAAAAQACmhOv78aHJKSviT4auMjIyUa8+6Nt7DmNqUkZFR3Y8TBe191CWLlStXtsb69u0refTo0Tm2pkgl8rXodkQpV66cZN3RSpdWGfPjTlPawoULJW/evFlyjLuqJO21mEgS+VpMIlyLxpjXX3/dOtadytztBXSnlXiRaNdi4cKFJX/xxRfWWNGiRSXrTjfvvfeeNa9u3bqSA9JFjGsxASTateiHXr16Sa5du7Y11rp1a8lxVEoc1rXIkzgAAAAAAAABwE0cAAAAAACAAOAmDgAAAAAAQADEVYtxAIlJt8R198RB/Pjmm2+s4w8//FDyY489ltPLAYCk0KxZM+tY71ep95RDzqhXr55kvQeOS++D06pVK2ssIPvgAAlP79sYag/HoOFJHAAAAAAAgADgJg4AAAAAAEAAUE4FIOpWrFghOTU11RrbuHFjTi8HAIC4kSsX/081nugS8P/85z/W2L59+yQ/8cQTkg8fPhz9hQHA//FTAwAAAAAAIAC4iQMAAAAAABAA3MQBAAAAAAAIgBTdxvCak1NSwp8MX2VkZKT4cR7ew5jalJGRUd2PE/E+xg7XYkLgWkwAXIsJgWsxAXAtJgSuxQTAtZgQwroWeRIHAAAAAAAgALiJAwAAAAAAEABZbTF+3BhzIBoLQUilfTwX72Hs8D4GH+9hYuB9DD7ew8TA+xh8vIeJgfcx+HgPE0NY72OW9sQBAAAAAABAbFBOBQAAAAAAEADcxAEAAAAAAAgAbuIAAAAAAAAEADdxAAAAAAAAAoCbOAAAAAAAAAHATRwAAAAAAIAA4CYOAAAAAABAAHATBwAAAAAAIAC4iQMAAAAAABAA/wOj6vqySBf1wwAAAABJRU5ErkJggg==\n", - "text/plain": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "score = model.evaluate(X_test, Y_test, verbose=1)\n", - "print('\\n''Test accuracy:', score[1])\n", - "mask = range(10,20)\n", - "X_valid = X_test[mask]\n", - "y_pred = model.predict_classes(X_valid)\n", - "print(y_pred)\n", - "plt.figure(figsize=(20, 4))\n", - "for i in range(n):\n", - " # display original\n", - " ax = plt.subplot(2, n, i + 1)\n", - " plt.imshow(X_valid[i].reshape(28, 28))\n", - " plt.gray()\n", - " ax.get_xaxis().set_visible(False)\n", - " ax.get_yaxis().set_visible(False)\n", - "plt.show()\n", - "plt.close()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/neural_network/gan.py_tf b/neural_network/gan.py_tf new file mode 100644 index 000000000000..deb062c48dc7 --- /dev/null +++ b/neural_network/gan.py_tf @@ -0,0 +1,506 @@ +import matplotlib.gridspec as gridspec +import matplotlib.pyplot as plt +import numpy as np +from sklearn.utils import shuffle +import input_data + +random_numer = 42 + +np.random.seed(random_numer) + + +def ReLu(x): + mask = (x > 0) * 1.0 + return mask * x + + +def d_ReLu(x): + mask = (x > 0) * 1.0 + return mask + + +def arctan(x): + return np.arctan(x) + + +def d_arctan(x): + return 1 / (1 + x ** 2) + + +def log(x): + return 1 / (1 + np.exp(-1 * x)) + + +def d_log(x): + return log(x) * (1 - log(x)) + + +def tanh(x): + return np.tanh(x) + + +def d_tanh(x): + return 1 - np.tanh(x) ** 2 + + +def plot(samples): + fig = plt.figure(figsize=(4, 4)) + gs = gridspec.GridSpec(4, 4) + gs.update(wspace=0.05, hspace=0.05) + + for i, sample in enumerate(samples): + ax = plt.subplot(gs[i]) + plt.axis("off") + ax.set_xticklabels([]) + ax.set_yticklabels([]) + ax.set_aspect("equal") + plt.imshow(sample.reshape(28, 28), cmap="Greys_r") + + return fig + + +if __name__ == "__main__": + # 1. Load Data and declare hyper + print("--------- Load Data ----------") + mnist = input_data.read_data_sets("MNIST_data", one_hot=False) + temp = mnist.test + images, labels = temp.images, temp.labels + images, labels = shuffle(np.asarray(images), np.asarray(labels)) + num_epoch = 10 + learing_rate = 0.00009 + G_input = 100 + hidden_input, hidden_input2, hidden_input3 = 128, 256, 346 + hidden_input4, hidden_input5, hidden_input6 = 480, 560, 686 + + print("--------- Declare Hyper Parameters ----------") + # 2. Declare Weights + D_W1 = ( + np.random.normal(size=(784, hidden_input), scale=(1.0 / np.sqrt(784 / 2.0))) + * 0.002 + ) + # D_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002 + D_b1 = np.zeros(hidden_input) + + D_W2 = ( + np.random.normal( + size=(hidden_input, 1), scale=(1.0 / np.sqrt(hidden_input / 2.0)) + ) + * 0.002 + ) + # D_b2 = np.random.normal(size=(1),scale=(1. / np.sqrt(1 / 2.))) *0.002 + D_b2 = np.zeros(1) + + G_W1 = ( + np.random.normal( + size=(G_input, hidden_input), scale=(1.0 / np.sqrt(G_input / 2.0)) + ) + * 0.002 + ) + # G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002 + G_b1 = np.zeros(hidden_input) + + G_W2 = ( + np.random.normal( + size=(hidden_input, hidden_input2), + scale=(1.0 / np.sqrt(hidden_input / 2.0)), + ) + * 0.002 + ) + # G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002 + G_b2 = np.zeros(hidden_input2) + + G_W3 = ( + np.random.normal( + size=(hidden_input2, hidden_input3), + scale=(1.0 / np.sqrt(hidden_input2 / 2.0)), + ) + * 0.002 + ) + # G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002 + G_b3 = np.zeros(hidden_input3) + + G_W4 = ( + np.random.normal( + size=(hidden_input3, hidden_input4), + scale=(1.0 / np.sqrt(hidden_input3 / 2.0)), + ) + * 0.002 + ) + # G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002 + G_b4 = np.zeros(hidden_input4) + + G_W5 = ( + np.random.normal( + size=(hidden_input4, hidden_input5), + scale=(1.0 / np.sqrt(hidden_input4 / 2.0)), + ) + * 0.002 + ) + # G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002 + G_b5 = np.zeros(hidden_input5) + + G_W6 = ( + np.random.normal( + size=(hidden_input5, hidden_input6), + scale=(1.0 / np.sqrt(hidden_input5 / 2.0)), + ) + * 0.002 + ) + # G_b1 = np.random.normal(size=(128),scale=(1. / np.sqrt(128 / 2.))) *0.002 + G_b6 = np.zeros(hidden_input6) + + G_W7 = ( + np.random.normal( + size=(hidden_input6, 784), scale=(1.0 / np.sqrt(hidden_input6 / 2.0)) + ) + * 0.002 + ) + # G_b2 = np.random.normal(size=(784),scale=(1. / np.sqrt(784 / 2.))) *0.002 + G_b7 = np.zeros(784) + + # 3. For Adam Optimzier + v1, m1 = 0, 0 + v2, m2 = 0, 0 + v3, m3 = 0, 0 + v4, m4 = 0, 0 + + v5, m5 = 0, 0 + v6, m6 = 0, 0 + v7, m7 = 0, 0 + v8, m8 = 0, 0 + v9, m9 = 0, 0 + v10, m10 = 0, 0 + v11, m11 = 0, 0 + v12, m12 = 0, 0 + + v13, m13 = 0, 0 + v14, m14 = 0, 0 + + v15, m15 = 0, 0 + v16, m16 = 0, 0 + + v17, m17 = 0, 0 + v18, m18 = 0, 0 + + beta_1, beta_2, eps = 0.9, 0.999, 0.00000001 + + print("--------- Started Training ----------") + for iter in range(num_epoch): + + random_int = np.random.randint(len(images) - 5) + current_image = np.expand_dims(images[random_int], axis=0) + + # Func: Generate The first Fake Data + Z = np.random.uniform(-1.0, 1.0, size=[1, G_input]) + Gl1 = Z.dot(G_W1) + G_b1 + Gl1A = arctan(Gl1) + Gl2 = Gl1A.dot(G_W2) + G_b2 + Gl2A = ReLu(Gl2) + Gl3 = Gl2A.dot(G_W3) + G_b3 + Gl3A = arctan(Gl3) + + Gl4 = Gl3A.dot(G_W4) + G_b4 + Gl4A = ReLu(Gl4) + Gl5 = Gl4A.dot(G_W5) + G_b5 + Gl5A = tanh(Gl5) + Gl6 = Gl5A.dot(G_W6) + G_b6 + Gl6A = ReLu(Gl6) + Gl7 = Gl6A.dot(G_W7) + G_b7 + + current_fake_data = log(Gl7) + + # Func: Forward Feed for Real data + Dl1_r = current_image.dot(D_W1) + D_b1 + Dl1_rA = ReLu(Dl1_r) + Dl2_r = Dl1_rA.dot(D_W2) + D_b2 + Dl2_rA = log(Dl2_r) + + # Func: Forward Feed for Fake Data + Dl1_f = current_fake_data.dot(D_W1) + D_b1 + Dl1_fA = ReLu(Dl1_f) + Dl2_f = Dl1_fA.dot(D_W2) + D_b2 + Dl2_fA = log(Dl2_f) + + # Func: Cost D + D_cost = -np.log(Dl2_rA) + np.log(1.0 - Dl2_fA) + + # Func: Gradient + grad_f_w2_part_1 = 1 / (1.0 - Dl2_fA) + grad_f_w2_part_2 = d_log(Dl2_f) + grad_f_w2_part_3 = Dl1_fA + grad_f_w2 = grad_f_w2_part_3.T.dot(grad_f_w2_part_1 * grad_f_w2_part_2) + grad_f_b2 = grad_f_w2_part_1 * grad_f_w2_part_2 + + grad_f_w1_part_1 = (grad_f_w2_part_1 * grad_f_w2_part_2).dot(D_W2.T) + grad_f_w1_part_2 = d_ReLu(Dl1_f) + grad_f_w1_part_3 = current_fake_data + grad_f_w1 = grad_f_w1_part_3.T.dot(grad_f_w1_part_1 * grad_f_w1_part_2) + grad_f_b1 = grad_f_w1_part_1 * grad_f_w1_part_2 + + grad_r_w2_part_1 = -1 / Dl2_rA + grad_r_w2_part_2 = d_log(Dl2_r) + grad_r_w2_part_3 = Dl1_rA + grad_r_w2 = grad_r_w2_part_3.T.dot(grad_r_w2_part_1 * grad_r_w2_part_2) + grad_r_b2 = grad_r_w2_part_1 * grad_r_w2_part_2 + + grad_r_w1_part_1 = (grad_r_w2_part_1 * grad_r_w2_part_2).dot(D_W2.T) + grad_r_w1_part_2 = d_ReLu(Dl1_r) + grad_r_w1_part_3 = current_image + grad_r_w1 = grad_r_w1_part_3.T.dot(grad_r_w1_part_1 * grad_r_w1_part_2) + grad_r_b1 = grad_r_w1_part_1 * grad_r_w1_part_2 + + grad_w1 = grad_f_w1 + grad_r_w1 + grad_b1 = grad_f_b1 + grad_r_b1 + + grad_w2 = grad_f_w2 + grad_r_w2 + grad_b2 = grad_f_b2 + grad_r_b2 + + # ---- Update Gradient ---- + m1 = beta_1 * m1 + (1 - beta_1) * grad_w1 + v1 = beta_2 * v1 + (1 - beta_2) * grad_w1 ** 2 + + m2 = beta_1 * m2 + (1 - beta_1) * grad_b1 + v2 = beta_2 * v2 + (1 - beta_2) * grad_b1 ** 2 + + m3 = beta_1 * m3 + (1 - beta_1) * grad_w2 + v3 = beta_2 * v3 + (1 - beta_2) * grad_w2 ** 2 + + m4 = beta_1 * m4 + (1 - beta_1) * grad_b2 + v4 = beta_2 * v4 + (1 - beta_2) * grad_b2 ** 2 + + D_W1 = D_W1 - (learing_rate / (np.sqrt(v1 / (1 - beta_2)) + eps)) * ( + m1 / (1 - beta_1) + ) + D_b1 = D_b1 - (learing_rate / (np.sqrt(v2 / (1 - beta_2)) + eps)) * ( + m2 / (1 - beta_1) + ) + + D_W2 = D_W2 - (learing_rate / (np.sqrt(v3 / (1 - beta_2)) + eps)) * ( + m3 / (1 - beta_1) + ) + D_b2 = D_b2 - (learing_rate / (np.sqrt(v4 / (1 - beta_2)) + eps)) * ( + m4 / (1 - beta_1) + ) + + # Func: Forward Feed for G + Z = np.random.uniform(-1.0, 1.0, size=[1, G_input]) + Gl1 = Z.dot(G_W1) + G_b1 + Gl1A = arctan(Gl1) + Gl2 = Gl1A.dot(G_W2) + G_b2 + Gl2A = ReLu(Gl2) + Gl3 = Gl2A.dot(G_W3) + G_b3 + Gl3A = arctan(Gl3) + + Gl4 = Gl3A.dot(G_W4) + G_b4 + Gl4A = ReLu(Gl4) + Gl5 = Gl4A.dot(G_W5) + G_b5 + Gl5A = tanh(Gl5) + Gl6 = Gl5A.dot(G_W6) + G_b6 + Gl6A = ReLu(Gl6) + Gl7 = Gl6A.dot(G_W7) + G_b7 + + current_fake_data = log(Gl7) + + Dl1 = current_fake_data.dot(D_W1) + D_b1 + Dl1_A = ReLu(Dl1) + Dl2 = Dl1_A.dot(D_W2) + D_b2 + Dl2_A = log(Dl2) + + # Func: Cost G + G_cost = -np.log(Dl2_A) + + # Func: Gradient + grad_G_w7_part_1 = ((-1 / Dl2_A) * d_log(Dl2).dot(D_W2.T) * (d_ReLu(Dl1))).dot( + D_W1.T + ) + grad_G_w7_part_2 = d_log(Gl7) + grad_G_w7_part_3 = Gl6A + grad_G_w7 = grad_G_w7_part_3.T.dot(grad_G_w7_part_1 * grad_G_w7_part_1) + grad_G_b7 = grad_G_w7_part_1 * grad_G_w7_part_2 + + grad_G_w6_part_1 = (grad_G_w7_part_1 * grad_G_w7_part_2).dot(G_W7.T) + grad_G_w6_part_2 = d_ReLu(Gl6) + grad_G_w6_part_3 = Gl5A + grad_G_w6 = grad_G_w6_part_3.T.dot(grad_G_w6_part_1 * grad_G_w6_part_2) + grad_G_b6 = grad_G_w6_part_1 * grad_G_w6_part_2 + + grad_G_w5_part_1 = (grad_G_w6_part_1 * grad_G_w6_part_2).dot(G_W6.T) + grad_G_w5_part_2 = d_tanh(Gl5) + grad_G_w5_part_3 = Gl4A + grad_G_w5 = grad_G_w5_part_3.T.dot(grad_G_w5_part_1 * grad_G_w5_part_2) + grad_G_b5 = grad_G_w5_part_1 * grad_G_w5_part_2 + + grad_G_w4_part_1 = (grad_G_w5_part_1 * grad_G_w5_part_2).dot(G_W5.T) + grad_G_w4_part_2 = d_ReLu(Gl4) + grad_G_w4_part_3 = Gl3A + grad_G_w4 = grad_G_w4_part_3.T.dot(grad_G_w4_part_1 * grad_G_w4_part_2) + grad_G_b4 = grad_G_w4_part_1 * grad_G_w4_part_2 + + grad_G_w3_part_1 = (grad_G_w4_part_1 * grad_G_w4_part_2).dot(G_W4.T) + grad_G_w3_part_2 = d_arctan(Gl3) + grad_G_w3_part_3 = Gl2A + grad_G_w3 = grad_G_w3_part_3.T.dot(grad_G_w3_part_1 * grad_G_w3_part_2) + grad_G_b3 = grad_G_w3_part_1 * grad_G_w3_part_2 + + grad_G_w2_part_1 = (grad_G_w3_part_1 * grad_G_w3_part_2).dot(G_W3.T) + grad_G_w2_part_2 = d_ReLu(Gl2) + grad_G_w2_part_3 = Gl1A + grad_G_w2 = grad_G_w2_part_3.T.dot(grad_G_w2_part_1 * grad_G_w2_part_2) + grad_G_b2 = grad_G_w2_part_1 * grad_G_w2_part_2 + + grad_G_w1_part_1 = (grad_G_w2_part_1 * grad_G_w2_part_2).dot(G_W2.T) + grad_G_w1_part_2 = d_arctan(Gl1) + grad_G_w1_part_3 = Z + grad_G_w1 = grad_G_w1_part_3.T.dot(grad_G_w1_part_1 * grad_G_w1_part_2) + grad_G_b1 = grad_G_w1_part_1 * grad_G_w1_part_2 + + # ---- Update Gradient ---- + m5 = beta_1 * m5 + (1 - beta_1) * grad_G_w1 + v5 = beta_2 * v5 + (1 - beta_2) * grad_G_w1 ** 2 + + m6 = beta_1 * m6 + (1 - beta_1) * grad_G_b1 + v6 = beta_2 * v6 + (1 - beta_2) * grad_G_b1 ** 2 + + m7 = beta_1 * m7 + (1 - beta_1) * grad_G_w2 + v7 = beta_2 * v7 + (1 - beta_2) * grad_G_w2 ** 2 + + m8 = beta_1 * m8 + (1 - beta_1) * grad_G_b2 + v8 = beta_2 * v8 + (1 - beta_2) * grad_G_b2 ** 2 + + m9 = beta_1 * m9 + (1 - beta_1) * grad_G_w3 + v9 = beta_2 * v9 + (1 - beta_2) * grad_G_w3 ** 2 + + m10 = beta_1 * m10 + (1 - beta_1) * grad_G_b3 + v10 = beta_2 * v10 + (1 - beta_2) * grad_G_b3 ** 2 + + m11 = beta_1 * m11 + (1 - beta_1) * grad_G_w4 + v11 = beta_2 * v11 + (1 - beta_2) * grad_G_w4 ** 2 + + m12 = beta_1 * m12 + (1 - beta_1) * grad_G_b4 + v12 = beta_2 * v12 + (1 - beta_2) * grad_G_b4 ** 2 + + m13 = beta_1 * m13 + (1 - beta_1) * grad_G_w5 + v13 = beta_2 * v13 + (1 - beta_2) * grad_G_w5 ** 2 + + m14 = beta_1 * m14 + (1 - beta_1) * grad_G_b5 + v14 = beta_2 * v14 + (1 - beta_2) * grad_G_b5 ** 2 + + m15 = beta_1 * m15 + (1 - beta_1) * grad_G_w6 + v15 = beta_2 * v15 + (1 - beta_2) * grad_G_w6 ** 2 + + m16 = beta_1 * m16 + (1 - beta_1) * grad_G_b6 + v16 = beta_2 * v16 + (1 - beta_2) * grad_G_b6 ** 2 + + m17 = beta_1 * m17 + (1 - beta_1) * grad_G_w7 + v17 = beta_2 * v17 + (1 - beta_2) * grad_G_w7 ** 2 + + m18 = beta_1 * m18 + (1 - beta_1) * grad_G_b7 + v18 = beta_2 * v18 + (1 - beta_2) * grad_G_b7 ** 2 + + G_W1 = G_W1 - (learing_rate / (np.sqrt(v5 / (1 - beta_2)) + eps)) * ( + m5 / (1 - beta_1) + ) + G_b1 = G_b1 - (learing_rate / (np.sqrt(v6 / (1 - beta_2)) + eps)) * ( + m6 / (1 - beta_1) + ) + + G_W2 = G_W2 - (learing_rate / (np.sqrt(v7 / (1 - beta_2)) + eps)) * ( + m7 / (1 - beta_1) + ) + G_b2 = G_b2 - (learing_rate / (np.sqrt(v8 / (1 - beta_2)) + eps)) * ( + m8 / (1 - beta_1) + ) + + G_W3 = G_W3 - (learing_rate / (np.sqrt(v9 / (1 - beta_2)) + eps)) * ( + m9 / (1 - beta_1) + ) + G_b3 = G_b3 - (learing_rate / (np.sqrt(v10 / (1 - beta_2)) + eps)) * ( + m10 / (1 - beta_1) + ) + + G_W4 = G_W4 - (learing_rate / (np.sqrt(v11 / (1 - beta_2)) + eps)) * ( + m11 / (1 - beta_1) + ) + G_b4 = G_b4 - (learing_rate / (np.sqrt(v12 / (1 - beta_2)) + eps)) * ( + m12 / (1 - beta_1) + ) + + G_W5 = G_W5 - (learing_rate / (np.sqrt(v13 / (1 - beta_2)) + eps)) * ( + m13 / (1 - beta_1) + ) + G_b5 = G_b5 - (learing_rate / (np.sqrt(v14 / (1 - beta_2)) + eps)) * ( + m14 / (1 - beta_1) + ) + + G_W6 = G_W6 - (learing_rate / (np.sqrt(v15 / (1 - beta_2)) + eps)) * ( + m15 / (1 - beta_1) + ) + G_b6 = G_b6 - (learing_rate / (np.sqrt(v16 / (1 - beta_2)) + eps)) * ( + m16 / (1 - beta_1) + ) + + G_W7 = G_W7 - (learing_rate / (np.sqrt(v17 / (1 - beta_2)) + eps)) * ( + m17 / (1 - beta_1) + ) + G_b7 = G_b7 - (learing_rate / (np.sqrt(v18 / (1 - beta_2)) + eps)) * ( + m18 / (1 - beta_1) + ) + + # --- Print Error ---- + # print("Current Iter: ",iter, " Current D cost:",D_cost, " Current G cost: ", G_cost,end='\r') + + if iter == 0: + learing_rate = learing_rate * 0.01 + if iter == 40: + learing_rate = learing_rate * 0.01 + + # ---- Print to Out put ---- + if iter % 10 == 0: + + print( + "Current Iter: ", + iter, + " Current D cost:", + D_cost, + " Current G cost: ", + G_cost, + end="\r", + ) + print("--------- Show Example Result See Tab Above ----------") + print("--------- Wait for the image to load ---------") + Z = np.random.uniform(-1.0, 1.0, size=[16, G_input]) + + Gl1 = Z.dot(G_W1) + G_b1 + Gl1A = arctan(Gl1) + Gl2 = Gl1A.dot(G_W2) + G_b2 + Gl2A = ReLu(Gl2) + Gl3 = Gl2A.dot(G_W3) + G_b3 + Gl3A = arctan(Gl3) + + Gl4 = Gl3A.dot(G_W4) + G_b4 + Gl4A = ReLu(Gl4) + Gl5 = Gl4A.dot(G_W5) + G_b5 + Gl5A = tanh(Gl5) + Gl6 = Gl5A.dot(G_W6) + G_b6 + Gl6A = ReLu(Gl6) + Gl7 = Gl6A.dot(G_W7) + G_b7 + + current_fake_data = log(Gl7) + + fig = plot(current_fake_data) + fig.savefig( + "Click_Me_{}.png".format( + str(iter).zfill(3) + + "_Ginput_" + + str(G_input) + + "_hiddenone" + + str(hidden_input) + + "_hiddentwo" + + str(hidden_input2) + + "_LR_" + + str(learing_rate) + ), + bbox_inches="tight", + ) + # for complete explanation visit https://towardsdatascience.com/only-numpy-implementing-gan-general-adversarial-networks-and-adam-optimizer-using-numpy-with-2a7e4e032021 + # -- end code -- diff --git a/neural_network/input_data.py_tf b/neural_network/input_data.py_tf new file mode 100644 index 000000000000..0e22ac0bcda5 --- /dev/null +++ b/neural_network/input_data.py_tf @@ -0,0 +1,345 @@ +# Copyright 2016 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Functions for downloading and reading MNIST data (deprecated). + +This module and all its submodules are deprecated. +""" + + +import collections +import gzip +import os + +import numpy +from six.moves import urllib +from six.moves import xrange # pylint: disable=redefined-builtin + +from tensorflow.python.framework import dtypes +from tensorflow.python.framework import random_seed +from tensorflow.python.platform import gfile +from tensorflow.python.util.deprecation import deprecated + +_Datasets = collections.namedtuple("_Datasets", ["train", "validation", "test"]) + +# CVDF mirror of http://yann.lecun.com/exdb/mnist/ +DEFAULT_SOURCE_URL = "https://storage.googleapis.com/cvdf-datasets/mnist/" + + +def _read32(bytestream): + dt = numpy.dtype(numpy.uint32).newbyteorder(">") + return numpy.frombuffer(bytestream.read(4), dtype=dt)[0] + + +@deprecated(None, "Please use tf.data to implement this functionality.") +def _extract_images(f): + """Extract the images into a 4D uint8 numpy array [index, y, x, depth]. + + Args: + f: A file object that can be passed into a gzip reader. + + Returns: + data: A 4D uint8 numpy array [index, y, x, depth]. + + Raises: + ValueError: If the bytestream does not start with 2051. + + """ + print("Extracting", f.name) + with gzip.GzipFile(fileobj=f) as bytestream: + magic = _read32(bytestream) + if magic != 2051: + raise ValueError( + "Invalid magic number %d in MNIST image file: %s" % (magic, f.name) + ) + num_images = _read32(bytestream) + rows = _read32(bytestream) + cols = _read32(bytestream) + buf = bytestream.read(rows * cols * num_images) + data = numpy.frombuffer(buf, dtype=numpy.uint8) + data = data.reshape(num_images, rows, cols, 1) + return data + + +@deprecated(None, "Please use tf.one_hot on tensors.") +def _dense_to_one_hot(labels_dense, num_classes): + """Convert class labels from scalars to one-hot vectors.""" + num_labels = labels_dense.shape[0] + index_offset = numpy.arange(num_labels) * num_classes + labels_one_hot = numpy.zeros((num_labels, num_classes)) + labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1 + return labels_one_hot + + +@deprecated(None, "Please use tf.data to implement this functionality.") +def _extract_labels(f, one_hot=False, num_classes=10): + """Extract the labels into a 1D uint8 numpy array [index]. + + Args: + f: A file object that can be passed into a gzip reader. + one_hot: Does one hot encoding for the result. + num_classes: Number of classes for the one hot encoding. + + Returns: + labels: a 1D uint8 numpy array. + + Raises: + ValueError: If the bystream doesn't start with 2049. + """ + print("Extracting", f.name) + with gzip.GzipFile(fileobj=f) as bytestream: + magic = _read32(bytestream) + if magic != 2049: + raise ValueError( + "Invalid magic number %d in MNIST label file: %s" % (magic, f.name) + ) + num_items = _read32(bytestream) + buf = bytestream.read(num_items) + labels = numpy.frombuffer(buf, dtype=numpy.uint8) + if one_hot: + return _dense_to_one_hot(labels, num_classes) + return labels + + +class _DataSet: + """Container class for a _DataSet (deprecated). + + THIS CLASS IS DEPRECATED. + """ + + @deprecated( + None, + "Please use alternatives such as official/mnist/_DataSet.py" + " from tensorflow/models.", + ) + def __init__( + self, + images, + labels, + fake_data=False, + one_hot=False, + dtype=dtypes.float32, + reshape=True, + seed=None, + ): + """Construct a _DataSet. + + one_hot arg is used only if fake_data is true. `dtype` can be either + `uint8` to leave the input as `[0, 255]`, or `float32` to rescale into + `[0, 1]`. Seed arg provides for convenient deterministic testing. + + Args: + images: The images + labels: The labels + fake_data: Ignore inages and labels, use fake data. + one_hot: Bool, return the labels as one hot vectors (if True) or ints (if + False). + dtype: Output image dtype. One of [uint8, float32]. `uint8` output has + range [0,255]. float32 output has range [0,1]. + reshape: Bool. If True returned images are returned flattened to vectors. + seed: The random seed to use. + """ + seed1, seed2 = random_seed.get_seed(seed) + # If op level seed is not set, use whatever graph level seed is returned + numpy.random.seed(seed1 if seed is None else seed2) + dtype = dtypes.as_dtype(dtype).base_dtype + if dtype not in (dtypes.uint8, dtypes.float32): + raise TypeError("Invalid image dtype %r, expected uint8 or float32" % dtype) + if fake_data: + self._num_examples = 10000 + self.one_hot = one_hot + else: + assert ( + images.shape[0] == labels.shape[0] + ), f"images.shape: {images.shape} labels.shape: {labels.shape}" + self._num_examples = images.shape[0] + + # Convert shape from [num examples, rows, columns, depth] + # to [num examples, rows*columns] (assuming depth == 1) + if reshape: + assert images.shape[3] == 1 + images = images.reshape( + images.shape[0], images.shape[1] * images.shape[2] + ) + if dtype == dtypes.float32: + # Convert from [0, 255] -> [0.0, 1.0]. + images = images.astype(numpy.float32) + images = numpy.multiply(images, 1.0 / 255.0) + self._images = images + self._labels = labels + self._epochs_completed = 0 + self._index_in_epoch = 0 + + @property + def images(self): + return self._images + + @property + def labels(self): + return self._labels + + @property + def num_examples(self): + return self._num_examples + + @property + def epochs_completed(self): + return self._epochs_completed + + def next_batch(self, batch_size, fake_data=False, shuffle=True): + """Return the next `batch_size` examples from this data set.""" + if fake_data: + fake_image = [1] * 784 + if self.one_hot: + fake_label = [1] + [0] * 9 + else: + fake_label = 0 + return ( + [fake_image for _ in xrange(batch_size)], + [fake_label for _ in xrange(batch_size)], + ) + start = self._index_in_epoch + # Shuffle for the first epoch + if self._epochs_completed == 0 and start == 0 and shuffle: + perm0 = numpy.arange(self._num_examples) + numpy.random.shuffle(perm0) + self._images = self.images[perm0] + self._labels = self.labels[perm0] + # Go to the next epoch + if start + batch_size > self._num_examples: + # Finished epoch + self._epochs_completed += 1 + # Get the rest examples in this epoch + rest_num_examples = self._num_examples - start + images_rest_part = self._images[start : self._num_examples] + labels_rest_part = self._labels[start : self._num_examples] + # Shuffle the data + if shuffle: + perm = numpy.arange(self._num_examples) + numpy.random.shuffle(perm) + self._images = self.images[perm] + self._labels = self.labels[perm] + # Start next epoch + start = 0 + self._index_in_epoch = batch_size - rest_num_examples + end = self._index_in_epoch + images_new_part = self._images[start:end] + labels_new_part = self._labels[start:end] + return ( + numpy.concatenate((images_rest_part, images_new_part), axis=0), + numpy.concatenate((labels_rest_part, labels_new_part), axis=0), + ) + else: + self._index_in_epoch += batch_size + end = self._index_in_epoch + return self._images[start:end], self._labels[start:end] + + +@deprecated(None, "Please write your own downloading logic.") +def _maybe_download(filename, work_directory, source_url): + """Download the data from source url, unless it's already here. + + Args: + filename: string, name of the file in the directory. + work_directory: string, path to working directory. + source_url: url to download from if file doesn't exist. + + Returns: + Path to resulting file. + """ + if not gfile.Exists(work_directory): + gfile.MakeDirs(work_directory) + filepath = os.path.join(work_directory, filename) + if not gfile.Exists(filepath): + urllib.request.urlretrieve(source_url, filepath) + with gfile.GFile(filepath) as f: + size = f.size() + print("Successfully downloaded", filename, size, "bytes.") + return filepath + + +@deprecated( + None, "Please use alternatives such as:" " tensorflow_datasets.load('mnist')" +) +def read_data_sets( + train_dir, + fake_data=False, + one_hot=False, + dtype=dtypes.float32, + reshape=True, + validation_size=5000, + seed=None, + source_url=DEFAULT_SOURCE_URL, +): + if fake_data: + + def fake(): + return _DataSet( + [], [], fake_data=True, one_hot=one_hot, dtype=dtype, seed=seed + ) + + train = fake() + validation = fake() + test = fake() + return _Datasets(train=train, validation=validation, test=test) + + if not source_url: # empty string check + source_url = DEFAULT_SOURCE_URL + + train_images_file = "train-images-idx3-ubyte.gz" + train_labels_file = "train-labels-idx1-ubyte.gz" + test_images_file = "t10k-images-idx3-ubyte.gz" + test_labels_file = "t10k-labels-idx1-ubyte.gz" + + local_file = _maybe_download( + train_images_file, train_dir, source_url + train_images_file + ) + with gfile.Open(local_file, "rb") as f: + train_images = _extract_images(f) + + local_file = _maybe_download( + train_labels_file, train_dir, source_url + train_labels_file + ) + with gfile.Open(local_file, "rb") as f: + train_labels = _extract_labels(f, one_hot=one_hot) + + local_file = _maybe_download( + test_images_file, train_dir, source_url + test_images_file + ) + with gfile.Open(local_file, "rb") as f: + test_images = _extract_images(f) + + local_file = _maybe_download( + test_labels_file, train_dir, source_url + test_labels_file + ) + with gfile.Open(local_file, "rb") as f: + test_labels = _extract_labels(f, one_hot=one_hot) + + if not 0 <= validation_size <= len(train_images): + raise ValueError( + f"Validation size should be between 0 and {len(train_images)}. Received: {validation_size}." + ) + + validation_images = train_images[:validation_size] + validation_labels = train_labels[:validation_size] + train_images = train_images[validation_size:] + train_labels = train_labels[validation_size:] + + options = dict(dtype=dtype, reshape=reshape, seed=seed) + + train = _DataSet(train_images, train_labels, **options) + validation = _DataSet(validation_images, validation_labels, **options) + test = _DataSet(test_images, test_labels, **options) + + return _Datasets(train=train, validation=validation, test=test) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index eb8b04e855d3..23b409b227c4 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -1,64 +1,115 @@ -''' - - Perceptron - w = w + N * (d(k) - y) * x(k) - - Using perceptron network for oil analysis, - with Measuring of 3 parameters that represent chemical characteristics we can classify the oil, in p1 or p2 - p1 = -1 - p2 = 1 - -''' -from __future__ import print_function - +""" + Perceptron + w = w + N * (d(k) - y) * x(k) + + Using perceptron network for oil analysis, with Measuring of 3 parameters + that represent chemical characteristics we can classify the oil, in p1 or p2 + p1 = -1 + p2 = 1 +""" import random class Perceptron: - def __init__(self, sample, exit, learn_rate=0.01, epoch_number=1000, bias=-1): + def __init__(self, sample, target, learning_rate=0.01, epoch_number=1000, bias=-1): + """ + Initializes a Perceptron network for oil analysis + :param sample: sample dataset of 3 parameters with shape [30,3] + :param target: variable for classification with two possible states -1 or 1 + :param learning_rate: learning rate used in optimizing. + :param epoch_number: number of epochs to train network on. + :param bias: bias value for the network. + + >>> p = Perceptron([], (0, 1, 2)) + Traceback (most recent call last): + ... + ValueError: Sample data can not be empty + >>> p = Perceptron(([0], 1, 2), []) + Traceback (most recent call last): + ... + ValueError: Target data can not be empty + >>> p = Perceptron(([0], 1, 2), (0, 1)) + Traceback (most recent call last): + ... + ValueError: Sample data and Target data do not have matching lengths + """ self.sample = sample - self.exit = exit - self.learn_rate = learn_rate + if len(self.sample) == 0: + raise ValueError("Sample data can not be empty") + self.target = target + if len(self.target) == 0: + raise ValueError("Target data can not be empty") + if len(self.sample) != len(self.target): + raise ValueError("Sample data and Target data do not have matching lengths") + self.learning_rate = learning_rate self.epoch_number = epoch_number self.bias = bias self.number_sample = len(sample) - self.col_sample = len(sample[0]) + self.col_sample = len(sample[0]) # number of columns in dataset self.weight = [] - def training(self): + def training(self) -> None: + """ + Trains perceptron for epochs <= given number of epochs + :return: None + >>> data = [[2.0149, 0.6192, 10.9263]] + >>> targets = [-1] + >>> perceptron = Perceptron(data,targets) + >>> perceptron.training() # doctest: +ELLIPSIS + ('\\nEpoch:\\n', ...) + ... + """ for sample in self.sample: sample.insert(0, self.bias) for i in range(self.col_sample): - self.weight.append(random.random()) + self.weight.append(random.random()) self.weight.insert(0, self.bias) epoch_count = 0 while True: - erro = False + has_misclassified = False for i in range(self.number_sample): u = 0 for j in range(self.col_sample + 1): u = u + self.weight[j] * self.sample[i][j] y = self.sign(u) - if y != self.exit[i]: - + if y != self.target[i]: for j in range(self.col_sample + 1): - - self.weight[j] = self.weight[j] + self.learn_rate * (self.exit[i] - y) * self.sample[i][j] - erro = True - #print('Epoch: \n',epoch_count) + self.weight[j] = ( + self.weight[j] + + self.learning_rate + * (self.target[i] - y) + * self.sample[i][j] + ) + has_misclassified = True + # print('Epoch: \n',epoch_count) epoch_count = epoch_count + 1 - # if you want controle the epoch or just by erro - if erro == False: - print(('\nEpoch:\n',epoch_count)) - print('------------------------\n') - #if epoch_count > self.epoch_number or not erro: + # if you want control the epoch or just by error + if not has_misclassified: + print(("\nEpoch:\n", epoch_count)) + print("------------------------\n") + # if epoch_count > self.epoch_number or not error: break - def sort(self, sample): + def sort(self, sample) -> None: + """ + :param sample: example row to classify as P1 or P2 + :return: None + >>> data = [[2.0149, 0.6192, 10.9263]] + >>> targets = [-1] + >>> perceptron = Perceptron(data,targets) + >>> perceptron.training() # doctest: +ELLIPSIS + ('\\nEpoch:\\n', ...) + ... + >>> perceptron.sort([-0.6508, 0.1097, 4.0009]) # doctest: +ELLIPSIS + ('Sample: ', ...) + classification: P... + """ + if len(self.sample) == 0: + raise ValueError("Sample data can not be empty") sample.insert(0, self.bias) u = 0 for i in range(self.col_sample + 1): @@ -66,14 +117,28 @@ def sort(self, sample): y = self.sign(u) - if y == -1: - print(('Sample: ', sample)) - print('classification: P1') + if y == -1: + print(("Sample: ", sample)) + print("classification: P1") else: - print(('Sample: ', sample)) - print('classification: P2') - - def sign(self, u): + print(("Sample: ", sample)) + print("classification: P2") + + def sign(self, u: float) -> int: + """ + threshold function for classification + :param u: input number + :return: 1 if the input is greater than 0, otherwise -1 + >>> data = [[0],[-0.5],[0.5]] + >>> targets = [1,-1,1] + >>> perceptron = Perceptron(data,targets) + >>> perceptron.sign(0) + 1 + >>> perceptron.sign(-0.5) + -1 + >>> perceptron.sign(0.5) + 1 + """ return 1 if u >= 0 else -1 @@ -107,18 +172,60 @@ def sign(self, u): [-0.1013, 0.5989, 7.1812], [2.4482, 0.9455, 11.2095], [2.0149, 0.6192, 10.9263], - [0.2012, 0.2611, 5.4631] + [0.2012, 0.2611, 5.4631], +] +exit = [ + -1, + -1, + -1, + 1, + 1, + -1, + 1, + -1, + 1, + 1, + -1, + 1, + -1, + -1, + -1, + -1, + 1, + 1, + 1, + 1, + -1, + 1, + 1, + 1, + 1, + -1, + -1, + 1, + -1, + 1, ] -exit = [-1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1] -network = Perceptron(sample=samples, exit = exit, learn_rate=0.01, epoch_number=1000, bias=-1) +if __name__ == "__main__": + import doctest -network.training() + doctest.testmod() -while True: - sample = [] - for i in range(3): - sample.insert(i, float(input('value: '))) - network.sort(sample) + network = Perceptron( + sample=samples, target=exit, learning_rate=0.01, epoch_number=1000, bias=-1 + ) + network.training() + print("Finished training perceptron") + print("Enter values to predict or q to exit") + while True: + sample = [] + for i in range(len(samples[0])): + observation = input("value: ").strip() + if observation == "q": + break + observation = float(observation) + sample.insert(i, observation) + network.sort(sample) diff --git a/other/__init__.py b/other/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/other/activity_selection.py b/other/activity_selection.py new file mode 100644 index 000000000000..c03956cce5d2 --- /dev/null +++ b/other/activity_selection.py @@ -0,0 +1,46 @@ +# flake8: noqa + +"""The following implementation assumes that the activities +are already sorted according to their finish time""" + +"""Prints a maximum set of activities that can be done by a +single person, one at a time""" +# n --> Total number of activities +# start[]--> An array that contains start time of all activities +# finish[] --> An array that contains finish time of all activities + + +def printMaxActivities(start, finish): + """ + >>> start = [1, 3, 0, 5, 8, 5] + >>> finish = [2, 4, 6, 7, 9, 9] + >>> printMaxActivities(start, finish) + The following activities are selected: + 0,1,3,4, + """ + n = len(finish) + print("The following activities are selected:") + + # The first activity is always selected + i = 0 + print(i, end=",") + + # Consider rest of the activities + for j in range(n): + + # If this activity has start time greater than + # or equal to the finish time of previously + # selected activity, then select it + if start[j] >= finish[i]: + print(j, end=",") + i = j + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + start = [1, 3, 0, 5, 8, 5] + finish = [2, 4, 6, 7, 9, 9] + printMaxActivities(start, finish) diff --git a/other/anagrams.py b/other/anagrams.py index 29b34fbdc5d3..0be013d5bc47 100644 --- a/other/anagrams.py +++ b/other/anagrams.py @@ -1,30 +1,35 @@ -from __future__ import print_function -import collections, pprint, time, os +import collections +import os +import pprint +import time start_time = time.time() -print('creating word list...') +print("creating word list...") path = os.path.split(os.path.realpath(__file__)) -with open(path[0] + '/words') as f: - word_list = sorted(list(set([word.strip().lower() for word in f]))) +with open(path[0] + "/words") as f: + word_list = sorted(list({word.strip().lower() for word in f})) + def signature(word): - return ''.join(sorted(word)) + return "".join(sorted(word)) + word_bysig = collections.defaultdict(list) for word in word_list: word_bysig[signature(word)].append(word) -def anagram(myword): - return word_bysig[signature(myword)] -print('finding anagrams...') -all_anagrams = {word: anagram(word) - for word in word_list if len(anagram(word)) > 1} +def anagram(my_word): + return word_bysig[signature(my_word)] + + +print("finding anagrams...") +all_anagrams = {word: anagram(word) for word in word_list if len(anagram(word)) > 1} -print('writing anagrams to file...') -with open('anagrams.txt', 'w') as file: - file.write('all_anagrams = ') +print("writing anagrams to file...") +with open("anagrams.txt", "w") as file: + file.write("all_anagrams = ") file.write(pprint.pformat(all_anagrams)) total_time = round(time.time() - start_time, 2) -print(('Done [', total_time, 'seconds ]')) +print(("Done [", total_time, "seconds ]")) diff --git a/other/autocomplete_using_trie.py b/other/autocomplete_using_trie.py new file mode 100644 index 000000000000..8aa0dc223680 --- /dev/null +++ b/other/autocomplete_using_trie.py @@ -0,0 +1,64 @@ +END = "#" + + +class Trie: + def __init__(self): + self._trie = {} + + def insert_word(self, text): + trie = self._trie + for char in text: + if char not in trie: + trie[char] = {} + trie = trie[char] + trie[END] = True + + def find_word(self, prefix): + trie = self._trie + for char in prefix: + if char in trie: + trie = trie[char] + else: + return [] + return self._elements(trie) + + def _elements(self, d): + result = [] + for c, v in d.items(): + if c == END: + sub_result = [" "] + else: + sub_result = [c + s for s in self._elements(v)] + result.extend(sub_result) + return tuple(result) + + +trie = Trie() +words = ("depart", "detergent", "daring", "dog", "deer", "deal") +for word in words: + trie.insert_word(word) + + +def autocomplete_using_trie(s): + """ + >>> trie = Trie() + >>> for word in words: + ... trie.insert_word(word) + ... + >>> matches = autocomplete_using_trie("de") + + "detergent " in matches + True + "dog " in matches + False + """ + suffixes = trie.find_word(s) + return tuple(s + w for w in suffixes) + + +def main(): + print(autocomplete_using_trie("de")) + + +if __name__ == "__main__": + main() diff --git a/other/binary_exponentiation.py b/other/binary_exponentiation.py index 1a30fb8fd266..dd4e70e74129 100644 --- a/other/binary_exponentiation.py +++ b/other/binary_exponentiation.py @@ -14,7 +14,7 @@ def b_expo(a, b): res = 1 while b > 0: - if b&1: + if b & 1: res *= a a *= a @@ -26,14 +26,15 @@ def b_expo(a, b): def b_expo_mod(a, b, c): res = 1 while b > 0: - if b&1: - res = ((res%c) * (a%c)) % c + if b & 1: + res = ((res % c) * (a % c)) % c a *= a b >>= 1 return res + """ * Wondering how this method works ! * It's pretty simple. diff --git a/other/binary_exponentiation_2.py b/other/binary_exponentiation_2.py index 217a616c99fb..51ec4baf2598 100644 --- a/other/binary_exponentiation_2.py +++ b/other/binary_exponentiation_2.py @@ -14,7 +14,7 @@ def b_expo(a, b): res = 0 while b > 0: - if b&1: + if b & 1: res += a a += a @@ -26,8 +26,8 @@ def b_expo(a, b): def b_expo_mod(a, b, c): res = 0 while b > 0: - if b&1: - res = ((res%c) + (a%c)) % c + if b & 1: + res = ((res % c) + (a % c)) % c a += a b >>= 1 diff --git "a/other/davis\342\200\223putnam\342\200\223logemann\342\200\223loveland.py" "b/other/davis\342\200\223putnam\342\200\223logemann\342\200\223loveland.py" new file mode 100644 index 000000000000..d16de6dd988b --- /dev/null +++ "b/other/davis\342\200\223putnam\342\200\223logemann\342\200\223loveland.py" @@ -0,0 +1,357 @@ +#!/usr/bin/env python3 + +""" +Davis–Putnam–Logemann–Loveland (DPLL) algorithm is a complete, backtracking-based +search algorithm for deciding the satisfiability of propositional logic formulae in +conjunctive normal form, i.e, for solving the Conjunctive Normal Form SATisfiability +(CNF-SAT) problem. + +For more information about the algorithm: https://en.wikipedia.org/wiki/DPLL_algorithm +""" + +import random +from typing import Dict, List + + +class Clause: + """ + A clause represented in Conjunctive Normal Form. + A clause is a set of literals, either complemented or otherwise. + For example: + {A1, A2, A3'} is the clause (A1 v A2 v A3') + {A5', A2', A1} is the clause (A5' v A2' v A1) + + Create model + >>> clause = Clause(["A1", "A2'", "A3"]) + >>> clause.evaluate({"A1": True}) + True + """ + + def __init__(self, literals: List[int]) -> None: + """ + Represent the literals and an assignment in a clause." + """ + # Assign all literals to None initially + self.literals = {literal: None for literal in literals} + + def __str__(self) -> str: + """ + To print a clause as in Conjunctive Normal Form. + >>> str(Clause(["A1", "A2'", "A3"])) + "{A1 , A2' , A3}" + """ + return "{" + " , ".join(self.literals) + "}" + + def __len__(self) -> int: + """ + To print a clause as in Conjunctive Normal Form. + >>> len(Clause([])) + 0 + >>> len(Clause(["A1", "A2'", "A3"])) + 3 + """ + return len(self.literals) + + def assign(self, model: Dict[str, bool]) -> None: + """ + Assign values to literals of the clause as given by model. + """ + for literal in self.literals: + symbol = literal[:2] + if symbol in model: + value = model[symbol] + else: + continue + if value is not None: + # Complement assignment if literal is in complemented form + if literal.endswith("'"): + value = not value + self.literals[literal] = value + + def evaluate(self, model: Dict[str, bool]) -> bool: + """ + Evaluates the clause with the assignments in model. + This has the following steps: + 1. Return True if both a literal and its complement exist in the clause. + 2. Return True if a single literal has the assignment True. + 3. Return None(unable to complete evaluation) if a literal has no assignment. + 4. Compute disjunction of all values assigned in clause. + """ + for literal in self.literals: + symbol = literal.rstrip("'") if literal.endswith("'") else literal + "'" + if symbol in self.literals: + return True + + self.assign(model) + for value in self.literals.values(): + if value in (True, None): + return value + return any(self.literals.values()) + + +class Formula: + """ + A formula represented in Conjunctive Normal Form. + A formula is a set of clauses. + For example, + {{A1, A2, A3'}, {A5', A2', A1}} is ((A1 v A2 v A3') and (A5' v A2' v A1)) + """ + + def __init__(self, clauses: List[Clause]) -> None: + """ + Represent the number of clauses and the clauses themselves. + """ + self.clauses = list(clauses) + + def __str__(self) -> str: + """ + To print a formula as in Conjunctive Normal Form. + str(Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])])) + "{{A1 , A2' , A3} , {A5' , A2' , A1}}" + """ + return "{" + " , ".join(str(clause) for clause in self.clauses) + "}" + + +def generate_clause() -> Clause: + """ + Randomly generate a clause. + All literals have the name Ax, where x is an integer from 1 to 5. + """ + literals = [] + no_of_literals = random.randint(1, 5) + base_var = "A" + i = 0 + while i < no_of_literals: + var_no = random.randint(1, 5) + var_name = base_var + str(var_no) + var_complement = random.randint(0, 1) + if var_complement == 1: + var_name += "'" + if var_name in literals: + i -= 1 + else: + literals.append(var_name) + i += 1 + return Clause(literals) + + +def generate_formula() -> Formula: + """ + Randomly generate a formula. + """ + clauses = set() + no_of_clauses = random.randint(1, 10) + while len(clauses) < no_of_clauses: + clauses.add(generate_clause()) + return Formula(set(clauses)) + + +def generate_parameters(formula: Formula) -> (List[Clause], List[str]): + """ + Return the clauses and symbols from a formula. + A symbol is the uncomplemented form of a literal. + For example, + Symbol of A3 is A3. + Symbol of A5' is A5. + + >>> formula = Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])]) + >>> clauses, symbols = generate_parameters(formula) + >>> clauses_list = [str(i) for i in clauses] + >>> clauses_list + ["{A1 , A2' , A3}", "{A5' , A2' , A1}"] + >>> symbols + ['A1', 'A2', 'A3', 'A5'] + """ + clauses = formula.clauses + symbols_set = [] + for clause in formula.clauses: + for literal in clause.literals: + symbol = literal[:2] + if symbol not in symbols_set: + symbols_set.append(symbol) + return clauses, symbols_set + + +def find_pure_symbols( + clauses: List[Clause], symbols: List[str], model: Dict[str, bool] +) -> (List[str], Dict[str, bool]): + """ + Return pure symbols and their values to satisfy clause. + Pure symbols are symbols in a formula that exist only + in one form, either complemented or otherwise. + For example, + { { A4 , A3 , A5' , A1 , A3' } , { A4 } , { A3 } } has + pure symbols A4, A5' and A1. + This has the following steps: + 1. Ignore clauses that have already evaluated to be True. + 2. Find symbols that occur only in one form in the rest of the clauses. + 3. Assign value True or False depending on whether the symbols occurs + in normal or complemented form respectively. + + >>> formula = Formula([Clause(["A1", "A2'", "A3"]), Clause(["A5'", "A2'", "A1"])]) + >>> clauses, symbols = generate_parameters(formula) + + >>> pure_symbols, values = find_pure_symbols(clauses, symbols, {}) + >>> pure_symbols + ['A1', 'A2', 'A3', 'A5'] + >>> values + {'A1': True, 'A2': False, 'A3': True, 'A5': False} + """ + pure_symbols = [] + assignment = dict() + literals = [] + + for clause in clauses: + if clause.evaluate(model) is True: + continue + for literal in clause.literals: + literals.append(literal) + + for s in symbols: + sym = s + "'" + if (s in literals and sym not in literals) or ( + s not in literals and sym in literals + ): + pure_symbols.append(s) + for p in pure_symbols: + assignment[p] = None + for s in pure_symbols: + sym = s + "'" + if s in literals: + assignment[s] = True + elif sym in literals: + assignment[s] = False + return pure_symbols, assignment + + +def find_unit_clauses( + clauses: List[Clause], model: Dict[str, bool] +) -> (List[str], Dict[str, bool]): + """ + Returns the unit symbols and their values to satisfy clause. + Unit symbols are symbols in a formula that are: + - Either the only symbol in a clause + - Or all other literals in that clause have been assigned False + This has the following steps: + 1. Find symbols that are the only occurrences in a clause. + 2. Find symbols in a clause where all other literals are assigned False. + 3. Assign True or False depending on whether the symbols occurs in + normal or complemented form respectively. + + >>> clause1 = Clause(["A4", "A3", "A5'", "A1", "A3'"]) + >>> clause2 = Clause(["A4"]) + >>> clause3 = Clause(["A3"]) + >>> clauses, symbols = generate_parameters(Formula([clause1, clause2, clause3])) + + >>> unit_clauses, values = find_unit_clauses(clauses, {}) + >>> unit_clauses + ['A4', 'A3'] + >>> values + {'A4': True, 'A3': True} + """ + unit_symbols = [] + for clause in clauses: + if len(clause) == 1: + unit_symbols.append(list(clause.literals.keys())[0]) + else: + Fcount, Ncount = 0, 0 + for literal, value in clause.literals.items(): + if value is False: + Fcount += 1 + elif value is None: + sym = literal + Ncount += 1 + if Fcount == len(clause) - 1 and Ncount == 1: + unit_symbols.append(sym) + assignment = dict() + for i in unit_symbols: + symbol = i[:2] + assignment[symbol] = len(i) == 2 + unit_symbols = [i[:2] for i in unit_symbols] + + return unit_symbols, assignment + + +def dpll_algorithm( + clauses: List[Clause], symbols: List[str], model: Dict[str, bool] +) -> (bool, Dict[str, bool]): + """ + Returns the model if the formula is satisfiable, else None + This has the following steps: + 1. If every clause in clauses is True, return True. + 2. If some clause in clauses is False, return False. + 3. Find pure symbols. + 4. Find unit symbols. + + >>> formula = Formula([Clause(["A4", "A3", "A5'", "A1", "A3'"]), Clause(["A4"])]) + >>> clauses, symbols = generate_parameters(formula) + + >>> soln, model = dpll_algorithm(clauses, symbols, {}) + >>> soln + True + >>> model + {'A4': True} + """ + check_clause_all_true = True + for clause in clauses: + clause_check = clause.evaluate(model) + if clause_check is False: + return False, None + elif clause_check is None: + check_clause_all_true = False + continue + + if check_clause_all_true: + return True, model + + try: + pure_symbols, assignment = find_pure_symbols(clauses, symbols, model) + except RecursionError: + print("raises a RecursionError and is") + return None, {} + P = None + if len(pure_symbols) > 0: + P, value = pure_symbols[0], assignment[pure_symbols[0]] + + if P: + tmp_model = model + tmp_model[P] = value + tmp_symbols = [i for i in symbols] + if P in tmp_symbols: + tmp_symbols.remove(P) + return dpll_algorithm(clauses, tmp_symbols, tmp_model) + + unit_symbols, assignment = find_unit_clauses(clauses, model) + P = None + if len(unit_symbols) > 0: + P, value = unit_symbols[0], assignment[unit_symbols[0]] + if P: + tmp_model = model + tmp_model[P] = value + tmp_symbols = [i for i in symbols] + if P in tmp_symbols: + tmp_symbols.remove(P) + return dpll_algorithm(clauses, tmp_symbols, tmp_model) + P = symbols[0] + rest = symbols[1:] + tmp1, tmp2 = model, model + tmp1[P], tmp2[P] = True, False + + return dpll_algorithm(clauses, rest, tmp1) or dpll_algorithm(clauses, rest, tmp2) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + formula = generate_formula() + print(f"The formula {formula} is", end=" ") + + clauses, symbols = generate_parameters(formula) + solution, model = dpll_algorithm(clauses, symbols, {}) + + if solution: + print(f"satisfiable with the assignment {model}.") + else: + print("not satisfiable.") diff --git a/other/detecting_english_programmatically.py b/other/detecting_english_programmatically.py index 005fd3c10ca3..44fb7191866b 100644 --- a/other/detecting_english_programmatically.py +++ b/other/detecting_english_programmatically.py @@ -1,18 +1,21 @@ import os -UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' -LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n' +UPPERLETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + " \t\n" + def loadDictionary(): path = os.path.split(os.path.realpath(__file__)) englishWords = {} - with open(path[0] + '/Dictionary.txt') as dictionaryFile: - for word in dictionaryFile.read().split('\n'): + with open(path[0] + "/dictionary.txt") as dictionaryFile: + for word in dictionaryFile.read().split("\n"): englishWords[word] = None return englishWords + ENGLISH_WORDS = loadDictionary() + def getEnglishCount(message): message = message.upper() message = removeNonLetters(message) @@ -28,14 +31,16 @@ def getEnglishCount(message): return float(matches) / len(possibleWords) + def removeNonLetters(message): lettersOnly = [] for symbol in message: if symbol in LETTERS_AND_SPACE: lettersOnly.append(symbol) - return ''.join(lettersOnly) + return "".join(lettersOnly) + -def isEnglish(message, wordPercentage = 20, letterPercentage = 85): +def isEnglish(message, wordPercentage=20, letterPercentage=85): """ >>> isEnglish('Hello World') True @@ -50,5 +55,7 @@ def isEnglish(message, wordPercentage = 20, letterPercentage = 85): return wordsMatch and lettersMatch -import doctest -doctest.testmod() +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/dictionary.txt b/other/dictionary.txt index 14528efe844f..75838996ba08 100644 --- a/other/dictionary.txt +++ b/other/dictionary.txt @@ -26131,6 +26131,7 @@ MICROECONOMICS MICROELECTRONICS MICROFILM MICROFILMS +MICROFINANCE MICROGRAMMING MICROINSTRUCTION MICROINSTRUCTIONS @@ -45330,4 +45331,4 @@ ZOROASTER ZOROASTRIAN ZULU ZULUS -ZURICH \ No newline at end of file +ZURICH diff --git a/other/dijkstra_bankers_algorithm.py b/other/dijkstra_bankers_algorithm.py new file mode 100644 index 000000000000..be7bceba125d --- /dev/null +++ b/other/dijkstra_bankers_algorithm.py @@ -0,0 +1,225 @@ +# A Python implementation of the Banker's Algorithm in Operating Systems using +# Processes and Resources +# { +# "Author: "Biney Kingsley (bluedistro@github.io), bineykingsley36@gmail.com", +# "Date": 28-10-2018 +# } +""" +The Banker's algorithm is a resource allocation and deadlock avoidance algorithm +developed by Edsger Dijkstra that tests for safety by simulating the allocation of +predetermined maximum possible amounts of all resources, and then makes a "s-state" +check to test for possible deadlock conditions for all other pending activities, +before deciding whether allocation should be allowed to continue. +[Source] Wikipedia +[Credit] Rosetta Code C implementation helped very much. + (https://rosettacode.org/wiki/Banker%27s_algorithm) +""" + +from __future__ import annotations + +import time + +import numpy as np + +test_claim_vector = [8, 5, 9, 7] +test_allocated_res_table = [ + [2, 0, 1, 1], + [0, 1, 2, 1], + [4, 0, 0, 3], + [0, 2, 1, 0], + [1, 0, 3, 0], +] +test_maximum_claim_table = [ + [3, 2, 1, 4], + [0, 2, 5, 2], + [5, 1, 0, 5], + [1, 5, 3, 0], + [3, 0, 3, 3], +] + + +class BankersAlgorithm: + def __init__( + self, + claim_vector: list[int], + allocated_resources_table: list[list[int]], + maximum_claim_table: list[list[int]], + ) -> None: + """ + :param claim_vector: A nxn/nxm list depicting the amount of each resources + (eg. memory, interface, semaphores, etc.) available. + :param allocated_resources_table: A nxn/nxm list depicting the amount of each + resource each process is currently holding + :param maximum_claim_table: A nxn/nxm list depicting how much of each resource + the system currently has available + """ + self.__claim_vector = claim_vector + self.__allocated_resources_table = allocated_resources_table + self.__maximum_claim_table = maximum_claim_table + + def __processes_resource_summation(self) -> list[int]: + """ + Check for allocated resources in line with each resource in the claim vector + """ + return [ + sum(p_item[i] for p_item in self.__allocated_resources_table) + for i in range(len(self.__allocated_resources_table[0])) + ] + + def __available_resources(self) -> list[int]: + """ + Check for available resources in line with each resource in the claim vector + """ + return np.array(self.__claim_vector) - np.array( + self.__processes_resource_summation() + ) + + def __need(self) -> list[list[int]]: + """ + Implement safety checker that calculates the needs by ensuring that + max_claim[i][j] - alloc_table[i][j] <= avail[j] + """ + return [ + list(np.array(self.__maximum_claim_table[i]) - np.array(allocated_resource)) + for i, allocated_resource in enumerate(self.__allocated_resources_table) + ] + + def __need_index_manager(self) -> dict[int, list[int]]: + """ + This function builds an index control dictionary to track original ids/indices + of processes when altered during execution of method "main" + Return: {0: [a: int, b: int], 1: [c: int, d: int]} + >>> (BankersAlgorithm(test_claim_vector, test_allocated_res_table, + ... test_maximum_claim_table)._BankersAlgorithm__need_index_manager() + ... ) # doctest: +NORMALIZE_WHITESPACE + {0: [1, 2, 0, 3], 1: [0, 1, 3, 1], 2: [1, 1, 0, 2], 3: [1, 3, 2, 0], + 4: [2, 0, 0, 3]} + """ + return {self.__need().index(i): i for i in self.__need()} + + def main(self, **kwargs) -> None: + """ + Utilize various methods in this class to simulate the Banker's algorithm + Return: None + >>> BankersAlgorithm(test_claim_vector, test_allocated_res_table, + ... test_maximum_claim_table).main(describe=True) + Allocated Resource Table + P1 2 0 1 1 + + P2 0 1 2 1 + + P3 4 0 0 3 + + P4 0 2 1 0 + + P5 1 0 3 0 + + System Resource Table + P1 3 2 1 4 + + P2 0 2 5 2 + + P3 5 1 0 5 + + P4 1 5 3 0 + + P5 3 0 3 3 + + Current Usage by Active Processes: 8 5 9 7 + Initial Available Resources: 1 2 2 2 + __________________________________________________ + + Process 3 is executing. + Updated available resource stack for processes: 5 2 2 5 + The process is in a safe state. + + Process 1 is executing. + Updated available resource stack for processes: 7 2 3 6 + The process is in a safe state. + + Process 2 is executing. + Updated available resource stack for processes: 7 3 5 7 + The process is in a safe state. + + Process 4 is executing. + Updated available resource stack for processes: 7 5 6 7 + The process is in a safe state. + + Process 5 is executing. + Updated available resource stack for processes: 8 5 9 7 + The process is in a safe state. + + """ + need_list = self.__need() + alloc_resources_table = self.__allocated_resources_table + available_resources = self.__available_resources() + need_index_manager = self.__need_index_manager() + for kw, val in kwargs.items(): + if kw and val is True: + self.__pretty_data() + print("_" * 50 + "\n") + while need_list: + safe = False + for each_need in need_list: + execution = True + for index, need in enumerate(each_need): + if need > available_resources[index]: + execution = False + break + if execution: + safe = True + # get the original index of the process from ind_ctrl db + for original_need_index, need_clone in need_index_manager.items(): + if each_need == need_clone: + process_number = original_need_index + print(f"Process {process_number + 1} is executing.") + # remove the process run from stack + need_list.remove(each_need) + # update available/freed resources stack + available_resources = np.array(available_resources) + np.array( + alloc_resources_table[process_number] + ) + print( + "Updated available resource stack for processes: " + + " ".join([str(x) for x in available_resources]) + ) + break + if safe: + print("The process is in a safe state.\n") + else: + print("System in unsafe state. Aborting...\n") + break + + def __pretty_data(self): + """ + Properly align display of the algorithm's solution + """ + print(" " * 9 + "Allocated Resource Table") + for item in self.__allocated_resources_table: + print( + f"P{self.__allocated_resources_table.index(item) + 1}" + + " ".join(f"{it:>8}" for it in item) + + "\n" + ) + print(" " * 9 + "System Resource Table") + for item in self.__maximum_claim_table: + print( + f"P{self.__maximum_claim_table.index(item) + 1}" + + " ".join(f"{it:>8}" for it in item) + + "\n" + ) + print( + "Current Usage by Active Processes: " + + " ".join(str(x) for x in self.__claim_vector) + ) + print( + "Initial Available Resources: " + + " ".join(str(x) for x in self.__available_resources()) + ) + time.sleep(1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/doomsday.py b/other/doomsday.py new file mode 100644 index 000000000000..d8fe261156a1 --- /dev/null +++ b/other/doomsday.py @@ -0,0 +1,59 @@ +#!/bin/python3 +# Doomsday algorithm info: https://en.wikipedia.org/wiki/Doomsday_rule + +DOOMSDAY_LEAP = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] +DOOMSDAY_NOT_LEAP = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5] +WEEK_DAY_NAMES = { + 0: "Sunday", + 1: "Monday", + 2: "Tuesday", + 3: "Wednesday", + 4: "Thursday", + 5: "Friday", + 6: "Saturday", +} + + +def get_week_day(year: int, month: int, day: int) -> str: + """Returns the week-day name out of a given date. + + >>> get_week_day(2020, 10, 24) + 'Saturday' + >>> get_week_day(2017, 10, 24) + 'Tuesday' + >>> get_week_day(2019, 5, 3) + 'Friday' + >>> get_week_day(1970, 9, 16) + 'Wednesday' + >>> get_week_day(1870, 8, 13) + 'Saturday' + >>> get_week_day(2040, 3, 14) + 'Wednesday' + + """ + # minimal input check: + assert len(str(year)) > 2, "year should be in YYYY format" + assert 1 <= month <= 12, "month should be between 1 to 12" + assert 1 <= day <= 31, "day should be between 1 to 31" + + # Doomsday algorithm: + century = year // 100 + century_anchor = (5 * (century % 4) + 2) % 7 + centurian = year % 100 + centurian_m = centurian % 12 + dooms_day = ( + (centurian // 12) + centurian_m + (centurian_m // 4) + century_anchor + ) % 7 + day_anchor = ( + DOOMSDAY_NOT_LEAP[month - 1] + if (year % 4 != 0) or (centurian == 0 and (year % 400) == 0) + else DOOMSDAY_LEAP[month - 1] + ) + week_day = (dooms_day + day - day_anchor) % 7 + return WEEK_DAY_NAMES[week_day] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/euclidean_gcd.py b/other/euclidean_gcd.py index 30853e172076..de4b250243db 100644 --- a/other/euclidean_gcd.py +++ b/other/euclidean_gcd.py @@ -1,19 +1,47 @@ -from __future__ import print_function -# https://en.wikipedia.org/wiki/Euclidean_algorithm +""" https://en.wikipedia.org/wiki/Euclidean_algorithm """ -def euclidean_gcd(a, b): + +def euclidean_gcd(a: int, b: int) -> int: + """ + Examples: + >>> euclidean_gcd(3, 5) + 1 + + >>> euclidean_gcd(6, 3) + 3 + """ while b: - t = b - b = a % b - a = t + a, b = b, a % b return a + +def euclidean_gcd_recursive(a: int, b: int) -> int: + """ + Recursive method for euclicedan gcd algorithm + + Examples: + >>> euclidean_gcd_recursive(3, 5) + 1 + + >>> euclidean_gcd_recursive(6, 3) + 3 + """ + return a if b == 0 else euclidean_gcd_recursive(b, a % b) + + def main(): - print("GCD(3, 5) = " + str(euclidean_gcd(3, 5))) - print("GCD(5, 3) = " + str(euclidean_gcd(5, 3))) - print("GCD(1, 3) = " + str(euclidean_gcd(1, 3))) - print("GCD(3, 6) = " + str(euclidean_gcd(3, 6))) - print("GCD(6, 3) = " + str(euclidean_gcd(6, 3))) + print(f"euclidean_gcd(3, 5) = {euclidean_gcd(3, 5)}") + print(f"euclidean_gcd(5, 3) = {euclidean_gcd(5, 3)}") + print(f"euclidean_gcd(1, 3) = {euclidean_gcd(1, 3)}") + print(f"euclidean_gcd(3, 6) = {euclidean_gcd(3, 6)}") + print(f"euclidean_gcd(6, 3) = {euclidean_gcd(6, 3)}") + + print(f"euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3, 5)}") + print(f"euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5, 3)}") + print(f"euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1, 3)}") + print(f"euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3, 6)}") + print(f"euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6, 3)}") + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/other/findingPrimes.py b/other/findingPrimes.py deleted file mode 100644 index 035a14f4a335..000000000000 --- a/other/findingPrimes.py +++ /dev/null @@ -1,21 +0,0 @@ -''' --The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or equal to a given value. --Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif -''' -from __future__ import print_function - - -from math import sqrt -def SOE(n): - check = round(sqrt(n)) #Need not check for multiples past the square root of n - - sieve = [False if i <2 else True for i in range(n+1)] #Set every index to False except for index 0 and 1 - - for i in range(2, check): - if(sieve[i] == True): #If i is a prime - for j in range(i+i, n+1, i): #Step through the list in increments of i(the multiples of the prime) - sieve[j] = False #Sets every multiple of i to False - - for i in range(n+1): - if(sieve[i] == True): - print(i, end=" ") diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index d87792f45558..6eec738c02e1 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -1,22 +1,24 @@ #!/usr/bin/python -# encoding=utf8 """ -The Fisher–Yates shuffle is an algorithm for generating a random permutation of a finite sequence. +The Fisher–Yates shuffle is an algorithm for generating a random permutation of a +finite sequence. For more details visit wikipedia/Fischer-Yates-Shuffle. """ import random -def FYshuffle(LIST): - for i in range(len(LIST)): - a = random.randint(0, len(LIST)-1) - b = random.randint(0, len(LIST)-1) - LIST[a], LIST[b] = LIST[b], LIST[a] - return LIST -if __name__ == '__main__': - integers = [0,1,2,3,4,5,6,7] - strings = ['python', 'says', 'hello', '!'] - print ('Fisher-Yates Shuffle:') - print ('List',integers, strings) - print ('FY Shuffle',FYshuffle(integers), FYshuffle(strings)) +def FYshuffle(list): + for i in range(len(list)): + a = random.randint(0, len(list) - 1) + b = random.randint(0, len(list) - 1) + list[a], list[b] = list[b], list[a] + return list + + +if __name__ == "__main__": + integers = [0, 1, 2, 3, 4, 5, 6, 7] + strings = ["python", "says", "hello", "!"] + print("Fisher-Yates Shuffle:") + print("List", integers, strings) + print("FY Shuffle", FYshuffle(integers), FYshuffle(strings)) diff --git a/other/frequency_finder.py b/other/frequency_finder.py index 6264b25bf303..48760a9deb09 100644 --- a/other/frequency_finder.py +++ b/other/frequency_finder.py @@ -1,29 +1,78 @@ # Frequency Finder # frequency taken from http://en.wikipedia.org/wiki/Letter_frequency -englishLetterFreq = {'E': 12.70, 'T': 9.06, 'A': 8.17, 'O': 7.51, 'I': 6.97, - 'N': 6.75, 'S': 6.33, 'H': 6.09, 'R': 5.99, 'D': 4.25, - 'L': 4.03, 'C': 2.78, 'U': 2.76, 'M': 2.41, 'W': 2.36, - 'F': 2.23, 'G': 2.02, 'Y': 1.97, 'P': 1.93, 'B': 1.29, - 'V': 0.98, 'K': 0.77, 'J': 0.15, 'X': 0.15, 'Q': 0.10, - 'Z': 0.07} -ETAOIN = 'ETAOINSHRDLCUMWFGYPBVKJXQZ' -LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +englishLetterFreq = { + "E": 12.70, + "T": 9.06, + "A": 8.17, + "O": 7.51, + "I": 6.97, + "N": 6.75, + "S": 6.33, + "H": 6.09, + "R": 5.99, + "D": 4.25, + "L": 4.03, + "C": 2.78, + "U": 2.76, + "M": 2.41, + "W": 2.36, + "F": 2.23, + "G": 2.02, + "Y": 1.97, + "P": 1.93, + "B": 1.29, + "V": 0.98, + "K": 0.77, + "J": 0.15, + "X": 0.15, + "Q": 0.10, + "Z": 0.07, +} +ETAOIN = "ETAOINSHRDLCUMWFGYPBVKJXQZ" +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + def getLetterCount(message): - letterCount = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0, - 'I': 0, 'J': 0, 'K': 0, 'L': 0, 'M': 0, 'N': 0, 'O': 0, 'P': 0, - 'Q': 0, 'R': 0, 'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0, - 'Y': 0, 'Z': 0} + letterCount = { + "A": 0, + "B": 0, + "C": 0, + "D": 0, + "E": 0, + "F": 0, + "G": 0, + "H": 0, + "I": 0, + "J": 0, + "K": 0, + "L": 0, + "M": 0, + "N": 0, + "O": 0, + "P": 0, + "Q": 0, + "R": 0, + "S": 0, + "T": 0, + "U": 0, + "V": 0, + "W": 0, + "X": 0, + "Y": 0, + "Z": 0, + } for letter in message.upper(): if letter in LETTERS: letterCount[letter] += 1 return letterCount + def getItemAtIndexZero(x): return x[0] + def getFrequencyOrder(message): letterToFreq = getLetterCount(message) freqToLetter = {} @@ -34,23 +83,24 @@ def getFrequencyOrder(message): freqToLetter[letterToFreq[letter]].append(letter) for freq in freqToLetter: - freqToLetter[freq].sort(key = ETAOIN.find, reverse = True) - freqToLetter[freq] = ''.join(freqToLetter[freq]) + freqToLetter[freq].sort(key=ETAOIN.find, reverse=True) + freqToLetter[freq] = "".join(freqToLetter[freq]) freqPairs = list(freqToLetter.items()) - freqPairs.sort(key = getItemAtIndexZero, reverse = True) + freqPairs.sort(key=getItemAtIndexZero, reverse=True) freqOrder = [] for freqPair in freqPairs: freqOrder.append(freqPair[1]) - return ''.join(freqOrder) + return "".join(freqOrder) + def englishFreqMatchScore(message): - ''' + """ >>> englishFreqMatchScore('Hello World') 1 - ''' + """ freqOrder = getFrequencyOrder(message) matchScore = 0 for commonLetter in ETAOIN[:6]: @@ -63,6 +113,8 @@ def englishFreqMatchScore(message): return matchScore -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() diff --git a/other/game_of_life/game_o_life.py b/other/game_of_life.py similarity index 52% rename from other/game_of_life/game_o_life.py rename to other/game_of_life.py index 1fdaa21b4a7b..09863993dc3a 100644 --- a/other/game_of_life/game_o_life.py +++ b/other/game_of_life.py @@ -1,4 +1,4 @@ -'''Conway's Game Of Life, Author Anurag Kumar(mailto:anuragkumarak95@gmail.com) +"""Conway's Game Of Life, Author Anurag Kumar(mailto:anuragkumarak95@gmail.com) Requirements: - numpy @@ -13,7 +13,7 @@ - $python3 game_o_life Game-Of-Life Rules: - + 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. @@ -26,28 +26,34 @@ 4. Any dead cell with exactly three live neighbours be- comes a live cell, as if by reproduction. - ''' + """ +import random +import sys + import numpy as np -import random, sys from matplotlib import pyplot as plt from matplotlib.colors import ListedColormap -usage_doc='Usage of script: script_nama ' +usage_doc = "Usage of script: script_nama " -choice = [0]*100 + [1]*10 +choice = [0] * 100 + [1] * 10 random.shuffle(choice) + def create_canvas(size): - canvas = [ [False for i in range(size)] for j in range(size)] + canvas = [[False for i in range(size)] for j in range(size)] return canvas + def seed(canvas): - for i,row in enumerate(canvas): - for j,_ in enumerate(row): - canvas[i][j]=bool(random.getrandbits(1)) + for i, row in enumerate(canvas): + for j, _ in enumerate(row): + canvas[i][j] = bool(random.getrandbits(1)) + def run(canvas): - ''' This function runs the rules of game through all points, and changes their status accordingly.(in the same canvas) + """This function runs the rules of game through all points, and changes their + status accordingly.(in the same canvas) @Args: -- canvas : canvas of population to run the rules on. @@ -55,63 +61,71 @@ def run(canvas): @returns: -- None - ''' + """ canvas = np.array(canvas) next_gen_canvas = np.array(create_canvas(canvas.shape[0])) for r, row in enumerate(canvas): for c, pt in enumerate(row): # print(r-1,r+2,c-1,c+2) - next_gen_canvas[r][c] = __judge_point(pt,canvas[r-1:r+2,c-1:c+2]) - + next_gen_canvas[r][c] = __judge_point( + pt, canvas[r - 1 : r + 2, c - 1 : c + 2] + ) + canvas = next_gen_canvas - del next_gen_canvas # cleaning memory as we move on. - return canvas.tolist() + del next_gen_canvas # cleaning memory as we move on. + return canvas.tolist() -def __judge_point(pt,neighbours): - dead = 0 + +def __judge_point(pt, neighbours): + dead = 0 alive = 0 # finding dead or alive neighbours count. for i in neighbours: for status in i: - if status: alive+=1 - else: dead+=1 + if status: + alive += 1 + else: + dead += 1 # handling duplicate entry for focus pt. - if pt : alive-=1 - else : dead-=1 - + if pt: + alive -= 1 + else: + dead -= 1 + # running the rules of game here. state = pt if pt: - if alive<2: - state=False - elif alive==2 or alive==3: - state=True - elif alive>3: - state=False + if alive < 2: + state = False + elif alive == 2 or alive == 3: + state = True + elif alive > 3: + state = False else: - if alive==3: - state=True + if alive == 3: + state = True return state -if __name__=='__main__': - if len(sys.argv) != 2: raise Exception(usage_doc) - +if __name__ == "__main__": + if len(sys.argv) != 2: + raise Exception(usage_doc) + canvas_size = int(sys.argv[1]) # main working structure of this module. - c=create_canvas(canvas_size) + c = create_canvas(canvas_size) seed(c) fig, ax = plt.subplots() - fig.show() - cmap = ListedColormap(['w','k']) + fig.show() + cmap = ListedColormap(["w", "k"]) try: while True: - c = run(c) - ax.matshow(c,cmap=cmap) + c = run(c) + ax.matshow(c, cmap=cmap) fig.canvas.draw() - ax.cla() + ax.cla() except KeyboardInterrupt: # do nothing. pass diff --git a/other/game_of_life/sample.gif b/other/game_of_life/sample.gif deleted file mode 100644 index 0bf2ae1f95e4..000000000000 Binary files a/other/game_of_life/sample.gif and /dev/null differ diff --git a/other/gauss_easter.py b/other/gauss_easter.py new file mode 100644 index 000000000000..4447d4ab86af --- /dev/null +++ b/other/gauss_easter.py @@ -0,0 +1,59 @@ +""" +https://en.wikipedia.org/wiki/Computus#Gauss'_Easter_algorithm +""" +import math +from datetime import datetime, timedelta + + +def gauss_easter(year: int) -> datetime: + """ + Calculation Gregorian easter date for given year + + >>> gauss_easter(2007) + datetime.datetime(2007, 4, 8, 0, 0) + + >>> gauss_easter(2008) + datetime.datetime(2008, 3, 23, 0, 0) + + >>> gauss_easter(2020) + datetime.datetime(2020, 4, 12, 0, 0) + + >>> gauss_easter(2021) + datetime.datetime(2021, 4, 4, 0, 0) + """ + metonic_cycle = year % 19 + julian_leap_year = year % 4 + non_leap_year = year % 7 + leap_day_inhibits = math.floor(year / 100) + lunar_orbit_correction = math.floor((13 + 8 * leap_day_inhibits) / 25) + leap_day_reinstall_number = leap_day_inhibits / 4 + secular_moon_shift = ( + 15 - lunar_orbit_correction + leap_day_inhibits - leap_day_reinstall_number + ) % 30 + century_starting_point = (4 + leap_day_inhibits - leap_day_reinstall_number) % 7 + + # days to be added to March 21 + days_to_add = (19 * metonic_cycle + secular_moon_shift) % 30 + + # PHM -> Paschal Full Moon + days_from_phm_to_sunday = ( + 2 * julian_leap_year + + 4 * non_leap_year + + 6 * days_to_add + + century_starting_point + ) % 7 + + if days_to_add == 29 and days_from_phm_to_sunday == 6: + return datetime(year, 4, 19) + elif days_to_add == 28 and days_from_phm_to_sunday == 6: + return datetime(year, 4, 18) + else: + return datetime(year, 3, 22) + timedelta( + days=int(days_to_add + days_from_phm_to_sunday) + ) + + +if __name__ == "__main__": + for year in (1994, 2000, 2010, 2021, 2023): + tense = "will be" if year > datetime.now().year else "was" + print(f"Easter in {year} {tense} {gauss_easter(year)}") diff --git a/other/greedy.py b/other/greedy.py new file mode 100644 index 000000000000..4b78bf1c0415 --- /dev/null +++ b/other/greedy.py @@ -0,0 +1,63 @@ +class things: + def __init__(self, name, value, weight): + self.name = name + self.value = value + self.weight = weight + + def __repr__(self): + return f"{self.__class__.__name__}({self.name}, {self.value}, {self.weight})" + + def get_value(self): + return self.value + + def get_name(self): + return self.name + + def get_weight(self): + return self.weight + + def value_Weight(self): + return self.value / self.weight + + +def build_menu(name, value, weight): + menu = [] + for i in range(len(value)): + menu.append(things(name[i], value[i], weight[i])) + return menu + + +def greedy(item, maxCost, keyFunc): + itemsCopy = sorted(item, key=keyFunc, reverse=True) + result = [] + totalValue, total_cost = 0.0, 0.0 + for i in range(len(itemsCopy)): + if (total_cost + itemsCopy[i].get_weight()) <= maxCost: + result.append(itemsCopy[i]) + total_cost += itemsCopy[i].get_weight() + totalValue += itemsCopy[i].get_value() + return (result, totalValue) + + +def test_greedy(): + """ + >>> food = ["Burger", "Pizza", "Coca Cola", "Rice", + ... "Sambhar", "Chicken", "Fries", "Milk"] + >>> value = [80, 100, 60, 70, 50, 110, 90, 60] + >>> weight = [40, 60, 40, 70, 100, 85, 55, 70] + >>> foods = build_menu(food, value, weight) + >>> foods # doctest: +NORMALIZE_WHITESPACE + [things(Burger, 80, 40), things(Pizza, 100, 60), things(Coca Cola, 60, 40), + things(Rice, 70, 70), things(Sambhar, 50, 100), things(Chicken, 110, 85), + things(Fries, 90, 55), things(Milk, 60, 70)] + >>> greedy(foods, 500, things.get_value) # doctest: +NORMALIZE_WHITESPACE + ([things(Chicken, 110, 85), things(Pizza, 100, 60), things(Fries, 90, 55), + things(Burger, 80, 40), things(Rice, 70, 70), things(Coca Cola, 60, 40), + things(Milk, 60, 70)], 570.0) + """ + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/integeration_by_simpson_approx.py b/other/integeration_by_simpson_approx.py new file mode 100644 index 000000000000..da0e1cffde02 --- /dev/null +++ b/other/integeration_by_simpson_approx.py @@ -0,0 +1,126 @@ +""" +Author : Syed Faizan ( 3rd Year IIIT Pune ) +Github : faizan2700 + +Purpose : You have one function f(x) which takes float integer and returns +float you have to integrate the function in limits a to b. +The approximation proposed by Thomas Simpsons in 1743 is one way to calculate +integration. + +( read article : https://cp-algorithms.com/num_methods/simpson-integration.html ) + +simpson_integration() takes function,lower_limit=a,upper_limit=b,precision and +returns the integration of function in given limit. +""" + +# constants +# the more the number of steps the more accurate +N_STEPS = 1000 + + +def f(x: float) -> float: + return x * x + + +""" +Summary of Simpson Approximation : + +By simpsons integration : +1. integration of fxdx with limit a to b is = + f(x0) + 4 * f(x1) + 2 * f(x2) + 4 * f(x3) + 2 * f(x4)..... + f(xn) +where x0 = a +xi = a + i * h +xn = b +""" + + +def simpson_integration(function, a: float, b: float, precision: int = 4) -> float: + + """ + Args: + function : the function which's integration is desired + a : the lower limit of integration + b : upper limit of integraion + precision : precision of the result,error required default is 4 + + Returns: + result : the value of the approximated integration of function in range a to b + + Raises: + AssertionError: function is not callable + AssertionError: a is not float or integer + AssertionError: function should return float or integer + AssertionError: b is not float or integer + AssertionError: precision is not positive integer + + >>> simpson_integration(lambda x : x*x,1,2,3) + 2.333 + + >>> simpson_integration(lambda x : x*x,'wrong_input',2,3) + Traceback (most recent call last): + ... + AssertionError: a should be float or integer your input : wrong_input + + >>> simpson_integration(lambda x : x*x,1,'wrong_input',3) + Traceback (most recent call last): + ... + AssertionError: b should be float or integer your input : wrong_input + + >>> simpson_integration(lambda x : x*x,1,2,'wrong_input') + Traceback (most recent call last): + ... + AssertionError: precision should be positive integer your input : wrong_input + >>> simpson_integration('wrong_input',2,3,4) + Traceback (most recent call last): + ... + AssertionError: the function(object) passed should be callable your input : ... + + >>> simpson_integration(lambda x : x*x,3.45,3.2,1) + -2.8 + + >>> simpson_integration(lambda x : x*x,3.45,3.2,0) + Traceback (most recent call last): + ... + AssertionError: precision should be positive integer your input : 0 + + >>> simpson_integration(lambda x : x*x,3.45,3.2,-1) + Traceback (most recent call last): + ... + AssertionError: precision should be positive integer your input : -1 + + """ + assert callable( + function + ), f"the function(object) passed should be callable your input : {function}" + assert isinstance(a, float) or isinstance( + a, int + ), f"a should be float or integer your input : {a}" + assert isinstance(function(a), float) or isinstance(function(a), int), ( + "the function should return integer or float return type of your function, " + f"{type(a)}" + ) + assert isinstance(b, float) or isinstance( + b, int + ), f"b should be float or integer your input : {b}" + assert ( + isinstance(precision, int) and precision > 0 + ), f"precision should be positive integer your input : {precision}" + + # just applying the formula of simpson for approximate integraion written in + # mentioned article in first comment of this file and above this function + + h = (b - a) / N_STEPS + result = function(a) + function(b) + + for i in range(1, N_STEPS): + a1 = a + h * i + result += function(a1) * (4 if i % 2 else 2) + + result *= h / 3 + return round(result, precision) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/largest_subarray_sum.py b/other/largest_subarray_sum.py new file mode 100644 index 000000000000..0449e72e64e3 --- /dev/null +++ b/other/largest_subarray_sum.py @@ -0,0 +1,23 @@ +from sys import maxsize + + +def max_sub_array_sum(a: list, size: int = 0): + """ + >>> max_sub_array_sum([-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]) + -3 + """ + size = size or len(a) + max_so_far = -maxsize - 1 + max_ending_here = 0 + for i in range(0, size): + max_ending_here = max_ending_here + a[i] + if max_so_far < max_ending_here: + max_so_far = max_ending_here + if max_ending_here < 0: + max_ending_here = 0 + return max_so_far + + +if __name__ == "__main__": + a = [-13, -3, -25, -20, 1, -16, -23, -12, -5, -22, -15, -4, -7] + print(("Maximum contiguous sum is", max_sub_array_sum(a, len(a)))) diff --git a/other/least_recently_used.py b/other/least_recently_used.py new file mode 100644 index 000000000000..213339636469 --- /dev/null +++ b/other/least_recently_used.py @@ -0,0 +1,64 @@ +import sys +from abc import abstractmethod +from collections import deque + + +class LRUCache: + """ Page Replacement Algorithm, Least Recently Used (LRU) Caching.""" + + dq_store = object() # Cache store of keys + key_reference_map = object() # References of the keys in cache + _MAX_CAPACITY: int = 10 # Maximum capacity of cache + + @abstractmethod + def __init__(self, n: int): + """Creates an empty store and map for the keys. + The LRUCache is set the size n. + """ + self.dq_store = deque() + self.key_reference_map = set() + if not n: + LRUCache._MAX_CAPACITY = sys.maxsize + elif n < 0: + raise ValueError("n should be an integer greater than 0.") + else: + LRUCache._MAX_CAPACITY = n + + def refer(self, x): + """ + Looks for a page in the cache store and adds reference to the set. + Remove the least recently used key if the store is full. + Update store to reflect recent access. + """ + if x not in self.key_reference_map: + if len(self.dq_store) == LRUCache._MAX_CAPACITY: + last_element = self.dq_store.pop() + self.key_reference_map.remove(last_element) + else: + index_remove = 0 + for idx, key in enumerate(self.dq_store): + if key == x: + index_remove = idx + break + self.dq_store.remove(index_remove) + + self.dq_store.appendleft(x) + self.key_reference_map.add(x) + + def display(self): + """ + Prints all the elements in the store. + """ + for k in self.dq_store: + print(k) + + +if __name__ == "__main__": + lru_cache = LRUCache(4) + lru_cache.refer(1) + lru_cache.refer(2) + lru_cache.refer(3) + lru_cache.refer(1) + lru_cache.refer(4) + lru_cache.refer(5) + lru_cache.display() diff --git a/other/lfu_cache.py b/other/lfu_cache.py new file mode 100644 index 000000000000..40268242f564 --- /dev/null +++ b/other/lfu_cache.py @@ -0,0 +1,187 @@ +from typing import Callable, Optional + + +class DoubleLinkedListNode: + """ + Double Linked List Node built specifically for LFU Cache + """ + + def __init__(self, key: int, val: int): + self.key = key + self.val = val + self.freq = 0 + self.next = None + self.prev = None + + +class DoubleLinkedList: + """ + Double Linked List built specifically for LFU Cache + """ + + def __init__(self): + self.head = DoubleLinkedListNode(None, None) + self.rear = DoubleLinkedListNode(None, None) + self.head.next, self.rear.prev = self.rear, self.head + + def add(self, node: DoubleLinkedListNode) -> None: + """ + Adds the given node at the head of the list and shifting it to proper position + """ + + temp = self.rear.prev + + self.rear.prev, node.next = node, self.rear + temp.next, node.prev = node, temp + node.freq += 1 + self._position_node(node) + + def _position_node(self, node: DoubleLinkedListNode) -> None: + while node.prev.key and node.prev.freq > node.freq: + node1, node2 = node, node.prev + node1.prev, node2.next = node2.prev, node1.prev + node1.next, node2.prev = node2, node1 + + def remove(self, node: DoubleLinkedListNode) -> DoubleLinkedListNode: + """ + Removes and returns the given node from the list + """ + + temp_last, temp_next = node.prev, node.next + node.prev, node.next = None, None + temp_last.next, temp_next.prev = temp_next, temp_last + return node + + +class LFUCache: + """ + LFU Cache to store a given capacity of data. Can be used as a stand-alone object + or as a function decorator. + + >>> cache = LFUCache(2) + >>> cache.set(1, 1) + >>> cache.set(2, 2) + >>> cache.get(1) + 1 + >>> cache.set(3, 3) + >>> cache.get(2) # None is returned + >>> cache.set(4, 4) + >>> cache.get(1) # None is returned + >>> cache.get(3) + 3 + >>> cache.get(4) + 4 + >>> cache + CacheInfo(hits=3, misses=2, capacity=2, current_size=2) + >>> @LFUCache.decorator(100) + ... def fib(num): + ... if num in (1, 2): + ... return 1 + ... return fib(num - 1) + fib(num - 2) + + >>> for i in range(1, 101): + ... res = fib(i) + + >>> fib.cache_info() + CacheInfo(hits=196, misses=100, capacity=100, current_size=100) + """ + + # class variable to map the decorator functions to their respective instance + decorator_function_to_instance_map = {} + + def __init__(self, capacity: int): + self.list = DoubleLinkedList() + self.capacity = capacity + self.num_keys = 0 + self.hits = 0 + self.miss = 0 + self.cache = {} + + def __repr__(self) -> str: + """ + Return the details for the cache instance + [hits, misses, capacity, current_size] + """ + + return ( + f"CacheInfo(hits={self.hits}, misses={self.miss}, " + f"capacity={self.capacity}, current_size={self.num_keys})" + ) + + def __contains__(self, key: int) -> bool: + """ + >>> cache = LFUCache(1) + >>> 1 in cache + False + >>> cache.set(1, 1) + >>> 1 in cache + True + """ + return key in self.cache + + def get(self, key: int) -> Optional[int]: + """ + Returns the value for the input key and updates the Double Linked List. Returns + None if key is not present in cache + """ + + if key in self.cache: + self.hits += 1 + self.list.add(self.list.remove(self.cache[key])) + return self.cache[key].val + self.miss += 1 + return None + + def set(self, key: int, value: int) -> None: + """ + Sets the value for the input key and updates the Double Linked List + """ + + if key not in self.cache: + if self.num_keys >= self.capacity: + key_to_delete = self.list.head.next.key + self.list.remove(self.cache[key_to_delete]) + del self.cache[key_to_delete] + self.num_keys -= 1 + self.cache[key] = DoubleLinkedListNode(key, value) + self.list.add(self.cache[key]) + self.num_keys += 1 + + else: + node = self.list.remove(self.cache[key]) + node.val = value + self.list.add(node) + + @staticmethod + def decorator(size: int = 128): + """ + Decorator version of LFU Cache + """ + + def cache_decorator_inner(func: Callable): + def cache_decorator_wrapper(*args, **kwargs): + if func not in LFUCache.decorator_function_to_instance_map: + LFUCache.decorator_function_to_instance_map[func] = LFUCache(size) + + result = LFUCache.decorator_function_to_instance_map[func].get(args[0]) + if result is None: + result = func(*args, **kwargs) + LFUCache.decorator_function_to_instance_map[func].set( + args[0], result + ) + return result + + def cache_info(): + return LFUCache.decorator_function_to_instance_map[func] + + cache_decorator_wrapper.cache_info = cache_info + + return cache_decorator_wrapper + + return cache_decorator_inner + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/linear_congruential_generator.py b/other/linear_congruential_generator.py index 34abdf34eaf3..f8b604b8562d 100644 --- a/other/linear_congruential_generator.py +++ b/other/linear_congruential_generator.py @@ -1,35 +1,37 @@ -from __future__ import print_function __author__ = "Tobias Carryer" from time import time -class LinearCongruentialGenerator(object): + +class LinearCongruentialGenerator: """ A pseudorandom number generator. """ - - def __init__( self, multiplier, increment, modulo, seed=int(time()) ): + + def __init__(self, multiplier, increment, modulo, seed=int(time())): """ These parameters are saved and used when nextNumber() is called. - + modulo is the largest number that can be generated (exclusive). The most - efficent values are powers of 2. 2^32 is a common value. + efficient values are powers of 2. 2^32 is a common value. """ self.multiplier = multiplier self.increment = increment self.modulo = modulo self.seed = seed - - def next_number( self ): + + def next_number(self): """ The smallest number that can be generated is zero. - The largest number that can be generated is modulo-1. modulo is set in the constructor. + The largest number that can be generated is modulo-1. modulo is set in the + constructor. """ self.seed = (self.multiplier * self.seed + self.increment) % self.modulo return self.seed + if __name__ == "__main__": # Show the LCG in action. - lcg = LinearCongruentialGenerator(1664525, 1013904223, 2<<31) - while True : - print(lcg.next_number()) \ No newline at end of file + lcg = LinearCongruentialGenerator(1664525, 1013904223, 2 << 31) + while True: + print(lcg.next_number()) diff --git a/other/lru_cache.py b/other/lru_cache.py new file mode 100644 index 000000000000..2a9d7e49b279 --- /dev/null +++ b/other/lru_cache.py @@ -0,0 +1,193 @@ +from typing import Callable, Optional + + +class DoubleLinkedListNode: + """ + Double Linked List Node built specifically for LRU Cache + """ + + def __init__(self, key: int, val: int): + self.key = key + self.val = val + self.next = None + self.prev = None + + +class DoubleLinkedList: + """ + Double Linked List built specifically for LRU Cache + """ + + def __init__(self): + self.head = DoubleLinkedListNode(None, None) + self.rear = DoubleLinkedListNode(None, None) + self.head.next, self.rear.prev = self.rear, self.head + + def add(self, node: DoubleLinkedListNode) -> None: + """ + Adds the given node to the end of the list (before rear) + """ + + temp = self.rear.prev + temp.next, node.prev = node, temp + self.rear.prev, node.next = node, self.rear + + def remove(self, node: DoubleLinkedListNode) -> DoubleLinkedListNode: + """ + Removes and returns the given node from the list + """ + + temp_last, temp_next = node.prev, node.next + node.prev, node.next = None, None + temp_last.next, temp_next.prev = temp_next, temp_last + + return node + + +class LRUCache: + """ + LRU Cache to store a given capacity of data. Can be used as a stand-alone object + or as a function decorator. + + >>> cache = LRUCache(2) + + >>> cache.set(1, 1) + + >>> cache.set(2, 2) + + >>> cache.get(1) + 1 + + >>> cache.set(3, 3) + + >>> cache.get(2) # None returned + + >>> cache.set(4, 4) + + >>> cache.get(1) # None returned + + >>> cache.get(3) + 3 + + >>> cache.get(4) + 4 + + >>> cache + CacheInfo(hits=3, misses=2, capacity=2, current size=2) + + >>> @LRUCache.decorator(100) + ... def fib(num): + ... if num in (1, 2): + ... return 1 + ... return fib(num - 1) + fib(num - 2) + + >>> for i in range(1, 100): + ... res = fib(i) + + >>> fib.cache_info() + CacheInfo(hits=194, misses=99, capacity=100, current size=99) + """ + + # class variable to map the decorator functions to their respective instance + decorator_function_to_instance_map = {} + + def __init__(self, capacity: int): + self.list = DoubleLinkedList() + self.capacity = capacity + self.num_keys = 0 + self.hits = 0 + self.miss = 0 + self.cache = {} + + def __repr__(self) -> str: + """ + Return the details for the cache instance + [hits, misses, capacity, current_size] + """ + + return ( + f"CacheInfo(hits={self.hits}, misses={self.miss}, " + f"capacity={self.capacity}, current size={self.num_keys})" + ) + + def __contains__(self, key: int) -> bool: + """ + >>> cache = LRUCache(1) + + >>> 1 in cache + False + + >>> cache.set(1, 1) + + >>> 1 in cache + True + """ + + return key in self.cache + + def get(self, key: int) -> Optional[int]: + """ + Returns the value for the input key and updates the Double Linked List. Returns + None if key is not present in cache + """ + + if key in self.cache: + self.hits += 1 + self.list.add(self.list.remove(self.cache[key])) + return self.cache[key].val + self.miss += 1 + return None + + def set(self, key: int, value: int) -> None: + """ + Sets the value for the input key and updates the Double Linked List + """ + + if key not in self.cache: + if self.num_keys >= self.capacity: + key_to_delete = self.list.head.next.key + self.list.remove(self.cache[key_to_delete]) + del self.cache[key_to_delete] + self.num_keys -= 1 + self.cache[key] = DoubleLinkedListNode(key, value) + self.list.add(self.cache[key]) + self.num_keys += 1 + + else: + node = self.list.remove(self.cache[key]) + node.val = value + self.list.add(node) + + @staticmethod + def decorator(size: int = 128): + """ + Decorator version of LRU Cache + """ + + def cache_decorator_inner(func: Callable): + def cache_decorator_wrapper(*args, **kwargs): + if func not in LRUCache.decorator_function_to_instance_map: + LRUCache.decorator_function_to_instance_map[func] = LRUCache(size) + + result = LRUCache.decorator_function_to_instance_map[func].get(args[0]) + if result is None: + result = func(*args, **kwargs) + LRUCache.decorator_function_to_instance_map[func].set( + args[0], result + ) + return result + + def cache_info(): + return LRUCache.decorator_function_to_instance_map[func] + + cache_decorator_wrapper.cache_info = cache_info + + return cache_decorator_wrapper + + return cache_decorator_inner + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/magicdiamondpattern.py b/other/magicdiamondpattern.py new file mode 100644 index 000000000000..71bc50b51fc2 --- /dev/null +++ b/other/magicdiamondpattern.py @@ -0,0 +1,55 @@ +# Python program for generating diamond pattern in Python 3.7+ + + +# Function to print upper half of diamond (pyramid) +def floyd(n): + """ + Parameters: + n : size of pattern + """ + for i in range(0, n): + for j in range(0, n - i - 1): # printing spaces + print(" ", end="") + for k in range(0, i + 1): # printing stars + print("* ", end="") + print() + + +# Function to print lower half of diamond (pyramid) +def reverse_floyd(n): + """ + Parameters: + n : size of pattern + """ + for i in range(n, 0, -1): + for j in range(i, 0, -1): # printing stars + print("* ", end="") + print() + for k in range(n - i + 1, 0, -1): # printing spaces + print(" ", end="") + + +# Function to print complete diamond pattern of "*" +def pretty_print(n): + """ + Parameters: + n : size of pattern + """ + if n <= 0: + print(" ... .... nothing printing :(") + return + floyd(n) # upper half + reverse_floyd(n) # lower half + + +if __name__ == "__main__": + print(r"| /\ | |- | |- |--| |\ /| |-") + print(r"|/ \| |- |_ |_ |__| | \/ | |_") + K = 1 + while K: + user_number = int(input("enter the number and , and see the magic : ")) + print() + pretty_print(user_number) + K = int(input("press 0 to exit... and 1 to continue...")) + + print("Good Bye...") diff --git a/other/markov_chain.py b/other/markov_chain.py new file mode 100644 index 000000000000..b93c408cd288 --- /dev/null +++ b/other/markov_chain.py @@ -0,0 +1,83 @@ +from __future__ import annotations + +from collections import Counter +from random import random + + +class MarkovChainGraphUndirectedUnweighted: + """ + Undirected Unweighted Graph for running Markov Chain Algorithm + """ + + def __init__(self): + self.connections = {} + + def add_node(self, node: str) -> None: + self.connections[node] = {} + + def add_transition_probability( + self, node1: str, node2: str, probability: float + ) -> None: + if node1 not in self.connections: + self.add_node(node1) + if node2 not in self.connections: + self.add_node(node2) + self.connections[node1][node2] = probability + + def get_nodes(self) -> list[str]: + return list(self.connections) + + def transition(self, node: str) -> str: + current_probability = 0 + random_value = random() + + for dest in self.connections[node]: + current_probability += self.connections[node][dest] + if current_probability > random_value: + return dest + + +def get_transitions( + start: str, transitions: list[tuple[str, str, float]], steps: int +) -> dict[str, int]: + """ + Running Markov Chain algorithm and calculating the number of times each node is + visited + + >>> transitions = [ + ... ('a', 'a', 0.9), + ... ('a', 'b', 0.075), + ... ('a', 'c', 0.025), + ... ('b', 'a', 0.15), + ... ('b', 'b', 0.8), + ... ('b', 'c', 0.05), + ... ('c', 'a', 0.25), + ... ('c', 'b', 0.25), + ... ('c', 'c', 0.5) + ... ] + + >>> result = get_transitions('a', transitions, 5000) + + >>> result['a'] > result['b'] > result['c'] + True + """ + + graph = MarkovChainGraphUndirectedUnweighted() + + for node1, node2, probability in transitions: + graph.add_transition_probability(node1, node2, probability) + + visited = Counter(graph.get_nodes()) + node = start + + for _ in range(steps): + node = graph.transition(node) + visited[node] += 1 + + return visited + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/max_sum_sliding_window.py b/other/max_sum_sliding_window.py new file mode 100644 index 000000000000..4be7d786f215 --- /dev/null +++ b/other/max_sum_sliding_window.py @@ -0,0 +1,45 @@ +""" +Given an array of integer elements and an integer 'k', we are required to find the +maximum sum of 'k' consecutive elements in the array. + +Instead of using a nested for loop, in a Brute force approach we will use a technique +called 'Window sliding technique' where the nested loops can be converted to a single +loop to reduce time complexity. +""" +from typing import List + + +def max_sum_in_array(array: List[int], k: int) -> int: + """ + Returns the maximum sum of k consecutive elements + >>> arr = [1, 4, 2, 10, 2, 3, 1, 0, 20] + >>> k = 4 + >>> max_sum_in_array(arr, k) + 24 + >>> k = 10 + >>> max_sum_in_array(arr,k) + Traceback (most recent call last): + ... + ValueError: Invalid Input + >>> arr = [1, 4, 2, 10, 2, 13, 1, 0, 2] + >>> k = 4 + >>> max_sum_in_array(arr, k) + 27 + """ + if len(array) < k or k < 0: + raise ValueError("Invalid Input") + max_sum = current_sum = sum(array[:k]) + for i in range(len(array) - k): + current_sum = current_sum - array[i] + array[i + k] + max_sum = max(max_sum, current_sum) + return max_sum + + +if __name__ == "__main__": + from doctest import testmod + from random import randint + + testmod() + array = [randint(-1000, 1000) for i in range(100)] + k = randint(0, 110) + print(f"The maximum sum of {k} consecutive elements is {max_sum_in_array(array,k)}") diff --git a/other/median_of_two_arrays.py b/other/median_of_two_arrays.py new file mode 100644 index 000000000000..cde12f5d7e3b --- /dev/null +++ b/other/median_of_two_arrays.py @@ -0,0 +1,33 @@ +from typing import List + + +def median_of_two_arrays(nums1: List[float], nums2: List[float]) -> float: + """ + >>> median_of_two_arrays([1, 2], [3]) + 2 + >>> median_of_two_arrays([0, -1.1], [2.5, 1]) + 0.5 + >>> median_of_two_arrays([], [2.5, 1]) + 1.75 + >>> median_of_two_arrays([], [0]) + 0 + >>> median_of_two_arrays([], []) + Traceback (most recent call last): + ... + IndexError: list index out of range + """ + all_numbers = sorted(nums1 + nums2) + div, mod = divmod(len(all_numbers), 2) + if mod == 1: + return all_numbers[div] + else: + return (all_numbers[div] + all_numbers[div - 1]) / 2 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + array_1 = [float(x) for x in input("Enter the elements of first array: ").split()] + array_2 = [float(x) for x in input("Enter the elements of second array: ").split()] + print(f"The median of two arrays is: {median_of_two_arrays(array_1, array_2)}") diff --git a/other/nested_brackets.py b/other/nested_brackets.py index 76677d56439a..99e2f3a38797 100644 --- a/other/nested_brackets.py +++ b/other/nested_brackets.py @@ -1,27 +1,25 @@ -''' +""" The nested brackets problem is a problem that determines if a sequence of brackets are properly nested. A sequence of brackets s is considered properly nested if any of the following conditions are true: - - s is empty - - s has the form (U) or [U] or {U} where U is a properly nested string - - s has the form VW where V and W are properly nested strings + - s is empty + - s has the form (U) or [U] or {U} where U is a properly nested string + - s has the form VW where V and W are properly nested strings For example, the string "()()[()]" is properly nested but "[(()]" is not. -The function called is_balanced takes as input a string S which is a sequence of brackets and -returns true if S is nested and false otherwise. - -''' -from __future__ import print_function +The function called is_balanced takes as input a string S which is a sequence of +brackets and returns true if S is nested and false otherwise. +""" def is_balanced(S): stack = [] - open_brackets = set({'(', '[', '{'}) - closed_brackets = set({')', ']', '}'}) - open_to_closed = dict({'{':'}', '[':']', '(':')'}) + open_brackets = set({"(", "[", "{"}) + closed_brackets = set({")", "]", "}"}) + open_to_closed = dict({"{": "}", "[": "]", "(": ")"}) for i in range(len(S)): @@ -29,21 +27,20 @@ def is_balanced(S): stack.append(S[i]) elif S[i] in closed_brackets: - if len(stack) == 0 or (len(stack) > 0 and open_to_closed[stack.pop()] != S[i]): + if len(stack) == 0 or ( + len(stack) > 0 and open_to_closed[stack.pop()] != S[i] + ): return False return len(stack) == 0 def main(): - - S = input("Enter sequence of brackets: ") - - if is_balanced(S): - print((S, "is balanced")) - + s = input("Enter sequence of brackets: ") + if is_balanced(s): + print(s, "is balanced") else: - print((S, "is not balanced")) + print(s, "is not balanced") if __name__ == "__main__": diff --git a/other/palindrome.py b/other/palindrome.py index 990ec844f9fb..dd1fe316f479 100644 --- a/other/palindrome.py +++ b/other/palindrome.py @@ -1,9 +1,31 @@ -# Program to find whether given string is palindrome or not -def is_palindrome(str): +# Algorithms to determine if a string is palindrome + +test_data = { + "MALAYALAM": True, + "String": False, + "rotor": True, + "level": True, + "A": True, + "BB": True, + "ABC": False, + "amanaplanacanalpanama": True, # "a man a plan a canal panama" +} +# Ensure our test data is valid +assert all((key == key[::-1]) is value for key, value in test_data.items()) + + +def is_palindrome(s: str) -> bool: + """ + Return True if s is a palindrome otherwise return False. + + >>> all(is_palindrome(key) is value for key, value in test_data.items()) + True + """ + start_i = 0 - end_i = len(str) - 1 + end_i = len(s) - 1 while start_i < end_i: - if str[start_i] == str[end_i]: + if s[start_i] == s[end_i]: start_i += 1 end_i -= 1 else: @@ -11,21 +33,34 @@ def is_palindrome(str): return True -# Recursive method -def recursive_palindrome(str): - if len(str) <= 1: +def is_palindrome_recursive(s: str) -> bool: + """ + Return True if s is a palindrome otherwise return False. + + >>> all(is_palindrome_recursive(key) is value for key, value in test_data.items()) + True + """ + if len(s) <= 1: return True - if str[0] == str[len(str) - 1]: - return recursive_palindrome(str[1:-1]) + if s[0] == s[len(s) - 1]: + return is_palindrome_recursive(s[1:-1]) else: return False -def main(): - str = 'ama' - print(recursive_palindrome(str.lower())) - print(is_palindrome(str.lower())) +def is_palindrome_slice(s: str) -> bool: + """ + Return True if s is a palindrome otherwise return False. + + >>> all(is_palindrome_slice(key) is value for key, value in test_data.items()) + True + """ + return s == s[::-1] -if __name__ == '__main__': - main() +if __name__ == "__main__": + for key, value in test_data.items(): + assert is_palindrome(key) is is_palindrome_recursive(key) + assert is_palindrome(key) is is_palindrome_slice(key) + print(f"{key:21} {value}") + print("a man a plan a canal panama") diff --git a/other/password_generator.py b/other/password_generator.py index 8916079fc758..35e11e4dfb78 100644 --- a/other/password_generator.py +++ b/other/password_generator.py @@ -1,35 +1,77 @@ -from __future__ import print_function -import string -import random +"""Password generator allows you to generate a random password of length N.""" +from random import choice, shuffle +from string import ascii_letters, digits, punctuation -letters = [letter for letter in string.ascii_letters] -digits = [digit for digit in string.digits] -symbols = [symbol for symbol in string.punctuation] -chars = letters + digits + symbols -random.shuffle(chars) -min_length = 8 -max_length = 16 -password = ''.join(random.choice(chars) for x in range(random.randint(min_length, max_length))) -print('Password: ' + password) -print('[ If you are thinking of using this passsword, You better save it. ]') +def password_generator(length=8): + """ + >>> len(password_generator()) + 8 + >>> len(password_generator(length=16)) + 16 + >>> len(password_generator(257)) + 257 + >>> len(password_generator(length=0)) + 0 + >>> len(password_generator(-1)) + 0 + """ + chars = tuple(ascii_letters) + tuple(digits) + tuple(punctuation) + return "".join(choice(chars) for x in range(length)) -# ALTERNATIVE METHODS +# ALTERNATIVE METHODS # ctbi= characters that must be in password -# i= how many letters or characters the password length will be -def password_generator(ctbi, i): - # Password generator = full boot with random_number, random_letters, and random_character FUNCTIONS - pass # Put your code here... +# i= how many letters or characters the password length will be +def alternative_password_generator(ctbi, i): + # Password generator = full boot with random_number, random_letters, and + # random_character FUNCTIONS + # Put your code here... + i = i - len(ctbi) + quotient = int(i / 3) + remainder = i % 3 + # chars = ctbi + random_letters(ascii_letters, i / 3 + remainder) + + # random_number(digits, i / 3) + random_characters(punctuation, i / 3) + chars = ( + ctbi + + random(ascii_letters, quotient + remainder) + + random(digits, quotient) + + random(punctuation, quotient) + ) + chars = list(chars) + shuffle(chars) + return "".join(chars) + + # random is a generalised function for letters, characters and numbers + + +def random(ctbi, i): + return "".join(choice(ctbi) for x in range(i)) def random_number(ctbi, i): - pass # Put your code here... + pass # Put your code here... def random_letters(ctbi, i): - pass # Put your code here... + pass # Put your code here... def random_characters(ctbi, i): - pass # Put your code here... + pass # Put your code here... + + +def main(): + length = int(input("Please indicate the max length of your password: ").strip()) + ctbi = input( + "Please indicate the characters that must be in your password: " + ).strip() + print("Password generated:", password_generator(length)) + print( + "Alternative Password generated:", alternative_password_generator(ctbi, length) + ) + print("[If you are thinking of using this passsword, You better save it.]") + + +if __name__ == "__main__": + main() diff --git a/other/primelib.py b/other/primelib.py index 19572f8611cb..37883d9cf591 100644 --- a/other/primelib.py +++ b/other/primelib.py @@ -1,10 +1,9 @@ -# -*- coding: utf-8 -*- """ Created on Thu Oct 5 16:44:23 2017 @author: Christian Bender -This python library contains some useful functions to deal with +This Python library contains some useful functions to deal with prime numbers and whole numbers. Overview: @@ -16,7 +15,7 @@ greatestPrimeFactor(number) smallestPrimeFactor(number) getPrime(n) -getPrimesBetween(pNumber1, pNumber2) +getPrimesBetween(pNumber1, pNumber2) ---- @@ -39,479 +38,507 @@ """ +from math import sqrt + + def isPrime(number): """ - input: positive integer 'number' - returns true if 'number' is prime otherwise false. + input: positive integer 'number' + returns true if 'number' is prime otherwise false. """ - import math # for function sqrt - + # precondition - assert isinstance(number,int) and (number >= 0) , \ - "'number' must been an int and positive" - + assert isinstance(number, int) and ( + number >= 0 + ), "'number' must been an int and positive" + status = True - - # 0 and 1 are none primes. + + # 0 and 1 are none primes. if number <= 1: status = False - - for divisor in range(2,int(round(math.sqrt(number)))+1): - + + for divisor in range(2, int(round(sqrt(number))) + 1): + # if 'number' divisible by 'divisor' then sets 'status' - # of false and break up the loop. + # of false and break up the loop. if number % divisor == 0: status = False break - + # precondition - assert isinstance(status,bool), "'status' must been from type bool" - + assert isinstance(status, bool), "'status' must been from type bool" + return status + # ------------------------------------------ + def sieveEr(N): """ - input: positive integer 'N' > 2 - returns a list of prime numbers from 2 up to N. - - This function implements the algorithm called - sieve of erathostenes. - + input: positive integer 'N' > 2 + returns a list of prime numbers from 2 up to N. + + This function implements the algorithm called + sieve of erathostenes. + """ - + # precondition - assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2" - - # beginList: conatins all natural numbers from 2 upt to N - beginList = [x for x in range(2,N+1)] + assert isinstance(N, int) and (N > 2), "'N' must been an int and > 2" + + # beginList: contains all natural numbers from 2 up to N + beginList = [x for x in range(2, N + 1)] + + ans = [] # this list will be returns. - ans = [] # this list will be returns. - # actual sieve of erathostenes for i in range(len(beginList)): - - for j in range(i+1,len(beginList)): - - if (beginList[i] != 0) and \ - (beginList[j] % beginList[i] == 0): + + for j in range(i + 1, len(beginList)): + + if (beginList[i] != 0) and (beginList[j] % beginList[i] == 0): beginList[j] = 0 - - # filters actual prime numbers. + + # filters actual prime numbers. ans = [x for x in beginList if x != 0] - + # precondition - assert isinstance(ans,list), "'ans' must been from type list" - + assert isinstance(ans, list), "'ans' must been from type list" + return ans - + # -------------------------------- + def getPrimeNumbers(N): """ - input: positive integer 'N' > 2 - returns a list of prime numbers from 2 up to N (inclusive) - This function is more efficient as function 'sieveEr(...)' - """ - - # precondition - assert isinstance(N,int) and (N > 2), "'N' must been an int and > 2" - - ans = [] - - # iterates over all numbers between 2 up to N+1 + input: positive integer 'N' > 2 + returns a list of prime numbers from 2 up to N (inclusive) + This function is more efficient as function 'sieveEr(...)' + """ + + # precondition + assert isinstance(N, int) and (N > 2), "'N' must been an int and > 2" + + ans = [] + + # iterates over all numbers between 2 up to N+1 # if a number is prime then appends to list 'ans' - for number in range(2,N+1): - + for number in range(2, N + 1): + if isPrime(number): - + ans.append(number) - + # precondition - assert isinstance(ans,list), "'ans' must been from type list" - + assert isinstance(ans, list), "'ans' must been from type list" + return ans # ----------------------------------------- - + + def primeFactorization(number): """ - input: positive integer 'number' - returns a list of the prime number factors of 'number' + input: positive integer 'number' + returns a list of the prime number factors of 'number' """ - import math # for function sqrt - # precondition - assert isinstance(number,int) and number >= 0, \ - "'number' must been an int and >= 0" - - ans = [] # this list will be returns of the function. + assert isinstance(number, int) and number >= 0, "'number' must been an int and >= 0" + + ans = [] # this list will be returns of the function. # potential prime number factors. - factor = 2 + factor = 2 quotient = number - - + if number == 0 or number == 1: - + ans.append(number) - - # if 'number' not prime then builds the prime factorization of 'number' + + # if 'number' not prime then builds the prime factorization of 'number' elif not isPrime(number): - - while (quotient != 1): - + + while quotient != 1: + if isPrime(factor) and (quotient % factor == 0): - ans.append(factor) - quotient /= factor + ans.append(factor) + quotient /= factor else: - factor += 1 - + factor += 1 + else: ans.append(number) - + # precondition - assert isinstance(ans,list), "'ans' must been from type list" - + assert isinstance(ans, list), "'ans' must been from type list" + return ans - + # ----------------------------------------- - + + def greatestPrimeFactor(number): """ - input: positive integer 'number' >= 0 - returns the greatest prime number factor of 'number' + input: positive integer 'number' >= 0 + returns the greatest prime number factor of 'number' """ - + # precondition - assert isinstance(number,int) and (number >= 0), \ - "'number' bust been an int and >= 0" - - ans = 0 - + assert isinstance(number, int) and ( + number >= 0 + ), "'number' bust been an int and >= 0" + + ans = 0 + # prime factorization of 'number' primeFactors = primeFactorization(number) - ans = max(primeFactors) - + ans = max(primeFactors) + # precondition - assert isinstance(ans,int), "'ans' must been from type int" - + assert isinstance(ans, int), "'ans' must been from type int" + return ans - + # ---------------------------------------------- - - + + def smallestPrimeFactor(number): """ - input: integer 'number' >= 0 - returns the smallest prime number factor of 'number' + input: integer 'number' >= 0 + returns the smallest prime number factor of 'number' """ - + # precondition - assert isinstance(number,int) and (number >= 0), \ - "'number' bust been an int and >= 0" - - ans = 0 - + assert isinstance(number, int) and ( + number >= 0 + ), "'number' bust been an int and >= 0" + + ans = 0 + # prime factorization of 'number' primeFactors = primeFactorization(number) - + ans = min(primeFactors) # precondition - assert isinstance(ans,int), "'ans' must been from type int" - + assert isinstance(ans, int), "'ans' must been from type int" + return ans - - + + # ---------------------- - + + def isEven(number): """ - input: integer 'number' - returns true if 'number' is even, otherwise false. - """ + input: integer 'number' + returns true if 'number' is even, otherwise false. + """ # precondition - assert isinstance(number, int), "'number' must been an int" + assert isinstance(number, int), "'number' must been an int" assert isinstance(number % 2 == 0, bool), "compare bust been from type bool" - + return number % 2 == 0 - + + # ------------------------ - + + def isOdd(number): """ - input: integer 'number' - returns true if 'number' is odd, otherwise false. - """ + input: integer 'number' + returns true if 'number' is odd, otherwise false. + """ # precondition - assert isinstance(number, int), "'number' must been an int" + assert isinstance(number, int), "'number' must been an int" assert isinstance(number % 2 != 0, bool), "compare bust been from type bool" - + return number % 2 != 0 - + + # ------------------------ - - + + def goldbach(number): """ - Goldbach's assumption - input: a even positive integer 'number' > 2 - returns a list of two prime numbers whose sum is equal to 'number' + Goldbach's assumption + input: a even positive integer 'number' > 2 + returns a list of two prime numbers whose sum is equal to 'number' """ - + # precondition - assert isinstance(number,int) and (number > 2) and isEven(number), \ - "'number' must been an int, even and > 2" - - ans = [] # this list will returned - + assert ( + isinstance(number, int) and (number > 2) and isEven(number) + ), "'number' must been an int, even and > 2" + + ans = [] # this list will returned + # creates a list of prime numbers between 2 up to 'number' primeNumbers = getPrimeNumbers(number) - lenPN = len(primeNumbers) + lenPN = len(primeNumbers) # run variable for while-loops. i = 0 - j = 1 - + j = None + # exit variable. for break up the loops loop = True - - while (i < lenPN and loop): - - j = i+1 - - - while (j < lenPN and loop): - + + while i < lenPN and loop: + + j = i + 1 + + while j < lenPN and loop: + if primeNumbers[i] + primeNumbers[j] == number: loop = False ans.append(primeNumbers[i]) ans.append(primeNumbers[j]) - + j += 1 i += 1 - + # precondition - assert isinstance(ans,list) and (len(ans) == 2) and \ - (ans[0] + ans[1] == number) and isPrime(ans[0]) and isPrime(ans[1]), \ - "'ans' must contains two primes. And sum of elements must been eq 'number'" - + assert ( + isinstance(ans, list) + and (len(ans) == 2) + and (ans[0] + ans[1] == number) + and isPrime(ans[0]) + and isPrime(ans[1]) + ), "'ans' must contains two primes. And sum of elements must been eq 'number'" + return ans - + + # ---------------------------------------------- -def gcd(number1,number2): + +def gcd(number1, number2): """ - Greatest common divisor - input: two positive integer 'number1' and 'number2' - returns the greatest common divisor of 'number1' and 'number2' + Greatest common divisor + input: two positive integer 'number1' and 'number2' + returns the greatest common divisor of 'number1' and 'number2' """ - + # precondition - assert isinstance(number1,int) and isinstance(number2,int) \ - and (number1 >= 0) and (number2 >= 0), \ - "'number1' and 'number2' must been positive integer." + assert ( + isinstance(number1, int) + and isinstance(number2, int) + and (number1 >= 0) + and (number2 >= 0) + ), "'number1' and 'number2' must been positive integer." + + rest = 0 - rest = 0 - while number2 != 0: - + rest = number1 % number2 number1 = number2 number2 = rest # precondition - assert isinstance(number1,int) and (number1 >= 0), \ - "'number' must been from type int and positive" - + assert isinstance(number1, int) and ( + number1 >= 0 + ), "'number' must been from type int and positive" + return number1 - + + # ---------------------------------------------------- - + + def kgV(number1, number2): """ - Least common multiple - input: two positive integer 'number1' and 'number2' - returns the least common multiple of 'number1' and 'number2' + Least common multiple + input: two positive integer 'number1' and 'number2' + returns the least common multiple of 'number1' and 'number2' """ - + # precondition - assert isinstance(number1,int) and isinstance(number2,int) \ - and (number1 >= 1) and (number2 >= 1), \ - "'number1' and 'number2' must been positive integer." - - ans = 1 # actual answer that will be return. - + assert ( + isinstance(number1, int) + and isinstance(number2, int) + and (number1 >= 1) + and (number2 >= 1) + ), "'number1' and 'number2' must been positive integer." + + ans = 1 # actual answer that will be return. + # for kgV (x,1) if number1 > 1 and number2 > 1: - + # builds the prime factorization of 'number1' and 'number2' primeFac1 = primeFactorization(number1) primeFac2 = primeFactorization(number2) - + elif number1 == 1 or number2 == 1: - + primeFac1 = [] primeFac2 = [] - ans = max(number1,number2) - + ans = max(number1, number2) + count1 = 0 count2 = 0 - - done = [] # captured numbers int both 'primeFac1' and 'primeFac2' - + + done = [] # captured numbers int both 'primeFac1' and 'primeFac2' + # iterates through primeFac1 for n in primeFac1: - + if n not in done: - + if n in primeFac2: - + count1 = primeFac1.count(n) count2 = primeFac2.count(n) - - for i in range(max(count1,count2)): + + for i in range(max(count1, count2)): ans *= n - + else: - + count1 = primeFac1.count(n) - + for i in range(count1): ans *= n - + done.append(n) - + # iterates through primeFac2 for n in primeFac2: - + if n not in done: - + count2 = primeFac2.count(n) - + for i in range(count2): ans *= n - + done.append(n) - + # precondition - assert isinstance(ans,int) and (ans >= 0), \ - "'ans' must been from type int and positive" - + assert isinstance(ans, int) and ( + ans >= 0 + ), "'ans' must been from type int and positive" + return ans - + + # ---------------------------------- - + + def getPrime(n): """ - Gets the n-th prime number. - input: positive integer 'n' >= 0 - returns the n-th prime number, beginning at index 0 + Gets the n-th prime number. + input: positive integer 'n' >= 0 + returns the n-th prime number, beginning at index 0 """ - + # precondition - assert isinstance(n,int) and (n >= 0), "'number' must been a positive int" - + assert isinstance(n, int) and (n >= 0), "'number' must been a positive int" + index = 0 - ans = 2 # this variable holds the answer - + ans = 2 # this variable holds the answer + while index < n: - + index += 1 - - ans += 1 # counts to the next number - + + ans += 1 # counts to the next number + # if ans not prime then - # runs to the next prime number. + # runs to the next prime number. while not isPrime(ans): ans += 1 - + # precondition - assert isinstance(ans,int) and isPrime(ans), \ - "'ans' must been a prime number and from type int" - + assert isinstance(ans, int) and isPrime( + ans + ), "'ans' must been a prime number and from type int" + return ans - + + # --------------------------------------------------- - + + def getPrimesBetween(pNumber1, pNumber2): """ - input: prime numbers 'pNumber1' and 'pNumber2' - pNumber1 < pNumber2 - returns a list of all prime numbers between 'pNumber1' (exclusiv) - and 'pNumber2' (exclusiv) + input: prime numbers 'pNumber1' and 'pNumber2' + pNumber1 < pNumber2 + returns a list of all prime numbers between 'pNumber1' (exclusive) + and 'pNumber2' (exclusive) """ - + # precondition - assert isPrime(pNumber1) and isPrime(pNumber2) and (pNumber1 < pNumber2), \ - "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" - - number = pNumber1 + 1 # jump to the next number - - ans = [] # this list will be returns. - + assert ( + isPrime(pNumber1) and isPrime(pNumber2) and (pNumber1 < pNumber2) + ), "The arguments must been prime numbers and 'pNumber1' < 'pNumber2'" + + number = pNumber1 + 1 # jump to the next number + + ans = [] # this list will be returns. + # if number is not prime then - # fetch the next prime number. + # fetch the next prime number. while not isPrime(number): number += 1 - + while number < pNumber2: - + ans.append(number) - + number += 1 - - # fetch the next prime number. + + # fetch the next prime number. while not isPrime(number): number += 1 - + # precondition - assert isinstance(ans,list) and ans[0] != pNumber1 \ - and ans[len(ans)-1] != pNumber2, \ - "'ans' must been a list without the arguments" - + assert ( + isinstance(ans, list) and ans[0] != pNumber1 and ans[len(ans) - 1] != pNumber2 + ), "'ans' must been a list without the arguments" + # 'ans' contains not 'pNumber1' and 'pNumber2' ! return ans - + + # ---------------------------------------------------- + def getDivisors(n): """ - input: positive integer 'n' >= 1 - returns all divisors of n (inclusive 1 and 'n') + input: positive integer 'n' >= 1 + returns all divisors of n (inclusive 1 and 'n') """ - + # precondition - assert isinstance(n,int) and (n >= 1), "'n' must been int and >= 1" + assert isinstance(n, int) and (n >= 1), "'n' must been int and >= 1" + + ans = [] # will be returned. + + for divisor in range(1, n + 1): - from math import sqrt - - ans = [] # will be returned. - - for divisor in range(1,n+1): - if n % divisor == 0: ans.append(divisor) - - - #precondition - assert ans[0] == 1 and ans[len(ans)-1] == n, \ - "Error in function getDivisiors(...)" - - + + # precondition + assert ans[0] == 1 and ans[len(ans) - 1] == n, "Error in function getDivisiors(...)" + return ans @@ -520,85 +547,98 @@ def getDivisors(n): def isPerfectNumber(number): """ - input: positive integer 'number' > 1 - returns true if 'number' is a perfect number otherwise false. + input: positive integer 'number' > 1 + returns true if 'number' is a perfect number otherwise false. """ - + # precondition - assert isinstance(number,int) and (number > 1), \ - "'number' must been an int and >= 1" - + assert isinstance(number, int) and ( + number > 1 + ), "'number' must been an int and >= 1" + divisors = getDivisors(number) - + # precondition - assert isinstance(divisors,list) and(divisors[0] == 1) and \ - (divisors[len(divisors)-1] == number), \ - "Error in help-function getDivisiors(...)" - + assert ( + isinstance(divisors, list) + and (divisors[0] == 1) + and (divisors[len(divisors) - 1] == number) + ), "Error in help-function getDivisiors(...)" + # summed all divisors up to 'number' (exclusive), hence [:-1] return sum(divisors[:-1]) == number + # ------------------------------------------------------------ + def simplifyFraction(numerator, denominator): """ - input: two integer 'numerator' and 'denominator' - assumes: 'denominator' != 0 - returns: a tuple with simplify numerator and denominator. - """ - + input: two integer 'numerator' and 'denominator' + assumes: 'denominator' != 0 + returns: a tuple with simplify numerator and denominator. + """ + # precondition - assert isinstance(numerator, int) and isinstance(denominator,int) \ - and (denominator != 0), \ - "The arguments must been from type int and 'denominator' != 0" - + assert ( + isinstance(numerator, int) + and isinstance(denominator, int) + and (denominator != 0) + ), "The arguments must been from type int and 'denominator' != 0" + # build the greatest common divisor of numerator and denominator. gcdOfFraction = gcd(abs(numerator), abs(denominator)) # precondition - assert isinstance(gcdOfFraction, int) and (numerator % gcdOfFraction == 0) \ - and (denominator % gcdOfFraction == 0), \ - "Error in function gcd(...,...)" - + assert ( + isinstance(gcdOfFraction, int) + and (numerator % gcdOfFraction == 0) + and (denominator % gcdOfFraction == 0) + ), "Error in function gcd(...,...)" + return (numerator // gcdOfFraction, denominator // gcdOfFraction) - + + # ----------------------------------------------------------------- - + + def factorial(n): """ - input: positive integer 'n' - returns the factorial of 'n' (n!) + input: positive integer 'n' + returns the factorial of 'n' (n!) """ - + # precondition - assert isinstance(n,int) and (n >= 0), "'n' must been a int and >= 0" - - ans = 1 # this will be return. - - for factor in range(1,n+1): + assert isinstance(n, int) and (n >= 0), "'n' must been a int and >= 0" + + ans = 1 # this will be return. + + for factor in range(1, n + 1): ans *= factor - + return ans - + + # ------------------------------------------------------------------- - + + def fib(n): """ - input: positive integer 'n' - returns the n-th fibonacci term , indexing by 0 - """ - + input: positive integer 'n' + returns the n-th fibonacci term , indexing by 0 + """ + # precondition assert isinstance(n, int) and (n >= 0), "'n' must been an int and >= 0" - + tmp = 0 fib1 = 1 - ans = 1 # this will be return - - for i in range(n-1): - + ans = 1 # this will be return + + for i in range(n - 1): + tmp = ans ans += fib1 fib1 = tmp - + return ans diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py new file mode 100644 index 000000000000..77e614e2622c --- /dev/null +++ b/other/scoring_algorithm.py @@ -0,0 +1,89 @@ +""" +developed by: markmelnic +original repo: https://github.com/markmelnic/Scoring-Algorithm + +Analyse data using a range based percentual proximity algorithm +and calculate the linear maximum likelihood estimation. +The basic principle is that all values supplied will be broken +down to a range from 0 to 1 and each column's score will be added +up to get the total score. + +========== +Example for data of vehicles +price|mileage|registration_year +20k |60k |2012 +22k |50k |2011 +23k |90k |2015 +16k |210k |2010 + +We want the vehicle with the lowest price, +lowest mileage but newest registration year. +Thus the weights for each column are as follows: +[0, 0, 1] + +>>> procentual_proximity([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]], [0, 0, 1]) +[[20, 60, 2012, 2.0], [23, 90, 2015, 1.0], [22, 50, 2011, 1.3333333333333335]] +""" + + +def procentual_proximity(source_data: list, weights: list) -> list: + + """ + weights - int list + possible values - 0 / 1 + 0 if lower values have higher weight in the data set + 1 if higher values have higher weight in the data set + """ + + # getting data + data_lists = [] + for item in source_data: + for i in range(len(item)): + try: + data_lists[i].append(float(item[i])) + except IndexError: + # generate corresponding number of lists + data_lists.append([]) + data_lists[i].append(float(item[i])) + + score_lists = [] + # calculating each score + for dlist, weight in zip(data_lists, weights): + mind = min(dlist) + maxd = max(dlist) + + score = [] + # for weight 0 score is 1 - actual score + if weight == 0: + for item in dlist: + try: + score.append(1 - ((item - mind) / (maxd - mind))) + except ZeroDivisionError: + score.append(1) + + elif weight == 1: + for item in dlist: + try: + score.append((item - mind) / (maxd - mind)) + except ZeroDivisionError: + score.append(0) + + # weight not 0 or 1 + else: + raise ValueError("Invalid weight of %f provided" % (weight)) + + score_lists.append(score) + + # initialize final scores + final_scores = [0 for i in range(len(score_lists[0]))] + + # generate final scores + for i, slist in enumerate(score_lists): + for j, ele in enumerate(slist): + final_scores[j] = final_scores[j] + ele + + # append scores to source data + for i, ele in enumerate(final_scores): + source_data[i].append(ele) + + return source_data diff --git a/other/sdes.py b/other/sdes.py new file mode 100644 index 000000000000..cfc5a53df2b2 --- /dev/null +++ b/other/sdes.py @@ -0,0 +1,97 @@ +def apply_table(inp, table): + """ + >>> apply_table("0123456789", list(range(10))) + '9012345678' + >>> apply_table("0123456789", list(range(9, -1, -1))) + '8765432109' + """ + res = "" + for i in table: + res += inp[i - 1] + return res + + +def left_shift(data): + """ + >>> left_shift("0123456789") + '1234567890' + """ + return data[1:] + data[0] + + +def XOR(a, b): + """ + >>> XOR("01010101", "00001111") + '01011010' + """ + res = "" + for i in range(len(a)): + if a[i] == b[i]: + res += "0" + else: + res += "1" + return res + + +def apply_sbox(s, data): + row = int("0b" + data[0] + data[-1], 2) + col = int("0b" + data[1:3], 2) + return bin(s[row][col])[2:] + + +def function(expansion, s0, s1, key, message): + left = message[:4] + right = message[4:] + temp = apply_table(right, expansion) + temp = XOR(temp, key) + l = apply_sbox(s0, temp[:4]) # noqa: E741 + r = apply_sbox(s1, temp[4:]) + l = "0" * (2 - len(l)) + l # noqa: E741 + r = "0" * (2 - len(r)) + r + temp = apply_table(l + r, p4_table) + temp = XOR(left, temp) + return temp + right + + +if __name__ == "__main__": + + key = input("Enter 10 bit key: ") + message = input("Enter 8 bit message: ") + + p8_table = [6, 3, 7, 4, 8, 5, 10, 9] + p10_table = [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] + p4_table = [2, 4, 3, 1] + IP = [2, 6, 3, 1, 4, 8, 5, 7] + IP_inv = [4, 1, 3, 5, 7, 2, 8, 6] + expansion = [4, 1, 2, 3, 2, 3, 4, 1] + s0 = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]] + s1 = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]] + + # key generation + temp = apply_table(key, p10_table) + left = temp[:5] + right = temp[5:] + left = left_shift(left) + right = left_shift(right) + key1 = apply_table(left + right, p8_table) + left = left_shift(left) + right = left_shift(right) + left = left_shift(left) + right = left_shift(right) + key2 = apply_table(left + right, p8_table) + + # encryption + temp = apply_table(message, IP) + temp = function(expansion, s0, s1, key1, temp) + temp = temp[4:] + temp[:4] + temp = function(expansion, s0, s1, key2, temp) + CT = apply_table(temp, IP_inv) + print("Cipher text is:", CT) + + # decryption + temp = apply_table(CT, IP) + temp = function(expansion, s0, s1, key2, temp) + temp = temp[4:] + temp[:4] + temp = function(expansion, s0, s1, key1, temp) + PT = apply_table(temp, IP_inv) + print("Plain text after decypting is:", PT) diff --git a/other/sierpinski_triangle.py b/other/sierpinski_triangle.py index 6a06058fe03e..cf41ffa5f190 100644 --- a/other/sierpinski_triangle.py +++ b/other/sierpinski_triangle.py @@ -1,16 +1,18 @@ #!/usr/bin/python -# encoding=utf8 -'''Author Anurag Kumar | anuragkumarak95@gmail.com | git/anuragkumarak95 +"""Author Anurag Kumar | anuragkumarak95@gmail.com | git/anuragkumarak95 Simple example of Fractal generation using recursive function. What is Sierpinski Triangle? ->>The Sierpinski triangle (also with the original orthography Sierpinski), also called the Sierpinski gasket or the Sierpinski Sieve, -is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller -equilateral triangles. Originally constructed as a curve, this is one of the basic examples of self-similar sets, i.e., -it is a mathematically generated pattern that can be reproducible at any magnification or reduction. It is named after -the Polish mathematician Wacław Sierpinski, but appeared as a decorative pattern many centuries prior to the work of Sierpinski. +>>The Sierpinski triangle (also with the original orthography Sierpinski), also called +the Sierpinski gasket or the Sierpinski Sieve, is a fractal and attractive fixed set +with the overall shape of an equilateral triangle, subdivided recursively into smaller +equilateral triangles. Originally constructed as a curve, this is one of the basic +examples of self-similar sets, i.e., it is a mathematically generated pattern that can +be reproducible at any magnification or reduction. It is named after the Polish +mathematician Wacław Sierpinski, but appeared as a decorative pattern many centuries +prior to the work of Sierpinski. Requirements(pip): - turtle @@ -21,47 +23,54 @@ Usage: - $python sierpinski_triangle.py -Credits: This code was written by editing the code from http://www.lpb-riannetrujillo.com/blog/python-fractal/ +Credits: This code was written by editing the code from +http://www.riannetrujillo.com/blog/python-fractal/ -''' -import turtle +""" import sys -PROGNAME = 'Sierpinski Triangle' -if len(sys.argv) !=2: - raise Exception('right format for using this script: $python fractals.py ') +import turtle + +PROGNAME = "Sierpinski Triangle" + +points = [[-175, -125], [0, 175], [175, -125]] # size of triangle -myPen = turtle.Turtle() -myPen.ht() -myPen.speed(5) -myPen.pencolor('red') -points = [[-175,-125],[0,175],[175,-125]] #size of triangle +def getMid(p1, p2): + return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2) # find midpoint -def getMid(p1,p2): - return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2) #find midpoint -def triangle(points,depth): +def triangle(points, depth): myPen.up() - myPen.goto(points[0][0],points[0][1]) + myPen.goto(points[0][0], points[0][1]) myPen.down() - myPen.goto(points[1][0],points[1][1]) - myPen.goto(points[2][0],points[2][1]) - myPen.goto(points[0][0],points[0][1]) - - if depth>0: - triangle([points[0], - getMid(points[0], points[1]), - getMid(points[0], points[2])], - depth-1) - triangle([points[1], - getMid(points[0], points[1]), - getMid(points[1], points[2])], - depth-1) - triangle([points[2], - getMid(points[2], points[1]), - getMid(points[0], points[2])], - depth-1) - - -triangle(points,int(sys.argv[1])) \ No newline at end of file + myPen.goto(points[1][0], points[1][1]) + myPen.goto(points[2][0], points[2][1]) + myPen.goto(points[0][0], points[0][1]) + + if depth > 0: + triangle( + [points[0], getMid(points[0], points[1]), getMid(points[0], points[2])], + depth - 1, + ) + triangle( + [points[1], getMid(points[0], points[1]), getMid(points[1], points[2])], + depth - 1, + ) + triangle( + [points[2], getMid(points[2], points[1]), getMid(points[0], points[2])], + depth - 1, + ) + + +if __name__ == "__main__": + if len(sys.argv) != 2: + raise ValueError( + "right format for using this script: " + "$python fractals.py " + ) + myPen = turtle.Turtle() + myPen.ht() + myPen.speed(5) + myPen.pencolor("red") + triangle(points, int(sys.argv[1])) diff --git a/other/tower_of_hanoi.py b/other/tower_of_hanoi.py index dc15b2ce8e58..3cc0e40b369f 100644 --- a/other/tower_of_hanoi.py +++ b/other/tower_of_hanoi.py @@ -1,6 +1,5 @@ -from __future__ import print_function -def moveTower(height, fromPole, toPole, withPole): - ''' +def moveTower(height, fromPole, toPole, withPole): + """ >>> moveTower(3, 'A', 'B', 'C') moving disk from A to B moving disk from A to C @@ -9,18 +8,21 @@ def moveTower(height, fromPole, toPole, withPole): moving disk from C to A moving disk from C to B moving disk from A to B - ''' + """ if height >= 1: - moveTower(height-1, fromPole, withPole, toPole) + moveTower(height - 1, fromPole, withPole, toPole) moveDisk(fromPole, toPole) - moveTower(height-1, withPole, toPole, fromPole) + moveTower(height - 1, withPole, toPole, fromPole) + + +def moveDisk(fp, tp): + print("moving disk from", fp, "to", tp) -def moveDisk(fp,tp): - print(('moving disk from', fp, 'to', tp)) def main(): - height = int(input('Height of hanoi: ')) - moveTower(height, 'A', 'B', 'C') + height = int(input("Height of hanoi: ").strip()) + moveTower(height, "A", "B", "C") + -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/other/triplet_sum.py b/other/triplet_sum.py new file mode 100644 index 000000000000..0e78bb52bb72 --- /dev/null +++ b/other/triplet_sum.py @@ -0,0 +1,89 @@ +""" +Given an array of integers and another integer target, +we are required to find a triplet from the array such that it's sum is equal to +the target. +""" +from __future__ import annotations + +from itertools import permutations +from random import randint +from timeit import repeat + + +def make_dataset() -> tuple[list[int], int]: + arr = [randint(-1000, 1000) for i in range(10)] + r = randint(-5000, 5000) + return (arr, r) + + +dataset = make_dataset() + + +def triplet_sum1(arr: list[int], target: int) -> tuple[int, int, int]: + """ + Returns a triplet in the array with sum equal to target, + else (0, 0, 0). + >>> triplet_sum1([13, 29, 7, 23, 5], 35) + (5, 7, 23) + >>> triplet_sum1([37, 9, 19, 50, 44], 65) + (9, 19, 37) + >>> arr = [6, 47, 27, 1, 15] + >>> target = 11 + >>> triplet_sum1(arr, target) + (0, 0, 0) + """ + for triplet in permutations(arr, 3): + if sum(triplet) == target: + return tuple(sorted(triplet)) + return (0, 0, 0) + + +def triplet_sum2(arr: list[int], target: int) -> tuple[int, int, int]: + """ + Returns a triplet in the array with sum equal to target, + else (0, 0, 0). + >>> triplet_sum2([13, 29, 7, 23, 5], 35) + (5, 7, 23) + >>> triplet_sum2([37, 9, 19, 50, 44], 65) + (9, 19, 37) + >>> arr = [6, 47, 27, 1, 15] + >>> target = 11 + >>> triplet_sum2(arr, target) + (0, 0, 0) + """ + arr.sort() + n = len(arr) + for i in range(n - 1): + left, right = i + 1, n - 1 + while left < right: + if arr[i] + arr[left] + arr[right] == target: + return (arr[i], arr[left], arr[right]) + elif arr[i] + arr[left] + arr[right] < target: + left += 1 + elif arr[i] + arr[left] + arr[right] > target: + right -= 1 + return (0, 0, 0) + + +def solution_times() -> tuple[float, float]: + setup_code = """ +from __main__ import dataset, triplet_sum1, triplet_sum2 +""" + test_code1 = """ +triplet_sum1(*dataset) +""" + test_code2 = """ +triplet_sum2(*dataset) +""" + times1 = repeat(setup=setup_code, stmt=test_code1, repeat=5, number=10000) + times2 = repeat(setup=setup_code, stmt=test_code2, repeat=5, number=10000) + return (min(times1), min(times2)) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + times = solution_times() + print(f"The time for naive implementation is {times[0]}.") + print(f"The time for optimized implementation is {times[1]}.") diff --git a/other/two_pointer.py b/other/two_pointer.py new file mode 100644 index 000000000000..ff234cddc9e4 --- /dev/null +++ b/other/two_pointer.py @@ -0,0 +1,61 @@ +""" +Given a sorted array of integers, return indices of the two numbers such +that they add up to a specific target using the two pointers technique. + +You may assume that each input would have exactly one solution, and you +may not use the same element twice. + +This is an alternative solution of the two-sum problem, which uses a +map to solve the problem. Hence can not solve the issue if there is a +constraint not use the same index twice. [1] + +Example: +Given nums = [2, 7, 11, 15], target = 9, + +Because nums[0] + nums[1] = 2 + 7 = 9, +return [0, 1]. + +[1]: https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py +""" +from __future__ import annotations + + +def two_pointer(nums: list[int], target: int) -> list[int]: + """ + >>> two_pointer([2, 7, 11, 15], 9) + [0, 1] + >>> two_pointer([2, 7, 11, 15], 17) + [0, 3] + >>> two_pointer([2, 7, 11, 15], 18) + [1, 2] + >>> two_pointer([2, 7, 11, 15], 26) + [2, 3] + >>> two_pointer([1, 3, 3], 6) + [1, 2] + >>> two_pointer([2, 7, 11, 15], 8) + [] + >>> two_pointer([3 * i for i in range(10)], 19) + [] + >>> two_pointer([1, 2, 3], 6) + [] + """ + i = 0 + j = len(nums) - 1 + + while i < j: + + if nums[i] + nums[j] == target: + return [i, j] + elif nums[i] + nums[j] < target: + i = i + 1 + else: + j = j - 1 + + return [] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print(f"{two_pointer([2, 7, 11, 15], 9) = }") diff --git a/other/two_sum.py b/other/two_sum.py index d4484aa85505..5209acbc7e44 100644 --- a/other/two_sum.py +++ b/other/two_sum.py @@ -1,7 +1,9 @@ """ -Given an array of integers, return indices of the two numbers such that they add up to a specific target. +Given an array of integers, return indices of the two numbers such that they add up to +a specific target. -You may assume that each input would have exactly one solution, and you may not use the same element twice. +You may assume that each input would have exactly one solution, and you may not use the +same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, @@ -9,21 +11,37 @@ Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. """ -from __future__ import print_function +from __future__ import annotations -def twoSum(nums, target): + +def two_sum(nums: list[int], target: int) -> list[int]: """ - :type nums: List[int] - :type target: int - :rtype: List[int] + >>> two_sum([2, 7, 11, 15], 9) + [0, 1] + >>> two_sum([15, 2, 11, 7], 13) + [1, 2] + >>> two_sum([2, 7, 11, 15], 17) + [0, 3] + >>> two_sum([7, 15, 11, 2], 18) + [0, 2] + >>> two_sum([2, 7, 11, 15], 26) + [2, 3] + >>> two_sum([2, 7, 11, 15], 8) + [] + >>> two_sum([3 * i for i in range(10)], 19) + [] """ chk_map = {} for index, val in enumerate(nums): - compl = target - val - if compl in chk_map: - indices = [chk_map[compl], index] - print(indices) - return [indices] - else: + compl = target - val + if compl in chk_map: + return [chk_map[compl], index] chk_map[val] = index - return False + return [] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print(f"{two_sum([2, 7, 11, 15], 9) = }") diff --git a/other/word_patterns.py b/other/word_patterns.py index c33d520087f7..d229954dea93 100644 --- a/other/word_patterns.py +++ b/other/word_patterns.py @@ -1,39 +1,44 @@ -from __future__ import print_function -import pprint, time - -def getWordPattern(word): +def get_word_pattern(word: str) -> str: + """ + >>> get_word_pattern("pattern") + '0.1.2.2.3.4.5' + >>> get_word_pattern("word pattern") + '0.1.2.3.4.5.6.7.7.8.2.9' + >>> get_word_pattern("get word pattern") + '0.1.2.3.4.5.6.7.3.8.9.2.2.1.6.10' + """ word = word.upper() - nextNum = 0 - letterNums = {} - wordPattern = [] + next_num = 0 + letter_nums = {} + word_pattern = [] for letter in word: - if letter not in letterNums: - letterNums[letter] = str(nextNum) - nextNum += 1 - wordPattern.append(letterNums[letter]) - return '.'.join(wordPattern) + if letter not in letter_nums: + letter_nums[letter] = str(next_num) + next_num += 1 + word_pattern.append(letter_nums[letter]) + return ".".join(word_pattern) -def main(): - startTime = time.time() - allPatterns = {} - with open('Dictionary.txt') as fo: - wordList = fo.read().split('\n') +if __name__ == "__main__": + import pprint + import time - for word in wordList: - pattern = getWordPattern(word) + start_time = time.time() + with open("dictionary.txt") as in_file: + wordList = in_file.read().splitlines() - if pattern not in allPatterns: - allPatterns[pattern] = [word] + all_patterns = {} + for word in wordList: + pattern = get_word_pattern(word) + if pattern in all_patterns: + all_patterns[pattern].append(word) else: - allPatterns[pattern].append(word) - - with open('Word Patterns.txt', 'w') as fo: - fo.write(pprint.pformat(allPatterns)) + all_patterns[pattern] = [word] - totalTime = round(time.time() - startTime, 2) - print(('Done! [', totalTime, 'seconds ]')) + with open("word_patterns.txt", "w") as out_file: + out_file.write(pprint.pformat(all_patterns)) -if __name__ == '__main__': - main() + totalTime = round(time.time() - start_time, 2) + print(f"Done! {len(all_patterns):,} word patterns found in {totalTime} seconds.") + # Done! 9,581 word patterns found in 0.58 seconds. diff --git a/project_euler/README.md b/project_euler/README.md index 9f77f719f0f1..1cc6f8150e38 100644 --- a/project_euler/README.md +++ b/project_euler/README.md @@ -1,58 +1,118 @@ -# ProjectEuler +# Project Euler -Problems are taken from https://projecteuler.net/. +Problems are taken from https://projecteuler.net/, the Project Euler. [Problems are licensed under CC BY-NC-SA 4.0](https://projecteuler.net/copyright). -Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical +Project Euler is a series of challenging mathematical/computer programming problems that require more than just mathematical insights to solve. Project Euler is ideal for mathematicians who are learning to code. -Here the efficiency of your code is also checked. -I've tried to provide all the best possible solutions. - -PROBLEMS: - -1. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23. - Find the sum of all the multiples of 3 or 5 below N. - -2. Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, - the first 10 terms will be: - 1,2,3,5,8,13,21,34,55,89,.. - By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. - e.g. for n=10, we have {2,8}, sum is 10. - -3. The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? - e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. - -4. A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. - Find the largest palindrome made from the product of two 3-digit numbers which is less than N. - -5. 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. - What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? - -6. The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 - The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 552 = 3025 - Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. - Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. - -7. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. - What is the Nth prime number? - -9. A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, - a^2 + b^2 = c^2 - There exists exactly one Pythagorean triplet for which a + b + c = 1000. - Find the product abc. - -14. The following iterative sequence is defined for the set of positive integers: - n → n/2 (n is even) - n → 3n + 1 (n is odd) - Using the rule above and starting with 13, we generate the following sequence: - 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 - Which starting number, under one million, produces the longest chain? - -16. 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. - What is the sum of the digits of the number 2^1000? -20. n! means n × (n − 1) × ... × 3 × 2 × 1 - For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, - and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. - Find the sum of the digits in the number 100! +The solutions will be checked by our [automated testing on Travis CI](https://travis-ci.com/github/TheAlgorithms/Python/pull_requests) with the help of [this script](https://github.com/TheAlgorithms/Python/blob/master/scripts/validate_solutions.py). The efficiency of your code is also checked. You can view the top 10 slowest solutions on Travis CI logs (under `slowest 10 durations`) and open a pull request to improve those solutions. + + +## Solution Guidelines + +Welcome to [TheAlgorithms/Python](https://github.com/TheAlgorithms/Python)! Before reading the solution guidelines, make sure you read the whole [Contributing Guidelines](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md) as it won't be repeated in here. If you have any doubt on the guidelines, please feel free to [state it clearly in an issue](https://github.com/TheAlgorithms/Python/issues/new) or ask the community in [Gitter](https://gitter.im/TheAlgorithms). You can use the [template](https://github.com/TheAlgorithms/Python/blob/master/project_euler/README.md#solution-template) we have provided below as your starting point but be sure to read the [Coding Style](https://github.com/TheAlgorithms/Python/blob/master/project_euler/README.md#coding-style) part first. + +### Coding Style + +* Please maintain consistency in project directory and solution file names. Keep the following points in mind: + * Create a new directory only for the problems which do not exist yet. + * If you create a new directory, please create an empty `__init__.py` file inside it as well. + * Please name the project **directory** as `problem_` where `problem_number` should be filled with 0s so as to occupy 3 digits. Example: `problem_001`, `problem_002`, `problem_067`, `problem_145`, and so on. + +* Please provide a link to the problem and other references, if used, in the **module-level docstring**. + +* All imports should come ***after*** the module-level docstring. + +* You can have as many helper functions as you want but there should be one main function called `solution` which should satisfy the conditions as stated below: + * It should contain positional argument(s) whose default value is the question input. Example: Please take a look at [Problem 1](https://projecteuler.net/problem=1) where the question is to *Find the sum of all the multiples of 3 or 5 below 1000.* In this case the main solution function will be `solution(limit: int = 1000)`. + * When the `solution` function is called without any arguments like so: `solution()`, it should return the answer to the problem. + +* Every function, which includes all the helper functions, if any, and the main solution function, should have `doctest` in the function docstring along with a brief statement mentioning what the function is about. + * There should not be a `doctest` for testing the answer as that is done by our Travis CI build using this [script](https://github.com/TheAlgorithms/Python/blob/master/project_euler/validate_solutions.py). Keeping in mind the above example of [Problem 1](https://projecteuler.net/problem=1): + + ```python + def solution(limit: int = 1000): + """ + A brief statement mentioning what the function is about. + + You can have a detailed explanation about the solution method in the + module-level docstring. + + >>> solution(1) + ... + >>> solution(16) + ... + >>> solution(100) + ... + """ + ``` + +### Solution Template + +You can use the below template as your starting point but please read the [Coding Style](https://github.com/TheAlgorithms/Python/blob/master/project_euler/README.md#coding-style) first to understand how the template works. + +Please change the name of the helper functions accordingly, change the parameter names with a descriptive one, replace the content within `[square brackets]` (including the brackets) with the appropriate content. + +```python +""" +Project Euler Problem [problem number]: [link to the original problem] + +... [Entire problem statement] ... + +... [Solution explanation - Optional] ... + +References [Optional]: +- [Wikipedia link to the topic] +- [Stackoverflow link] +... + +""" +import module1 +import module2 +... + +def helper1(arg1: [type hint], arg2: [type hint], ...) -> [Return type hint]: + """ + A brief statement explaining what the function is about. + + ... A more elaborate description ... [Optional] + + ... + [Doctest] + ... + + """ + ... + # calculations + ... + + return + + +# You can have multiple helper functions but the solution function should be +# after all the helper functions ... + + +def solution(arg1: [type hint], arg2: [type hint], ...) -> [Return type hint]: + """ + A brief statement mentioning what the function is about. + + You can have a detailed explanation about the solution in the + module-level docstring. + + ... + [Doctest as mentioned above] + ... + + """ + + ... + # calculations + ... + + return answer + + +if __name__ == "__main__": + print(f"{solution() = }") +``` diff --git a/project_euler/__init__.py b/project_euler/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_001/__init__.py b/project_euler/problem_001/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_001/sol1.py b/project_euler/problem_001/sol1.py new file mode 100644 index 000000000000..85ad32294c9b --- /dev/null +++ b/project_euler/problem_001/sol1.py @@ -0,0 +1,33 @@ +""" +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3, 5, 6 and 9. The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. +""" + + +def solution(n: int = 1000) -> int: + """ + Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + >>> solution(-7) + 0 + """ + + return sum([e for e in range(3, n) if e % 3 == 0 or e % 5 == 0]) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol2.py b/project_euler/problem_001/sol2.py new file mode 100644 index 000000000000..7093d3513378 --- /dev/null +++ b/project_euler/problem_001/sol2.py @@ -0,0 +1,38 @@ +""" +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3, 5, 6 and 9. The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. +""" + + +def solution(n: int = 1000) -> int: + """ + Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + total = 0 + terms = (n - 1) // 3 + total += ((terms) * (6 + (terms - 1) * 3)) // 2 # total of an A.P. + terms = (n - 1) // 5 + total += ((terms) * (10 + (terms - 1) * 5)) // 2 + terms = (n - 1) // 15 + total -= ((terms) * (30 + (terms - 1) * 15)) // 2 + return total + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol3.py b/project_euler/problem_001/sol3.py new file mode 100644 index 000000000000..8267fec84155 --- /dev/null +++ b/project_euler/problem_001/sol3.py @@ -0,0 +1,64 @@ +""" +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3, 5, 6 and 9. The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. +""" + + +def solution(n: int = 1000) -> int: + """ + This solution is based on the pattern that the successive numbers in the + series follow: 0+3,+2,+1,+3,+1,+2,+3. + Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + total = 0 + num = 0 + while 1: + num += 3 + if num >= n: + break + total += num + num += 2 + if num >= n: + break + total += num + num += 1 + if num >= n: + break + total += num + num += 3 + if num >= n: + break + total += num + num += 1 + if num >= n: + break + total += num + num += 2 + if num >= n: + break + total += num + num += 3 + if num >= n: + break + total += num + return total + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol4.py b/project_euler/problem_001/sol4.py new file mode 100644 index 000000000000..a0643c05b34f --- /dev/null +++ b/project_euler/problem_001/sol4.py @@ -0,0 +1,52 @@ +""" +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3, 5, 6 and 9. The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. +""" + + +def solution(n: int = 1000) -> int: + """ + Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + xmulti = [] + zmulti = [] + z = 3 + x = 5 + temp = 1 + while True: + result = z * temp + if result < n: + zmulti.append(result) + temp += 1 + else: + temp = 1 + break + while True: + result = x * temp + if result < n: + xmulti.append(result) + temp += 1 + else: + break + collection = list(set(xmulti + zmulti)) + return sum(collection) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol5.py b/project_euler/problem_001/sol5.py new file mode 100644 index 000000000000..7f0b0bd1bc7c --- /dev/null +++ b/project_euler/problem_001/sol5.py @@ -0,0 +1,32 @@ +""" +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3, 5, 6 and 9. The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. +""" + + +def solution(n: int = 1000) -> int: + """ + Returns the sum of all the multiples of 3 or 5 below n. + A straightforward pythonic solution using list comprehension. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + return sum([i for i in range(n) if i % 3 == 0 or i % 5 == 0]) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol6.py b/project_euler/problem_001/sol6.py new file mode 100644 index 000000000000..8ddce18ced04 --- /dev/null +++ b/project_euler/problem_001/sol6.py @@ -0,0 +1,39 @@ +""" +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3, 5, 6 and 9. The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. +""" + + +def solution(n: int = 1000) -> int: + """ + Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + a = 3 + result = 0 + while a < n: + if a % 3 == 0 or a % 5 == 0: + result += a + elif a % 15 == 0: + result -= a + a += 1 + return result + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_001/sol7.py b/project_euler/problem_001/sol7.py new file mode 100644 index 000000000000..8f5d1977fdde --- /dev/null +++ b/project_euler/problem_001/sol7.py @@ -0,0 +1,37 @@ +""" +Project Euler Problem 1: https://projecteuler.net/problem=1 + +Multiples of 3 and 5 + +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3, 5, 6 and 9. The sum of these multiples is 23. + +Find the sum of all the multiples of 3 or 5 below 1000. +""" + + +def solution(n: int = 1000) -> int: + """ + Returns the sum of all the multiples of 3 or 5 below n. + + >>> solution(3) + 0 + >>> solution(4) + 3 + >>> solution(10) + 23 + >>> solution(600) + 83700 + """ + + result = 0 + for i in range(n): + if i % 3 == 0: + result += i + elif i % 5 == 0: + result += i + return result + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_002/__init__.py b/project_euler/problem_002/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_002/sol1.py b/project_euler/problem_002/sol1.py new file mode 100644 index 000000000000..539f68fb6bc1 --- /dev/null +++ b/project_euler/problem_002/sol1.py @@ -0,0 +1,48 @@ +""" +Project Euler Problem 2: https://projecteuler.net/problem=2 + +Even Fibonacci Numbers + +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + +By considering the terms in the Fibonacci sequence whose values do not exceed +four million, find the sum of the even-valued terms. + +References: + - https://en.wikipedia.org/wiki/Fibonacci_number +""" + + +def solution(n: int = 4000000) -> int: + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + + i = 1 + j = 2 + total = 0 + while j <= n: + if j % 2 == 0: + total += j + i, j = j, i + j + + return total + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol2.py b/project_euler/problem_002/sol2.py new file mode 100644 index 000000000000..9033d0a69bcf --- /dev/null +++ b/project_euler/problem_002/sol2.py @@ -0,0 +1,46 @@ +""" +Project Euler Problem 2: https://projecteuler.net/problem=2 + +Even Fibonacci Numbers + +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + +By considering the terms in the Fibonacci sequence whose values do not exceed +four million, find the sum of the even-valued terms. + +References: + - https://en.wikipedia.org/wiki/Fibonacci_number +""" + + +def solution(n: int = 4000000) -> int: + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + + even_fibs = [] + a, b = 0, 1 + while b <= n: + if b % 2 == 0: + even_fibs.append(b) + a, b = b, a + b + return sum(even_fibs) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol3.py b/project_euler/problem_002/sol3.py new file mode 100644 index 000000000000..3ae175a99815 --- /dev/null +++ b/project_euler/problem_002/sol3.py @@ -0,0 +1,48 @@ +""" +Project Euler Problem 2: https://projecteuler.net/problem=2 + +Even Fibonacci Numbers + +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + +By considering the terms in the Fibonacci sequence whose values do not exceed +four million, find the sum of the even-valued terms. + +References: + - https://en.wikipedia.org/wiki/Fibonacci_number +""" + + +def solution(n: int = 4000000) -> int: + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + + if n <= 1: + return 0 + a = 0 + b = 2 + count = 0 + while 4 * b + a <= n: + a, b = b, 4 * b + a + count += a + return count + b + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol4.py b/project_euler/problem_002/sol4.py new file mode 100644 index 000000000000..70b7d6a80a1d --- /dev/null +++ b/project_euler/problem_002/sol4.py @@ -0,0 +1,72 @@ +""" +Project Euler Problem 2: https://projecteuler.net/problem=2 + +Even Fibonacci Numbers + +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + +By considering the terms in the Fibonacci sequence whose values do not exceed +four million, find the sum of the even-valued terms. + +References: + - https://en.wikipedia.org/wiki/Fibonacci_number +""" +import math +from decimal import Decimal, getcontext + + +def solution(n: int = 4000000) -> int: + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + >>> solution(3.4) + 2 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + """ + + try: + n = int(n) + except (TypeError, ValueError): + raise TypeError("Parameter n must be int or castable to int.") + if n <= 0: + raise ValueError("Parameter n must be greater than or equal to one.") + getcontext().prec = 100 + phi = (Decimal(5) ** Decimal(0.5) + 1) / Decimal(2) + + index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2 + num = Decimal(round(phi ** Decimal(index + 1))) / (phi + 2) + total = num // 2 + return int(total) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_002/sol5.py b/project_euler/problem_002/sol5.py new file mode 100644 index 000000000000..390fd19ef638 --- /dev/null +++ b/project_euler/problem_002/sol5.py @@ -0,0 +1,52 @@ +""" +Project Euler Problem 2: https://projecteuler.net/problem=2 + +Even Fibonacci Numbers + +Each new term in the Fibonacci sequence is generated by adding the previous +two terms. By starting with 1 and 2, the first 10 terms will be: + +1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + +By considering the terms in the Fibonacci sequence whose values do not exceed +four million, find the sum of the even-valued terms. + +References: + - https://en.wikipedia.org/wiki/Fibonacci_number +""" + + +def solution(n: int = 4000000) -> int: + """ + Returns the sum of all even fibonacci sequence elements that are lower + or equal to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + + fib = [0, 1] + i = 0 + while fib[i] <= n: + fib.append(fib[i] + fib[i + 1]) + if fib[i + 2] > n: + break + i += 1 + total = 0 + for j in range(len(fib) - 1): + if fib[j] % 2 == 0: + total += fib[j] + + return total + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_003/__init__.py b/project_euler/problem_003/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_003/sol1.py b/project_euler/problem_003/sol1.py new file mode 100644 index 000000000000..3441dbf9e0b3 --- /dev/null +++ b/project_euler/problem_003/sol1.py @@ -0,0 +1,104 @@ +""" +Project Euler Problem 3: https://projecteuler.net/problem=3 + +Largest prime factor + +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization +""" +import math + + +def isprime(num: int) -> bool: + """ + Returns boolean representing primality of given number num. + + >>> isprime(2) + True + >>> isprime(3) + True + >>> isprime(27) + False + >>> isprime(2999) + True + >>> isprime(0) + Traceback (most recent call last): + ... + ValueError: Parameter num must be greater than or equal to two. + >>> isprime(1) + Traceback (most recent call last): + ... + ValueError: Parameter num must be greater than or equal to two. + """ + + if num <= 1: + raise ValueError("Parameter num must be greater than or equal to two.") + if num == 2: + return True + elif num % 2 == 0: + return False + for i in range(3, int(math.sqrt(num)) + 1, 2): + if num % i == 0: + return False + return True + + +def solution(n: int = 600851475143) -> int: + """ + Returns the largest prime factor of a given number n. + + >>> solution(13195) + 29 + >>> solution(10) + 5 + >>> solution(17) + 17 + >>> solution(3.4) + 3 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + """ + + try: + n = int(n) + except (TypeError, ValueError): + raise TypeError("Parameter n must be int or castable to int.") + if n <= 0: + raise ValueError("Parameter n must be greater than or equal to one.") + max_number = 0 + if isprime(n): + return n + while n % 2 == 0: + n //= 2 + if isprime(n): + return n + for i in range(3, int(math.sqrt(n)) + 1, 2): + if n % i == 0: + if isprime(n / i): + max_number = n / i + break + elif isprime(i): + max_number = i + return max_number + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_003/sol2.py b/project_euler/problem_003/sol2.py new file mode 100644 index 000000000000..0af0daceed06 --- /dev/null +++ b/project_euler/problem_003/sol2.py @@ -0,0 +1,64 @@ +""" +Project Euler Problem 3: https://projecteuler.net/problem=3 + +Largest prime factor + +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization +""" + + +def solution(n: int = 600851475143) -> int: + """ + Returns the largest prime factor of a given number n. + + >>> solution(13195) + 29 + >>> solution(10) + 5 + >>> solution(17) + 17 + >>> solution(3.4) + 3 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + """ + + try: + n = int(n) + except (TypeError, ValueError): + raise TypeError("Parameter n must be int or castable to int.") + if n <= 0: + raise ValueError("Parameter n must be greater than or equal to one.") + prime = 1 + i = 2 + while i * i <= n: + while n % i == 0: + prime = i + n //= i + i += 1 + if n > 1: + prime = n + return int(prime) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_003/sol3.py b/project_euler/problem_003/sol3.py new file mode 100644 index 000000000000..bc6f1d2f61ca --- /dev/null +++ b/project_euler/problem_003/sol3.py @@ -0,0 +1,66 @@ +""" +Project Euler Problem 3: https://projecteuler.net/problem=3 + +Largest prime factor + +The prime factors of 13195 are 5, 7, 13 and 29. + +What is the largest prime factor of the number 600851475143? + +References: + - https://en.wikipedia.org/wiki/Prime_number#Unique_factorization +""" + + +def solution(n: int = 600851475143) -> int: + """ + Returns the largest prime factor of a given number n. + + >>> solution(13195) + 29 + >>> solution(10) + 5 + >>> solution(17) + 17 + >>> solution(3.4) + 3 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + """ + + try: + n = int(n) + except (TypeError, ValueError): + raise TypeError("Parameter n must be int or castable to int.") + if n <= 0: + raise ValueError("Parameter n must be greater than or equal to one.") + i = 2 + ans = 0 + if n == 2: + return 2 + while n > 2: + while n % i != 0: + i += 1 + ans = i + while n % i == 0: + n = n / i + i += 1 + return int(ans) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_004/__init__.py b/project_euler/problem_004/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_004/sol1.py b/project_euler/problem_004/sol1.py new file mode 100644 index 000000000000..db6133a1a1d2 --- /dev/null +++ b/project_euler/problem_004/sol1.py @@ -0,0 +1,53 @@ +""" +Project Euler Problem 4: https://projecteuler.net/problem=4 + +Largest palindrome product + +A palindromic number reads the same both ways. The largest palindrome made +from the product of two 2-digit numbers is 9009 = 91 × 99. + +Find the largest palindrome made from the product of two 3-digit numbers. + +References: + - https://en.wikipedia.org/wiki/Palindromic_number +""" + + +def solution(n: int = 998001) -> int: + """ + Returns the largest palindrome made from the product of two 3-digit + numbers which is less than n. + + >>> solution(20000) + 19591 + >>> solution(30000) + 29992 + >>> solution(40000) + 39893 + >>> solution(10000) + Traceback (most recent call last): + ... + ValueError: That number is larger than our acceptable range. + """ + + # fetches the next number + for number in range(n - 1, 9999, -1): + + str_number = str(number) + + # checks whether 'str_number' is a palindrome. + if str_number == str_number[::-1]: + + divisor = 999 + + # if 'number' is a product of two 3-digit numbers + # then number is the answer otherwise fetch next number. + while divisor != 99: + if (number % divisor == 0) and (len(str(number // divisor)) == 3.0): + return number + divisor -= 1 + raise ValueError("That number is larger than our acceptable range.") + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_004/sol2.py b/project_euler/problem_004/sol2.py new file mode 100644 index 000000000000..abc880966d58 --- /dev/null +++ b/project_euler/problem_004/sol2.py @@ -0,0 +1,39 @@ +""" +Project Euler Problem 4: https://projecteuler.net/problem=4 + +Largest palindrome product + +A palindromic number reads the same both ways. The largest palindrome made +from the product of two 2-digit numbers is 9009 = 91 × 99. + +Find the largest palindrome made from the product of two 3-digit numbers. + +References: + - https://en.wikipedia.org/wiki/Palindromic_number +""" + + +def solution(n: int = 998001) -> int: + """ + Returns the largest palindrome made from the product of two 3-digit + numbers which is less than n. + + >>> solution(20000) + 19591 + >>> solution(30000) + 29992 + >>> solution(40000) + 39893 + """ + + answer = 0 + for i in range(999, 99, -1): # 3 digit numbers range from 999 down to 100 + for j in range(999, 99, -1): + product_string = str(i * j) + if product_string == product_string[::-1] and i * j < n: + answer = max(answer, i * j) + return answer + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_005/__init__.py b/project_euler/problem_005/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_005/sol1.py b/project_euler/problem_005/sol1.py new file mode 100644 index 000000000000..f272c102d2bb --- /dev/null +++ b/project_euler/problem_005/sol1.py @@ -0,0 +1,69 @@ +""" +Project Euler Problem 5: https://projecteuler.net/problem=5 + +Smallest multiple + +2520 is the smallest number that can be divided by each of the numbers +from 1 to 10 without any remainder. + +What is the smallest positive number that is _evenly divisible_ by all +of the numbers from 1 to 20? + +References: + - https://en.wiktionary.org/wiki/evenly_divisible +""" + + +def solution(n: int = 20) -> int: + """ + Returns the smallest positive number that is evenly divisible (divisible + with no remainder) by all of the numbers from 1 to n. + + >>> solution(10) + 2520 + >>> solution(15) + 360360 + >>> solution(22) + 232792560 + >>> solution(3.4) + 6 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater than or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or castable to int. + """ + + try: + n = int(n) + except (TypeError, ValueError): + raise TypeError("Parameter n must be int or castable to int.") + if n <= 0: + raise ValueError("Parameter n must be greater than or equal to one.") + i = 0 + while 1: + i += n * (n - 1) + nfound = 0 + for j in range(2, n): + if i % j != 0: + nfound = 1 + break + if nfound == 0: + if i == 0: + i = 1 + return i + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_005/sol2.py b/project_euler/problem_005/sol2.py new file mode 100644 index 000000000000..c88044487d20 --- /dev/null +++ b/project_euler/problem_005/sol2.py @@ -0,0 +1,75 @@ +""" +Project Euler Problem 5: https://projecteuler.net/problem=5 + +Smallest multiple + +2520 is the smallest number that can be divided by each of the numbers +from 1 to 10 without any remainder. + +What is the smallest positive number that is _evenly divisible_ by all +of the numbers from 1 to 20? + +References: + - https://en.wiktionary.org/wiki/evenly_divisible + - https://en.wikipedia.org/wiki/Euclidean_algorithm + - https://en.wikipedia.org/wiki/Least_common_multiple +""" + + +def gcd(x: int, y: int) -> int: + """ + Euclidean GCD algorithm (Greatest Common Divisor) + + >>> gcd(0, 0) + 0 + >>> gcd(23, 42) + 1 + >>> gcd(15, 33) + 3 + >>> gcd(12345, 67890) + 15 + """ + + return x if y == 0 else gcd(y, x % y) + + +def lcm(x: int, y: int) -> int: + """ + Least Common Multiple. + + Using the property that lcm(a, b) * gcd(a, b) = a*b + + >>> lcm(3, 15) + 15 + >>> lcm(1, 27) + 27 + >>> lcm(13, 27) + 351 + >>> lcm(64, 48) + 192 + """ + + return (x * y) // gcd(x, y) + + +def solution(n: int = 20) -> int: + """ + Returns the smallest positive number that is evenly divisible (divisible + with no remainder) by all of the numbers from 1 to n. + + >>> solution(10) + 2520 + >>> solution(15) + 360360 + >>> solution(22) + 232792560 + """ + + g = 1 + for i in range(1, n + 1): + g = lcm(g, i) + return g + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_006/__init__.py b/project_euler/problem_006/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_006/sol1.py b/project_euler/problem_006/sol1.py new file mode 100644 index 000000000000..61dd7a321011 --- /dev/null +++ b/project_euler/problem_006/sol1.py @@ -0,0 +1,44 @@ +""" +Project Euler Problem 6: https://projecteuler.net/problem=6 + +Sum square difference + +The sum of the squares of the first ten natural numbers is, + 1^2 + 2^2 + ... + 10^2 = 385 + +The square of the sum of the first ten natural numbers is, + (1 + 2 + ... + 10)^2 = 55^2 = 3025 + +Hence the difference between the sum of the squares of the first ten +natural numbers and the square of the sum is 3025 - 385 = 2640. + +Find the difference between the sum of the squares of the first one +hundred natural numbers and the square of the sum. +""" + + +def solution(n: int = 100) -> int: + """ + Returns the difference between the sum of the squares of the first n + natural numbers and the square of the sum. + + >>> solution(10) + 2640 + >>> solution(15) + 13160 + >>> solution(20) + 41230 + >>> solution(50) + 1582700 + """ + + sum_of_squares = 0 + sum_of_ints = 0 + for i in range(1, n + 1): + sum_of_squares += i ** 2 + sum_of_ints += i + return sum_of_ints ** 2 - sum_of_squares + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_006/sol2.py b/project_euler/problem_006/sol2.py new file mode 100644 index 000000000000..cd1bc5071e0e --- /dev/null +++ b/project_euler/problem_006/sol2.py @@ -0,0 +1,41 @@ +""" +Project Euler Problem 6: https://projecteuler.net/problem=6 + +Sum square difference + +The sum of the squares of the first ten natural numbers is, + 1^2 + 2^2 + ... + 10^2 = 385 + +The square of the sum of the first ten natural numbers is, + (1 + 2 + ... + 10)^2 = 55^2 = 3025 + +Hence the difference between the sum of the squares of the first ten +natural numbers and the square of the sum is 3025 - 385 = 2640. + +Find the difference between the sum of the squares of the first one +hundred natural numbers and the square of the sum. +""" + + +def solution(n: int = 100) -> int: + """ + Returns the difference between the sum of the squares of the first n + natural numbers and the square of the sum. + + >>> solution(10) + 2640 + >>> solution(15) + 13160 + >>> solution(20) + 41230 + >>> solution(50) + 1582700 + """ + + sum_cubes = (n * (n + 1) // 2) ** 2 + sum_squares = n * (n + 1) * (2 * n + 1) // 6 + return sum_cubes - sum_squares + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_006/sol3.py b/project_euler/problem_006/sol3.py new file mode 100644 index 000000000000..c87931309574 --- /dev/null +++ b/project_euler/problem_006/sol3.py @@ -0,0 +1,42 @@ +""" +Project Euler Problem 6: https://projecteuler.net/problem=6 + +Sum square difference + +The sum of the squares of the first ten natural numbers is, + 1^2 + 2^2 + ... + 10^2 = 385 + +The square of the sum of the first ten natural numbers is, + (1 + 2 + ... + 10)^2 = 55^2 = 3025 + +Hence the difference between the sum of the squares of the first ten +natural numbers and the square of the sum is 3025 - 385 = 2640. + +Find the difference between the sum of the squares of the first one +hundred natural numbers and the square of the sum. +""" +import math + + +def solution(n: int = 100) -> int: + """ + Returns the difference between the sum of the squares of the first n + natural numbers and the square of the sum. + + >>> solution(10) + 2640 + >>> solution(15) + 13160 + >>> solution(20) + 41230 + >>> solution(50) + 1582700 + """ + + sum_of_squares = sum([i * i for i in range(1, n + 1)]) + square_of_sum = int(math.pow(sum(range(1, n + 1)), 2)) + return square_of_sum - sum_of_squares + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_006/sol4.py b/project_euler/problem_006/sol4.py new file mode 100644 index 000000000000..748b141490a0 --- /dev/null +++ b/project_euler/problem_006/sol4.py @@ -0,0 +1,41 @@ +""" +Project Euler Problem 6: https://projecteuler.net/problem=6 + +Sum square difference + +The sum of the squares of the first ten natural numbers is, + 1^2 + 2^2 + ... + 10^2 = 385 + +The square of the sum of the first ten natural numbers is, + (1 + 2 + ... + 10)^2 = 55^2 = 3025 + +Hence the difference between the sum of the squares of the first ten +natural numbers and the square of the sum is 3025 - 385 = 2640. + +Find the difference between the sum of the squares of the first one +hundred natural numbers and the square of the sum. +""" + + +def solution(n: int = 100) -> int: + """ + Returns the difference between the sum of the squares of the first n + natural numbers and the square of the sum. + + >>> solution(10) + 2640 + >>> solution(15) + 13160 + >>> solution(20) + 41230 + >>> solution(50) + 1582700 + """ + + sum_of_squares = n * (n + 1) * (2 * n + 1) / 6 + square_of_sum = (n * (n + 1) / 2) ** 2 + return int(square_of_sum - sum_of_squares) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_007/__init__.py b/project_euler/problem_007/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_007/sol1.py b/project_euler/problem_007/sol1.py new file mode 100644 index 000000000000..78fbcb511611 --- /dev/null +++ b/project_euler/problem_007/sol1.py @@ -0,0 +1,76 @@ +""" +Project Euler Problem 7: https://projecteuler.net/problem=7 + +10001st prime + +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. + +What is the 10001st prime number? + +References: + - https://en.wikipedia.org/wiki/Prime_number +""" + +from math import sqrt + + +def is_prime(num: int) -> bool: + """ + Determines whether the given number is prime or not + + >>> is_prime(2) + True + >>> is_prime(15) + False + >>> is_prime(29) + True + >>> is_prime(0) + False + """ + + if num == 2: + return True + elif num % 2 == 0: + return False + else: + sq = int(sqrt(num)) + 1 + for i in range(3, sq, 2): + if num % i == 0: + return False + return True + + +def solution(nth: int = 10001) -> int: + """ + Returns the n-th prime number. + + >>> solution(6) + 13 + >>> solution(1) + 2 + >>> solution(3) + 5 + >>> solution(20) + 71 + >>> solution(50) + 229 + >>> solution(100) + 541 + """ + + count = 0 + number = 1 + while count != nth and number < 3: + number += 1 + if is_prime(number): + count += 1 + while count != nth: + number += 2 + if is_prime(number): + count += 1 + return number + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_007/sol2.py b/project_euler/problem_007/sol2.py new file mode 100644 index 000000000000..b395c631b766 --- /dev/null +++ b/project_euler/problem_007/sol2.py @@ -0,0 +1,88 @@ +""" +Project Euler Problem 7: https://projecteuler.net/problem=7 + +10001st prime + +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. + +What is the 10001st prime number? + +References: + - https://en.wikipedia.org/wiki/Prime_number +""" + + +def isprime(number: int) -> bool: + """ + Determines whether the given number is prime or not + + >>> isprime(2) + True + >>> isprime(15) + False + >>> isprime(29) + True + """ + + for i in range(2, int(number ** 0.5) + 1): + if number % i == 0: + return False + return True + + +def solution(nth: int = 10001) -> int: + """ + Returns the n-th prime number. + + >>> solution(6) + 13 + >>> solution(1) + 2 + >>> solution(3) + 5 + >>> solution(20) + 71 + >>> solution(50) + 229 + >>> solution(100) + 541 + >>> solution(3.4) + 5 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter nth must be greater than or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter nth must be greater than or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter nth must be int or castable to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter nth must be int or castable to int. + """ + + try: + nth = int(nth) + except (TypeError, ValueError): + raise TypeError("Parameter nth must be int or castable to int.") from None + if nth <= 0: + raise ValueError("Parameter nth must be greater than or equal to one.") + primes = [] + num = 2 + while len(primes) < nth: + if isprime(num): + primes.append(num) + num += 1 + else: + num += 1 + return primes[len(primes) - 1] + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_007/sol3.py b/project_euler/problem_007/sol3.py new file mode 100644 index 000000000000..7911fa3e9d6f --- /dev/null +++ b/project_euler/problem_007/sol3.py @@ -0,0 +1,68 @@ +""" +Project Euler Problem 7: https://projecteuler.net/problem=7 + +10001st prime + +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we +can see that the 6th prime is 13. + +What is the 10001st prime number? + +References: + - https://en.wikipedia.org/wiki/Prime_number +""" +import itertools +import math + + +def prime_check(number: int) -> bool: + """ + Determines whether a given number is prime or not + + >>> prime_check(2) + True + >>> prime_check(15) + False + >>> prime_check(29) + True + """ + + if number % 2 == 0 and number > 2: + return False + return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) + + +def prime_generator(): + """ + Generate a sequence of prime numbers + """ + + num = 2 + while True: + if prime_check(num): + yield num + num += 1 + + +def solution(nth: int = 10001) -> int: + """ + Returns the n-th prime number. + + >>> solution(6) + 13 + >>> solution(1) + 2 + >>> solution(3) + 5 + >>> solution(20) + 71 + >>> solution(50) + 229 + >>> solution(100) + 541 + """ + return next(itertools.islice(prime_generator(), nth - 1, nth)) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_008/__init__.py b/project_euler/problem_008/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_008/sol1.py b/project_euler/problem_008/sol1.py new file mode 100644 index 000000000000..796080127778 --- /dev/null +++ b/project_euler/problem_008/sol1.py @@ -0,0 +1,82 @@ +""" +Project Euler Problem 8: https://projecteuler.net/problem=8 + +Largest product in a series + +The four adjacent digits in the 1000-digit number that have the greatest +product are 9 × 9 × 8 × 9 = 5832. + + 73167176531330624919225119674426574742355349194934 + 96983520312774506326239578318016984801869478851843 + 85861560789112949495459501737958331952853208805511 + 12540698747158523863050715693290963295227443043557 + 66896648950445244523161731856403098711121722383113 + 62229893423380308135336276614282806444486645238749 + 30358907296290491560440772390713810515859307960866 + 70172427121883998797908792274921901699720888093776 + 65727333001053367881220235421809751254540594752243 + 52584907711670556013604839586446706324415722155397 + 53697817977846174064955149290862569321978468622482 + 83972241375657056057490261407972968652414535100474 + 82166370484403199890008895243450658541227588666881 + 16427171479924442928230863465674813919123162824586 + 17866458359124566529476545682848912883142607690042 + 24219022671055626321111109370544217506941658960408 + 07198403850962455444362981230987879927244284909188 + 84580156166097919133875499200524063689912560717606 + 05886116467109405077541002256983155200055935729725 + 71636269561882670428252483600823257530420752963450 + +Find the thirteen adjacent digits in the 1000-digit number that have the +greatest product. What is the value of this product? +""" + +import sys + +N = """73167176531330624919225119674426574742355349194934\ +96983520312774506326239578318016984801869478851843\ +85861560789112949495459501737958331952853208805511\ +12540698747158523863050715693290963295227443043557\ +66896648950445244523161731856403098711121722383113\ +62229893423380308135336276614282806444486645238749\ +30358907296290491560440772390713810515859307960866\ +70172427121883998797908792274921901699720888093776\ +65727333001053367881220235421809751254540594752243\ +52584907711670556013604839586446706324415722155397\ +53697817977846174064955149290862569321978468622482\ +83972241375657056057490261407972968652414535100474\ +82166370484403199890008895243450658541227588666881\ +16427171479924442928230863465674813919123162824586\ +17866458359124566529476545682848912883142607690042\ +24219022671055626321111109370544217506941658960408\ +07198403850962455444362981230987879927244284909188\ +84580156166097919133875499200524063689912560717606\ +05886116467109405077541002256983155200055935729725\ +71636269561882670428252483600823257530420752963450""" + + +def solution(n: str = N) -> int: + """ + Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. + + >>> solution("13978431290823798458352374") + 609638400 + >>> solution("13978431295823798458352374") + 2612736000 + >>> solution("1397843129582379841238352374") + 209018880 + """ + + largest_product = -sys.maxsize - 1 + for i in range(len(n) - 12): + product = 1 + for j in range(13): + product *= int(n[i + j]) + if product > largest_product: + largest_product = product + return largest_product + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_008/sol2.py b/project_euler/problem_008/sol2.py new file mode 100644 index 000000000000..d2c1b4f7ca48 --- /dev/null +++ b/project_euler/problem_008/sol2.py @@ -0,0 +1,81 @@ +""" +Project Euler Problem 8: https://projecteuler.net/problem=8 + +Largest product in a series + +The four adjacent digits in the 1000-digit number that have the greatest +product are 9 × 9 × 8 × 9 = 5832. + + 73167176531330624919225119674426574742355349194934 + 96983520312774506326239578318016984801869478851843 + 85861560789112949495459501737958331952853208805511 + 12540698747158523863050715693290963295227443043557 + 66896648950445244523161731856403098711121722383113 + 62229893423380308135336276614282806444486645238749 + 30358907296290491560440772390713810515859307960866 + 70172427121883998797908792274921901699720888093776 + 65727333001053367881220235421809751254540594752243 + 52584907711670556013604839586446706324415722155397 + 53697817977846174064955149290862569321978468622482 + 83972241375657056057490261407972968652414535100474 + 82166370484403199890008895243450658541227588666881 + 16427171479924442928230863465674813919123162824586 + 17866458359124566529476545682848912883142607690042 + 24219022671055626321111109370544217506941658960408 + 07198403850962455444362981230987879927244284909188 + 84580156166097919133875499200524063689912560717606 + 05886116467109405077541002256983155200055935729725 + 71636269561882670428252483600823257530420752963450 + +Find the thirteen adjacent digits in the 1000-digit number that have the +greatest product. What is the value of this product? +""" +from functools import reduce + +N = ( + "73167176531330624919225119674426574742355349194934" + "96983520312774506326239578318016984801869478851843" + "85861560789112949495459501737958331952853208805511" + "12540698747158523863050715693290963295227443043557" + "66896648950445244523161731856403098711121722383113" + "62229893423380308135336276614282806444486645238749" + "30358907296290491560440772390713810515859307960866" + "70172427121883998797908792274921901699720888093776" + "65727333001053367881220235421809751254540594752243" + "52584907711670556013604839586446706324415722155397" + "53697817977846174064955149290862569321978468622482" + "83972241375657056057490261407972968652414535100474" + "82166370484403199890008895243450658541227588666881" + "16427171479924442928230863465674813919123162824586" + "17866458359124566529476545682848912883142607690042" + "24219022671055626321111109370544217506941658960408" + "07198403850962455444362981230987879927244284909188" + "84580156166097919133875499200524063689912560717606" + "05886116467109405077541002256983155200055935729725" + "71636269561882670428252483600823257530420752963450" +) + + +def solution(n: str = N) -> int: + """ + Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. + + >>> solution("13978431290823798458352374") + 609638400 + >>> solution("13978431295823798458352374") + 2612736000 + >>> solution("1397843129582379841238352374") + 209018880 + """ + + return max( + [ + reduce(lambda x, y: int(x) * int(y), n[i : i + 13]) + for i in range(len(n) - 12) + ] + ) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_008/sol3.py b/project_euler/problem_008/sol3.py new file mode 100644 index 000000000000..4b99d0ea6e76 --- /dev/null +++ b/project_euler/problem_008/sol3.py @@ -0,0 +1,94 @@ +""" +Project Euler Problem 8: https://projecteuler.net/problem=8 + +Largest product in a series + +The four adjacent digits in the 1000-digit number that have the greatest +product are 9 × 9 × 8 × 9 = 5832. + + 73167176531330624919225119674426574742355349194934 + 96983520312774506326239578318016984801869478851843 + 85861560789112949495459501737958331952853208805511 + 12540698747158523863050715693290963295227443043557 + 66896648950445244523161731856403098711121722383113 + 62229893423380308135336276614282806444486645238749 + 30358907296290491560440772390713810515859307960866 + 70172427121883998797908792274921901699720888093776 + 65727333001053367881220235421809751254540594752243 + 52584907711670556013604839586446706324415722155397 + 53697817977846174064955149290862569321978468622482 + 83972241375657056057490261407972968652414535100474 + 82166370484403199890008895243450658541227588666881 + 16427171479924442928230863465674813919123162824586 + 17866458359124566529476545682848912883142607690042 + 24219022671055626321111109370544217506941658960408 + 07198403850962455444362981230987879927244284909188 + 84580156166097919133875499200524063689912560717606 + 05886116467109405077541002256983155200055935729725 + 71636269561882670428252483600823257530420752963450 + +Find the thirteen adjacent digits in the 1000-digit number that have the +greatest product. What is the value of this product? +""" +import sys + +N = """73167176531330624919225119674426574742355349194934\ +96983520312774506326239578318016984801869478851843\ +85861560789112949495459501737958331952853208805511\ +12540698747158523863050715693290963295227443043557\ +66896648950445244523161731856403098711121722383113\ +62229893423380308135336276614282806444486645238749\ +30358907296290491560440772390713810515859307960866\ +70172427121883998797908792274921901699720888093776\ +65727333001053367881220235421809751254540594752243\ +52584907711670556013604839586446706324415722155397\ +53697817977846174064955149290862569321978468622482\ +83972241375657056057490261407972968652414535100474\ +82166370484403199890008895243450658541227588666881\ +16427171479924442928230863465674813919123162824586\ +17866458359124566529476545682848912883142607690042\ +24219022671055626321111109370544217506941658960408\ +07198403850962455444362981230987879927244284909188\ +84580156166097919133875499200524063689912560717606\ +05886116467109405077541002256983155200055935729725\ +71636269561882670428252483600823257530420752963450""" + + +def str_eval(s: str) -> int: + """ + Returns product of digits in given string n + + >>> str_eval("987654321") + 362880 + >>> str_eval("22222222") + 256 + """ + + product = 1 + for digit in s: + product *= int(digit) + return product + + +def solution(n: str = N) -> int: + """ + Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. + """ + + largest_product = -sys.maxsize - 1 + substr = n[:13] + cur_index = 13 + while cur_index < len(n) - 13: + if int(n[cur_index]) >= int(substr[0]): + substr = substr[1:] + n[cur_index] + cur_index += 1 + else: + largest_product = max(largest_product, str_eval(substr)) + substr = n[cur_index : cur_index + 13] + cur_index += 13 + return largest_product + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_009/__init__.py b/project_euler/problem_009/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_009/sol1.py b/project_euler/problem_009/sol1.py new file mode 100644 index 000000000000..a58ea943e48b --- /dev/null +++ b/project_euler/problem_009/sol1.py @@ -0,0 +1,79 @@ +""" +Project Euler Problem 9: https://projecteuler.net/problem=9 + +Special Pythagorean triplet + +A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + + a^2 + b^2 = c^2 + +For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. + +There exists exactly one Pythagorean triplet for which a + b + c = 1000. +Find the product a*b*c. + +References: + - https://en.wikipedia.org/wiki/Pythagorean_triple +""" + + +def solution() -> int: + """ + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = 1000 + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution() + # 31875000 + """ + + for a in range(300): + for b in range(400): + for c in range(500): + if a < b < c: + if (a ** 2) + (b ** 2) == (c ** 2): + if (a + b + c) == 1000: + return a * b * c + + +def solution_fast() -> int: + """ + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = 1000 + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution_fast() + # 31875000 + """ + + for a in range(300): + for b in range(400): + c = 1000 - a - b + if a < b < c and (a ** 2) + (b ** 2) == (c ** 2): + return a * b * c + + +def benchmark() -> None: + """ + Benchmark code comparing two different version function. + """ + import timeit + + print( + timeit.timeit("solution()", setup="from __main__ import solution", number=1000) + ) + print( + timeit.timeit( + "solution_fast()", setup="from __main__ import solution_fast", number=1000 + ) + ) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_009/sol2.py b/project_euler/problem_009/sol2.py new file mode 100644 index 000000000000..722ad522ee45 --- /dev/null +++ b/project_euler/problem_009/sol2.py @@ -0,0 +1,48 @@ +""" +Project Euler Problem 9: https://projecteuler.net/problem=9 + +Special Pythagorean triplet + +A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + + a^2 + b^2 = c^2 + +For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. + +There exists exactly one Pythagorean triplet for which a + b + c = 1000. +Find the product a*b*c. + +References: + - https://en.wikipedia.org/wiki/Pythagorean_triple +""" + + +def solution(n: int = 1000) -> int: + """ + Return the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a < b < c + 2. a**2 + b**2 = c**2 + 3. a + b + c = n + + >>> solution(36) + 1620 + >>> solution(126) + 66780 + """ + + product = -1 + candidate = 0 + for a in range(1, n // 3): + # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c + b = (n * n - 2 * a * n) // (2 * n - 2 * a) + c = n - a - b + if c * c == (a * a + b * b): + candidate = a * b * c + if candidate >= product: + product = candidate + return product + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_009/sol3.py b/project_euler/problem_009/sol3.py new file mode 100644 index 000000000000..03aed4b70761 --- /dev/null +++ b/project_euler/problem_009/sol3.py @@ -0,0 +1,41 @@ +""" +Project Euler Problem 9: https://projecteuler.net/problem=9 + +Special Pythagorean triplet + +A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + + a^2 + b^2 = c^2 + +For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. + +There exists exactly one Pythagorean triplet for which a + b + c = 1000. +Find the product a*b*c. + +References: + - https://en.wikipedia.org/wiki/Pythagorean_triple +""" + + +def solution() -> int: + """ + Returns the product of a,b,c which are Pythagorean Triplet that satisfies + the following: + 1. a**2 + b**2 = c**2 + 2. a + b + c = 1000 + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution() + # 31875000 + """ + + return [ + a * b * (1000 - a - b) + for a in range(1, 999) + for b in range(a, 999) + if (a * a + b * b == (1000 - a - b) ** 2) + ][0] + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_01/sol1.py b/project_euler/problem_01/sol1.py deleted file mode 100644 index 27031c3cfa9a..000000000000 --- a/project_euler/problem_01/sol1.py +++ /dev/null @@ -1,17 +0,0 @@ -''' -Problem Statement: -If we list all the natural numbers below 10 that are multiples of 3 or 5, -we get 3,5,6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. -''' -from __future__ import print_function -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 -n = int(raw_input().strip()) -sum=0 -for a in range(3,n): - if(a%3==0 or a%5==0): - sum+=a -print(sum) diff --git a/project_euler/problem_01/sol2.py b/project_euler/problem_01/sol2.py deleted file mode 100644 index 2b7760e0bfff..000000000000 --- a/project_euler/problem_01/sol2.py +++ /dev/null @@ -1,20 +0,0 @@ -''' -Problem Statement: -If we list all the natural numbers below 10 that are multiples of 3 or 5, -we get 3,5,6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. -''' -from __future__ import print_function -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 -n = int(raw_input().strip()) -sum = 0 -terms = (n-1)//3 -sum+= ((terms)*(6+(terms-1)*3))//2 #sum of an A.P. -terms = (n-1)//5 -sum+= ((terms)*(10+(terms-1)*5))//2 -terms = (n-1)//15 -sum-= ((terms)*(30+(terms-1)*15))//2 -print(sum) diff --git a/project_euler/problem_01/sol3.py b/project_euler/problem_01/sol3.py deleted file mode 100644 index f4f3aefcc5de..000000000000 --- a/project_euler/problem_01/sol3.py +++ /dev/null @@ -1,50 +0,0 @@ -from __future__ import print_function - -''' -Problem Statement: -If we list all the natural numbers below 10 that are multiples of 3 or 5, -we get 3,5,6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. -''' -''' -This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3. -''' - -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 -n = int(raw_input().strip()) -sum=0 -num=0 -while(1): - num+=3 - if(num>=n): - break - sum+=num - num+=2 - if(num>=n): - break - sum+=num - num+=1 - if(num>=n): - break - sum+=num - num+=3 - if(num>=n): - break - sum+=num - num+=1 - if(num>=n): - break - sum+=num - num+=2 - if(num>=n): - break - sum+=num - num+=3 - if(num>=n): - break - sum+=num - -print(sum); diff --git a/project_euler/problem_01/sol4.py b/project_euler/problem_01/sol4.py deleted file mode 100644 index 7941f5fcd3fe..000000000000 --- a/project_euler/problem_01/sol4.py +++ /dev/null @@ -1,30 +0,0 @@ -def mulitples(limit): - xmulti = [] - zmulti = [] - z = 3 - x = 5 - temp = 1 - while True: - result = z * temp - if (result < limit): - zmulti.append(result) - temp += 1 - else: - temp = 1 - break - while True: - result = x * temp - if (result < limit): - xmulti.append(result) - temp += 1 - else: - break - collection = list(set(xmulti+zmulti)) - return (sum(collection)) - - - - - - -print (mulitples(1000)) diff --git a/project_euler/problem_01/sol5.py b/project_euler/problem_01/sol5.py deleted file mode 100644 index e261cc8fc729..000000000000 --- a/project_euler/problem_01/sol5.py +++ /dev/null @@ -1,16 +0,0 @@ -''' -Problem Statement: -If we list all the natural numbers below 10 that are multiples of 3 or 5, -we get 3,5,6 and 9. The sum of these multiples is 23. -Find the sum of all the multiples of 3 or 5 below N. -''' -from __future__ import print_function -try: - input = raw_input #python3 -except NameError: - pass #python 2 - -"""A straightforward pythonic solution using list comprehension""" -n = int(input().strip()) -print(sum([i for i in range(n) if i%3==0 or i%5==0])) - diff --git a/project_euler/problem_010/__init__.py b/project_euler/problem_010/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_010/sol1.py b/project_euler/problem_010/sol1.py new file mode 100644 index 000000000000..bd49b3523c97 --- /dev/null +++ b/project_euler/problem_010/sol1.py @@ -0,0 +1,65 @@ +""" +Project Euler Problem 10: https://projecteuler.net/problem=10 + +Summation of primes + +The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + +Find the sum of all the primes below two million. + +References: + - https://en.wikipedia.org/wiki/Prime_number +""" + +from math import sqrt + + +def is_prime(n: int) -> bool: + """ + Returns boolean representing primality of given number num. + + >>> is_prime(2) + True + >>> is_prime(3) + True + >>> is_prime(27) + False + >>> is_prime(2999) + True + """ + + for i in range(2, int(sqrt(n)) + 1): + if n % i == 0: + return False + + return True + + +def solution(n: int = 2000000) -> int: + """ + Returns the sum of all the primes below n. + + >>> solution(1000) + 76127 + >>> solution(5000) + 1548136 + >>> solution(10000) + 5736396 + >>> solution(7) + 10 + """ + + if n > 2: + sum_of_primes = 2 + else: + return 0 + + for i in range(3, n, 2): + if is_prime(i): + sum_of_primes += i + + return sum_of_primes + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_010/sol2.py b/project_euler/problem_010/sol2.py new file mode 100644 index 000000000000..3a2f485dde50 --- /dev/null +++ b/project_euler/problem_010/sol2.py @@ -0,0 +1,67 @@ +""" +Project Euler Problem 10: https://projecteuler.net/problem=10 + +Summation of primes + +The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + +Find the sum of all the primes below two million. + +References: + - https://en.wikipedia.org/wiki/Prime_number +""" +import math +from itertools import takewhile +from typing import Iterator + + +def is_prime(number: int) -> bool: + """ + Returns boolean representing primality of given number num. + + >>> is_prime(2) + True + >>> is_prime(3) + True + >>> is_prime(27) + False + >>> is_prime(2999) + True + """ + + if number % 2 == 0 and number > 2: + return False + return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) + + +def prime_generator() -> Iterator[int]: + """ + Generate a list sequence of prime numbers + """ + + num = 2 + while True: + if is_prime(num): + yield num + num += 1 + + +def solution(n: int = 2000000) -> int: + """ + Returns the sum of all the primes below n. + + >>> solution(1000) + 76127 + >>> solution(5000) + 1548136 + >>> solution(10000) + 5736396 + >>> solution(7) + 10 + """ + + return sum(takewhile(lambda x: x < n, prime_generator())) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_010/sol3.py b/project_euler/problem_010/sol3.py new file mode 100644 index 000000000000..f49d9393c7af --- /dev/null +++ b/project_euler/problem_010/sol3.py @@ -0,0 +1,61 @@ +""" +Project Euler Problem 10: https://projecteuler.net/problem=10 + +Summation of primes + +The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + +Find the sum of all the primes below two million. + +References: + - https://en.wikipedia.org/wiki/Prime_number + - https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes +""" + + +def solution(n: int = 2000000) -> int: + """ + Returns the sum of all the primes below n using Sieve of Eratosthenes: + + The sieve of Eratosthenes is one of the most efficient ways to find all primes + smaller than n when n is smaller than 10 million. Only for positive numbers. + + >>> solution(1000) + 76127 + >>> solution(5000) + 1548136 + >>> solution(10000) + 5736396 + >>> solution(7) + 10 + >>> solution(7.1) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> solution(-7) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + IndexError: list assignment index out of range + >>> solution("seven") # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: can only concatenate str (not "int") to str + """ + + primality_list = [0 for i in range(n + 1)] + primality_list[0] = 1 + primality_list[1] = 1 + + for i in range(2, int(n ** 0.5) + 1): + if primality_list[i] == 0: + for j in range(i * i, n + 1, i): + primality_list[j] = 1 + sum_of_primes = 0 + for i in range(n): + if primality_list[i] == 0: + sum_of_primes += i + return sum_of_primes + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_011/__init__.py b/project_euler/problem_011/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_11/grid.txt b/project_euler/problem_011/grid.txt similarity index 95% rename from project_euler/problem_11/grid.txt rename to project_euler/problem_011/grid.txt index 1fc75c66a314..4ac24518973e 100644 --- a/project_euler/problem_11/grid.txt +++ b/project_euler/problem_011/grid.txt @@ -17,4 +17,4 @@ 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 -01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 \ No newline at end of file +01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 diff --git a/project_euler/problem_011/sol1.py b/project_euler/problem_011/sol1.py new file mode 100644 index 000000000000..9dea73e8cef2 --- /dev/null +++ b/project_euler/problem_011/sol1.py @@ -0,0 +1,88 @@ +""" +What is the greatest product of four adjacent numbers (horizontally, +vertically, or diagonally) in this 20x20 array? + +08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 +49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 +81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 +52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 +22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 +24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 +32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 +67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 +24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 +21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 +78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 +16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 +86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 +19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 +04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 +88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 +04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 +20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 +20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 +01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 +""" + +import os + + +def largest_product(grid): + nColumns = len(grid[0]) + nRows = len(grid) + + largest = 0 + lrDiagProduct = 0 + rlDiagProduct = 0 + + # Check vertically, horizontally, diagonally at the same time (only works + # for nxn grid) + for i in range(nColumns): + for j in range(nRows - 3): + vertProduct = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i] + horzProduct = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3] + + # Left-to-right diagonal (\) product + if i < nColumns - 3: + lrDiagProduct = ( + grid[i][j] + * grid[i + 1][j + 1] + * grid[i + 2][j + 2] + * grid[i + 3][j + 3] + ) + + # Right-to-left diagonal(/) product + if i > 2: + rlDiagProduct = ( + grid[i][j] + * grid[i - 1][j + 1] + * grid[i - 2][j + 2] + * grid[i - 3][j + 3] + ) + + maxProduct = max(vertProduct, horzProduct, lrDiagProduct, rlDiagProduct) + if maxProduct > largest: + largest = maxProduct + + return largest + + +def solution(): + """Returns the greatest product of four adjacent numbers (horizontally, + vertically, or diagonally). + + >>> solution() + 70600674 + """ + grid = [] + with open(os.path.dirname(__file__) + "/grid.txt") as file: + for line in file: + grid.append(line.strip("\n").split(" ")) + + grid = [[int(i) for i in grid[j]] for j in range(len(grid))] + + return largest_product(grid) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_011/sol2.py b/project_euler/problem_011/sol2.py new file mode 100644 index 000000000000..839ca6717571 --- /dev/null +++ b/project_euler/problem_011/sol2.py @@ -0,0 +1,75 @@ +""" +What is the greatest product of four adjacent numbers (horizontally, +vertically, or diagonally) in this 20x20 array? + +08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 +49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 +81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 +52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 +22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 +24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 +32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 +67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 +24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 +21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 +78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 +16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 +86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 +19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 +04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 +88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 +04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 +20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 +20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 +01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 +""" + +import os + + +def solution(): + """Returns the greatest product of four adjacent numbers (horizontally, + vertically, or diagonally). + + >>> solution() + 70600674 + """ + with open(os.path.dirname(__file__) + "/grid.txt") as f: + l = [] # noqa: E741 + for i in range(20): + l.append([int(x) for x in f.readline().split()]) + + maximum = 0 + + # right + for i in range(20): + for j in range(17): + temp = l[i][j] * l[i][j + 1] * l[i][j + 2] * l[i][j + 3] + if temp > maximum: + maximum = temp + + # down + for i in range(17): + for j in range(20): + temp = l[i][j] * l[i + 1][j] * l[i + 2][j] * l[i + 3][j] + if temp > maximum: + maximum = temp + + # diagonal 1 + for i in range(17): + for j in range(17): + temp = l[i][j] * l[i + 1][j + 1] * l[i + 2][j + 2] * l[i + 3][j + 3] + if temp > maximum: + maximum = temp + + # diagonal 2 + for i in range(17): + for j in range(3, 20): + temp = l[i][j] * l[i + 1][j - 1] * l[i + 2][j - 2] * l[i + 3][j - 3] + if temp > maximum: + maximum = temp + return maximum + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_012/__init__.py b/project_euler/problem_012/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_012/sol1.py b/project_euler/problem_012/sol1.py new file mode 100644 index 000000000000..7e080c4e45a1 --- /dev/null +++ b/project_euler/problem_012/sol1.py @@ -0,0 +1,60 @@ +""" +Highly divisible triangular numbers +Problem 12 +The sequence of triangle numbers is generated by adding the natural numbers. So +the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten +terms would be: + +1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + +Let us list the factors of the first seven triangle numbers: + + 1: 1 + 3: 1,3 + 6: 1,2,3,6 +10: 1,2,5,10 +15: 1,3,5,15 +21: 1,3,7,21 +28: 1,2,4,7,14,28 +We can see that 28 is the first triangle number to have over five divisors. + +What is the value of the first triangle number to have over five hundred +divisors? +""" +from math import sqrt + + +def count_divisors(n): + nDivisors = 0 + for i in range(1, int(sqrt(n)) + 1): + if n % i == 0: + nDivisors += 2 + # check if n is perfect square + if n ** 0.5 == int(n ** 0.5): + nDivisors -= 1 + return nDivisors + + +def solution(): + """Returns the value of the first triangle number to have over five hundred + divisors. + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution() + # 76576500 + """ + tNum = 1 + i = 1 + + while True: + i += 1 + tNum += i + + if count_divisors(tNum) > 500: + break + + return tNum + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_012/sol2.py b/project_euler/problem_012/sol2.py new file mode 100644 index 000000000000..5ff0d8349b90 --- /dev/null +++ b/project_euler/problem_012/sol2.py @@ -0,0 +1,47 @@ +""" +Highly divisible triangular numbers +Problem 12 +The sequence of triangle numbers is generated by adding the natural numbers. So +the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten +terms would be: + +1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + +Let us list the factors of the first seven triangle numbers: + + 1: 1 + 3: 1,3 + 6: 1,2,3,6 +10: 1,2,5,10 +15: 1,3,5,15 +21: 1,3,7,21 +28: 1,2,4,7,14,28 +We can see that 28 is the first triangle number to have over five divisors. + +What is the value of the first triangle number to have over five hundred +divisors? +""" + + +def triangle_number_generator(): + for n in range(1, 1000000): + yield n * (n + 1) // 2 + + +def count_divisors(n): + return sum([2 for i in range(1, int(n ** 0.5) + 1) if n % i == 0 and i * i != n]) + + +def solution(): + """Returns the value of the first triangle number to have over five hundred + divisors. + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution() + # 76576500 + """ + return next(i for i in triangle_number_generator() if count_divisors(i) > 500) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_013/__init__.py b/project_euler/problem_013/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_013/num.txt b/project_euler/problem_013/num.txt new file mode 100644 index 000000000000..43b568e812a8 --- /dev/null +++ b/project_euler/problem_013/num.txt @@ -0,0 +1,100 @@ +37107287533902102798797998220837590246510135740250 +46376937677490009712648124896970078050417018260538 +74324986199524741059474233309513058123726617309629 +91942213363574161572522430563301811072406154908250 +23067588207539346171171980310421047513778063246676 +89261670696623633820136378418383684178734361726757 +28112879812849979408065481931592621691275889832738 +44274228917432520321923589422876796487670272189318 +47451445736001306439091167216856844588711603153276 +70386486105843025439939619828917593665686757934951 +62176457141856560629502157223196586755079324193331 +64906352462741904929101432445813822663347944758178 +92575867718337217661963751590579239728245598838407 +58203565325359399008402633568948830189458628227828 +80181199384826282014278194139940567587151170094390 +35398664372827112653829987240784473053190104293586 +86515506006295864861532075273371959191420517255829 +71693888707715466499115593487603532921714970056938 +54370070576826684624621495650076471787294438377604 +53282654108756828443191190634694037855217779295145 +36123272525000296071075082563815656710885258350721 +45876576172410976447339110607218265236877223636045 +17423706905851860660448207621209813287860733969412 +81142660418086830619328460811191061556940512689692 +51934325451728388641918047049293215058642563049483 +62467221648435076201727918039944693004732956340691 +15732444386908125794514089057706229429197107928209 +55037687525678773091862540744969844508330393682126 +18336384825330154686196124348767681297534375946515 +80386287592878490201521685554828717201219257766954 +78182833757993103614740356856449095527097864797581 +16726320100436897842553539920931837441497806860984 +48403098129077791799088218795327364475675590848030 +87086987551392711854517078544161852424320693150332 +59959406895756536782107074926966537676326235447210 +69793950679652694742597709739166693763042633987085 +41052684708299085211399427365734116182760315001271 +65378607361501080857009149939512557028198746004375 +35829035317434717326932123578154982629742552737307 +94953759765105305946966067683156574377167401875275 +88902802571733229619176668713819931811048770190271 +25267680276078003013678680992525463401061632866526 +36270218540497705585629946580636237993140746255962 +24074486908231174977792365466257246923322810917141 +91430288197103288597806669760892938638285025333403 +34413065578016127815921815005561868836468420090470 +23053081172816430487623791969842487255036638784583 +11487696932154902810424020138335124462181441773470 +63783299490636259666498587618221225225512486764533 +67720186971698544312419572409913959008952310058822 +95548255300263520781532296796249481641953868218774 +76085327132285723110424803456124867697064507995236 +37774242535411291684276865538926205024910326572967 +23701913275725675285653248258265463092207058596522 +29798860272258331913126375147341994889534765745501 +18495701454879288984856827726077713721403798879715 +38298203783031473527721580348144513491373226651381 +34829543829199918180278916522431027392251122869539 +40957953066405232632538044100059654939159879593635 +29746152185502371307642255121183693803580388584903 +41698116222072977186158236678424689157993532961922 +62467957194401269043877107275048102390895523597457 +23189706772547915061505504953922979530901129967519 +86188088225875314529584099251203829009407770775672 +11306739708304724483816533873502340845647058077308 +82959174767140363198008187129011875491310547126581 +97623331044818386269515456334926366572897563400500 +42846280183517070527831839425882145521227251250327 +55121603546981200581762165212827652751691296897789 +32238195734329339946437501907836945765883352399886 +75506164965184775180738168837861091527357929701337 +62177842752192623401942399639168044983993173312731 +32924185707147349566916674687634660915035914677504 +99518671430235219628894890102423325116913619626622 +73267460800591547471830798392868535206946944540724 +76841822524674417161514036427982273348055556214818 +97142617910342598647204516893989422179826088076852 +87783646182799346313767754307809363333018982642090 +10848802521674670883215120185883543223812876952786 +71329612474782464538636993009049310363619763878039 +62184073572399794223406235393808339651327408011116 +66627891981488087797941876876144230030984490851411 +60661826293682836764744779239180335110989069790714 +85786944089552990653640447425576083659976645795096 +66024396409905389607120198219976047599490197230297 +64913982680032973156037120041377903785566085089252 +16730939319872750275468906903707539413042652315011 +94809377245048795150954100921645863754710598436791 +78639167021187492431995700641917969777599028300699 +15368713711936614952811305876380278410754449733078 +40789923115535562561142322423255033685442488917353 +44889911501440648020369068063960672322193204149535 +41503128880339536053299340368006977710650566631954 +81234880673210146739058568557934581403627822703280 +82616570773948327592232845941706525094512325230608 +22918802058777319719839450180888072429661980811197 +77158542502016545090413245809786882778948721859617 +72107838435069186155435662884062257473692284509516 +20849603980134001723930671666823555245252804609722 +53503534226472524250874054075591789781264330331690 diff --git a/project_euler/problem_013/sol1.py b/project_euler/problem_013/sol1.py new file mode 100644 index 000000000000..1ea08b12ee93 --- /dev/null +++ b/project_euler/problem_013/sol1.py @@ -0,0 +1,25 @@ +""" +Problem 13: https://projecteuler.net/problem=13 + +Problem Statement: +Work out the first ten digits of the sum of the following one-hundred 50-digit +numbers. +""" +import os + + +def solution(): + """ + Returns the first ten digits of the sum of the array elements + from the file num.txt + + >>> solution() + '5537376230' + """ + file_path = os.path.join(os.path.dirname(__file__), "num.txt") + with open(file_path) as file_hand: + return str(sum([int(line) for line in file_hand]))[:10] + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_014/__init__.py b/project_euler/problem_014/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_014/sol1.py b/project_euler/problem_014/sol1.py new file mode 100644 index 000000000000..1745ec931e5a --- /dev/null +++ b/project_euler/problem_014/sol1.py @@ -0,0 +1,60 @@ +""" +Problem 14: https://projecteuler.net/problem=14 + +Problem Statement: +The following iterative sequence is defined for the set of positive integers: + + n → n/2 (n is even) + n → 3n + 1 (n is odd) + +Using the rule above and starting with 13, we generate the following sequence: + + 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 + +It can be seen that this sequence (starting at 13 and finishing at 1) contains +10 terms. Although it has not been proved yet (Collatz Problem), it is thought +that all starting numbers finish at 1. + +Which starting number, under one million, produces the longest chain? +""" + + +def solution(n: int = 1000000) -> int: + """Returns the number under n that generates the longest sequence using the + formula: + n → n/2 (n is even) + n → 3n + 1 (n is odd) + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution(1000000) + # 837799 + >>> solution(200) + 171 + >>> solution(5000) + 3711 + >>> solution(15000) + 13255 + """ + largest_number = 0 + pre_counter = 0 + + for input1 in range(n): + counter = 1 + number = input1 + + while number > 1: + if number % 2 == 0: + number /= 2 + counter += 1 + else: + number = (3 * number) + 1 + counter += 1 + + if counter > pre_counter: + largest_number = input1 + pre_counter = counter + return largest_number + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_014/sol2.py b/project_euler/problem_014/sol2.py new file mode 100644 index 000000000000..20ad96327498 --- /dev/null +++ b/project_euler/problem_014/sol2.py @@ -0,0 +1,62 @@ +""" +Problem 14: https://projecteuler.net/problem=14 + +Collatz conjecture: start with any positive integer n. Next term obtained from +the previous term as follows: + +If the previous term is even, the next term is one half the previous term. +If the previous term is odd, the next term is 3 times the previous term plus 1. +The conjecture states the sequence will always reach 1 regardless of starting +n. + +Problem Statement: +The following iterative sequence is defined for the set of positive integers: + + n → n/2 (n is even) + n → 3n + 1 (n is odd) + +Using the rule above and starting with 13, we generate the following sequence: + + 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 + +It can be seen that this sequence (starting at 13 and finishing at 1) contains +10 terms. Although it has not been proved yet (Collatz Problem), it is thought +that all starting numbers finish at 1. + +Which starting number, under one million, produces the longest chain? +""" +from typing import List + + +def collatz_sequence(n: int) -> List[int]: + """Returns the Collatz sequence for n.""" + sequence = [n] + while n != 1: + if n % 2 == 0: + n //= 2 + else: + n = 3 * n + 1 + sequence.append(n) + return sequence + + +def solution(n: int = 1000000) -> int: + """Returns the number under n that generates the longest Collatz sequence. + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution(1000000) + # 837799 + >>> solution(200) + 171 + >>> solution(5000) + 3711 + >>> solution(15000) + 13255 + """ + + result = max([(len(collatz_sequence(i)), i) for i in range(1, n)]) + return result[1] + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_015/__init__.py b/project_euler/problem_015/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_015/sol1.py b/project_euler/problem_015/sol1.py new file mode 100644 index 000000000000..da079d26120a --- /dev/null +++ b/project_euler/problem_015/sol1.py @@ -0,0 +1,44 @@ +""" +Problem 15: https://projecteuler.net/problem=15 + +Starting in the top left corner of a 2×2 grid, and only being able to move to +the right and down, there are exactly 6 routes to the bottom right corner. +How many such routes are there through a 20×20 grid? +""" +from math import factorial + + +def solution(n: int = 20) -> int: + """ + Returns the number of paths possible in a n x n grid starting at top left + corner going to bottom right corner and being able to move right and down + only. + >>> solution(25) + 126410606437752 + >>> solution(23) + 8233430727600 + >>> solution(20) + 137846528820 + >>> solution(15) + 155117520 + >>> solution(1) + 2 + """ + n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, + # 2, 3,... + k = n / 2 + + return int(factorial(n) / (factorial(k) * factorial(n - k))) + + +if __name__ == "__main__": + import sys + + if len(sys.argv) == 1: + print(solution(20)) + else: + try: + n = int(sys.argv[1]) + print(solution(n)) + except ValueError: + print("Invalid entry - please enter a number.") diff --git a/project_euler/problem_016/__init__.py b/project_euler/problem_016/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_016/sol1.py b/project_euler/problem_016/sol1.py new file mode 100644 index 000000000000..f6620aa9482f --- /dev/null +++ b/project_euler/problem_016/sol1.py @@ -0,0 +1,36 @@ +""" +Problem 16: https://projecteuler.net/problem=16 + +2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + +What is the sum of the digits of the number 2^1000? +""" + + +def solution(power: int = 1000) -> int: + """Returns the sum of the digits of the number 2^power. + >>> solution(1000) + 1366 + >>> solution(50) + 76 + >>> solution(20) + 31 + >>> solution(15) + 26 + """ + num = 2 ** power + string_num = str(num) + list_num = list(string_num) + sum_of_num = 0 + + for i in list_num: + sum_of_num += int(i) + + return sum_of_num + + +if __name__ == "__main__": + power = int(input("Enter the power of 2: ").strip()) + print("2 ^ ", power, " = ", 2 ** power) + result = solution(power) + print("Sum of the digits is: ", result) diff --git a/project_euler/problem_016/sol2.py b/project_euler/problem_016/sol2.py new file mode 100644 index 000000000000..304d27d1e5d0 --- /dev/null +++ b/project_euler/problem_016/sol2.py @@ -0,0 +1,30 @@ +""" +Problem 16: https://projecteuler.net/problem=16 + +2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + +What is the sum of the digits of the number 2^1000? +""" + + +def solution(power: int = 1000) -> int: + """Returns the sum of the digits of the number 2^power. + + >>> solution(1000) + 1366 + >>> solution(50) + 76 + >>> solution(20) + 31 + >>> solution(15) + 26 + """ + n = 2 ** power + r = 0 + while n: + r, n = r + n % 10, n // 10 + return r + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_017/__init__.py b/project_euler/problem_017/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_017/sol1.py b/project_euler/problem_017/sol1.py new file mode 100644 index 000000000000..d4db1beb5a4e --- /dev/null +++ b/project_euler/problem_017/sol1.py @@ -0,0 +1,63 @@ +""" +Number letter counts +Problem 17: https://projecteuler.net/problem=17 + +If the numbers 1 to 5 are written out in words: one, two, three, four, five, +then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. + +If all the numbers from 1 to 1000 (one thousand) inclusive were written out in +words, how many letters would be used? + + +NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and +forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 +letters. The use of "and" when writing out numbers is in compliance withBritish +usage. +""" + + +def solution(n: int = 1000) -> int: + """Returns the number of letters used to write all numbers from 1 to n. + where n is lower or equals to 1000. + >>> solution(1000) + 21124 + >>> solution(5) + 19 + """ + # number of letters in zero, one, two, ..., nineteen (0 for zero since it's + # never said aloud) + ones_counts = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8] + # number of letters in twenty, thirty, ..., ninety (0 for numbers less than + # 20 due to inconsistency in teens) + tens_counts = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6] + + count = 0 + + for i in range(1, n + 1): + if i < 1000: + if i >= 100: + # add number of letters for "n hundred" + count += ones_counts[i // 100] + 7 + + if i % 100 != 0: + # add number of letters for "and" if number is not multiple + # of 100 + count += 3 + + if 0 < i % 100 < 20: + # add number of letters for one, two, three, ..., nineteen + # (could be combined with below if not for inconsistency in + # teens) + count += ones_counts[i % 100] + else: + # add number of letters for twenty, twenty one, ..., ninety + # nine + count += ones_counts[i % 10] + count += tens_counts[(i % 100 - i % 10) // 10] + else: + count += ones_counts[i // 1000] + 8 + return count + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_018/__init__.py b/project_euler/problem_018/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_018/solution.py b/project_euler/problem_018/solution.py new file mode 100644 index 000000000000..82fc3ce3c9db --- /dev/null +++ b/project_euler/problem_018/solution.py @@ -0,0 +1,64 @@ +""" +By starting at the top of the triangle below and moving to adjacent numbers on +the row below, the maximum total from top to bottom is 23. + +3 +7 4 +2 4 6 +8 5 9 3 + +That is, 3 + 7 + 4 + 9 = 23. + +Find the maximum total from top to bottom of the triangle below: + +75 +95 64 +17 47 82 +18 35 87 10 +20 04 82 47 65 +19 01 23 75 03 34 +88 02 77 73 07 63 67 +99 65 04 28 06 16 70 92 +41 41 26 56 83 40 80 70 33 +41 48 72 33 47 32 37 16 94 29 +53 71 44 65 25 43 91 52 97 51 14 +70 11 33 28 77 73 17 78 39 68 17 57 +91 71 52 38 17 14 91 43 58 50 27 29 48 +63 66 04 68 89 53 67 30 73 16 69 87 40 31 +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 +""" +import os + + +def solution(): + """ + Finds the maximum total in a triangle as described by the problem statement + above. + + >>> solution() + 1074 + """ + script_dir = os.path.dirname(os.path.realpath(__file__)) + triangle = os.path.join(script_dir, "triangle.txt") + + with open(triangle) as f: + triangle = f.readlines() + + a = [[int(y) for y in x.rstrip("\r\n").split(" ")] for x in triangle] + + for i in range(1, len(a)): + for j in range(len(a[i])): + if j != len(a[i - 1]): + number1 = a[i - 1][j] + else: + number1 = 0 + if j > 0: + number2 = a[i - 1][j - 1] + else: + number2 = 0 + a[i][j] += max(number1, number2) + return max(a[-1]) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_018/triangle.txt b/project_euler/problem_018/triangle.txt new file mode 100644 index 000000000000..e236c2ff7ee2 --- /dev/null +++ b/project_euler/problem_018/triangle.txt @@ -0,0 +1,15 @@ +75 +95 64 +17 47 82 +18 35 87 10 +20 04 82 47 65 +19 01 23 75 03 34 +88 02 77 73 07 63 67 +99 65 04 28 06 16 70 92 +41 41 26 56 83 40 80 70 33 +41 48 72 33 47 32 37 16 94 29 +53 71 44 65 25 43 91 52 97 51 14 +70 11 33 28 77 73 17 78 39 68 17 57 +91 71 52 38 17 14 91 43 58 50 27 29 48 +63 66 04 68 89 53 67 30 73 16 69 87 40 31 +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 diff --git a/project_euler/problem_019/__init__.py b/project_euler/problem_019/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_019/sol1.py b/project_euler/problem_019/sol1.py new file mode 100644 index 000000000000..ab59365843b2 --- /dev/null +++ b/project_euler/problem_019/sol1.py @@ -0,0 +1,64 @@ +""" +Counting Sundays +Problem 19 + +You are given the following information, but you may prefer to do some research +for yourself. + +1 Jan 1900 was a Monday. +Thirty days has September, +April, June and November. +All the rest have thirty-one, +Saving February alone, +Which has twenty-eight, rain or shine. +And on leap years, twenty-nine. + +A leap year occurs on any year evenly divisible by 4, but not on a century +unless it is divisible by 400. + +How many Sundays fell on the first of the month during the twentieth century +(1 Jan 1901 to 31 Dec 2000)? +""" + + +def solution(): + """Returns the number of mondays that fall on the first of the month during + the twentieth century (1 Jan 1901 to 31 Dec 2000)? + + >>> solution() + 171 + """ + days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + + day = 6 + month = 1 + year = 1901 + + sundays = 0 + + while year < 2001: + day += 7 + + if (year % 4 == 0 and not year % 100 == 0) or (year % 400 == 0): + if day > days_per_month[month - 1] and month != 2: + month += 1 + day = day - days_per_month[month - 2] + elif day > 29 and month == 2: + month += 1 + day = day - 29 + else: + if day > days_per_month[month - 1]: + month += 1 + day = day - days_per_month[month - 2] + + if month > 12: + year += 1 + month = 1 + + if year < 2001 and day == 1: + sundays += 1 + return sundays + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_02/sol1.py b/project_euler/problem_02/sol1.py deleted file mode 100644 index 44ea980f2df0..000000000000 --- a/project_euler/problem_02/sol1.py +++ /dev/null @@ -1,24 +0,0 @@ -''' -Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, -the first 10 terms will be: - 1,2,3,5,8,13,21,34,55,89,.. -By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. -e.g. for n=10, we have {2,8}, sum is 10. -''' -from __future__ import print_function - -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 - -n = int(raw_input().strip()) -i=1 -j=2 -sum=0 -while(j<=n): - if j%2 == 0: - sum+=j - i , j = j, i+j -print(sum) diff --git a/project_euler/problem_02/sol2.py b/project_euler/problem_02/sol2.py deleted file mode 100644 index a2772697bb79..000000000000 --- a/project_euler/problem_02/sol2.py +++ /dev/null @@ -1,15 +0,0 @@ -def fib(n): - """ - Returns a list of all the even terms in the Fibonacci sequence that are less than n. - """ - ls = [] - a, b = 0, 1 - while b < n: - if b % 2 == 0: - ls.append(b) - a, b = b, a+b - return ls - -if __name__ == '__main__': - n = int(input("Enter max number: ").strip()) - print(sum(fib(n))) diff --git a/project_euler/problem_02/sol3.py b/project_euler/problem_02/sol3.py deleted file mode 100644 index 0eb46d879704..000000000000 --- a/project_euler/problem_02/sol3.py +++ /dev/null @@ -1,18 +0,0 @@ -''' -Problem: -Each new term in the Fibonacci sequence is generated by adding the previous two terms. - 0,1,1,2,3,5,8,13,21,34,55,89,.. -Every third term from 0 is even So using this I have written a simple code -By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. -e.g. for n=10, we have {2,8}, sum is 10. -''' -"""Python 3""" -n = int(input()) -a=0 -b=2 -count=0 -while 4*b+a int: + """Find the factorial of a given number n""" + fact = 1 + for i in range(1, num + 1): + fact *= i + return fact + + +def split_and_add(number: int) -> int: + """Split number digits and add them.""" + sum_of_digits = 0 + while number > 0: + last_digit = number % 10 + sum_of_digits += last_digit + number = number // 10 # Removing the last_digit from the given number + return sum_of_digits + + +def solution(num: int = 100) -> int: + """Returns the sum of the digits in the factorial of num + >>> solution(100) + 648 + >>> solution(50) + 216 + >>> solution(10) + 27 + >>> solution(5) + 3 + >>> solution(3) + 6 + >>> solution(2) + 2 + >>> solution(1) + 1 + """ + nfact = factorial(num) + result = split_and_add(nfact) + return result + + +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_020/sol2.py b/project_euler/problem_020/sol2.py new file mode 100644 index 000000000000..92e1e724a647 --- /dev/null +++ b/project_euler/problem_020/sol2.py @@ -0,0 +1,35 @@ +""" +Problem 20: https://projecteuler.net/problem=20 + +n! means n × (n − 1) × ... × 3 × 2 × 1 + +For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + +Find the sum of the digits in the number 100! +""" +from math import factorial + + +def solution(num: int = 100) -> int: + """Returns the sum of the digits in the factorial of num + >>> solution(100) + 648 + >>> solution(50) + 216 + >>> solution(10) + 27 + >>> solution(5) + 3 + >>> solution(3) + 6 + >>> solution(2) + 2 + >>> solution(1) + 1 + """ + return sum([int(x) for x in str(factorial(num))]) + + +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_020/sol3.py b/project_euler/problem_020/sol3.py new file mode 100644 index 000000000000..4f28ac5fcfde --- /dev/null +++ b/project_euler/problem_020/sol3.py @@ -0,0 +1,41 @@ +""" +Problem 20: https://projecteuler.net/problem=20 + +n! means n × (n − 1) × ... × 3 × 2 × 1 + +For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + +Find the sum of the digits in the number 100! +""" +from math import factorial + + +def solution(num: int = 100) -> int: + """Returns the sum of the digits in the factorial of num + >>> solution(1000) + 10539 + >>> solution(200) + 1404 + >>> solution(100) + 648 + >>> solution(50) + 216 + >>> solution(10) + 27 + >>> solution(5) + 3 + >>> solution(3) + 6 + >>> solution(2) + 2 + >>> solution(1) + 1 + >>> solution(0) + 1 + """ + return sum(map(int, str(factorial(num)))) + + +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_020/sol4.py b/project_euler/problem_020/sol4.py new file mode 100644 index 000000000000..b32ce309dfa6 --- /dev/null +++ b/project_euler/problem_020/sol4.py @@ -0,0 +1,42 @@ +""" +Problem 20: https://projecteuler.net/problem=20 + +n! means n × (n − 1) × ... × 3 × 2 × 1 + +For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + +Find the sum of the digits in the number 100! +""" + + +def solution(num: int = 100) -> int: + """Returns the sum of the digits in the factorial of num + >>> solution(100) + 648 + >>> solution(50) + 216 + >>> solution(10) + 27 + >>> solution(5) + 3 + >>> solution(3) + 6 + >>> solution(2) + 2 + >>> solution(1) + 1 + """ + fact = 1 + result = 0 + for i in range(1, num + 1): + fact *= i + + for j in str(fact): + result += int(j) + + return result + + +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_021/__init__.py b/project_euler/problem_021/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_021/sol1.py b/project_euler/problem_021/sol1.py new file mode 100644 index 000000000000..3fac79156e41 --- /dev/null +++ b/project_euler/problem_021/sol1.py @@ -0,0 +1,54 @@ +""" +Amicable Numbers +Problem 21 + +Let d(n) be defined as the sum of proper divisors of n (numbers less than n +which divide evenly into n). +If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and +each of a and b are called amicable numbers. + +For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 +and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and +142; so d(284) = 220. + +Evaluate the sum of all the amicable numbers under 10000. +""" +from math import sqrt + + +def sum_of_divisors(n: int) -> int: + total = 0 + for i in range(1, int(sqrt(n) + 1)): + if n % i == 0 and i != sqrt(n): + total += i + n // i + elif i == sqrt(n): + total += i + return total - n + + +def solution(n: int = 10000) -> int: + """Returns the sum of all the amicable numbers under n. + + >>> solution(10000) + 31626 + >>> solution(5000) + 8442 + >>> solution(1000) + 504 + >>> solution(100) + 0 + >>> solution(50) + 0 + """ + total = sum( + [ + i + for i in range(1, n) + if sum_of_divisors(sum_of_divisors(i)) == i and sum_of_divisors(i) != i + ] + ) + return total + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_022/__init__.py b/project_euler/problem_022/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_22/p022_names.txt b/project_euler/problem_022/p022_names.txt similarity index 100% rename from project_euler/problem_22/p022_names.txt rename to project_euler/problem_022/p022_names.txt diff --git a/project_euler/problem_022/sol1.py b/project_euler/problem_022/sol1.py new file mode 100644 index 000000000000..982906245e87 --- /dev/null +++ b/project_euler/problem_022/sol1.py @@ -0,0 +1,45 @@ +""" +Name scores +Problem 22 + +Using names.txt (right click and 'Save Link/Target As...'), a 46K text file +containing over five-thousand first names, begin by sorting it into +alphabetical order. Then working out the alphabetical value for each name, +multiply this value by its alphabetical position in the list to obtain a name +score. + +For example, when the list is sorted into alphabetical order, COLIN, which is +worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would +obtain a score of 938 × 53 = 49714. + +What is the total of all the name scores in the file? +""" +import os + + +def solution(): + """Returns the total of all the name scores in the file. + + >>> solution() + 871198282 + """ + with open(os.path.dirname(__file__) + "/p022_names.txt") as file: + names = str(file.readlines()[0]) + names = names.replace('"', "").split(",") + + names.sort() + + name_score = 0 + total_score = 0 + + for i, name in enumerate(names): + for letter in name: + name_score += ord(letter) - 64 + + total_score += (i + 1) * name_score + name_score = 0 + return total_score + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_022/sol2.py b/project_euler/problem_022/sol2.py new file mode 100644 index 000000000000..5ae41c84686e --- /dev/null +++ b/project_euler/problem_022/sol2.py @@ -0,0 +1,42 @@ +""" +Name scores +Problem 22 + +Using names.txt (right click and 'Save Link/Target As...'), a 46K text file +containing over five-thousand first names, begin by sorting it into +alphabetical order. Then working out the alphabetical value for each name, +multiply this value by its alphabetical position in the list to obtain a name +score. + +For example, when the list is sorted into alphabetical order, COLIN, which is +worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would +obtain a score of 938 × 53 = 49714. + +What is the total of all the name scores in the file? +""" +import os + + +def solution(): + """Returns the total of all the name scores in the file. + + >>> solution() + 871198282 + """ + total_sum = 0 + temp_sum = 0 + with open(os.path.dirname(__file__) + "/p022_names.txt") as file: + name = str(file.readlines()[0]) + name = name.replace('"', "").split(",") + + name.sort() + for i in range(len(name)): + for j in name[i]: + temp_sum += ord(j) - ord("A") + 1 + total_sum += (i + 1) * temp_sum + temp_sum = 0 + return total_sum + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_023/__init__.py b/project_euler/problem_023/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_023/sol1.py b/project_euler/problem_023/sol1.py new file mode 100644 index 000000000000..a72b6123e3ee --- /dev/null +++ b/project_euler/problem_023/sol1.py @@ -0,0 +1,52 @@ +""" +A perfect number is a number for which the sum of its proper divisors is exactly +equal to the number. For example, the sum of the proper divisors of 28 would be +1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. + +A number n is called deficient if the sum of its proper divisors is less than n +and it is called abundant if this sum exceeds n. + +As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest +number that can be written as the sum of two abundant numbers is 24. By +mathematical analysis, it can be shown that all integers greater than 28123 +can be written as the sum of two abundant numbers. However, this upper limit +cannot be reduced any further by analysis even though it is known that the +greatest number that cannot be expressed as the sum of two abundant numbers +is less than this limit. + +Find the sum of all the positive integers which cannot be written as the sum +of two abundant numbers. +""" + + +def solution(limit=28123): + """ + Finds the sum of all the positive integers which cannot be written as + the sum of two abundant numbers + as described by the statement above. + + >>> solution() + 4179871 + """ + sumDivs = [1] * (limit + 1) + + for i in range(2, int(limit ** 0.5) + 1): + sumDivs[i * i] += i + for k in range(i + 1, limit // i + 1): + sumDivs[k * i] += k + i + + abundants = set() + res = 0 + + for n in range(1, limit + 1): + if sumDivs[n] > n: + abundants.add(n) + + if not any((n - a in abundants) for a in abundants): + res += n + + return res + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_024/__init__.py b/project_euler/problem_024/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_024/sol1.py b/project_euler/problem_024/sol1.py new file mode 100644 index 000000000000..1c6378b38260 --- /dev/null +++ b/project_euler/problem_024/sol1.py @@ -0,0 +1,27 @@ +""" +A permutation is an ordered arrangement of objects. For example, 3124 is one +possible permutation of the digits 1, 2, 3 and 4. If all of the permutations +are listed numerically or alphabetically, we call it lexicographic order. The +lexicographic permutations of 0, 1 and 2 are: + + 012 021 102 120 201 210 + +What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, +6, 7, 8 and 9? +""" +from itertools import permutations + + +def solution(): + """Returns the millionth lexicographic permutation of the digits 0, 1, 2, + 3, 4, 5, 6, 7, 8 and 9. + + >>> solution() + '2783915460' + """ + result = list(map("".join, permutations("0123456789"))) + return result[999999] + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_025/__init__.py b/project_euler/problem_025/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_025/sol1.py b/project_euler/problem_025/sol1.py new file mode 100644 index 000000000000..c30a74a43cb0 --- /dev/null +++ b/project_euler/problem_025/sol1.py @@ -0,0 +1,101 @@ +""" +The Fibonacci sequence is defined by the recurrence relation: + + Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. + +Hence the first 12 terms will be: + + F1 = 1 + F2 = 1 + F3 = 2 + F4 = 3 + F5 = 5 + F6 = 8 + F7 = 13 + F8 = 21 + F9 = 34 + F10 = 55 + F11 = 89 + F12 = 144 + +The 12th term, F12, is the first term to contain three digits. + +What is the index of the first term in the Fibonacci sequence to contain 1000 +digits? +""" + + +def fibonacci(n: int) -> int: + """ + Computes the Fibonacci number for input n by iterating through n numbers + and creating an array of ints using the Fibonacci formula. + Returns the nth element of the array. + + >>> fibonacci(2) + 1 + >>> fibonacci(3) + 2 + >>> fibonacci(5) + 5 + >>> fibonacci(10) + 55 + >>> fibonacci(12) + 144 + + """ + if n == 1 or type(n) is not int: + return 0 + elif n == 2: + return 1 + else: + sequence = [0, 1] + for i in range(2, n + 1): + sequence.append(sequence[i - 1] + sequence[i - 2]) + + return sequence[n] + + +def fibonacci_digits_index(n: int) -> int: + """ + Computes incrementing Fibonacci numbers starting from 3 until the length + of the resulting Fibonacci result is the input value n. Returns the term + of the Fibonacci sequence where this occurs. + + >>> fibonacci_digits_index(1000) + 4782 + >>> fibonacci_digits_index(100) + 476 + >>> fibonacci_digits_index(50) + 237 + >>> fibonacci_digits_index(3) + 12 + """ + digits = 0 + index = 2 + + while digits < n: + index += 1 + digits = len(str(fibonacci(index))) + + return index + + +def solution(n: int = 1000) -> int: + """ + Returns the index of the first term in the Fibonacci sequence to contain + n digits. + + >>> solution(1000) + 4782 + >>> solution(100) + 476 + >>> solution(50) + 237 + >>> solution(3) + 12 + """ + return fibonacci_digits_index(n) + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_025/sol2.py b/project_euler/problem_025/sol2.py new file mode 100644 index 000000000000..ed3b54bb351f --- /dev/null +++ b/project_euler/problem_025/sol2.py @@ -0,0 +1,71 @@ +""" +The Fibonacci sequence is defined by the recurrence relation: + + Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. + +Hence the first 12 terms will be: + + F1 = 1 + F2 = 1 + F3 = 2 + F4 = 3 + F5 = 5 + F6 = 8 + F7 = 13 + F8 = 21 + F9 = 34 + F10 = 55 + F11 = 89 + F12 = 144 + +The 12th term, F12, is the first term to contain three digits. + +What is the index of the first term in the Fibonacci sequence to contain 1000 +digits? +""" + + +def fibonacci_generator() -> int: + """ + A generator that produces numbers in the Fibonacci sequence + + >>> generator = fibonacci_generator() + >>> next(generator) + 1 + >>> next(generator) + 2 + >>> next(generator) + 3 + >>> next(generator) + 5 + >>> next(generator) + 8 + """ + a, b = 0, 1 + while True: + a, b = b, a + b + yield b + + +def solution(n: int = 1000) -> int: + """Returns the index of the first term in the Fibonacci sequence to contain + n digits. + + >>> solution(1000) + 4782 + >>> solution(100) + 476 + >>> solution(50) + 237 + >>> solution(3) + 12 + """ + answer = 1 + gen = fibonacci_generator() + while len(str(next(gen))) < n: + answer += 1 + return answer + 1 + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_025/sol3.py b/project_euler/problem_025/sol3.py new file mode 100644 index 000000000000..c66411dc55fc --- /dev/null +++ b/project_euler/problem_025/sol3.py @@ -0,0 +1,56 @@ +""" +The Fibonacci sequence is defined by the recurrence relation: + + Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. + +Hence the first 12 terms will be: + + F1 = 1 + F2 = 1 + F3 = 2 + F4 = 3 + F5 = 5 + F6 = 8 + F7 = 13 + F8 = 21 + F9 = 34 + F10 = 55 + F11 = 89 + F12 = 144 + +The 12th term, F12, is the first term to contain three digits. + +What is the index of the first term in the Fibonacci sequence to contain 1000 +digits? +""" + + +def solution(n: int = 1000) -> int: + """Returns the index of the first term in the Fibonacci sequence to contain + n digits. + + >>> solution(1000) + 4782 + >>> solution(100) + 476 + >>> solution(50) + 237 + >>> solution(3) + 12 + """ + f1, f2 = 1, 1 + index = 2 + while True: + i = 0 + f = f1 + f2 + f1, f2 = f2, f + index += 1 + for j in str(f): + i += 1 + if i == n: + break + return index + + +if __name__ == "__main__": + print(solution(int(str(input()).strip()))) diff --git a/project_euler/problem_026/__init__.py b/project_euler/problem_026/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_026/sol1.py b/project_euler/problem_026/sol1.py new file mode 100644 index 000000000000..64e0bbfef472 --- /dev/null +++ b/project_euler/problem_026/sol1.py @@ -0,0 +1,60 @@ +""" +Euler Problem 26 +https://projecteuler.net/problem=26 + +Problem Statement: + +A unit fraction contains 1 in the numerator. The decimal representation of the +unit fractions with denominators 2 to 10 are given: + +1/2 = 0.5 +1/3 = 0.(3) +1/4 = 0.25 +1/5 = 0.2 +1/6 = 0.1(6) +1/7 = 0.(142857) +1/8 = 0.125 +1/9 = 0.(1) +1/10 = 0.1 +Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be +seen that 1/7 has a 6-digit recurring cycle. + +Find the value of d < 1000 for which 1/d contains the longest recurring cycle +in its decimal fraction part. +""" + + +def solution(numerator: int = 1, digit: int = 1000) -> int: + """ + Considering any range can be provided, + because as per the problem, the digit d < 1000 + >>> solution(1, 10) + 7 + >>> solution(10, 100) + 97 + >>> solution(10, 1000) + 983 + """ + the_digit = 1 + longest_list_length = 0 + + for divide_by_number in range(numerator, digit + 1): + has_been_divided = [] + now_divide = numerator + for division_cycle in range(1, digit + 1): + if now_divide in has_been_divided: + if longest_list_length < len(has_been_divided): + longest_list_length = len(has_been_divided) + the_digit = divide_by_number + else: + has_been_divided.append(now_divide) + now_divide = now_divide * 10 % divide_by_number + + return the_digit + + +# Tests +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/project_euler/problem_027/__init__.py b/project_euler/problem_027/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_027/sol1.py b/project_euler/problem_027/sol1.py new file mode 100644 index 000000000000..6f28b925be08 --- /dev/null +++ b/project_euler/problem_027/sol1.py @@ -0,0 +1,74 @@ +""" +Project Euler Problem 27 +https://projecteuler.net/problem=27 + +Problem Statement: + +Euler discovered the remarkable quadratic formula: +n2 + n + 41 +It turns out that the formula will produce 40 primes for the consecutive values +n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible +by 41, and certainly when n = 41, 412 + 41 + 41 is clearly divisible by 41. +The incredible formula n2 − 79n + 1601 was discovered, which produces 80 primes +for the consecutive values n = 0 to 79. The product of the coefficients, −79 and +1601, is −126479. +Considering quadratics of the form: +n² + an + b, where |a| < 1000 and |b| < 1000 +where |n| is the modulus/absolute value of ne.g. |11| = 11 and |−4| = 4 +Find the product of the coefficients, a and b, for the quadratic expression that +produces the maximum number of primes for consecutive values of n, starting with +n = 0. +""" + +import math + + +def is_prime(k: int) -> bool: + """ + Determine if a number is prime + >>> is_prime(10) + False + >>> is_prime(11) + True + """ + if k < 2 or k % 2 == 0: + return False + elif k == 2: + return True + else: + for x in range(3, int(math.sqrt(k) + 1), 2): + if k % x == 0: + return False + return True + + +def solution(a_limit: int = 1000, b_limit: int = 1000) -> int: + """ + >>> solution(1000, 1000) + -59231 + >>> solution(200, 1000) + -59231 + >>> solution(200, 200) + -4925 + >>> solution(-1000, 1000) + 0 + >>> solution(-1000, -1000) + 0 + """ + longest = [0, 0, 0] # length, a, b + for a in range((a_limit * -1) + 1, a_limit): + for b in range(2, b_limit): + if is_prime(b): + count = 0 + n = 0 + while is_prime((n ** 2) + (a * n) + b): + count += 1 + n += 1 + if count > longest[0]: + longest = [count, a, b] + ans = longest[1] * longest[2] + return ans + + +if __name__ == "__main__": + print(solution(1000, 1000)) diff --git a/project_euler/problem_028/__init__.py b/project_euler/problem_028/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_028/sol1.py b/project_euler/problem_028/sol1.py new file mode 100644 index 000000000000..cbc7de6bea9a --- /dev/null +++ b/project_euler/problem_028/sol1.py @@ -0,0 +1,58 @@ +""" +Problem 28 +Url: https://projecteuler.net/problem=28 +Statement: +Starting with the number 1 and moving to the right in a clockwise direction a 5 +by 5 spiral is formed as follows: + + 21 22 23 24 25 + 20 7 8 9 10 + 19 6 1 2 11 + 18 5 4 3 12 + 17 16 15 14 13 + +It can be verified that the sum of the numbers on the diagonals is 101. + +What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed +in the same way? +""" + +from math import ceil + + +def solution(n: int = 1001) -> int: + """Returns the sum of the numbers on the diagonals in a n by n spiral + formed in the same way. + + >>> solution(1001) + 669171001 + >>> solution(500) + 82959497 + >>> solution(100) + 651897 + >>> solution(50) + 79697 + >>> solution(10) + 537 + """ + total = 1 + + for i in range(1, int(ceil(n / 2.0))): + odd = 2 * i + 1 + even = 2 * i + total = total + 4 * odd ** 2 - 6 * even + + return total + + +if __name__ == "__main__": + import sys + + if len(sys.argv) == 1: + print(solution()) + else: + try: + n = int(sys.argv[1]) + print(solution(n)) + except ValueError: + print("Invalid entry - please enter a number") diff --git a/project_euler/problem_029/__init__.py b/project_euler/problem_029/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_029/sol1.py b/project_euler/problem_029/sol1.py new file mode 100644 index 000000000000..726bcaf6ebd8 --- /dev/null +++ b/project_euler/problem_029/sol1.py @@ -0,0 +1,50 @@ +""" +Consider all integer combinations of ab for 2 <= a <= 5 and 2 <= b <= 5: + +2^2=4, 2^3=8, 2^4=16, 2^5=32 +3^2=9, 3^3=27, 3^4=81, 3^5=243 +4^2=16, 4^3=64, 4^4=256, 4^5=1024 +5^2=25, 5^3=125, 5^4=625, 5^5=3125 + +If they are then placed in numerical order, with any repeats removed, we get +the following sequence of 15 distinct terms: + +4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 + +How many distinct terms are in the sequence generated by ab +for 2 <= a <= 100 and 2 <= b <= 100? +""" + + +def solution(n: int = 100) -> int: + """Returns the number of distinct terms in the sequence generated by a^b + for 2 <= a <= 100 and 2 <= b <= 100. + + >>> solution(100) + 9183 + >>> solution(50) + 2184 + >>> solution(20) + 324 + >>> solution(5) + 15 + >>> solution(2) + 1 + >>> solution(1) + 0 + """ + collectPowers = set() + + currentPow = 0 + + N = n + 1 # maximum limit + + for a in range(2, N): + for b in range(2, N): + currentPow = a ** b # calculates the current power + collectPowers.add(currentPow) # adds the result to the set + return len(collectPowers) + + +if __name__ == "__main__": + print("Number of terms ", solution(int(str(input()).strip()))) diff --git a/project_euler/problem_03/sol1.py b/project_euler/problem_03/sol1.py deleted file mode 100644 index bb9f8ca9ad12..000000000000 --- a/project_euler/problem_03/sol1.py +++ /dev/null @@ -1,39 +0,0 @@ -''' -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? -e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. -''' -from __future__ import print_function, division - -import math - -def isprime(no): - if(no==2): - return True - elif (no%2==0): - return False - sq = int(math.sqrt(no))+1 - for i in range(3,sq,2): - if(no%i==0): - return False - return True - -maxNumber = 0 -n=int(input()) -if(isprime(n)): - print(n) -else: - while (n%2==0): - n=n/2 - if(isprime(n)): - print(n) - else: - n1 = int(math.sqrt(n))+1 - for i in range(3,n1,2): - if(n%i==0): - if(isprime(n/i)): - maxNumber = n/i - break - elif(isprime(i)): - maxNumber = i - print(maxNumber) diff --git a/project_euler/problem_03/sol2.py b/project_euler/problem_03/sol2.py deleted file mode 100644 index 44f9c63dfb6a..000000000000 --- a/project_euler/problem_03/sol2.py +++ /dev/null @@ -1,18 +0,0 @@ -''' -Problem: -The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? -e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. -''' - -from __future__ import print_function -n=int(input()) -prime=1 -i=2 -while(i*i<=n): - while(n%i==0): - prime=i - n//=i - i+=1 -if(n>1): - prime=n -print(prime) diff --git a/project_euler/problem_030/__init__.py b/project_euler/problem_030/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_030/sol1.py b/project_euler/problem_030/sol1.py new file mode 100644 index 000000000000..c9f2d71965e3 --- /dev/null +++ b/project_euler/problem_030/sol1.py @@ -0,0 +1,39 @@ +""" Problem Statement (Digit Fifth Power ): https://projecteuler.net/problem=30 + +Surprisingly there are only three numbers that can be written as the sum of fourth +powers of their digits: + +1634 = 1^4 + 6^4 + 3^4 + 4^4 +8208 = 8^4 + 2^4 + 0^4 + 8^4 +9474 = 9^4 + 4^4 + 7^4 + 4^4 +As 1 = 1^4 is not a sum it is not included. + +The sum of these numbers is 1634 + 8208 + 9474 = 19316. + +Find the sum of all the numbers that can be written as the sum of fifth powers of their +digits. + +(9^5)=59,049‬ +59049*7=4,13,343 (which is only 6 digit number ) +So, number greater than 9,99,999 are rejected +and also 59049*3=1,77,147 (which exceeds the criteria of number being 3 digit) +So, n>999 +and hence a bound between (1000,1000000) +""" + + +def digitsum(s: str) -> int: + """ + >>> all(digitsum(str(i)) == (1 if i == 1 else 0) for i in range(100)) + True + """ + i = sum(pow(int(c), 5) for c in s) + return i if i == int(s) else 0 + + +def solution() -> int: + return sum(digitsum(str(i)) for i in range(1000, 1000000)) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_031/__init__.py b/project_euler/problem_031/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_31/sol1.py b/project_euler/problem_031/sol1.py similarity index 55% rename from project_euler/problem_31/sol1.py rename to project_euler/problem_031/sol1.py index 33653722f890..ba40cf383175 100644 --- a/project_euler/problem_31/sol1.py +++ b/project_euler/problem_031/sol1.py @@ -1,12 +1,7 @@ -# -*- coding: utf-8 -*- -from __future__ import print_function -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 -''' +""" Coin sums -Problem 31 +Problem 31: https://projecteuler.net/problem=31 + In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: @@ -15,39 +10,56 @@ 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p How many different ways can £2 be made using any number of coins? -''' +""" -def one_pence(): +def one_pence() -> int: return 1 -def two_pence(x): +def two_pence(x: int) -> int: return 0 if x < 0 else two_pence(x - 2) + one_pence() -def five_pence(x): +def five_pence(x: int) -> int: return 0 if x < 0 else five_pence(x - 5) + two_pence(x) -def ten_pence(x): +def ten_pence(x: int) -> int: return 0 if x < 0 else ten_pence(x - 10) + five_pence(x) -def twenty_pence(x): +def twenty_pence(x: int) -> int: return 0 if x < 0 else twenty_pence(x - 20) + ten_pence(x) -def fifty_pence(x): +def fifty_pence(x: int) -> int: return 0 if x < 0 else fifty_pence(x - 50) + twenty_pence(x) -def one_pound(x): +def one_pound(x: int) -> int: return 0 if x < 0 else one_pound(x - 100) + fifty_pence(x) -def two_pound(x): +def two_pound(x: int) -> int: return 0 if x < 0 else two_pound(x - 200) + one_pound(x) -print(two_pound(200)) +def solution(n: int = 200) -> int: + """Returns the number of different ways can n pence be made using any number of + coins? + + >>> solution(500) + 6295434 + >>> solution(200) + 73682 + >>> solution(50) + 451 + >>> solution(10) + 11 + """ + return two_pound(n) + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_031/sol2.py b/project_euler/problem_031/sol2.py new file mode 100644 index 000000000000..f9e4dc384bff --- /dev/null +++ b/project_euler/problem_031/sol2.py @@ -0,0 +1,59 @@ +""" +Problem 31: https://projecteuler.net/problem=31 + +Coin sums + +In England the currency is made up of pound, £, and pence, p, and there are +eight coins in general circulation: + +1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). +It is possible to make £2 in the following way: + +1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p +How many different ways can £2 be made using any number of coins? + +Hint: + > There are 100 pence in a pound (£1 = 100p) + > There are coins(in pence) are available: 1, 2, 5, 10, 20, 50, 100 and 200. + > how many different ways you can combine these values to create 200 pence. + +Example: + to make 6p there are 5 ways + 1,1,1,1,1,1 + 1,1,1,1,2 + 1,1,2,2 + 2,2,2 + 1,5 + to make 5p there are 4 ways + 1,1,1,1,1 + 1,1,1,2 + 1,2,2 + 5 +""" + + +def solution(pence: int = 200) -> int: + """Returns the number of different ways to make X pence using any number of coins. + The solution is based on dynamic programming paradigm in a bottom-up fashion. + + >>> solution(500) + 6295434 + >>> solution(200) + 73682 + >>> solution(50) + 451 + >>> solution(10) + 11 + """ + coins = [1, 2, 5, 10, 20, 50, 100, 200] + number_of_ways = [0] * (pence + 1) + number_of_ways[0] = 1 # base case: 1 way to make 0 pence + + for coin in coins: + for i in range(coin, pence + 1, 1): + number_of_ways[i] += number_of_ways[i - coin] + return number_of_ways[pence] + + +if __name__ == "__main__": + assert solution(200) == 73682 diff --git a/project_euler/problem_032/__init__.py b/project_euler/problem_032/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_032/sol32.py b/project_euler/problem_032/sol32.py new file mode 100644 index 000000000000..393218339e9f --- /dev/null +++ b/project_euler/problem_032/sol32.py @@ -0,0 +1,58 @@ +""" +We shall say that an n-digit number is pandigital if it makes use of all the +digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through +5 pandigital. + +The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing +multiplicand, multiplier, and product is 1 through 9 pandigital. + +Find the sum of all products whose multiplicand/multiplier/product identity can +be written as a 1 through 9 pandigital. + +HINT: Some products can be obtained in more than one way so be sure to only +include it once in your sum. +""" +import itertools + + +def isCombinationValid(combination): + """ + Checks if a combination (a tuple of 9 digits) + is a valid product equation. + + >>> isCombinationValid(('3', '9', '1', '8', '6', '7', '2', '5', '4')) + True + + >>> isCombinationValid(('1', '2', '3', '4', '5', '6', '7', '8', '9')) + False + + """ + return ( + int("".join(combination[0:2])) * int("".join(combination[2:5])) + == int("".join(combination[5:9])) + ) or ( + int("".join(combination[0])) * int("".join(combination[1:5])) + == int("".join(combination[5:9])) + ) + + +def solution(): + """ + Finds the sum of all products whose multiplicand/multiplier/product identity + can be written as a 1 through 9 pandigital + + >>> solution() + 45228 + """ + + return sum( + { + int("".join(pandigital[5:9])) + for pandigital in itertools.permutations("123456789") + if isCombinationValid(pandigital) + } + ) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_033/__init__.py b/project_euler/problem_033/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_033/sol1.py b/project_euler/problem_033/sol1.py new file mode 100644 index 000000000000..ba6e553d8689 --- /dev/null +++ b/project_euler/problem_033/sol1.py @@ -0,0 +1,68 @@ +""" +Problem 33: https://projecteuler.net/problem=33 + +The fraction 49/98 is a curious fraction, as an inexperienced +mathematician in attempting to simplify it may incorrectly believe +that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s. + +We shall consider fractions like, 30/50 = 3/5, to be trivial examples. + +There are exactly four non-trivial examples of this type of fraction, +less than one in value, and containing two digits in the numerator +and denominator. + +If the product of these four fractions is given in its lowest common +terms, find the value of the denominator. +""" +from fractions import Fraction +from typing import List + + +def is_digit_cancelling(num: int, den: int) -> bool: + if num != den: + if num % 10 == den // 10: + if (num // 10) / (den % 10) == num / den: + return True + return False + + +def fraction_list(digit_len: int) -> List[str]: + """ + >>> fraction_list(2) + ['16/64', '19/95', '26/65', '49/98'] + >>> fraction_list(3) + ['16/64', '19/95', '26/65', '49/98'] + >>> fraction_list(4) + ['16/64', '19/95', '26/65', '49/98'] + >>> fraction_list(0) + [] + >>> fraction_list(5) + ['16/64', '19/95', '26/65', '49/98'] + """ + solutions = [] + den = 11 + last_digit = int("1" + "0" * digit_len) + for num in range(den, last_digit): + while den <= 99: + if (num != den) and (num % 10 == den // 10) and (den % 10 != 0): + if is_digit_cancelling(num, den): + solutions.append(f"{num}/{den}") + den += 1 + num += 1 + den = 10 + return solutions + + +def solution(n: int = 2) -> int: + """ + Return the solution to the problem + """ + result = 1.0 + for fraction in fraction_list(n): + frac = Fraction(fraction) + result *= frac.denominator / frac.numerator + return int(result) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_034/__init__.py b/project_euler/problem_034/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_034/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_034/sol1.py b/project_euler/problem_034/sol1.py new file mode 100644 index 000000000000..78b318b76d06 --- /dev/null +++ b/project_euler/problem_034/sol1.py @@ -0,0 +1,36 @@ +""" +Problem 34: https://projecteuler.net/problem=34 + +145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. +Find the sum of all numbers which are equal to the sum of the factorial of their digits. +Note: As 1! = 1 and 2! = 2 are not sums they are not included. +""" + +from math import factorial + + +def sum_of_digit_factorial(n: int) -> int: + """ + Returns the sum of the digits in n + >>> sum_of_digit_factorial(15) + 121 + >>> sum_of_digit_factorial(0) + 1 + """ + return sum(factorial(int(char)) for char in str(n)) + + +def solution() -> int: + """ + Returns the sum of all numbers whose + sum of the factorials of all digits + add up to the number itself. + >>> solution() + 40730 + """ + limit = 7 * factorial(9) + 1 + return sum(i for i in range(3, limit) if sum_of_digit_factorial(i) == i) + + +if __name__ == "__main__": + print(f"{solution()} = ") diff --git a/project_euler/problem_035/__init__.py b/project_euler/problem_035/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_035/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_035/sol1.py b/project_euler/problem_035/sol1.py new file mode 100644 index 000000000000..17a4e9088ae2 --- /dev/null +++ b/project_euler/problem_035/sol1.py @@ -0,0 +1,82 @@ +""" +Project Euler Problem 35 +https://projecteuler.net/problem=35 + +Problem Statement: + +The number 197 is called a circular prime because all rotations of the digits: +197, 971, and 719, are themselves prime. +There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, +79, and 97. +How many circular primes are there below one million? + +To solve this problem in an efficient manner, we will first mark all the primes +below 1 million using the Seive of Eratosthenes. Then, out of all these primes, +we will rule out the numbers which contain an even digit. After this we will +generate each circular combination of the number and check if all are prime. +""" +from __future__ import annotations + +seive = [True] * 1000001 +i = 2 +while i * i <= 1000000: + if seive[i]: + for j in range(i * i, 1000001, i): + seive[j] = False + i += 1 + + +def is_prime(n: int) -> bool: + """ + For 2 <= n <= 1000000, return True if n is prime. + >>> is_prime(87) + False + >>> is_prime(23) + True + >>> is_prime(25363) + False + """ + return seive[n] + + +def contains_an_even_digit(n: int) -> bool: + """ + Return True if n contains an even digit. + >>> contains_an_even_digit(0) + True + >>> contains_an_even_digit(975317933) + False + >>> contains_an_even_digit(-245679) + True + """ + return any(digit in "02468" for digit in str(n)) + + +def find_circular_primes(limit: int = 1000000) -> list[int]: + """ + Return circular primes below limit. + >>> len(find_circular_primes(100)) + 13 + >>> len(find_circular_primes(1000000)) + 55 + """ + result = [2] # result already includes the number 2. + for num in range(3, limit + 1, 2): + if is_prime(num) and not contains_an_even_digit(num): + str_num = str(num) + list_nums = [int(str_num[j:] + str_num[:j]) for j in range(len(str_num))] + if all(is_prime(i) for i in list_nums): + result.append(num) + return result + + +def solution() -> int: + """ + >>> solution() + 55 + """ + return len(find_circular_primes()) + + +if __name__ == "__main__": + print(f"{len(find_circular_primes()) = }") diff --git a/project_euler/problem_036/__init__.py b/project_euler/problem_036/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_036/sol1.py b/project_euler/problem_036/sol1.py new file mode 100644 index 000000000000..13a749862e5f --- /dev/null +++ b/project_euler/problem_036/sol1.py @@ -0,0 +1,69 @@ +""" +Project Euler Problem 36 +https://projecteuler.net/problem=36 + +Problem Statement: + +Double-base palindromes +Problem 36 +The decimal number, 585 = 10010010012 (binary), is palindromic in both bases. + +Find the sum of all numbers, less than one million, which are palindromic in +base 10 and base 2. + +(Please note that the palindromic number, in either base, may not include +leading zeros.) +""" + +from typing import Union + + +def is_palindrome(n: Union[int, str]) -> bool: + """ + Return true if the input n is a palindrome. + Otherwise return false. n can be an integer or a string. + + >>> is_palindrome(909) + True + >>> is_palindrome(908) + False + >>> is_palindrome('10101') + True + >>> is_palindrome('10111') + False + """ + n = str(n) + return True if n == n[::-1] else False + + +def solution(n: int = 1000000): + """Return the sum of all numbers, less than n , which are palindromic in + base 10 and base 2. + + >>> solution(1000000) + 872187 + >>> solution(500000) + 286602 + >>> solution(100000) + 286602 + >>> solution(1000) + 1772 + >>> solution(100) + 157 + >>> solution(10) + 25 + >>> solution(2) + 1 + >>> solution(1) + 0 + """ + total = 0 + + for i in range(1, n): + if is_palindrome(i) and is_palindrome(bin(i).split("b")[1]): + total += i + return total + + +if __name__ == "__main__": + print(solution(int(str(input().strip())))) diff --git a/project_euler/problem_037/__init__.py b/project_euler/problem_037/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_037/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_037/sol1.py b/project_euler/problem_037/sol1.py new file mode 100644 index 000000000000..5423aac37c01 --- /dev/null +++ b/project_euler/problem_037/sol1.py @@ -0,0 +1,98 @@ +""" +The number 3797 has an interesting property. Being prime itself, it is possible +to continuously remove digits from left to right, and remain prime at each stage: +3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. + +Find the sum of the only eleven primes that are both truncatable from left to right +and right to left. + +NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. +""" + +from __future__ import annotations + +seive = [True] * 1000001 +seive[1] = False +i = 2 +while i * i <= 1000000: + if seive[i]: + for j in range(i * i, 1000001, i): + seive[j] = False + i += 1 + + +def is_prime(n: int) -> bool: + """ + Returns True if n is prime, + False otherwise, for 1 <= n <= 1000000 + >>> is_prime(87) + False + >>> is_prime(1) + False + >>> is_prime(25363) + False + """ + return seive[n] + + +def list_truncated_nums(n: int) -> list[int]: + """ + Returns a list of all left and right truncated numbers of n + >>> list_truncated_nums(927628) + [927628, 27628, 92762, 7628, 9276, 628, 927, 28, 92, 8, 9] + >>> list_truncated_nums(467) + [467, 67, 46, 7, 4] + >>> list_truncated_nums(58) + [58, 8, 5] + """ + str_num = str(n) + list_nums = [n] + for i in range(1, len(str_num)): + list_nums.append(int(str_num[i:])) + list_nums.append(int(str_num[:-i])) + return list_nums + + +def validate(n: int) -> bool: + """ + To optimize the approach, we will rule out the numbers above 1000, + whose first or last three digits are not prime + >>> validate(74679) + False + >>> validate(235693) + False + >>> validate(3797) + True + """ + if len(str(n)) > 3: + if not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3])): + return False + return True + + +def compute_truncated_primes(count: int = 11) -> list[int]: + """ + Returns the list of truncated primes + >>> compute_truncated_primes(11) + [23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397] + """ + list_truncated_primes = [] + num = 13 + while len(list_truncated_primes) != count: + if validate(num): + list_nums = list_truncated_nums(num) + if all(is_prime(i) for i in list_nums): + list_truncated_primes.append(num) + num += 2 + return list_truncated_primes + + +def solution() -> int: + """ + Returns the sum of truncated primes + """ + return sum(compute_truncated_primes(11)) + + +if __name__ == "__main__": + print(f"{sum(compute_truncated_primes(11)) = }") diff --git a/project_euler/problem_038/__init__.py b/project_euler/problem_038/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_038/sol1.py b/project_euler/problem_038/sol1.py new file mode 100644 index 000000000000..6d54f6df7ff8 --- /dev/null +++ b/project_euler/problem_038/sol1.py @@ -0,0 +1,77 @@ +""" +Project Euler Problem 38: https://projecteuler.net/problem=38 + +Take the number 192 and multiply it by each of 1, 2, and 3: + +192 × 1 = 192 +192 × 2 = 384 +192 × 3 = 576 + +By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call +192384576 the concatenated product of 192 and (1,2,3) + +The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, +giving the pandigital, 918273645, which is the concatenated product of 9 and +(1,2,3,4,5). + +What is the largest 1 to 9 pandigital 9-digit number that can be formed as the +concatenated product of an integer with (1,2, ... , n) where n > 1? + +Solution: +Since n>1, the largest candidate for the solution will be a concactenation of +a 4-digit number and its double, a 5-digit number. +Let a be the 4-digit number. +a has 4 digits => 1000 <= a < 10000 +2a has 5 digits => 10000 <= 2a < 100000 +=> 5000 <= a < 10000 + +The concatenation of a with 2a = a * 10^5 + 2a +so our candidate for a given a is 100002 * a. +We iterate through the search space 5000 <= a < 10000 in reverse order, +calculating the candidates for each a and checking if they are 1-9 pandigital. + +In case there are no 4-digit numbers that satisfy this property, we check +the 3-digit numbers with a similar formula (the example a=192 gives a lower +bound on the length of a): +a has 3 digits, etc... +=> 100 <= a < 334, candidate = a * 10^6 + 2a * 10^3 + 3a + = 1002003 * a +""" + +from typing import Union + + +def is_9_pandigital(n: int) -> bool: + """ + Checks whether n is a 9-digit 1 to 9 pandigital number. + >>> is_9_pandigital(12345) + False + >>> is_9_pandigital(156284973) + True + >>> is_9_pandigital(1562849733) + False + """ + s = str(n) + return len(s) == 9 and set(s) == set("123456789") + + +def solution() -> Union[int, None]: + """ + Return the largest 1 to 9 pandigital 9-digital number that can be formed as the + concatenated product of an integer with (1,2,...,n) where n > 1. + """ + for base_num in range(9999, 4999, -1): + candidate = 100002 * base_num + if is_9_pandigital(candidate): + return candidate + + for base_num in range(333, 99, -1): + candidate = 1002003 * base_num + if is_9_pandigital(candidate): + return candidate + + return None + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_039/__init__.py b/project_euler/problem_039/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_039/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_039/sol1.py b/project_euler/problem_039/sol1.py new file mode 100644 index 000000000000..c8ffa8934159 --- /dev/null +++ b/project_euler/problem_039/sol1.py @@ -0,0 +1,55 @@ +""" +Problem 39: https://projecteuler.net/problem=39 + +If p is the perimeter of a right angle triangle with integral length sides, +{a,b,c}, there are exactly three solutions for p = 120. +{20,48,52}, {24,45,51}, {30,40,50} + +For which value of p ≤ 1000, is the number of solutions maximised? +""" + +from __future__ import annotations + +import typing +from collections import Counter + + +def pythagorean_triple(max_perimeter: int) -> typing.Counter[int]: + """ + Returns a dictionary with keys as the perimeter of a right angled triangle + and value as the number of corresponding triplets. + >>> pythagorean_triple(15) + Counter({12: 1}) + >>> pythagorean_triple(40) + Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1}) + >>> pythagorean_triple(50) + Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1}) + """ + triplets: typing.Counter[int] = Counter() + for base in range(1, max_perimeter + 1): + for perpendicular in range(base, max_perimeter + 1): + hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5 + if hypotenuse == int(hypotenuse): + perimeter = int(base + perpendicular + hypotenuse) + if perimeter > max_perimeter: + continue + triplets[perimeter] += 1 + return triplets + + +def solution(n: int = 1000) -> int: + """ + Returns perimeter with maximum solutions. + >>> solution(100) + 90 + >>> solution(200) + 180 + >>> solution(1000) + 840 + """ + triplets = pythagorean_triple(n) + return triplets.most_common(1)[0][0] + + +if __name__ == "__main__": + print(f"Perimeter {solution()} has maximum solutions") diff --git a/project_euler/problem_04/sol1.py b/project_euler/problem_04/sol1.py deleted file mode 100644 index 05fdd9ebab55..000000000000 --- a/project_euler/problem_04/sol1.py +++ /dev/null @@ -1,29 +0,0 @@ -''' -Problem: -A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99. -Find the largest palindrome made from the product of two 3-digit numbers which is less than N. -''' -from __future__ import print_function -limit = int(input("limit? ")) - -# fetchs the next number -for number in range(limit-1,10000,-1): - - # converts number into string. - strNumber = str(number) - - # checks whether 'strNumber' is a palindrome. - if(strNumber == strNumber[::-1]): - - divisor = 999 - - # if 'number' is a product of two 3-digit numbers - # then number is the answer otherwise fetch next number. - while(divisor != 99): - - if((number % divisor == 0) and (len(str(number / divisor)) == 3)): - - print(number) - exit(0) - - divisor -=1 diff --git a/project_euler/problem_04/sol2.py b/project_euler/problem_04/sol2.py deleted file mode 100644 index 70810c38986f..000000000000 --- a/project_euler/problem_04/sol2.py +++ /dev/null @@ -1,17 +0,0 @@ -''' -Problem: -A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99. -Find the largest palindrome made from the product of two 3-digit numbers which is less than N. -''' -from __future__ import print_function -n = int(input().strip()) -answer = 0 -for i in range(999,99,-1): #3 digit nimbers range from 999 down to 100 - for j in range(999,99,-1): - t = str(i*j) - if t == t[::-1] and i*j < n: - answer = max(answer,i*j) -print(answer) -exit(0) - - diff --git a/project_euler/problem_040/__init__.py b/project_euler/problem_040/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_040/sol1.py b/project_euler/problem_040/sol1.py new file mode 100644 index 000000000000..69be377723a5 --- /dev/null +++ b/project_euler/problem_040/sol1.py @@ -0,0 +1,45 @@ +""" +Champernowne's constant +Problem 40 +An irrational decimal fraction is created by concatenating the positive +integers: + +0.123456789101112131415161718192021... + +It can be seen that the 12th digit of the fractional part is 1. + +If dn represents the nth digit of the fractional part, find the value of the +following expression. + +d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000 +""" + + +def solution(): + """Returns + + >>> solution() + 210 + """ + constant = [] + i = 1 + + while len(constant) < 1e6: + constant.append(str(i)) + i += 1 + + constant = "".join(constant) + + return ( + int(constant[0]) + * int(constant[9]) + * int(constant[99]) + * int(constant[999]) + * int(constant[9999]) + * int(constant[99999]) + * int(constant[999999]) + ) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_041/__init__.py b/project_euler/problem_041/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_041/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_041/sol1.py b/project_euler/problem_041/sol1.py new file mode 100644 index 000000000000..80ef2125b82a --- /dev/null +++ b/project_euler/problem_041/sol1.py @@ -0,0 +1,56 @@ +""" +Pandigital prime +Problem 41: https://projecteuler.net/problem=41 + +We shall say that an n-digit number is pandigital if it makes use of all the digits +1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. +What is the largest n-digit pandigital prime that exists? + +All pandigital numbers except for 1, 4 ,7 pandigital numbers are divisible by 3. +So we will check only 7 digit pandigital numbers to obtain the largest possible +pandigital prime. +""" +from __future__ import annotations + +from itertools import permutations +from math import sqrt + + +def is_prime(n: int) -> bool: + """ + Returns True if n is prime, + False otherwise. + >>> is_prime(67483) + False + >>> is_prime(563) + True + >>> is_prime(87) + False + """ + if n % 2 == 0: + return False + for i in range(3, int(sqrt(n) + 1), 2): + if n % i == 0: + return False + return True + + +def solution(n: int = 7) -> int: + """ + Returns the maximum pandigital prime number of length n. + If there are none, then it will return 0. + >>> solution(2) + 0 + >>> solution(4) + 4231 + >>> solution(7) + 7652413 + """ + pandigital_str = "".join(str(i) for i in range(1, n + 1)) + perm_list = [int("".join(i)) for i in permutations(pandigital_str, n)] + pandigitals = [num for num in perm_list if is_prime(num)] + return max(pandigitals) if pandigitals else 0 + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_042/__init__.py b/project_euler/problem_042/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_042/solution42.py b/project_euler/problem_042/solution42.py new file mode 100644 index 000000000000..b3aecf4cf144 --- /dev/null +++ b/project_euler/problem_042/solution42.py @@ -0,0 +1,47 @@ +""" +The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so +the first ten triangle numbers are: + +1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + +By converting each letter in a word to a number corresponding to its +alphabetical position and adding these values we form a word value. For example, +the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a +triangle number then we shall call the word a triangle word. + +Using words.txt (right click and 'Save Link/Target As...'), a 16K text file +containing nearly two-thousand common English words, how many are triangle +words? +""" +import os + +# Precomputes a list of the 100 first triangular numbers +TRIANGULAR_NUMBERS = [int(0.5 * n * (n + 1)) for n in range(1, 101)] + + +def solution(): + """ + Finds the amount of triangular words in the words file. + + >>> solution() + 162 + """ + script_dir = os.path.dirname(os.path.realpath(__file__)) + wordsFilePath = os.path.join(script_dir, "words.txt") + + words = "" + with open(wordsFilePath) as f: + words = f.readline() + + words = list(map(lambda word: word.strip('"'), words.strip("\r\n").split(","))) + words = list( + filter( + lambda word: word in TRIANGULAR_NUMBERS, + map(lambda word: sum(map(lambda x: ord(x) - 64, word)), words), + ) + ) + return len(words) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_042/words.txt b/project_euler/problem_042/words.txt new file mode 100644 index 000000000000..af3aeb42f151 --- /dev/null +++ b/project_euler/problem_042/words.txt @@ -0,0 +1 @@ +"A","ABILITY","ABLE","ABOUT","ABOVE","ABSENCE","ABSOLUTELY","ACADEMIC","ACCEPT","ACCESS","ACCIDENT","ACCOMPANY","ACCORDING","ACCOUNT","ACHIEVE","ACHIEVEMENT","ACID","ACQUIRE","ACROSS","ACT","ACTION","ACTIVE","ACTIVITY","ACTUAL","ACTUALLY","ADD","ADDITION","ADDITIONAL","ADDRESS","ADMINISTRATION","ADMIT","ADOPT","ADULT","ADVANCE","ADVANTAGE","ADVICE","ADVISE","AFFAIR","AFFECT","AFFORD","AFRAID","AFTER","AFTERNOON","AFTERWARDS","AGAIN","AGAINST","AGE","AGENCY","AGENT","AGO","AGREE","AGREEMENT","AHEAD","AID","AIM","AIR","AIRCRAFT","ALL","ALLOW","ALMOST","ALONE","ALONG","ALREADY","ALRIGHT","ALSO","ALTERNATIVE","ALTHOUGH","ALWAYS","AMONG","AMONGST","AMOUNT","AN","ANALYSIS","ANCIENT","AND","ANIMAL","ANNOUNCE","ANNUAL","ANOTHER","ANSWER","ANY","ANYBODY","ANYONE","ANYTHING","ANYWAY","APART","APPARENT","APPARENTLY","APPEAL","APPEAR","APPEARANCE","APPLICATION","APPLY","APPOINT","APPOINTMENT","APPROACH","APPROPRIATE","APPROVE","AREA","ARGUE","ARGUMENT","ARISE","ARM","ARMY","AROUND","ARRANGE","ARRANGEMENT","ARRIVE","ART","ARTICLE","ARTIST","AS","ASK","ASPECT","ASSEMBLY","ASSESS","ASSESSMENT","ASSET","ASSOCIATE","ASSOCIATION","ASSUME","ASSUMPTION","AT","ATMOSPHERE","ATTACH","ATTACK","ATTEMPT","ATTEND","ATTENTION","ATTITUDE","ATTRACT","ATTRACTIVE","AUDIENCE","AUTHOR","AUTHORITY","AVAILABLE","AVERAGE","AVOID","AWARD","AWARE","AWAY","AYE","BABY","BACK","BACKGROUND","BAD","BAG","BALANCE","BALL","BAND","BANK","BAR","BASE","BASIC","BASIS","BATTLE","BE","BEAR","BEAT","BEAUTIFUL","BECAUSE","BECOME","BED","BEDROOM","BEFORE","BEGIN","BEGINNING","BEHAVIOUR","BEHIND","BELIEF","BELIEVE","BELONG","BELOW","BENEATH","BENEFIT","BESIDE","BEST","BETTER","BETWEEN","BEYOND","BIG","BILL","BIND","BIRD","BIRTH","BIT","BLACK","BLOCK","BLOOD","BLOODY","BLOW","BLUE","BOARD","BOAT","BODY","BONE","BOOK","BORDER","BOTH","BOTTLE","BOTTOM","BOX","BOY","BRAIN","BRANCH","BREAK","BREATH","BRIDGE","BRIEF","BRIGHT","BRING","BROAD","BROTHER","BUDGET","BUILD","BUILDING","BURN","BUS","BUSINESS","BUSY","BUT","BUY","BY","CABINET","CALL","CAMPAIGN","CAN","CANDIDATE","CAPABLE","CAPACITY","CAPITAL","CAR","CARD","CARE","CAREER","CAREFUL","CAREFULLY","CARRY","CASE","CASH","CAT","CATCH","CATEGORY","CAUSE","CELL","CENTRAL","CENTRE","CENTURY","CERTAIN","CERTAINLY","CHAIN","CHAIR","CHAIRMAN","CHALLENGE","CHANCE","CHANGE","CHANNEL","CHAPTER","CHARACTER","CHARACTERISTIC","CHARGE","CHEAP","CHECK","CHEMICAL","CHIEF","CHILD","CHOICE","CHOOSE","CHURCH","CIRCLE","CIRCUMSTANCE","CITIZEN","CITY","CIVIL","CLAIM","CLASS","CLEAN","CLEAR","CLEARLY","CLIENT","CLIMB","CLOSE","CLOSELY","CLOTHES","CLUB","COAL","CODE","COFFEE","COLD","COLLEAGUE","COLLECT","COLLECTION","COLLEGE","COLOUR","COMBINATION","COMBINE","COME","COMMENT","COMMERCIAL","COMMISSION","COMMIT","COMMITMENT","COMMITTEE","COMMON","COMMUNICATION","COMMUNITY","COMPANY","COMPARE","COMPARISON","COMPETITION","COMPLETE","COMPLETELY","COMPLEX","COMPONENT","COMPUTER","CONCENTRATE","CONCENTRATION","CONCEPT","CONCERN","CONCERNED","CONCLUDE","CONCLUSION","CONDITION","CONDUCT","CONFERENCE","CONFIDENCE","CONFIRM","CONFLICT","CONGRESS","CONNECT","CONNECTION","CONSEQUENCE","CONSERVATIVE","CONSIDER","CONSIDERABLE","CONSIDERATION","CONSIST","CONSTANT","CONSTRUCTION","CONSUMER","CONTACT","CONTAIN","CONTENT","CONTEXT","CONTINUE","CONTRACT","CONTRAST","CONTRIBUTE","CONTRIBUTION","CONTROL","CONVENTION","CONVERSATION","COPY","CORNER","CORPORATE","CORRECT","COS","COST","COULD","COUNCIL","COUNT","COUNTRY","COUNTY","COUPLE","COURSE","COURT","COVER","CREATE","CREATION","CREDIT","CRIME","CRIMINAL","CRISIS","CRITERION","CRITICAL","CRITICISM","CROSS","CROWD","CRY","CULTURAL","CULTURE","CUP","CURRENT","CURRENTLY","CURRICULUM","CUSTOMER","CUT","DAMAGE","DANGER","DANGEROUS","DARK","DATA","DATE","DAUGHTER","DAY","DEAD","DEAL","DEATH","DEBATE","DEBT","DECADE","DECIDE","DECISION","DECLARE","DEEP","DEFENCE","DEFENDANT","DEFINE","DEFINITION","DEGREE","DELIVER","DEMAND","DEMOCRATIC","DEMONSTRATE","DENY","DEPARTMENT","DEPEND","DEPUTY","DERIVE","DESCRIBE","DESCRIPTION","DESIGN","DESIRE","DESK","DESPITE","DESTROY","DETAIL","DETAILED","DETERMINE","DEVELOP","DEVELOPMENT","DEVICE","DIE","DIFFERENCE","DIFFERENT","DIFFICULT","DIFFICULTY","DINNER","DIRECT","DIRECTION","DIRECTLY","DIRECTOR","DISAPPEAR","DISCIPLINE","DISCOVER","DISCUSS","DISCUSSION","DISEASE","DISPLAY","DISTANCE","DISTINCTION","DISTRIBUTION","DISTRICT","DIVIDE","DIVISION","DO","DOCTOR","DOCUMENT","DOG","DOMESTIC","DOOR","DOUBLE","DOUBT","DOWN","DRAW","DRAWING","DREAM","DRESS","DRINK","DRIVE","DRIVER","DROP","DRUG","DRY","DUE","DURING","DUTY","EACH","EAR","EARLY","EARN","EARTH","EASILY","EAST","EASY","EAT","ECONOMIC","ECONOMY","EDGE","EDITOR","EDUCATION","EDUCATIONAL","EFFECT","EFFECTIVE","EFFECTIVELY","EFFORT","EGG","EITHER","ELDERLY","ELECTION","ELEMENT","ELSE","ELSEWHERE","EMERGE","EMPHASIS","EMPLOY","EMPLOYEE","EMPLOYER","EMPLOYMENT","EMPTY","ENABLE","ENCOURAGE","END","ENEMY","ENERGY","ENGINE","ENGINEERING","ENJOY","ENOUGH","ENSURE","ENTER","ENTERPRISE","ENTIRE","ENTIRELY","ENTITLE","ENTRY","ENVIRONMENT","ENVIRONMENTAL","EQUAL","EQUALLY","EQUIPMENT","ERROR","ESCAPE","ESPECIALLY","ESSENTIAL","ESTABLISH","ESTABLISHMENT","ESTATE","ESTIMATE","EVEN","EVENING","EVENT","EVENTUALLY","EVER","EVERY","EVERYBODY","EVERYONE","EVERYTHING","EVIDENCE","EXACTLY","EXAMINATION","EXAMINE","EXAMPLE","EXCELLENT","EXCEPT","EXCHANGE","EXECUTIVE","EXERCISE","EXHIBITION","EXIST","EXISTENCE","EXISTING","EXPECT","EXPECTATION","EXPENDITURE","EXPENSE","EXPENSIVE","EXPERIENCE","EXPERIMENT","EXPERT","EXPLAIN","EXPLANATION","EXPLORE","EXPRESS","EXPRESSION","EXTEND","EXTENT","EXTERNAL","EXTRA","EXTREMELY","EYE","FACE","FACILITY","FACT","FACTOR","FACTORY","FAIL","FAILURE","FAIR","FAIRLY","FAITH","FALL","FAMILIAR","FAMILY","FAMOUS","FAR","FARM","FARMER","FASHION","FAST","FATHER","FAVOUR","FEAR","FEATURE","FEE","FEEL","FEELING","FEMALE","FEW","FIELD","FIGHT","FIGURE","FILE","FILL","FILM","FINAL","FINALLY","FINANCE","FINANCIAL","FIND","FINDING","FINE","FINGER","FINISH","FIRE","FIRM","FIRST","FISH","FIT","FIX","FLAT","FLIGHT","FLOOR","FLOW","FLOWER","FLY","FOCUS","FOLLOW","FOLLOWING","FOOD","FOOT","FOOTBALL","FOR","FORCE","FOREIGN","FOREST","FORGET","FORM","FORMAL","FORMER","FORWARD","FOUNDATION","FREE","FREEDOM","FREQUENTLY","FRESH","FRIEND","FROM","FRONT","FRUIT","FUEL","FULL","FULLY","FUNCTION","FUND","FUNNY","FURTHER","FUTURE","GAIN","GAME","GARDEN","GAS","GATE","GATHER","GENERAL","GENERALLY","GENERATE","GENERATION","GENTLEMAN","GET","GIRL","GIVE","GLASS","GO","GOAL","GOD","GOLD","GOOD","GOVERNMENT","GRANT","GREAT","GREEN","GREY","GROUND","GROUP","GROW","GROWING","GROWTH","GUEST","GUIDE","GUN","HAIR","HALF","HALL","HAND","HANDLE","HANG","HAPPEN","HAPPY","HARD","HARDLY","HATE","HAVE","HE","HEAD","HEALTH","HEAR","HEART","HEAT","HEAVY","HELL","HELP","HENCE","HER","HERE","HERSELF","HIDE","HIGH","HIGHLY","HILL","HIM","HIMSELF","HIS","HISTORICAL","HISTORY","HIT","HOLD","HOLE","HOLIDAY","HOME","HOPE","HORSE","HOSPITAL","HOT","HOTEL","HOUR","HOUSE","HOUSEHOLD","HOUSING","HOW","HOWEVER","HUGE","HUMAN","HURT","HUSBAND","I","IDEA","IDENTIFY","IF","IGNORE","ILLUSTRATE","IMAGE","IMAGINE","IMMEDIATE","IMMEDIATELY","IMPACT","IMPLICATION","IMPLY","IMPORTANCE","IMPORTANT","IMPOSE","IMPOSSIBLE","IMPRESSION","IMPROVE","IMPROVEMENT","IN","INCIDENT","INCLUDE","INCLUDING","INCOME","INCREASE","INCREASED","INCREASINGLY","INDEED","INDEPENDENT","INDEX","INDICATE","INDIVIDUAL","INDUSTRIAL","INDUSTRY","INFLUENCE","INFORM","INFORMATION","INITIAL","INITIATIVE","INJURY","INSIDE","INSIST","INSTANCE","INSTEAD","INSTITUTE","INSTITUTION","INSTRUCTION","INSTRUMENT","INSURANCE","INTEND","INTENTION","INTEREST","INTERESTED","INTERESTING","INTERNAL","INTERNATIONAL","INTERPRETATION","INTERVIEW","INTO","INTRODUCE","INTRODUCTION","INVESTIGATE","INVESTIGATION","INVESTMENT","INVITE","INVOLVE","IRON","IS","ISLAND","ISSUE","IT","ITEM","ITS","ITSELF","JOB","JOIN","JOINT","JOURNEY","JUDGE","JUMP","JUST","JUSTICE","KEEP","KEY","KID","KILL","KIND","KING","KITCHEN","KNEE","KNOW","KNOWLEDGE","LABOUR","LACK","LADY","LAND","LANGUAGE","LARGE","LARGELY","LAST","LATE","LATER","LATTER","LAUGH","LAUNCH","LAW","LAWYER","LAY","LEAD","LEADER","LEADERSHIP","LEADING","LEAF","LEAGUE","LEAN","LEARN","LEAST","LEAVE","LEFT","LEG","LEGAL","LEGISLATION","LENGTH","LESS","LET","LETTER","LEVEL","LIABILITY","LIBERAL","LIBRARY","LIE","LIFE","LIFT","LIGHT","LIKE","LIKELY","LIMIT","LIMITED","LINE","LINK","LIP","LIST","LISTEN","LITERATURE","LITTLE","LIVE","LIVING","LOAN","LOCAL","LOCATION","LONG","LOOK","LORD","LOSE","LOSS","LOT","LOVE","LOVELY","LOW","LUNCH","MACHINE","MAGAZINE","MAIN","MAINLY","MAINTAIN","MAJOR","MAJORITY","MAKE","MALE","MAN","MANAGE","MANAGEMENT","MANAGER","MANNER","MANY","MAP","MARK","MARKET","MARRIAGE","MARRIED","MARRY","MASS","MASTER","MATCH","MATERIAL","MATTER","MAY","MAYBE","ME","MEAL","MEAN","MEANING","MEANS","MEANWHILE","MEASURE","MECHANISM","MEDIA","MEDICAL","MEET","MEETING","MEMBER","MEMBERSHIP","MEMORY","MENTAL","MENTION","MERELY","MESSAGE","METAL","METHOD","MIDDLE","MIGHT","MILE","MILITARY","MILK","MIND","MINE","MINISTER","MINISTRY","MINUTE","MISS","MISTAKE","MODEL","MODERN","MODULE","MOMENT","MONEY","MONTH","MORE","MORNING","MOST","MOTHER","MOTION","MOTOR","MOUNTAIN","MOUTH","MOVE","MOVEMENT","MUCH","MURDER","MUSEUM","MUSIC","MUST","MY","MYSELF","NAME","NARROW","NATION","NATIONAL","NATURAL","NATURE","NEAR","NEARLY","NECESSARILY","NECESSARY","NECK","NEED","NEGOTIATION","NEIGHBOUR","NEITHER","NETWORK","NEVER","NEVERTHELESS","NEW","NEWS","NEWSPAPER","NEXT","NICE","NIGHT","NO","NOBODY","NOD","NOISE","NONE","NOR","NORMAL","NORMALLY","NORTH","NORTHERN","NOSE","NOT","NOTE","NOTHING","NOTICE","NOTION","NOW","NUCLEAR","NUMBER","NURSE","OBJECT","OBJECTIVE","OBSERVATION","OBSERVE","OBTAIN","OBVIOUS","OBVIOUSLY","OCCASION","OCCUR","ODD","OF","OFF","OFFENCE","OFFER","OFFICE","OFFICER","OFFICIAL","OFTEN","OIL","OKAY","OLD","ON","ONCE","ONE","ONLY","ONTO","OPEN","OPERATE","OPERATION","OPINION","OPPORTUNITY","OPPOSITION","OPTION","OR","ORDER","ORDINARY","ORGANISATION","ORGANISE","ORGANIZATION","ORIGIN","ORIGINAL","OTHER","OTHERWISE","OUGHT","OUR","OURSELVES","OUT","OUTCOME","OUTPUT","OUTSIDE","OVER","OVERALL","OWN","OWNER","PACKAGE","PAGE","PAIN","PAINT","PAINTING","PAIR","PANEL","PAPER","PARENT","PARK","PARLIAMENT","PART","PARTICULAR","PARTICULARLY","PARTLY","PARTNER","PARTY","PASS","PASSAGE","PAST","PATH","PATIENT","PATTERN","PAY","PAYMENT","PEACE","PENSION","PEOPLE","PER","PERCENT","PERFECT","PERFORM","PERFORMANCE","PERHAPS","PERIOD","PERMANENT","PERSON","PERSONAL","PERSUADE","PHASE","PHONE","PHOTOGRAPH","PHYSICAL","PICK","PICTURE","PIECE","PLACE","PLAN","PLANNING","PLANT","PLASTIC","PLATE","PLAY","PLAYER","PLEASE","PLEASURE","PLENTY","PLUS","POCKET","POINT","POLICE","POLICY","POLITICAL","POLITICS","POOL","POOR","POPULAR","POPULATION","POSITION","POSITIVE","POSSIBILITY","POSSIBLE","POSSIBLY","POST","POTENTIAL","POUND","POWER","POWERFUL","PRACTICAL","PRACTICE","PREFER","PREPARE","PRESENCE","PRESENT","PRESIDENT","PRESS","PRESSURE","PRETTY","PREVENT","PREVIOUS","PREVIOUSLY","PRICE","PRIMARY","PRIME","PRINCIPLE","PRIORITY","PRISON","PRISONER","PRIVATE","PROBABLY","PROBLEM","PROCEDURE","PROCESS","PRODUCE","PRODUCT","PRODUCTION","PROFESSIONAL","PROFIT","PROGRAM","PROGRAMME","PROGRESS","PROJECT","PROMISE","PROMOTE","PROPER","PROPERLY","PROPERTY","PROPORTION","PROPOSE","PROPOSAL","PROSPECT","PROTECT","PROTECTION","PROVE","PROVIDE","PROVIDED","PROVISION","PUB","PUBLIC","PUBLICATION","PUBLISH","PULL","PUPIL","PURPOSE","PUSH","PUT","QUALITY","QUARTER","QUESTION","QUICK","QUICKLY","QUIET","QUITE","RACE","RADIO","RAILWAY","RAIN","RAISE","RANGE","RAPIDLY","RARE","RATE","RATHER","REACH","REACTION","READ","READER","READING","READY","REAL","REALISE","REALITY","REALIZE","REALLY","REASON","REASONABLE","RECALL","RECEIVE","RECENT","RECENTLY","RECOGNISE","RECOGNITION","RECOGNIZE","RECOMMEND","RECORD","RECOVER","RED","REDUCE","REDUCTION","REFER","REFERENCE","REFLECT","REFORM","REFUSE","REGARD","REGION","REGIONAL","REGULAR","REGULATION","REJECT","RELATE","RELATION","RELATIONSHIP","RELATIVE","RELATIVELY","RELEASE","RELEVANT","RELIEF","RELIGION","RELIGIOUS","RELY","REMAIN","REMEMBER","REMIND","REMOVE","REPEAT","REPLACE","REPLY","REPORT","REPRESENT","REPRESENTATION","REPRESENTATIVE","REQUEST","REQUIRE","REQUIREMENT","RESEARCH","RESOURCE","RESPECT","RESPOND","RESPONSE","RESPONSIBILITY","RESPONSIBLE","REST","RESTAURANT","RESULT","RETAIN","RETURN","REVEAL","REVENUE","REVIEW","REVOLUTION","RICH","RIDE","RIGHT","RING","RISE","RISK","RIVER","ROAD","ROCK","ROLE","ROLL","ROOF","ROOM","ROUND","ROUTE","ROW","ROYAL","RULE","RUN","RURAL","SAFE","SAFETY","SALE","SAME","SAMPLE","SATISFY","SAVE","SAY","SCALE","SCENE","SCHEME","SCHOOL","SCIENCE","SCIENTIFIC","SCIENTIST","SCORE","SCREEN","SEA","SEARCH","SEASON","SEAT","SECOND","SECONDARY","SECRETARY","SECTION","SECTOR","SECURE","SECURITY","SEE","SEEK","SEEM","SELECT","SELECTION","SELL","SEND","SENIOR","SENSE","SENTENCE","SEPARATE","SEQUENCE","SERIES","SERIOUS","SERIOUSLY","SERVANT","SERVE","SERVICE","SESSION","SET","SETTLE","SETTLEMENT","SEVERAL","SEVERE","SEX","SEXUAL","SHAKE","SHALL","SHAPE","SHARE","SHE","SHEET","SHIP","SHOE","SHOOT","SHOP","SHORT","SHOT","SHOULD","SHOULDER","SHOUT","SHOW","SHUT","SIDE","SIGHT","SIGN","SIGNAL","SIGNIFICANCE","SIGNIFICANT","SILENCE","SIMILAR","SIMPLE","SIMPLY","SINCE","SING","SINGLE","SIR","SISTER","SIT","SITE","SITUATION","SIZE","SKILL","SKIN","SKY","SLEEP","SLIGHTLY","SLIP","SLOW","SLOWLY","SMALL","SMILE","SO","SOCIAL","SOCIETY","SOFT","SOFTWARE","SOIL","SOLDIER","SOLICITOR","SOLUTION","SOME","SOMEBODY","SOMEONE","SOMETHING","SOMETIMES","SOMEWHAT","SOMEWHERE","SON","SONG","SOON","SORRY","SORT","SOUND","SOURCE","SOUTH","SOUTHERN","SPACE","SPEAK","SPEAKER","SPECIAL","SPECIES","SPECIFIC","SPEECH","SPEED","SPEND","SPIRIT","SPORT","SPOT","SPREAD","SPRING","STAFF","STAGE","STAND","STANDARD","STAR","START","STATE","STATEMENT","STATION","STATUS","STAY","STEAL","STEP","STICK","STILL","STOCK","STONE","STOP","STORE","STORY","STRAIGHT","STRANGE","STRATEGY","STREET","STRENGTH","STRIKE","STRONG","STRONGLY","STRUCTURE","STUDENT","STUDIO","STUDY","STUFF","STYLE","SUBJECT","SUBSTANTIAL","SUCCEED","SUCCESS","SUCCESSFUL","SUCH","SUDDENLY","SUFFER","SUFFICIENT","SUGGEST","SUGGESTION","SUITABLE","SUM","SUMMER","SUN","SUPPLY","SUPPORT","SUPPOSE","SURE","SURELY","SURFACE","SURPRISE","SURROUND","SURVEY","SURVIVE","SWITCH","SYSTEM","TABLE","TAKE","TALK","TALL","TAPE","TARGET","TASK","TAX","TEA","TEACH","TEACHER","TEACHING","TEAM","TEAR","TECHNICAL","TECHNIQUE","TECHNOLOGY","TELEPHONE","TELEVISION","TELL","TEMPERATURE","TEND","TERM","TERMS","TERRIBLE","TEST","TEXT","THAN","THANK","THANKS","THAT","THE","THEATRE","THEIR","THEM","THEME","THEMSELVES","THEN","THEORY","THERE","THEREFORE","THESE","THEY","THIN","THING","THINK","THIS","THOSE","THOUGH","THOUGHT","THREAT","THREATEN","THROUGH","THROUGHOUT","THROW","THUS","TICKET","TIME","TINY","TITLE","TO","TODAY","TOGETHER","TOMORROW","TONE","TONIGHT","TOO","TOOL","TOOTH","TOP","TOTAL","TOTALLY","TOUCH","TOUR","TOWARDS","TOWN","TRACK","TRADE","TRADITION","TRADITIONAL","TRAFFIC","TRAIN","TRAINING","TRANSFER","TRANSPORT","TRAVEL","TREAT","TREATMENT","TREATY","TREE","TREND","TRIAL","TRIP","TROOP","TROUBLE","TRUE","TRUST","TRUTH","TRY","TURN","TWICE","TYPE","TYPICAL","UNABLE","UNDER","UNDERSTAND","UNDERSTANDING","UNDERTAKE","UNEMPLOYMENT","UNFORTUNATELY","UNION","UNIT","UNITED","UNIVERSITY","UNLESS","UNLIKELY","UNTIL","UP","UPON","UPPER","URBAN","US","USE","USED","USEFUL","USER","USUAL","USUALLY","VALUE","VARIATION","VARIETY","VARIOUS","VARY","VAST","VEHICLE","VERSION","VERY","VIA","VICTIM","VICTORY","VIDEO","VIEW","VILLAGE","VIOLENCE","VISION","VISIT","VISITOR","VITAL","VOICE","VOLUME","VOTE","WAGE","WAIT","WALK","WALL","WANT","WAR","WARM","WARN","WASH","WATCH","WATER","WAVE","WAY","WE","WEAK","WEAPON","WEAR","WEATHER","WEEK","WEEKEND","WEIGHT","WELCOME","WELFARE","WELL","WEST","WESTERN","WHAT","WHATEVER","WHEN","WHERE","WHEREAS","WHETHER","WHICH","WHILE","WHILST","WHITE","WHO","WHOLE","WHOM","WHOSE","WHY","WIDE","WIDELY","WIFE","WILD","WILL","WIN","WIND","WINDOW","WINE","WING","WINNER","WINTER","WISH","WITH","WITHDRAW","WITHIN","WITHOUT","WOMAN","WONDER","WONDERFUL","WOOD","WORD","WORK","WORKER","WORKING","WORKS","WORLD","WORRY","WORTH","WOULD","WRITE","WRITER","WRITING","WRONG","YARD","YEAH","YEAR","YES","YESTERDAY","YET","YOU","YOUNG","YOUR","YOURSELF","YOUTH" diff --git a/project_euler/problem_043/__init__.py b/project_euler/problem_043/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_043/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_043/sol1.py b/project_euler/problem_043/sol1.py new file mode 100644 index 000000000000..1febe4a4d37f --- /dev/null +++ b/project_euler/problem_043/sol1.py @@ -0,0 +1,60 @@ +""" +Problem 43: https://projecteuler.net/problem=43 + +The number, 1406357289, is a 0 to 9 pandigital number because it is made up of +each of the digits 0 to 9 in some order, but it also has a rather interesting +sub-string divisibility property. + +Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note +the following: + +d2d3d4=406 is divisible by 2 +d3d4d5=063 is divisible by 3 +d4d5d6=635 is divisible by 5 +d5d6d7=357 is divisible by 7 +d6d7d8=572 is divisible by 11 +d7d8d9=728 is divisible by 13 +d8d9d10=289 is divisible by 17 +Find the sum of all 0 to 9 pandigital numbers with this property. +""" + + +from itertools import permutations + + +def is_substring_divisible(num: tuple) -> bool: + """ + Returns True if the pandigital number passes + all the divisibility tests. + >>> is_substring_divisible((0, 1, 2, 4, 6, 5, 7, 3, 8, 9)) + False + >>> is_substring_divisible((5, 1, 2, 4, 6, 0, 7, 8, 3, 9)) + False + >>> is_substring_divisible((1, 4, 0, 6, 3, 5, 7, 2, 8, 9)) + True + """ + tests = [2, 3, 5, 7, 11, 13, 17] + for i, test in enumerate(tests): + if (num[i + 1] * 100 + num[i + 2] * 10 + num[i + 3]) % test != 0: + return False + return True + + +def solution(n: int = 10) -> int: + """ + Returns the sum of all pandigital numbers which pass the + divisiility tests. + >>> solution(10) + 16695334890 + """ + list_nums = [ + int("".join(map(str, num))) + for num in permutations(range(n)) + if is_substring_divisible(num) + ] + + return sum(list_nums) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_044/__init__.py b/project_euler/problem_044/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_044/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_044/sol1.py b/project_euler/problem_044/sol1.py new file mode 100644 index 000000000000..d3ae6476d45f --- /dev/null +++ b/project_euler/problem_044/sol1.py @@ -0,0 +1,47 @@ +""" +Problem 44: https://projecteuler.net/problem=44 + +Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten +pentagonal numbers are: +1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... +It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, +70 − 22 = 48, is not pentagonal. + +Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference +are pentagonal and D = |Pk − Pj| is minimised; what is the value of D? +""" + + +def is_pentagonal(n: int) -> bool: + """ + Returns True if n is pentagonal, False otherwise. + >>> is_pentagonal(330) + True + >>> is_pentagonal(7683) + False + >>> is_pentagonal(2380) + True + """ + root = (1 + 24 * n) ** 0.5 + return ((1 + root) / 6) % 1 == 0 + + +def solution(limit: int = 5000) -> int: + """ + Returns the minimum difference of two pentagonal numbers P1 and P2 such that + P1 + P2 is pentagonal and P2 - P1 is pentagonal. + >>> solution(5000) + 5482660 + """ + pentagonal_nums = [(i * (3 * i - 1)) // 2 for i in range(1, limit)] + for i, pentagonal_i in enumerate(pentagonal_nums): + for j in range(i, len(pentagonal_nums)): + pentagonal_j = pentagonal_nums[j] + a = pentagonal_i + pentagonal_j + b = pentagonal_j - pentagonal_i + if is_pentagonal(a) and is_pentagonal(b): + return b + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_045/__init__.py b/project_euler/problem_045/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_045/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_045/sol1.py b/project_euler/problem_045/sol1.py new file mode 100644 index 000000000000..cb30a4d97339 --- /dev/null +++ b/project_euler/problem_045/sol1.py @@ -0,0 +1,59 @@ +""" +Problem 45: https://projecteuler.net/problem=45 + +Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: +Triangle T(n) = (n * (n + 1)) / 2 1, 3, 6, 10, 15, ... +Pentagonal P(n) = (n * (3 * n − 1)) / 2 1, 5, 12, 22, 35, ... +Hexagonal H(n) = n * (2 * n − 1) 1, 6, 15, 28, 45, ... +It can be verified that T(285) = P(165) = H(143) = 40755. + +Find the next triangle number that is also pentagonal and hexagonal. +All trinagle numbers are hexagonal numbers. +T(2n-1) = n * (2 * n - 1) = H(n) +So we shall check only for hexagonal numbers which are also pentagonal. +""" + + +def hexagonal_num(n: int) -> int: + """ + Returns nth hexagonal number + >>> hexagonal_num(143) + 40755 + >>> hexagonal_num(21) + 861 + >>> hexagonal_num(10) + 190 + """ + return n * (2 * n - 1) + + +def is_pentagonal(n: int) -> bool: + """ + Returns True if n is pentagonal, False otherwise. + >>> is_pentagonal(330) + True + >>> is_pentagonal(7683) + False + >>> is_pentagonal(2380) + True + """ + root = (1 + 24 * n) ** 0.5 + return ((1 + root) / 6) % 1 == 0 + + +def solution(start: int = 144) -> int: + """ + Returns the next number which is traingular, pentagonal and hexagonal. + >>> solution(144) + 1533776805 + """ + n = start + num = hexagonal_num(n) + while not is_pentagonal(num): + n += 1 + num = hexagonal_num(n) + return num + + +if __name__ == "__main__": + print(f"{solution()} = ") diff --git a/project_euler/problem_046/__init__.py b/project_euler/problem_046/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_046/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_046/sol1.py b/project_euler/problem_046/sol1.py new file mode 100644 index 000000000000..3fdf567551cc --- /dev/null +++ b/project_euler/problem_046/sol1.py @@ -0,0 +1,95 @@ +""" +Problem 46: https://projecteuler.net/problem=46 + +It was proposed by Christian Goldbach that every odd composite number can be +written as the sum of a prime and twice a square. + +9 = 7 + 2 × 12 +15 = 7 + 2 × 22 +21 = 3 + 2 × 32 +25 = 7 + 2 × 32 +27 = 19 + 2 × 22 +33 = 31 + 2 × 12 + +It turns out that the conjecture was false. + +What is the smallest odd composite that cannot be written as the sum of a +prime and twice a square? +""" + +from __future__ import annotations + +seive = [True] * 100001 +i = 2 +while i * i <= 100000: + if seive[i]: + for j in range(i * i, 100001, i): + seive[j] = False + i += 1 + + +def is_prime(n: int) -> bool: + """ + Returns True if n is prime, + False otherwise, for 2 <= n <= 100000 + >>> is_prime(87) + False + >>> is_prime(23) + True + >>> is_prime(25363) + False + """ + return seive[n] + + +odd_composites = [num for num in range(3, len(seive), 2) if not is_prime(num)] + + +def compute_nums(n: int) -> list[int]: + """ + Returns a list of first n odd composite numbers which do + not follow the conjecture. + >>> compute_nums(1) + [5777] + >>> compute_nums(2) + [5777, 5993] + >>> compute_nums(0) + Traceback (most recent call last): + ... + ValueError: n must be >= 0 + >>> compute_nums("a") + Traceback (most recent call last): + ... + ValueError: n must be an integer + >>> compute_nums(1.1) + Traceback (most recent call last): + ... + ValueError: n must be an integer + + """ + if not isinstance(n, int): + raise ValueError("n must be an integer") + if n <= 0: + raise ValueError("n must be >= 0") + + list_nums = [] + for num in range(len(odd_composites)): + i = 0 + while 2 * i * i <= odd_composites[num]: + rem = odd_composites[num] - 2 * i * i + if is_prime(rem): + break + i += 1 + else: + list_nums.append(odd_composites[num]) + if len(list_nums) == n: + return list_nums + + +def solution() -> int: + """Return the solution to the problem""" + return compute_nums(1)[0] + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_047/__init__.py b/project_euler/problem_047/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_047/sol1.py b/project_euler/problem_047/sol1.py new file mode 100644 index 000000000000..1287e0d9e107 --- /dev/null +++ b/project_euler/problem_047/sol1.py @@ -0,0 +1,112 @@ +""" +Combinatoric selections + +Problem 47 + +The first two consecutive numbers to have two distinct prime factors are: + +14 = 2 × 7 +15 = 3 × 5 + +The first three consecutive numbers to have three distinct prime factors are: + +644 = 2² × 7 × 23 +645 = 3 × 5 × 43 +646 = 2 × 17 × 19. + +Find the first four consecutive integers to have four distinct prime factors each. +What is the first of these numbers? +""" + +from functools import lru_cache + + +def unique_prime_factors(n: int) -> set: + """ + Find unique prime factors of an integer. + Tests include sorting because only the set really matters, + not the order in which it is produced. + >>> sorted(set(unique_prime_factors(14))) + [2, 7] + >>> sorted(set(unique_prime_factors(644))) + [2, 7, 23] + >>> sorted(set(unique_prime_factors(646))) + [2, 17, 19] + """ + i = 2 + factors = set() + while i * i <= n: + if n % i: + i += 1 + else: + n //= i + factors.add(i) + if n > 1: + factors.add(n) + return factors + + +@lru_cache +def upf_len(num: int) -> int: + """ + Memoize upf() length results for a given value. + >>> upf_len(14) + 2 + """ + return len(unique_prime_factors(num)) + + +def equality(iterable: list) -> bool: + """ + Check equality of ALL elements in an interable. + >>> equality([1, 2, 3, 4]) + False + >>> equality([2, 2, 2, 2]) + True + >>> equality([1, 2, 3, 2, 1]) + False + """ + return len(set(iterable)) in (0, 1) + + +def run(n: int) -> list: + """ + Runs core process to find problem solution. + >>> run(3) + [644, 645, 646] + """ + + # Incrementor variable for our group list comprehension. + # This serves as the first number in each list of values + # to test. + base = 2 + + while True: + # Increment each value of a generated range + group = [base + i for i in range(n)] + + # Run elements through out unique_prime_factors function + # Append our target number to the end. + checker = [upf_len(x) for x in group] + checker.append(n) + + # If all numbers in the list are equal, return the group variable. + if equality(checker): + return group + + # Increment our base variable by 1 + base += 1 + + +def solution(n: int = 4) -> int: + """Return the first value of the first four consecutive integers to have four + distinct prime factors each. + >>> solution() + 134043 + """ + results = run(n) + return results[0] if len(results) else None + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_048/__init__.py b/project_euler/problem_048/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_048/sol1.py b/project_euler/problem_048/sol1.py new file mode 100644 index 000000000000..01ff702d9cd5 --- /dev/null +++ b/project_euler/problem_048/sol1.py @@ -0,0 +1,25 @@ +""" +Self Powers +Problem 48 + +The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. + +Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. +""" + + +def solution(): + """ + Returns the last 10 digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. + + >>> solution() + '9110846700' + """ + total = 0 + for i in range(1, 1001): + total += i ** i + return str(total)[-10:] + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_049/__init__.py b/project_euler/problem_049/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_049/sol1.py b/project_euler/problem_049/sol1.py new file mode 100644 index 000000000000..c0d0715be91c --- /dev/null +++ b/project_euler/problem_049/sol1.py @@ -0,0 +1,139 @@ +""" +Prime permutations + +Problem 49 + +The arithmetic sequence, 1487, 4817, 8147, in which each of +the terms increases by 3330, is unusual in two ways: +(i) each of the three terms are prime, +(ii) each of the 4-digit numbers are permutations of one another. + +There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, +exhibiting this property, but there is one other 4-digit increasing sequence. + +What 12-digit number do you form by concatenating the three terms in this sequence? + +Solution: + +First, we need to generate all 4 digits prime numbers. Then greedy +all of them and use permutation to form new numbers. Use binary search +to check if the permutated numbers is in our prime list and include +them in a candidate list. + +After that, bruteforce all passed candidates sequences using +3 nested loops since we know the answer will be 12 digits. +The bruteforce of this solution will be about 1 sec. +""" + +from itertools import permutations +from math import floor, sqrt + + +def is_prime(number: int) -> bool: + """ + function to check whether the number is prime or not. + >>> is_prime(2) + True + >>> is_prime(6) + False + >>> is_prime(1) + False + >>> is_prime(-800) + False + >>> is_prime(104729) + True + """ + + if number < 2: + return False + + for i in range(2, floor(sqrt(number)) + 1): + if number % i == 0: + return False + + return True + + +def search(target: int, prime_list: list) -> bool: + """ + function to search a number in a list using Binary Search. + >>> search(3, [1, 2, 3]) + True + >>> search(4, [1, 2, 3]) + False + >>> search(101, list(range(-100, 100))) + False + """ + + left, right = 0, len(prime_list) - 1 + while left <= right: + middle = (left + right) // 2 + if prime_list[middle] == target: + return True + elif prime_list[middle] < target: + left = middle + 1 + else: + right = middle - 1 + + return False + + +def solution(): + """ + Return the solution of the problem. + >>> solution() + 296962999629 + """ + prime_list = [n for n in range(1001, 10000, 2) if is_prime(n)] + candidates = [] + + for number in prime_list: + tmp_numbers = [] + + for prime_member in permutations(list(str(number))): + prime = int("".join(prime_member)) + + if prime % 2 == 0: + continue + + if search(prime, prime_list): + tmp_numbers.append(prime) + + tmp_numbers.sort() + if len(tmp_numbers) >= 3: + candidates.append(tmp_numbers) + + passed = [] + for candidate in candidates: + length = len(candidate) + found = False + + for i in range(length): + for j in range(i + 1, length): + for k in range(j + 1, length): + if ( + abs(candidate[i] - candidate[j]) + == abs(candidate[j] - candidate[k]) + and len({candidate[i], candidate[j], candidate[k]}) == 3 + ): + passed.append( + sorted([candidate[i], candidate[j], candidate[k]]) + ) + found = True + + if found: + break + if found: + break + if found: + break + + answer = set() + for seq in passed: + answer.add("".join([str(i) for i in seq])) + + return max([int(x) for x in answer]) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_05/sol1.py b/project_euler/problem_05/sol1.py deleted file mode 100644 index 7896d75e3456..000000000000 --- a/project_euler/problem_05/sol1.py +++ /dev/null @@ -1,21 +0,0 @@ -''' -Problem: -2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. -What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? -''' -from __future__ import print_function - -n = int(input()) -i = 0 -while 1: - i+=n*(n-1) - nfound=0 - for j in range(2,n): - if (i%j != 0): - nfound=1 - break - if(nfound==0): - if(i==0): - i=1 - print(i) - break diff --git a/project_euler/problem_05/sol2.py b/project_euler/problem_05/sol2.py deleted file mode 100644 index cd11437f30db..000000000000 --- a/project_euler/problem_05/sol2.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/python3 -''' -Problem: -2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. -What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N? -''' - -""" Euclidean GCD Algorithm """ -def gcd(x,y): - return x if y==0 else gcd(y,x%y) - -""" Using the property lcm*gcd of two numbers = product of them """ -def lcm(x,y): - return (x*y)//gcd(x,y) - -n = int(input()) -g=1 -for i in range(1,n+1): - g=lcm(g,i) -print(g) diff --git a/project_euler/problem_050/__init__.py b/project_euler/problem_050/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_050/sol1.py b/project_euler/problem_050/sol1.py new file mode 100644 index 000000000000..7d142e5ffc91 --- /dev/null +++ b/project_euler/problem_050/sol1.py @@ -0,0 +1,85 @@ +""" +Project Euler Problem 50: https://projecteuler.net/problem=50 + +Consecutive prime sum + +The prime 41, can be written as the sum of six consecutive primes: +41 = 2 + 3 + 5 + 7 + 11 + 13 + +This is the longest sum of consecutive primes that adds to a prime below +one-hundred. + +The longest sum of consecutive primes below one-thousand that adds to a prime, +contains 21 terms, and is equal to 953. + +Which prime, below one-million, can be written as the sum of the most +consecutive primes? +""" +from typing import List + + +def prime_sieve(limit: int) -> List[int]: + """ + Sieve of Erotosthenes + Function to return all the prime numbers up to a number 'limit' + https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + + >>> prime_sieve(3) + [2] + + >>> prime_sieve(50) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] + """ + is_prime = [True] * limit + is_prime[0] = False + is_prime[1] = False + is_prime[2] = True + + for i in range(3, int(limit ** 0.5 + 1), 2): + index = i * 2 + while index < limit: + is_prime[index] = False + index = index + i + + primes = [2] + + for i in range(3, limit, 2): + if is_prime[i]: + primes.append(i) + + return primes + + +def solution(ceiling: int = 1_000_000) -> int: + """ + Returns the biggest prime, below the celing, that can be written as the sum + of consecutive the most consecutive primes. + + >>> solution(500) + 499 + + >>> solution(1_000) + 953 + + >>> solution(10_000) + 9521 + """ + primes = prime_sieve(ceiling) + length = 0 + largest = 0 + + for i in range(len(primes)): + for j in range(i + length, len(primes)): + sol = sum(primes[i:j]) + if sol >= ceiling: + break + + if sol in primes: + length = j - i + largest = sol + + return largest + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_051/__init__.py b/project_euler/problem_051/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_051/sol1.py b/project_euler/problem_051/sol1.py new file mode 100644 index 000000000000..b160b5a2dbd4 --- /dev/null +++ b/project_euler/problem_051/sol1.py @@ -0,0 +1,111 @@ +""" +https://projecteuler.net/problem=51 +Prime digit replacements +Problem 51 + +By replacing the 1st digit of the 2-digit number *3, it turns out that six of +the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime. + +By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit +number is the first example having seven primes among the ten generated numbers, +yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. +Consequently 56003, being the first member of this family, is the smallest prime +with this property. + +Find the smallest prime which, by replacing part of the number (not necessarily +adjacent digits) with the same digit, is part of an eight prime value family. +""" + +from collections import Counter +from typing import List + + +def prime_sieve(n: int) -> List[int]: + """ + Sieve of Erotosthenes + Function to return all the prime numbers up to a certain number + https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + + >>> prime_sieve(3) + [2] + + >>> prime_sieve(50) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] + """ + is_prime = [True] * n + is_prime[0] = False + is_prime[1] = False + is_prime[2] = True + + for i in range(3, int(n ** 0.5 + 1), 2): + index = i * 2 + while index < n: + is_prime[index] = False + index = index + i + + primes = [2] + + for i in range(3, n, 2): + if is_prime[i]: + primes.append(i) + + return primes + + +def digit_replacements(number: int) -> List[List[int]]: + """ + Returns all the possible families of digit replacements in a number which + contains at least one repeating digit + + >>> digit_replacements(544) + [[500, 511, 522, 533, 544, 555, 566, 577, 588, 599]] + + >>> digit_replacements(3112) + [[3002, 3112, 3222, 3332, 3442, 3552, 3662, 3772, 3882, 3992]] + """ + number = str(number) + replacements = [] + digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] + + for duplicate in Counter(number) - Counter(set(number)): + family = [int(number.replace(duplicate, digit)) for digit in digits] + replacements.append(family) + + return replacements + + +def solution(family_length: int = 8) -> int: + """ + Returns the solution of the problem + + >>> solution(2) + 229399 + + >>> solution(3) + 221311 + """ + numbers_checked = set() + + # Filter primes with less than 3 replaceable digits + primes = { + x for x in set(prime_sieve(1_000_000)) if len(str(x)) - len(set(str(x))) >= 3 + } + + for prime in primes: + if prime in numbers_checked: + continue + + replacements = digit_replacements(prime) + + for family in replacements: + numbers_checked.update(family) + primes_in_family = primes.intersection(family) + + if len(primes_in_family) != family_length: + continue + + return min(primes_in_family) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_052/__init__.py b/project_euler/problem_052/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_052/sol1.py b/project_euler/problem_052/sol1.py new file mode 100644 index 000000000000..df5c46ae05d1 --- /dev/null +++ b/project_euler/problem_052/sol1.py @@ -0,0 +1,37 @@ +""" +Permuted multiples +Problem 52 + +It can be seen that the number, 125874, and its double, 251748, contain exactly +the same digits, but in a different order. + +Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, +contain the same digits. +""" + + +def solution(): + """Returns the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and + 6x, contain the same digits. + + >>> solution() + 142857 + """ + i = 1 + + while True: + if ( + sorted(list(str(i))) + == sorted(list(str(2 * i))) + == sorted(list(str(3 * i))) + == sorted(list(str(4 * i))) + == sorted(list(str(5 * i))) + == sorted(list(str(6 * i))) + ): + return i + + i += 1 + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_053/__init__.py b/project_euler/problem_053/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_053/sol1.py b/project_euler/problem_053/sol1.py new file mode 100644 index 000000000000..0692bbe0ebb8 --- /dev/null +++ b/project_euler/problem_053/sol1.py @@ -0,0 +1,43 @@ +""" +Combinatoric selections +Problem 53 + +There are exactly ten ways of selecting three from five, 12345: + + 123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 + +In combinatorics, we use the notation, 5C3 = 10. + +In general, + +nCr = n!/(r!(n−r)!),where r ≤ n, n! = n×(n−1)×...×3×2×1, and 0! = 1. +It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066. + +How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater +than one-million? +""" +from math import factorial + + +def combinations(n, r): + return factorial(n) / (factorial(r) * factorial(n - r)) + + +def solution(): + """Returns the number of values of nCr, for 1 ≤ n ≤ 100, are greater than + one-million + + >>> solution() + 4075 + """ + total = 0 + + for i in range(1, 101): + for j in range(1, i + 1): + if combinations(i, j) > 1e6: + total += 1 + return total + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_054/__init__.py b/project_euler/problem_054/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_054/poker_hands.txt b/project_euler/problem_054/poker_hands.txt new file mode 100644 index 000000000000..9ab00248c061 --- /dev/null +++ b/project_euler/problem_054/poker_hands.txt @@ -0,0 +1,1000 @@ +8C TS KC 9H 4S 7D 2S 5D 3S AC +5C AD 5D AC 9C 7C 5H 8D TD KS +3H 7H 6S KC JS QH TD JC 2D 8S +TH 8H 5C QS TC 9H 4D JC KS JS +7C 5H KC QH JD AS KH 4C AD 4S +5H KS 9C 7D 9H 8D 3S 5D 5C AH +6H 4H 5C 3H 2H 3S QH 5S 6S AS +TD 8C 4H 7C TC KC 4C 3H 7S KS +7C 9C 6D KD 3H 4C QS QC AC KH +JC 6S 5H 2H 2D KD 9D 7C AS JS +AD QH TH 9D 8H TS 6D 3S AS AC +2H 4S 5C 5S TC KC JD 6C TS 3C +QD AS 6H JS 2C 3D 9H KC 4H 8S +KD 8S 9S 7C 2S 3S 6D 6S 4H KC +3C 8C 2D 7D 4D 9S 4S QH 4H JD +8C KC 7S TC 2D TS 8H QD AC 5C +3D KH QD 6C 6S AD AS 8H 2H QS +6S 8D 4C 8S 6C QH TC 6D 7D 9D +2S 8D 8C 4C TS 9S 9D 9C AC 3D +3C QS 2S 4H JH 3D 2D TD 8S 9H +5H QS 8S 6D 3C 8C JD AS 7H 7D +6H TD 9D AS JH 6C QC 9S KD JC +AH 8S QS 4D TH AC TS 3C 3D 5C +5S 4D JS 3D 8H 6C TS 3S AD 8C +6D 7C 5D 5H 3S 5C JC 2H 5S 3D +5H 6H 2S KS 3D 5D JD 7H JS 8H +KH 4H AS JS QS QC TC 6D 7C KS +3D QS TS 2H JS 4D AS 9S JC KD +QD 5H 4D 5D KH 7H 3D JS KD 4H +2C 9H 6H 5C 9D 6C JC 2D TH 9S +7D 6D AS QD JH 4D JS 7C QS 5C +3H KH QD AD 8C 8H 3S TH 9D 5S +AH 9S 4D 9D 8S 4H JS 3C TC 8D +2C KS 5H QD 3S TS 9H AH AD 8S +5C 7H 5D KD 9H 4D 3D 2D KS AD +KS KC 9S 6D 2C QH 9D 9H TS TC +9C 6H 5D QH 4D AD 6D QC JS KH +9S 3H 9D JD 5C 4D 9H AS TC QH +2C 6D JC 9C 3C AD 9S KH 9D 7D +KC 9C 7C JC JS KD 3H AS 3C 7D +QD KH QS 2C 3S 8S 8H 9H 9C JC +QH 8D 3C KC 4C 4H 6D AD 9H 9D +3S KS QS 7H KH 7D 5H 5D JD AD +2H 2C 6H TH TC 7D 8D 4H 8C AS +4S 2H AC QC 3S 6D TH 4D 4C KH +4D TC KS AS 7C 3C 6D 2D 9H 6C +8C TD 5D QS 2C 7H 4C 9C 3H 9H +5H JH TS 7S TD 6H AD QD 8H 8S +5S AD 9C 8C 7C 8D 5H 9D 8S 2S +4H KH KS 9S 2S KC 5S AD 4S 7D +QS 9C QD 6H JS 5D AC 8D 2S AS +KH AC JC 3S 9D 9S 3C 9C 5S JS +AD 3C 3D KS 3S 5C 9C 8C TS 4S +JH 8D 5D 6H KD QS QD 3D 6C KC +8S JD 6C 3S 8C TC QC 3C QH JS +KC JC 8H 2S 9H 9C JH 8S 8C 9S +8S 2H QH 4D QC 9D KC AS TH 3C +8S 6H TH 7C 2H 6S 3C 3H AS 7S +QH 5S JS 4H 5H TS 8H AH AC JC +9D 8H 2S 4S TC JC 3C 7H 3H 5C +3D AD 3C 3S 4C QC AS 5D TH 8C +6S 9D 4C JS KH AH TS JD 8H AD +4C 6S 9D 7S AC 4D 3D 3S TC JD +AD 7H 6H 4H JH KC TD TS 7D 6S +8H JH TC 3S 8D 8C 9S 2C 5C 4D +2C 9D KC QH TH QS JC 9C 4H TS +QS 3C QD 8H KH 4H 8D TD 8S AC +7C 3C TH 5S 8H 8C 9C JD TC KD +QC TC JD TS 8C 3H 6H KD 7C TD +JH QS KS 9C 6D 6S AS 9H KH 6H +2H 4D AH 2D JH 6H TD 5D 4H JD +KD 8C 9S JH QD JS 2C QS 5C 7C +4S TC 7H 8D 2S 6H 7S 9C 7C KC +8C 5D 7H 4S TD QC 8S JS 4H KS +AD 8S JH 6D TD KD 7C 6C 2D 7D +JC 6H 6S JS 4H QH 9H AH 4C 3C +6H 5H AS 7C 7S 3D KH KC 5D 5C +JC 3D TD AS 4D 6D 6S QH JD KS +8C 7S 8S QH 2S JD 5C 7H AH QD +8S 3C 6H 6C 2C 8D TD 7D 4C 4D +5D QH KH 7C 2S 7H JS 6D QC QD +AD 6C 6S 7D TH 6H 2H 8H KH 4H +KS JS KD 5D 2D KH 7D 9C 8C 3D +9C 6D QD 3C KS 3S 7S AH JD 2D +AH QH AS JC 8S 8H 4C KC TH 7D +JC 5H TD 7C 5D KD 4C AD 8H JS +KC 2H AC AH 7D JH KH 5D 7S 6D +9S 5S 9C 6H 8S TD JD 9H 6C AC +7D 8S 6D TS KD 7H AC 5S 7C 5D +AH QC JC 4C TC 8C 2H TS 2C 7D +KD KC 6S 3D 7D 2S 8S 3H 5S 5C +8S 5D 8H 4C 6H KC 3H 7C 5S KD +JH 8C 3D 3C 6C KC TD 7H 7C 4C +JC KC 6H TS QS TD KS 8H 8C 9S +6C 5S 9C QH 7D AH KS KC 9S 2C +4D 4S 8H TD 9C 3S 7D 9D AS TH +6S 7D 3C 6H 5D KD 2C 5C 9D 9C +2H KC 3D AD 3H QD QS 8D JC 4S +8C 3H 9C 7C AD 5D JC 9D JS AS +5D 9H 5C 7H 6S 6C QC JC QD 9S +JC QS JH 2C 6S 9C QC 3D 4S TC +4H 5S 8D 3D 4D 2S KC 2H JS 2C +TD 3S TH KD 4D 7H JH JS KS AC +7S 8C 9S 2D 8S 7D 5C AD 9D AS +8C 7H 2S 6C TH 3H 4C 3S 8H AC +KD 5H JC 8H JD 2D 4H TD JH 5C +3D AS QH KS 7H JD 8S 5S 6D 5H +9S 6S TC QS JC 5C 5D 9C TH 8C +5H 3S JH 9H 2S 2C 6S 7S AS KS +8C QD JC QS TC QC 4H AC KH 6C +TC 5H 7D JH 4H 2H 8D JC KS 4D +5S 9C KH KD 9H 5C TS 3D 7D 2D +5H AS TC 4D 8C 2C TS 9D 3H 8D +6H 8D 2D 9H JD 6C 4S 5H 5S 6D +AD 9C JC 7D 6H 9S 6D JS 9H 3C +AD JH TC QS 4C 5D 9S 7C 9C AH +KD 6H 2H TH 8S QD KS 9D 9H AS +4H 8H 8D 5H 6C AH 5S AS AD 8S +QS 5D 4S 2H TD KS 5H AC 3H JC +9C 7D QD KD AC 6D 5H QH 6H 5S +KC AH QH 2H 7D QS 3H KS 7S JD +6C 8S 3H 6D KS QD 5D 5C 8H TC +9H 4D 4S 6S 9D KH QC 4H 6C JD +TD 2D QH 4S 6H JH KD 3C QD 8C +4S 6H 7C QD 9D AS AH 6S AD 3C +2C KC TH 6H 8D AH 5C 6D 8S 5D +TD TS 7C AD JC QD 9H 3C KC 7H +5D 4D 5S 8H 4H 7D 3H JD KD 2D +JH TD 6H QS 4S KD 5C 8S 7D 8H +AC 3D AS 8C TD 7H KH 5D 6C JD +9D KS 7C 6D QH TC JD KD AS KC +JH 8S 5S 7S 7D AS 2D 3D AD 2H +2H 5D AS 3C QD KC 6H 9H 9S 2C +9D 5D TH 4C JH 3H 8D TC 8H 9H +6H KD 2C TD 2H 6C 9D 2D JS 8C +KD 7S 3C 7C AS QH TS AD 8C 2S +QS 8H 6C JS 4C 9S QC AD TD TS +2H 7C TS TC 8C 3C 9H 2D 6D JC +TC 2H 8D JH KS 6D 3H TD TH 8H +9D TD 9H QC 5D 6C 8H 8C KC TS +2H 8C 3D AH 4D TH TC 7D 8H KC +TS 5C 2D 8C 6S KH AH 5H 6H KC +5S 5D AH TC 4C JD 8D 6H 8C 6C +KC QD 3D 8H 2D JC 9H 4H AD 2S +TD 6S 7D JS KD 4H QS 2S 3S 8C +4C 9H JH TS 3S 4H QC 5S 9S 9C +2C KD 9H JS 9S 3H JC TS 5D AC +AS 2H 5D AD 5H JC 7S TD JS 4C +2D 4S 8H 3D 7D 2C AD KD 9C TS +7H QD JH 5H JS AC 3D TH 4C 8H +6D KH KC QD 5C AD 7C 2D 4H AC +3D 9D TC 8S QD 2C JC 4H JD AH +6C TD 5S TC 8S AH 2C 5D AS AC +TH 7S 3D AS 6C 4C 7H 7D 4H AH +5C 2H KS 6H 7S 4H 5H 3D 3C 7H +3C 9S AC 7S QH 2H 3D 6S 3S 3H +2D 3H AS 2C 6H TC JS 6S 9C 6C +QH KD QD 6D AC 6H KH 2C TS 8C +8H 7D 3S 9H 5D 3H 4S QC 9S 5H +2D 9D 7H 6H 3C 8S 5H 4D 3S 4S +KD 9S 4S TC 7S QC 3S 8S 2H 7H +TC 3D 8C 3H 6C 2H 6H KS KD 4D +KC 3D 9S 3H JS 4S 8H 2D 6C 8S +6H QS 6C TC QD 9H 7D 7C 5H 4D +TD 9D 8D 6S 6C TC 5D TS JS 8H +4H KC JD 9H TC 2C 6S 5H 8H AS +JS 9C 5C 6S 9D JD 8H KC 4C 6D +4D 8D 8S 6C 7C 6H 7H 8H 5C KC +TC 3D JC 6D KS 9S 6H 7S 9C 2C +6C 3S KD 5H TS 7D 9H 9S 6H KH +3D QD 4C 6H TS AC 3S 5C 2H KD +4C AS JS 9S 7C TS 7H 9H JC KS +4H 8C JD 3H 6H AD 9S 4S 5S KS +4C 2C 7D 3D AS 9C 2S QS KC 6C +8S 5H 3D 2S AC 9D 6S 3S 4D TD +QD TH 7S TS 3D AC 7H 6C 5D QC +TC QD AD 9C QS 5C 8D KD 3D 3C +9D 8H AS 3S 7C 8S JD 2D 8D KC +4C TH AC QH JS 8D 7D 7S 9C KH +9D 8D 4C JH 2C 2S QD KD TS 4H +4D 6D 5D 2D JH 3S 8S 3H TC KH +AD 4D 2C QS 8C KD JH JD AH 5C +5C 6C 5H 2H JH 4H KS 7C TC 3H +3C 4C QC 5D JH 9C QD KH 8D TC +3H 9C JS 7H QH AS 7C 9H 5H JC +2D 5S QD 4S 3C KC 6S 6C 5C 4C +5D KH 2D TS 8S 9C AS 9S 7C 4C +7C AH 8C 8D 5S KD QH QS JH 2C +8C 9D AH 2H AC QC 5S 8H 7H 2C +QD 9H 5S QS QC 9C 5H JC TH 4H +6C 6S 3H 5H 3S 6H KS 8D AC 7S +AC QH 7H 8C 4S KC 6C 3D 3S TC +9D 3D JS TH AC 5H 3H 8S 3S TC +QD KH JS KS 9S QC 8D AH 3C AC +5H 6C KH 3S 9S JH 2D QD AS 8C +6C 4D 7S 7H 5S JC 6S 9H 4H JH +AH 5S 6H 9S AD 3S TH 2H 9D 8C +4C 8D 9H 7C QC AD 4S 9C KC 5S +9D 6H 4D TC 4C JH 2S 5D 3S AS +2H 6C 7C KH 5C AD QS TH JD 8S +3S 4S 7S AH AS KC JS 2S AD TH +JS KC 2S 7D 8C 5C 9C TS 5H 9D +7S 9S 4D TD JH JS KH 6H 5D 2C +JD JS JC TH 2D 3D QD 8C AC 5H +7S KH 5S 9D 5D TD 4S 6H 3C 2D +4S 5D AC 8D 4D 7C AD AS AH 9C +6S TH TS KS 2C QC AH AS 3C 4S +2H 8C 3S JC 5C 7C 3H 3C KH JH +7S 3H JC 5S 6H 4C 2S 4D KC 7H +4D 7C 4H 9S 8S 6S AD TC 6C JC +KH QS 3S TC 4C 8H 8S AC 3C TS +QD QS TH 3C TS 7H 7D AH TD JC +TD JD QC 4D 9S 7S TS AD 7D AC +AH 7H 4S 6D 7C 2H 9D KS JC TD +7C AH JD 4H 6D QS TS 2H 2C 5C +TC KC 8C 9S 4C JS 3C JC 6S AH +AS 7D QC 3D 5S JC JD 9D TD KH +TH 3C 2S 6H AH AC 5H 5C 7S 8H +QC 2D AC QD 2S 3S JD QS 6S 8H +KC 4H 3C 9D JS 6H 3S 8S AS 8C +7H KC 7D JD 2H JC QH 5S 3H QS +9H TD 3S 8H 7S AC 5C 6C AH 7C +8D 9H AH JD TD QS 7D 3S 9C 8S +AH QH 3C JD KC 4S 5S 5D TD KS +9H 7H 6S JH TH 4C 7C AD 5C 2D +7C KD 5S TC 9D 6S 6C 5D 2S TH +KC 9H 8D 5H 7H 4H QC 3D 7C AS +6S 8S QC TD 4S 5C TH QS QD 2S +8S 5H TH QC 9H 6S KC 7D 7C 5C +7H KD AH 4D KH 5C 4S 2D KC QH +6S 2C TD JC AS 4D 6C 8C 4H 5S +JC TC JD 5S 6S 8D AS 9D AD 3S +6D 6H 5D 5S TC 3D 7D QS 9D QD +4S 6C 8S 3S 7S AD KS 2D 7D 7C +KC QH JC AC QD 5D 8D QS 7H 7D +JS AH 8S 5H 3D TD 3H 4S 6C JH +4S QS 7D AS 9H JS KS 6D TC 5C +2D 5C 6H TC 4D QH 3D 9H 8S 6C +6D 7H TC TH 5S JD 5C 9C KS KD +8D TD QH 6S 4S 6C 8S KC 5C TC +5S 3D KS AC 4S 7D QD 4C TH 2S +TS 8H 9S 6S 7S QH 3C AH 7H 8C +4C 8C TS JS QC 3D 7D 5D 7S JH +8S 7S 9D QC AC 7C 6D 2H JH KC +JS KD 3C 6S 4S 7C AH QC KS 5H +KS 6S 4H JD QS TC 8H KC 6H AS +KH 7C TC 6S TD JC 5C 7D AH 3S +3H 4C 4H TC TH 6S 7H 6D 9C QH +7D 5H 4S 8C JS 4D 3D 8S QH KC +3H 6S AD 7H 3S QC 8S 4S 7S JS +3S JD KH TH 6H QS 9C 6C 2D QD +4S QH 4D 5H KC 7D 6D 8D TH 5S +TD AD 6S 7H KD KH 9H 5S KC JC +3H QC AS TS 4S QD KS 9C 7S KC +TS 6S QC 6C TH TC 9D 5C 5D KD +JS 3S 4H KD 4C QD 6D 9S JC 9D +8S JS 6D 4H JH 6H 6S 6C KS KH +AC 7D 5D TC 9S KH 6S QD 6H AS +AS 7H 6D QH 8D TH 2S KH 5C 5H +4C 7C 3D QC TC 4S KH 8C 2D JS +6H 5D 7S 5H 9C 9H JH 8S TH 7H +AS JS 2S QD KH 8H 4S AC 8D 8S +3H 4C TD KD 8C JC 5C QS 2D JD +TS 7D 5D 6C 2C QS 2H 3C AH KS +4S 7C 9C 7D JH 6C 5C 8H 9D QD +2S TD 7S 6D 9C 9S QS KH QH 5C +JC 6S 9C QH JH 8D 7S JS KH 2H +8D 5H TH KC 4D 4S 3S 6S 3D QS +2D JD 4C TD 7C 6D TH 7S JC AH +QS 7S 4C TH 9D TS AD 4D 3H 6H +2D 3H 7D JD 3D AS 2S 9C QC 8S +4H 9H 9C 2C 7S JH KD 5C 5D 6H +TC 9H 8H JC 3C 9S 8D KS AD KC +TS 5H JD QS QH QC 8D 5D KH AH +5D AS 8S 6S 4C AH QC QD TH 7H +3H 4H 7D 6S 4S 9H AS 8H JS 9D +JD 8C 2C 9D 7D 5H 5S 9S JC KD +KD 9C 4S QD AH 7C AD 9D AC TD +6S 4H 4S 9C 8D KS TC 9D JH 7C +5S JC 5H 4S QH AC 2C JS 2S 9S +8C 5H AS QD AD 5C 7D 8S QC TD +JC 4C 8D 5C KH QS 4D 6H 2H 2C +TH 4S 2D KC 3H QD AC 7H AD 9D +KH QD AS 8H TH KC 8D 7S QH 8C +JC 6C 7D 8C KH AD QS 2H 6S 2D +JC KH 2D 7D JS QC 5H 4C 5D AD +TS 3S AD 4S TD 2D TH 6S 9H JH +9H 2D QS 2C 4S 3D KH AS AC 9D +KH 6S 8H 4S KD 7D 9D TS QD QC +JH 5H AH KS AS AD JC QC 5S KH +5D 7D 6D KS KD 3D 7C 4D JD 3S +AC JS 8D 5H 9C 3H 4H 4D TS 2C +6H KS KH 9D 7C 2S 6S 8S 2H 3D +6H AC JS 7S 3S TD 8H 3H 4H TH +9H TC QC KC 5C KS 6H 4H AC 8S +TC 7D QH 4S JC TS 6D 6C AC KH +QH 7D 7C JH QS QD TH 3H 5D KS +3D 5S 8D JS 4C 2C KS 7H 9C 4H +5H 8S 4H TD 2C 3S QD QC 3H KC +QC JS KD 9C AD 5S 9D 7D 7H TS +8C JC KH 7C 7S 6C TS 2C QD TH +5S 9D TH 3C 7S QH 8S 9C 2H 5H +5D 9H 6H 2S JS KH 3H 7C 2H 5S +JD 5D 5S 2C TC 2S 6S 6C 3C 8S +4D KH 8H 4H 2D KS 3H 5C 2S 9H +3S 2D TD 7H 8S 6H JD KC 9C 8D +6S QD JH 7C 9H 5H 8S 8H TH TD +QS 7S TD 7D TS JC KD 7C 3C 2C +3C JD 8S 4H 2D 2S TD AS 4D AC +AH KS 6C 4C 4S 7D 8C 9H 6H AS +5S 3C 9S 2C QS KD 4D 4S AC 5D +2D TS 2C JS KH QH 5D 8C AS KC +KD 3H 6C TH 8S 7S KH 6H 9S AC +6H 7S 6C QS AH 2S 2H 4H 5D 5H +5H JC QD 2C 2S JD AS QC 6S 7D +6C TC AS KD 8H 9D 2C 7D JH 9S +2H 4C 6C AH 8S TD 3H TH 7C TS +KD 4S TS 6C QH 8D 9D 9C AH 7D +6D JS 5C QD QC 9C 5D 8C 2H KD +3C QH JH AD 6S AH KC 8S 6D 6H +3D 7C 4C 7S 5S 3S 6S 5H JC 3C +QH 7C 5H 3C 3S 8C TS 4C KD 9C +QD 3S 7S 5H 7H QH JC 7C 8C KD +3C KD KH 2S 4C TS AC 6S 2C 7C +2C KH 3C 4C 6H 4D 5H 5S 7S QD +4D 7C 8S QD TS 9D KS 6H KD 3C +QS 4D TS 7S 4C 3H QD 8D 9S TC +TS QH AC 6S 3C 9H 9D QS 8S 6H +3S 7S 5D 4S JS 2D 6C QH 6S TH +4C 4H AS JS 5D 3D TS 9C AC 8S +6S 9C 7C 3S 5C QS AD AS 6H 3C +9S 8C 7H 3H 6S 7C AS 9H JD KH +3D 3H 7S 4D 6C 7C AC 2H 9C TH +4H 5S 3H AC TC TH 9C 9H 9S 8D +8D 9H 5H 4D 6C 2H QD 6S 5D 3S +4C 5C JD QS 4D 3H TH AC QH 8C +QC 5S 3C 7H AD 4C KS 4H JD 6D +QS AH 3H KS 9H 2S JS JH 5H 2H +2H 5S TH 6S TS 3S KS 3C 5H JS +2D 9S 7H 3D KC JH 6D 7D JS TD +AC JS 8H 2C 8C JH JC 2D TH 7S +5D 9S 8H 2H 3D TC AH JC KD 9C +9D QD JC 2H 6D KH TS 9S QH TH +2C 8D 4S JD 5H 3H TH TC 9C KC +AS 3D 9H 7D 4D TH KH 2H 7S 3H +4H 7S KS 2S JS TS 8S 2H QD 8D +5S 6H JH KS 8H 2S QC AC 6S 3S +JC AS AD QS 8H 6C KH 4C 4D QD +2S 3D TS TD 9S KS 6S QS 5C 8D +3C 6D 4S QC KC JH QD TH KH AD +9H AH 4D KS 2S 8D JH JC 7C QS +2D 6C TH 3C 8H QD QH 2S 3S KS +6H 5D 9S 4C TS TD JS QD 9D JD +5H 8H KH 8S KS 7C TD AD 4S KD +2C 7C JC 5S AS 6C 7D 8S 5H 9C +6S QD 9S TS KH QS 5S QH 3C KC +7D 3H 3C KD 5C AS JH 7H 6H JD +9D 5C 9H KC 8H KS 4S AD 4D 2S +3S JD QD 8D 2S 7C 5S 6S 5H TS +6D 9S KC TD 3S 6H QD JD 5C 8D +5H 9D TS KD 8D 6H TD QC 4C 7D +6D 4S JD 9D AH 9S AS TD 9H QD +2D 5S 2H 9C 6H 9S TD QC 7D TC +3S 2H KS TS 2C 9C 8S JS 9D 7D +3C KC 6D 5D 6C 6H 8S AS 7S QS +JH 9S 2H 8D 4C 8H 9H AD TH KH +QC AS 2S JS 5C 6H KD 3H 7H 2C +QD 8H 2S 8D 3S 6D AH 2C TC 5C +JD JS TS 8S 3H 5D TD KC JC 6H +6S QS TC 3H 5D AH JC 7C 7D 4H +7C 5D 8H 9C 2H 9H JH KH 5S 2C +9C 7H 6S TH 3S QC QD 4C AC JD +2H 5D 9S 7D KC 3S QS 2D AS KH +2S 4S 2H 7D 5C TD TH QH 9S 4D +6D 3S TS 6H 4H KS 9D 8H 5S 2D +9H KS 4H 3S 5C 5D KH 6H 6S JS +KC AS 8C 4C JC KH QC TH QD AH +6S KH 9S 2C 5H TC 3C 7H JC 4D +JD 4S 6S 5S 8D 7H 7S 4D 4C 2H +7H 9H 5D KH 9C 7C TS TC 7S 5H +4C 8D QC TS 4S 9H 3D AD JS 7C +8C QS 5C 5D 3H JS AH KC 4S 9D +TS JD 8S QS TH JH KH 2D QD JS +JD QC 5D 6S 9H 3S 2C 8H 9S TS +2S 4C AD 7H JC 5C 2D 6D 4H 3D +7S JS 2C 4H 8C AD QD 9C 3S TD +JD TS 4C 6H 9H 7D QD 6D 3C AS +AS 7C 4C 6S 5D 5S 5C JS QC 4S +KD 6S 9S 7C 3C 5S 7D JH QD JS +4S 7S JH 2C 8S 5D 7H 3D QH AD +TD 6H 2H 8D 4H 2D 7C AD KH 5D +TS 3S 5H 2C QD AH 2S 5C KH TD +KC 4D 8C 5D AS 6C 2H 2S 9H 7C +KD JS QC TS QS KH JH 2C 5D AD +3S 5H KC 6C 9H 3H 2H AD 7D 7S +7S JS JH KD 8S 7D 2S 9H 7C 2H +9H 2D 8D QC 6S AD AS 8H 5H 6C +2S 7H 6C 6D 7D 8C 5D 9D JC 3C +7C 9C 7H JD 2H KD 3S KH AD 4S +QH AS 9H 4D JD KS KD TS KH 5H +4C 8H 5S 3S 3D 7D TD AD 7S KC +JS 8S 5S JC 8H TH 9C 4D 5D KC +7C 5S 9C QD 2C QH JS 5H 8D KH +TD 2S KS 3D AD KC 7S TC 3C 5D +4C 2S AD QS 6C 9S QD TH QH 5C +8C AD QS 2D 2S KC JD KS 6C JC +8D 4D JS 2H 5D QD 7S 7D QH TS +6S 7H 3S 8C 8S 9D QS 8H 6C 9S +4S TC 2S 5C QD 4D QS 6D TH 6S +3S 5C 9D 6H 8D 4C 7D TC 7C TD +AH 6S AS 7H 5S KD 3H 5H AC 4C +8D 8S AH KS QS 2C AD 6H 7D 5D +6H 9H 9S 2H QS 8S 9C 5D 2D KD +TS QC 5S JH 7D 7S TH 9S 9H AC +7H 3H 6S KC 4D 6D 5C 4S QD TS +TD 2S 7C QD 3H JH 9D 4H 7S 7H +KS 3D 4H 5H TC 2S AS 2D 6D 7D +8H 3C 7H TD 3H AD KC TH 9C KH +TC 4C 2C 9S 9D 9C 5C 2H JD 3C +3H AC TS 5D AD 8D 6H QC 6S 8C +2S TS 3S JD 7H 8S QH 4C 5S 8D +AC 4S 6C 3C KH 3D 7C 2D 8S 2H +4H 6C 8S TH 2H 4S 8H 9S 3H 7S +7C 4C 9C 2C 5C AS 5D KD 4D QH +9H 4H TS AS 7D 8D 5D 9S 8C 2H +QC KD AC AD 2H 7S AS 3S 2D 9S +2H QC 8H TC 6D QD QS 5D KH 3C +TH JD QS 4C 2S 5S AD 7H 3S AS +7H JS 3D 6C 3S 6D AS 9S AC QS +9C TS AS 8C TC 8S 6H 9D 8D 6C +4D JD 9C KC 7C 6D KS 3S 8C AS +3H 6S TC 8D TS 3S KC 9S 7C AS +8C QC 4H 4S 8S 6C 3S TC AH AC +4D 7D 5C AS 2H 6S TS QC AD TC +QD QC 8S 4S TH 3D AH TS JH 4H +5C 2D 9S 2C 3H 3C 9D QD QH 7D +KC 9H 6C KD 7S 3C 4D AS TC 2D +3D JS 4D 9D KS 7D TH QC 3H 3C +8D 5S 2H 9D 3H 8C 4C 4H 3C TH +JC TH 4S 6S JD 2D 4D 6C 3D 4C +TS 3S 2D 4H AC 2C 6S 2H JH 6H +TD 8S AD TC AH AC JH 9S 6S 7S +6C KC 4S JD 8D 9H 5S 7H QH AH +KD 8D TS JH 5C 5H 3H AD AS JS +2D 4H 3D 6C 8C 7S AD 5D 5C 8S +TD 5D 7S 9C 4S 5H 6C 8C 4C 8S +JS QH 9C AS 5C QS JC 3D QC 7C +JC 9C KH JH QS QC 2C TS 3D AD +5D JH AC 5C 9S TS 4C JD 8C KS +KC AS 2D KH 9H 2C 5S 4D 3D 6H +TH AH 2D 8S JC 3D 8C QH 7S 3S +8H QD 4H JC AS KH KS 3C 9S 6D +9S QH 7D 9C 4S AC 7H KH 4D KD +AH AD TH 6D 9C 9S KD KS QH 4H +QD 6H 9C 7C QS 6D 6S 9D 5S JH +AH 8D 5H QD 2H JC KS 4H KH 5S +5C 2S JS 8D 9C 8C 3D AS KC AH +JD 9S 2H QS 8H 5S 8C TH 5C 4C +QC QS 8C 2S 2C 3S 9C 4C KS KH +2D 5D 8S AH AD TD 2C JS KS 8C +TC 5S 5H 8H QC 9H 6H JD 4H 9S +3C JH 4H 9H AH 4S 2H 4C 8D AC +8S TH 4D 7D 6D QD QS 7S TC 7C +KH 6D 2D JD 5H JS QD JH 4H 4S +9C 7S JH 4S 3S TS QC 8C TC 4H +QH 9D 4D JH QS 3S 2C 7C 6C 2D +4H 9S JD 5C 5H AH 9D TS 2D 4C +KS JH TS 5D 2D AH JS 7H AS 8D +JS AH 8C AD KS 5S 8H 2C 6C TH +2H 5D AD AC KS 3D 8H TS 6H QC +6D 4H TS 9C 5H JS JH 6S JD 4C +JH QH 4H 2C 6D 3C 5D 4C QS KC +6H 4H 6C 7H 6S 2S 8S KH QC 8C +3H 3D 5D KS 4H TD AD 3S 4D TS +5S 7C 8S 7D 2C KS 7S 6C 8C JS +5D 2H 3S 7C 5C QD 5H 6D 9C 9H +JS 2S KD 9S 8D TD TS AC 8C 9D +5H QD 2S AC 8C 9H KS 7C 4S 3C +KH AS 3H 8S 9C JS QS 4S AD 4D +AS 2S TD AD 4D 9H JC 4C 5H QS +5D 7C 4H TC 2D 6C JS 4S KC 3S +4C 2C 5D AC 9H 3D JD 8S QS QH +2C 8S 6H 3C QH 6D TC KD AC AH +QC 6C 3S QS 4S AC 8D 5C AD KH +5S 4C AC KH AS QC 2C 5C 8D 9C +8H JD 3C KH 8D 5C 9C QD QH 9D +7H TS 2C 8C 4S TD JC 9C 5H QH +JS 4S 2C 7C TH 6C AS KS 7S JD +JH 7C 9H 7H TC 5H 3D 6D 5D 4D +2C QD JH 2H 9D 5S 3D TD AD KS +JD QH 3S 4D TH 7D 6S QS KS 4H +TC KS 5S 8D 8H AD 2S 2D 4C JH +5S JH TC 3S 2D QS 9D 4C KD 9S +AC KH 3H AS 9D KC 9H QD 6C 6S +9H 7S 3D 5C 7D KC TD 8H 4H 6S +3C 7H 8H TC QD 4D 7S 6S QH 6C +6D AD 4C QD 6C 5D 7D 9D KS TS +JH 2H JD 9S 7S TS KH 8D 5D 8H +2D 9S 4C 7D 9D 5H QD 6D AC 6S +7S 6D JC QD JH 4C 6S QS 2H 7D +8C TD JH KD 2H 5C QS 2C JS 7S +TC 5H 4H JH QD 3S 5S 5D 8S KH +KS KH 7C 2C 5D JH 6S 9C 6D JC +5H AH JD 9C JS KC 2H 6H 4D 5S +AS 3C TH QC 6H 9C 8S 8C TD 7C +KC 2C QD 9C KH 4D 7S 3C TS 9H +9C QC 2S TS 8C TD 9S QD 3S 3C +4D 9D TH JH AH 6S 2S JD QH JS +QD 9H 6C KD 7D 7H 5D 6S 8H AH +8H 3C 4S 2H 5H QS QH 7S 4H AC +QS 3C 7S 9S 4H 3S AH KS 9D 7C +AD 5S 6S 2H 2D 5H TC 4S 3C 8C +QH TS 6S 4D JS KS JH AS 8S 6D +2C 8S 2S TD 5H AS TC TS 6C KC +KC TS 8H 2H 3H 7C 4C 5S TH TD +KD AD KH 7H 7S 5D 5H 5S 2D 9C +AD 9S 3D 7S 8C QC 7C 9C KD KS +3C QC 9S 8C 4D 5C AS QD 6C 2C +2H KC 8S JD 7S AC 8D 5C 2S 4D +9D QH 3D 2S TC 3S KS 3C 9H TD +KD 6S AC 2C 7H 5H 3S 6C 6H 8C +QH TC 8S 6S KH TH 4H 5D TS 4D +8C JS 4H 6H 2C 2H 7D AC QD 3D +QS KC 6S 2D 5S 4H TD 3H JH 4C +7S 5H 7H 8H KH 6H QS TH KD 7D +5H AD KD 7C KH 5S TD 6D 3C 6C +8C 9C 5H JD 7C KC KH 7H 2H 3S +7S 4H AD 4D 8S QS TH 3D 7H 5S +8D TC KS KD 9S 6D AD JD 5C 2S +7H 8H 6C QD 2H 6H 9D TC 9S 7C +8D 6D 4C 7C 6C 3C TH KH JS JH +5S 3S 8S JS 9H AS AD 8H 7S KD +JH 7C 2C KC 5H AS AD 9C 9S JS +AD AC 2C 6S QD 7C 3H TH KS KD +9D JD 4H 8H 4C KH 7S TS 8C KC +3S 5S 2H 7S 6H 7D KS 5C 6D AD +5S 8C 9H QS 7H 7S 2H 6C 7D TD +QS 5S TD AC 9D KC 3D TC 2D 4D +TD 2H 7D JD QD 4C 7H 5D KC 3D +4C 3H 8S KD QH 5S QC 9H TC 5H +9C QD TH 5H TS 5C 9H AH QH 2C +4D 6S 3C AC 6C 3D 2C 2H TD TH +AC 9C 5D QC 4D AD 8D 6D 8C KC +AD 3C 4H AC 8D 8H 7S 9S TD JC +4H 9H QH JS 2D TH TD TC KD KS +5S 6S 9S 8D TH AS KH 5H 5C 8S +JD 2S 9S 6S 5S 8S 5D 7S 7H 9D +5D 8C 4C 9D AD TS 2C 7D KD TC +8S QS 4D KC 5C 8D 4S KH JD KD +AS 5C AD QH 7D 2H 9S 7H 7C TC +2S 8S JD KH 7S 6C 6D AD 5D QC +9H 6H 3S 8C 8H AH TC 4H JS TD +2C TS 4D 7H 2D QC 9C 5D TH 7C +6C 8H QC 5D TS JH 5C 5H 9H 4S +2D QC 7H AS JS 8S 2H 4C 4H 8D +JS 6S AC KD 3D 3C 4S 7H TH KC +QH KH 6S QS 5S 4H 3C QD 3S 3H +7H AS KH 8C 4H 9C 5S 3D 6S TS +9C 7C 3H 5S QD 2C 3D AD AC 5H +JH TD 2D 4C TS 3H KH AD 3S 7S +AS 4C 5H 4D 6S KD JC 3C 6H 2D +3H 6S 8C 2D TH 4S AH QH AD 5H +7C 2S 9H 7H KC 5C 6D 5S 3H JC +3C TC 9C 4H QD TD JH 6D 9H 5S +7C 6S 5C 5D 6C 4S 7H 9H 6H AH +AD 2H 7D KC 2C 4C 2S 9S 7H 3S +TH 4C 8S 6S 3S AD KS AS JH TD +5C TD 4S 4D AD 6S 5D TC 9C 7D +8H 3S 4D 4S 5S 6H 5C AC 3H 3D +9H 3C AC 4S QS 8S 9D QH 5H 4D +JC 6C 5H TS AC 9C JD 8C 7C QD +8S 8H 9C JD 2D QC QH 6H 3C 8D +KS JS 2H 6H 5H QH QS 3H 7C 6D +TC 3H 4S 7H QC 2H 3S 8C JS KH +AH 8H 5S 4C 9H JD 3H 7S JC AC +3C 2D 4C 5S 6C 4S QS 3S JD 3D +5H 2D TC AH KS 6D 7H AD 8C 6H +6C 7S 3C JD 7C 8H KS KH AH 6D +AH 7D 3H 8H 8S 7H QS 5H 9D 2D +JD AC 4H 7S 8S 9S KS AS 9D QH +7S 2C 8S 5S JH QS JC AH KD 4C +AH 2S 9H 4H 8D TS TD 6H QH JD +4H JC 3H QS 6D 7S 9C 8S 9D 8D +5H TD 4S 9S 4C 8C 8D 7H 3H 3D +QS KH 3S 2C 2S 3C 7S TD 4S QD +7C TD 4D 5S KH AC AS 7H 4C 6C +2S 5H 6D JD 9H QS 8S 2C 2H TD +2S TS 6H 9H 7S 4H JC 4C 5D 5S +2C 5H 7D 4H 3S QH JC JS 6D 8H +4C QH 7C QD 3S AD TH 8S 5S TS +9H TC 2S TD JC 7D 3S 3D TH QH +7D 4C 8S 5C JH 8H 6S 3S KC 3H +JC 3H KH TC QH TH 6H 2C AC 5H +QS 2H 9D 2C AS 6S 6C 2S 8C 8S +9H 7D QC TH 4H KD QS AC 7S 3C +4D JH 6S 5S 8H KS 9S QC 3S AS +JD 2D 6S 7S TC 9H KC 3H 7D KD +2H KH 7C 4D 4S 3H JS QD 7D KC +4C JC AS 9D 3C JS 6C 8H QD 4D +AH JS 3S 6C 4C 3D JH 6D 9C 9H +9H 2D 8C 7H 5S KS 6H 9C 2S TC +6C 8C AD 7H 6H 3D KH AS 5D TH +KS 8C 3S TS 8S 4D 5S 9S 6C 4H +9H 4S 4H 5C 7D KC 2D 2H 9D JH +5C JS TC 9D 9H 5H 7S KH JC 6S +7C 9H 8H 4D JC KH JD 2H TD TC +8H 6C 2H 2C KH 6H 9D QS QH 5H +AC 7D 2S 3D QD JC 2D 8D JD JH +2H JC 2D 7H 2C 3C 8D KD TD 4H +3S 4H 6D 8D TS 3H TD 3D 6H TH +JH JC 3S AC QH 9H 7H 8S QC 2C +7H TD QS 4S 8S 9C 2S 5D 4D 2H +3D TS 3H 2S QC 8H 6H KC JC KS +5D JD 7D TC 8C 6C 9S 3D 8D AC +8H 6H JH 6C 5D 8D 8S 4H AD 2C +9D 4H 2D 2C 3S TS AS TC 3C 5D +4D TH 5H KS QS 6C 4S 2H 3D AD +5C KC 6H 2C 5S 3C 4D 2D 9H 9S +JD 4C 3H TH QH 9H 5S AH 8S AC +7D 9S 6S 2H TD 9C 4H 8H QS 4C +3C 6H 5D 4H 8C 9C KC 6S QD QS +3S 9H KD TC 2D JS 8C 6S 4H 4S +2S 4C 8S QS 6H KH 3H TH 8C 5D +2C KH 5S 3S 7S 7H 6C 9D QD 8D +8H KS AC 2D KH TS 6C JS KC 7H +9C KS 5C TD QC AH 6C 5H 9S 7C +5D 4D 3H 4H 6S 7C 7S AH QD TD +2H 7D QC 6S TC TS AH 7S 9D 3H +TH 5H QD 9S KS 7S 7C 6H 8C TD +TH 2D 4D QC 5C 7D JD AH 9C 4H +4H 3H AH 8D 6H QC QH 9H 2H 2C +2D AD 4C TS 6H 7S TH 4H QS TD +3C KD 2H 3H QS JD TC QC 5D 8H +KS JC QD TH 9S KD 8D 8C 2D 9C +3C QD KD 6D 4D 8D AH AD QC 8S +8H 3S 9D 2S 3H KS 6H 4C 7C KC +TH 9S 5C 3D 7D 6H AC 7S 4D 2C +5C 3D JD 4D 2D 6D 5H 9H 4C KH +AS 7H TD 6C 2H 3D QD KS 4C 4S +JC 3C AC 7C JD JS 8H 9S QC 5D +JD 6S 5S 2H AS 8C 7D 5H JH 3D +8D TC 5S 9S 8S 3H JC 5H 7S AS +5C TD 3D 7D 4H 8D 7H 4D 5D JS +QS 9C KS TD 2S 8S 5C 2H 4H AS +TH 7S 4H 7D 3H JD KD 5D 2S KC +JD 7H 4S 8H 4C JS 6H QH 5S 4H +2C QS 8C 5S 3H QC 2S 6C QD AD +8C 3D JD TC 4H 2H AD 5S AC 2S +5D 2C JS 2D AD 9D 3D 4C 4S JH +8D 5H 5D 6H 7S 4D KS 9D TD JD +3D 6D 9C 2S AS 7D 5S 5C 8H JD +7C 8S 3S 6S 5H JD TC AD 7H 7S +2S 9D TS 4D AC 8D 6C QD JD 3H +9S KH 2C 3C AC 3D 5H 6H 8D 5D +KS 3D 2D 6S AS 4C 2S 7C 7H KH +AC 2H 3S JC 5C QH 4D 2D 5H 7S +TS AS JD 8C 6H JC 8S 5S 2C 5D +7S QH 7H 6C QC 8H 2D 7C JD 2S +2C QD 2S 2H JC 9C 5D 2D JD JH +7C 5C 9C 8S 7D 6D 8D 6C 9S JH +2C AD 6S 5H 3S KS 7S 9D KH 4C +7H 6C 2C 5C TH 9D 8D 3S QC AH +5S KC 6H TC 5H 8S TH 6D 3C AH +9C KD 4H AD TD 9S 4S 7D 6H 5D +7H 5C 5H 6D AS 4C KD KH 4H 9D +3C 2S 5C 6C JD QS 2H 9D 7D 3H +AC 2S 6S 7S JS QD 5C QS 6H AD +5H TH QC 7H TC 3S 7C 6D KC 3D +4H 3D QC 9S 8H 2C 3S JC KS 5C +4S 6S 2C 6H 8S 3S 3D 9H 3H JS +4S 8C 4D 2D 8H 9H 7D 9D AH TS +9S 2C 9H 4C 8D AS 7D 3D 6D 5S +6S 4C 7H 8C 3H 5H JC AH 9D 9C +2S 7C 5S JD 8C 3S 3D 4D 7D 6S +3C KC 4S 5D 7D 3D JD 7H 3H 4H +9C 9H 4H 4D TH 6D QD 8S 9S 7S +2H AC 8S 4S AD 8C 2C AH 7D TC +TS 9H 3C AD KS TC 3D 8C 8H JD +QC 8D 2C 3C 7D 7C JD 9H 9C 6C +AH 6S JS JH 5D AS QC 2C JD TD +9H KD 2H 5D 2D 3S 7D TC AH TS +TD 8H AS 5D AH QC AC 6S TC 5H +KS 4S 7H 4D 8D 9C TC 2H 6H 3H +3H KD 4S QD QH 3D 8H 8C TD 7S +8S JD TC AH JS QS 2D KH KS 4D +3C AD JC KD JS KH 4S TH 9H 2C +QC 5S JS 9S KS AS 7C QD 2S JD +KC 5S QS 3S 2D AC 5D 9H 8H KS +6H 9C TC AD 2C 6D 5S JD 6C 7C +QS KH TD QD 2C 3H 8S 2S QC AH +9D 9H JH TC QH 3C 2S JS 5C 7H +6C 3S 3D 2S 4S QD 2D TH 5D 2C +2D 6H 6D 2S JC QH AS 7H 4H KH +5H 6S KS AD TC TS 7C AC 4S 4H +AD 3C 4H QS 8C 9D KS 2H 2D 4D +4S 9D 6C 6D 9C AC 8D 3H 7H KD +JC AH 6C TS JD 6D AD 3S 5D QD +JC JH JD 3S 7S 8S JS QC 3H 4S +JD TH 5C 2C AD JS 7H 9S 2H 7S +8D 3S JH 4D QC AS JD 2C KC 6H +2C AC 5H KD 5S 7H QD JH AH 2D +JC QH 8D 8S TC 5H 5C AH 8C 6C +3H JS 8S QD JH 3C 4H 6D 5C 3S +6D 4S 4C AH 5H 5S 3H JD 7C 8D +8H AH 2H 3H JS 3C 7D QC 4H KD +6S 2H KD 5H 8H 2D 3C 8S 7S QD +2S 7S KC QC AH TC QS 6D 4C 8D +5S 9H 2C 3S QD 7S 6C 2H 7C 9D +3C 6C 5C 5S JD JC KS 3S 5D TS +7C KS 6S 5S 2S 2D TC 2H 5H QS +AS 7H 6S TS 5H 9S 9D 3C KD 2H +4S JS QS 3S 4H 7C 2S AC 6S 9D +8C JH 2H 5H 7C 5D QH QS KH QC +3S TD 3H 7C KC 8D 5H 8S KH 8C +4H KH JD TS 3C 7H AS QC JS 5S +AH 9D 2C 8D 4D 2D 6H 6C KC 6S +2S 6H 9D 3S 7H 4D KH 8H KD 3D +9C TC AC JH KH 4D JD 5H TD 3S +7S 4H 9D AS 4C 7D QS 9S 2S KH +3S 8D 8S KS 8C JC 5C KH 2H 5D +8S QH 2C 4D KC JS QC 9D AC 6H +8S 8C 7C JS JD 6S 4C 9C AC 4S +QH 5D 2C 7D JC 8S 2D JS JH 4C +JS 4C 7S TS JH KC KH 5H QD 4S +QD 8C 8D 2D 6S TD 9D AC QH 5S +QH QC JS 3D 3C 5C 4H KH 8S 7H +7C 2C 5S JC 8S 3H QC 5D 2H KC +5S 8D KD 6H 4H QD QH 6D AH 3D +7S KS 6C 2S 4D AC QS 5H TS JD +7C 2D TC 5D QS AC JS QC 6C KC +2C KS 4D 3H TS 8S AD 4H 7S 9S +QD 9H QH 5H 4H 4D KH 3S JC AD +4D AC KC 8D 6D 4C 2D KH 2C JD +2C 9H 2D AH 3H 6D 9C 7D TC KS +8C 3H KD 7C 5C 2S 4S 5H AS AH +TH JD 4H KD 3H TC 5C 3S AC KH +6D 7H AH 7S QC 6H 2D TD JD AS +JH 5D 7H TC 9S 7D JC AS 5S KH +2H 8C AD TH 6H QD KD 9H 6S 6C +QH KC 9D 4D 3S JS JH 4H 2C 9H +TC 7H KH 4H JC 7D 9S 3H QS 7S +AD 7D JH 6C 7H 4H 3S 3H 4D QH +JD 2H 5C AS 6C QC 4D 3C TC JH +AC JD 3H 6H 4C JC AD 7D 7H 9H +4H TC TS 2C 8C 6S KS 2H JD 9S +4C 3H QS QC 9S 9H 6D KC 9D 9C +5C AD 8C 2C QH TH QD JC 8D 8H +QC 2C 2S QD 9C 4D 3S 8D JH QS +9D 3S 2C 7S 7C JC TD 3C TC 9H +3C TS 8H 5C 4C 2C 6S 8D 7C 4H +KS 7H 2H TC 4H 2C 3S AS AH QS +8C 2D 2H 2C 4S 4C 6S 7D 5S 3S +TH QC 5D TD 3C QS KD KC KS AS +4D AH KD 9H KS 5C 4C 6H JC 7S +KC 4H 5C QS TC 2H JC 9S AH QH +4S 9H 3H 5H 3C QD 2H QC JH 8H +5D AS 7H 2C 3D JH 6H 4C 6S 7D +9C JD 9H AH JS 8S QH 3H KS 8H +3S AC QC TS 4D AD 3D AH 8S 9H +7H 3H QS 9C 9S 5H JH JS AH AC +8D 3C JD 2H AC 9C 7H 5S 4D 8H +7C JH 9H 6C JS 9S 7H 8C 9D 4H +2D AS 9S 6H 4D JS JH 9H AD QD +6H 7S JH KH AH 7H TD 5S 6S 2C +8H JH 6S 5H 5S 9D TC 4C QC 9S +7D 2C KD 3H 5H AS QD 7H JS 4D +TS QH 6C 8H TH 5H 3C 3H 9C 9D +AD KH JS 5D 3H AS AC 9S 5C KC +2C KH 8C JC QS 6D AH 2D KC TC +9D 3H 2S 7C 4D 6D KH KS 8D 7D +9H 2S TC JH AC QC 3H 5S 3S 8H +3S AS KD 8H 4C 3H 7C JH QH TS +7S 6D 7H 9D JH 4C 3D 3S 6C AS +4S 2H 2C 4C 8S 5H KC 8C QC QD +3H 3S 6C QS QC 2D 6S 5D 2C 9D +2H 8D JH 2S 3H 2D 6C 5C 7S AD +9H JS 5D QH 8S TS 2H 7S 6S AD +6D QC 9S 7H 5H 5C 7D KC JD 4H +QC 5S 9H 9C 4D 6S KS 2S 4C 7C +9H 7C 4H 8D 3S 6H 5C 8H JS 7S +2D 6H JS TD 4H 4D JC TH 5H KC +AC 7C 8D TH 3H 9S 2D 4C KC 4D +KD QS 9C 7S 3D KS AD TS 4C 4H +QH 9C 8H 2S 7D KS 7H 5D KD 4C +9C 2S 2H JC 6S 6C TC QC JH 5C +7S AC 8H KC 8S 6H QS JC 3D 6S +JS 2D JH 8C 4S 6H 8H 6D 5D AD +6H 7D 2S 4H 9H 7C AS AC 8H 5S +3C JS 4S 6D 5H 2S QH 6S 9C 2C +3D 5S 6S 9S 4C QS 8D QD 8S TC +9C 3D AH 9H 5S 2C 7D AD JC 3S +7H TC AS 3C 6S 6D 7S KH KC 9H +3S TC 8H 6S 5H JH 8C 7D AC 2S +QD 9D 9C 3S JC 8C KS 8H 5D 4D +JS AH JD 6D 9D 8C 9H 9S 8H 3H +2D 6S 4C 4D 8S AD 4S TC AH 9H +TS AC QC TH KC 6D 4H 7S 8C 2H +3C QD JS 9D 5S JC AH 2H TS 9H +3H 4D QH 5D 9C 5H 7D 4S JC 3S +8S TH 3H 7C 2H JD JS TS AC 8D +9C 2H TD KC JD 2S 8C 5S AD 2C +3D KD 7C 5H 4D QH QD TC 6H 7D +7H 2C KC 5S KD 6H AH QC 7S QH +6H 5C AC 5H 2C 9C 2D 7C TD 2S +4D 9D AH 3D 7C JD 4H 8C 4C KS +TH 3C JS QH 8H 4C AS 3D QS QC +4D 7S 5H JH 6D 7D 6H JS KH 3C +QD 8S 7D 2H 2C 7C JC 2S 5H 8C +QH 8S 9D TC 2H AD 7C 8D QD 6S +3S 7C AD 9H 2H 9S JD TS 4C 2D +3S AS 4H QC 2C 8H 8S 7S TD TC +JH TH TD 3S 4D 4H 5S 5D QS 2C +8C QD QH TC 6D 4S 9S 9D 4H QC +8C JS 9D 6H JD 3H AD 6S TD QC +KC 8S 3D 7C TD 7D 8D 9H 4S 3S +6C 4S 3D 9D KD TC KC KS AC 5S +7C 6S QH 3D JS KD 6H 6D 2D 8C +JD 2S 5S 4H 8S AC 2D 6S TS 5C +5H 8C 5S 3C 4S 3D 7C 8D AS 3H +AS TS 7C 3H AD 7D JC QS 6C 6H +3S 9S 4C AC QH 5H 5D 9H TS 4H +6C 5C 7H 7S TD AD JD 5S 2H 2S +7D 6C KC 3S JD 8D 8S TS QS KH +8S QS 8D 6C TH AC AH 2C 8H 9S +7H TD KH QH 8S 3D 4D AH JD AS +TS 3D 2H JC 2S JH KH 6C QC JS +KC TH 2D 6H 7S 2S TC 8C 9D QS +3C 9D 6S KH 8H 6D 5D TH 2C 2H +6H TC 7D AD 4D 8S TS 9H TD 7S +JS 6D JD JC 2H AC 6C 3D KH 8D +KH JD 9S 5D 4H 4C 3H 7S QS 5C +4H JD 5D 3S 3C 4D KH QH QS 7S +JD TS 8S QD AH 4C 6H 3S 5S 2C +QS 3D JD AS 8D TH 7C 6S QC KS +7S 2H 8C QC 7H AC 6D 2D TH KH +5S 6C 7H KH 7D AH 8C 5C 7S 3D +3C KD AD 7D 6C 4D KS 2D 8C 4S +7C 8D 5S 2D 2S AH AD 2C 9D TD +3C AD 4S KS JH 7C 5C 8C 9C TH +AS TD 4D 7C JD 8C QH 3C 5H 9S +3H 9C 8S 9S 6S QD KS AH 5H JH +QC 9C 5S 4H 2H TD 7D AS 8C 9D +8C 2C 9D KD TC 7S 3D KH QC 3C +4D AS 4C QS 5S 9D 6S JD QH KS +6D AH 6C 4C 5H TS 9H 7D 3D 5S +QS JD 7C 8D 9C AC 3S 6S 6C KH +8H JH 5D 9S 6D AS 6S 3S QC 7H +QD AD 5C JH 2H AH 4H AS KC 2C +JH 9C 2C 6H 2D JS 5D 9H KC 6D +7D 9D KD TH 3H AS 6S QC 6H AD +JD 4H 7D KC 3H JS 3C TH 3D QS +4C 3H 8C QD 5H 6H AS 8H AD JD +TH 8S KD 5D QC 7D JS 5S 5H TS +7D KC 9D QS 3H 3C 6D TS 7S AH +7C 4H 7H AH QC AC 4D 5D 6D TH +3C 4H 2S KD 8H 5H JH TC 6C JD +4S 8C 3D 4H JS TD 7S JH QS KD +7C QC KD 4D 7H 6S AD TD TC KH +5H 9H KC 3H 4D 3D AD 6S QD 6H +TH 7C 6H TS QH 5S 2C KC TD 6S +7C 4D 5S JD JH 7D AC KD KH 4H +7D 6C 8D 8H 5C JH 8S QD TH JD +8D 7D 6C 7C 9D KD AS 5C QH JH +9S 2C 8C 3C 4C KS JH 2D 8D 4H +7S 6C JH KH 8H 3H 9D 2D AH 6D +4D TC 9C 8D 7H TD KS TH KD 3C +JD 9H 8D QD AS KD 9D 2C 2S 9C +8D 3H 5C 7H KS 5H QH 2D 8C 9H +2D TH 6D QD 6C KC 3H 3S AD 4C +4H 3H JS 9D 3C TC 5H QH QC JC +3D 5C 6H 3S 3C JC 5S 7S 2S QH +AC 5C 8C 4D 5D 4H 2S QD 3C 3H +2C TD AH 9C KD JS 6S QD 4C QC +QS 8C 3S 4H TC JS 3H 7C JC AD +5H 4D 9C KS JC TD 9S TS 8S 9H +QD TS 7D AS AC 2C TD 6H 8H AH +6S AD 8C 4S 9H 8D 9D KH 8S 3C +QS 4D 2D 7S KH JS JC AD 4C 3C +QS 9S 7H KC TD TH 5H JS AC JH +6D AC 2S QS 7C AS KS 6S KH 5S +6D 8H KH 3C QS 2H 5C 9C 9D 6C +JS 2C 4C 6H 7D JC AC QD TD 3H +4H QC 8H JD 4C KD KS 5C KC 7S +6D 2D 3H 2S QD 5S 7H AS TH 6S +AS 6D 8D 2C 8S TD 8H QD JC AH +9C 9H 2D TD QH 2H 5C TC 3D 8H +KC 8S 3D KH 2S TS TC 6S 4D JH +9H 9D QS AC KC 6H 5D 4D 8D AH +9S 5C QS 4H 7C 7D 2H 8S AD JS +3D AC 9S AS 2C 2D 2H 3H JC KH +7H QH KH JD TC KS 5S 8H 4C 8D +2H 7H 3S 2S 5H QS 3C AS 9H KD +AD 3D JD 6H 5S 9C 6D AC 9S 3S +3D 5D 9C 2D AC 4S 2S AD 6C 6S +QC 4C 2D 3H 6S KC QH QD 2H JH +QC 3C 8S 4D 9S 2H 5C 8H QS QD +6D KD 6S 7H 3S KH 2H 5C JC 6C +3S 9S TC 6S 8H 2D AD 7S 8S TS +3C 6H 9C 3H 5C JC 8H QH TD QD +3C JS QD 5D TD 2C KH 9H TH AS +9S TC JD 3D 5C 5H AD QH 9H KC +TC 7H 4H 8H 3H TD 6S AC 7C 2S +QS 9D 5D 3C JC KS 4D 6C JH 2S +9S 6S 3C 7H TS 4C KD 6D 3D 9C +2D 9H AH AC 7H 2S JH 3S 7C QC +QD 9H 3C 2H AC AS 8S KD 8C KH +2D 7S TD TH 6D JD 8D 4D 2H 5S +8S QH KD JD QS JH 4D KC 5H 3S +3C KH QC 6D 8H 3S AH 7D TD 2D +5S 9H QH 4S 6S 6C 6D TS TH 7S +6C 4C 6D QS JS 9C TS 3H 8D 8S +JS 5C 7S AS 2C AH 2H AD 5S TC +KD 6C 9C 9D TS 2S JC 4H 2C QD +QS 9H TC 3H KC KS 4H 3C AD TH +KH 9C 2H KD 9D TC 7S KC JH 2D +7C 3S KC AS 8C 5D 9C 9S QH 3H +2D 8C TD 4C 2H QC 5D TC 2C 7D +KS 4D 6C QH TD KH 5D 7C AD 8D +2S 9S 8S 4C 8C 3D 6H QD 7C 7H +6C 8S QH 5H TS 5C 3C 4S 2S 2H +8S 6S 2H JC 3S 3H 9D 8C 2S 7H +QC 2C 8H 9C AC JD 4C 4H 6S 3S +3H 3S 7D 4C 9S 5H 8H JC 3D TC +QH 2S 2D 9S KD QD 9H AD 6D 9C +8D 2D KS 9S JC 4C JD KC 4S TH +KH TS 6D 4D 5C KD 5H AS 9H AD +QD JS 7C 6D 5D 5C TH 5H QH QS +9D QH KH 5H JH 4C 4D TC TH 6C +KH AS TS 9D KD 9C 7S 4D 8H 5S +KH AS 2S 7D 9D 4C TS TH AH 7C +KS 4D AC 8S 9S 8D TH QH 9D 5C +5D 5C 8C QS TC 4C 3D 3S 2C 8D +9D KS 2D 3C KC 4S 8C KH 6C JC +8H AH 6H 7D 7S QD 3C 4C 6C KC +3H 2C QH 8H AS 7D 4C 8C 4H KC +QD 5S 4H 2C TD AH JH QH 4C 8S +3H QS 5S JS 8H 2S 9H 9C 3S 2C +6H TS 7S JC QD AC TD KC 5S 3H +QH AS QS 7D JC KC 2C 4C 5C 5S +QH 3D AS JS 4H 8D 7H JC 2S 9C +5D 4D 2S 4S 9D 9C 2D QS 8H 7H +6D 7H 3H JS TS AC 2D JH 7C 8S +JH 5H KC 3C TC 5S 9H 4C 8H 9D +8S KC 5H 9H AD KS 9D KH 8D AH +JC 2H 9H KS 6S 3H QC 5H AH 9C +5C KH 5S AD 6C JC 9H QC 9C TD +5S 5D JC QH 2D KS 8H QS 2H TS +JH 5H 5S AH 7H 3C 8S AS TD KH +6H 3D JD 2C 4C KC 7S AH 6C JH +4C KS 9D AD 7S KC 7D 8H 3S 9C +7H 5C 5H 3C 8H QC 3D KH 6D JC +2D 4H 5D 7D QC AD AH 9H QH 8H +KD 8C JS 9D 3S 3C 2H 5D 6D 2S +8S 6S TS 3C 6H 8D 5S 3H TD 6C +KS 3D JH 9C 7C 9S QS 5S 4H 6H +7S 6S TH 4S KC KD 3S JC JH KS +7C 3C 2S 6D QH 2C 7S 5H 8H AH +KC 8D QD 6D KH 5C 7H 9D 3D 9C +6H 2D 8S JS 9S 2S 6D KC 7C TC +KD 9C JH 7H KC 8S 2S 7S 3D 6H +4H 9H 2D 4C 8H 7H 5S 8S 2H 8D +AD 7C 3C 7S 5S 4D 9H 3D JC KH +5D AS 7D 6D 9C JC 4C QH QS KH +KD JD 7D 3D QS QC 8S 6D JS QD +6S 8C 5S QH TH 9H AS AC 2C JD +QC KS QH 7S 3C 4C 5C KC 5D AH +6C 4H 9D AH 2C 3H KD 3D TS 5C +TD 8S QS AS JS 3H KD AC 4H KS +7D 5D TS 9H 4H 4C 9C 2H 8C QC +2C 7D 9H 4D KS 4C QH AD KD JS +QD AD AH KH 9D JS 9H JC KD JD +8S 3C 4S TS 7S 4D 5C 2S 6H 7C +JS 7S 5C KD 6D QH 8S TD 2H 6S +QH 6C TC 6H TD 4C 9D 2H QC 8H +3D TS 4D 2H 6H 6S 2C 7H 8S 6C +9H 9D JD JH 3S AH 2C 6S 3H 8S +2C QS 8C 5S 3H 2S 7D 3C AD 4S +5C QC QH AS TS 4S 6S 4C 5H JS +JH 5C TD 4C 6H JS KD KH QS 4H +TC KH JC 4D 9H 9D 8D KC 3C 8H +2H TC 8S AD 9S 4H TS 7H 2C 5C +4H 2S 6C 5S KS AH 9C 7C 8H KD +TS QH TD QS 3C JH AH 2C 8D 7D +5D KC 3H 5S AC 4S 7H QS 4C 2H +3D 7D QC KH JH 6D 6C TD TH KD +5S 8D TH 6C 9D 7D KH 8C 9S 6D +JD QS 7S QC 2S QH JC 4S KS 8D +7S 5S 9S JD KD 9C JC AD 2D 7C +4S 5H AH JH 9C 5D TD 7C 2D 6S +KC 6C 7H 6S 9C QD 5S 4H KS TD +6S 8D KS 2D TH TD 9H JD TS 3S +KH JS 4H 5D 9D TC TD QC JD TS +QS QD AC AD 4C 6S 2D AS 3H KC +4C 7C 3C TD QS 9C KC AS 8D AD +KC 7H QC 6D 8H 6S 5S AH 7S 8C +3S AD 9H JC 6D JD AS KH 6S JH +AD 3D TS KS 7H JH 2D JS QD AC +9C JD 7C 6D TC 6H 6C JC 3D 3S +QC KC 3S JC KD 2C 8D AH QS TS +AS KD 3D JD 8H 7C 8C 5C QD 6C diff --git a/project_euler/problem_054/sol1.py b/project_euler/problem_054/sol1.py new file mode 100644 index 000000000000..d2fd810d1b69 --- /dev/null +++ b/project_euler/problem_054/sol1.py @@ -0,0 +1,381 @@ +""" +Problem: https://projecteuler.net/problem=54 + +In the card game poker, a hand consists of five cards and are ranked, +from lowest to highest, in the following way: + +High Card: Highest value card. +One Pair: Two cards of the same value. +Two Pairs: Two different pairs. +Three of a Kind: Three cards of the same value. +Straight: All cards are consecutive values. +Flush: All cards of the same suit. +Full House: Three of a kind and a pair. +Four of a Kind: Four cards of the same value. +Straight Flush: All cards are consecutive values of same suit. +Royal Flush: Ten, Jack, Queen, King, Ace, in same suit. + +The cards are valued in the order: +2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace. + +If two players have the same ranked hands then the rank made up of the highest +value wins; for example, a pair of eights beats a pair of fives. +But if two ranks tie, for example, both players have a pair of queens, then highest +cards in each hand are compared; if the highest cards tie then the next highest +cards are compared, and so on. + +The file, poker.txt, contains one-thousand random hands dealt to two players. +Each line of the file contains ten cards (separated by a single space): the +first five are Player 1's cards and the last five are Player 2's cards. +You can assume that all hands are valid (no invalid characters or repeated cards), +each player's hand is in no specific order, and in each hand there is a clear winner. + +How many hands does Player 1 win? + +Resources used: +https://en.wikipedia.org/wiki/Texas_hold_%27em +https://en.wikipedia.org/wiki/List_of_poker_hands + +Similar problem on codewars: +https://www.codewars.com/kata/ranking-poker-hands +https://www.codewars.com/kata/sortable-poker-hands +""" +from __future__ import annotations + +import os + + +class PokerHand: + """Create an object representing a Poker Hand based on an input of a + string which represents the best 5 card combination from the player's hand + and board cards. + + Attributes: (read-only) + hand: string representing the hand consisting of five cards + + Methods: + compare_with(opponent): takes in player's hand (self) and + opponent's hand (opponent) and compares both hands according to + the rules of Texas Hold'em. + Returns one of 3 strings (Win, Loss, Tie) based on whether + player's hand is better than opponent's hand. + + hand_name(): Returns a string made up of two parts: hand name + and high card. + + Supported operators: + Rich comparison operators: <, >, <=, >=, ==, != + + Supported builtin methods and functions: + list.sort(), sorted() + """ + + _HAND_NAME = [ + "High card", + "One pair", + "Two pairs", + "Three of a kind", + "Straight", + "Flush", + "Full house", + "Four of a kind", + "Straight flush", + "Royal flush", + ] + + _CARD_NAME = [ + "", # placeholder as lists are zero indexed + "One", + "Two", + "Three", + "Four", + "Five", + "Six", + "Seven", + "Eight", + "Nine", + "Ten", + "Jack", + "Queen", + "King", + "Ace", + ] + + def __init__(self, hand: str) -> None: + """ + Initialize hand. + Hand should of type str and should contain only five cards each + separated by a space. + + The cards should be of the following format: + [card value][card suit] + + The first character is the value of the card: + 2, 3, 4, 5, 6, 7, 8, 9, T(en), J(ack), Q(ueen), K(ing), A(ce) + + The second character represents the suit: + S(pades), H(earts), D(iamonds), C(lubs) + + For example: "6S 4C KC AS TH" + """ + if not isinstance(hand, str): + raise TypeError(f"Hand should be of type 'str': {hand!r}") + # split removes duplicate whitespaces so no need of strip + if len(hand.split(" ")) != 5: + raise ValueError(f"Hand should contain only 5 cards: {hand!r}") + self._hand = hand + self._first_pair = 0 + self._second_pair = 0 + self._card_values, self._card_suit = self._internal_state() + self._hand_type = self._get_hand_type() + self._high_card = self._card_values[0] + + @property + def hand(self): + """Returns the self hand""" + return self._hand + + def compare_with(self, other: "PokerHand") -> str: + """ + Determines the outcome of comparing self hand with other hand. + Returns the output as 'Win', 'Loss', 'Tie' according to the rules of + Texas Hold'em. + + Here are some examples: + >>> player = PokerHand("2H 3H 4H 5H 6H") # Stright flush + >>> opponent = PokerHand("KS AS TS QS JS") # Royal flush + >>> player.compare_with(opponent) + 'Loss' + + >>> player = PokerHand("2S AH 2H AS AC") # Full house + >>> opponent = PokerHand("2H 3H 5H 6H 7H") # Flush + >>> player.compare_with(opponent) + 'Win' + + >>> player = PokerHand("2S AH 4H 5S 6C") # High card + >>> opponent = PokerHand("AD 4C 5H 6H 2C") # High card + >>> player.compare_with(opponent) + 'Tie' + """ + # Breaking the tie works on the following order of precedence: + # 1. First pair (default 0) + # 2. Second pair (default 0) + # 3. Compare all cards in reverse order because they are sorted. + + # First pair and second pair will only be a non-zero value if the card + # type is either from the following: + # 21: Four of a kind + # 20: Full house + # 17: Three of a kind + # 16: Two pairs + # 15: One pair + if self._hand_type > other._hand_type: + return "Win" + elif self._hand_type < other._hand_type: + return "Loss" + elif self._first_pair == other._first_pair: + if self._second_pair == other._second_pair: + return self._compare_cards(other) + else: + return "Win" if self._second_pair > other._second_pair else "Loss" + return "Win" if self._first_pair > other._first_pair else "Loss" + + # This function is not part of the problem, I did it just for fun + def hand_name(self) -> str: + """ + Return the name of the hand in the following format: + 'hand name, high card' + + Here are some examples: + >>> PokerHand("KS AS TS QS JS").hand_name() + 'Royal flush' + + >>> PokerHand("2D 6D 3D 4D 5D").hand_name() + 'Straight flush, Six-high' + + >>> PokerHand("JC 6H JS JD JH").hand_name() + 'Four of a kind, Jacks' + + >>> PokerHand("3D 2H 3H 2C 2D").hand_name() + 'Full house, Twos over Threes' + + >>> PokerHand("2H 4D 3C AS 5S").hand_name() # Low ace + 'Straight, Five-high' + + Source: https://en.wikipedia.org/wiki/List_of_poker_hands + """ + name = PokerHand._HAND_NAME[self._hand_type - 14] + high = PokerHand._CARD_NAME[self._high_card] + pair1 = PokerHand._CARD_NAME[self._first_pair] + pair2 = PokerHand._CARD_NAME[self._second_pair] + if self._hand_type in [22, 19, 18]: + return name + f", {high}-high" + elif self._hand_type in [21, 17, 15]: + return name + f", {pair1}s" + elif self._hand_type in [20, 16]: + join = "over" if self._hand_type == 20 else "and" + return name + f", {pair1}s {join} {pair2}s" + elif self._hand_type == 23: + return name + else: + return name + f", {high}" + + def _compare_cards(self, other: "PokerHand") -> str: + # Enumerate gives us the index as well as the element of a list + for index, card_value in enumerate(self._card_values): + if card_value != other._card_values[index]: + return "Win" if card_value > other._card_values[index] else "Loss" + return "Tie" + + def _get_hand_type(self) -> int: + # Number representing the type of hand internally: + # 23: Royal flush + # 22: Straight flush + # 21: Four of a kind + # 20: Full house + # 19: Flush + # 18: Straight + # 17: Three of a kind + # 16: Two pairs + # 15: One pair + # 14: High card + if self._is_flush(): + if self._is_five_high_straight() or self._is_straight(): + return 23 if sum(self._card_values) == 60 else 22 + return 19 + elif self._is_five_high_straight() or self._is_straight(): + return 18 + return 14 + self._is_same_kind() + + def _is_flush(self) -> bool: + return len(self._card_suit) == 1 + + def _is_five_high_straight(self) -> bool: + # If a card is a five high straight (low ace) change the location of + # ace from the start of the list to the end. Check whether the first + # element is ace or not. (Don't want to change again) + # Five high straight (low ace): AH 2H 3S 4C 5D + # Why use sorted here? One call to this function will mutate the list to + # [5, 4, 3, 2, 14] and so for subsequent calls (which will be rare) we + # need to compare the sorted version. + # Refer test_multiple_calls_five_high_straight in test_poker_hand.py + if sorted(self._card_values) == [2, 3, 4, 5, 14]: + if self._card_values[0] == 14: + # Remember, our list is sorted in reverse order + ace_card = self._card_values.pop(0) + self._card_values.append(ace_card) + return True + return False + + def _is_straight(self) -> bool: + for i in range(4): + if self._card_values[i] - self._card_values[i + 1] != 1: + return False + return True + + def _is_same_kind(self) -> int: + # Kind Values for internal use: + # 7: Four of a kind + # 6: Full house + # 3: Three of a kind + # 2: Two pairs + # 1: One pair + # 0: False + kind = val1 = val2 = 0 + for i in range(4): + # Compare two cards at a time, if they are same increase 'kind', + # add the value of the card to val1, if it is repeating again we + # will add 2 to 'kind' as there are now 3 cards with same value. + # If we get card of different value than val1, we will do the same + # thing with val2 + if self._card_values[i] == self._card_values[i + 1]: + if not val1: + val1 = self._card_values[i] + kind += 1 + elif val1 == self._card_values[i]: + kind += 2 + elif not val2: + val2 = self._card_values[i] + kind += 1 + elif val2 == self._card_values[i]: + kind += 2 + # For consistency in hand type (look at note in _get_hand_type function) + kind = kind + 2 if kind in [4, 5] else kind + # first meaning first pair to compare in 'compare_with' + first = max(val1, val2) + second = min(val1, val2) + # If it's full house (three count pair + two count pair), make sure + # first pair is three count and if not then switch them both. + if kind == 6 and self._card_values.count(first) != 3: + first, second = second, first + self._first_pair = first + self._second_pair = second + return kind + + def _internal_state(self) -> tuple[list[int], set[str]]: + # Internal representation of hand as a list of card values and + # a set of card suit + trans: dict = {"T": "10", "J": "11", "Q": "12", "K": "13", "A": "14"} + new_hand = self._hand.translate(str.maketrans(trans)).split() + card_values = [int(card[:-1]) for card in new_hand] + card_suit = {card[-1] for card in new_hand} + return sorted(card_values, reverse=True), card_suit + + def __repr__(self): + return f'{self.__class__}("{self._hand}")' + + def __str__(self): + return self._hand + + # Rich comparison operators (used in list.sort() and sorted() builtin functions) + # Note that this is not part of the problem but another extra feature where + # if you have a list of PokerHand objects, you can sort them just through + # the builtin functions. + def __eq__(self, other): + if isinstance(other, PokerHand): + return self.compare_with(other) == "Tie" + return NotImplemented + + def __lt__(self, other): + if isinstance(other, PokerHand): + return self.compare_with(other) == "Loss" + return NotImplemented + + def __le__(self, other): + if isinstance(other, PokerHand): + return self < other or self == other + return NotImplemented + + def __gt__(self, other): + if isinstance(other, PokerHand): + return not self < other and self != other + return NotImplemented + + def __ge__(self, other): + if isinstance(other, PokerHand): + return not self < other + return NotImplemented + + def __hash__(self): + return object.__hash__(self) + + +def solution() -> int: + # Solution for problem number 54 from Project Euler + # Input from poker_hands.txt file + answer = 0 + script_dir = os.path.abspath(os.path.dirname(__file__)) + poker_hands = os.path.join(script_dir, "poker_hands.txt") + with open(poker_hands) as file_hand: + for line in file_hand: + player_hand = line[:14].strip() + opponent_hand = line[15:].strip() + player, opponent = PokerHand(player_hand), PokerHand(opponent_hand) + output = player.compare_with(opponent) + if output == "Win": + answer += 1 + return answer + + +if __name__ == "__main__": + solution() diff --git a/project_euler/problem_054/test_poker_hand.py b/project_euler/problem_054/test_poker_hand.py new file mode 100644 index 000000000000..96317fc7df33 --- /dev/null +++ b/project_euler/problem_054/test_poker_hand.py @@ -0,0 +1,228 @@ +import os +from itertools import chain +from random import randrange, shuffle + +import pytest + +from .sol1 import PokerHand + +SORTED_HANDS = ( + "4S 3H 2C 7S 5H", + "9D 8H 2C 6S 7H", + "2D 6D 9D TH 7D", + "TC 8C 2S JH 6C", + "JH 8S TH AH QH", + "TS KS 5S 9S AC", + "KD 6S 9D TH AD", + "KS 8D 4D 9S 4S", # pair + "8C 4S KH JS 4D", # pair + "QH 8H KD JH 8S", # pair + "KC 4H KS 2H 8D", # pair + "KD 4S KC 3H 8S", # pair + "AH 8S AS KC JH", # pair + "3H 4C 4H 3S 2H", # 2 pairs + "5S 5D 2C KH KH", # 2 pairs + "3C KH 5D 5S KH", # 2 pairs + "AS 3C KH AD KH", # 2 pairs + "7C 7S 3S 7H 5S", # 3 of a kind + "7C 7S KH 2H 7H", # 3 of a kind + "AC KH QH AH AS", # 3 of a kind + "2H 4D 3C AS 5S", # straight (low ace) + "3C 5C 4C 2C 6H", # straight + "6S 8S 7S 5H 9H", # straight + "JS QS 9H TS KH", # straight + "QC KH TS JS AH", # straight (high ace) + "8C 9C 5C 3C TC", # flush + "3S 8S 9S 5S KS", # flush + "4C 5C 9C 8C KC", # flush + "JH 8H AH KH QH", # flush + "3D 2H 3H 2C 2D", # full house + "2H 2C 3S 3H 3D", # full house + "KH KC 3S 3H 3D", # full house + "JC 6H JS JD JH", # 4 of a kind + "JC 7H JS JD JH", # 4 of a kind + "JC KH JS JD JH", # 4 of a kind + "2S AS 4S 5S 3S", # straight flush (low ace) + "2D 6D 3D 4D 5D", # straight flush + "5C 6C 3C 7C 4C", # straight flush + "JH 9H TH KH QH", # straight flush + "JH AH TH KH QH", # royal flush (high ace straight flush) +) + +TEST_COMPARE = ( + ("2H 3H 4H 5H 6H", "KS AS TS QS JS", "Loss"), + ("2H 3H 4H 5H 6H", "AS AD AC AH JD", "Win"), + ("AS AH 2H AD AC", "JS JD JC JH 3D", "Win"), + ("2S AH 2H AS AC", "JS JD JC JH AD", "Loss"), + ("2S AH 2H AS AC", "2H 3H 5H 6H 7H", "Win"), + ("AS 3S 4S 8S 2S", "2H 3H 5H 6H 7H", "Win"), + ("2H 3H 5H 6H 7H", "2S 3H 4H 5S 6C", "Win"), + ("2S 3H 4H 5S 6C", "3D 4C 5H 6H 2S", "Tie"), + ("2S 3H 4H 5S 6C", "AH AC 5H 6H AS", "Win"), + ("2S 2H 4H 5S 4C", "AH AC 5H 6H AS", "Loss"), + ("2S 2H 4H 5S 4C", "AH AC 5H 6H 7S", "Win"), + ("6S AD 7H 4S AS", "AH AC 5H 6H 7S", "Loss"), + ("2S AH 4H 5S KC", "AH AC 5H 6H 7S", "Loss"), + ("2S 3H 6H 7S 9C", "7H 3C TH 6H 9S", "Loss"), + ("4S 5H 6H TS AC", "3S 5H 6H TS AC", "Win"), + ("2S AH 4H 5S 6C", "AD 4C 5H 6H 2C", "Tie"), + ("AS AH 3H AD AC", "AS AH 2H AD AC", "Win"), + ("AH AC 5H 5C QS", "AH AC 5H 5C KS", "Loss"), + ("AH AC 5H 5C QS", "KH KC 5H 5C QS", "Win"), + ("7C 7S KH 2H 7H", "3C 3S AH 2H 3H", "Win"), + ("3C 3S AH 2H 3H", "7C 7S KH 2H 7H", "Loss"), + ("6H 5H 4H 3H 2H", "5H 4H 3H 2H AH", "Win"), + ("5H 4H 3H 2H AH", "5H 4H 3H 2H AH", "Tie"), + ("5H 4H 3H 2H AH", "6H 5H 4H 3H 2H", "Loss"), + ("AH AD KS KC AC", "AH KD KH AC KC", "Win"), + ("2H 4D 3C AS 5S", "2H 4D 3C 6S 5S", "Loss"), + ("2H 3S 3C 3H 2S", "3S 3C 2S 2H 2D", "Win"), + ("4D 6D 5D 2D JH", "3S 8S 3H TC KH", "Loss"), + ("4S 6C 8S 3S 7S", "AD KS 2D 7D 7C", "Loss"), + ("6S 4C 7H 8C 3H", "5H JC AH 9D 9C", "Loss"), + ("9D 9H JH TC QH", "3C 2S JS 5C 7H", "Win"), + ("2H TC 8S AD 9S", "4H TS 7H 2C 5C", "Win"), + ("9D 3S 2C 7S 7C", "JC TD 3C TC 9H", "Loss"), +) + +TEST_FLUSH = ( + ("2H 3H 4H 5H 6H", True), + ("AS AH 2H AD AC", False), + ("2H 3H 5H 6H 7H", True), + ("KS AS TS QS JS", True), + ("8H 9H QS JS TH", False), + ("AS 3S 4S 8S 2S", True), +) + +TEST_STRAIGHT = ( + ("2H 3H 4H 5H 6H", True), + ("AS AH 2H AD AC", False), + ("2H 3H 5H 6H 7H", False), + ("KS AS TS QS JS", True), + ("8H 9H QS JS TH", True), +) + +TEST_FIVE_HIGH_STRAIGHT = ( + ("2H 4D 3C AS 5S", True, [5, 4, 3, 2, 14]), + ("2H 5D 3C AS 5S", False, [14, 5, 5, 3, 2]), + ("JH QD KC AS TS", False, [14, 13, 12, 11, 10]), + ("9D 3S 2C 7S 7C", False, [9, 7, 7, 3, 2]), +) + +TEST_KIND = ( + ("JH AH TH KH QH", 0), + ("JH 9H TH KH QH", 0), + ("JC KH JS JD JH", 7), + ("KH KC 3S 3H 3D", 6), + ("8C 9C 5C 3C TC", 0), + ("JS QS 9H TS KH", 0), + ("7C 7S KH 2H 7H", 3), + ("3C KH 5D 5S KH", 2), + ("QH 8H KD JH 8S", 1), + ("2D 6D 9D TH 7D", 0), +) + +TEST_TYPES = ( + ("JH AH TH KH QH", 23), + ("JH 9H TH KH QH", 22), + ("JC KH JS JD JH", 21), + ("KH KC 3S 3H 3D", 20), + ("8C 9C 5C 3C TC", 19), + ("JS QS 9H TS KH", 18), + ("7C 7S KH 2H 7H", 17), + ("3C KH 5D 5S KH", 16), + ("QH 8H KD JH 8S", 15), + ("2D 6D 9D TH 7D", 14), +) + + +def generate_random_hand(): + play, oppo = randrange(len(SORTED_HANDS)), randrange(len(SORTED_HANDS)) + expected = ["Loss", "Tie", "Win"][(play >= oppo) + (play > oppo)] + hand, other = SORTED_HANDS[play], SORTED_HANDS[oppo] + return hand, other, expected + + +def generate_random_hands(number_of_hands: int = 100): + return (generate_random_hand() for _ in range(number_of_hands)) + + +@pytest.mark.parametrize("hand, expected", TEST_FLUSH) +def test_hand_is_flush(hand, expected): + assert PokerHand(hand)._is_flush() == expected + + +@pytest.mark.parametrize("hand, expected", TEST_STRAIGHT) +def test_hand_is_straight(hand, expected): + assert PokerHand(hand)._is_straight() == expected + + +@pytest.mark.parametrize("hand, expected, card_values", TEST_FIVE_HIGH_STRAIGHT) +def test_hand_is_five_high_straight(hand, expected, card_values): + player = PokerHand(hand) + assert player._is_five_high_straight() == expected + assert player._card_values == card_values + + +@pytest.mark.parametrize("hand, expected", TEST_KIND) +def test_hand_is_same_kind(hand, expected): + assert PokerHand(hand)._is_same_kind() == expected + + +@pytest.mark.parametrize("hand, expected", TEST_TYPES) +def test_hand_values(hand, expected): + assert PokerHand(hand)._hand_type == expected + + +@pytest.mark.parametrize("hand, other, expected", TEST_COMPARE) +def test_compare_simple(hand, other, expected): + assert PokerHand(hand).compare_with(PokerHand(other)) == expected + + +@pytest.mark.parametrize("hand, other, expected", generate_random_hands()) +def test_compare_random(hand, other, expected): + assert PokerHand(hand).compare_with(PokerHand(other)) == expected + + +def test_hand_sorted(): + POKER_HANDS = [PokerHand(hand) for hand in SORTED_HANDS] + list_copy = POKER_HANDS.copy() + shuffle(list_copy) + user_sorted = chain(sorted(list_copy)) + for index, hand in enumerate(user_sorted): + assert hand == POKER_HANDS[index] + + +def test_custom_sort_five_high_straight(): + # Test that five high straights are compared correctly. + pokerhands = [PokerHand("2D AC 3H 4H 5S"), PokerHand("2S 3H 4H 5S 6C")] + pokerhands.sort(reverse=True) + assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C" + + +def test_multiple_calls_five_high_straight(): + # Multiple calls to five_high_straight function should still return True + # and shouldn't mutate the list in every call other than the first. + pokerhand = PokerHand("2C 4S AS 3D 5C") + expected = True + expected_card_values = [5, 4, 3, 2, 14] + for _ in range(10): + assert pokerhand._is_five_high_straight() == expected + assert pokerhand._card_values == expected_card_values + + +def test_euler_project(): + # Problem number 54 from Project Euler + # Testing from poker_hands.txt file + answer = 0 + script_dir = os.path.abspath(os.path.dirname(__file__)) + poker_hands = os.path.join(script_dir, "poker_hands.txt") + with open(poker_hands) as file_hand: + for line in file_hand: + player_hand = line[:14].strip() + opponent_hand = line[15:].strip() + player, opponent = PokerHand(player_hand), PokerHand(opponent_hand) + output = player.compare_with(opponent) + if output == "Win": + answer += 1 + assert answer == 376 diff --git a/project_euler/problem_055/__init__.py b/project_euler/problem_055/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_055/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_055/sol1.py b/project_euler/problem_055/sol1.py new file mode 100644 index 000000000000..14e411541f3a --- /dev/null +++ b/project_euler/problem_055/sol1.py @@ -0,0 +1,81 @@ +""" +Lychrel numbers +Problem 55: https://projecteuler.net/problem=55 + +If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. + +Not all numbers produce palindromes so quickly. For example, +349 + 943 = 1292, +1292 + 2921 = 4213 +4213 + 3124 = 7337 +That is, 349 took three iterations to arrive at a palindrome. + +Although no one has proved it yet, it is thought that some numbers, like 196, +never produce a palindrome. A number that never forms a palindrome through the +reverse and add process is called a Lychrel number. Due to the theoretical nature +of these numbers, and for the purpose of this problem, we shall assume that a number +is Lychrel until proven otherwise. In addition you are given that for every number +below ten-thousand, it will either (i) become a palindrome in less than fifty +iterations, or, (ii) no one, with all the computing power that exists, has managed +so far to map it to a palindrome. In fact, 10677 is the first number to be shown +to require over fifty iterations before producing a palindrome: +4668731596684224866951378664 (53 iterations, 28-digits). + +Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; +the first example is 4994. +How many Lychrel numbers are there below ten-thousand? +""" + + +def is_palindrome(n: int) -> bool: + """ + Returns True if a number is palindrome. + >>> is_palindrome(12567321) + False + >>> is_palindrome(1221) + True + >>> is_palindrome(9876789) + True + """ + return str(n) == str(n)[::-1] + + +def sum_reverse(n: int) -> int: + """ + Returns the sum of n and reverse of n. + >>> sum_reverse(123) + 444 + >>> sum_reverse(3478) + 12221 + >>> sum_reverse(12) + 33 + """ + return int(n) + int(str(n)[::-1]) + + +def solution(limit: int = 10000) -> int: + """ + Returns the count of all lychrel numbers below limit. + >>> solution(10000) + 249 + >>> solution(5000) + 76 + >>> solution(1000) + 13 + """ + lychrel_nums = [] + for num in range(1, limit): + iterations = 0 + a = num + while iterations < 50: + num = sum_reverse(num) + iterations += 1 + if is_palindrome(num): + break + else: + lychrel_nums.append(a) + return len(lychrel_nums) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_056/__init__.py b/project_euler/problem_056/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_056/sol1.py b/project_euler/problem_056/sol1.py new file mode 100644 index 000000000000..8eaa6e553342 --- /dev/null +++ b/project_euler/problem_056/sol1.py @@ -0,0 +1,45 @@ +""" +Project Euler Problem 56: https://projecteuler.net/problem=56 + +A googol (10^100) is a massive number: one followed by one-hundred zeros; +100^100 is almost unimaginably large: one followed by two-hundred zeros. +Despite their size, the sum of the digits in each number is only 1. + +Considering natural numbers of the form, ab, where a, b < 100, +what is the maximum digital sum? +""" + + +def solution(a: int = 100, b: int = 100) -> int: + """ + Considering natural numbers of the form, a**b, where a, b < 100, + what is the maximum digital sum? + :param a: + :param b: + :return: + >>> solution(10,10) + 45 + + >>> solution(100,100) + 972 + + >>> solution(100,200) + 1872 + """ + + # RETURN the MAXIMUM from the list of SUMs of the list of INT converted from STR of + # BASE raised to the POWER + return max( + [ + sum([int(x) for x in str(base ** power)]) + for base in range(a) + for power in range(b) + ] + ) + + +# Tests +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/project_euler/problem_057/__init__.py b/project_euler/problem_057/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_057/sol1.py b/project_euler/problem_057/sol1.py new file mode 100644 index 000000000000..04b6199f4717 --- /dev/null +++ b/project_euler/problem_057/sol1.py @@ -0,0 +1,48 @@ +""" +Project Euler Problem 57: https://projecteuler.net/problem=57 +It is possible to show that the square root of two can be expressed as an infinite +continued fraction. + +sqrt(2) = 1 + 1 / (2 + 1 / (2 + 1 / (2 + ...))) + +By expanding this for the first four iterations, we get: +1 + 1 / 2 = 3 / 2 = 1.5 +1 + 1 / (2 + 1 / 2} = 7 / 5 = 1.4 +1 + 1 / (2 + 1 / (2 + 1 / 2)) = 17 / 12 = 1.41666... +1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / 2))) = 41/ 29 = 1.41379... + +The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, +1393/985, is the first example where the number of digits in the numerator exceeds +the number of digits in the denominator. + +In the first one-thousand expansions, how many fractions contain a numerator with +more digits than the denominator? +""" + + +def solution(n: int = 1000) -> int: + """ + returns number of fractions containing a numerator with more digits than + the denominator in the first n expansions. + >>> solution(14) + 2 + >>> solution(100) + 15 + >>> solution(10000) + 1508 + """ + prev_numerator, prev_denominator = 1, 1 + result = [] + for i in range(1, n + 1): + numerator = prev_numerator + 2 * prev_denominator + denominator = prev_numerator + prev_denominator + if len(str(numerator)) > len(str(denominator)): + result.append(i) + prev_numerator = numerator + prev_denominator = denominator + + return len(result) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_058/__init__.py b/project_euler/problem_058/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_058/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_058/sol1.py b/project_euler/problem_058/sol1.py new file mode 100644 index 000000000000..d3b15157fbbd --- /dev/null +++ b/project_euler/problem_058/sol1.py @@ -0,0 +1,86 @@ +""" +Project Euler Problem 58:https://projecteuler.net/problem=58 + + +Starting with 1 and spiralling anticlockwise in the following way, +a square spiral with side length 7 is formed. + +37 36 35 34 33 32 31 +38 17 16 15 14 13 30 +39 18 5 4 3 12 29 +40 19 6 1 2 11 28 +41 20 7 8 9 10 27 +42 21 22 23 24 25 26 +43 44 45 46 47 48 49 + +It is interesting to note that the odd squares lie along the bottom right +diagonal ,but what is more interesting is that 8 out of the 13 numbers +lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%. + +If one complete new layer is wrapped around the spiral above, +a square spiral with side length 9 will be formed. +If this process is continued, +what is the side length of the square spiral for which +the ratio of primes along both diagonals first falls below 10%? + +Solution: We have to find an odd length side for which square falls below +10%. With every layer we add 4 elements are being added to the diagonals +,lets say we have a square spiral of odd length with side length j, +then if we move from j to j+2, we are adding j*j+j+1,j*j+2*(j+1),j*j+3*(j+1) +j*j+4*(j+1). Out of these 4 only the first three can become prime +because last one reduces to (j+2)*(j+2). +So we check individually each one of these before incrementing our +count of current primes. + +""" + + +def isprime(d: int) -> int: + """ + returns whether the given digit is prime or not + >>> isprime(1) + 0 + >>> isprime(17) + 1 + >>> isprime(10000) + 0 + """ + if d == 1: + return 0 + + i = 2 + while i * i <= d: + if d % i == 0: + return 0 + i = i + 1 + return 1 + + +def solution(ratio: float = 0.1) -> int: + """ + returns the side length of the square spiral of odd length greater + than 1 for which the ratio of primes along both diagonals + first falls below the given ratio. + >>> solution(.5) + 11 + >>> solution(.2) + 309 + >>> solution(.111) + 11317 + """ + + j = 3 + primes = 3 + + while primes / (2 * j - 1) >= ratio: + for i in range(j * j + j + 1, (j + 2) * (j + 2), j + 1): + primes = primes + isprime(i) + + j = j + 2 + return j + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/project_euler/problem_059/__init__.py b/project_euler/problem_059/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_059/p059_cipher.txt b/project_euler/problem_059/p059_cipher.txt new file mode 100644 index 000000000000..b3b3247298d1 --- /dev/null +++ b/project_euler/problem_059/p059_cipher.txt @@ -0,0 +1 @@ +36,22,80,0,0,4,23,25,19,17,88,4,4,19,21,11,88,22,23,23,29,69,12,24,0,88,25,11,12,2,10,28,5,6,12,25,10,22,80,10,30,80,10,22,21,69,23,22,69,61,5,9,29,2,66,11,80,8,23,3,17,88,19,0,20,21,7,10,17,17,29,20,69,8,17,21,29,2,22,84,80,71,60,21,69,11,5,8,21,25,22,88,3,0,10,25,0,10,5,8,88,2,0,27,25,21,10,31,6,25,2,16,21,82,69,35,63,11,88,4,13,29,80,22,13,29,22,88,31,3,88,3,0,10,25,0,11,80,10,30,80,23,29,19,12,8,2,10,27,17,9,11,45,95,88,57,69,16,17,19,29,80,23,29,19,0,22,4,9,1,80,3,23,5,11,28,92,69,9,5,12,12,21,69,13,30,0,0,0,0,27,4,0,28,28,28,84,80,4,22,80,0,20,21,2,25,30,17,88,21,29,8,2,0,11,3,12,23,30,69,30,31,23,88,4,13,29,80,0,22,4,12,10,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,65,69,83,80,84,87,68,69,83,80,84,87,73,69,83,80,84,87,65,83,88,91,69,29,4,6,86,92,69,15,24,12,27,24,69,28,21,21,29,30,1,11,80,10,22,80,17,16,21,69,9,5,4,28,2,4,12,5,23,29,80,10,30,80,17,16,21,69,27,25,23,27,28,0,84,80,22,23,80,17,16,17,17,88,25,3,88,4,13,29,80,17,10,5,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,12,11,80,10,26,4,4,17,30,0,28,92,69,30,2,10,21,80,12,12,80,4,12,80,10,22,19,0,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,69,30,31,9,20,31,18,11,94,69,54,17,8,29,28,28,84,80,44,88,24,4,14,21,69,30,31,16,22,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,17,3,69,11,21,23,17,21,22,88,25,22,88,17,69,11,25,29,12,24,69,8,17,23,12,80,10,30,80,17,16,21,69,11,1,16,25,2,0,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,23,22,69,12,24,0,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,67,80,10,10,80,7,1,80,21,13,4,17,17,30,2,88,4,13,29,80,22,13,29,69,23,22,69,12,24,12,11,80,22,29,2,12,29,3,69,29,1,16,25,28,69,12,31,69,11,92,69,17,4,69,16,17,22,88,4,13,29,80,23,25,4,12,23,80,22,9,2,17,80,70,76,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,11,80,17,23,80,84,88,31,3,88,4,13,29,80,21,29,2,12,21,21,17,29,2,69,12,31,69,12,24,0,88,20,12,25,29,0,12,21,23,86,80,44,88,7,12,20,28,69,11,31,10,22,80,22,16,31,18,88,4,13,25,4,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,3,0,10,25,0,11,80,17,23,80,7,29,80,4,8,0,23,23,8,12,21,17,17,29,28,28,88,65,75,78,68,81,65,67,81,72,70,83,64,68,87,74,70,81,75,70,81,67,80,4,22,20,69,30,2,10,21,80,8,13,28,17,17,0,9,1,25,11,31,80,17,16,25,22,88,30,16,21,18,0,10,80,7,1,80,22,17,8,73,88,17,11,28,80,17,16,21,11,88,4,4,19,25,11,31,80,17,16,21,69,11,1,16,25,2,0,88,2,10,23,4,73,88,4,13,29,80,11,13,29,7,29,2,69,75,94,84,76,65,80,65,66,83,77,67,80,64,73,82,65,67,87,75,72,69,17,3,69,17,30,1,29,21,1,88,0,23,23,20,16,27,21,1,84,80,18,16,25,6,16,80,0,0,0,23,29,3,22,29,3,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,35,23,28,9,23,7,12,22,23,69,25,23,4,17,30,69,12,24,0,88,3,4,21,21,69,11,4,0,8,3,69,26,9,69,15,24,12,27,24,69,49,80,13,25,20,69,25,2,23,17,6,0,28,80,4,12,80,17,16,25,22,88,3,16,21,92,69,49,80,13,25,6,0,88,20,12,11,19,10,14,21,23,29,20,69,12,24,4,12,80,17,16,21,69,11,5,8,88,31,3,88,4,13,29,80,22,29,2,12,29,3,69,73,80,78,88,65,74,73,70,69,83,80,84,87,72,84,88,91,69,73,95,87,77,70,69,83,80,84,87,70,87,77,80,78,88,21,17,27,94,69,25,28,22,23,80,1,29,0,0,22,20,22,88,31,11,88,4,13,29,80,20,13,17,1,10,17,17,13,2,0,88,31,3,88,4,13,29,80,6,17,2,6,20,21,75,88,62,4,21,21,9,1,92,69,12,24,0,88,3,16,21,80,10,30,80,17,16,25,22,88,29,16,20,4,12,8,28,12,29,20,69,26,9,69,65,64,69,31,25,19,29,3,69,12,24,0,88,18,12,9,5,4,28,2,4,12,21,69,80,22,10,13,2,17,16,80,21,23,7,0,10,89,69,23,22,69,12,24,0,88,19,12,10,19,16,21,22,0,10,21,11,27,21,69,23,22,69,12,24,0,88,0,0,10,25,8,29,4,0,10,80,10,30,80,4,88,19,12,10,19,9,29,80,18,16,31,22,29,80,1,17,17,8,29,4,0,10,80,12,11,80,84,86,80,36,22,20,69,26,9,69,11,25,8,17,28,4,10,80,23,29,17,22,23,30,12,22,23,69,49,80,13,25,6,0,88,28,12,19,21,18,17,3,0,88,18,0,29,30,69,25,18,9,29,80,17,23,80,1,29,4,0,10,29,12,22,21,69,12,24,0,88,3,16,21,3,69,23,22,69,12,24,0,88,3,16,26,3,0,9,5,0,22,4,69,11,21,23,17,21,22,88,25,11,88,7,13,17,19,13,88,4,13,29,80,0,0,0,10,22,21,11,12,3,69,25,2,0,88,21,19,29,30,69,22,5,8,26,21,23,11,94 \ No newline at end of file diff --git a/project_euler/problem_059/sol1.py b/project_euler/problem_059/sol1.py new file mode 100644 index 000000000000..1f55029b2613 --- /dev/null +++ b/project_euler/problem_059/sol1.py @@ -0,0 +1,128 @@ +""" +Each character on a computer is assigned a unique code and the preferred standard is +ASCII (American Standard Code for Information Interchange). +For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107. + +A modern encryption method is to take a text file, convert the bytes to ASCII, then +XOR each byte with a given value, taken from a secret key. The advantage with the +XOR function is that using the same encryption key on the cipher text, restores +the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65. + +For unbreakable encryption, the key is the same length as the plain text message, and +the key is made up of random bytes. The user would keep the encrypted message and the +encryption key in different locations, and without both "halves", it is impossible to +decrypt the message. + +Unfortunately, this method is impractical for most users, so the modified method is +to use a password as a key. If the password is shorter than the message, which is +likely, the key is repeated cyclically throughout the message. The balance for this +method is using a sufficiently long password key for security, but short enough to +be memorable. + +Your task has been made easy, as the encryption key consists of three lower case +characters. Using p059_cipher.txt (right click and 'Save Link/Target As...'), a +file containing the encrypted ASCII codes, and the knowledge that the plain text +must contain common English words, decrypt the message and find the sum of the ASCII +values in the original text. +""" + + +import string +from itertools import cycle, product +from pathlib import Path +from typing import List, Optional, Set, Tuple + +VALID_CHARS: str = ( + string.ascii_letters + string.digits + string.punctuation + string.whitespace +) +LOWERCASE_INTS: List[int] = [ord(letter) for letter in string.ascii_lowercase] +VALID_INTS: Set[int] = {ord(char) for char in VALID_CHARS} + +COMMON_WORDS: List[str] = ["the", "be", "to", "of", "and", "in", "that", "have"] + + +def try_key(ciphertext: List[int], key: Tuple[int, ...]) -> Optional[str]: + """ + Given an encrypted message and a possible 3-character key, decrypt the message. + If the decrypted message contains a invalid character, i.e. not an ASCII letter, + a digit, punctuation or whitespace, then we know the key is incorrect, so return + None. + >>> try_key([0, 17, 20, 4, 27], (104, 116, 120)) + 'hello' + >>> try_key([68, 10, 300, 4, 27], (104, 116, 120)) is None + True + """ + decoded: str = "" + keychar: int + cipherchar: int + decodedchar: int + + for keychar, cipherchar in zip(cycle(key), ciphertext): + decodedchar = cipherchar ^ keychar + if decodedchar not in VALID_INTS: + return None + decoded += chr(decodedchar) + + return decoded + + +def filter_valid_chars(ciphertext: List[int]) -> List[str]: + """ + Given an encrypted message, test all 3-character strings to try and find the + key. Return a list of the possible decrypted messages. + >>> from itertools import cycle + >>> text = "The enemy's gate is down" + >>> key = "end" + >>> encoded = [ord(k) ^ ord(c) for k,c in zip(cycle(key), text)] + >>> text in filter_valid_chars(encoded) + True + """ + possibles: List[str] = [] + for key in product(LOWERCASE_INTS, repeat=3): + encoded = try_key(ciphertext, key) + if encoded is not None: + possibles.append(encoded) + return possibles + + +def filter_common_word(possibles: List[str], common_word: str) -> List[str]: + """ + Given a list of possible decoded messages, narrow down the possibilities + for checking for the presence of a specified common word. Only decoded messages + containing common_word will be returned. + >>> filter_common_word(['asfla adf', 'I am here', ' !?! #a'], 'am') + ['I am here'] + >>> filter_common_word(['athla amf', 'I am here', ' !?! #a'], 'am') + ['athla amf', 'I am here'] + """ + return [possible for possible in possibles if common_word in possible.lower()] + + +def solution(filename: str = "p059_cipher.txt") -> int: + """ + Test the ciphertext against all possible 3-character keys, then narrow down the + possibilities by filtering using common words until there's only one possible + decoded message. + >>> solution("test_cipher.txt") + 3000 + """ + ciphertext: List[int] + possibles: List[str] + common_word: str + decoded_text: str + data: str = Path(__file__).parent.joinpath(filename).read_text(encoding="utf-8") + + ciphertext = [int(number) for number in data.strip().split(",")] + + possibles = filter_valid_chars(ciphertext) + for common_word in COMMON_WORDS: + possibles = filter_common_word(possibles, common_word) + if len(possibles) == 1: + break + + decoded_text = possibles[0] + return sum([ord(char) for char in decoded_text]) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_059/test_cipher.txt b/project_euler/problem_059/test_cipher.txt new file mode 100644 index 000000000000..27c53740cc1a --- /dev/null +++ b/project_euler/problem_059/test_cipher.txt @@ -0,0 +1 @@ +63,13,28,75,0,23,14,8,0,76,22,89,12,4,13,14,69,16,24,69,29,4,18,23,69,69,59,14,69,11,14,4,29,18 diff --git a/project_euler/problem_06/sol1.py b/project_euler/problem_06/sol1.py deleted file mode 100644 index 852d4e2f9fc4..000000000000 --- a/project_euler/problem_06/sol1.py +++ /dev/null @@ -1,20 +0,0 @@ -# -*- coding: utf-8 -*- -''' -Problem: -The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 -The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 552 = 3025 -Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. -Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. -''' -from __future__ import print_function - -suma = 0 -sumb = 0 -n = int(input()) -for i in range(1,n+1): - suma += i**2 - sumb += i -sum = sumb**2 - suma -print(sum) diff --git a/project_euler/problem_06/sol2.py b/project_euler/problem_06/sol2.py deleted file mode 100644 index aa8aea58fd7b..000000000000 --- a/project_euler/problem_06/sol2.py +++ /dev/null @@ -1,16 +0,0 @@ -# -*- coding: utf-8 -*- -''' -Problem: -The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 -The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 552 = 3025 -Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. -Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. -''' -from __future__ import print_function -n = int(input()) -suma = n*(n+1)/2 -suma **= 2 -sumb = n*(n+1)*(2*n+1)/6 -print(suma-sumb) diff --git a/project_euler/problem_06/sol3.py b/project_euler/problem_06/sol3.py deleted file mode 100644 index b2d9f444d9a9..000000000000 --- a/project_euler/problem_06/sol3.py +++ /dev/null @@ -1,20 +0,0 @@ -''' -Problem: -The sum of the squares of the first ten natural numbers is, - 1^2 + 2^2 + ... + 10^2 = 385 -The square of the sum of the first ten natural numbers is, - (1 + 2 + ... + 10)^2 = 552 = 3025 -Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. -Find the difference between the sum of the squares of the first N natural numbers and the square of the sum. -''' -from __future__ import print_function -import math -def problem6(number=100): - sum_of_squares = sum([i*i for i in range(1,number+1)]) - square_of_sum = int(math.pow(sum(range(1,number+1)),2)) - return square_of_sum - sum_of_squares -def main(): - print(problem6()) - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/project_euler/problem_062/__init__.py b/project_euler/problem_062/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_062/sol1.py b/project_euler/problem_062/sol1.py new file mode 100644 index 000000000000..83286c801301 --- /dev/null +++ b/project_euler/problem_062/sol1.py @@ -0,0 +1,62 @@ +""" +Project Euler 62 +https://projecteuler.net/problem=62 + +The cube, 41063625 (345^3), can be permuted to produce two other cubes: +56623104 (384^3) and 66430125 (405^3). In fact, 41063625 is the smallest cube +which has exactly three permutations of its digits which are also cube. + +Find the smallest cube for which exactly five permutations of its digits are +cube. +""" + +from collections import defaultdict + + +def solution(max_base: int = 5) -> int: + """ + Iterate through every possible cube and sort the cube's digits in + ascending order. Sorting maintains an ordering of the digits that allows + you to compare permutations. Store each sorted sequence of digits in a + dictionary, whose key is the sequence of digits and value is a list of + numbers that are the base of the cube. + + Once you find 5 numbers that produce the same sequence of digits, return + the smallest one, which is at index 0 since we insert each base number in + ascending order. + + >>> solution(2) + 125 + >>> solution(3) + 41063625 + """ + freqs = defaultdict(list) + num = 0 + + while True: + digits = get_digits(num) + freqs[digits].append(num) + + if len(freqs[digits]) == max_base: + base = freqs[digits][0] ** 3 + return base + + num += 1 + + +def get_digits(num: int) -> str: + """ + Computes the sorted sequence of digits of the cube of num. + + >>> get_digits(3) + '27' + >>> get_digits(99) + '027999' + >>> get_digits(123) + '0166788' + """ + return "".join(sorted(list(str(num ** 3)))) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_063/__init__.py b/project_euler/problem_063/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_063/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_063/sol1.py b/project_euler/problem_063/sol1.py new file mode 100644 index 000000000000..29efddba4216 --- /dev/null +++ b/project_euler/problem_063/sol1.py @@ -0,0 +1,34 @@ +""" +The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, +134217728=89, is a ninth power. +How many n-digit positive integers exist which are also an nth power? +""" + +""" +The maximum base can be 9 because all n-digit numbers < 10^n. +Now 9**23 has 22 digits so the maximum power can be 22. +Using these conclusions, we will calculate the result. +""" + + +def solution(max_base: int = 10, max_power: int = 22) -> int: + """ + Returns the count of all n-digit numbers which are nth power + >>> solution(10, 22) + 49 + >>> solution(0, 0) + 0 + >>> solution(1, 1) + 0 + >>> solution(-1, -1) + 0 + """ + bases = range(1, max_base) + powers = range(1, max_power) + return sum( + 1 for power in powers for base in bases if len(str(base ** power)) == power + ) + + +if __name__ == "__main__": + print(f"{solution(10, 22) = }") diff --git a/project_euler/problem_064/__init__.py b/project_euler/problem_064/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_064/sol1.py b/project_euler/problem_064/sol1.py new file mode 100644 index 000000000000..69e3f6d97580 --- /dev/null +++ b/project_euler/problem_064/sol1.py @@ -0,0 +1,77 @@ +""" +Project Euler Problem 64: https://projecteuler.net/problem=64 + +All square roots are periodic when written as continued fractions. +For example, let us consider sqrt(23). +It can be seen that the sequence is repeating. +For conciseness, we use the notation sqrt(23)=[4;(1,3,1,8)], +to indicate that the block (1,3,1,8) repeats indefinitely. +Exactly four continued fractions, for N<=13, have an odd period. +How many continued fractions for N<=10000 have an odd period? + +References: +- https://en.wikipedia.org/wiki/Continued_fraction +""" + +from math import floor, sqrt + + +def continuous_fraction_period(n: int) -> int: + """ + Returns the continued fraction period of a number n. + + >>> continuous_fraction_period(2) + 1 + >>> continuous_fraction_period(5) + 1 + >>> continuous_fraction_period(7) + 4 + >>> continuous_fraction_period(11) + 2 + >>> continuous_fraction_period(13) + 5 + """ + numerator = 0.0 + denominator = 1.0 + ROOT = int(sqrt(n)) + integer_part = ROOT + period = 0 + while integer_part != 2 * ROOT: + numerator = denominator * integer_part - numerator + denominator = (n - numerator ** 2) / denominator + integer_part = int((ROOT + numerator) / denominator) + period += 1 + return period + + +def solution(n: int = 10000) -> int: + """ + Returns the count of numbers <= 10000 with odd periods. + This function calls continuous_fraction_period for numbers which are + not perfect squares. + This is checked in if sr - floor(sr) != 0 statement. + If an odd period is returned by continuous_fraction_period, + count_odd_periods is increased by 1. + + >>> solution(2) + 1 + >>> solution(5) + 2 + >>> solution(7) + 2 + >>> solution(11) + 3 + >>> solution(13) + 4 + """ + count_odd_periods = 0 + for i in range(2, n + 1): + sr = sqrt(i) + if sr - floor(sr) != 0: + if continuous_fraction_period(i) % 2 == 1: + count_odd_periods += 1 + return count_odd_periods + + +if __name__ == "__main__": + print(f"{solution(int(input().strip()))}") diff --git a/project_euler/problem_065/__init__.py b/project_euler/problem_065/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_065/sol1.py b/project_euler/problem_065/sol1.py new file mode 100644 index 000000000000..229769a77d07 --- /dev/null +++ b/project_euler/problem_065/sol1.py @@ -0,0 +1,99 @@ +""" +Project Euler Problem 65: https://projecteuler.net/problem=65 + +The square root of 2 can be written as an infinite continued fraction. + +sqrt(2) = 1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / (2 + ...)))) + +The infinite continued fraction can be written, sqrt(2) = [1;(2)], (2) +indicates that 2 repeats ad infinitum. In a similar way, sqrt(23) = +[4;(1,3,1,8)]. + +It turns out that the sequence of partial values of continued +fractions for square roots provide the best rational approximations. +Let us consider the convergents for sqrt(2). + +1 + 1 / 2 = 3/2 +1 + 1 / (2 + 1 / 2) = 7/5 +1 + 1 / (2 + 1 / (2 + 1 / 2)) = 17/12 +1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / 2))) = 41/29 + +Hence the sequence of the first ten convergents for sqrt(2) are: +1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, ... + +What is most surprising is that the important mathematical constant, +e = [2;1,2,1,1,4,1,1,6,1,...,1,2k,1,...]. + +The first ten terms in the sequence of convergents for e are: +2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, ... + +The sum of digits in the numerator of the 10th convergent is +1 + 4 + 5 + 7 = 17. + +Find the sum of the digits in the numerator of the 100th convergent +of the continued fraction for e. + +----- + +The solution mostly comes down to finding an equation that will generate +the numerator of the continued fraction. For the i-th numerator, the +pattern is: + +n_i = m_i * n_(i-1) + n_(i-2) + +for m_i = the i-th index of the continued fraction representation of e, +n_0 = 1, and n_1 = 2 as the first 2 numbers of the representation. + +For example: +n_9 = 6 * 193 + 106 = 1264 +1 + 2 + 6 + 4 = 13 + +n_10 = 1 * 193 + 1264 = 1457 +1 + 4 + 5 + 7 = 17 +""" + + +def sum_digits(num: int) -> int: + """ + Returns the sum of every digit in num. + + >>> sum_digits(1) + 1 + >>> sum_digits(12345) + 15 + >>> sum_digits(999001) + 28 + """ + digit_sum = 0 + while num > 0: + digit_sum += num % 10 + num //= 10 + return digit_sum + + +def solution(max: int = 100) -> int: + """ + Returns the sum of the digits in the numerator of the max-th convergent of + the continued fraction for e. + + >>> solution(9) + 13 + >>> solution(10) + 17 + >>> solution(50) + 91 + """ + pre_numerator = 1 + cur_numerator = 2 + + for i in range(2, max + 1): + temp = pre_numerator + e_cont = 2 * i // 3 if i % 3 == 0 else 1 + pre_numerator = cur_numerator + cur_numerator = e_cont * pre_numerator + temp + + return sum_digits(cur_numerator) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_067/__init__.py b/project_euler/problem_067/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_067/sol1.py b/project_euler/problem_067/sol1.py new file mode 100644 index 000000000000..ebfa865a9479 --- /dev/null +++ b/project_euler/problem_067/sol1.py @@ -0,0 +1,49 @@ +""" +Problem Statement: +By starting at the top of the triangle below and moving to adjacent numbers on +the row below, the maximum total from top to bottom is 23. +3 +7 4 +2 4 6 +8 5 9 3 +That is, 3 + 7 + 4 + 9 = 23. +Find the maximum total from top to bottom in triangle.txt (right click and +'Save Link/Target As...'), a 15K text file containing a triangle with +one-hundred rows. +""" +import os + + +def solution(): + """ + Finds the maximum total in a triangle as described by the problem statement + above. + + >>> solution() + 7273 + """ + script_dir = os.path.dirname(os.path.realpath(__file__)) + triangle = os.path.join(script_dir, "triangle.txt") + + with open(triangle) as f: + triangle = f.readlines() + + a = map(lambda x: x.rstrip("\r\n").split(" "), triangle) + a = list(map(lambda x: list(map(lambda y: int(y), x)), a)) + + for i in range(1, len(a)): + for j in range(len(a[i])): + if j != len(a[i - 1]): + number1 = a[i - 1][j] + else: + number1 = 0 + if j > 0: + number2 = a[i - 1][j - 1] + else: + number2 = 0 + a[i][j] += max(number1, number2) + return max(a[-1]) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_067/triangle.txt b/project_euler/problem_067/triangle.txt new file mode 100644 index 000000000000..00aa2bc6382d --- /dev/null +++ b/project_euler/problem_067/triangle.txt @@ -0,0 +1,100 @@ +59 +73 41 +52 40 09 +26 53 06 34 +10 51 87 86 81 +61 95 66 57 25 68 +90 81 80 38 92 67 73 +30 28 51 76 81 18 75 44 +84 14 95 87 62 81 17 78 58 +21 46 71 58 02 79 62 39 31 09 +56 34 35 53 78 31 81 18 90 93 15 +78 53 04 21 84 93 32 13 97 11 37 51 +45 03 81 79 05 18 78 86 13 30 63 99 95 +39 87 96 28 03 38 42 17 82 87 58 07 22 57 +06 17 51 17 07 93 09 07 75 97 95 78 87 08 53 +67 66 59 60 88 99 94 65 55 77 55 34 27 53 78 28 +76 40 41 04 87 16 09 42 75 69 23 97 30 60 10 79 87 +12 10 44 26 21 36 32 84 98 60 13 12 36 16 63 31 91 35 +70 39 06 05 55 27 38 48 28 22 34 35 62 62 15 14 94 89 86 +66 56 68 84 96 21 34 34 34 81 62 40 65 54 62 05 98 03 02 60 +38 89 46 37 99 54 34 53 36 14 70 26 02 90 45 13 31 61 83 73 47 +36 10 63 96 60 49 41 05 37 42 14 58 84 93 96 17 09 43 05 43 06 59 +66 57 87 57 61 28 37 51 84 73 79 15 39 95 88 87 43 39 11 86 77 74 18 +54 42 05 79 30 49 99 73 46 37 50 02 45 09 54 52 27 95 27 65 19 45 26 45 +71 39 17 78 76 29 52 90 18 99 78 19 35 62 71 19 23 65 93 85 49 33 75 09 02 +33 24 47 61 60 55 32 88 57 55 91 54 46 57 07 77 98 52 80 99 24 25 46 78 79 05 +92 09 13 55 10 67 26 78 76 82 63 49 51 31 24 68 05 57 07 54 69 21 67 43 17 63 12 +24 59 06 08 98 74 66 26 61 60 13 03 09 09 24 30 71 08 88 70 72 70 29 90 11 82 41 34 +66 82 67 04 36 60 92 77 91 85 62 49 59 61 30 90 29 94 26 41 89 04 53 22 83 41 09 74 90 +48 28 26 37 28 52 77 26 51 32 18 98 79 36 62 13 17 08 19 54 89 29 73 68 42 14 08 16 70 37 +37 60 69 70 72 71 09 59 13 60 38 13 57 36 09 30 43 89 30 39 15 02 44 73 05 73 26 63 56 86 12 +55 55 85 50 62 99 84 77 28 85 03 21 27 22 19 26 82 69 54 04 13 07 85 14 01 15 70 59 89 95 10 19 +04 09 31 92 91 38 92 86 98 75 21 05 64 42 62 84 36 20 73 42 21 23 22 51 51 79 25 45 85 53 03 43 22 +75 63 02 49 14 12 89 14 60 78 92 16 44 82 38 30 72 11 46 52 90 27 08 65 78 03 85 41 57 79 39 52 33 48 +78 27 56 56 39 13 19 43 86 72 58 95 39 07 04 34 21 98 39 15 39 84 89 69 84 46 37 57 59 35 59 50 26 15 93 +42 89 36 27 78 91 24 11 17 41 05 94 07 69 51 96 03 96 47 90 90 45 91 20 50 56 10 32 36 49 04 53 85 92 25 65 +52 09 61 30 61 97 66 21 96 92 98 90 06 34 96 60 32 69 68 33 75 84 18 31 71 50 84 63 03 03 19 11 28 42 75 45 45 +61 31 61 68 96 34 49 39 05 71 76 59 62 67 06 47 96 99 34 21 32 47 52 07 71 60 42 72 94 56 82 83 84 40 94 87 82 46 +01 20 60 14 17 38 26 78 66 81 45 95 18 51 98 81 48 16 53 88 37 52 69 95 72 93 22 34 98 20 54 27 73 61 56 63 60 34 63 +93 42 94 83 47 61 27 51 79 79 45 01 44 73 31 70 83 42 88 25 53 51 30 15 65 94 80 44 61 84 12 77 02 62 02 65 94 42 14 94 +32 73 09 67 68 29 74 98 10 19 85 48 38 31 85 67 53 93 93 77 47 67 39 72 94 53 18 43 77 40 78 32 29 59 24 06 02 83 50 60 66 +32 01 44 30 16 51 15 81 98 15 10 62 86 79 50 62 45 60 70 38 31 85 65 61 64 06 69 84 14 22 56 43 09 48 66 69 83 91 60 40 36 61 +92 48 22 99 15 95 64 43 01 16 94 02 99 19 17 69 11 58 97 56 89 31 77 45 67 96 12 73 08 20 36 47 81 44 50 64 68 85 40 81 85 52 09 +91 35 92 45 32 84 62 15 19 64 21 66 06 01 52 80 62 59 12 25 88 28 91 50 40 16 22 99 92 79 87 51 21 77 74 77 07 42 38 42 74 83 02 05 +46 19 77 66 24 18 05 32 02 84 31 99 92 58 96 72 91 36 62 99 55 29 53 42 12 37 26 58 89 50 66 19 82 75 12 48 24 87 91 85 02 07 03 76 86 +99 98 84 93 07 17 33 61 92 20 66 60 24 66 40 30 67 05 37 29 24 96 03 27 70 62 13 04 45 47 59 88 43 20 66 15 46 92 30 04 71 66 78 70 53 99 +67 60 38 06 88 04 17 72 10 99 71 07 42 25 54 05 26 64 91 50 45 71 06 30 67 48 69 82 08 56 80 67 18 46 66 63 01 20 08 80 47 07 91 16 03 79 87 +18 54 78 49 80 48 77 40 68 23 60 88 58 80 33 57 11 69 55 53 64 02 94 49 60 92 16 35 81 21 82 96 25 24 96 18 02 05 49 03 50 77 06 32 84 27 18 38 +68 01 50 04 03 21 42 94 53 24 89 05 92 26 52 36 68 11 85 01 04 42 02 45 15 06 50 04 53 73 25 74 81 88 98 21 67 84 79 97 99 20 95 04 40 46 02 58 87 +94 10 02 78 88 52 21 03 88 60 06 53 49 71 20 91 12 65 07 49 21 22 11 41 58 99 36 16 09 48 17 24 52 36 23 15 72 16 84 56 02 99 43 76 81 71 29 39 49 17 +64 39 59 84 86 16 17 66 03 09 43 06 64 18 63 29 68 06 23 07 87 14 26 35 17 12 98 41 53 64 78 18 98 27 28 84 80 67 75 62 10 11 76 90 54 10 05 54 41 39 66 +43 83 18 37 32 31 52 29 95 47 08 76 35 11 04 53 35 43 34 10 52 57 12 36 20 39 40 55 78 44 07 31 38 26 08 15 56 88 86 01 52 62 10 24 32 05 60 65 53 28 57 99 +03 50 03 52 07 73 49 92 66 80 01 46 08 67 25 36 73 93 07 42 25 53 13 96 76 83 87 90 54 89 78 22 78 91 73 51 69 09 79 94 83 53 09 40 69 62 10 79 49 47 03 81 30 +71 54 73 33 51 76 59 54 79 37 56 45 84 17 62 21 98 69 41 95 65 24 39 37 62 03 24 48 54 64 46 82 71 78 33 67 09 16 96 68 52 74 79 68 32 21 13 78 96 60 09 69 20 36 +73 26 21 44 46 38 17 83 65 98 07 23 52 46 61 97 33 13 60 31 70 15 36 77 31 58 56 93 75 68 21 36 69 53 90 75 25 82 39 50 65 94 29 30 11 33 11 13 96 02 56 47 07 49 02 +76 46 73 30 10 20 60 70 14 56 34 26 37 39 48 24 55 76 84 91 39 86 95 61 50 14 53 93 64 67 37 31 10 84 42 70 48 20 10 72 60 61 84 79 69 65 99 73 89 25 85 48 92 56 97 16 +03 14 80 27 22 30 44 27 67 75 79 32 51 54 81 29 65 14 19 04 13 82 04 91 43 40 12 52 29 99 07 76 60 25 01 07 61 71 37 92 40 47 99 66 57 01 43 44 22 40 53 53 09 69 26 81 07 +49 80 56 90 93 87 47 13 75 28 87 23 72 79 32 18 27 20 28 10 37 59 21 18 70 04 79 96 03 31 45 71 81 06 14 18 17 05 31 50 92 79 23 47 09 39 47 91 43 54 69 47 42 95 62 46 32 85 +37 18 62 85 87 28 64 05 77 51 47 26 30 65 05 70 65 75 59 80 42 52 25 20 44 10 92 17 71 95 52 14 77 13 24 55 11 65 26 91 01 30 63 15 49 48 41 17 67 47 03 68 20 90 98 32 04 40 68 +90 51 58 60 06 55 23 68 05 19 76 94 82 36 96 43 38 90 87 28 33 83 05 17 70 83 96 93 06 04 78 47 80 06 23 84 75 23 87 72 99 14 50 98 92 38 90 64 61 58 76 94 36 66 87 80 51 35 61 38 +57 95 64 06 53 36 82 51 40 33 47 14 07 98 78 65 39 58 53 06 50 53 04 69 40 68 36 69 75 78 75 60 03 32 39 24 74 47 26 90 13 40 44 71 90 76 51 24 36 50 25 45 70 80 61 80 61 43 90 64 11 +18 29 86 56 68 42 79 10 42 44 30 12 96 18 23 18 52 59 02 99 67 46 60 86 43 38 55 17 44 93 42 21 55 14 47 34 55 16 49 24 23 29 96 51 55 10 46 53 27 92 27 46 63 57 30 65 43 27 21 20 24 83 +81 72 93 19 69 52 48 01 13 83 92 69 20 48 69 59 20 62 05 42 28 89 90 99 32 72 84 17 08 87 36 03 60 31 36 36 81 26 97 36 48 54 56 56 27 16 91 08 23 11 87 99 33 47 02 14 44 73 70 99 43 35 33 +90 56 61 86 56 12 70 59 63 32 01 15 81 47 71 76 95 32 65 80 54 70 34 51 40 45 33 04 64 55 78 68 88 47 31 47 68 87 03 84 23 44 89 72 35 08 31 76 63 26 90 85 96 67 65 91 19 14 17 86 04 71 32 95 +37 13 04 22 64 37 37 28 56 62 86 33 07 37 10 44 52 82 52 06 19 52 57 75 90 26 91 24 06 21 14 67 76 30 46 14 35 89 89 41 03 64 56 97 87 63 22 34 03 79 17 45 11 53 25 56 96 61 23 18 63 31 37 37 47 +77 23 26 70 72 76 77 04 28 64 71 69 14 85 96 54 95 48 06 62 99 83 86 77 97 75 71 66 30 19 57 90 33 01 60 61 14 12 90 99 32 77 56 41 18 14 87 49 10 14 90 64 18 50 21 74 14 16 88 05 45 73 82 47 74 44 +22 97 41 13 34 31 54 61 56 94 03 24 59 27 98 77 04 09 37 40 12 26 87 09 71 70 07 18 64 57 80 21 12 71 83 94 60 39 73 79 73 19 97 32 64 29 41 07 48 84 85 67 12 74 95 20 24 52 41 67 56 61 29 93 35 72 69 +72 23 63 66 01 11 07 30 52 56 95 16 65 26 83 90 50 74 60 18 16 48 43 77 37 11 99 98 30 94 91 26 62 73 45 12 87 73 47 27 01 88 66 99 21 41 95 80 02 53 23 32 61 48 32 43 43 83 14 66 95 91 19 81 80 67 25 88 +08 62 32 18 92 14 83 71 37 96 11 83 39 99 05 16 23 27 10 67 02 25 44 11 55 31 46 64 41 56 44 74 26 81 51 31 45 85 87 09 81 95 22 28 76 69 46 48 64 87 67 76 27 89 31 11 74 16 62 03 60 94 42 47 09 34 94 93 72 +56 18 90 18 42 17 42 32 14 86 06 53 33 95 99 35 29 15 44 20 49 59 25 54 34 59 84 21 23 54 35 90 78 16 93 13 37 88 54 19 86 67 68 55 66 84 65 42 98 37 87 56 33 28 58 38 28 38 66 27 52 21 81 15 08 22 97 32 85 27 +91 53 40 28 13 34 91 25 01 63 50 37 22 49 71 58 32 28 30 18 68 94 23 83 63 62 94 76 80 41 90 22 82 52 29 12 18 56 10 08 35 14 37 57 23 65 67 40 72 39 93 39 70 89 40 34 07 46 94 22 20 05 53 64 56 30 05 56 61 88 27 +23 95 11 12 37 69 68 24 66 10 87 70 43 50 75 07 62 41 83 58 95 93 89 79 45 39 02 22 05 22 95 43 62 11 68 29 17 40 26 44 25 71 87 16 70 85 19 25 59 94 90 41 41 80 61 70 55 60 84 33 95 76 42 63 15 09 03 40 38 12 03 32 +09 84 56 80 61 55 85 97 16 94 82 94 98 57 84 30 84 48 93 90 71 05 95 90 73 17 30 98 40 64 65 89 07 79 09 19 56 36 42 30 23 69 73 72 07 05 27 61 24 31 43 48 71 84 21 28 26 65 65 59 65 74 77 20 10 81 61 84 95 08 52 23 70 +47 81 28 09 98 51 67 64 35 51 59 36 92 82 77 65 80 24 72 53 22 07 27 10 21 28 30 22 48 82 80 48 56 20 14 43 18 25 50 95 90 31 77 08 09 48 44 80 90 22 93 45 82 17 13 96 25 26 08 73 34 99 06 49 24 06 83 51 40 14 15 10 25 01 +54 25 10 81 30 64 24 74 75 80 36 75 82 60 22 69 72 91 45 67 03 62 79 54 89 74 44 83 64 96 66 73 44 30 74 50 37 05 09 97 70 01 60 46 37 91 39 75 75 18 58 52 72 78 51 81 86 52 08 97 01 46 43 66 98 62 81 18 70 93 73 08 32 46 34 +96 80 82 07 59 71 92 53 19 20 88 66 03 26 26 10 24 27 50 82 94 73 63 08 51 33 22 45 19 13 58 33 90 15 22 50 36 13 55 06 35 47 82 52 33 61 36 27 28 46 98 14 73 20 73 32 16 26 80 53 47 66 76 38 94 45 02 01 22 52 47 96 64 58 52 39 +88 46 23 39 74 63 81 64 20 90 33 33 76 55 58 26 10 46 42 26 74 74 12 83 32 43 09 02 73 55 86 54 85 34 28 23 29 79 91 62 47 41 82 87 99 22 48 90 20 05 96 75 95 04 43 28 81 39 81 01 28 42 78 25 39 77 90 57 58 98 17 36 73 22 63 74 51 +29 39 74 94 95 78 64 24 38 86 63 87 93 06 70 92 22 16 80 64 29 52 20 27 23 50 14 13 87 15 72 96 81 22 08 49 72 30 70 24 79 31 16 64 59 21 89 34 96 91 48 76 43 53 88 01 57 80 23 81 90 79 58 01 80 87 17 99 86 90 72 63 32 69 14 28 88 69 +37 17 71 95 56 93 71 35 43 45 04 98 92 94 84 96 11 30 31 27 31 60 92 03 48 05 98 91 86 94 35 90 90 08 48 19 33 28 68 37 59 26 65 96 50 68 22 07 09 49 34 31 77 49 43 06 75 17 81 87 61 79 52 26 27 72 29 50 07 98 86 01 17 10 46 64 24 18 56 +51 30 25 94 88 85 79 91 40 33 63 84 49 67 98 92 15 26 75 19 82 05 18 78 65 93 61 48 91 43 59 41 70 51 22 15 92 81 67 91 46 98 11 11 65 31 66 10 98 65 83 21 05 56 05 98 73 67 46 74 69 34 08 30 05 52 07 98 32 95 30 94 65 50 24 63 28 81 99 57 +19 23 61 36 09 89 71 98 65 17 30 29 89 26 79 74 94 11 44 48 97 54 81 55 39 66 69 45 28 47 13 86 15 76 74 70 84 32 36 33 79 20 78 14 41 47 89 28 81 05 99 66 81 86 38 26 06 25 13 60 54 55 23 53 27 05 89 25 23 11 13 54 59 54 56 34 16 24 53 44 06 +13 40 57 72 21 15 60 08 04 19 11 98 34 45 09 97 86 71 03 15 56 19 15 44 97 31 90 04 87 87 76 08 12 30 24 62 84 28 12 85 82 53 99 52 13 94 06 65 97 86 09 50 94 68 69 74 30 67 87 94 63 07 78 27 80 36 69 41 06 92 32 78 37 82 30 05 18 87 99 72 19 99 +44 20 55 77 69 91 27 31 28 81 80 27 02 07 97 23 95 98 12 25 75 29 47 71 07 47 78 39 41 59 27 76 13 15 66 61 68 35 69 86 16 53 67 63 99 85 41 56 08 28 33 40 94 76 90 85 31 70 24 65 84 65 99 82 19 25 54 37 21 46 33 02 52 99 51 33 26 04 87 02 08 18 96 +54 42 61 45 91 06 64 79 80 82 32 16 83 63 42 49 19 78 65 97 40 42 14 61 49 34 04 18 25 98 59 30 82 72 26 88 54 36 21 75 03 88 99 53 46 51 55 78 22 94 34 40 68 87 84 25 30 76 25 08 92 84 42 61 40 38 09 99 40 23 29 39 46 55 10 90 35 84 56 70 63 23 91 39 +52 92 03 71 89 07 09 37 68 66 58 20 44 92 51 56 13 71 79 99 26 37 02 06 16 67 36 52 58 16 79 73 56 60 59 27 44 77 94 82 20 50 98 33 09 87 94 37 40 83 64 83 58 85 17 76 53 02 83 52 22 27 39 20 48 92 45 21 09 42 24 23 12 37 52 28 50 78 79 20 86 62 73 20 59 +54 96 80 15 91 90 99 70 10 09 58 90 93 50 81 99 54 38 36 10 30 11 35 84 16 45 82 18 11 97 36 43 96 79 97 65 40 48 23 19 17 31 64 52 65 65 37 32 65 76 99 79 34 65 79 27 55 33 03 01 33 27 61 28 66 08 04 70 49 46 48 83 01 45 19 96 13 81 14 21 31 79 93 85 50 05 +92 92 48 84 59 98 31 53 23 27 15 22 79 95 24 76 05 79 16 93 97 89 38 89 42 83 02 88 94 95 82 21 01 97 48 39 31 78 09 65 50 56 97 61 01 07 65 27 21 23 14 15 80 97 44 78 49 35 33 45 81 74 34 05 31 57 09 38 94 07 69 54 69 32 65 68 46 68 78 90 24 28 49 51 45 86 35 +41 63 89 76 87 31 86 09 46 14 87 82 22 29 47 16 13 10 70 72 82 95 48 64 58 43 13 75 42 69 21 12 67 13 64 85 58 23 98 09 37 76 05 22 31 12 66 50 29 99 86 72 45 25 10 28 19 06 90 43 29 31 67 79 46 25 74 14 97 35 76 37 65 46 23 82 06 22 30 76 93 66 94 17 96 13 20 72 +63 40 78 08 52 09 90 41 70 28 36 14 46 44 85 96 24 52 58 15 87 37 05 98 99 39 13 61 76 38 44 99 83 74 90 22 53 80 56 98 30 51 63 39 44 30 91 91 04 22 27 73 17 35 53 18 35 45 54 56 27 78 48 13 69 36 44 38 71 25 30 56 15 22 73 43 32 69 59 25 93 83 45 11 34 94 44 39 92 +12 36 56 88 13 96 16 12 55 54 11 47 19 78 17 17 68 81 77 51 42 55 99 85 66 27 81 79 93 42 65 61 69 74 14 01 18 56 12 01 58 37 91 22 42 66 83 25 19 04 96 41 25 45 18 69 96 88 36 93 10 12 98 32 44 83 83 04 72 91 04 27 73 07 34 37 71 60 59 31 01 54 54 44 96 93 83 36 04 45 +30 18 22 20 42 96 65 79 17 41 55 69 94 81 29 80 91 31 85 25 47 26 43 49 02 99 34 67 99 76 16 14 15 93 08 32 99 44 61 77 67 50 43 55 87 55 53 72 17 46 62 25 50 99 73 05 93 48 17 31 70 80 59 09 44 59 45 13 74 66 58 94 87 73 16 14 85 38 74 99 64 23 79 28 71 42 20 37 82 31 23 +51 96 39 65 46 71 56 13 29 68 53 86 45 33 51 49 12 91 21 21 76 85 02 17 98 15 46 12 60 21 88 30 92 83 44 59 42 50 27 88 46 86 94 73 45 54 23 24 14 10 94 21 20 34 23 51 04 83 99 75 90 63 60 16 22 33 83 70 11 32 10 50 29 30 83 46 11 05 31 17 86 42 49 01 44 63 28 60 07 78 95 40 +44 61 89 59 04 49 51 27 69 71 46 76 44 04 09 34 56 39 15 06 94 91 75 90 65 27 56 23 74 06 23 33 36 69 14 39 05 34 35 57 33 22 76 46 56 10 61 65 98 09 16 69 04 62 65 18 99 76 49 18 72 66 73 83 82 40 76 31 89 91 27 88 17 35 41 35 32 51 32 67 52 68 74 85 80 57 07 11 62 66 47 22 67 +65 37 19 97 26 17 16 24 24 17 50 37 64 82 24 36 32 11 68 34 69 31 32 89 79 93 96 68 49 90 14 23 04 04 67 99 81 74 70 74 36 96 68 09 64 39 88 35 54 89 96 58 66 27 88 97 32 14 06 35 78 20 71 06 85 66 57 02 58 91 72 05 29 56 73 48 86 52 09 93 22 57 79 42 12 01 31 68 17 59 63 76 07 77 +73 81 14 13 17 20 11 09 01 83 08 85 91 70 84 63 62 77 37 07 47 01 59 95 39 69 39 21 99 09 87 02 97 16 92 36 74 71 90 66 33 73 73 75 52 91 11 12 26 53 05 26 26 48 61 50 90 65 01 87 42 47 74 35 22 73 24 26 56 70 52 05 48 41 31 18 83 27 21 39 80 85 26 08 44 02 71 07 63 22 05 52 19 08 20 +17 25 21 11 72 93 33 49 64 23 53 82 03 13 91 65 85 02 40 05 42 31 77 42 05 36 06 54 04 58 07 76 87 83 25 57 66 12 74 33 85 37 74 32 20 69 03 97 91 68 82 44 19 14 89 28 85 85 80 53 34 87 58 98 88 78 48 65 98 40 11 57 10 67 70 81 60 79 74 72 97 59 79 47 30 20 54 80 89 91 14 05 33 36 79 39 +60 85 59 39 60 07 57 76 77 92 06 35 15 72 23 41 45 52 95 18 64 79 86 53 56 31 69 11 91 31 84 50 44 82 22 81 41 40 30 42 30 91 48 94 74 76 64 58 74 25 96 57 14 19 03 99 28 83 15 75 99 01 89 85 79 50 03 95 32 67 44 08 07 41 62 64 29 20 14 76 26 55 48 71 69 66 19 72 44 25 14 01 48 74 12 98 07 +64 66 84 24 18 16 27 48 20 14 47 69 30 86 48 40 23 16 61 21 51 50 26 47 35 33 91 28 78 64 43 68 04 79 51 08 19 60 52 95 06 68 46 86 35 97 27 58 04 65 30 58 99 12 12 75 91 39 50 31 42 64 70 04 46 07 98 73 98 93 37 89 77 91 64 71 64 65 66 21 78 62 81 74 42 20 83 70 73 95 78 45 92 27 34 53 71 15 +30 11 85 31 34 71 13 48 05 14 44 03 19 67 23 73 19 57 06 90 94 72 57 69 81 62 59 68 88 57 55 69 49 13 07 87 97 80 89 05 71 05 05 26 38 40 16 62 45 99 18 38 98 24 21 26 62 74 69 04 85 57 77 35 58 67 91 79 79 57 86 28 66 34 72 51 76 78 36 95 63 90 08 78 47 63 45 31 22 70 52 48 79 94 15 77 61 67 68 +23 33 44 81 80 92 93 75 94 88 23 61 39 76 22 03 28 94 32 06 49 65 41 34 18 23 08 47 62 60 03 63 33 13 80 52 31 54 73 43 70 26 16 69 57 87 83 31 03 93 70 81 47 95 77 44 29 68 39 51 56 59 63 07 25 70 07 77 43 53 64 03 94 42 95 39 18 01 66 21 16 97 20 50 90 16 70 10 95 69 29 06 25 61 41 26 15 59 63 35 diff --git a/project_euler/problem_069/__init__.py b/project_euler/problem_069/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_069/sol1.py b/project_euler/problem_069/sol1.py new file mode 100644 index 000000000000..d148dd79a777 --- /dev/null +++ b/project_euler/problem_069/sol1.py @@ -0,0 +1,66 @@ +""" +Totient maximum +Problem 69: https://projecteuler.net/problem=69 + +Euler's Totient function, φ(n) [sometimes called the phi function], +is used to determine the number of numbers less than n which are relatively prime to n. +For example, as 1, 2, 4, 5, 7, and 8, +are all less than nine and relatively prime to nine, φ(9)=6. + +n Relatively Prime φ(n) n/φ(n) +2 1 1 2 +3 1,2 2 1.5 +4 1,3 2 2 +5 1,2,3,4 4 1.25 +6 1,5 2 3 +7 1,2,3,4,5,6 6 1.1666... +8 1,3,5,7 4 2 +9 1,2,4,5,7,8 6 1.5 +10 1,3,7,9 4 2.5 + +It can be seen that n=6 produces a maximum n/φ(n) for n ≤ 10. + +Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum. +""" + + +def solution(n: int = 10 ** 6) -> int: + """ + Returns solution to problem. + Algorithm: + 1. Precompute φ(k) for all natural k, k <= n using product formula (wikilink below) + https://en.wikipedia.org/wiki/Euler%27s_totient_function#Euler's_product_formula + + 2. Find k/φ(k) for all k ≤ n and return the k that attains maximum + + >>> solution(10) + 6 + + >>> solution(100) + 30 + + >>> solution(9973) + 2310 + + """ + + if n <= 0: + raise ValueError("Please enter an integer greater than 0") + + phi = list(range(n + 1)) + for number in range(2, n + 1): + if phi[number] == number: + phi[number] -= 1 + for multiple in range(number * 2, n + 1, number): + phi[multiple] = (phi[multiple] // number) * (number - 1) + + answer = 1 + for number in range(1, n + 1): + if (answer / phi[answer]) < (number / phi[number]): + answer = number + + return answer + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_07/sol1.py b/project_euler/problem_07/sol1.py deleted file mode 100644 index ea31d0b2bb2c..000000000000 --- a/project_euler/problem_07/sol1.py +++ /dev/null @@ -1,30 +0,0 @@ -''' -By listing the first six prime numbers: -2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. -What is the Nth prime number? -''' -from __future__ import print_function -from math import sqrt -def isprime(n): - if (n==2): - return True - elif (n%2==0): - return False - else: - sq = int(sqrt(n))+1 - for i in range(3,sq,2): - if(n%i==0): - return False - return True -n = int(input()) -i=0 -j=1 -while(i!=n and j<3): - j+=1 - if (isprime(j)): - i+=1 -while(i!=n): - j+=2 - if(isprime(j)): - i+=1 -print(j) diff --git a/project_euler/problem_07/sol2.py b/project_euler/problem_07/sol2.py deleted file mode 100644 index fdf39cbc4d26..000000000000 --- a/project_euler/problem_07/sol2.py +++ /dev/null @@ -1,16 +0,0 @@ -# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the Nth prime number? -def isprime(number): - for i in range(2,int(number**0.5)+1): - if number%i==0: - return False - return True -n = int(input('Enter The N\'th Prime Number You Want To Get: ')) # Ask For The N'th Prime Number Wanted -primes = [] -num = 2 -while len(primes) < n: - if isprime(num): - primes.append(num) - num += 1 - else: - num += 1 -print(primes[len(primes) - 1]) diff --git a/project_euler/problem_07/sol3.py b/project_euler/problem_07/sol3.py deleted file mode 100644 index 0001e4318cc9..000000000000 --- a/project_euler/problem_07/sol3.py +++ /dev/null @@ -1,28 +0,0 @@ -''' -By listing the first six prime numbers: -2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. -What is the Nth prime number? -''' -from __future__ import print_function -# from Python.Math import PrimeCheck -import math -import itertools -def primeCheck(number): - if number % 2 == 0 and number > 2: - return False - return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) - -def prime_generator(): - num = 2 - while True: - if primeCheck(num): - yield num - num+=1 - -def main(): - n = int(input('Enter The N\'th Prime Number You Want To Get: ')) # Ask For The N'th Prime Number Wanted - print(next(itertools.islice(prime_generator(),n-1,n))) - - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/project_euler/problem_070/__init__.py b/project_euler/problem_070/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_070/sol1.py b/project_euler/problem_070/sol1.py new file mode 100644 index 000000000000..9d27119ba95c --- /dev/null +++ b/project_euler/problem_070/sol1.py @@ -0,0 +1,119 @@ +""" +Project Euler Problem 70: https://projecteuler.net/problem=70 + +Euler's Totient function, φ(n) [sometimes called the phi function], is used to +determine the number of positive numbers less than or equal to n which are +relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than +nine and relatively prime to nine, φ(9)=6. + +The number 1 is considered to be relatively prime to every positive number, so +φ(1)=1. + +Interestingly, φ(87109)=79180, and it can be seen that 87109 is a permutation +of 79180. + +Find the value of n, 1 < n < 10^7, for which φ(n) is a permutation of n and +the ratio n/φ(n) produces a minimum. + +----- + +This is essentially brute force. Calculate all totients up to 10^7 and +find the minimum ratio of n/φ(n) that way. To minimize the ratio, we want +to minimize n and maximize φ(n) as much as possible, so we can store the +minimum fraction's numerator and denominator and calculate new fractions +with each totient to compare against. To avoid dividing by zero, I opt to +use cross multiplication. + +References: +Finding totients +https://en.wikipedia.org/wiki/Euler's_totient_function#Euler's_product_formula +""" +from typing import List + + +def get_totients(max_one: int) -> List[int]: + """ + Calculates a list of totients from 0 to max_one exclusive, using the + definition of Euler's product formula. + + >>> get_totients(5) + [0, 1, 1, 2, 2] + + >>> get_totients(10) + [0, 1, 1, 2, 2, 4, 2, 6, 4, 6] + """ + totients = [0] * max_one + + for i in range(0, max_one): + totients[i] = i + + for i in range(2, max_one): + if totients[i] == i: + for j in range(i, max_one, i): + totients[j] -= totients[j] // i + + return totients + + +def has_same_digits(num1: int, num2: int) -> bool: + """ + Return True if num1 and num2 have the same frequency of every digit, False + otherwise. + + digits[] is a frequency table where the index represents the digit from + 0-9, and the element stores the number of appearances. Increment the + respective index every time you see the digit in num1, and decrement if in + num2. At the end, if the numbers have the same digits, every index must + contain 0. + + >>> has_same_digits(123456789, 987654321) + True + + >>> has_same_digits(123, 12) + False + + >>> has_same_digits(1234566, 123456) + False + """ + digits = [0] * 10 + + while num1 > 0 and num2 > 0: + digits[num1 % 10] += 1 + digits[num2 % 10] -= 1 + num1 //= 10 + num2 //= 10 + + for digit in digits: + if digit != 0: + return False + + return True + + +def solution(max: int = 10000000) -> int: + """ + Finds the value of n from 1 to max such that n/φ(n) produces a minimum. + + >>> solution(100) + 21 + + >>> solution(10000) + 4435 + """ + + min_numerator = 1 # i + min_denominator = 0 # φ(i) + totients = get_totients(max + 1) + + for i in range(2, max + 1): + t = totients[i] + + if i * min_denominator < min_numerator * t and has_same_digits(i, t): + min_numerator = i + min_denominator = t + + return min_numerator + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_071/__init__.py b/project_euler/problem_071/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_071/sol1.py b/project_euler/problem_071/sol1.py new file mode 100644 index 000000000000..415b127e5374 --- /dev/null +++ b/project_euler/problem_071/sol1.py @@ -0,0 +1,48 @@ +""" +Ordered fractions +Problem 71 +https://projecteuler.net/problem=71 + +Consider the fraction n/d, where n and d are positive +integers. If n int: + """ + Returns the closest numerator of the fraction immediately to the + left of given fraction (numerator/denominator) from a list of reduced + proper fractions. + >>> solution() + 428570 + >>> solution(3, 7, 8) + 2 + >>> solution(6, 7, 60) + 47 + """ + max_numerator = 0 + max_denominator = 1 + + for current_denominator in range(1, limit + 1): + current_numerator = current_denominator * numerator // denominator + if current_denominator % denominator == 0: + current_numerator -= 1 + if current_numerator * max_denominator > current_denominator * max_numerator: + max_numerator = current_numerator + max_denominator = current_denominator + return max_numerator + + +if __name__ == "__main__": + print(solution(numerator=3, denominator=7, limit=1000000)) diff --git a/project_euler/problem_072/__init__.py b/project_euler/problem_072/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_072/sol1.py b/project_euler/problem_072/sol1.py new file mode 100644 index 000000000000..846396ab0f9c --- /dev/null +++ b/project_euler/problem_072/sol1.py @@ -0,0 +1,46 @@ +""" +Problem 72 Counting fractions: https://projecteuler.net/problem=72 + +Description: + +Consider the fraction, n/d, where n and d are positive integers. If n int: + """ + Returns an integer, the solution to the problem + >>> solution(10) + 31 + >>> solution(100) + 3043 + >>> solution(1_000) + 304191 + """ + + phi = [i - 1 for i in range(limit + 1)] + + for i in range(2, limit + 1): + for j in range(2 * i, limit + 1, i): + phi[j] -= phi[i] + + return sum(phi[2 : limit + 1]) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_072/sol2.py b/project_euler/problem_072/sol2.py new file mode 100644 index 000000000000..08e92c18bb3a --- /dev/null +++ b/project_euler/problem_072/sol2.py @@ -0,0 +1,45 @@ +""" +Project Euler Problem 72: https://projecteuler.net/problem=72 + +Consider the fraction, n/d, where n and d are positive integers. If n int: + """ + Return the number of reduced proper fractions with denominator less than limit. + >>> solution(8) + 21 + >>> solution(1000) + 304191 + """ + primes = set(range(3, limit, 2)) + primes.add(2) + for p in range(3, limit, 2): + if p not in primes: + continue + primes.difference_update(set(range(p * p, limit, p))) + + phi = [float(n) for n in range(limit + 1)] + + for p in primes: + for n in range(p, limit + 1, p): + phi[n] *= 1 - 1 / p + + return int(sum(phi[2:])) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_074/__init__.py b/project_euler/problem_074/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_074/sol1.py b/project_euler/problem_074/sol1.py new file mode 100644 index 000000000000..5e6aff6f52f2 --- /dev/null +++ b/project_euler/problem_074/sol1.py @@ -0,0 +1,111 @@ +""" +Project Euler Problem 74: https://projecteuler.net/problem=74 + +The number 145 is well known for the property that the sum of the factorial of its +digits is equal to 145: + +1! + 4! + 5! = 1 + 24 + 120 = 145 + +Perhaps less well known is 169, in that it produces the longest chain of numbers that +link back to 169; it turns out that there are only three such loops that exist: + +169 → 363601 → 1454 → 169 +871 → 45361 → 871 +872 → 45362 → 872 + +It is not difficult to prove that EVERY starting number will eventually get stuck in +a loop. For example, + +69 → 363600 → 1454 → 169 → 363601 (→ 1454) +78 → 45360 → 871 → 45361 (→ 871) +540 → 145 (→ 145) + +Starting with 69 produces a chain of five non-repeating terms, but the longest +non-repeating chain with a starting number below one million is sixty terms. + +How many chains, with a starting number below one million, contain exactly sixty +non-repeating terms? +""" + + +DIGIT_FACTORIALS = { + "0": 1, + "1": 1, + "2": 2, + "3": 6, + "4": 24, + "5": 120, + "6": 720, + "7": 5040, + "8": 40320, + "9": 362880, +} + +CACHE_SUM_DIGIT_FACTORIALS = {145: 145} + +CHAIN_LENGTH_CACHE = { + 145: 0, + 169: 3, + 36301: 3, + 1454: 3, + 871: 2, + 45361: 2, + 872: 2, + 45361: 2, +} + + +def sum_digit_factorials(n: int) -> int: + """ + Return the sum of the factorial of the digits of n. + >>> sum_digit_factorials(145) + 145 + >>> sum_digit_factorials(45361) + 871 + >>> sum_digit_factorials(540) + 145 + """ + if n in CACHE_SUM_DIGIT_FACTORIALS: + return CACHE_SUM_DIGIT_FACTORIALS[n] + ret = sum([DIGIT_FACTORIALS[let] for let in str(n)]) + CACHE_SUM_DIGIT_FACTORIALS[n] = ret + return ret + + +def chain_length(n: int, previous: set = None) -> int: + """ + Calculate the length of the chain of non-repeating terms starting with n. + Previous is a set containing the previous member of the chain. + >>> chain_length(10101) + 11 + >>> chain_length(555) + 20 + >>> chain_length(178924) + 39 + """ + previous = previous or set() + if n in CHAIN_LENGTH_CACHE: + return CHAIN_LENGTH_CACHE[n] + next_number = sum_digit_factorials(n) + if next_number in previous: + CHAIN_LENGTH_CACHE[n] = 0 + return 0 + else: + previous.add(n) + ret = 1 + chain_length(next_number, previous) + CHAIN_LENGTH_CACHE[n] = ret + return ret + + +def solution(num_terms: int = 60, max_start: int = 1000000) -> int: + """ + Return the number of chains with a starting number below one million which + contain exactly n non-repeating terms. + >>> solution(10,1000) + 28 + """ + return sum(1 for i in range(1, max_start) if chain_length(i) == num_terms) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_074/sol2.py b/project_euler/problem_074/sol2.py new file mode 100644 index 000000000000..689593277a81 --- /dev/null +++ b/project_euler/problem_074/sol2.py @@ -0,0 +1,126 @@ +""" + Project Euler Problem 074: https://projecteuler.net/problem=74 + + Starting from any positive integer number + it is possible to attain another one summing the factorial of its digits. + + Repeating this step, we can build chains of numbers. + It is not difficult to prove that EVERY starting number + will eventually get stuck in a loop. + + The request is to find how many numbers less than one million + produce a chain with exactly 60 non repeating items. + + Solution approach: + This solution simply consists in a loop that generates + the chains of non repeating items. + The generation of the chain stops before a repeating item + or if the size of the chain is greater then the desired one. + After generating each chain, the length is checked and the + counter increases. +""" + +factorial_cache = {} +factorial_sum_cache = {} + + +def factorial(a: int) -> int: + """Returns the factorial of the input a + >>> factorial(5) + 120 + + >>> factorial(6) + 720 + + >>> factorial(0) + 1 + """ + + # The factorial function is not defined for negative numbers + if a < 0: + raise ValueError("Invalid negative input!", a) + + if a in factorial_cache: + return factorial_cache[a] + + # The case of 0! is handled separately + if a == 0: + factorial_cache[a] = 1 + else: + # use a temporary support variable to store the computation + temporary_number = a + temporary_computation = 1 + + while temporary_number > 0: + temporary_computation *= temporary_number + temporary_number -= 1 + + factorial_cache[a] = temporary_computation + return factorial_cache[a] + + +def factorial_sum(a: int) -> int: + """Function to perform the sum of the factorial + of all the digits in a + + >>> factorial_sum(69) + 363600 + """ + if a in factorial_sum_cache: + return factorial_sum_cache[a] + # Prepare a variable to hold the computation + fact_sum = 0 + + """ Convert a in string to iterate on its digits + convert the digit back into an int + and add its factorial to fact_sum. + """ + for i in str(a): + fact_sum += factorial(int(i)) + factorial_sum_cache[a] = fact_sum + return fact_sum + + +def solution(chain_length: int = 60, number_limit: int = 1000000) -> int: + """Returns the number of numbers that produce + chains with exactly 60 non repeating elements. + >>> solution(10, 1000) + 26 + """ + + # the counter for the chains with the exact desired length + chain_counter = 0 + + for i in range(1, number_limit + 1): + + # The temporary list will contain the elements of the chain + chain_set = {i} + len_chain_set = 1 + last_chain_element = i + + # The new element of the chain + new_chain_element = factorial_sum(last_chain_element) + + # Stop computing the chain when you find a repeating item + # or the length it greater then the desired one. + + while new_chain_element not in chain_set and len_chain_set <= chain_length: + chain_set.add(new_chain_element) + + len_chain_set += 1 + last_chain_element = new_chain_element + new_chain_element = factorial_sum(last_chain_element) + + # If the while exited because the chain list contains the exact amount + # of elements increase the counter + if len_chain_set == chain_length: + chain_counter += 1 + + return chain_counter + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print(f"{solution()}") diff --git a/project_euler/problem_075/__init__.py b/project_euler/problem_075/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_075/sol1.py b/project_euler/problem_075/sol1.py new file mode 100644 index 000000000000..b57604d76a86 --- /dev/null +++ b/project_euler/problem_075/sol1.py @@ -0,0 +1,60 @@ +""" +Project Euler Problem 75: https://projecteuler.net/problem=75 + +It turns out that 12 cm is the smallest length of wire that can be bent to form an +integer sided right angle triangle in exactly one way, but there are many more examples. + +12 cm: (3,4,5) +24 cm: (6,8,10) +30 cm: (5,12,13) +36 cm: (9,12,15) +40 cm: (8,15,17) +48 cm: (12,16,20) + +In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided +right angle triangle, and other lengths allow more than one solution to be found; for +example, using 120 cm it is possible to form exactly three different integer sided +right angle triangles. + +120 cm: (30,40,50), (20,48,52), (24,45,51) + +Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can +exactly one integer sided right angle triangle be formed? + +Solution: we generate all pythagorean triples using Euclid's formula and +keep track of the frequencies of the perimeters. + +Reference: https://en.wikipedia.org/wiki/Pythagorean_triple#Generating_a_triple +""" + +from collections import defaultdict +from math import gcd +from typing import DefaultDict + + +def solution(limit: int = 1500000) -> int: + """ + Return the number of values of L <= limit such that a wire of length L can be + formmed into an integer sided right angle triangle in exactly one way. + >>> solution(50) + 6 + >>> solution(1000) + 112 + >>> solution(50000) + 5502 + """ + frequencies: DefaultDict = defaultdict(int) + euclid_m = 2 + while 2 * euclid_m * (euclid_m + 1) <= limit: + for euclid_n in range((euclid_m % 2) + 1, euclid_m, 2): + if gcd(euclid_m, euclid_n) > 1: + continue + primitive_perimeter = 2 * euclid_m * (euclid_m + euclid_n) + for perimeter in range(primitive_perimeter, limit + 1, primitive_perimeter): + frequencies[perimeter] += 1 + euclid_m += 1 + return sum(1 for frequency in frequencies.values() if frequency == 1) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_076/__init__.py b/project_euler/problem_076/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_076/sol1.py b/project_euler/problem_076/sol1.py new file mode 100644 index 000000000000..60bb87089e7e --- /dev/null +++ b/project_euler/problem_076/sol1.py @@ -0,0 +1,55 @@ +""" +Counting Summations +Problem 76: https://projecteuler.net/problem=76 + +It is possible to write five as a sum in exactly six different ways: + +4 + 1 +3 + 2 +3 + 1 + 1 +2 + 2 + 1 +2 + 1 + 1 + 1 +1 + 1 + 1 + 1 + 1 + +How many different ways can one hundred be written as a sum of at least two +positive integers? +""" + + +def solution(m: int = 100) -> int: + """ + Returns the number of different ways the number m can be written as a + sum of at least two positive integers. + + >>> solution(100) + 190569291 + >>> solution(50) + 204225 + >>> solution(30) + 5603 + >>> solution(10) + 41 + >>> solution(5) + 6 + >>> solution(3) + 2 + >>> solution(2) + 1 + >>> solution(1) + 0 + """ + memo = [[0 for _ in range(m)] for _ in range(m + 1)] + for i in range(m + 1): + memo[i][0] = 1 + + for n in range(m + 1): + for k in range(1, m): + memo[n][k] += memo[n][k - 1] + if n > k: + memo[n][k] += memo[n - k - 1][k] + + return memo[m][m - 1] - 1 + + +if __name__ == "__main__": + print(solution(int(input("Enter a number: ").strip()))) diff --git a/project_euler/problem_077/__init__.py b/project_euler/problem_077/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_077/sol1.py b/project_euler/problem_077/sol1.py new file mode 100644 index 000000000000..e92992a90ab3 --- /dev/null +++ b/project_euler/problem_077/sol1.py @@ -0,0 +1,81 @@ +""" +Project Euler Problem 77: https://projecteuler.net/problem=77 + +It is possible to write ten as the sum of primes in exactly five different ways: + +7 + 3 +5 + 5 +5 + 3 + 2 +3 + 3 + 2 + 2 +2 + 2 + 2 + 2 + 2 + +What is the first value which can be written as the sum of primes in over +five thousand different ways? +""" + +from functools import lru_cache +from math import ceil +from typing import Optional, Set + +NUM_PRIMES = 100 + +primes = set(range(3, NUM_PRIMES, 2)) +primes.add(2) +prime: int + +for prime in range(3, ceil(NUM_PRIMES ** 0.5), 2): + if prime not in primes: + continue + primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) + + +@lru_cache(maxsize=100) +def partition(number_to_partition: int) -> Set[int]: + """ + Return a set of integers corresponding to unique prime partitions of n. + The unique prime partitions can be represented as unique prime decompositions, + e.g. (7+3) <-> 7*3 = 12, (3+3+2+2) = 3*3*2*2 = 36 + >>> partition(10) + {32, 36, 21, 25, 30} + >>> partition(15) + {192, 160, 105, 44, 112, 243, 180, 150, 216, 26, 125, 126} + >>> len(partition(20)) + 26 + """ + if number_to_partition < 0: + return set() + elif number_to_partition == 0: + return {1} + + ret: Set[int] = set() + prime: int + sub: int + + for prime in primes: + if prime > number_to_partition: + continue + for sub in partition(number_to_partition - prime): + ret.add(sub * prime) + + return ret + + +def solution(number_unique_partitions: int = 5000) -> Optional[int]: + """ + Return the smallest integer that can be written as the sum of primes in over + m unique ways. + >>> solution(4) + 10 + >>> solution(500) + 45 + >>> solution(1000) + 53 + """ + for number_to_partition in range(1, NUM_PRIMES): + if len(partition(number_to_partition)) > number_unique_partitions: + return number_to_partition + return None + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_08/sol1.py b/project_euler/problem_08/sol1.py deleted file mode 100644 index 817fd3f87507..000000000000 --- a/project_euler/problem_08/sol1.py +++ /dev/null @@ -1,15 +0,0 @@ -import sys -def main(): - LargestProduct = -sys.maxsize-1 - number=input().strip() - for i in range(len(number)-12): - product=1 - for j in range(13): - product *= int(number[i+j]) - if product > LargestProduct: - LargestProduct = product - print(LargestProduct) - - -if __name__ == '__main__': - main() diff --git a/project_euler/problem_08/sol2.py b/project_euler/problem_08/sol2.py deleted file mode 100644 index ae03f3ad0aa6..000000000000 --- a/project_euler/problem_08/sol2.py +++ /dev/null @@ -1,8 +0,0 @@ -from functools import reduce - -def main(): - number=input().strip() - print(max([reduce(lambda x,y: int(x)*int(y),number[i:i+13]) for i in range(len(number)-12)])) - -if __name__ == '__main__': - main() diff --git a/project_euler/problem_080/__init__.py b/project_euler/problem_080/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_080/sol1.py b/project_euler/problem_080/sol1.py new file mode 100644 index 000000000000..db69d7e8451c --- /dev/null +++ b/project_euler/problem_080/sol1.py @@ -0,0 +1,37 @@ +""" +Project Euler Problem 80: https://projecteuler.net/problem=80 +Author: Sandeep Gupta +Problem statement: For the first one hundred natural numbers, find the total of +the digital sums of the first one hundred decimal digits for all the irrational +square roots. +Time: 5 October 2020, 18:30 +""" +import decimal + + +def solution() -> int: + """ + To evaluate the sum, Used decimal python module to calculate the decimal + places up to 100, the most important thing would be take calculate + a few extra places for decimal otherwise there will be rounding + error. + + >>> solution() + 40886 + """ + answer = 0 + decimal_context = decimal.Context(prec=105) + for i in range(2, 100): + number = decimal.Decimal(i) + sqrt_number = number.sqrt(decimal_context) + if len(str(sqrt_number)) > 1: + answer += int(str(sqrt_number)[0]) + sqrt_number = str(sqrt_number)[2:101] + answer += sum([int(x) for x in sqrt_number]) + return answer + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/project_euler/problem_081/__init__.py b/project_euler/problem_081/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_081/matrix.txt b/project_euler/problem_081/matrix.txt new file mode 100644 index 000000000000..f65322a7e541 --- /dev/null +++ b/project_euler/problem_081/matrix.txt @@ -0,0 +1,80 @@ +4445,2697,5115,718,2209,2212,654,4348,3079,6821,7668,3276,8874,4190,3785,2752,9473,7817,9137,496,7338,3434,7152,4355,4552,7917,7827,2460,2350,691,3514,5880,3145,7633,7199,3783,5066,7487,3285,1084,8985,760,872,8609,8051,1134,9536,5750,9716,9371,7619,5617,275,9721,2997,2698,1887,8825,6372,3014,2113,7122,7050,6775,5948,2758,1219,3539,348,7989,2735,9862,1263,8089,6401,9462,3168,2758,3748,5870 +1096,20,1318,7586,5167,2642,1443,5741,7621,7030,5526,4244,2348,4641,9827,2448,6918,5883,3737,300,7116,6531,567,5997,3971,6623,820,6148,3287,1874,7981,8424,7672,7575,6797,6717,1078,5008,4051,8795,5820,346,1851,6463,2117,6058,3407,8211,117,4822,1317,4377,4434,5925,8341,4800,1175,4173,690,8978,7470,1295,3799,8724,3509,9849,618,3320,7068,9633,2384,7175,544,6583,1908,9983,481,4187,9353,9377 +9607,7385,521,6084,1364,8983,7623,1585,6935,8551,2574,8267,4781,3834,2764,2084,2669,4656,9343,7709,2203,9328,8004,6192,5856,3555,2260,5118,6504,1839,9227,1259,9451,1388,7909,5733,6968,8519,9973,1663,5315,7571,3035,4325,4283,2304,6438,3815,9213,9806,9536,196,5542,6907,2475,1159,5820,9075,9470,2179,9248,1828,4592,9167,3713,4640,47,3637,309,7344,6955,346,378,9044,8635,7466,5036,9515,6385,9230 +7206,3114,7760,1094,6150,5182,7358,7387,4497,955,101,1478,7777,6966,7010,8417,6453,4955,3496,107,449,8271,131,2948,6185,784,5937,8001,6104,8282,4165,3642,710,2390,575,715,3089,6964,4217,192,5949,7006,715,3328,1152,66,8044,4319,1735,146,4818,5456,6451,4113,1063,4781,6799,602,1504,6245,6550,1417,1343,2363,3785,5448,4545,9371,5420,5068,4613,4882,4241,5043,7873,8042,8434,3939,9256,2187 +3620,8024,577,9997,7377,7682,1314,1158,6282,6310,1896,2509,5436,1732,9480,706,496,101,6232,7375,2207,2306,110,6772,3433,2878,8140,5933,8688,1399,2210,7332,6172,6403,7333,4044,2291,1790,2446,7390,8698,5723,3678,7104,1825,2040,140,3982,4905,4160,2200,5041,2512,1488,2268,1175,7588,8321,8078,7312,977,5257,8465,5068,3453,3096,1651,7906,253,9250,6021,8791,8109,6651,3412,345,4778,5152,4883,7505 +1074,5438,9008,2679,5397,5429,2652,3403,770,9188,4248,2493,4361,8327,9587,707,9525,5913,93,1899,328,2876,3604,673,8576,6908,7659,2544,3359,3883,5273,6587,3065,1749,3223,604,9925,6941,2823,8767,7039,3290,3214,1787,7904,3421,7137,9560,8451,2669,9219,6332,1576,5477,6755,8348,4164,4307,2984,4012,6629,1044,2874,6541,4942,903,1404,9125,5160,8836,4345,2581,460,8438,1538,5507,668,3352,2678,6942 +4295,1176,5596,1521,3061,9868,7037,7129,8933,6659,5947,5063,3653,9447,9245,2679,767,714,116,8558,163,3927,8779,158,5093,2447,5782,3967,1716,931,7772,8164,1117,9244,5783,7776,3846,8862,6014,2330,6947,1777,3112,6008,3491,1906,5952,314,4602,8994,5919,9214,3995,5026,7688,6809,5003,3128,2509,7477,110,8971,3982,8539,2980,4689,6343,5411,2992,5270,5247,9260,2269,7474,1042,7162,5206,1232,4556,4757 +510,3556,5377,1406,5721,4946,2635,7847,4251,8293,8281,6351,4912,287,2870,3380,3948,5322,3840,4738,9563,1906,6298,3234,8959,1562,6297,8835,7861,239,6618,1322,2553,2213,5053,5446,4402,6500,5182,8585,6900,5756,9661,903,5186,7687,5998,7997,8081,8955,4835,6069,2621,1581,732,9564,1082,1853,5442,1342,520,1737,3703,5321,4793,2776,1508,1647,9101,2499,6891,4336,7012,3329,3212,1442,9993,3988,4930,7706 +9444,3401,5891,9716,1228,7107,109,3563,2700,6161,5039,4992,2242,8541,7372,2067,1294,3058,1306,320,8881,5756,9326,411,8650,8824,5495,8282,8397,2000,1228,7817,2099,6473,3571,5994,4447,1299,5991,543,7874,2297,1651,101,2093,3463,9189,6872,6118,872,1008,1779,2805,9084,4048,2123,5877,55,3075,1737,9459,4535,6453,3644,108,5982,4437,5213,1340,6967,9943,5815,669,8074,1838,6979,9132,9315,715,5048 +3327,4030,7177,6336,9933,5296,2621,4785,2755,4832,2512,2118,2244,4407,2170,499,7532,9742,5051,7687,970,6924,3527,4694,5145,1306,2165,5940,2425,8910,3513,1909,6983,346,6377,4304,9330,7203,6605,3709,3346,970,369,9737,5811,4427,9939,3693,8436,5566,1977,3728,2399,3985,8303,2492,5366,9802,9193,7296,1033,5060,9144,2766,1151,7629,5169,5995,58,7619,7565,4208,1713,6279,3209,4908,9224,7409,1325,8540 +6882,1265,1775,3648,4690,959,5837,4520,5394,1378,9485,1360,4018,578,9174,2932,9890,3696,116,1723,1178,9355,7063,1594,1918,8574,7594,7942,1547,6166,7888,354,6932,4651,1010,7759,6905,661,7689,6092,9292,3845,9605,8443,443,8275,5163,7720,7265,6356,7779,1798,1754,5225,6661,1180,8024,5666,88,9153,1840,3508,1193,4445,2648,3538,6243,6375,8107,5902,5423,2520,1122,5015,6113,8859,9370,966,8673,2442 +7338,3423,4723,6533,848,8041,7921,8277,4094,5368,7252,8852,9166,2250,2801,6125,8093,5738,4038,9808,7359,9494,601,9116,4946,2702,5573,2921,9862,1462,1269,2410,4171,2709,7508,6241,7522,615,2407,8200,4189,5492,5649,7353,2590,5203,4274,710,7329,9063,956,8371,3722,4253,4785,1194,4828,4717,4548,940,983,2575,4511,2938,1827,2027,2700,1236,841,5760,1680,6260,2373,3851,1841,4968,1172,5179,7175,3509 +4420,1327,3560,2376,6260,2988,9537,4064,4829,8872,9598,3228,1792,7118,9962,9336,4368,9189,6857,1829,9863,6287,7303,7769,2707,8257,2391,2009,3975,4993,3068,9835,3427,341,8412,2134,4034,8511,6421,3041,9012,2983,7289,100,1355,7904,9186,6920,5856,2008,6545,8331,3655,5011,839,8041,9255,6524,3862,8788,62,7455,3513,5003,8413,3918,2076,7960,6108,3638,6999,3436,1441,4858,4181,1866,8731,7745,3744,1000 +356,8296,8325,1058,1277,4743,3850,2388,6079,6462,2815,5620,8495,5378,75,4324,3441,9870,1113,165,1544,1179,2834,562,6176,2313,6836,8839,2986,9454,5199,6888,1927,5866,8760,320,1792,8296,7898,6121,7241,5886,5814,2815,8336,1576,4314,3109,2572,6011,2086,9061,9403,3947,5487,9731,7281,3159,1819,1334,3181,5844,5114,9898,4634,2531,4412,6430,4262,8482,4546,4555,6804,2607,9421,686,8649,8860,7794,6672 +9870,152,1558,4963,8750,4754,6521,6256,8818,5208,5691,9659,8377,9725,5050,5343,2539,6101,1844,9700,7750,8114,5357,3001,8830,4438,199,9545,8496,43,2078,327,9397,106,6090,8181,8646,6414,7499,5450,4850,6273,5014,4131,7639,3913,6571,8534,9703,4391,7618,445,1320,5,1894,6771,7383,9191,4708,9706,6939,7937,8726,9382,5216,3685,2247,9029,8154,1738,9984,2626,9438,4167,6351,5060,29,1218,1239,4785 +192,5213,8297,8974,4032,6966,5717,1179,6523,4679,9513,1481,3041,5355,9303,9154,1389,8702,6589,7818,6336,3539,5538,3094,6646,6702,6266,2759,4608,4452,617,9406,8064,6379,444,5602,4950,1810,8391,1536,316,8714,1178,5182,5863,5110,5372,4954,1978,2971,5680,4863,2255,4630,5723,2168,538,1692,1319,7540,440,6430,6266,7712,7385,5702,620,641,3136,7350,1478,3155,2820,9109,6261,1122,4470,14,8493,2095 +1046,4301,6082,474,4974,7822,2102,5161,5172,6946,8074,9716,6586,9962,9749,5015,2217,995,5388,4402,7652,6399,6539,1349,8101,3677,1328,9612,7922,2879,231,5887,2655,508,4357,4964,3554,5930,6236,7384,4614,280,3093,9600,2110,7863,2631,6626,6620,68,1311,7198,7561,1768,5139,1431,221,230,2940,968,5283,6517,2146,1646,869,9402,7068,8645,7058,1765,9690,4152,2926,9504,2939,7504,6074,2944,6470,7859 +4659,736,4951,9344,1927,6271,8837,8711,3241,6579,7660,5499,5616,3743,5801,4682,9748,8796,779,1833,4549,8138,4026,775,4170,2432,4174,3741,7540,8017,2833,4027,396,811,2871,1150,9809,2719,9199,8504,1224,540,2051,3519,7982,7367,2761,308,3358,6505,2050,4836,5090,7864,805,2566,2409,6876,3361,8622,5572,5895,3280,441,7893,8105,1634,2929,274,3926,7786,6123,8233,9921,2674,5340,1445,203,4585,3837 +5759,338,7444,7968,7742,3755,1591,4839,1705,650,7061,2461,9230,9391,9373,2413,1213,431,7801,4994,2380,2703,6161,6878,8331,2538,6093,1275,5065,5062,2839,582,1014,8109,3525,1544,1569,8622,7944,2905,6120,1564,1839,5570,7579,1318,2677,5257,4418,5601,7935,7656,5192,1864,5886,6083,5580,6202,8869,1636,7907,4759,9082,5854,3185,7631,6854,5872,5632,5280,1431,2077,9717,7431,4256,8261,9680,4487,4752,4286 +1571,1428,8599,1230,7772,4221,8523,9049,4042,8726,7567,6736,9033,2104,4879,4967,6334,6716,3994,1269,8995,6539,3610,7667,6560,6065,874,848,4597,1711,7161,4811,6734,5723,6356,6026,9183,2586,5636,1092,7779,7923,8747,6887,7505,9909,1792,3233,4526,3176,1508,8043,720,5212,6046,4988,709,5277,8256,3642,1391,5803,1468,2145,3970,6301,7767,2359,8487,9771,8785,7520,856,1605,8972,2402,2386,991,1383,5963 +1822,4824,5957,6511,9868,4113,301,9353,6228,2881,2966,6956,9124,9574,9233,1601,7340,973,9396,540,4747,8590,9535,3650,7333,7583,4806,3593,2738,8157,5215,8472,2284,9473,3906,6982,5505,6053,7936,6074,7179,6688,1564,1103,6860,5839,2022,8490,910,7551,7805,881,7024,1855,9448,4790,1274,3672,2810,774,7623,4223,4850,6071,9975,4935,1915,9771,6690,3846,517,463,7624,4511,614,6394,3661,7409,1395,8127 +8738,3850,9555,3695,4383,2378,87,6256,6740,7682,9546,4255,6105,2000,1851,4073,8957,9022,6547,5189,2487,303,9602,7833,1628,4163,6678,3144,8589,7096,8913,5823,4890,7679,1212,9294,5884,2972,3012,3359,7794,7428,1579,4350,7246,4301,7779,7790,3294,9547,4367,3549,1958,8237,6758,3497,3250,3456,6318,1663,708,7714,6143,6890,3428,6853,9334,7992,591,6449,9786,1412,8500,722,5468,1371,108,3939,4199,2535 +7047,4323,1934,5163,4166,461,3544,2767,6554,203,6098,2265,9078,2075,4644,6641,8412,9183,487,101,7566,5622,1975,5726,2920,5374,7779,5631,3753,3725,2672,3621,4280,1162,5812,345,8173,9785,1525,955,5603,2215,2580,5261,2765,2990,5979,389,3907,2484,1232,5933,5871,3304,1138,1616,5114,9199,5072,7442,7245,6472,4760,6359,9053,7876,2564,9404,3043,9026,2261,3374,4460,7306,2326,966,828,3274,1712,3446 +3975,4565,8131,5800,4570,2306,8838,4392,9147,11,3911,7118,9645,4994,2028,6062,5431,2279,8752,2658,7836,994,7316,5336,7185,3289,1898,9689,2331,5737,3403,1124,2679,3241,7748,16,2724,5441,6640,9368,9081,5618,858,4969,17,2103,6035,8043,7475,2181,939,415,1617,8500,8253,2155,7843,7974,7859,1746,6336,3193,2617,8736,4079,6324,6645,8891,9396,5522,6103,1857,8979,3835,2475,1310,7422,610,8345,7615 +9248,5397,5686,2988,3446,4359,6634,9141,497,9176,6773,7448,1907,8454,916,1596,2241,1626,1384,2741,3649,5362,8791,7170,2903,2475,5325,6451,924,3328,522,90,4813,9737,9557,691,2388,1383,4021,1609,9206,4707,5200,7107,8104,4333,9860,5013,1224,6959,8527,1877,4545,7772,6268,621,4915,9349,5970,706,9583,3071,4127,780,8231,3017,9114,3836,7503,2383,1977,4870,8035,2379,9704,1037,3992,3642,1016,4303 +5093,138,4639,6609,1146,5565,95,7521,9077,2272,974,4388,2465,2650,722,4998,3567,3047,921,2736,7855,173,2065,4238,1048,5,6847,9548,8632,9194,5942,4777,7910,8971,6279,7253,2516,1555,1833,3184,9453,9053,6897,7808,8629,4877,1871,8055,4881,7639,1537,7701,2508,7564,5845,5023,2304,5396,3193,2955,1088,3801,6203,1748,3737,1276,13,4120,7715,8552,3047,2921,106,7508,304,1280,7140,2567,9135,5266 +6237,4607,7527,9047,522,7371,4883,2540,5867,6366,5301,1570,421,276,3361,527,6637,4861,2401,7522,5808,9371,5298,2045,5096,5447,7755,5115,7060,8529,4078,1943,1697,1764,5453,7085,960,2405,739,2100,5800,728,9737,5704,5693,1431,8979,6428,673,7540,6,7773,5857,6823,150,5869,8486,684,5816,9626,7451,5579,8260,3397,5322,6920,1879,2127,2884,5478,4977,9016,6165,6292,3062,5671,5968,78,4619,4763 +9905,7127,9390,5185,6923,3721,9164,9705,4341,1031,1046,5127,7376,6528,3248,4941,1178,7889,3364,4486,5358,9402,9158,8600,1025,874,1839,1783,309,9030,1843,845,8398,1433,7118,70,8071,2877,3904,8866,6722,4299,10,1929,5897,4188,600,1889,3325,2485,6473,4474,7444,6992,4846,6166,4441,2283,2629,4352,7775,1101,2214,9985,215,8270,9750,2740,8361,7103,5930,8664,9690,8302,9267,344,2077,1372,1880,9550 +5825,8517,7769,2405,8204,1060,3603,7025,478,8334,1997,3692,7433,9101,7294,7498,9415,5452,3850,3508,6857,9213,6807,4412,7310,854,5384,686,4978,892,8651,3241,2743,3801,3813,8588,6701,4416,6990,6490,3197,6838,6503,114,8343,5844,8646,8694,65,791,5979,2687,2621,2019,8097,1423,3644,9764,4921,3266,3662,5561,2476,8271,8138,6147,1168,3340,1998,9874,6572,9873,6659,5609,2711,3931,9567,4143,7833,8887 +6223,2099,2700,589,4716,8333,1362,5007,2753,2848,4441,8397,7192,8191,4916,9955,6076,3370,6396,6971,3156,248,3911,2488,4930,2458,7183,5455,170,6809,6417,3390,1956,7188,577,7526,2203,968,8164,479,8699,7915,507,6393,4632,1597,7534,3604,618,3280,6061,9793,9238,8347,568,9645,2070,5198,6482,5000,9212,6655,5961,7513,1323,3872,6170,3812,4146,2736,67,3151,5548,2781,9679,7564,5043,8587,1893,4531 +5826,3690,6724,2121,9308,6986,8106,6659,2142,1642,7170,2877,5757,6494,8026,6571,8387,9961,6043,9758,9607,6450,8631,8334,7359,5256,8523,2225,7487,1977,9555,8048,5763,2414,4948,4265,2427,8978,8088,8841,9208,9601,5810,9398,8866,9138,4176,5875,7212,3272,6759,5678,7649,4922,5422,1343,8197,3154,3600,687,1028,4579,2084,9467,4492,7262,7296,6538,7657,7134,2077,1505,7332,6890,8964,4879,7603,7400,5973,739 +1861,1613,4879,1884,7334,966,2000,7489,2123,4287,1472,3263,4726,9203,1040,4103,6075,6049,330,9253,4062,4268,1635,9960,577,1320,3195,9628,1030,4092,4979,6474,6393,2799,6967,8687,7724,7392,9927,2085,3200,6466,8702,265,7646,8665,7986,7266,4574,6587,612,2724,704,3191,8323,9523,3002,704,5064,3960,8209,2027,2758,8393,4875,4641,9584,6401,7883,7014,768,443,5490,7506,1852,2005,8850,5776,4487,4269 +4052,6687,4705,7260,6645,6715,3706,5504,8672,2853,1136,8187,8203,4016,871,1809,1366,4952,9294,5339,6872,2645,6083,7874,3056,5218,7485,8796,7401,3348,2103,426,8572,4163,9171,3176,948,7654,9344,3217,1650,5580,7971,2622,76,2874,880,2034,9929,1546,2659,5811,3754,7096,7436,9694,9960,7415,2164,953,2360,4194,2397,1047,2196,6827,575,784,2675,8821,6802,7972,5996,6699,2134,7577,2887,1412,4349,4380 +4629,2234,6240,8132,7592,3181,6389,1214,266,1910,2451,8784,2790,1127,6932,1447,8986,2492,5476,397,889,3027,7641,5083,5776,4022,185,3364,5701,2442,2840,4160,9525,4828,6602,2614,7447,3711,4505,7745,8034,6514,4907,2605,7753,6958,7270,6936,3006,8968,439,2326,4652,3085,3425,9863,5049,5361,8688,297,7580,8777,7916,6687,8683,7141,306,9569,2384,1500,3346,4601,7329,9040,6097,2727,6314,4501,4974,2829 +8316,4072,2025,6884,3027,1808,5714,7624,7880,8528,4205,8686,7587,3230,1139,7273,6163,6986,3914,9309,1464,9359,4474,7095,2212,7302,2583,9462,7532,6567,1606,4436,8981,5612,6796,4385,5076,2007,6072,3678,8331,1338,3299,8845,4783,8613,4071,1232,6028,2176,3990,2148,3748,103,9453,538,6745,9110,926,3125,473,5970,8728,7072,9062,1404,1317,5139,9862,6496,6062,3338,464,1600,2532,1088,8232,7739,8274,3873 +2341,523,7096,8397,8301,6541,9844,244,4993,2280,7689,4025,4196,5522,7904,6048,2623,9258,2149,9461,6448,8087,7245,1917,8340,7127,8466,5725,6996,3421,5313,512,9164,9837,9794,8369,4185,1488,7210,1524,1016,4620,9435,2478,7765,8035,697,6677,3724,6988,5853,7662,3895,9593,1185,4727,6025,5734,7665,3070,138,8469,6748,6459,561,7935,8646,2378,462,7755,3115,9690,8877,3946,2728,8793,244,6323,8666,4271 +6430,2406,8994,56,1267,3826,9443,7079,7579,5232,6691,3435,6718,5698,4144,7028,592,2627,217,734,6194,8156,9118,58,2640,8069,4127,3285,694,3197,3377,4143,4802,3324,8134,6953,7625,3598,3584,4289,7065,3434,2106,7132,5802,7920,9060,7531,3321,1725,1067,3751,444,5503,6785,7937,6365,4803,198,6266,8177,1470,6390,1606,2904,7555,9834,8667,2033,1723,5167,1666,8546,8152,473,4475,6451,7947,3062,3281 +2810,3042,7759,1741,2275,2609,7676,8640,4117,1958,7500,8048,1757,3954,9270,1971,4796,2912,660,5511,3553,1012,5757,4525,6084,7198,8352,5775,7726,8591,7710,9589,3122,4392,6856,5016,749,2285,3356,7482,9956,7348,2599,8944,495,3462,3578,551,4543,7207,7169,7796,1247,4278,6916,8176,3742,8385,2310,1345,8692,2667,4568,1770,8319,3585,4920,3890,4928,7343,5385,9772,7947,8786,2056,9266,3454,2807,877,2660 +6206,8252,5928,5837,4177,4333,207,7934,5581,9526,8906,1498,8411,2984,5198,5134,2464,8435,8514,8674,3876,599,5327,826,2152,4084,2433,9327,9697,4800,2728,3608,3849,3861,3498,9943,1407,3991,7191,9110,5666,8434,4704,6545,5944,2357,1163,4995,9619,6754,4200,9682,6654,4862,4744,5953,6632,1054,293,9439,8286,2255,696,8709,1533,1844,6441,430,1999,6063,9431,7018,8057,2920,6266,6799,356,3597,4024,6665 +3847,6356,8541,7225,2325,2946,5199,469,5450,7508,2197,9915,8284,7983,6341,3276,3321,16,1321,7608,5015,3362,8491,6968,6818,797,156,2575,706,9516,5344,5457,9210,5051,8099,1617,9951,7663,8253,9683,2670,1261,4710,1068,8753,4799,1228,2621,3275,6188,4699,1791,9518,8701,5932,4275,6011,9877,2933,4182,6059,2930,6687,6682,9771,654,9437,3169,8596,1827,5471,8909,2352,123,4394,3208,8756,5513,6917,2056 +5458,8173,3138,3290,4570,4892,3317,4251,9699,7973,1163,1935,5477,6648,9614,5655,9592,975,9118,2194,7322,8248,8413,3462,8560,1907,7810,6650,7355,2939,4973,6894,3933,3784,3200,2419,9234,4747,2208,2207,1945,2899,1407,6145,8023,3484,5688,7686,2737,3828,3704,9004,5190,9740,8643,8650,5358,4426,1522,1707,3613,9887,6956,2447,2762,833,1449,9489,2573,1080,4167,3456,6809,2466,227,7125,2759,6250,6472,8089 +3266,7025,9756,3914,1265,9116,7723,9788,6805,5493,2092,8688,6592,9173,4431,4028,6007,7131,4446,4815,3648,6701,759,3312,8355,4485,4187,5188,8746,7759,3528,2177,5243,8379,3838,7233,4607,9187,7216,2190,6967,2920,6082,7910,5354,3609,8958,6949,7731,494,8753,8707,1523,4426,3543,7085,647,6771,9847,646,5049,824,8417,5260,2730,5702,2513,9275,4279,2767,8684,1165,9903,4518,55,9682,8963,6005,2102,6523 +1998,8731,936,1479,5259,7064,4085,91,7745,7136,3773,3810,730,8255,2705,2653,9790,6807,2342,355,9344,2668,3690,2028,9679,8102,574,4318,6481,9175,5423,8062,2867,9657,7553,3442,3920,7430,3945,7639,3714,3392,2525,4995,4850,2867,7951,9667,486,9506,9888,781,8866,1702,3795,90,356,1483,4200,2131,6969,5931,486,6880,4404,1084,5169,4910,6567,8335,4686,5043,2614,3352,2667,4513,6472,7471,5720,1616 +8878,1613,1716,868,1906,2681,564,665,5995,2474,7496,3432,9491,9087,8850,8287,669,823,347,6194,2264,2592,7871,7616,8508,4827,760,2676,4660,4881,7572,3811,9032,939,4384,929,7525,8419,5556,9063,662,8887,7026,8534,3111,1454,2082,7598,5726,6687,9647,7608,73,3014,5063,670,5461,5631,3367,9796,8475,7908,5073,1565,5008,5295,4457,1274,4788,1728,338,600,8415,8535,9351,7750,6887,5845,1741,125 +3637,6489,9634,9464,9055,2413,7824,9517,7532,3577,7050,6186,6980,9365,9782,191,870,2497,8498,2218,2757,5420,6468,586,3320,9230,1034,1393,9886,5072,9391,1178,8464,8042,6869,2075,8275,3601,7715,9470,8786,6475,8373,2159,9237,2066,3264,5000,679,355,3069,4073,494,2308,5512,4334,9438,8786,8637,9774,1169,1949,6594,6072,4270,9158,7916,5752,6794,9391,6301,5842,3285,2141,3898,8027,4310,8821,7079,1307 +8497,6681,4732,7151,7060,5204,9030,7157,833,5014,8723,3207,9796,9286,4913,119,5118,7650,9335,809,3675,2597,5144,3945,5090,8384,187,4102,1260,2445,2792,4422,8389,9290,50,1765,1521,6921,8586,4368,1565,5727,7855,2003,4834,9897,5911,8630,5070,1330,7692,7557,7980,6028,5805,9090,8265,3019,3802,698,9149,5748,1965,9658,4417,5994,5584,8226,2937,272,5743,1278,5698,8736,2595,6475,5342,6596,1149,6920 +8188,8009,9546,6310,8772,2500,9846,6592,6872,3857,1307,8125,7042,1544,6159,2330,643,4604,7899,6848,371,8067,2062,3200,7295,1857,9505,6936,384,2193,2190,301,8535,5503,1462,7380,5114,4824,8833,1763,4974,8711,9262,6698,3999,2645,6937,7747,1128,2933,3556,7943,2885,3122,9105,5447,418,2899,5148,3699,9021,9501,597,4084,175,1621,1,1079,6067,5812,4326,9914,6633,5394,4233,6728,9084,1864,5863,1225 +9935,8793,9117,1825,9542,8246,8437,3331,9128,9675,6086,7075,319,1334,7932,3583,7167,4178,1726,7720,695,8277,7887,6359,5912,1719,2780,8529,1359,2013,4498,8072,1129,9998,1147,8804,9405,6255,1619,2165,7491,1,8882,7378,3337,503,5758,4109,3577,985,3200,7615,8058,5032,1080,6410,6873,5496,1466,2412,9885,5904,4406,3605,8770,4361,6205,9193,1537,9959,214,7260,9566,1685,100,4920,7138,9819,5637,976 +3466,9854,985,1078,7222,8888,5466,5379,3578,4540,6853,8690,3728,6351,7147,3134,6921,9692,857,3307,4998,2172,5783,3931,9417,2541,6299,13,787,2099,9131,9494,896,8600,1643,8419,7248,2660,2609,8579,91,6663,5506,7675,1947,6165,4286,1972,9645,3805,1663,1456,8853,5705,9889,7489,1107,383,4044,2969,3343,152,7805,4980,9929,5033,1737,9953,7197,9158,4071,1324,473,9676,3984,9680,3606,8160,7384,5432 +1005,4512,5186,3953,2164,3372,4097,3247,8697,3022,9896,4101,3871,6791,3219,2742,4630,6967,7829,5991,6134,1197,1414,8923,8787,1394,8852,5019,7768,5147,8004,8825,5062,9625,7988,1110,3992,7984,9966,6516,6251,8270,421,3723,1432,4830,6935,8095,9059,2214,6483,6846,3120,1587,6201,6691,9096,9627,6671,4002,3495,9939,7708,7465,5879,6959,6634,3241,3401,2355,9061,2611,7830,3941,2177,2146,5089,7079,519,6351 +7280,8586,4261,2831,7217,3141,9994,9940,5462,2189,4005,6942,9848,5350,8060,6665,7519,4324,7684,657,9453,9296,2944,6843,7499,7847,1728,9681,3906,6353,5529,2822,3355,3897,7724,4257,7489,8672,4356,3983,1948,6892,7415,4153,5893,4190,621,1736,4045,9532,7701,3671,1211,1622,3176,4524,9317,7800,5638,6644,6943,5463,3531,2821,1347,5958,3436,1438,2999,994,850,4131,2616,1549,3465,5946,690,9273,6954,7991 +9517,399,3249,2596,7736,2142,1322,968,7350,1614,468,3346,3265,7222,6086,1661,5317,2582,7959,4685,2807,2917,1037,5698,1529,3972,8716,2634,3301,3412,8621,743,8001,4734,888,7744,8092,3671,8941,1487,5658,7099,2781,99,1932,4443,4756,4652,9328,1581,7855,4312,5976,7255,6480,3996,2748,1973,9731,4530,2790,9417,7186,5303,3557,351,7182,9428,1342,9020,7599,1392,8304,2070,9138,7215,2008,9937,1106,7110 +7444,769,9688,632,1571,6820,8743,4338,337,3366,3073,1946,8219,104,4210,6986,249,5061,8693,7960,6546,1004,8857,5997,9352,4338,6105,5008,2556,6518,6694,4345,3727,7956,20,3954,8652,4424,9387,2035,8358,5962,5304,5194,8650,8282,1256,1103,2138,6679,1985,3653,2770,2433,4278,615,2863,1715,242,3790,2636,6998,3088,1671,2239,957,5411,4595,6282,2881,9974,2401,875,7574,2987,4587,3147,6766,9885,2965 +3287,3016,3619,6818,9073,6120,5423,557,2900,2015,8111,3873,1314,4189,1846,4399,7041,7583,2427,2864,3525,5002,2069,748,1948,6015,2684,438,770,8367,1663,7887,7759,1885,157,7770,4520,4878,3857,1137,3525,3050,6276,5569,7649,904,4533,7843,2199,5648,7628,9075,9441,3600,7231,2388,5640,9096,958,3058,584,5899,8150,1181,9616,1098,8162,6819,8171,1519,1140,7665,8801,2632,1299,9192,707,9955,2710,7314 +1772,2963,7578,3541,3095,1488,7026,2634,6015,4633,4370,2762,1650,2174,909,8158,2922,8467,4198,4280,9092,8856,8835,5457,2790,8574,9742,5054,9547,4156,7940,8126,9824,7340,8840,6574,3547,1477,3014,6798,7134,435,9484,9859,3031,4,1502,4133,1738,1807,4825,463,6343,9701,8506,9822,9555,8688,8168,3467,3234,6318,1787,5591,419,6593,7974,8486,9861,6381,6758,194,3061,4315,2863,4665,3789,2201,1492,4416 +126,8927,6608,5682,8986,6867,1715,6076,3159,788,3140,4744,830,9253,5812,5021,7616,8534,1546,9590,1101,9012,9821,8132,7857,4086,1069,7491,2988,1579,2442,4321,2149,7642,6108,250,6086,3167,24,9528,7663,2685,1220,9196,1397,5776,1577,1730,5481,977,6115,199,6326,2183,3767,5928,5586,7561,663,8649,9688,949,5913,9160,1870,5764,9887,4477,6703,1413,4995,5494,7131,2192,8969,7138,3997,8697,646,1028 +8074,1731,8245,624,4601,8706,155,8891,309,2552,8208,8452,2954,3124,3469,4246,3352,1105,4509,8677,9901,4416,8191,9283,5625,7120,2952,8881,7693,830,4580,8228,9459,8611,4499,1179,4988,1394,550,2336,6089,6872,269,7213,1848,917,6672,4890,656,1478,6536,3165,4743,4990,1176,6211,7207,5284,9730,4738,1549,4986,4942,8645,3698,9429,1439,2175,6549,3058,6513,1574,6988,8333,3406,5245,5431,7140,7085,6407 +7845,4694,2530,8249,290,5948,5509,1588,5940,4495,5866,5021,4626,3979,3296,7589,4854,1998,5627,3926,8346,6512,9608,1918,7070,4747,4182,2858,2766,4606,6269,4107,8982,8568,9053,4244,5604,102,2756,727,5887,2566,7922,44,5986,621,1202,374,6988,4130,3627,6744,9443,4568,1398,8679,397,3928,9159,367,2917,6127,5788,3304,8129,911,2669,1463,9749,264,4478,8940,1109,7309,2462,117,4692,7724,225,2312 +4164,3637,2000,941,8903,39,3443,7172,1031,3687,4901,8082,4945,4515,7204,9310,9349,9535,9940,218,1788,9245,2237,1541,5670,6538,6047,5553,9807,8101,1925,8714,445,8332,7309,6830,5786,5736,7306,2710,3034,1838,7969,6318,7912,2584,2080,7437,6705,2254,7428,820,782,9861,7596,3842,3631,8063,5240,6666,394,4565,7865,4895,9890,6028,6117,4724,9156,4473,4552,602,470,6191,4927,5387,884,3146,1978,3000 +4258,6880,1696,3582,5793,4923,2119,1155,9056,9698,6603,3768,5514,9927,9609,6166,6566,4536,4985,4934,8076,9062,6741,6163,7399,4562,2337,5600,2919,9012,8459,1308,6072,1225,9306,8818,5886,7243,7365,8792,6007,9256,6699,7171,4230,7002,8720,7839,4533,1671,478,7774,1607,2317,5437,4705,7886,4760,6760,7271,3081,2997,3088,7675,6208,3101,6821,6840,122,9633,4900,2067,8546,4549,2091,7188,5605,8599,6758,5229 +7854,5243,9155,3556,8812,7047,2202,1541,5993,4600,4760,713,434,7911,7426,7414,8729,322,803,7960,7563,4908,6285,6291,736,3389,9339,4132,8701,7534,5287,3646,592,3065,7582,2592,8755,6068,8597,1982,5782,1894,2900,6236,4039,6569,3037,5837,7698,700,7815,2491,7272,5878,3083,6778,6639,3589,5010,8313,2581,6617,5869,8402,6808,2951,2321,5195,497,2190,6187,1342,1316,4453,7740,4154,2959,1781,1482,8256 +7178,2046,4419,744,8312,5356,6855,8839,319,2962,5662,47,6307,8662,68,4813,567,2712,9931,1678,3101,8227,6533,4933,6656,92,5846,4780,6256,6361,4323,9985,1231,2175,7178,3034,9744,6155,9165,7787,5836,9318,7860,9644,8941,6480,9443,8188,5928,161,6979,2352,5628,6991,1198,8067,5867,6620,3778,8426,2994,3122,3124,6335,3918,8897,2655,9670,634,1088,1576,8935,7255,474,8166,7417,9547,2886,5560,3842 +6957,3111,26,7530,7143,1295,1744,6057,3009,1854,8098,5405,2234,4874,9447,2620,9303,27,7410,969,40,2966,5648,7596,8637,4238,3143,3679,7187,690,9980,7085,7714,9373,5632,7526,6707,3951,9734,4216,2146,3602,5371,6029,3039,4433,4855,4151,1449,3376,8009,7240,7027,4602,2947,9081,4045,8424,9352,8742,923,2705,4266,3232,2264,6761,363,2651,3383,7770,6730,7856,7340,9679,2158,610,4471,4608,910,6241 +4417,6756,1013,8797,658,8809,5032,8703,7541,846,3357,2920,9817,1745,9980,7593,4667,3087,779,3218,6233,5568,4296,2289,2654,7898,5021,9461,5593,8214,9173,4203,2271,7980,2983,5952,9992,8399,3468,1776,3188,9314,1720,6523,2933,621,8685,5483,8986,6163,3444,9539,4320,155,3992,2828,2150,6071,524,2895,5468,8063,1210,3348,9071,4862,483,9017,4097,6186,9815,3610,5048,1644,1003,9865,9332,2145,1944,2213 +9284,3803,4920,1927,6706,4344,7383,4786,9890,2010,5228,1224,3158,6967,8580,8990,8883,5213,76,8306,2031,4980,5639,9519,7184,5645,7769,3259,8077,9130,1317,3096,9624,3818,1770,695,2454,947,6029,3474,9938,3527,5696,4760,7724,7738,2848,6442,5767,6845,8323,4131,2859,7595,2500,4815,3660,9130,8580,7016,8231,4391,8369,3444,4069,4021,556,6154,627,2778,1496,4206,6356,8434,8491,3816,8231,3190,5575,1015 +3787,7572,1788,6803,5641,6844,1961,4811,8535,9914,9999,1450,8857,738,4662,8569,6679,2225,7839,8618,286,2648,5342,2294,3205,4546,176,8705,3741,6134,8324,8021,7004,5205,7032,6637,9442,5539,5584,4819,5874,5807,8589,6871,9016,983,1758,3786,1519,6241,185,8398,495,3370,9133,3051,4549,9674,7311,9738,3316,9383,2658,2776,9481,7558,619,3943,3324,6491,4933,153,9738,4623,912,3595,7771,7939,1219,4405 +2650,3883,4154,5809,315,7756,4430,1788,4451,1631,6461,7230,6017,5751,138,588,5282,2442,9110,9035,6349,2515,1570,6122,4192,4174,3530,1933,4186,4420,4609,5739,4135,2963,6308,1161,8809,8619,2796,3819,6971,8228,4188,1492,909,8048,2328,6772,8467,7671,9068,2226,7579,6422,7056,8042,3296,2272,3006,2196,7320,3238,3490,3102,37,1293,3212,4767,5041,8773,5794,4456,6174,7279,7054,2835,7053,9088,790,6640 +3101,1057,7057,3826,6077,1025,2955,1224,1114,6729,5902,4698,6239,7203,9423,1804,4417,6686,1426,6941,8071,1029,4985,9010,6122,6597,1622,1574,3513,1684,7086,5505,3244,411,9638,4150,907,9135,829,981,1707,5359,8781,9751,5,9131,3973,7159,1340,6955,7514,7993,6964,8198,1933,2797,877,3993,4453,8020,9349,8646,2779,8679,2961,3547,3374,3510,1129,3568,2241,2625,9138,5974,8206,7669,7678,1833,8700,4480 +4865,9912,8038,8238,782,3095,8199,1127,4501,7280,2112,2487,3626,2790,9432,1475,6312,8277,4827,2218,5806,7132,8752,1468,7471,6386,739,8762,8323,8120,5169,9078,9058,3370,9560,7987,8585,8531,5347,9312,1058,4271,1159,5286,5404,6925,8606,9204,7361,2415,560,586,4002,2644,1927,2824,768,4409,2942,3345,1002,808,4941,6267,7979,5140,8643,7553,9438,7320,4938,2666,4609,2778,8158,6730,3748,3867,1866,7181 +171,3771,7134,8927,4778,2913,3326,2004,3089,7853,1378,1729,4777,2706,9578,1360,5693,3036,1851,7248,2403,2273,8536,6501,9216,613,9671,7131,7719,6425,773,717,8803,160,1114,7554,7197,753,4513,4322,8499,4533,2609,4226,8710,6627,644,9666,6260,4870,5744,7385,6542,6203,7703,6130,8944,5589,2262,6803,6381,7414,6888,5123,7320,9392,9061,6780,322,8975,7050,5089,1061,2260,3199,1150,1865,5386,9699,6501 +3744,8454,6885,8277,919,1923,4001,6864,7854,5519,2491,6057,8794,9645,1776,5714,9786,9281,7538,6916,3215,395,2501,9618,4835,8846,9708,2813,3303,1794,8309,7176,2206,1602,1838,236,4593,2245,8993,4017,10,8215,6921,5206,4023,5932,6997,7801,262,7640,3107,8275,4938,7822,2425,3223,3886,2105,8700,9526,2088,8662,8034,7004,5710,2124,7164,3574,6630,9980,4242,2901,9471,1491,2117,4562,1130,9086,4117,6698 +2810,2280,2331,1170,4554,4071,8387,1215,2274,9848,6738,1604,7281,8805,439,1298,8318,7834,9426,8603,6092,7944,1309,8828,303,3157,4638,4439,9175,1921,4695,7716,1494,1015,1772,5913,1127,1952,1950,8905,4064,9890,385,9357,7945,5035,7082,5369,4093,6546,5187,5637,2041,8946,1758,7111,6566,1027,1049,5148,7224,7248,296,6169,375,1656,7993,2816,3717,4279,4675,1609,3317,42,6201,3100,3144,163,9530,4531 +7096,6070,1009,4988,3538,5801,7149,3063,2324,2912,7911,7002,4338,7880,2481,7368,3516,2016,7556,2193,1388,3865,8125,4637,4096,8114,750,3144,1938,7002,9343,4095,1392,4220,3455,6969,9647,1321,9048,1996,1640,6626,1788,314,9578,6630,2813,6626,4981,9908,7024,4355,3201,3521,3864,3303,464,1923,595,9801,3391,8366,8084,9374,1041,8807,9085,1892,9431,8317,9016,9221,8574,9981,9240,5395,2009,6310,2854,9255 +8830,3145,2960,9615,8220,6061,3452,2918,6481,9278,2297,3385,6565,7066,7316,5682,107,7646,4466,68,1952,9603,8615,54,7191,791,6833,2560,693,9733,4168,570,9127,9537,1925,8287,5508,4297,8452,8795,6213,7994,2420,4208,524,5915,8602,8330,2651,8547,6156,1812,6271,7991,9407,9804,1553,6866,1128,2119,4691,9711,8315,5879,9935,6900,482,682,4126,1041,428,6247,3720,5882,7526,2582,4327,7725,3503,2631 +2738,9323,721,7434,1453,6294,2957,3786,5722,6019,8685,4386,3066,9057,6860,499,5315,3045,5194,7111,3137,9104,941,586,3066,755,4177,8819,7040,5309,3583,3897,4428,7788,4721,7249,6559,7324,825,7311,3760,6064,6070,9672,4882,584,1365,9739,9331,5783,2624,7889,1604,1303,1555,7125,8312,425,8936,3233,7724,1480,403,7440,1784,1754,4721,1569,652,3893,4574,5692,9730,4813,9844,8291,9199,7101,3391,8914 +6044,2928,9332,3328,8588,447,3830,1176,3523,2705,8365,6136,5442,9049,5526,8575,8869,9031,7280,706,2794,8814,5767,4241,7696,78,6570,556,5083,1426,4502,3336,9518,2292,1885,3740,3153,9348,9331,8051,2759,5407,9028,7840,9255,831,515,2612,9747,7435,8964,4971,2048,4900,5967,8271,1719,9670,2810,6777,1594,6367,6259,8316,3815,1689,6840,9437,4361,822,9619,3065,83,6344,7486,8657,8228,9635,6932,4864 +8478,4777,6334,4678,7476,4963,6735,3096,5860,1405,5127,7269,7793,4738,227,9168,2996,8928,765,733,1276,7677,6258,1528,9558,3329,302,8901,1422,8277,6340,645,9125,8869,5952,141,8141,1816,9635,4025,4184,3093,83,2344,2747,9352,7966,1206,1126,1826,218,7939,2957,2729,810,8752,5247,4174,4038,8884,7899,9567,301,5265,5752,7524,4381,1669,3106,8270,6228,6373,754,2547,4240,2313,5514,3022,1040,9738 +2265,8192,1763,1369,8469,8789,4836,52,1212,6690,5257,8918,6723,6319,378,4039,2421,8555,8184,9577,1432,7139,8078,5452,9628,7579,4161,7490,5159,8559,1011,81,478,5840,1964,1334,6875,8670,9900,739,1514,8692,522,9316,6955,1345,8132,2277,3193,9773,3923,4177,2183,1236,6747,6575,4874,6003,6409,8187,745,8776,9440,7543,9825,2582,7381,8147,7236,5185,7564,6125,218,7991,6394,391,7659,7456,5128,5294 +2132,8992,8160,5782,4420,3371,3798,5054,552,5631,7546,4716,1332,6486,7892,7441,4370,6231,4579,2121,8615,1145,9391,1524,1385,2400,9437,2454,7896,7467,2928,8400,3299,4025,7458,4703,7206,6358,792,6200,725,4275,4136,7390,5984,4502,7929,5085,8176,4600,119,3568,76,9363,6943,2248,9077,9731,6213,5817,6729,4190,3092,6910,759,2682,8380,1254,9604,3011,9291,5329,9453,9746,2739,6522,3765,5634,1113,5789 +5304,5499,564,2801,679,2653,1783,3608,7359,7797,3284,796,3222,437,7185,6135,8571,2778,7488,5746,678,6140,861,7750,803,9859,9918,2425,3734,2698,9005,4864,9818,6743,2475,132,9486,3825,5472,919,292,4411,7213,7699,6435,9019,6769,1388,802,2124,1345,8493,9487,8558,7061,8777,8833,2427,2238,5409,4957,8503,3171,7622,5779,6145,2417,5873,5563,5693,9574,9491,1937,7384,4563,6842,5432,2751,3406,7981 diff --git a/project_euler/problem_081/sol1.py b/project_euler/problem_081/sol1.py new file mode 100644 index 000000000000..afa143f23b33 --- /dev/null +++ b/project_euler/problem_081/sol1.py @@ -0,0 +1,47 @@ +""" +Problem 81: https://projecteuler.net/problem=81 +In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, +by only moving to the right and down, is indicated in bold red and is equal to 2427. + + [131] 673 234 103 18 + [201] [96] [342] 965 150 + 630 803 [746] [422] 111 + 537 699 497 [121] 956 + 805 732 524 [37] [331] + +Find the minimal path sum from the top left to the bottom right by only moving right +and down in matrix.txt (https://projecteuler.net/project/resources/p081_matrix.txt), +a 31K text file containing an 80 by 80 matrix. +""" +import os + + +def solution(filename: str = "matrix.txt") -> int: + """ + Returns the minimal path sum from the top left to the bottom right of the matrix. + >>> solution() + 427337 + """ + with open(os.path.join(os.path.dirname(__file__), filename), "r") as in_file: + data = in_file.read() + + grid = [[int(cell) for cell in row.split(",")] for row in data.strip().splitlines()] + dp = [[0 for cell in row] for row in grid] + n = len(grid[0]) + + dp = [[0 for i in range(n)] for j in range(n)] + dp[0][0] = grid[0][0] + for i in range(1, n): + dp[0][i] = grid[0][i] + dp[0][i - 1] + for i in range(1, n): + dp[i][0] = grid[i][0] + dp[i - 1][0] + + for i in range(1, n): + for j in range(1, n): + dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]) + + return dp[-1][-1] + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_085/__init__.py b/project_euler/problem_085/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_085/sol1.py b/project_euler/problem_085/sol1.py new file mode 100644 index 000000000000..74e36b1301a4 --- /dev/null +++ b/project_euler/problem_085/sol1.py @@ -0,0 +1,108 @@ +""" +Project Euler Problem 85: https://projecteuler.net/problem=85 + +By counting carefully it can be seen that a rectangular grid measuring 3 by 2 +contains eighteen rectangles. + +Although there exists no rectangular grid that contains exactly two million +rectangles, find the area of the grid with the nearest solution. + +Solution: + + For a grid with side-lengths a and b, the number of rectangles contained in the grid + is [a*(a+1)/2] * [b*(b+1)/2)], which happens to be the product of the a-th and b-th + triangle numbers. So to find the solution grid (a,b), we need to find the two + triangle numbers whose product is closest to two million. + + Denote these two triangle numbers Ta and Tb. We want their product Ta*Tb to be + as close as possible to 2m. Assuming that the best solution is fairly close to 2m, + We can assume that both Ta and Tb are roughly bounded by 2m. Since Ta = a(a+1)/2, + we can assume that a (and similarly b) are roughly bounded by sqrt(2 * 2m) = 2000. + Since this is a rough bound, to be on the safe side we add 10%. Therefore we start + by generating all the triangle numbers Ta for 1 <= a <= 2200. This can be done + iteratively since the ith triangle number is the sum of 1,2, ... ,i, and so + T(i) = T(i-1) + i. + + We then search this list of triangle numbers for the two that give a product + closest to our target of two million. Rather than testing every combination of 2 + elements of the list, which would find the result in quadratic time, we can find + the best pair in linear time. + + We iterate through the list of triangle numbers using enumerate() so we have a + and Ta. Since we want Ta * Tb to be as close as possible to 2m, we know that Tb + needs to be roughly 2m / Ta. Using the formula Tb = b*(b+1)/2 as well as the + quadratic formula, we can solve for b: + b is roughly (-1 + sqrt(1 + 8 * 2m / Ta)) / 2. + + Since the closest integers to this estimate will give product closest to 2m, + we only need to consider the integers above and below. It's then a simple matter + to get the triangle numbers corresponding to those integers, calculate the product + Ta * Tb, compare that product to our target 2m, and keep track of the (a,b) pair + that comes the closest. + + +Reference: https://en.wikipedia.org/wiki/Triangular_number + https://en.wikipedia.org/wiki/Quadratic_formula +""" + + +from math import ceil, floor, sqrt +from typing import List + + +def solution(target: int = 2000000) -> int: + """ + Find the area of the grid which contains as close to two million rectangles + as possible. + >>> solution(20) + 6 + >>> solution(2000) + 72 + >>> solution(2000000000) + 86595 + """ + triangle_numbers: List[int] = [0] + idx: int + + for idx in range(1, ceil(sqrt(target * 2) * 1.1)): + triangle_numbers.append(triangle_numbers[-1] + idx) + + # we want this to be as close as possible to target + best_product: int = 0 + # the area corresponding to the grid that gives the product closest to target + area: int = 0 + # an estimate of b, using the quadratic formula + b_estimate: float + # the largest integer less than b_estimate + b_floor: int + # the largest integer less than b_estimate + b_ceil: int + # the triangle number corresponding to b_floor + triangle_b_first_guess: int + # the triangle number corresponding to b_ceil + triangle_b_second_guess: int + + for idx_a, triangle_a in enumerate(triangle_numbers[1:], 1): + b_estimate = (-1 + sqrt(1 + 8 * target / triangle_a)) / 2 + b_floor = floor(b_estimate) + b_ceil = ceil(b_estimate) + triangle_b_first_guess = triangle_numbers[b_floor] + triangle_b_second_guess = triangle_numbers[b_ceil] + + if abs(target - triangle_b_first_guess * triangle_a) < abs( + target - best_product + ): + best_product = triangle_b_first_guess * triangle_a + area = idx_a * b_floor + + if abs(target - triangle_b_second_guess * triangle_a) < abs( + target - best_product + ): + best_product = triangle_b_second_guess * triangle_a + area = idx_a * b_ceil + + return area + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_086/__init__.py b/project_euler/problem_086/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_086/sol1.py b/project_euler/problem_086/sol1.py new file mode 100644 index 000000000000..0bf66e6b5a31 --- /dev/null +++ b/project_euler/problem_086/sol1.py @@ -0,0 +1,105 @@ +""" +Project Euler Problem 86: https://projecteuler.net/problem=86 + +A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, +sits in the opposite corner. By travelling on the surfaces of the room the shortest +"straight line" distance from S to F is 10 and the path is shown on the diagram. + +However, there are up to three "shortest" path candidates for any given cuboid and the +shortest route doesn't always have integer length. + +It can be shown that there are exactly 2060 distinct cuboids, ignoring rotations, with +integer dimensions, up to a maximum size of M by M by M, for which the shortest route +has integer length when M = 100. This is the least value of M for which the number of +solutions first exceeds two thousand; the number of solutions when M = 99 is 1975. + +Find the least value of M such that the number of solutions first exceeds one million. + +Solution: + Label the 3 side-lengths of the cuboid a,b,c such that 1 <= a <= b <= c <= M. + By conceptually "opening up" the cuboid and laying out its faces on a plane, + it can be seen that the shortest distance between 2 opposite corners is + sqrt((a+b)^2 + c^2). This distance is an integer if and only if (a+b),c make up + the first 2 sides of a pythagorean triplet. + + The second useful insight is rather than calculate the number of cuboids + with integral shortest distance for each maximum cuboid side-length M, + we can calculate this number iteratively each time we increase M, as follows. + The set of cuboids satisfying this property with maximum side-length M-1 is a + subset of the cuboids satisfying the property with maximum side-length M + (since any cuboids with side lengths <= M-1 are also <= M). To calculate the + number of cuboids in the larger set (corresponding to M) we need only consider + the cuboids which have at least one side of length M. Since we have ordered the + side lengths a <= b <= c, we can assume that c = M. Then we just need to count + the number of pairs a,b satisfying the conditions: + sqrt((a+b)^2 + M^2) is integer + 1 <= a <= b <= M + + To count the number of pairs (a,b) satisfying these conditions, write d = a+b. + Now we have: + 1 <= a <= b <= M => 2 <= d <= 2*M + we can actually make the second equality strict, + since d = 2*M => d^2 + M^2 = 5M^2 + => shortest distance = M * sqrt(5) + => not integral. + a + b = d => b = d - a + and a <= b + => a <= d/2 + also a <= M + => a <= min(M, d//2) + + a + b = d => a = d - b + and b <= M + => a >= d - M + also a >= 1 + => a >= max(1, d - M) + + So a is in range(max(1, d - M), min(M, d // 2) + 1) + + For a given d, the number of cuboids satisfying the required property with c = M + and a + b = d is the length of this range, which is + min(M, d // 2) + 1 - max(1, d - M). + + In the code below, d is sum_shortest_sides + and M is max_cuboid_size. + + +""" + + +from math import sqrt + + +def solution(limit: int = 1000000) -> int: + """ + Return the least value of M such that there are more than one million cuboids + of side lengths 1 <= a,b,c <= M such that the shortest distance between two + opposite vertices of the cuboid is integral. + >>> solution(100) + 24 + >>> solution(1000) + 72 + >>> solution(2000) + 100 + >>> solution(20000) + 288 + """ + num_cuboids: int = 0 + max_cuboid_size: int = 0 + sum_shortest_sides: int + + while num_cuboids <= limit: + max_cuboid_size += 1 + for sum_shortest_sides in range(2, 2 * max_cuboid_size + 1): + if sqrt(sum_shortest_sides ** 2 + max_cuboid_size ** 2).is_integer(): + num_cuboids += ( + min(max_cuboid_size, sum_shortest_sides // 2) + - max(1, sum_shortest_sides - max_cuboid_size) + + 1 + ) + + return max_cuboid_size + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_087/__init__.py b/project_euler/problem_087/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_087/sol1.py b/project_euler/problem_087/sol1.py new file mode 100644 index 000000000000..f444481c17ac --- /dev/null +++ b/project_euler/problem_087/sol1.py @@ -0,0 +1,52 @@ +""" +Project Euler Problem 87: https://projecteuler.net/problem=87 + +The smallest number expressible as the sum of a prime square, prime cube, and prime +fourth power is 28. In fact, there are exactly four numbers below fifty that can be +expressed in such a way: + +28 = 22 + 23 + 24 +33 = 32 + 23 + 24 +49 = 52 + 23 + 24 +47 = 22 + 33 + 24 + +How many numbers below fifty million can be expressed as the sum of a prime square, +prime cube, and prime fourth power? +""" + + +def solution(limit: int = 50000000) -> int: + """ + Return the number of integers less than limit which can be expressed as the sum + of a prime square, prime cube, and prime fourth power. + >>> solution(50) + 4 + """ + ret = set() + prime_square_limit = int((limit - 24) ** (1 / 2)) + + primes = set(range(3, prime_square_limit + 1, 2)) + primes.add(2) + for p in range(3, prime_square_limit + 1, 2): + if p not in primes: + continue + primes.difference_update(set(range(p * p, prime_square_limit + 1, p))) + + for prime1 in primes: + square = prime1 * prime1 + for prime2 in primes: + cube = prime2 * prime2 * prime2 + if square + cube >= limit - 16: + break + for prime3 in primes: + tetr = prime3 * prime3 * prime3 * prime3 + total = square + cube + tetr + if total >= limit: + break + ret.add(total) + + return len(ret) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_089/__init__.py b/project_euler/problem_089/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_089/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_089/numeralcleanup_test.txt b/project_euler/problem_089/numeralcleanup_test.txt new file mode 100644 index 000000000000..06142142cca9 --- /dev/null +++ b/project_euler/problem_089/numeralcleanup_test.txt @@ -0,0 +1,5 @@ +IIII +IV +IIIIIIIIII +X +VIIIII diff --git a/project_euler/problem_089/p089_roman.txt b/project_euler/problem_089/p089_roman.txt new file mode 100644 index 000000000000..50651c355a5b --- /dev/null +++ b/project_euler/problem_089/p089_roman.txt @@ -0,0 +1,1000 @@ +MMMMDCLXXII +MMDCCCLXXXIII +MMMDLXVIIII +MMMMDXCV +DCCCLXXII +MMCCCVI +MMMCDLXXXVII +MMMMCCXXI +MMMCCXX +MMMMDCCCLXXIII +MMMCCXXXVII +MMCCCLXXXXIX +MDCCCXXIIII +MMCXCVI +CCXCVIII +MMMCCCXXXII +MDCCXXX +MMMDCCCL +MMMMCCLXXXVI +MMDCCCXCVI +MMMDCII +MMMCCXII +MMMMDCCCCI +MMDCCCXCII +MDCXX +CMLXXXVII +MMMXXI +MMMMCCCXIV +MLXXII +MCCLXXVIIII +MMMMCCXXXXI +MMDCCCLXXII +MMMMXXXI +MMMDCCLXXX +MMDCCCLXXIX +MMMMLXXXV +MCXXI +MDCCCXXXVII +MMCCCLXVII +MCDXXXV +CCXXXIII +CMXX +MMMCLXIV +MCCCLXXXVI +DCCCXCVIII +MMMDCCCCXXXIV +CDXVIIII +MMCCXXXV +MDCCCXXXII +MMMMD +MMDCCLXIX +MMMMCCCLXXXXVI +MMDCCXLII +MMMDCCCVIIII +DCCLXXXIIII +MDCCCCXXXII +MMCXXVII +DCCCXXX +CCLXIX +MMMXI +MMMMCMLXXXXVIII +MMMMDLXXXVII +MMMMDCCCLX +MMCCLIV +CMIX +MMDCCCLXXXIIII +CLXXXII +MMCCCCXXXXV +MMMMDLXXXVIIII +MMMDCCCXXI +MMDCCCCLXXVI +MCCCCLXX +MMCDLVIIII +MMMDCCCLIX +MMMMCCCCXIX +MMMDCCCLXXV +XXXI +CDLXXXIII +MMMCXV +MMDCCLXIII +MMDXXX +MMMMCCCLVII +MMMDCI +MMMMCDLXXXIIII +MMMMCCCXVI +CCCLXXXVIII +MMMMCML +MMMMXXIV +MMMCCCCXXX +DCCX +MMMCCLX +MMDXXXIII +CCCLXIII +MMDCCXIII +MMMCCCXLIV +CLXXXXI +CXVI +MMMMCXXXIII +CLXX +DCCCXVIII +MLXVII +DLXXXX +MMDXXI +MMMMDLXXXXVIII +MXXII +LXI +DCCCCXLIII +MMMMDV +MMMMXXXIV +MDCCCLVIII +MMMCCLXXII +MMMMDCCXXXVI +MMMMLXXXIX +MDCCCLXXXI +MMMMDCCCXV +MMMMCCCCXI +MMMMCCCLIII +MDCCCLXXI +MMCCCCXI +MLXV +MMCDLXII +MMMMDXXXXII +MMMMDCCCXL +MMMMCMLVI +CCLXXXIV +MMMDCCLXXXVI +MMCLII +MMMCCCCXV +MMLXXXIII +MMMV +MMMV +DCCLXII +MMDCCCCXVI +MMDCXLVIII +CCLIIII +CCCXXV +MMDCCLXXXVIIII +MMMMDCLXXVIII +MMMMDCCCXCI +MMMMCCCXX +MMCCXLV +MMMDCCCLXIX +MMCCLXIIII +MMMDCCCXLIX +MMMMCCCLXIX +CMLXXXXI +MCMLXXXIX +MMCDLXI +MMDCLXXVIII +MMMMDCCLXI +MCDXXV +DL +CCCLXXII +MXVIIII +MCCCCLXVIII +CIII +MMMDCCLXXIIII +MMMDVIII +MMMMCCCLXXXXVII +MMDXXVII +MMDCCLXXXXV +MMMMCXLVI +MMMDCCLXXXII +MMMDXXXVI +MCXXII +CLI +DCLXXXIX +MMMCLI +MDCLXIII +MMMMDCCXCVII +MMCCCLXXXV +MMMDCXXVIII +MMMCDLX +MMMCMLII +MMMIV +MMMMDCCCLVIII +MMMDLXXXVIII +MCXXIV +MMMMLXXVI +CLXXIX +MMMCCCCXXVIIII +DCCLXXXV +MMMDCCCVI +LI +CLXXXVI +MMMMCCCLXXVI +MCCCLXVI +CCXXXIX +MMDXXXXI +MMDCCCXLI +DCCCLXXXVIII +MMMMDCCCIV +MDCCCCXV +MMCMVI +MMMMCMLXXXXV +MMDCCLVI +MMMMCCXLVIII +DCCCCIIII +MMCCCCIII +MMMDCCLXXXVIIII +MDCCCLXXXXV +DVII +MMMV +DCXXV +MMDCCCXCV +DCVIII +MMCDLXVI +MCXXVIII +MDCCXCVIII +MMDCLX +MMMDCCLXIV +MMCDLXXVII +MMDLXXXIIII +MMMMCCCXXII +MMMDCCCXLIIII +DCCCCLXVII +MMMCLXXXXIII +MCCXV +MMMMDCXI +MMMMDCLXXXXV +MMMCCCLII +MMCMIX +MMDCCXXV +MMDLXXXVI +MMMMDCXXVIIII +DCCCCXXXVIIII +MMCCXXXIIII +MMDCCLXXVIII +MDCCLXVIIII +MMCCLXXXV +MMMMDCCCLXXXVIII +MMCMXCI +MDXLII +MMMMDCCXIV +MMMMLI +DXXXXIII +MMDCCXI +MMMMCCLXXXIII +MMMDCCCLXXIII +MDCLVII +MMCD +MCCCXXVII +MMMMDCCIIII +MMMDCCXLVI +MMMCLXXXVII +MMMCCVIIII +MCCCCLXXIX +DL +DCCCLXXVI +MMDXCI +MMMMDCCCCXXXVI +MMCII +MMMDCCCXXXXV +MMMCDXLV +MMDCXXXXIV +MMD +MDCCCLXXXX +MMDCXLIII +MMCCXXXII +MMDCXXXXVIIII +DCCCLXXI +MDXCVIIII +MMMMCCLXXVIII +MDCLVIIII +MMMCCCLXXXIX +MDCLXXXV +MDLVIII +MMMMCCVII +MMMMDCXIV +MMMCCCLXIIII +MMIIII +MMMMCCCLXXIII +CCIII +MMMCCLV +MMMDXIII +MMMCCCXC +MMMDCCCXXI +MMMMCCCCXXXII +CCCLVI +MMMCCCLXXXVI +MXVIIII +MMMCCCCXIIII +CLXVII +MMMCCLXX +CCCCLXIV +MMXXXXII +MMMMCCLXXXX +MXL +CCXVI +CCCCLVIIII +MMCCCII +MCCCLVIII +MMMMCCCX +MCDLXXXXIV +MDCCCXIII +MMDCCCXL +MMMMCCCXXIII +DXXXIV +CVI +MMMMDCLXXX +DCCCVII +MMCMLXIIII +MMMDCCCXXXIII +DCCC +MDIII +MMCCCLXVI +MMMCCCCLXXI +MMDCCCCXVIII +CCXXXVII +CCCXXV +MDCCCXII +MMMCMV +MMMMCMXV +MMMMDCXCI +DXXI +MMCCXLVIIII +MMMMCMLII +MDLXXX +MMDCLXVI +CXXI +MMMDCCCLIIII +MMMCXXI +MCCIII +MMDCXXXXI +CCXCII +MMMMDXXXV +MMMCCCLXV +MMMMDLXV +MMMCCCCXXXII +MMMCCCVIII +DCCCCLXXXXII +MMCLXIV +MMMMCXI +MLXXXXVII +MMMCDXXXVIII +MDXXII +MLV +MMMMDLXVI +MMMCXII +XXXIII +MMMMDCCCXXVI +MMMLXVIIII +MMMLX +MMMCDLXVII +MDCCCLVII +MMCXXXVII +MDCCCCXXX +MMDCCCLXIII +MMMMDCXLIX +MMMMCMXLVIII +DCCCLXXVIIII +MDCCCLIII +MMMCMLXI +MMMMCCLXI +MMDCCCLIII +MMMDCCCVI +MMDXXXXIX +MMCLXXXXV +MMDXXX +MMMXIII +DCLXXIX +DCCLXII +MMMMDCCLXVIII +MDCCXXXXIII +CCXXXII +MMMMDCXXV +MMMCCCXXVIII +MDCVIII +MMMCLXXXXIIII +CLXXXI +MDCCCCXXXIII +MMMMDCXXX +MMMDCXXIV +MMMCCXXXVII +MCCCXXXXIIII +CXVIII +MMDCCCCIV +MMMMCDLXXV +MMMDLXIV +MDXCIII +MCCLXXXI +MMMDCCCXXIV +MCXLIII +MMMDCCCI +MCCLXXX +CCXV +MMDCCLXXI +MMDLXXXIII +MMMMDCXVII +MMMCMLXV +MCLXVIII +MMMMCCLXXVI +MMMDCCLXVIIII +MMMMDCCCIX +DLXXXXIX +DCCCXXII +MMMMIII +MMMMCCCLXXVI +DCCCXCIII +DXXXI +MXXXIIII +CCXII +MMMDCCLXXXIIII +MMMCXX +MMMCMXXVII +DCCCXXXX +MMCDXXXVIIII +MMMMDCCXVIII +LV +MMMDCCCCVI +MCCCII +MMCMLXVIIII +MDCCXI +MMMMDLXVII +MMCCCCLXI +MMDCCV +MMMCCCXXXIIII +MMMMDI +MMMDCCCXCV +MMDCCLXXXXI +MMMDXXVI +MMMDCCCLVI +MMDCXXX +MCCCVII +MMMMCCCLXII +MMMMXXV +MMCMXXV +MMLVI +MMDXXX +MMMMCVII +MDC +MCCIII +MMMMDCC +MMCCLXXV +MMDCCCXXXXVI +MMMMCCCLXV +CDXIIII +MLXIIII +CCV +MMMCMXXXI +CCCCLXVI +MDXXXII +MMMMCCCLVIII +MMV +MMMCLII +MCMLI +MMDCCXX +MMMMCCCCXXXVI +MCCLXXXI +MMMCMVI +DCCXXX +MMMMCCCLXV +DCCCXI +MMMMDCCCXIV +CCCXXI +MMDLXXV +CCCCLXXXX +MCCCLXXXXII +MMDCIX +DCCXLIIII +DXIV +MMMMCLII +CDLXI +MMMCXXVII +MMMMDCCCCLXIII +MMMDCLIIII +MCCCCXXXXII +MMCCCLX +CCCCLIII +MDCCLXXVI +MCMXXIII +MMMMDLXXVIII +MMDCCCCLX +MMMCCCLXXXX +MMMCDXXVI +MMMDLVIII +CCCLXI +MMMMDCXXII +MMDCCCXXI +MMDCCXIII +MMMMCLXXXVI +MDCCCCXXVI +MDV +MMDCCCCLXXVI +MMMMCCXXXVII +MMMDCCLXXVIIII +MMMCCCCLXVII +DCCXLI +MMCLXXXVIII +MCCXXXVI +MMDCXLVIII +MMMMCXXXII +MMMMDCCLXVI +MMMMCMLI +MMMMCLXV +MMMMDCCCXCIV +MCCLXXVII +LXXVIIII +DCCLII +MMMCCCXCVI +MMMCLV +MMDCCCXXXXVIII +DCCCXV +MXC +MMDCCLXXXXVII +MMMMCML +MMDCCCLXXVIII +DXXI +MCCCXLI +DCLXXXXI +MMCCCLXXXXVIII +MDCCCCLXXVIII +MMMMDXXV +MMMDCXXXVI +MMMCMXCVII +MMXVIIII +MMMDCCLXXIV +MMMCXXV +DXXXVIII +MMMMCLXVI +MDXII +MMCCCLXX +CCLXXI +DXIV +MMMCLIII +DLII +MMMCCCXLIX +MMCCCCXXVI +MMDCXLIII +MXXXXII +CCCLXXXV +MDCLXXVI +MDCXII +MMMCCCLXXXIII +MMDCCCCLXXXII +MMMMCCCLXXXV +MMDCXXI +DCCCXXX +MMMDCCCCLII +MMMDCCXXII +MMMMCDXCVIII +MMMCCLXVIIII +MMXXV +MMMMCDXIX +MMMMCCCX +MMMCCCCLXVI +MMMMDCLXXVIIII +MMMMDCXXXXIV +MMMCMXII +MMMMXXXIII +MMMMDLXXXII +DCCCLIV +MDXVIIII +MMMCLXXXXV +CCCCXX +MMDIX +MMCMLXXXVIII +DCCXLIII +DCCLX +D +MCCCVII +MMMMCCCLXXXIII +MDCCCLXXIIII +MMMDCCCCLXXXVII +MMMMCCCVII +MMMDCCLXXXXVI +CDXXXIV +MCCLXVIII +MMMMDLX +MMMMDXII +MMMMCCCCLIIII +MCMLXXXXIII +MMMMDCCCIII +MMDCLXXXIII +MDCCCXXXXIV +XXXXVII +MMMDCCCXXXII +MMMDCCCXLII +MCXXXV +MDCXXVIIII +MMMCXXXXIIII +MMMMCDXVII +MMMDXXIII +MMMMCCCCLXI +DCLXXXXVIIII +LXXXXI +CXXXIII +MCDX +MCCLVII +MDCXXXXII +MMMCXXIV +MMMMLXXXX +MMDCCCCXLV +MLXXX +MMDCCCCLX +MCDLIII +MMMCCCLXVII +MMMMCCCLXXIV +MMMDCVIII +DCCCCXXIII +MMXCI +MMDCCIV +MMMMDCCCXXXIV +CCCLXXI +MCCLXXXII +MCMIII +CCXXXI +DCCXXXVIII +MMMMDCCXLVIIII +MMMMCMXXXV +DCCCLXXV +DCCXCI +MMMMDVII +MMMMDCCCLXVIIII +CCCXCV +MMMMDCCXX +MCCCCII +MMMCCCXC +MMMCCCII +MMDCCLXXVII +MMDCLIIII +CCXLIII +MMMDCXVIII +MMMCCCIX +MCXV +MMCCXXV +MLXXIIII +MDCCXXVI +MMMCCCXX +MMDLXX +MMCCCCVI +MMDCCXX +MMMMDCCCCXCV +MDCCCXXXII +MMMMDCCCCXXXX +XCIV +MMCCCCLX +MMXVII +MLXXI +MMMDXXVIII +MDCCCCII +MMMCMLVII +MMCLXXXXVIII +MDCCCCLV +MCCCCLXXIIII +MCCCLII +MCDXLVI +MMMMDXVIII +DCCLXXXIX +MMMDCCLXIV +MDCCCCXLIII +CLXXXXV +MMMMCCXXXVI +MMMDCCCXXI +MMMMCDLXXVII +MCDLIII +MMCCXLVI +DCCCLV +MCDLXX +DCLXXVIII +MMDCXXXIX +MMMMDCLX +MMDCCLI +MMCXXXV +MMMCCXII +MMMMCMLXII +MMMMCCV +MCCCCLXIX +MMMMCCIII +CLXVII +MCCCLXXXXIIII +MMMMDCVIII +MMDCCCLXI +MMLXXIX +CMLXIX +MMDCCCXLVIIII +DCLXII +MMMCCCXLVII +MDCCCXXXV +MMMMDCCXCVI +DCXXX +XXVI +MMLXIX +MMCXI +DCXXXVII +MMMMCCCXXXXVIII +MMMMDCLXI +MMMMDCLXXIIII +MMMMVIII +MMMMDCCCLXII +MDCXCI +MMCCCXXIIII +CCCCXXXXV +MMDCCCXXI +MCVI +MMDCCLXVIII +MMMMCXL +MLXVIII +CMXXVII +CCCLV +MDCCLXXXIX +MMMCCCCLXV +MMDCCLXII +MDLXVI +MMMCCCXVIII +MMMMCCLXXXI +MMCXXVII +MMDCCCLXVIII +MMMCXCII +MMMMDCLVIII +MMMMDCCCXXXXII +MMDCCCCLXXXXVI +MDCCXL +MDCCLVII +MMMMDCCCLXXXVI +DCCXXXIII +MMMMDCCCCLXXXV +MMCCXXXXVIII +MMMCCLXXVIII +MMMDCLXXVIII +DCCCI +MMMMLXXXXVIIII +MMMCCCCLXXII +MMCLXXXVII +CCLXVI +MCDXLIII +MMCXXVIII +MDXIV +CCCXCVIII +CLXXVIII +MMCXXXXVIIII +MMMDCLXXXIV +CMLVIII +MCDLIX +MMMMDCCCXXXII +MMMMDCXXXIIII +MDCXXI +MMMDCXLV +MCLXXVIII +MCDXXII +IV +MCDLXXXXIII +MMMMDCCLXV +CCLI +MMMMDCCCXXXVIII +DCLXII +MCCCLXVII +MMMMDCCCXXXVI +MMDCCXLI +MLXI +MMMCDLXVIII +MCCCCXCIII +XXXIII +MMMDCLXIII +MMMMDCL +DCCCXXXXIIII +MMDLVII +DXXXVII +MCCCCXXIIII +MCVII +MMMMDCCXL +MMMMCXXXXIIII +MCCCCXXIV +MMCLXVIII +MMXCIII +MDCCLXXX +MCCCLIIII +MMDCLXXI +MXI +MCMLIV +MMMCCIIII +DCCLXXXVIIII +MDCLIV +MMMDCXIX +CMLXXXI +DCCLXXXVII +XXV +MMMXXXVI +MDVIIII +CLXIII +MMMCDLVIIII +MMCCCCVII +MMMLXX +MXXXXII +MMMMCCCLXVIII +MMDCCCXXVIII +MMMMDCXXXXI +MMMMDCCCXXXXV +MMMXV +MMMMCCXVIIII +MMDCCXIIII +MMMXXVII +MDCCLVIIII +MMCXXIIII +MCCCLXXIV +DCLVIII +MMMLVII +MMMCXLV +MMXCVII +MMMCCCLXXXVII +MMMMCCXXII +DXII +MMMDLV +MCCCLXXVIII +MMMCLIIII +MMMMCLXXXX +MMMCLXXXIIII +MDCXXIII +MMMMCCXVI +MMMMDLXXXIII +MMMDXXXXIII +MMMMCCCCLV +MMMDLXXXI +MMMCCLXXVI +MMMMXX +MMMMDLVI +MCCCCLXXX +MMMXXII +MMXXII +MMDCCCCXXXI +MMMDXXV +MMMDCLXXXVIIII +MMMDLXXXXVII +MDLXIIII +CMXC +MMMXXXVIII +MDLXXXVIII +MCCCLXXVI +MMCDLIX +MMDCCCXVIII +MDCCCXXXXVI +MMMMCMIV +MMMMDCIIII +MMCCXXXV +XXXXVI +MMMMCCXVII +MMCCXXIV +MCMLVIIII +MLXXXIX +MMMMLXXXIX +CLXXXXIX +MMMDCCCCLVIII +MMMMCCLXXIII +MCCCC +DCCCLIX +MMMCCCLXXXII +MMMCCLXVIIII +MCLXXXV +CDLXXXVII +DCVI +MMX +MMCCXIII +MMMMDCXX +MMMMXXVIII +DCCCLXII +MMMMCCCXLIII +MMMMCLXV +DXCI +MMMMCLXXX +MMMDCCXXXXI +MMMMXXXXVI +DCLX +MMMCCCXI +MCCLXXX +MMCDLXXII +DCCLXXI +MMMCCCXXXVI +MCCCCLXXXVIIII +CDLVIII +DCCLVI +MMMMDCXXXVIII +MMCCCLXXXIII +MMMMDCCLXXV +MMMXXXVI +CCCLXXXXIX +CV +CCCCXIII +CCCCXVI +MDCCCLXXXIIII +MMDCCLXXXII +MMMMCCCCLXXXI +MXXV +MMCCCLXXVIIII +MMMCCXII +MMMMCCXXXIII +MMCCCLXXXVI +MMMDCCCLVIIII +MCCXXXVII +MDCLXXV +XXXV +MMDLI +MMMCCXXX +MMMMCXXXXV +CCCCLIX +MMMMDCCCLXXIII +MMCCCXVII +DCCCXVI +MMMCCCXXXXV +MDCCCCXCV +CLXXXI +MMMMDCCLXX +MMMDCCCIII +MMCLXXVII +MMMDCCXXIX +MMDCCCXCIIII +MMMCDXXIIII +MMMMXXVIII +MMMMDCCCCLXVIII +MDCCCXX +MMMMCDXXI +MMMMDLXXXIX +CCXVI +MDVIII +MMCCLXXI +MMMDCCCLXXI +MMMCCCLXXVI +MMCCLXI +MMMMDCCCXXXIV +DLXXXVI +MMMMDXXXII +MMMXXIIII +MMMMCDIV +MMMMCCCXLVIII +MMMMCXXXVIII +MMMCCCLXVI +MDCCXVIII +MMCXX +CCCLIX +MMMMDCCLXXII +MDCCCLXXV +MMMMDCCCXXIV +DCCCXXXXVIII +MMMDCCCCXXXVIIII +MMMMCCXXXV +MDCLXXXIII +MMCCLXXXIV +MCLXXXXIIII +DXXXXIII +MCCCXXXXVIII +MMCLXXIX +MMMMCCLXIV +MXXII +MMMCXIX +MDCXXXVII +MMDCCVI +MCLXXXXVIII +MMMCXVI +MCCCLX +MMMCDX +CCLXVIIII +MMMCCLX +MCXXVIII +LXXXII +MCCCCLXXXI +MMMI +MMMCCCLXIV +MMMCCCXXVIIII +CXXXVIII +MMCCCXX +MMMCCXXVIIII +MCCLXVI +MMMCCCCXXXXVI +MMDCCXCIX +MCMLXXI +MMCCLXVIII +CDLXXXXIII +MMMMDCCXXII +MMMMDCCLXXXVII +MMMDCCLIV +MMCCLXIII +MDXXXVII +DCCXXXIIII +MCII +MMMDCCCLXXI +MMMLXXIII +MDCCCLIII +MMXXXVIII +MDCCXVIIII +MDCCCCXXXVII +MMCCCXVI +MCMXXII +MMMCCCLVIII +MMMMDCCCXX +MCXXIII +MMMDLXI +MMMMDXXII +MDCCCX +MMDXCVIIII +MMMDCCCCVIII +MMMMDCCCCXXXXVI +MMDCCCXXXV +MMCXCIV +MCMLXXXXIII +MMMCCCLXXVI +MMMMDCLXXXV +CMLXIX +DCXCII +MMXXVIII +MMMMCCCXXX +XXXXVIIII \ No newline at end of file diff --git a/project_euler/problem_089/sol1.py b/project_euler/problem_089/sol1.py new file mode 100644 index 000000000000..11582aa4ab1a --- /dev/null +++ b/project_euler/problem_089/sol1.py @@ -0,0 +1,141 @@ +""" +Project Euler Problem 89: https://projecteuler.net/problem=89 + +For a number written in Roman numerals to be considered valid there are basic rules +which must be followed. Even though the rules allow some numbers to be expressed in +more than one way there is always a "best" way of writing a particular number. + +For example, it would appear that there are at least six ways of writing the number +sixteen: + +IIIIIIIIIIIIIIII +VIIIIIIIIIII +VVIIIIII +XIIIIII +VVVI +XVI + +However, according to the rules only XIIIIII and XVI are valid, and the last example +is considered to be the most efficient, as it uses the least number of numerals. + +The 11K text file, roman.txt (right click and 'Save Link/Target As...'), contains one +thousand numbers written in valid, but not necessarily minimal, Roman numerals; see +About... Roman Numerals for the definitive rules for this problem. + +Find the number of characters saved by writing each of these in their minimal form. + +Note: You can assume that all the Roman numerals in the file contain no more than four +consecutive identical units. +""" + +import os + +SYMBOLS = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} + + +def parse_roman_numerals(numerals: str) -> int: + """ + Converts a string of roman numerals to an integer. + e.g. + >>> parse_roman_numerals("LXXXIX") + 89 + >>> parse_roman_numerals("IIII") + 4 + """ + + total_value = 0 + + index = 0 + while index < len(numerals) - 1: + current_value = SYMBOLS[numerals[index]] + next_value = SYMBOLS[numerals[index + 1]] + if current_value < next_value: + total_value -= current_value + else: + total_value += current_value + index += 1 + total_value += SYMBOLS[numerals[index]] + + return total_value + + +def generate_roman_numerals(num: int) -> str: + """ + Generates a string of roman numerals for a given integer. + e.g. + >>> generate_roman_numerals(89) + 'LXXXIX' + >>> generate_roman_numerals(4) + 'IV' + """ + + numerals = "" + + m_count = num // 1000 + numerals += m_count * "M" + num %= 1000 + + c_count = num // 100 + if c_count == 9: + numerals += "CM" + c_count -= 9 + elif c_count == 4: + numerals += "CD" + c_count -= 4 + if c_count >= 5: + numerals += "D" + c_count -= 5 + numerals += c_count * "C" + num %= 100 + + x_count = num // 10 + if x_count == 9: + numerals += "XC" + x_count -= 9 + elif x_count == 4: + numerals += "XL" + x_count -= 4 + if x_count >= 5: + numerals += "L" + x_count -= 5 + numerals += x_count * "X" + num %= 10 + + if num == 9: + numerals += "IX" + num -= 9 + elif num == 4: + numerals += "IV" + num -= 4 + if num >= 5: + numerals += "V" + num -= 5 + numerals += num * "I" + + return numerals + + +def solution(roman_numerals_filename: str = "/p089_roman.txt") -> int: + """ + Calculates and returns the answer to project euler problem 89. + + >>> solution("/numeralcleanup_test.txt") + 16 + """ + + savings = 0 + + file1 = open(os.path.dirname(__file__) + roman_numerals_filename, "r") + lines = file1.readlines() + for line in lines: + original = line.strip() + num = parse_roman_numerals(original) + shortened = generate_roman_numerals(num) + savings += len(original) - len(shortened) + + return savings + + +if __name__ == "__main__": + + print(f"{solution() = }") diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py deleted file mode 100644 index e54c543b4721..000000000000 --- a/project_euler/problem_09/sol1.py +++ /dev/null @@ -1,15 +0,0 @@ -from __future__ import print_function -# Program to find the product of a,b,c which are Pythagorean Triplet that satisfice the following: -# 1. a < b < c -# 2. a**2 + b**2 = c**2 -# 3. a + b + c = 1000 - -print("Please Wait...") -for a in range(300): - for b in range(400): - for c in range(500): - if(a < b < c): - if((a**2) + (b**2) == (c**2)): - if((a+b+c) == 1000): - print(("Product of",a,"*",b,"*",c,"=",(a*b*c))) - break diff --git a/project_euler/problem_09/sol2.py b/project_euler/problem_09/sol2.py deleted file mode 100644 index 933f5c557d71..000000000000 --- a/project_euler/problem_09/sol2.py +++ /dev/null @@ -1,18 +0,0 @@ -"""A Pythagorean triplet is a set of three natural numbers, for which, -a^2+b^2=c^2 -Given N, Check if there exists any Pythagorean triplet for which a+b+c=N -Find maximum possible value of product of a,b,c among all such Pythagorean triplets, If there is no such Pythagorean triplet print -1.""" -#!/bin/python3 - -product=-1 -d=0 -N = int(input()) -for a in range(1,N//3): - """Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c """ - b=(N*N-2*a*N)//(2*N-2*a) - c=N-a-b - if c*c==(a*a+b*b): - d=(a*b*c) - if d>=product: - product=d -print(product) diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py deleted file mode 100644 index 5ebf38e76e1a..000000000000 --- a/project_euler/problem_09/sol3.py +++ /dev/null @@ -1,6 +0,0 @@ -def main(): - print([a*b*c for a in range(1,999) for b in range(a,999) for c in range(b,999) - if (a*a+b*b==c*c) and (a+b+c==1000 ) ][0]) - -if __name__ == '__main__': - main() diff --git a/project_euler/problem_091/__init__.py b/project_euler/problem_091/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_091/sol1.py b/project_euler/problem_091/sol1.py new file mode 100644 index 000000000000..6c9aa3fa6c70 --- /dev/null +++ b/project_euler/problem_091/sol1.py @@ -0,0 +1,59 @@ +""" +Project Euler Problem 91: https://projecteuler.net/problem=91 + +The points P (x1, y1) and Q (x2, y2) are plotted at integer coordinates and +are joined to the origin, O(0,0), to form ΔOPQ. + +There are exactly fourteen triangles containing a right angle that can be formed +when each coordinate lies between 0 and 2 inclusive; that is, +0 ≤ x1, y1, x2, y2 ≤ 2. + +Given that 0 ≤ x1, y1, x2, y2 ≤ 50, how many right triangles can be formed? +""" + + +from itertools import combinations, product + + +def is_right(x1: int, y1: int, x2: int, y2: int) -> bool: + """ + Check if the triangle described by P(x1,y1), Q(x2,y2) and O(0,0) is right-angled. + Note: this doesn't check if P and Q are equal, but that's handled by the use of + itertools.combinations in the solution function. + + >>> is_right(0, 1, 2, 0) + True + >>> is_right(1, 0, 2, 2) + False + """ + if x1 == y1 == 0 or x2 == y2 == 0: + return False + a_square = x1 * x1 + y1 * y1 + b_square = x2 * x2 + y2 * y2 + c_square = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + return ( + a_square + b_square == c_square + or a_square + c_square == b_square + or b_square + c_square == a_square + ) + + +def solution(limit: int = 50) -> int: + """ + Return the number of right triangles OPQ that can be formed by two points P, Q + which have both x- and y- coordinates between 0 and limit inclusive. + + >>> solution(2) + 14 + >>> solution(10) + 448 + """ + return sum( + 1 + for pt1, pt2 in combinations(product(range(limit + 1), repeat=2), 2) + if is_right(*pt1, *pt2) + ) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_097/__init__.py b/project_euler/problem_097/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_097/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_097/sol1.py b/project_euler/problem_097/sol1.py new file mode 100644 index 000000000000..2e848c09a940 --- /dev/null +++ b/project_euler/problem_097/sol1.py @@ -0,0 +1,46 @@ +""" +The first known prime found to exceed one million digits was discovered in 1999, +and is a Mersenne prime of the form 2**6972593 − 1; it contains exactly 2,098,960 +digits. Subsequently other Mersenne primes, of the form 2**p − 1, have been found +which contain more digits. +However, in 2004 there was found a massive non-Mersenne prime which contains +2,357,207 digits: (28433 * (2 ** 7830457 + 1)). + +Find the last ten digits of this prime number. +""" + + +def solution(n: int = 10) -> str: + """ + Returns the last n digits of NUMBER. + >>> solution() + '8739992577' + >>> solution(8) + '39992577' + >>> solution(1) + '7' + >>> solution(-1) + Traceback (most recent call last): + ... + ValueError: Invalid input + >>> solution(8.3) + Traceback (most recent call last): + ... + ValueError: Invalid input + >>> solution("a") + Traceback (most recent call last): + ... + ValueError: Invalid input + """ + if not isinstance(n, int) or n < 0: + raise ValueError("Invalid input") + MODULUS = 10 ** n + NUMBER = 28433 * (pow(2, 7830457, MODULUS)) + 1 + return str(NUMBER % MODULUS) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + print(f"{solution(10) = }") diff --git a/project_euler/problem_099/__init__.py b/project_euler/problem_099/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_099/base_exp.txt b/project_euler/problem_099/base_exp.txt new file mode 100644 index 000000000000..e4b2d01522ca --- /dev/null +++ b/project_euler/problem_099/base_exp.txt @@ -0,0 +1,1000 @@ +519432,525806 +632382,518061 +78864,613712 +466580,530130 +780495,510032 +525895,525320 +15991,714883 +960290,502358 +760018,511029 +166800,575487 +210884,564478 +555151,523163 +681146,515199 +563395,522587 +738250,512126 +923525,503780 +595148,520429 +177108,572629 +750923,511482 +440902,532446 +881418,505504 +422489,534197 +979858,501616 +685893,514935 +747477,511661 +167214,575367 +234140,559696 +940238,503122 +728969,512609 +232083,560102 +900971,504694 +688801,514772 +189664,569402 +891022,505104 +445689,531996 +119570,591871 +821453,508118 +371084,539600 +911745,504251 +623655,518600 +144361,582486 +352442,541775 +420726,534367 +295298,549387 +6530,787777 +468397,529976 +672336,515696 +431861,533289 +84228,610150 +805376,508857 +444409,532117 +33833,663511 +381850,538396 +402931,536157 +92901,604930 +304825,548004 +731917,512452 +753734,511344 +51894,637373 +151578,580103 +295075,549421 +303590,548183 +333594,544123 +683952,515042 +60090,628880 +951420,502692 +28335,674991 +714940,513349 +343858,542826 +549279,523586 +804571,508887 +260653,554881 +291399,549966 +402342,536213 +408889,535550 +40328,652524 +375856,539061 +768907,510590 +165993,575715 +976327,501755 +898500,504795 +360404,540830 +478714,529095 +694144,514472 +488726,528258 +841380,507226 +328012,544839 +22389,690868 +604053,519852 +329514,544641 +772965,510390 +492798,527927 +30125,670983 +895603,504906 +450785,531539 +840237,507276 +380711,538522 +63577,625673 +76801,615157 +502694,527123 +597706,520257 +310484,547206 +944468,502959 +121283,591152 +451131,531507 +566499,522367 +425373,533918 +40240,652665 +39130,654392 +714926,513355 +469219,529903 +806929,508783 +287970,550487 +92189,605332 +103841,599094 +671839,515725 +452048,531421 +987837,501323 +935192,503321 +88585,607450 +613883,519216 +144551,582413 +647359,517155 +213902,563816 +184120,570789 +258126,555322 +502546,527130 +407655,535678 +401528,536306 +477490,529193 +841085,507237 +732831,512408 +833000,507595 +904694,504542 +581435,521348 +455545,531110 +873558,505829 +94916,603796 +720176,513068 +545034,523891 +246348,557409 +556452,523079 +832015,507634 +173663,573564 +502634,527125 +250732,556611 +569786,522139 +216919,563178 +521815,525623 +92304,605270 +164446,576167 +753413,511364 +11410,740712 +448845,531712 +925072,503725 +564888,522477 +7062,780812 +641155,517535 +738878,512100 +636204,517828 +372540,539436 +443162,532237 +571192,522042 +655350,516680 +299741,548735 +581914,521307 +965471,502156 +513441,526277 +808682,508700 +237589,559034 +543300,524025 +804712,508889 +247511,557192 +543486,524008 +504383,526992 +326529,545039 +792493,509458 +86033,609017 +126554,589005 +579379,521481 +948026,502823 +404777,535969 +265767,554022 +266876,553840 +46631,643714 +492397,527958 +856106,506581 +795757,509305 +748946,511584 +294694,549480 +409781,535463 +775887,510253 +543747,523991 +210592,564536 +517119,525990 +520253,525751 +247926,557124 +592141,520626 +346580,542492 +544969,523902 +506501,526817 +244520,557738 +144745,582349 +69274,620858 +292620,549784 +926027,503687 +736320,512225 +515528,526113 +407549,535688 +848089,506927 +24141,685711 +9224,757964 +980684,501586 +175259,573121 +489160,528216 +878970,505604 +969546,502002 +525207,525365 +690461,514675 +156510,578551 +659778,516426 +468739,529945 +765252,510770 +76703,615230 +165151,575959 +29779,671736 +928865,503569 +577538,521605 +927555,503618 +185377,570477 +974756,501809 +800130,509093 +217016,563153 +365709,540216 +774508,510320 +588716,520851 +631673,518104 +954076,502590 +777828,510161 +990659,501222 +597799,520254 +786905,509727 +512547,526348 +756449,511212 +869787,505988 +653747,516779 +84623,609900 +839698,507295 +30159,670909 +797275,509234 +678136,515373 +897144,504851 +989554,501263 +413292,535106 +55297,633667 +788650,509637 +486748,528417 +150724,580377 +56434,632490 +77207,614869 +588631,520859 +611619,519367 +100006,601055 +528924,525093 +190225,569257 +851155,506789 +682593,515114 +613043,519275 +514673,526183 +877634,505655 +878905,505602 +1926,914951 +613245,519259 +152481,579816 +841774,507203 +71060,619442 +865335,506175 +90244,606469 +302156,548388 +399059,536557 +478465,529113 +558601,522925 +69132,620966 +267663,553700 +988276,501310 +378354,538787 +529909,525014 +161733,576968 +758541,511109 +823425,508024 +149821,580667 +269258,553438 +481152,528891 +120871,591322 +972322,501901 +981350,501567 +676129,515483 +950860,502717 +119000,592114 +392252,537272 +191618,568919 +946699,502874 +289555,550247 +799322,509139 +703886,513942 +194812,568143 +261823,554685 +203052,566221 +217330,563093 +734748,512313 +391759,537328 +807052,508777 +564467,522510 +59186,629748 +113447,594545 +518063,525916 +905944,504492 +613922,519213 +439093,532607 +445946,531981 +230530,560399 +297887,549007 +459029,530797 +403692,536075 +855118,506616 +963127,502245 +841711,507208 +407411,535699 +924729,503735 +914823,504132 +333725,544101 +176345,572832 +912507,504225 +411273,535308 +259774,555036 +632853,518038 +119723,591801 +163902,576321 +22691,689944 +402427,536212 +175769,572988 +837260,507402 +603432,519893 +313679,546767 +538165,524394 +549026,523608 +61083,627945 +898345,504798 +992556,501153 +369999,539727 +32847,665404 +891292,505088 +152715,579732 +824104,507997 +234057,559711 +730507,512532 +960529,502340 +388395,537687 +958170,502437 +57105,631806 +186025,570311 +993043,501133 +576770,521664 +215319,563513 +927342,503628 +521353,525666 +39563,653705 +752516,511408 +110755,595770 +309749,547305 +374379,539224 +919184,503952 +990652,501226 +647780,517135 +187177,570017 +168938,574877 +649558,517023 +278126,552016 +162039,576868 +658512,516499 +498115,527486 +896583,504868 +561170,522740 +747772,511647 +775093,510294 +652081,516882 +724905,512824 +499707,527365 +47388,642755 +646668,517204 +571700,522007 +180430,571747 +710015,513617 +435522,532941 +98137,602041 +759176,511070 +486124,528467 +526942,525236 +878921,505604 +408313,535602 +926980,503640 +882353,505459 +566887,522345 +3326,853312 +911981,504248 +416309,534800 +392991,537199 +622829,518651 +148647,581055 +496483,527624 +666314,516044 +48562,641293 +672618,515684 +443676,532187 +274065,552661 +265386,554079 +347668,542358 +31816,667448 +181575,571446 +961289,502320 +365689,540214 +987950,501317 +932299,503440 +27388,677243 +746701,511701 +492258,527969 +147823,581323 +57918,630985 +838849,507333 +678038,515375 +27852,676130 +850241,506828 +818403,508253 +131717,587014 +850216,506834 +904848,504529 +189758,569380 +392845,537217 +470876,529761 +925353,503711 +285431,550877 +454098,531234 +823910,508003 +318493,546112 +766067,510730 +261277,554775 +421530,534289 +694130,514478 +120439,591498 +213308,563949 +854063,506662 +365255,540263 +165437,575872 +662240,516281 +289970,550181 +847977,506933 +546083,523816 +413252,535113 +975829,501767 +361540,540701 +235522,559435 +224643,561577 +736350,512229 +328303,544808 +35022,661330 +307838,547578 +474366,529458 +873755,505819 +73978,617220 +827387,507845 +670830,515791 +326511,545034 +309909,547285 +400970,536363 +884827,505352 +718307,513175 +28462,674699 +599384,520150 +253565,556111 +284009,551093 +343403,542876 +446557,531921 +992372,501160 +961601,502308 +696629,514342 +919537,503945 +894709,504944 +892201,505051 +358160,541097 +448503,531745 +832156,507636 +920045,503924 +926137,503675 +416754,534757 +254422,555966 +92498,605151 +826833,507873 +660716,516371 +689335,514746 +160045,577467 +814642,508425 +969939,501993 +242856,558047 +76302,615517 +472083,529653 +587101,520964 +99066,601543 +498005,527503 +709800,513624 +708000,513716 +20171,698134 +285020,550936 +266564,553891 +981563,501557 +846502,506991 +334,1190800 +209268,564829 +9844,752610 +996519,501007 +410059,535426 +432931,533188 +848012,506929 +966803,502110 +983434,501486 +160700,577267 +504374,526989 +832061,507640 +392825,537214 +443842,532165 +440352,532492 +745125,511776 +13718,726392 +661753,516312 +70500,619875 +436952,532814 +424724,533973 +21954,692224 +262490,554567 +716622,513264 +907584,504425 +60086,628882 +837123,507412 +971345,501940 +947162,502855 +139920,584021 +68330,621624 +666452,516038 +731446,512481 +953350,502619 +183157,571042 +845400,507045 +651548,516910 +20399,697344 +861779,506331 +629771,518229 +801706,509026 +189207,569512 +737501,512168 +719272,513115 +479285,529045 +136046,585401 +896746,504860 +891735,505067 +684771,514999 +865309,506184 +379066,538702 +503117,527090 +621780,518717 +209518,564775 +677135,515423 +987500,501340 +197049,567613 +329315,544673 +236756,559196 +357092,541226 +520440,525733 +213471,563911 +956852,502490 +702223,514032 +404943,535955 +178880,572152 +689477,514734 +691351,514630 +866669,506128 +370561,539656 +739805,512051 +71060,619441 +624861,518534 +261660,554714 +366137,540160 +166054,575698 +601878,519990 +153445,579501 +279899,551729 +379166,538691 +423209,534125 +675310,515526 +145641,582050 +691353,514627 +917468,504026 +284778,550976 +81040,612235 +161699,576978 +616394,519057 +767490,510661 +156896,578431 +427408,533714 +254849,555884 +737217,512182 +897133,504851 +203815,566051 +270822,553189 +135854,585475 +778805,510111 +784373,509847 +305426,547921 +733418,512375 +732087,512448 +540668,524215 +702898,513996 +628057,518328 +640280,517587 +422405,534204 +10604,746569 +746038,511733 +839808,507293 +457417,530938 +479030,529064 +341758,543090 +620223,518824 +251661,556451 +561790,522696 +497733,527521 +724201,512863 +489217,528217 +415623,534867 +624610,518548 +847541,506953 +432295,533249 +400391,536421 +961158,502319 +139173,584284 +421225,534315 +579083,521501 +74274,617000 +701142,514087 +374465,539219 +217814,562985 +358972,540995 +88629,607424 +288597,550389 +285819,550812 +538400,524385 +809930,508645 +738326,512126 +955461,502535 +163829,576343 +826475,507891 +376488,538987 +102234,599905 +114650,594002 +52815,636341 +434037,533082 +804744,508880 +98385,601905 +856620,506559 +220057,562517 +844734,507078 +150677,580387 +558697,522917 +621751,518719 +207067,565321 +135297,585677 +932968,503404 +604456,519822 +579728,521462 +244138,557813 +706487,513800 +711627,513523 +853833,506674 +497220,527562 +59428,629511 +564845,522486 +623621,518603 +242689,558077 +125091,589591 +363819,540432 +686453,514901 +656813,516594 +489901,528155 +386380,537905 +542819,524052 +243987,557841 +693412,514514 +488484,528271 +896331,504881 +336730,543721 +728298,512647 +604215,519840 +153729,579413 +595687,520398 +540360,524240 +245779,557511 +924873,503730 +509628,526577 +528523,525122 +3509,847707 +522756,525555 +895447,504922 +44840,646067 +45860,644715 +463487,530404 +398164,536654 +894483,504959 +619415,518874 +966306,502129 +990922,501212 +835756,507474 +548881,523618 +453578,531282 +474993,529410 +80085,612879 +737091,512193 +50789,638638 +979768,501620 +792018,509483 +665001,516122 +86552,608694 +462772,530469 +589233,520821 +891694,505072 +592605,520594 +209645,564741 +42531,649269 +554376,523226 +803814,508929 +334157,544042 +175836,572970 +868379,506051 +658166,516520 +278203,551995 +966198,502126 +627162,518387 +296774,549165 +311803,547027 +843797,507118 +702304,514032 +563875,522553 +33103,664910 +191932,568841 +543514,524006 +506835,526794 +868368,506052 +847025,506971 +678623,515342 +876139,505726 +571997,521984 +598632,520198 +213590,563892 +625404,518497 +726508,512738 +689426,514738 +332495,544264 +411366,535302 +242546,558110 +315209,546555 +797544,509219 +93889,604371 +858879,506454 +124906,589666 +449072,531693 +235960,559345 +642403,517454 +720567,513047 +705534,513858 +603692,519870 +488137,528302 +157370,578285 +63515,625730 +666326,516041 +619226,518883 +443613,532186 +597717,520257 +96225,603069 +86940,608450 +40725,651929 +460976,530625 +268875,553508 +270671,553214 +363254,540500 +384248,538137 +762889,510892 +377941,538833 +278878,551890 +176615,572755 +860008,506412 +944392,502967 +608395,519571 +225283,561450 +45095,645728 +333798,544090 +625733,518476 +995584,501037 +506135,526853 +238050,558952 +557943,522972 +530978,524938 +634244,517949 +177168,572616 +85200,609541 +953043,502630 +523661,525484 +999295,500902 +840803,507246 +961490,502312 +471747,529685 +380705,538523 +911180,504275 +334149,544046 +478992,529065 +325789,545133 +335884,543826 +426976,533760 +749007,511582 +667067,516000 +607586,519623 +674054,515599 +188534,569675 +565185,522464 +172090,573988 +87592,608052 +907432,504424 +8912,760841 +928318,503590 +757917,511138 +718693,513153 +315141,546566 +728326,512645 +353492,541647 +638429,517695 +628892,518280 +877286,505672 +620895,518778 +385878,537959 +423311,534113 +633501,517997 +884833,505360 +883402,505416 +999665,500894 +708395,513697 +548142,523667 +756491,511205 +987352,501340 +766520,510705 +591775,520647 +833758,507563 +843890,507108 +925551,503698 +74816,616598 +646942,517187 +354923,541481 +256291,555638 +634470,517942 +930904,503494 +134221,586071 +282663,551304 +986070,501394 +123636,590176 +123678,590164 +481717,528841 +423076,534137 +866246,506145 +93313,604697 +783632,509880 +317066,546304 +502977,527103 +141272,583545 +71708,618938 +617748,518975 +581190,521362 +193824,568382 +682368,515131 +352956,541712 +351375,541905 +505362,526909 +905165,504518 +128645,588188 +267143,553787 +158409,577965 +482776,528754 +628896,518282 +485233,528547 +563606,522574 +111001,595655 +115920,593445 +365510,540237 +959724,502374 +938763,503184 +930044,503520 +970959,501956 +913658,504176 +68117,621790 +989729,501253 +567697,522288 +820427,508163 +54236,634794 +291557,549938 +124961,589646 +403177,536130 +405421,535899 +410233,535417 +815111,508403 +213176,563974 +83099,610879 +998588,500934 +513640,526263 +129817,587733 +1820,921851 +287584,550539 +299160,548820 +860621,506386 +529258,525059 +586297,521017 +953406,502616 +441234,532410 +986217,501386 +781938,509957 +461247,530595 +735424,512277 +146623,581722 +839838,507288 +510667,526494 +935085,503327 +737523,512167 +303455,548204 +992779,501145 +60240,628739 +939095,503174 +794368,509370 +501825,527189 +459028,530798 +884641,505363 +512287,526364 +835165,507499 +307723,547590 +160587,577304 +735043,512300 +493289,527887 +110717,595785 +306480,547772 +318593,546089 +179810,571911 +200531,566799 +314999,546580 +197020,567622 +301465,548487 +237808,559000 +131944,586923 +882527,505449 +468117,530003 +711319,513541 +156240,578628 +965452,502162 +992756,501148 +437959,532715 +739938,512046 +614249,519196 +391496,537356 +62746,626418 +688215,514806 +75501,616091 +883573,505412 +558824,522910 +759371,511061 +173913,573489 +891351,505089 +727464,512693 +164833,576051 +812317,508529 +540320,524243 +698061,514257 +69149,620952 +471673,529694 +159092,577753 +428134,533653 +89997,606608 +711061,513557 +779403,510081 +203327,566155 +798176,509187 +667688,515963 +636120,517833 +137410,584913 +217615,563034 +556887,523038 +667229,515991 +672276,515708 +325361,545187 +172115,573985 +13846,725685 diff --git a/project_euler/problem_099/sol1.py b/project_euler/problem_099/sol1.py new file mode 100644 index 000000000000..88912e1f0f9e --- /dev/null +++ b/project_euler/problem_099/sol1.py @@ -0,0 +1,34 @@ +""" +Problem: + +Comparing two numbers written in index form like 2'11 and 3'7 is not difficult, as any +calculator would confirm that 2^11 = 2048 < 3^7 = 2187. + +However, confirming that 632382^518061 > 519432^525806 would be much more difficult, as +both numbers contain over three million digits. + +Using base_exp.txt, a 22K text file containing one thousand lines with a base/exponent +pair on each line, determine which line number has the greatest numerical value. + +NOTE: The first two lines in the file represent the numbers in the example given above. +""" + +import os +from math import log10 + + +def solution(data_file: str = "base_exp.txt") -> int: + """ + >>> solution() + 709 + """ + largest = [0, 0] + for i, line in enumerate(open(os.path.join(os.path.dirname(__file__), data_file))): + a, x = list(map(int, line.split(","))) + if x * log10(a) > largest[0]: + largest = [x * log10(a), i + 1] + return largest[1] + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_10/sol1.py b/project_euler/problem_10/sol1.py deleted file mode 100644 index 94e5b7362114..000000000000 --- a/project_euler/problem_10/sol1.py +++ /dev/null @@ -1,38 +0,0 @@ -from __future__ import print_function -from math import sqrt - -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -def is_prime(n): - for i in xrange(2, int(sqrt(n))+1): - if n%i == 0: - return False - - return True - -def sum_of_primes(n): - if n > 2: - sumOfPrimes = 2 - else: - return 0 - - for i in xrange(3, n, 2): - if is_prime(i): - sumOfPrimes += i - - return sumOfPrimes - -if __name__ == '__main__': - import sys - - if len(sys.argv) == 1: - print(sum_of_primes(2000000)) - else: - try: - n = int(sys.argv[1]) - print(sum_of_primes(n)) - except ValueError: - print('Invalid entry - please enter a number.') diff --git a/project_euler/problem_10/sol2.py b/project_euler/problem_10/sol2.py deleted file mode 100644 index 22df95c063e2..000000000000 --- a/project_euler/problem_10/sol2.py +++ /dev/null @@ -1,22 +0,0 @@ -#from Python.Math import prime_generator -import math -from itertools import takewhile - -def primeCheck(number): - if number % 2 == 0 and number > 2: - return False - return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2)) - -def prime_generator(): - num = 2 - while True: - if primeCheck(num): - yield num - num+=1 - -def main(): - n = int(input('Enter The upper limit of prime numbers: ')) - print(sum(takewhile(lambda x: x < n,prime_generator()))) - -if __name__ == '__main__': - main() diff --git a/project_euler/problem_101/__init__.py b/project_euler/problem_101/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_101/sol1.py b/project_euler/problem_101/sol1.py new file mode 100644 index 000000000000..e66316090fb2 --- /dev/null +++ b/project_euler/problem_101/sol1.py @@ -0,0 +1,219 @@ +""" +If we are presented with the first k terms of a sequence it is impossible to say with +certainty the value of the next term, as there are infinitely many polynomial functions +that can model the sequence. + +As an example, let us consider the sequence of cube +numbers. This is defined by the generating function, +u(n) = n3: 1, 8, 27, 64, 125, 216, ... + +Suppose we were only given the first two terms of this sequence. Working on the +principle that "simple is best" we should assume a linear relationship and predict the +next term to be 15 (common difference 7). Even if we were presented with the first three +terms, by the same principle of simplicity, a quadratic relationship should be +assumed. + +We shall define OP(k, n) to be the nth term of the optimum polynomial +generating function for the first k terms of a sequence. It should be clear that +OP(k, n) will accurately generate the terms of the sequence for n ≤ k, and potentially +the first incorrect term (FIT) will be OP(k, k+1); in which case we shall call it a +bad OP (BOP). + +As a basis, if we were only given the first term of sequence, it would be most +sensible to assume constancy; that is, for n ≥ 2, OP(1, n) = u(1). + +Hence we obtain the +following OPs for the cubic sequence: + +OP(1, n) = 1 1, 1, 1, 1, ... +OP(2, n) = 7n-6 1, 8, 15, ... +OP(3, n) = 6n^2-11n+6 1, 8, 27, 58, ... +OP(4, n) = n^3 1, 8, 27, 64, 125, ... + +Clearly no BOPs exist for k ≥ 4. + +By considering the sum of FITs generated by the BOPs (indicated in red above), we +obtain 1 + 15 + 58 = 74. + +Consider the following tenth degree polynomial generating function: + +1 - n + n^2 - n^3 + n^4 - n^5 + n^6 - n^7 + n^8 - n^9 + n^10 + +Find the sum of FITs for the BOPs. +""" + + +from typing import Callable, List, Union + +Matrix = List[List[Union[float, int]]] + + +def solve(matrix: Matrix, vector: Matrix) -> Matrix: + """ + Solve the linear system of equations Ax = b (A = "matrix", b = "vector") + for x using Gaussian elimination and back substitution. We assume that A + is an invertible square matrix and that b is a column vector of the + same height. + >>> solve([[1, 0], [0, 1]], [[1],[2]]) + [[1.0], [2.0]] + >>> solve([[2, 1, -1],[-3, -1, 2],[-2, 1, 2]],[[8], [-11],[-3]]) + [[2.0], [3.0], [-1.0]] + """ + size: int = len(matrix) + augmented: Matrix = [[0 for _ in range(size + 1)] for _ in range(size)] + row: int + row2: int + col: int + col2: int + pivot_row: int + ratio: float + + for row in range(size): + for col in range(size): + augmented[row][col] = matrix[row][col] + + augmented[row][size] = vector[row][0] + + row = 0 + col = 0 + while row < size and col < size: + # pivoting + pivot_row = max( + [(abs(augmented[row2][col]), row2) for row2 in range(col, size)] + )[1] + if augmented[pivot_row][col] == 0: + col += 1 + continue + else: + augmented[row], augmented[pivot_row] = augmented[pivot_row], augmented[row] + + for row2 in range(row + 1, size): + ratio = augmented[row2][col] / augmented[row][col] + augmented[row2][col] = 0 + for col2 in range(col + 1, size + 1): + augmented[row2][col2] -= augmented[row][col2] * ratio + + row += 1 + col += 1 + + # back substitution + for col in range(1, size): + for row in range(col): + ratio = augmented[row][col] / augmented[col][col] + for col2 in range(col, size + 1): + augmented[row][col2] -= augmented[col][col2] * ratio + + # round to get rid of numbers like 2.000000000000004 + return [ + [round(augmented[row][size] / augmented[row][row], 10)] for row in range(size) + ] + + +def interpolate(y_list: List[int]) -> Callable[[int], int]: + """ + Given a list of data points (1,y0),(2,y1), ..., return a function that + interpolates the data points. We find the coefficients of the interpolating + polynomial by solving a system of linear equations corresponding to + x = 1, 2, 3... + + >>> interpolate([1])(3) + 1 + >>> interpolate([1, 8])(3) + 15 + >>> interpolate([1, 8, 27])(4) + 58 + >>> interpolate([1, 8, 27, 64])(6) + 216 + """ + + size: int = len(y_list) + matrix: Matrix = [[0 for _ in range(size)] for _ in range(size)] + vector: Matrix = [[0] for _ in range(size)] + coeffs: Matrix + x_val: int + y_val: int + col: int + + for x_val, y_val in enumerate(y_list): + for col in range(size): + matrix[x_val][col] = (x_val + 1) ** (size - col - 1) + vector[x_val][0] = y_val + + coeffs = solve(matrix, vector) + + def interpolated_func(var: int) -> int: + """ + >>> interpolate([1])(3) + 1 + >>> interpolate([1, 8])(3) + 15 + >>> interpolate([1, 8, 27])(4) + 58 + >>> interpolate([1, 8, 27, 64])(6) + 216 + """ + return sum( + round(coeffs[x_val][0]) * (var ** (size - x_val - 1)) + for x_val in range(size) + ) + + return interpolated_func + + +def question_function(variable: int) -> int: + """ + The generating function u as specified in the question. + >>> question_function(0) + 1 + >>> question_function(1) + 1 + >>> question_function(5) + 8138021 + >>> question_function(10) + 9090909091 + """ + return ( + 1 + - variable + + variable ** 2 + - variable ** 3 + + variable ** 4 + - variable ** 5 + + variable ** 6 + - variable ** 7 + + variable ** 8 + - variable ** 9 + + variable ** 10 + ) + + +def solution(func: Callable[[int], int] = question_function, order: int = 10) -> int: + """ + Find the sum of the FITs of the BOPS. For each interpolating polynomial of order + 1, 2, ... , 10, find the first x such that the value of the polynomial at x does + not equal u(x). + >>> solution(lambda n: n ** 3, 3) + 74 + """ + data_points: List[int] = [func(x_val) for x_val in range(1, order + 1)] + + polynomials: List[Callable[[int], int]] = [ + interpolate(data_points[:max_coeff]) for max_coeff in range(1, order + 1) + ] + + ret: int = 0 + poly: int + x_val: int + + for poly in polynomials: + x_val = 1 + while func(x_val) == poly(x_val): + x_val += 1 + + ret += poly(x_val) + + return ret + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_102/__init__.py b/project_euler/problem_102/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_102/p102_triangles.txt b/project_euler/problem_102/p102_triangles.txt new file mode 100644 index 000000000000..3f01a1ac1f41 --- /dev/null +++ b/project_euler/problem_102/p102_triangles.txt @@ -0,0 +1,1000 @@ +-340,495,-153,-910,835,-947 +-175,41,-421,-714,574,-645 +-547,712,-352,579,951,-786 +419,-864,-83,650,-399,171 +-429,-89,-357,-930,296,-29 +-734,-702,823,-745,-684,-62 +-971,762,925,-776,-663,-157 +162,570,628,485,-807,-896 +641,91,-65,700,887,759 +215,-496,46,-931,422,-30 +-119,359,668,-609,-358,-494 +440,929,968,214,760,-857 +-700,785,838,29,-216,411 +-770,-458,-325,-53,-505,633 +-752,-805,349,776,-799,687 +323,5,561,-36,919,-560 +-907,358,264,320,204,274 +-728,-466,350,969,292,-345 +940,836,272,-533,748,185 +411,998,813,520,316,-949 +-152,326,658,-762,148,-651 +330,507,-9,-628,101,174 +551,-496,772,-541,-702,-45 +-164,-489,-90,322,631,-59 +673,366,-4,-143,-606,-704 +428,-609,801,-449,740,-269 +453,-924,-785,-346,-853,111 +-738,555,-181,467,-426,-20 +958,-692,784,-343,505,-569 +620,27,263,54,-439,-726 +804,87,998,859,871,-78 +-119,-453,-709,-292,-115,-56 +-626,138,-940,-476,-177,-274 +-11,160,142,588,446,158 +538,727,550,787,330,810 +420,-689,854,-546,337,516 +872,-998,-607,748,473,-192 +653,440,-516,-985,808,-857 +374,-158,331,-940,-338,-641 +137,-925,-179,771,734,-715 +-314,198,-115,29,-641,-39 +759,-574,-385,355,590,-603 +-189,-63,-168,204,289,305 +-182,-524,-715,-621,911,-255 +331,-816,-833,471,168,126 +-514,581,-855,-220,-731,-507 +129,169,576,651,-87,-458 +783,-444,-881,658,-266,298 +603,-430,-598,585,368,899 +43,-724,962,-376,851,409 +-610,-646,-883,-261,-482,-881 +-117,-237,978,641,101,-747 +579,125,-715,-712,208,534 +672,-214,-762,372,874,533 +-564,965,38,715,367,242 +500,951,-700,-981,-61,-178 +-382,-224,-959,903,-282,-60 +-355,295,426,-331,-591,655 +892,128,958,-271,-993,274 +-454,-619,302,138,-790,-874 +-642,601,-574,159,-290,-318 +266,-109,257,-686,54,975 +162,628,-478,840,264,-266 +466,-280,982,1,904,-810 +721,839,730,-807,777,981 +-129,-430,748,263,943,96 +434,-94,410,-990,249,-704 +237,42,122,-732,44,-51 +909,-116,-229,545,292,717 +824,-768,-807,-370,-262,30 +675,58,332,-890,-651,791 +363,825,-717,254,684,240 +405,-715,900,166,-589,422 +-476,686,-830,-319,634,-807 +633,837,-971,917,-764,207 +-116,-44,-193,-70,908,809 +-26,-252,998,408,70,-713 +-601,645,-462,842,-644,-591 +-160,653,274,113,-138,687 +369,-273,-181,925,-167,-693 +-338,135,480,-967,-13,-840 +-90,-270,-564,695,161,907 +607,-430,869,-713,461,-469 +919,-165,-776,522,606,-708 +-203,465,288,207,-339,-458 +-453,-534,-715,975,838,-677 +-973,310,-350,934,546,-805 +-835,385,708,-337,-594,-772 +-14,914,900,-495,-627,594 +833,-713,-213,578,-296,699 +-27,-748,484,455,915,291 +270,889,739,-57,442,-516 +119,811,-679,905,184,130 +-678,-469,925,553,612,482 +101,-571,-732,-842,644,588 +-71,-737,566,616,957,-663 +-634,-356,90,-207,936,622 +598,443,964,-895,-58,529 +847,-467,929,-742,91,10 +-633,829,-780,-408,222,-30 +-818,57,275,-38,-746,198 +-722,-825,-549,597,-391,99 +-570,908,430,873,-103,-360 +342,-681,512,434,542,-528 +297,850,479,609,543,-357 +9,784,212,548,56,859 +-152,560,-240,-969,-18,713 +140,-133,34,-635,250,-163 +-272,-22,-169,-662,989,-604 +471,-765,355,633,-742,-118 +-118,146,942,663,547,-376 +583,16,162,264,715,-33 +-230,-446,997,-838,561,555 +372,397,-729,-318,-276,649 +92,982,-970,-390,-922,922 +-981,713,-951,-337,-669,670 +-999,846,-831,-504,7,-128 +455,-954,-370,682,-510,45 +822,-960,-892,-385,-662,314 +-668,-686,-367,-246,530,-341 +-723,-720,-926,-836,-142,757 +-509,-134,384,-221,-873,-639 +-803,-52,-706,-669,373,-339 +933,578,631,-616,770,555 +741,-564,-33,-605,-576,275 +-715,445,-233,-730,734,-704 +120,-10,-266,-685,-490,-17 +-232,-326,-457,-946,-457,-116 +811,52,639,826,-200,147 +-329,279,293,612,943,955 +-721,-894,-393,-969,-642,453 +-688,-826,-352,-75,371,79 +-809,-979,407,497,858,-248 +-485,-232,-242,-582,-81,849 +141,-106,123,-152,806,-596 +-428,57,-992,811,-192,478 +864,393,122,858,255,-876 +-284,-780,240,457,354,-107 +956,605,-477,44,26,-678 +86,710,-533,-815,439,327 +-906,-626,-834,763,426,-48 +201,-150,-904,652,475,412 +-247,149,81,-199,-531,-148 +923,-76,-353,175,-121,-223 +427,-674,453,472,-410,585 +931,776,-33,85,-962,-865 +-655,-908,-902,208,869,792 +-316,-102,-45,-436,-222,885 +-309,768,-574,653,745,-975 +896,27,-226,993,332,198 +323,655,-89,260,240,-902 +501,-763,-424,793,813,616 +993,375,-938,-621,672,-70 +-880,-466,-283,770,-824,143 +63,-283,886,-142,879,-116 +-964,-50,-521,-42,-306,-161 +724,-22,866,-871,933,-383 +-344,135,282,966,-80,917 +-281,-189,420,810,362,-582 +-515,455,-588,814,162,332 +555,-436,-123,-210,869,-943 +589,577,232,286,-554,876 +-773,127,-58,-171,-452,125 +-428,575,906,-232,-10,-224 +437,276,-335,-348,605,878 +-964,511,-386,-407,168,-220 +307,513,912,-463,-423,-416 +-445,539,273,886,-18,760 +-396,-585,-670,414,47,364 +143,-506,754,906,-971,-203 +-544,472,-180,-541,869,-465 +-779,-15,-396,890,972,-220 +-430,-564,503,182,-119,456 +89,-10,-739,399,506,499 +954,162,-810,-973,127,870 +890,952,-225,158,828,237 +-868,952,349,465,574,750 +-915,369,-975,-596,-395,-134 +-135,-601,575,582,-667,640 +413,890,-560,-276,-555,-562 +-633,-269,561,-820,-624,499 +371,-92,-784,-593,864,-717 +-971,655,-439,367,754,-951 +172,-347,36,279,-247,-402 +633,-301,364,-349,-683,-387 +-780,-211,-713,-948,-648,543 +72,58,762,-465,-66,462 +78,502,781,-832,713,836 +-431,-64,-484,-392,208,-343 +-64,101,-29,-860,-329,844 +398,391,828,-858,700,395 +578,-896,-326,-604,314,180 +97,-321,-695,185,-357,852 +854,839,283,-375,951,-209 +194,96,-564,-847,162,524 +-354,532,494,621,580,560 +419,-678,-450,926,-5,-924 +-661,905,519,621,-143,394 +-573,268,296,-562,-291,-319 +-211,266,-196,158,564,-183 +18,-585,-398,777,-581,864 +790,-894,-745,-604,-418,70 +848,-339,150,773,11,851 +-954,-809,-53,-20,-648,-304 +658,-336,-658,-905,853,407 +-365,-844,350,-625,852,-358 +986,-315,-230,-159,21,180 +-15,599,45,-286,-941,847 +-613,-68,184,639,-987,550 +334,675,-56,-861,923,340 +-848,-596,960,231,-28,-34 +707,-811,-994,-356,-167,-171 +-470,-764,72,576,-600,-204 +379,189,-542,-576,585,800 +440,540,-445,-563,379,-334 +-155,64,514,-288,853,106 +-304,751,481,-520,-708,-694 +-709,132,594,126,-844,63 +723,471,421,-138,-962,892 +-440,-263,39,513,-672,-954 +775,809,-581,330,752,-107 +-376,-158,335,-708,-514,578 +-343,-769,456,-187,25,413 +548,-877,-172,300,-500,928 +938,-102,423,-488,-378,-969 +-36,564,-55,131,958,-800 +-322,511,-413,503,700,-847 +-966,547,-88,-17,-359,-67 +637,-341,-437,-181,527,-153 +-74,449,-28,3,485,189 +-997,658,-224,-948,702,-807 +-224,736,-896,127,-945,-850 +-395,-106,439,-553,-128,124 +-841,-445,-758,-572,-489,212 +633,-327,13,-512,952,771 +-940,-171,-6,-46,-923,-425 +-142,-442,-817,-998,843,-695 +340,847,-137,-920,-988,-658 +-653,217,-679,-257,651,-719 +-294,365,-41,342,74,-892 +690,-236,-541,494,408,-516 +180,-807,225,790,494,59 +707,605,-246,656,284,271 +65,294,152,824,442,-442 +-321,781,-540,341,316,415 +420,371,-2,545,995,248 +56,-191,-604,971,615,449 +-981,-31,510,592,-390,-362 +-317,-968,913,365,97,508 +832,63,-864,-510,86,202 +-483,456,-636,340,-310,676 +981,-847,751,-508,-962,-31 +-157,99,73,797,63,-172 +220,858,872,924,866,-381 +996,-169,805,321,-164,971 +896,11,-625,-973,-782,76 +578,-280,730,-729,307,-905 +-580,-749,719,-698,967,603 +-821,874,-103,-623,662,-491 +-763,117,661,-644,672,-607 +592,787,-798,-169,-298,690 +296,644,-526,-762,-447,665 +534,-818,852,-120,57,-379 +-986,-549,-329,294,954,258 +-133,352,-660,-77,904,-356 +748,343,215,500,317,-277 +311,7,910,-896,-809,795 +763,-602,-753,313,-352,917 +668,619,-474,-597,-650,650 +-297,563,-701,-987,486,-902 +-461,-740,-657,233,-482,-328 +-446,-250,-986,-458,-629,520 +542,-49,-327,-469,257,-947 +121,-575,-634,-143,-184,521 +30,504,455,-645,-229,-945 +-12,-295,377,764,771,125 +-686,-133,225,-25,-376,-143 +-6,-46,338,270,-405,-872 +-623,-37,582,467,963,898 +-804,869,-477,420,-475,-303 +94,41,-842,-193,-768,720 +-656,-918,415,645,-357,460 +-47,-486,-911,468,-608,-686 +-158,251,419,-394,-655,-895 +272,-695,979,508,-358,959 +-776,650,-918,-467,-690,-534 +-85,-309,-626,167,-366,-429 +-880,-732,-186,-924,970,-875 +517,645,-274,962,-804,544 +721,402,104,640,478,-499 +198,684,-134,-723,-452,-905 +-245,745,239,238,-826,441 +-217,206,-32,462,-981,-895 +-51,989,526,-173,560,-676 +-480,-659,-976,-580,-727,466 +-996,-90,-995,158,-239,642 +302,288,-194,-294,17,924 +-943,969,-326,114,-500,103 +-619,163,339,-880,230,421 +-344,-601,-795,557,565,-779 +590,345,-129,-202,-125,-58 +-777,-195,159,674,775,411 +-939,312,-665,810,121,855 +-971,254,712,815,452,581 +442,-9,327,-750,61,757 +-342,869,869,-160,390,-772 +620,601,565,-169,-69,-183 +-25,924,-817,964,321,-970 +-64,-6,-133,978,825,-379 +601,436,-24,98,-115,940 +-97,502,614,-574,922,513 +-125,262,-946,695,99,-220 +429,-721,719,-694,197,-558 +326,689,-70,-908,-673,338 +-468,-856,-902,-254,-358,305 +-358,530,542,355,-253,-47 +-438,-74,-362,963,988,788 +137,717,467,622,319,-380 +-86,310,-336,851,918,-288 +721,395,646,-53,255,-425 +255,175,912,84,-209,878 +-632,-485,-400,-357,991,-608 +235,-559,992,-297,857,-591 +87,-71,148,130,647,578 +-290,-584,-639,-788,-21,592 +386,984,625,-731,-993,-336 +-538,634,-209,-828,-150,-774 +-754,-387,607,-781,976,-199 +412,-798,-664,295,709,-537 +-412,932,-880,-232,561,852 +-656,-358,-198,-964,-433,-848 +-762,-668,-632,186,-673,-11 +-876,237,-282,-312,-83,682 +403,73,-57,-436,-622,781 +-587,873,798,976,-39,329 +-369,-622,553,-341,817,794 +-108,-616,920,-849,-679,96 +290,-974,234,239,-284,-321 +-22,394,-417,-419,264,58 +-473,-551,69,923,591,-228 +-956,662,-113,851,-581,-794 +-258,-681,413,-471,-637,-817 +-866,926,992,-653,-7,794 +556,-350,602,917,831,-610 +188,245,-906,361,492,174 +-720,384,-818,329,638,-666 +-246,846,890,-325,-59,-850 +-118,-509,620,-762,-256,15 +-787,-536,-452,-338,-399,813 +458,560,525,-311,-608,-419 +494,-811,-825,-127,-812,894 +-801,890,-629,-860,574,925 +-709,-193,-213,138,-410,-403 +861,91,708,-187,5,-222 +789,646,777,154,90,-49 +-267,-830,-114,531,591,-698 +-126,-82,881,-418,82,652 +-894,130,-726,-935,393,-815 +-142,563,654,638,-712,-597 +-759,60,-23,977,100,-765 +-305,595,-570,-809,482,762 +-161,-267,53,963,998,-529 +-300,-57,798,353,703,486 +-990,696,-764,699,-565,719 +-232,-205,566,571,977,369 +740,865,151,-817,-204,-293 +94,445,-768,229,537,-406 +861,620,37,-424,-36,656 +390,-369,952,733,-464,569 +-482,-604,959,554,-705,-626 +-396,-615,-991,108,272,-723 +143,780,535,142,-917,-147 +138,-629,-217,-908,905,115 +915,103,-852,64,-468,-642 +570,734,-785,-268,-326,-759 +738,531,-332,586,-779,24 +870,440,-217,473,-383,415 +-296,-333,-330,-142,-924,950 +118,120,-35,-245,-211,-652 +61,634,153,-243,838,789 +726,-582,210,105,983,537 +-313,-323,758,234,29,848 +-847,-172,-593,733,-56,617 +54,255,-512,156,-575,675 +-873,-956,-148,623,95,200 +700,-370,926,649,-978,157 +-639,-202,719,130,747,222 +194,-33,955,943,505,114 +-226,-790,28,-930,827,783 +-392,-74,-28,714,218,-612 +209,626,-888,-683,-912,495 +487,751,614,933,631,445 +-348,-34,-411,-106,835,321 +-689,872,-29,-800,312,-542 +-52,566,827,570,-862,-77 +471,992,309,-402,389,912 +24,520,-83,-51,555,503 +-265,-317,283,-970,-472,690 +606,526,137,71,-651,150 +217,-518,663,66,-605,-331 +-562,232,-76,-503,205,-323 +842,-521,546,285,625,-186 +997,-927,344,909,-546,974 +-677,419,81,121,-705,771 +719,-379,-944,-797,784,-155 +-378,286,-317,-797,-111,964 +-288,-573,784,80,-532,-646 +-77,407,-248,-797,769,-816 +-24,-637,287,-858,-927,-333 +-902,37,894,-823,141,684 +125,467,-177,-516,686,399 +-321,-542,641,-590,527,-224 +-400,-712,-876,-208,632,-543 +-676,-429,664,-242,-269,922 +-608,-273,-141,930,687,380 +786,-12,498,494,310,326 +-739,-617,606,-960,804,188 +384,-368,-243,-350,-459,31 +-550,397,320,-868,328,-279 +969,-179,853,864,-110,514 +910,793,302,-822,-285,488 +-605,-128,218,-283,-17,-227 +16,324,667,708,750,3 +485,-813,19,585,71,930 +-218,816,-687,-97,-732,-360 +-497,-151,376,-23,3,315 +-412,-989,-610,-813,372,964 +-878,-280,87,381,-311,69 +-609,-90,-731,-679,150,585 +889,27,-162,605,75,-770 +448,617,-988,0,-103,-504 +-800,-537,-69,627,608,-668 +534,686,-664,942,830,920 +-238,775,495,932,-793,497 +-343,958,-914,-514,-691,651 +568,-136,208,359,728,28 +286,912,-794,683,556,-102 +-638,-629,-484,445,-64,-497 +58,505,-801,-110,872,632 +-390,777,353,267,976,369 +-993,515,105,-133,358,-572 +964,996,355,-212,-667,38 +-725,-614,-35,365,132,-196 +237,-536,-416,-302,312,477 +-664,574,-210,224,48,-925 +869,-261,-256,-240,-3,-698 +712,385,32,-34,916,-315 +895,-409,-100,-346,728,-624 +-806,327,-450,889,-781,-939 +-586,-403,698,318,-939,899 +557,-57,-920,659,333,-51 +-441,232,-918,-205,246,1 +783,167,-797,-595,245,-736 +-36,-531,-486,-426,-813,-160 +777,-843,817,313,-228,-572 +735,866,-309,-564,-81,190 +-413,645,101,719,-719,218 +-83,164,767,796,-430,-459 +122,779,-15,-295,-96,-892 +462,379,70,548,834,-312 +-630,-534,124,187,-737,114 +-299,-604,318,-591,936,826 +-879,218,-642,-483,-318,-866 +-691,62,-658,761,-895,-854 +-822,493,687,569,910,-202 +-223,784,304,-5,541,925 +-914,541,737,-662,-662,-195 +-622,615,414,358,881,-878 +339,745,-268,-968,-280,-227 +-364,855,148,-709,-827,472 +-890,-532,-41,664,-612,577 +-702,-859,971,-722,-660,-920 +-539,-605,737,149,973,-802 +800,42,-448,-811,152,511 +-933,377,-110,-105,-374,-937 +-766,152,482,120,-308,390 +-568,775,-292,899,732,890 +-177,-317,-502,-259,328,-511 +612,-696,-574,-660,132,31 +-119,563,-805,-864,179,-672 +425,-627,183,-331,839,318 +-711,-976,-749,152,-916,261 +181,-63,497,211,262,406 +-537,700,-859,-765,-928,77 +892,832,231,-749,-82,613 +816,216,-642,-216,-669,-912 +-6,624,-937,-370,-344,268 +737,-710,-869,983,-324,-274 +565,952,-547,-158,374,-444 +51,-683,645,-845,515,636 +-953,-631,114,-377,-764,-144 +-8,470,-242,-399,-675,-730 +-540,689,-20,47,-607,590 +-329,-710,-779,942,-388,979 +123,829,674,122,203,563 +46,782,396,-33,386,610 +872,-846,-523,-122,-55,-190 +388,-994,-525,974,127,596 +781,-680,796,-34,-959,-62 +-749,173,200,-384,-745,-446 +379,618,136,-250,-224,970 +-58,240,-921,-760,-901,-626 +366,-185,565,-100,515,688 +489,999,-893,-263,-637,816 +838,-496,-316,-513,419,479 +107,676,-15,882,98,-397 +-999,941,-903,-424,670,-325 +171,-979,835,178,169,-984 +-609,-607,378,-681,184,402 +-316,903,-575,-800,224,983 +591,-18,-460,551,-167,918 +-756,405,-117,441,163,-320 +456,24,6,881,-836,-539 +-489,-585,915,651,-892,-382 +-177,-122,73,-711,-386,591 +181,724,530,686,-131,241 +737,288,886,216,233,33 +-548,-386,-749,-153,-85,-982 +-835,227,904,160,-99,25 +-9,-42,-162,728,840,-963 +217,-763,870,771,47,-846 +-595,808,-491,556,337,-900 +-134,281,-724,441,-134,708 +-789,-508,651,-962,661,315 +-839,-923,339,402,41,-487 +300,-790,48,703,-398,-811 +955,-51,462,-685,960,-717 +910,-880,592,-255,-51,-776 +-885,169,-793,368,-565,458 +-905,940,-492,-630,-535,-988 +245,797,763,869,-82,550 +-310,38,-933,-367,-650,824 +-95,32,-83,337,226,990 +-218,-975,-191,-208,-785,-293 +-672,-953,517,-901,-247,465 +681,-148,261,-857,544,-923 +640,341,446,-618,195,769 +384,398,-846,365,671,815 +578,576,-911,907,762,-859 +548,-428,144,-630,-759,-146 +710,-73,-700,983,-97,-889 +-46,898,-973,-362,-817,-717 +151,-81,-125,-900,-478,-154 +483,615,-537,-932,181,-68 +786,-223,518,25,-306,-12 +-422,268,-809,-683,635,468 +983,-734,-694,-608,-110,4 +-786,-196,749,-354,137,-8 +-181,36,668,-200,691,-973 +-629,-838,692,-736,437,-871 +-208,-536,-159,-596,8,197 +-3,370,-686,170,913,-376 +44,-998,-149,-993,-200,512 +-519,136,859,497,536,434 +77,-985,972,-340,-705,-837 +-381,947,250,360,344,322 +-26,131,699,750,707,384 +-914,655,299,193,406,955 +-883,-921,220,595,-546,794 +-599,577,-569,-404,-704,489 +-594,-963,-624,-460,880,-760 +-603,88,-99,681,55,-328 +976,472,139,-453,-531,-860 +192,-290,513,-89,666,432 +417,487,575,293,567,-668 +655,711,-162,449,-980,972 +-505,664,-685,-239,603,-592 +-625,-802,-67,996,384,-636 +365,-593,522,-666,-200,-431 +-868,708,560,-860,-630,-355 +-702,785,-637,-611,-597,960 +-137,-696,-93,-803,408,406 +891,-123,-26,-609,-610,518 +133,-832,-198,555,708,-110 +791,617,-69,487,696,315 +-900,694,-565,517,-269,-416 +914,135,-781,600,-71,-600 +991,-915,-422,-351,-837,313 +-840,-398,-302,21,590,146 +62,-558,-702,-384,-625,831 +-363,-426,-924,-496,792,-908 +73,361,-817,-466,400,922 +-626,-164,-626,860,-524,286 +255,26,-944,809,-606,986 +-457,-256,-103,50,-867,-871 +-223,803,196,480,612,136 +-820,-928,700,780,-977,721 +717,332,53,-933,-128,793 +-602,-648,562,593,890,702 +-469,-875,-527,911,-475,-222 +110,-281,-552,-536,-816,596 +-981,654,413,-981,-75,-95 +-754,-742,-515,894,-220,-344 +795,-52,156,408,-603,76 +474,-157,423,-499,-807,-791 +260,688,40,-52,702,-122 +-584,-517,-390,-881,302,-504 +61,797,665,708,14,668 +366,166,458,-614,564,-983 +72,539,-378,796,381,-824 +-485,201,-588,842,736,379 +-149,-894,-298,705,-303,-406 +660,-935,-580,521,93,633 +-382,-282,-375,-841,-828,171 +-567,743,-100,43,144,122 +-281,-786,-749,-551,296,304 +11,-426,-792,212,857,-175 +594,143,-699,289,315,137 +341,596,-390,107,-631,-804 +-751,-636,-424,-854,193,651 +-145,384,749,675,-786,517 +224,-865,-323,96,-916,258 +-309,403,-388,826,35,-270 +-942,709,222,158,-699,-103 +-589,842,-997,29,-195,-210 +264,426,566,145,-217,623 +217,965,507,-601,-453,507 +-206,307,-982,4,64,-292 +676,-49,-38,-701,550,883 +5,-850,-438,659,745,-773 +933,238,-574,-570,91,-33 +-866,121,-928,358,459,-843 +-568,-631,-352,-580,-349,189 +-737,849,-963,-486,-662,970 +135,334,-967,-71,-365,-792 +789,21,-227,51,990,-275 +240,412,-886,230,591,256 +-609,472,-853,-754,959,661 +401,521,521,314,929,982 +-499,784,-208,71,-302,296 +-557,-948,-553,-526,-864,793 +270,-626,828,44,37,14 +-412,224,617,-593,502,699 +41,-908,81,562,-849,163 +165,917,761,-197,331,-341 +-687,314,799,755,-969,648 +-164,25,578,439,-334,-576 +213,535,874,-177,-551,24 +-689,291,-795,-225,-496,-125 +465,461,558,-118,-568,-909 +567,660,-810,46,-485,878 +-147,606,685,-690,-774,984 +568,-886,-43,854,-738,616 +-800,386,-614,585,764,-226 +-518,23,-225,-732,-79,440 +-173,-291,-689,636,642,-447 +-598,-16,227,410,496,211 +-474,-930,-656,-321,-420,36 +-435,165,-819,555,540,144 +-969,149,828,568,394,648 +65,-848,257,720,-625,-851 +981,899,275,635,465,-877 +80,290,792,760,-191,-321 +-605,-858,594,33,706,593 +585,-472,318,-35,354,-927 +-365,664,803,581,-965,-814 +-427,-238,-480,146,-55,-606 +879,-193,250,-890,336,117 +-226,-322,-286,-765,-836,-218 +-913,564,-667,-698,937,283 +872,-901,810,-623,-52,-709 +473,171,717,38,-429,-644 +225,824,-219,-475,-180,234 +-530,-797,-948,238,851,-623 +85,975,-363,529,598,28 +-799,166,-804,210,-769,851 +-687,-158,885,736,-381,-461 +447,592,928,-514,-515,-661 +-399,-777,-493,80,-544,-78 +-884,631,171,-825,-333,551 +191,268,-577,676,137,-33 +212,-853,709,798,583,-56 +-908,-172,-540,-84,-135,-56 +303,311,406,-360,-240,811 +798,-708,824,59,234,-57 +491,693,-74,585,-85,877 +509,-65,-936,329,-51,722 +-122,858,-52,467,-77,-609 +850,760,547,-495,-953,-952 +-460,-541,890,910,286,724 +-914,843,-579,-983,-387,-460 +989,-171,-877,-326,-899,458 +846,175,-915,540,-1000,-982 +-852,-920,-306,496,530,-18 +338,-991,160,85,-455,-661 +-186,-311,-460,-563,-231,-414 +-932,-302,959,597,793,748 +-366,-402,-788,-279,514,53 +-940,-956,447,-956,211,-285 +564,806,-911,-914,934,754 +575,-858,-277,15,409,-714 +848,462,100,-381,135,242 +330,718,-24,-190,860,-78 +479,458,941,108,-866,-653 +212,980,962,-962,115,841 +-827,-474,-206,881,323,765 +506,-45,-30,-293,524,-133 +832,-173,547,-852,-561,-842 +-397,-661,-708,819,-545,-228 +521,51,-489,852,36,-258 +227,-164,189,465,-987,-882 +-73,-997,641,-995,449,-615 +151,-995,-638,415,257,-400 +-663,-297,-748,537,-734,198 +-585,-401,-81,-782,-80,-105 +99,-21,238,-365,-704,-368 +45,416,849,-211,-371,-1 +-404,-443,795,-406,36,-933 +272,-363,981,-491,-380,77 +713,-342,-366,-849,643,911 +-748,671,-537,813,961,-200 +-194,-909,703,-662,-601,188 +281,500,724,286,267,197 +-832,847,-595,820,-316,637 +520,521,-54,261,923,-10 +4,-808,-682,-258,441,-695 +-793,-107,-969,905,798,446 +-108,-739,-590,69,-855,-365 +380,-623,-930,817,468,713 +759,-849,-236,433,-723,-931 +95,-320,-686,124,-69,-329 +-655,518,-210,-523,284,-866 +144,303,639,70,-171,269 +173,-333,947,-304,55,40 +274,878,-482,-888,-835,375 +-982,-854,-36,-218,-114,-230 +905,-979,488,-485,-479,114 +877,-157,553,-530,-47,-321 +350,664,-881,442,-220,-284 +434,-423,-365,878,-726,584 +535,909,-517,-447,-660,-141 +-966,191,50,353,182,-642 +-785,-634,123,-907,-162,511 +146,-850,-214,814,-704,25 +692,1,521,492,-637,274 +-662,-372,-313,597,983,-647 +-962,-526,68,-549,-819,231 +740,-890,-318,797,-666,948 +-190,-12,-468,-455,948,284 +16,478,-506,-888,628,-154 +272,630,-976,308,433,3 +-169,-391,-132,189,302,-388 +109,-784,474,-167,-265,-31 +-177,-532,283,464,421,-73 +650,635,592,-138,1,-387 +-932,703,-827,-492,-355,686 +586,-311,340,-618,645,-434 +-951,736,647,-127,-303,590 +188,444,903,718,-931,500 +-872,-642,-296,-571,337,241 +23,65,152,125,880,470 +512,823,-42,217,823,-263 +180,-831,-380,886,607,762 +722,443,-149,-216,-115,759 +-19,660,-36,901,923,231 +562,-322,-626,-968,194,-825 +204,-920,938,784,362,150 +-410,-266,-715,559,-672,124 +-198,446,-140,454,-461,-447 +83,-346,830,-493,-759,-382 +-881,601,581,234,-134,-925 +-494,914,-42,899,235,629 +-390,50,956,437,774,-700 +-514,514,44,-512,-576,-313 +63,-688,808,-534,-570,-399 +-726,572,-896,102,-294,-28 +-688,757,401,406,955,-511 +-283,423,-485,480,-767,908 +-541,952,-594,116,-854,451 +-273,-796,236,625,-626,257 +-407,-493,373,826,-309,297 +-750,955,-476,641,-809,713 +8,415,695,226,-111,2 +733,209,152,-920,401,995 +921,-103,-919,66,871,-947 +-907,89,-869,-214,851,-559 +-307,748,524,-755,314,-711 +188,897,-72,-763,482,103 +545,-821,-232,-596,-334,-754 +-217,-788,-820,388,-200,-662 +779,160,-723,-975,-142,-998 +-978,-519,-78,-981,842,904 +-504,-736,-295,21,-472,-482 +391,115,-705,574,652,-446 +813,-988,865,830,-263,487 +194,80,774,-493,-761,-872 +-415,-284,-803,7,-810,670 +-484,-4,881,-872,55,-852 +-379,822,-266,324,-48,748 +-304,-278,406,-60,959,-89 +404,756,577,-643,-332,658 +291,460,125,491,-312,83 +311,-734,-141,582,282,-557 +-450,-661,-981,710,-177,794 +328,264,-787,971,-743,-407 +-622,518,993,-241,-738,229 +273,-826,-254,-917,-710,-111 +809,770,96,368,-818,725 +-488,773,502,-342,534,745 +-28,-414,236,-315,-484,363 +179,-466,-566,713,-683,56 +560,-240,-597,619,916,-940 +893,473,872,-868,-642,-461 +799,489,383,-321,-776,-833 +980,490,-508,764,-512,-426 +917,961,-16,-675,440,559 +-812,212,784,-987,-132,554 +-886,454,747,806,190,231 +910,341,21,-66,708,725 +29,929,-831,-494,-303,389 +-103,492,-271,-174,-515,529 +-292,119,419,788,247,-951 +483,543,-347,-673,664,-549 +-926,-871,-437,337,162,-877 +299,472,-771,5,-88,-643 +-103,525,-725,-998,264,22 +-505,708,550,-545,823,347 +-738,931,59,147,-156,-259 +456,968,-162,889,132,-911 +535,120,968,-517,-864,-541 +24,-395,-593,-766,-565,-332 +834,611,825,-576,280,629 +211,-548,140,-278,-592,929 +-999,-240,-63,-78,793,573 +-573,160,450,987,529,322 +63,353,315,-187,-461,577 +189,-950,-247,656,289,241 +209,-297,397,664,-805,484 +-655,452,435,-556,917,874 +253,-756,262,-888,-778,-214 +793,-451,323,-251,-401,-458 +-396,619,-651,-287,-668,-781 +698,720,-349,742,-807,546 +738,280,680,279,-540,858 +-789,387,530,-36,-551,-491 +162,579,-427,-272,228,710 +689,356,917,-580,729,217 +-115,-638,866,424,-82,-194 +411,-338,-917,172,227,-29 +-612,63,630,-976,-64,-204 +-200,911,583,-571,682,-579 +91,298,396,-183,788,-955 +141,-873,-277,149,-396,916 +321,958,-136,573,541,-777 +797,-909,-469,-877,988,-653 +784,-198,129,883,-203,399 +-68,-810,223,-423,-467,-512 +531,-445,-603,-997,-841,641 +-274,-242,174,261,-636,-158 +-574,494,-796,-798,-798,99 +95,-82,-613,-954,-753,986 +-883,-448,-864,-401,938,-392 +913,930,-542,-988,310,410 +506,-99,43,512,790,-222 +724,31,49,-950,260,-134 +-287,-947,-234,-700,56,588 +-33,782,-144,948,105,-791 +548,-546,-652,-293,881,-520 +691,-91,76,991,-631,742 +-520,-429,-244,-296,724,-48 +778,646,377,50,-188,56 +-895,-507,-898,-165,-674,652 +654,584,-634,177,-349,-620 +114,-980,355,62,182,975 +516,9,-442,-298,274,-579 +-238,262,-431,-896,506,-850 +47,748,846,821,-537,-293 +839,726,593,285,-297,840 +634,-486,468,-304,-887,-567 +-864,914,296,-124,335,233 +88,-253,-523,-956,-554,803 +-587,417,281,-62,-409,-363 +-136,-39,-292,-768,-264,876 +-127,506,-891,-331,-744,-430 +778,584,-750,-129,-479,-94 +-876,-771,-987,-757,180,-641 +-777,-694,411,-87,329,190 +-347,-999,-882,158,-754,232 +-105,918,188,237,-110,-591 +-209,703,-838,77,838,909 +-995,-339,-762,750,860,472 +185,271,-289,173,811,-300 +2,65,-656,-22,36,-139 +765,-210,883,974,961,-905 +-212,295,-615,-840,77,474 +211,-910,-440,703,-11,859 +-559,-4,-196,841,-277,969 +-73,-159,-887,126,978,-371 +-569,633,-423,-33,512,-393 +503,143,-383,-109,-649,-998 +-663,339,-317,-523,-2,596 +690,-380,570,378,-652,132 +72,-744,-930,399,-525,935 +865,-983,115,37,995,826 +594,-621,-872,443,188,-241 +-1000,291,754,234,-435,-869 +-868,901,654,-907,59,181 +-868,-793,-431,596,-446,-564 +900,-944,-680,-796,902,-366 +331,430,943,853,-851,-942 +315,-538,-354,-909,139,721 +170,-884,-225,-818,-808,-657 +-279,-34,-533,-871,-972,552 +691,-986,-800,-950,654,-747 +603,988,899,841,-630,591 +876,-949,809,562,602,-536 +-693,363,-189,495,738,-1000 +-383,431,-633,297,665,959 +-740,686,-207,-803,188,-520 +-820,226,31,-339,10,121 +-312,-844,624,-516,483,621 +-822,-529,69,-278,800,328 +834,-82,-759,420,811,-264 +-960,-240,-921,561,173,46 +-324,909,-790,-814,-2,-785 +976,334,-290,-891,704,-581 +150,-798,689,-823,237,-639 +-551,-320,876,-502,-622,-628 +-136,845,904,595,-702,-261 +-857,-377,-522,-101,-943,-805 +-682,-787,-888,-459,-752,-985 +-571,-81,623,-133,447,643 +-375,-158,72,-387,-324,-696 +-660,-650,340,188,569,526 +727,-218,16,-7,-595,-988 +-966,-684,802,-783,-272,-194 +115,-566,-888,47,712,180 +-237,-69,45,-272,981,-812 +48,897,439,417,50,325 +348,616,180,254,104,-784 +-730,811,-548,612,-736,790 +138,-810,123,930,65,865 +-768,-299,-49,-895,-692,-418 +487,-531,802,-159,-12,634 +808,-179,552,-73,470,717 +720,-644,886,-141,625,144 +-485,-505,-347,-244,-916,66 +600,-565,995,-5,324,227 +-771,-35,904,-482,753,-303 +-701,65,426,-763,-504,-479 +409,733,-823,475,64,718 +865,975,368,893,-413,-433 +812,-597,-970,819,813,624 +193,-642,-381,-560,545,398 +711,28,-316,771,717,-865 +-509,462,809,-136,786,635 +618,-49,484,169,635,547 +-747,685,-882,-496,-332,82 +-501,-851,870,563,290,570 +-279,-829,-509,397,457,816 +-508,80,850,-188,483,-326 +860,-100,360,119,-205,787 +-870,21,-39,-827,-185,932 +826,284,-136,-866,-330,-97 +-944,-82,745,899,-97,365 +929,262,564,632,-115,632 +244,-276,713,330,-897,-214 +-890,-109,664,876,-974,-907 +716,249,816,489,723,141 +-96,-560,-272,45,-70,645 +762,-503,414,-828,-254,-646 +909,-13,903,-422,-344,-10 +658,-486,743,545,50,674 +-241,507,-367,18,-48,-241 +886,-268,884,-762,120,-486 +-412,-528,879,-647,223,-393 +851,810,234,937,-726,797 +-999,942,839,-134,-996,-189 +100,979,-527,-521,378,800 +544,-844,-832,-530,-77,-641 +43,889,31,442,-934,-503 +-330,-370,-309,-439,173,547 +169,945,62,-753,-542,-597 +208,751,-372,-647,-520,70 +765,-840,907,-257,379,918 +334,-135,-689,730,-427,618 +137,-508,66,-695,78,169 +-962,-123,400,-417,151,969 +328,689,666,427,-555,-642 +-907,343,605,-341,-647,582 +-667,-363,-571,818,-265,-399 +525,-938,904,898,725,692 +-176,-802,-858,-9,780,275 +580,170,-740,287,691,-97 +365,557,-375,361,-288,859 +193,737,842,-808,520,282 +-871,65,-799,836,179,-720 +958,-144,744,-789,797,-48 +122,582,662,912,68,757 +595,241,-801,513,388,186 +-103,-677,-259,-731,-281,-857 +921,319,-696,683,-88,-997 +775,200,78,858,648,768 +316,821,-763,68,-290,-741 +564,664,691,504,760,787 +694,-119,973,-385,309,-760 +777,-947,-57,990,74,19 +971,626,-496,-781,-602,-239 +-651,433,11,-339,939,294 +-965,-728,560,569,-708,-247 diff --git a/project_euler/problem_102/sol1.py b/project_euler/problem_102/sol1.py new file mode 100644 index 000000000000..00af726656ce --- /dev/null +++ b/project_euler/problem_102/sol1.py @@ -0,0 +1,81 @@ +""" +Three distinct points are plotted at random on a Cartesian plane, +for which -1000 ≤ x, y ≤ 1000, such that a triangle is formed. + +Consider the following two triangles: + +A(-340,495), B(-153,-910), C(835,-947) + +X(-175,41), Y(-421,-714), Z(574,-645) + +It can be verified that triangle ABC contains the origin, whereas +triangle XYZ does not. + +Using triangles.txt (right click and 'Save Link/Target As...'), a 27K text +file containing the coordinates of one thousand "random" triangles, find +the number of triangles for which the interior contains the origin. + +NOTE: The first two examples in the file represent the triangles in the +example given above. +""" + +from pathlib import Path +from typing import List, Tuple + + +def vector_product(point1: Tuple[int, int], point2: Tuple[int, int]) -> int: + """ + Return the 2-d vector product of two vectors. + >>> vector_product((1, 2), (-5, 0)) + 10 + >>> vector_product((3, 1), (6, 10)) + 24 + """ + return point1[0] * point2[1] - point1[1] * point2[0] + + +def contains_origin(x1: int, y1: int, x2: int, y2: int, x3: int, y3: int) -> bool: + """ + Check if the triangle given by the points A(x1, y1), B(x2, y2), C(x3, y3) + contains the origin. + >>> contains_origin(-340, 495, -153, -910, 835, -947) + True + >>> contains_origin(-175, 41, -421, -714, 574, -645) + False + """ + point_a: Tuple[int, int] = (x1, y1) + point_a_to_b: Tuple[int, int] = (x2 - x1, y2 - y1) + point_a_to_c: Tuple[int, int] = (x3 - x1, y3 - y1) + a: float = -vector_product(point_a, point_a_to_b) / vector_product( + point_a_to_c, point_a_to_b + ) + b: float = +vector_product(point_a, point_a_to_c) / vector_product( + point_a_to_c, point_a_to_b + ) + + return a > 0 and b > 0 and a + b < 1 + + +def solution(filename: str = "p102_triangles.txt") -> int: + """ + Find the number of triangles whose interior contains the origin. + >>> solution("test_triangles.txt") + 1 + """ + data: str = Path(__file__).parent.joinpath(filename).read_text(encoding="utf-8") + + triangles: List[List[int]] = [] + for line in data.strip().split("\n"): + triangles.append([int(number) for number in line.split(",")]) + + ret: int = 0 + triangle: List[int] + + for triangle in triangles: + ret += contains_origin(*triangle) + + return ret + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_102/test_triangles.txt b/project_euler/problem_102/test_triangles.txt new file mode 100644 index 000000000000..5c10cd651e9b --- /dev/null +++ b/project_euler/problem_102/test_triangles.txt @@ -0,0 +1,2 @@ +-340,495,-153,-910,835,-947 +-175,41,-421,-714,574,-645 diff --git a/project_euler/problem_107/__init__.py b/project_euler/problem_107/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_107/p107_network.txt b/project_euler/problem_107/p107_network.txt new file mode 100644 index 000000000000..fcc3c4192b96 --- /dev/null +++ b/project_euler/problem_107/p107_network.txt @@ -0,0 +1,40 @@ +-,-,-,427,668,495,377,678,-,177,-,-,870,-,869,624,300,609,131,-,251,-,-,-,856,221,514,-,591,762,182,56,-,884,412,273,636,-,-,774 +-,-,262,-,-,508,472,799,-,956,578,363,940,143,-,162,122,910,-,729,802,941,922,573,531,539,667,607,-,920,-,-,315,649,937,-,185,102,636,289 +-,262,-,-,926,-,958,158,647,47,621,264,81,-,402,813,649,386,252,391,264,637,349,-,-,-,108,-,727,225,578,699,-,898,294,-,575,168,432,833 +427,-,-,-,366,-,-,635,-,32,962,468,893,854,718,427,448,916,258,-,760,909,529,311,404,-,-,588,680,875,-,615,-,409,758,221,-,-,76,257 +668,-,926,366,-,-,-,250,268,-,503,944,-,677,-,727,793,457,981,191,-,-,-,351,969,925,987,328,282,589,-,873,477,-,-,19,450,-,-,- +495,508,-,-,-,-,-,765,711,819,305,302,926,-,-,582,-,861,-,683,293,-,-,66,-,27,-,-,290,-,786,-,554,817,33,-,54,506,386,381 +377,472,958,-,-,-,-,-,-,120,42,-,134,219,457,639,538,374,-,-,-,966,-,-,-,-,-,449,120,797,358,232,550,-,305,997,662,744,686,239 +678,799,158,635,250,765,-,-,-,35,-,106,385,652,160,-,890,812,605,953,-,-,-,79,-,712,613,312,452,-,978,900,-,901,-,-,225,533,770,722 +-,-,647,-,268,711,-,-,-,283,-,172,-,663,236,36,403,286,986,-,-,810,761,574,53,793,-,-,777,330,936,883,286,-,174,-,-,-,828,711 +177,956,47,32,-,819,120,35,283,-,50,-,565,36,767,684,344,489,565,-,-,103,810,463,733,665,494,644,863,25,385,-,342,470,-,-,-,730,582,468 +-,578,621,962,503,305,42,-,-,50,-,155,519,-,-,256,990,801,154,53,474,650,402,-,-,-,966,-,-,406,989,772,932,7,-,823,391,-,-,933 +-,363,264,468,944,302,-,106,172,-,155,-,-,-,380,438,-,41,266,-,-,104,867,609,-,270,861,-,-,165,-,675,250,686,995,366,191,-,433,- +870,940,81,893,-,926,134,385,-,565,519,-,-,313,851,-,-,-,248,220,-,826,359,829,-,234,198,145,409,68,359,-,814,218,186,-,-,929,203,- +-,143,-,854,677,-,219,652,663,36,-,-,313,-,132,-,433,598,-,-,168,870,-,-,-,128,437,-,383,364,966,227,-,-,807,993,-,-,526,17 +869,-,402,718,-,-,457,160,236,767,-,380,851,132,-,-,596,903,613,730,-,261,-,142,379,885,89,-,848,258,112,-,900,-,-,818,639,268,600,- +624,162,813,427,727,582,639,-,36,684,256,438,-,-,-,-,539,379,664,561,542,-,999,585,-,-,321,398,-,-,950,68,193,-,697,-,390,588,848,- +300,122,649,448,793,-,538,890,403,344,990,-,-,433,596,539,-,-,73,-,318,-,-,500,-,968,-,291,-,-,765,196,504,757,-,542,-,395,227,148 +609,910,386,916,457,861,374,812,286,489,801,41,-,598,903,379,-,-,-,946,136,399,-,941,707,156,757,258,251,-,807,-,-,-,461,501,-,-,616,- +131,-,252,258,981,-,-,605,986,565,154,266,248,-,613,664,73,-,-,686,-,-,575,627,817,282,-,698,398,222,-,649,-,-,-,-,-,654,-,- +-,729,391,-,191,683,-,953,-,-,53,-,220,-,730,561,-,946,686,-,-,389,729,553,304,703,455,857,260,-,991,182,351,477,867,-,-,889,217,853 +251,802,264,760,-,293,-,-,-,-,474,-,-,168,-,542,318,136,-,-,-,-,392,-,-,-,267,407,27,651,80,927,-,974,977,-,-,457,117,- +-,941,637,909,-,-,966,-,810,103,650,104,826,870,261,-,-,399,-,389,-,-,-,202,-,-,-,-,867,140,403,962,785,-,511,-,1,-,707,- +-,922,349,529,-,-,-,-,761,810,402,867,359,-,-,999,-,-,575,729,392,-,-,388,939,-,959,-,83,463,361,-,-,512,931,-,224,690,369,- +-,573,-,311,351,66,-,79,574,463,-,609,829,-,142,585,500,941,627,553,-,202,388,-,164,829,-,620,523,639,936,-,-,490,-,695,-,505,109,- +856,531,-,404,969,-,-,-,53,733,-,-,-,-,379,-,-,707,817,304,-,-,939,164,-,-,616,716,728,-,889,349,-,963,150,447,-,292,586,264 +221,539,-,-,925,27,-,712,793,665,-,270,234,128,885,-,968,156,282,703,-,-,-,829,-,-,-,822,-,-,-,736,576,-,697,946,443,-,205,194 +514,667,108,-,987,-,-,613,-,494,966,861,198,437,89,321,-,757,-,455,267,-,959,-,616,-,-,-,349,156,339,-,102,790,359,-,439,938,809,260 +-,607,-,588,328,-,449,312,-,644,-,-,145,-,-,398,291,258,698,857,407,-,-,620,716,822,-,-,293,486,943,-,779,-,6,880,116,775,-,947 +591,-,727,680,282,290,120,452,777,863,-,-,409,383,848,-,-,251,398,260,27,867,83,523,728,-,349,293,-,212,684,505,341,384,9,992,507,48,-,- +762,920,225,875,589,-,797,-,330,25,406,165,68,364,258,-,-,-,222,-,651,140,463,639,-,-,156,486,212,-,-,349,723,-,-,186,-,36,240,752 +182,-,578,-,-,786,358,978,936,385,989,-,359,966,112,950,765,807,-,991,80,403,361,936,889,-,339,943,684,-,-,965,302,676,725,-,327,134,-,147 +56,-,699,615,873,-,232,900,883,-,772,675,-,227,-,68,196,-,649,182,927,962,-,-,349,736,-,-,505,349,965,-,474,178,833,-,-,555,853,- +-,315,-,-,477,554,550,-,286,342,932,250,814,-,900,193,504,-,-,351,-,785,-,-,-,576,102,779,341,723,302,474,-,689,-,-,-,451,-,- +884,649,898,409,-,817,-,901,-,470,7,686,218,-,-,-,757,-,-,477,974,-,512,490,963,-,790,-,384,-,676,178,689,-,245,596,445,-,-,343 +412,937,294,758,-,33,305,-,174,-,-,995,186,807,-,697,-,461,-,867,977,511,931,-,150,697,359,6,9,-,725,833,-,245,-,949,-,270,-,112 +273,-,-,221,19,-,997,-,-,-,823,366,-,993,818,-,542,501,-,-,-,-,-,695,447,946,-,880,992,186,-,-,-,596,949,-,91,-,768,273 +636,185,575,-,450,54,662,225,-,-,391,191,-,-,639,390,-,-,-,-,-,1,224,-,-,443,439,116,507,-,327,-,-,445,-,91,-,248,-,344 +-,102,168,-,-,506,744,533,-,730,-,-,929,-,268,588,395,-,654,889,457,-,690,505,292,-,938,775,48,36,134,555,451,-,270,-,248,-,371,680 +-,636,432,76,-,386,686,770,828,582,-,433,203,526,600,848,227,616,-,217,117,707,369,109,586,205,809,-,-,240,-,853,-,-,-,768,-,371,-,540 +774,289,833,257,-,381,239,722,711,468,933,-,-,17,-,-,148,-,-,853,-,-,-,-,264,194,260,947,-,752,147,-,-,343,112,273,344,680,540,- diff --git a/project_euler/problem_107/sol1.py b/project_euler/problem_107/sol1.py new file mode 100644 index 000000000000..80a10e499f76 --- /dev/null +++ b/project_euler/problem_107/sol1.py @@ -0,0 +1,128 @@ +""" +The following undirected network consists of seven vertices and twelve edges +with a total weight of 243. + +The same network can be represented by the matrix below. + + A B C D E F G +A - 16 12 21 - - - +B 16 - - 17 20 - - +C 12 - - 28 - 31 - +D 21 17 28 - 18 19 23 +E - 20 - 18 - - 11 +F - - 31 19 - - 27 +G - - - 23 11 27 - + +However, it is possible to optimise the network by removing some edges and still +ensure that all points on the network remain connected. The network which achieves +the maximum saving is shown below. It has a weight of 93, representing a saving of +243 - 93 = 150 from the original network. + +Using network.txt (right click and 'Save Link/Target As...'), a 6K text file +containing a network with forty vertices, and given in matrix form, find the maximum +saving which can be achieved by removing redundant edges whilst ensuring that the +network remains connected. + +Solution: + We use Prim's algorithm to find a Minimum Spanning Tree. + Reference: https://en.wikipedia.org/wiki/Prim%27s_algorithm +""" + +import os +from typing import Dict, List, Mapping, Set, Tuple + +EdgeT = Tuple[int, int] + + +class Graph: + """ + A class representing an undirected weighted graph. + """ + + def __init__(self, vertices: Set[int], edges: Mapping[EdgeT, int]) -> None: + self.vertices: Set[int] = vertices + self.edges: Dict[EdgeT, int] = { + (min(edge), max(edge)): weight for edge, weight in edges.items() + } + + def add_edge(self, edge: EdgeT, weight: int) -> None: + """ + Add a new edge to the graph. + >>> graph = Graph({1, 2}, {(2, 1): 4}) + >>> graph.add_edge((3, 1), 5) + >>> sorted(graph.vertices) + [1, 2, 3] + >>> sorted([(v,k) for k,v in graph.edges.items()]) + [(4, (1, 2)), (5, (1, 3))] + """ + self.vertices.add(edge[0]) + self.vertices.add(edge[1]) + self.edges[(min(edge), max(edge))] = weight + + def prims_algorithm(self) -> "Graph": + """ + Run Prim's algorithm to find the minimum spanning tree. + Reference: https://en.wikipedia.org/wiki/Prim%27s_algorithm + >>> graph = Graph({1,2,3,4},{(1,2):5, (1,3):10, (1,4):20, (2,4):30, (3,4):1}) + >>> mst = graph.prims_algorithm() + >>> sorted(mst.vertices) + [1, 2, 3, 4] + >>> sorted(mst.edges) + [(1, 2), (1, 3), (3, 4)] + """ + subgraph: Graph = Graph({min(self.vertices)}, {}) + min_edge: EdgeT + min_weight: int + edge: EdgeT + weight: int + + while len(subgraph.vertices) < len(self.vertices): + min_weight = max(self.edges.values()) + 1 + for edge, weight in self.edges.items(): + if (edge[0] in subgraph.vertices) ^ (edge[1] in subgraph.vertices): + if weight < min_weight: + min_edge = edge + min_weight = weight + + subgraph.add_edge(min_edge, min_weight) + + return subgraph + + +def solution(filename: str = "p107_network.txt") -> int: + """ + Find the maximum saving which can be achieved by removing redundant edges + whilst ensuring that the network remains connected. + >>> solution("test_network.txt") + 150 + """ + script_dir: str = os.path.abspath(os.path.dirname(__file__)) + network_file: str = os.path.join(script_dir, filename) + adjacency_matrix: List[List[str]] + edges: Dict[EdgeT, int] = dict() + data: List[str] + edge1: int + edge2: int + + with open(network_file, "r") as f: + data = f.read().strip().split("\n") + + adjaceny_matrix = [line.split(",") for line in data] + + for edge1 in range(1, len(adjaceny_matrix)): + for edge2 in range(edge1): + if adjaceny_matrix[edge1][edge2] != "-": + edges[(edge2, edge1)] = int(adjaceny_matrix[edge1][edge2]) + + graph: Graph = Graph(set(range(len(adjaceny_matrix))), edges) + + subgraph: Graph = graph.prims_algorithm() + + initial_total: int = sum(graph.edges.values()) + optimal_total: int = sum(subgraph.edges.values()) + + return initial_total - optimal_total + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_107/test_network.txt b/project_euler/problem_107/test_network.txt new file mode 100644 index 000000000000..f5f2accb5720 --- /dev/null +++ b/project_euler/problem_107/test_network.txt @@ -0,0 +1,7 @@ +-,16,12,21,-,-,- +16,-,-,17,20,-,- +12,-,-,28,-,31,- +21,17,28,-,18,19,23 +-,20,-,18,-,-,11 +-,-,31,19,-,-,27 +-,-,-,23,11,27,- diff --git a/project_euler/problem_11/sol1.py b/project_euler/problem_11/sol1.py deleted file mode 100644 index b882dc449156..000000000000 --- a/project_euler/problem_11/sol1.py +++ /dev/null @@ -1,68 +0,0 @@ -from __future__ import print_function -''' -What is the greatest product of four adjacent numbers (horizontally, vertically, or diagonally) in this 20x20 array? - -08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 -49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 -81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 -52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 -22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 -24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 -32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 -67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 -24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 -21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 -78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 -16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 -86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 -19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 -04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 -88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 -04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 -20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 -20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 -01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 -''' - -try: - xrange #Python 2 -except NameError: - xrange = range #Python 2 - -def largest_product(grid): - nColumns = len(grid[0]) - nRows = len(grid) - - largest = 0 - lrDiagProduct = 0 - rlDiagProduct = 0 - - #Check vertically, horizontally, diagonally at the same time (only works for nxn grid) - for i in xrange(nColumns): - for j in xrange(nRows-3): - vertProduct = grid[j][i]*grid[j+1][i]*grid[j+2][i]*grid[j+3][i] - horzProduct = grid[i][j]*grid[i][j+1]*grid[i][j+2]*grid[i][j+3] - - #Left-to-right diagonal (\) product - if (i < nColumns-3): - lrDiagProduct = grid[i][j]*grid[i+1][j+1]*grid[i+2][j+2]*grid[i+3][j+3] - - #Right-to-left diagonal(/) product - if (i > 2): - rlDiagProduct = grid[i][j]*grid[i-1][j+1]*grid[i-2][j+2]*grid[i-3][j+3] - - maxProduct = max(vertProduct, horzProduct, lrDiagProduct, rlDiagProduct) - if maxProduct > largest: - largest = maxProduct - - return largest - -if __name__ == '__main__': - grid = [] - with open('grid.txt') as file: - for line in file: - grid.append(line.strip('\n').split(' ')) - - grid = [[int(i) for i in grid[j]] for j in xrange(len(grid))] - - print(largest_product(grid)) \ No newline at end of file diff --git a/project_euler/problem_11/sol2.py b/project_euler/problem_11/sol2.py deleted file mode 100644 index b03395f01697..000000000000 --- a/project_euler/problem_11/sol2.py +++ /dev/null @@ -1,39 +0,0 @@ -def main(): - with open ("grid.txt", "r") as f: - l = [] - for i in range(20): - l.append([int(x) for x in f.readline().split()]) - - maximum = 0 - - # right - for i in range(20): - for j in range(17): - temp = l[i][j] * l[i][j+1] * l[i][j+2] * l[i][j+3] - if temp > maximum: - maximum = temp - - # down - for i in range(17): - for j in range(20): - temp = l[i][j] * l[i+1][j] * l[i+2][j] * l[i+3][j] - if temp > maximum: - maximum = temp - - #diagonal 1 - for i in range(17): - for j in range(17): - temp = l[i][j] * l[i+1][j+1] * l[i+2][j+2] * l[i+3][j+3] - if temp > maximum: - maximum = temp - - #diagonal 2 - for i in range(17): - for j in range(3, 20): - temp = l[i][j] * l[i+1][j-1] * l[i+2][j-2] * l[i+3][j-3] - if temp > maximum: - maximum = temp - print(maximum) - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/project_euler/problem_112/__init__.py b/project_euler/problem_112/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_112/sol1.py b/project_euler/problem_112/sol1.py new file mode 100644 index 000000000000..d8cb334c9508 --- /dev/null +++ b/project_euler/problem_112/sol1.py @@ -0,0 +1,89 @@ +""" +Problem 112: https://projecteuler.net/problem=112 + +Working from left-to-right if no digit is exceeded by the digit to its left it is +called an increasing number; for example, 134468. +Similarly if no digit is exceeded by the digit to its right it is called a decreasing +number; for example, 66420. +We shall call a positive integer that is neither increasing nor decreasing a "bouncy" +number, for example, 155349. +Clearly there cannot be any bouncy numbers below one-hundred, but just over half of +the numbers below one-thousand (525) are bouncy. In fact, the least number for which +the proportion of bouncy numbers first reaches 50% is 538. +Surprisingly, bouncy numbers become more and more common and by the time we reach +21780 the proportion of bouncy numbers is equal to 90%. + +Find the least number for which the proportion of bouncy numbers is exactly 99%. +""" + + +def check_bouncy(n: int) -> bool: + """ + Returns True if number is bouncy, False otherwise + >>> check_bouncy(6789) + False + >>> check_bouncy(-12345) + False + >>> check_bouncy(0) + False + >>> check_bouncy(6.74) + Traceback (most recent call last): + ... + ValueError: check_bouncy() accepts only integer arguments + >>> check_bouncy(132475) + True + >>> check_bouncy(34) + False + >>> check_bouncy(341) + True + >>> check_bouncy(47) + False + >>> check_bouncy(-12.54) + Traceback (most recent call last): + ... + ValueError: check_bouncy() accepts only integer arguments + >>> check_bouncy(-6548) + True + """ + if not isinstance(n, int): + raise ValueError("check_bouncy() accepts only integer arguments") + return "".join(sorted(str(n))) != str(n) and "".join(sorted(str(n)))[::-1] != str(n) + + +def solution(percent: float = 99) -> int: + """ + Returns the least number for which the proportion of bouncy numbers is + exactly 'percent' + >>> solution(50) + 538 + >>> solution(90) + 21780 + >>> solution(80) + 4770 + >>> solution(105) + Traceback (most recent call last): + ... + ValueError: solution() only accepts values from 0 to 100 + >>> solution(100.011) + Traceback (most recent call last): + ... + ValueError: solution() only accepts values from 0 to 100 + """ + if not 0 < percent < 100: + raise ValueError("solution() only accepts values from 0 to 100") + bouncy_num = 0 + num = 1 + + while True: + if check_bouncy(num): + bouncy_num += 1 + if (bouncy_num / num) * 100 >= percent: + return num + num += 1 + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + print(f"{solution(99)}") diff --git a/project_euler/problem_113/__init__.py b/project_euler/problem_113/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_113/sol1.py b/project_euler/problem_113/sol1.py new file mode 100644 index 000000000000..951d9b49c104 --- /dev/null +++ b/project_euler/problem_113/sol1.py @@ -0,0 +1,75 @@ +""" +Project Euler Problem 113: https://projecteuler.net/problem=113 + +Working from left-to-right if no digit is exceeded by the digit to its left it is +called an increasing number; for example, 134468. + +Similarly if no digit is exceeded by the digit to its right it is called a decreasing +number; for example, 66420. + +We shall call a positive integer that is neither increasing nor decreasing a +"bouncy" number; for example, 155349. + +As n increases, the proportion of bouncy numbers below n increases such that there +are only 12951 numbers below one-million that are not bouncy and only 277032 +non-bouncy numbers below 10^10. + +How many numbers below a googol (10^100) are not bouncy? +""" + + +def choose(n: int, r: int) -> int: + """ + Calculate the binomial coefficient c(n,r) using the multiplicative formula. + >>> choose(4,2) + 6 + >>> choose(5,3) + 10 + >>> choose(20,6) + 38760 + """ + ret = 1.0 + for i in range(1, r + 1): + ret *= (n + 1 - i) / i + return round(ret) + + +def non_bouncy_exact(n: int) -> int: + """ + Calculate the number of non-bouncy numbers with at most n digits. + >>> non_bouncy_exact(1) + 9 + >>> non_bouncy_exact(6) + 7998 + >>> non_bouncy_exact(10) + 136126 + """ + return choose(8 + n, n) + choose(9 + n, n) - 10 + + +def non_bouncy_upto(n: int) -> int: + """ + Calculate the number of non-bouncy numbers with at most n digits. + >>> non_bouncy_upto(1) + 9 + >>> non_bouncy_upto(6) + 12951 + >>> non_bouncy_upto(10) + 277032 + """ + return sum(non_bouncy_exact(i) for i in range(1, n + 1)) + + +def solution(num_digits: int = 100) -> int: + """ + Caclulate the number of non-bouncy numbers less than a googol. + >>> solution(6) + 12951 + >>> solution(10) + 277032 + """ + return non_bouncy_upto(num_digits) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_119/__init__.py b/project_euler/problem_119/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_119/sol1.py b/project_euler/problem_119/sol1.py new file mode 100644 index 000000000000..7f343ac242e9 --- /dev/null +++ b/project_euler/problem_119/sol1.py @@ -0,0 +1,51 @@ +""" +Problem 119: https://projecteuler.net/problem=119 + +Name: Digit power sum + +The number 512 is interesting because it is equal to the sum of its digits +raised to some power: 5 + 1 + 2 = 8, and 8^3 = 512. Another example of a number +with this property is 614656 = 28^4. We shall define an to be the nth term of +this sequence and insist that a number must contain at least two digits to have a sum. +You are given that a2 = 512 and a10 = 614656. Find a30 +""" + +import math + + +def digit_sum(n: int) -> int: + """ + Returns the sum of the digits of the number. + >>> digit_sum(123) + 6 + >>> digit_sum(456) + 15 + >>> digit_sum(78910) + 25 + """ + return sum([int(digit) for digit in str(n)]) + + +def solution(n: int = 30) -> int: + """ + Returns the value of 30th digit power sum. + >>> solution(2) + 512 + >>> solution(5) + 5832 + >>> solution(10) + 614656 + """ + digit_to_powers = [] + for digit in range(2, 100): + for power in range(2, 100): + number = int(math.pow(digit, power)) + if digit == digit_sum(number): + digit_to_powers.append(number) + + digit_to_powers.sort() + return digit_to_powers[n - 1] + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_12/sol1.py b/project_euler/problem_12/sol1.py deleted file mode 100644 index 73d48a2ec897..000000000000 --- a/project_euler/problem_12/sol1.py +++ /dev/null @@ -1,48 +0,0 @@ -from __future__ import print_function -from math import sqrt -''' -Highly divisible triangular numbers -Problem 12 -The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: - -1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... - -Let us list the factors of the first seven triangle numbers: - - 1: 1 - 3: 1,3 - 6: 1,2,3,6 -10: 1,2,5,10 -15: 1,3,5,15 -21: 1,3,7,21 -28: 1,2,4,7,14,28 -We can see that 28 is the first triangle number to have over five divisors. - -What is the value of the first triangle number to have over five hundred divisors? -''' -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -def count_divisors(n): - nDivisors = 0 - for i in xrange(1, int(sqrt(n))+1): - if n%i == 0: - nDivisors += 2 - #check if n is perfect square - if n**0.5 == int(n**0.5): - nDivisors -= 1 - return nDivisors - -tNum = 1 -i = 1 - -while True: - i += 1 - tNum += i - - if count_divisors(tNum) > 500: - break - -print(tNum) diff --git a/project_euler/problem_12/sol2.py b/project_euler/problem_12/sol2.py deleted file mode 100644 index 479ab2b900cb..000000000000 --- a/project_euler/problem_12/sol2.py +++ /dev/null @@ -1,8 +0,0 @@ -def triangle_number_generator(): - for n in range(1,1000000): - yield n*(n+1)//2 - -def count_divisors(n): - return sum([2 for i in range(1,int(n**0.5)+1) if n%i==0 and i*i != n]) - -print(next(i for i in triangle_number_generator() if count_divisors(i) > 500)) diff --git a/project_euler/problem_120/__init__.py b/project_euler/problem_120/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_120/sol1.py b/project_euler/problem_120/sol1.py new file mode 100644 index 000000000000..0e6821214560 --- /dev/null +++ b/project_euler/problem_120/sol1.py @@ -0,0 +1,32 @@ +""" +Problem 120 Square remainders: https://projecteuler.net/problem=120 + +Description: + +Let r be the remainder when (a−1)^n + (a+1)^n is divided by a^2. +For example, if a = 7 and n = 3, then r = 42: 6^3 + 8^3 = 728 ≡ 42 mod 49. +And as n varies, so too will r, but for a = 7 it turns out that r_max = 42. +For 3 ≤ a ≤ 1000, find ∑ r_max. + +Solution: + +On expanding the terms, we get 2 if n is even and 2an if n is odd. +For maximizing the value, 2an < a*a => n <= (a - 1)/2 (integer division) +""" + + +def solution(n: int = 1000) -> int: + """ + Returns ∑ r_max for 3 <= a <= n as explained above + >>> solution(10) + 300 + >>> solution(100) + 330750 + >>> solution(1000) + 333082500 + """ + return sum(2 * a * ((a - 1) // 2) for a in range(3, n + 1)) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_123/__init__.py b/project_euler/problem_123/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_123/sol1.py b/project_euler/problem_123/sol1.py new file mode 100644 index 000000000000..85350c8bae49 --- /dev/null +++ b/project_euler/problem_123/sol1.py @@ -0,0 +1,99 @@ +""" +Problem 123: https://projecteuler.net/problem=123 + +Name: Prime square remainders + +Let pn be the nth prime: 2, 3, 5, 7, 11, ..., and +let r be the remainder when (pn−1)^n + (pn+1)^n is divided by pn^2. + +For example, when n = 3, p3 = 5, and 43 + 63 = 280 ≡ 5 mod 25. +The least value of n for which the remainder first exceeds 10^9 is 7037. + +Find the least value of n for which the remainder first exceeds 10^10. + + +Solution: + +n=1: (p-1) + (p+1) = 2p +n=2: (p-1)^2 + (p+1)^2 + = p^2 + 1 - 2p + p^2 + 1 + 2p (Using (p+b)^2 = (p^2 + b^2 + 2pb), + (p-b)^2 = (p^2 + b^2 - 2pb) and b = 1) + = 2p^2 + 2 +n=3: (p-1)^3 + (p+1)^3 (Similarly using (p+b)^3 & (p-b)^3 formula and so on) + = 2p^3 + 6p +n=4: 2p^4 + 12p^2 + 2 +n=5: 2p^5 + 20p^3 + 10p + +As you could see, when the expression is divided by p^2. +Except for the last term, the rest will result in the remainder 0. + +n=1: 2p +n=2: 2 +n=3: 6p +n=4: 2 +n=5: 10p + +So it could be simplified as, + r = 2pn when n is odd + r = 2 when n is even. +""" + +from typing import Dict, Generator + + +def sieve() -> Generator[int, None, None]: + """ + Returns a prime number generator using sieve method. + >>> type(sieve()) + + >>> primes = sieve() + >>> next(primes) + 2 + >>> next(primes) + 3 + >>> next(primes) + 5 + >>> next(primes) + 7 + >>> next(primes) + 11 + >>> next(primes) + 13 + """ + factor_map: Dict[int, int] = {} + prime = 2 + while True: + factor = factor_map.pop(prime, None) + if factor: + x = factor + prime + while x in factor_map: + x += factor + factor_map[x] = factor + else: + factor_map[prime * prime] = prime + yield prime + prime += 1 + + +def solution(limit: float = 1e10) -> int: + """ + Returns the least value of n for which the remainder first exceeds 10^10. + >>> solution(1e8) + 2371 + >>> solution(1e9) + 7037 + """ + primes = sieve() + + n = 1 + while True: + prime = next(primes) + if (2 * prime * n) > limit: + return n + # Ignore the next prime as the reminder will be 2. + next(primes) + n += 2 + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_125/__init__.py b/project_euler/problem_125/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_125/sol1.py b/project_euler/problem_125/sol1.py new file mode 100644 index 000000000000..afc1f2890cef --- /dev/null +++ b/project_euler/problem_125/sol1.py @@ -0,0 +1,56 @@ +""" +Problem 125: https://projecteuler.net/problem=125 + +The palindromic number 595 is interesting because it can be written as the sum +of consecutive squares: 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2. + +There are exactly eleven palindromes below one-thousand that can be written as +consecutive square sums, and the sum of these palindromes is 4164. Note that +1 = 0^2 + 1^2 has not been included as this problem is concerned with the +squares of positive integers. + +Find the sum of all the numbers less than 10^8 that are both palindromic and can +be written as the sum of consecutive squares. +""" + + +def is_palindrome(n: int) -> bool: + """ + Check if an integer is palindromic. + >>> is_palindrome(12521) + True + >>> is_palindrome(12522) + False + >>> is_palindrome(12210) + False + """ + if n % 10 == 0: + return False + s = str(n) + return s == s[::-1] + + +def solution() -> int: + """ + Returns the sum of all numbers less than 1e8 that are both palindromic and + can be written as the sum of consecutive squares. + """ + LIMIT = 10 ** 8 + answer = set() + first_square = 1 + sum_squares = 5 + while sum_squares < LIMIT: + last_square = first_square + 1 + while sum_squares < LIMIT: + if is_palindrome(sum_squares): + answer.add(sum_squares) + last_square += 1 + sum_squares += last_square ** 2 + first_square += 1 + sum_squares = first_square ** 2 + (first_square + 1) ** 2 + + return sum(answer) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_129/__init__.py b/project_euler/problem_129/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_129/sol1.py b/project_euler/problem_129/sol1.py new file mode 100644 index 000000000000..8afe82df162e --- /dev/null +++ b/project_euler/problem_129/sol1.py @@ -0,0 +1,57 @@ +""" +Project Euler Problem 129: https://projecteuler.net/problem=129 + +A number consisting entirely of ones is called a repunit. We shall define R(k) to be +a repunit of length k; for example, R(6) = 111111. + +Given that n is a positive integer and GCD(n, 10) = 1, it can be shown that there +always exists a value, k, for which R(k) is divisible by n, and let A(n) be the least +such value of k; for example, A(7) = 6 and A(41) = 5. + +The least value of n for which A(n) first exceeds ten is 17. + +Find the least value of n for which A(n) first exceeds one-million. +""" + + +def least_divisible_repunit(divisor: int) -> int: + """ + Return the least value k such that the Repunit of length k is divisible by divisor. + >>> least_divisible_repunit(7) + 6 + >>> least_divisible_repunit(41) + 5 + >>> least_divisible_repunit(1234567) + 34020 + """ + if divisor % 5 == 0 or divisor % 2 == 0: + return 0 + repunit = 1 + repunit_index = 1 + while repunit: + repunit = (10 * repunit + 1) % divisor + repunit_index += 1 + return repunit_index + + +def solution(limit: int = 1000000) -> int: + """ + Return the least value of n for which least_divisible_repunit(n) + first exceeds limit. + >>> solution(10) + 17 + >>> solution(100) + 109 + >>> solution(1000) + 1017 + """ + divisor = limit - 1 + if divisor % 2 == 0: + divisor += 1 + while least_divisible_repunit(divisor) <= limit: + divisor += 2 + return divisor + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_13/sol1.py b/project_euler/problem_13/sol1.py deleted file mode 100644 index faaaad5e88c1..000000000000 --- a/project_euler/problem_13/sol1.py +++ /dev/null @@ -1,14 +0,0 @@ -''' -Problem Statement: -Work out the first ten digits of the sum of the N 50-digit numbers. -''' -from __future__ import print_function - -n = int(input().strip()) - -array = [] -for i in range(n): - array.append(int(input().strip())) - -print(str(sum(array))[:10]) - diff --git a/project_euler/problem_135/__init__.py b/project_euler/problem_135/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_135/sol1.py b/project_euler/problem_135/sol1.py new file mode 100644 index 000000000000..d71a0439c7e9 --- /dev/null +++ b/project_euler/problem_135/sol1.py @@ -0,0 +1,61 @@ +""" +Project Euler Problem 135: https://projecteuler.net/problem=135 + +Given the positive integers, x, y, and z, +are consecutive terms of an arithmetic progression, +the least value of the positive integer, n, +for which the equation, +x2 − y2 − z2 = n, has exactly two solutions is n = 27: + +342 − 272 − 202 = 122 − 92 − 62 = 27 + +It turns out that n = 1155 is the least value +which has exactly ten solutions. + +How many values of n less than one million +have exactly ten distinct solutions? + + +Taking x,y,z of the form a+d,a,a-d respectively, +the given equation reduces to a*(4d-a)=n. +Calculating no of solutions for every n till 1 million by fixing a +,and n must be multiple of a. +Total no of steps=n*(1/1+1/2+1/3+1/4..+1/n) +,so roughly O(nlogn) time complexity. + +""" + + +def solution(limit: int = 1000000) -> int: + """ + returns the values of n less than or equal to the limit + have exactly ten distinct solutions. + >>> solution(100) + 0 + >>> solution(10000) + 45 + >>> solution(50050) + 292 + """ + limit = limit + 1 + frequency = [0] * limit + for first_term in range(1, limit): + for n in range(first_term, limit, first_term): + common_difference = first_term + n / first_term + if common_difference % 4: # d must be divisble by 4 + continue + else: + common_difference /= 4 + if ( + first_term > common_difference + and first_term < 4 * common_difference + ): # since x,y,z are positive integers + frequency[n] += 1 # so z>0 and a>d ,also 4d 1: - if number % 2 == 0: - number /=2 - counter += 1 - else: - number = (3*number)+1 - counter += 1 - - if counter > pre_counter: - largest_number = input1 - pre_counter = counter - -print(('Largest Number:',largest_number,'->',pre_counter,'digits')) diff --git a/project_euler/problem_14/sol2.py b/project_euler/problem_14/sol2.py deleted file mode 100644 index b9de42be1108..000000000000 --- a/project_euler/problem_14/sol2.py +++ /dev/null @@ -1,16 +0,0 @@ -def collatz_sequence(n): - """Collatz conjecture: start with any positive integer n.Next termis obtained from the previous term as follows: - if the previous term is even, the next term is one half the previous term. - If the previous term is odd, the next term is 3 times the previous term plus 1. - The conjecture states the sequence will always reach 1 regaardess of starting n.""" - sequence = [n] - while n != 1: - if n % 2 == 0:# even - n //= 2 - else: - n = 3*n +1 - sequence.append(n) - return sequence - -answer = max([(len(collatz_sequence(i)), i) for i in range(1,1000000)]) -print("Longest Collatz sequence under one million is %d with length %d" % (answer[1],answer[0])) \ No newline at end of file diff --git a/project_euler/problem_15/sol1.py b/project_euler/problem_15/sol1.py deleted file mode 100644 index d24748011ef9..000000000000 --- a/project_euler/problem_15/sol1.py +++ /dev/null @@ -1,20 +0,0 @@ -from __future__ import print_function -from math import factorial - -def lattice_paths(n): - n = 2*n #middle entry of odd rows starting at row 3 is the solution for n = 1, 2, 3,... - k = n/2 - - return factorial(n)/(factorial(k)*factorial(n-k)) - -if __name__ == '__main__': - import sys - - if len(sys.argv) == 1: - print(lattice_paths(20)) - else: - try: - n = int(sys.argv[1]) - print(lattice_paths(n)) - except ValueError: - print('Invalid entry - please enter a number.') diff --git a/project_euler/problem_16/sol1.py b/project_euler/problem_16/sol1.py deleted file mode 100644 index 05c7916bd10a..000000000000 --- a/project_euler/problem_16/sol1.py +++ /dev/null @@ -1,15 +0,0 @@ -power = int(input("Enter the power of 2: ")) -num = 2**power - -string_num = str(num) - -list_num = list(string_num) - -sum_of_num = 0 - -print("2 ^",power,"=",num) - -for i in list_num: - sum_of_num += int(i) - -print("Sum of the digits are:",sum_of_num) diff --git a/project_euler/problem_17/sol1.py b/project_euler/problem_17/sol1.py deleted file mode 100644 index 9de5d80b9b29..000000000000 --- a/project_euler/problem_17/sol1.py +++ /dev/null @@ -1,35 +0,0 @@ -from __future__ import print_function -''' -Number letter counts -Problem 17 - -If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. - -If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? - - -NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) -contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage. -''' - -ones_counts = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8] #number of letters in zero, one, two, ..., nineteen (0 for zero since it's never said aloud) -tens_counts = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6] #number of letters in twenty, thirty, ..., ninety (0 for numbers less than 20 due to inconsistency in teens) - -count = 0 - -for i in range(1, 1001): - if i < 1000: - if i >= 100: - count += ones_counts[i/100] + 7 #add number of letters for "n hundred" - - if i%100 is not 0: - count += 3 #add number of letters for "and" if number is not multiple of 100 - - if 0 < i%100 < 20: - count += ones_counts[i%100] #add number of letters for one, two, three, ..., nineteen (could be combined with below if not for inconsistency in teens) - else: - count += ones_counts[i%10] + tens_counts[(i%100-i%10)/10] #add number of letters for twenty, twenty one, ..., ninety nine - else: - count += ones_counts[i/1000] + 8 - -print(count) \ No newline at end of file diff --git a/project_euler/problem_173/__init__.py b/project_euler/problem_173/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_173/sol1.py b/project_euler/problem_173/sol1.py new file mode 100644 index 000000000000..d539b1437ef1 --- /dev/null +++ b/project_euler/problem_173/sol1.py @@ -0,0 +1,41 @@ +""" +Project Euler Problem 173: https://projecteuler.net/problem=173 + +We shall define a square lamina to be a square outline with a square "hole" so that +the shape possesses vertical and horizontal symmetry. For example, using exactly +thirty-two square tiles we can form two different square laminae: + +With one-hundred tiles, and not necessarily using all of the tiles at one time, it is +possible to form forty-one different square laminae. + +Using up to one million tiles how many different square laminae can be formed? +""" + + +from math import ceil, sqrt + + +def solution(limit: int = 1000000) -> int: + """ + Return the number of different square laminae that can be formed using up to + one million tiles. + >>> solution(100) + 41 + """ + answer = 0 + + for outer_width in range(3, (limit // 4) + 2): + if outer_width ** 2 > limit: + hole_width_lower_bound = max(ceil(sqrt(outer_width ** 2 - limit)), 1) + else: + hole_width_lower_bound = 1 + if (outer_width - hole_width_lower_bound) % 2: + hole_width_lower_bound += 1 + + answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 + + return answer + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_174/__init__.py b/project_euler/problem_174/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_174/sol1.py b/project_euler/problem_174/sol1.py new file mode 100644 index 000000000000..cbc0df5a9d65 --- /dev/null +++ b/project_euler/problem_174/sol1.py @@ -0,0 +1,52 @@ +""" +Project Euler Problem 174: https://projecteuler.net/problem=174 + +We shall define a square lamina to be a square outline with a square "hole" so that +the shape possesses vertical and horizontal symmetry. + +Given eight tiles it is possible to form a lamina in only one way: 3x3 square with a +1x1 hole in the middle. However, using thirty-two tiles it is possible to form two +distinct laminae. + +If t represents the number of tiles used, we shall say that t = 8 is type L(1) and +t = 32 is type L(2). + +Let N(n) be the number of t ≤ 1000000 such that t is type L(n); for example, +N(15) = 832. + +What is ∑ N(n) for 1 ≤ n ≤ 10? +""" + +from collections import defaultdict +from math import ceil, sqrt + + +def solution(t_limit: int = 1000000, n_limit: int = 10) -> int: + """ + Return the sum of N(n) for 1 <= n <= n_limit. + + >>> solution(1000,5) + 249 + >>> solution(10000,10) + 2383 + """ + count: defaultdict = defaultdict(int) + + for outer_width in range(3, (t_limit // 4) + 2): + if outer_width * outer_width > t_limit: + hole_width_lower_bound = max( + ceil(sqrt(outer_width * outer_width - t_limit)), 1 + ) + else: + hole_width_lower_bound = 1 + + hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 + + for hole_width in range(hole_width_lower_bound, outer_width - 1, 2): + count[outer_width * outer_width - hole_width * hole_width] += 1 + + return sum(1 for n in count.values() if 1 <= n <= 10) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_180/__init__.py b/project_euler/problem_180/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_180/sol1.py b/project_euler/problem_180/sol1.py new file mode 100644 index 000000000000..6112db2ea370 --- /dev/null +++ b/project_euler/problem_180/sol1.py @@ -0,0 +1,174 @@ +""" +Project Euler Problem 234: https://projecteuler.net/problem=234 + +For any integer n, consider the three functions + +f1,n(x,y,z) = x^(n+1) + y^(n+1) - z^(n+1) +f2,n(x,y,z) = (xy + yz + zx)*(x^(n-1) + y^(n-1) - z^(n-1)) +f3,n(x,y,z) = xyz*(xn-2 + yn-2 - zn-2) + +and their combination + +fn(x,y,z) = f1,n(x,y,z) + f2,n(x,y,z) - f3,n(x,y,z) + +We call (x,y,z) a golden triple of order k if x, y, and z are all rational numbers +of the form a / b with 0 < a < b ≤ k and there is (at least) one integer n, +so that fn(x,y,z) = 0. + +Let s(x,y,z) = x + y + z. +Let t = u / v be the sum of all distinct s(x,y,z) for all golden triples +(x,y,z) of order 35. +All the s(x,y,z) and t must be in reduced form. + +Find u + v. + + +Solution: + +By expanding the brackets it is easy to show that +fn(x, y, z) = (x + y + z) * (x^n + y^n - z^n). + +Since x,y,z are positive, the requirement fn(x, y, z) = 0 is fulfilled if and +only if x^n + y^n = z^n. + +By Fermat's Last Theorem, this means that the absolute value of n can not +exceed 2, i.e. n is in {-2, -1, 0, 1, 2}. We can eliminate n = 0 since then the +equation would reduce to 1 + 1 = 1, for which there are no solutions. + +So all we have to do is iterate through the possible numerators and denominators +of x and y, calculate the corresponding z, and check if the corresponding numerator and +denominator are integer and satisfy 0 < z_num < z_den <= 0. We use a set "uniquq_s" +to make sure there are no duplicates, and the fractions.Fraction class to make sure +we get the right numerator and denominator. + +Reference: +https://en.wikipedia.org/wiki/Fermat%27s_Last_Theorem +""" + + +from fractions import Fraction +from math import gcd, sqrt +from typing import Tuple + + +def is_sq(number: int) -> bool: + """ + Check if number is a perfect square. + + >>> is_sq(1) + True + >>> is_sq(1000001) + False + >>> is_sq(1000000) + True + """ + sq: int = int(number ** 0.5) + return number == sq * sq + + +def add_three( + x_num: int, x_den: int, y_num: int, y_den: int, z_num: int, z_den: int +) -> Tuple[int, int]: + """ + Given the numerators and denominators of three fractions, return the + numerator and denominator of their sum in lowest form. + >>> add_three(1, 3, 1, 3, 1, 3) + (1, 1) + >>> add_three(2, 5, 4, 11, 12, 3) + (262, 55) + """ + top: int = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den + bottom: int = x_den * y_den * z_den + hcf: int = gcd(top, bottom) + top //= hcf + bottom //= hcf + return top, bottom + + +def solution(order: int = 35) -> int: + """ + Find the sum of the numerator and denominator of the sum of all s(x,y,z) for + golden triples (x,y,z) of the given order. + + >>> solution(5) + 296 + >>> solution(10) + 12519 + >>> solution(20) + 19408891927 + """ + unique_s: set = set() + hcf: int + total: Fraction = Fraction(0) + fraction_sum: Tuple[int, int] + + for x_num in range(1, order + 1): + for x_den in range(x_num + 1, order + 1): + for y_num in range(1, order + 1): + for y_den in range(y_num + 1, order + 1): + # n=1 + z_num = x_num * y_den + x_den * y_num + z_den = x_den * y_den + hcf = gcd(z_num, z_den) + z_num //= hcf + z_den //= hcf + if 0 < z_num < z_den <= order: + fraction_sum = add_three( + x_num, x_den, y_num, y_den, z_num, z_den + ) + unique_s.add(fraction_sum) + + # n=2 + z_num = ( + x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num + ) + z_den = x_den * x_den * y_den * y_den + if is_sq(z_num) and is_sq(z_den): + z_num = int(sqrt(z_num)) + z_den = int(sqrt(z_den)) + hcf = gcd(z_num, z_den) + z_num //= hcf + z_den //= hcf + if 0 < z_num < z_den <= order: + fraction_sum = add_three( + x_num, x_den, y_num, y_den, z_num, z_den + ) + unique_s.add(fraction_sum) + + # n=-1 + z_num = x_num * y_num + z_den = x_den * y_num + x_num * y_den + hcf = gcd(z_num, z_den) + z_num //= hcf + z_den //= hcf + if 0 < z_num < z_den <= order: + fraction_sum = add_three( + x_num, x_den, y_num, y_den, z_num, z_den + ) + unique_s.add(fraction_sum) + + # n=2 + z_num = x_num * x_num * y_num * y_num + z_den = ( + x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den + ) + if is_sq(z_num) and is_sq(z_den): + z_num = int(sqrt(z_num)) + z_den = int(sqrt(z_den)) + hcf = gcd(z_num, z_den) + z_num //= hcf + z_den //= hcf + if 0 < z_num < z_den <= order: + fraction_sum = add_three( + x_num, x_den, y_num, y_den, z_num, z_den + ) + unique_s.add(fraction_sum) + + for num, den in unique_s: + total += Fraction(num, den) + + return total.denominator + total.numerator + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_188/__init__.py b/project_euler/problem_188/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_188/sol1.py b/project_euler/problem_188/sol1.py new file mode 100644 index 000000000000..6473c63620ed --- /dev/null +++ b/project_euler/problem_188/sol1.py @@ -0,0 +1,68 @@ +""" +Project Euler Problem 188: https://projecteuler.net/problem=188 + +The hyperexponentiation of a number + +The hyperexponentiation or tetration of a number a by a positive integer b, +denoted by a↑↑b or b^a, is recursively defined by: + +a↑↑1 = a, +a↑↑(k+1) = a(a↑↑k). + +Thus we have e.g. 3↑↑2 = 3^3 = 27, hence 3↑↑3 = 3^27 = 7625597484987 and +3↑↑4 is roughly 103.6383346400240996*10^12. + +Find the last 8 digits of 1777↑↑1855. + +References: + - https://en.wikipedia.org/wiki/Tetration +""" + + +# small helper function for modular exponentiation +def _modexpt(base: int, exponent: int, modulo_value: int) -> int: + """ + Returns the modular exponentiation, that is the value + of `base ** exponent % modulo_value`, without calculating + the actual number. + >>> _modexpt(2, 4, 10) + 6 + >>> _modexpt(2, 1024, 100) + 16 + >>> _modexpt(13, 65535, 7) + 6 + """ + + if exponent == 1: + return base + if exponent % 2 == 0: + x = _modexpt(base, exponent / 2, modulo_value) % modulo_value + return (x * x) % modulo_value + else: + return (base * _modexpt(base, exponent - 1, modulo_value)) % modulo_value + + +def solution(base: int = 1777, height: int = 1855, digits: int = 8) -> int: + """ + Returns the last 8 digits of the hyperexponentiation of base by + height, i.e. the number base↑↑height: + + >>> solution(base=3, height=2) + 27 + >>> solution(base=3, height=3) + 97484987 + >>> solution(base=123, height=456, digits=4) + 2547 + """ + + # calculate base↑↑height by right-assiciative repeated modular + # exponentiation + result = base + for i in range(1, height): + result = _modexpt(base, result, 10 ** digits) + + return result + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_19/sol1.py b/project_euler/problem_19/sol1.py deleted file mode 100644 index 94cf117026a4..000000000000 --- a/project_euler/problem_19/sol1.py +++ /dev/null @@ -1,51 +0,0 @@ -from __future__ import print_function -''' -Counting Sundays -Problem 19 - -You are given the following information, but you may prefer to do some research for yourself. - -1 Jan 1900 was a Monday. -Thirty days has September, -April, June and November. -All the rest have thirty-one, -Saving February alone, -Which has twenty-eight, rain or shine. -And on leap years, twenty-nine. - -A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. - -How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? -''' - -days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - -day = 6 -month = 1 -year = 1901 - -sundays = 0 - -while year < 2001: - day += 7 - - if (year%4 == 0 and not year%100 == 0) or (year%400 == 0): - if day > days_per_month[month-1] and month is not 2: - month += 1 - day = day-days_per_month[month-2] - elif day > 29 and month is 2: - month += 1 - day = day-29 - else: - if day > days_per_month[month-1]: - month += 1 - day = day-days_per_month[month-2] - - if month > 12: - year += 1 - month = 1 - - if year < 2001 and day is 1: - sundays += 1 - -print(sundays) \ No newline at end of file diff --git a/project_euler/problem_191/__init__.py b/project_euler/problem_191/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_191/sol1.py b/project_euler/problem_191/sol1.py new file mode 100644 index 000000000000..38325b363b89 --- /dev/null +++ b/project_euler/problem_191/sol1.py @@ -0,0 +1,105 @@ +""" +Prize Strings +Problem 191 + +A particular school offers cash rewards to children with good attendance and +punctuality. If they are absent for three consecutive days or late on more +than one occasion then they forfeit their prize. + +During an n-day period a trinary string is formed for each child consisting +of L's (late), O's (on time), and A's (absent). + +Although there are eighty-one trinary strings for a 4-day period that can be +formed, exactly forty-three strings would lead to a prize: + +OOOO OOOA OOOL OOAO OOAA OOAL OOLO OOLA OAOO OAOA +OAOL OAAO OAAL OALO OALA OLOO OLOA OLAO OLAA AOOO +AOOA AOOL AOAO AOAA AOAL AOLO AOLA AAOO AAOA AAOL +AALO AALA ALOO ALOA ALAO ALAA LOOO LOOA LOAO LOAA +LAOO LAOA LAAO + +How many "prize" strings exist over a 30-day period? + +References: + - The original Project Euler project page: + https://projecteuler.net/problem=191 +""" + + +cache = {} + + +def _calculate(days: int, absent: int, late: int) -> int: + """ + A small helper function for the recursion, mainly to have + a clean interface for the solution() function below. + + It should get called with the number of days (corresponding + to the desired length of the 'prize strings'), and the + initial values for the number of consecutive absent days and + number of total late days. + + >>> _calculate(days=4, absent=0, late=0) + 43 + >>> _calculate(days=30, absent=2, late=0) + 0 + >>> _calculate(days=30, absent=1, late=0) + 98950096 + """ + + # if we are absent twice, or late 3 consecutive days, + # no further prize strings are possible + if late == 3 or absent == 2: + return 0 + + # if we have no days left, and have not failed any other rules, + # we have a prize string + if days == 0: + return 1 + + # No easy solution, so now we need to do the recursive calculation + + # First, check if the combination is already in the cache, and + # if yes, return the stored value from there since we already + # know the number of possible prize strings from this point on + key = (days, absent, late) + if key in cache: + return cache[key] + + # now we calculate the three possible ways that can unfold from + # this point on, depending on our attendance today + + # 1) if we are late (but not absent), the "absent" counter stays as + # it is, but the "late" counter increases by one + state_late = _calculate(days - 1, absent, late + 1) + + # 2) if we are absent, the "absent" counter increases by 1, and the + # "late" counter resets to 0 + state_absent = _calculate(days - 1, absent + 1, 0) + + # 3) if we are on time, this resets the "late" counter and keeps the + # absent counter + state_ontime = _calculate(days - 1, absent, 0) + + prizestrings = state_late + state_absent + state_ontime + + cache[key] = prizestrings + return prizestrings + + +def solution(days: int = 30) -> int: + """ + Returns the number of possible prize strings for a particular number + of days, using a simple recursive function with caching to speed it up. + + >>> solution() + 1918080160 + >>> solution(4) + 43 + """ + + return _calculate(days, absent=0, late=0) + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_20/sol1.py b/project_euler/problem_20/sol1.py deleted file mode 100644 index 73e41d5cc8fa..000000000000 --- a/project_euler/problem_20/sol1.py +++ /dev/null @@ -1,27 +0,0 @@ -# Finding the factorial. -def factorial(n): - fact = 1 - for i in range(1,n+1): - fact *= i - return fact - -# Spliting the digits and adding it. -def split_and_add(number): - sum_of_digits = 0 - while(number>0): - last_digit = number % 10 - sum_of_digits += last_digit - number = int(number/10) # Removing the last_digit from the given number. - return sum_of_digits - -# Taking the user input. -number = int(input("Enter the Number: ")) - -# Assigning the factorial from the factorial function. -factorial = factorial(number) - -# Spliting and adding the factorial into answer. -answer = split_and_add(factorial) - -# Printing the answer. -print(answer) diff --git a/project_euler/problem_20/sol2.py b/project_euler/problem_20/sol2.py deleted file mode 100644 index bca9af9cb9ef..000000000000 --- a/project_euler/problem_20/sol2.py +++ /dev/null @@ -1,5 +0,0 @@ -from math import factorial -def main(): - print(sum([int(x) for x in str(factorial(100))])) -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/project_euler/problem_203/__init__.py b/project_euler/problem_203/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_203/sol1.py b/project_euler/problem_203/sol1.py new file mode 100644 index 000000000000..227b476da131 --- /dev/null +++ b/project_euler/problem_203/sol1.py @@ -0,0 +1,188 @@ +""" +Project Euler Problem 203: https://projecteuler.net/problem=203 + +The binomial coefficients (n k) can be arranged in triangular form, Pascal's +triangle, like this: + 1 + 1 1 + 1 2 1 + 1 3 3 1 + 1 4 6 4 1 + 1 5 10 10 5 1 + 1 6 15 20 15 6 1 +1 7 21 35 35 21 7 1 + ......... + +It can be seen that the first eight rows of Pascal's triangle contain twelve +distinct numbers: 1, 2, 3, 4, 5, 6, 7, 10, 15, 20, 21 and 35. + +A positive integer n is called squarefree if no square of a prime divides n. +Of the twelve distinct numbers in the first eight rows of Pascal's triangle, +all except 4 and 20 are squarefree. The sum of the distinct squarefree numbers +in the first eight rows is 105. + +Find the sum of the distinct squarefree numbers in the first 51 rows of +Pascal's triangle. + +References: +- https://en.wikipedia.org/wiki/Pascal%27s_triangle +""" + +import math +from typing import List, Set + + +def get_pascal_triangle_unique_coefficients(depth: int) -> Set[int]: + """ + Returns the unique coefficients of a Pascal's triangle of depth "depth". + + The coefficients of this triangle are symmetric. A further improvement to this + method could be to calculate the coefficients once per level. Nonetheless, + the current implementation is fast enough for the original problem. + + >>> get_pascal_triangle_unique_coefficients(1) + {1} + >>> get_pascal_triangle_unique_coefficients(2) + {1} + >>> get_pascal_triangle_unique_coefficients(3) + {1, 2} + >>> get_pascal_triangle_unique_coefficients(8) + {1, 2, 3, 4, 5, 6, 7, 35, 10, 15, 20, 21} + """ + coefficients = {1} + previous_coefficients = [1] + for step in range(2, depth + 1): + coefficients_begins_one = previous_coefficients + [0] + coefficients_ends_one = [0] + previous_coefficients + previous_coefficients = [] + for x, y in zip(coefficients_begins_one, coefficients_ends_one): + coefficients.add(x + y) + previous_coefficients.append(x + y) + return coefficients + + +def get_primes_squared(max_number: int) -> List[int]: + """ + Calculates all primes between 2 and round(sqrt(max_number)) and returns + them squared up. + + >>> get_primes_squared(2) + [] + >>> get_primes_squared(4) + [4] + >>> get_primes_squared(10) + [4, 9] + >>> get_primes_squared(100) + [4, 9, 25, 49] + """ + max_prime = round(math.sqrt(max_number)) + non_primes = set() + primes = [] + for num in range(2, max_prime + 1): + if num in non_primes: + continue + + counter = 2 + while num * counter <= max_prime: + non_primes.add(num * counter) + counter += 1 + + primes.append(num ** 2) + return primes + + +def get_squared_primes_to_use( + num_to_look: int, squared_primes: List[int], previous_index: int +) -> int: + """ + Returns an int indicating the last index on which squares of primes + in primes are lower than num_to_look. + + This method supposes that squared_primes is sorted in ascending order and that + each num_to_look is provided in ascending order as well. Under these + assumptions, it needs a previous_index parameter that tells what was + the index returned by the method for the previous num_to_look. + + If all the elements in squared_primes are greater than num_to_look, then the + method returns -1. + + >>> get_squared_primes_to_use(1, [4, 9, 16, 25], 0) + -1 + >>> get_squared_primes_to_use(4, [4, 9, 16, 25], 0) + 1 + >>> get_squared_primes_to_use(16, [4, 9, 16, 25], 1) + 3 + """ + idx = max(previous_index, 0) + + while idx < len(squared_primes) and squared_primes[idx] <= num_to_look: + idx += 1 + + if idx == 0 and squared_primes[idx] > num_to_look: + return -1 + + if idx == len(squared_primes) and squared_primes[-1] > num_to_look: + return -1 + + return idx + + +def get_squarefree( + unique_coefficients: Set[int], squared_primes: List[int] +) -> Set[int]: + """ + Calculates the squarefree numbers inside unique_coefficients given a + list of square of primes. + + Based on the definition of a non-squarefree number, then any non-squarefree + n can be decomposed as n = p*p*r, where p is positive prime number and r + is a positive integer. + + Under the previous formula, any coefficient that is lower than p*p is + squarefree as r cannot be negative. On the contrary, if any r exists such + that n = p*p*r, then the number is non-squarefree. + + >>> get_squarefree({1}, []) + set() + >>> get_squarefree({1, 2}, []) + set() + >>> get_squarefree({1, 2, 3, 4, 5, 6, 7, 35, 10, 15, 20, 21}, [4, 9, 25]) + {1, 2, 3, 5, 6, 7, 35, 10, 15, 21} + """ + + if len(squared_primes) == 0: + return set() + + non_squarefrees = set() + prime_squared_idx = 0 + for num in sorted(unique_coefficients): + prime_squared_idx = get_squared_primes_to_use( + num, squared_primes, prime_squared_idx + ) + if prime_squared_idx == -1: + continue + if any(num % prime == 0 for prime in squared_primes[:prime_squared_idx]): + non_squarefrees.add(num) + + return unique_coefficients.difference(non_squarefrees) + + +def solution(n: int = 51) -> int: + """ + Returns the sum of squarefrees for a given Pascal's Triangle of depth n. + + >>> solution(1) + 0 + >>> solution(8) + 105 + >>> solution(9) + 175 + """ + unique_coefficients = get_pascal_triangle_unique_coefficients(n) + primes = get_primes_squared(max(unique_coefficients)) + squarefrees = get_squarefree(unique_coefficients, primes) + return sum(squarefrees) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_206/__init__.py b/project_euler/problem_206/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_206/sol1.py b/project_euler/problem_206/sol1.py new file mode 100644 index 000000000000..ffac2b32aa77 --- /dev/null +++ b/project_euler/problem_206/sol1.py @@ -0,0 +1,74 @@ +""" +Project Euler Problem 206: https://projecteuler.net/problem=206 + +Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, +where each “_” is a single digit. + +----- + +Instead of computing every single permutation of that number and going +through a 10^9 search space, we can narrow it down considerably. + +If the square ends in a 0, then the square root must also end in a 0. Thus, +the last missing digit must be 0 and the square root is a multiple of 10. +We can narrow the search space down to the first 8 digits and multiply the +result of that by 10 at the end. + +Now the last digit is a 9, which can only happen if the square root ends +in a 3 or 7. From this point, we can try one of two different methods to find +the answer: + +1. Start at the lowest possible base number whose square would be in the +format, and count up. The base we would start at is 101010103, whose square is +the closest number to 10203040506070809. Alternate counting up by 4 and 6 so +the last digit of the base is always a 3 or 7. + +2. Start at the highest possible base number whose square would be in the +format, and count down. That base would be 138902663, whose square is the +closest number to 1929394959697989. Alternate counting down by 6 and 4 so the +last digit of the base is always a 3 or 7. + +The solution does option 2 because the answer happens to be much closer to the +starting point. +""" + + +def is_square_form(num: int) -> bool: + """ + Determines if num is in the form 1_2_3_4_5_6_7_8_9 + + >>> is_square_form(1) + False + >>> is_square_form(112233445566778899) + True + >>> is_square_form(123456789012345678) + False + """ + digit = 9 + + while num > 0: + if num % 10 != digit: + return False + num //= 100 + digit -= 1 + + return True + + +def solution() -> int: + """ + Returns the first integer whose square is of the form 1_2_3_4_5_6_7_8_9_0 + """ + num = 138902663 + + while not is_square_form(num * num): + if num % 10 == 3: + num -= 6 # (3 - 6) % 10 = 7 + else: + num -= 4 # (7 - 4) % 10 = 3 + + return num * 10 + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_207/__init__.py b/project_euler/problem_207/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_207/sol1.py b/project_euler/problem_207/sol1.py new file mode 100644 index 000000000000..fb901fde1624 --- /dev/null +++ b/project_euler/problem_207/sol1.py @@ -0,0 +1,98 @@ +""" + +Project Euler Problem 207: https://projecteuler.net/problem=207 + +Problem Statement: +For some positive integers k, there exists an integer partition of the form +4**t = 2**t + k, where 4**t, 2**t, and k are all positive integers and t is a real +number. The first two such partitions are 4**1 = 2**1 + 2 and +4**1.5849625... = 2**1.5849625... + 6. +Partitions where t is also an integer are called perfect. +For any m ≥ 1 let P(m) be the proportion of such partitions that are perfect with +k ≤ m. +Thus P(6) = 1/2. +In the following table are listed some values of P(m) + + P(5) = 1/1 + P(10) = 1/2 + P(15) = 2/3 + P(20) = 1/2 + P(25) = 1/2 + P(30) = 2/5 + ... + P(180) = 1/4 + P(185) = 3/13 + +Find the smallest m for which P(m) < 1/12345 + +Solution: +Equation 4**t = 2**t + k solved for t gives: + t = log2(sqrt(4*k+1)/2 + 1/2) +For t to be real valued, sqrt(4*k+1) must be an integer which is implemented in +function check_t_real(k). For a perfect partition t must be an integer. +To speed up significantly the search for partitions, instead of incrementing k by one +per iteration, the next valid k is found by k = (i**2 - 1) / 4 with an integer i and +k has to be a positive integer. If this is the case a partition is found. The partition +is perfect if t os an integer. The integer i is increased with increment 1 until the +proportion perfect partitions / total partitions drops under the given value. + +""" + +import math + + +def check_partition_perfect(positive_integer: int) -> bool: + """ + + Check if t = f(positive_integer) = log2(sqrt(4*positive_integer+1)/2 + 1/2) is a + real number. + + >>> check_partition_perfect(2) + True + + >>> check_partition_perfect(6) + False + + """ + + exponent = math.log2(math.sqrt(4 * positive_integer + 1) / 2 + 1 / 2) + + return exponent == int(exponent) + + +def solution(max_proportion: float = 1 / 12345) -> int: + """ + Find m for which the proportion of perfect partitions to total partitions is lower + than max_proportion + + >>> solution(1) > 5 + True + + >>> solution(1/2) > 10 + True + + >>> solution(3 / 13) > 185 + True + + """ + + total_partitions = 0 + perfect_partitions = 0 + + integer = 3 + while True: + partition_candidate = (integer ** 2 - 1) / 4 + # if candidate is an integer, then there is a partition for k + if partition_candidate == int(partition_candidate): + partition_candidate = int(partition_candidate) + total_partitions += 1 + if check_partition_perfect(partition_candidate): + perfect_partitions += 1 + if perfect_partitions > 0: + if perfect_partitions / total_partitions < max_proportion: + return partition_candidate + integer += 1 + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_21/sol1.py b/project_euler/problem_21/sol1.py deleted file mode 100644 index da29a5c7b631..000000000000 --- a/project_euler/problem_21/sol1.py +++ /dev/null @@ -1,30 +0,0 @@ -#-.- coding: latin-1 -.- -from __future__ import print_function -from math import sqrt -''' -Amicable Numbers -Problem 21 - -Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). -If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. - -For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. - -Evaluate the sum of all the amicable numbers under 10000. -''' -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -def sum_of_divisors(n): - total = 0 - for i in xrange(1, int(sqrt(n)+1)): - if n%i == 0 and i != sqrt(n): - total += i + n//i - elif i == sqrt(n): - total += i - return total-n - -total = [i for i in range(1,10000) if sum_of_divisors(sum_of_divisors(i)) == i and sum_of_divisors(i) != i] -print(sum(total)) diff --git a/project_euler/problem_22/sol1.py b/project_euler/problem_22/sol1.py deleted file mode 100644 index 7754306583dc..000000000000 --- a/project_euler/problem_22/sol1.py +++ /dev/null @@ -1,37 +0,0 @@ -# -*- coding: latin-1 -*- -from __future__ import print_function -''' -Name scores -Problem 22 - -Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it -into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list -to obtain a name score. - -For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. -So, COLIN would obtain a score of 938 × 53 = 49714. - -What is the total of all the name scores in the file? -''' -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -with open('p022_names.txt') as file: - names = str(file.readlines()[0]) - names = names.replace('"', '').split(',') - -names.sort() - -name_score = 0 -total_score = 0 - -for i, name in enumerate(names): - for letter in name: - name_score += ord(letter) - 64 - - total_score += (i+1)*name_score - name_score = 0 - -print(total_score) \ No newline at end of file diff --git a/project_euler/problem_22/sol2.py b/project_euler/problem_22/sol2.py deleted file mode 100644 index d7f9abf09d49..000000000000 --- a/project_euler/problem_22/sol2.py +++ /dev/null @@ -1,533 +0,0 @@ -def main(): - name = [ - "MARY", "PATRICIA", "LINDA", "BARBARA", "ELIZABETH", "JENNIFER", "MARIA", "SUSAN", "MARGARET", "DOROTHY", - "LISA", "NANCY", "KAREN", "BETTY", "HELEN", "SANDRA", "DONNA", "CAROL", "RUTH", "SHARON", - "MICHELLE", "LAURA", "SARAH", "KIMBERLY", "DEBORAH", "JESSICA", "SHIRLEY", "CYNTHIA", "ANGELA", "MELISSA", - "BRENDA", "AMY", "ANNA", "REBECCA", "VIRGINIA", "KATHLEEN", "PAMELA", "MARTHA", "DEBRA", "AMANDA", - "STEPHANIE", "CAROLYN", "CHRISTINE", "MARIE", "JANET", "CATHERINE", "FRANCES", "ANN", "JOYCE", "DIANE", - "ALICE", "JULIE", "HEATHER", "TERESA", "DORIS", "GLORIA", "EVELYN", "JEAN", "CHERYL", "MILDRED", - "KATHERINE", "JOAN", "ASHLEY", "JUDITH", "ROSE", "JANICE", "KELLY", "NICOLE", "JUDY", "CHRISTINA", - "KATHY", "THERESA", "BEVERLY", "DENISE", "TAMMY", "IRENE", "JANE", "LORI", "RACHEL", "MARILYN", - "ANDREA", "KATHRYN", "LOUISE", "SARA", "ANNE", "JACQUELINE", "WANDA", "BONNIE", "JULIA", "RUBY", - "LOIS", "TINA", "PHYLLIS", "NORMA", "PAULA", "DIANA", "ANNIE", "LILLIAN", "EMILY", "ROBIN", - "PEGGY", "CRYSTAL", "GLADYS", "RITA", "DAWN", "CONNIE", "FLORENCE", "TRACY", "EDNA", "TIFFANY", - "CARMEN", "ROSA", "CINDY", "GRACE", "WENDY", "VICTORIA", "EDITH", "KIM", "SHERRY", "SYLVIA", - "JOSEPHINE", "THELMA", "SHANNON", "SHEILA", "ETHEL", "ELLEN", "ELAINE", "MARJORIE", "CARRIE", "CHARLOTTE", - "MONICA", "ESTHER", "PAULINE", "EMMA", "JUANITA", "ANITA", "RHONDA", "HAZEL", "AMBER", "EVA", - "DEBBIE", "APRIL", "LESLIE", "CLARA", "LUCILLE", "JAMIE", "JOANNE", "ELEANOR", "VALERIE", "DANIELLE", - "MEGAN", "ALICIA", "SUZANNE", "MICHELE", "GAIL", "BERTHA", "DARLENE", "VERONICA", "JILL", "ERIN", - "GERALDINE", "LAUREN", "CATHY", "JOANN", "LORRAINE", "LYNN", "SALLY", "REGINA", "ERICA", "BEATRICE", - "DOLORES", "BERNICE", "AUDREY", "YVONNE", "ANNETTE", "JUNE", "SAMANTHA", "MARION", "DANA", "STACY", - "ANA", "RENEE", "IDA", "VIVIAN", "ROBERTA", "HOLLY", "BRITTANY", "MELANIE", "LORETTA", "YOLANDA", - "JEANETTE", "LAURIE", "KATIE", "KRISTEN", "VANESSA", "ALMA", "SUE", "ELSIE", "BETH", "JEANNE", - "VICKI", "CARLA", "TARA", "ROSEMARY", "EILEEN", "TERRI", "GERTRUDE", "LUCY", "TONYA", "ELLA", - "STACEY", "WILMA", "GINA", "KRISTIN", "JESSIE", "NATALIE", "AGNES", "VERA", "WILLIE", "CHARLENE", - "BESSIE", "DELORES", "MELINDA", "PEARL", "ARLENE", "MAUREEN", "COLLEEN", "ALLISON", "TAMARA", "JOY", - "GEORGIA", "CONSTANCE", "LILLIE", "CLAUDIA", "JACKIE", "MARCIA", "TANYA", "NELLIE", "MINNIE", "MARLENE", - "HEIDI", "GLENDA", "LYDIA", "VIOLA", "COURTNEY", "MARIAN", "STELLA", "CAROLINE", "DORA", "JO", - "VICKIE", "MATTIE", "TERRY", "MAXINE", "IRMA", "MABEL", "MARSHA", "MYRTLE", "LENA", "CHRISTY", - "DEANNA", "PATSY", "HILDA", "GWENDOLYN", "JENNIE", "NORA", "MARGIE", "NINA", "CASSANDRA", "LEAH", - "PENNY", "KAY", "PRISCILLA", "NAOMI", "CAROLE", "BRANDY", "OLGA", "BILLIE", "DIANNE", "TRACEY", - "LEONA", "JENNY", "FELICIA", "SONIA", "MIRIAM", "VELMA", "BECKY", "BOBBIE", "VIOLET", "KRISTINA", - "TONI", "MISTY", "MAE", "SHELLY", "DAISY", "RAMONA", "SHERRI", "ERIKA", "KATRINA", "CLAIRE", - "LINDSEY", "LINDSAY", "GENEVA", "GUADALUPE", "BELINDA", "MARGARITA", "SHERYL", "CORA", "FAYE", "ADA", - "NATASHA", "SABRINA", "ISABEL", "MARGUERITE", "HATTIE", "HARRIET", "MOLLY", "CECILIA", "KRISTI", "BRANDI", - "BLANCHE", "SANDY", "ROSIE", "JOANNA", "IRIS", "EUNICE", "ANGIE", "INEZ", "LYNDA", "MADELINE", - "AMELIA", "ALBERTA", "GENEVIEVE", "MONIQUE", "JODI", "JANIE", "MAGGIE", "KAYLA", "SONYA", "JAN", - "LEE", "KRISTINE", "CANDACE", "FANNIE", "MARYANN", "OPAL", "ALISON", "YVETTE", "MELODY", "LUZ", - "SUSIE", "OLIVIA", "FLORA", "SHELLEY", "KRISTY", "MAMIE", "LULA", "LOLA", "VERNA", "BEULAH", - "ANTOINETTE", "CANDICE", "JUANA", "JEANNETTE", "PAM", "KELLI", "HANNAH", "WHITNEY", "BRIDGET", "KARLA", - "CELIA", "LATOYA", "PATTY", "SHELIA", "GAYLE", "DELLA", "VICKY", "LYNNE", "SHERI", "MARIANNE", - "KARA", "JACQUELYN", "ERMA", "BLANCA", "MYRA", "LETICIA", "PAT", "KRISTA", "ROXANNE", "ANGELICA", - "JOHNNIE", "ROBYN", "FRANCIS", "ADRIENNE", "ROSALIE", "ALEXANDRA", "BROOKE", "BETHANY", "SADIE", "BERNADETTE", - "TRACI", "JODY", "KENDRA", "JASMINE", "NICHOLE", "RACHAEL", "CHELSEA", "MABLE", "ERNESTINE", "MURIEL", - "MARCELLA", "ELENA", "KRYSTAL", "ANGELINA", "NADINE", "KARI", "ESTELLE", "DIANNA", "PAULETTE", "LORA", - "MONA", "DOREEN", "ROSEMARIE", "ANGEL", "DESIREE", "ANTONIA", "HOPE", "GINGER", "JANIS", "BETSY", - "CHRISTIE", "FREDA", "MERCEDES", "MEREDITH", "LYNETTE", "TERI", "CRISTINA", "EULA", "LEIGH", "MEGHAN", - "SOPHIA", "ELOISE", "ROCHELLE", "GRETCHEN", "CECELIA", "RAQUEL", "HENRIETTA", "ALYSSA", "JANA", "KELLEY", - "GWEN", "KERRY", "JENNA", "TRICIA", "LAVERNE", "OLIVE", "ALEXIS", "TASHA", "SILVIA", "ELVIRA", - "CASEY", "DELIA", "SOPHIE", "KATE", "PATTI", "LORENA", "KELLIE", "SONJA", "LILA", "LANA", - "DARLA", "MAY", "MINDY", "ESSIE", "MANDY", "LORENE", "ELSA", "JOSEFINA", "JEANNIE", "MIRANDA", - "DIXIE", "LUCIA", "MARTA", "FAITH", "LELA", "JOHANNA", "SHARI", "CAMILLE", "TAMI", "SHAWNA", - "ELISA", "EBONY", "MELBA", "ORA", "NETTIE", "TABITHA", "OLLIE", "JAIME", "WINIFRED", "KRISTIE", - "MARINA", "ALISHA", "AIMEE", "RENA", "MYRNA", "MARLA", "TAMMIE", "LATASHA", "BONITA", "PATRICE", - "RONDA", "SHERRIE", "ADDIE", "FRANCINE", "DELORIS", "STACIE", "ADRIANA", "CHERI", "SHELBY", "ABIGAIL", - "CELESTE", "JEWEL", "CARA", "ADELE", "REBEKAH", "LUCINDA", "DORTHY", "CHRIS", "EFFIE", "TRINA", - "REBA", "SHAWN", "SALLIE", "AURORA", "LENORA", "ETTA", "LOTTIE", "KERRI", "TRISHA", "NIKKI", - "ESTELLA", "FRANCISCA", "JOSIE", "TRACIE", "MARISSA", "KARIN", "BRITTNEY", "JANELLE", "LOURDES", "LAUREL", - "HELENE", "FERN", "ELVA", "CORINNE", "KELSEY", "INA", "BETTIE", "ELISABETH", "AIDA", "CAITLIN", - "INGRID", "IVA", "EUGENIA", "CHRISTA", "GOLDIE", "CASSIE", "MAUDE", "JENIFER", "THERESE", "FRANKIE", - "DENA", "LORNA", "JANETTE", "LATONYA", "CANDY", "MORGAN", "CONSUELO", "TAMIKA", "ROSETTA", "DEBORA", - "CHERIE", "POLLY", "DINA", "JEWELL", "FAY", "JILLIAN", "DOROTHEA", "NELL", "TRUDY", "ESPERANZA", - "PATRICA", "KIMBERLEY", "SHANNA", "HELENA", "CAROLINA", "CLEO", "STEFANIE", "ROSARIO", "OLA", "JANINE", - "MOLLIE", "LUPE", "ALISA", "LOU", "MARIBEL", "SUSANNE", "BETTE", "SUSANA", "ELISE", "CECILE", - "ISABELLE", "LESLEY", "JOCELYN", "PAIGE", "JONI", "RACHELLE", "LEOLA", "DAPHNE", "ALTA", "ESTER", - "PETRA", "GRACIELA", "IMOGENE", "JOLENE", "KEISHA", "LACEY", "GLENNA", "GABRIELA", "KERI", "URSULA", - "LIZZIE", "KIRSTEN", "SHANA", "ADELINE", "MAYRA", "JAYNE", "JACLYN", "GRACIE", "SONDRA", "CARMELA", - "MARISA", "ROSALIND", "CHARITY", "TONIA", "BEATRIZ", "MARISOL", "CLARICE", "JEANINE", "SHEENA", "ANGELINE", - "FRIEDA", "LILY", "ROBBIE", "SHAUNA", "MILLIE", "CLAUDETTE", "CATHLEEN", "ANGELIA", "GABRIELLE", "AUTUMN", - "KATHARINE", "SUMMER", "JODIE", "STACI", "LEA", "CHRISTI", "JIMMIE", "JUSTINE", "ELMA", "LUELLA", - "MARGRET", "DOMINIQUE", "SOCORRO", "RENE", "MARTINA", "MARGO", "MAVIS", "CALLIE", "BOBBI", "MARITZA", - "LUCILE", "LEANNE", "JEANNINE", "DEANA", "AILEEN", "LORIE", "LADONNA", "WILLA", "MANUELA", "GALE", - "SELMA", "DOLLY", "SYBIL", "ABBY", "LARA", "DALE", "IVY", "DEE", "WINNIE", "MARCY", - "LUISA", "JERI", "MAGDALENA", "OFELIA", "MEAGAN", "AUDRA", "MATILDA", "LEILA", "CORNELIA", "BIANCA", - "SIMONE", "BETTYE", "RANDI", "VIRGIE", "LATISHA", "BARBRA", "GEORGINA", "ELIZA", "LEANN", "BRIDGETTE", - "RHODA", "HALEY", "ADELA", "NOLA", "BERNADINE", "FLOSSIE", "ILA", "GRETA", "RUTHIE", "NELDA", - "MINERVA", "LILLY", "TERRIE", "LETHA", "HILARY", "ESTELA", "VALARIE", "BRIANNA", "ROSALYN", "EARLINE", - "CATALINA", "AVA", "MIA", "CLARISSA", "LIDIA", "CORRINE", "ALEXANDRIA", "CONCEPCION", "TIA", "SHARRON", - "RAE", "DONA", "ERICKA", "JAMI", "ELNORA", "CHANDRA", "LENORE", "NEVA", "MARYLOU", "MELISA", - "TABATHA", "SERENA", "AVIS", "ALLIE", "SOFIA", "JEANIE", "ODESSA", "NANNIE", "HARRIETT", "LORAINE", - "PENELOPE", "MILAGROS", "EMILIA", "BENITA", "ALLYSON", "ASHLEE", "TANIA", "TOMMIE", "ESMERALDA", "KARINA", - "EVE", "PEARLIE", "ZELMA", "MALINDA", "NOREEN", "TAMEKA", "SAUNDRA", "HILLARY", "AMIE", "ALTHEA", - "ROSALINDA", "JORDAN", "LILIA", "ALANA", "GAY", "CLARE", "ALEJANDRA", "ELINOR", "MICHAEL", "LORRIE", - "JERRI", "DARCY", "EARNESTINE", "CARMELLA", "TAYLOR", "NOEMI", "MARCIE", "LIZA", "ANNABELLE", "LOUISA", - "EARLENE", "MALLORY", "CARLENE", "NITA", "SELENA", "TANISHA", "KATY", "JULIANNE", "JOHN", "LAKISHA", - "EDWINA", "MARICELA", "MARGERY", "KENYA", "DOLLIE", "ROXIE", "ROSLYN", "KATHRINE", "NANETTE", "CHARMAINE", - "LAVONNE", "ILENE", "KRIS", "TAMMI", "SUZETTE", "CORINE", "KAYE", "JERRY", "MERLE", "CHRYSTAL", - "LINA", "DEANNE", "LILIAN", "JULIANA", "ALINE", "LUANN", "KASEY", "MARYANNE", "EVANGELINE", "COLETTE", - "MELVA", "LAWANDA", "YESENIA", "NADIA", "MADGE", "KATHIE", "EDDIE", "OPHELIA", "VALERIA", "NONA", - "MITZI", "MARI", "GEORGETTE", "CLAUDINE", "FRAN", "ALISSA", "ROSEANN", "LAKEISHA", "SUSANNA", "REVA", - "DEIDRE", "CHASITY", "SHEREE", "CARLY", "JAMES", "ELVIA", "ALYCE", "DEIRDRE", "GENA", "BRIANA", - "ARACELI", "KATELYN", "ROSANNE", "WENDI", "TESSA", "BERTA", "MARVA", "IMELDA", "MARIETTA", "MARCI", - "LEONOR", "ARLINE", "SASHA", "MADELYN", "JANNA", "JULIETTE", "DEENA", "AURELIA", "JOSEFA", "AUGUSTA", - "LILIANA", "YOUNG", "CHRISTIAN", "LESSIE", "AMALIA", "SAVANNAH", "ANASTASIA", "VILMA", "NATALIA", "ROSELLA", - "LYNNETTE", "CORINA", "ALFREDA", "LEANNA", "CAREY", "AMPARO", "COLEEN", "TAMRA", "AISHA", "WILDA", - "KARYN", "CHERRY", "QUEEN", "MAURA", "MAI", "EVANGELINA", "ROSANNA", "HALLIE", "ERNA", "ENID", - "MARIANA", "LACY", "JULIET", "JACKLYN", "FREIDA", "MADELEINE", "MARA", "HESTER", "CATHRYN", "LELIA", - "CASANDRA", "BRIDGETT", "ANGELITA", "JANNIE", "DIONNE", "ANNMARIE", "KATINA", "BERYL", "PHOEBE", "MILLICENT", - "KATHERYN", "DIANN", "CARISSA", "MARYELLEN", "LIZ", "LAURI", "HELGA", "GILDA", "ADRIAN", "RHEA", - "MARQUITA", "HOLLIE", "TISHA", "TAMERA", "ANGELIQUE", "FRANCESCA", "BRITNEY", "KAITLIN", "LOLITA", "FLORINE", - "ROWENA", "REYNA", "TWILA", "FANNY", "JANELL", "INES", "CONCETTA", "BERTIE", "ALBA", "BRIGITTE", - "ALYSON", "VONDA", "PANSY", "ELBA", "NOELLE", "LETITIA", "KITTY", "DEANN", "BRANDIE", "LOUELLA", - "LETA", "FELECIA", "SHARLENE", "LESA", "BEVERLEY", "ROBERT", "ISABELLA", "HERMINIA", "TERRA", "CELINA", - "TORI", "OCTAVIA", "JADE", "DENICE", "GERMAINE", "SIERRA", "MICHELL", "CORTNEY", "NELLY", "DORETHA", - "SYDNEY", "DEIDRA", "MONIKA", "LASHONDA", "JUDI", "CHELSEY", "ANTIONETTE", "MARGOT", "BOBBY", "ADELAIDE", - "NAN", "LEEANN", "ELISHA", "DESSIE", "LIBBY", "KATHI", "GAYLA", "LATANYA", "MINA", "MELLISA", - "KIMBERLEE", "JASMIN", "RENAE", "ZELDA", "ELDA", "MA", "JUSTINA", "GUSSIE", "EMILIE", "CAMILLA", - "ABBIE", "ROCIO", "KAITLYN", "JESSE", "EDYTHE", "ASHLEIGH", "SELINA", "LAKESHA", "GERI", "ALLENE", - "PAMALA", "MICHAELA", "DAYNA", "CARYN", "ROSALIA", "SUN", "JACQULINE", "REBECA", "MARYBETH", "KRYSTLE", - "IOLA", "DOTTIE", "BENNIE", "BELLE", "AUBREY", "GRISELDA", "ERNESTINA", "ELIDA", "ADRIANNE", "DEMETRIA", - "DELMA", "CHONG", "JAQUELINE", "DESTINY", "ARLEEN", "VIRGINA", "RETHA", "FATIMA", "TILLIE", "ELEANORE", - "CARI", "TREVA", "BIRDIE", "WILHELMINA", "ROSALEE", "MAURINE", "LATRICE", "YONG", "JENA", "TARYN", - "ELIA", "DEBBY", "MAUDIE", "JEANNA", "DELILAH", "CATRINA", "SHONDA", "HORTENCIA", "THEODORA", "TERESITA", - "ROBBIN", "DANETTE", "MARYJANE", "FREDDIE", "DELPHINE", "BRIANNE", "NILDA", "DANNA", "CINDI", "BESS", - "IONA", "HANNA", "ARIEL", "WINONA", "VIDA", "ROSITA", "MARIANNA", "WILLIAM", "RACHEAL", "GUILLERMINA", - "ELOISA", "CELESTINE", "CAREN", "MALISSA", "LONA", "CHANTEL", "SHELLIE", "MARISELA", "LEORA", "AGATHA", - "SOLEDAD", "MIGDALIA", "IVETTE", "CHRISTEN", "ATHENA", "JANEL", "CHLOE", "VEDA", "PATTIE", "TESSIE", - "TERA", "MARILYNN", "LUCRETIA", "KARRIE", "DINAH", "DANIELA", "ALECIA", "ADELINA", "VERNICE", "SHIELA", - "PORTIA", "MERRY", "LASHAWN", "DEVON", "DARA", "TAWANA", "OMA", "VERDA", "CHRISTIN", "ALENE", - "ZELLA", "SANDI", "RAFAELA", "MAYA", "KIRA", "CANDIDA", "ALVINA", "SUZAN", "SHAYLA", "LYN", - "LETTIE", "ALVA", "SAMATHA", "ORALIA", "MATILDE", "MADONNA", "LARISSA", "VESTA", "RENITA", "INDIA", - "DELOIS", "SHANDA", "PHILLIS", "LORRI", "ERLINDA", "CRUZ", "CATHRINE", "BARB", "ZOE", "ISABELL", - "IONE", "GISELA", "CHARLIE", "VALENCIA", "ROXANNA", "MAYME", "KISHA", "ELLIE", "MELLISSA", "DORRIS", - "DALIA", "BELLA", "ANNETTA", "ZOILA", "RETA", "REINA", "LAURETTA", "KYLIE", "CHRISTAL", "PILAR", - "CHARLA", "ELISSA", "TIFFANI", "TANA", "PAULINA", "LEOTA", "BREANNA", "JAYME", "CARMEL", "VERNELL", - "TOMASA", "MANDI", "DOMINGA", "SANTA", "MELODIE", "LURA", "ALEXA", "TAMELA", "RYAN", "MIRNA", - "KERRIE", "VENUS", "NOEL", "FELICITA", "CRISTY", "CARMELITA", "BERNIECE", "ANNEMARIE", "TIARA", "ROSEANNE", - "MISSY", "CORI", "ROXANA", "PRICILLA", "KRISTAL", "JUNG", "ELYSE", "HAYDEE", "ALETHA", "BETTINA", - "MARGE", "GILLIAN", "FILOMENA", "CHARLES", "ZENAIDA", "HARRIETTE", "CARIDAD", "VADA", "UNA", "ARETHA", - "PEARLINE", "MARJORY", "MARCELA", "FLOR", "EVETTE", "ELOUISE", "ALINA", "TRINIDAD", "DAVID", "DAMARIS", - "CATHARINE", "CARROLL", "BELVA", "NAKIA", "MARLENA", "LUANNE", "LORINE", "KARON", "DORENE", "DANITA", - "BRENNA", "TATIANA", "SAMMIE", "LOUANN", "LOREN", "JULIANNA", "ANDRIA", "PHILOMENA", "LUCILA", "LEONORA", - "DOVIE", "ROMONA", "MIMI", "JACQUELIN", "GAYE", "TONJA", "MISTI", "JOE", "GENE", "CHASTITY", - "STACIA", "ROXANN", "MICAELA", "NIKITA", "MEI", "VELDA", "MARLYS", "JOHNNA", "AURA", "LAVERN", - "IVONNE", "HAYLEY", "NICKI", "MAJORIE", "HERLINDA", "GEORGE", "ALPHA", "YADIRA", "PERLA", "GREGORIA", - "DANIEL", "ANTONETTE", "SHELLI", "MOZELLE", "MARIAH", "JOELLE", "CORDELIA", "JOSETTE", "CHIQUITA", "TRISTA", - "LOUIS", "LAQUITA", "GEORGIANA", "CANDI", "SHANON", "LONNIE", "HILDEGARD", "CECIL", "VALENTINA", "STEPHANY", - "MAGDA", "KAROL", "GERRY", "GABRIELLA", "TIANA", "ROMA", "RICHELLE", "RAY", "PRINCESS", "OLETA", - "JACQUE", "IDELLA", "ALAINA", "SUZANNA", "JOVITA", "BLAIR", "TOSHA", "RAVEN", "NEREIDA", "MARLYN", - "KYLA", "JOSEPH", "DELFINA", "TENA", "STEPHENIE", "SABINA", "NATHALIE", "MARCELLE", "GERTIE", "DARLEEN", - "THEA", "SHARONDA", "SHANTEL", "BELEN", "VENESSA", "ROSALINA", "ONA", "GENOVEVA", "COREY", "CLEMENTINE", - "ROSALBA", "RENATE", "RENATA", "MI", "IVORY", "GEORGIANNA", "FLOY", "DORCAS", "ARIANA", "TYRA", - "THEDA", "MARIAM", "JULI", "JESICA", "DONNIE", "VIKKI", "VERLA", "ROSELYN", "MELVINA", "JANNETTE", - "GINNY", "DEBRAH", "CORRIE", "ASIA", "VIOLETA", "MYRTIS", "LATRICIA", "COLLETTE", "CHARLEEN", "ANISSA", - "VIVIANA", "TWYLA", "PRECIOUS", "NEDRA", "LATONIA", "LAN", "HELLEN", "FABIOLA", "ANNAMARIE", "ADELL", - "SHARYN", "CHANTAL", "NIKI", "MAUD", "LIZETTE", "LINDY", "KIA", "KESHA", "JEANA", "DANELLE", - "CHARLINE", "CHANEL", "CARROL", "VALORIE", "LIA", "DORTHA", "CRISTAL", "SUNNY", "LEONE", "LEILANI", - "GERRI", "DEBI", "ANDRA", "KESHIA", "IMA", "EULALIA", "EASTER", "DULCE", "NATIVIDAD", "LINNIE", - "KAMI", "GEORGIE", "CATINA", "BROOK", "ALDA", "WINNIFRED", "SHARLA", "RUTHANN", "MEAGHAN", "MAGDALENE", - "LISSETTE", "ADELAIDA", "VENITA", "TRENA", "SHIRLENE", "SHAMEKA", "ELIZEBETH", "DIAN", "SHANTA", "MICKEY", - "LATOSHA", "CARLOTTA", "WINDY", "SOON", "ROSINA", "MARIANN", "LEISA", "JONNIE", "DAWNA", "CATHIE", - "BILLY", "ASTRID", "SIDNEY", "LAUREEN", "JANEEN", "HOLLI", "FAWN", "VICKEY", "TERESSA", "SHANTE", - "RUBYE", "MARCELINA", "CHANDA", "CARY", "TERESE", "SCARLETT", "MARTY", "MARNIE", "LULU", "LISETTE", - "JENIFFER", "ELENOR", "DORINDA", "DONITA", "CARMAN", "BERNITA", "ALTAGRACIA", "ALETA", "ADRIANNA", "ZORAIDA", - "RONNIE", "NICOLA", "LYNDSEY", "KENDALL", "JANINA", "CHRISSY", "AMI", "STARLA", "PHYLIS", "PHUONG", - "KYRA", "CHARISSE", "BLANCH", "SANJUANITA", "RONA", "NANCI", "MARILEE", "MARANDA", "CORY", "BRIGETTE", - "SANJUANA", "MARITA", "KASSANDRA", "JOYCELYN", "IRA", "FELIPA", "CHELSIE", "BONNY", "MIREYA", "LORENZA", - "KYONG", "ILEANA", "CANDELARIA", "TONY", "TOBY", "SHERIE", "OK", "MARK", "LUCIE", "LEATRICE", - "LAKESHIA", "GERDA", "EDIE", "BAMBI", "MARYLIN", "LAVON", "HORTENSE", "GARNET", "EVIE", "TRESSA", - "SHAYNA", "LAVINA", "KYUNG", "JEANETTA", "SHERRILL", "SHARA", "PHYLISS", "MITTIE", "ANABEL", "ALESIA", - "THUY", "TAWANDA", "RICHARD", "JOANIE", "TIFFANIE", "LASHANDA", "KARISSA", "ENRIQUETA", "DARIA", "DANIELLA", - "CORINNA", "ALANNA", "ABBEY", "ROXANE", "ROSEANNA", "MAGNOLIA", "LIDA", "KYLE", "JOELLEN", "ERA", - "CORAL", "CARLEEN", "TRESA", "PEGGIE", "NOVELLA", "NILA", "MAYBELLE", "JENELLE", "CARINA", "NOVA", - "MELINA", "MARQUERITE", "MARGARETTE", "JOSEPHINA", "EVONNE", "DEVIN", "CINTHIA", "ALBINA", "TOYA", "TAWNYA", - "SHERITA", "SANTOS", "MYRIAM", "LIZABETH", "LISE", "KEELY", "JENNI", "GISELLE", "CHERYLE", "ARDITH", - "ARDIS", "ALESHA", "ADRIANE", "SHAINA", "LINNEA", "KAROLYN", "HONG", "FLORIDA", "FELISHA", "DORI", - "DARCI", "ARTIE", "ARMIDA", "ZOLA", "XIOMARA", "VERGIE", "SHAMIKA", "NENA", "NANNETTE", "MAXIE", - "LOVIE", "JEANE", "JAIMIE", "INGE", "FARRAH", "ELAINA", "CAITLYN", "STARR", "FELICITAS", "CHERLY", - "CARYL", "YOLONDA", "YASMIN", "TEENA", "PRUDENCE", "PENNIE", "NYDIA", "MACKENZIE", "ORPHA", "MARVEL", - "LIZBETH", "LAURETTE", "JERRIE", "HERMELINDA", "CAROLEE", "TIERRA", "MIRIAN", "META", "MELONY", "KORI", - "JENNETTE", "JAMILA", "ENA", "ANH", "YOSHIKO", "SUSANNAH", "SALINA", "RHIANNON", "JOLEEN", "CRISTINE", - "ASHTON", "ARACELY", "TOMEKA", "SHALONDA", "MARTI", "LACIE", "KALA", "JADA", "ILSE", "HAILEY", - "BRITTANI", "ZONA", "SYBLE", "SHERRYL", "RANDY", "NIDIA", "MARLO", "KANDICE", "KANDI", "DEB", - "DEAN", "AMERICA", "ALYCIA", "TOMMY", "RONNA", "NORENE", "MERCY", "JOSE", "INGEBORG", "GIOVANNA", - "GEMMA", "CHRISTEL", "AUDRY", "ZORA", "VITA", "VAN", "TRISH", "STEPHAINE", "SHIRLEE", "SHANIKA", - "MELONIE", "MAZIE", "JAZMIN", "INGA", "HOA", "HETTIE", "GERALYN", "FONDA", "ESTRELLA", "ADELLA", - "SU", "SARITA", "RINA", "MILISSA", "MARIBETH", "GOLDA", "EVON", "ETHELYN", "ENEDINA", "CHERISE", - "CHANA", "VELVA", "TAWANNA", "SADE", "MIRTA", "LI", "KARIE", "JACINTA", "ELNA", "DAVINA", - "CIERRA", "ASHLIE", "ALBERTHA", "TANESHA", "STEPHANI", "NELLE", "MINDI", "LU", "LORINDA", "LARUE", - "FLORENE", "DEMETRA", "DEDRA", "CIARA", "CHANTELLE", "ASHLY", "SUZY", "ROSALVA", "NOELIA", "LYDA", - "LEATHA", "KRYSTYNA", "KRISTAN", "KARRI", "DARLINE", "DARCIE", "CINDA", "CHEYENNE", "CHERRIE", "AWILDA", - "ALMEDA", "ROLANDA", "LANETTE", "JERILYN", "GISELE", "EVALYN", "CYNDI", "CLETA", "CARIN", "ZINA", - "ZENA", "VELIA", "TANIKA", "PAUL", "CHARISSA", "THOMAS", "TALIA", "MARGARETE", "LAVONDA", "KAYLEE", - "KATHLENE", "JONNA", "IRENA", "ILONA", "IDALIA", "CANDIS", "CANDANCE", "BRANDEE", "ANITRA", "ALIDA", - "SIGRID", "NICOLETTE", "MARYJO", "LINETTE", "HEDWIG", "CHRISTIANA", "CASSIDY", "ALEXIA", "TRESSIE", "MODESTA", - "LUPITA", "LITA", "GLADIS", "EVELIA", "DAVIDA", "CHERRI", "CECILY", "ASHELY", "ANNABEL", "AGUSTINA", - "WANITA", "SHIRLY", "ROSAURA", "HULDA", "EUN", "BAILEY", "YETTA", "VERONA", "THOMASINA", "SIBYL", - "SHANNAN", "MECHELLE", "LUE", "LEANDRA", "LANI", "KYLEE", "KANDY", "JOLYNN", "FERNE", "EBONI", - "CORENE", "ALYSIA", "ZULA", "NADA", "MOIRA", "LYNDSAY", "LORRETTA", "JUAN", "JAMMIE", "HORTENSIA", - "GAYNELL", "CAMERON", "ADRIA", "VINA", "VICENTA", "TANGELA", "STEPHINE", "NORINE", "NELLA", "LIANA", - "LESLEE", "KIMBERELY", "ILIANA", "GLORY", "FELICA", "EMOGENE", "ELFRIEDE", "EDEN", "EARTHA", "CARMA", - "BEA", "OCIE", "MARRY", "LENNIE", "KIARA", "JACALYN", "CARLOTA", "ARIELLE", "YU", "STAR", - "OTILIA", "KIRSTIN", "KACEY", "JOHNETTA", "JOEY", "JOETTA", "JERALDINE", "JAUNITA", "ELANA", "DORTHEA", - "CAMI", "AMADA", "ADELIA", "VERNITA", "TAMAR", "SIOBHAN", "RENEA", "RASHIDA", "OUIDA", "ODELL", - "NILSA", "MERYL", "KRISTYN", "JULIETA", "DANICA", "BREANNE", "AUREA", "ANGLEA", "SHERRON", "ODETTE", - "MALIA", "LORELEI", "LIN", "LEESA", "KENNA", "KATHLYN", "FIONA", "CHARLETTE", "SUZIE", "SHANTELL", - "SABRA", "RACQUEL", "MYONG", "MIRA", "MARTINE", "LUCIENNE", "LAVADA", "JULIANN", "JOHNIE", "ELVERA", - "DELPHIA", "CLAIR", "CHRISTIANE", "CHAROLETTE", "CARRI", "AUGUSTINE", "ASHA", "ANGELLA", "PAOLA", "NINFA", - "LEDA", "LAI", "EDA", "SUNSHINE", "STEFANI", "SHANELL", "PALMA", "MACHELLE", "LISSA", "KECIA", - "KATHRYNE", "KARLENE", "JULISSA", "JETTIE", "JENNIFFER", "HUI", "CORRINA", "CHRISTOPHER", "CAROLANN", "ALENA", - "TESS", "ROSARIA", "MYRTICE", "MARYLEE", "LIANE", "KENYATTA", "JUDIE", "JANEY", "IN", "ELMIRA", - "ELDORA", "DENNA", "CRISTI", "CATHI", "ZAIDA", "VONNIE", "VIVA", "VERNIE", "ROSALINE", "MARIELA", - "LUCIANA", "LESLI", "KARAN", "FELICE", "DENEEN", "ADINA", "WYNONA", "TARSHA", "SHERON", "SHASTA", - "SHANITA", "SHANI", "SHANDRA", "RANDA", "PINKIE", "PARIS", "NELIDA", "MARILOU", "LYLA", "LAURENE", - "LACI", "JOI", "JANENE", "DOROTHA", "DANIELE", "DANI", "CAROLYNN", "CARLYN", "BERENICE", "AYESHA", - "ANNELIESE", "ALETHEA", "THERSA", "TAMIKO", "RUFINA", "OLIVA", "MOZELL", "MARYLYN", "MADISON", "KRISTIAN", - "KATHYRN", "KASANDRA", "KANDACE", "JANAE", "GABRIEL", "DOMENICA", "DEBBRA", "DANNIELLE", "CHUN", "BUFFY", - "BARBIE", "ARCELIA", "AJA", "ZENOBIA", "SHAREN", "SHAREE", "PATRICK", "PAGE", "MY", "LAVINIA", - "KUM", "KACIE", "JACKELINE", "HUONG", "FELISA", "EMELIA", "ELEANORA", "CYTHIA", "CRISTIN", "CLYDE", - "CLARIBEL", "CARON", "ANASTACIA", "ZULMA", "ZANDRA", "YOKO", "TENISHA", "SUSANN", "SHERILYN", "SHAY", - "SHAWANDA", "SABINE", "ROMANA", "MATHILDA", "LINSEY", "KEIKO", "JOANA", "ISELA", "GRETTA", "GEORGETTA", - "EUGENIE", "DUSTY", "DESIRAE", "DELORA", "CORAZON", "ANTONINA", "ANIKA", "WILLENE", "TRACEE", "TAMATHA", - "REGAN", "NICHELLE", "MICKIE", "MAEGAN", "LUANA", "LANITA", "KELSIE", "EDELMIRA", "BREE", "AFTON", - "TEODORA", "TAMIE", "SHENA", "MEG", "LINH", "KELI", "KACI", "DANYELLE", "BRITT", "ARLETTE", - "ALBERTINE", "ADELLE", "TIFFINY", "STORMY", "SIMONA", "NUMBERS", "NICOLASA", "NICHOL", "NIA", "NAKISHA", - "MEE", "MAIRA", "LOREEN", "KIZZY", "JOHNNY", "JAY", "FALLON", "CHRISTENE", "BOBBYE", "ANTHONY", - "YING", "VINCENZA", "TANJA", "RUBIE", "RONI", "QUEENIE", "MARGARETT", "KIMBERLI", "IRMGARD", "IDELL", - "HILMA", "EVELINA", "ESTA", "EMILEE", "DENNISE", "DANIA", "CARL", "CARIE", "ANTONIO", "WAI", - "SANG", "RISA", "RIKKI", "PARTICIA", "MUI", "MASAKO", "MARIO", "LUVENIA", "LOREE", "LONI", - "LIEN", "KEVIN", "GIGI", "FLORENCIA", "DORIAN", "DENITA", "DALLAS", "CHI", "BILLYE", "ALEXANDER", - "TOMIKA", "SHARITA", "RANA", "NIKOLE", "NEOMA", "MARGARITE", "MADALYN", "LUCINA", "LAILA", "KALI", - "JENETTE", "GABRIELE", "EVELYNE", "ELENORA", "CLEMENTINA", "ALEJANDRINA", "ZULEMA", "VIOLETTE", "VANNESSA", "THRESA", - "RETTA", "PIA", "PATIENCE", "NOELLA", "NICKIE", "JONELL", "DELTA", "CHUNG", "CHAYA", "CAMELIA", - "BETHEL", "ANYA", "ANDREW", "THANH", "SUZANN", "SPRING", "SHU", "MILA", "LILLA", "LAVERNA", - "KEESHA", "KATTIE", "GIA", "GEORGENE", "EVELINE", "ESTELL", "ELIZBETH", "VIVIENNE", "VALLIE", "TRUDIE", - "STEPHANE", "MICHEL", "MAGALY", "MADIE", "KENYETTA", "KARREN", "JANETTA", "HERMINE", "HARMONY", "DRUCILLA", - "DEBBI", "CELESTINA", "CANDIE", "BRITNI", "BECKIE", "AMINA", "ZITA", "YUN", "YOLANDE", "VIVIEN", - "VERNETTA", "TRUDI", "SOMMER", "PEARLE", "PATRINA", "OSSIE", "NICOLLE", "LOYCE", "LETTY", "LARISA", - "KATHARINA", "JOSELYN", "JONELLE", "JENELL", "IESHA", "HEIDE", "FLORINDA", "FLORENTINA", "FLO", "ELODIA", - "DORINE", "BRUNILDA", "BRIGID", "ASHLI", "ARDELLA", "TWANA", "THU", "TARAH", "SUNG", "SHEA", - "SHAVON", "SHANE", "SERINA", "RAYNA", "RAMONITA", "NGA", "MARGURITE", "LUCRECIA", "KOURTNEY", "KATI", - "JESUS", "JESENIA", "DIAMOND", "CRISTA", "AYANA", "ALICA", "ALIA", "VINNIE", "SUELLEN", "ROMELIA", - "RACHELL", "PIPER", "OLYMPIA", "MICHIKO", "KATHALEEN", "JOLIE", "JESSI", "JANESSA", "HANA", "HA", - "ELEASE", "CARLETTA", "BRITANY", "SHONA", "SALOME", "ROSAMOND", "REGENA", "RAINA", "NGOC", "NELIA", - "LOUVENIA", "LESIA", "LATRINA", "LATICIA", "LARHONDA", "JINA", "JACKI", "HOLLIS", "HOLLEY", "EMMY", - "DEEANN", "CORETTA", "ARNETTA", "VELVET", "THALIA", "SHANICE", "NETA", "MIKKI", "MICKI", "LONNA", - "LEANA", "LASHUNDA", "KILEY", "JOYE", "JACQULYN", "IGNACIA", "HYUN", "HIROKO", "HENRY", "HENRIETTE", - "ELAYNE", "DELINDA", "DARNELL", "DAHLIA", "COREEN", "CONSUELA", "CONCHITA", "CELINE", "BABETTE", "AYANNA", - "ANETTE", "ALBERTINA", "SKYE", "SHAWNEE", "SHANEKA", "QUIANA", "PAMELIA", "MIN", "MERRI", "MERLENE", - "MARGIT", "KIESHA", "KIERA", "KAYLENE", "JODEE", "JENISE", "ERLENE", "EMMIE", "ELSE", "DARYL", - "DALILA", "DAISEY", "CODY", "CASIE", "BELIA", "BABARA", "VERSIE", "VANESA", "SHELBA", "SHAWNDA", - "SAM", "NORMAN", "NIKIA", "NAOMA", "MARNA", "MARGERET", "MADALINE", "LAWANA", "KINDRA", "JUTTA", - "JAZMINE", "JANETT", "HANNELORE", "GLENDORA", "GERTRUD", "GARNETT", "FREEDA", "FREDERICA", "FLORANCE", "FLAVIA", - "DENNIS", "CARLINE", "BEVERLEE", "ANJANETTE", "VALDA", "TRINITY", "TAMALA", "STEVIE", "SHONNA", "SHA", - "SARINA", "ONEIDA", "MICAH", "MERILYN", "MARLEEN", "LURLINE", "LENNA", "KATHERIN", "JIN", "JENI", - "HAE", "GRACIA", "GLADY", "FARAH", "ERIC", "ENOLA", "EMA", "DOMINQUE", "DEVONA", "DELANA", - "CECILA", "CAPRICE", "ALYSHA", "ALI", "ALETHIA", "VENA", "THERESIA", "TAWNY", "SONG", "SHAKIRA", - "SAMARA", "SACHIKO", "RACHELE", "PAMELLA", "NICKY", "MARNI", "MARIEL", "MAREN", "MALISA", "LIGIA", - "LERA", "LATORIA", "LARAE", "KIMBER", "KATHERN", "KAREY", "JENNEFER", "JANETH", "HALINA", "FREDIA", - "DELISA", "DEBROAH", "CIERA", "CHIN", "ANGELIKA", "ANDREE", "ALTHA", "YEN", "VIVAN", "TERRESA", - "TANNA", "SUK", "SUDIE", "SOO", "SIGNE", "SALENA", "RONNI", "REBBECCA", "MYRTIE", "MCKENZIE", - "MALIKA", "MAIDA", "LOAN", "LEONARDA", "KAYLEIGH", "FRANCE", "ETHYL", "ELLYN", "DAYLE", "CAMMIE", - "BRITTNI", "BIRGIT", "AVELINA", "ASUNCION", "ARIANNA", "AKIKO", "VENICE", "TYESHA", "TONIE", "TIESHA", - "TAKISHA", "STEFFANIE", "SINDY", "SANTANA", "MEGHANN", "MANDA", "MACIE", "LADY", "KELLYE", "KELLEE", - "JOSLYN", "JASON", "INGER", "INDIRA", "GLINDA", "GLENNIS", "FERNANDA", "FAUSTINA", "ENEIDA", "ELICIA", - "DOT", "DIGNA", "DELL", "ARLETTA", "ANDRE", "WILLIA", "TAMMARA", "TABETHA", "SHERRELL", "SARI", - "REFUGIO", "REBBECA", "PAULETTA", "NIEVES", "NATOSHA", "NAKITA", "MAMMIE", "KENISHA", "KAZUKO", "KASSIE", - "GARY", "EARLEAN", "DAPHINE", "CORLISS", "CLOTILDE", "CAROLYNE", "BERNETTA", "AUGUSTINA", "AUDREA", "ANNIS", - "ANNABELL", "YAN", "TENNILLE", "TAMICA", "SELENE", "SEAN", "ROSANA", "REGENIA", "QIANA", "MARKITA", - "MACY", "LEEANNE", "LAURINE", "KYM", "JESSENIA", "JANITA", "GEORGINE", "GENIE", "EMIKO", "ELVIE", - "DEANDRA", "DAGMAR", "CORIE", "COLLEN", "CHERISH", "ROMAINE", "PORSHA", "PEARLENE", "MICHELINE", "MERNA", - "MARGORIE", "MARGARETTA", "LORE", "KENNETH", "JENINE", "HERMINA", "FREDERICKA", "ELKE", "DRUSILLA", "DORATHY", - "DIONE", "DESIRE", "CELENA", "BRIGIDA", "ANGELES", "ALLEGRA", "THEO", "TAMEKIA", "SYNTHIA", "STEPHEN", - "SOOK", "SLYVIA", "ROSANN", "REATHA", "RAYE", "MARQUETTA", "MARGART", "LING", "LAYLA", "KYMBERLY", - "KIANA", "KAYLEEN", "KATLYN", "KARMEN", "JOELLA", "IRINA", "EMELDA", "ELENI", "DETRA", "CLEMMIE", - "CHERYLL", "CHANTELL", "CATHEY", "ARNITA", "ARLA", "ANGLE", "ANGELIC", "ALYSE", "ZOFIA", "THOMASINE", - "TENNIE", "SON", "SHERLY", "SHERLEY", "SHARYL", "REMEDIOS", "PETRINA", "NICKOLE", "MYUNG", "MYRLE", - "MOZELLA", "LOUANNE", "LISHA", "LATIA", "LANE", "KRYSTA", "JULIENNE", "JOEL", "JEANENE", "JACQUALINE", - "ISAURA", "GWENDA", "EARLEEN", "DONALD", "CLEOPATRA", "CARLIE", "AUDIE", "ANTONIETTA", "ALISE", "ALEX", - "VERDELL", "VAL", "TYLER", "TOMOKO", "THAO", "TALISHA", "STEVEN", "SO", "SHEMIKA", "SHAUN", - "SCARLET", "SAVANNA", "SANTINA", "ROSIA", "RAEANN", "ODILIA", "NANA", "MINNA", "MAGAN", "LYNELLE", - "LE", "KARMA", "JOEANN", "IVANA", "INELL", "ILANA", "HYE", "HONEY", "HEE", "GUDRUN", - "FRANK", "DREAMA", "CRISSY", "CHANTE", "CARMELINA", "ARVILLA", "ARTHUR", "ANNAMAE", "ALVERA", "ALEIDA", - "AARON", "YEE", "YANIRA", "VANDA", "TIANNA", "TAM", "STEFANIA", "SHIRA", "PERRY", "NICOL", - "NANCIE", "MONSERRATE", "MINH", "MELYNDA", "MELANY", "MATTHEW", "LOVELLA", "LAURE", "KIRBY", "KACY", - "JACQUELYNN", "HYON", "GERTHA", "FRANCISCO", "ELIANA", "CHRISTENA", "CHRISTEEN", "CHARISE", "CATERINA", "CARLEY", - "CANDYCE", "ARLENA", "AMMIE", "YANG", "WILLETTE", "VANITA", "TUYET", "TINY", "SYREETA", "SILVA", - "SCOTT", "RONALD", "PENNEY", "NYLA", "MICHAL", "MAURICE", "MARYAM", "MARYA", "MAGEN", "LUDIE", - "LOMA", "LIVIA", "LANELL", "KIMBERLIE", "JULEE", "DONETTA", "DIEDRA", "DENISHA", "DEANE", "DAWNE", - "CLARINE", "CHERRYL", "BRONWYN", "BRANDON", "ALLA", "VALERY", "TONDA", "SUEANN", "SORAYA", "SHOSHANA", - "SHELA", "SHARLEEN", "SHANELLE", "NERISSA", "MICHEAL", "MERIDITH", "MELLIE", "MAYE", "MAPLE", "MAGARET", - "LUIS", "LILI", "LEONILA", "LEONIE", "LEEANNA", "LAVONIA", "LAVERA", "KRISTEL", "KATHEY", "KATHE", - "JUSTIN", "JULIAN", "JIMMY", "JANN", "ILDA", "HILDRED", "HILDEGARDE", "GENIA", "FUMIKO", "EVELIN", - "ERMELINDA", "ELLY", "DUNG", "DOLORIS", "DIONNA", "DANAE", "BERNEICE", "ANNICE", "ALIX", "VERENA", - "VERDIE", "TRISTAN", "SHAWNNA", "SHAWANA", "SHAUNNA", "ROZELLA", "RANDEE", "RANAE", "MILAGRO", "LYNELL", - "LUISE", "LOUIE", "LOIDA", "LISBETH", "KARLEEN", "JUNITA", "JONA", "ISIS", "HYACINTH", "HEDY", - "GWENN", "ETHELENE", "ERLINE", "EDWARD", "DONYA", "DOMONIQUE", "DELICIA", "DANNETTE", "CICELY", "BRANDA", - "BLYTHE", "BETHANN", "ASHLYN", "ANNALEE", "ALLINE", "YUKO", "VELLA", "TRANG", "TOWANDA", "TESHA", - "SHERLYN", "NARCISA", "MIGUELINA", "MERI", "MAYBELL", "MARLANA", "MARGUERITA", "MADLYN", "LUNA", "LORY", - "LORIANN", "LIBERTY", "LEONORE", "LEIGHANN", "LAURICE", "LATESHA", "LARONDA", "KATRICE", "KASIE", "KARL", - "KALEY", "JADWIGA", "GLENNIE", "GEARLDINE", "FRANCINA", "EPIFANIA", "DYAN", "DORIE", "DIEDRE", "DENESE", - "DEMETRICE", "DELENA", "DARBY", "CRISTIE", "CLEORA", "CATARINA", "CARISA", "BERNIE", "BARBERA", "ALMETA", - "TRULA", "TEREASA", "SOLANGE", "SHEILAH", "SHAVONNE", "SANORA", "ROCHELL", "MATHILDE", "MARGARETA", "MAIA", - "LYNSEY", "LAWANNA", "LAUNA", "KENA", "KEENA", "KATIA", "JAMEY", "GLYNDA", "GAYLENE", "ELVINA", - "ELANOR", "DANUTA", "DANIKA", "CRISTEN", "CORDIE", "COLETTA", "CLARITA", "CARMON", "BRYNN", "AZUCENA", - "AUNDREA", "ANGELE", "YI", "WALTER", "VERLIE", "VERLENE", "TAMESHA", "SILVANA", "SEBRINA", "SAMIRA", - "REDA", "RAYLENE", "PENNI", "PANDORA", "NORAH", "NOMA", "MIREILLE", "MELISSIA", "MARYALICE", "LARAINE", - "KIMBERY", "KARYL", "KARINE", "KAM", "JOLANDA", "JOHANA", "JESUSA", "JALEESA", "JAE", "JACQUELYNE", - "IRISH", "ILUMINADA", "HILARIA", "HANH", "GENNIE", "FRANCIE", "FLORETTA", "EXIE", "EDDA", "DREMA", - "DELPHA", "BEV", "BARBAR", "ASSUNTA", "ARDELL", "ANNALISA", "ALISIA", "YUKIKO", "YOLANDO", "WONDA", - "WEI", "WALTRAUD", "VETA", "TEQUILA", "TEMEKA", "TAMEIKA", "SHIRLEEN", "SHENITA", "PIEDAD", "OZELLA", - "MIRTHA", "MARILU", "KIMIKO", "JULIANE", "JENICE", "JEN", "JANAY", "JACQUILINE", "HILDE", "FE", - "FAE", "EVAN", "EUGENE", "ELOIS", "ECHO", "DEVORAH", "CHAU", "BRINDA", "BETSEY", "ARMINDA", - "ARACELIS", "APRYL", "ANNETT", "ALISHIA", "VEOLA", "USHA", "TOSHIKO", "THEOLA", "TASHIA", "TALITHA", - "SHERY", "RUDY", "RENETTA", "REIKO", "RASHEEDA", "OMEGA", "OBDULIA", "MIKA", "MELAINE", "MEGGAN", - "MARTIN", "MARLEN", "MARGET", "MARCELINE", "MANA", "MAGDALEN", "LIBRADA", "LEZLIE", "LEXIE", "LATASHIA", - "LASANDRA", "KELLE", "ISIDRA", "ISA", "INOCENCIA", "GWYN", "FRANCOISE", "ERMINIA", "ERINN", "DIMPLE", - "DEVORA", "CRISELDA", "ARMANDA", "ARIE", "ARIANE", "ANGELO", "ANGELENA", "ALLEN", "ALIZA", "ADRIENE", - "ADALINE", "XOCHITL", "TWANNA", "TRAN", "TOMIKO", "TAMISHA", "TAISHA", "SUSY", "SIU", "RUTHA", - "ROXY", "RHONA", "RAYMOND", "OTHA", "NORIKO", "NATASHIA", "MERRIE", "MELVIN", "MARINDA", "MARIKO", - "MARGERT", "LORIS", "LIZZETTE", "LEISHA", "KAILA", "KA", "JOANNIE", "JERRICA", "JENE", "JANNET", - "JANEE", "JACINDA", "HERTA", "ELENORE", "DORETTA", "DELAINE", "DANIELL", "CLAUDIE", "CHINA", "BRITTA", - "APOLONIA", "AMBERLY", "ALEASE", "YURI", "YUK", "WEN", "WANETA", "UTE", "TOMI", "SHARRI", - "SANDIE", "ROSELLE", "REYNALDA", "RAGUEL", "PHYLICIA", "PATRIA", "OLIMPIA", "ODELIA", "MITZIE", "MITCHELL", - "MISS", "MINDA", "MIGNON", "MICA", "MENDY", "MARIVEL", "MAILE", "LYNETTA", "LAVETTE", "LAURYN", - "LATRISHA", "LAKIESHA", "KIERSTEN", "KARY", "JOSPHINE", "JOLYN", "JETTA", "JANISE", "JACQUIE", "IVELISSE", - "GLYNIS", "GIANNA", "GAYNELLE", "EMERALD", "DEMETRIUS", "DANYELL", "DANILLE", "DACIA", "CORALEE", "CHER", - "CEOLA", "BRETT", "BELL", "ARIANNE", "ALESHIA", "YUNG", "WILLIEMAE", "TROY", "TRINH", "THORA", - "TAI", "SVETLANA", "SHERIKA", "SHEMEKA", "SHAUNDA", "ROSELINE", "RICKI", "MELDA", "MALLIE", "LAVONNA", - "LATINA", "LARRY", "LAQUANDA", "LALA", "LACHELLE", "KLARA", "KANDIS", "JOHNA", "JEANMARIE", "JAYE", - "HANG", "GRAYCE", "GERTUDE", "EMERITA", "EBONIE", "CLORINDA", "CHING", "CHERY", "CAROLA", "BREANN", - "BLOSSOM", "BERNARDINE", "BECKI", "ARLETHA", "ARGELIA", "ARA", "ALITA", "YULANDA", "YON", "YESSENIA", - "TOBI", "TASIA", "SYLVIE", "SHIRL", "SHIRELY", "SHERIDAN", "SHELLA", "SHANTELLE", "SACHA", "ROYCE", - "REBECKA", "REAGAN", "PROVIDENCIA", "PAULENE", "MISHA", "MIKI", "MARLINE", "MARICA", "LORITA", "LATOYIA", - "LASONYA", "KERSTIN", "KENDA", "KEITHA", "KATHRIN", "JAYMIE", "JACK", "GRICELDA", "GINETTE", "ERYN", - "ELINA", "ELFRIEDA", "DANYEL", "CHEREE", "CHANELLE", "BARRIE", "AVERY", "AURORE", "ANNAMARIA", "ALLEEN", - "AILENE", "AIDE", "YASMINE", "VASHTI", "VALENTINE", "TREASA", "TORY", "TIFFANEY", "SHERYLL", "SHARIE", - "SHANAE", "SAU", "RAISA", "PA", "NEDA", "MITSUKO", "MIRELLA", "MILDA", "MARYANNA", "MARAGRET", - "MABELLE", "LUETTA", "LORINA", "LETISHA", "LATARSHA", "LANELLE", "LAJUANA", "KRISSY", "KARLY", "KARENA", - "JON", "JESSIKA", "JERICA", "JEANELLE", "JANUARY", "JALISA", "JACELYN", "IZOLA", "IVEY", "GREGORY", - "EUNA", "ETHA", "DREW", "DOMITILA", "DOMINICA", "DAINA", "CREOLA", "CARLI", "CAMIE", "BUNNY", - "BRITTNY", "ASHANTI", "ANISHA", "ALEEN", "ADAH", "YASUKO", "WINTER", "VIKI", "VALRIE", "TONA", - "TINISHA", "THI", "TERISA", "TATUM", "TANEKA", "SIMONNE", "SHALANDA", "SERITA", "RESSIE", "REFUGIA", - "PAZ", "OLENE", "NA", "MERRILL", "MARGHERITA", "MANDIE", "MAN", "MAIRE", "LYNDIA", "LUCI", - "LORRIANE", "LORETA", "LEONIA", "LAVONA", "LASHAWNDA", "LAKIA", "KYOKO", "KRYSTINA", "KRYSTEN", "KENIA", - "KELSI", "JUDE", "JEANICE", "ISOBEL", "GEORGIANN", "GENNY", "FELICIDAD", "EILENE", "DEON", "DELOISE", - "DEEDEE", "DANNIE", "CONCEPTION", "CLORA", "CHERILYN", "CHANG", "CALANDRA", "BERRY", "ARMANDINA", "ANISA", - "ULA", "TIMOTHY", "TIERA", "THERESSA", "STEPHANIA", "SIMA", "SHYLA", "SHONTA", "SHERA", "SHAQUITA", - "SHALA", "SAMMY", "ROSSANA", "NOHEMI", "NERY", "MORIAH", "MELITA", "MELIDA", "MELANI", "MARYLYNN", - "MARISHA", "MARIETTE", "MALORIE", "MADELENE", "LUDIVINA", "LORIA", "LORETTE", "LORALEE", "LIANNE", "LEON", - "LAVENIA", "LAURINDA", "LASHON", "KIT", "KIMI", "KEILA", "KATELYNN", "KAI", "JONE", "JOANE", - "JI", "JAYNA", "JANELLA", "JA", "HUE", "HERTHA", "FRANCENE", "ELINORE", "DESPINA", "DELSIE", - "DEEDRA", "CLEMENCIA", "CARRY", "CAROLIN", "CARLOS", "BULAH", "BRITTANIE", "BOK", "BLONDELL", "BIBI", - "BEAULAH", "BEATA", "ANNITA", "AGRIPINA", "VIRGEN", "VALENE", "UN", "TWANDA", "TOMMYE", "TOI", - "TARRA", "TARI", "TAMMERA", "SHAKIA", "SADYE", "RUTHANNE", "ROCHEL", "RIVKA", "PURA", "NENITA", - "NATISHA", "MING", "MERRILEE", "MELODEE", "MARVIS", "LUCILLA", "LEENA", "LAVETA", "LARITA", "LANIE", - "KEREN", "ILEEN", "GEORGEANN", "GENNA", "GENESIS", "FRIDA", "EWA", "EUFEMIA", "EMELY", "ELA", - "EDYTH", "DEONNA", "DEADRA", "DARLENA", "CHANELL", "CHAN", "CATHERN", "CASSONDRA", "CASSAUNDRA", "BERNARDA", - "BERNA", "ARLINDA", "ANAMARIA", "ALBERT", "WESLEY", "VERTIE", "VALERI", "TORRI", "TATYANA", "STASIA", - "SHERISE", "SHERILL", "SEASON", "SCOTTIE", "SANDA", "RUTHE", "ROSY", "ROBERTO", "ROBBI", "RANEE", - "QUYEN", "PEARLY", "PALMIRA", "ONITA", "NISHA", "NIESHA", "NIDA", "NEVADA", "NAM", "MERLYN", - "MAYOLA", "MARYLOUISE", "MARYLAND", "MARX", "MARTH", "MARGENE", "MADELAINE", "LONDA", "LEONTINE", "LEOMA", - "LEIA", "LAWRENCE", "LAURALEE", "LANORA", "LAKITA", "KIYOKO", "KETURAH", "KATELIN", "KAREEN", "JONIE", - "JOHNETTE", "JENEE", "JEANETT", "IZETTA", "HIEDI", "HEIKE", "HASSIE", "HAROLD", "GIUSEPPINA", "GEORGANN", - "FIDELA", "FERNANDE", "ELWANDA", "ELLAMAE", "ELIZ", "DUSTI", "DOTTY", "CYNDY", "CORALIE", "CELESTA", - "ARGENTINA", "ALVERTA", "XENIA", "WAVA", "VANETTA", "TORRIE", "TASHINA", "TANDY", "TAMBRA", "TAMA", - "STEPANIE", "SHILA", "SHAUNTA", "SHARAN", "SHANIQUA", "SHAE", "SETSUKO", "SERAFINA", "SANDEE", "ROSAMARIA", - "PRISCILA", "OLINDA", "NADENE", "MUOI", "MICHELINA", "MERCEDEZ", "MARYROSE", "MARIN", "MARCENE", "MAO", - "MAGALI", "MAFALDA", "LOGAN", "LINN", "LANNIE", "KAYCE", "KAROLINE", "KAMILAH", "KAMALA", "JUSTA", - "JOLINE", "JENNINE", "JACQUETTA", "IRAIDA", "GERALD", "GEORGEANNA", "FRANCHESCA", "FAIRY", "EMELINE", "ELANE", - "EHTEL", "EARLIE", "DULCIE", "DALENE", "CRIS", "CLASSIE", "CHERE", "CHARIS", "CAROYLN", "CARMINA", - "CARITA", "BRIAN", "BETHANIE", "AYAKO", "ARICA", "AN", "ALYSA", "ALESSANDRA", "AKILAH", "ADRIEN", - "ZETTA", "YOULANDA", "YELENA", "YAHAIRA", "XUAN", "WENDOLYN", "VICTOR", "TIJUANA", "TERRELL", "TERINA", - "TERESIA", "SUZI", "SUNDAY", "SHERELL", "SHAVONDA", "SHAUNTE", "SHARDA", "SHAKITA", "SENA", "RYANN", - "RUBI", "RIVA", "REGINIA", "REA", "RACHAL", "PARTHENIA", "PAMULA", "MONNIE", "MONET", "MICHAELE", - "MELIA", "MARINE", "MALKA", "MAISHA", "LISANDRA", "LEO", "LEKISHA", "LEAN", "LAURENCE", "LAKENDRA", - "KRYSTIN", "KORTNEY", "KIZZIE", "KITTIE", "KERA", "KENDAL", "KEMBERLY", "KANISHA", "JULENE", "JULE", - "JOSHUA", "JOHANNE", "JEFFREY", "JAMEE", "HAN", "HALLEY", "GIDGET", "GALINA", "FREDRICKA", "FLETA", - "FATIMAH", "EUSEBIA", "ELZA", "ELEONORE", "DORTHEY", "DORIA", "DONELLA", "DINORAH", "DELORSE", "CLARETHA", - "CHRISTINIA", "CHARLYN", "BONG", "BELKIS", "AZZIE", "ANDERA", "AIKO", "ADENA", "YER", "YAJAIRA", - "WAN", "VANIA", "ULRIKE", "TOSHIA", "TIFANY", "STEFANY", "SHIZUE", "SHENIKA", "SHAWANNA", "SHAROLYN", - "SHARILYN", "SHAQUANA", "SHANTAY", "SEE", "ROZANNE", "ROSELEE", "RICKIE", "REMONA", "REANNA", "RAELENE", - "QUINN", "PHUNG", "PETRONILA", "NATACHA", "NANCEY", "MYRL", "MIYOKO", "MIESHA", "MERIDETH", "MARVELLA", - "MARQUITTA", "MARHTA", "MARCHELLE", "LIZETH", "LIBBIE", "LAHOMA", "LADAWN", "KINA", "KATHELEEN", "KATHARYN", - "KARISA", "KALEIGH", "JUNIE", "JULIEANN", "JOHNSIE", "JANEAN", "JAIMEE", "JACKQUELINE", "HISAKO", "HERMA", - "HELAINE", "GWYNETH", "GLENN", "GITA", "EUSTOLIA", "EMELINA", "ELIN", "EDRIS", "DONNETTE", "DONNETTA", - "DIERDRE", "DENAE", "DARCEL", "CLAUDE", "CLARISA", "CINDERELLA", "CHIA", "CHARLESETTA", "CHARITA", "CELSA", - "CASSY", "CASSI", "CARLEE", "BRUNA", "BRITTANEY", "BRANDE", "BILLI", "BAO", "ANTONETTA", "ANGLA", - "ANGELYN", "ANALISA", "ALANE", "WENONA", "WENDIE", "VERONIQUE", "VANNESA", "TOBIE", "TEMPIE", "SUMIKO", - "SULEMA", "SPARKLE", "SOMER", "SHEBA", "SHAYNE", "SHARICE", "SHANEL", "SHALON", "SAGE", "ROY", - "ROSIO", "ROSELIA", "RENAY", "REMA", "REENA", "PORSCHE", "PING", "PEG", "OZIE", "ORETHA", - "ORALEE", "ODA", "NU", "NGAN", "NAKESHA", "MILLY", "MARYBELLE", "MARLIN", "MARIS", "MARGRETT", - "MARAGARET", "MANIE", "LURLENE", "LILLIA", "LIESELOTTE", "LAVELLE", "LASHAUNDA", "LAKEESHA", "KEITH", "KAYCEE", - "KALYN", "JOYA", "JOETTE", "JENAE", "JANIECE", "ILLA", "GRISEL", "GLAYDS", "GENEVIE", "GALA", - "FREDDA", "FRED", "ELMER", "ELEONOR", "DEBERA", "DEANDREA", "DAN", "CORRINNE", "CORDIA", "CONTESSA", - "COLENE", "CLEOTILDE", "CHARLOTT", "CHANTAY", "CECILLE", "BEATRIS", "AZALEE", "ARLEAN", "ARDATH", "ANJELICA", - "ANJA", "ALFREDIA", "ALEISHA", "ADAM", "ZADA", "YUONNE", "XIAO", "WILLODEAN", "WHITLEY", "VENNIE", - "VANNA", "TYISHA", "TOVA", "TORIE", "TONISHA", "TILDA", "TIEN", "TEMPLE", "SIRENA", "SHERRIL", - "SHANTI", "SHAN", "SENAIDA", "SAMELLA", "ROBBYN", "RENDA", "REITA", "PHEBE", "PAULITA", "NOBUKO", - "NGUYET", "NEOMI", "MOON", "MIKAELA", "MELANIA", "MAXIMINA", "MARG", "MAISIE", "LYNNA", "LILLI", - "LAYNE", "LASHAUN", "LAKENYA", "LAEL", "KIRSTIE", "KATHLINE", "KASHA", "KARLYN", "KARIMA", "JOVAN", - "JOSEFINE", "JENNELL", "JACQUI", "JACKELYN", "HYO", "HIEN", "GRAZYNA", "FLORRIE", "FLORIA", "ELEONORA", - "DWANA", "DORLA", "DONG", "DELMY", "DEJA", "DEDE", "DANN", "CRYSTA", "CLELIA", "CLARIS", - "CLARENCE", "CHIEKO", "CHERLYN", "CHERELLE", "CHARMAIN", "CHARA", "CAMMY", "BEE", "ARNETTE", "ARDELLE", - "ANNIKA", "AMIEE", "AMEE", "ALLENA", "YVONE", "YUKI", "YOSHIE", "YEVETTE", "YAEL", "WILLETTA", - "VONCILE", "VENETTA", "TULA", "TONETTE", "TIMIKA", "TEMIKA", "TELMA", "TEISHA", "TAREN", "TA", - "STACEE", "SHIN", "SHAWNTA", "SATURNINA", "RICARDA", "POK", "PASTY", "ONIE", "NUBIA", "MORA", - "MIKE", "MARIELLE", "MARIELLA", "MARIANELA", "MARDELL", "MANY", "LUANNA", "LOISE", "LISABETH", "LINDSY", - "LILLIANA", "LILLIAM", "LELAH", "LEIGHA", "LEANORA", "LANG", "KRISTEEN", "KHALILAH", "KEELEY", "KANDRA", - "JUNKO", "JOAQUINA", "JERLENE", "JANI", "JAMIKA", "JAME", "HSIU", "HERMILA", "GOLDEN", "GENEVIVE", - "EVIA", "EUGENA", "EMMALINE", "ELFREDA", "ELENE", "DONETTE", "DELCIE", "DEEANNA", "DARCEY", "CUC", - "CLARINDA", "CIRA", "CHAE", "CELINDA", "CATHERYN", "CATHERIN", "CASIMIRA", "CARMELIA", "CAMELLIA", "BREANA", - "BOBETTE", "BERNARDINA", "BEBE", "BASILIA", "ARLYNE", "AMAL", "ALAYNA", "ZONIA", "ZENIA", "YURIKO", - "YAEKO", "WYNELL", "WILLOW", "WILLENA", "VERNIA", "TU", "TRAVIS", "TORA", "TERRILYN", "TERICA", - "TENESHA", "TAWNA", "TAJUANA", "TAINA", "STEPHNIE", "SONA", "SOL", "SINA", "SHONDRA", "SHIZUKO", - "SHERLENE", "SHERICE", "SHARIKA", "ROSSIE", "ROSENA", "RORY", "RIMA", "RIA", "RHEBA", "RENNA", - "PETER", "NATALYA", "NANCEE", "MELODI", "MEDA", "MAXIMA", "MATHA", "MARKETTA", "MARICRUZ", "MARCELENE", - "MALVINA", "LUBA", "LOUETTA", "LEIDA", "LECIA", "LAURAN", "LASHAWNA", "LAINE", "KHADIJAH", "KATERINE", - "KASI", "KALLIE", "JULIETTA", "JESUSITA", "JESTINE", "JESSIA", "JEREMY", "JEFFIE", "JANYCE", "ISADORA", - "GEORGIANNE", "FIDELIA", "EVITA", "EURA", "EULAH", "ESTEFANA", "ELSY", "ELIZABET", "ELADIA", "DODIE", - "DION", "DIA", "DENISSE", "DELORAS", "DELILA", "DAYSI", "DAKOTA", "CURTIS", "CRYSTLE", "CONCHA", - "COLBY", "CLARETTA", "CHU", "CHRISTIA", "CHARLSIE", "CHARLENA", "CARYLON", "BETTYANN", "ASLEY", "ASHLEA", - "AMIRA", "AI", "AGUEDA", "AGNUS", "YUETTE", "VINITA", "VICTORINA", "TYNISHA", "TREENA", "TOCCARA", - "TISH", "THOMASENA", "TEGAN", "SOILA", "SHILOH", "SHENNA", "SHARMAINE", "SHANTAE", "SHANDI", "SEPTEMBER", - "SARAN", "SARAI", "SANA", "SAMUEL", "SALLEY", "ROSETTE", "ROLANDE", "REGINE", "OTELIA", "OSCAR", - "OLEVIA", "NICHOLLE", "NECOLE", "NAIDA", "MYRTA", "MYESHA", "MITSUE", "MINTA", "MERTIE", "MARGY", - "MAHALIA", "MADALENE", "LOVE", "LOURA", "LOREAN", "LEWIS", "LESHA", "LEONIDA", "LENITA", "LAVONE", - "LASHELL", "LASHANDRA", "LAMONICA", "KIMBRA", "KATHERINA", "KARRY", "KANESHA", "JULIO", "JONG", "JENEVA", - "JAQUELYN", "HWA", "GILMA", "GHISLAINE", "GERTRUDIS", "FRANSISCA", "FERMINA", "ETTIE", "ETSUKO", "ELLIS", - "ELLAN", "ELIDIA", "EDRA", "DORETHEA", "DOREATHA", "DENYSE", "DENNY", "DEETTA", "DAINE", "CYRSTAL", - "CORRIN", "CAYLA", "CARLITA", "CAMILA", "BURMA", "BULA", "BUENA", "BLAKE", "BARABARA", "AVRIL", - "AUSTIN", "ALAINE", "ZANA", "WILHEMINA", "WANETTA", "VIRGIL", "VI", "VERONIKA", "VERNON", "VERLINE", - "VASILIKI", "TONITA", "TISA", "TEOFILA", "TAYNA", "TAUNYA", "TANDRA", "TAKAKO", "SUNNI", "SUANNE", - "SIXTA", "SHARELL", "SEEMA", "RUSSELL", "ROSENDA", "ROBENA", "RAYMONDE", "PEI", "PAMILA", "OZELL", - "NEIDA", "NEELY", "MISTIE", "MICHA", "MERISSA", "MAURITA", "MARYLN", "MARYETTA", "MARSHALL", "MARCELL", - "MALENA", "MAKEDA", "MADDIE", "LOVETTA", "LOURIE", "LORRINE", "LORILEE", "LESTER", "LAURENA", "LASHAY", - "LARRAINE", "LAREE", "LACRESHA", "KRISTLE", "KRISHNA", "KEVA", "KEIRA", "KAROLE", "JOIE", "JINNY", - "JEANNETTA", "JAMA", "HEIDY", "GILBERTE", "GEMA", "FAVIOLA", "EVELYNN", "ENDA", "ELLI", "ELLENA", - "DIVINA", "DAGNY", "COLLENE", "CODI", "CINDIE", "CHASSIDY", "CHASIDY", "CATRICE", "CATHERINA", "CASSEY", - "CAROLL", "CARLENA", "CANDRA", "CALISTA", "BRYANNA", "BRITTENY", "BEULA", "BARI", "AUDRIE", "AUDRIA", - "ARDELIA", "ANNELLE", "ANGILA", "ALONA", "ALLYN", "DOUGLAS", "ROGER", "JONATHAN", "RALPH", "NICHOLAS", - "BENJAMIN", "BRUCE", "HARRY", "WAYNE", "STEVE", "HOWARD", "ERNEST", "PHILLIP", "TODD", "CRAIG", - "ALAN", "PHILIP", "EARL", "DANNY", "BRYAN", "STANLEY", "LEONARD", "NATHAN", "MANUEL", "RODNEY", - "MARVIN", "VINCENT", "JEFFERY", "JEFF", "CHAD", "JACOB", "ALFRED", "BRADLEY", "HERBERT", "FREDERICK", - "EDWIN", "DON", "RICKY", "RANDALL", "BARRY", "BERNARD", "LEROY", "MARCUS", "THEODORE", "CLIFFORD", - "MIGUEL", "JIM", "TOM", "CALVIN", "BILL", "LLOYD", "DEREK", "WARREN", "DARRELL", "JEROME", - "FLOYD", "ALVIN", "TIM", "GORDON", "GREG", "JORGE", "DUSTIN", "PEDRO", "DERRICK", "ZACHARY", - "HERMAN", "GLEN", "HECTOR", "RICARDO", "RICK", "BRENT", "RAMON", "GILBERT", "MARC", "REGINALD", - "RUBEN", "NATHANIEL", "RAFAEL", "EDGAR", "MILTON", "RAUL", "BEN", "CHESTER", "DUANE", "FRANKLIN", - "BRAD", "RON", "ROLAND", "ARNOLD", "HARVEY", "JARED", "ERIK", "DARRYL", "NEIL", "JAVIER", - "FERNANDO", "CLINTON", "TED", "MATHEW", "TYRONE", "DARREN", "LANCE", "KURT", "ALLAN", "NELSON", - "GUY", "CLAYTON", "HUGH", "MAX", "DWAYNE", "DWIGHT", "ARMANDO", "FELIX", "EVERETT", "IAN", - "WALLACE", "KEN", "BOB", "ALFREDO", "ALBERTO", "DAVE", "IVAN", "BYRON", "ISAAC", "MORRIS", - "CLIFTON", "WILLARD", "ROSS", "ANDY", "SALVADOR", "KIRK", "SERGIO", "SETH", "KENT", "TERRANCE", - "EDUARDO", "TERRENCE", "ENRIQUE", "WADE", "STUART", "FREDRICK", "ARTURO", "ALEJANDRO", "NICK", "LUTHER", - "WENDELL", "JEREMIAH", "JULIUS", "OTIS", "TREVOR", "OLIVER", "LUKE", "HOMER", "GERARD", "DOUG", - "KENNY", "HUBERT", "LYLE", "MATT", "ALFONSO", "ORLANDO", "REX", "CARLTON", "ERNESTO", "NEAL", - "PABLO", "LORENZO", "OMAR", "WILBUR", "GRANT", "HORACE", "RODERICK", "ABRAHAM", "WILLIS", "RICKEY", - "ANDRES", "CESAR", "JOHNATHAN", "MALCOLM", "RUDOLPH", "DAMON", "KELVIN", "PRESTON", "ALTON", "ARCHIE", - "MARCO", "WM", "PETE", "RANDOLPH", "GARRY", "GEOFFREY", "JONATHON", "FELIPE", "GERARDO", "ED", - "DOMINIC", "DELBERT", "COLIN", "GUILLERMO", "EARNEST", "LUCAS", "BENNY", "SPENCER", "RODOLFO", "MYRON", - "EDMUND", "GARRETT", "SALVATORE", "CEDRIC", "LOWELL", "GREGG", "SHERMAN", "WILSON", "SYLVESTER", "ROOSEVELT", - "ISRAEL", "JERMAINE", "FORREST", "WILBERT", "LELAND", "SIMON", "CLARK", "IRVING", "BRYANT", "OWEN", - "RUFUS", "WOODROW", "KRISTOPHER", "MACK", "LEVI", "MARCOS", "GUSTAVO", "JAKE", "LIONEL", "GILBERTO", - "CLINT", "NICOLAS", "ISMAEL", "ORVILLE", "ERVIN", "DEWEY", "AL", "WILFRED", "JOSH", "HUGO", - "IGNACIO", "CALEB", "TOMAS", "SHELDON", "ERICK", "STEWART", "DOYLE", "DARREL", "ROGELIO", "TERENCE", - "SANTIAGO", "ALONZO", "ELIAS", "BERT", "ELBERT", "RAMIRO", "CONRAD", "NOAH", "GRADY", "PHIL", - "CORNELIUS", "LAMAR", "ROLANDO", "CLAY", "PERCY", "DEXTER", "BRADFORD", "DARIN", "AMOS", "MOSES", - "IRVIN", "SAUL", "ROMAN", "RANDAL", "TIMMY", "DARRIN", "WINSTON", "BRENDAN", "ABEL", "DOMINICK", - "BOYD", "EMILIO", "ELIJAH", "DOMINGO", "EMMETT", "MARLON", "EMANUEL", "JERALD", "EDMOND", "EMIL", - "DEWAYNE", "WILL", "OTTO", "TEDDY", "REYNALDO", "BRET", "JESS", "TRENT", "HUMBERTO", "EMMANUEL", - "STEPHAN", "VICENTE", "LAMONT", "GARLAND", "MILES", "EFRAIN", "HEATH", "RODGER", "HARLEY", "ETHAN", - "ELDON", "ROCKY", "PIERRE", "JUNIOR", "FREDDY", "ELI", "BRYCE", "ANTOINE", "STERLING", "CHASE", - "GROVER", "ELTON", "CLEVELAND", "DYLAN", "CHUCK", "DAMIAN", "REUBEN", "STAN", "AUGUST", "LEONARDO", - "JASPER", "RUSSEL", "ERWIN", "BENITO", "HANS", "MONTE", "BLAINE", "ERNIE", "CURT", "QUENTIN", - "AGUSTIN", "MURRAY", "JAMAL", "ADOLFO", "HARRISON", "TYSON", "BURTON", "BRADY", "ELLIOTT", "WILFREDO", - "BART", "JARROD", "VANCE", "DENIS", "DAMIEN", "JOAQUIN", "HARLAN", "DESMOND", "ELLIOT", "DARWIN", - "GREGORIO", "BUDDY", "XAVIER", "KERMIT", "ROSCOE", "ESTEBAN", "ANTON", "SOLOMON", "SCOTTY", "NORBERT", - "ELVIN", "WILLIAMS", "NOLAN", "ROD", "QUINTON", "HAL", "BRAIN", "ROB", "ELWOOD", "KENDRICK", - "DARIUS", "MOISES", "FIDEL", "THADDEUS", "CLIFF", "MARCEL", "JACKSON", "RAPHAEL", "BRYON", "ARMAND", - "ALVARO", "JEFFRY", "DANE", "JOESPH", "THURMAN", "NED", "RUSTY", "MONTY", "FABIAN", "REGGIE", - "MASON", "GRAHAM", "ISAIAH", "VAUGHN", "GUS", "LOYD", "DIEGO", "ADOLPH", "NORRIS", "MILLARD", - "ROCCO", "GONZALO", "DERICK", "RODRIGO", "WILEY", "RIGOBERTO", "ALPHONSO", "TY", "NOE", "VERN", - "REED", "JEFFERSON", "ELVIS", "BERNARDO", "MAURICIO", "HIRAM", "DONOVAN", "BASIL", "RILEY", "NICKOLAS", - "MAYNARD", "SCOT", "VINCE", "QUINCY", "EDDY", "SEBASTIAN", "FEDERICO", "ULYSSES", "HERIBERTO", "DONNELL", - "COLE", "DAVIS", "GAVIN", "EMERY", "WARD", "ROMEO", "JAYSON", "DANTE", "CLEMENT", "COY", - "MAXWELL", "JARVIS", "BRUNO", "ISSAC", "DUDLEY", "BROCK", "SANFORD", "CARMELO", "BARNEY", "NESTOR", - "STEFAN", "DONNY", "ART", "LINWOOD", "BEAU", "WELDON", "GALEN", "ISIDRO", "TRUMAN", "DELMAR", - "JOHNATHON", "SILAS", "FREDERIC", "DICK", "IRWIN", "MERLIN", "CHARLEY", "MARCELINO", "HARRIS", "CARLO", - "TRENTON", "KURTIS", "HUNTER", "AURELIO", "WINFRED", "VITO", "COLLIN", "DENVER", "CARTER", "LEONEL", - "EMORY", "PASQUALE", "MOHAMMAD", "MARIANO", "DANIAL", "LANDON", "DIRK", "BRANDEN", "ADAN", "BUFORD", - "GERMAN", "WILMER", "EMERSON", "ZACHERY", "FLETCHER", "JACQUES", "ERROL", "DALTON", "MONROE", "JOSUE", - "EDWARDO", "BOOKER", "WILFORD", "SONNY", "SHELTON", "CARSON", "THERON", "RAYMUNDO", "DAREN", "HOUSTON", - "ROBBY", "LINCOLN", "GENARO", "BENNETT", "OCTAVIO", "CORNELL", "HUNG", "ARRON", "ANTONY", "HERSCHEL", - "GIOVANNI", "GARTH", "CYRUS", "CYRIL", "RONNY", "LON", "FREEMAN", "DUNCAN", "KENNITH", "CARMINE", - "ERICH", "CHADWICK", "WILBURN", "RUSS", "REID", "MYLES", "ANDERSON", "MORTON", "JONAS", "FOREST", - "MITCHEL", "MERVIN", "ZANE", "RICH", "JAMEL", "LAZARO", "ALPHONSE", "RANDELL", "MAJOR", "JARRETT", - "BROOKS", "ABDUL", "LUCIANO", "SEYMOUR", "EUGENIO", "MOHAMMED", "VALENTIN", "CHANCE", "ARNULFO", "LUCIEN", - "FERDINAND", "THAD", "EZRA", "ALDO", "RUBIN", "ROYAL", "MITCH", "EARLE", "ABE", "WYATT", - "MARQUIS", "LANNY", "KAREEM", "JAMAR", "BORIS", "ISIAH", "EMILE", "ELMO", "ARON", "LEOPOLDO", - "EVERETTE", "JOSEF", "ELOY", "RODRICK", "REINALDO", "LUCIO", "JERROD", "WESTON", "HERSHEL", "BARTON", - "PARKER", "LEMUEL", "BURT", "JULES", "GIL", "ELISEO", "AHMAD", "NIGEL", "EFREN", "ANTWAN", - "ALDEN", "MARGARITO", "COLEMAN", "DINO", "OSVALDO", "LES", "DEANDRE", "NORMAND", "KIETH", "TREY", - "NORBERTO", "NAPOLEON", "JEROLD", "FRITZ", "ROSENDO", "MILFORD", "CHRISTOPER", "ALFONZO", "LYMAN", "JOSIAH", - "BRANT", "WILTON", "RICO", "JAMAAL", "DEWITT", "BRENTON", "OLIN", "FOSTER", "FAUSTINO", "CLAUDIO", - "JUDSON", "GINO", "EDGARDO", "ALEC", "TANNER", "JARRED", "DONN", "TAD", "PRINCE", "PORFIRIO", - "ODIS", "LENARD", "CHAUNCEY", "TOD", "MEL", "MARCELO", "KORY", "AUGUSTUS", "KEVEN", "HILARIO", - "BUD", "SAL", "ORVAL", "MAURO", "ZACHARIAH", "OLEN", "ANIBAL", "MILO", "JED", "DILLON", - "AMADO", "NEWTON", "LENNY", "RICHIE", "HORACIO", "BRICE", "MOHAMED", "DELMER", "DARIO", "REYES", - "MAC", "JONAH", "JERROLD", "ROBT", "HANK", "RUPERT", "ROLLAND", "KENTON", "DAMION", "ANTONE", - "WALDO", "FREDRIC", "BRADLY", "KIP", "BURL", "WALKER", "TYREE", "JEFFEREY", "AHMED", "WILLY", - "STANFORD", "OREN", "NOBLE", "MOSHE", "MIKEL", "ENOCH", "BRENDON", "QUINTIN", "JAMISON", "FLORENCIO", - "DARRICK", "TOBIAS", "HASSAN", "GIUSEPPE", "DEMARCUS", "CLETUS", "TYRELL", "LYNDON", "KEENAN", "WERNER", - "GERALDO", "COLUMBUS", "CHET", "BERTRAM", "MARKUS", "HUEY", "HILTON", "DWAIN", "DONTE", "TYRON", - "OMER", "ISAIAS", "HIPOLITO", "FERMIN", "ADALBERTO", "BO", "BARRETT", "TEODORO", "MCKINLEY", "MAXIMO", - "GARFIELD", "RALEIGH", "LAWERENCE", "ABRAM", "RASHAD", "KING", "EMMITT", "DARON", "SAMUAL", "MIQUEL", - "EUSEBIO", "DOMENIC", "DARRON", "BUSTER", "WILBER", "RENATO", "JC", "HOYT", "HAYWOOD", "EZEKIEL", - "CHAS", "FLORENTINO", "ELROY", "CLEMENTE", "ARDEN", "NEVILLE", "EDISON", "DESHAWN", "NATHANIAL", "JORDON", - "DANILO", "CLAUD", "SHERWOOD", "RAYMON", "RAYFORD", "CRISTOBAL", "AMBROSE", "TITUS", "HYMAN", "FELTON", - "EZEQUIEL", "ERASMO", "STANTON", "LONNY", "LEN", "IKE", "MILAN", "LINO", "JAROD", "HERB", - "ANDREAS", "WALTON", "RHETT", "PALMER", "DOUGLASS", "CORDELL", "OSWALDO", "ELLSWORTH", "VIRGILIO", "TONEY", - "NATHANAEL", "DEL", "BENEDICT", "MOSE", "JOHNSON", "ISREAL", "GARRET", "FAUSTO", "ASA", "ARLEN", - "ZACK", "WARNER", "MODESTO", "FRANCESCO", "MANUAL", "GAYLORD", "GASTON", "FILIBERTO", "DEANGELO", "MICHALE", - "GRANVILLE", "WES", "MALIK", "ZACKARY", "TUAN", "ELDRIDGE", "CRISTOPHER", "CORTEZ", "ANTIONE", "MALCOM", - "LONG", "KOREY", "JOSPEH", "COLTON", "WAYLON", "VON", "HOSEA", "SHAD", "SANTO", "RUDOLF", - "ROLF", "REY", "RENALDO", "MARCELLUS", "LUCIUS", "KRISTOFER", "BOYCE", "BENTON", "HAYDEN", "HARLAND", - "ARNOLDO", "RUEBEN", "LEANDRO", "KRAIG", "JERRELL", "JEROMY", "HOBERT", "CEDRICK", "ARLIE", "WINFORD", - "WALLY", "LUIGI", "KENETH", "JACINTO", "GRAIG", "FRANKLYN", "EDMUNDO", "SID", "PORTER", "LEIF", - "JERAMY", "BUCK", "WILLIAN", "VINCENZO", "SHON", "LYNWOOD", "JERE", "HAI", "ELDEN", "DORSEY", - "DARELL", "BRODERICK", "ALONSO" - ] - total_sum = 0 - temp_sum = 0 - name.sort() - for i in range(len(name)): - for j in name[i]: - temp_sum += ord(j) - ord('A') + 1 - total_sum += (i + 1) * temp_sum - temp_sum = 0 - print(total_sum) - - -if __name__ == '__main__': - main() diff --git a/project_euler/problem_234/__init__.py b/project_euler/problem_234/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_234/sol1.py b/project_euler/problem_234/sol1.py new file mode 100644 index 000000000000..7516b164db2d --- /dev/null +++ b/project_euler/problem_234/sol1.py @@ -0,0 +1,119 @@ +""" +https://projecteuler.net/problem=234 + +For an integer n ≥ 4, we define the lower prime square root of n, denoted by +lps(n), as the largest prime ≤ √n and the upper prime square root of n, ups(n), +as the smallest prime ≥ √n. + +So, for example, lps(4) = 2 = ups(4), lps(1000) = 31, ups(1000) = 37. Let us +call an integer n ≥ 4 semidivisible, if one of lps(n) and ups(n) divides n, +but not both. + +The sum of the semidivisible numbers not exceeding 15 is 30, the numbers are 8, +10 and 12. 15 is not semidivisible because it is a multiple of both lps(15) = 3 +and ups(15) = 5. As a further example, the sum of the 92 semidivisible numbers +up to 1000 is 34825. + +What is the sum of all semidivisible numbers not exceeding 999966663333 ? +""" + +import math + + +def prime_sieve(n: int) -> list: + """ + Sieve of Erotosthenes + Function to return all the prime numbers up to a certain number + https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes + >>> prime_sieve(3) + [2] + >>> prime_sieve(50) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] + """ + is_prime = [True] * n + is_prime[0] = False + is_prime[1] = False + is_prime[2] = True + + for i in range(3, int(n ** 0.5 + 1), 2): + index = i * 2 + while index < n: + is_prime[index] = False + index = index + i + + primes = [2] + + for i in range(3, n, 2): + if is_prime[i]: + primes.append(i) + + return primes + + +def solution(limit: int = 999_966_663_333) -> int: + """ + Computes the solution to the problem up to the specified limit + >>> solution(1000) + 34825 + + >>> solution(10_000) + 1134942 + + >>> solution(100_000) + 36393008 + """ + primes_upper_bound = math.floor(math.sqrt(limit)) + 100 + primes = prime_sieve(primes_upper_bound) + + matches_sum = 0 + prime_index = 0 + last_prime = primes[prime_index] + + while (last_prime ** 2) <= limit: + next_prime = primes[prime_index + 1] + + lower_bound = last_prime ** 2 + upper_bound = next_prime ** 2 + + # Get numbers divisible by lps(current) + current = lower_bound + last_prime + while upper_bound > current <= limit: + matches_sum += current + current += last_prime + + # Reset the upper_bound + while (upper_bound - next_prime) > limit: + upper_bound -= next_prime + + # Add the numbers divisible by ups(current) + current = upper_bound - next_prime + while current > lower_bound: + matches_sum += current + current -= next_prime + + # Remove the numbers divisible by both ups and lps + current = 0 + while upper_bound > current <= limit: + if current <= lower_bound: + # Increment the current number + current += last_prime * next_prime + continue + + if current > limit: + break + + # Remove twice since it was added by both ups and lps + matches_sum -= current * 2 + + # Increment the current number + current += last_prime * next_prime + + # Setup for next pair + last_prime = next_prime + prime_index += 1 + + return matches_sum + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_24/sol1.py b/project_euler/problem_24/sol1.py deleted file mode 100644 index b20493cb03af..000000000000 --- a/project_euler/problem_24/sol1.py +++ /dev/null @@ -1,7 +0,0 @@ -from itertools import permutations -def main(): - result=list(map("".join, permutations('0123456789'))) - print(result[999999]) - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/project_euler/problem_25/sol1.py b/project_euler/problem_25/sol1.py deleted file mode 100644 index f8cea3093dcf..000000000000 --- a/project_euler/problem_25/sol1.py +++ /dev/null @@ -1,31 +0,0 @@ -from __future__ import print_function - -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -def fibonacci(n): - if n == 1 or type(n) is not int: - return 0 - elif n == 2: - return 1 - else: - sequence = [0, 1] - for i in xrange(2, n+1): - sequence.append(sequence[i-1] + sequence[i-2]) - - return sequence[n] - -def fibonacci_digits_index(n): - digits = 0 - index = 2 - - while digits < n: - index += 1 - digits = len(str(fibonacci(index))) - - return index - -if __name__ == '__main__': - print(fibonacci_digits_index(1000)) \ No newline at end of file diff --git a/project_euler/problem_25/sol2.py b/project_euler/problem_25/sol2.py deleted file mode 100644 index 35147a9bfb14..000000000000 --- a/project_euler/problem_25/sol2.py +++ /dev/null @@ -1,10 +0,0 @@ -def fibonacci_genrator(): - a, b = 0,1 - while True: - a,b = b,a+b - yield b -answer = 1 -gen = fibonacci_genrator() -while len(str(next(gen))) < 1000: - answer += 1 -assert answer+1 == 4782 diff --git a/project_euler/problem_28/sol1.py b/project_euler/problem_28/sol1.py deleted file mode 100644 index 4942115ce537..000000000000 --- a/project_euler/problem_28/sol1.py +++ /dev/null @@ -1,29 +0,0 @@ -from __future__ import print_function -from math import ceil - -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -def diagonal_sum(n): - total = 1 - - for i in xrange(1, int(ceil(n/2.0))): - odd = 2*i+1 - even = 2*i - total = total + 4*odd**2 - 6*even - - return total - -if __name__ == '__main__': - import sys - - if len(sys.argv) == 1: - print(diagonal_sum(1001)) - else: - try: - n = int(sys.argv[1]) - diagonal_sum(n) - except ValueError: - print('Invalid entry - please enter a number') \ No newline at end of file diff --git a/project_euler/problem_29/solution.py b/project_euler/problem_29/solution.py deleted file mode 100644 index 64d35c84d9ca..000000000000 --- a/project_euler/problem_29/solution.py +++ /dev/null @@ -1,33 +0,0 @@ -def main(): - """ - Consider all integer combinations of ab for 2 <= a <= 5 and 2 <= b <= 5: - - 22=4, 23=8, 24=16, 25=32 - 32=9, 33=27, 34=81, 35=243 - 42=16, 43=64, 44=256, 45=1024 - 52=25, 53=125, 54=625, 55=3125 - If they are then placed in numerical order, with any repeats removed, - we get the following sequence of 15 distinct terms: - - 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 - - How many distinct terms are in the sequence generated by ab - for 2 <= a <= 100 and 2 <= b <= 100? - """ - - collectPowers = set() - - currentPow = 0 - - N = 101 # maximum limit - - for a in range(2, N): - for b in range(2, N): - currentPow = a**b # calculates the current power - collectPowers.add(currentPow) # adds the result to the set - - print("Number of terms ", len(collectPowers)) - - -if __name__ == '__main__': - main() diff --git a/project_euler/problem_301/__init__.py b/project_euler/problem_301/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_301/sol1.py b/project_euler/problem_301/sol1.py new file mode 100644 index 000000000000..b1d434c189b7 --- /dev/null +++ b/project_euler/problem_301/sol1.py @@ -0,0 +1,58 @@ +""" +Project Euler Problem 301: https://projecteuler.net/problem=301 + +Problem Statement: +Nim is a game played with heaps of stones, where two players take +it in turn to remove any number of stones from any heap until no stones remain. + +We'll consider the three-heap normal-play version of +Nim, which works as follows: +- At the start of the game there are three heaps of stones. +- On each player's turn, the player may remove any positive + number of stones from any single heap. +- The first player unable to move (because no stones remain) loses. + +If (n1, n2, n3) indicates a Nim position consisting of heaps of size +n1, n2, and n3, then there is a simple function, which you may look up +or attempt to deduce for yourself, X(n1, n2, n3) that returns: +- zero if, with perfect strategy, the player about to + move will eventually lose; or +- non-zero if, with perfect strategy, the player about + to move will eventually win. + +For example X(1,2,3) = 0 because, no matter what the current player does, +the opponent can respond with a move that leaves two heaps of equal size, +at which point every move by the current player can be mirrored by the +opponent until no stones remain; so the current player loses. To illustrate: +- current player moves to (1,2,1) +- opponent moves to (1,0,1) +- current player moves to (0,0,1) +- opponent moves to (0,0,0), and so wins. + +For how many positive integers n <= 2^30 does X(n,2n,3n) = 0? +""" + + +def solution(exponent: int = 30) -> int: + """ + For any given exponent x >= 0, 1 <= n <= 2^x. + This function returns how many Nim games are lost given that + each Nim game has three heaps of the form (n, 2*n, 3*n). + >>> solution(0) + 1 + >>> solution(2) + 3 + >>> solution(10) + 144 + """ + # To find how many total games were lost for a given exponent x, + # we need to find the Fibonacci number F(x+2). + fibonacci_index = exponent + 2 + phi = (1 + 5 ** 0.5) / 2 + fibonacci = (phi ** fibonacci_index - (phi - 1) ** fibonacci_index) / 5 ** 0.5 + + return int(fibonacci) + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_36/sol1.py b/project_euler/problem_36/sol1.py deleted file mode 100644 index d78e7e59f210..000000000000 --- a/project_euler/problem_36/sol1.py +++ /dev/null @@ -1,30 +0,0 @@ -from __future__ import print_function -''' -Double-base palindromes -Problem 36 -The decimal number, 585 = 10010010012 (binary), is palindromic in both bases. - -Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. - -(Please note that the palindromic number, in either base, may not include leading zeros.) -''' -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -def is_palindrome(n): - n = str(n) - - if n == n[::-1]: - return True - else: - return False - -total = 0 - -for i in xrange(1, 1000000): - if is_palindrome(i) and is_palindrome(bin(i).split('b')[1]): - total += i - -print(total) \ No newline at end of file diff --git a/project_euler/problem_40/sol1.py b/project_euler/problem_40/sol1.py deleted file mode 100644 index ab4017512a1a..000000000000 --- a/project_euler/problem_40/sol1.py +++ /dev/null @@ -1,26 +0,0 @@ -#-.- coding: latin-1 -.- -from __future__ import print_function -''' -Champernowne's constant -Problem 40 -An irrational decimal fraction is created by concatenating the positive integers: - -0.123456789101112131415161718192021... - -It can be seen that the 12th digit of the fractional part is 1. - -If dn represents the nth digit of the fractional part, find the value of the following expression. - -d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000 -''' - -constant = [] -i = 1 - -while len(constant) < 1e6: - constant.append(str(i)) - i += 1 - -constant = ''.join(constant) - -print(int(constant[0])*int(constant[9])*int(constant[99])*int(constant[999])*int(constant[9999])*int(constant[99999])*int(constant[999999])) \ No newline at end of file diff --git a/project_euler/problem_48/sol1.py b/project_euler/problem_48/sol1.py deleted file mode 100644 index 5c4bdb0f6384..000000000000 --- a/project_euler/problem_48/sol1.py +++ /dev/null @@ -1,21 +0,0 @@ -from __future__ import print_function -''' -Self Powers -Problem 48 - -The series, 11 + 22 + 33 + ... + 1010 = 10405071317. - -Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000. -''' - -try: - xrange -except NameError: - xrange = range - -total = 0 -for i in xrange(1, 1001): - total += i**i - - -print(str(total)[-10:]) \ No newline at end of file diff --git a/project_euler/problem_52/sol1.py b/project_euler/problem_52/sol1.py deleted file mode 100644 index 376b4cfa1d63..000000000000 --- a/project_euler/problem_52/sol1.py +++ /dev/null @@ -1,23 +0,0 @@ -from __future__ import print_function -''' -Permuted multiples -Problem 52 - -It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. - -Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. -''' -i = 1 - -while True: - if sorted(list(str(i))) == \ - sorted(list(str(2*i))) == \ - sorted(list(str(3*i))) == \ - sorted(list(str(4*i))) == \ - sorted(list(str(5*i))) == \ - sorted(list(str(6*i))): - break - - i += 1 - -print(i) \ No newline at end of file diff --git a/project_euler/problem_53/sol1.py b/project_euler/problem_53/sol1.py deleted file mode 100644 index ed6d5329eb4e..000000000000 --- a/project_euler/problem_53/sol1.py +++ /dev/null @@ -1,36 +0,0 @@ -#-.- coding: latin-1 -.- -from __future__ import print_function -from math import factorial -''' -Combinatoric selections -Problem 53 - -There are exactly ten ways of selecting three from five, 12345: - -123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 - -In combinatorics, we use the notation, 5C3 = 10. - -In general, - -nCr = n!/(r!(n−r)!),where r ≤ n, n! = n×(n−1)×...×3×2×1, and 0! = 1. -It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066. - -How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million? -''' -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -def combinations(n, r): - return factorial(n)/(factorial(r)*factorial(n-r)) - -total = 0 - -for i in xrange(1, 101): - for j in xrange(1, i+1): - if combinations(i, j) > 1e6: - total += 1 - -print(total) \ No newline at end of file diff --git a/project_euler/problem_551/__init__.py b/project_euler/problem_551/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_551/sol1.py b/project_euler/problem_551/sol1.py new file mode 100644 index 000000000000..71956691a56d --- /dev/null +++ b/project_euler/problem_551/sol1.py @@ -0,0 +1,200 @@ +""" +Sum of digits sequence +Problem 551 + +Let a(0), a(1),... be an integer sequence defined by: + a(0) = 1 + for n >= 1, a(n) is the sum of the digits of all preceding terms + +The sequence starts with 1, 1, 2, 4, 8, ... +You are given a(10^6) = 31054319. + +Find a(10^15) +""" + +ks = [k for k in range(2, 20 + 1)] +base = [10 ** k for k in range(ks[-1] + 1)] +memo = {} + + +def next_term(a_i, k, i, n): + """ + Calculates and updates a_i in-place to either the n-th term or the + smallest term for which c > 10^k when the terms are written in the form: + a(i) = b * 10^k + c + + For any a(i), if digitsum(b) and c have the same value, the difference + between subsequent terms will be the same until c >= 10^k. This difference + is cached to greatly speed up the computation. + + Arguments: + a_i -- array of digits starting from the one's place that represent + the i-th term in the sequence + k -- k when terms are written in the from a(i) = b*10^k + c. + Term are calulcated until c > 10^k or the n-th term is reached. + i -- position along the sequence + n -- term to calculate up to if k is large enough + + Return: a tuple of difference between ending term and starting term, and + the number of terms calculated. ex. if starting term is a_0=1, and + ending term is a_10=62, then (61, 9) is returned. + """ + # ds_b - digitsum(b) + ds_b = sum(a_i[j] for j in range(k, len(a_i))) + c = sum(a_i[j] * base[j] for j in range(min(len(a_i), k))) + + diff, dn = 0, 0 + max_dn = n - i + + sub_memo = memo.get(ds_b) + + if sub_memo is not None: + jumps = sub_memo.get(c) + + if jumps is not None and len(jumps) > 0: + # find and make the largest jump without going over + max_jump = -1 + for _k in range(len(jumps) - 1, -1, -1): + if jumps[_k][2] <= k and jumps[_k][1] <= max_dn: + max_jump = _k + break + + if max_jump >= 0: + diff, dn, _kk = jumps[max_jump] + # since the difference between jumps is cached, add c + new_c = diff + c + for j in range(min(k, len(a_i))): + new_c, a_i[j] = divmod(new_c, 10) + if new_c > 0: + add(a_i, k, new_c) + + else: + sub_memo[c] = [] + else: + sub_memo = {c: []} + memo[ds_b] = sub_memo + + if dn >= max_dn or c + diff >= base[k]: + return diff, dn + + if k > ks[0]: + while True: + # keep doing smaller jumps + _diff, terms_jumped = next_term(a_i, k - 1, i + dn, n) + diff += _diff + dn += terms_jumped + + if dn >= max_dn or c + diff >= base[k]: + break + else: + # would be too small a jump, just compute sequential terms instead + _diff, terms_jumped = compute(a_i, k, i + dn, n) + diff += _diff + dn += terms_jumped + + jumps = sub_memo[c] + + # keep jumps sorted by # of terms skipped + j = 0 + while j < len(jumps): + if jumps[j][1] > dn: + break + j += 1 + + # cache the jump for this value digitsum(b) and c + sub_memo[c].insert(j, (diff, dn, k)) + return (diff, dn) + + +def compute(a_i, k, i, n): + """ + same as next_term(a_i, k, i, n) but computes terms without memoizing results. + """ + if i >= n: + return 0, i + if k > len(a_i): + a_i.extend([0 for _ in range(k - len(a_i))]) + + # note: a_i -> b * 10^k + c + # ds_b -> digitsum(b) + # ds_c -> digitsum(c) + start_i = i + ds_b, ds_c, diff = 0, 0, 0 + for j in range(len(a_i)): + if j >= k: + ds_b += a_i[j] + else: + ds_c += a_i[j] + + while i < n: + i += 1 + addend = ds_c + ds_b + diff += addend + ds_c = 0 + for j in range(k): + s = a_i[j] + addend + addend, a_i[j] = divmod(s, 10) + + ds_c += a_i[j] + + if addend > 0: + break + + if addend > 0: + add(a_i, k, addend) + return diff, i - start_i + + +def add(digits, k, addend): + """ + adds addend to digit array given in digits + starting at index k + """ + for j in range(k, len(digits)): + s = digits[j] + addend + if s >= 10: + quotient, digits[j] = divmod(s, 10) + addend = addend // 10 + quotient + else: + digits[j] = s + addend = addend // 10 + + if addend == 0: + break + + while addend > 0: + addend, digit = divmod(addend, 10) + digits.append(digit) + + +def solution(n: int = 10 ** 15) -> int: + """ + returns n-th term of sequence + + >>> solution(10) + 62 + + >>> solution(10**6) + 31054319 + + >>> solution(10**15) + 73597483551591773 + """ + + digits = [1] + i = 1 + dn = 0 + while True: + diff, terms_jumped = next_term(digits, 20, i + dn, n) + dn += terms_jumped + if dn == n - i: + break + + a_n = 0 + for j in range(len(digits)): + a_n += digits[j] * 10 ** j + return a_n + + +if __name__ == "__main__": + print(f"{solution() = }") diff --git a/project_euler/problem_76/sol1.py b/project_euler/problem_76/sol1.py deleted file mode 100644 index 2832f6d7afb6..000000000000 --- a/project_euler/problem_76/sol1.py +++ /dev/null @@ -1,35 +0,0 @@ -from __future__ import print_function -''' -Counting Summations -Problem 76 - -It is possible to write five as a sum in exactly six different ways: - -4 + 1 -3 + 2 -3 + 1 + 1 -2 + 2 + 1 -2 + 1 + 1 + 1 -1 + 1 + 1 + 1 + 1 - -How many different ways can one hundred be written as a sum of at least two positive integers? -''' -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -def partition(m): - memo = [[0 for _ in xrange(m)] for _ in xrange(m+1)] - for i in xrange(m+1): - memo[i][0] = 1 - - for n in xrange(m+1): - for k in xrange(1, m): - memo[n][k] += memo[n][k-1] - if n > k: - memo[n][k] += memo[n-k-1][k] - - return (memo[m][m-1] - 1) - -print(partition(100)) \ No newline at end of file diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 000000000000..488379278230 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,5 @@ +# Setup for pytest +[pytest] +markers = + mat_ops: mark a test as utilizing matrix operations. +addopts = --durations=10 diff --git a/quantum/README.md b/quantum/README.md new file mode 100644 index 000000000000..423d34fa3364 --- /dev/null +++ b/quantum/README.md @@ -0,0 +1,15 @@ +# Welcome to Quantum Algorithms + +Started at https://github.com/TheAlgorithms/Python/issues/1831 + +* D-Wave: https://www.dwavesys.com and https://github.com/dwavesystems +* Google: https://research.google/teams/applied-science/quantum +* IBM: https://qiskit.org and https://github.com/Qiskit +* Rigetti: https://rigetti.com and https://github.com/rigetti + +## IBM Qiskit +- Start using by installing `pip install qiskit`, refer the [docs](https://qiskit.org/documentation/install.html) for more info. +- Tutorials & References + - https://github.com/Qiskit/qiskit-tutorials + - https://quantum-computing.ibm.com/docs/iql/first-circuit + - https://medium.com/qiskit/how-to-program-a-quantum-computer-982a9329ed02 diff --git a/quantum/__init__.py b/quantum/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/quantum/deutsch_jozsa.py b/quantum/deutsch_jozsa.py new file mode 100755 index 000000000000..304eea196e03 --- /dev/null +++ b/quantum/deutsch_jozsa.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python3 +""" +Deutsch-Josza Algorithm is one of the first examples of a quantum +algorithm that is exponentially faster than any possible deterministic +classical algorithm + +Premise: +We are given a hidden Boolean function f, +which takes as input a string of bits, and returns either 0 or 1: + +f({x0,x1,x2,...}) -> 0 or 1, where xn is 0 or 1 + +The property of the given Boolean function is that it is guaranteed to +either be balanced or constant. A constant function returns all 0's +or all 1's for any input, while a balanced function returns 0's for +exactly half of all inputs and 1's for the other half. Our task is to +determine whether the given function is balanced or constant. + +References: +- https://en.wikipedia.org/wiki/Deutsch-Jozsa_algorithm +- https://qiskit.org/textbook/ch-algorithms/deutsch-jozsa.html +""" + +import numpy as np +import qiskit as q + + +def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit: + """ + Returns a Quantum Circuit for the Oracle function. + The circuit returned can represent balanced or constant function, + according to the arguments passed + """ + # This circuit has num_qubits+1 qubits: the size of the input, + # plus one output qubit + oracle_qc = q.QuantumCircuit(num_qubits + 1) + + # First, let's deal with the case in which oracle is balanced + if case == "balanced": + # First generate a random number that tells us which CNOTs to + # wrap in X-gates: + b = np.random.randint(1, 2 ** num_qubits) + # Next, format 'b' as a binary string of length 'n', padded with zeros: + b_str = format(b, f"0{num_qubits}b") + # Next, we place the first X-gates. Each digit in our binary string + # correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1 + # we apply an X-gate to that qubit: + for index, bit in enumerate(b_str): + if bit == "1": + oracle_qc.x(index) + # Do the controlled-NOT gates for each qubit, using the output qubit + # as the target: + for index in range(num_qubits): + oracle_qc.cx(index, num_qubits) + # Next, place the final X-gates + for index, bit in enumerate(b_str): + if bit == "1": + oracle_qc.x(index) + + # Case in which oracle is constant + if case == "constant": + # First decide what the fixed output of the oracle will be + # (either always 0 or always 1) + output = np.random.randint(2) + if output == 1: + oracle_qc.x(num_qubits) + + oracle_gate = oracle_qc.to_gate() + oracle_gate.name = "Oracle" # To show when we display the circuit + return oracle_gate + + +def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit: + """ + Returns the complete Deustch-Jozsa Quantum Circuit, + adding Input & Output registers and Hadamard & Measurement Gates, + to the Oracle Circuit passed in arguments + """ + dj_circuit = q.QuantumCircuit(num_qubits + 1, num_qubits) + # Set up the output qubit: + dj_circuit.x(num_qubits) + dj_circuit.h(num_qubits) + # And set up the input register: + for qubit in range(num_qubits): + dj_circuit.h(qubit) + # Let's append the oracle gate to our circuit: + dj_circuit.append(oracle, range(num_qubits + 1)) + # Finally, perform the H-gates again and measure: + for qubit in range(num_qubits): + dj_circuit.h(qubit) + + for i in range(num_qubits): + dj_circuit.measure(i, i) + + return dj_circuit + + +def deutsch_jozsa(case: str, num_qubits: int) -> q.result.counts.Counts: + """ + Main function that builds the circuit using other helper functions, + runs the experiment 1000 times & returns the resultant qubit counts + >>> deutsch_jozsa("constant", 3) + {'000': 1000} + >>> deutsch_jozsa("balanced", 3) + {'111': 1000} + """ + # Use Aer's qasm_simulator + simulator = q.Aer.get_backend("qasm_simulator") + + oracle_gate = dj_oracle(case, num_qubits) + dj_circuit = dj_algorithm(oracle_gate, num_qubits) + + # Execute the circuit on the qasm simulator + job = q.execute(dj_circuit, simulator, shots=1000) + + # Return the histogram data of the results of the experiment. + return job.result().get_counts(dj_circuit) + + +if __name__ == "__main__": + print(f"Deutsch Jozsa - Constant Oracle: {deutsch_jozsa('constant', 3)}") + print(f"Deutsch Jozsa - Balanced Oracle: {deutsch_jozsa('balanced', 3)}") diff --git a/quantum/half_adder.py b/quantum/half_adder.py new file mode 100755 index 000000000000..4af704e640be --- /dev/null +++ b/quantum/half_adder.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +""" +Build a half-adder quantum circuit that takes two bits as input, +encodes them into qubits, then runs the half-adder circuit calculating +the sum and carry qubits, observed over 1000 runs of the experiment +. + +References: +https://en.wikipedia.org/wiki/Adder_(electronics) +https://qiskit.org/textbook/ch-states/atoms-computation.html#4.2-Remembering-how-to-add- +""" + +import qiskit as q + + +def half_adder(bit0: int, bit1: int) -> q.result.counts.Counts: + """ + >>> half_adder(0, 0) + {'00': 1000} + >>> half_adder(0, 1) + {'01': 1000} + >>> half_adder(1, 0) + {'01': 1000} + >>> half_adder(1, 1) + {'10': 1000} + """ + # Use Aer's qasm_simulator + simulator = q.Aer.get_backend("qasm_simulator") + + qc_ha = q.QuantumCircuit(4, 2) + # encode inputs in qubits 0 and 1 + if bit0 == 1: + qc_ha.x(0) + if bit1 == 1: + qc_ha.x(1) + qc_ha.barrier() + + # use cnots to write XOR of the inputs on qubit2 + qc_ha.cx(0, 2) + qc_ha.cx(1, 2) + + # use ccx / toffoli gate to write AND of the inputs on qubit3 + qc_ha.ccx(0, 1, 3) + qc_ha.barrier() + + # extract outputs + qc_ha.measure(2, 0) # extract XOR value + qc_ha.measure(3, 1) # extract AND value + + # Execute the circuit on the qasm simulator + job = q.execute(qc_ha, simulator, shots=1000) + + # Return the histogram data of the results of the experiment. + return job.result().get_counts(qc_ha) + + +if __name__ == "__main__": + counts = half_adder(1, 1) + print(f"Half Adder Output Qubit Counts: {counts}") diff --git a/quantum/not_gate.py b/quantum/not_gate.py new file mode 100644 index 000000000000..e68a780091c7 --- /dev/null +++ b/quantum/not_gate.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +""" +Build a simple bare-minimum quantum circuit that starts with a single +qubit (by default, in state 0) and inverts it. Run the experiment 1000 +times and print the total count of the states finally observed. +Qiskit Docs: https://qiskit.org/documentation/getting_started.html +""" + +import qiskit as q + + +def single_qubit_measure(qubits: int, classical_bits: int) -> q.result.counts.Counts: + """ + >>> single_qubit_measure(2, 2) + {'11': 1000} + >>> single_qubit_measure(4, 4) + {'0011': 1000} + """ + # Use Aer's qasm_simulator + simulator = q.Aer.get_backend("qasm_simulator") + + # Create a Quantum Circuit acting on the q register + circuit = q.QuantumCircuit(qubits, classical_bits) + + # Apply X (NOT) Gate to Qubits 0 & 1 + circuit.x(0) + circuit.x(1) + + # Map the quantum measurement to the classical bits + circuit.measure([0, 1], [0, 1]) + + # Execute the circuit on the qasm simulator + job = q.execute(circuit, simulator, shots=1000) + + # Return the histogram data of the results of the experiment. + return job.result().get_counts(circuit) + + +if __name__ == "__main__": + counts = single_qubit_measure(2, 2) + print(f"Total count for various states are: {counts}") diff --git a/quantum/quantum_entanglement.py b/quantum/quantum_entanglement.py new file mode 100644 index 000000000000..3d8e2771361c --- /dev/null +++ b/quantum/quantum_entanglement.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +""" +Build a quantum circuit with pair or group of qubits to perform +quantum entanglement. +Quantum entanglement is a phenomenon observed at the quantum scale +where entangled particles stay connected (in some sense) so that +the actions performed on one of the particles affects the other, +no matter the distance between two particles. +""" + +import qiskit + + +def quantum_entanglement(qubits: int = 2) -> qiskit.result.counts.Counts: + """ + # >>> quantum_entanglement(2) + # {'00': 500, '11': 500} + # ┌───┐ ┌─┐ + # q_0: ┤ H ├──■──┤M├─── + # └───┘┌─┴─┐└╥┘┌─┐ + # q_1: ─────┤ X ├─╫─┤M├ + # └───┘ ║ └╥┘ + # c: 2/═══════════╩══╩═ + # 0 1 + Args: + qubits (int): number of quibits to use. Defaults to 2 + Returns: + qiskit.result.counts.Counts: mapping of states to its counts + """ + classical_bits = qubits + + # Using Aer's qasm_simulator + simulator = qiskit.Aer.get_backend("qasm_simulator") + + # Creating a Quantum Circuit acting on the q register + circuit = qiskit.QuantumCircuit(qubits, classical_bits) + + # Adding a H gate on qubit 0 (now q0 in superposition) + circuit.h(0) + + for i in range(1, qubits): + # Adding CX (CNOT) gate + circuit.cx(i - 1, i) + + # Mapping the quantum measurement to the classical bits + circuit.measure(list(range(qubits)), list(range(classical_bits))) + + # Now measuring any one qubit would affect other qubits to collapse + # their super position and have same state as the measured one. + + # Executing the circuit on the qasm simulator + job = qiskit.execute(circuit, simulator, shots=1000) + + return job.result().get_counts(circuit) + + +if __name__ == "__main__": + print(f"Total count for various states are: {quantum_entanglement(3)}") diff --git a/quantum/ripple_adder_classic.py b/quantum/ripple_adder_classic.py new file mode 100644 index 000000000000..f5b0a980c8e2 --- /dev/null +++ b/quantum/ripple_adder_classic.py @@ -0,0 +1,108 @@ +# https://github.com/rupansh/QuantumComputing/blob/master/rippleadd.py +# https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder +# https://en.wikipedia.org/wiki/Controlled_NOT_gate + +from qiskit import Aer, QuantumCircuit, execute +from qiskit.providers import BaseBackend + + +def store_two_classics(val1: int, val2: int) -> (QuantumCircuit, str, str): + """ + Generates a Quantum Circuit which stores two classical integers + Returns the circuit and binary representation of the integers + """ + x, y = bin(val1)[2:], bin(val2)[2:] # Remove leading '0b' + + # Ensure that both strings are of the same length + if len(x) > len(y): + y = y.zfill(len(x)) + else: + x = x.zfill(len(y)) + + # We need (3 * number of bits in the larger number)+1 qBits + # The second parameter is the number of classical registers, to measure the result + circuit = QuantumCircuit((len(x) * 3) + 1, len(x) + 1) + + # We are essentially "not-ing" the bits that are 1 + # Reversed because its easier to perform ops on more significant bits + for i in range(len(x)): + if x[::-1][i] == "1": + circuit.x(i) + for j in range(len(y)): + if y[::-1][j] == "1": + circuit.x(len(x) + j) + + return circuit, x, y + + +def full_adder( + circuit: QuantumCircuit, + input1_loc: int, + input2_loc: int, + carry_in: int, + carry_out: int, +): + """ + Quantum Equivalent of a Full Adder Circuit + CX/CCX is like 2-way/3-way XOR + """ + circuit.ccx(input1_loc, input2_loc, carry_out) + circuit.cx(input1_loc, input2_loc) + circuit.ccx(input2_loc, carry_in, carry_out) + circuit.cx(input2_loc, carry_in) + circuit.cx(input1_loc, input2_loc) + + +def ripple_adder( + val1: int, val2: int, backend: BaseBackend = Aer.get_backend("qasm_simulator") +) -> int: + """ + Quantum Equivalent of a Ripple Adder Circuit + Uses qasm_simulator backend by default + + Currently only adds 'emulated' Classical Bits + but nothing prevents us from doing this with hadamard'd bits :) + + Only supports adding +ve Integers + + >>> ripple_adder(3, 4) + 7 + >>> ripple_adder(10, 4) + 14 + >>> ripple_adder(-1, 10) + Traceback (most recent call last): + ... + ValueError: Both Integers must be positive! + """ + + if val1 < 0 or val2 < 0: + raise ValueError("Both Integers must be positive!") + + # Store the Integers + circuit, x, y = store_two_classics(val1, val2) + + """ + We are essentially using each bit of x & y respectively as full_adder's input + the carry_input is used from the previous circuit (for circuit num > 1) + + the carry_out is just below carry_input because + it will be essentially the carry_input for the next full_adder + """ + for i in range(len(x)): + full_adder(circuit, i, len(x) + i, len(x) + len(y) + i, len(x) + len(y) + i + 1) + circuit.barrier() # Optional, just for aesthetics + + # Measure the resultant qBits + for i in range(len(x) + 1): + circuit.measure([(len(x) * 2) + i], [i]) + + res = execute(circuit, backend, shots=1).result() + + # The result is in binary. Convert it back to int + return int(list(res.get_counts().keys())[0], 2) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/quantum/single_qubit_measure.py b/quantum/single_qubit_measure.py new file mode 100755 index 000000000000..7f058c2179a9 --- /dev/null +++ b/quantum/single_qubit_measure.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +""" +Build a simple bare-minimum quantum circuit that starts with a single +qubit (by default, in state 0), runs the experiment 1000 times, and +finally prints the total count of the states finally observed. +Qiskit Docs: https://qiskit.org/documentation/getting_started.html +""" + +import qiskit as q + + +def single_qubit_measure(qubits: int, classical_bits: int) -> q.result.counts.Counts: + """ + >>> single_qubit_measure(1, 1) + {'0': 1000} + """ + # Use Aer's qasm_simulator + simulator = q.Aer.get_backend("qasm_simulator") + + # Create a Quantum Circuit acting on the q register + circuit = q.QuantumCircuit(qubits, classical_bits) + + # Map the quantum measurement to the classical bits + circuit.measure([0], [0]) + + # Execute the circuit on the qasm simulator + job = q.execute(circuit, simulator, shots=1000) + + # Return the histogram data of the results of the experiment. + return job.result().get_counts(circuit) + + +if __name__ == "__main__": + print(f"Total count for various states are: {single_qubit_measure(1, 1)}") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000000..349d88944656 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,17 @@ +beautifulsoup4 +fake_useragent +keras; python_version < '3.9' +lxml +matplotlib +numpy +opencv-python +pandas +pillow +qiskit +requests +scikit-fuzzy +sklearn +statsmodels +sympy +tensorflow; python_version < '3.9' +xgboost diff --git a/scheduling/__init__.py b/scheduling/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py new file mode 100644 index 000000000000..b51fc9fe0c04 --- /dev/null +++ b/scheduling/first_come_first_served.py @@ -0,0 +1,108 @@ +# Implementation of First Come First Served scheduling algorithm +# In this Algorithm we just care about the order that the processes arrived +# without carring about their duration time +# https://en.wikipedia.org/wiki/Scheduling_(computing)#First_come,_first_served +from typing import List + + +def calculate_waiting_times(duration_times: List[int]) -> List[int]: + """ + This function calculates the waiting time of some processes that have a + specified duration time. + Return: The waiting time for each process. + >>> calculate_waiting_times([5, 10, 15]) + [0, 5, 15] + >>> calculate_waiting_times([1, 2, 3, 4, 5]) + [0, 1, 3, 6, 10] + >>> calculate_waiting_times([10, 3]) + [0, 10] + """ + waiting_times = [0] * len(duration_times) + for i in range(1, len(duration_times)): + waiting_times[i] = duration_times[i - 1] + waiting_times[i - 1] + return waiting_times + + +def calculate_turnaround_times( + duration_times: List[int], waiting_times: List[int] +) -> List[int]: + """ + This function calculates the turnaround time of some processes. + Return: The time difference between the completion time and the + arrival time. + Practically waiting_time + duration_time + >>> calculate_turnaround_times([5, 10, 15], [0, 5, 15]) + [5, 15, 30] + >>> calculate_turnaround_times([1, 2, 3, 4, 5], [0, 1, 3, 6, 10]) + [1, 3, 6, 10, 15] + >>> calculate_turnaround_times([10, 3], [0, 10]) + [10, 13] + """ + return [ + duration_time + waiting_times[i] + for i, duration_time in enumerate(duration_times) + ] + + +def calculate_average_turnaround_time(turnaround_times: List[int]) -> float: + """ + This function calculates the average of the turnaround times + Return: The average of the turnaround times. + >>> calculate_average_turnaround_time([0, 5, 16]) + 7.0 + >>> calculate_average_turnaround_time([1, 5, 8, 12]) + 6.5 + >>> calculate_average_turnaround_time([10, 24]) + 17.0 + """ + return sum(turnaround_times) / len(turnaround_times) + + +def calculate_average_waiting_time(waiting_times: List[int]) -> float: + """ + This function calculates the average of the waiting times + Return: The average of the waiting times. + >>> calculate_average_waiting_time([0, 5, 16]) + 7.0 + >>> calculate_average_waiting_time([1, 5, 8, 12]) + 6.5 + >>> calculate_average_waiting_time([10, 24]) + 17.0 + """ + return sum(waiting_times) / len(waiting_times) + + +if __name__ == "__main__": + # process id's + processes = [1, 2, 3] + + # ensure that we actually have processes + if len(processes) == 0: + print("Zero amount of processes") + exit() + + # duration time of all processes + duration_times = [19, 8, 9] + + # ensure we can match each id to a duration time + if len(duration_times) != len(processes): + print("Unable to match all id's with their duration time") + exit() + + # get the waiting times and the turnaround times + waiting_times = calculate_waiting_times(duration_times) + turnaround_times = calculate_turnaround_times(duration_times, waiting_times) + + # get the average times + average_waiting_time = calculate_average_waiting_time(waiting_times) + average_turnaround_time = calculate_average_turnaround_time(turnaround_times) + + # print all the results + print("Process ID\tDuration Time\tWaiting Time\tTurnaround Time") + for i, process in enumerate(processes): + print( + f"{process}\t\t{duration_times[i]}\t\t{waiting_times[i]}\t\t" + f"{turnaround_times[i]}" + ) + print(f"Average waiting time = {average_waiting_time}") + print(f"Average turn around time = {average_turnaround_time}") diff --git a/scheduling/round_robin.py b/scheduling/round_robin.py new file mode 100644 index 000000000000..4a79301c1816 --- /dev/null +++ b/scheduling/round_robin.py @@ -0,0 +1,65 @@ +""" +Round Robin is a scheduling algorithm. +In Round Robin each process is assigned a fixed time slot in a cyclic way. +https://en.wikipedia.org/wiki/Round-robin_scheduling +""" +from statistics import mean +from typing import List + + +def calculate_waiting_times(burst_times: List[int]) -> List[int]: + """ + Calculate the waiting times of a list of processes that have a specified duration. + + Return: The waiting time for each process. + >>> calculate_waiting_times([10, 5, 8]) + [13, 10, 13] + >>> calculate_waiting_times([4, 6, 3, 1]) + [5, 8, 9, 6] + >>> calculate_waiting_times([12, 2, 10]) + [12, 2, 12] + """ + quantum = 2 + rem_burst_times = list(burst_times) + waiting_times = [0] * len(burst_times) + t = 0 + while True: + done = True + for i, burst_time in enumerate(burst_times): + if rem_burst_times[i] > 0: + done = False + if rem_burst_times[i] > quantum: + t += quantum + rem_burst_times[i] -= quantum + else: + t += rem_burst_times[i] + waiting_times[i] = t - burst_time + rem_burst_times[i] = 0 + if done is True: + return waiting_times + + +def calculate_turn_around_times( + burst_times: List[int], waiting_times: List[int] +) -> List[int]: + """ + >>> calculate_turn_around_times([1, 2, 3, 4], [0, 1, 3]) + [1, 3, 6] + >>> calculate_turn_around_times([10, 3, 7], [10, 6, 11]) + [20, 9, 18] + """ + return [burst + waiting for burst, waiting in zip(burst_times, waiting_times)] + + +if __name__ == "__main__": + burst_times = [3, 5, 7] + waiting_times = calculate_waiting_times(burst_times) + turn_around_times = calculate_turn_around_times(burst_times, waiting_times) + print("Process ID \tBurst Time \tWaiting Time \tTurnaround Time") + for i, burst_time in enumerate(burst_times): + print( + f" {i + 1}\t\t {burst_time}\t\t {waiting_times[i]}\t\t " + f"{turn_around_times[i]}" + ) + print(f"\nAverage waiting time = {mean(waiting_times):.5f}") + print(f"Average turn around time = {mean(turn_around_times):.5f}") diff --git a/scheduling/shortest_job_first.py b/scheduling/shortest_job_first.py new file mode 100644 index 000000000000..a49d037d6a23 --- /dev/null +++ b/scheduling/shortest_job_first.py @@ -0,0 +1,151 @@ +""" +Shortest job remaining first +Please note arrival time and burst +Please use spaces to separate times entered. +""" +from typing import List + +import pandas as pd + + +def calculate_waitingtime( + arrival_time: List[int], burst_time: List[int], no_of_processes: int +) -> List[int]: + """ + Calculate the waiting time of each processes + Return: List of waiting times. + >>> calculate_waitingtime([1,2,3,4],[3,3,5,1],4) + [0, 3, 5, 0] + >>> calculate_waitingtime([1,2,3],[2,5,1],3) + [0, 2, 0] + >>> calculate_waitingtime([2,3],[5,1],2) + [1, 0] + """ + remaining_time = [0] * no_of_processes + waiting_time = [0] * no_of_processes + # Copy the burst time into remaining_time[] + for i in range(no_of_processes): + remaining_time[i] = burst_time[i] + + complete = 0 + increment_time = 0 + minm = 999999999 + short = 0 + check = False + + # Process until all processes are completed + while complete != no_of_processes: + for j in range(no_of_processes): + if arrival_time[j] <= increment_time: + if remaining_time[j] > 0: + if remaining_time[j] < minm: + minm = remaining_time[j] + short = j + check = True + + if not check: + increment_time += 1 + continue + remaining_time[short] -= 1 + + minm = remaining_time[short] + if minm == 0: + minm = 999999999 + + if remaining_time[short] == 0: + complete += 1 + check = False + + # Find finish time of current process + finish_time = increment_time + 1 + + # Calculate waiting time + finar = finish_time - arrival_time[short] + waiting_time[short] = finar - burst_time[short] + + if waiting_time[short] < 0: + waiting_time[short] = 0 + + # Increment time + increment_time += 1 + return waiting_time + + +def calculate_turnaroundtime( + burst_time: List[int], no_of_processes: int, waiting_time: List[int] +) -> List[int]: + """ + Calculate the turn around time of each Processes + Return: list of turn around times. + >>> calculate_turnaroundtime([3,3,5,1], 4, [0,3,5,0]) + [3, 6, 10, 1] + >>> calculate_turnaroundtime([3,3], 2, [0,3]) + [3, 6] + >>> calculate_turnaroundtime([8,10,1], 3, [1,0,3]) + [9, 10, 4] + """ + turn_around_time = [0] * no_of_processes + for i in range(no_of_processes): + turn_around_time[i] = burst_time[i] + waiting_time[i] + return turn_around_time + + +def calculate_average_times( + waiting_time: List[int], turn_around_time: List[int], no_of_processes: int +) -> None: + """ + This function calculates the average of the waiting & turnaround times + Prints: Average Waiting time & Average Turn Around Time + >>> calculate_average_times([0,3,5,0],[3,6,10,1],4) + Average waiting time = 2.00000 + Average turn around time = 5.0 + >>> calculate_average_times([2,3],[3,6],2) + Average waiting time = 2.50000 + Average turn around time = 4.5 + >>> calculate_average_times([10,4,3],[2,7,6],3) + Average waiting time = 5.66667 + Average turn around time = 5.0 + """ + total_waiting_time = 0 + total_turn_around_time = 0 + for i in range(no_of_processes): + total_waiting_time = total_waiting_time + waiting_time[i] + total_turn_around_time = total_turn_around_time + turn_around_time[i] + print("Average waiting time = %.5f" % (total_waiting_time / no_of_processes)) + print("Average turn around time =", total_turn_around_time / no_of_processes) + + +if __name__ == "__main__": + print("Enter how many process you want to analyze") + no_of_processes = int(input()) + burst_time = [0] * no_of_processes + arrival_time = [0] * no_of_processes + processes = list(range(1, no_of_processes + 1)) + + for i in range(no_of_processes): + print("Enter the arrival time and brust time for process:--" + str(i + 1)) + arrival_time[i], burst_time[i] = map(int, input().split()) + + waiting_time = calculate_waitingtime(arrival_time, burst_time, no_of_processes) + + bt = burst_time + n = no_of_processes + wt = waiting_time + turn_around_time = calculate_turnaroundtime(bt, n, wt) + + calculate_average_times(waiting_time, turn_around_time, no_of_processes) + + fcfs = pd.DataFrame( + list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)), + columns=[ + "Process", + "BurstTime", + "ArrivalTime", + "WaitingTime", + "TurnAroundTime", + ], + ) + + # Printing the dataFrame + pd.set_option("display.max_rows", fcfs.shape[0] + 1) + print(fcfs) diff --git a/scripts/__init__.py b/scripts/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/scripts/build_directory_md.py b/scripts/build_directory_md.py new file mode 100755 index 000000000000..7a4bc3a4b258 --- /dev/null +++ b/scripts/build_directory_md.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import os +from typing import Iterator + +URL_BASE = "https://github.com/TheAlgorithms/Python/blob/master" + + +def good_file_paths(top_dir: str = ".") -> Iterator[str]: + for dir_path, dir_names, filenames in os.walk(top_dir): + dir_names[:] = [d for d in dir_names if d != "scripts" and d[0] not in "._"] + for filename in filenames: + if filename == "__init__.py": + continue + if os.path.splitext(filename)[1] in (".py", ".ipynb"): + yield os.path.join(dir_path, filename).lstrip("./") + + +def md_prefix(i): + return f"{i * ' '}*" if i else "\n##" + + +def print_path(old_path: str, new_path: str) -> str: + old_parts = old_path.split(os.sep) + for i, new_part in enumerate(new_path.split(os.sep)): + if i + 1 > len(old_parts) or old_parts[i] != new_part: + if new_part: + print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") + return new_path + + +def print_directory_md(top_dir: str = ".") -> None: + old_path = "" + for filepath in sorted(good_file_paths(top_dir)): + filepath, filename = os.path.split(filepath) + if filepath != old_path: + old_path = print_path(old_path, filepath) + indent = (filepath.count(os.sep) + 1) if filepath else 0 + url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") + filename = os.path.splitext(filename.replace("_", " ").title())[0] + print(f"{md_prefix(indent)} [{filename}]({url})") + + +if __name__ == "__main__": + print_directory_md(".") diff --git a/scripts/project_euler_answers.json b/scripts/project_euler_answers.json new file mode 100644 index 000000000000..05c144d1e06a --- /dev/null +++ b/scripts/project_euler_answers.json @@ -0,0 +1,727 @@ +{ + "001": "233168", + "002": "4613732", + "003": "6857", + "004": "906609", + "005": "232792560", + "006": "25164150", + "007": "104743", + "008": "23514624000", + "009": "31875000", + "010": "142913828922", + "011": "70600674", + "012": "76576500", + "013": "5537376230", + "014": "837799", + "015": "137846528820", + "016": "1366", + "017": "21124", + "018": "1074", + "019": "171", + "020": "648", + "021": "31626", + "022": "871198282", + "023": "4179871", + "024": "2783915460", + "025": "4782", + "026": "983", + "027": "-59231", + "028": "669171001", + "029": "9183", + "030": "443839", + "031": "73682", + "032": "45228", + "033": "100", + "034": "40730", + "035": "55", + "036": "872187", + "037": "748317", + "038": "932718654", + "039": "840", + "040": "210", + "041": "7652413", + "042": "162", + "043": "16695334890", + "044": "5482660", + "045": "1533776805", + "046": "5777", + "047": "134043", + "048": "9110846700", + "049": "296962999629", + "050": "997651", + "051": "121313", + "052": "142857", + "053": "4075", + "054": "376", + "055": "249", + "056": "972", + "057": "153", + "058": "26241", + "059": "129448", + "060": "26033", + "061": "28684", + "062": "127035954683", + "063": "49", + "064": "1322", + "065": "272", + "066": "661", + "067": "7273", + "068": "6531031914842725", + "069": "510510", + "070": "8319823", + "071": "428570", + "072": "303963552391", + "073": "7295372", + "074": "402", + "075": "161667", + "076": "190569291", + "077": "71", + "078": "55374", + "079": "73162890", + "080": "40886", + "081": "427337", + "082": "260324", + "083": "425185", + "084": "101524", + "085": "2772", + "086": "1818", + "087": "1097343", + "088": "7587457", + "089": "743", + "090": "1217", + "091": "14234", + "092": "8581146", + "093": "1258", + "094": "518408346", + "095": "14316", + "096": "24702", + "097": "8739992577", + "098": "18769", + "099": "709", + "100": "756872327473", + "101": "37076114526", + "102": "228", + "103": "20313839404245", + "104": "329468", + "105": "73702", + "106": "21384", + "107": "259679", + "108": "180180", + "109": "38182", + "110": "9350130049860600", + "111": "612407567715", + "112": "1587000", + "113": "51161058134250", + "114": "16475640049", + "115": "168", + "116": "20492570929", + "117": "100808458960497", + "118": "44680", + "119": "248155780267521", + "120": "333082500", + "121": "2269", + "122": "1582", + "123": "21035", + "124": "21417", + "125": "2906969179", + "126": "18522", + "127": "18407904", + "128": "14516824220", + "129": "1000023", + "130": "149253", + "131": "173", + "132": "843296", + "133": "453647705", + "134": "18613426663617118", + "135": "4989", + "136": "2544559", + "137": "1120149658760", + "138": "1118049290473932", + "139": "10057761", + "140": "5673835352990", + "141": "878454337159", + "142": "1006193", + "143": "30758397", + "144": "354", + "145": "608720", + "146": "676333270", + "147": "846910284", + "148": "2129970655314432", + "149": "52852124", + "150": "-271248680", + "151": "0.464399", + "152": "301", + "153": "17971254122360635", + "154": "479742450", + "155": "3857447", + "156": "21295121502550", + "157": "53490", + "158": "409511334375", + "159": "14489159", + "160": "16576", + "161": "20574308184277971", + "162": "3D58725572C62302", + "163": "343047", + "164": "378158756814587", + "165": "2868868", + "166": "7130034", + "167": "3916160068885", + "168": "59206", + "169": "178653872807", + "170": "9857164023", + "171": "142989277", + "172": "227485267000992000", + "173": "1572729", + "174": "209566", + "175": "1,13717420,8", + "176": "96818198400000", + "177": "129325", + "178": "126461847755", + "179": "986262", + "180": "285196020571078987", + "181": "83735848679360680", + "182": "399788195976", + "183": "48861552", + "184": "1725323624056", + "185": "4640261571849533", + "186": "2325629", + "187": "17427258", + "188": "95962097", + "189": "10834893628237824", + "190": "371048281", + "191": "1918080160", + "192": "57060635927998347", + "193": "684465067343069", + "194": "61190912", + "195": "75085391", + "196": "322303240771079935", + "197": "1.710637717", + "198": "52374425", + "199": "0.00396087", + "200": "229161792008", + "201": "115039000", + "202": "1209002624", + "203": "34029210557338", + "204": "2944730", + "205": "0.5731441", + "206": "1389019170", + "207": "44043947822", + "208": "331951449665644800", + "209": "15964587728784", + "210": "1598174770174689458", + "211": "1922364685", + "212": "328968937309", + "213": "330.721154", + "214": "1677366278943", + "215": "806844323190414", + "216": "5437849", + "217": "6273134", + "218": "0", + "219": "64564225042", + "220": "139776,963904", + "221": "1884161251122450", + "222": "1590933", + "223": "61614848", + "224": "4137330", + "225": "2009", + "226": "0.11316017", + "227": "3780.618622", + "228": "86226", + "229": "11325263", + "230": "850481152593119296", + "231": "7526965179680", + "232": "0.83648556", + "233": "271204031455541309", + "234": "1259187438574927161", + "235": "1.002322108633", + "236": "123/59", + "237": "15836928", + "238": "9922545104535661", + "239": "0.001887854841", + "240": "7448717393364181966", + "241": "482316491800641154", + "242": "997104142249036713", + "243": "892371480", + "244": "96356848", + "245": "288084712410001", + "246": "810834388", + "247": "782252", + "248": "23507044290", + "249": "9275262564250418", + "250": "1425480602091519", + "251": "18946051", + "252": "104924.0", + "253": "11.492847", + "254": "8184523820510", + "255": "4.4474011180", + "256": "85765680", + "257": "139012411", + "258": "12747994", + "259": "20101196798", + "260": "167542057", + "261": "238890850232021", + "262": "2531.205", + "263": "2039506520", + "264": "2816417.1055", + "265": "209110240768", + "266": "1096883702440585", + "267": "0.999992836187", + "268": "785478606870985", + "269": "1311109198529286", + "270": "82282080", + "271": "4617456485273129588", + "272": "8495585919506151122", + "273": "2032447591196869022", + "274": "1601912348822", + "275": "15030564", + "276": "5777137137739632912", + "277": "1125977393124310", + "278": "1228215747273908452", + "279": "416577688", + "280": "430.088247", + "281": "1485776387445623", + "282": "1098988351", + "283": "28038042525570324", + "284": "5a411d7b", + "285": "157055.80999", + "286": "52.6494571953", + "287": "313135496", + "288": "605857431263981935", + "289": "6567944538", + "290": "20444710234716473", + "291": "4037526", + "292": "3600060866", + "293": "2209", + "294": "789184709", + "295": "4884650818", + "296": "1137208419", + "297": "2252639041804718029", + "298": "1.76882294", + "299": "549936643", + "300": "8.0540771484375", + "301": "2178309", + "302": "1170060", + "303": "1111981904675169", + "304": "283988410192", + "305": "18174995535140", + "306": "852938", + "307": "0.7311720251", + "308": "1539669807660924", + "309": "210139", + "310": "2586528661783", + "311": "2466018557", + "312": "324681947", + "313": "2057774861813004", + "314": "132.52756426", + "315": "13625242", + "316": "542934735751917735", + "317": "1856532.8455", + "318": "709313889", + "319": "268457129", + "320": "278157919195482643", + "321": "2470433131948040", + "322": "999998760323313995", + "323": "6.3551758451", + "324": "96972774", + "325": "54672965", + "326": "1966666166408794329", + "327": "34315549139516", + "328": "260511850222", + "329": "199740353/29386561536000", + "330": "15955822", + "331": "467178235146843549", + "332": "2717.751525", + "333": "3053105", + "334": "150320021261690835", + "335": "5032316", + "336": "CAGBIHEFJDK", + "337": "85068035", + "338": "15614292", + "339": "19823.542204", + "340": "291504964", + "341": "56098610614277014", + "342": "5943040885644", + "343": "269533451410884183", + "344": "65579304332", + "345": "13938", + "346": "336108797689259276", + "347": "11109800204052", + "348": "1004195061", + "349": "115384615384614952", + "350": "84664213", + "351": "11762187201804552", + "352": "378563.260589", + "353": "1.2759860331", + "354": "58065134", + "355": "1726545007", + "356": "28010159", + "357": "1739023853137", + "358": "3284144505", + "359": "40632119", + "360": "878825614395267072", + "361": "178476944", + "362": "457895958010", + "363": "0.0000372091", + "364": "44855254", + "365": "162619462356610313", + "366": "88351299", + "367": "48271207", + "368": "253.6135092068", + "369": "862400558448", + "370": "41791929448408", + "371": "40.66368097", + "372": "301450082318807027", + "373": "727227472448913", + "374": "334420941", + "375": "7435327983715286168", + "376": "973059630185670", + "377": "732385277", + "378": "147534623725724718", + "379": "132314136838185", + "380": "6.3202e25093", + "381": "139602943319822", + "382": "697003956", + "383": "22173624649806", + "384": "3354706415856332783", + "385": "3776957309612153700", + "386": "528755790", + "387": "696067597313468", + "388": "831907372805129931", + "389": "2406376.3623", + "390": "2919133642971", + "391": "61029882288", + "392": "3.1486734435", + "393": "112398351350823112", + "394": "3.2370342194", + "395": "28.2453753155", + "396": "173214653", + "397": "141630459461893728", + "398": "2010.59096", + "399": "1508395636674243,6.5e27330467", + "400": "438505383468410633", + "401": "281632621", + "402": "356019862", + "403": "18224771", + "404": "1199215615081353", + "405": "237696125", + "406": "36813.12757207", + "407": "39782849136421", + "408": "299742733", + "409": "253223948", + "410": "799999783589946560", + "411": "9936352", + "412": "38788800", + "413": "3079418648040719", + "414": "552506775824935461", + "415": "55859742", + "416": "898082747", + "417": "446572970925740", + "418": "1177163565297340320", + "419": "998567458,1046245404,43363922", + "420": "145159332", + "421": "2304215802083466198", + "422": "92060460", + "423": "653972374", + "424": "1059760019628", + "425": "46479497324", + "426": "31591886008", + "427": "97138867", + "428": "747215561862", + "429": "98792821", + "430": "5000624921.38", + "431": "23.386029052", + "432": "754862080", + "433": "326624372659664", + "434": "863253606", + "435": "252541322550", + "436": "0.5276662759", + "437": "74204709657207", + "438": "2046409616809", + "439": "968697378", + "440": "970746056", + "441": "5000088.8395", + "442": "1295552661530920149", + "443": "2744233049300770", + "444": "1.200856722e263", + "445": "659104042", + "446": "907803852", + "447": "530553372", + "448": "106467648", + "449": "103.37870096", + "450": "583333163984220940", + "451": "153651073760956", + "452": "345558983", + "453": "104354107", + "454": "5435004633092", + "455": "450186511399999", + "456": "333333208685971546", + "457": "2647787126797397063", + "458": "423341841", + "459": "3996390106631", + "460": "18.420738199", + "461": "159820276", + "462": "5.5350769703e1512", + "463": "808981553", + "464": "198775297232878", + "465": "585965659", + "466": "258381958195474745", + "467": "775181359", + "468": "852950321", + "469": "0.56766764161831", + "470": "147668794", + "471": "1.895093981e31", + "472": "73811586", + "473": "35856681704365", + "474": "9690646731515010", + "475": "75780067", + "476": "110242.87794", + "477": "25044905874565165", + "478": "59510340", + "479": "191541795", + "480": "turnthestarson", + "481": "729.12106947", + "482": "1400824879147", + "483": "4.993401567e22", + "484": "8907904768686152599", + "485": "51281274340", + "486": "11408450515", + "487": "106650212746", + "488": "216737278", + "489": "1791954757162", + "490": "777577686", + "491": "194505988824000", + "492": "242586962923928", + "493": "6.818741802", + "494": "2880067194446832666", + "495": "789107601", + "496": "2042473533769142717", + "497": "684901360", + "498": "472294837", + "499": "0.8660312", + "500": "35407281", + "501": "197912312715", + "502": "749485217", + "503": "3.8694550145", + "504": "694687", + "505": "714591308667615832", + "506": "18934502", + "507": "316558047002627270", + "508": "891874596", + "509": "151725678", + "510": "315306518862563689", + "511": "935247012", + "512": "50660591862310323", + "513": "2925619196", + "514": "8986.86698", + "515": "2422639000800", + "516": "939087315", + "517": "581468882", + "518": "100315739184392", + "519": "804739330", + "520": "238413705", + "521": "44389811", + "522": "96772715", + "523": "37125450.44", + "524": "2432925835413407847", + "525": "44.69921807", + "526": "49601160286750947", + "527": "11.92412011", + "528": "779027989", + "529": "23624465", + "530": "207366437157977206", + "531": "4515432351156203105", + "532": "827306.56", + "533": "789453601", + "534": "11726115562784664", + "535": "611778217", + "536": "3557005261906288", + "537": "779429131", + "538": "22472871503401097", + "539": "426334056", + "540": "500000000002845", + "541": "4580726482872451", + "542": "697586734240314852", + "543": "199007746081234640", + "544": "640432376", + "545": "921107572", + "546": "215656873", + "547": "11730879.0023", + "548": "12144044603581281", + "549": "476001479068717", + "550": "328104836", + "551": "73597483551591773", + "552": "326227335", + "553": "57717170", + "554": "89539872", + "555": "208517717451208352", + "556": "52126939292957", + "557": "2699929328", + "558": "226754889", + "559": "684724920", + "560": "994345168", + "561": "452480999988235494", + "562": "51208732914368", + "563": "27186308211734760", + "564": "12363.698850", + "565": "2992480851924313898", + "566": "329569369413585", + "567": "75.44817535", + "568": "4228020", + "569": "21025060", + "570": "271197444", + "571": "30510390701978", + "572": "19737656", + "573": "1252.9809", + "574": "5780447552057000454", + "575": "0.000989640561", + "576": "344457.5871", + "577": "265695031399260211", + "578": "9219696799346", + "579": "3805524", + "580": "2327213148095366", + "581": "2227616372734", + "582": "19903", + "583": "1174137929000", + "584": "32.83822408", + "585": "17714439395932", + "586": "82490213", + "587": "2240", + "588": "11651930052", + "589": "131776959.25", + "590": "834171904", + "591": "526007984625966", + "592": "13415DF2BE9C", + "593": "96632320042.0", + "594": "47067598", + "595": "54.17529329", + "596": "734582049", + "597": "0.5001817828", + "598": "543194779059", + "599": "12395526079546335", + "600": "2668608479740672", + "601": "1617243", + "602": "269496760", + "603": "879476477", + "604": "1398582231101", + "605": "59992576", + "606": "158452775", + "607": "13.1265108586", + "608": "439689828", + "609": "172023848", + "610": "319.30207833", + "611": "49283233900", + "612": "819963842", + "613": "0.3916721504", + "614": "130694090", + "615": "108424772", + "616": "310884668312456458", + "617": "1001133757", + "618": "634212216", + "619": "857810883", + "620": "1470337306", + "621": "11429712", + "622": "3010983666182123972", + "623": "3679796", + "624": "984524441", + "625": "551614306", + "626": "695577663", + "627": "220196142", + "628": "210286684", + "629": "626616617", + "630": "9669182880384", + "631": "869588692", + "632": "728378714", + "633": "1.0012e-10", + "634": "4019680944", + "635": "689294705", + "636": "888316", + "637": "49000634845039", + "638": "18423394", + "639": "797866893", + "640": "50.317928", + "641": "793525366", + "642": "631499044", + "643": "968274154", + "644": "20.11208767", + "645": "48894.2174", + "646": "845218467", + "647": "563132994232918611", + "648": "301483197", + "649": "924668016", + "650": "538319652", + "651": "448233151", + "652": "983924497", + "653": "1130658687", + "654": "815868280", + "655": "2000008332", + "656": "888873503555187", + "657": "219493139", + "658": "958280177", + "659": "238518915714422000", + "660": "474766783", + "661": "646231.2177", + "662": "860873428", + "663": "1884138010064752", + "664": "35295862", + "665": "11541685709674", + "666": "0.48023168", + "667": "1.5276527928", + "668": "2811077773", + "669": "56342087360542122", + "670": "551055065", + "671": "946106780", + "672": "91627537", + "673": "700325380", + "674": "416678753", + "675": "416146418", + "676": "3562668074339584", + "677": "984183023", + "678": "1986065", + "679": "644997092988678", + "680": "563917241", + "681": "2611227421428", + "682": "290872710", + "683": "2.38955315e11", + "684": "922058210", + "685": "662878999", + "686": "193060223", + "687": "0.3285320869", + "688": "110941813", + "689": "0.56565454", + "690": "415157690", + "691": "11570761", + "692": "842043391019219959", + "693": "699161", + "694": "1339784153569958487", + "695": "0.1017786859", + "696": "436944244", + "697": "4343871.06", + "698": "57808202", + "699": "37010438774467572", + "700": "1517926517777556", + "701": "13.51099836", + "702": "622305608172525546", + "703": "843437991", + "704": "501985601490518144", + "705": "480440153", + "706": "884837055", + "707": "652907799", + "708": "28874142998632109", + "709": "773479144", + "710": "1275000", + "711": "541510990", + "712": "413876461", + "713": "788626351539895", + "714": "2.452767775565e20", + "715": "883188017", + "716": "238948623", + "717": "1603036763131", + "718": "228579116", + "719": "128088830547982", + "720": "688081048", + "721": "700792959", + "722": "3.376792776502e132", + "723": "1395793419248", + "724": "18128250110", + "725": "4598797036650685" +} diff --git a/scripts/validate_filenames.py b/scripts/validate_filenames.py new file mode 100755 index 000000000000..419295fe679d --- /dev/null +++ b/scripts/validate_filenames.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +import os + +try: + from .build_directory_md import good_file_paths +except ImportError: + from build_directory_md import good_file_paths + +filepaths = list(good_file_paths()) +assert filepaths, "good_file_paths() failed!" + +upper_files = [file for file in filepaths if file != file.lower()] +if upper_files: + print(f"{len(upper_files)} files contain uppercase characters:") + print("\n".join(upper_files) + "\n") + +space_files = [file for file in filepaths if " " in file] +if space_files: + print(f"{len(space_files)} files contain space characters:") + print("\n".join(space_files) + "\n") + +hyphen_files = [file for file in filepaths if "-" in file] +if hyphen_files: + print(f"{len(hyphen_files)} files contain hyphen characters:") + print("\n".join(hyphen_files) + "\n") + +nodir_files = [file for file in filepaths if os.sep not in file] +if nodir_files: + print(f"{len(nodir_files)} files are not in a directory:") + print("\n".join(nodir_files) + "\n") + +bad_files = len(upper_files + space_files + hyphen_files + nodir_files) +if bad_files: + import sys + + sys.exit(bad_files) diff --git a/scripts/validate_solutions.py b/scripts/validate_solutions.py new file mode 100755 index 000000000000..fd804ea5aa31 --- /dev/null +++ b/scripts/validate_solutions.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +import importlib.util +import json +import os +import pathlib +from types import ModuleType +from typing import Dict, List + +import pytest +import requests + +PROJECT_EULER_DIR_PATH = pathlib.Path.cwd().joinpath("project_euler") +PROJECT_EULER_ANSWERS_PATH = pathlib.Path.cwd().joinpath( + "scripts", "project_euler_answers.json" +) + +with open(PROJECT_EULER_ANSWERS_PATH) as file_handle: + PROBLEM_ANSWERS: Dict[str, str] = json.load(file_handle) + + +def convert_path_to_module(file_path: pathlib.Path) -> ModuleType: + """Converts a file path to a Python module""" + spec = importlib.util.spec_from_file_location(file_path.name, str(file_path)) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def all_solution_file_paths() -> List[pathlib.Path]: + """Collects all the solution file path in the Project Euler directory""" + solution_file_paths = [] + for problem_dir_path in PROJECT_EULER_DIR_PATH.iterdir(): + if problem_dir_path.is_file() or problem_dir_path.name.startswith("_"): + continue + for file_path in problem_dir_path.iterdir(): + if file_path.suffix != ".py" or file_path.name.startswith(("_", "test")): + continue + solution_file_paths.append(file_path) + return solution_file_paths + + +def get_files_url() -> str: + """Return the pull request number which triggered this action.""" + with open(os.environ["GITHUB_EVENT_PATH"]) as file: + event = json.load(file) + return event["pull_request"]["url"] + "/files" + + +def added_solution_file_path() -> List[pathlib.Path]: + """Collects only the solution file path which got added in the current + pull request. + + This will only be triggered if the script is ran from GitHub Actions. + """ + solution_file_paths = [] + headers = { + "Accept": "application/vnd.github.v3+json", + "Authorization": "token " + os.environ["GITHUB_TOKEN"], + } + files = requests.get(get_files_url(), headers=headers).json() + for file in files: + filepath = pathlib.Path.cwd().joinpath(file["filename"]) + if ( + filepath.suffix != ".py" + or filepath.name.startswith(("_", "test")) + or not filepath.name.startswith("sol") + ): + continue + solution_file_paths.append(filepath) + return solution_file_paths + + +def collect_solution_file_paths() -> List[pathlib.Path]: + if os.environ.get("CI") and os.environ.get("GITHUB_EVENT_NAME") == "pull_request": + # Return only if there are any, otherwise default to all solutions + if filepaths := added_solution_file_path(): + return filepaths + return all_solution_file_paths() + + +@pytest.mark.parametrize( + "solution_path", + collect_solution_file_paths(), + ids=lambda path: f"{path.parent.name}/{path.name}", +) +def test_project_euler(solution_path: pathlib.Path) -> None: + """Testing for all Project Euler solutions""" + # problem_[extract this part] and pad it with zeroes for width 3 + problem_number: str = solution_path.parent.name[8:].zfill(3) + expected: str = PROBLEM_ANSWERS[problem_number] + solution_module = convert_path_to_module(solution_path) + answer = str(solution_module.solution()) + assert answer == expected, f"Expected {expected} but got {answer}" diff --git a/searches/__init__.py b/searches/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/searches/binary_search.py b/searches/binary_search.py index 1d5da96586cd..35e0dd0596d2 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -1,24 +1,196 @@ +#!/usr/bin/env python3 + """ -This is pure python implementation of binary search algorithm +This is pure Python implementation of binary search algorithms For doctests run following command: -python -m doctest -v binary_search.py -or python3 -m doctest -v binary_search.py For manual testing run: -python binary_search.py +python3 binary_search.py """ -from __future__ import print_function import bisect +from typing import List, Optional + + +def bisect_left( + sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1 +) -> int: + """ + Locates the first element in a sorted array that is larger or equal to a given + value. + + It has the same interface as + https://docs.python.org/3/library/bisect.html#bisect.bisect_left . + + :param sorted_collection: some ascending sorted collection with comparable items + :param item: item to bisect + :param lo: lowest index to consider (as in sorted_collection[lo:hi]) + :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) + :return: index i such that all values in sorted_collection[lo:i] are < item and all + values in sorted_collection[i:hi] are >= item. + + Examples: + >>> bisect_left([0, 5, 7, 10, 15], 0) + 0 + + >>> bisect_left([0, 5, 7, 10, 15], 6) + 2 + + >>> bisect_left([0, 5, 7, 10, 15], 20) + 5 + + >>> bisect_left([0, 5, 7, 10, 15], 15, 1, 3) + 3 + + >>> bisect_left([0, 5, 7, 10, 15], 6, 2) + 2 + """ + if hi < 0: + hi = len(sorted_collection) + + while lo < hi: + mid = (lo + hi) // 2 + if sorted_collection[mid] < item: + lo = mid + 1 + else: + hi = mid + + return lo + + +def bisect_right( + sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1 +) -> int: + """ + Locates the first element in a sorted array that is larger than a given value. + + It has the same interface as + https://docs.python.org/3/library/bisect.html#bisect.bisect_right . + + :param sorted_collection: some ascending sorted collection with comparable items + :param item: item to bisect + :param lo: lowest index to consider (as in sorted_collection[lo:hi]) + :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) + :return: index i such that all values in sorted_collection[lo:i] are <= item and + all values in sorted_collection[i:hi] are > item. + + Examples: + >>> bisect_right([0, 5, 7, 10, 15], 0) + 1 + + >>> bisect_right([0, 5, 7, 10, 15], 15) + 5 + + >>> bisect_right([0, 5, 7, 10, 15], 6) + 2 + + >>> bisect_right([0, 5, 7, 10, 15], 15, 1, 3) + 3 + + >>> bisect_right([0, 5, 7, 10, 15], 6, 2) + 2 + """ + if hi < 0: + hi = len(sorted_collection) + + while lo < hi: + mid = (lo + hi) // 2 + if sorted_collection[mid] <= item: + lo = mid + 1 + else: + hi = mid + + return lo -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 +def insort_left( + sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1 +) -> None: + """ + Inserts a given value into a sorted array before other values with the same value. -def binary_search(sorted_collection, item): + It has the same interface as + https://docs.python.org/3/library/bisect.html#bisect.insort_left . + + :param sorted_collection: some ascending sorted collection with comparable items + :param item: item to insert + :param lo: lowest index to consider (as in sorted_collection[lo:hi]) + :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) + + Examples: + >>> sorted_collection = [0, 5, 7, 10, 15] + >>> insort_left(sorted_collection, 6) + >>> sorted_collection + [0, 5, 6, 7, 10, 15] + + >>> sorted_collection = [(0, 0), (5, 5), (7, 7), (10, 10), (15, 15)] + >>> item = (5, 5) + >>> insort_left(sorted_collection, item) + >>> sorted_collection + [(0, 0), (5, 5), (5, 5), (7, 7), (10, 10), (15, 15)] + >>> item is sorted_collection[1] + True + >>> item is sorted_collection[2] + False + + >>> sorted_collection = [0, 5, 7, 10, 15] + >>> insort_left(sorted_collection, 20) + >>> sorted_collection + [0, 5, 7, 10, 15, 20] + + >>> sorted_collection = [0, 5, 7, 10, 15] + >>> insort_left(sorted_collection, 15, 1, 3) + >>> sorted_collection + [0, 5, 7, 15, 10, 15] + """ + sorted_collection.insert(bisect_left(sorted_collection, item, lo, hi), item) + + +def insort_right( + sorted_collection: List[int], item: int, lo: int = 0, hi: int = -1 +) -> None: + """ + Inserts a given value into a sorted array after other values with the same value. + + It has the same interface as + https://docs.python.org/3/library/bisect.html#bisect.insort_right . + + :param sorted_collection: some ascending sorted collection with comparable items + :param item: item to insert + :param lo: lowest index to consider (as in sorted_collection[lo:hi]) + :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) + + Examples: + >>> sorted_collection = [0, 5, 7, 10, 15] + >>> insort_right(sorted_collection, 6) + >>> sorted_collection + [0, 5, 6, 7, 10, 15] + + >>> sorted_collection = [(0, 0), (5, 5), (7, 7), (10, 10), (15, 15)] + >>> item = (5, 5) + >>> insort_right(sorted_collection, item) + >>> sorted_collection + [(0, 0), (5, 5), (5, 5), (7, 7), (10, 10), (15, 15)] + >>> item is sorted_collection[1] + False + >>> item is sorted_collection[2] + True + + >>> sorted_collection = [0, 5, 7, 10, 15] + >>> insort_right(sorted_collection, 20) + >>> sorted_collection + [0, 5, 7, 10, 15, 20] + + >>> sorted_collection = [0, 5, 7, 10, 15] + >>> insort_right(sorted_collection, 15, 1, 3) + >>> sorted_collection + [0, 5, 7, 15, 10, 15] + """ + sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item) + + +def binary_search(sorted_collection: List[int], item: int) -> Optional[int]: """Pure implementation of binary search algorithm in Python Be careful collection must be ascending sorted, otherwise result will be @@ -45,19 +217,18 @@ def binary_search(sorted_collection, item): right = len(sorted_collection) - 1 while left <= right: - midpoint = (left + right) // 2 + midpoint = left + (right - left) // 2 current_item = sorted_collection[midpoint] if current_item == item: return midpoint + elif item < current_item: + right = midpoint - 1 else: - if item < current_item: - right = midpoint - 1 - else: - left = midpoint + 1 + left = midpoint + 1 return None -def binary_search_std_lib(sorted_collection, item): +def binary_search_std_lib(sorted_collection: List[int], item: int) -> Optional[int]: """Pure implementation of binary search algorithm in Python using stdlib Be careful collection must be ascending sorted, otherwise result will be @@ -85,7 +256,10 @@ def binary_search_std_lib(sorted_collection, item): return index return None -def binary_search_by_recursion(sorted_collection, item, left, right): + +def binary_search_by_recursion( + sorted_collection: List[int], item: int, left: int, right: int +) -> Optional[int]: """Pure implementation of binary search algorithm in Python by recursion @@ -98,64 +272,37 @@ def binary_search_by_recursion(sorted_collection, item, left, right): :return: index of found item or None if item is not found Examples: - >>> binary_search_std_lib([0, 5, 7, 10, 15], 0) + >>> binary_search_by_recursion([0, 5, 7, 10, 15], 0, 0, 4) 0 - >>> binary_search_std_lib([0, 5, 7, 10, 15], 15) + >>> binary_search_by_recursion([0, 5, 7, 10, 15], 15, 0, 4) 4 - >>> binary_search_std_lib([0, 5, 7, 10, 15], 5) + >>> binary_search_by_recursion([0, 5, 7, 10, 15], 5, 0, 4) 1 - >>> binary_search_std_lib([0, 5, 7, 10, 15], 6) + >>> binary_search_by_recursion([0, 5, 7, 10, 15], 6, 0, 4) """ - if (right < left): + if right < left: return None - + midpoint = left + (right - left) // 2 if sorted_collection[midpoint] == item: return midpoint elif sorted_collection[midpoint] > item: - return binary_search_by_recursion(sorted_collection, item, left, midpoint-1) + return binary_search_by_recursion(sorted_collection, item, left, midpoint - 1) else: - return binary_search_by_recursion(sorted_collection, item, midpoint+1, right) - -def __assert_sorted(collection): - """Check if collection is ascending sorted, if not - raises :py:class:`ValueError` + return binary_search_by_recursion(sorted_collection, item, midpoint + 1, right) - :param collection: collection - :return: True if collection is ascending sorted - :raise: :py:class:`ValueError` if collection is not ascending sorted - Examples: - >>> __assert_sorted([0, 1, 2, 4]) - True - - >>> __assert_sorted([10, -1, 5]) - Traceback (most recent call last): - ... - ValueError: Collection must be ascending sorted - """ - if collection != sorted(collection): - raise ValueError('Collection must be ascending sorted') - return True - - -if __name__ == '__main__': - import sys - user_input = raw_input('Enter numbers separated by comma:\n').strip() - collection = [int(item) for item in user_input.split(',')] - try: - __assert_sorted(collection) - except ValueError: - sys.exit('Sequence must be ascending sorted to apply binary search') - - target_input = raw_input('Enter a single number to be found in the list:\n') - target = int(target_input) +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n").strip() + collection = sorted(int(item) for item in user_input.split(",")) + target = int(input("Enter a single number to be found in the list:\n")) result = binary_search(collection, target) - if result is not None: - print('{} found at positions: {}'.format(target, result)) + if result is None: + print(f"{target} was not found in {collection}.") else: - print('Not found') + print(f"{target} was found at position {result} in {collection}.") diff --git a/searches/double_linear_search.py b/searches/double_linear_search.py new file mode 100644 index 000000000000..d9dad3c685b6 --- /dev/null +++ b/searches/double_linear_search.py @@ -0,0 +1,37 @@ +from __future__ import annotations + + +def double_linear_search(array: list[int], search_item: int) -> int: + """ + Iterate through the array from both sides to find the index of search_item. + + :param array: the array to be searched + :param search_item: the item to be searched + :return the index of search_item, if search_item is in array, else -1 + + Examples: + >>> double_linear_search([1, 5, 5, 10], 1) + 0 + >>> double_linear_search([1, 5, 5, 10], 5) + 1 + >>> double_linear_search([1, 5, 5, 10], 100) + -1 + >>> double_linear_search([1, 5, 5, 10], 10) + 3 + """ + # define the start and end index of the given array + start_ind, end_ind = 0, len(array) - 1 + while start_ind <= end_ind: + if array[start_ind] == search_item: + return start_ind + elif array[end_ind] == search_item: + return end_ind + else: + start_ind += 1 + end_ind -= 1 + # returns -1 if search_item is not found in array + return -1 + + +if __name__ == "__main__": + print(double_linear_search(list(range(100)), 40)) diff --git a/searches/double_linear_search_recursion.py b/searches/double_linear_search_recursion.py new file mode 100644 index 000000000000..1c483e974ca7 --- /dev/null +++ b/searches/double_linear_search_recursion.py @@ -0,0 +1,35 @@ +def search(list_data: list, key: int, left: int = 0, right: int = 0) -> int: + """ + Iterate through the array to find the index of key using recursion. + :param list_data: the list to be searched + :param key: the key to be searched + :param left: the index of first element + :param right: the index of last element + :return: the index of key value if found, -1 otherwise. + + >>> search(list(range(0, 11)), 5) + 5 + >>> search([1, 2, 4, 5, 3], 4) + 2 + >>> search([1, 2, 4, 5, 3], 6) + -1 + >>> search([5], 5) + 0 + >>> search([], 1) + -1 + """ + right = right or len(list_data) - 1 + if left > right: + return -1 + elif list_data[left] == key: + return left + elif list_data[right] == key: + return right + else: + return search(list_data, key, left + 1, right - 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/searches/fibonacci_search.py b/searches/fibonacci_search.py new file mode 100644 index 000000000000..ac8ecc99a187 --- /dev/null +++ b/searches/fibonacci_search.py @@ -0,0 +1,132 @@ +""" +This is pure Python implementation of fibonacci search. + +Resources used: +https://en.wikipedia.org/wiki/Fibonacci_search_technique + +For doctests run following command: +python3 -m doctest -v fibonacci_search.py + +For manual testing run: +python3 fibonacci_search.py +""" +from functools import lru_cache + + +@lru_cache() +def fibonacci(k: int) -> int: + """Finds fibonacci number in index k. + + Parameters + ---------- + k : + Index of fibonacci. + + Returns + ------- + int + Fibonacci number in position k. + + >>> fibonacci(0) + 0 + >>> fibonacci(2) + 1 + >>> fibonacci(5) + 5 + >>> fibonacci(15) + 610 + >>> fibonacci('a') + Traceback (most recent call last): + TypeError: k must be an integer. + >>> fibonacci(-5) + Traceback (most recent call last): + ValueError: k integer must be greater or equal to zero. + """ + if not isinstance(k, int): + raise TypeError("k must be an integer.") + if k < 0: + raise ValueError("k integer must be greater or equal to zero.") + if k == 0: + return 0 + elif k == 1: + return 1 + else: + return fibonacci(k - 1) + fibonacci(k - 2) + + +def fibonacci_search(arr: list, val: int) -> int: + """A pure Python implementation of a fibonacci search algorithm. + + Parameters + ---------- + arr + List of sorted elements. + val + Element to search in list. + + Returns + ------- + int + The index of the element in the array. + -1 if the element is not found. + + >>> fibonacci_search([4, 5, 6, 7], 4) + 0 + >>> fibonacci_search([4, 5, 6, 7], -10) + -1 + >>> fibonacci_search([-18, 2], -18) + 0 + >>> fibonacci_search([5], 5) + 0 + >>> fibonacci_search(['a', 'c', 'd'], 'c') + 1 + >>> fibonacci_search(['a', 'c', 'd'], 'f') + -1 + >>> fibonacci_search([], 1) + -1 + >>> fibonacci_search([.1, .4 , 7], .4) + 1 + >>> fibonacci_search([], 9) + -1 + >>> fibonacci_search(list(range(100)), 63) + 63 + >>> fibonacci_search(list(range(100)), 99) + 99 + >>> fibonacci_search(list(range(-100, 100, 3)), -97) + 1 + >>> fibonacci_search(list(range(-100, 100, 3)), 0) + -1 + >>> fibonacci_search(list(range(-100, 100, 5)), 0) + 20 + >>> fibonacci_search(list(range(-100, 100, 5)), 95) + 39 + """ + len_list = len(arr) + # Find m such that F_m >= n where F_i is the i_th fibonacci number. + i = 0 + while True: + if fibonacci(i) >= len_list: + fibb_k = i + break + i += 1 + offset = 0 + while fibb_k > 0: + index_k = min( + offset + fibonacci(fibb_k - 1), len_list - 1 + ) # Prevent out of range + item_k_1 = arr[index_k] + if item_k_1 == val: + return index_k + elif val < item_k_1: + fibb_k -= 1 + elif val > item_k_1: + offset += fibonacci(fibb_k - 1) + fibb_k -= 2 + else: + return -1 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py new file mode 100644 index 000000000000..70622ebefb4e --- /dev/null +++ b/searches/hill_climbing.py @@ -0,0 +1,197 @@ +# https://en.wikipedia.org/wiki/Hill_climbing +import math + + +class SearchProblem: + """ + An interface to define search problems. + The interface will be illustrated using the example of mathematical function. + """ + + def __init__(self, x: int, y: int, step_size: int, function_to_optimize): + """ + The constructor of the search problem. + + x: the x coordinate of the current search state. + y: the y coordinate of the current search state. + step_size: size of the step to take when looking for neighbors. + function_to_optimize: a function to optimize having the signature f(x, y). + """ + self.x = x + self.y = y + self.step_size = step_size + self.function = function_to_optimize + + def score(self) -> int: + """ + Returns the output of the function called with current x and y coordinates. + >>> def test_function(x, y): + ... return x + y + >>> SearchProblem(0, 0, 1, test_function).score() # 0 + 0 = 0 + 0 + >>> SearchProblem(5, 7, 1, test_function).score() # 5 + 7 = 12 + 12 + """ + return self.function(self.x, self.y) + + def get_neighbors(self): + """ + Returns a list of coordinates of neighbors adjacent to the current coordinates. + + Neighbors: + | 0 | 1 | 2 | + | 3 | _ | 4 | + | 5 | 6 | 7 | + """ + step_size = self.step_size + return [ + SearchProblem(x, y, step_size, self.function) + for x, y in ( + (self.x - step_size, self.y - step_size), + (self.x - step_size, self.y), + (self.x - step_size, self.y + step_size), + (self.x, self.y - step_size), + (self.x, self.y + step_size), + (self.x + step_size, self.y - step_size), + (self.x + step_size, self.y), + (self.x + step_size, self.y + step_size), + ) + ] + + def __hash__(self): + """ + hash the string represetation of the current search state. + """ + return hash(str(self)) + + def __eq__(self, obj): + """ + Check if the 2 objects are equal. + """ + if isinstance(obj, SearchProblem): + return hash(str(self)) == hash(str(obj)) + return False + + def __str__(self): + """ + string representation of the current search state. + >>> str(SearchProblem(0, 0, 1, None)) + 'x: 0 y: 0' + >>> str(SearchProblem(2, 5, 1, None)) + 'x: 2 y: 5' + """ + return f"x: {self.x} y: {self.y}" + + +def hill_climbing( + search_prob, + find_max: bool = True, + max_x: float = math.inf, + min_x: float = -math.inf, + max_y: float = math.inf, + min_y: float = -math.inf, + visualization: bool = False, + max_iter: int = 10000, +) -> SearchProblem: + """ + Implementation of the hill climbling algorithm. + We start with a given state, find all its neighbors, + move towards the neighbor which provides the maximum (or minimum) change. + We keep doing this until we are at a state where we do not have any + neighbors which can improve the solution. + Args: + search_prob: The search state at the start. + find_max: If True, the algorithm should find the maximum else the minimum. + max_x, min_x, max_y, min_y: the maximum and minimum bounds of x and y. + visualization: If True, a matplotlib graph is displayed. + max_iter: number of times to run the iteration. + Returns a search state having the maximum (or minimum) score. + """ + current_state = search_prob + scores = [] # list to store the current score at each iteration + iterations = 0 + solution_found = False + visited = set() + while not solution_found and iterations < max_iter: + visited.add(current_state) + iterations += 1 + current_score = current_state.score() + scores.append(current_score) + neighbors = current_state.get_neighbors() + max_change = -math.inf + min_change = math.inf + next_state = None # to hold the next best neighbor + for neighbor in neighbors: + if neighbor in visited: + continue # do not want to visit the same state again + if ( + neighbor.x > max_x + or neighbor.x < min_x + or neighbor.y > max_y + or neighbor.y < min_y + ): + continue # neighbor outside our bounds + change = neighbor.score() - current_score + if find_max: # finding max + # going to direction with greatest ascent + if change > max_change and change > 0: + max_change = change + next_state = neighbor + else: # finding min + # to direction with greatest descent + if change < min_change and change < 0: + min_change = change + next_state = neighbor + if next_state is not None: + # we found at least one neighbor which improved the current state + current_state = next_state + else: + # since we have no neighbor that improves the solution we stop the search + solution_found = True + + if visualization: + from matplotlib import pyplot as plt + + plt.plot(range(iterations), scores) + plt.xlabel("Iterations") + plt.ylabel("Function values") + plt.show() + + return current_state + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + def test_f1(x, y): + return (x ** 2) + (y ** 2) + + # starting the problem with initial coordinates (3, 4) + prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) + local_min = hill_climbing(prob, find_max=False) + print( + "The minimum score for f(x, y) = x^2 + y^2 found via hill climbing: " + f"{local_min.score()}" + ) + + # starting the problem with initial coordinates (12, 47) + prob = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_f1) + local_min = hill_climbing( + prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True + ) + print( + "The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 " + f"and 50 > y > - 5 found via hill climbing: {local_min.score()}" + ) + + def test_f2(x, y): + return (3 * x ** 2) - (6 * y) + + prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) + local_min = hill_climbing(prob, find_max=True) + print( + "The maximum score for f(x, y) = x^2 + y^2 found via hill climbing: " + f"{local_min.score()}" + ) diff --git a/searches/interpolation_search.py b/searches/interpolation_search.py index db9893bdb5d4..f4fa8e1203df 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -1,19 +1,13 @@ """ -This is pure python implementation of interpolation search algorithm +This is pure Python implementation of interpolation search algorithm """ -from __future__ import print_function - -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 def interpolation_search(sorted_collection, item): """Pure implementation of interpolation search algorithm in Python - Be careful collection must be sorted, otherwise result will be + Be careful collection must be ascending sorted, otherwise result will be unpredictable - :param sorted_collection: some sorted collection with comparable items + :param sorted_collection: some ascending sorted collection with comparable items :param item: item value to search :return: index of found item or None if item is not found """ @@ -21,78 +15,126 @@ def interpolation_search(sorted_collection, item): right = len(sorted_collection) - 1 while left <= right: - point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left]) - - #out of range check - if point<0 or point>=len(sorted_collection): + # avoid divided by 0 during interpolation + if sorted_collection[left] == sorted_collection[right]: + if sorted_collection[left] == item: + return left + else: + return None + + point = left + ((item - sorted_collection[left]) * (right - left)) // ( + sorted_collection[right] - sorted_collection[left] + ) + + # out of range check + if point < 0 or point >= len(sorted_collection): return None current_item = sorted_collection[point] if current_item == item: return point else: - if item < current_item: - right = point - 1 + if point < left: + right = left + left = point + elif point > right: + left = right + right = point else: - left = point + 1 + if item < current_item: + right = point - 1 + else: + left = point + 1 return None def interpolation_search_by_recursion(sorted_collection, item, left, right): """Pure implementation of interpolation search algorithm in Python by recursion - Be careful collection must be sorted, otherwise result will be + Be careful collection must be ascending sorted, otherwise result will be unpredictable First recursion should be started with left=0 and right=(len(sorted_collection)-1) - :param sorted_collection: some sorted collection with comparable items + :param sorted_collection: some ascending sorted collection with comparable items :param item: item value to search :return: index of found item or None if item is not found """ - point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left]) - #out of range check - if point<0 or point>=len(sorted_collection): + # avoid divided by 0 during interpolation + if sorted_collection[left] == sorted_collection[right]: + if sorted_collection[left] == item: + return left + else: + return None + + point = left + ((item - sorted_collection[left]) * (right - left)) // ( + sorted_collection[right] - sorted_collection[left] + ) + + # out of range check + if point < 0 or point >= len(sorted_collection): return None if sorted_collection[point] == item: return point - elif sorted_collection[point] > item: - return interpolation_search_by_recursion(sorted_collection, item, left, point-1) + elif point < left: + return interpolation_search_by_recursion(sorted_collection, item, point, left) + elif point > right: + return interpolation_search_by_recursion(sorted_collection, item, right, left) else: - return interpolation_search_by_recursion(sorted_collection, item, point+1, right) - + if sorted_collection[point] > item: + return interpolation_search_by_recursion( + sorted_collection, item, left, point - 1 + ) + else: + return interpolation_search_by_recursion( + sorted_collection, item, point + 1, right + ) + + def __assert_sorted(collection): - """Check if collection is sorted, if not - raises :py:class:`ValueError` + """Check if collection is ascending sorted, if not - raises :py:class:`ValueError` :param collection: collection - :return: True if collection is sorted - :raise: :py:class:`ValueError` if collection is not sorted + :return: True if collection is ascending sorted + :raise: :py:class:`ValueError` if collection is not ascending sorted Examples: >>> __assert_sorted([0, 1, 2, 4]) True >>> __assert_sorted([10, -1, 5]) Traceback (most recent call last): ... - ValueError: Collection must be sorted + ValueError: Collection must be ascending sorted """ if collection != sorted(collection): - raise ValueError('Collection must be sorted') + raise ValueError("Collection must be ascending sorted") return True -if __name__ == '__main__': +if __name__ == "__main__": import sys - user_input = raw_input('Enter numbers separated by comma:\n').strip() + """ + user_input = input('Enter numbers separated by comma:\n').strip() collection = [int(item) for item in user_input.split(',')] try: __assert_sorted(collection) except ValueError: - sys.exit('Sequence must be sorted to apply interpolation search') + sys.exit('Sequence must be ascending sorted to apply interpolation search') - target_input = raw_input('Enter a single number to be found in the list:\n') + target_input = input('Enter a single number to be found in the list:\n') target = int(target_input) + """ + + debug = 0 + if debug == 1: + collection = [10, 30, 40, 45, 50, 66, 77, 93] + try: + __assert_sorted(collection) + except ValueError: + sys.exit("Sequence must be ascending sorted to apply interpolation search") + target = 67 + result = interpolation_search(collection, target) if result is not None: - print('{} found at positions: {}'.format(target, result)) + print(f"{target} found at positions: {result}") else: - print('Not found') + print("Not found") diff --git a/searches/jump_search.py b/searches/jump_search.py index 10cb933f2f35..31a9656c55fe 100644 --- a/searches/jump_search.py +++ b/searches/jump_search.py @@ -1,10 +1,32 @@ -from __future__ import print_function +""" +Pure Python implementation of the jump search algorithm. +This algorithm iterates through a sorted collection with a step of n^(1/2), +until the element compared is bigger than the one searched. +It will then perform a linear search until it matches the wanted number. +If not found, it returns -1. +""" + import math -def jump_search(arr, x): + + +def jump_search(arr: list, x: int) -> int: + """ + Pure Python implementation of the jump search algorithm. + Examples: + >>> jump_search([0, 1, 2, 3, 4, 5], 3) + 3 + >>> jump_search([-5, -2, -1], -1) + 2 + >>> jump_search([0, 5, 10, 20], 8) + -1 + >>> jump_search([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610], 55) + 10 + """ + n = len(arr) step = int(math.floor(math.sqrt(n))) prev = 0 - while arr[min(step, n)-1] < x: + while arr[min(step, n) - 1] < x: prev = step step += int(math.floor(math.sqrt(n))) if prev >= n: @@ -19,8 +41,12 @@ def jump_search(arr, x): return -1 - -arr = [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] -x = 55 -index = jump_search(arr, x) -print("\nNumber " + str(x) +" is at index " + str(index)); +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + arr = [int(item) for item in user_input.split(",")] + x = int(input("Enter the number to be searched:\n")) + res = jump_search(arr, x) + if res == -1: + print("Number not found!") + else: + print(f"Number {x} is at index {res}") diff --git a/searches/linear_search.py b/searches/linear_search.py index 058322f21d09..777080d14e36 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -1,55 +1,77 @@ """ -This is pure python implementation of linear search algorithm +This is pure Python implementation of linear search algorithm For doctests run following command: -python -m doctest -v linear_search.py -or python3 -m doctest -v linear_search.py For manual testing run: -python linear_search.py +python3 linear_search.py """ -from __future__ import print_function -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 -def linear_search(sequence, target): - """Pure implementation of linear search algorithm in Python +def linear_search(sequence: list, target: int) -> int: + """A pure Python implementation of a linear search algorithm - :param sequence: some sorted collection with comparable items + :param sequence: a collection with comparable items (as sorted items not required + in Linear Search) :param target: item value to search :return: index of found item or None if item is not found Examples: >>> linear_search([0, 5, 7, 10, 15], 0) 0 - >>> linear_search([0, 5, 7, 10, 15], 15) 4 - >>> linear_search([0, 5, 7, 10, 15], 5) 1 - >>> linear_search([0, 5, 7, 10, 15], 6) - + -1 """ for index, item in enumerate(sequence): if item == target: return index - return None + return -1 + + +def rec_linear_search(sequence: list, low: int, high: int, target: int) -> int: + """ + A pure Python implementation of a recursive linear search algorithm + + :param sequence: a collection with comparable items (as sorted items not required + in Linear Search) + :param low: Lower bound of the array + :param high: Higher bound of the array + :param target: The element to be found + :return: Index of the key or -1 if key not found + + Examples: + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0) + 0 + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700) + 4 + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30) + 1 + >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6) + -1 + """ + if not (0 <= high < len(sequence) and 0 <= low < len(sequence)): + raise Exception("Invalid upper or lower bound!") + if high < low: + return -1 + if sequence[low] == target: + return low + if sequence[high] == target: + return high + return rec_linear_search(sequence, low + 1, high - 1, target) -if __name__ == '__main__': - user_input = raw_input('Enter numbers separated by comma:\n').strip() - sequence = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n").strip() + sequence = [int(item.strip()) for item in user_input.split(",")] - target_input = raw_input('Enter a single number to be found in the list:\n') - target = int(target_input) + target = int(input("Enter a single number to be found in the list:\n").strip()) result = linear_search(sequence, target) - if result is not None: - print('{} found at positions: {}'.format(target, result)) + if result != -1: + print(f"linear_search({sequence}, {target}) = {result}") else: - print('Not found') + print(f"{target} was not found in {sequence}") diff --git a/searches/quick_select.py b/searches/quick_select.py index 1596cf040e0c..5ede8c4dd07f 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -1,10 +1,13 @@ -import random - """ -A python implementation of the quick select algorithm, which is efficient for calculating the value that would appear in the index of a list if it would be sorted, even if it is not already sorted +A Python implementation of the quick select algorithm, which is efficient for +calculating the value that would appear in the index of a list if it would be +sorted, even if it is not already sorted https://en.wikipedia.org/wiki/Quickselect """ -def _partition(data, pivot): +import random + + +def _partition(data: list, pivot) -> tuple: """ Three way partition the data into smaller, equal and greater lists, in relationship to the pivot @@ -14,31 +17,45 @@ def _partition(data, pivot): """ less, equal, greater = [], [], [] for element in data: - if element.address < pivot.address: + if element < pivot: less.append(element) - elif element.address > pivot.address: + elif element > pivot: greater.append(element) else: equal.append(element) return less, equal, greater - -def quickSelect(list, k): - #k = len(list) // 2 when trying to find the median (index that value would be when list is sorted) - smaller = [] - larger = [] - pivot = random.randint(0, len(list) - 1) - pivot = list[pivot] + + +def quick_select(items: list, index: int): + """ + >>> quick_select([2, 4, 5, 7, 899, 54, 32], 5) + 54 + >>> quick_select([2, 4, 5, 7, 899, 54, 32], 1) + 4 + >>> quick_select([5, 4, 3, 2], 2) + 4 + >>> quick_select([3, 5, 7, 10, 2, 12], 3) + 7 + """ + # index = len(items) // 2 when trying to find the median + # (value of index when items is sorted) + + # invalid input + if index >= len(items) or index < 0: + return None + + pivot = items[random.randint(0, len(items) - 1)] count = 0 - smaller, equal, larger =_partition(list, pivot) + smaller, equal, larger = _partition(items, pivot) count = len(equal) m = len(smaller) - #k is the pivot - if m <= k < m + count: + # index is the pivot + if m <= index < m + count: return pivot # must be in smaller - elif m > k: - return quickSelect(smaller, k) - #must be in larger + elif m > index: + return quick_select(smaller, index) + # must be in larger else: - return quickSelect(larger, k - (m + count)) + return quick_select(larger, index - (m + count)) diff --git a/searches/sentinel_linear_search.py b/searches/sentinel_linear_search.py index 336cc5ab3b74..69c1cf9f351a 100644 --- a/searches/sentinel_linear_search.py +++ b/searches/sentinel_linear_search.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of sentinel linear search algorithm +This is pure Python implementation of sentinel linear search algorithm For doctests run following command: python -m doctest -v sentinel_linear_search.py @@ -10,6 +10,7 @@ python sentinel_linear_search.py """ + def sentinel_linear_search(sequence, target): """Pure implementation of sentinel linear search algorithm in Python @@ -44,19 +45,14 @@ def sentinel_linear_search(sequence, target): return index -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - - user_input = raw_input('Enter numbers separated by comma:\n').strip() - sequence = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n").strip() + sequence = [int(item) for item in user_input.split(",")] - target_input = raw_input('Enter a single number to be found in the list:\n') + target_input = input("Enter a single number to be found in the list:\n") target = int(target_input) result = sentinel_linear_search(sequence, target) if result is not None: - print('{} found at positions: {}'.format(target, result)) + print(f"{target} found at positions: {result}") else: - print('Not found') \ No newline at end of file + print("Not found") diff --git a/searches/simple_binary_search.py b/searches/simple_binary_search.py new file mode 100644 index 000000000000..d1f7f7a51cbc --- /dev/null +++ b/searches/simple_binary_search.py @@ -0,0 +1,59 @@ +""" +Pure Python implementation of a binary search algorithm. + +For doctests run following command: +python3 -m doctest -v simple_binary_search.py + +For manual testing run: +python3 simple_binary_search.py +""" +from __future__ import annotations + + +def binary_search(a_list: list[int], item: int) -> bool: + """ + >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] + >>> print(binary_search(test_list, 3)) + False + >>> print(binary_search(test_list, 13)) + True + >>> print(binary_search([4, 4, 5, 6, 7], 4)) + True + >>> print(binary_search([4, 4, 5, 6, 7], -10)) + False + >>> print(binary_search([-18, 2], -18)) + True + >>> print(binary_search([5], 5)) + True + >>> print(binary_search(['a', 'c', 'd'], 'c')) + True + >>> print(binary_search(['a', 'c', 'd'], 'f')) + False + >>> print(binary_search([], 1)) + False + >>> print(binary_search([-.1, .1 , .8], .1)) + True + >>> binary_search(range(-5000, 5000, 10), 80) + True + >>> binary_search(range(-5000, 5000, 10), 1255) + False + >>> binary_search(range(0, 10000, 5), 2) + False + """ + if len(a_list) == 0: + return False + midpoint = len(a_list) // 2 + if a_list[midpoint] == item: + return True + if item < a_list[midpoint]: + return binary_search(a_list[:midpoint], item) + else: + return binary_search(a_list[midpoint + 1 :], item) + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n").strip() + sequence = [int(item.strip()) for item in user_input.split(",")] + target = int(input("Enter the number to be found in the list:\n").strip()) + not_str = "" if binary_search(sequence, target) else "not " + print(f"{target} was {not_str}found in {sequence}") diff --git a/searches/simulated_annealing.py b/searches/simulated_annealing.py new file mode 100644 index 000000000000..2aa980be7748 --- /dev/null +++ b/searches/simulated_annealing.py @@ -0,0 +1,136 @@ +# https://en.wikipedia.org/wiki/Simulated_annealing +import math +import random + +from .hill_climbing import SearchProblem + + +def simulated_annealing( + search_prob, + find_max: bool = True, + max_x: float = math.inf, + min_x: float = -math.inf, + max_y: float = math.inf, + min_y: float = -math.inf, + visualization: bool = False, + start_temperate: float = 100, + rate_of_decrease: float = 0.01, + threshold_temp: float = 1, +) -> SearchProblem: + """ + Implementation of the simulated annealing algorithm. We start with a given state, + find all its neighbors. Pick a random neighbor, if that neighbor improves the + solution, we move in that direction, if that neighbor does not improve the solution, + we generate a random real number between 0 and 1, if the number is within a certain + range (calculated using temperature) we move in that direction, else we pick + another neighbor randomly and repeat the process. + + Args: + search_prob: The search state at the start. + find_max: If True, the algorithm should find the minimum else the minimum. + max_x, min_x, max_y, min_y: the maximum and minimum bounds of x and y. + visualization: If True, a matplotlib graph is displayed. + start_temperate: the initial temperate of the system when the program starts. + rate_of_decrease: the rate at which the temperate decreases in each iteration. + threshold_temp: the threshold temperature below which we end the search + Returns a search state having the maximum (or minimum) score. + """ + search_end = False + current_state = search_prob + current_temp = start_temperate + scores = [] + iterations = 0 + best_state = None + + while not search_end: + current_score = current_state.score() + if best_state is None or current_score > best_state.score(): + best_state = current_state + scores.append(current_score) + iterations += 1 + next_state = None + neighbors = current_state.get_neighbors() + while ( + next_state is None and neighbors + ): # till we do not find a neighbor that we can move to + index = random.randint(0, len(neighbors) - 1) # picking a random neighbor + picked_neighbor = neighbors.pop(index) + change = picked_neighbor.score() - current_score + + if ( + picked_neighbor.x > max_x + or picked_neighbor.x < min_x + or picked_neighbor.y > max_y + or picked_neighbor.y < min_y + ): + continue # neighbor outside our bounds + + if not find_max: + change = change * -1 # in case we are finding minimum + if change > 0: # improves the solution + next_state = picked_neighbor + else: + probability = (math.e) ** ( + change / current_temp + ) # probability generation function + if random.random() < probability: # random number within probability + next_state = picked_neighbor + current_temp = current_temp - (current_temp * rate_of_decrease) + + if current_temp < threshold_temp or next_state is None: + # temperature below threshold, or could not find a suitable neighbor + search_end = True + else: + current_state = next_state + + if visualization: + from matplotlib import pyplot as plt + + plt.plot(range(iterations), scores) + plt.xlabel("Iterations") + plt.ylabel("Function values") + plt.show() + return best_state + + +if __name__ == "__main__": + + def test_f1(x, y): + return (x ** 2) + (y ** 2) + + # starting the problem with initial coordinates (12, 47) + prob = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_f1) + local_min = simulated_annealing( + prob, find_max=False, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True + ) + print( + "The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 " + f"and 50 > y > - 5 found via hill climbing: {local_min.score()}" + ) + + # starting the problem with initial coordinates (12, 47) + prob = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_f1) + local_min = simulated_annealing( + prob, find_max=True, max_x=100, min_x=5, max_y=50, min_y=-5, visualization=True + ) + print( + "The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 " + f"and 50 > y > - 5 found via hill climbing: {local_min.score()}" + ) + + def test_f2(x, y): + return (3 * x ** 2) - (6 * y) + + prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) + local_min = simulated_annealing(prob, find_max=False, visualization=True) + print( + "The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: " + f"{local_min.score()}" + ) + + prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) + local_min = simulated_annealing(prob, find_max=True, visualization=True) + print( + "The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: " + f"{local_min.score()}" + ) diff --git a/searches/tabu_search.py b/searches/tabu_search.py index e21ddd53cc78..24d0dbf6f1c2 100644 --- a/searches/tabu_search.py +++ b/searches/tabu_search.py @@ -1,8 +1,9 @@ """ -This is pure python implementation of Tabu search algorithm for a Travelling Salesman Problem, that the distances -between the cities are symmetric (the distance between city 'a' and city 'b' is the same between city 'b' and city 'a'). -The TSP can be represented into a graph. The cities are represented by nodes and the distance between them is -represented by the weight of the ark between the nodes. +This is pure Python implementation of Tabu search algorithm for a Travelling Salesman +Problem, that the distances between the cities are symmetric (the distance between city +'a' and city 'b' is the same between city 'b' and city 'a'). +The TSP can be represented into a graph. The cities are represented by nodes and the +distance between them is represented by the weight of the ark between the nodes. The .txt file with the graph has the form: @@ -10,8 +11,8 @@ node1 node3 distance_between_node1_and_node3 ... -Be careful node1, node2 and the distance between them, must exist only once. This means in the .txt file -should not exist: +Be careful node1, node2 and the distance between them, must exist only once. This means +in the .txt file should not exist: node1 node2 distance_between_node1_and_node2 node2 node1 distance_between_node2_and_node1 @@ -19,13 +20,12 @@ pytest For manual testing run: -python tabu_search.py -f your_file_name.txt -number_of_iterations_of_tabu_search -s size_of_tabu_search +python tabu_search.py -f your_file_name.txt -number_of_iterations_of_tabu_search \ + -s size_of_tabu_search e.g. python tabu_search.py -f tabudata2.txt -i 4 -s 3 """ - -import copy import argparse -import sys +import copy def generate_neighbours(path): @@ -34,16 +34,16 @@ def generate_neighbours(path): neighbor, given a path file that includes a graph. :param path: The path to the .txt file that includes the graph (e.g.tabudata2.txt) - :return dict_of_neighbours: Dictionary with key each node and value a list of lists with the neighbors of the node - and the cost (distance) for each neighbor. + :return dict_of_neighbours: Dictionary with key each node and value a list of lists + with the neighbors of the node and the cost (distance) for each neighbor. Example of dict_of_neighbours: - >>> dict_of_neighbours[a] + >>) dict_of_neighbours[a] [[b,20],[c,18],[d,22],[e,26]] - This indicates the neighbors of node (city) 'a', which has neighbor the node 'b' with distance 20, - the node 'c' with distance 18, the node 'd' with distance 22 and the node 'e' with distance 26. - + This indicates the neighbors of node (city) 'a', which has neighbor the node 'b' + with distance 20, the node 'c' with distance 18, the node 'd' with distance 22 and + the node 'e' with distance 26. """ dict_of_neighbours = {} @@ -55,32 +55,36 @@ def generate_neighbours(path): _list.append([line.split()[1], line.split()[2]]) dict_of_neighbours[line.split()[0]] = _list else: - dict_of_neighbours[line.split()[0]].append([line.split()[1], line.split()[2]]) + dict_of_neighbours[line.split()[0]].append( + [line.split()[1], line.split()[2]] + ) if line.split()[1] not in dict_of_neighbours: _list = list() _list.append([line.split()[0], line.split()[2]]) dict_of_neighbours[line.split()[1]] = _list else: - dict_of_neighbours[line.split()[1]].append([line.split()[0], line.split()[2]]) + dict_of_neighbours[line.split()[1]].append( + [line.split()[0], line.split()[2]] + ) return dict_of_neighbours def generate_first_solution(path, dict_of_neighbours): """ - Pure implementation of generating the first solution for the Tabu search to start, with the redundant resolution - strategy. That means that we start from the starting node (e.g. node 'a'), then we go to the city nearest (lowest - distance) to this node (let's assume is node 'c'), then we go to the nearest city of the node 'c', etc + Pure implementation of generating the first solution for the Tabu search to start, + with the redundant resolution strategy. That means that we start from the starting + node (e.g. node 'a'), then we go to the city nearest (lowest distance) to this node + (let's assume is node 'c'), then we go to the nearest city of the node 'c', etc. till we have visited all cities and return to the starting node. :param path: The path to the .txt file that includes the graph (e.g.tabudata2.txt) - :param dict_of_neighbours: Dictionary with key each node and value a list of lists with the neighbors of the node - and the cost (distance) for each neighbor. - :return first_solution: The solution for the first iteration of Tabu search using the redundant resolution strategy - in a list. - :return distance_of_first_solution: The total distance that Travelling Salesman will travel, if he follows the path - in first_solution. - + :param dict_of_neighbours: Dictionary with key each node and value a list of lists + with the neighbors of the node and the cost (distance) for each neighbor. + :return first_solution: The solution for the first iteration of Tabu search using + the redundant resolution strategy in a list. + :return distance_of_first_solution: The total distance that Travelling Salesman + will travel, if he follows the path in first_solution. """ with open(path) as f: @@ -111,29 +115,42 @@ def generate_first_solution(path, dict_of_neighbours): break position += 1 - distance_of_first_solution = distance_of_first_solution + int( - dict_of_neighbours[first_solution[-2]][position][1]) - 10000 + distance_of_first_solution = ( + distance_of_first_solution + + int(dict_of_neighbours[first_solution[-2]][position][1]) + - 10000 + ) return first_solution, distance_of_first_solution def find_neighborhood(solution, dict_of_neighbours): """ - Pure implementation of generating the neighborhood (sorted by total distance of each solution from - lowest to highest) of a solution with 1-1 exchange method, that means we exchange each node in a solution with each - other node and generating a number of solution named neighborhood. + Pure implementation of generating the neighborhood (sorted by total distance of + each solution from lowest to highest) of a solution with 1-1 exchange method, that + means we exchange each node in a solution with each other node and generating a + number of solution named neighborhood. :param solution: The solution in which we want to find the neighborhood. - :param dict_of_neighbours: Dictionary with key each node and value a list of lists with the neighbors of the node - and the cost (distance) for each neighbor. - :return neighborhood_of_solution: A list that includes the solutions and the total distance of each solution - (in form of list) that are produced with 1-1 exchange from the solution that the method took as an input - + :param dict_of_neighbours: Dictionary with key each node and value a list of lists + with the neighbors of the node and the cost (distance) for each neighbor. + :return neighborhood_of_solution: A list that includes the solutions and the total + distance of each solution (in form of list) that are produced with 1-1 exchange + from the solution that the method took as an input Example: - >>> find_neighborhood(['a','c','b','d','e','a']) - [['a','e','b','d','c','a',90], [['a','c','d','b','e','a',90],['a','d','b','c','e','a',93], - ['a','c','b','e','d','a',102], ['a','c','e','d','b','a',113], ['a','b','c','d','e','a',93]] - + >>> find_neighborhood(['a', 'c', 'b', 'd', 'e', 'a'], + ... {'a': [['b', '20'], ['c', '18'], ['d', '22'], ['e', '26']], + ... 'c': [['a', '18'], ['b', '10'], ['d', '23'], ['e', '24']], + ... 'b': [['a', '20'], ['c', '10'], ['d', '11'], ['e', '12']], + ... 'e': [['a', '26'], ['b', '12'], ['c', '24'], ['d', '40']], + ... 'd': [['a', '22'], ['b', '11'], ['c', '23'], ['e', '40']]} + ... ) # doctest: +NORMALIZE_WHITESPACE + [['a', 'e', 'b', 'd', 'c', 'a', 90], + ['a', 'c', 'd', 'b', 'e', 'a', 90], + ['a', 'd', 'b', 'c', 'e', 'a', 93], + ['a', 'c', 'b', 'e', 'd', 'a', 102], + ['a', 'c', 'e', 'd', 'b', 'a', 113], + ['a', 'b', 'c', 'd', 'e', 'a', 119]] """ neighborhood_of_solution = [] @@ -167,22 +184,25 @@ def find_neighborhood(solution, dict_of_neighbours): return neighborhood_of_solution -def tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, iters, size): +def tabu_search( + first_solution, distance_of_first_solution, dict_of_neighbours, iters, size +): """ - Pure implementation of Tabu search algorithm for a Travelling Salesman Problem in Python. - - :param first_solution: The solution for the first iteration of Tabu search using the redundant resolution strategy - in a list. - :param distance_of_first_solution: The total distance that Travelling Salesman will travel, if he follows the path - in first_solution. - :param dict_of_neighbours: Dictionary with key each node and value a list of lists with the neighbors of the node - and the cost (distance) for each neighbor. + Pure implementation of Tabu search algorithm for a Travelling Salesman Problem in + Python. + + :param first_solution: The solution for the first iteration of Tabu search using + the redundant resolution strategy in a list. + :param distance_of_first_solution: The total distance that Travelling Salesman will + travel, if he follows the path in first_solution. + :param dict_of_neighbours: Dictionary with key each node and value a list of lists + with the neighbors of the node and the cost (distance) for each neighbor. :param iters: The number of iterations that Tabu search will execute. :param size: The size of Tabu List. - :return best_solution_ever: The solution with the lowest distance that occured during the execution of Tabu search. - :return best_cost: The total distance that Travelling Salesman will travel, if he follows the path in best_solution - ever. - + :return best_solution_ever: The solution with the lowest distance that occurred + during the execution of Tabu search. + :return best_cost: The total distance that Travelling Salesman will travel, if he + follows the path in best_solution ever. """ count = 1 solution = first_solution @@ -197,7 +217,7 @@ def tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, best_cost_index = len(best_solution) - 1 found = False - while found is False: + while not found: i = 0 while i < len(best_solution): @@ -207,8 +227,10 @@ def tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, break i = i + 1 - if [first_exchange_node, second_exchange_node] not in tabu_list and [second_exchange_node, - first_exchange_node] not in tabu_list: + if [first_exchange_node, second_exchange_node] not in tabu_list and [ + second_exchange_node, + first_exchange_node, + ] not in tabu_list: tabu_list.append([first_exchange_node, second_exchange_node]) found = True solution = best_solution[:-1] @@ -231,22 +253,40 @@ def tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, def main(args=None): dict_of_neighbours = generate_neighbours(args.File) - first_solution, distance_of_first_solution = generate_first_solution(args.File, dict_of_neighbours) + first_solution, distance_of_first_solution = generate_first_solution( + args.File, dict_of_neighbours + ) - best_sol, best_cost = tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, args.Iterations, - args.Size) + best_sol, best_cost = tabu_search( + first_solution, + distance_of_first_solution, + dict_of_neighbours, + args.Iterations, + args.Size, + ) - print("Best solution: {0}, with total distance: {1}.".format(best_sol, best_cost)) + print(f"Best solution: {best_sol}, with total distance: {best_cost}.") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Tabu Search") parser.add_argument( - "-f", "--File", type=str, help="Path to the file containing the data", required=True) + "-f", + "--File", + type=str, + help="Path to the file containing the data", + required=True, + ) parser.add_argument( - "-i", "--Iterations", type=int, help="How many iterations the algorithm should perform", required=True) + "-i", + "--Iterations", + type=int, + help="How many iterations the algorithm should perform", + required=True, + ) parser.add_argument( - "-s", "--Size", type=int, help="Size of the tabu list", required=True) + "-s", "--Size", type=int, help="Size of the tabu list", required=True + ) # Pass the arguments to main method - sys.exit(main(parser.parse_args())) + main(parser.parse_args()) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index c610f9b3c6da..9422a4ccb966 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -1,107 +1,170 @@ -''' +""" This is a type of divide and conquer algorithm which divides the search space into -3 parts and finds the target value based on the property of the array or list +3 parts and finds the target value based on the property of the array or list (usually monotonic property). Time Complexity : O(log3 N) Space Complexity : O(1) -''' -from __future__ import print_function - -import sys - -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 +""" +from typing import List # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. precision = 10 + # This is the linear search that will occur after the search space has become smaller. -def lin_search(left, right, A, target): - for i in range(left, right+1): - if(A[i] == target): + + +def lin_search(left: int, right: int, array: List[int], target: int) -> int: + """Perform linear search in list. Returns -1 if element is not found. + + Parameters + ---------- + left : int + left index bound. + right : int + right index bound. + array : List[int] + List of elements to be searched on + target : int + Element that is searched + + Returns + ------- + int + index of element that is looked for. + + Examples + -------- + >>> lin_search(0, 4, [4, 5, 6, 7], 7) + 3 + >>> lin_search(0, 3, [4, 5, 6, 7], 7) + -1 + >>> lin_search(0, 2, [-18, 2], -18) + 0 + >>> lin_search(0, 1, [5], 5) + 0 + >>> lin_search(0, 3, ['a', 'c', 'd'], 'c') + 1 + >>> lin_search(0, 3, [.1, .4 , -.1], .1) + 0 + >>> lin_search(0, 3, [.1, .4 , -.1], -.1) + 2 + """ + for i in range(left, right): + if array[i] == target: return i + return -1 + + +def ite_ternary_search(array: List[int], target: int) -> int: + """Iterative method of the ternary search algorithm. + >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] + >>> ite_ternary_search(test_list, 3) + -1 + >>> ite_ternary_search(test_list, 13) + 4 + >>> ite_ternary_search([4, 5, 6, 7], 4) + 0 + >>> ite_ternary_search([4, 5, 6, 7], -10) + -1 + >>> ite_ternary_search([-18, 2], -18) + 0 + >>> ite_ternary_search([5], 5) + 0 + >>> ite_ternary_search(['a', 'c', 'd'], 'c') + 1 + >>> ite_ternary_search(['a', 'c', 'd'], 'f') + -1 + >>> ite_ternary_search([], 1) + -1 + >>> ite_ternary_search([.1, .4 , -.1], .1) + 0 + """ -# This is the iterative method of the ternary search algorithm. -def ite_ternary_search(A, target): left = 0 - right = len(A) - 1; - while(True): - if(left int: + """Recursive method of the ternary search algorithm. -# This function is to check if the array is sorted. -def __assert_sorted(collection): - if collection != sorted(collection): - raise ValueError('Collection must be sorted') - return True + >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] + >>> rec_ternary_search(0, len(test_list), test_list, 3) + -1 + >>> rec_ternary_search(4, len(test_list), test_list, 42) + 8 + >>> rec_ternary_search(0, 2, [4, 5, 6, 7], 4) + 0 + >>> rec_ternary_search(0, 3, [4, 5, 6, 7], -10) + -1 + >>> rec_ternary_search(0, 1, [-18, 2], -18) + 0 + >>> rec_ternary_search(0, 1, [5], 5) + 0 + >>> rec_ternary_search(0, 2, ['a', 'c', 'd'], 'c') + 1 + >>> rec_ternary_search(0, 2, ['a', 'c', 'd'], 'f') + -1 + >>> rec_ternary_search(0, 0, [], 1) + -1 + >>> rec_ternary_search(0, 3, [.1, .4 , -.1], .1) + 0 + """ + if left < right: + if right - left < precision: + return lin_search(left, right, array, target) + one_third = (left + right) / 3 + 1 + two_third = 2 * (left + right) / 3 + 1 + if array[one_third] == target: + return one_third + elif array[two_third] == target: + return two_third -if __name__ == '__main__': - user_input = raw_input('Enter numbers separated by coma:\n').strip() - collection = [int(item) for item in user_input.split(',')] + elif target < array[one_third]: + return rec_ternary_search(left, one_third - 1, array, target) + elif array[two_third] < target: + return rec_ternary_search(two_third + 1, right, array, target) + else: + return rec_ternary_search(one_third + 1, two_third - 1, array, target) + else: + return -1 - try: - __assert_sorted(collection) - except ValueError: - sys.exit('Sequence must be sorted to apply the ternary search') - target_input = raw_input('Enter a single number to be found in the list:\n') - target = int(target_input) +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n").strip() + collection = [int(item.strip()) for item in user_input.split(",")] + assert collection == sorted(collection), f"List must be ordered.\n{collection}." + target = int(input("Enter the number to be found in the list:\n").strip()) result1 = ite_ternary_search(collection, target) - result2 = rec_ternary_search(0, len(collection)-1, collection, target) - - if result2 is not None: - print('Iterative search: {} found at positions: {}'.format(target, result1)) - print('Recursive search: {} found at positions: {}'.format(target, result2)) + result2 = rec_ternary_search(0, len(collection) - 1, collection, target) + if result2 != -1: + print(f"Iterative search: {target} found at positions: {result1}") + print(f"Recursive search: {target} found at positions: {result2}") else: - print('Not found') + print("Not found") diff --git a/searches/test_tabu_search.py b/searches/test_tabu_search.py deleted file mode 100644 index e6f73e6a9002..000000000000 --- a/searches/test_tabu_search.py +++ /dev/null @@ -1,46 +0,0 @@ -import unittest -import os -from tabu_search import generate_neighbours, generate_first_solution, find_neighborhood, tabu_search - -TEST_FILE = os.path.join(os.path.dirname(__file__), './tabu_test_data.txt') - -NEIGHBOURS_DICT = {'a': [['b', '20'], ['c', '18'], ['d', '22'], ['e', '26']], - 'c': [['a', '18'], ['b', '10'], ['d', '23'], ['e', '24']], - 'b': [['a', '20'], ['c', '10'], ['d', '11'], ['e', '12']], - 'e': [['a', '26'], ['b', '12'], ['c', '24'], ['d', '40']], - 'd': [['a', '22'], ['b', '11'], ['c', '23'], ['e', '40']]} - -FIRST_SOLUTION = ['a', 'c', 'b', 'd', 'e', 'a'] - -DISTANCE = 105 - -NEIGHBOURHOOD_OF_SOLUTIONS = [['a', 'e', 'b', 'd', 'c', 'a', 90], - ['a', 'c', 'd', 'b', 'e', 'a', 90], - ['a', 'd', 'b', 'c', 'e', 'a', 93], - ['a', 'c', 'b', 'e', 'd', 'a', 102], - ['a', 'c', 'e', 'd', 'b', 'a', 113], - ['a', 'b', 'c', 'd', 'e', 'a', 119]] - - -class TestClass(unittest.TestCase): - def test_generate_neighbours(self): - neighbours = generate_neighbours(TEST_FILE) - - self.assertEqual(NEIGHBOURS_DICT, neighbours) - - def test_generate_first_solutions(self): - first_solution, distance = generate_first_solution(TEST_FILE, NEIGHBOURS_DICT) - - self.assertEqual(FIRST_SOLUTION, first_solution) - self.assertEqual(DISTANCE, distance) - - def test_find_neighbours(self): - neighbour_of_solutions = find_neighborhood(FIRST_SOLUTION, NEIGHBOURS_DICT) - - self.assertEqual(NEIGHBOURHOOD_OF_SOLUTIONS, neighbour_of_solutions) - - def test_tabu_search(self): - best_sol, best_cost = tabu_search(FIRST_SOLUTION, DISTANCE, NEIGHBOURS_DICT, 4, 3) - - self.assertEqual(['a', 'd', 'b', 'e', 'c', 'a'], best_sol) - self.assertEqual(87, best_cost) diff --git a/simple_client/README.md b/simple_client/README.md deleted file mode 100644 index f51947f2105a..000000000000 --- a/simple_client/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# simple client server - -#### Note: -- Run **`server.py`** first. -- Now, run **`client.py`**. -- verify the output. diff --git a/simple_client/client.py b/simple_client/client.py deleted file mode 100644 index db162f43c78a..000000000000 --- a/simple_client/client.py +++ /dev/null @@ -1,29 +0,0 @@ -# client.py - -import socket - -HOST, PORT = '127.0.0.1', 1400 - -s = socket.socket( - - socket.AF_INET, # ADDRESS FAMILIES - #Name Purpose - #AF_UNIX, AF_LOCAL Local communication - #AF_INET IPv4 Internet protocols - #AF_INET6 IPv6 Internet protocols - #AF_APPLETALK Appletalk - #AF_BLUETOOTH Bluetooth - - - socket.SOCK_STREAM # SOCKET TYPES - #Name Way of Interaction - #SOCK_STREAM TCP - #SOCK_DGRAM UDP -) -s.connect((HOST, PORT)) - -s.send('Hello World'.encode('ascii'))#in UDP use sendto() -data = s.recv(1024)#in UDP use recvfrom() - -s.close()#end the connection -print(repr(data.decode('ascii'))) diff --git a/simple_client/server.py b/simple_client/server.py deleted file mode 100644 index c23075608a90..000000000000 --- a/simple_client/server.py +++ /dev/null @@ -1,21 +0,0 @@ -# server.py - -import socket - -HOST, PORT = '127.0.0.1', 1400 - -s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#refer to client.py -s.bind((HOST, PORT)) -s.listen(1)#listen for 1 connection - -conn, addr = s.accept()#start the actual data flow - -print('connected to:', addr) - -while 1: - data = conn.recv(1024).decode('ascii')#receive 1024 bytes and decode using ascii - if not data: - break - conn.send((data + ' [ addition by server ]').encode('ascii')) - -conn.close() diff --git a/sorts/BitonicSort.py b/sorts/BitonicSort.py deleted file mode 100644 index bae95b4346f6..000000000000 --- a/sorts/BitonicSort.py +++ /dev/null @@ -1,56 +0,0 @@ -# Python program for Bitonic Sort. Note that this program -# works only when size of input is a power of 2. - -# The parameter dir indicates the sorting direction, ASCENDING -# or DESCENDING; if (a[i] > a[j]) agrees with the direction, -# then a[i] and a[j] are interchanged.*/ -def compAndSwap(a, i, j, dire): - if (dire == 1 and a[i] > a[j]) or (dire == 0 and a[i] < a[j]): - a[i], a[j] = a[j], a[i] - - # It recursively sorts a bitonic sequence in ascending order, - - -# if dir = 1, and in descending order otherwise (means dir=0). -# The sequence to be sorted starts at index position low, -# the parameter cnt is the number of elements to be sorted. -def bitonicMerge(a, low, cnt, dire): - if cnt > 1: - k = int(cnt / 2) - for i in range(low, low + k): - compAndSwap(a, i, i + k, dire) - bitonicMerge(a, low, k, dire) - bitonicMerge(a, low + k, k, dire) - - # This funcion first produces a bitonic sequence by recursively - - -# sorting its two halves in opposite sorting orders, and then -# calls bitonicMerge to make them in the same order -def bitonicSort(a, low, cnt, dire): - if cnt > 1: - k = int(cnt / 2) - bitonicSort(a, low, k, 1) - bitonicSort(a, low + k, k, 0) - bitonicMerge(a, low, cnt, dire) - - # Caller of bitonicSort for sorting the entire array of length N - - -# in ASCENDING order -def sort(a, N, up): - bitonicSort(a, 0, N, up) - - -# Driver code to test above -a = [] - -n = int(input()) -for i in range(n): - a.append(int(input())) -up = 1 - -sort(a, n, up) -print("\n\nSorted array is") -for i in range(n): - print("%d" % a[i]) diff --git a/sorts/__init__.py b/sorts/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/sorts/bead_sort.py b/sorts/bead_sort.py new file mode 100644 index 000000000000..3767e842d8c2 --- /dev/null +++ b/sorts/bead_sort.py @@ -0,0 +1,43 @@ +""" +Bead sort only works for sequences of nonegative integers. +https://en.wikipedia.org/wiki/Bead_sort +""" + + +def bead_sort(sequence: list) -> list: + """ + >>> bead_sort([6, 11, 12, 4, 1, 5]) + [1, 4, 5, 6, 11, 12] + + >>> bead_sort([9, 8, 7, 6, 5, 4 ,3, 2, 1]) + [1, 2, 3, 4, 5, 6, 7, 8, 9] + + >>> bead_sort([5, 0, 4, 3]) + [0, 3, 4, 5] + + >>> bead_sort([8, 2, 1]) + [1, 2, 8] + + >>> bead_sort([1, .9, 0.0, 0, -1, -.9]) + Traceback (most recent call last): + ... + TypeError: Sequence must be list of nonnegative integers + + >>> bead_sort("Hello world") + Traceback (most recent call last): + ... + TypeError: Sequence must be list of nonnegative integers + """ + if any(not isinstance(x, int) or x < 0 for x in sequence): + raise TypeError("Sequence must be list of nonnegative integers") + for _ in range(len(sequence)): + for i, (rod_upper, rod_lower) in enumerate(zip(sequence, sequence[1:])): + if rod_upper > rod_lower: + sequence[i] -= rod_upper - rod_lower + sequence[i + 1] += rod_upper - rod_lower + return sequence + + +if __name__ == "__main__": + assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] + assert bead_sort([7, 9, 4, 3, 5]) == [3, 4, 5, 7, 9] diff --git a/sorts/bitonic_sort.py b/sorts/bitonic_sort.py new file mode 100644 index 000000000000..c718973e5ecb --- /dev/null +++ b/sorts/bitonic_sort.py @@ -0,0 +1,96 @@ +""" +Python program for Bitonic Sort. + +Note that this program works only when size of input is a power of 2. +""" +from typing import List + + +def comp_and_swap(array: List[int], index1: int, index2: int, direction: int) -> None: + """Compare the value at given index1 and index2 of the array and swap them as per + the given direction. + + The parameter direction indicates the sorting direction, ASCENDING(1) or + DESCENDING(0); if (a[i] > a[j]) agrees with the direction, then a[i] and a[j] are + interchanged. + + >>> arr = [12, 42, -21, 1] + >>> comp_and_swap(arr, 1, 2, 1) + >>> print(arr) + [12, -21, 42, 1] + + >>> comp_and_swap(arr, 1, 2, 0) + >>> print(arr) + [12, 42, -21, 1] + + >>> comp_and_swap(arr, 0, 3, 1) + >>> print(arr) + [1, 42, -21, 12] + + >>> comp_and_swap(arr, 0, 3, 0) + >>> print(arr) + [12, 42, -21, 1] + """ + if (direction == 1 and array[index1] > array[index2]) or ( + direction == 0 and array[index1] < array[index2] + ): + array[index1], array[index2] = array[index2], array[index1] + + +def bitonic_merge(array: List[int], low: int, length: int, direction: int) -> None: + """ + It recursively sorts a bitonic sequence in ascending order, if direction = 1, and in + descending if direction = 0. + The sequence to be sorted starts at index position low, the parameter length is the + number of elements to be sorted. + + >>> arr = [12, 42, -21, 1] + >>> bitonic_merge(arr, 0, 4, 1) + >>> print(arr) + [-21, 1, 12, 42] + + >>> bitonic_merge(arr, 0, 4, 0) + >>> print(arr) + [42, 12, 1, -21] + """ + if length > 1: + middle = int(length / 2) + for i in range(low, low + middle): + comp_and_swap(array, i, i + middle, direction) + bitonic_merge(array, low, middle, direction) + bitonic_merge(array, low + middle, middle, direction) + + +def bitonic_sort(array: List[int], low: int, length: int, direction: int) -> None: + """ + This function first produces a bitonic sequence by recursively sorting its two + halves in opposite sorting orders, and then calls bitonic_merge to make them in the + same order. + + >>> arr = [12, 34, 92, -23, 0, -121, -167, 145] + >>> bitonic_sort(arr, 0, 8, 1) + >>> arr + [-167, -121, -23, 0, 12, 34, 92, 145] + + >>> bitonic_sort(arr, 0, 8, 0) + >>> arr + [145, 92, 34, 12, 0, -23, -121, -167] + """ + if length > 1: + middle = int(length / 2) + bitonic_sort(array, low, middle, 1) + bitonic_sort(array, low + middle, middle, 0) + bitonic_merge(array, low, length, direction) + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item.strip()) for item in user_input.split(",")] + + bitonic_sort(unsorted, 0, len(unsorted), 1) + print("\nSorted array in ascending order is: ", end="") + print(*unsorted, sep=", ") + + bitonic_merge(unsorted, 0, len(unsorted), 0) + print("Sorted array in descending order is: ", end="") + print(*unsorted, sep=", ") diff --git a/sorts/bogo_sort.py b/sorts/bogo_sort.py new file mode 100644 index 000000000000..b72f2089f3d2 --- /dev/null +++ b/sorts/bogo_sort.py @@ -0,0 +1,49 @@ +""" +This is a pure Python implementation of the bogosort algorithm, +also known as permutation sort, stupid sort, slowsort, shotgun sort, or monkey sort. +Bogosort generates random permutations until it guesses the correct one. + +More info on: https://en.wikipedia.org/wiki/Bogosort + +For doctests run following command: +python -m doctest -v bogo_sort.py +or +python3 -m doctest -v bogo_sort.py +For manual testing run: +python bogo_sort.py +""" + +import random + + +def bogo_sort(collection): + """Pure implementation of the bogosort algorithm in Python + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + Examples: + >>> bogo_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + >>> bogo_sort([]) + [] + >>> bogo_sort([-2, -5, -45]) + [-45, -5, -2] + """ + + def is_sorted(collection): + if len(collection) < 2: + return True + for i in range(len(collection) - 1): + if collection[i] > collection[i + 1]: + return False + return True + + while not is_sorted(collection): + random.shuffle(collection) + return collection + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(bogo_sort(unsorted)) diff --git a/sorts/bogosort.py b/sorts/bogosort.py deleted file mode 100644 index 33eac66bf21c..000000000000 --- a/sorts/bogosort.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -This is a pure python implementation of the bogosort algorithm -For doctests run following command: -python -m doctest -v bogosort.py -or -python3 -m doctest -v bogosort.py -For manual testing run: -python bogosort.py -""" - -from __future__ import print_function -import random - - -def bogosort(collection): - """Pure implementation of the bogosort algorithm in Python - :param collection: some mutable ordered collection with heterogeneous - comparable items inside - :return: the same collection ordered by ascending - Examples: - >>> bogosort([0, 5, 3, 2, 2]) - [0, 2, 2, 3, 5] - >>> bogosort([]) - [] - >>> bogosort([-2, -5, -45]) - [-45, -5, -2] - """ - - def isSorted(collection): - if len(collection) < 2: - return True - for i in range(len(collection) - 1): - if collection[i] > collection[i + 1]: - return False - return True - - while not isSorted(collection): - random.shuffle(collection) - return collection - -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] - print(bogosort(unsorted)) diff --git a/sorts/bubble_sort.py b/sorts/bubble_sort.py index e17fc3358d53..d4f0d25ca77c 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -1,6 +1,3 @@ -from __future__ import print_function - - def bubble_sort(collection): """Pure implementation of bubble sort algorithm in Python @@ -9,34 +6,47 @@ def bubble_sort(collection): :return: the same collection ordered by ascending Examples: - >>> bubble_sort([0, 5, 3, 2, 2]) + >>> bubble_sort([0, 5, 2, 3, 2]) [0, 2, 2, 3, 5] - - >>> bubble_sort([]) - [] - - >>> bubble_sort([-2, -5, -45]) - [-45, -5, -2] - - >>> bubble_sort([-23,0,6,-4,34]) - [-23,-4,0,6,34] + >>> bubble_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2]) + True + >>> bubble_sort([]) == sorted([]) + True + >>> bubble_sort([-2, -45, -5]) == sorted([-2, -45, -5]) + True + >>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34]) + True + >>> bubble_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c']) + True + >>> import random + >>> collection = random.sample(range(-50, 50), 100) + >>> bubble_sort(collection) == sorted(collection) + True + >>> import string + >>> collection = random.choices(string.ascii_letters + string.digits, k=100) + >>> bubble_sort(collection) == sorted(collection) + True """ length = len(collection) - for i in range(length-1): + for i in range(length - 1): swapped = False - for j in range(length-1-i): - if collection[j] > collection[j+1]: + for j in range(length - 1 - i): + if collection[j] > collection[j + 1]: swapped = True - collection[j], collection[j+1] = collection[j+1], collection[j] - if not swapped: break # Stop iteration if the collection is sorted. + collection[j], collection[j + 1] = collection[j + 1], collection[j] + if not swapped: + break # Stop iteration if the collection is sorted. return collection -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - user_input = raw_input('Enter numbers separated by a comma:').strip() - unsorted = [int(item) for item in user_input.split(',')] - print(*bubble_sort(unsorted), sep=',') +if __name__ == "__main__": + import doctest + import time + + doctest.testmod() + + user_input = input("Enter numbers separated by a comma:").strip() + unsorted = [int(item) for item in user_input.split(",")] + start = time.process_time() + print(*bubble_sort(unsorted), sep=",") + print(f"Processing time: {time.process_time() - start}") diff --git a/sorts/bucket_sort.py b/sorts/bucket_sort.py index bd4281e463eb..a0566be662e3 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -1,57 +1,67 @@ -#!/usr/bin/env python -# Author: OMKAR PATHAK -# This program will illustrate how to implement bucket sort algorithm - -# Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the -# elements of an array into a number of buckets. Each bucket is then sorted individually, either using -# a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a -# distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. -# Bucket sort is a generalization of pigeonhole sort. Bucket sort can be implemented with comparisons -# and therefore can also be considered a comparison sort algorithm. The computational complexity estimates -# involve the number of buckets. - -# Time Complexity of Solution: -# Best Case O(n); Average Case O(n); Worst Case O(n) - -from __future__ import print_function -from insertion_sort import insertion_sort -import math - -DEFAULT_BUCKET_SIZE = 5 - -def bucketSort(myList, bucketSize=DEFAULT_BUCKET_SIZE): - if(len(myList) == 0): - print('You don\'t have any elements in array!') - - minValue = myList[0] - maxValue = myList[0] - - # For finding minimum and maximum values - for i in range(0, len(myList)): - if myList[i] < minValue: - minValue = myList[i] - elif myList[i] > maxValue: - maxValue = myList[i] - - # Initialize buckets - bucketCount = math.floor((maxValue - minValue) / bucketSize) + 1 - buckets = [] - for i in range(0, bucketCount): - buckets.append([]) - - # For putting values in buckets - for i in range(0, len(myList)): - buckets[math.floor((myList[i] - minValue) / bucketSize)].append(myList[i]) - - # Sort buckets and place back into input array - sortedArray = [] - for i in range(0, len(buckets)): - insertion_sort(buckets[i]) - for j in range(0, len(buckets[i])): - sortedArray.append(buckets[i][j]) - - return sortedArray - -if __name__ == '__main__': - sortedArray = bucketSort([12, 23, 4, 5, 3, 2, 12, 81, 56, 95]) - print(sortedArray) +#!/usr/bin/env python3 +""" +Illustrate how to implement bucket sort algorithm. + +Author: OMKAR PATHAK +This program will illustrate how to implement bucket sort algorithm + +Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works +by distributing the elements of an array into a number of buckets. +Each bucket is then sorted individually, either using a different sorting +algorithm, or by recursively applying the bucket sorting algorithm. It is a +distribution sort, and is a cousin of radix sort in the most to least +significant digit flavour. +Bucket sort is a generalization of pigeonhole sort. Bucket sort can be +implemented with comparisons and therefore can also be considered a +comparison sort algorithm. The computational complexity estimates involve the +number of buckets. + +Time Complexity of Solution: +Worst case scenario occurs when all the elements are placed in a single bucket. +The overall performance would then be dominated by the algorithm used to sort each +bucket. In this case, O(n log n), because of TimSort + +Average Case O(n + (n^2)/k + k), where k is the number of buckets + +If k = O(n), time complexity is O(n) + +Source: https://en.wikipedia.org/wiki/Bucket_sort +""" + + +def bucket_sort(my_list: list) -> list: + """ + >>> data = [-1, 2, -5, 0] + >>> bucket_sort(data) == sorted(data) + True + >>> data = [9, 8, 7, 6, -12] + >>> bucket_sort(data) == sorted(data) + True + >>> data = [.4, 1.2, .1, .2, -.9] + >>> bucket_sort(data) == sorted(data) + True + >>> bucket_sort([]) == sorted([]) + True + >>> import random + >>> collection = random.sample(range(-50, 50), 50) + >>> bucket_sort(collection) == sorted(collection) + True + """ + if len(my_list) == 0: + return [] + min_value, max_value = min(my_list), max(my_list) + bucket_count = int(max_value - min_value) + 1 + buckets = [[] for _ in range(bucket_count)] + + for i in range(len(my_list)): + buckets[(int(my_list[i] - min_value) // bucket_count)].append(my_list[i]) + + return [v for bucket in buckets for v in sorted(bucket)] + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] + assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15] diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py index 8ad3383bbe9f..42015abc5f97 100644 --- a/sorts/cocktail_shaker_sort.py +++ b/sorts/cocktail_shaker_sort.py @@ -1,32 +1,45 @@ -from __future__ import print_function +""" https://en.wikipedia.org/wiki/Cocktail_shaker_sort """ -def cocktail_shaker_sort(unsorted): + +def cocktail_shaker_sort(unsorted: list) -> list: """ Pure implementation of the cocktail shaker sort algorithm in Python. + >>> cocktail_shaker_sort([4, 5, 2, 1, 2]) + [1, 2, 2, 4, 5] + + >>> cocktail_shaker_sort([-4, 5, 0, 1, 2, 11]) + [-4, 0, 1, 2, 5, 11] + + >>> cocktail_shaker_sort([0.1, -2.4, 4.4, 2.2]) + [-2.4, 0.1, 2.2, 4.4] + + >>> cocktail_shaker_sort([1, 2, 3, 4, 5]) + [1, 2, 3, 4, 5] + + >>> cocktail_shaker_sort([-4, -5, -24, -7, -11]) + [-24, -11, -7, -5, -4] """ - for i in range(len(unsorted)-1, 0, -1): + for i in range(len(unsorted) - 1, 0, -1): swapped = False - + for j in range(i, 0, -1): - if unsorted[j] < unsorted[j-1]: - unsorted[j], unsorted[j-1] = unsorted[j-1], unsorted[j] + if unsorted[j] < unsorted[j - 1]: + unsorted[j], unsorted[j - 1] = unsorted[j - 1], unsorted[j] swapped = True for j in range(i): - if unsorted[j] > unsorted[j+1]: - unsorted[j], unsorted[j+1] = unsorted[j+1], unsorted[j] + if unsorted[j] > unsorted[j + 1]: + unsorted[j], unsorted[j + 1] = unsorted[j + 1], unsorted[j] swapped = True - + if not swapped: return unsorted - -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] - cocktail_shaker_sort(unsorted) - print(unsorted) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(f"{cocktail_shaker_sort(unsorted) = }") diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py index 22b6f66f04cc..16bd10c78fe5 100644 --- a/sorts/comb_sort.py +++ b/sorts/comb_sort.py @@ -1,8 +1,15 @@ """ -Comb sort is a relatively simple sorting algorithm originally designed by Wlodzimierz Dobosiewicz in 1980. -Later it was rediscovered by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort. +This is pure Python implementation of comb sort algorithm. +Comb sort is a relatively simple sorting algorithm originally designed by Wlodzimierz +Dobosiewicz in 1980. It was rediscovered by Stephen Lacey and Richard Box in 1991. +Comb sort improves on bubble sort algorithm. +In bubble sort, distance (or gap) between two compared elements is always one. +Comb sort improvement is that gap can be much more than 1, in order to prevent slowing +down by small values +at the end of a list. + +More info on: https://en.wikipedia.org/wiki/Comb_sort -This is pure python implementation of comb sort algorithm For doctests run following command: python -m doctest -v comb_sort.py or @@ -12,47 +19,46 @@ python comb_sort.py """ -def comb_sort(data): + +def comb_sort(data: list) -> list: """Pure implementation of comb sort algorithm in Python - :param collection: some mutable ordered collection with heterogeneous - comparable items inside - :return: the same collection ordered by ascending + :param data: mutable collection with comparable items + :return: the same collection in ascending order Examples: >>> comb_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> comb_sort([]) [] - >>> comb_sort([-2, -5, -45]) - [-45, -5, -2] + >>> comb_sort([99, 45, -7, 8, 2, 0, -15, 3]) + [-15, -7, 0, 2, 3, 8, 45, 99] """ shrink_factor = 1.3 gap = len(data) - swapped = True - i = 0 + completed = False - while gap > 1 or swapped: - # Update the gap value for a next comb - gap = int(float(gap) / shrink_factor) + while not completed: - swapped = False - i = 0 + # Update the gap value for a next comb + gap = int(gap / shrink_factor) + if gap <= 1: + completed = True - while gap + i < len(data): - if data[i] > data[i+gap]: + index = 0 + while index + gap < len(data): + if data[index] > data[index + gap]: # Swap values - data[i], data[i+gap] = data[i+gap], data[i] - swapped = True - i += 1 + data[index], data[index + gap] = data[index + gap], data[index] + completed = False + index += 1 return data -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 +if __name__ == "__main__": + import doctest + + doctest.testmod() - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(comb_sort(unsorted)) diff --git a/sorts/counting_sort.py b/sorts/counting_sort.py index ad98f1a0da4c..892ec5d5f344 100644 --- a/sorts/counting_sort.py +++ b/sorts/counting_sort.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of counting sort algorithm +This is pure Python implementation of counting sort algorithm For doctests run following command: python -m doctest -v counting_sort.py or @@ -8,8 +8,6 @@ python counting_sort.py """ -from __future__ import print_function - def counting_sort(collection): """Pure implementation of counting sort algorithm in Python @@ -44,7 +42,7 @@ def counting_sort(collection): # sum each position with it's predecessors. now, counting_arr[i] tells # us how many elements <= i has in the collection for i in range(1, counting_arr_length): - counting_arr[i] = counting_arr[i] + counting_arr[i-1] + counting_arr[i] = counting_arr[i] + counting_arr[i - 1] # create the output collection ordered = [0] * coll_len @@ -52,24 +50,24 @@ def counting_sort(collection): # place the elements in the output, respecting the original order (stable # sort) from end to begin, updating counting_arr for i in reversed(range(0, coll_len)): - ordered[counting_arr[collection[i] - coll_min]-1] = collection[i] + ordered[counting_arr[collection[i] - coll_min] - 1] = collection[i] counting_arr[collection[i] - coll_min] -= 1 return ordered + def counting_sort_string(string): - return ''.join([chr(i) for i in counting_sort([ord(c) for c in string])]) + """ + >>> counting_sort_string("thisisthestring") + 'eghhiiinrsssttt' + """ + return "".join([chr(i) for i in counting_sort([ord(c) for c in string])]) -if __name__ == '__main__': +if __name__ == "__main__": # Test string sort assert "eghhiiinrsssttt" == counting_sort_string("thisisthestring") - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(counting_sort(unsorted)) diff --git a/sorts/cycle_sort.py b/sorts/cycle_sort.py new file mode 100644 index 000000000000..806f40441d79 --- /dev/null +++ b/sorts/cycle_sort.py @@ -0,0 +1,53 @@ +""" +Code contributed by Honey Sharma +Source: https://en.wikipedia.org/wiki/Cycle_sort +""" + + +def cycle_sort(array: list) -> list: + """ + >>> cycle_sort([4, 3, 2, 1]) + [1, 2, 3, 4] + + >>> cycle_sort([-4, 20, 0, -50, 100, -1]) + [-50, -4, -1, 0, 20, 100] + + >>> cycle_sort([-.1, -.2, 1.3, -.8]) + [-0.8, -0.2, -0.1, 1.3] + + >>> cycle_sort([]) + [] + """ + array_len = len(array) + for cycle_start in range(0, array_len - 1): + item = array[cycle_start] + + pos = cycle_start + for i in range(cycle_start + 1, array_len): + if array[i] < item: + pos += 1 + + if pos == cycle_start: + continue + + while item == array[pos]: + pos += 1 + + array[pos], item = item, array[pos] + while pos != cycle_start: + pos = cycle_start + for i in range(cycle_start + 1, array_len): + if array[i] < item: + pos += 1 + + while item == array[pos]: + pos += 1 + + array[pos], item = item, array[pos] + + return array + + +if __name__ == "__main__": + assert cycle_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5] + assert cycle_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15] diff --git a/sorts/cyclesort.py b/sorts/cyclesort.py deleted file mode 100644 index ee19a1ade360..000000000000 --- a/sorts/cyclesort.py +++ /dev/null @@ -1,60 +0,0 @@ -# Code contributed by Honey Sharma -from __future__ import print_function - - -def cycle_sort(array): - ans = 0 - - # Pass through the array to find cycles to rotate. - for cycleStart in range(0, len(array) - 1): - item = array[cycleStart] - - # finding the position for putting the item. - pos = cycleStart - for i in range(cycleStart + 1, len(array)): - if array[i] < item: - pos += 1 - - # If the item is already present-not a cycle. - if pos == cycleStart: - continue - - # Otherwise, put the item there or right after any duplicates. - while item == array[pos]: - pos += 1 - array[pos], item = item, array[pos] - ans += 1 - - # Rotate the rest of the cycle. - while pos != cycleStart: - - # Find where to put the item. - pos = cycleStart - for i in range(cycleStart + 1, len(array)): - if array[i] < item: - pos += 1 - - # Put the item there or right after any duplicates. - while item == array[pos]: - pos += 1 - array[pos], item = item, array[pos] - ans += 1 - - return ans - - -# Main Code starts here -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - -user_input = raw_input('Enter numbers separated by a comma:\n') -unsorted = [int(item) for item in user_input.split(',')] -n = len(unsorted) -cycle_sort(unsorted) - -print("After sort : ") -for i in range(0, n): - print(unsorted[i], end=' ') diff --git a/sorts/double_sort.py b/sorts/double_sort.py new file mode 100644 index 000000000000..04e18682017c --- /dev/null +++ b/sorts/double_sort.py @@ -0,0 +1,42 @@ +def double_sort(lst): + """this sorting algorithm sorts an array using the principle of bubble sort, + but does it both from left to right and right to left, + hence i decided to call it "double sort" + :param collection: mutable ordered sequence of elements + :return: the same collection in ascending order + Examples: + >>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6 ,-7]) + [-7, -6, -5, -4, -3, -2, -1] + >>> double_sort([]) + [] + >>> double_sort([-1 ,-2 ,-3 ,-4 ,-5 ,-6]) + [-6, -5, -4, -3, -2, -1] + >>> double_sort([-3, 10, 16, -42, 29]) == sorted([-3, 10, 16, -42, 29]) + True + """ + no_of_elements = len(lst) + for i in range( + 0, int(((no_of_elements - 1) / 2) + 1) + ): # we don't need to traverse to end of list as + for j in range(0, no_of_elements - 1): + if ( + lst[j + 1] < lst[j] + ): # applying bubble sort algorithm from left to right (or forwards) + temp = lst[j + 1] + lst[j + 1] = lst[j] + lst[j] = temp + if ( + lst[no_of_elements - 1 - j] < lst[no_of_elements - 2 - j] + ): # applying bubble sort algorithm from right to left (or backwards) + temp = lst[no_of_elements - 1 - j] + lst[no_of_elements - 1 - j] = lst[no_of_elements - 2 - j] + lst[no_of_elements - 2 - j] = temp + return lst + + +if __name__ == "__main__": + print("enter the list to be sorted") + lst = [int(x) for x in input().split()] # inputing elements of the list in one line + sorted_lst = double_sort(lst) + print("the sorted list is") + print(sorted_lst) diff --git a/sorts/external-sort.py b/sorts/external_sort.py similarity index 76% rename from sorts/external-sort.py rename to sorts/external_sort.py index 1638e9efafee..060e67adf827 100644 --- a/sorts/external-sort.py +++ b/sorts/external_sort.py @@ -3,11 +3,12 @@ # # Sort large text files in a minimum amount of memory # -import os import argparse +import os + -class FileSplitter(object): - BLOCK_FILENAME_FORMAT = 'block_{0}.dat' +class FileSplitter: + BLOCK_FILENAME_FORMAT = "block_{0}.dat" def __init__(self, filename): self.filename = filename @@ -15,7 +16,7 @@ def __init__(self, filename): def write_block(self, data, block_number): filename = self.BLOCK_FILENAME_FORMAT.format(block_number) - with open(filename, 'w') as file: + with open(filename, "w") as file: file.write(data) self.block_filenames.append(filename) @@ -36,14 +37,14 @@ def split(self, block_size, sort_key=None): else: lines.sort(key=sort_key) - self.write_block(''.join(lines), i) + self.write_block("".join(lines), i) i += 1 def cleanup(self): map(lambda f: os.remove(f), self.block_filenames) -class NWayMerge(object): +class NWayMerge: def select(self, choices): min_index = -1 min_str = None @@ -55,7 +56,7 @@ def select(self, choices): return min_index -class FilesArray(object): +class FilesArray: def __init__(self, files): self.files = files self.empty = set() @@ -63,14 +64,16 @@ def __init__(self, files): self.buffers = {i: None for i in range(self.num_buffers)} def get_dict(self): - return {i: self.buffers[i] for i in range(self.num_buffers) if i not in self.empty} + return { + i: self.buffers[i] for i in range(self.num_buffers) if i not in self.empty + } def refresh(self): for i in range(self.num_buffers): if self.buffers[i] is None and i not in self.empty: self.buffers[i] = self.files[i].readline() - if self.buffers[i] == '': + if self.buffers[i] == "": self.empty.add(i) self.files[i].close() @@ -86,13 +89,13 @@ def unshift(self, index): return value -class FileMerger(object): +class FileMerger: def __init__(self, merge_strategy): self.merge_strategy = merge_strategy def merge(self, filenames, outfilename, buffer_size): buffers = FilesArray(self.get_file_handles(filenames, buffer_size)) - with open(outfilename, 'w', buffer_size) as outfile: + with open(outfilename, "w", buffer_size) as outfile: while buffers.refresh(): min_index = self.merge_strategy.select(buffers.get_dict()) outfile.write(buffers.unshift(min_index)) @@ -101,13 +104,12 @@ def get_file_handles(self, filenames, buffer_size): files = {} for i in range(len(filenames)): - files[i] = open(filenames[i], 'r', buffer_size) + files[i] = open(filenames[i], "r", buffer_size) return files - -class ExternalSort(object): +class ExternalSort: def __init__(self, block_size): self.block_size = block_size @@ -118,7 +120,7 @@ def sort(self, filename, sort_key=None): merger = FileMerger(NWayMerge()) buffer_size = self.block_size / (num_blocks + 1) - merger.merge(splitter.get_block_filenames(), filename + '.out', buffer_size) + merger.merge(splitter.get_block_filenames(), filename + ".out", buffer_size) splitter.cleanup() @@ -127,32 +129,29 @@ def get_number_blocks(self, filename, block_size): def parse_memory(string): - if string[-1].lower() == 'k': + if string[-1].lower() == "k": return int(string[:-1]) * 1024 - elif string[-1].lower() == 'm': + elif string[-1].lower() == "m": return int(string[:-1]) * 1024 * 1024 - elif string[-1].lower() == 'g': + elif string[-1].lower() == "g": return int(string[:-1]) * 1024 * 1024 * 1024 else: return int(string) - def main(): parser = argparse.ArgumentParser() - parser.add_argument('-m', - '--mem', - help='amount of memory to use for sorting', - default='100M') - parser.add_argument('filename', - metavar='', - nargs=1, - help='name of file to sort') + parser.add_argument( + "-m", "--mem", help="amount of memory to use for sorting", default="100M" + ) + parser.add_argument( + "filename", metavar="", nargs=1, help="name of file to sort" + ) args = parser.parse_args() sorter = ExternalSort(parse_memory(args.mem)) sorter.sort(args.filename[0]) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/sorts/gnome_sort.py b/sorts/gnome_sort.py index 2927b097f11d..ea96e0a926a3 100644 --- a/sorts/gnome_sort.py +++ b/sorts/gnome_sort.py @@ -1,30 +1,56 @@ -from __future__ import print_function +""" +Gnome Sort Algorithm (A.K.A. Stupid Sort) -def gnome_sort(unsorted): +This algorithm iterates over a list comparing an element with the previous one. +If order is not respected, it swaps element backward until order is respected with +previous element. It resumes the initial iteration from element new position. + +For doctests run following command: +python3 -m doctest -v gnome_sort.py + +For manual testing run: +python3 gnome_sort.py +""" + + +def gnome_sort(lst: list) -> list: """ - Pure implementation of the gnome sort algorithm in Python. + Pure implementation of the gnome sort algorithm in Python + + Take some mutable ordered collection with heterogeneous comparable items inside as + arguments, return the same collection ordered by ascending. + + Examples: + >>> gnome_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + + >>> gnome_sort([]) + [] + + >>> gnome_sort([-2, -5, -45]) + [-45, -5, -2] + + >>> "".join(gnome_sort(list(set("Gnomes are stupid!")))) + ' !Gadeimnoprstu' """ - if len(unsorted) <= 1: - return unsorted - + if len(lst) <= 1: + return lst + i = 1 - - while i < len(unsorted): - if unsorted[i-1] <= unsorted[i]: + + while i < len(lst): + if lst[i - 1] <= lst[i]: i += 1 else: - unsorted[i-1], unsorted[i] = unsorted[i], unsorted[i-1] + lst[i - 1], lst[i] = lst[i], lst[i - 1] i -= 1 - if (i == 0): + if i == 0: i = 1 - -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] - gnome_sort(unsorted) - print(unsorted) + + return lst + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(gnome_sort(unsorted)) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 3c72abca8059..4dca879bd89c 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -1,5 +1,5 @@ -''' -This is a pure python implementation of the heap sort algorithm. +""" +This is a pure Python implementation of the heap sort algorithm. For doctests run following command: python -m doctest -v heap_sort.py @@ -8,9 +8,7 @@ For manual testing run: python heap_sort.py -''' - -from __future__ import print_function +""" def heapify(unsorted, index, heap_size): @@ -29,7 +27,7 @@ def heapify(unsorted, index, heap_size): def heap_sort(unsorted): - ''' + """ Pure implementation of the heap sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -44,7 +42,7 @@ def heap_sort(unsorted): >>> heap_sort([-2, -5, -45]) [-45, -5, -2] - ''' + """ n = len(unsorted) for i in range(n // 2 - 1, -1, -1): heapify(unsorted, i, n) @@ -53,12 +51,8 @@ def heap_sort(unsorted): heapify(unsorted, 0, i) return unsorted -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(heap_sort(unsorted)) diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index 59917ac059a7..6d5bb2b46013 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -1,19 +1,21 @@ """ -This is a pure python implementation of the insertion sort algorithm +A pure Python implementation of the insertion sort algorithm + +This algorithm sorts a collection by comparing adjacent elements. +When it finds that order is not respected, it moves the element compared +backward until the order is correct. It then goes back directly to the +element's initial position resuming forward comparison. For doctests run following command: -python -m doctest -v insertion_sort.py -or python3 -m doctest -v insertion_sort.py For manual testing run: -python insertion_sort.py +python3 insertion_sort.py """ -from __future__ import print_function -def insertion_sort(collection): - """Pure implementation of the insertion sort algorithm in Python +def insertion_sort(collection: list) -> list: + """A pure Python implementation of the insertion sort algorithm :param collection: some mutable ordered collection with heterogeneous comparable items inside @@ -22,27 +24,37 @@ def insertion_sort(collection): Examples: >>> insertion_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] - - >>> insertion_sort([]) - [] - - >>> insertion_sort([-2, -5, -45]) - [-45, -5, -2] + >>> insertion_sort([]) == sorted([]) + True + >>> insertion_sort([-2, -5, -45]) == sorted([-2, -5, -45]) + True + >>> insertion_sort(['d', 'a', 'b', 'e', 'c']) == sorted(['d', 'a', 'b', 'e', 'c']) + True + >>> import random + >>> collection = random.sample(range(-50, 50), 100) + >>> insertion_sort(collection) == sorted(collection) + True + >>> import string + >>> collection = random.choices(string.ascii_letters + string.digits, k=100) + >>> insertion_sort(collection) == sorted(collection) + True """ - for index in range(1, len(collection)): - while index > 0 and collection[index - 1] > collection[index]: - collection[index], collection[index - 1] = collection[index - 1], collection[index] - index -= 1 + for insert_index, insert_value in enumerate(collection[1:]): + temp_index = insert_index + while insert_index >= 0 and insert_value < collection[insert_index]: + collection[insert_index + 1] = collection[insert_index] + insert_index -= 1 + if insert_index != temp_index: + collection[insert_index + 1] = insert_value return collection -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 +if __name__ == "__main__": + from doctest import testmod + + testmod() - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] - print(insertion_sort(unsorted)) + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(f"{insertion_sort(unsorted) = }") diff --git a/sorts/intro_sort.py b/sorts/intro_sort.py new file mode 100644 index 000000000000..f0e3645adbb7 --- /dev/null +++ b/sorts/intro_sort.py @@ -0,0 +1,173 @@ +""" +Introspective Sort is hybrid sort (Quick Sort + Heap Sort + Insertion Sort) +if the size of the list is under 16, use insertion sort +https://en.wikipedia.org/wiki/Introsort +""" +import math + + +def insertion_sort(array: list, start: int = 0, end: int = 0) -> list: + """ + >>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12] + + >>> insertion_sort(array, 0, len(array)) + [1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79] + """ + end = end or len(array) + for i in range(start, end): + temp_index = i + temp_index_value = array[i] + while temp_index != start and temp_index_value < array[temp_index - 1]: + array[temp_index] = array[temp_index - 1] + temp_index -= 1 + array[temp_index] = temp_index_value + return array + + +def heapify(array: list, index: int, heap_size: int) -> None: # Max Heap + """ + >>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12] + + >>> heapify(array, len(array) // 2 ,len(array)) + """ + largest = index + left_index = 2 * index + 1 # Left Node + right_index = 2 * index + 2 # Right Node + + if left_index < heap_size and array[largest] < array[left_index]: + largest = left_index + + if right_index < heap_size and array[largest] < array[right_index]: + largest = right_index + + if largest != index: + array[index], array[largest] = array[largest], array[index] + heapify(array, largest, heap_size) + + +def heap_sort(array: list) -> list: + """ + >>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12] + + >>> heap_sort(array) + [1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79] + """ + n = len(array) + + for i in range(n // 2, -1, -1): + heapify(array, i, n) + + for i in range(n - 1, 0, -1): + array[i], array[0] = array[0], array[i] + heapify(array, 0, i) + + return array + + +def median_of_3( + array: list, first_index: int, middle_index: int, last_index: int +) -> int: + """ + >>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12] + + >>> median_of_3(array, 0, 0 + ((len(array) - 0) // 2) + 1, len(array) - 1) + 12 + """ + if (array[first_index] > array[middle_index]) != ( + array[first_index] > array[last_index] + ): + return array[first_index] + elif (array[middle_index] > array[first_index]) != ( + array[middle_index] > array[last_index] + ): + return array[middle_index] + else: + return array[last_index] + + +def partition(array: list, low: int, high: int, pivot: int) -> int: + """ + >>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12] + + >>> partition(array, 0, len(array), 12) + 8 + """ + i = low + j = high + while True: + while array[i] < pivot: + i += 1 + j -= 1 + while pivot < array[j]: + j -= 1 + if i >= j: + return i + array[i], array[j] = array[j], array[i] + i += 1 + + +def sort(array: list) -> list: + """ + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + Examples: + >>> sort([4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]) + [1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79] + + >>> sort([-1, -5, -3, -13, -44]) + [-44, -13, -5, -3, -1] + + >>> sort([]) + [] + + >>> sort([5]) + [5] + + >>> sort([-3, 0, -7, 6, 23, -34]) + [-34, -7, -3, 0, 6, 23] + + >>> sort([1.7, 1.0, 3.3, 2.1, 0.3 ]) + [0.3, 1.0, 1.7, 2.1, 3.3] + + >>> sort(['d', 'a', 'b', 'e', 'c']) + ['a', 'b', 'c', 'd', 'e'] + """ + if len(array) == 0: + return array + max_depth = 2 * math.ceil(math.log2(len(array))) + size_threshold = 16 + return intro_sort(array, 0, len(array), size_threshold, max_depth) + + +def intro_sort( + array: list, start: int, end: int, size_threshold: int, max_depth: int +) -> list: + """ + >>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12] + + >>> max_depth = 2 * math.ceil(math.log2(len(array))) + + >>> intro_sort(array, 0, len(array), 16, max_depth) + [1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79] + """ + while end - start > size_threshold: + if max_depth == 0: + return heap_sort(array) + max_depth -= 1 + pivot = median_of_3(array, start, start + ((end - start) // 2) + 1, end - 1) + p = partition(array, start, end, pivot) + intro_sort(array, p, end, size_threshold, max_depth) + end = p + return insertion_sort(array, start, end) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + user_input = input("Enter numbers separated by a comma : ").strip() + unsorted = [float(item) for item in user_input.split(",")] + print(sort(unsorted)) diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py new file mode 100644 index 000000000000..5ee0badab9e6 --- /dev/null +++ b/sorts/iterative_merge_sort.py @@ -0,0 +1,73 @@ +""" +Implementation of iterative merge sort in Python +Author: Aman Gupta + +For doctests run following command: +python3 -m doctest -v iterative_merge_sort.py + +For manual testing run: +python3 iterative_merge_sort.py +""" + +from __future__ import annotations + + +def merge(input_list: list, low: int, mid: int, high: int) -> list: + """ + sorting left-half and right-half individually + then merging them into result + """ + result = [] + left, right = input_list[low:mid], input_list[mid : high + 1] + while left and right: + result.append((left if left[0] <= right[0] else right).pop(0)) + input_list[low : high + 1] = result + left + right + return input_list + + +# iteration over the unsorted list +def iter_merge_sort(input_list: list) -> list: + """ + Return a sorted copy of the input list + + >>> iter_merge_sort([5, 9, 8, 7, 1, 2, 7]) + [1, 2, 5, 7, 7, 8, 9] + >>> iter_merge_sort([6]) + [6] + >>> iter_merge_sort([]) + [] + >>> iter_merge_sort([-2, -9, -1, -4]) + [-9, -4, -2, -1] + >>> iter_merge_sort([1.1, 1, 0.0, -1, -1.1]) + [-1.1, -1, 0.0, 1, 1.1] + >>> iter_merge_sort(['c', 'b', 'a']) + ['a', 'b', 'c'] + >>> iter_merge_sort('cba') + ['a', 'b', 'c'] + """ + if len(input_list) <= 1: + return input_list + input_list = list(input_list) + + # iteration for two-way merging + p = 2 + while p < len(input_list): + # getting low, high and middle value for merge-sort of single list + for i in range(0, len(input_list), p): + low = i + high = i + p - 1 + mid = (low + high + 1) // 2 + input_list = merge(input_list, low, mid, high) + # final merge of last two parts + if p * 2 >= len(input_list): + mid = i + input_list = merge(input_list, 0, mid, len(input_list) - 1) + p *= 2 + + return input_list + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item.strip()) for item in user_input.split(",")] + print(iter_merge_sort(unsorted)) diff --git a/sorts/merge_insertion_sort.py b/sorts/merge_insertion_sort.py new file mode 100644 index 000000000000..fb71d84a3c14 --- /dev/null +++ b/sorts/merge_insertion_sort.py @@ -0,0 +1,179 @@ +""" +This is a pure Python implementation of the merge-insertion sort algorithm +Source: https://en.wikipedia.org/wiki/Merge-insertion_sort + +For doctests run following command: +python3 -m doctest -v merge_insertion_sort.py +or +python -m doctest -v merge_insertion_sort.py + +For manual testing run: +python3 merge_insertion_sort.py +""" + +from __future__ import annotations + + +def merge_insertion_sort(collection: list[int]) -> list[int]: + """Pure implementation of merge-insertion sort algorithm in Python + + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + Examples: + >>> merge_insertion_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + + >>> merge_insertion_sort([99]) + [99] + + >>> merge_insertion_sort([-2, -5, -45]) + [-45, -5, -2] + """ + + def binary_search_insertion(sorted_list, item): + left = 0 + right = len(sorted_list) - 1 + while left <= right: + middle = (left + right) // 2 + if left == right: + if sorted_list[middle] < item: + left = middle + 1 + break + elif sorted_list[middle] < item: + left = middle + 1 + else: + right = middle - 1 + sorted_list.insert(left, item) + return sorted_list + + def sortlist_2d(list_2d): + def merge(left, right): + result = [] + while left and right: + if left[0][0] < right[0][0]: + result.append(left.pop(0)) + else: + result.append(right.pop(0)) + return result + left + right + + length = len(list_2d) + if length <= 1: + return list_2d + middle = length // 2 + return merge(sortlist_2d(list_2d[:middle]), sortlist_2d(list_2d[middle:])) + + if len(collection) <= 1: + return collection + + """ + Group the items into two pairs, and leave one element if there is a last odd item. + + Example: [999, 100, 75, 40, 10000] + -> [999, 100], [75, 40]. Leave 10000. + """ + two_paired_list = [] + has_last_odd_item = False + for i in range(0, len(collection), 2): + if i == len(collection) - 1: + has_last_odd_item = True + else: + """ + Sort two-pairs in each groups. + + Example: [999, 100], [75, 40] + -> [100, 999], [40, 75] + """ + if collection[i] < collection[i + 1]: + two_paired_list.append([collection[i], collection[i + 1]]) + else: + two_paired_list.append([collection[i + 1], collection[i]]) + + """ + Sort two_paired_list. + + Example: [100, 999], [40, 75] + -> [40, 75], [100, 999] + """ + sorted_list_2d = sortlist_2d(two_paired_list) + + """ + 40 < 100 is sure because it has already been sorted. + Generate the sorted_list of them so that you can avoid unnecessary comparison. + + Example: + group0 group1 + 40 100 + 75 999 + -> + group0 group1 + [40, 100] + 75 999 + """ + result = [i[0] for i in sorted_list_2d] + + """ + 100 < 999 is sure because it has already been sorted. + Put 999 in last of the sorted_list so that you can avoid unnecessary comparison. + + Example: + group0 group1 + [40, 100] + 75 999 + -> + group0 group1 + [40, 100, 999] + 75 + """ + result.append(sorted_list_2d[-1][1]) + + """ + Insert the last odd item left if there is. + + Example: + group0 group1 + [40, 100, 999] + 75 + -> + group0 group1 + [40, 100, 999, 10000] + 75 + """ + if has_last_odd_item: + pivot = collection[-1] + result = binary_search_insertion(result, pivot) + + """ + Insert the remaining items. + In this case, 40 < 75 is sure because it has already been sorted. + Therefore, you only need to insert 75 into [100, 999, 10000], + so that you can avoid unnecessary comparison. + + Example: + group0 group1 + [40, 100, 999, 10000] + ^ You don't need to compare with this as 40 < 75 is already sure. + 75 + -> + [40, 75, 100, 999, 10000] + """ + is_last_odd_item_inserted_before_this_index = False + for i in range(len(sorted_list_2d) - 1): + if result[i] == collection[-i]: + is_last_odd_item_inserted_before_this_index = True + pivot = sorted_list_2d[i][1] + # If last_odd_item is inserted before the item's index, + # you should forward index one more. + if is_last_odd_item_inserted_before_this_index: + result = result[: i + 2] + binary_search_insertion(result[i + 2 :], pivot) + else: + result = result[: i + 1] + binary_search_insertion(result[i + 1 :], pivot) + + return result + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(merge_insertion_sort(unsorted)) diff --git a/sorts/merge_sort.py b/sorts/merge_sort.py index ca4d319fa7f1..4da29f32a36d 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -1,72 +1,53 @@ """ -This is a pure python implementation of the merge sort algorithm - +This is a pure Python implementation of the merge sort algorithm For doctests run following command: python -m doctest -v merge_sort.py or python3 -m doctest -v merge_sort.py - For manual testing run: python merge_sort.py """ -from __future__ import print_function -def merge_sort(collection): +def merge_sort(collection: list) -> list: """Pure implementation of the merge sort algorithm in Python - :param collection: some mutable ordered collection with heterogeneous comparable items inside :return: the same collection ordered by ascending - Examples: >>> merge_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] - >>> merge_sort([]) [] - >>> merge_sort([-2, -5, -45]) [-45, -5, -2] """ - length = len(collection) - if length > 1: - midpoint = length // 2 - left_half = merge_sort(collection[:midpoint]) - right_half = merge_sort(collection[midpoint:]) - i = 0 - j = 0 - k = 0 - left_length = len(left_half) - right_length = len(right_half) - while i < left_length and j < right_length: - if left_half[i] < right_half[j]: - collection[k] = left_half[i] - i += 1 - else: - collection[k] = right_half[j] - j += 1 - k += 1 - while i < left_length: - collection[k] = left_half[i] - i += 1 - k += 1 + def merge(left: list, right: list) -> list: + """merge left and right + :param left: left collection + :param right: right collection + :return: merge result + """ + + def _merge(): + while left and right: + yield (left if left[0] <= right[0] else right).pop(0) + yield from left + yield from right - while j < right_length: - collection[k] = right_half[j] - j += 1 - k += 1 + return list(_merge()) - return collection + if len(collection) <= 1: + return collection + mid = len(collection) // 2 + return merge(merge_sort(collection[:mid]), merge_sort(collection[mid:])) -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 +if __name__ == "__main__": + import doctest - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] - print(merge_sort(unsorted)) + doctest.testmod() + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(*merge_sort(unsorted), sep=",") diff --git a/sorts/merge_sort_fastest.py b/sorts/merge_sort_fastest.py deleted file mode 100644 index 9fc9275aacba..000000000000 --- a/sorts/merge_sort_fastest.py +++ /dev/null @@ -1,19 +0,0 @@ -''' -Python implementation of merge sort algorithm. -Takes an average of 0.6 microseconds to sort a list of length 1000 items. -Best Case Scenario : O(n) -Worst Case Scenario : O(n) -''' -def merge_sort(LIST): - start = [] - end = [] - while len(LIST) > 1: - a = min(LIST) - b = max(LIST) - start.append(a) - end.append(b) - LIST.remove(a) - LIST.remove(b) - if LIST: start.append(LIST[0]) - end.reverse() - return (start + end) diff --git a/sorts/natural_sort.py b/sorts/natural_sort.py new file mode 100644 index 000000000000..001ff2cf5b41 --- /dev/null +++ b/sorts/natural_sort.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +import re + + +def natural_sort(input_list: list[str]) -> list[str]: + """ + Sort the given list of strings in the way that humans expect. + + The normal Python sort algorithm sorts lexicographically, + so you might not get the results that you expect... + + >>> example1 = ['2 ft 7 in', '1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '7 ft 6 in'] + >>> sorted(example1) + ['1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '2 ft 7 in', '7 ft 6 in'] + >>> # The natural sort algorithm sort based on meaning and not computer code point. + >>> natural_sort(example1) + ['1 ft 5 in', '2 ft 7 in', '2 ft 11 in', '7 ft 6 in', '10 ft 2 in'] + + >>> example2 = ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] + >>> sorted(example2) + ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] + >>> natural_sort(example2) + ['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13'] + """ + + def alphanum_key(key): + return [int(s) if s.isdigit() else s.lower() for s in re.split("([0-9]+)", key)] + + return sorted(input_list, key=alphanum_key) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/sorts/normal_distribution_quick_sort.md b/sorts/normal_distribution_quick_sort.md index 635262bfdf7d..2a9f77b3ee95 100644 --- a/sorts/normal_distribution_quick_sort.md +++ b/sorts/normal_distribution_quick_sort.md @@ -13,9 +13,9 @@ The array elements are taken from a Standard Normal Distribution , having mean = ```python ->>> import numpy as np +>>> import numpy as np >>> from tempfile import TemporaryFile ->>> outfile = TemporaryFile() +>>> outfile = TemporaryFile() >>> p = 100 # 100 elements are to be sorted >>> mu, sigma = 0, 1 # mean and standard deviation >>> X = np.random.normal(mu, sigma, p) @@ -34,7 +34,7 @@ The array elements are taken from a Standard Normal Distribution , having mean = >>> s = np.random.normal(mu, sigma, p) >>> count, bins, ignored = plt.hist(s, 30, normed=True) >>> plt.plot(bins , 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) ),linewidth=2, color='r') ->>> plt.show() +>>> plt.show() ``` @@ -52,15 +52,15 @@ The array elements are taken from a Standard Normal Distribution , having mean = -- -## Plotting the function for Checking 'The Number of Comparisons' taking place between Normal Distribution QuickSort and Ordinary QuickSort +## Plotting the function for Checking 'The Number of Comparisons' taking place between Normal Distribution QuickSort and Ordinary QuickSort ```python >>>import matplotlib.pyplot as plt - + # Normal Disrtibution QuickSort is red >>> plt.plot([1,2,4,16,32,64,128,256,512,1024,2048],[1,1,6,15,43,136,340,800,2156,6821,16325],linewidth=2, color='r') - + #Ordinary QuickSort is green >>> plt.plot([1,2,4,16,32,64,128,256,512,1024,2048],[1,1,4,16,67,122,362,949,2131,5086,12866],linewidth=2, color='g') @@ -73,4 +73,3 @@ The array elements are taken from a Standard Normal Distribution , having mean = ------------------ - diff --git a/sorts/odd_even_transposition_parallel.py b/sorts/odd_even_transposition_parallel.py new file mode 100644 index 000000000000..5de7a016c628 --- /dev/null +++ b/sorts/odd_even_transposition_parallel.py @@ -0,0 +1,144 @@ +""" +This is an implementation of odd-even transposition sort. + +It works by performing a series of parallel swaps between odd and even pairs of +variables in the list. + +This implementation represents each variable in the list with a process and +each process communicates with its neighboring processes in the list to perform +comparisons. +They are synchronized with locks and message passing but other forms of +synchronization could be used. +""" +from multiprocessing import Lock, Pipe, Process + +# lock used to ensure that two processes do not access a pipe at the same time +processLock = Lock() + +""" +The function run by the processes that sorts the list + +position = the position in the list the process represents, used to know which + neighbor we pass our value to +value = the initial value at list[position] +LSend, RSend = the pipes we use to send to our left and right neighbors +LRcv, RRcv = the pipes we use to receive from our left and right neighbors +resultPipe = the pipe used to send results back to main +""" + + +def oeProcess(position, value, LSend, RSend, LRcv, RRcv, resultPipe): + global processLock + + # we perform n swaps since after n swaps we know we are sorted + # we *could* stop early if we are sorted already, but it takes as long to + # find out we are sorted as it does to sort the list with this algorithm + for i in range(0, 10): + + if (i + position) % 2 == 0 and RSend is not None: + # send your value to your right neighbor + processLock.acquire() + RSend[1].send(value) + processLock.release() + + # receive your right neighbor's value + processLock.acquire() + temp = RRcv[0].recv() + processLock.release() + + # take the lower value since you are on the left + value = min(value, temp) + elif (i + position) % 2 != 0 and LSend is not None: + # send your value to your left neighbor + processLock.acquire() + LSend[1].send(value) + processLock.release() + + # receive your left neighbor's value + processLock.acquire() + temp = LRcv[0].recv() + processLock.release() + + # take the higher value since you are on the right + value = max(value, temp) + # after all swaps are performed, send the values back to main + resultPipe[1].send(value) + + +""" +the function which creates the processes that perform the parallel swaps + +arr = the list to be sorted +""" + + +def OddEvenTransposition(arr): + processArray = [] + resultPipe = [] + # initialize the list of pipes where the values will be retrieved + for _ in arr: + resultPipe.append(Pipe()) + # creates the processes + # the first and last process only have one neighbor so they are made outside + # of the loop + tempRs = Pipe() + tempRr = Pipe() + processArray.append( + Process( + target=oeProcess, + args=(0, arr[0], None, tempRs, None, tempRr, resultPipe[0]), + ) + ) + tempLr = tempRs + tempLs = tempRr + + for i in range(1, len(arr) - 1): + tempRs = Pipe() + tempRr = Pipe() + processArray.append( + Process( + target=oeProcess, + args=(i, arr[i], tempLs, tempRs, tempLr, tempRr, resultPipe[i]), + ) + ) + tempLr = tempRs + tempLs = tempRr + + processArray.append( + Process( + target=oeProcess, + args=( + len(arr) - 1, + arr[len(arr) - 1], + tempLs, + None, + tempLr, + None, + resultPipe[len(arr) - 1], + ), + ) + ) + + # start the processes + for p in processArray: + p.start() + + # wait for the processes to end and write their values to the list + for p in range(0, len(resultPipe)): + arr[p] = resultPipe[p][0].recv() + processArray[p].join() + return arr + + +# creates a reverse sorted list and sorts it +def main(): + arr = list(range(10, 0, -1)) + print("Initial List") + print(*arr) + arr = OddEvenTransposition(arr) + print("Sorted List\n") + print(*arr) + + +if __name__ == "__main__": + main() diff --git a/sorts/odd_even_transposition_single_threaded.py b/sorts/odd_even_transposition_single_threaded.py new file mode 100644 index 000000000000..fe06459e8dd1 --- /dev/null +++ b/sorts/odd_even_transposition_single_threaded.py @@ -0,0 +1,33 @@ +""" +Source: https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort + +This is a non-parallelized implementation of odd-even transpostiion sort. + +Normally the swaps in each set happen simultaneously, without that the algorithm +is no better than bubble sort. +""" + + +def odd_even_transposition(arr: list) -> list: + """ + >>> odd_even_transposition([5, 4, 3, 2, 1]) + [1, 2, 3, 4, 5] + + >>> odd_even_transposition([13, 11, 18, 0, -1]) + [-1, 0, 11, 13, 18] + + >>> odd_even_transposition([-.1, 1.1, .1, -2.9]) + [-2.9, -0.1, 0.1, 1.1] + """ + arr_size = len(arr) + for _ in range(arr_size): + for i in range(_ % 2, arr_size - 1, 2): + if arr[i + 1] < arr[i]: + arr[i], arr[i + 1] = arr[i + 1], arr[i] + + return arr + + +if __name__ == "__main__": + arr = list(range(10, 0, -1)) + print(f"Original: {arr}. Sorted: {odd_even_transposition(arr)}") diff --git a/sorts/pancake_sort.py b/sorts/pancake_sort.py index 26fd40b7f67c..e5d600738435 100644 --- a/sorts/pancake_sort.py +++ b/sorts/pancake_sort.py @@ -1,16 +1,39 @@ -# Pancake sort algorithm -# Only can reverse array from 0 to i +""" +This is a pure Python implementation of the pancake sort algorithm +For doctests run following command: +python3 -m doctest -v pancake_sort.py +or +python -m doctest -v pancake_sort.py +For manual testing run: +python pancake_sort.py +""" -def pancakesort(arr): + +def pancake_sort(arr): + """Sort Array with Pancake Sort. + :param arr: Collection containing comparable items + :return: Collection ordered in ascending order of items + Examples: + >>> pancake_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + >>> pancake_sort([]) + [] + >>> pancake_sort([-2, -5, -45]) + [-45, -5, -2] + """ cur = len(arr) while cur > 1: # Find the maximum number in arr mi = arr.index(max(arr[0:cur])) - # Reverse from 0 to mi - arr = arr[mi::-1] + arr[mi+1:len(arr)] - # Reverse whole list - arr = arr[cur-1::-1] + arr[cur:len(arr)] + # Reverse from 0 to mi + arr = arr[mi::-1] + arr[mi + 1 : len(arr)] + # Reverse whole list + arr = arr[cur - 1 :: -1] + arr[cur : len(arr)] cur -= 1 return arr -print(pancakesort([0,10,15,3,2,9,14,13])) + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(pancake_sort(unsorted)) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py new file mode 100644 index 000000000000..f4e35d9a0ac6 --- /dev/null +++ b/sorts/patience_sort.py @@ -0,0 +1,64 @@ +from bisect import bisect_left +from functools import total_ordering +from heapq import merge + +""" +A pure Python implementation of the patience sort algorithm + +For more information: https://en.wikipedia.org/wiki/Patience_sorting + +This algorithm is based on the card game patience + +For doctests run following command: +python3 -m doctest -v patience_sort.py + +For manual testing run: +python3 patience_sort.py +""" + + +@total_ordering +class Stack(list): + def __lt__(self, other): + return self[-1] < other[-1] + + def __eq__(self, other): + return self[-1] == other[-1] + + +def patience_sort(collection: list) -> list: + """A pure implementation of quick sort algorithm in Python + + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + Examples: + >>> patience_sort([1, 9, 5, 21, 17, 6]) + [1, 5, 6, 9, 17, 21] + + >>> patience_sort([]) + [] + + >>> patience_sort([-3, -17, -48]) + [-48, -17, -3] + """ + stacks = [] + # sort into stacks + for element in collection: + new_stacks = Stack([element]) + i = bisect_left(stacks, new_stacks) + if i != len(stacks): + stacks[i].append(element) + else: + stacks.append(new_stacks) + + # use a heap-based merge to merge stack efficiently + collection[:] = merge(*[reversed(stack) for stack in stacks]) + return collection + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(patience_sort(unsorted)) diff --git a/sorts/pigeon_sort.py b/sorts/pigeon_sort.py new file mode 100644 index 000000000000..3d81f0643865 --- /dev/null +++ b/sorts/pigeon_sort.py @@ -0,0 +1,60 @@ +""" + This is an implementation of Pigeon Hole Sort. + For doctests run following command: + + python3 -m doctest -v pigeon_sort.py + or + python -m doctest -v pigeon_sort.py + + For manual testing run: + python pigeon_sort.py +""" +from typing import List + + +def pigeon_sort(array: List[int]) -> List[int]: + """ + Implementation of pigeon hole sort algorithm + :param array: Collection of comparable items + :return: Collection sorted in ascending order + >>> pigeon_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + >>> pigeon_sort([]) + [] + >>> pigeon_sort([-2, -5, -45]) + [-45, -5, -2] + """ + if len(array) == 0: + return array + + _min, _max = min(array), max(array) + + # Compute the variables + holes_range = _max - _min + 1 + holes, holes_repeat = [0] * holes_range, [0] * holes_range + + # Make the sorting. + for i in array: + index = i - _min + holes[index] = i + holes_repeat[index] += 1 + + # Makes the array back by replacing the numbers. + index = 0 + for i in range(holes_range): + while holes_repeat[i] > 0: + array[index] = holes[i] + index += 1 + holes_repeat[i] -= 1 + + # Returns the sorted array. + return array + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + user_input = input("Enter numbers separated by comma:\n") + unsorted = [int(x) for x in user_input.split(",")] + print(pigeon_sort(unsorted)) diff --git a/sorts/pigeonhole_sort.py b/sorts/pigeonhole_sort.py new file mode 100644 index 000000000000..bfa9bb11b8a6 --- /dev/null +++ b/sorts/pigeonhole_sort.py @@ -0,0 +1,45 @@ +# Python program to implement Pigeonhole Sorting in python + +# Algorithm for the pigeonhole sorting + + +def pigeonhole_sort(a): + """ + >>> a = [8, 3, 2, 7, 4, 6, 8] + >>> b = sorted(a) # a nondestructive sort + >>> pigeonhole_sort(a) # a destructive sort + >>> a == b + True + """ + # size of range of values in the list (ie, number of pigeonholes we need) + + min_val = min(a) # min() finds the minimum value + max_val = max(a) # max() finds the maximum value + + size = max_val - min_val + 1 # size is difference of max and min values plus one + + # list of pigeonholes of size equal to the variable size + holes = [0] * size + + # Populate the pigeonholes. + for x in a: + assert isinstance(x, int), "integers only please" + holes[x - min_val] += 1 + + # Putting the elements back into the array in an order. + i = 0 + for count in range(size): + while holes[count] > 0: + holes[count] -= 1 + a[i] = count + min_val + i += 1 + + +def main(): + a = [8, 3, 2, 7, 4, 6, 8] + pigeonhole_sort(a) + print("Sorted order is:", " ".join(a)) + + +if __name__ == "__main__": + main() diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index e01d319a4b29..c6687a7fa8d5 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -1,50 +1,39 @@ """ -This is a pure python implementation of the quick sort algorithm +A pure Python implementation of the quick sort algorithm For doctests run following command: -python -m doctest -v quick_sort.py -or python3 -m doctest -v quick_sort.py For manual testing run: -python quick_sort.py +python3 quick_sort.py """ -from __future__ import print_function -def quick_sort(collection): - """Pure implementation of quick sort algorithm in Python +def quick_sort(collection: list) -> list: + """A pure Python implementation of quick sort algorithm - :param collection: some mutable ordered collection with heterogeneous - comparable items inside + :param collection: a mutable collection of comparable items :return: the same collection ordered by ascending Examples: >>> quick_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] - >>> quick_sort([]) [] - - >>> quick_sort([-2, -5, -45]) - [-45, -5, -2] + >>> quick_sort([-2, 5, 0, -45]) + [-45, -2, 0, 5] """ - length = len(collection) - if length <= 1: + if len(collection) < 2: return collection - else: - pivot = collection[0] - greater = [element for element in collection[1:] if element > pivot] - lesser = [element for element in collection[1:] if element <= pivot] - return quick_sort(lesser) + [pivot] + quick_sort(greater) - - -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [ int(item) for item in user_input.split(',') ] - print( quick_sort(unsorted) ) + pivot = collection.pop() # Use the last element as the first pivot + greater = [] # All elements greater than pivot + lesser = [] # All elements less than or equal to pivot + for element in collection: + (greater if element > pivot else lesser).append(element) + return quick_sort(lesser) + [pivot] + quick_sort(greater) + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(quick_sort(unsorted)) diff --git a/sorts/quick_sort_3_partition.py b/sorts/quick_sort_3_partition.py index def646cdbc50..18c6e0f876d2 100644 --- a/sorts/quick_sort_3_partition.py +++ b/sorts/quick_sort_3_partition.py @@ -1,6 +1,4 @@ -from __future__ import print_function - -def quick_sort_3partition(sorting, left, right): +def quick_sort_3partition(sorting: list, left: int, right: int) -> None: if right <= left: return a = i = left @@ -19,13 +17,38 @@ def quick_sort_3partition(sorting, left, right): quick_sort_3partition(sorting, left, a - 1) quick_sort_3partition(sorting, b + 1, right) -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [ int(item) for item in user_input.split(',') ] - quick_sort_3partition(unsorted,0,len(unsorted)-1) +def three_way_radix_quicksort(sorting: list) -> list: + """ + Three-way radix quicksort: + https://en.wikipedia.org/wiki/Quicksort#Three-way_radix_quicksort + First divide the list into three parts. + Then recursively sort the "less than" and "greater than" partitions. + + >>> three_way_radix_quicksort([]) + [] + >>> three_way_radix_quicksort([1]) + [1] + >>> three_way_radix_quicksort([-5, -2, 1, -2, 0, 1]) + [-5, -2, -2, 0, 1, 1] + >>> three_way_radix_quicksort([1, 2, 5, 1, 2, 0, 0, 5, 2, -1]) + [-1, 0, 0, 1, 1, 2, 2, 2, 5, 5] + """ + if len(sorting) <= 1: + return sorting + return ( + three_way_radix_quicksort([i for i in sorting if i < sorting[0]]) + + [i for i in sorting if i == sorting[0]] + + three_way_radix_quicksort([i for i in sorting if i > sorting[0]]) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) + + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + quick_sort_3partition(unsorted, 0, len(unsorted) - 1) print(unsorted) diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index e4cee61f35e3..57dbbaa79076 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -1,26 +1,52 @@ -def radixsort(lst): - RADIX = 10 - placement = 1 +""" +This is a pure Python implementation of the quick sort algorithm +For doctests run following command: +python -m doctest -v radix_sort.py +or +python3 -m doctest -v radix_sort.py +For manual testing run: +python radix_sort.py +""" +from __future__ import annotations + +from typing import List - # get the maximum number - max_digit = max(lst) - while placement < max_digit: - # declare and initialize buckets - buckets = [list() for _ in range( RADIX )] +def radix_sort(list_of_ints: List[int]) -> List[int]: + """ + Examples: + >>> radix_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + + >>> radix_sort(list(range(15))) == sorted(range(15)) + True + >>> radix_sort(list(range(14,-1,-1))) == sorted(range(15)) + True + >>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000]) + True + """ + RADIX = 10 + placement = 1 + max_digit = max(list_of_ints) + while placement <= max_digit: + # declare and initialize empty buckets + buckets = [list() for _ in range(RADIX)] + # split list_of_ints between the buckets + for i in list_of_ints: + tmp = int((i / placement) % RADIX) + buckets[tmp].append(i) + # put each buckets' contents into list_of_ints + a = 0 + for b in range(RADIX): + for i in buckets[b]: + list_of_ints[a] = i + a += 1 + # move to next + placement *= RADIX + return list_of_ints - # split lst between lists - for i in lst: - tmp = int((i / placement) % RADIX) - buckets[tmp].append(i) - # empty lists into lst array - a = 0 - for b in range( RADIX ): - buck = buckets[b] - for i in buck: - lst[a] = i - a += 1 +if __name__ == "__main__": + import doctest - # move to next - placement *= RADIX + doctest.testmod() diff --git a/sorts/random_normal_distribution_quicksort.py b/sorts/random_normal_distribution_quicksort.py index dfa37da61e26..73eb70bea07f 100644 --- a/sorts/random_normal_distribution_quicksort.py +++ b/sorts/random_normal_distribution_quicksort.py @@ -1,66 +1,64 @@ -from __future__ import print_function from random import randint from tempfile import TemporaryFile -import numpy as np +import numpy as np -def _inPlaceQuickSort(A,start,end): +def _inPlaceQuickSort(A, start, end): count = 0 - if start list: + """ + It is similar is bubble sort but recursive. + :param list_data: mutable ordered sequence of elements + :param length: length of list data + :return: the same list in ascending order + + >>> bubble_sort([0, 5, 2, 3, 2], 5) + [0, 2, 2, 3, 5] + + >>> bubble_sort([], 0) + [] + + >>> bubble_sort([-2, -45, -5], 3) + [-45, -5, -2] + + >>> bubble_sort([-23, 0, 6, -4, 34], 5) + [-23, -4, 0, 6, 34] + + >>> bubble_sort([-23, 0, 6, -4, 34], 5) == sorted([-23, 0, 6, -4, 34]) + True + + >>> bubble_sort(['z','a','y','b','x','c'], 6) + ['a', 'b', 'c', 'x', 'y', 'z'] + + >>> bubble_sort([1.1, 3.3, 5.5, 7.7, 2.2, 4.4, 6.6]) + [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7] + """ + length = length or len(list_data) + swapped = False + for i in range(length - 1): + if list_data[i] > list_data[i + 1]: + list_data[i], list_data[i + 1] = list_data[i + 1], list_data[i] + swapped = True + + return list_data if not swapped else bubble_sort(list_data, length - 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/sorts/recursive_insertion_sort.py b/sorts/recursive_insertion_sort.py new file mode 100644 index 000000000000..66dd08157df1 --- /dev/null +++ b/sorts/recursive_insertion_sort.py @@ -0,0 +1,75 @@ +""" +A recursive implementation of the insertion sort algorithm +""" + +from __future__ import annotations + + +def rec_insertion_sort(collection: list, n: int): + """ + Given a collection of numbers and its length, sorts the collections + in ascending order + + :param collection: A mutable collection of comparable elements + :param n: The length of collections + + >>> col = [1, 2, 1] + >>> rec_insertion_sort(col, len(col)) + >>> print(col) + [1, 1, 2] + + >>> col = [2, 1, 0, -1, -2] + >>> rec_insertion_sort(col, len(col)) + >>> print(col) + [-2, -1, 0, 1, 2] + + >>> col = [1] + >>> rec_insertion_sort(col, len(col)) + >>> print(col) + [1] + """ + # Checks if the entire collection has been sorted + if len(collection) <= 1 or n <= 1: + return + + insert_next(collection, n - 1) + rec_insertion_sort(collection, n - 1) + + +def insert_next(collection: list, index: int): + """ + Inserts the '(index-1)th' element into place + + >>> col = [3, 2, 4, 2] + >>> insert_next(col, 1) + >>> print(col) + [2, 3, 4, 2] + + >>> col = [3, 2, 3] + >>> insert_next(col, 2) + >>> print(col) + [3, 2, 3] + + >>> col = [] + >>> insert_next(col, 1) + >>> print(col) + [] + """ + # Checks order between adjacent elements + if index >= len(collection) or collection[index - 1] <= collection[index]: + return + + # Swaps adjacent elements since they are not in ascending order + collection[index - 1], collection[index] = ( + collection[index], + collection[index - 1], + ) + + insert_next(collection, index + 1) + + +if __name__ == "__main__": + numbers = input("Enter integers separated by spaces: ") + numbers = [int(num) for num in numbers.split()] + rec_insertion_sort(numbers, len(numbers)) + print(numbers) diff --git a/sorts/recursive_quick_sort.py b/sorts/recursive_quick_sort.py new file mode 100644 index 000000000000..c28a14e37ebd --- /dev/null +++ b/sorts/recursive_quick_sort.py @@ -0,0 +1,22 @@ +def quick_sort(data: list) -> list: + """ + >>> for data in ([2, 1, 0], [2.2, 1.1, 0], "quick_sort"): + ... quick_sort(data) == sorted(data) + True + True + True + """ + if len(data) <= 1: + return data + else: + return ( + quick_sort([e for e in data[1:] if e <= data[0]]) + + [data[0]] + + quick_sort([e for e in data[1:] if e > data[0]]) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/sorts/selection_sort.py b/sorts/selection_sort.py index c1d66c5a8b38..f3beb31b7070 100644 --- a/sorts/selection_sort.py +++ b/sorts/selection_sort.py @@ -1,5 +1,5 @@ """ -This is a pure python implementation of the selection sort algorithm +This is a pure Python implementation of the selection sort algorithm For doctests run following command: python -m doctest -v selection_sort.py @@ -9,7 +9,6 @@ For manual testing run: python selection_sort.py """ -from __future__ import print_function def selection_sort(collection): @@ -36,18 +35,12 @@ def selection_sort(collection): for k in range(i + 1, length): if collection[k] < collection[least]: least = k - collection[least], collection[i] = ( - collection[i], collection[least] - ) + if least != i: + collection[least], collection[i] = (collection[i], collection[least]) return collection -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(selection_sort(unsorted)) diff --git a/sorts/shell_sort.py b/sorts/shell_sort.py index dc1846758243..2e749e43d056 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -1,15 +1,6 @@ """ -This is a pure python implementation of the shell sort algorithm - -For doctests run following command: -python -m doctest -v shell_sort.py -or -python3 -m doctest -v shell_sort.py - -For manual testing run: -python shell_sort.py +https://en.wikipedia.org/wiki/Shellsort#Pseudocode """ -from __future__ import print_function def shell_sort(collection): @@ -20,35 +11,29 @@ def shell_sort(collection): >>> shell_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] - >>> shell_sort([]) [] - >>> shell_sort([-2, -5, -45]) [-45, -5, -2] """ # Marcin Ciura's gap sequence - gaps = [701, 301, 132, 57, 23, 10, 4, 1] + gaps = [701, 301, 132, 57, 23, 10, 4, 1] for gap in gaps: - i = gap - while i < len(collection): - temp = collection[i] + for i in range(gap, len(collection)): + insert_value = collection[i] j = i - while j >= gap and collection[j - gap] > temp: + while j >= gap and collection[j - gap] > insert_value: collection[j] = collection[j - gap] j -= gap - collection[j] = temp - i += 1 - + collection[j] = insert_value return collection -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] +if __name__ == "__main__": + from doctest import testmod + + testmod() + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] print(shell_sort(unsorted)) diff --git a/sorts/sorting_graphs.png b/sorts/sorting_graphs.png deleted file mode 100644 index 628245f3eb39..000000000000 Binary files a/sorts/sorting_graphs.png and /dev/null differ diff --git a/sorts/stooge_sort.py b/sorts/stooge_sort.py new file mode 100644 index 000000000000..de997a85df12 --- /dev/null +++ b/sorts/stooge_sort.py @@ -0,0 +1,40 @@ +def stooge_sort(arr): + """ + Examples: + >>> stooge_sort([18.1, 0, -7.1, -1, 2, 2]) + [-7.1, -1, 0, 2, 2, 18.1] + + >>> stooge_sort([]) + [] + """ + stooge(arr, 0, len(arr) - 1) + return arr + + +def stooge(arr, i, h): + + if i >= h: + return + + # If first element is smaller than the last then swap them + if arr[i] > arr[h]: + arr[i], arr[h] = arr[h], arr[i] + + # If there are more than 2 elements in the array + if h - i + 1 > 2: + t = (int)((h - i + 1) / 3) + + # Recursively sort first 2/3 elements + stooge(arr, i, (h - t)) + + # Recursively sort last 2/3 elements + stooge(arr, i + t, (h)) + + # Recursively sort first 2/3 elements + stooge(arr, i, (h - t)) + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(stooge_sort(unsorted)) diff --git a/sorts/strand_sort.py b/sorts/strand_sort.py new file mode 100644 index 000000000000..a89135a0691f --- /dev/null +++ b/sorts/strand_sort.py @@ -0,0 +1,51 @@ +import operator + + +def strand_sort(arr: list, reverse: bool = False, solution: list = None) -> list: + """ + Strand sort implementation + source: https://en.wikipedia.org/wiki/Strand_sort + + :param arr: Unordered input list + :param reverse: Descent ordering flag + :param solution: Ordered items container + + Examples: + >>> strand_sort([4, 2, 5, 3, 0, 1]) + [0, 1, 2, 3, 4, 5] + + >>> strand_sort([4, 2, 5, 3, 0, 1], reverse=True) + [5, 4, 3, 2, 1, 0] + """ + _operator = operator.lt if reverse else operator.gt + solution = solution or [] + + if not arr: + return solution + + sublist = [arr.pop(0)] + for i, item in enumerate(arr): + if _operator(item, sublist[-1]): + sublist.append(item) + arr.pop(i) + + # merging sublist into solution list + if not solution: + solution.extend(sublist) + else: + while sublist: + item = sublist.pop(0) + for i, xx in enumerate(solution): + if not _operator(item, xx): + solution.insert(i, item) + break + else: + solution.append(item) + + strand_sort(arr, reverse, solution) + return solution + + +if __name__ == "__main__": + assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5] + assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1] diff --git a/sorts/timsort.py b/sorts/tim_sort.py similarity index 62% rename from sorts/timsort.py rename to sorts/tim_sort.py index 80c5cd1e8d3f..b95ff34cf384 100644 --- a/sorts/timsort.py +++ b/sorts/tim_sort.py @@ -1,10 +1,6 @@ -from __future__ import print_function def binary_search(lst, item, start, end): if start == end: - if lst[start] > item: - return start - else: - return start + 1 + return start if lst[start] > item else start + 1 if start > end: return start @@ -23,7 +19,7 @@ def insertion_sort(lst): for index in range(1, length): value = lst[index] pos = binary_search(lst, value, 0, index - 1) - lst = lst[:pos] + [value] + lst[pos:index] + lst[index+1:] + lst = lst[:pos] + [value] + lst[pos:index] + lst[index + 1 :] return lst @@ -41,31 +37,35 @@ def merge(left, right): return [right[0]] + merge(left, right[1:]) -def timsort(lst): - runs, sorted_runs = [], [] +def tim_sort(lst): + """ + >>> tim_sort("Python") + ['P', 'h', 'n', 'o', 't', 'y'] + >>> tim_sort((1.1, 1, 0, -1, -1.1)) + [-1.1, -1, 0, 1, 1.1] + >>> tim_sort(list(reversed(list(range(7))))) + [0, 1, 2, 3, 4, 5, 6] + >>> tim_sort([3, 2, 1]) == insertion_sort([3, 2, 1]) + True + >>> tim_sort([3, 2, 1]) == sorted([3, 2, 1]) + True + """ length = len(lst) + runs, sorted_runs = [], [] new_run = [lst[0]] sorted_array = [] - - for i in range(1, length): - if i == length - 1: - new_run.append(lst[i]) - runs.append(new_run) - break - + i = 1 + while i < length: if lst[i] < lst[i - 1]: - if not new_run: - runs.append([lst[i - 1]]) - new_run.append(lst[i]) - else: - runs.append(new_run) - new_run = [] + runs.append(new_run) + new_run = [lst[i]] else: new_run.append(lst[i]) + i += 1 + runs.append(new_run) for run in runs: sorted_runs.append(insertion_sort(run)) - for run in sorted_runs: sorted_array = merge(sorted_array, run) @@ -74,9 +74,10 @@ def timsort(lst): def main(): - lst = [5,9,10,3,-4,5,178,92,46,-18,0,7] - sorted_lst = timsort(lst) + lst = [5, 9, 10, 3, -4, 5, 178, 92, 46, -18, 0, 7] + sorted_lst = tim_sort(lst) print(sorted_lst) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index 52dc34f4f733..e7a52f7c7714 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -1,11 +1,12 @@ -from __future__ import print_function +"""Topological Sort.""" + # a # / \ # b c # / \ # d e -edges = {'a': ['c', 'b'], 'b': ['d', 'e'], 'c': [], 'd': [], 'e': []} -vertices = ['a', 'b', 'c', 'd', 'e'] +edges = {"a": ["c", "b"], "b": ["d", "e"], "c": [], "d": [], "e": []} +vertices = ["a", "b", "c", "d", "e"] def topological_sort(start, visited, sort): @@ -29,5 +30,6 @@ def topological_sort(start, visited, sort): return sort -sort = topological_sort('a', [], []) -print(sort) +if __name__ == "__main__": + sort = topological_sort("a", [], []) + print(sort) diff --git a/sorts/tree_sort.py b/sorts/tree_sort.py index f8ecf84c6ff8..e445fb4520aa 100644 --- a/sorts/tree_sort.py +++ b/sorts/tree_sort.py @@ -1,14 +1,18 @@ -# Tree_sort algorithm -# Build a BST and in order traverse. +""" +Tree_sort algorithm. -class node(): +Build a BST and in order traverse. +""" + + +class node: # BST data structure def __init__(self, val): self.val = val - self.left = None - self.right = None - - def insert(self,val): + self.left = None + self.right = None + + def insert(self, val): if self.val: if val < self.val: if self.left is None: @@ -23,23 +27,27 @@ def insert(self,val): else: self.val = val + def inorder(root, res): - # Recursive travesal + # Recursive traversal if root: - inorder(root.left,res) + inorder(root.left, res) res.append(root.val) - inorder(root.right,res) + inorder(root.right, res) + -def treesort(arr): +def tree_sort(arr): # Build BST if len(arr) == 0: return arr root = node(arr[0]) - for i in range(1,len(arr)): + for i in range(1, len(arr)): root.insert(arr[i]) - # Traverse BST in order. + # Traverse BST in order. res = [] - inorder(root,res) + inorder(root, res) return res -print(treesort([10,1,3,2,9,14,13])) \ No newline at end of file + +if __name__ == "__main__": + print(tree_sort([10, 1, 3, 2, 9, 14, 13])) diff --git a/sorts/unknown_sort.py b/sorts/unknown_sort.py new file mode 100644 index 000000000000..9fa9d22fb5e0 --- /dev/null +++ b/sorts/unknown_sort.py @@ -0,0 +1,40 @@ +""" +Python implementation of a sort algorithm. +Best Case Scenario : O(n) +Worst Case Scenario : O(n^2) because native Python functions:min, max and remove are +already O(n) +""" + + +def merge_sort(collection): + """Pure implementation of the fastest merge sort algorithm in Python + + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: a collection ordered by ascending + + Examples: + >>> merge_sort([0, 5, 3, 2, 2]) + [0, 2, 2, 3, 5] + + >>> merge_sort([]) + [] + + >>> merge_sort([-2, -5, -45]) + [-45, -5, -2] + """ + start, end = [], [] + while len(collection) > 1: + min_one, max_one = min(collection), max(collection) + start.append(min_one) + end.append(max_one) + collection.remove(min_one) + collection.remove(max_one) + end.reverse() + return start + collection + end + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(*merge_sort(unsorted), sep=",") diff --git a/sorts/wiggle_sort.py b/sorts/wiggle_sort.py index cc83487bdeb1..13bc3ce9606f 100644 --- a/sorts/wiggle_sort.py +++ b/sorts/wiggle_sort.py @@ -1,21 +1,38 @@ """ -Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... +Wiggle Sort. + +Given an unsorted array nums, reorder it such +that nums[0] < nums[1] > nums[2] < nums[3].... For example: -if input numbers = [3, 5, 2, 1, 6, 4] +if input numbers = [3, 5, 2, 1, 6, 4] one possible Wiggle Sorted answer is [3, 5, 1, 6, 2, 4]. """ -def wiggle_sort(nums): - for i in range(len(nums)): - if (i % 2 == 1) == (nums[i-1] > nums[i]): - nums[i-1], nums[i] = nums[i], nums[i-1] -print("Enter the array elements:\n") -array=list(map(int,input().split())) -print("The unsorted array is:\n") -print(array) -wiggle_sort(array) -print("Array after Wiggle sort:\n") -print(array) +def wiggle_sort(nums: list) -> list: + """ + Python implementation of wiggle. + Example: + >>> wiggle_sort([0, 5, 3, 2, 2]) + [0, 5, 2, 3, 2] + >>> wiggle_sort([]) + [] + >>> wiggle_sort([-2, -5, -45]) + [-45, -2, -5] + >>> wiggle_sort([-2.1, -5.68, -45.11]) + [-45.11, -2.1, -5.68] + """ + for i, _ in enumerate(nums): + if (i % 2 == 1) == (nums[i - 1] > nums[i]): + nums[i - 1], nums[i] = nums[i], nums[i - 1] + + return nums +if __name__ == "__main__": + print("Enter the array elements:") + array = list(map(int, input().split())) + print("The unsorted array is:") + print(array) + print("Array after Wiggle sort:") + print(wiggle_sort(array)) diff --git a/strings/__init__.py b/strings/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/strings/aho_corasick.py b/strings/aho_corasick.py new file mode 100644 index 000000000000..b959dbd58c32 --- /dev/null +++ b/strings/aho_corasick.py @@ -0,0 +1,93 @@ +from collections import deque +from typing import Dict, List, Union + + +class Automaton: + def __init__(self, keywords: List[str]): + self.adlist = list() + self.adlist.append( + {"value": "", "next_states": [], "fail_state": 0, "output": []} + ) + + for keyword in keywords: + self.add_keyword(keyword) + self.set_fail_transitions() + + def find_next_state(self, current_state: int, char: str) -> Union[int, None]: + for state in self.adlist[current_state]["next_states"]: + if char == self.adlist[state]["value"]: + return state + return None + + def add_keyword(self, keyword: str) -> None: + current_state = 0 + for character in keyword: + if self.find_next_state(current_state, character): + current_state = self.find_next_state(current_state, character) + else: + self.adlist.append( + { + "value": character, + "next_states": [], + "fail_state": 0, + "output": [], + } + ) + self.adlist[current_state]["next_states"].append(len(self.adlist) - 1) + current_state = len(self.adlist) - 1 + self.adlist[current_state]["output"].append(keyword) + + def set_fail_transitions(self) -> None: + q = deque() + for node in self.adlist[0]["next_states"]: + q.append(node) + self.adlist[node]["fail_state"] = 0 + while q: + r = q.popleft() + for child in self.adlist[r]["next_states"]: + q.append(child) + state = self.adlist[r]["fail_state"] + while ( + self.find_next_state(state, self.adlist[child]["value"]) is None + and state != 0 + ): + state = self.adlist[state]["fail_state"] + self.adlist[child]["fail_state"] = self.find_next_state( + state, self.adlist[child]["value"] + ) + if self.adlist[child]["fail_state"] is None: + self.adlist[child]["fail_state"] = 0 + self.adlist[child]["output"] = ( + self.adlist[child]["output"] + + self.adlist[self.adlist[child]["fail_state"]]["output"] + ) + + def search_in(self, string: str) -> Dict[str, List[int]]: + """ + >>> A = Automaton(["what", "hat", "ver", "er"]) + >>> A.search_in("whatever, err ... , wherever") + {'what': [0], 'hat': [1], 'ver': [5, 25], 'er': [6, 10, 22, 26]} + """ + result = dict() # returns a dict with keywords and list of its occurrences + current_state = 0 + for i in range(len(string)): + while ( + self.find_next_state(current_state, string[i]) is None + and current_state != 0 + ): + current_state = self.adlist[current_state]["fail_state"] + current_state = self.find_next_state(current_state, string[i]) + if current_state is None: + current_state = 0 + else: + for key in self.adlist[current_state]["output"]: + if not (key in result): + result[key] = [] + result[key].append(i - len(key) + 1) + return result + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/strings/boyer_moore_search.py b/strings/boyer_moore_search.py new file mode 100644 index 000000000000..a3e6cf614eab --- /dev/null +++ b/strings/boyer_moore_search.py @@ -0,0 +1,86 @@ +""" +The algorithm finds the pattern in given text using following rule. + +The bad-character rule considers the mismatched character in Text. +The next occurrence of that character to the left in Pattern is found, + +If the mismatched character occurs to the left in Pattern, +a shift is proposed that aligns text block and pattern. + +If the mismatched character does not occur to the left in Pattern, +a shift is proposed that moves the entirety of Pattern past +the point of mismatch in the text. + +If there no mismatch then the pattern matches with text block. + +Time Complexity : O(n/m) + n=length of main string + m=length of pattern string +""" +from typing import List + + +class BoyerMooreSearch: + def __init__(self, text: str, pattern: str): + self.text, self.pattern = text, pattern + self.textLen, self.patLen = len(text), len(pattern) + + def match_in_pattern(self, char: str) -> int: + """finds the index of char in pattern in reverse order + + Parameters : + char (chr): character to be searched + + Returns : + i (int): index of char from last in pattern + -1 (int): if char is not found in pattern + """ + + for i in range(self.patLen - 1, -1, -1): + if char == self.pattern[i]: + return i + return -1 + + def mismatch_in_text(self, currentPos: int) -> int: + """ + find the index of mis-matched character in text when compared with pattern + from last + + Parameters : + currentPos (int): current index position of text + + Returns : + i (int): index of mismatched char from last in text + -1 (int): if there is no mismatch between pattern and text block + """ + + for i in range(self.patLen - 1, -1, -1): + if self.pattern[i] != self.text[currentPos + i]: + return currentPos + i + return -1 + + def bad_character_heuristic(self) -> List[int]: + # searches pattern in text and returns index positions + positions = [] + for i in range(self.textLen - self.patLen + 1): + mismatch_index = self.mismatch_in_text(i) + if mismatch_index == -1: + positions.append(i) + else: + match_index = self.match_in_pattern(self.text[mismatch_index]) + i = ( + mismatch_index - match_index + ) # shifting index lgtm [py/multiple-definition] + return positions + + +text = "ABAABA" +pattern = "AB" +bms = BoyerMooreSearch(text, pattern) +positions = bms.bad_character_heuristic() + +if len(positions) == 0: + print("No match found") +else: + print("Pattern found in following positions: ") + print(positions) diff --git a/strings/can_string_be_rearranged_as_palindrome.py b/strings/can_string_be_rearranged_as_palindrome.py new file mode 100644 index 000000000000..7fedc5877e26 --- /dev/null +++ b/strings/can_string_be_rearranged_as_palindrome.py @@ -0,0 +1,115 @@ +# Created by susmith98 + +from collections import Counter +from timeit import timeit + +# Problem Description: +# Check if characters of the given string can be rearranged to form a palindrome. +# Counter is faster for long strings and non-Counter is faster for short strings. + + +def can_string_be_rearranged_as_palindrome_counter( + input_str: str = "", +) -> bool: + """ + A Palindrome is a String that reads the same forward as it does backwards. + Examples of Palindromes mom, dad, malayalam + >>> can_string_be_rearranged_as_palindrome_counter("Momo") + True + >>> can_string_be_rearranged_as_palindrome_counter("Mother") + False + >>> can_string_be_rearranged_as_palindrome_counter("Father") + False + >>> can_string_be_rearranged_as_palindrome_counter("A man a plan a canal Panama") + True + """ + return sum(c % 2 for c in Counter(input_str.replace(" ", "").lower()).values()) < 2 + + +def can_string_be_rearranged_as_palindrome(input_str: str = "") -> bool: + """ + A Palindrome is a String that reads the same forward as it does backwards. + Examples of Palindromes mom, dad, malayalam + >>> can_string_be_rearranged_as_palindrome("Momo") + True + >>> can_string_be_rearranged_as_palindrome("Mother") + False + >>> can_string_be_rearranged_as_palindrome("Father") + False + >>> can_string_be_rearranged_as_palindrome_counter("A man a plan a canal Panama") + True + """ + if len(input_str) == 0: + return True + lower_case_input_str = input_str.replace(" ", "").lower() + # character_freq_dict: Stores the frequency of every character in the input string + character_freq_dict = {} + + for character in lower_case_input_str: + character_freq_dict[character] = character_freq_dict.get(character, 0) + 1 + """ + Above line of code is equivalent to: + 1) Getting the frequency of current character till previous index + >>> character_freq = character_freq_dict.get(character, 0) + 2) Incrementing the frequency of current character by 1 + >>> character_freq = character_freq + 1 + 3) Updating the frequency of current character + >>> character_freq_dict[character] = character_freq + """ + """ + OBSERVATIONS: + Even length palindrome + -> Every character appears even no.of times. + Odd length palindrome + -> Every character appears even no.of times except for one character. + LOGIC: + Step 1: We'll count number of characters that appear odd number of times i.e oddChar + Step 2:If we find more than 1 character that appears odd number of times, + It is not possible to rearrange as a palindrome + """ + oddChar = 0 + + for character_count in character_freq_dict.values(): + if character_count % 2: + oddChar += 1 + if oddChar > 1: + return False + return True + + +def benchmark(input_str: str = "") -> None: + """ + Benchmark code for comparing above 2 functions + """ + print("\nFor string = ", input_str, ":") + print( + "> can_string_be_rearranged_as_palindrome_counter()", + "\tans =", + can_string_be_rearranged_as_palindrome_counter(input_str), + "\ttime =", + timeit( + "z.can_string_be_rearranged_as_palindrome_counter(z.check_str)", + setup="import __main__ as z", + ), + "seconds", + ) + print( + "> can_string_be_rearranged_as_palindrome()", + "\tans =", + can_string_be_rearranged_as_palindrome(input_str), + "\ttime =", + timeit( + "z.can_string_be_rearranged_as_palindrome(z.check_str)", + setup="import __main__ as z", + ), + "seconds", + ) + + +if __name__ == "__main__": + check_str = input( + "Enter string to determine if it can be rearranged as a palindrome or not: " + ).strip() + benchmark(check_str) + status = can_string_be_rearranged_as_palindrome_counter(check_str) + print(f"{check_str} can {'' if status else 'not '}be rearranged as a palindrome") diff --git a/strings/capitalize.py b/strings/capitalize.py new file mode 100644 index 000000000000..63603aa07e2d --- /dev/null +++ b/strings/capitalize.py @@ -0,0 +1,27 @@ +from string import ascii_lowercase, ascii_uppercase + + +def capitalize(sentence: str) -> str: + """ + This function will capitalize the first letter of a sentence or a word + >>> capitalize("hello world") + 'Hello world' + >>> capitalize("123 hello world") + '123 hello world' + >>> capitalize(" hello world") + ' hello world' + >>> capitalize("a") + 'A' + >>> capitalize("") + '' + """ + if not sentence: + return "" + lower_to_upper = {lc: uc for lc, uc in zip(ascii_lowercase, ascii_uppercase)} + return lower_to_upper.get(sentence[0], sentence[0]) + sentence[1:] + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/strings/check_anagrams.py b/strings/check_anagrams.py new file mode 100644 index 000000000000..3083000cbb5d --- /dev/null +++ b/strings/check_anagrams.py @@ -0,0 +1,33 @@ +""" +wiki: https://en.wikipedia.org/wiki/Anagram +""" + + +def check_anagrams(first_str: str, second_str: str) -> bool: + """ + Two strings are anagrams if they are made of the same letters + arranged differently (ignoring the case). + >>> check_anagrams('Silent', 'Listen') + True + >>> check_anagrams('This is a string', 'Is this a string') + True + >>> check_anagrams('This is a string', 'Is this a string') + True + >>> check_anagrams('There', 'Their') + False + """ + return ( + "".join(sorted(first_str.lower())).strip() + == "".join(sorted(second_str.lower())).strip() + ) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() + input_A = input("Enter the first string ").strip() + input_B = input("Enter the second string ").strip() + + status = check_anagrams(input_A, input_B) + print(f"{input_A} and {input_B} are {'' if status else 'not '}anagrams.") diff --git a/strings/check_pangram.py b/strings/check_pangram.py new file mode 100644 index 000000000000..e3695b918524 --- /dev/null +++ b/strings/check_pangram.py @@ -0,0 +1,72 @@ +""" +wiki: https://en.wikipedia.org/wiki/Pangram +""" + + +def check_pangram( + input_str: str = "The quick brown fox jumps over the lazy dog", +) -> bool: + """ + A Pangram String contains all the alphabets at least once. + >>> check_pangram("The quick brown fox jumps over the lazy dog") + True + >>> check_pangram("Waltz, bad nymph, for quick jigs vex.") + True + >>> check_pangram("Jived fox nymph grabs quick waltz.") + True + >>> check_pangram("My name is Unknown") + False + >>> check_pangram("The quick brown fox jumps over the la_y dog") + False + >>> check_pangram() + True + """ + frequency = set() + input_str = input_str.replace( + " ", "" + ) # Replacing all the Whitespaces in our sentence + for alpha in input_str: + if "a" <= alpha.lower() <= "z": + frequency.add(alpha.lower()) + + return True if len(frequency) == 26 else False + + +def check_pangram_faster( + input_str: str = "The quick brown fox jumps over the lazy dog", +) -> bool: + """ + >>> check_pangram_faster("The quick brown fox jumps over the lazy dog") + True + >>> check_pangram("Waltz, bad nymph, for quick jigs vex.") + True + >>> check_pangram("Jived fox nymph grabs quick waltz.") + True + >>> check_pangram_faster("The quick brown fox jumps over the la_y dog") + False + >>> check_pangram_faster() + True + """ + flag = [False] * 26 + for char in input_str: + if char.islower(): + flag[ord(char) - ord("a")] = True + return all(flag) + + +def benchmark() -> None: + """ + Benchmark code comparing different version. + """ + from timeit import timeit + + setup = "from __main__ import check_pangram, check_pangram_faster" + print(timeit("check_pangram()", setup=setup)) + print(timeit("check_pangram_faster()", setup=setup)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + benchmark() diff --git a/strings/is_palindrome.py b/strings/is_palindrome.py new file mode 100644 index 000000000000..4776a5fc29c4 --- /dev/null +++ b/strings/is_palindrome.py @@ -0,0 +1,28 @@ +def is_palindrome(s: str) -> bool: + """ + Determine whether the string is palindrome + :param s: + :return: Boolean + >>> is_palindrome("a man a plan a canal panama".replace(" ", "")) + True + >>> is_palindrome("Hello") + False + >>> is_palindrome("Able was I ere I saw Elba") + True + >>> is_palindrome("racecar") + True + >>> is_palindrome("Mr. Owl ate my metal worm?") + True + """ + # Since Punctuation, capitalization, and spaces are usually ignored while checking + # Palindrome, we first remove them from our string. + s = "".join([character for character in s.lower() if character.isalnum()]) + return s == s[::-1] + + +if __name__ == "__main__": + s = input("Enter string to determine whether its palindrome or not: ").strip() + if is_palindrome(s): + print("Given string is palindrome") + else: + print("Given string is not palindrome") diff --git a/strings/jaro_winkler.py b/strings/jaro_winkler.py new file mode 100644 index 000000000000..f4a8fbad3ac8 --- /dev/null +++ b/strings/jaro_winkler.py @@ -0,0 +1,78 @@ +"""https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance""" + + +def jaro_winkler(str1: str, str2: str) -> float: + """ + Jaro–Winkler distance is a string metric measuring an edit distance between two + sequences. + Output value is between 0.0 and 1.0. + + >>> jaro_winkler("martha", "marhta") + 0.9611111111111111 + >>> jaro_winkler("CRATE", "TRACE") + 0.7333333333333334 + >>> jaro_winkler("test", "dbdbdbdb") + 0.0 + >>> jaro_winkler("test", "test") + 1.0 + >>> jaro_winkler("hello world", "HeLLo W0rlD") + 0.6363636363636364 + >>> jaro_winkler("test", "") + 0.0 + >>> jaro_winkler("hello", "world") + 0.4666666666666666 + >>> jaro_winkler("hell**o", "*world") + 0.4365079365079365 + """ + + def get_matched_characters(_str1: str, _str2: str) -> str: + matched = [] + limit = min(len(_str1), len(_str2)) // 2 + for i, l in enumerate(_str1): + left = int(max(0, i - limit)) + right = int(min(i + limit + 1, len(_str2))) + if l in _str2[left:right]: + matched.append(l) + _str2 = f"{_str2[0:_str2.index(l)]} {_str2[_str2.index(l) + 1:]}" + + return "".join(matched) + + # matching characters + matching_1 = get_matched_characters(str1, str2) + matching_2 = get_matched_characters(str2, str1) + match_count = len(matching_1) + + # transposition + transpositions = ( + len([(c1, c2) for c1, c2 in zip(matching_1, matching_2) if c1 != c2]) // 2 + ) + + if not match_count: + jaro = 0.0 + else: + jaro = ( + 1 + / 3 + * ( + match_count / len(str1) + + match_count / len(str2) + + (match_count - transpositions) / match_count + ) + ) + + # common prefix up to 4 characters + prefix_len = 0 + for c1, c2 in zip(str1[:4], str2[:4]): + if c1 == c2: + prefix_len += 1 + else: + break + + return jaro + 0.1 * prefix_len * (1 - jaro) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print(jaro_winkler("hello", "world")) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 4553944284be..a205ce37e3e5 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -1,15 +1,18 @@ -def kmp(pattern, text): +from typing import List + + +def kmp(pattern: str, text: str) -> bool: """ The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text with complexity O(n + m) 1) Preprocess pattern to identify any suffixes that are identical to prefixes - This tells us where to continue from if we get a mismatch between a character in our pattern - and the text. + This tells us where to continue from if we get a mismatch between a character + in our pattern and the text. - 2) Step through the text one character at a time and compare it to a character in the pattern - updating our location within the pattern if necessary + 2) Step through the text one character at a time and compare it to a character in + the pattern updating our location within the pattern if necessary """ @@ -33,7 +36,7 @@ def kmp(pattern, text): return False -def get_failure_array(pattern): +def get_failure_array(pattern: str) -> List[int]: """ Calculates the new index we should go to if we fail a comparison :param pattern: @@ -46,14 +49,14 @@ def get_failure_array(pattern): if pattern[i] == pattern[j]: i += 1 elif i > 0: - i = failure[i-1] + i = failure[i - 1] continue j += 1 failure.append(i) return failure -if __name__ == '__main__': +if __name__ == "__main__": # Test 1) pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" diff --git a/strings/levenshtein_distance.py b/strings/levenshtein_distance.py index 274dfd7ccf9b..54948a96670b 100644 --- a/strings/levenshtein_distance.py +++ b/strings/levenshtein_distance.py @@ -13,7 +13,7 @@ """ -def levenshtein_distance(first_word, second_word): +def levenshtein_distance(first_word: str, second_word: str) -> int: """Implementation of the levenshtein distance in Python. :param first_word: the first word to measure the difference. :param second_word: the second word to measure the difference. @@ -64,15 +64,13 @@ def levenshtein_distance(first_word, second_word): return previous_row[-1] -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 - - first_word = raw_input('Enter the first word:\n').strip() - second_word = raw_input('Enter the second word:\n').strip() +if __name__ == "__main__": + first_word = input("Enter the first word:\n").strip() + second_word = input("Enter the second word:\n").strip() result = levenshtein_distance(first_word, second_word) - print('Levenshtein distance between {} and {} is {}'.format( - first_word, second_word, result)) + print( + "Levenshtein distance between {} and {} is {}".format( + first_word, second_word, result + ) + ) diff --git a/strings/lower.py b/strings/lower.py new file mode 100644 index 000000000000..b7abe9fc957d --- /dev/null +++ b/strings/lower.py @@ -0,0 +1,26 @@ +def lower(word: str) -> str: + """ + Will convert the entire string to lowecase letters + + >>> lower("wow") + 'wow' + >>> lower("HellZo") + 'hellzo' + >>> lower("WHAT") + 'what' + >>> lower("wh[]32") + 'wh[]32' + >>> lower("whAT") + 'what' + """ + + # converting to ascii value int value and checking to see if char is a capital + # letter if it is a capital letter it is getting shift by 32 which makes it a lower + # case letter + return "".join(chr(ord(char) + 32) if "A" <= char <= "Z" else char for char in word) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/strings/manacher.py b/strings/manacher.py index 9a44b19ba77a..5476e06839b7 100644 --- a/strings/manacher.py +++ b/strings/manacher.py @@ -1,52 +1,107 @@ -# calculate palindromic length from center with incresmenting difference -def palindromic_length( center, diff, string): - if center-diff == -1 or center+diff == len(string) or string[center-diff] != string[center+diff] : - return 0 - return 1 + palindromic_length(center, diff+1, string) - -def palindromic_string( input_string ): +def palindromic_string(input_string: str) -> str: """ - Manacher’s algorithm which finds Longest Palindromic Substring in linear time. - - 1. first this conver input_string("xyx") into new_string("x|y|x") where odd positions are actual input - characters. - 2. for each character in new_string it find corresponding length and store, - a. max_length - b. max_length's center - 3. return output_string from center - max_length to center + max_length and remove all "|" + >>> palindromic_string('abbbaba') + 'abbba' + >>> palindromic_string('ababa') + 'ababa' + + Manacher’s algorithm which finds Longest palindromic Substring in linear time. + + 1. first this convert input_string("xyx") into new_string("x|y|x") where odd + positions are actual input characters. + 2. for each character in new_string it find corresponding length and store the + length and l,r to store previously calculated info.(please look the explanation + for details) + + 3. return corresponding output_string by removing all "|" """ max_length = 0 - + # if input_string is "aba" than new_input_string become "a|b|a" new_input_string = "" output_string = "" # append each character + "|" in new_string for range(0, length-1) - for i in input_string[:len(input_string)-1] : + for i in input_string[: len(input_string) - 1]: new_input_string += i + "|" - #append last character + # append last character new_input_string += input_string[-1] + # we will store the starting and ending of previous furthest ending palindromic + # substring + l, r = 0, 0 + + # length[i] shows the length of palindromic substring with center i + length = [1 for i in range(len(new_input_string))] # for each character in new_string find corresponding palindromic string - for i in range(len(new_input_string)) : + for i in range(len(new_input_string)): + k = 1 if i > r else min(length[l + r - i] // 2, r - i + 1) + while ( + i - k >= 0 + and i + k < len(new_input_string) + and new_input_string[k + i] == new_input_string[i - k] + ): + k += 1 + + length[i] = 2 * k - 1 - # get palindromic length from ith position - length = palindromic_length(i, 1, new_input_string) + # does this string is ending after the previously explored end (that is r) ? + # if yes the update the new r to the last index of this + if i + k - 1 > r: + l = i - k + 1 # noqa: E741 + r = i + k - 1 # update max_length and start position - if max_length < length : - max_length = length + if max_length < length[i]: + max_length = length[i] start = i - - #create that string - for i in new_input_string[start-max_length:start+max_length+1] : + + # create that string + s = new_input_string[start - max_length // 2 : start + max_length // 2 + 1] + for i in s: if i != "|": output_string += i - + return output_string -if __name__ == '__main__': - n = input() - print(palindromic_string(n)) +if __name__ == "__main__": + import doctest + + doctest.testmod() + +""" +...a0...a1...a2.....a3......a4...a5...a6.... + +consider the string for which we are calculating the longest palindromic substring is +shown above where ... are some characters in between and right now we are calculating +the length of palindromic substring with center at a5 with following conditions : +i) we have stored the length of palindromic substring which has center at a3 (starts at + l ends at r) and it is the furthest ending till now, and it has ending after a6 +ii) a2 and a4 are equally distant from a3 so char(a2) == char(a4) +iii) a0 and a6 are equally distant from a3 so char(a0) == char(a6) +iv) a1 is corresponding equal character of a5 in palindrome with center a3 (remember + that in below derivation of a4==a6) + +now for a5 we will calculate the length of palindromic substring with center as a5 but +can we use previously calculated information in some way? +Yes, look the above string we know that a5 is inside the palindrome with center a3 and +previously we have have calculated that +a0==a2 (palindrome of center a1) +a2==a4 (palindrome of center a3) +a0==a6 (palindrome of center a3) +so a4==a6 + +so we can say that palindrome at center a5 is at least as long as palindrome at center +a1 but this only holds if a0 and a6 are inside the limits of palindrome centered at a3 +so finally .. + +len_of_palindrome__at(a5) = min(len_of_palindrome_at(a1), r-a5) +where a3 lies from l to r and we have to keep updating that + +and if the a5 lies outside of l,r boundary we calculate length of palindrome with +bruteforce and update l,r. + +it gives the linear time complexity just like z-function +""" diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index de7f9f727283..e990aaa2679b 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -1,121 +1,131 @@ -from __future__ import print_function +from typing import List, Tuple -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -''' -Algorithm for calculating the most cost-efficient sequence for converting one string into another. +""" +Algorithm for calculating the most cost-efficient sequence for converting one string +into another. The only allowed operations are ----Copy character with cost cC ----Replace character with cost cR ----Delete character with cost cD ----Insert character with cost cI -''' -def compute_transform_tables(X, Y, cC, cR, cD, cI): - X = list(X) - Y = list(Y) - m = len(X) - n = len(Y) - - costs = [[0 for _ in xrange(n+1)] for _ in xrange(m+1)] - ops = [[0 for _ in xrange(n+1)] for _ in xrange(m+1)] - - for i in xrange(1, m+1): - costs[i][0] = i*cD - ops[i][0] = 'D%c' % X[i-1] - - for i in xrange(1, n+1): - costs[0][i] = i*cI - ops[0][i] = 'I%c' % Y[i-1] - - for i in xrange(1, m+1): - for j in xrange(1, n+1): - if X[i-1] == Y[j-1]: - costs[i][j] = costs[i-1][j-1] + cC - ops[i][j] = 'C%c' % X[i-1] - else: - costs[i][j] = costs[i-1][j-1] + cR - ops[i][j] = 'R%c' % X[i-1] + str(Y[j-1]) - - if costs[i-1][j] + cD < costs[i][j]: - costs[i][j] = costs[i-1][j] + cD - ops[i][j] = 'D%c' % X[i-1] - - if costs[i][j-1] + cI < costs[i][j]: - costs[i][j] = costs[i][j-1] + cI - ops[i][j] = 'I%c' % Y[j-1] - - return costs, ops - -def assemble_transformation(ops, i, j): - if i == 0 and j == 0: - seq = [] - return seq - else: - if ops[i][j][0] == 'C' or ops[i][j][0] == 'R': - seq = assemble_transformation(ops, i-1, j-1) - seq.append(ops[i][j]) - return seq - elif ops[i][j][0] == 'D': - seq = assemble_transformation(ops, i-1, j) - seq.append(ops[i][j]) - return seq - else: - seq = assemble_transformation(ops, i, j-1) - seq.append(ops[i][j]) - return seq - -if __name__ == '__main__': - _, operations = compute_transform_tables('Python', 'Algorithms', -1, 1, 2, 2) - - m = len(operations) - n = len(operations[0]) - sequence = assemble_transformation(operations, m-1, n-1) - - string = list('Python') - i = 0 - cost = 0 - - with open('min_cost.txt', 'w') as file: - for op in sequence: - print(''.join(string)) - - if op[0] == 'C': - file.write('%-16s' % 'Copy %c' % op[1]) - file.write('\t\t\t' + ''.join(string)) - file.write('\r\n') - - cost -= 1 - elif op[0] == 'R': - string[i] = op[2] - - file.write('%-16s' % ('Replace %c' % op[1] + ' with ' + str(op[2]))) - file.write('\t\t' + ''.join(string)) - file.write('\r\n') - - cost += 1 - elif op[0] == 'D': - string.pop(i) - - file.write('%-16s' % 'Delete %c' % op[1]) - file.write('\t\t\t' + ''.join(string)) - file.write('\r\n') - - cost += 2 - else: - string.insert(i, op[1]) - - file.write('%-16s' % 'Insert %c' % op[1]) - file.write('\t\t\t' + ''.join(string)) - file.write('\r\n') - - cost += 2 - - i += 1 - - print(''.join(string)) - print('Cost: ', cost) - - file.write('\r\nMinimum cost: ' + str(cost)) +--- Cost to copy a character is copy_cost +--- Cost to replace a character is replace_cost +--- Cost to delete a character is delete_cost +--- Cost to insert a character is insert_cost +""" + + +def compute_transform_tables( + source_string: str, + destination_string: str, + copy_cost: int, + replace_cost: int, + delete_cost: int, + insert_cost: int, +) -> Tuple[List[int], List[str]]: + source_seq = list(source_string) + destination_seq = list(destination_string) + len_source_seq = len(source_seq) + len_destination_seq = len(destination_seq) + + costs = [ + [0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) + ] + ops = [ + [0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) + ] + + for i in range(1, len_source_seq + 1): + costs[i][0] = i * delete_cost + ops[i][0] = "D%c" % source_seq[i - 1] + + for i in range(1, len_destination_seq + 1): + costs[0][i] = i * insert_cost + ops[0][i] = "I%c" % destination_seq[i - 1] + + for i in range(1, len_source_seq + 1): + for j in range(1, len_destination_seq + 1): + if source_seq[i - 1] == destination_seq[j - 1]: + costs[i][j] = costs[i - 1][j - 1] + copy_cost + ops[i][j] = "C%c" % source_seq[i - 1] + else: + costs[i][j] = costs[i - 1][j - 1] + replace_cost + ops[i][j] = "R%c" % source_seq[i - 1] + str(destination_seq[j - 1]) + + if costs[i - 1][j] + delete_cost < costs[i][j]: + costs[i][j] = costs[i - 1][j] + delete_cost + ops[i][j] = "D%c" % source_seq[i - 1] + + if costs[i][j - 1] + insert_cost < costs[i][j]: + costs[i][j] = costs[i][j - 1] + insert_cost + ops[i][j] = "I%c" % destination_seq[j - 1] + + return costs, ops + + +def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]: + if i == 0 and j == 0: + return [] + else: + if ops[i][j][0] == "C" or ops[i][j][0] == "R": + seq = assemble_transformation(ops, i - 1, j - 1) + seq.append(ops[i][j]) + return seq + elif ops[i][j][0] == "D": + seq = assemble_transformation(ops, i - 1, j) + seq.append(ops[i][j]) + return seq + else: + seq = assemble_transformation(ops, i, j - 1) + seq.append(ops[i][j]) + return seq + + +if __name__ == "__main__": + _, operations = compute_transform_tables("Python", "Algorithms", -1, 1, 2, 2) + + m = len(operations) + n = len(operations[0]) + sequence = assemble_transformation(operations, m - 1, n - 1) + + string = list("Python") + i = 0 + cost = 0 + + with open("min_cost.txt", "w") as file: + for op in sequence: + print("".join(string)) + + if op[0] == "C": + file.write("%-16s" % "Copy %c" % op[1]) + file.write("\t\t\t" + "".join(string)) + file.write("\r\n") + + cost -= 1 + elif op[0] == "R": + string[i] = op[2] + + file.write("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2]))) + file.write("\t\t" + "".join(string)) + file.write("\r\n") + + cost += 1 + elif op[0] == "D": + string.pop(i) + + file.write("%-16s" % "Delete %c" % op[1]) + file.write("\t\t\t" + "".join(string)) + file.write("\r\n") + + cost += 2 + else: + string.insert(i, op[1]) + + file.write("%-16s" % "Insert %c" % op[1]) + file.write("\t\t\t" + "".join(string)) + file.write("\r\n") + + cost += 2 + + i += 1 + + print("".join(string)) + print("Cost: ", cost) + + file.write("\r\nMinimum cost: " + str(cost)) diff --git a/strings/naiveStringSearch.py b/strings/naiveStringSearch.py deleted file mode 100644 index 04c0d8157b24..000000000000 --- a/strings/naiveStringSearch.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -this algorithm tries to find the pattern from every position of -the mainString if pattern is found from position i it add it to -the answer and does the same for position i+1 - -Complexity : O(n*m) - n=length of main string - m=length of pattern string -""" -def naivePatternSearch(mainString,pattern): - patLen=len(pattern) - strLen=len(mainString) - position=[] - for i in range(strLen-patLen+1): - match_found=True - for j in range(patLen): - if mainString[i+j]!=pattern[j]: - match_found=False - break - if match_found: - position.append(i) - return position - -mainString="ABAAABCDBBABCDDEBCABC" -pattern="ABC" -position=naivePatternSearch(mainString,pattern) -print("Pattern found in position ") -for x in position: - print(x) \ No newline at end of file diff --git a/strings/naive_string_search.py b/strings/naive_string_search.py new file mode 100644 index 000000000000..f28950264121 --- /dev/null +++ b/strings/naive_string_search.py @@ -0,0 +1,42 @@ +""" +https://en.wikipedia.org/wiki/String-searching_algorithm#Na%C3%AFve_string_search + +this algorithm tries to find the pattern from every position of +the mainString if pattern is found from position i it add it to +the answer and does the same for position i+1 + +Complexity : O(n*m) + n=length of main string + m=length of pattern string +""" + + +def naive_pattern_search(s: str, pattern: str) -> list: + """ + >>> naive_pattern_search("ABAAABCDBBABCDDEBCABC", "ABC") + [4, 10, 18] + >>> naive_pattern_search("ABC", "ABAAABCDBBABCDDEBCABC") + [] + >>> naive_pattern_search("", "ABC") + [] + >>> naive_pattern_search("TEST", "TEST") + [0] + >>> naive_pattern_search("ABCDEGFTEST", "TEST") + [7] + """ + pat_len = len(pattern) + position = [] + for i in range(len(s) - pat_len + 1): + match_found = True + for j in range(pat_len): + if s[i + j] != pattern[j]: + match_found = False + break + if match_found: + position.append(i) + return position + + +if __name__ == "__main__": + assert naive_pattern_search("ABCDEFG", "DE") == [3] + print(f"{naive_pattern_search('ABAAABCDBBABCDDEBCABC', 'ABC') = }") diff --git a/strings/prefix_function.py b/strings/prefix_function.py new file mode 100644 index 000000000000..9e6dbbf5408f --- /dev/null +++ b/strings/prefix_function.py @@ -0,0 +1,65 @@ +""" +https://cp-algorithms.com/string/prefix-function.html + +Prefix function Knuth–Morris–Pratt algorithm + +Different algorithm than Knuth-Morris-Pratt pattern finding + +E.x. Finding longest prefix which is also suffix + +Time Complexity: O(n) - where n is the length of the string +""" + + +def prefix_function(input_string: str) -> list: + """ + For the given string this function computes value for each index(i), + which represents the longest coincidence of prefix and sufix + for given substring (input_str[0...i]) + + For the value of the first element the algorithm always returns 0 + + >>> prefix_function("aabcdaabc") + [0, 1, 0, 0, 0, 1, 2, 3, 4] + >>> prefix_function("asdasdad") + [0, 0, 0, 1, 2, 3, 4, 0] + """ + + # list for the result values + prefix_result = [0] * len(input_string) + + for i in range(1, len(input_string)): + + # use last results for better performance - dynamic programming + j = prefix_result[i - 1] + while j > 0 and input_string[i] != input_string[j]: + j = prefix_result[j - 1] + + if input_string[i] == input_string[j]: + j += 1 + prefix_result[i] = j + + return prefix_result + + +def longest_prefix(input_str: str) -> int: + """ + Prefix-function use case + Finding longest prefix which is sufix as well + + >>> longest_prefix("aabcdaabc") + 4 + >>> longest_prefix("asdasdad") + 4 + >>> longest_prefix("abcab") + 2 + """ + + # just returning maximum value of the array gives us answer + return max(prefix_function(input_str)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/strings/rabin_karp.py b/strings/rabin_karp.py index 04a849266ead..81ca611a76b3 100644 --- a/strings/rabin_karp.py +++ b/strings/rabin_karp.py @@ -1,33 +1,61 @@ -def rabin_karp(pattern, text): - """ +# Numbers of alphabet which we call base +alphabet_size = 256 +# Modulus to hash a string +modulus = 1000003 + +def rabin_karp(pattern: str, text: str) -> bool: + """ The Rabin-Karp Algorithm for finding a pattern within a piece of text with complexity O(nm), most efficient when it is used with multiple patterns - as it is able to check if any of a set of patterns match a section of text in o(1) given the precomputed hashes. + as it is able to check if any of a set of patterns match a section of text in o(1) + given the precomputed hashes. - This will be the simple version which only assumes one pattern is being searched for but it's not hard to modify + This will be the simple version which only assumes one pattern is being searched + for but it's not hard to modify 1) Calculate pattern hash - 2) Step through the text one character at a time passing a window with the same length as the pattern - calculating the hash of the text within the window compare it with the hash of the pattern. Only testing - equality if the hashes match - + 2) Step through the text one character at a time passing a window with the same + length as the pattern + calculating the hash of the text within the window compare it with the hash + of the pattern. Only testing equality if the hashes match """ p_len = len(pattern) - p_hash = hash(pattern) + t_len = len(text) + if p_len > t_len: + return False + + p_hash = 0 + text_hash = 0 + modulus_power = 1 - for i in range(0, len(text) - (p_len - 1)): + # Calculating the hash of pattern and substring of text + for i in range(p_len): + p_hash = (ord(pattern[i]) + p_hash * alphabet_size) % modulus + text_hash = (ord(text[i]) + text_hash * alphabet_size) % modulus + if i == p_len - 1: + continue + modulus_power = (modulus_power * alphabet_size) % modulus - # written like this t - text_hash = hash(text[i:i + p_len]) - if text_hash == p_hash and \ - text[i:i + p_len] == pattern: + for i in range(0, t_len - p_len + 1): + if text_hash == p_hash and text[i : i + p_len] == pattern: return True + if i == t_len - p_len: + continue + # Calculate the https://en.wikipedia.org/wiki/Rolling_hash + text_hash = ( + (text_hash - ord(text[i]) * modulus_power) * alphabet_size + + ord(text[i + p_len]) + ) % modulus return False -if __name__ == '__main__': +def test_rabin_karp() -> None: + """ + >>> test_rabin_karp() + Success. + """ # Test 1) pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" @@ -48,3 +76,15 @@ def rabin_karp(pattern, text): pattern = "abcdabcy" text = "abcxabcdabxabcdabcdabcy" assert rabin_karp(pattern, text) + + # Test 5) + pattern = "Lü" + text = "Lüsai" + assert rabin_karp(pattern, text) + pattern = "Lue" + assert not rabin_karp(pattern, text) + print("Success.") + + +if __name__ == "__main__": + test_rabin_karp() diff --git a/strings/remove_duplicate.py b/strings/remove_duplicate.py new file mode 100644 index 000000000000..5ab0e9962752 --- /dev/null +++ b/strings/remove_duplicate.py @@ -0,0 +1,15 @@ +def remove_duplicates(sentence: str) -> str: + """ + Remove duplicates from sentence + >>> remove_duplicates("Python is great and Java is also great") + 'Java Python also and great is' + >>> remove_duplicates("Python is great and Java is also great") + 'Java Python also and great is' + """ + return " ".join(sorted(set(sentence.split()))) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py new file mode 100644 index 000000000000..10b8a6d72a0f --- /dev/null +++ b/strings/reverse_letters.py @@ -0,0 +1,19 @@ +def reverse_letters(input_str: str) -> str: + """ + Reverses letters in a given string without adjusting the position of the words + >>> reverse_letters('The cat in the hat') + 'ehT tac ni eht tah' + >>> reverse_letters('The quick brown fox jumped over the lazy dog.') + 'ehT kciuq nworb xof depmuj revo eht yzal .god' + >>> reverse_letters('Is this true?') + 'sI siht ?eurt' + >>> reverse_letters("I love Python") + 'I evol nohtyP' + """ + return " ".join([word[::-1] for word in input_str.split()]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/strings/reverse_words.py b/strings/reverse_words.py new file mode 100644 index 000000000000..504c1c2089dd --- /dev/null +++ b/strings/reverse_words.py @@ -0,0 +1,15 @@ +def reverse_words(input_str: str) -> str: + """ + Reverses words in a given string + >>> reverse_words("I love Python") + 'Python love I' + >>> reverse_words("I Love Python") + 'Python Love I' + """ + return " ".join(input_str.split()[::-1]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/strings/split.py b/strings/split.py new file mode 100644 index 000000000000..b62b86d2401f --- /dev/null +++ b/strings/split.py @@ -0,0 +1,34 @@ +def split(string: str, separator: str = " ") -> list: + """ + Will split the string up into all the values separated by the separator + (defaults to spaces) + + >>> split("apple#banana#cherry#orange",separator='#') + ['apple', 'banana', 'cherry', 'orange'] + + >>> split("Hello there") + ['Hello', 'there'] + + >>> split("11/22/63",separator = '/') + ['11', '22', '63'] + + >>> split("12:43:39",separator = ":") + ['12', '43', '39'] + """ + + split_words = [] + + last_index = 0 + for index, char in enumerate(string): + if char == separator: + split_words.append(string[last_index:index]) + last_index = index + 1 + elif index + 1 == len(string): + split_words.append(string[last_index : index + 1]) + return split_words + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/strings/swap_case.py b/strings/swap_case.py new file mode 100644 index 000000000000..107fda4b52ec --- /dev/null +++ b/strings/swap_case.py @@ -0,0 +1,37 @@ +""" +This algorithm helps you to swap cases. + +User will give input and then program will perform swap cases. + +In other words, convert all lowercase letters to uppercase letters and vice versa. +For example: +1. Please input sentence: Algorithm.Python@89 + aLGORITHM.pYTHON@89 +2. Please input sentence: github.com/mayur200 + GITHUB.COM/MAYUR200 + +""" + + +def swap_case(sentence: str) -> str: + """ + This function will convert all lowercase letters to uppercase letters + and vice versa. + + >>> swap_case('Algorithm.Python@89') + 'aLGORITHM.pYTHON@89' + """ + new_string = "" + for char in sentence: + if char.isupper(): + new_string += char.lower() + elif char.islower(): + new_string += char.upper() + else: + new_string += char + + return new_string + + +if __name__ == "__main__": + print(swap_case(input("Please input sentence: "))) diff --git a/strings/upper.py b/strings/upper.py new file mode 100644 index 000000000000..5edd40b79808 --- /dev/null +++ b/strings/upper.py @@ -0,0 +1,24 @@ +def upper(word: str) -> str: + """ + Will convert the entire string to uppercase letters + + >>> upper("wow") + 'WOW' + >>> upper("Hello") + 'HELLO' + >>> upper("WHAT") + 'WHAT' + >>> upper("wh[]32") + 'WH[]32' + """ + + # Converting to ascii value int value and checking to see if char is a lower letter + # if it is a lowercase letter it is getting shift by 32 which makes it an uppercase + # case letter + return "".join(chr(ord(char) - 32) if "a" <= char <= "z" else char for char in word) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/strings/word_occurrence.py b/strings/word_occurrence.py new file mode 100644 index 000000000000..ef612e12dfa4 --- /dev/null +++ b/strings/word_occurrence.py @@ -0,0 +1,26 @@ +# Created by sarathkaul on 17/11/19 +# Modified by Arkadip Bhattacharya(@darkmatter18) on 20/04/2020 +from collections import defaultdict + + +def word_occurence(sentence: str) -> dict: + """ + >>> from collections import Counter + >>> SENTENCE = "a b A b c b d b d e f e g e h e i e j e 0" + >>> occurence_dict = word_occurence(SENTENCE) + >>> all(occurence_dict[word] == count for word, count + ... in Counter(SENTENCE.split()).items()) + True + >>> dict(word_occurence("Two spaces")) + {'Two': 1, 'spaces': 1} + """ + occurrence = defaultdict(int) + # Creating a dictionary containing count of each word + for word in sentence.split(): + occurrence[word] += 1 + return occurrence + + +if __name__ == "__main__": + for word, count in word_occurence("INPUT STRING").items(): + print(f"{word}: {count}") diff --git a/strings/z_function.py b/strings/z_function.py new file mode 100644 index 000000000000..d8d823a37efb --- /dev/null +++ b/strings/z_function.py @@ -0,0 +1,90 @@ +""" +https://cp-algorithms.com/string/z-function.html + +Z-function or Z algorithm + +Efficient algorithm for pattern occurrence in a string + +Time Complexity: O(n) - where n is the length of the string + +""" + + +def z_function(input_str: str) -> list: + """ + For the given string this function computes value for each index, + which represents the maximal length substring starting from the index + and is the same as the prefix of the same size + + e.x. for string 'abab' for second index value would be 2 + + For the value of the first element the algorithm always returns 0 + + >>> z_function("abracadabra") + [0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 1] + >>> z_function("aaaa") + [0, 3, 2, 1] + >>> z_function("zxxzxxz") + [0, 0, 0, 4, 0, 0, 1] + """ + z_result = [0] * len(input_str) + + # initialize interval's left pointer and right pointer + left_pointer, right_pointer = 0, 0 + + for i in range(1, len(input_str)): + # case when current index is inside the interval + if i <= right_pointer: + min_edge = min(right_pointer - i + 1, z_result[i - left_pointer]) + z_result[i] = min_edge + + while go_next(i, z_result, input_str): + z_result[i] += 1 + + # if new index's result gives us more right interval, + # we've to update left_pointer and right_pointer + if i + z_result[i] - 1 > right_pointer: + left_pointer, right_pointer = i, i + z_result[i] - 1 + + return z_result + + +def go_next(i, z_result, s): + """ + Check if we have to move forward to the next characters or not + """ + return i + z_result[i] < len(s) and s[z_result[i]] == s[i + z_result[i]] + + +def find_pattern(pattern: str, input_str: str) -> int: + """ + Example of using z-function for pattern occurrence + Given function returns the number of times 'pattern' + appears in 'input_str' as a substring + + >>> find_pattern("abr", "abracadabra") + 2 + >>> find_pattern("a", "aaaa") + 4 + >>> find_pattern("xz", "zxxzxxz") + 2 + """ + answer = 0 + # concatenate 'pattern' and 'input_str' and call z_function + # with concatenated string + z_result = z_function(pattern + input_str) + + for val in z_result: + # if value is greater then length of the pattern string + # that means this index is starting position of substring + # which is equal to pattern string + if val >= len(pattern): + answer += 1 + + return answer + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/traversals/__init__.py b/traversals/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/traversals/binary_tree_traversals.py b/traversals/binary_tree_traversals.py index 393664579146..cb471ba55bac 100644 --- a/traversals/binary_tree_traversals.py +++ b/traversals/binary_tree_traversals.py @@ -1,15 +1,12 @@ +# flake8: noqa + """ -This is pure python implementation of tree traversal algorithms +This is pure Python implementation of tree traversal algorithms """ -from __future__ import print_function +from __future__ import annotations import queue -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 - class TreeNode: def __init__(self, data): @@ -20,82 +17,150 @@ def __init__(self, data): def build_tree(): print("\n********Press N to stop entering at any point of time********\n") - print("Enter the value of the root node: ", end="") - check = raw_input().strip().lower() - if check == 'n': + check = input("Enter the value of the root node: ").strip().lower() or "n" + if check == "n": return None - data = int(check) - q = queue.Queue() - tree_node = TreeNode(data) + q: queue.Queue = queue.Queue() + tree_node = TreeNode(int(check)) q.put(tree_node) while not q.empty(): node_found = q.get() - print("Enter the left node of %s: " % node_found.data, end="") - check = raw_input().strip().lower() - if check == 'n': + msg = "Enter the left node of %s: " % node_found.data + check = input(msg).strip().lower() or "n" + if check == "n": return tree_node - left_data = int(check) - left_node = TreeNode(left_data) + left_node = TreeNode(int(check)) node_found.left = left_node q.put(left_node) - print("Enter the right node of %s: " % node_found.data, end="") - check = raw_input().strip().lower() - if check == 'n': + msg = "Enter the right node of %s: " % node_found.data + check = input(msg).strip().lower() or "n" + if check == "n": return tree_node - right_data = int(check) - right_node = TreeNode(right_data) + right_node = TreeNode(int(check)) node_found.right = right_node q.put(right_node) -def pre_order(node): +def pre_order(node: TreeNode) -> None: + """ + >>> root = TreeNode(1) + >>> tree_node2 = TreeNode(2) + >>> tree_node3 = TreeNode(3) + >>> tree_node4 = TreeNode(4) + >>> tree_node5 = TreeNode(5) + >>> tree_node6 = TreeNode(6) + >>> tree_node7 = TreeNode(7) + >>> root.left, root.right = tree_node2, tree_node3 + >>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5 + >>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7 + >>> pre_order(root) + 1,2,4,5,3,6,7, + """ if not isinstance(node, TreeNode) or not node: return - print(node.data, end=" ") + print(node.data, end=",") pre_order(node.left) pre_order(node.right) -def in_order(node): +def in_order(node: TreeNode) -> None: + """ + >>> root = TreeNode(1) + >>> tree_node2 = TreeNode(2) + >>> tree_node3 = TreeNode(3) + >>> tree_node4 = TreeNode(4) + >>> tree_node5 = TreeNode(5) + >>> tree_node6 = TreeNode(6) + >>> tree_node7 = TreeNode(7) + >>> root.left, root.right = tree_node2, tree_node3 + >>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5 + >>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7 + >>> in_order(root) + 4,2,5,1,6,3,7, + """ if not isinstance(node, TreeNode) or not node: return in_order(node.left) - print(node.data, end=" ") + print(node.data, end=",") in_order(node.right) -def post_order(node): +def post_order(node: TreeNode) -> None: + """ + >>> root = TreeNode(1) + >>> tree_node2 = TreeNode(2) + >>> tree_node3 = TreeNode(3) + >>> tree_node4 = TreeNode(4) + >>> tree_node5 = TreeNode(5) + >>> tree_node6 = TreeNode(6) + >>> tree_node7 = TreeNode(7) + >>> root.left, root.right = tree_node2, tree_node3 + >>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5 + >>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7 + >>> post_order(root) + 4,5,2,6,7,3,1, + """ if not isinstance(node, TreeNode) or not node: return post_order(node.left) post_order(node.right) - print(node.data, end=" ") - - -def level_order(node): + print(node.data, end=",") + + +def level_order(node: TreeNode) -> None: + """ + >>> root = TreeNode(1) + >>> tree_node2 = TreeNode(2) + >>> tree_node3 = TreeNode(3) + >>> tree_node4 = TreeNode(4) + >>> tree_node5 = TreeNode(5) + >>> tree_node6 = TreeNode(6) + >>> tree_node7 = TreeNode(7) + >>> root.left, root.right = tree_node2, tree_node3 + >>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5 + >>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7 + >>> level_order(root) + 1,2,3,4,5,6,7, + """ if not isinstance(node, TreeNode) or not node: return - q = queue.Queue() + q: queue.Queue = queue.Queue() q.put(node) while not q.empty(): node_dequeued = q.get() - print(node_dequeued.data, end=" ") + print(node_dequeued.data, end=",") if node_dequeued.left: q.put(node_dequeued.left) if node_dequeued.right: q.put(node_dequeued.right) -def level_order_actual(node): +def level_order_actual(node: TreeNode) -> None: + """ + >>> root = TreeNode(1) + >>> tree_node2 = TreeNode(2) + >>> tree_node3 = TreeNode(3) + >>> tree_node4 = TreeNode(4) + >>> tree_node5 = TreeNode(5) + >>> tree_node6 = TreeNode(6) + >>> tree_node7 = TreeNode(7) + >>> root.left, root.right = tree_node2, tree_node3 + >>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5 + >>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7 + >>> level_order_actual(root) + 1, + 2,3, + 4,5,6,7, + """ if not isinstance(node, TreeNode) or not node: return - q = queue.Queue() + q: queue.Queue = queue.Queue() q.put(node) while not q.empty(): list = [] while not q.empty(): node_dequeued = q.get() - print(node_dequeued.data, end=" ") + print(node_dequeued.data, end=",") if node_dequeued.left: list.append(node_dequeued.left) if node_dequeued.right: @@ -106,14 +171,28 @@ def level_order_actual(node): # iteration version -def pre_order_iter(node): +def pre_order_iter(node: TreeNode) -> None: + """ + >>> root = TreeNode(1) + >>> tree_node2 = TreeNode(2) + >>> tree_node3 = TreeNode(3) + >>> tree_node4 = TreeNode(4) + >>> tree_node5 = TreeNode(5) + >>> tree_node6 = TreeNode(6) + >>> tree_node7 = TreeNode(7) + >>> root.left, root.right = tree_node2, tree_node3 + >>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5 + >>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7 + >>> pre_order_iter(root) + 1,2,4,5,3,6,7, + """ if not isinstance(node, TreeNode) or not node: return - stack = [] + stack: List[TreeNode] = [] n = node while n or stack: while n: # start from root node, find its left child - print(n.data, end=" ") + print(n.data, end=",") stack.append(n) n = n.left # end of while means current node doesn't have left child @@ -122,21 +201,49 @@ def pre_order_iter(node): n = n.right -def in_order_iter(node): +def in_order_iter(node: TreeNode) -> None: + """ + >>> root = TreeNode(1) + >>> tree_node2 = TreeNode(2) + >>> tree_node3 = TreeNode(3) + >>> tree_node4 = TreeNode(4) + >>> tree_node5 = TreeNode(5) + >>> tree_node6 = TreeNode(6) + >>> tree_node7 = TreeNode(7) + >>> root.left, root.right = tree_node2, tree_node3 + >>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5 + >>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7 + >>> in_order_iter(root) + 4,2,5,1,6,3,7, + """ if not isinstance(node, TreeNode) or not node: return - stack = [] + stack: List[TreeNode] = [] n = node while n or stack: while n: stack.append(n) n = n.left n = stack.pop() - print(n.data, end=" ") + print(n.data, end=",") n = n.right -def post_order_iter(node): +def post_order_iter(node: TreeNode) -> None: + """ + >>> root = TreeNode(1) + >>> tree_node2 = TreeNode(2) + >>> tree_node3 = TreeNode(3) + >>> tree_node4 = TreeNode(4) + >>> tree_node5 = TreeNode(5) + >>> tree_node6 = TreeNode(6) + >>> tree_node7 = TreeNode(7) + >>> root.left, root.right = tree_node2, tree_node3 + >>> tree_node2.left, tree_node2.right = tree_node4 , tree_node5 + >>> tree_node3.left, tree_node3.right = tree_node6 , tree_node7 + >>> post_order_iter(root) + 4,5,2,6,7,3,1, + """ if not isinstance(node, TreeNode) or not node: return stack1, stack2 = [], [] @@ -150,41 +257,51 @@ def post_order_iter(node): stack1.append(n.right) stack2.append(n) while stack2: # pop up from stack2 will be the post order - print(stack2.pop().data, end=" ") + print(stack2.pop().data, end=",") + + +def prompt(s: str = "", width=50, char="*") -> str: + if not s: + return "\n" + width * char + left, extra = divmod(width - len(s) - 2, 2) + return f"{left * char} {s} {(left + extra) * char}" + +if __name__ == "__main__": + import doctest -if __name__ == '__main__': - print("\n********* Binary Tree Traversals ************\n") + doctest.testmod() + print(prompt("Binary Tree Traversals")) node = build_tree() - print("\n********* Pre Order Traversal ************") + print(prompt("Pre Order Traversal")) pre_order(node) - print("\n******************************************\n") + print(prompt() + "\n") - print("\n********* In Order Traversal ************") + print(prompt("In Order Traversal")) in_order(node) - print("\n******************************************\n") + print(prompt() + "\n") - print("\n********* Post Order Traversal ************") + print(prompt("Post Order Traversal")) post_order(node) - print("\n******************************************\n") + print(prompt() + "\n") - print("\n********* Level Order Traversal ************") + print(prompt("Level Order Traversal")) level_order(node) - print("\n******************************************\n") + print(prompt() + "\n") - print("\n********* Actual Level Order Traversal ************") + print(prompt("Actual Level Order Traversal")) level_order_actual(node) - print("\n******************************************\n") + print("*" * 50 + "\n") - print("\n********* Pre Order Traversal - Iteration Version ************") + print(prompt("Pre Order Traversal - Iteration Version")) pre_order_iter(node) - print("\n******************************************\n") + print(prompt() + "\n") - print("\n********* In Order Traversal - Iteration Version ************") + print(prompt("In Order Traversal - Iteration Version")) in_order_iter(node) - print("\n******************************************\n") + print(prompt() + "\n") - print("\n********* Post Order Traversal - Iteration Version ************") + print(prompt("Post Order Traversal - Iteration Version")) post_order_iter(node) - print("\n******************************************\n") + print(prompt()) diff --git a/web_programming/__init__.py b/web_programming/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/web_programming/co2_emission.py b/web_programming/co2_emission.py new file mode 100644 index 000000000000..97927e7ef541 --- /dev/null +++ b/web_programming/co2_emission.py @@ -0,0 +1,25 @@ +""" +Get CO2 emission data from the UK CarbonIntensity API +""" +from datetime import date + +import requests + +BASE_URL = "https://api.carbonintensity.org.uk/intensity" + + +# Emission in the last half hour +def fetch_last_half_hour() -> str: + last_half_hour = requests.get(BASE_URL).json()["data"][0] + return last_half_hour["intensity"]["actual"] + + +# Emissions in a specific date range +def fetch_from_to(start, end) -> list: + return requests.get(f"{BASE_URL}/{start}/{end}").json()["data"] + + +if __name__ == "__main__": + for entry in fetch_from_to(start=date(2020, 10, 1), end=date(2020, 10, 3)): + print("from {from} to {to}: {intensity[actual]}".format(**entry)) + print(f"{fetch_last_half_hour() = }") diff --git a/web_programming/covid_stats_via_xpath.py b/web_programming/covid_stats_via_xpath.py new file mode 100644 index 000000000000..d22ed017878c --- /dev/null +++ b/web_programming/covid_stats_via_xpath.py @@ -0,0 +1,23 @@ +""" +This is to show simple COVID19 info fetching from worldometers site using lxml +* The main motivation to use lxml in place of bs4 is that it is faster and therefore +more convenient to use in Python web projects (e.g. Django or Flask-based) +""" + +from collections import namedtuple + +import requests +from lxml import html + +covid_data = namedtuple("covid_data", "cases deaths recovered") + + +def covid_stats(url: str = "https://www.worldometers.info/coronavirus/") -> covid_data: + xpath_str = '//div[@class = "maincounter-number"]/span/text()' + return covid_data(*html.fromstring(requests.get(url).content).xpath(xpath_str)) + + +fmt = """Total COVID-19 cases in the world: {} +Total deaths due to COVID-19 in the world: {} +Total COVID-19 patients recovered in the world: {}""" +print(fmt.format(*covid_stats())) diff --git a/web_programming/crawl_google_results.py b/web_programming/crawl_google_results.py new file mode 100644 index 000000000000..a33a3f3bbe5c --- /dev/null +++ b/web_programming/crawl_google_results.py @@ -0,0 +1,24 @@ +import sys +import webbrowser + +import requests +from bs4 import BeautifulSoup +from fake_useragent import UserAgent + +if __name__ == "__main__": + print("Googling.....") + url = "https://www.google.com/search?q=" + " ".join(sys.argv[1:]) + res = requests.get(url, headers={"UserAgent": UserAgent().random}) + # res.raise_for_status() + with open("project1a.html", "wb") as out_file: # only for knowing the class + for data in res.iter_content(10000): + out_file.write(data) + soup = BeautifulSoup(res.text, "html.parser") + links = list(soup.select(".eZt8xd"))[:5] + + print(len(links)) + for link in links: + if link.text == "Maps": + webbrowser.open(link.get("href")) + else: + webbrowser.open(f"http://google.com{link.get('href')}") diff --git a/web_programming/crawl_google_scholar_citation.py b/web_programming/crawl_google_scholar_citation.py new file mode 100644 index 000000000000..d023380c0818 --- /dev/null +++ b/web_programming/crawl_google_scholar_citation.py @@ -0,0 +1,32 @@ +""" +Get the citation from google scholar +using title and year of publication, and volume and pages of journal. +""" + +import requests +from bs4 import BeautifulSoup + + +def get_citation(base_url: str, params: dict) -> str: + """ + Return the citation number. + """ + soup = BeautifulSoup(requests.get(base_url, params=params).content, "html.parser") + div = soup.find("div", attrs={"class": "gs_ri"}) + anchors = div.find("div", attrs={"class": "gs_fl"}).find_all("a") + return anchors[2].get_text() + + +if __name__ == "__main__": + params = { + "title": ( + "Precisely geometry controlled microsupercapacitors for ultrahigh areal " + "capacitance, volumetric capacitance, and energy density" + ), + "journal": "Chem. Mater.", + "volume": 30, + "pages": "3979-3990", + "year": 2018, + "hl": "en", + } + print(get_citation("http://scholar.google.com/scholar_lookup", params=params)) diff --git a/web_programming/currency_converter.py b/web_programming/currency_converter.py new file mode 100644 index 000000000000..6aed2a5578a5 --- /dev/null +++ b/web_programming/currency_converter.py @@ -0,0 +1,192 @@ +""" +This is used to convert the currency using the Amdoren Currency API +https://www.amdoren.com +""" + +import os + +import requests + +URL_BASE = "https://www.amdoren.com/api/currency.php" +TESTING = os.getenv("CI", False) +API_KEY = os.getenv("AMDOREN_API_KEY") +if not API_KEY and not TESTING: + raise KeyError("Please put your API key in an environment variable.") + + +# Currency and their description +list_of_currencies = """ +AED United Arab Emirates Dirham +AFN Afghan Afghani +ALL Albanian Lek +AMD Armenian Dram +ANG Netherlands Antillean Guilder +AOA Angolan Kwanza +ARS Argentine Peso +AUD Australian Dollar +AWG Aruban Florin +AZN Azerbaijani Manat +BAM Bosnia & Herzegovina Convertible Mark +BBD Barbadian Dollar +BDT Bangladeshi Taka +BGN Bulgarian Lev +BHD Bahraini Dinar +BIF Burundian Franc +BMD Bermudian Dollar +BND Brunei Dollar +BOB Bolivian Boliviano +BRL Brazilian Real +BSD Bahamian Dollar +BTN Bhutanese Ngultrum +BWP Botswana Pula +BYN Belarus Ruble +BZD Belize Dollar +CAD Canadian Dollar +CDF Congolese Franc +CHF Swiss Franc +CLP Chilean Peso +CNY Chinese Yuan +COP Colombian Peso +CRC Costa Rican Colon +CUC Cuban Convertible Peso +CVE Cape Verdean Escudo +CZK Czech Republic Koruna +DJF Djiboutian Franc +DKK Danish Krone +DOP Dominican Peso +DZD Algerian Dinar +EGP Egyptian Pound +ERN Eritrean Nakfa +ETB Ethiopian Birr +EUR Euro +FJD Fiji Dollar +GBP British Pound Sterling +GEL Georgian Lari +GHS Ghanaian Cedi +GIP Gibraltar Pound +GMD Gambian Dalasi +GNF Guinea Franc +GTQ Guatemalan Quetzal +GYD Guyanaese Dollar +HKD Hong Kong Dollar +HNL Honduran Lempira +HRK Croatian Kuna +HTG Haiti Gourde +HUF Hungarian Forint +IDR Indonesian Rupiah +ILS Israeli Shekel +INR Indian Rupee +IQD Iraqi Dinar +IRR Iranian Rial +ISK Icelandic Krona +JMD Jamaican Dollar +JOD Jordanian Dinar +JPY Japanese Yen +KES Kenyan Shilling +KGS Kyrgystani Som +KHR Cambodian Riel +KMF Comorian Franc +KPW North Korean Won +KRW South Korean Won +KWD Kuwaiti Dinar +KYD Cayman Islands Dollar +KZT Kazakhstan Tenge +LAK Laotian Kip +LBP Lebanese Pound +LKR Sri Lankan Rupee +LRD Liberian Dollar +LSL Lesotho Loti +LYD Libyan Dinar +MAD Moroccan Dirham +MDL Moldovan Leu +MGA Malagasy Ariary +MKD Macedonian Denar +MMK Myanma Kyat +MNT Mongolian Tugrik +MOP Macau Pataca +MRO Mauritanian Ouguiya +MUR Mauritian Rupee +MVR Maldivian Rufiyaa +MWK Malawi Kwacha +MXN Mexican Peso +MYR Malaysian Ringgit +MZN Mozambican Metical +NAD Namibian Dollar +NGN Nigerian Naira +NIO Nicaragua Cordoba +NOK Norwegian Krone +NPR Nepalese Rupee +NZD New Zealand Dollar +OMR Omani Rial +PAB Panamanian Balboa +PEN Peruvian Nuevo Sol +PGK Papua New Guinean Kina +PHP Philippine Peso +PKR Pakistani Rupee +PLN Polish Zloty +PYG Paraguayan Guarani +QAR Qatari Riyal +RON Romanian Leu +RSD Serbian Dinar +RUB Russian Ruble +RWF Rwanda Franc +SAR Saudi Riyal +SBD Solomon Islands Dollar +SCR Seychellois Rupee +SDG Sudanese Pound +SEK Swedish Krona +SGD Singapore Dollar +SHP Saint Helena Pound +SLL Sierra Leonean Leone +SOS Somali Shilling +SRD Surinamese Dollar +SSP South Sudanese Pound +STD Sao Tome and Principe Dobra +SYP Syrian Pound +SZL Swazi Lilangeni +THB Thai Baht +TJS Tajikistan Somoni +TMT Turkmenistani Manat +TND Tunisian Dinar +TOP Tonga Paanga +TRY Turkish Lira +TTD Trinidad and Tobago Dollar +TWD New Taiwan Dollar +TZS Tanzanian Shilling +UAH Ukrainian Hryvnia +UGX Ugandan Shilling +USD United States Dollar +UYU Uruguayan Peso +UZS Uzbekistan Som +VEF Venezuelan Bolivar +VND Vietnamese Dong +VUV Vanuatu Vatu +WST Samoan Tala +XAF Central African CFA franc +XCD East Caribbean Dollar +XOF West African CFA franc +XPF CFP Franc +YER Yemeni Rial +ZAR South African Rand +ZMW Zambian Kwacha +""" + + +def convert_currency( + from_: str = "USD", to: str = "INR", amount: float = 1.0, api_key: str = API_KEY +) -> str: + """https://www.amdoren.com/currency-api/""" + params = locals() + params["from"] = params.pop("from_") + res = requests.get(URL_BASE, params=params).json() + return str(res["amount"]) if res["error"] == 0 else res["error_message"] + + +if __name__ == "__main__": + print( + convert_currency( + input("Enter from currency: ").strip(), + input("Enter to currency: ").strip(), + float(input("Enter the amount: ").strip()), + ) + ) diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py new file mode 100644 index 000000000000..df44da4ef351 --- /dev/null +++ b/web_programming/current_stock_price.py @@ -0,0 +1,14 @@ +import requests +from bs4 import BeautifulSoup + + +def stock_price(symbol: str = "AAPL") -> str: + url = f"https://in.finance.yahoo.com/quote/{symbol}?s={symbol}" + soup = BeautifulSoup(requests.get(url).text, "html.parser") + class_ = "My(6px) Pos(r) smartphone_Mt(6px)" + return soup.find("div", class_=class_).find("span").text + + +if __name__ == "__main__": + for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split(): + print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}") diff --git a/web_programming/current_weather.py b/web_programming/current_weather.py new file mode 100644 index 000000000000..e043b438473f --- /dev/null +++ b/web_programming/current_weather.py @@ -0,0 +1,30 @@ +import requests + +APPID = "" # <-- Put your OpenWeatherMap appid here! +URL_BASE = "http://api.openweathermap.org/data/2.5/" + + +def current_weather(q: str = "Chicago", appid: str = APPID) -> dict: + """https://openweathermap.org/api""" + return requests.get(URL_BASE + "weather", params=locals()).json() + + +def weather_forecast(q: str = "Kolkata, India", appid: str = APPID) -> dict: + """https://openweathermap.org/forecast5""" + return requests.get(URL_BASE + "forecast", params=locals()).json() + + +def weather_onecall(lat: float = 55.68, lon: float = 12.57, appid: str = APPID) -> dict: + """https://openweathermap.org/api/one-call-api""" + return requests.get(URL_BASE + "onecall", params=locals()).json() + + +if __name__ == "__main__": + from pprint import pprint + + while True: + location = input("Enter a location:").strip() + if location: + pprint(current_weather(location)) + else: + break diff --git a/web_programming/daily_horoscope.py b/web_programming/daily_horoscope.py new file mode 100644 index 000000000000..b0dd1cd65924 --- /dev/null +++ b/web_programming/daily_horoscope.py @@ -0,0 +1,35 @@ +import requests +from bs4 import BeautifulSoup + + +def horoscope(zodiac_sign: int, day: str) -> str: + url = ( + "https://www.horoscope.com/us/horoscopes/general/" + f"horoscope-general-daily-{day}.aspx?sign={zodiac_sign}" + ) + soup = BeautifulSoup(requests.get(url).content, "html.parser") + return soup.find("div", class_="main-horoscope").p.text + + +if __name__ == "__main__": + print("Daily Horoscope. \n") + print( + "enter your Zodiac sign number:\n", + "1. Aries\n", + "2. Taurus\n", + "3. Gemini\n", + "4. Cancer\n", + "5. Leo\n", + "6. Virgo\n", + "7. Libra\n", + "8. Scorpio\n", + "9. Sagittarius\n", + "10. Capricorn\n", + "11. Aquarius\n", + "12. Pisces\n", + ) + zodiac_sign = int(input("number> ").strip()) + print("choose some day:\n", "yesterday\n", "today\n", "tomorrow\n") + day = input("enter the day> ") + horoscope_text = horoscope(zodiac_sign, day) + print(horoscope_text) diff --git a/web_programming/emails_from_url.py b/web_programming/emails_from_url.py new file mode 100644 index 000000000000..01dee274f015 --- /dev/null +++ b/web_programming/emails_from_url.py @@ -0,0 +1,103 @@ +"""Get the site emails from URL.""" +__author__ = "Muhammad Umer Farooq" +__license__ = "MIT" +__version__ = "1.0.0" +__maintainer__ = "Muhammad Umer Farooq" +__email__ = "contact@muhammadumerfarooq.me" +__status__ = "Alpha" + +import re +from html.parser import HTMLParser +from urllib import parse + +import requests + + +class Parser(HTMLParser): + def __init__(self, domain: str): + HTMLParser.__init__(self) + self.data = [] + self.domain = domain + + def handle_starttag(self, tag: str, attrs: str) -> None: + """ + This function parse html to take takes url from tags + """ + # Only parse the 'anchor' tag. + if tag == "a": + # Check the list of defined attributes. + for name, value in attrs: + # If href is defined, and not empty nor # print it. + if name == "href" and value != "#" and value != "": + # If not already in data. + if value not in self.data: + url = parse.urljoin(self.domain, value) + self.data.append(url) + + +# Get main domain name (example.com) +def get_domain_name(url: str) -> str: + """ + This function get the main domain name + + >>> get_domain_name("https://a.b.c.d/e/f?g=h,i=j#k") + 'c.d' + >>> get_domain_name("Not a URL!") + '' + """ + return ".".join(get_sub_domain_name(url).split(".")[-2:]) + + +# Get sub domain name (sub.example.com) +def get_sub_domain_name(url: str) -> str: + """ + >>> get_sub_domain_name("https://a.b.c.d/e/f?g=h,i=j#k") + 'a.b.c.d' + >>> get_sub_domain_name("Not a URL!") + '' + """ + return parse.urlparse(url).netloc + + +def emails_from_url(url: str = "https://github.com") -> list: + """ + This function takes url and return all valid urls + """ + # Get the base domain from the url + domain = get_domain_name(url) + + # Initialize the parser + parser = Parser(domain) + + try: + # Open URL + r = requests.get(url) + + # pass the raw HTML to the parser to get links + parser.feed(r.text) + + # Get links and loop through + valid_emails = set() + for link in parser.data: + # open URL. + # read = requests.get(link) + try: + read = requests.get(link) + # Get the valid email. + emails = re.findall("[a-zA-Z0-9]+@" + domain, read.text) + # If not in list then append it. + for email in emails: + valid_emails.add(email) + except ValueError: + pass + except ValueError: + exit(-1) + + # Finally return a sorted list of email addresses with no duplicates. + return sorted(valid_emails) + + +if __name__ == "__main__": + emails = emails_from_url("https://github.com") + print(f"{len(emails)} emails found:") + print("\n".join(sorted(emails))) diff --git a/web_programming/fetch_bbc_news.py b/web_programming/fetch_bbc_news.py new file mode 100644 index 000000000000..7f8bc57b69f5 --- /dev/null +++ b/web_programming/fetch_bbc_news.py @@ -0,0 +1,17 @@ +# Created by sarathkaul on 12/11/19 + +import requests + +_NEWS_API = "https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=" + + +def fetch_bbc_news(bbc_news_api_key: str) -> None: + # fetching a list of articles in json format + bbc_news_page = requests.get(_NEWS_API + bbc_news_api_key).json() + # each article in the list is a dict + for i, article in enumerate(bbc_news_page["articles"], 1): + print(f"{i}.) {article['title']}") + + +if __name__ == "__main__": + fetch_bbc_news(bbc_news_api_key="") diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py new file mode 100644 index 000000000000..c9198460f211 --- /dev/null +++ b/web_programming/fetch_github_info.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +""" +Created by sarathkaul on 14/11/19 +Updated by lawric1 on 24/11/20 + +Authentication will be made via access token. +To generate your personal access token visit https://github.com/settings/tokens. + +NOTE: +Never hardcode any credential information in the code. Always use an environment +file to store the private information and use the `os` module to get the information +during runtime. + +Create a ".env" file in the root directory and write these two lines in that file +with your token:: + +#!/usr/bin/env bash +export USER_TOKEN="" +""" +import os +from typing import Any, Dict + +import requests + +BASE_URL = "https://api.github.com" + +# https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user +AUTHENTICATED_USER_ENDPOINT = BASE_URL + "/user" + +# https://github.com/settings/tokens +USER_TOKEN = os.environ.get("USER_TOKEN", "") + + +def fetch_github_info(auth_token: str) -> Dict[Any, Any]: + """ + Fetch GitHub info of a user using the requests module + """ + headers = { + "Authorization": f"token {auth_token}", + "Accept": "application/vnd.github.v3+json", + } + return requests.get(AUTHENTICATED_USER_ENDPOINT, headers=headers).json() + + +if __name__ == "__main__": # pragma: no cover + if USER_TOKEN: + for key, value in fetch_github_info(USER_TOKEN).items(): + print(f"{key}: {value}") + else: + raise ValueError("'USER_TOKEN' field cannot be empty.") diff --git a/web_programming/fetch_jobs.py b/web_programming/fetch_jobs.py new file mode 100644 index 000000000000..bb2171e1f0ee --- /dev/null +++ b/web_programming/fetch_jobs.py @@ -0,0 +1,25 @@ +""" +Scraping jobs given job title and location from indeed website +""" +from __future__ import annotations + +from typing import Generator + +import requests +from bs4 import BeautifulSoup + +url = "https://www.indeed.co.in/jobs?q=mobile+app+development&l=" + + +def fetch_jobs(location: str = "mumbai") -> Generator[tuple[str, str], None, None]: + soup = BeautifulSoup(requests.get(url + location).content, "html.parser") + # This attribute finds out all the specifics listed in a job + for job in soup.find_all("div", attrs={"data-tn-component": "organicJob"}): + job_title = job.find("a", attrs={"data-tn-element": "jobTitle"}).text.strip() + company_name = job.find("span", {"class": "company"}).text.strip() + yield job_title, company_name + + +if __name__ == "__main__": + for i, job in enumerate(fetch_jobs("Bangalore"), 1): + print(f"Job {i:>2} is {job[0]} at {job[1]}") diff --git a/web_programming/get_imdb_top_250_movies_csv.py b/web_programming/get_imdb_top_250_movies_csv.py new file mode 100644 index 000000000000..e54b076ebd94 --- /dev/null +++ b/web_programming/get_imdb_top_250_movies_csv.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +import csv + +import requests +from bs4 import BeautifulSoup + + +def get_imdb_top_250_movies(url: str = "") -> dict[str, float]: + url = url or "https://www.imdb.com/chart/top/?ref_=nv_mv_250" + soup = BeautifulSoup(requests.get(url).text, "html.parser") + titles = soup.find_all("td", attrs="titleColumn") + ratings = soup.find_all("td", class_="ratingColumn imdbRating") + return { + title.a.text: float(rating.strong.text) + for title, rating in zip(titles, ratings) + } + + +def write_movies(filename: str = "IMDb_Top_250_Movies.csv") -> None: + movies = get_imdb_top_250_movies() + with open(filename, "w", newline="") as out_file: + writer = csv.writer(out_file) + writer.writerow(["Movie title", "IMDb rating"]) + for title, rating in movies.items(): + writer.writerow([title, rating]) + + +if __name__ == "__main__": + write_movies() diff --git a/web_programming/get_imdbtop.py b/web_programming/get_imdbtop.py new file mode 100644 index 000000000000..669e7f89824b --- /dev/null +++ b/web_programming/get_imdbtop.py @@ -0,0 +1,20 @@ +import requests +from bs4 import BeautifulSoup + + +def imdb_top(imdb_top_n): + base_url = ( + f"https://www.imdb.com/search/title?title_type=" + f"feature&sort=num_votes,desc&count={imdb_top_n}" + ) + source = BeautifulSoup(requests.get(base_url).content, "html.parser") + for m in source.findAll("div", class_="lister-item mode-advanced"): + print("\n" + m.h3.a.text) # movie's name + print(m.find("span", attrs={"class": "genre"}).text) # genre + print(m.strong.text) # movie's rating + print(f"https://www.imdb.com{m.a.get('href')}") # movie's page link + print("*" * 40) + + +if __name__ == "__main__": + imdb_top(input("How many movies would you like to see? ")) diff --git a/web_programming/instagram_crawler.py b/web_programming/instagram_crawler.py new file mode 100644 index 000000000000..4536257a984e --- /dev/null +++ b/web_programming/instagram_crawler.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python3 +from __future__ import annotations + +import json + +import requests +from bs4 import BeautifulSoup +from fake_useragent import UserAgent + +headers = {"UserAgent": UserAgent().random} + + +def extract_user_profile(script) -> dict: + """ + May raise json.decoder.JSONDecodeError + """ + data = script.contents[0] + info = json.loads(data[data.find('{"config"') : -1]) + return info["entry_data"]["ProfilePage"][0]["graphql"]["user"] + + +class InstagramUser: + """ + Class Instagram crawl instagram user information + + Usage: (doctest failing on GitHub Actions) + # >>> instagram_user = InstagramUser("github") + # >>> instagram_user.is_verified + True + # >>> instagram_user.biography + 'Built for developers.' + """ + + def __init__(self, username): + self.url = f"https://www.instagram.com/{username}/" + self.user_data = self.get_json() + + def get_json(self) -> dict: + """ + Return a dict of user information + """ + html = requests.get(self.url, headers=headers).text + scripts = BeautifulSoup(html, "html.parser").find_all("script") + try: + return extract_user_profile(scripts[4]) + except (json.decoder.JSONDecodeError, KeyError): + return extract_user_profile(scripts[3]) + + def __repr__(self) -> str: + return f"{self.__class__.__name__}('{self.username}')" + + def __str__(self) -> str: + return f"{self.fullname} ({self.username}) is {self.biography}" + + @property + def username(self) -> str: + return self.user_data["username"] + + @property + def fullname(self) -> str: + return self.user_data["full_name"] + + @property + def biography(self) -> str: + return self.user_data["biography"] + + @property + def email(self) -> str: + return self.user_data["business_email"] + + @property + def website(self) -> str: + return self.user_data["external_url"] + + @property + def number_of_followers(self) -> int: + return self.user_data["edge_followed_by"]["count"] + + @property + def number_of_followings(self) -> int: + return self.user_data["edge_follow"]["count"] + + @property + def number_of_posts(self) -> int: + return self.user_data["edge_owner_to_timeline_media"]["count"] + + @property + def profile_picture_url(self) -> str: + return self.user_data["profile_pic_url_hd"] + + @property + def is_verified(self) -> bool: + return self.user_data["is_verified"] + + @property + def is_private(self) -> bool: + return self.user_data["is_private"] + + +def test_instagram_user(username: str = "github") -> None: + """ + A self running doctest + >>> test_instagram_user() + """ + import os + + if os.environ.get("CI"): + return None # test failing on GitHub Actions + instagram_user = InstagramUser(username) + assert instagram_user.user_data + assert isinstance(instagram_user.user_data, dict) + assert instagram_user.username == username + if username != "github": + return + assert instagram_user.fullname == "GitHub" + assert instagram_user.biography == "Built for developers." + assert instagram_user.number_of_posts > 150 + assert instagram_user.number_of_followers > 120000 + assert instagram_user.number_of_followings > 15 + assert instagram_user.email == "support@github.com" + assert instagram_user.website == "https://github.com/readme" + assert instagram_user.profile_picture_url.startswith("https://instagram.") + assert instagram_user.is_verified is True + assert instagram_user.is_private is False + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + instagram_user = InstagramUser("github") + print(instagram_user) + print(f"{instagram_user.number_of_posts = }") + print(f"{instagram_user.number_of_followers = }") + print(f"{instagram_user.number_of_followings = }") + print(f"{instagram_user.email = }") + print(f"{instagram_user.website = }") + print(f"{instagram_user.profile_picture_url = }") + print(f"{instagram_user.is_verified = }") + print(f"{instagram_user.is_private = }") diff --git a/web_programming/instagram_pic.py b/web_programming/instagram_pic.py new file mode 100644 index 000000000000..8521da674d7d --- /dev/null +++ b/web_programming/instagram_pic.py @@ -0,0 +1,16 @@ +from datetime import datetime + +import requests +from bs4 import BeautifulSoup + +if __name__ == "__main__": + url = input("Enter image url: ").strip() + print(f"Downloading image from {url} ...") + soup = BeautifulSoup(requests.get(url).content, "html.parser") + # The image URL is in the content field of the first meta tag with property og:image + image_url = soup.find("meta", {"property": "og:image"})["content"] + image_data = requests.get(image_url).content + file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg" + with open(file_name, "wb") as fp: + fp.write(image_data) + print(f"Done. Image saved to disk as {file_name}.") diff --git a/web_programming/instagram_video.py b/web_programming/instagram_video.py new file mode 100644 index 000000000000..243cece1a50e --- /dev/null +++ b/web_programming/instagram_video.py @@ -0,0 +1,17 @@ +from datetime import datetime + +import requests + + +def download_video(url: str) -> bytes: + base_url = "https://downloadgram.net/wp-json/wppress/video-downloader/video?url=" + video_url = requests.get(base_url + url).json()[0]["urls"][0]["src"] + return requests.get(video_url).content + + +if __name__ == "__main__": + url = input("Enter Video/IGTV url: ").strip() + file_name = f"{datetime.now():%Y-%m-%d_%H:%M:%S}.mp4" + with open(file_name, "wb") as fp: + fp.write(download_video(url)) + print(f"Done. Video saved to disk as {file_name}.") diff --git a/web_programming/recaptcha_verification.py b/web_programming/recaptcha_verification.py new file mode 100644 index 000000000000..47c6c42f2ad0 --- /dev/null +++ b/web_programming/recaptcha_verification.py @@ -0,0 +1,66 @@ +""" +Recaptcha is a free captcha service offered by Google in order to secure websites and +forms. At https://www.google.com/recaptcha/admin/create you can create new recaptcha +keys and see the keys that your have already created. +* Keep in mind that recaptcha doesn't work with localhost +When you create a recaptcha key, your will get two separate keys: ClientKey & SecretKey. +ClientKey should be kept in your site's front end +SecretKey should be kept in your site's back end + +# An example HTML login form with recaptcha tag is shown below + +
+

Log in

+ {% csrf_token %} +
+ +
+
+ +
+
+ +
+ +
+
+ + + + +Below a Django function for the views.py file contains a login form for demonstrating +recaptcha verification. +""" +import requests + +try: + from django.contrib.auth import authenticate, login + from django.shortcuts import redirect, render +except ImportError: + authenticate = login = render = redirect = print + + +def login_using_recaptcha(request): + # Enter your recaptcha secret key here + secret_key = "secretKey" + url = "https://www.google.com/recaptcha/api/siteverify" + + # when method is not POST, direct user to login page + if request.method != "POST": + return render(request, "login.html") + + # from the frontend, get username, password, and client_key + username = request.POST.get("username") + password = request.POST.get("password") + client_key = request.POST.get("g-recaptcha-response") + + # post recaptcha response to Google's recaptcha api + response = requests.post(url, data={"secret": secret_key, "response": client_key}) + # if the recaptcha api verified our keys + if response.json().get("success", False): + # authenticate the user + user_in_database = authenticate(request, username=username, password=password) + if user_in_database: + login(request, user_in_database) + return redirect("/your-webpage") + return render(request, "login.html") diff --git a/web_programming/slack_message.py b/web_programming/slack_message.py new file mode 100644 index 000000000000..f35aa3ca587e --- /dev/null +++ b/web_programming/slack_message.py @@ -0,0 +1,19 @@ +# Created by sarathkaul on 12/11/19 + +import requests + + +def send_slack_message(message_body: str, slack_url: str) -> None: + headers = {"Content-Type": "application/json"} + response = requests.post(slack_url, json={"text": message_body}, headers=headers) + if response.status_code != 200: + raise ValueError( + f"Request to slack returned an error {response.status_code}, " + f"the response is:\n{response.text}" + ) + + +if __name__ == "__main__": + # Set the slack url to the one provided by Slack when you create the webhook at + # https://my.slack.com/services/new/incoming-webhook/ + send_slack_message("", "") diff --git a/web_programming/test_fetch_github_info.py b/web_programming/test_fetch_github_info.py new file mode 100644 index 000000000000..2da97c782df7 --- /dev/null +++ b/web_programming/test_fetch_github_info.py @@ -0,0 +1,27 @@ +import json + +import requests + +from .fetch_github_info import AUTHENTICATED_USER_ENDPOINT, fetch_github_info + + +def test_fetch_github_info(monkeypatch): + class FakeResponse: + def __init__(self, content) -> None: + assert isinstance(content, (bytes, str)) + self.content = content + + def json(self): + return json.loads(self.content) + + def mock_response(*args, **kwargs): + assert args[0] == AUTHENTICATED_USER_ENDPOINT + assert "Authorization" in kwargs["headers"] + assert kwargs["headers"]["Authorization"].startswith("token ") + assert "Accept" in kwargs["headers"] + return FakeResponse(b'{"login":"test","id":1}') + + monkeypatch.setattr(requests, "get", mock_response) + result = fetch_github_info("token") + assert result["login"] == "test" + assert result["id"] == 1 diff --git a/web_programming/world_covid19_stats.py b/web_programming/world_covid19_stats.py new file mode 100644 index 000000000000..1dd1ff6d188e --- /dev/null +++ b/web_programming/world_covid19_stats.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +""" +Provide the current worldwide COVID-19 statistics. +This data is being scrapped from 'https://www.worldometers.info/coronavirus/'. +""" + +import requests +from bs4 import BeautifulSoup + + +def world_covid19_stats(url: str = "https://www.worldometers.info/coronavirus") -> dict: + """ + Return a dict of current worldwide COVID-19 statistics + """ + soup = BeautifulSoup(requests.get(url).text, "html.parser") + keys = soup.findAll("h1") + values = soup.findAll("div", {"class": "maincounter-number"}) + keys += soup.findAll("span", {"class": "panel-title"}) + values += soup.findAll("div", {"class": "number-table-main"}) + return {key.text.strip(): value.text.strip() for key, value in zip(keys, values)} + + +if __name__ == "__main__": + print("\033[1m" + "COVID-19 Status of the World" + "\033[0m\n") + for key, value in world_covid19_stats().items(): + print(f"{key}\n{value}\n")