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/.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..fe51e49e4707 --- /dev/null +++ b/.github/stale.yml @@ -0,0 +1,20 @@ +# Number of days of inactivity before an issue becomes stale +daysUntilStale: 30 +# Number of days of inactivity before a stale issue is closed +daysUntilClose: 7 +# Issues with these labels will never be considered stale +exemptLabels: + - bug + - help wanted + - OK to merge +# Label to use when marking an issue as stale +staleLabel: wontfix +# Comment to post when marking an issue 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. Set to `false` to disable +closeComment: > + Please reopen this issue once you commit the changes requested or + make improvements on the code. Thank you for your contributions. diff --git a/.github/workflows/autoblack.yml b/.github/workflows/autoblack.yml new file mode 100644 index 000000000000..44249f78725c --- /dev/null +++ b/.github/workflows/autoblack.yml @@ -0,0 +1,24 @@ +# GitHub Action that uses Black to reformat Python code (if needed) when doing a git push. +# If all Python code in the repo is compliant with Black then this Action does nothing. +# Otherwise, Black is run and its changes are committed to the repo. +# https://github.com/cclauss/autoblack + +name: autoblack_push +on: [push] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 # Use v1, NOT v2 + - uses: actions/setup-python@v2 + - run: pip install black + - run: black --check . + - name: If needed, commit black changes to a new pull request + if: failure() + run: | + black . + 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 + git commit -am "fixup! Format Python code with psf/black push" + git push --force origin HEAD:$GITHUB_REF diff --git a/.github/workflows/codespell.yml b/.github/workflows/codespell.yml new file mode 100644 index 000000000000..010adffa9053 --- /dev/null +++ b/.github/workflows/codespell.yml @@ -0,0 +1,14 @@ +# GitHub Action to automate the identification of common misspellings in text files +# https://github.com/codespell-project/codespell +name: codespell +on: [push, pull_request] +jobs: + codespell: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - run: pip install codespell flake8 + - run: | + SKIP="./.*,./other/dictionary.txt,./other/words,./project_euler/problem_22/p022_names.txt" + codespell -L ans,fo,hist,iff,secant,tim --skip=$SKIP --quiet-level=2 diff --git a/.github/workflows/directory_writer.yml b/.github/workflows/directory_writer.yml new file mode 100644 index 000000000000..6547d1c18c79 --- /dev/null +++ b/.github/workflows/directory_writer.yml @@ -0,0 +1,23 @@ +# 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 + with: + python-version: 3.x + - 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/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 000000000000..4793f54f7af8 --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,23 @@ +name: Mark stale issues and pull requests +on: + schedule: + - cron: "0 0 * * *" +jobs: + stale: + runs-on: ubuntu-latest + steps: + - uses: actions/stale@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + stale-issue-message: > + 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! + stale-pr-message: > + 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! + stale-issue-label: 'no-issue-activity' + stale-pr-label: 'no-pr-activity' diff --git a/.gitignore b/.gitignore index 96422cb2ec94..574cdf312836 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,6 @@ __pycache__/ # Distribution / packaging .Python -env/ build/ develop-eggs/ dist/ @@ -20,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 @@ -42,8 +43,9 @@ htmlcov/ .cache nosetests.xml coverage.xml -*,cover +*.cover .hypothesis/ +.pytest_cache/ # Translations *.mo @@ -52,6 +54,7 @@ coverage.xml # Django stuff: *.log local_settings.py +db.sqlite3 # Flask stuff: instance/ @@ -66,7 +69,7 @@ docs/_build/ # PyBuilder target/ -# IPython Notebook +# Jupyter Notebook .ipynb_checkpoints # pyenv @@ -75,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/.travis.yml b/.travis.yml index 5fba6987bb66..22eea20c727e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,26 +1,17 @@ +os: linux +dist: focal language: python +python: 3.8 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_install: pip install --upgrade pip setuptools six +install: pip install -r requirements.txt 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 + - black --check . || true + - IGNORE=E123,E203,E265,E266,E302,E401,E402,E712,E731,E741,E743,F811,F841,W291,W293,W503 + - flake8 . --count --ignore=$IGNORE --max-complexity=25 --max-line-length=127 --show-source --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 + - scripts/validate_filenames.py # no uppercase, no spaces, in a directory + - mypy --ignore-missing-imports . + - pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=. . +after_success: + - scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md diff --git a/ArithmeticAnalysis/Intersection.py b/ArithmeticAnalysis/Intersection.py deleted file mode 100644 index 22c50f2ecafd..000000000000 --- a/ArithmeticAnalysis/Intersection.py +++ /dev/null @@ -1,16 +0,0 @@ -import math - -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 - 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)<0.00001 : - return x_n2 - x_n=x_n1 - x_n1=x_n2 - -def f(x): - return math.pow(x,3)-2*x-5 - -print(intersection(f,3,3.5)) diff --git a/ArithmeticAnalysis/LUdecomposition.py b/ArithmeticAnalysis/LUdecomposition.py deleted file mode 100644 index bb0827bb39b1..000000000000 --- a/ArithmeticAnalysis/LUdecomposition.py +++ /dev/null @@ -1,34 +0,0 @@ -import math -import numpy - -def LUDecompose (table): #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 - - - - - - - -matrix =numpy.array([[2,-2,1],[0,1,2],[5,3,1]]) -L,U = LUDecompose(matrix) -print(L) -print(U) \ No newline at end of file diff --git a/ArithmeticAnalysis/NewtonMethod.py b/ArithmeticAnalysis/NewtonMethod.py deleted file mode 100644 index c3d5efb47d01..000000000000 --- a/ArithmeticAnalysis/NewtonMethod.py +++ /dev/null @@ -1,15 +0,0 @@ -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)<0.00001: - return x_n1 - x_n=x_n1 - -def f(x): - return (x**3)-2*x-5 - -def f1(x): - return 3*(x**2)-2 - -print(newton(f,f1,3)) diff --git a/ArithmeticAnalysis/NewtonRaphsonMethod.py b/ArithmeticAnalysis/NewtonRaphsonMethod.py deleted file mode 100644 index 40501134e28a..000000000000 --- a/ArithmeticAnalysis/NewtonRaphsonMethod.py +++ /dev/null @@ -1,38 +0,0 @@ -# Implementing Newton Raphson method in python -# Author: Haseeb - -from sympy import diff -from decimal import Decimal -from math import sin, cos, exp - -def NewtonRaphson(func, a): - ''' Finds root from the point 'a' onwards by Newton-Raphson method ''' - while True: - x = a - c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) ) - - x = c - 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 trignometric fucntion - # 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/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000000..dd4295404f78 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,167 @@ +# 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) 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. + +**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. + +#### 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+. __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 the 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 + pip3 install black # only required the first time + black . + ``` + +- All submissions will need to pass the test __flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics__ before they will be accepted so if possible, try this test locally on your Python file(s) before submitting your pull request. + + ```bash + pip3 install flake8 # only required the first time + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + ``` + +- 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 Standard While Submitting Your Work + +- File extension for code should be `.py`. Jupyter notebook files are acceptable in machine learning algorithms. +- 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..711d0a9e972f --- /dev/null +++ b/DIRECTORY.md @@ -0,0 +1,642 @@ + +## 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) + * [Minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py) + * [N Queens](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens.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) + +## 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 + * [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 Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base64_cipher.py) + * [Base85](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base85.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) + * [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) + * [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) + * [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) + * [Peak Signal To Noise Ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py) + +## Conversions + * [Decimal To Binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.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) + * [Roman To Integer](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_to_integer.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) + * [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) + * Data Structures + * Heap + * [Heap Generic](https://github.com/TheAlgorithms/Python/blob/master/data_structures/data_structures/heap/heap_generic.py) + * Disjoint Set + * [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) + * [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) + * 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) + * [From Sequence](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/from_sequence.py) + * [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/is_palindrome.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) + * [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) + * [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 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) + * [Inversions](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/inversions.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) + * [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 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 Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_partition.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) + +## 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) + +## Fuzzy Logic + * [Fuzzy Operations](https://github.com/TheAlgorithms/Python/blob/master/fuzzy_logic/fuzzy_operations.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) + +## 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](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs.py) + * [Bfs Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs_shortest_path.py) + * [Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search.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) + * [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) + * [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) + * [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) + * [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py) + * [Minimum Spanning Tree Prims](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_prims.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) + * [Tarjans Scc](https://github.com/TheAlgorithms/Python/blob/master/graphs/tarjans_scc.py) + +## Hashes + * [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.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) + * [Sha1](https://github.com/TheAlgorithms/Python/blob/master/hashes/sha1.py) + +## Linear Algebra + * Src + * [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) + * [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) + +## Machine Learning + * [Astar](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/astar.py) + * [Decision Tree](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/decision_tree.py) + * [Gaussian Naive Bayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gaussian_naive_bayes.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) + * Lstm + * [Lstm Prediction](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/lstm/lstm_prediction.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) + * [Support Vector Machines](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/support_vector_machines.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 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) + * [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) + * [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) + * [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) + * [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) + * [Numerical Integration](https://github.com/TheAlgorithms/Python/blob/master/maths/numerical_integration.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) + * [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) + * [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) + * [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) + * [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 + * [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 + * [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) + * [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) + * [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) + * [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) + * [Linear Congruential Generator](https://github.com/TheAlgorithms/Python/blob/master/other/linear_congruential_generator.py) + * [Magicdiamondpattern](https://github.com/TheAlgorithms/Python/blob/master/other/magicdiamondpattern.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) + * [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) + * [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 01 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol3.py) + * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol4.py) + * [Sol5](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol5.py) + * [Sol6](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol6.py) + * [Sol7](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_01/sol7.py) + * Problem 02 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_02/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_02/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_02/sol3.py) + * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_02/sol4.py) + * [Sol5](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_02/sol5.py) + * Problem 03 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_03/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_03/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_03/sol3.py) + * Problem 04 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_04/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_04/sol2.py) + * Problem 05 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_05/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_05/sol2.py) + * Problem 06 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol3.py) + * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_06/sol4.py) + * Problem 07 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_07/sol3.py) + * Problem 08 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_08/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_08/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_08/sol3.py) + * Problem 09 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_09/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_09/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_09/sol3.py) + * Problem 10 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_10/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_10/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_10/sol3.py) + * Problem 11 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_11/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_11/sol2.py) + * Problem 12 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol2.py) + * Problem 13 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_13/sol1.py) + * Problem 14 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_14/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_14/sol2.py) + * Problem 15 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_15/sol1.py) + * Problem 16 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_16/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_16/sol2.py) + * Problem 17 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_17/sol1.py) + * Problem 18 + * [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_18/solution.py) + * Problem 19 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_19/sol1.py) + * Problem 20 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol3.py) + * [Sol4](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol4.py) + * Problem 21 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_21/sol1.py) + * Problem 22 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_22/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_22/sol2.py) + * Problem 23 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_23/sol1.py) + * Problem 234 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_234/sol1.py) + * Problem 24 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_24/sol1.py) + * Problem 25 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_25/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_25/sol2.py) + * [Sol3](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_25/sol3.py) + * Problem 26 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_26/sol1.py) + * Problem 27 + * [Problem 27 Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_27/problem_27_sol1.py) + * Problem 28 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_28/sol1.py) + * Problem 29 + * [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_29/solution.py) + * Problem 30 + * [Soln](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/soln.py) + * Problem 31 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol2.py) + * Problem 32 + * [Sol32](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_32/sol32.py) + * Problem 33 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_33/sol1.py) + * Problem 36 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_36/sol1.py) + * Problem 40 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_40/sol1.py) + * Problem 42 + * [Solution42](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_42/solution42.py) + * Problem 48 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_48/sol1.py) + * Problem 52 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_52/sol1.py) + * Problem 53 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_53/sol1.py) + * Problem 551 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py) + * Problem 56 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_56/sol1.py) + * Problem 67 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_67/sol1.py) + * Problem 76 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py) + * Problem 99 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_99/sol1.py) + +## Scheduling + * [First Come First Served](https://github.com/TheAlgorithms/Python/blob/master/scheduling/first_come_first_served.py) + * [Shortest Job First Algorithm](https://github.com/TheAlgorithms/Python/blob/master/scheduling/shortest_job_first_algorithm.py) + +## Searches + * [Binary Search](https://github.com/TheAlgorithms/Python/blob/master/searches/binary_search.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 + * [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) + * [I Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/i_sort.py) + * [Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/insertion_sort.py) + * [Iterative Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/iterative_merge_sort.py) + * [Merge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/merge_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) + * [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-Quick-Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive-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) + * [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) + * [Sleep Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/sleep_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) + * [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) + * [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) + * [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 Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py) + * [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.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) + +## Traversals + * [Binary Tree Traversals](https://github.com/TheAlgorithms/Python/blob/master/traversals/binary_tree_traversals.py) + +## Web Programming + * [Crawl Google Results](https://github.com/TheAlgorithms/Python/blob/master/web_programming/crawl_google_results.py) + * [Current Stock Price](https://github.com/TheAlgorithms/Python/blob/master/web_programming/current_stock_price.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) + * [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) + * [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py) diff --git a/File_Transfer_Protocol/ftp_client_server.py b/File_Transfer_Protocol/ftp_client_server.py deleted file mode 100644 index f73f858431f3..000000000000 --- a/File_Transfer_Protocol/ftp_client_server.py +++ /dev/null @@ -1,58 +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' - f = open(filename, 'rb') - in_data = f.read(1024) - while (in_data): - conn.send(in_data) - print('Sent ', repr(in_data)) - in_data = f.read(1024) - f.close() - - 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 d4919158a02e..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 """ - LocalFile = open(FileName, 'wb') - ftp.retrbinary('RETR ' + FileName, LocalFile.write, 1024) - ftp.quit() - LocalFile.close() - -""" - 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 """ - ftp.storbinary('STOR ' + FileName, open(FileName, 'rb')) - ftp.quit() diff --git a/Graphs/minimum_spanning_tree_kruskal.py b/Graphs/minimum_spanning_tree_kruskal.py deleted file mode 100644 index 81d64f421a31..000000000000 --- a/Graphs/minimum_spanning_tree_kruskal.py +++ /dev/null @@ -1,32 +0,0 @@ -from __future__ import print_function -num_nodes, num_edges = list(map(int,input().split())) - -edges = [] - -for i in range(num_edges): - node1, node2, cost = list(map(int,input().split())) - edges.append((i,node1,node2,cost)) - -edges = sorted(edges, key=lambda edge: edge[3]) - -parent = [i for i in range(num_nodes)] - -def find_parent(i): - if(i != parent[i]): - parent[i] = find_parent(parent[i]) - return parent[i] - -minimum_spanning_tree_cost = 0 -minimum_spanning_tree = [] - -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 - -print(minimum_spanning_tree_cost) -for edge in minimum_spanning_tree: - print(edge) diff --git a/Graphs/scc_kosaraju.py b/Graphs/scc_kosaraju.py deleted file mode 100644 index 1f13ebaba36b..000000000000 --- a/Graphs/scc_kosaraju.py +++ /dev/null @@ -1,46 +0,0 @@ -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 - 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 - 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 - for i in stack[::-1]: - if visit[i]: continue - component = [] - dfs2(i) - scc.append(component) - return scc - -print(kosaraju()) 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/Maths/ModularExponential.py b/Maths/ModularExponential.py deleted file mode 100644 index b3f4c00bd5d8..000000000000 --- a/Maths/ModularExponential.py +++ /dev/null @@ -1,20 +0,0 @@ -def modularExponential(base, power, mod): - 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)) - - -if __name__ == '__main__': - main() diff --git a/Maths/SimpsonRule.py b/Maths/SimpsonRule.py deleted file mode 100644 index 091c86c17f1b..000000000000 --- a/Maths/SimpsonRule.py +++ /dev/null @@ -1,49 +0,0 @@ - -''' -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' - -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 - -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() diff --git a/Maths/TrapezoidalRule.py b/Maths/TrapezoidalRule.py deleted file mode 100644 index 52310c1ed3b0..000000000000 --- a/Maths/TrapezoidalRule.py +++ /dev/null @@ -1,46 +0,0 @@ -''' -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' - -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 - -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() diff --git a/Multi_Hueristic_Astar.py b/Multi_Hueristic_Astar.py deleted file mode 100644 index 7fbd2ff04542..000000000000 --- a/Multi_Hueristic_Astar.py +++ /dev/null @@ -1,268 +0,0 @@ -from __future__ import print_function -import heapq -import numpy as np -import math -import copy - -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/Neural_Network/convolution_neural_network.py b/Neural_Network/convolution_neural_network.py deleted file mode 100644 index 0dca2bc485d1..000000000000 --- a/Neural_Network/convolution_neural_network.py +++ /dev/null @@ -1,306 +0,0 @@ -#-*- coding: utf-8 -*- - -''' - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - Name - - CNN - Convolution Neural Network For Photo Recognizing - Goal - - Recognize Handing Writting Word Photo - Detail:Total 5 layers neural network - * Convolution layer - * Pooling layer - * Input layer layer of BP - * Hiden layer of BP - * Output layer of BP - Author: Stephen Lee - Github: 245885195@qq.com - Date: 2017.9.20 - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - ''' -from __future__ import print_function - -import numpy as np -import matplotlib.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): - ''' - :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 - :param bp_num2: units number of hidden layer - :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 - self.conv1 = conv1_get[:2] - self.step_conv1 = conv1_get[2] - 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.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 - - - 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: - pickle.dump(model_dic, f) - - print('Model saved: %s'% save_path) - - @classmethod - def ReadModel(cls,model_path): - #read saved model - import pickle - 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') - return conv_ins - - - def sig(self,x): - return 1 / (1 + np.exp(-1*x)) - - def do_round(self,x): - return round(x, 3) - - def convolute(self,data,convs,w_convs,thre_convs,conv_step): - #convolution process - size_conv = convs[0] - num_conv =convs[1] - size_data = np.shape(data)[0] - #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] - data_focus.append(focus) - #caculate 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] - featuremap.append(self.sig(net_focus)) - featuremap = np.asmatrix(featuremap).reshape(Size_FeatureMap, Size_FeatureMap) - data_featuremap.append(featuremap) - - #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 - - def pooling(self,featuremaps,size_pooling,type='average_pool'): - #pooling process - size_map = len(featuremaps[0]) - 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 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 - map_pooled.append(np.average(focus)) - elif type == 'max_pooling': - #max pooling - map_pooled.append(np.max(focus)) - 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 - data_expanded = [] - for i in range(len(datas)): - shapes = np.shape(datas[i]) - data_listed = datas[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 - data_mat = np.asarray(data_mat) - shapes = np.shape(data_mat) - 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 - 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] - i_pool = i_pool + 1 - 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))) - rp = 0 - all_mse = [] - mse = 10000 - while rp < n_repeat and mse >= error_accuracy: - alle = 0 - print('-------------Learning Time %d--------------'%rp) - for p in range(len(datas_train)): - #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) - 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_out2 = self.sig(bp_net_j) - 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) - - 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 - 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) - - 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.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) - rp = rp + 1 - mse = alle/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.grid(True, alpha=0.5) - plt.show() - print('------------------Training Complished---------------------') - print((' - - Training epoch: ', rp, ' - - Mse: %.6f' % mse)) - if draw_e: - draw_error() - return mse - - def predict(self,datas_test): - #model predict - produce_out = [] - 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_pooled1 = self.pooling(data_conved1, self.size_pooling1) - data_bp_input = self._expand(data_pooled1) - - bp_out1 = data_bp_input - bp_net_j = bp_out1 * self.vji.T - self.thre_bp2 - bp_out2 = self.sig(bp_net_j) - 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] - return np.asarray(res) - - 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_pooled1 = self.pooling(data_conved1, self.size_pooling1) - - return data_conved1,data_pooled1 - - -if __name__ == '__main__': - pass - ''' - I will put the example on other file - ''' \ No newline at end of file diff --git a/Neural_Network/perceptron.py b/Neural_Network/perceptron.py deleted file mode 100644 index 8ac3e8fc69e9..000000000000 --- a/Neural_Network/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) \ No newline at end of file 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 02/sol1.py b/Project Euler/Problem 02/sol1.py deleted file mode 100644 index f8257fb615fb..000000000000 --- a/Project Euler/Problem 02/sol1.py +++ /dev/null @@ -1,26 +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&1)==0): #can also use (j%2==0) - sum+=j - temp=i - i=j - j=temp+i -print(sum) diff --git a/Project Euler/Problem 02/sol2.py b/Project Euler/Problem 02/sol2.py deleted file mode 100644 index 9bbd0c535d63..000000000000 --- a/Project Euler/Problem 02/sol2.py +++ /dev/null @@ -1,12 +0,0 @@ -def fib(n): - a, b, s = 0, 1, 0 - while b < n: - if b % 2 == 0 and b < n: s += b - a, b = b, a+b - ls.append(s) - -T = int(input().strip()) -ls = [] -for _ in range(T): - fib(int(input().strip())) -print(*ls, sep = '\n') diff --git a/Project Euler/Problem 02/sol3.py b/Project Euler/Problem 02/sol3.py deleted file mode 100644 index d36b741bb4f9..000000000000 --- a/Project Euler/Problem 02/sol3.py +++ /dev/null @@ -1,20 +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+a1): - prime=n -print(prime) diff --git a/Project Euler/Problem 04/sol1.py b/Project Euler/Problem 04/sol1.py deleted file mode 100644 index 77135ec7fc1a..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 \ No newline at end of file diff --git a/Project Euler/Problem 04/sol2.py b/Project Euler/Problem 04/sol2.py deleted file mode 100644 index e27e7d30471e..000000000000 --- a/Project Euler/Problem 04/sol2.py +++ /dev/null @@ -1,19 +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 -arr = [] -for i in range(999,100,-1): - for j in range(999,100,-1): - t = str(i*j) - if t == t[::-1]: - arr.append(i*j) -arr.sort() - -n=int(input()) -for i in arr[::-1]: - if(i LargestProduct: - LargestProduct = product - print(LargestProduct) - - -if __name__ == '__main__': - main() 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 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 12/sol1.py b/Project Euler/Problem 12/sol1.py deleted file mode 100644 index 9c4483fd62e5..000000000000 --- a/Project Euler/Problem 12/sol1.py +++ /dev/null @@ -1,46 +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 - - return nDivisors - -tNum = 1 -i = 1 - -while True: - i += 1 - tNum += i - - if count_divisors(tNum) > 500: - break - -print(tNum) \ No newline at end of file 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 14/sol1.py b/Project Euler/Problem 14/sol1.py deleted file mode 100644 index 9037f6eb8bd5..000000000000 --- a/Project Euler/Problem 14/sol1.py +++ /dev/null @@ -1,21 +0,0 @@ -from __future__ import print_function -largest_number = 0 -pre_counter = 0 - -for input1 in range(750000,1000000): - 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 - -print(('Largest Number:',largest_number,'->',pre_counter,'digits')) diff --git a/Project Euler/Problem 15/sol1.py b/Project Euler/Problem 15/sol1.py deleted file mode 100644 index 9b61b37d2081..000000000000 --- a/Project Euler/Problem 15/sol1.py +++ /dev/null @@ -1,20 +0,0 @@ -from __future__ import print_function -from math import factorial, ceil - -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 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 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 21/sol1.py b/Project Euler/Problem 21/sol1.py deleted file mode 100644 index 6d137a7d4332..000000000000 --- a/Project Euler/Problem 21/sol1.py +++ /dev/null @@ -1,42 +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 - -sums = [] -total = 0 - -for i in xrange(1, 10000): - n = sum_of_divisors(i) - - if n < len(sums): - if sums[n-1] == i: - total += n + i - - sums.append(n) - -print(total) \ No newline at end of file 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 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 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 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 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/Project Euler/Problem 9/sol1.py b/Project Euler/Problem 9/sol1.py deleted file mode 100644 index e54c543b4721..000000000000 --- a/Project Euler/Problem 9/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 9/sol2.py b/Project Euler/Problem 9/sol2.py deleted file mode 100644 index 13674d25875e..000000000000 --- a/Project Euler/Problem 9/sol2.py +++ /dev/null @@ -1,19 +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 -import sys - -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/README.md b/Project Euler/README.md deleted file mode 100644 index 9f77f719f0f1..000000000000 --- a/Project Euler/README.md +++ /dev/null @@ -1,58 +0,0 @@ -# ProjectEuler - -Problems are taken from https://projecteuler.net/. - -Project Euler is a series of challenging mathematical/computer programming problems that will 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! diff --git a/README.md b/README.md index bc31d5389dfd..106f5907eebf 100644 --- a/README.md +++ b/README.md @@ -1,174 +1,25 @@ -# The Algorithms - Python +# The Algorithms - Python +[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](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)  +[![Build Status](https://img.shields.io/travis/TheAlgorithms/Python.svg?label=Travis%20CI&logo=travis&style=flat-square)](https://travis-ci.com/TheAlgorithms/Python)  +[![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)  + ### All algorithms implemented in Python (for education) -These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons. +These implementations are for learning purposes. They may be less efficient than the implementations in the Python standard library. -## Sort Algorithms +## Contribution Guidelines +Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute. -### Bubble -![alt text][bubble-image] +## Community Channel -From [Wikipedia][bubble-wiki]: 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. +We're on [Gitter](https://gitter.im/TheAlgorithms)! Please join us. -__Properties__ -* Worst case performance O(n^2) -* Best case performance O(n) -* Average case performance O(n^2) +## List of Algorithms -###### View the algorithm in [action][bubble-toptal] - - - -### Insertion -![alt text][insertion-image] - -From [Wikipedia][insertion-wiki]: 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(n^2) -* Best case performance O(n) -* Average case performance O(n^2) - -###### View the algorithm in [action][insertion-toptal] - - -### Merge -![alt text][merge-image] - -From [Wikipedia][merge-wiki]: In computer science, 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) - - -###### View the algorithm in [action][merge-toptal] - -### Quick -![alt text][quick-image] - -From [Wikipedia][quick-wiki]: 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(n^2) -* Best case performance O(n log n) or O(n) with three-way partition -* Average case performance O(n log n) - -###### View the algorithm in [action][quick-toptal] - -### Selection -![alt text][selection-image] - -From [Wikipedia][selection-wiki]: The algorithm 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(n^2) -* Best case performance O(n^2) -* Average case performance O(n^2) - -###### View the algorithm in [action][selection-toptal] - -### Shell -![alt text][shell-image] - -From [Wikipedia][shell-wiki]: 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(nlog2 2n) -* Best case performance O(n log n) -* Average case performance depends on gap sequence - -###### View the algorithm in [action][shell-toptal] - -### 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) - ----------------------------------------------------------------------------------- - -## Search Algorithms - -### Linear -![alt text][linear-image] - -From [Wikipedia][linear-wiki]: 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 - -### Binary -![alt text][binary-image] - -From [Wikipedia][binary-wiki]: 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) - ----------------------------------------------------------------------------------------------------------------------- - -## Ciphers - -### Caesar -![alt text][caesar]
-In cryptography, a **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 -The **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 -In cryptography, a **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) - -[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" - -[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" - -[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" - -[linear-wiki]: https://en.wikipedia.org/wiki/Linear_search -[linear-image]: http://www.tutorialspoint.com/data_structures_algorithms/images/linear_search.gif - -[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 - - -[caesar]: https://upload.wikimedia.org/wikipedia/commons/4/4a/Caesar_cipher_left_shift_of_3.svg +See our [directory](DIRECTORY.md). diff --git a/Travis_CI_tests_are_failing.md b/Travis_CI_tests_are_failing.md new file mode 100644 index 000000000000..10bf5a6655d2 --- /dev/null +++ b/Travis_CI_tests_are_failing.md @@ -0,0 +1,9 @@ +# Travis CI test are failing +### How do I find out what is wrong with my pull request? +1. In your PR look for the failing test and click the `Details` link: ![Travis_CI_fail_1.png](images/Travis_CI_fail_1.png) +2. On the next page, click `The build failed` link: ![Travis_CI_fail_2.png](images/Travis_CI_fail_2.png) +3. Now scroll down and look for `red` text describing the error(s) in the test log. + +Pull requests will __not__ be merged if the Travis CI tests are failing. + +If anything is unclear, please read through [CONTRIBUTING.md](CONTRIBUTING.md) and attempt to run the failing tests on your computer before asking for assistance. diff --git a/ArithmeticAnalysis/Bisection.py b/arithmetic_analysis/bisection.py similarity index 50% rename from ArithmeticAnalysis/Bisection.py rename to arithmetic_analysis/bisection.py index a6af547db03b..78582b025880 100644 --- a/ArithmeticAnalysis/Bisection.py +++ b/arithmetic_analysis/bisection.py @@ -1,7 +1,9 @@ import math -def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano +def bisection( + function, a, b +): # finds where the function becomes 0 in [a,b] using bolzano start = a end = b @@ -9,25 +11,28 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us 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, + 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 else: - mid = (start + end) / 2 - while abs(start - mid) > 0.0000001: # until we achieve precise equals to 10^-7 + mid = start + (end - start) / 2.0 + while abs(start - mid) > 10 ** -7: # until we achieve precise 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 + return math.pow(x, 3) - 2 * x - 5 -print(bisection(f, 1, 1000)) +if __name__ == "__main__": + print(bisection(f, 1, 1000)) 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/in_static_equilibrium.py b/arithmetic_analysis/in_static_equilibrium.py new file mode 100644 index 000000000000..addaff888f7f --- /dev/null +++ b/arithmetic_analysis/in_static_equilibrium.py @@ -0,0 +1,86 @@ +""" +Checks if a system of forces is in static equilibrium. + +python/black : true +flake8 : passed +mypy : passed +""" + +from numpy import array, cos, sin, radians, cross # type: ignore +from typing import List + + +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 new file mode 100644 index 000000000000..0fdcfbf1943e --- /dev/null +++ b/arithmetic_analysis/intersection.py @@ -0,0 +1,24 @@ +import math + + +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 + 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: + return x_n2 + x_n = x_n1 + x_n1 = x_n2 + + +def f(x): + return math.pow(x, 3) - (2 * x) - 5 + + +if __name__ == "__main__": + print(intersection(f, 3, 3.5)) diff --git a/arithmetic_analysis/lu_decomposition.py b/arithmetic_analysis/lu_decomposition.py new file mode 100644 index 000000000000..4372621d74cb --- /dev/null +++ b/arithmetic_analysis/lu_decomposition.py @@ -0,0 +1,34 @@ +"""Lower-Upper (LU) Decomposition.""" + +# lower–upper (LU) decomposition - https://en.wikipedia.org/wiki/LU_decomposition +import numpy + + +def LUDecompose(table): + # 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 + + +if __name__ == "__main__": + matrix = numpy.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]]) + L, U = LUDecompose(matrix) + print(L) + print(U) diff --git a/arithmetic_analysis/newton_forward_interpolation.py b/arithmetic_analysis/newton_forward_interpolation.py new file mode 100644 index 000000000000..d91b9709f3d6 --- /dev/null +++ b/arithmetic_analysis/newton_forward_interpolation.py @@ -0,0 +1,55 @@ +# https://www.geeksforgeeks.org/newton-forward-backward-interpolation/ + +import math + +# for calculating u value +def ucal(u, p): + """ + >>> 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(): + n = int(input("enter the numbers of values: ")) + y = [] + 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 new file mode 100644 index 000000000000..1408a983041d --- /dev/null +++ b/arithmetic_analysis/newton_method.py @@ -0,0 +1,25 @@ +"""Newton's Method.""" + +# Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method + + +# function is the f(x) and function1 is the f'(x) +def newton(function, function1, startingInt): + 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): + return 3 * (x ** 2) - 2 + + +if __name__ == "__main__": + 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..a3e8bbf0fc21 --- /dev/null +++ b/arithmetic_analysis/newton_raphson.py @@ -0,0 +1,40 @@ +# 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 sympy import diff + + +def newton_raphson(func: str, a: int, precision: int = 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/secant_method.py b/arithmetic_analysis/secant_method.py new file mode 100644 index 000000000000..b05d44c627d8 --- /dev/null +++ b/arithmetic_analysis/secant_method.py @@ -0,0 +1,28 @@ +# Implementing Secant method in Python +# Author: dimgrichr + + +from math import exp + + +def f(x): + """ + >>> f(5) + 39.98652410600183 + """ + return 8 * x - 2 * exp(-x) + + +def SecantMethod(lower_bound, upper_bound, repeats): + """ + >>> SecantMethod(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 + + +print(f"The solution is: {SecantMethod(1, 3, 2)}") diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py new file mode 100644 index 000000000000..854dc5198422 --- /dev/null +++ b/backtracking/all_combinations.py @@ -0,0 +1,39 @@ +""" + 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)!))) +""" + + +def generate_all_combinations(n: int, k: int) -> [[int]]: + """ + >>> generate_all_combinations(n=4, k=2) + [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] + """ + + result = [] + create_all_state(1, n, k, [], result) + return result + + +def create_all_state(increment, total_number, level, current_list, total_list): + 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): + 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..d144436033de --- /dev/null +++ b/backtracking/all_permutations.py @@ -0,0 +1,45 @@ +""" + 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. +""" + + +def generate_all_permutations(sequence): + create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) + + +def create_state_space_tree(sequence, current_sequence, index, index_used): + """ + 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 = [3, 1, 2, 4] +generate_all_permutations(sequence) + +sequence = ["A", "B", "C"] +generate_all_permutations(sequence) diff --git a/backtracking/all_subsequences.py b/backtracking/all_subsequences.py new file mode 100644 index 000000000000..8283386991d9 --- /dev/null +++ b/backtracking/all_subsequences.py @@ -0,0 +1,42 @@ +""" + 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. +""" + + +def generate_all_subsequences(sequence): + create_state_space_tree(sequence, [], 0) + + +def create_state_space_tree(sequence, current_subsequence, index): + """ + 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() + + +""" +remove the comment to take an input from the user + +print("Enter the elements") +sequence = list(map(int, input().split())) +""" + +sequence = [3, 1, 2, 4] +generate_all_subsequences(sequence) + +sequence = ["A", "B", "C"] +generate_all_subsequences(sequence) diff --git a/backtracking/coloring.py b/backtracking/coloring.py new file mode 100644 index 000000000000..77beb5fc1956 --- /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..e4f2c62d2341 --- /dev/null +++ b/backtracking/hamiltonian_cycle.py @@ -0,0 +1,175 @@ +""" + 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. Chceck 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/minimax.py b/backtracking/minimax.py new file mode 100644 index 000000000000..af07b8d8171a --- /dev/null +++ b/backtracking/minimax.py @@ -0,0 +1,34 @@ +import math + +""" 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 +""" + + +def minimax(Depth, nodeIndex, isMax, scores, height): + + if Depth == height: + return scores[nodeIndex] + + if isMax: + return max( + minimax(Depth + 1, nodeIndex * 2, False, scores, height), + minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height), + ) + return min( + minimax(Depth + 1, nodeIndex * 2, True, scores, height), + minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height), + ) + + +if __name__ == "__main__": + + 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)) diff --git a/backtracking/n_queens.py b/backtracking/n_queens.py new file mode 100644 index 000000000000..58d9c4279a35 --- /dev/null +++ b/backtracking/n_queens.py @@ -0,0 +1,88 @@ +""" + + 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. + +""" +solution = [] + + +def isSafe(board, row, column): + """ + 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, row): + """ + 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 + 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): + """ + 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/sudoku.py b/backtracking/sudoku.py new file mode 100644 index 000000000000..d864e2823a9b --- /dev/null +++ b/backtracking/sudoku.py @@ -0,0 +1,147 @@ +""" + 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. +""" +# assigning initial values to the grid +initial_grid = [ + [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 = [ + [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, row, column, n): + """ + 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): + """ + 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): + """ + 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 + + +def sudoku(grid): + """ + 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) + False + """ + + if is_completed(grid): + return grid + + row, column = find_empty_location(grid) + + for digit in range(1, 10): + if is_safe(grid, row, column, digit): + grid[row][column] = digit + + if sudoku(grid): + return grid + + grid[row][column] = 0 + + return False + + +def print_solution(grid): + """ + 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 grid in (initial_grid, no_solution): + grid = list(map(list, grid)) + solution = sudoku(grid) + if solution: + print("grid after solving:") + 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..e765a1b69714 --- /dev/null +++ b/backtracking/sum_of_subsets.py @@ -0,0 +1,55 @@ +""" + 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. +""" + + +def generate_sum_of_subsets_soln(nums, max_sum): + result = [] + path = [] + 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, max_sum, num_index, path, result, remaining_nums_sum): + """ + 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/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/blockchain/chinese_remainder_theorem.py b/blockchain/chinese_remainder_theorem.py new file mode 100644 index 000000000000..8c3eb9b4b01e --- /dev/null +++ b/blockchain/chinese_remainder_theorem.py @@ -0,0 +1,91 @@ +# 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 + + +# Extended Euclid +def extended_euclid(a, b): + """ + >>> 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, r1, n2, r2): + """ + >>> 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, n): + """ + >>> 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, r1, n2, r2): + """ + >>> 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 + + +# import testmod for testing our function +from doctest import testmod + +if __name__ == "__main__": + 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..ec2ed26e40ec --- /dev/null +++ b/blockchain/diophantine_equation.py @@ -0,0 +1,128 @@ +# 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 ) + + +def diophantine(a, b, c): + """ + >>> 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) + + +# 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 + + +def diophantine_all_soln(a, b, c, n=2): + """ + >>> 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) + + +# Euclid's Lemma : d divides a and b, if and only if d divides a-b and b + +# Euclid's Algorithm + + +def greatest_common_divisor(a, b): + """ + >>> 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 + + +# 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) + + +def extended_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) + + +# import testmod for testing our function +from doctest import testmod + +if __name__ == "__main__": + 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..1255f04328d5 --- /dev/null +++ b/blockchain/modular_division.py @@ -0,0 +1,151 @@ +# 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 + + +def modular_division(a, b, n): + """ + >>> 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 + + +# This function find the inverses of a i.e., a^(-1) +def invert_modulo(a, n): + """ + >>> 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 ------------------- + +# This function used the above inversion of a to find x = (b*a^(-1))mod n +def modular_division2(a, b, 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 + + +# 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) + + +def extended_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) + + +# Extended Euclid +def extended_euclid(a, b): + """ + >>> 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) + + +# Euclid's Lemma : d divides a and b, if and only if d divides a-b and b +# Euclid's Algorithm + + +def greatest_common_divisor(a, b): + """ + >>> 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 + + +# Import testmod for testing our function +from doctest import testmod + +if __name__ == "__main__": + 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/Quine_McCluskey/QuineMcCluskey.py b/boolean_algebra/Quine_McCluskey/QuineMcCluskey.py deleted file mode 100644 index eb3a11f7ee31..000000000000 --- a/boolean_algebra/Quine_McCluskey/QuineMcCluskey.py +++ /dev/null @@ -1,116 +0,0 @@ -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 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 newline at end of file diff --git a/boolean_algebra/quine_mc_cluskey.py b/boolean_algebra/quine_mc_cluskey.py new file mode 100644 index 000000000000..036cfbe63e79 --- /dev/null +++ b/boolean_algebra/quine_mc_cluskey.py @@ -0,0 +1,168 @@ +def compare_string(string1, string2): + """ + >>> 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): + """ + >>> 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, minterms): + """ + >>> 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, string2, count): + """ + >>> 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, prime_implicants): + """ + >>> 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, binary): + """ + >>> 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__": + 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/one_dimensional.py b/cellular_automata/one_dimensional.py new file mode 100644 index 000000000000..7819088c8cff --- /dev/null +++ b/cellular_automata/one_dimensional.py @@ -0,0 +1,73 @@ +""" +Return an image of 16 generations of one-dimensional cellular automata based on a given +ruleset number +https://mathworld.wolfram.com/ElementaryCellularAutomaton.html +""" + +from typing import List + +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 + left_neighbor = 0 if i == 0 else cells[time][i - 1] # special: leftmost cell + right_neighbor = 0 if i == population - 1 else cells[time][i + 1] # rightmost + # 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/XOR_cipher.py b/ciphers/XOR_cipher.py deleted file mode 100644 index 727fac3b0703..000000000000 --- a/ciphers/XOR_cipher.py +++ /dev/null @@ -1,209 +0,0 @@ -""" - 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 - - 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 - """ - - # precondition - assert (isinstance(key,int) and isinstance(content,str)) - - key = key or self.__key or 1 - - # make sure key can be any size - while (key > 255): - key -= 255 - - # This will be returned - ans = [] - - for ch in content: - ans.append(chr(ord(ch) ^ key)) - - return ans - - 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 - """ - - # precondition - assert (isinstance(key,int) and isinstance(content,list)) - - key = key or self.__key or 1 - - # make sure key can be any size - while (key > 255): - key -= 255 - - # This will be returned - 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 - """ - - # precondition - assert (isinstance(key,int) and isinstance(content,str)) - - key = key or self.__key or 1 - - # 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) - - 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 - """ - - # precondition - assert (isinstance(key,int) and isinstance(content,str)) - - key = key or self.__key or 1 - - # 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) - - 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 - """ - - #precondition - assert (isinstance(file,str) and isinstance(key,int)) - - try: - with open(file,"r") as fin: - with open("encrypt.out","w+") as fout: - - # actual encrypt-process - for line in fin: - fout.write(self.encrypt_string(line,key)) - - except: - return False - - return True - - - 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)) - - 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 - - - - -# Tests -# crypt = XORCipher() -# key = 67 - -# # test enrcypt -# print crypt.encrypt("hallo welt",key) -# # test decrypt -# print crypt.decrypt(crypt.encrypt("hallo welt",key), key) - -# # test encrypt_string -# print crypt.encrypt_string("hallo welt",key) - -# # test decrypt_string -# print crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key) - -# if (crypt.encrypt_file("test.txt",key)): -# print "encrypt successful" -# else: -# print "encrypt unsuccessful" - -# if (crypt.decrypt_file("encrypt.out",key)): -# print "decrypt successful" -# else: -# print "decrypt unsuccessful" \ No newline at end of file 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 6c1ba06f6850..21c92c6437e7 100644 --- a/ciphers/affine_cipher.py +++ b/ciphers/affine_cipher.py @@ -1,44 +1,66 @@ -from __future__ import print_function -import sys, random, cryptomath_module as cryptoMath +import random +import sys + +import cryptomath_module as cryptomath + +SYMBOLS = ( + r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`""" + r"""abcdefghijklmnopqrstuvwxyz{|}~""" +) -SYMBOLS = """ !"#$%&'()*+,-./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, keyB, mode): + 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 +69,16 @@ 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 +87,17 @@ def decryptMessage(key, message): plainText += symbol return plainText -def getRandomKey(): + +def get_random_key(): while True: keyA = random.randint(2, len(SYMBOLS)) keyB = random.randint(2, len(SYMBOLS)) - if cryptoMath.gcd(keyA, len(SYMBOLS)) == 1: - return keyA * len(SYMBOLS) + keyB + if cryptomath.gcd(keyA, len(SYMBOLS)) == 1: + return keyA * len(SYMBOLS) + keyB -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() main() diff --git a/ciphers/atbash.py b/ciphers/atbash.py new file mode 100644 index 000000000000..4cf003859856 --- /dev/null +++ b/ciphers/atbash.py @@ -0,0 +1,15 @@ +def atbash(): + output = "" + for i in input("Enter the sentence to be encrypted ").strip(): + extract = ord(i) + if 65 <= extract <= 90: + output += chr(155 - extract) + elif 97 <= extract <= 122: + output += chr(219 - extract) + else: + output += i + print(output) + + +if __name__ == "__main__": + atbash() diff --git a/ciphers/base16.py b/ciphers/base16.py new file mode 100644 index 000000000000..0210315d54e6 --- /dev/null +++ b/ciphers/base16.py @@ -0,0 +1,13 @@ +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() diff --git a/ciphers/base32.py b/ciphers/base32.py new file mode 100644 index 000000000000..5bba8c4dd685 --- /dev/null +++ b/ciphers/base32.py @@ -0,0 +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 + print(b32encoded) + print(base64.b32decode(b32encoded).decode("utf-8")) # decoded it + + +if __name__ == "__main__": + main() diff --git a/ciphers/base64_cipher.py b/ciphers/base64_cipher.py new file mode 100644 index 000000000000..f95403c7b426 --- /dev/null +++ b/ciphers/base64_cipher.py @@ -0,0 +1,88 @@ +def encode_base64(text): + r""" + >>> encode_base64('WELCOME to base64 encoding 😁') + 'V0VMQ09NRSB0byBiYXNlNjQgZW5jb2Rpbmcg8J+YgQ==' + >>> encode_base64('AÅᐃ𐀏🤓') + 'QcOF4ZCD8JCAj/CfpJM=' + >>> encode_base64('A'*60) + 'QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\r\nQUFB' + """ + base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + + byte_text = bytes(text, "utf-8") # put text in bytes for unicode support + r = "" # the result + c = -len(byte_text) % 3 # the length of padding + p = "=" * c # the padding + s = byte_text + b"\x00" * c # the text to encode + + i = 0 + while i < len(s): + if i > 0 and ((i / 3 * 4) % 76) == 0: + r = r + "\r\n" # for unix newline, put "\n" + + n = (s[i] << 16) + (s[i + 1] << 8) + s[i + 2] + + n1 = (n >> 18) & 63 + n2 = (n >> 12) & 63 + n3 = (n >> 6) & 63 + n4 = n & 63 + + r += base64_chars[n1] + base64_chars[n2] + base64_chars[n3] + base64_chars[n4] + i += 3 + + return r[0 : len(r) - len(p)] + p + + +def decode_base64(text): + r""" + >>> decode_base64('V0VMQ09NRSB0byBiYXNlNjQgZW5jb2Rpbmcg8J+YgQ==') + 'WELCOME to base64 encoding 😁' + >>> decode_base64('QcOF4ZCD8JCAj/CfpJM=') + 'AÅᐃ𐀏🤓' + >>> decode_base64("QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\r\nQUFB") + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + """ + base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" + s = "" + + for i in text: + if i in base64_chars: + s += i + c = "" + else: + if i == "=": + c += "=" + + p = "" + if c == "=": + p = "A" + else: + if c == "==": + p = "AA" + + r = b"" + s = s + p + + i = 0 + while i < len(s): + n = ( + (base64_chars.index(s[i]) << 18) + + (base64_chars.index(s[i + 1]) << 12) + + (base64_chars.index(s[i + 2]) << 6) + + base64_chars.index(s[i + 3]) + ) + + r += bytes([(n >> 16) & 255]) + bytes([(n >> 8) & 255]) + bytes([n & 255]) + + i += 4 + + return str(r[0 : len(r) - len(p)], "utf-8") + + +def main(): + print(encode_base64("WELCOME to base64 encoding 😁")) + print(decode_base64(encode_base64("WELCOME to base64 encoding 😁"))) + + +if __name__ == "__main__": + main() diff --git a/ciphers/base85.py b/ciphers/base85.py new file mode 100644 index 000000000000..ebfd0480f794 --- /dev/null +++ b/ciphers/base85.py @@ -0,0 +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 + print(a85encoded) + print(base64.a85decode(a85encoded).decode("utf-8")) # decoded it + + +if __name__ == "__main__": + main() diff --git a/ciphers/brute-force_caesar_cipher.py b/ciphers/brute_force_caesar_cipher.py similarity index 93% rename from ciphers/brute-force_caesar_cipher.py rename to ciphers/brute_force_caesar_cipher.py index 3b0716442fc5..5f11cb848c41 100644 --- a/ciphers/brute-force_caesar_cipher.py +++ b/ciphers/brute_force_caesar_cipher.py @@ -1,4 +1,3 @@ -from __future__ import print_function def decrypt(message): """ >>> decrypt('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 6cd35e73db0d..7bda519767a1 100644 --- a/ciphers/caesar_cipher.py +++ b/ciphers/caesar_cipher.py @@ -1,68 +1,254 @@ -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): +from string import ascii_letters + + +def encrypt(input_string: str, key: int, alphabet=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=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=None) -> dict: + """ + 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 + + # The key during testing (will increase) 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 = '' + + # The encoded result + result = "" + + # To store data on all the combinations + brute_force_data = {} + + # Cycle through each combination + while key <= len(alpha): + # Decrypt the message + result = decrypt(input_string, key, alpha) + + # Update the data + brute_force_data[key] = result + + # Reset result and increase the key + result = "" key += 1 - return None + + return brute_force_data def main(): - print('-' * 10 + "\n**Menu**\n" + '-' * 10) - print("1.Encrpyt") - print("2.Decrypt") - print("3.BruteForce") - print("4.Quit") while True: - choice = input("What would you like to do?: ") - if choice not in ['1', '2', '3', '4']: - print ("Invalid choice") - elif choice == '1': - strng = input("Please enter the string to be ecrypted: ") - while True: - key = int(input("Please enter off-set between 1-94: ")) - if key in range(1, 95): - print (encrypt(strng, key)) - main() - elif choice == '2': - strng = input("Please enter the string to be decrypted: ") - while True: - key = int(input("Please enter off-set between 1-94: ")) - if key > 0 and key <= 94: - print(decrypt(strng, key)) - main() - elif choice == '3': - strng = input("Please enter the string to be decrypted: ") - brute_force(strng) - main() - elif choice == '4': - print ("Goodbye.") - sys.exit() - -main() + print(f'\n{"-" * 10}\n Menu\n{"-" * 10}') + print(*["1.Encrpyt", "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 + + +if __name__ == "__main__": + main() diff --git a/ciphers/cryptomath_module.py b/ciphers/cryptomath_module.py index 3e8e71b117ed..fc38e4bd2a22 100644 --- a/ciphers/cryptomath_module.py +++ b/ciphers/cryptomath_module.py @@ -3,6 +3,7 @@ def gcd(a, b): a, b = b % a, a return b + def findModInverse(a, m): if gcd(a, m) != 1: return None @@ -10,5 +11,5 @@ def findModInverse(a, m): 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..3c37631c7b35 --- /dev/null +++ b/ciphers/decrypt_caesar_with_chi_squared.py @@ -0,0 +1,228 @@ +def decrypt_caesar_with_chi_squared( + ciphertext: str, + cipher_alphabet=None, + frequencies_dict=None, + case_sensetive: bool = False, +) -> list: + """ + 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!') + (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 + + # 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..e604a7b84166 --- /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, allow_probable=False): + """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(): + """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..c349aaa2f3b8 --- /dev/null +++ b/ciphers/diffie.py @@ -0,0 +1,25 @@ +def find_primitive(n): + 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..cc6b297f2daf 100644 --- a/ciphers/elgamal_key_generator.py +++ b/ciphers/elgamal_key_generator.py @@ -7,9 +7,9 @@ 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 @@ -19,7 +19,7 @@ def main(): def primitiveRoot(p_val): 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: @@ -28,7 +28,7 @@ def primitiveRoot(p_val): def generateKey(keySize): - print('Generating prime p...') + 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. @@ -41,23 +41,28 @@ def generateKey(keySize): 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)) + 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/hill_cipher.py b/ciphers/hill_cipher.py new file mode 100644 index 000000000000..9cd4a73b4f44 --- /dev/null +++ b/ciphers/hill_cipher.py @@ -0,0 +1,233 @@ +""" + +Hill Cipher: +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. + +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 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. + +While decrypting, the decrypting key is found which is the inverse of the +encrypting key modular 36. The same process is repeated for decrypting to get +the original message back. + +Constraints: +The determinant of the encryption key matrix must be relatively prime w.r.t 36. + +Note: +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 +https://www.youtube.com/watch?v=kfmNeskzs2o +https://www.youtube.com/watch?v=4RhLNDqcjpA + +""" + +import string +import numpy + + +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 = string.ascii_uppercase + string.digits + # This cipher takes alphanumerics into account + # i.e. a total of 36 characters + + # take x and return x % len(key_string) + modulus = numpy.vectorize(lambda x: x % 36) + + to_int = numpy.vectorize(lambda x: round(x)) + + def __init__(self, encrypt_key): + """ + encrypt_key is an NxN numpy array + """ + 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 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 greatest_common_divisor(det, len(self.key_string)) != 1: + raise ValueError( + f"determinant modular {req_l} of encryption key({det}) is not co prime w.r.t {req_l}.\nTry another key." + ) + + 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 = chars[-1] + while len(chars) % self.break_key != 0: + chars.append(last) + + 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 = [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 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 + for i in range(len(self.key_string)): + if (det * i) % len(self.key_string) == 1: + det_inv = i + break + + inv_key = ( + det_inv + * numpy.linalg.det(self.encrypt_key) + * numpy.linalg.inv(self.encrypt_key) + ) + + 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 = [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 + + +def main(): + N = int(input("Enter the order of the encryption key: ")) + hill_matrix = [] + + print("Enter each row of the encryption key with space separated integers") + for i in range(N): + row = [int(x) for x in input().split()] + hill_matrix.append(row) + + hc = HillCipher(numpy.array(hill_matrix)) + + print("Would you like to encrypt or decrypt some text? (1 or 2)") + 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": + 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..a546e4c781e6 --- /dev/null +++ b/ciphers/mixed_keyword_cypher.py @@ -0,0 +1,68 @@ +def mixed_keyword(key="college", pt="UNIVERSITY"): + """ + + 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) + l = 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(l): + t.append(temp[k]) + if not (k < 25): + break + k += 1 + modalpha.append(t) + # print(modalpha) + d = dict() + j = 0 + k = 0 + for j in range(l): + 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/morse_code_implementation.py b/ciphers/morse_code_implementation.py new file mode 100644 index 000000000000..6df4632af4cb --- /dev/null +++ b/ciphers/morse_code_implementation.py @@ -0,0 +1,107 @@ +# 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": "-----", + ", ": "--..--", + ".": ".-.-.-", + "?": "..--..", + "/": "-..-.", + "-": "-....-", + "(": "-.--.", + ")": "-.--.-", +} + + +def encrypt(message): + cipher = "" + for letter in message: + if letter != " ": + + cipher += MORSE_CODE_DICT[letter] + " " + else: + + cipher += " " + + return cipher + + +def decrypt(message): + + message += " " + + decipher = "" + citext = "" + for letter in message: + + if letter != " ": + + i = 0 + + citext += letter + + else: + + i += 1 + + if i == 2: + + decipher += " " + else: + + decipher += list(MORSE_CODE_DICT.keys())[ + list(MORSE_CODE_DICT.values()).index(citext) + ] + citext = "" + + 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 similarity index 57% rename from ciphers/Onepad_Cipher.py rename to ciphers/onepad_cipher.py index 7e1be5fdc077..fe07908afff5 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''' + """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.''' + """Function to decrypt text using pseudo-random numbers.""" plain = [] for i in range(len(key)): - p = (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..030fe8155a69 100644 --- a/ciphers/playfair_cipher.py +++ b/ciphers/playfair_cipher.py @@ -1,14 +1,14 @@ import string import itertools + 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): @@ -16,32 +16,33 @@ def prepare_input(dirty): 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): # 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,6 +58,7 @@ def generate_table(key): return table + def encode(plaintext, key): table = generate_table(key) plaintext = prepare_input(plaintext) @@ -68,14 +70,14 @@ 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 @@ -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..a8e79415958d --- /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): + """ + >>> 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, words): + """ + >>> 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, words): + """ + >>> decrypt('marvin', 'QRACRWU') + 'JESSICA' + """ + return encrypt(key, words) + + +def get_position(table, char): + """ + >>> 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, char): + """ + >>> 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 similarity index 100% rename from ciphers/Prehistoric Men.txt rename to ciphers/prehistoric_men.txt diff --git a/ciphers/rabin_miller.py b/ciphers/rabin_miller.py index f71fb03c0051..c544abdf9acc 100644 --- a/ciphers/rabin_miller.py +++ b/ciphers/rabin_miller.py @@ -1,8 +1,8 @@ -from __future__ import print_function # Primality Testing with the Rabin-Miller Algorithm import random + def rabinMiller(num): s = num - 1 t = 0 @@ -24,24 +24,181 @@ def rabinMiller(num): v = (v ** 2) % num return True + def isPrime(num): - if (num < 2): + 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=1024): 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/rot13.py b/ciphers/rot13.py index 2abf981e9d7d..6bcb471d6e05 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): + """ + 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 94f69ddc2533..371966b8379b 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 + +import rsa_key_generator as rkg DEFAULT_BLOCK_SIZE = 128 BYTE_SIZE = 256 + def main(): - filename = 'encrypted_file.txt' - response = input('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') + messageBytes = message.encode("ascii") blockInts = [] for blockStart in range(0, len(messageBytes), blockSize): @@ -59,7 +62,7 @@ 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): @@ -80,46 +83,57 @@ def decryptMessage(encryptedBlocks, messageLength, key, blockSize=DEFAULT_BLOCK_ def readKeyFile(keyFilename): - fo = open(keyFilename) - content = fo.read() - fo.close() - keySize, n, EorD = content.split(',') + with open(keyFilename) as fo: + content = fo.read() + keySize, n, EorD = content.split(",") return (int(keySize), int(n), int(EorD)) -def encryptAndWriteToFile(messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE): +def encryptAndWriteToFile( + messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE +): 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) - fo = open(messageFilename, 'w') - fo.write(encryptedContent) - fo.close() + encryptedContent = ",".join(encryptedBlocks) + encryptedContent = "{}_{}_{}".format(len(message), blockSize, encryptedContent) + with open(messageFilename, "w") as fo: + fo.write(encryptedContent) return encryptedContent def readFromFileAndDecrypt(messageFilename, keyFilename): keySize, n, d = readKeyFile(keyFilename) - fo = open(messageFilename) - content = fo.read() - messageLength, blockSize, encryptedMessage = content.split('_') + with open(messageFilename) as fo: + content = fo.read() + 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..9ec34e6c5a17 --- /dev/null +++ b/ciphers/rsa_factorization.py @@ -0,0 +1,56 @@ +""" +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. +""" +import math +import random +from typing import List + + +def rsafactor(d: int, e: int, N: int) -> List[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..5514c69917bf 100644 --- a/ciphers/rsa_key_generator.py +++ b/ciphers/rsa_key_generator.py @@ -1,46 +1,59 @@ -from __future__ import print_function -import random, sys, os -import rabin_miller as rabinMiller, cryptomath_module as cryptoMath +import os +import random +import sys + +import cryptomath_module as cryptoMath +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...') + 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)) + 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..be5c6caf845b --- /dev/null +++ b/ciphers/shuffled_shift_cipher.py @@ -0,0 +1,177 @@ +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..7da18482db8c 100644 --- a/ciphers/simple_substitution_cipher.py +++ b/ciphers/simple_substitution_cipher.py @@ -1,24 +1,25 @@ -from __future__ import print_function import sys, random -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)) - + print("\n{}ion: \n{}".format(mode.title(), translated)) + + def checkValidKey(key): keyList = list(key) lettersList = list(LETTERS) @@ -26,30 +27,33 @@ def checkValidKey(key): 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): """ >>> encryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Harshil Darji') 'Ilcrism Olcvs' """ - return translateMessage(key, message, 'encrypt') + return translateMessage(key, message, "encrypt") + def decryptMessage(key, message): """ >>> decryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Ilcrism Olcvs') 'Harshil Darji' """ - return translateMessage(key, message, 'decrypt') + return translateMessage(key, message, "decrypt") + def translateMessage(key, message, mode): - translated = '' + 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 +66,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 new file mode 100644 index 000000000000..f1c954b5c34f --- /dev/null +++ b/ciphers/trafid_cipher.py @@ -0,0 +1,120 @@ +# https://en.wikipedia.org/wiki/Trifid_cipher + + +def __encryptPart(messagePart, character2Number): + one, two, three = "", "", "" + tmp = [] + + for character in messagePart: + tmp.append(character2Number[character]) + + for each in tmp: + one += each[0] + two += each[1] + three += each[2] + + return one + two + three + + +def __decryptPart(messagePart, character2Number): + tmp, thisPart = "", "" + result = [] + + for character in messagePart: + thisPart += character2Number[character] + + for digit in thisPart: + tmp += digit + if len(tmp) == len(messagePart): + result.append(tmp) + tmp = "" + + return result[0], result[1], result[2] + + +def __prepare(message, alphabet): + # Validate message and alphabet, set to upper and remove spaces + alphabet = alphabet.replace(" ", "").upper() + message = message.replace(" ", "").upper() + + # 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", + ) + 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): + 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(encrypted_numeric), 3): + encrypted += number2Character[encrypted_numeric[i : i + 3]] + + return encrypted + + +def decryptMessage(message, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): + 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 j in range(0, len(a)): + decrypted_numeric.append(a[j] + b[j] + c[j]) + + for each in decrypted_numeric: + decrypted += number2Character[each] + + return decrypted + + +if __name__ == "__main__": + msg = "DEFEND THE EAST WALL OF THE CASTLE." + encrypted = encryptMessage(msg, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") + decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") + print(f"Encrypted: {encrypted}\nDecrypted: {decrypted}") diff --git a/ciphers/transposition_cipher.py b/ciphers/transposition_cipher.py index dbb358315d22..4bba88955433 100644 --- a/ciphers/transposition_cipher.py +++ b/ciphers/transposition_cipher.py @@ -1,31 +1,40 @@ -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): """ >>> 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): """ @@ -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 deleted file mode 100644 index 57620d83948c..000000000000 --- a/ciphers/transposition_cipher_encrypt-decrypt_file.py +++ /dev/null @@ -1,36 +0,0 @@ -from __future__ import print_function -import time, os, sys -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]: ') - - if not os.path.exists(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'): - sys.exit() - - startTime = time.time() - if mode.lower().startswith('e'): - content = open(inputFile).read() - translated = transCipher.encryptMessage(key, content) - elif mode.lower().startswith('d'): - content = open(outputFile).read() - translated =transCipher .decryptMessage(key, content) - - outputObj = open(outputFile, 'w') - outputObj.write(translated) - outputObj.close() - - totalTime = round(time.time() - startTime, 2) - print(('Done (', totalTime, 'seconds )')) - -if __name__ == '__main__': - main() diff --git a/ciphers/transposition_cipher_encrypt_decrypt_file.py b/ciphers/transposition_cipher_encrypt_decrypt_file.py new file mode 100644 index 000000000000..775df354e117 --- /dev/null +++ b/ciphers/transposition_cipher_encrypt_decrypt_file.py @@ -0,0 +1,38 @@ +import time, os, sys +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]: ") + + if not os.path.exists(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"): + sys.exit() + + startTime = time.time() + if mode.lower().startswith("e"): + with open(inputFile) as f: + content = f.read() + translated = transCipher.encryptMessage(key, content) + elif mode.lower().startswith("d"): + with open(outputFile) as f: + content = f.read() + translated = transCipher.decryptMessage(key, content) + + with open(outputFile, "w") as outputObj: + outputObj.write(translated) + + totalTime = round(time.time() - startTime, 2) + print(("Done (", totalTime, "seconds )")) + + +if __name__ == "__main__": + main() diff --git a/ciphers/vigenere_cipher.py b/ciphers/vigenere_cipher.py index 5d5be0792835..6c10e7d773f2 100644 --- a/ciphers/vigenere_cipher.py +++ b/ciphers/vigenere_cipher.py @@ -1,34 +1,37 @@ -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): - ''' + """ >>> 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): - ''' + """ >>> 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): translated = [] @@ -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 new file mode 100644 index 000000000000..0fcfbb0b9ae2 --- /dev/null +++ b/ciphers/xor_cipher.py @@ -0,0 +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 +""" + + +class XORCipher: + def __init__(self, key=0): + """ + simple constructor that receives a key or uses + default key = 0 + """ + + # private field + self.__key = key + + 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 + """ + + # precondition + assert isinstance(key, int) and isinstance(content, str) + + key = key or self.__key or 1 + + # make sure key can be any size + while key > 255: + key -= 255 + + # This will be returned + ans = [] + + for ch in content: + ans.append(chr(ord(ch) ^ key)) + + return ans + + 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 + """ + + # precondition + assert isinstance(key, int) and isinstance(content, list) + + key = key or self.__key or 1 + + # make sure key can be any size + while key > 255: + key -= 255 + + # This will be returned + 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 + """ + + # precondition + assert isinstance(key, int) and isinstance(content, str) + + key = key or self.__key or 1 + + # 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) + + 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 + """ + + # precondition + assert isinstance(key, int) and isinstance(content, str) + + key = key or self.__key or 1 + + # 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) + + 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 + """ + + # precondition + assert isinstance(file, str) and isinstance(key, int) + + try: + with open(file, "r") as fin: + with open("encrypt.out", "w+") as fout: + + # actual encrypt-process + for line in fin: + fout.write(self.encrypt_string(line, key)) + + except IOError: + return False + + return True + + 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) + + 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 IOError: + return False + + return True + + +# Tests +# crypt = XORCipher() +# key = 67 + +# # test enrcypt +# print(crypt.encrypt("hallo welt",key)) +# # test decrypt +# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key)) + +# # test encrypt_string +# print(crypt.encrypt_string("hallo welt",key)) + +# # test decrypt_string +# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key)) + +# if (crypt.encrypt_file("test.txt",key)): +# print("encrypt successful") +# else: +# print("encrypt unsuccessful") + +# if (crypt.decrypt_file("encrypt.out",key)): +# print("decrypt successful") +# else: +# print("decrypt unsuccessful") diff --git a/compression/burrows_wheeler.py b/compression/burrows_wheeler.py new file mode 100644 index 000000000000..2a08c6092cda --- /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 typing import List, Dict + + +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/compression/image_data/PSNR-example-base.png b/compression/image_data/PSNR-example-base.png new file mode 100644 index 000000000000..bf12a5450b96 Binary files /dev/null and b/compression/image_data/PSNR-example-base.png differ diff --git a/compression/image_data/PSNR-example-comp-10.jpg b/compression/image_data/PSNR-example-comp-10.jpg new file mode 100644 index 000000000000..a137ebb1100f Binary files /dev/null and b/compression/image_data/PSNR-example-comp-10.jpg differ diff --git a/compression/image_data/compressed_image.png b/compression/image_data/compressed_image.png new file mode 100644 index 000000000000..75c41c21c7b5 Binary files /dev/null and b/compression/image_data/compressed_image.png differ diff --git a/compression/image_data/example_image.jpg b/compression/image_data/example_image.jpg new file mode 100644 index 000000000000..3a58ba3a8c33 Binary files /dev/null and b/compression/image_data/example_image.jpg differ diff --git a/compression/image_data/example_wikipedia_image.jpg b/compression/image_data/example_wikipedia_image.jpg new file mode 100644 index 000000000000..1e983e845651 Binary files /dev/null and b/compression/image_data/example_wikipedia_image.jpg differ diff --git a/compression/image_data/original_image.png b/compression/image_data/original_image.png new file mode 100644 index 000000000000..09a76ffa8ce1 Binary files /dev/null and b/compression/image_data/original_image.png differ diff --git a/compression/peak_signal_to_noise_ratio.py b/compression/peak_signal_to_noise_ratio.py new file mode 100644 index 000000000000..f4a1ca41e14d --- /dev/null +++ b/compression/peak_signal_to_noise_ratio.py @@ -0,0 +1,43 @@ +""" + 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 +import os + +import cv2 +import numpy as np + + +def psnr(original, contrast): + mse = np.mean((original - contrast) ** 2) + if mse == 0: + return 100 + PIXEL_MAX = 255.0 + PSNR = 20 * math.log10(PIXEL_MAX / math.sqrt(mse)) + return PSNR + + +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, "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, "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 --") + print(f"PSNR value is {psnr(original, contrast)} dB") + + # # Value expected: 31.53dB (Wikipedia Example) + print("\n-- Second Test --") + print(f"PSNR value is {psnr(original2, contrast2)} dB") + + +if __name__ == "__main__": + main() diff --git a/conversions/decimal_to_binary.py b/conversions/decimal_to_binary.py new file mode 100644 index 000000000000..ad4ba166745d --- /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): + + """ + Convert a 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_hexadecimal.py b/conversions/decimal_to_hexadecimal.py new file mode 100644 index 000000000000..a70e3c7b97bf --- /dev/null +++ b/conversions/decimal_to_hexadecimal.py @@ -0,0 +1,77 @@ +""" 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..a89a2be982b8 --- /dev/null +++ b/conversions/decimal_to_octal.py @@ -0,0 +1,42 @@ +"""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/roman_to_integer.py b/conversions/roman_to_integer.py new file mode 100644 index 000000000000..ce52b6fb7cbb --- /dev/null +++ b/conversions/roman_to_integer.py @@ -0,0 +1,27 @@ +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 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/AVL/AVL.py b/data_structures/AVL/AVL.py deleted file mode 100644 index d01e8f825368..000000000000 --- a/data_structures/AVL/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/Arrays.py b/data_structures/Arrays.py deleted file mode 100644 index 3ec9f8976673..000000000000 --- a/data_structures/Arrays.py +++ /dev/null @@ -1,3 +0,0 @@ -arr = [10, 20, 30, 40] -arr[1] = 30 -print(arr) diff --git a/data_structures/Binary Tree/FenwickTree.py b/data_structures/Binary Tree/FenwickTree.py deleted file mode 100644 index f429161c8c36..000000000000 --- a/data_structures/Binary Tree/FenwickTree.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/LazySegmentTree.py b/data_structures/Binary Tree/LazySegmentTree.py deleted file mode 100644 index 9b14b24e81fa..000000000000 --- a/data_structures/Binary Tree/LazySegmentTree.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/binary_search_tree.py b/data_structures/Binary Tree/binary_search_tree.py deleted file mode 100644 index b4021e4f861f..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(): - ''' - Example - 8 - / \ - 3 10 - / \ \ - 1 6 14 - / \ / - 4 7 13 - ''' - - ''' - 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/Graph/BellmanFord.py b/data_structures/Graph/BellmanFord.py deleted file mode 100644 index 82db80546b94..000000000000 --- a/data_structures/Graph/BellmanFord.py +++ /dev/null @@ -1,54 +0,0 @@ -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() - -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) diff --git a/data_structures/Graph/BreadthFirstSearch.py b/data_structures/Graph/BreadthFirstSearch.py deleted file mode 100644 index 02f6af83ff66..000000000000 --- a/data_structures/Graph/BreadthFirstSearch.py +++ /dev/null @@ -1,63 +0,0 @@ -# Author: OMKAR PATHAK -from __future__ import print_function - - -class Graph(): - def __init__(self): - self.vertex = {} - - # 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]])) - - # 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 BFS(self, startVertex): - # Take a list for stoting already visited vertexes - visited = [False] * len(self.vertex) - - # create a list to store all the vertexes for BFS - queue = [] - - # mark the source node as visited and enqueue it - visited[startVertex] = True - queue.append(startVertex) - - while queue: - startVertex = queue.pop(0) - print(startVertex, end = ' ') - - # 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 - -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 diff --git a/data_structures/Graph/Dijkstra.py b/data_structures/Graph/Dijkstra.py deleted file mode 100644 index 8917171417c4..000000000000 --- a/data_structures/Graph/Dijkstra.py +++ /dev/null @@ -1,57 +0,0 @@ -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() - -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 - -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) diff --git a/data_structures/Graph/FloydWarshall.py b/data_structures/Graph/FloydWarshall.py deleted file mode 100644 index fae8b19b351a..000000000000 --- a/data_structures/Graph/FloydWarshall.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/data_structures/Graph/Graph_list.py b/data_structures/Graph/Graph_list.py deleted file mode 100644 index d67bc96c4a81..000000000000 --- a/data_structures/Graph/Graph_list.py +++ /dev/null @@ -1,31 +0,0 @@ -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() - diff --git a/data_structures/Heap/heap.py b/data_structures/Heap/heap.py deleted file mode 100644 index e66d02b6d99f..000000000000 --- a/data_structures/Heap/heap.py +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/python - -from __future__ import print_function - -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 - -class Heap: - def __init__(self): - 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): - self.currsize = len(a) - self.h = list(a) - for i in range(self.currsize/2,-1,-1): - self.maxHeapify(i) - - def getMax(self): - 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): - 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): - 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): - 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() - - diff --git a/data_structures/LinkedList/DoublyLinkedList.py b/data_structures/LinkedList/DoublyLinkedList.py deleted file mode 100644 index 6f17b7c81033..000000000000 --- a/data_structures/LinkedList/DoublyLinkedList.py +++ /dev/null @@ -1,76 +0,0 @@ -''' -- 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. -''' -from __future__ import print_function - - -class LinkedList: - 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 - 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 == None): - self.tail = None - 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 == None) - - def display(self): #Prints contents of the list - 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=" ") diff --git a/data_structures/LinkedList/singly_LinkedList.py b/data_structures/LinkedList/singly_LinkedList.py deleted file mode 100644 index eb7f48f17359..000000000000 --- a/data_structures/LinkedList/singly_LinkedList.py +++ /dev/null @@ -1,69 +0,0 @@ -from __future__ import print_function - - -class Node: # create a Node - def __init__(self, data): - self.data = data # given data - self.next = None # given next to None - - -class Linked_List: - def insert_tail(Head, data): - if Head.next is None: - Head.next = Node(data) - else: - Head.next.insert_tail(data) - - def insert_head(Head, data): - tamp = Head - if tamp is None: - newNod = Node() # create a new Node - newNod.data = data - newNod.next = None - Head = newNod # make new node to Head - else: - newNod = Node() - newNod.data = data - newNod.next = Head # put the Head at NewNode Next - Head = newNod # make a NewNode to Head - return Head - - def printList(Head): # print every node data - tamp = Head - while tamp is not None: - print(tamp.data) - tamp = tamp.next - - def delete_head(Head): # delete from head - if Head is not None: - Head = Head.next - return Head # return new Head - - def delete_tail(Head): # delete from tail - if Head is not None: - tamp = Node() - tamp = Head - while tamp.next.next is not None: # find the 2nd last element - tamp = tamp.next - # delete the last element by give next None to 2nd last Element - tamp.next = None - return Head - - def isEmpty(Head): - return Head is None # Return if Head is none - - def reverse(Head): - prev = None - current = Head - - while current: - # Store the current node's next node. - next_node = current.next - # Make the current node's next point backwards - current.next = prev - # Make the previous node be the current node - prev = current - # Make the current node the next node (to progress iteration) - current = next_node - # Return prev in order to put the head at the end - Head = prev diff --git a/data_structures/Queue/DeQueue.py b/data_structures/Queue/DeQueue.py deleted file mode 100644 index fdee64eb6ae0..000000000000 --- a/data_structures/Queue/DeQueue.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/Stacks/__init__.py b/data_structures/Stacks/__init__.py deleted file mode 100644 index f7e92ae2d269..000000000000 --- a/data_structures/Stacks/__init__.py +++ /dev/null @@ -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 deleted file mode 100644 index 8d99358bea87..000000000000 --- a/data_structures/Stacks/balanced_parentheses.py +++ /dev/null @@ -1,23 +0,0 @@ -from __future__ import print_function -from __future__ import absolute_import -from .Stack import Stack - -__author__ = 'Omkar Pathak' - - -def balanced_parentheses(parentheses): - """ Use a stack to check if a string of parentheses are balanced.""" - stack = Stack(len(parentheses)) - for parenthesis in parentheses: - if parenthesis == '(': - stack.push(parenthesis) - elif parenthesis == ')': - stack.pop() - return not stack.is_empty() - - -if __name__ == '__main__': - examples = ['((()))', '((())'] - print('Balanced parentheses demonstration:\n') - for example in examples: - print(example + ': ' + str(balanced_parentheses(example))) 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/Trie/Trie.py b/data_structures/Trie/Trie.py deleted file mode 100644 index b6234c6704c6..000000000000 --- a/data_structures/Trie/Trie.py +++ /dev/null @@ -1,75 +0,0 @@ -""" -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. - -""" - - -class TrieNode: - 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 - """ - Inserts a list of words into the Trie - :param words: list of string words - :return: None - """ - for word in words: - self.insert(word) - - def insert(self, word: str): # noqa: E999 This syntax is Python 3 only - """ - Inserts a word into the Trie - :param word: word to be inserted - :return: None - """ - curr = self - for char in word: - if char not in curr.nodes: - curr.nodes[char] = TrieNode() - curr = curr.nodes[char] - curr.is_leaf = True - - def find(self, word: str) -> bool: # noqa: E999 This syntax is Python 3 only - """ - Tries to find word in a Trie - :param word: word to look for - :return: Returns True if word is found, False otherwise - """ - curr = self - for char in word: - if char not in curr.nodes: - return False - curr = curr.nodes[char] - return curr.is_leaf - - -def print_words(node: TrieNode, word: str): # noqa: E999 This syntax is Python 3 only - """ - Prints all the words in a Trie - :param node: root node of Trie - :param word: Word variable should be empty at start - :return: None - """ - if node.is_leaf: - 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'] - 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') - -test() diff --git a/data_structures/UnionFind/tests_union_find.py b/data_structures/UnionFind/tests_union_find.py deleted file mode 100644 index b0708778ddbd..000000000000 --- a/data_structures/UnionFind/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/UnionFind/union_find.py b/data_structures/UnionFind/union_find.py deleted file mode 100644 index 40eea67ac944..000000000000 --- a/data_structures/UnionFind/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/data_structures/binary_tree/avl_tree.py b/data_structures/binary_tree/avl_tree.py new file mode 100644 index 000000000000..2df747c105ad --- /dev/null +++ b/data_structures/binary_tree/avl_tree.py @@ -0,0 +1,288 @@ +""" +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/basic_binary_tree.py b/data_structures/binary_tree/basic_binary_tree.py new file mode 100644 index 000000000000..3ed34fc6c68e --- /dev/null +++ b/data_structures/binary_tree/basic_binary_tree.py @@ -0,0 +1,69 @@ +class Node: # This is the Class Node with a 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 function 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 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..3868130357fb --- /dev/null +++ b/data_structures/binary_tree/binary_search_tree.py @@ -0,0 +1,207 @@ +""" +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 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/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..acd551b41b96 --- /dev/null +++ b/data_structures/binary_tree/lazy_segment_tree.py @@ -0,0 +1,94 @@ +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/lowest_common_ancestor.py b/data_structures/binary_tree/lowest_common_ancestor.py new file mode 100644 index 000000000000..f560eaa5ef29 --- /dev/null +++ b/data_structures/binary_tree/lowest_common_ancestor.py @@ -0,0 +1,94 @@ +# https://en.wikipedia.org/wiki/Lowest_common_ancestor +# https://en.wikipedia.org/wiki/Breadth-first_search + +import queue + + +def swap(a, b): + a ^= b + b ^= a + a ^= b + return a, b + + +# creating sparse table which saves each nodes 2^i-th parent +def creatSparse(max_node, parent): + j = 1 + while (1 << j) < max_node: + for i in range(1, max_node + 1): + parent[j][i] = parent[j - 1][parent[j - 1][i]] + j += 1 + return parent + + +# returns lca of node u,v +def LCA(u, v, level, parent): + # u must be deeper in the tree than v + if level[u] < level[v]: + u, v = swap(u, v) + # making depth of u same as depth of v + for i in range(18, -1, -1): + if level[u] - (1 << i) >= level[v]: + u = parent[i][u] + # at the same depth if u==v that mean lca is found + if u == v: + return u + # moving both nodes upwards till lca in found + for i in range(18, -1, -1): + if parent[i][u] != 0 and parent[i][u] != parent[i][v]: + u, v = parent[i][u], parent[i][v] + # returning longest common ancestor of u,v + return parent[0][u] + + +# 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): + level[root] = 0 + q = queue.Queue(maxsize=max_node) + q.put(root) + while q.qsize() != 0: + u = q.get() + for v in graph[u]: + if level[v] == -1: + level[v] = level[u] + 1 + q.put(v) + parent[0][v] = u + return level, parent + + +def main(): + max_node = 13 + # initializing with 0 + parent = [[0 for _ in range(max_node + 10)] for _ in range(20)] + # initializing with -1 which means every node is unvisited + level = [-1 for _ in range(max_node + 10)] + graph = { + 1: [2, 3, 4], + 2: [5], + 3: [6, 7], + 4: [8], + 5: [9, 10], + 6: [11], + 7: [], + 8: [12, 13], + 9: [], + 10: [], + 11: [], + 12: [], + 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)) + + +if __name__ == "__main__": + 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..877ee45b5baa --- /dev/null +++ b/data_structures/binary_tree/non_recursive_segment_tree.py @@ -0,0 +1,153 @@ +""" +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 typing import List, 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: + """ + 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 + res = None + while l <= r: + 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..f038b587616d --- /dev/null +++ b/data_structures/binary_tree/red_black_tree.py @@ -0,0 +1,710 @@ +""" +python/black : true +flake8 : passed +""" + + +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=None, color=0, parent=None, left=None, right=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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """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): + """Returns true iff this node is the right child of its parent.""" + return self.parent and self.parent.right is self + + def __bool__(self): + return True + + def __len__(self): + """ + 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): + 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): + 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): + if self.left: + yield from self.left.postorder_traverse() + if self.right: + yield from self.right.postorder_traverse() + yield self.label + + def __repr__(self): + 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): + """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): + """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(): + """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(): + """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(): + """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(): + """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(): + """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(): + """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(): + """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(): + """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(): + """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(): + 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(): + """ + >>> 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/SegmentTree.py b/data_structures/binary_tree/segment_tree.py similarity index 56% rename from data_structures/Binary Tree/SegmentTree.py rename to data_structures/binary_tree/segment_tree.py index 001bf999f391..ad9476b4514b 100644 --- a/data_structures/Binary Tree/SegmentTree.py +++ b/data_structures/binary_tree/segment_tree.py @@ -1,13 +1,14 @@ -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 @@ -21,51 +22,55 @@ def build(self, idx, l, r): 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 + ): # 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: 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 + ): # 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: 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..c3ab493d5f4f --- /dev/null +++ b/data_structures/binary_tree/segment_tree_other.py @@ -0,0 +1,242 @@ +""" +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 queue import Queue +from collections.abc import Sequence + + +class SegmentTreeNode(object): + 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 "val: %s, start: %s, end: %s" % (self.val, self.start, self.end) + + +class SegmentTree(object): + """ + >>> 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..26f021445ca4 --- /dev/null +++ b/data_structures/binary_tree/treap.py @@ -0,0 +1,180 @@ +from random import random +from typing import Tuple + + +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/data_structures/heap/heap_generic.py b/data_structures/data_structures/heap/heap_generic.py new file mode 100644 index 000000000000..8993d501331b --- /dev/null +++ b/data_structures/data_structures/heap/heap_generic.py @@ -0,0 +1,163 @@ +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): + """Returns 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/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 deleted file mode 100644 index b96ddd478458..000000000000 --- a/data_structures/hashing/__init__.py +++ /dev/null @@ -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..ce4454db0bef 100644 --- a/data_structures/hashing/double_hash.py +++ b/data_structures/hashing/double_hash.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from .hash_table import HashTable +from hash_table import HashTable from number_theory.prime_numbers import next_prime, check_prime @@ -8,26 +8,35 @@ class DoubleHash(HashTable): """ 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..3b39742f9d09 100644 --- a/data_structures/hashing/hash_table.py +++ b/data_structures/hashing/hash_table.py @@ -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..48d93bbc5cff 100644 --- a/data_structures/hashing/hash_table_with_linked_list.py +++ b/data_structures/hashing/hash_table_with_linked_list.py @@ -1,4 +1,4 @@ -from .hash_table import HashTable +from hash_table import HashTable from collections import deque @@ -7,18 +7,20 @@ 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..2a966e0da7f2 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)]) + 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..0dd84a5d987c 100644 --- a/data_structures/hashing/quadratic_probing.py +++ b/data_structures/hashing/quadratic_probing.py @@ -1,24 +1,27 @@ #!/usr/bin/env python3 -from .hash_table import HashTable +from hash_table import HashTable class QuadraticProbing(HashTable): """ 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/binomial_heap.py b/data_structures/heap/binomial_heap.py new file mode 100644 index 000000000000..ac244023082a --- /dev/null +++ b/data_structures/heap/binomial_heap.py @@ -0,0 +1,401 @@ +""" +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 new file mode 100644 index 000000000000..b901c54a4284 --- /dev/null +++ b/data_structures/heap/heap.py @@ -0,0 +1,135 @@ +#!/usr/bin/python3 + + +class Heap(object): + """ + >>> unsorted = [103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5] + >>> h = Heap() + >>> h.build_heap(unsorted) + >>> h.display() + [209, 201, 25, 103, 107, 15, 1, 9, 7, 11, 5] + >>> + >>> h.get_max() + 209 + >>> h.display() + [201, 107, 25, 103, 11, 15, 1, 9, 7, 5] + >>> + >>> h.insert(100) + >>> h.display() + [201, 107, 25, 103, 100, 15, 1, 9, 7, 5, 11] + >>> + >>> h.heap_sort() + >>> h.display() + [1, 5, 7, 9, 11, 15, 25, 100, 103, 107, 201] + >>> + """ + + def __init__(self): + self.h = [] + self.curr_size = 0 + + def get_left_child_index(self, i): + left_child_index = 2 * i + 1 + if left_child_index < self.curr_size: + return left_child_index + return None + + def get_right_child(self, i): + right_child_index = 2 * i + 2 + if right_child_index < self.curr_size: + return right_child_index + return None + + def max_heapify(self, index): + if index < self.curr_size: + largest = index + lc = self.get_left_child_index(index) + rc = self.get_right_child(index) + if lc is not None and self.h[lc] > self.h[largest]: + largest = lc + if rc is not None and self.h[rc] > self.h[largest]: + largest = rc + if largest != index: + self.h[largest], self.h[index] = self.h[index], self.h[largest] + self.max_heapify(largest) + + def build_heap(self, collection): + self.curr_size = len(collection) + self.h = list(collection) + if self.curr_size <= 1: + return + for i in range(self.curr_size // 2 - 1, -1, -1): + self.max_heapify(i) + + def get_max(self): + if self.curr_size >= 2: + me = self.h[0] + self.h[0] = self.h.pop(-1) + self.curr_size -= 1 + self.max_heapify(0) + return me + elif self.curr_size == 1: + self.curr_size -= 1 + return self.h.pop(-1) + return None + + def heap_sort(self): + size = self.curr_size + for j in range(size - 1, 0, -1): + self.h[0], self.h[j] = self.h[j], self.h[0] + self.curr_size -= 1 + self.max_heapify(0) + self.curr_size = size + + def insert(self, data): + self.h.append(data) + curr = (self.curr_size - 1) // 2 + self.curr_size += 1 + while curr >= 0: + self.max_heapify(curr) + curr = (curr - 1) // 2 + + def display(self): + print(self.h) + + +def main(): + 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("source unsorted list: %s" % unsorted) + + h = Heap() + h.build_heap(unsorted) + print("after build heap: ", end=" ") + h.display() + + print("max value: %s" % h.get_max()) + print("delete max value: ", end=" ") + h.display() + + h.insert(100) + print("after insert new value 100: ", end=" ") + h.display() + + h.heap_sort() + print("heap sort: ", end=" ") + h.display() + print() + + +if __name__ == "__main__": + main() 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..e68853837faa --- /dev/null +++ b/data_structures/heap/min_heap.py @@ -0,0 +1,170 @@ +# Min head 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) + 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/LinkedList/__init__.py b/data_structures/linked_list/__init__.py similarity index 92% rename from data_structures/LinkedList/__init__.py rename to data_structures/linked_list/__init__.py index 1d220599fb16..a050adba42b2 100644 --- a/data_structures/LinkedList/__init__.py +++ b/data_structures/linked_list/__init__.py @@ -3,6 +3,7 @@ def __init__(self, item, next): self.item = item self.next = next + class LinkedList: def __init__(self): self.head = None @@ -19,4 +20,4 @@ def remove(self): return item def is_empty(self): - return self.head == None + return self.head is None 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..cf523f0a4380 --- /dev/null +++ b/data_structures/linked_list/circular_linked_list.py @@ -0,0 +1,186 @@ +from typing import Any + + +class Node: + """ + Class to represent a single node. + + Each node has following attributes + * data + * next_ptr + """ + + def __init__(self, data: Any): + self.data = data + self.next_ptr = None + + +class CircularLinkedList: + """ + Class to represent the CircularLinkedList. + + CircularLinkedList has following attributes. + * head + * length + """ + + def __init__(self): + self.head = None + self.length = 0 + + def __len__(self) -> int: + """ + Dunder method to return length of the CircularLinkedList + >>> cll = CircularLinkedList() + >>> len(cll) + 0 + >>> cll.append(1) + >>> len(cll) + 1 + """ + return self.length + + def __str__(self) -> str: + """ + Dunder method to represent the string representation of the CircularLinkedList + >>> cll = CircularLinkedList() + >>> print(cll) + Empty linked list + >>> cll.append(1) + >>> cll.append(2) + >>> print(cll) + => + """ + current_node = self.head + if not current_node: + return "Empty linked list" + + results = [current_node.data] + current_node = current_node.next_ptr + + while current_node != self.head: + results.append(current_node.data) + current_node = current_node.next_ptr + + return " => ".join(f"" for result in results) + + def append(self, data: Any) -> None: + """ + Adds a node with given data to the end of the CircularLinkedList + >>> cll = CircularLinkedList() + >>> cll.append(1) + >>> print(f"{len(cll)}: {cll}") + 1: + >>> cll.append(2) + >>> print(f"{len(cll)}: {cll}") + 2: => + """ + current_node = self.head + + new_node = Node(data) + new_node.next_ptr = new_node + + if current_node: + while current_node.next_ptr != self.head: + current_node = current_node.next_ptr + + current_node.next_ptr = new_node + new_node.next_ptr = self.head + else: + self.head = new_node + + self.length += 1 + + def prepend(self, data: Any) -> None: + """ + Adds a ndoe with given data to the front of the CircularLinkedList + >>> cll = CircularLinkedList() + >>> cll.prepend(1) + >>> cll.prepend(2) + >>> print(f"{len(cll)}: {cll}") + 2: => + """ + current_node = self.head + + new_node = Node(data) + new_node.next_ptr = new_node + + if current_node: + while current_node.next_ptr != self.head: + current_node = current_node.next_ptr + + current_node.next_ptr = new_node + new_node.next_ptr = self.head + + self.head = new_node + self.length += 1 + + def delete_front(self) -> None: + """ + Removes the 1st node from the CircularLinkedList + >>> cll = CircularLinkedList() + >>> cll.delete_front() + Traceback (most recent call last): + ... + IndexError: Deleting from an empty list + >>> cll.append(1) + >>> cll.append(2) + >>> print(f"{len(cll)}: {cll}") + 2: => + >>> cll.delete_front() + >>> print(f"{len(cll)}: {cll}") + 1: + """ + if not self.head: + raise IndexError("Deleting from an empty list") + + current_node = self.head + + if current_node.next_ptr == current_node: + self.head, self.length = None, 0 + else: + while current_node.next_ptr != self.head: + current_node = current_node.next_ptr + + current_node.next_ptr = self.head.next_ptr + self.head = self.head.next_ptr + + self.length -= 1 + + def delete_rear(self) -> None: + """ + Removes the last node from the CircularLinkedList + >>> cll = CircularLinkedList() + >>> cll.delete_rear() + Traceback (most recent call last): + ... + IndexError: Deleting from an empty list + >>> cll.append(1) + >>> cll.append(2) + >>> print(f"{len(cll)}: {cll}") + 2: => + >>> cll.delete_rear() + >>> print(f"{len(cll)}: {cll}") + 1: + """ + if not self.head: + raise IndexError("Deleting from an empty list") + + temp_node, current_node = self.head, self.head + + if current_node.next_ptr == current_node: + self.head, self.length = None, 0 + else: + while current_node.next_ptr != self.head: + temp_node = current_node + current_node = current_node.next_ptr + + temp_node.next_ptr = current_node.next_ptr + + self.length -= 1 + + +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..b2e73a8f789b --- /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 -> {0}, Next -> {1}".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 new file mode 100644 index 000000000000..e449ed6ec8ac --- /dev/null +++ b/data_structures/linked_list/doubly_linked_list.py @@ -0,0 +1,84 @@ +""" +- 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 LinkedList: # making main class named linked list + 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(): # Set the first element added to be the tail + self.tail = newLink + 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) + # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be + # removed + self.head.previous = None + 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 + current = self.head + while current is not 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(f"{self.value}", end=" ") 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/is_palindrome.py b/data_structures/linked_list/is_palindrome.py new file mode 100644 index 000000000000..acc87c1c272b --- /dev/null +++ b/data_structures/linked_list/is_palindrome.py @@ -0,0 +1,77 @@ +def is_palindrome(head): + if not head: + return True + # split the list to two parts + fast, slow = head.next, head + while fast and fast.next: + fast = fast.next.next + slow = slow.next + second = slow.next + slow.next = None # Don't forget here! But forget still works! + # reverse the second part + node = None + while second: + nxt = second.next + second.next = node + node = second + second = nxt + # compare two parts + # second part has the same or one less node + while node: + if node.val != head.val: + return False + node = node.next + head = head.next + return True + + +def is_palindrome_stack(head): + if not head or not head.next: + return True + + # 1. Get the midpoint (slow) + slow = fast = cur = head + while fast and fast.next: + fast, slow = fast.next.next, slow.next + + # 2. Push the second half into the stack + stack = [slow.val] + while slow.next: + slow = slow.next + stack.append(slow.val) + + # 3. Comparison + while stack: + if stack.pop() != cur.val: + return False + cur = cur.next + + return True + + +def is_palindrome_dict(head): + if not head or not head.next: + return True + d = {} + pos = 0 + while head: + if head.val in d.keys(): + d[head.val].append(pos) + else: + d[head.val] = [pos] + head = head.next + pos += 1 + checksum = pos - 1 + middle = 0 + for v in d.values(): + if len(v) % 2 != 0: + middle += 1 + else: + step = 0 + for i in range(0, len(v)): + if v[i] + v[len(v) - 1 - step] != checksum: + return False + step += 1 + if middle > 1: + return False + return True 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..b845d2f19c20 --- /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..c3a72b6b7a23 --- /dev/null +++ b/data_structures/linked_list/print_reverse.py @@ -0,0 +1,55 @@ +# Program to print the elements of a linked list in reverse + + +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 + + +def print_reverse(head_node): + """Prints the elements of the given Linked List in reverse order""" + + # If reached end of the List + if head_node is None: + return None + else: + # Recurse + print_reverse(head_node.next) + print(head_node.data) + + +list_data = [14, 52, 14, 12, 43] +linked_list = make_linked_list(list_data) +print("Linked List:") +print(linked_list) +print("Elements in Reverse:") +print_reverse(linked_list) diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py new file mode 100644 index 000000000000..c90cfb6e59a9 --- /dev/null +++ b/data_structures/linked_list/singly_linked_list.py @@ -0,0 +1,141 @@ +class Node: # create a Node + def __init__(self, data): + self.data = data # given data + self.next = None # given next to None + + def __repr__(self): # string representation of a Node + return f"Node({self.data})" + + +class LinkedList: + def __init__(self): + self.head = None # initialize head to None + + def insert_tail(self, data) -> None: + if self.head is None: + self.insert_head(data) # if this is first node, call insert_head + else: + temp = self.head + while temp.next: # traverse to last node + temp = temp.next + temp.next = Node(data) # create node & link to tail + + def insert_head(self, data) -> None: + new_node = Node(data) # create a new node + if self.head: + new_node.next = self.head # link new_node to head + self.head = new_node # make NewNode as head + + def print_list(self) -> None: # print every node data + temp = self.head + while temp: + print(temp.data) + temp = temp.next + + def delete_head(self): # delete from head + temp = self.head + if self.head: + self.head = self.head.next + temp.next = None + return temp + + def delete_tail(self): # delete from tail + temp = self.head + if self.head: + if self.head.next is None: # if head is the only Node in the Linked List + self.head = None + else: + while temp.next.next: # find the 2nd last element + temp = temp.next + # (2nd last element).next = None and temp = last element + temp.next, temp = None, temp.next + return temp + + def is_empty(self) -> bool: + return self.head is None # return True if head is none + + def reverse(self): + prev = None + current = self.head + + while current: + # Store the current node's next node. + next_node = current.next + # Make the current node's next point backwards + current.next = prev + # Make the previous node be the current node + prev = current + # 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 + + def __repr__(self): # String representation/visualization of a Linked Lists + current = self.head + string_repr = "" + while current: + string_repr += f"{current} --> " + current = current.next + # END represents end of the LinkedList + return string_repr + "END" + + # Indexing Support. Used to get a node at particular position + def __getitem__(self, index): + current = self.head + + # If LinkedList is empty + if current is None: + raise IndexError("The Linked List is empty") + + # Move Forward 'index' times + for _ in range(index): + # If the LinkedList ends before reaching specified node + if current.next is None: + raise IndexError("Index out of range.") + current = current.next + return current + + # Used to change the data of a particular node + def __setitem__(self, index, data): + current = self.head + # If list is empty + if current is None: + raise IndexError("The Linked List is empty") + for i in range(index): + if current.next is None: + raise IndexError("Index out of range.") + current = current.next + current.data = data + + +def main(): + A = LinkedList() + A.insert_head(input("Inserting 1st at head ").strip()) + A.insert_head(input("Inserting 2nd at head ").strip()) + print("\nPrint list:") + A.print_list() + A.insert_tail(input("\nInserting 1st at tail ").strip()) + A.insert_tail(input("Inserting 2nd at tail ").strip()) + print("\nPrint list:") + A.print_list() + print("\nDelete head") + A.delete_head() + print("Delete tail") + A.delete_tail() + print("\nPrint list:") + A.print_list() + print("\nReverse linked list") + A.reverse() + print("\nPrint list:") + A.print_list() + print("\nString representation of linked list:") + print(A) + print("\nReading/changing Node data using indexing:") + print(f"Element at Position 1: {A[1]}") + A[1] = input("Enter New Value: ").strip() + print("New list:") + print(A) + + +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..195d7ffc6b91 --- /dev/null +++ b/data_structures/linked_list/skip_list.py @@ -0,0 +1,443 @@ +""" +Based on "Skip Lists: A Probabilistic Alternative to Balanced Trees" by William Pugh +https://epaperpress.com/sortsearch/download/skiplist.pdf +""" + +from random import random +from typing import Generic, List, Optional, Tuple, 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/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..229a67ebb8be --- /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/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..614c60cd1ae2 --- /dev/null +++ b/data_structures/queue/linked_queue.py @@ -0,0 +1,74 @@ +""" A Queue using a Linked List like structure """ +from typing import Any, Optional + + +class Node: + def __init__(self, data: Any, next: Optional["Node"] = None): + self.data: Any = data + self.next: Optional["Node"] = next + + +class LinkedQueue: + """ + Linked List Queue implementing put (to end of queue), + get (from front of queue) and is_empty + + >>> 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: get from empty queue + """ + + def __init__(self) -> None: + self.front: Optional[Node] = None + self.rear: Optional[Node] = None + + def is_empty(self) -> bool: + """ returns boolean describing if queue is empty """ + return self.front is None + + def put(self, item: Any) -> None: + """ append item to rear of queue """ + node: Node = Node(item) + if self.is_empty(): + # the queue contains just the single element + self.front = node + self.rear = node + else: + # not empty, so we add it to the rear of the queue + assert isinstance(self.rear, Node) + self.rear.next = node + self.rear = node + + def get(self) -> Any: + """ returns and removes item at front of queue """ + if self.is_empty(): + raise IndexError("get from empty queue") + else: + # "remove" element by having front point to the next one + assert isinstance(self.front, Node) + node: Node = self.front + self.front = node.next + if self.front is None: + self.rear = None + + return node.data diff --git a/data_structures/Queue/QueueOnList.py b/data_structures/queue/queue_on_list.py similarity index 74% rename from data_structures/Queue/QueueOnList.py rename to data_structures/queue/queue_on_list.py index c8d0b41de5d5..485cf0b6f7a3 100644 --- a/data_structures/Queue/QueueOnList.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/QueueOnPseudoStack.py b/data_structures/queue/queue_on_pseudo_stack.py similarity index 80% rename from data_structures/Queue/QueueOnPseudoStack.py rename to data_structures/queue/queue_on_pseudo_stack.py index b69fbcc988f7..7fa2fb2566af 100644 --- a/data_structures/Queue/QueueOnPseudoStack.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 new file mode 100644 index 000000000000..f6995cf98977 --- /dev/null +++ b/data_structures/stacks/__init__.py @@ -0,0 +1,22 @@ +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 new file mode 100644 index 000000000000..7aacd5969277 --- /dev/null +++ b/data_structures/stacks/balanced_parentheses.py @@ -0,0 +1,23 @@ +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(): + return False + stack.pop() + return stack.is_empty() + + +if __name__ == "__main__": + examples = ["((()))", "((())", "(()))"] + print("Balanced parentheses demonstration:\n") + for example in examples: + print(example + ": " + str(balanced_parentheses(example))) diff --git a/data_structures/Stacks/infix_to_postfix_conversion.py b/data_structures/stacks/infix_to_postfix_conversion.py similarity index 56% rename from data_structures/Stacks/infix_to_postfix_conversion.py rename to data_structures/stacks/infix_to_postfix_conversion.py index 75211fed258d..61114402377a 100644 --- a/data_structures/Stacks/infix_to_postfix_conversion.py +++ b/data_structures/stacks/infix_to_postfix_conversion.py @@ -1,10 +1,8 @@ -from __future__ import print_function -from __future__ import absolute_import import string -from .Stack import Stack +from .stack import Stack -__author__ = 'Omkar Pathak' +__author__ = "Omkar Pathak" def is_operand(char): @@ -17,9 +15,7 @@ def precedence(char): https://en.wikipedia.org/wiki/Order_of_operations """ - dictionary = {'+': 1, '-': 1, - '*': 2, '/': 2, - '^': 3} + dictionary = {"+": 1, "-": 1, "*": 2, "/": 2, "^": 3} return dictionary.get(char, -1) @@ -36,29 +32,28 @@ def infix_to_postfix(expression): for char in expression: if is_operand(char): postfix.append(char) - elif char not in {'(', ')'}: - while (not stack.is_empty() - and precedence(char) <= precedence(stack.peek())): + 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') + if stack.peek() != "(": + raise ValueError("Mismatched parentheses") stack.pop() while not stack.is_empty(): postfix.append(stack.pop()) - return ' '.join(postfix) + return " ".join(postfix) -if __name__ == '__main__': - expression = 'a+b*(c^d-e)^(f+g*h)-i' +if __name__ == "__main__": + 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 new file mode 100644 index 000000000000..5aa41a119528 --- /dev/null +++ b/data_structures/stacks/infix_to_prefix_conversion.py @@ -0,0 +1,95 @@ +""" +Output: + +Enter an Infix Equation = a + b ^c + Symbol | Stack | Postfix +---------------------------- + c | | c + ^ | ^ | c + b | ^ | cb + + | + | cb^ + a | + | cb^a + | | cb^a+ + + 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 + + # Print table header for output + 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 + 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 + + 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 + + return "".join(Postfix) # return Postfix as str + + +def infix_2_prefix(Infix): + 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 __name__ == "__main__": + 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..18ba87ddc221 --- /dev/null +++ b/data_structures/stacks/linked_stack.py @@ -0,0 +1,67 @@ +""" A Stack using a Linked List like structure """ +from typing import Any, Optional + + +class Node: + def __init__(self, data: Any, next: Optional["Node"] = None): + self.data: Any = data + self.next: Optional["Node"] = next + + +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: Optional[Node] = None + + def is_empty(self) -> bool: + """ returns boolean describing if stack is empty """ + return self.top is None + + def push(self, item: Any) -> None: + """ append item to top of stack """ + node: Node = Node(item) + if self.is_empty(): + self.top = node + else: + # each node points to the item "lower" in the stack + node.next = self.top + self.top = node + + def pop(self) -> Any: + """ returns and removes item at top of stack """ + if self.is_empty(): + raise IndexError("pop from empty stack") + else: + # "remove" element by having top point to the next one + assert isinstance(self.top, Node) + node: Node = self.top + self.top = node.next + return node.data 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 new file mode 100644 index 000000000000..4cee0ba380b0 --- /dev/null +++ b/data_structures/stacks/postfix_evaluation.py @@ -0,0 +1,70 @@ +""" +Output: + +Enter a Postfix Equation (space separated) = 5 6 9 * + + Symbol | Action | Stack +----------------------------------- + 5 | push(5) | 5 + 6 | push(6) | 5,6 + 9 | push(9) | 5,6,9 + | pop(9) | 5,6 + | pop(6) | 5 + * | push(6*9) | 5,54 + | pop(54) | 5 + | pop(5) | + + | push(5+54) | 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 + + # print table header + 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 + else: + B = Stack.pop() # pop stack + print( + "".rjust(8), ("pop(" + B + ")").ljust(12), ",".join(Stack), sep=" | " + ) # output in tabular format + + A = Stack.pop() # pop stack + print( + "".rjust(8), ("pop(" + A + ")").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 + print( + x.rjust(8), + ("push(" + A + x + B + ")").ljust(12), + ",".join(Stack), + sep=" | ", + ) # output in tabular format + + return int(Stack[0]) + + +if __name__ == "__main__": + 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 similarity index 66% rename from data_structures/Stacks/stack.py rename to data_structures/stacks/stack.py index 66af8c025d8c..baa0857eec0a 100644 --- a/data_structures/Stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -1,8 +1,7 @@ -from __future__ import print_function -__author__ = 'Omkar Pathak' +__author__ = "Omkar Pathak" -class Stack(object): +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 @@ -17,7 +16,7 @@ def __init__(self, limit=10): self.limit = limit def __bool__(self): - return not bool(self.stack) + return bool(self.stack) def __str__(self): return str(self.stack) @@ -33,7 +32,7 @@ def pop(self): if self.stack: return self.stack.pop() else: - raise IndexError('pop from an empty stack') + raise IndexError("pop from an empty stack") def peek(self): """ Peek at the top-most element of the stack.""" @@ -48,22 +47,29 @@ def size(self): """ 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 -if __name__ == '__main__': +if __name__ == "__main__": stack = Stack() for i in range(10): 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())) + 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())) 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())) + print("After push(100), the stack is now: " + str(stack)) + print("is_empty(): " + str(stack.is_empty())) + print("size(): " + str(stack.size())) + num = 5 + if num in stack: + print(f"{num} is in 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 new file mode 100644 index 000000000000..cc2adfdd6c21 --- /dev/null +++ b/data_structures/stacks/stock_span_problem.py @@ -0,0 +1,53 @@ +""" +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 +on the current day is less than or equal to its price on the given day. +""" + + +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/trie.py b/data_structures/trie/trie.py new file mode 100644 index 000000000000..5a560b97c293 --- /dev/null +++ b/data_structures/trie/trie.py @@ -0,0 +1,127 @@ +""" +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. +""" + + +class TrieNode: + def __init__(self): + self.nodes = dict() # Mapping from char to TrieNode + self.is_leaf = False + + def insert_many(self, words: [str]): + """ + Inserts a list of words into the Trie + :param words: list of string words + :return: None + """ + for word in words: + self.insert(word) + + def insert(self, word: str): + """ + Inserts a word into the Trie + :param word: word to be inserted + :return: None + """ + curr = self + for char in word: + if char not in curr.nodes: + curr.nodes[char] = TrieNode() + curr = curr.nodes[char] + curr.is_leaf = True + + def find(self, word: str) -> bool: + """ + Tries to find word in a Trie + :param word: word to look for + :return: Returns True if word is found, False otherwise + """ + curr = self + for char in word: + if char not in curr.nodes: + return False + 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 _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 + :param word: Word variable should be empty at start + :return: None + """ + if node.is_leaf: + print(word, end=" ") + + for key, value in node.nodes.items(): + print_words(value, word + key) + + +def test_trie(): + words = "banana bananas bandana band apple all beast".split() + root = TrieNode() + root.insert_many(words) + # 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()) + + +if __name__ == "__main__": + main() diff --git a/data_structures/Queue/__init__.py b/digital_image_processing/__init__.py similarity index 100% rename from data_structures/Queue/__init__.py rename to digital_image_processing/__init__.py diff --git a/digital_image_processing/change_contrast.py b/digital_image_processing/change_contrast.py new file mode 100644 index 000000000000..c7da52298ae2 --- /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: float) -> Image: + """ + Function to change contrast + """ + factor = (259 * (level + 255)) / (255 * (259 - level)) + + def contrast(c: int) -> float: + """ + Fundamental Transformation/Operation that'll be performed on + every bit. + """ + return 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..cba503938aec --- /dev/null +++ b/digital_image_processing/convert_to_negative.py @@ -0,0 +1,30 @@ +""" + Implemented an algorithm using opencv to convert a colored image into its negative +""" + +from cv2 import imread, imshow, waitKey, destroyAllWindows + + +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/data_structures/UnionFind/__init__.py b/digital_image_processing/dithering/__init__.py similarity index 100% rename from data_structures/UnionFind/__init__.py rename to digital_image_processing/dithering/__init__.py diff --git a/digital_image_processing/dithering/burkes.py b/digital_image_processing/dithering/burkes.py new file mode 100644 index 000000000000..54a243bd255a --- /dev/null +++ b/digital_image_processing/dithering/burkes.py @@ -0,0 +1,87 @@ +""" +Implementation Burke's algorithm (dithering) +""" +from cv2 import destroyAllWindows, imread, imshow, waitKey +import numpy as np + + +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/data_structures/__init__.py b/digital_image_processing/edge_detection/__init__.py similarity index 100% rename from data_structures/__init__.py rename to digital_image_processing/edge_detection/__init__.py diff --git a/digital_image_processing/edge_detection/canny.py b/digital_image_processing/edge_detection/canny.py new file mode 100644 index 000000000000..6f98fee6308e --- /dev/null +++ b/digital_image_processing/edge_detection/canny.py @@ -0,0 +1,117 @@ +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/__init__.py b/digital_image_processing/filters/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/digital_image_processing/filters/bilateral_filter.py b/digital_image_processing/filters/bilateral_filter.py new file mode 100644 index 000000000000..753d6ddb7a3f --- /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 cv2 +import numpy as np +import math +import sys + + +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..ec500d940366 --- /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 imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey +from numpy import array, zeros, ravel, pad, dot, uint8 + + +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..79026ddcc57a --- /dev/null +++ b/digital_image_processing/filters/gaussian_filter.py @@ -0,0 +1,53 @@ +""" +Implementation of gaussian filter algorithm +""" +from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey +from numpy import pi, mgrid, exp, square, zeros, ravel, dot, uint8 +from itertools import product + + +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 new file mode 100644 index 000000000000..151ef8a55df1 --- /dev/null +++ b/digital_image_processing/filters/median_filter.py @@ -0,0 +1,42 @@ +""" +Implementation of median filter algorithm +""" + +from cv2 import imread, cvtColor, COLOR_BGR2GRAY, imshow, waitKey +from numpy import zeros_like, ravel, sort, multiply, divide, int8 + + +def median_filter(gray_img, mask=3): + """ + :param gray_img: gray image + :param mask: mask size + :return: image with median filter + """ + # set image borders + bd = int(mask / 2) + # copy image size + 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]) + # calculate mask median + median = sort(kernel)[int8(divide((multiply(mask, mask)), 2) + 1)] + median_img[i, j] = median + return median_img + + +if __name__ == "__main__": + # read original image + img = imread("../image_data/lena.jpg") + # turn image in gray scale value + gray = cvtColor(img, COLOR_BGR2GRAY) + + # get values with two different mask size + median3x3 = median_filter(gray, 3) + median5x5 = median_filter(gray, 5) + + # show result images + 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..822d49fe38a1 --- /dev/null +++ b/digital_image_processing/filters/sobel_filter.py @@ -0,0 +1,38 @@ +# @Author : lightXu +# @File : sobel_filter.py +# @Time : 2019/7/8 0008 下午 16:26 +import numpy as np +from cv2 import imread, cvtColor, COLOR_BGR2GRAY, 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/histogram_stretch.py b/digital_image_processing/histogram_equalization/histogram_stretch.py new file mode 100644 index 000000000000..d4a6c08418ee --- /dev/null +++ b/digital_image_processing/histogram_equalization/histogram_stretch.py @@ -0,0 +1,64 @@ +""" +Created on Fri Sep 28 15:22:29 2018 + +@author: Binish125 +""" +import copy +import os + +import numpy as np + +import cv2 +import matplotlib.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/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/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/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..a34e51e56310 --- /dev/null +++ b/digital_image_processing/index_calculation.py @@ -0,0 +1,568 @@ +# 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): + """ + 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..4fd222ccd6c6 --- /dev/null +++ b/digital_image_processing/resize/resize.py @@ -0,0 +1,69 @@ +""" Multiple image resizing techniques """ +import numpy as np +from cv2 import imread, imshow, waitKey, destroyAllWindows + + +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..37b45ca39897 --- /dev/null +++ b/digital_image_processing/rotation/rotation.py @@ -0,0 +1,52 @@ +from matplotlib import pyplot as plt +import numpy as np +import cv2 + + +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..e91d57d0379c --- /dev/null +++ b/digital_image_processing/sepia.py @@ -0,0 +1,48 @@ +""" + Implemented an algorithm using opencv to tone an image with sepia technique +""" + +from cv2 import imread, imshow, waitKey, destroyAllWindows + + +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..fe8890de9a31 --- /dev/null +++ b/digital_image_processing/test_digital_image_processing.py @@ -0,0 +1,93 @@ +""" +PyTest's for Digital Image Processing +""" + +import digital_image_processing.edge_detection.canny as canny +import digital_image_processing.filters.gaussian_filter as gg +import digital_image_processing.filters.median_filter as med +import digital_image_processing.filters.sobel_filter as sob +import digital_image_processing.filters.convolve as conv +import digital_image_processing.change_contrast as cc +import digital_image_processing.convert_to_negative as cn +import digital_image_processing.sepia as sp +import digital_image_processing.dithering.burkes as bs +import digital_image_processing.resize.resize as rs +from cv2 import imread, cvtColor, COLOR_BGR2GRAY +from numpy import array, uint8 +from PIL import Image + +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..11b16975c8b4 --- /dev/null +++ b/divide_and_conquer/convex_hull.py @@ -0,0 +1,427 @@ +""" +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. + +""" + + +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): + """ + 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 = [] + if list_of_tuples: + for p in list_of_tuples: + 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): + """ + 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 points: + raise ValueError(f"Expecting a list of points but got {points}") + + if isinstance(points, set): + points = list(points) + + try: + if hasattr(points, "__iter__") and not isinstance(points[0], Point): + if isinstance(points[0], (list, tuple)): + points = _construct_points(points) + else: + raise ValueError( + "Expecting an iterable of type Point, list or tuple. " + f"Found objects of type {type(points[0])} instead" + ) + elif not hasattr(points, "__iter__"): + raise ValueError( + f"Expecting an iterable object but got an non-iterable type {points}" + ) + except TypeError: + print("Expecting an iterable of type Point, list or tuple.") + raise + + return points + + +def _det(a, b, c): + """ + 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): + """ + 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): + """ + 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, left, right, convex_set): + """ + + 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 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_recursive = convex_hull_recursive(points) + results_bf = convex_hull_bf(points) + assert results_bf == results_recursive + + print(results_bf) + + +if __name__ == "__main__": + main() 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/max_subarray_sum.py b/divide_and_conquer/max_subarray_sum.py new file mode 100644 index 000000000000..9e81c83649a6 --- /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..d6693eb36a0a --- /dev/null +++ b/divide_and_conquer/mergesort.py @@ -0,0 +1,48 @@ +def merge(a, b, m, e): + l = a[b : m + 1] + r = a[m + 1 : e + 1] + k = b + i = 0 + j = 0 + while i < len(l) and j < len(r): + # change sign for Descending order + if l[i] < r[j]: + a[k] = l[i] + i += 1 + else: + a[k] = r[j] + j += 1 + k += 1 + while i < len(l): + a[k] = l[i] + i += 1 + k += 1 + while j < len(r): + a[k] = r[j] + j += 1 + k += 1 + return a + + +def mergesort(a, b, e): + """ + >>> mergesort([3,2,1],0,2) + [1, 2, 3] + >>> mergesort([3,2,1,0,1,2,3,5,4],0,8) + [0, 1, 1, 2, 2, 3, 3, 4, 5] + """ + if b < e: + m = (b + e) // 2 + # print("ms1",a,b,m) + mergesort(a, b, m) + # print("ms2",a,m+1,e) + mergesort(a, m + 1, e) + # print("m",a,b,m,e) + merge(a, b, m, e) + return a + + +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..ea54b0f52d29 --- /dev/null +++ b/divide_and_conquer/strassen_matrix_multiplication.py @@ -0,0 +1,170 @@ +import math +from typing import List, Tuple + + +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/FloydWarshall.py b/dynamic_programming/FloydWarshall.py deleted file mode 100644 index 038499ca03b6..000000000000 --- a/dynamic_programming/FloydWarshall.py +++ /dev/null @@ -1,37 +0,0 @@ -import math - -class Graph: - - 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 - - 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): - 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__': - 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.floyd_warshall() - graph.showMin(1,4) - graph.showMin(0,3) 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 new file mode 100644 index 000000000000..2994db5b5e1e --- /dev/null +++ b/dynamic_programming/bitmask.py @@ -0,0 +1,93 @@ +""" + +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. +Find the total no of ways in which the tasks can be distributed. +""" +from collections import defaultdict + + +class AssignmentUsingBitmask: + 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 + + # final_mask is used to check if all persons are included by setting all bits + # to 1 + self.final_mask = (1 << len(task_performed)) - 1 + + def CountWaysUtil(self, mask, task_no): + + # if mask == self.finalmask all persons are distributed tasks, return 1 + if mask == self.final_mask: + return 1 + + # 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][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) + + # 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]: + + # if p is already given a task + if mask & (1 << p): + continue + + # assign this task to p and change the mask value. And recursively + # assign tasks with the new mask value. + total_ways_util += self.CountWaysUtil(mask | (1 << p), task_no + 1) + + # save the value. + self.dp[mask][task_no] = total_ways_util + + return self.dp[mask][task_no] + + def countNoOfWays(self, task_performed): + + # Store the list of persons for each task + for i in range(len(task_performed)): + for j in task_performed[i]: + self.task[j].append(i) + + # call the function to fill the DP table, final answer is stored in dp[0][1] + return self.CountWaysUtil(0, 1) + + +if __name__ == "__main__": + + total_tasks = 5 # total no of tasks (the value of N) + + # the list of tasks that can be done by M persons. + task_performed = [[1, 3, 4], [1, 2, 5], [3, 4]] + print( + AssignmentUsingBitmask(task_performed, total_tasks).countNoOfWays( + task_performed + ) + ) + """ + For the particular example the tasks can be distributed as + (1,2,3), (1,2,4), (1,5,3), (1,5,4), (3,1,4), + (3,2,4), (3,5,4), (4,1,3), (4,2,3), (4,5,3) + total 10 + """ diff --git a/dynamic_programming/climbing_stairs.py b/dynamic_programming/climbing_stairs.py new file mode 100644 index 000000000000..79605261f981 --- /dev/null +++ b/dynamic_programming/climbing_stairs.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + + +def climb_stairs(n: int) -> 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 0116df0c024e..2d7106f0cc6f 100644 --- a/dynamic_programming/coin_change.py +++ b/dynamic_programming/coin_change.py @@ -5,22 +5,39 @@ the given types of coins? https://www.hackerrank.com/challenges/coin-change/problem """ -from __future__ import print_function + + def dp_count(S, m, n): + """ + >>> dp_count([1, 2, 3], 3, 4) + 4 + >>> dp_count([1, 2, 3], 3, 7) + 8 + >>> dp_count([2, 5, 3, 6], 4, 10) + 5 + >>> dp_count([10], 1, 99) + 0 + >>> dp_count([4, 5, 6], 3, 0) + 1 + """ + + # table[i] represents the number of ways to get to amount i table = [0] * (n + 1) - # Base case (If given value is 0) + # There is exactly 1 way to get to zero(You pick no coins). table[0] = 1 # Pick all coins one by one and update table[] values # after the index greater than or equal to the value of the # picked coin - for i in range(0, m): - for j in range(S[i], n + 1): - table[j] += table[j - S[i]] + for coin_val in S: + for j in range(coin_val, n + 1): + table[j] += table[j - coin_val] 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..0269014e7a18 --- /dev/null +++ b/dynamic_programming/factorial.py @@ -0,0 +1,38 @@ +# Factorial of a number using memoization +result = [-1] * 10 +result[0] = result[1] = 1 + + +def factorial(num): + """ + >>> factorial(7) + 5040 + >>> factorial(-1) + 'Number should not be negative.' + >>> [factorial(i) for i in range(5)] + [1, 1, 2, 6, 24] + """ + + if num < 0: + return "Number should not be negative." + if result[num] != -1: + return result[num] + else: + result[num] = num * factorial(num - 1) + # uncomment the following to see how recalculations are avoided + # print(result) + return result[num] + + +# factorial of num +# uncomment the following to see how recalculations are avoided +##result=[-1]*10 +##result[0]=result[1]=1 +##print(factorial(5)) +# print(factorial(3)) +# print(factorial(7)) + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/dynamic_programming/fastfibonacci.py b/dynamic_programming/fast_fibonacci.py similarity index 96% rename from dynamic_programming/fastfibonacci.py rename to dynamic_programming/fast_fibonacci.py index 66d2b2ff0a54..77094a40384b 100644 --- a/dynamic_programming/fastfibonacci.py +++ b/dynamic_programming/fast_fibonacci.py @@ -1,8 +1,9 @@ +#!/usr/bin/python + """ 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 diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index b453ce255853..45319269f5d4 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -1,11 +1,9 @@ """ 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 +14,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 +58,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 new file mode 100644 index 000000000000..a4b6c6a82568 --- /dev/null +++ b/dynamic_programming/floyd_warshall.py @@ -0,0 +1,42 @@ +import math + + +class Graph: + 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 + + 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): + 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__": + 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.floyd_warshall() + 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..15210146bf66 --- /dev/null +++ b/dynamic_programming/fractional_knapsack.py @@ -0,0 +1,27 @@ +from itertools import accumulate +from bisect import bisect + + +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..eadb73c61a39 --- /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 typing import List, Tuple + + +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..ec8c5bf62d7d 100644 --- a/dynamic_programming/integer_partition.py +++ b/dynamic_programming/integer_partition.py @@ -1,45 +1,36 @@ -from __future__ import print_function - -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..edeacc3124fa --- /dev/null +++ b/dynamic_programming/iterating_through_submasks.py @@ -0,0 +1,60 @@ +""" +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 typing import List + + +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 50% rename from dynamic_programming/k_means_clustering_tensorflow.py rename to dynamic_programming/k_means_clustering_tensorflow.py_tf index ad495c71a978..4fbcedeaa0dc 100644 --- a/dynamic_programming/k_means_clustering_tensorflow.py +++ b/dynamic_programming/k_means_clustering_tensorflow.py_tf @@ -1,5 +1,5 @@ import tensorflow as tf -from random import choice, shuffle +from random import shuffle from numpy import array @@ -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..1987dc35fd03 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): - ''' + + +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..a7206b221d96 100644 --- a/dynamic_programming/longest_common_subsequence.py +++ b/dynamic_programming/longest_common_subsequence.py @@ -1,37 +1,82 @@ """ 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. +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..81b7f8f8ff17 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -1,42 +1,56 @@ -''' +""" Author : Mehdi ALAOUI 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. +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 +""" +from typing import List + + +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] + """ + 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_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..46790a5a8d41 --- /dev/null +++ b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py @@ -0,0 +1,54 @@ +############################# +# 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 typing import 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: 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..f3b5705b7de2 100644 --- a/dynamic_programming/longest_sub_array.py +++ b/dynamic_programming/longest_sub_array.py @@ -1,33 +1,32 @@ -''' -Auther : Yvonne +""" +Author : Yvonne 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 +""" 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 011e85755d36..9411bc704f1c 100644 --- a/dynamic_programming/matrix_chain_order.py +++ b/dynamic_programming/matrix_chain_order.py @@ -1,48 +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)] - for i in range(1,N): - Matrix[i][i]=0 + 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_sub_array.py b/dynamic_programming/max_sub_array.py index 58711f22ce90..7350eaf373cb 100644 --- a/dynamic_programming/max_sub_array.py +++ b/dynamic_programming/max_sub_array.py @@ -1,60 +1,93 @@ """ 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 typing import List + + +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) - + 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() +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 + import matplotlib.pyplot as plt + from random import randint + + 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_partition.py b/dynamic_programming/minimum_partition.py index 18aa1faa2fa6..d5750326fea4 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): + for j in range(int(s / 2), -1, -1): if dp[n][j] == True: - diff = s-2*j - break; + diff = s - 2 * j + break return diff diff --git a/dynamic_programming/optimal_binary_search_tree.py b/dynamic_programming/optimal_binary_search_tree.py new file mode 100644 index 000000000000..f33ca01bd933 --- /dev/null +++ b/dynamic_programming/optimal_binary_search_tree.py @@ -0,0 +1,145 @@ +#!/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 l in range(2, n + 1): # l is an interval length + for i in range(n - l + 1): + j = i + l - 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 new file mode 100644 index 000000000000..26af71915833 --- /dev/null +++ b/dynamic_programming/rod_cutting.py @@ -0,0 +1,200 @@ +""" +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 + +The rod-cutting problem is the problem of finding the maximum possible revenue +obtainable from a rod of length ``n`` given a list of prices for each integral piece +of the rod. The maximum revenue can thus be obtained by cutting the rod and selling the +pieces separately or not cutting it at all if the price of it is the maximum obtainable. + +""" + + +def naive_cut_rod_recursive(n: int, prices: list): + """ + Solves the rod-cutting problem via naively without using the benefit of dynamic programming. + The results is the same sub-problems are solved several times leading to an exponential runtime + + Runtime: O(2^n) + + 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`` + + Returns + ------- + The maximum revenue obtainable for a rod of length n given the list of prices for each piece. + + Examples + -------- + >>> 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)}" + ) + + +def main(): + prices = [6, 10, 12, 15, 20, 23] + n = len(prices) + + # 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 + + +if __name__ == "__main__": + main() diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py new file mode 100644 index 000000000000..196b81c22045 --- /dev/null +++ b/dynamic_programming/subset_generation.py @@ -0,0 +1,40 @@ +# Python program to print all subset combinations of n element in given set of r element. +# 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 +def combination_util(arr, n, r, index, data, i): + # Current combination is ready to be printed, + # print it + 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..9394d29dabc0 --- /dev/null +++ b/dynamic_programming/sum_of_subset.py @@ -0,0 +1,36 @@ +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/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..6494114a9072 --- /dev/null +++ b/file_transfer/send_file.py @@ -0,0 +1,38 @@ +if __name__ == "__main__": + import socket # Import socket module + + ONE_CONNECTION_ONLY = ( + True # Set this to False if you wish to continuously accept connections + ) + + filename = "mytext.txt" + 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 ( + ONE_CONNECTION_ONLY + ): # This is to make sure that the program doesn't hang while testing + break + + sock.shutdown(1) + sock.close() diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py new file mode 100644 index 000000000000..34dd9c029be7 --- /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(). + import matplotlib.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/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..224b9404a5b7 --- /dev/null +++ b/geodesy/lamberts_ellipsoidal_distance.py @@ -0,0 +1,83 @@ +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/bezier_curve.py b/graphics/bezier_curve.py new file mode 100644 index 000000000000..512efadf86ee --- /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 typing import List, Tuple +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. + """ + import matplotlib.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/Graphs/a_star.py b/graphs/a_star.py similarity index 52% rename from Graphs/a_star.py rename to graphs/a_star.py index 584222e6f62b..a5d59626b0bc 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 + cell.sort() # to choose the least costliest action so as to move closer to the goal 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 new file mode 100644 index 000000000000..3ecc829946e8 --- /dev/null +++ b/graphs/articulation_points.py @@ -0,0 +1,55 @@ +# Finding Articulation Points in Undirected Graph +def computeAP(l): + n = len(l) + outEdgeCount = 0 + low = [0] * n + visited = [False] * n + isArt = [False] * n + + def dfs(root, at, parent, outEdgeCount): + if parent == root: + outEdgeCount += 1 + visited[at] = True + low[at] = at + + for to in l[at]: + if to == parent: + pass + elif not visited[to]: + outEdgeCount = dfs(root, to, at, outEdgeCount) + low[at] = min(low[at], low[to]) + + # AP found via bridge + if at < low[to]: + isArt[at] = True + # AP found via cycle + if at == low[to]: + isArt[at] = True + else: + low[at] = min(low[at], to) + return outEdgeCount + + for i in range(n): + if not visited[i]: + outEdgeCount = 0 + outEdgeCount = dfs(i, i, -1, outEdgeCount) + isArt[i] = outEdgeCount > 1 + + for x in range(len(isArt)): + if isArt[x] == 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) diff --git a/Graphs/basic-graphs.py b/graphs/basic_graphs.py similarity index 74% rename from Graphs/basic-graphs.py rename to graphs/basic_graphs.py index 6e433b5bd725..8cdde6abc819 100644 --- a/Graphs/basic-graphs.py +++ b/graphs/basic_graphs.py @@ -1,51 +1,40 @@ -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) - -""" --------------------------------------------------------------------------------- - 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 +48,7 @@ def dfs(G, s): - vis, S = set([s]), [s] + vis, S = {s}, [s] print(s) while S: flag = 0 @@ -80,14 +69,14 @@ 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() @@ -139,9 +128,11 @@ def dijk(G, s): from collections import deque -def topo(G, ind=None, Q=[1]): - if ind == None: - ind = [0] * (len(G) + 1) # SInce oth Index is ignored +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: for v in G[u]: ind[v] += 1 @@ -168,9 +159,10 @@ def topo(G, ind=None, Q=[1]): def adjm(): - n, a = 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 @@ -242,10 +234,10 @@ def prim(G, s): def edglist(): - n, m = map(int, raw_input().split(" ")) + n, m = map(int, input().split(" ")) l = [] - for i in xrange(m): - l.append(map(int, raw_input().split(' '))) + for i in range(m): + l.append(map(int, input().split(" "))) return l, n @@ -263,19 +255,28 @@ 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 s[j].update(s[i]) s.pop(i) break + + +# find the isolated node in the graph +def find_isolated_nodes(graph): + isolated = [] + for node in graph: + if not graph[node]: + isolated.append(node) + return isolated diff --git a/graphs/bellman_ford.py b/graphs/bellman_ford.py new file mode 100644 index 000000000000..807e0b0fcdb9 --- /dev/null +++ b/graphs/bellman_ford.py @@ -0,0 +1,56 @@ +from typing import Dict, List + + +def printDist(dist, V): + 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.py b/graphs/bfs.py new file mode 100644 index 000000000000..9d9b1ac037d9 --- /dev/null +++ b/graphs/bfs.py @@ -0,0 +1,46 @@ +""" +BFS. + +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) + +""" + +G = { + "A": ["B", "C"], + "B": ["A", "D", "E"], + "C": ["A", "F"], + "D": ["B"], + "E": ["B", "F"], + "F": ["C", "E"], +} + + +def bfs(graph, start): + """ + >>> ''.join(sorted(bfs(G, 'A'))) + 'ABCDEF' + """ + 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 + + +if __name__ == "__main__": + print(bfs(G, "A")) diff --git a/graphs/bfs_shortest_path.py b/graphs/bfs_shortest_path.py new file mode 100644 index 000000000000..c3664796e677 --- /dev/null +++ b/graphs/bfs_shortest_path.py @@ -0,0 +1,114 @@ +"""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 = [] + # 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.append(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 = [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.append(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/breadth_first_search.py b/graphs/breadth_first_search.py new file mode 100644 index 000000000000..e40ec9d1d06d --- /dev/null +++ b/graphs/breadth_first_search.py @@ -0,0 +1,59 @@ +#!/usr/bin/python + +""" Author: OMKAR PATHAK """ + + +class Graph: + def __init__(self): + self.vertices = {} + + def printGraph(self): + """prints adjacency list representation of graaph""" + for i in self.vertices.keys(): + print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]])) + + def addEdge(self, fromVertex, toVertex): + """adding the edge between two vertices""" + if fromVertex in self.vertices.keys(): + self.vertices[fromVertex].append(toVertex) + else: + self.vertices[fromVertex] = [toVertex] + + def BFS(self, startVertex): + # initialize set for storing already visited vertices + visited = set() + + # 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.add(startVertex) + queue.append(startVertex) + + while queue: + vertex = queue.pop(0) + + # 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__": + 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() + # 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_shortest_path.py b/graphs/breadth_first_search_shortest_path.py new file mode 100644 index 000000000000..514aed6d7211 --- /dev/null +++ b/graphs/breadth_first_search_shortest_path.py @@ -0,0 +1,81 @@ +"""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. +""" +graph = { + "A": ["B", "C", "E"], + "B": ["A", "D", "E"], + "C": ["A", "F", "G"], + "D": ["B"], + "E": ["A", "B", "D"], + "F": ["C"], + "G": ["C"], +} + +from typing import Dict + + +class Graph: + def __init__(self, graph: Dict[str, str], source_vertex: str) -> None: + """Graph is implemented as dictionary of adjancency 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 adjancent_vertex in self.graph[vertex]: + if adjancent_vertex not in visited: + visited.add(adjancent_vertex) + self.parent[adjancent_vertex] = vertex + queue.append(adjancent_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 new file mode 100644 index 000000000000..1ec3e3d1d45f --- /dev/null +++ b/graphs/check_bipartite_graph_bfs.py @@ -0,0 +1,44 @@ +# 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): + queue = [] + visited = [False] * len(l) + color = [-1] * len(l) + + def bfs(): + while queue: + u = queue.pop(0) + visited[u] = True + + for neighbour in l[u]: + + if neighbour == u: + return False + + if color[neighbour] == -1: + color[neighbour] = 1 - color[u] + queue.append(neighbour) + + elif color[neighbour] == color[u]: + return False + + return True + + for i in range(len(l)): + if not visited[i]: + queue.append(i) + color[i] = 0 + if bfs() == 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)) diff --git a/graphs/check_bipartite_graph_dfs.py b/graphs/check_bipartite_graph_dfs.py new file mode 100644 index 000000000000..6fe54a6723c5 --- /dev/null +++ b/graphs/check_bipartite_graph_dfs.py @@ -0,0 +1,33 @@ +# 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(l): + visited = [False] * len(l) + color = [-1] * len(l) + + def dfs(v, c): + visited[v] = True + color[v] = c + for u in l[v]: + if not visited[u]: + dfs(u, 1 - c) + + for i in range(len(l)): + if not visited[i]: + dfs(i, 0) + + for i in range(len(l)): + for j in l[i]: + if color[i] == color[j]: + return False + + return True + + +# Adjacency list of graph +l = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []} +print(check_bipartite_dfs(l)) diff --git a/graphs/depth_first_search.py b/graphs/depth_first_search.py new file mode 100644 index 000000000000..1206d5ae9252 --- /dev/null +++ b/graphs/depth_first_search.py @@ -0,0 +1,59 @@ +"""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. + +Pseudocode: + all nodes initially unexplored + mark s as explored + for every edge (s, v): + if v unexplored: + DFS(G, v) +""" + +from typing import Set, Dict + + +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 vectex 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() + # one 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", "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/data_structures/Graph/DepthFirstSearch.py b/graphs/depth_first_search_2.py similarity index 75% rename from data_structures/Graph/DepthFirstSearch.py rename to graphs/depth_first_search_2.py index 0f10a8600099..0593e120b1da 100644 --- a/data_structures/Graph/DepthFirstSearch.py +++ b/graphs/depth_first_search_2.py @@ -1,18 +1,19 @@ -# Author: OMKAR PATHAK -from __future__ import print_function +#!/usr/bin/python +""" Author: OMKAR PATHAK """ -class Graph(): + +class Graph: def __init__(self): self.vertex = {} - # for printing the Graph vertexes + # for printing the Graph vertices def printGraph(self): print(self.vertex) for i in self.vertex.keys(): - print(i,' -> ', ' -> '.join([str(j) for j in self.vertex[i]])) + print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]])) - # for adding the edge beween two vertexes + # for adding the edge between two vertices def addEdge(self, fromVertex, toVertex): # check if vertex is already present, if fromVertex in self.vertex.keys(): @@ -34,14 +35,15 @@ def DFSRec(self, startVertex, visited): # mark start vertex as visited visited[startVertex] = True - print(startVertex, end = ' ') + print(startVertex, end=" ") - # Recur for all the vertexes that are adjacent to this node + # Recur for all the vertices that are adjacent to this node for i in self.vertex.keys(): if visited[i] == False: self.DFSRec(i, visited) -if __name__ == '__main__': + +if __name__ == "__main__": g = Graph() g.addEdge(0, 1) g.addEdge(0, 2) @@ -51,7 +53,7 @@ def DFSRec(self, startVertex, visited): g.addEdge(3, 3) g.printGraph() - print('DFS:') + print("DFS:") g.DFS() # OUTPUT: @@ -60,4 +62,4 @@ def DFSRec(self, startVertex, visited): # 2  ->  0 -> 3 # 3  ->  3 # DFS: - # 0 1 2 3 + #  0 1 2 3 diff --git a/graphs/dijkstra.py b/graphs/dijkstra.py new file mode 100644 index 000000000000..f156602beb6e --- /dev/null +++ b/graphs/dijkstra.py @@ -0,0 +1,119 @@ +"""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 = set() + while heap: + (cost, u) = heapq.heappop(heap) + if u in visited: + continue + visited.add(u) + if u == end: + return cost + for v, c in graph[u]: + if v in visited: + continue + next = cost + c + heapq.heappush(heap, (next, v)) + 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 + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/graphs/dijkstra_2.py b/graphs/dijkstra_2.py new file mode 100644 index 000000000000..762884136e4a --- /dev/null +++ b/graphs/dijkstra_2.py @@ -0,0 +1,58 @@ +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 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 + + +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) + + +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/data_structures/Graph/dijkstra_algorithm.py b/graphs/dijkstra_algorithm.py similarity index 88% rename from data_structures/Graph/dijkstra_algorithm.py rename to graphs/dijkstra_algorithm.py index 985c7f6c1301..6b64834acd81 100644 --- a/data_structures/Graph/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..4f5e81236984 --- /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): # 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..26c87cd8f4b2 --- /dev/null +++ b/graphs/directed_and_undirected_(weighted)_graph.py @@ -0,0 +1,497 @@ +from collections import deque +import random as rand +import math as math +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.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 function 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 + # 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.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 function 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/edmonds_karp_multiple_source_and_sink.py b/graphs/edmonds_karp_multiple_source_and_sink.py new file mode 100644 index 000000000000..0f359ff1aea3 --- /dev/null +++ b/graphs/edmonds_karp_multiple_source_and_sink.py @@ -0,0 +1,193 @@ +class FlowNetwork: + def __init__(self, graph, sources, sinks): + self.sourceIndex = None + self.sinkIndex = None + self.graph = graph + + self._normalizeGraph(sources, sinks) + self.verticesCount = len(graph) + self.maximumFlowAlgorithm = None + + # make only one source and one sink + def _normalizeGraph(self, sources, sinks): + if sources is int: + sources = [sources] + if sinks is int: + sinks = [sinks] + + if len(sources) == 0 or len(sinks) == 0: + return + + self.sourceIndex = sources[0] + self.sinkIndex = sinks[0] + + # make fake vertex if there are more + # than one source or sink + if len(sources) > 1 or len(sinks) > 1: + maxInputFlow = 0 + 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 + + size = len(self.graph) + 1 + for room in self.graph: + room.append(0) + self.graph.append([0] * size) + for i in 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.") + if self.sourceIndex is None or self.sinkIndex is None: + return 0 + + self.maximumFlowAlgorithm.execute() + return self.maximumFlowAlgorithm.getMaximumFlow() + + def setMaximumFlowAlgorithm(self, Algorithm): + self.maximumFlowAlgorithm = Algorithm(self) + + +class FlowNetworkAlgorithmExecutor: + def __init__(self, flowNetwork): + self.flowNetwork = flowNetwork + self.verticesCount = flowNetwork.verticesCount + self.sourceIndex = flowNetwork.sourceIndex + self.sinkIndex = flowNetwork.sinkIndex + # it's just a reference, so you shouldn't change + # it in your algorithms, use deep copy before doing that + self.graph = flowNetwork.graph + self.executed = False + + def execute(self): + if not self.executed: + self._algorithm() + self.executed = True + + # You should override it + def _algorithm(self): + pass + + +class MaximumFlowAlgorithmExecutor(FlowNetworkAlgorithmExecutor): + def __init__(self, flowNetwork): + super().__init__(flowNetwork) + # use this to save your result + self.maximumFlow = -1 + + def getMaximumFlow(self): + if not self.executed: + raise Exception("You should execute algorithm before using its result!") + + return self.maximumFlow + + +class PushRelabelExecutor(MaximumFlowAlgorithmExecutor): + def __init__(self, flowNetwork): + super().__init__(flowNetwork) + + self.preflow = [[0] * self.verticesCount for i in range(self.verticesCount)] + + self.heights = [0] * self.verticesCount + self.excesses = [0] * self.verticesCount + + def _algorithm(self): + self.heights[self.sourceIndex] = self.verticesCount + + # push some substance to graph + for nextVertexIndex, bandwidth in enumerate(self.graph[self.sourceIndex]): + self.preflow[self.sourceIndex][nextVertexIndex] += bandwidth + self.preflow[nextVertexIndex][self.sourceIndex] -= bandwidth + 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 + ] + + # move through list + i = 0 + while i < len(verticesList): + vertexIndex = verticesList[i] + previousHeight = self.heights[vertexIndex] + self.processVertex(vertexIndex) + if self.heights[vertexIndex] > previousHeight: + # if it was relabeled, swap elements + # and start from 0 index + verticesList.insert(0, verticesList.pop(i)) + i = 0 + else: + i += 1 + + self.maximumFlow = sum(self.preflow[self.sourceIndex]) + + 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] + ): + 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], + ) + self.preflow[fromIndex][toIndex] += preflowDelta + self.preflow[toIndex][fromIndex] -= preflowDelta + self.excesses[fromIndex] -= preflowDelta + self.excesses[toIndex] += preflowDelta + + def relabel(self, vertexIndex): + minHeight = None + for toIndex in range(self.verticesCount): + 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__": + entrances = [0] + exits = [3] + # graph = [ + # [0, 0, 4, 6, 0, 0], + # [0, 0, 5, 2, 0, 0], + # [0, 0, 0, 0, 4, 4], + # [0, 0, 0, 0, 6, 6], + # [0, 0, 0, 0, 0, 0], + # [0, 0, 0, 0, 0, 0], + # ] + graph = [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]] + + # prepare our network + flowNetwork = FlowNetwork(graph, entrances, exits) + # set algorithm + flowNetwork.setMaximumFlowAlgorithm(PushRelabelExecutor) + # and calculate + maximumFlow = flowNetwork.findMaximumFlow() + + 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..a2e5cf4da26a --- /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] == 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/data_structures/Graph/even_tree.py b/graphs/even_tree.py similarity index 83% rename from data_structures/Graph/even_tree.py rename to graphs/even_tree.py index 9383ea9a13c1..c9aef6e7861f 100644 --- a/data_structures/Graph/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 new file mode 100644 index 000000000000..e18a3bafa9c0 --- /dev/null +++ b/graphs/finding_bridges.py @@ -0,0 +1,42 @@ +# Finding Bridges in Undirected Graph +def computeBridges(l): + id = 0 + n = len(l) # No of vertices in graph + low = [0] * n + visited = [False] * n + + def dfs(at, parent, bridges, id): + visited[at] = True + low[at] = id + id += 1 + for to in l[at]: + if to == parent: + pass + elif not visited[to]: + dfs(to, at, bridges, id) + low[at] = min(low[at], low[to]) + if at < low[to]: + bridges.append([at, to]) + else: + # This edge is a back edge and cannot be a bridge + low[at] = min(low[at], to) + + bridges = [] + for i in range(n): + 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) diff --git a/graphs/frequent_pattern_graph_miner.py b/graphs/frequent_pattern_graph_miner.py new file mode 100644 index 000000000000..aa14fbdd3a3c --- /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/data_structures/Graph/Graph.py b/graphs/graph_list.py similarity index 73% rename from data_structures/Graph/Graph.py rename to graphs/graph_list.py index d091f713b8d9..a20940ab1598 100644 --- a/data_structures/Graph/Graph.py +++ b/graphs/graph_list.py @@ -1,9 +1,11 @@ -from __future__ import print_function +#!/usr/bin/python + # Author: OMKAR PATHAK -# We can use Python's dictionary for constructing the graph +# We can use Python's dictionary for constructing the graph. + -class AdjacencyList(object): +class AdjacencyList: def __init__(self): self.List = {} @@ -15,10 +17,11 @@ def addEdge(self, fromVertex, toVertex): self.List[fromVertex] = [toVertex] def printList(self): - for i in self.List: - print((i,'->',' -> '.join([str(j) for j in self.List[i]]))) + for i in self.List: + print((i, "->", " -> ".join([str(j) for j in self.List[i]]))) + -if __name__ == '__main__': +if __name__ == "__main__": al = AdjacencyList() al.addEdge(0, 1) al.addEdge(0, 4) diff --git a/data_structures/Graph/Graph_matrix.py b/graphs/graph_matrix.py similarity index 53% rename from data_structures/Graph/Graph_matrix.py rename to graphs/graph_matrix.py index de25301d6dd1..987168426ba5 100644 --- a/data_structures/Graph/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..5727a2f21d89 --- /dev/null +++ b/graphs/graphs_floyd_warshall.py @@ -0,0 +1,101 @@ +# 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/kahns_algorithm_long.py b/graphs/kahns_algorithm_long.py new file mode 100644 index 000000000000..0651040365d0 --- /dev/null +++ b/graphs/kahns_algorithm_long.py @@ -0,0 +1,31 @@ +# Finding longest distance in Directed Acyclic Graph using KahnsAlgorithm +def longestDistance(l): + indegree = [0] * len(l) + queue = [] + longDist = [1] * len(l) + + for key, values in l.items(): + for i in values: + indegree[i] += 1 + + for i in range(len(indegree)): + if indegree[i] == 0: + queue.append(i) + + while queue: + vertex = queue.pop(0) + for x in l[vertex]: + indegree[x] -= 1 + + if longDist[vertex] + 1 > longDist[x]: + 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) diff --git a/graphs/kahns_algorithm_topo.py b/graphs/kahns_algorithm_topo.py new file mode 100644 index 000000000000..d50bc9a43d19 --- /dev/null +++ b/graphs/kahns_algorithm_topo.py @@ -0,0 +1,33 @@ +# Kahn's Algorithm is used to find Topological ordering of Directed Acyclic Graph using BFS +def topologicalSort(l): + indegree = [0] * len(l) + queue = [] + topo = [] + cnt = 0 + + for key, values in l.items(): + for i in values: + indegree[i] += 1 + + for i in range(len(indegree)): + if indegree[i] == 0: + queue.append(i) + + while queue: + vertex = queue.pop(0) + cnt += 1 + topo.append(vertex) + for x in l[vertex]: + indegree[x] -= 1 + if indegree[x] == 0: + queue.append(x) + + if cnt != len(l): + 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) diff --git a/graphs/minimum_spanning_tree_kruskal.py b/graphs/minimum_spanning_tree_kruskal.py new file mode 100644 index 000000000000..91b44f6508e7 --- /dev/null +++ b/graphs/minimum_spanning_tree_kruskal.py @@ -0,0 +1,32 @@ +if __name__ == "__main__": + num_nodes, num_edges = list(map(int, input().strip().split())) + + edges = [] + + for i in range(num_edges): + node1, node2, cost = list(map(int, input().strip().split())) + edges.append((i, node1, node2, cost)) + + edges = sorted(edges, key=lambda edge: edge[3]) + + parent = list(range(num_nodes)) + + def find_parent(i): + if i != parent[i]: + parent[i] = find_parent(parent[i]) + return parent[i] + + minimum_spanning_tree_cost = 0 + minimum_spanning_tree = [] + + 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 + + print(minimum_spanning_tree_cost) + for edge in minimum_spanning_tree: + print(edge) diff --git a/graphs/minimum_spanning_tree_prims.py b/graphs/minimum_spanning_tree_prims.py new file mode 100644 index 000000000000..6255b6af64ad --- /dev/null +++ b/graphs/minimum_spanning_tree_prims.py @@ -0,0 +1,115 @@ +import sys +from collections import defaultdict + + +def PrimsAlgorithm(l): + + nodePosition = [] + + def get_position(vertex): + return nodePosition[vertex] + + def set_position(vertex, pos): + nodePosition[vertex] = pos + + def top_to_bottom(heap, start, size, positions): + if start > size // 2 - 1: + return + else: + if 2 * start + 2 >= size: + m = 2 * start + 1 + else: + if heap[2 * start + 1] < heap[2 * start + 2]: + m = 2 * start + 1 + else: + m = 2 * start + 2 + if heap[m] < heap[start]: + temp, temp1 = heap[m], positions[m] + heap[m], positions[m] = heap[start], positions[start] + heap[start], positions[start] = temp, temp1 + + temp = get_position(positions[m]) + set_position(positions[m], get_position(positions[start])) + set_position(positions[start], temp) + + top_to_bottom(heap, m, size, positions) + + # Update function if value of any node in min-heap decreases + def bottom_to_top(val, index, heap, position): + temp = position[index] + + while index != 0: + if index % 2 == 0: + parent = int((index - 2) / 2) + else: + parent = int((index - 1) / 2) + + if val < heap[parent]: + heap[index] = heap[parent] + position[index] = position[parent] + set_position(position[parent], index) + else: + heap[index] = val + position[index] = temp + set_position(temp, index) + break + index = parent + else: + heap[0] = val + position[0] = temp + set_position(temp, 0) + + def heapify(heap, positions): + start = len(heap) // 2 - 1 + for i in range(start, -1, -1): + top_to_bottom(heap, i, len(heap), positions) + + def deleteMinimum(heap, positions): + temp = positions[0] + heap[0] = sys.maxsize + 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 + Positions = [] + + for x in range(len(l)): + p = sys.maxsize + Distance_TV.append(p) + Positions.append(x) + nodePosition.append(x) + + TreeEdges = [] + visited[0] = 1 + Distance_TV[0] = sys.maxsize + for x in l[0]: + Nbr_TV[x[0]] = 0 + Distance_TV[x[0]] = x[1] + heapify(Distance_TV, Positions) + + for i in range(1, len(l)): + vertex = deleteMinimum(Distance_TV, Positions) + if visited[vertex] == 0: + TreeEdges.append((Nbr_TV[vertex], vertex)) + visited[vertex] = 1 + for v in l[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 + + +if __name__ == "__main__": + # < --------- 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()] + adjlist[l[0]].append([l[1], l[2]]) + adjlist[l[1]].append([l[0], l[2]]) + print(PrimsAlgorithm(adjlist)) diff --git a/graphs/multi_heuristic_astar.py b/graphs/multi_heuristic_astar.py new file mode 100644 index 000000000000..386aab695bb0 --- /dev/null +++ b/graphs/multi_heuristic_astar.py @@ -0,0 +1,312 @@ +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/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..a1d46a5a12a4 --- /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 = [v for v in 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 new file mode 100644 index 000000000000..573c1bf5e363 --- /dev/null +++ b/graphs/scc_kosaraju.py @@ -0,0 +1,51 @@ +def dfs(u): + global g, r, scc, component, visit, stack + 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 + 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 + for i in stack[::-1]: + if visit[i]: + continue + component = [] + dfs2(i) + scc.append(component) + return scc + + +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/tarjans_scc.py b/graphs/tarjans_scc.py similarity index 88% rename from Graphs/tarjans_scc.py rename to graphs/tarjans_scc.py index 89754e593508..4b0a689ea3c0 100644 --- a/Graphs/tarjans_scc.py +++ b/graphs/tarjans_scc.py @@ -36,9 +36,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 +71,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/hashes/chaos_machine.py b/hashes/chaos_machine.py index f0a305bfeade..8d3bbd4c0251 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,75 +11,73 @@ # 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 ####################################### @@ -92,15 +86,17 @@ def reset(): # Pushing Data (Input) import random + message = random.sample(range(0xFFFFFFFF), 100) for chunk in message: - push(chunk) + push(chunk) -# for controlling +# 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() + print("%s" % format(pull(), "#04x")) + print(buffer_space) + print(params_space) + inp = input("(e)exit? ").strip() 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..c1ed7fe1d727 --- /dev/null +++ b/hashes/hamming_code.py @@ -0,0 +1,312 @@ +# 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 + if contBO % 2 == 0: + parity.append(0) + else: + parity.append(1) + + 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: + 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": + 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: + 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(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": + if x == "1": + contBO += 1 + contLoop += 1 + if contBO % 2 == 0: + parity.append("0") + else: + parity.append("1") + + 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]) + + if parityReceived == parity: + ack = True + else: + ack = False + + 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..85565533d175 100644 --- a/hashes/md5.py +++ b/hashes/md5.py @@ -1,155 +1,241 @@ -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): + """ -def sum32(a,b): - return (a + b) % 2**32 + """ + 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/sha1.py b/hashes/sha1.py new file mode 100644 index 000000000000..c74ec0c853de --- /dev/null +++ b/hashes/sha1.py @@ -0,0 +1,169 @@ +""" +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!!" + 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 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 +and cryptocurrency etc. +The Algorithm as described in the reference: +First we start with a message. The message is padded and the length of the message +is added to the end. It is then split into blocks of 512 bits or 64 bytes. The blocks +are then processed one at a time. Each block must be expanded and compressed. +The value after each compression is added to a 160bit buffer called the current hash +state. After the last block is processed the current hash state is returned as +the final hash. +Reference: https://deadhacker.com/2006/02/21/sha-1-illustrated/ +""" + +import argparse +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) + respectively. We will start with this as a message digest. 0x is how you write + Hexadecimal numbers in Python + """ + self.data = data + self.h = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0] + + @staticmethod + 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 + + 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)) + 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) + ] + + # @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 after some bit operations + """ + 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) + 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). + 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. + """ + self.padded_data = self.padding() + self.blocks = self.split_blocks() + for block in self.blocks: + expanded_block = self.expand_block(block) + a, b, c, d, e = self.h + for i in range(0, 80): + if 0 <= i < 20: + f = (b & c) | ((~b) & d) + k = 0x5A827999 + elif 20 <= i < 40: + f = b ^ c ^ d + k = 0x6ED9EBA1 + elif 40 <= i < 60: + f = (b & c) | (b & d) | (c & d) + k = 0x8F1BBCDC + 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) + + +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") + 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 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") + args = parser.parse_args() + input_string = args.input_string + # In any case hash input should be a bytestring + if args.input_file: + with open(args.input_file, "rb") as f: + hash_input = f.read() + else: + hash_input = bytes(input_string, "utf-8") + print(SHA1Hash(hash_input).final_hash()) + + +if __name__ == "__main__": + main() + import doctest + + doctest.testmod() diff --git a/images/Travis_CI_fail_1.png b/images/Travis_CI_fail_1.png new file mode 100644 index 000000000000..451e54e4844a Binary files /dev/null and b/images/Travis_CI_fail_1.png differ diff --git a/images/Travis_CI_fail_2.png b/images/Travis_CI_fail_2.png new file mode 100644 index 000000000000..caa406099da1 Binary files /dev/null and b/images/Travis_CI_fail_2.png differ diff --git a/linear-algebra-python/src/lib.pyc b/linear-algebra-python/src/lib.pyc deleted file mode 100644 index 7aeca0e1c6d5..000000000000 Binary files a/linear-algebra-python/src/lib.pyc and /dev/null differ diff --git a/linear-algebra-python/src/tests.py b/linear-algebra-python/src/tests.py deleted file mode 100644 index b84612b4ced4..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(x.__str__(),"(0,0,0,0,0,1)") - def test_size(self): - """ - test for size()-method - """ - x = Vector([1,2,3,4]) - self.assertEqual(x.size(),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((x*3.0).__str__(),"(3.0,6.0,9.0)") - self.assertEqual((a*b),0) - def test_zeroVector(self): - """ - test for the global function zeroVector(...) - """ - self.assertTrue(zeroVector(10).__str__().count("0") == 10) - def test_unitBasisVector(self): - """ - test for the global function unitBasisVector(...) - """ - self.assertEqual(unitBasisVector(3,1).__str__(),"(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(axpy(2,x,y).__str__(),"(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(x.__str__(),y.__str__()) - def test_changeComponent(self): - """ - test for the changeComponent(...)-method - """ - x = Vector([1,0,0]) - x.changeComponent(0,0) - x.changeComponent(1,1) - self.assertEqual(x.__str__(),"(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",A.__str__()) - 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)",(A*x).__str__()) - self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n",(A*2).__str__()) - 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",A.__str__()) - 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",(A+B).__str__()) - 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",(A-B).__str__()) - 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',squareZeroMatrix(5).__str__()) - - -if __name__ == "__main__": - unittest.main() \ No newline at end of file diff --git a/linear-algebra-python/README.md b/linear_algebra/README.md similarity index 60% rename from linear-algebra-python/README.md rename to linear_algebra/README.md index ebfcdab7b179..9f8d150a72fa 100644 --- a/linear-algebra-python/README.md +++ b/linear_algebra/README.md @@ -1,21 +1,22 @@ # Linear algebra library for Python -This module contains some useful classes and functions for dealing with linear algebra in python 2. +This module contains classes and functions for doing linear algebra. --- ## Overview -- class Vector - - This class represents a vector of arbitray size and operations on it. +### 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 + - \_\_str\_\_() : toString method - component(i : int): gets the i-th component (start by 0) - - size() : gets the size of the vector (number of components) + - \_\_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 @@ -31,20 +32,24 @@ This module contains some useful classes and functions for dealing with linear a - 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 + +### class Matrix +- - This class represents a matrix of arbitrary size and operations on it. **Overview about the methods:** - - __str__() : returns a string representation + - \_\_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 + - 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) @@ -53,22 +58,18 @@ This module contains some useful classes and functions for dealing with linear a ## 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)```. +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 -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. +Import the module `lib.py` from the **src** directory into your project. +Alternatively, you can directly use the Python bytecode file `lib.pyc`. --- ## 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**. +`src/tests.py` contains Python unit tests which can be run with `python3 -m unittest -v`. diff --git a/linear-algebra-python/src/lib.py b/linear_algebra/src/lib.py similarity index 56% rename from linear-algebra-python/src/lib.py rename to linear_algebra/src/lib.py index 66f27ff8946e..f4628f1d964a 100644 --- a/linear-algebra-python/src/lib.py +++ b/linear_algebra/src/lib.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ Created on Mon Feb 26 14:29:11 2018 @@ -25,9 +24,9 @@ import random -class Vector(object): +class Vector: """ - This class represents a vector of arbitray size. + This class represents a vector of arbitrary size. You need to give the vector components. Overview about the methods: @@ -36,8 +35,8 @@ class Vector(object): set(components : list) : changes the vector components. __str__() : toString method component(i : int): gets the i-th component (start by 0) - size() : gets the size of the vector (number of components) - euclidLength() : returns the eulidean length of the vector. + __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 @@ -45,182 +44,174 @@ class Vector(object): changeComponent(pos,value) : changes the specified component. TODO: compare-operator """ - def __init__(self,components): + + def __init__(self, components=None): """ input: components or nothing simple constructor for init the vector """ - self.__components = components - def set(self,components): + 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 = components + self.__components = list(components) else: raise Exception("please give any vector") + def __str__(self): """ returns a string representation of the vector """ - ans = "(" - length = len(self.__components) - for i in range(length): - if i != length-1: - ans += str(self.__components[i]) + "," - else: - ans += str(self.__components[i]) + ")" - if len(ans) == 1: - ans += ")" - return ans - def component(self,i): + return "(" + ",".join(map(str, self.__components)) + ")" + + def component(self, i): """ input: index (start at 0) output: the i-th component of the vector. """ - if i < len(self.__components) and i >= 0: + 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 size(self): + + def __len__(self): """ returns the size of the vector """ return len(self.__components) - def eulidLength(self): + + def euclidLength(self): """ - returns the eulidean length of the vector + returns the euclidean length of the vector """ summe = 0 for c in self.__components: - summe += c**2 + summe += c ** 2 return math.sqrt(summe) - def __add__(self,other): + + def __add__(self, other): """ input: other vector assumes: other vector has the same size returns a new vector that represents the sum. """ - size = self.size() - result = [] - if size == other.size(): - for i in range(size): - result.append(self.__components[i] + other.component(i)) + 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") - return Vector(result) - def __sub__(self,other): + + def __sub__(self, other): """ input: other vector assumes: other vector has the same size - returns a new vector that represents the differenz. + returns a new vector that represents the difference. """ - size = self.size() - result = [] - if size == other.size(): - for i in range(size): - result.append(self.__components[i] - other.component(i)) - else: # error case + 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") - return Vector(result) - def __mul__(self,other): + + def __mul__(self, other): """ mul implements the scalar multiplication and the dot-product """ - ans = [] - if isinstance(other,float) or isinstance(other,int): - for c in self.__components: - ans.append(c*other) - elif (isinstance(other,Vector) and (self.size() == other.size())): - size = self.size() + 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("invalide operand!") - return Vector(ans) + else: # error case + raise Exception("invalid operand!") + def copy(self): """ copies this vector and returns it. """ - components = [x for x in self.__components] - return Vector(components) - def changeComponent(self,pos,value): + 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 (pos >= 0 and pos < len(self.__components)) + # 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)) - ans = [] - for i in range(dimension): - ans.append(0) - return Vector(ans) + """ + # precondition + assert isinstance(dimension, int) + return Vector([0] * dimension) -def unitBasisVector(dimension,pos): +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 = [] - for i in range(dimension): - if i != pos: - ans.append(0) - else: - ans.append(1) + # precondition + assert isinstance(dimension, int) and (isinstance(pos, int)) + ans = [0] * dimension + ans[pos] = 1 return Vector(ans) - -def axpy(scalar,x,y): + +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) - + 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): + +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'. """ - ans = zeroVector(N) random.seed(None) - for i in range(N): - ans.changeComponent(i,random.randint(a,b)) - return ans + ans = [random.randint(a, b) for i in range(N)] + return Vector(ans) -class Matrix(object): +class Matrix: """ class: Matrix This class represents a arbitrary matrix. Overview about the methods: - __str__() : returns a string representation + __str__() : returns a string representation operator * : implements the matrix vector multiplication implements the matrix-scalar multiplication. changeComponent(x,y,value) : changes the specified component. @@ -230,14 +221,16 @@ class Matrix(object): operator + : implements the matrix-addition. operator - _ implements the matrix-subtraction """ - def __init__(self,matrix,w,h): + + def __init__(self, matrix, w, h): """ - simple constructor for initialzes + 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 @@ -247,118 +240,140 @@ def __str__(self): for i in range(self.__height): ans += "|" for j in range(self.__width): - if j < self.__width -1: + 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): + + 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: + 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): + 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: + if 0 <= x < self.__height and 0 <= y < self.__width: return self.__matrix[x][y] else: - raise Exception ("changeComponent: indices out of bounds") + 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): + + 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 (other.size() == self.__width): + 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) + 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 = [] - for i in range(self.__height): - row = [] - for j in range(self.__width): - row.append(self.__matrix[i][j] * other) - matrix.append(row) - return Matrix(matrix,self.__width,self.__height) - def __add__(self,other): + 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()): + 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)) + row.append(self.__matrix[i][j] + other.component(i, j)) matrix.append(row) - return Matrix(matrix,self.__width,self.__height) + return Matrix(matrix, self.__width, self.__height) else: raise Exception("matrix must have the same dimension!") - def __sub__(self,other): + + def __sub__(self, other): """ implements the matrix-subtraction. """ - if (self.__width == other.width() and self.__height == other.height()): + 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)) + row.append(self.__matrix[i][j] - other.component(i, j)) matrix.append(row) - return Matrix(matrix,self.__width,self.__height) + 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 = [] - for i in range(N): - row = [] - for j in range(N): - row.append(0) - ans.append(row) - return Matrix(ans,N,N) - - -def randomMatrix(W,H,a,b): + 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' """ - matrix = [] random.seed(None) - for i in range(H): - row = [] - for j in range(W): - row.append(random.randint(a,b)) - matrix.append(row) - return Matrix(matrix,W,H) - - \ No newline at end of file + 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..dc0c3d95102e --- /dev/null +++ b/linear_algebra/src/polynom-for-points.py @@ -0,0 +1,129 @@ +def points_to_polynomial(coordinates): + """ + 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 = [] + 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 = [] + 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 = [] + while count < x: + solution.append(vector[count] / matrix[count][count]) + count += 1 + + count = 0 + solved = "f(x)=" + + while count < x: + remove_e = 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/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..8d2170e46da4 --- /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) + y = 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/machine_learning/astar.py b/machine_learning/astar.py new file mode 100644 index 000000000000..ec8f214ab082 --- /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(object): + """ + Class cell represents a cell in the world which have the property + position : The position of the represented by tupleof x and y + co-ordinates 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(object): + """ + 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/decision_tree.py b/machine_learning/decision_tree.py index 71849904ccf2..fe1d54736563 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,38 @@ 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 +174,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/gaussian_naive_bayes.py b/machine_learning/gaussian_naive_bayes.py new file mode 100644 index 000000000000..24c884adb98a --- /dev/null +++ b/machine_learning/gaussian_naive_bayes.py @@ -0,0 +1,45 @@ +# Gaussian Naive Bayes Example + +from sklearn.naive_bayes import GaussianNB +from sklearn.metrics import plot_confusion_matrix +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +import matplotlib.pyplot as plt + + +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_descent.py b/machine_learning/gradient_descent.py index db6415999bd7..811cc68467f9 100644 --- a/machine_learning/gradient_descent.py +++ b/machine_learning/gradient_descent.py @@ -1,25 +1,31 @@ """ Implementation of gradient descent algorithm for minimizing cost of a linear hypothesis function. """ -from __future__ import print_function 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 +39,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 +50,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] @@ -76,7 +82,7 @@ def summation_of_cost_derivative(index, end=m): 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 @@ -86,7 +92,7 @@ def get_cost_derivative(index): :return: derivative wrt to that index 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 +106,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 +123,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..7a4f69eb77ce 100644 --- a/machine_learning/k_means_clust.py +++ b/machine_learning/k_means_clust.py @@ -1,4 +1,4 @@ -'''README, Author - Anurag Kumar(mailto:anuragkumarak95@gmail.com) +"""README, Author - Anurag Kumar(mailto:anuragkumarak95@gmail.com) Requirements: - sklearn @@ -17,157 +17,183 @@ Usage: 1. define 'k' value, 'X' features array and 'hetrogeneity' empty list - + 2. create initial_centroids, initial_centroids = get_initial_centroids( - X, - k, + 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. plot_heterogeneity( - heterogeneity, + heterogeneity, k ) - + 5. Have fun.. - -''' -from __future__ import print_function + +""" from sklearn.metrics import pairwise_distances import numpy as np -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. + +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''' + 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) - + 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) - + 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. +if False: # change to true to run this test case. import sklearn.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) diff --git a/machine_learning/k_nearest_neighbours.py b/machine_learning/k_nearest_neighbours.py new file mode 100644 index 000000000000..481a8e1dbcd0 --- /dev/null +++ b/machine_learning/k_nearest_neighbours.py @@ -0,0 +1,57 @@ +import numpy as np +from collections import Counter +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..c36a530736cd --- /dev/null +++ b/machine_learning/knn_sklearn.py @@ -0,0 +1,31 @@ +from sklearn.model_selection import train_test_split +from sklearn.datasets import load_iris +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..6998db1ce4a0 --- /dev/null +++ b/machine_learning/linear_discriminant_analysis.py @@ -0,0 +1,406 @@ +""" + 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 +from random import seed + + +# 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 [l.index(max(l)) for l 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 + + +# 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 = 0 + while True: + try: + user_input = int( + input("Enter the number of classes (Data Groupings): ").strip() + ) + if user_input > 0: + n_classes = user_input + break + else: + print( + f"Your entered value is {user_input} , Number of classes " + f"should be positive!" + ) + continue + except ValueError: + print("Your entered value is not numerical!") + + print("-" * 100) + + std_dev = 1.0 # Default value for standard deviation of dataset + # Trying to get the value of standard deviation + while True: + try: + user_sd = float( + input( + "Enter the value of standard deviation" + "(Default value is 1.0 for all classes): " + ).strip() + or "1.0" + ) + if user_sd >= 0.0: + std_dev = user_sd + break + else: + print( + f"Your entered value is {user_sd}, Standard deviation should " + f"not be negative!" + ) + continue + except ValueError: + print("Your entered value is not numerical!") + + 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): + while True: + try: + user_count = int( + input(f"Enter The number of instances for class_{i+1}: ") + ) + if user_count > 0: + counts.append(user_count) + break + else: + print( + f"Your entered value is {user_count}, Number of " + "instances should be positive!" + ) + continue + except ValueError: + print("Your entered value is not numerical!") + print("-" * 100) + + # An empty list to store values of user-entered means of classes + user_means = [] + for a in range(n_classes): + while True: + try: + user_mean = float( + input(f"Enter the value of mean for class_{a+1}: ") + ) + if isinstance(user_mean, float): + user_means.append(user_mean) + break + print(f"You entered an invalid value: {user_mean}") + except ValueError: + print("Your entered value is not numerical!") + 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 eb1f019c502c..29bb7c48589e 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -1,14 +1,12 @@ """ 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 @@ -18,21 +16,22 @@ def collect_dataset(): 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): +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 @@ -59,7 +58,6 @@ def sum_of_square_error(data_x, data_y, len_data, theta): :param theta : contains the feature vector :return : sum of square error computed from given feature's """ - error = 0.0 prod = np.dot(theta, data_x.transpose()) prod -= data_y.transpose() sum_elem = np.sum(np.square(prod)) @@ -82,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 @@ -100,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 new file mode 100644 index 000000000000..1c1906e8e6b2 --- /dev/null +++ b/machine_learning/logistic_regression.py @@ -0,0 +1,89 @@ +#!/usr/bin/python + +# Logistic Regression from scratch + +# In[62]: + +# In[63]: + +# importing all the required libraries + +""" +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 + +# 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 + + +def sigmoid_function(z): + return 1 / (1 + np.exp(-z)) + + +def cost_function(h, y): + return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean() + + +def log_likelihood(X, Y, weights): + scores = np.dot(X, weights) + return np.sum(Y * scores - np.log(1 + np.exp(scores))) + + +# 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]) + + 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 # updating the weights + z = np.dot(X, theta) + h = sigmoid_function(z) + J = cost_function(h, y) + if iterations % 100 == 0: + print(f"loss: {J} \t") # printing the loss after every 100 iterations + return theta + + +# In[68]: + +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: ", 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 + + 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") + (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)) + 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.legend() + plt.show() diff --git a/machine_learning/lstm/lstm_prediction.py b/machine_learning/lstm/lstm_prediction.py new file mode 100644 index 000000000000..fbf802f4d8ee --- /dev/null +++ b/machine_learning/lstm/lstm_prediction.py @@ -0,0 +1,56 @@ +""" + 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 +""" + +from keras.layers import Dense, LSTM +from keras.models import Sequential +import numpy as np +import pandas as pd +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..d78d2a9ed8eb --- /dev/null +++ b/machine_learning/multilayer_perceptron_classifier.py @@ -0,0 +1,30 @@ +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 8ac3e8fc69e9..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) \ No newline at end of file diff --git a/machine_learning/polymonial_regression.py b/machine_learning/polymonial_regression.py new file mode 100644 index 000000000000..7b080715b762 --- /dev/null +++ b/machine_learning/polymonial_regression.py @@ -0,0 +1,44 @@ +import matplotlib.pyplot as plt +import pandas as pd + +# 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 + + +# Splitting the dataset into the Training set and Test set +from sklearn.model_selection import train_test_split + +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) + + +# Fitting Polynomial Regression to the dataset +from sklearn.preprocessing import PolynomialFeatures +from sklearn.linear_model import LinearRegression + +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..e7acd91346a1 --- /dev/null +++ b/machine_learning/random_forest_classifier.py @@ -0,0 +1,45 @@ +# Random Forest Classifier Example + +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +from sklearn.ensemble import RandomForestClassifier +from sklearn.metrics import plot_confusion_matrix +import matplotlib.pyplot as plt + + +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..f78b6bbd0f42 --- /dev/null +++ b/machine_learning/random_forest_regressor.py @@ -0,0 +1,42 @@ +# Random Forest Regressor Example + +from sklearn.datasets import load_boston +from sklearn.model_selection import train_test_split +from sklearn.ensemble import RandomForestRegressor +from sklearn.metrics import mean_absolute_error +from sklearn.metrics import mean_squared_error + + +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/scoring_functions.py b/machine_learning/scoring_functions.py index a2d97b09ded2..2b891d4eb9d5 100755 --- a/machine_learning/scoring_functions.py +++ b/machine_learning/scoring_functions.py @@ -10,12 +10,22 @@ 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 +34,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 +56,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 +78,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 +105,32 @@ 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 diff --git a/machine_learning/sequential_minimum_optimization.py b/machine_learning/sequential_minimum_optimization.py new file mode 100644 index 000000000000..a0b99a788cbd --- /dev/null +++ b/machine_learning/sequential_minimum_optimization.py @@ -0,0 +1,626 @@ +""" + 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 matplotlib.pyplot as plt +import numpy as np +import pandas as pd +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/support_vector_machines.py b/machine_learning/support_vector_machines.py new file mode 100644 index 000000000000..3bf54a69128d --- /dev/null +++ b/machine_learning/support_vector_machines.py @@ -0,0 +1,58 @@ +from sklearn.datasets import load_iris +from sklearn import svm +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/maths/3n_plus_1.py b/maths/3n_plus_1.py new file mode 100644 index 000000000000..d3684561827f --- /dev/null +++ b/maths/3n_plus_1.py @@ -0,0 +1,149 @@ +from typing import Tuple, List + + +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/__init__.py b/maths/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/maths/abs.py b/maths/abs.py new file mode 100644 index 000000000000..68c99a1d51d8 --- /dev/null +++ b/maths/abs.py @@ -0,0 +1,28 @@ +"""Absolute Value.""" + + +def abs_val(num): + """ + Find the absolute value of a number. + + >>> abs_val(-5.1) + 5.1 + >>> abs_val(-5) == abs_val(5) + True + >>> abs_val(0) + 0 + """ + 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) + + +if __name__ == "__main__": + print(abs_val(-34)) # --> 34 diff --git a/maths/abs_max.py b/maths/abs_max.py new file mode 100644 index 000000000000..554e27f6ee66 --- /dev/null +++ b/maths/abs_max.py @@ -0,0 +1,35 @@ +from typing import List + + +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..ac5fa58f41cf --- /dev/null +++ b/maths/aliquot_sum.py @@ -0,0 +1,46 @@ +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) 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..fd002b0c4361 --- /dev/null +++ b/maths/allocation_number.py @@ -0,0 +1,58 @@ +from typing import List + + +def allocation_num(number_of_bytes: int, partitions: int) -> List[str]: + """ + Divide a number of bytes into x partitions. + + 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}'}) + + parameter + ------------ + : param number_of_bytes + : param partitions + + return + ------------ + : return: list of bytes to be assigned to each worker thread + + Examples: + ------------ + >>> allocation_num(16647, 4) + ['0-4161', '4162-8322', '8323-12483', '12484-16647'] + >>> allocation_num(888, 888) + Traceback (most recent call last): + ... + ValueError: partitions can not >= number_of_bytes! + >>> 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 = [f"0-{bytes_per_partition}"] + for i in range(1, partitions - 1): + length = f"{bytes_per_partition * i + 1}-{bytes_per_partition * (i + 1)}" + allocation_list.append(length) + allocation_list.append( + f"{(bytes_per_partition * (partitions - 1)) + 1}-" f"{number_of_bytes}" + ) + return allocation_list + + +if __name__ == "__main__": + import doctest + + doctest.testmod() 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_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..ccb250d7718c --- /dev/null +++ b/maths/average_median.py @@ -0,0 +1,36 @@ +def median(nums): + """ + Find median of a list of numbers. + + >>> median([0]) + 0 + >>> median([4,1,3,2]) + 2.5 + + Args: + nums: List of nums + + Returns: + Median. + """ + sorted_list = sorted(nums) + med = None + if len(sorted_list) % 2 == 0: + mid_index_1 = len(sorted_list) // 2 + mid_index_2 = (len(sorted_list) // 2) - 1 + med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2) + else: + mid_index = (len(sorted_list) - 1) // 2 + med = sorted_list[mid_index] + return med + + +def main(): + print("Odd number of numbers:") + print(median([2, 4, 6, 8, 20, 50, 70])) + print("Even number of numbers:") + print(median([2, 4, 6, 8, 20, 50])) + + +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/basic_maths.py b/maths/basic_maths.py new file mode 100644 index 000000000000..07ee3b3df296 --- /dev/null +++ b/maths/basic_maths.py @@ -0,0 +1,80 @@ +"""Implementation of Basic Math in Python.""" +import math + + +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): + while n % i == 0: + pf.append(i) + n = int(n / i) + if n > 2: + pf.append(n) + return pf + + +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 *= 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 *= temp + return div + + +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): + temp = 1 + while n % i == 0: + temp += 1 + n = int(n / i) + if temp > 1: + s *= (i ** temp - 1) / (i - 1) + return int(s) + + +def euler_phi(n: int) -> int: + """Calculate Euler's Phi Function. + >>> euler_phi(100) + 40 + """ + s = n + for x in set(prime_factors(n)): + s *= (x - 1) / x + return int(s) + + +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/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..ff136f685524 --- /dev/null +++ b/maths/ceil.py @@ -0,0 +1,20 @@ +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 ( + x if isinstance(x, int) or x - int(x) == 0 else int(x + 1) if x > 0 else int(x) + ) + + +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..fb188cd6a3d8 --- /dev/null +++ b/maths/chudnovsky_algorithm.py @@ -0,0 +1,61 @@ +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() + multinomial_term = 1 + 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..d3eb6e756dcd --- /dev/null +++ b/maths/collatz_sequence.py @@ -0,0 +1,43 @@ +from typing import List + + +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) + [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..fd98992e6c16 --- /dev/null +++ b/maths/combinations.py @@ -0,0 +1,19 @@ +from math import factorial + + +def combinations(n, k): + """ + >>> combinations(10,5) + 252 + >>> combinations(6,3) + 20 + >>> combinations(20,5) + 15504 + """ + return int(factorial(n) / ((factorial(k)) * (factorial(n - k)))) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() 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 new file mode 100644 index 000000000000..fe81bcfaf71d --- /dev/null +++ b/maths/extended_euclidean_algorithm.py @@ -0,0 +1,72 @@ +""" +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) +""" + +# @Author: S. Sharma +# @Date: 2019-02-25T12:08:53-06:00 +# @Email: silentcat@protonmail.com +# @Last modified by: PatOnTheBack +# @Last modified time: 2019-07-05 + +import sys + + +def extended_euclidean_algorithm(m, n): + """ + 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) + """ + a = 0 + a_prime = 1 + b = 1 + b_prime = 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 = a_prime + a_prime = a + a = t - q * a + + t = b_prime + b_prime = 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") + exit(1) + m = int(sys.argv[1]) + n = int(sys.argv[2]) + print(extended_euclidean_algorithm(m, n)) + + +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 new file mode 100644 index 000000000000..46688261af56 --- /dev/null +++ b/maths/factorial_python.py @@ -0,0 +1,34 @@ +def factorial(input_number: int) -> int: + """ + Calculate the factorial of specified number + + >>> 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 + """ + + 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 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py new file mode 100644 index 000000000000..137112738905 --- /dev/null +++ b/maths/factorial_recursive.py @@ -0,0 +1,28 @@ +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..24d558115795 --- /dev/null +++ b/maths/fermat_little_theorem.py @@ -0,0 +1,30 @@ +# 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..5ba9f6636364 --- /dev/null +++ b/maths/fibonacci.py @@ -0,0 +1,127 @@ +# 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 math +import functools +import time +from decimal import getcontext, Decimal + +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 new file mode 100644 index 000000000000..91619600d5b4 --- /dev/null +++ b/maths/fibonacci_sequence_recursion.py @@ -0,0 +1,22 @@ +# Fibonacci Sequence Using Recursion + + +def recur_fibo(n): + """ + >>> [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(): + limit = int(input("How many terms to include in fibonacci series: ")) + 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__": + 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..fc10ecf3757a --- /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..ae6e5129a6ff --- /dev/null +++ b/maths/floor.py @@ -0,0 +1,20 @@ +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 ( + x if isinstance(x, int) or x - int(x) == 0 else int(x) if 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..ef5e7dae6187 --- /dev/null +++ b/maths/gamma.py @@ -0,0 +1,60 @@ +import math +from scipy.integrate import quad +from numpy import inf + + +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(gamma(i)/math_gamma(i) <= 1.000000001 and abs(gamma(i)/math_gamma(i)) > .99999999 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..ffea20fb2ba1 --- /dev/null +++ b/maths/gaussian.py @@ -0,0 +1,59 @@ +""" +Reference: https://en.wikipedia.org/wiki/Gaussian_function + +python/black : True +python : 3.7.3 + +""" +from numpy import pi, sqrt, exp + + +def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int: + """ + >>> gaussian(1) + 0.24197072451914337 + + >>> gaussian(24) + 3.342714441794458e-126 + + 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/greatest_common_divisor.py b/maths/greatest_common_divisor.py new file mode 100644 index 000000000000..21c427d5b227 --- /dev/null +++ b/maths/greatest_common_divisor.py @@ -0,0 +1,61 @@ +""" +Greatest Common Divisor. + +Wikipedia reference: https://en.wikipedia.org/wiki/Greatest_common_divisor +""" + + +def greatest_common_divisor(a, b): + """ + 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 + """ + return b if a == 0 else greatest_common_divisor(b % a, a) + + +""" +Below method is more memory efficient because it does not use the stack (chunk of memory). +While above method is good, uses more memory for huge numbers because of the recursive calls +required to calculate the greatest common divisor. +""" + + +def gcd_by_iterative(x, y): + """ + >>> gcd_by_iterative(24, 40) + 8 + >>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40) + True + """ + while y: # --> when y=0 then loop will terminate and return x as final GCD. + x, y = y, x % y + return 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}) = {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/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..acc13fa5f833 --- /dev/null +++ b/maths/is_square_free.py @@ -0,0 +1,39 @@ +""" +References: wikipedia:square free number +python/black : True +flake8 : True +""" +from typing import List + + +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/kth_lexicographic_permutation.py b/maths/kth_lexicographic_permutation.py new file mode 100644 index 000000000000..1820be7274e3 --- /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..863744e182b6 --- /dev/null +++ b/maths/least_common_multiple.py @@ -0,0 +1,44 @@ +import unittest + + +def find_lcm(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 + + >>> find_lcm(5,2) + 10 + >>> find_lcm(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 + + +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): + actual_result = find_lcm(first_num, second_num) + with self.subTest(i=i): + self.assertEqual(actual_result, self.expected_results[i]) + + +if __name__ == "__main__": + unittest.main() diff --git a/maths/line_length.py b/maths/line_length.py new file mode 100644 index 000000000000..0b1ddb5b7866 --- /dev/null +++ b/maths/line_length.py @@ -0,0 +1,65 @@ +from typing import Callable, Union +import math as m + + +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 m.sin(5 * x) + m.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 += m.hypot(x2 - x1, fx2 - fx1) + + # Increment step + x1 = x2 + fx1 = fx2 + + return length + + +if __name__ == "__main__": + + def f(x): + return m.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..8dac658f16d1 --- /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..22ad893a6567 --- /dev/null +++ b/maths/lucas_series.py @@ -0,0 +1,22 @@ +# Lucas Sequence Using Recursion + + +def recur_luc(n): + """ + >>> recur_luc(1) + 1 + >>> recur_luc(0) + 2 + """ + if n == 1: + return n + if n == 0: + return 2 + return recur_luc(n - 1) + recur_luc(n - 2) + + +if __name__ == "__main__": + limit = int(input("How many terms to include in Lucas series:")) + print("Lucas series:") + for i in range(limit): + print(recur_luc(i)) diff --git a/maths/matrix_exponentiation.py b/maths/matrix_exponentiation.py new file mode 100644 index 000000000000..56d03b56fc1f --- /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 the 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..df0f66177501 --- /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.prime_factors import prime_factors +from maths.is_square_free import is_square_free + + +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 new file mode 100644 index 000000000000..8b7b17575a33 --- /dev/null +++ b/maths/modular_exponential.py @@ -0,0 +1,44 @@ +""" + 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 +""" + +"""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(): + """Call Modular Exponential Function.""" + print(modular_exponential(3, 200, 13)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + main() diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py new file mode 100644 index 000000000000..dedca9f6cdf5 --- /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..c36c3e83e00b --- /dev/null +++ b/maths/monte_carlo_dice.py @@ -0,0 +1,46 @@ +import random +from typing import List + + +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 new file mode 100644 index 000000000000..c4975c73e037 --- /dev/null +++ b/maths/newton_raphson.py @@ -0,0 +1,55 @@ +""" + 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 + x[n+1] = x[n] + f(x[n])/f'(x[n]) + 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 + steps = [a] + error = abs(f(a)) + f1 = lambda x: calc_derivative(f, x, h=step) # 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 + if logsteps: + steps.append(a) + if error < maxerror: + break + else: + raise ValueError("Iteration limit reached, no converging solution found") + if logsteps: + # 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 + ) + plt.plot([abs(f(x)) for x in steps]) + plt.xlabel("step") + plt.ylabel("error") + plt.show() + print(f"solution = {{{solution:f}}}, error = {{{error:f}}}") diff --git a/maths/numerical_integration.py b/maths/numerical_integration.py new file mode 100644 index 000000000000..67fbc0ddbf30 --- /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("with {} steps: {}".format(i, area)) + i *= 10 diff --git a/maths/perfect_square.py b/maths/perfect_square.py new file mode 100644 index 000000000000..3e7a1c07a75f --- /dev/null +++ b/maths/perfect_square.py @@ -0,0 +1,27 @@ +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 + + +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..d2394f398c36 --- /dev/null +++ b/maths/polynomial_evaluation.py @@ -0,0 +1,53 @@ +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 + >>> print(evaluate_poly(poly, x)) # f(-13) = 7.0(-13)^4 + 9.3(-13)^3 + 5.0(-13)^2 = 180339.9 + 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/prime_check.py b/maths/prime_check.py new file mode 100644 index 000000000000..e60281228fda --- /dev/null +++ b/maths/prime_check.py @@ -0,0 +1,58 @@ +"""Prime Check.""" + +import math +import unittest + + +def prime_check(number): + """ + Check to See if a Number is Prime. + + A number is prime if it has exactly two dividers: 1 and itself. + """ + if number < 2: + # Negatives, 0 and 1 are not primes + return False + if number < 4: + # 2 and 3 are primes + return True + if number % 2 == 0: + # Even values are not primes + return False + + # Except 2, all primes are odd. If any odd value divide + # the number, then that number is not prime. + odd_numbers = range(3, int(math.sqrt(number)) + 1, 2) + return not any(number % i == 0 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 not prime.") + self.assertFalse( + prime_check(0), "Zero doesn't have any divider, primes must have two" + ) + self.assertFalse( + prime_check(1), "One just have 1 divider, primes must have 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..eb3de00de6a7 --- /dev/null +++ b/maths/prime_factors.py @@ -0,0 +1,52 @@ +""" +python/black : True +""" +from typing import List + + +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..1b2a29f1a203 --- /dev/null +++ b/maths/prime_numbers.py @@ -0,0 +1,70 @@ +from typing import Generator +import math + + +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..05363cf62953 --- /dev/null +++ b/maths/prime_sieve_eratosthenes.py @@ -0,0 +1,42 @@ +""" +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__": + 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..7c47bdef2297 --- /dev/null +++ b/maths/quadratic_equations_complex_numbers.py @@ -0,0 +1,37 @@ +from cmath import sqrt +from typing import Tuple + + +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..3788b3e8a3a0 --- /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..3911fea1d04d --- /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..2c41d2e9dad9 --- /dev/null +++ b/maths/relu.py @@ -0,0 +1,39 @@ +""" +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) +""" + +import numpy as np +from typing import List + + +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 new file mode 100644 index 000000000000..c1cc497ad33e --- /dev/null +++ b/maths/segmented_sieve.py @@ -0,0 +1,51 @@ +"""Segmented Sieve.""" + +import math + + +def sieve(n): + """Segmented Sieve.""" + in_prime = [] + start = 2 + end = int(math.sqrt(n)) # Size of every segment + temp = [True] * (end + 1) + prime = [] + + while start <= end: + if temp[start] is True: + in_prime.append(start) + 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) + for each in in_prime: + + t = math.floor(low / each) * each + if t < low: + t += each + + for j in range(t, high + 1, each): + temp[j - low] = False + + for j in range(len(temp)): + 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)) 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 new file mode 100644 index 000000000000..4761c9339ea0 --- /dev/null +++ b/maths/sieve_of_eratosthenes.py @@ -0,0 +1,59 @@ +""" +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 Dmitry (https://github.com/LizardWizzard) for finding the problem +""" + + +import math + + +def sieve(n): + """ + Returns a list with all prime numbers up to n. + + >>> sieve(50) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] + >>> sieve(25) + [2, 3, 5, 7, 11, 13, 17, 19, 23] + >>> sieve(10) + [2, 3, 5, 7] + >>> sieve(9) + [2, 3, 5, 7] + >>> sieve(2) + [2] + >>> sieve(1) + [] + """ + + l = [True] * (n + 1) + prime = [] + start = 2 + end = int(math.sqrt(n)) + + while start <= end: + # If start is a prime + if l[start] is True: + prime.append(start) + + # Set multiples of start be False + for i in range(start * start, n + 1, start): + if l[i] is True: + l[i] = False + + start += 1 + + for j in range(end + 1, n + 1): + if l[j] is True: + prime.append(j) + + return prime + + +if __name__ == "__main__": + print(sieve(int(input("Enter n: ").strip()))) diff --git a/maths/simpson_rule.py b/maths/simpson_rule.py new file mode 100644 index 000000000000..d66dc39a7171 --- /dev/null +++ b/maths/simpson_rule.py @@ -0,0 +1,51 @@ +""" +Numerical integration or quadrature for a smooth function f with known values at x_i + +This method is the classical approach of suming 'Equally Spaced Abscissas' + +method 2: +"Simpson Rule" + +""" + + +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 = 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(f"y = {y}") + + +if __name__ == "__main__": + main() diff --git a/maths/softmax.py b/maths/softmax.py new file mode 100644 index 000000000000..92ff4ca27b88 --- /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..d4c5e311b0b7 --- /dev/null +++ b/maths/square_root.py @@ -0,0 +1,63 @@ +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(0, 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 100755 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/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 new file mode 100644 index 000000000000..9a4ddc8af66b --- /dev/null +++ b/maths/trapezoidal_rule.py @@ -0,0 +1,50 @@ +""" +Numerical integration or quadrature for a smooth function f with known values at x_i + +This method is the classical approach of suming 'Equally Spaced Abscissas' + +method 1: +"extended trapezoidal rule" + +""" + + +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 = 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(f"y = {y}") + + +if __name__ == "__main__": + main() diff --git a/maths/volume.py b/maths/volume.py new file mode 100644 index 000000000000..04283743d71f --- /dev/null +++ b/maths/volume.py @@ -0,0 +1,132 @@ +""" +Find Volumes of Various Shapes. + +Wikipedia reference: https://en.wikipedia.org/wiki/Volume +""" +from typing import Union +from math import pi, pow + + +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..9c13c29210b1 --- /dev/null +++ b/maths/zellers_congruence.py @@ -0,0 +1,159 @@ +import datetime +import argparse + + +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/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_operation.py b/matrix/matrix_operation.py new file mode 100644 index 000000000000..307e8b6ba32e --- /dev/null +++ b/matrix/matrix_operation.py @@ -0,0 +1,154 @@ +""" +function based version of matrix operations, which are just 2D arrays +""" + + +def add(matrix_a, matrix_b): + if _check_not_integer(matrix_a) and _check_not_integer(matrix_b): + rows, cols = _verify_matrix_sizes(matrix_a, matrix_b) + matrix_c = [] + for i in range(rows[0]): + list_1 = [] + for j in range(cols[0]): + val = matrix_a[i][j] + matrix_b[i][j] + list_1.append(val) + matrix_c.append(list_1) + return matrix_c + + +def subtract(matrix_a, matrix_b): + if _check_not_integer(matrix_a) and _check_not_integer(matrix_b): + rows, cols = _verify_matrix_sizes(matrix_a, matrix_b) + matrix_c = [] + for i in range(rows[0]): + list_1 = [] + for j in range(cols[0]): + val = matrix_a[i][j] - matrix_b[i][j] + list_1.append(val) + matrix_c.append(list_1) + return matrix_c + + +def scalar_multiply(matrix, n): + return [[x * n for x in row] for row in matrix] + + +def multiply(matrix_a, matrix_b): + if _check_not_integer(matrix_a) and _check_not_integer(matrix_b): + matrix_c = [] + 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]})" + ) + for i in range(rows[0]): + list_1 = [] + for j in range(cols[1]): + val = 0 + for k in range(cols[1]): + 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): + """ + :param n: dimension for nxn matrix + :type n: int + :return: Identity matrix of shape [n, n] + """ + n = int(n) + return [[int(row == column) for column in range(n)] for row in range(n)] + + +def transpose(matrix, return_map=True): + if _check_not_integer(matrix): + if return_map: + return map(list, zip(*matrix)) + else: + # mt = [] + # for i in range(len(matrix[0])): + # mt.append([row[i] for row in matrix]) + # return mt + return [[row[i] for row in matrix] for i in range(len(matrix[0]))] + + +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 + + matrix_minor = [[] for _ in range(len(matrix))] + for i in range(len(matrix)): + for j in range(len(matrix)): + matrix_minor[i].append(determinant(minor(matrix, i, j))) + + 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): + 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): + return list((len(matrix), len(matrix[0]))) + + +def _verify_matrix_sizes(matrix_a, matrix_b): + shape = _shape(matrix_a) + shape += _shape(matrix_b) + if shape[0] != shape[2] or shape[1] != shape[3]: + 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( + "Add Operation, %s + %s = %s \n" + % (matrix_a, matrix_b, (add(matrix_a, matrix_b))) + ) + print( + "Multiply Operation, %s * %s = %s \n" + % (matrix_a, matrix_b, multiply(matrix_a, matrix_b)) + ) + print("Identity: %s \n" % identity(5)) + print("Minor of {} = {} \n".format(matrix_c, minor(matrix_c, 1, 2))) + print("Determinant of {} = {} \n".format(matrix_b, determinant(matrix_b))) + print("Inverse of {} = {}\n".format(matrix_d, inverse(matrix_d))) + + +if __name__ == "__main__": + 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..222779f454f9 --- /dev/null +++ b/matrix/nth_fibonacci_using_matrix_exponentiation.py @@ -0,0 +1,87 @@ +""" +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..14a9493cd12f --- /dev/null +++ b/matrix/rotate_matrix.py @@ -0,0 +1,98 @@ +""" +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 new file mode 100644 index 000000000000..3897ffb1d9c6 --- /dev/null +++ b/matrix/searching_in_sorted_matrix.py @@ -0,0 +1,22 @@ +def search_in_a_sorted_matrix(mat, m, n, key): + i, j = m - 1, 0 + while i >= 0 and j < n: + if key == mat[i][j]: + print("Key {} found at row- {} column- {}".format(key, i + 1, j + 1)) + return + if key < mat[i][j]: + i -= 1 + else: + j += 1 + print("Key %s not found" % (key)) + + +def main(): + 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__": + main() diff --git a/matrix/sherman_morrison.py b/matrix/sherman_morrison.py new file mode 100644 index 000000000000..257cf33712d5 --- /dev/null +++ b/matrix/sherman_morrison.py @@ -0,0 +1,268 @@ +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/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..f9f72cf59af8 --- /dev/null +++ b/matrix/tests/test_matrix_operation.py @@ -0,0 +1,117 @@ +""" +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 +""" + +# standard libraries +import sys +import numpy as np +import pytest +import logging + +# 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/Ford_Fulkerson.py b/networking_flow/ford_fulkerson.py similarity index 65% rename from networking_flow/Ford_Fulkerson.py rename to networking_flow/ford_fulkerson.py index d51f1f0661b3..0028c7cc577f 100644 --- a/networking_flow/Ford_Fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -4,14 +4,15 @@ (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])): @@ -21,36 +22,40 @@ def BFS(graph, s, t, parent): 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 similarity index 53% rename from networking_flow/Minimum_cut.py rename to networking_flow/minimum_cut.py index 8ad6e03b00c6..0f6781fbb88c 100644 --- a/networking_flow/Minimum_cut.py +++ b/networking_flow/minimum_cut.py @@ -1,12 +1,21 @@ # 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])): @@ -16,26 +25,30 @@ def BFS(graph, s, t, parent): 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/bpnn.py b/neural_network/back_propagation_neural_network.py similarity index 57% rename from Neural_Network/bpnn.py rename to neural_network/back_propagation_neural_network.py index ed5d4c8cbf79..c771dc46afc2 100644 --- a/Neural_Network/bpnn.py +++ b/neural_network/back_propagation_neural_network.py @@ -1,4 +1,6 @@ -''' +#!/usr/bin/python + +""" A Framework of Back Propagation Neural Network(BP) model @@ -14,7 +16,7 @@ Github : https://github.com/RiptideBo Date: 2017.11.23 -''' +""" import numpy as np import matplotlib.pyplot as plt @@ -23,18 +25,22 @@ 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 @@ -44,21 +50,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 @@ -66,57 +73,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 @@ -126,8 +132,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: @@ -136,55 +142,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.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 new file mode 100644 index 000000000000..ac0eddeb5cb6 --- /dev/null +++ b/neural_network/convolution_neural_network.py @@ -0,0 +1,354 @@ +""" + - - - - - -- - - - - - - - - - - - - - - - - - - - - - - + Name - - CNN - Convolution Neural Network For Photo Recognizing + Goal - - Recognize Handing Writing Word Photo + Detail:Total 5 layers neural network + * Convolution layer + * Pooling layer + * Input layer layer of BP + * Hidden layer of BP + * Output layer of BP + Author: Stephen Lee + Github: 245885195@qq.com + Date: 2017.9.20 + - - - - - -- - - - - - - - - - - - - - - - - - - - - - - +""" +import pickle +import numpy as np +import matplotlib.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 + ): + """ + :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 + :param bp_num2: units number of hidden layer + :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 + self.conv1 = conv1_get[:2] + self.step_conv1 = conv1_get[2] + 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.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 + + 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) + + @classmethod + 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") + return conv_ins + + def sig(self, x): + return 1 / (1 + np.exp(-1 * x)) + + def do_round(self, x): + return round(x, 3) + + def convolute(self, data, convs, w_convs, thre_convs, conv_step): + # convolution process + size_conv = convs[0] + num_conv = convs[1] + size_data = np.shape(data)[0] + # 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 + ] + data_focus.append(focus) + # 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] + ) + featuremap.append(self.sig(net_focus)) + featuremap = np.asmatrix(featuremap).reshape( + Size_FeatureMap, Size_FeatureMap + ) + data_featuremap.append(featuremap) + + # 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 + + def pooling(self, featuremaps, size_pooling, type="average_pool"): + # pooling process + size_map = len(featuremaps[0]) + 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 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 + map_pooled.append(np.average(focus)) + elif type == "max_pooling": + # max pooling + map_pooled.append(np.max(focus)) + map_pooled = np.asmatrix(map_pooled).reshape(size_pooled, size_pooled) + featuremap_pooled.append(map_pooled) + return featuremap_pooled + + def _expand(self, data): + # expanding three dimension data to one dimension list + data_expanded = [] + 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 + data_mat = np.asarray(data_mat) + shapes = np.shape(data_mat) + 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 + ): + """ + 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 + ] + i_pool = i_pool + 1 + 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 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 + while rp < n_repeat and mse >= error_accuracy: + error_count = 0 + print("-------------Learning Time %d--------------" % rp) + for p in range(len(datas_train)): + # 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) + 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_out2 = self.sig(bp_net_j) + bp_net_k = np.dot(bp_out2, self.wkj.T) - self.thre_bp3 + bp_out3 = self.sig(bp_net_k) + + # --------------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_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 + 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) + + 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.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)) + error_count += errors + # print(' ----Teach ',data_teach) + # print(' ----BP_output ',bp_out3) + rp = rp + 1 + 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.grid(True, alpha=0.5) + plt.show() + + print("------------------Training Complished---------------------") + print((" - - Training epoch: ", rp, " - - Mse: %.6f" % mse)) + if draw_e: + draw_error() + return mse + + def predict(self, datas_test): + # model predict + produce_out = [] + 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_pooled1 = self.pooling(data_conved1, self.size_pooling1) + data_bp_input = self._expand(data_pooled1) + + bp_out1 = data_bp_input + bp_net_j = bp_out1 * self.vji.T - self.thre_bp2 + bp_out2 = self.sig(bp_net_j) + 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] + return np.asarray(res) + + 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_pooled1 = self.pooling(data_conved1, self.size_pooling1) + + return data_conved1, data_pooled1 + + +if __name__ == "__main__": + """ + I will put the example on other file + """ 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 new file mode 100644 index 000000000000..23b409b227c4 --- /dev/null +++ b/neural_network/perceptron.py @@ -0,0 +1,231 @@ +""" + 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, 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 + 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]) # number of columns in dataset + self.weight = [] + + 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.insert(0, self.bias) + + epoch_count = 0 + + while True: + 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.target[i]: + for j in range(self.col_sample + 1): + 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 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) -> 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): + 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: 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 + + +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, +] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + 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/optimization/requirements.txt b/optimization/requirements.txt new file mode 100644 index 000000000000..4b43f7e68658 --- /dev/null +++ b/optimization/requirements.txt @@ -0,0 +1 @@ +matplotlib \ No newline at end of file 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 deleted file mode 100644 index 28cdff75ab85..000000000000 --- a/other/Fischer-Yates_Shuffle.py +++ /dev/null @@ -1,20 +0,0 @@ -''' -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)) diff --git a/other/activity_selection.py b/other/activity_selection.py new file mode 100644 index 000000000000..4e8e6c78e3f5 --- /dev/null +++ b/other/activity_selection.py @@ -0,0 +1,45 @@ +"""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 + + +# Driver program to test above function +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 +""" diff --git a/other/anagrams.py b/other/anagrams.py index 44cd96b75f62..471413194498 100644 --- a/other/anagrams.py +++ b/other/anagrams.py @@ -1,29 +1,32 @@ -from __future__ import print_function import collections, pprint, time, os start_time = time.time() -print('creating word list...') +print("creating word list...") path = os.path.split(os.path.realpath(__file__)) -word_list = sorted(list(set([word.strip().lower() for word in open(path[0] + '/words')]))) +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/detecting_english_programmatically.py b/other/detecting_english_programmatically.py index 305174e31cf7..4b0bb37ce520 100644 --- a/other/detecting_english_programmatically.py +++ b/other/detecting_english_programmatically.py @@ -1,19 +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__)) - dictionaryFile = open(path[0] + '/Dictionary.txt') englishWords = {} - for word in dictionaryFile.read().split('\n'): - englishWords[word] = None - dictionaryFile.close() + 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) @@ -29,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 @@ -52,4 +56,5 @@ def isEnglish(message, wordPercentage = 20, letterPercentage = 85): import doctest + doctest.testmod() diff --git a/other/Dictionary.txt b/other/dictionary.txt similarity index 99% rename from other/Dictionary.txt rename to 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..1f78941d3afc --- /dev/null +++ b/other/dijkstra_bankers_algorithm.py @@ -0,0 +1,222 @@ +# 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) +""" + +import time +from typing import Dict, List + +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() + {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/euclidean_gcd.py b/other/euclidean_gcd.py index 30853e172076..c6c11f947a08 100644 --- a/other/euclidean_gcd.py +++ b/other/euclidean_gcd.py @@ -1,6 +1,6 @@ -from __future__ import print_function # https://en.wikipedia.org/wiki/Euclidean_algorithm + def euclidean_gcd(a, b): while b: t = b @@ -8,6 +8,7 @@ def euclidean_gcd(a, b): a = t return a + def main(): print("GCD(3, 5) = " + str(euclidean_gcd(3, 5))) print("GCD(5, 3) = " + str(euclidean_gcd(5, 3))) @@ -15,5 +16,6 @@ def main(): print("GCD(3, 6) = " + str(euclidean_gcd(3, 6))) print("GCD(6, 3) = " + str(euclidean_gcd(6, 3))) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py new file mode 100644 index 000000000000..99121ba632c7 --- /dev/null +++ b/other/fischer_yates_shuffle.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +""" +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)) 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 55% rename from other/game_of_life/game_o_life.py rename to other/game_of_life.py index 32ebe0fc1301..2b4d1116fa8c 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 @@ -26,29 +26,31 @@ 4. Any dead cell with exactly three live neighbours be- comes a live cell, as if by reproduction. - ''' + """ import numpy as np -import random, time, sys +import random, sys from matplotlib import pyplot as plt -import matplotlib.animation as animation 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. @@ -56,63 +58,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/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..0f7bfacf030a --- /dev/null +++ b/other/integeration_by_simpson_approx.py @@ -0,0 +1,123 @@ +""" +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 : wrong_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 + ), f"the function should return integer or float return type of your function, {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..e1b5ab5bd380 --- /dev/null +++ b/other/least_recently_used.py @@ -0,0 +1,64 @@ +from abc import abstractmethod +import sys +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/LinearCongruentialGenerator.py b/other/linear_congruential_generator.py similarity index 66% rename from other/LinearCongruentialGenerator.py rename to other/linear_congruential_generator.py index 34abdf34eaf3..058f270d0584 100644 --- a/other/LinearCongruentialGenerator.py +++ b/other/linear_congruential_generator.py @@ -1,26 +1,26 @@ -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. @@ -28,8 +28,9 @@ def next_number( self ): 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/magicdiamondpattern.py b/other/magicdiamondpattern.py new file mode 100644 index 000000000000..4ca698d80c28 --- /dev/null +++ b/other/magicdiamondpattern.py @@ -0,0 +1,54 @@ +# 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/nested_brackets.py b/other/nested_brackets.py index 76677d56439a..c712bc21b5ce 100644 --- a/other/nested_brackets.py +++ b/other/nested_brackets.py @@ -1,27 +1,26 @@ -''' +""" 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 +""" 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,7 +28,9 @@ 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 diff --git a/other/palindrome.py b/other/palindrome.py new file mode 100644 index 000000000000..dd1fe316f479 --- /dev/null +++ b/other/palindrome.py @@ -0,0 +1,66 @@ +# 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(s) - 1 + while start_i < end_i: + if s[start_i] == s[end_i]: + start_i += 1 + end_i -= 1 + else: + return False + return True + + +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 s[0] == s[len(s) - 1]: + return is_palindrome_recursive(s[1:-1]) + else: + return False + + +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__": + 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 16c44a0938a5..a6d1d7dfb324 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,480 +38,507 @@ """ +from math import sqrt + + def isPrime(number): """ 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. - + 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 + """ + + # 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' + 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' """ - + # 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' """ - + # 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. - """ + """ # 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. - """ + """ # 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' """ - + # 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; - - + + 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' """ - + # 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' """ - + # 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 """ - + # 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) - """ - - # 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. - + 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. + # 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') """ - + # 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 @@ -524,82 +550,95 @@ def isPerfectNumber(number): 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. - """ - + """ + # 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!) """ - + # 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 - """ - + """ + # 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/sdes.py b/other/sdes.py new file mode 100644 index 000000000000..3038ff193ae9 --- /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]) + r = apply_sbox(s1, temp[4:]) + l = "0" * (2 - len(l)) + l + 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 e566f693f63b..e27db3a2e8c4 100644 --- a/other/sierpinski_triangle.py +++ b/other/sierpinski_triangle.py @@ -1,13 +1,18 @@ -'''Author Anurag Kumar | anuragkumarak95@gmail.com | git/anuragkumarak95 +#!/usr/bin/python + +"""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 @@ -18,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 ') -myPen = turtle.Turtle() -myPen.ht() -myPen.speed(5) -myPen.pencolor('red') +PROGNAME = "Sierpinski Triangle" + +points = [[-175, -125], [0, 175], [175, -125]] # size of triangle + -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/two-sum.py b/other/two_sum.py similarity index 69% rename from other/two-sum.py rename to other/two_sum.py index d4484aa85505..70d5c5375026 100644 --- a/other/two-sum.py +++ b/other/two_sum.py @@ -9,7 +9,7 @@ Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. """ -from __future__ import print_function + def twoSum(nums, target): """ @@ -19,11 +19,11 @@ def twoSum(nums, target): """ 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: - chk_map[val] = index + compl = target - val + if compl in chk_map: + indices = [chk_map[compl], index] + print(indices) + return [indices] + else: + chk_map[val] = index return False 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 new file mode 100644 index 000000000000..f80d58ea0038 --- /dev/null +++ b/project_euler/README.md @@ -0,0 +1,11 @@ +# ProjectEuler + +Problems are taken from https://projecteuler.net/. + +Project Euler is a series of challenging mathematical/computer programming problems that will 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. + +For description of the problem statements, kindly visit https://projecteuler.net/show=all diff --git a/project_euler/problem_01/__init__.py b/project_euler/problem_01/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_01/sol1.py b/project_euler/problem_01/sol1.py new file mode 100644 index 000000000000..e81156edaee4 --- /dev/null +++ b/project_euler/problem_01/sol1.py @@ -0,0 +1,28 @@ +""" +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. +""" + + +def solution(n): + """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(solution(int(input().strip()))) diff --git a/project_euler/problem_01/sol2.py b/project_euler/problem_01/sol2.py new file mode 100644 index 000000000000..8041c7ffa589 --- /dev/null +++ b/project_euler/problem_01/sol2.py @@ -0,0 +1,33 @@ +""" +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. +""" + + +def solution(n): + """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 + """ + + 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 + return sum + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_01/sol3.py b/project_euler/problem_01/sol3.py new file mode 100644 index 000000000000..c0bcbc06ec83 --- /dev/null +++ b/project_euler/problem_01/sol3.py @@ -0,0 +1,60 @@ +""" +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. +""" + + +def solution(n): + """ + 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 + """ + + 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 + return sum + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_01/sol4.py b/project_euler/problem_01/sol4.py new file mode 100644 index 000000000000..e01dc977d8cf --- /dev/null +++ b/project_euler/problem_01/sol4.py @@ -0,0 +1,47 @@ +""" +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. +""" + + +def solution(n): + """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(solution(int(input().strip()))) diff --git a/project_euler/problem_01/sol5.py b/project_euler/problem_01/sol5.py new file mode 100644 index 000000000000..bd96d965f92d --- /dev/null +++ b/project_euler/problem_01/sol5.py @@ -0,0 +1,28 @@ +""" +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. +""" + +"""A straightforward pythonic solution using list comprehension""" + + +def solution(n): + """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 + """ + + return sum([i for i in range(n) if i % 3 == 0 or i % 5 == 0]) + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_01/sol6.py b/project_euler/problem_01/sol6.py new file mode 100644 index 000000000000..c9f94b9f77c8 --- /dev/null +++ b/project_euler/problem_01/sol6.py @@ -0,0 +1,34 @@ +""" +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. +""" + + +def solution(n): + """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(solution(int(input().strip()))) diff --git a/project_euler/problem_01/sol7.py b/project_euler/problem_01/sol7.py new file mode 100644 index 000000000000..a0510b54c409 --- /dev/null +++ b/project_euler/problem_01/sol7.py @@ -0,0 +1,32 @@ +""" +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. +""" + + +def solution(n): + """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(solution(int(input().strip()))) diff --git a/project_euler/problem_02/__init__.py b/project_euler/problem_02/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_02/sol1.py b/project_euler/problem_02/sol1.py new file mode 100644 index 000000000000..ec89ddaeb2b5 --- /dev/null +++ b/project_euler/problem_02/sol1.py @@ -0,0 +1,41 @@ +""" +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. +""" + + +def solution(n): + """Returns the sum of all fibonacci sequence even elements that are lower + or equals to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + i = 1 + j = 2 + sum = 0 + while j <= n: + if j % 2 == 0: + sum += j + i, j = j, i + j + + return sum + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_02/sol2.py b/project_euler/problem_02/sol2.py new file mode 100644 index 000000000000..bc5040cc6b3b --- /dev/null +++ b/project_euler/problem_02/sol2.py @@ -0,0 +1,39 @@ +""" +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. +""" + + +def solution(n): + """Returns the sum of all fibonacci sequence even elements that are lower + or equals to n. + + >>> solution(10) + [2, 8] + >>> solution(15) + [2, 8] + >>> solution(2) + [2] + >>> solution(1) + [] + >>> solution(34) + [2, 8, 34] + """ + 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__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_02/sol3.py b/project_euler/problem_02/sol3.py new file mode 100644 index 000000000000..f29f21c287e5 --- /dev/null +++ b/project_euler/problem_02/sol3.py @@ -0,0 +1,41 @@ +""" +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. +""" + + +def solution(n): + """Returns the sum of all fibonacci sequence even elements that are lower + or equals 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(solution(int(input().strip()))) diff --git a/project_euler/problem_02/sol4.py b/project_euler/problem_02/sol4.py new file mode 100644 index 000000000000..92ea0a51e026 --- /dev/null +++ b/project_euler/problem_02/sol4.py @@ -0,0 +1,65 @@ +""" +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. +""" +import math +from decimal import Decimal, getcontext + + +def solution(n): + """Returns the sum of all fibonacci sequence even elements that are lower + or equals 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 or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater 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) + sum = num // 2 + return int(sum) + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_02/sol5.py b/project_euler/problem_02/sol5.py new file mode 100644 index 000000000000..180906cf8717 --- /dev/null +++ b/project_euler/problem_02/sol5.py @@ -0,0 +1,46 @@ +""" +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. +""" + + +def solution(n): + """Returns the sum of all fibonacci sequence even elements that are lower + or equals to n. + + >>> solution(10) + 10 + >>> solution(15) + 10 + >>> solution(2) + 2 + >>> solution(1) + 0 + >>> solution(34) + 44 + """ + + a = [0, 1] + i = 0 + while a[i] <= n: + a.append(a[i] + a[i + 1]) + if a[i + 2] > n: + break + i += 1 + sum = 0 + for j in range(len(a) - 1): + if a[j] % 2 == 0: + sum += a[j] + + return sum + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_03/__init__.py b/project_euler/problem_03/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_03/sol1.py b/project_euler/problem_03/sol1.py new file mode 100644 index 000000000000..9f8ecc5e6565 --- /dev/null +++ b/project_euler/problem_03/sol1.py @@ -0,0 +1,78 @@ +""" +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. +""" +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 + + +def solution(n): + """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 or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater or equal to one.") + maxNumber = 0 + if isprime(n): + return n + else: + while n % 2 == 0: + n = n / 2 + if isprime(n): + return int(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 + return maxNumber + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_03/sol2.py b/project_euler/problem_03/sol2.py new file mode 100644 index 000000000000..b6fad079fa31 --- /dev/null +++ b/project_euler/problem_03/sol2.py @@ -0,0 +1,57 @@ +""" +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. +""" + + +def solution(n): + """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 or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater 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(solution(int(input().strip()))) diff --git a/project_euler/problem_03/sol3.py b/project_euler/problem_03/sol3.py new file mode 100644 index 000000000000..5fe45df59984 --- /dev/null +++ b/project_euler/problem_03/sol3.py @@ -0,0 +1,63 @@ +""" +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. +""" + + +def solution(n: int) -> 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 or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + """ + try: + n = int(n) + except (TypeError, ValueError): + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater 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(solution(int(input().strip()))) + import doctest + + doctest.testmod() diff --git a/project_euler/problem_04/__init__.py b/project_euler/problem_04/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_04/sol1.py b/project_euler/problem_04/sol1.py new file mode 100644 index 000000000000..599345b5ab79 --- /dev/null +++ b/project_euler/problem_04/sol1.py @@ -0,0 +1,42 @@ +""" +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. +""" + + +def solution(n): + """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 + """ + # fetches the next number + for number in range(n - 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(int(number / divisor))) == 3): + return number + divisor -= 1 + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_04/sol2.py b/project_euler/problem_04/sol2.py new file mode 100644 index 000000000000..0f185f1216ab --- /dev/null +++ b/project_euler/problem_04/sol2.py @@ -0,0 +1,32 @@ +""" +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. +""" + + +def solution(n): + """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): + t = str(i * j) + if t == t[::-1] and i * j < n: + answer = max(answer, i * j) + return answer + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_05/__init__.py b/project_euler/problem_05/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_05/sol1.py b/project_euler/problem_05/sol1.py new file mode 100644 index 000000000000..b3a231f4dcf5 --- /dev/null +++ b/project_euler/problem_05/sol1.py @@ -0,0 +1,63 @@ +""" +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? +""" + + +def solution(n): + """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(20) + 232792560 + >>> solution(22) + 232792560 + >>> solution(3.4) + 6 + >>> solution(0) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater 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(solution(int(input().strip()))) diff --git a/project_euler/problem_05/sol2.py b/project_euler/problem_05/sol2.py new file mode 100644 index 000000000000..5aa84d21c8e8 --- /dev/null +++ b/project_euler/problem_05/sol2.py @@ -0,0 +1,43 @@ +""" +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) + + +def solution(n): + """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(20) + 232792560 + >>> solution(22) + 232792560 + """ + g = 1 + for i in range(1, n + 1): + g = lcm(g, i) + return g + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_06/__init__.py b/project_euler/problem_06/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_06/sol1.py b/project_euler/problem_06/sol1.py new file mode 100644 index 000000000000..513b354679a9 --- /dev/null +++ b/project_euler/problem_06/sol1.py @@ -0,0 +1,41 @@ +""" +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. +""" + + +def solution(n): + """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 + """ + suma = 0 + sumb = 0 + for i in range(1, n + 1): + suma += i ** 2 + sumb += i + sum = sumb ** 2 - suma + return sum + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_06/sol2.py b/project_euler/problem_06/sol2.py new file mode 100644 index 000000000000..18cdb51752ea --- /dev/null +++ b/project_euler/problem_06/sol2.py @@ -0,0 +1,38 @@ +""" +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. +""" + + +def solution(n): + """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 + """ + suma = n * (n + 1) / 2 + suma **= 2 + sumb = n * (n + 1) * (2 * n + 1) / 6 + return int(suma - sumb) + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_06/sol3.py b/project_euler/problem_06/sol3.py new file mode 100644 index 000000000000..ee739c9a1293 --- /dev/null +++ b/project_euler/problem_06/sol3.py @@ -0,0 +1,38 @@ +""" +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. +""" +import math + + +def solution(n): + """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(solution(int(input().strip()))) diff --git a/project_euler/problem_06/sol4.py b/project_euler/problem_06/sol4.py new file mode 100644 index 000000000000..07eed57ba9b5 --- /dev/null +++ b/project_euler/problem_06/sol4.py @@ -0,0 +1,39 @@ +""" +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. +""" + + +def solution(n): + """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 + >>> solution(100) + 25164150 + """ + 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(solution(int(input("Enter a number: ").strip()))) diff --git a/project_euler/problem_07/__init__.py b/project_euler/problem_07/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_07/sol1.py b/project_euler/problem_07/sol1.py new file mode 100644 index 000000000000..373915f886e1 --- /dev/null +++ b/project_euler/problem_07/sol1.py @@ -0,0 +1,54 @@ +""" +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 math import sqrt + + +def is_prime(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 + + +def solution(n): + """Returns the n-th prime number. + + >>> solution(6) + 13 + >>> solution(1) + 2 + >>> solution(3) + 5 + >>> solution(20) + 71 + >>> solution(50) + 229 + >>> solution(100) + 541 + """ + i = 0 + j = 1 + while i != n and j < 3: + j += 1 + if is_prime(j): + i += 1 + while i != n: + j += 2 + if is_prime(j): + i += 1 + return j + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_07/sol2.py b/project_euler/problem_07/sol2.py new file mode 100644 index 000000000000..6bfc5881f548 --- /dev/null +++ b/project_euler/problem_07/sol2.py @@ -0,0 +1,69 @@ +""" +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 + + +def solution(n): + """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 n must be greater or equal to one. + >>> solution(-17) + Traceback (most recent call last): + ... + ValueError: Parameter n must be greater or equal to one. + >>> solution([]) + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + >>> solution("asd") + Traceback (most recent call last): + ... + TypeError: Parameter n must be int or passive of cast to int. + """ + try: + n = int(n) + except (TypeError, ValueError) as e: + raise TypeError("Parameter n must be int or passive of cast to int.") + if n <= 0: + raise ValueError("Parameter n must be greater or equal to one.") + primes = [] + num = 2 + while len(primes) < n: + if isprime(num): + primes.append(num) + num += 1 + else: + num += 1 + return primes[len(primes) - 1] + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_07/sol3.py b/project_euler/problem_07/sol3.py new file mode 100644 index 000000000000..9b02ea87ec49 --- /dev/null +++ b/project_euler/problem_07/sol3.py @@ -0,0 +1,46 @@ +""" +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? +""" +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 solution(n): + """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(), n - 1, n)) + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_08/__init__.py b/project_euler/problem_08/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_08/sol1.py b/project_euler/problem_08/sol1.py new file mode 100644 index 000000000000..e7582d46c351 --- /dev/null +++ b/project_euler/problem_08/sol1.py @@ -0,0 +1,71 @@ +""" +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): + """Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. + + >>> solution(N) + 23514624000 + """ + LargestProduct = -sys.maxsize - 1 + for i in range(len(n) - 12): + product = 1 + for j in range(13): + product *= int(n[i + j]) + if product > LargestProduct: + LargestProduct = product + return LargestProduct + + +if __name__ == "__main__": + print(solution(N)) diff --git a/project_euler/problem_08/sol2.py b/project_euler/problem_08/sol2.py new file mode 100644 index 000000000000..bf8afa8379ee --- /dev/null +++ b/project_euler/problem_08/sol2.py @@ -0,0 +1,72 @@ +""" +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): + """Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. + + >>> solution(N) + 23514624000 + """ + 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(solution(str(N))) diff --git a/project_euler/problem_08/sol3.py b/project_euler/problem_08/sol3.py new file mode 100644 index 000000000000..dfbef5755dd7 --- /dev/null +++ b/project_euler/problem_08/sol3.py @@ -0,0 +1,82 @@ +""" +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 streval(s: str) -> int: + ret = 1 + for it in s: + ret *= int(it) + return ret + + +def solution(n: str) -> int: + """Find the thirteen adjacent digits in the 1000-digit number n that have + the greatest product and returns it. + + >>> solution(N) + 23514624000 + """ + LargestProduct = -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: + LargestProduct = max(LargestProduct, streval(substr)) + substr = n[cur_index : cur_index + 13] + cur_index += 13 + return LargestProduct + + +if __name__ == "__main__": + print(solution(N)) diff --git a/project_euler/problem_09/__init__.py b/project_euler/problem_09/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_09/sol1.py b/project_euler/problem_09/sol1.py new file mode 100644 index 000000000000..3bb5c968115d --- /dev/null +++ b/project_euler/problem_09/sol1.py @@ -0,0 +1,35 @@ +""" +Problem Statement: +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 abc. +""" + + +def solution(): + """ + 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 + + +if __name__ == "__main__": + print("Please Wait...") + print(solution()) diff --git a/project_euler/problem_09/sol2.py b/project_euler/problem_09/sol2.py new file mode 100644 index 000000000000..de7b12d40c09 --- /dev/null +++ b/project_euler/problem_09/sol2.py @@ -0,0 +1,38 @@ +""" +Problem Statement: +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 abc. +""" + + +def solution(n): + """ + 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 = 1000 + + >>> solution(1000) + 31875000 + """ + product = -1 + d = 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): + d = a * b * c + if d >= product: + product = d + return product + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_09/sol3.py b/project_euler/problem_09/sol3.py new file mode 100644 index 000000000000..a6df46a3a66b --- /dev/null +++ b/project_euler/problem_09/sol3.py @@ -0,0 +1,37 @@ +""" +Problem Statement: + +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 abc. +""" + + +def solution(): + """ + 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 * 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__": + print(solution()) diff --git a/project_euler/problem_10/__init__.py b/project_euler/problem_10/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_10/sol1.py b/project_euler/problem_10/sol1.py new file mode 100644 index 000000000000..c81085951ecf --- /dev/null +++ b/project_euler/problem_10/sol1.py @@ -0,0 +1,50 @@ +""" +Problem Statement: +The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + +Find the sum of all the primes below two million. +""" +from math import sqrt + + +def is_prime(n): + for i in range(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 range(3, n, 2): + if is_prime(i): + sumOfPrimes += i + + return sumOfPrimes + + +def solution(n): + """Returns the sum of all the primes below n. + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution(2000000) + # 142913828922 + >>> solution(1000) + 76127 + >>> solution(5000) + 1548136 + >>> solution(10000) + 5736396 + >>> solution(7) + 10 + """ + return sum_of_primes(n) + + +if __name__ == "__main__": + print(solution(int(input().strip()))) diff --git a/project_euler/problem_10/sol2.py b/project_euler/problem_10/sol2.py new file mode 100644 index 000000000000..b2e2b6e1adf3 --- /dev/null +++ b/project_euler/problem_10/sol2.py @@ -0,0 +1,44 @@ +""" +Problem Statement: +The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + +Find the sum of all the primes below two million. +""" +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 solution(n): + """Returns the sum of all the primes below n. + + # The code below has been commented due to slow execution affecting Travis. + # >>> solution(2000000) + # 142913828922 + >>> 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(solution(int(input().strip()))) diff --git a/project_euler/problem_10/sol3.py b/project_euler/problem_10/sol3.py new file mode 100644 index 000000000000..e5bc0731d8ab --- /dev/null +++ b/project_euler/problem_10/sol3.py @@ -0,0 +1,58 @@ +""" +https://projecteuler.net/problem=10 + +Problem Statement: +The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + +Find the sum of all the primes below two million 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. +""" + + +def prime_sum(n: int) -> int: + """ Returns the sum of all the primes below n. + + >>> prime_sum(2_000_000) + 142913828922 + >>> prime_sum(1_000) + 76127 + >>> prime_sum(5_000) + 1548136 + >>> prime_sum(10_000) + 5736396 + >>> prime_sum(7) + 10 + >>> prime_sum(7.1) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: 'float' object cannot be interpreted as an integer + >>> prime_sum(-7) # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + IndexError: list assignment index out of range + >>> prime_sum("seven") # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: can only concatenate str (not "int") to str + """ + list_ = [0 for i in range(n + 1)] + list_[0] = 1 + list_[1] = 1 + + for i in range(2, int(n ** 0.5) + 1): + if list_[i] == 0: + for j in range(i * i, n + 1, i): + list_[j] = 1 + s = 0 + for i in range(n): + if list_[i] == 0: + s += i + return s + + +if __name__ == "__main__": + # import doctest + # doctest.testmod() + print(prime_sum(int(input().strip()))) diff --git a/project_euler/problem_11/__init__.py b/project_euler/problem_11/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 11/grid.txt b/project_euler/problem_11/grid.txt similarity index 100% rename from Project Euler/Problem 11/grid.txt rename to project_euler/problem_11/grid.txt diff --git a/project_euler/problem_11/sol1.py b/project_euler/problem_11/sol1.py new file mode 100644 index 000000000000..4e49013c8210 --- /dev/null +++ b/project_euler/problem_11/sol1.py @@ -0,0 +1,87 @@ +""" +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 sum of all the multiples of 3 or 5 below n. + + >>> 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_11/sol2.py b/project_euler/problem_11/sol2.py new file mode 100644 index 000000000000..64702e852b0f --- /dev/null +++ b/project_euler/problem_11/sol2.py @@ -0,0 +1,74 @@ +""" +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 sum of all the multiples of 3 or 5 below n. + + >>> solution() + 70600674 + """ + with open(os.path.dirname(__file__) + "/grid.txt") 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 + return maximum + + +if __name__ == "__main__": + print(solution()) diff --git a/project_euler/problem_12/__init__.py b/project_euler/problem_12/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_12/sol1.py b/project_euler/problem_12/sol1.py new file mode 100644 index 000000000000..7e080c4e45a1 --- /dev/null +++ b/project_euler/problem_12/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_12/sol2.py b/project_euler/problem_12/sol2.py new file mode 100644 index 000000000000..5ff0d8349b90 --- /dev/null +++ b/project_euler/problem_12/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_13/__init__.py b/project_euler/problem_13/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_13/num.txt b/project_euler/problem_13/num.txt new file mode 100644 index 000000000000..43b568e812a8 --- /dev/null +++ b/project_euler/problem_13/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_13/sol1.py b/project_euler/problem_13/sol1.py new file mode 100644 index 000000000000..e36065ec8e11 --- /dev/null +++ b/project_euler/problem_13/sol1.py @@ -0,0 +1,30 @@ +""" +Problem Statement: +Work out the first ten digits of the sum of the following one-hundred 50-digit +numbers. +""" + + +def solution(array): + """Returns the first ten digits of the sum of the array elements. + + >>> import os + >>> sum = 0 + >>> array = [] + >>> with open(os.path.dirname(__file__) + "/num.txt","r") as f: + ... for line in f: + ... array.append(int(line)) + ... + >>> solution(array) + '5537376230' + """ + return str(sum(array))[:10] + + +if __name__ == "__main__": + n = int(input().strip()) + + array = [] + for i in range(n): + array.append(int(input().strip())) + print(solution(array)) diff --git a/project_euler/problem_14/__init__.py b/project_euler/problem_14/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_14/sol1.py b/project_euler/problem_14/sol1.py new file mode 100644 index 000000000000..fda45bc94bb7 --- /dev/null +++ b/project_euler/problem_14/sol1.py @@ -0,0 +1,61 @@ +""" +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): + """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) + # {'counter': 525, 'largest_number': 837799} + >>> solution(200) + {'counter': 125, 'largest_number': 171} + >>> solution(5000) + {'counter': 238, 'largest_number': 3711} + >>> solution(15000) + {'counter': 276, 'largest_number': 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 {"counter": pre_counter, "largest_number": largest_number} + + +if __name__ == "__main__": + result = solution(int(input().strip())) + print( + ("Largest Number:", result["largest_number"], "->", result["counter"], "digits") + ) diff --git a/project_euler/problem_14/sol2.py b/project_euler/problem_14/sol2.py new file mode 100644 index 000000000000..375a34c72f57 --- /dev/null +++ b/project_euler/problem_14/sol2.py @@ -0,0 +1,63 @@ +""" +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? +""" + + +def collatz_sequence(n): + """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): + """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) + # {'counter': 525, 'largest_number': 837799} + >>> solution(200) + {'counter': 125, 'largest_number': 171} + >>> solution(5000) + {'counter': 238, 'largest_number': 3711} + >>> solution(15000) + {'counter': 276, 'largest_number': 13255} + """ + + result = max([(len(collatz_sequence(i)), i) for i in range(1, n)]) + return {"counter": result[0], "largest_number": result[1]} + + +if __name__ == "__main__": + result = solution(int(input().strip())) + print( + "Longest Collatz sequence under one million is %d with length %d" + % (result["largest_number"], result["counter"]) + ) diff --git a/project_euler/problem_15/__init__.py b/project_euler/problem_15/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_15/sol1.py b/project_euler/problem_15/sol1.py new file mode 100644 index 000000000000..1be7d10ed674 --- /dev/null +++ b/project_euler/problem_15/sol1.py @@ -0,0 +1,55 @@ +""" +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 lattice_paths(n): + """ + 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. + +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 50 +1.008913445455642e+29 +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 25 +126410606437752.0 +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 23 +8233430727600.0 +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 15 +155117520.0 +bruno@bruno-laptop:~/git/Python/project_euler/problem_15$ python3 sol1.py 1 +2.0 + + >>> lattice_paths(25) + 126410606437752 + >>> lattice_paths(23) + 8233430727600 + >>> lattice_paths(20) + 137846528820 + >>> lattice_paths(15) + 155117520 + >>> lattice_paths(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(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/__init__.py b/project_euler/problem_16/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_16/sol1.py b/project_euler/problem_16/sol1.py new file mode 100644 index 000000000000..67c50ac87876 --- /dev/null +++ b/project_euler/problem_16/sol1.py @@ -0,0 +1,34 @@ +""" +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): + """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_16/sol2.py b/project_euler/problem_16/sol2.py new file mode 100644 index 000000000000..88672e9a9e54 --- /dev/null +++ b/project_euler/problem_16/sol2.py @@ -0,0 +1,28 @@ +""" +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): + """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_17/__init__.py b/project_euler/problem_17/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_17/sol1.py b/project_euler/problem_17/sol1.py new file mode 100644 index 000000000000..d585d81a0825 --- /dev/null +++ b/project_euler/problem_17/sol1.py @@ -0,0 +1,63 @@ +""" +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 withBritish +usage. +""" + + +def solution(n): + """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_18/solution.py b/project_euler/problem_18/solution.py new file mode 100644 index 000000000000..38593813901e --- /dev/null +++ b/project_euler/problem_18/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, "r") 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_18/triangle.txt b/project_euler/problem_18/triangle.txt new file mode 100644 index 000000000000..e236c2ff7ee2 --- /dev/null +++ b/project_euler/problem_18/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_19/__init__.py b/project_euler/problem_19/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_19/sol1.py b/project_euler/problem_19/sol1.py new file mode 100644 index 000000000000..ab59365843b2 --- /dev/null +++ b/project_euler/problem_19/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_20/__init__.py b/project_euler/problem_20/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_20/sol1.py b/project_euler/problem_20/sol1.py new file mode 100644 index 000000000000..13b3c987f046 --- /dev/null +++ b/project_euler/problem_20/sol1.py @@ -0,0 +1,51 @@ +""" +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 factorial(n): + fact = 1 + for i in range(1, n + 1): + fact *= i + return fact + + +def split_and_add(number): + """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(n): + """Returns the sum of the digits in the number 100! + >>> solution(100) + 648 + >>> solution(50) + 216 + >>> solution(10) + 27 + >>> solution(5) + 3 + >>> solution(3) + 6 + >>> solution(2) + 2 + >>> solution(1) + 1 + """ + f = factorial(n) + result = split_and_add(f) + return result + + +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_20/sol2.py b/project_euler/problem_20/sol2.py new file mode 100644 index 000000000000..14e591795292 --- /dev/null +++ b/project_euler/problem_20/sol2.py @@ -0,0 +1,33 @@ +""" +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(n): + """Returns the sum of the digits in the number 100! + >>> 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(n))]) + + +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_20/sol3.py b/project_euler/problem_20/sol3.py new file mode 100644 index 000000000000..13f9d7831c47 --- /dev/null +++ b/project_euler/problem_20/sol3.py @@ -0,0 +1,39 @@ +""" +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(n): + """Returns the sum of the digits in the number 100! + >>> 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(n)))) + + +if __name__ == "__main__": + print(solution(int(input("Enter the Number: ").strip()))) diff --git a/project_euler/problem_20/sol4.py b/project_euler/problem_20/sol4.py new file mode 100644 index 000000000000..4c597220f09b --- /dev/null +++ b/project_euler/problem_20/sol4.py @@ -0,0 +1,40 @@ +""" +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(n): + """Returns the sum of the digits in the number 100! + >>> 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, n + 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_21/__init__.py b/project_euler/problem_21/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_21/sol1.py b/project_euler/problem_21/sol1.py new file mode 100644 index 000000000000..f01c9d0dad73 --- /dev/null +++ b/project_euler/problem_21/sol1.py @@ -0,0 +1,55 @@ +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. +""" + + +def sum_of_divisors(n): + 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): + """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_22/__init__.py b/project_euler/problem_22/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/Project Euler/Problem 22/p022_names.txt b/project_euler/problem_22/p022_names.txt similarity index 100% rename from Project Euler/Problem 22/p022_names.txt rename to project_euler/problem_22/p022_names.txt diff --git a/project_euler/problem_22/sol1.py b/project_euler/problem_22/sol1.py new file mode 100644 index 000000000000..982906245e87 --- /dev/null +++ b/project_euler/problem_22/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_22/sol2.py b/project_euler/problem_22/sol2.py new file mode 100644 index 000000000000..5ae41c84686e --- /dev/null +++ b/project_euler/problem_22/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_23/sol1.py b/project_euler/problem_23/sol1.py new file mode 100644 index 000000000000..a72b6123e3ee --- /dev/null +++ b/project_euler/problem_23/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_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..28d82b550c85 --- /dev/null +++ b/project_euler/problem_234/sol1.py @@ -0,0 +1,56 @@ +""" +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 ? +""" + + +def fib(a, b, n): + + if n == 1: + return a + elif n == 2: + return b + elif n == 3: + return str(a) + str(b) + + temp = 0 + for x in range(2, n): + c = str(a) + str(b) + temp = b + b = c + a = temp + return c + + +def solution(n): + """Returns the sum of all semidivisible numbers not exceeding n.""" + semidivisible = [] + for x in range(n): + l = [i for i in input().split()] + c2 = 1 + while 1: + if len(fib(l[0], l[1], c2)) < int(l[2]): + c2 += 1 + else: + break + semidivisible.append(fib(l[0], l[1], c2 + 1)[int(l[2]) - 1]) + return semidivisible + + +if __name__ == "__main__": + for i in solution(int(str(input()).strip())): + print(i) diff --git a/project_euler/problem_24/__init__.py b/project_euler/problem_24/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_24/sol1.py b/project_euler/problem_24/sol1.py new file mode 100644 index 000000000000..1c6378b38260 --- /dev/null +++ b/project_euler/problem_24/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_25/__init__.py b/project_euler/problem_25/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_25/sol1.py b/project_euler/problem_25/sol1.py new file mode 100644 index 000000000000..f0228915dc15 --- /dev/null +++ b/project_euler/problem_25/sol1.py @@ -0,0 +1,69 @@ +""" +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): + 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): + digits = 0 + index = 2 + + while digits < n: + index += 1 + digits = len(str(fibonacci(index))) + + return index + + +def solution(n): + """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_25/sol2.py b/project_euler/problem_25/sol2.py new file mode 100644 index 000000000000..c98f09b1d316 --- /dev/null +++ b/project_euler/problem_25/sol2.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 fibonacci_generator(): + a, b = 0, 1 + while True: + a, b = b, a + b + yield b + + +def solution(n): + """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_25/sol3.py b/project_euler/problem_25/sol3.py new file mode 100644 index 000000000000..4a1d9da76bf7 --- /dev/null +++ b/project_euler/problem_25/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): + """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_26/__init__.py b/project_euler/problem_26/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_26/sol1.py b/project_euler/problem_26/sol1.py new file mode 100644 index 000000000000..cab8e0eb580b --- /dev/null +++ b/project_euler/problem_26/sol1.py @@ -0,0 +1,42 @@ +""" +Euler Problem 26 +https://projecteuler.net/problem=26 +Find the value of d < 1000 for which 1/d contains the longest recurring cycle +in its decimal fraction part. +""" + + +def find_digit(numerator: int, digit: int) -> int: + """ + Considering any range can be provided, + because as per the problem, the digit d < 1000 + >>> find_digit(1, 10) + 7 + >>> find_digit(10, 100) + 97 + >>> find_digit(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_27/problem_27_sol1.py b/project_euler/problem_27/problem_27_sol1.py new file mode 100644 index 000000000000..84b007a0bc88 --- /dev/null +++ b/project_euler/problem_27/problem_27_sol1.py @@ -0,0 +1,69 @@ +""" +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, b_limit: int) -> 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_28/__init__.py b/project_euler/problem_28/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_28/sol1.py b/project_euler/problem_28/sol1.py new file mode 100644 index 000000000000..11b48fea9adf --- /dev/null +++ b/project_euler/problem_28/sol1.py @@ -0,0 +1,55 @@ +""" +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 diagonal_sum(n): + """Returns the sum of the numbers on the diagonals in a n by n spiral + formed in the same way. + + >>> diagonal_sum(1001) + 669171001 + >>> diagonal_sum(500) + 82959497 + >>> diagonal_sum(100) + 651897 + >>> diagonal_sum(50) + 79697 + >>> diagonal_sum(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(diagonal_sum(1001)) + else: + try: + n = int(sys.argv[1]) + print(diagonal_sum(n)) + except ValueError: + print("Invalid entry - please enter a number") diff --git a/project_euler/problem_29/__init__.py b/project_euler/problem_29/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_29/solution.py b/project_euler/problem_29/solution.py new file mode 100644 index 000000000000..4313a7b06392 --- /dev/null +++ b/project_euler/problem_29/solution.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): + """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_30/soln.py b/project_euler/problem_30/soln.py new file mode 100644 index 000000000000..9d45739845a3 --- /dev/null +++ b/project_euler/problem_30/soln.py @@ -0,0 +1,34 @@ +""" 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 + + +if __name__ == "__main__": + count = sum(digitsum(str(i)) for i in range(1000, 1000000)) + print(count) # --> 443839 diff --git a/project_euler/problem_31/__init__.py b/project_euler/problem_31/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_31/sol1.py b/project_euler/problem_31/sol1.py new file mode 100644 index 000000000000..09b60cdae89d --- /dev/null +++ b/project_euler/problem_31/sol1.py @@ -0,0 +1,64 @@ +""" +Coin sums +Problem 31 +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? +""" + + +def one_pence(): + return 1 + + +def two_pence(x): + return 0 if x < 0 else two_pence(x - 2) + one_pence() + + +def five_pence(x): + return 0 if x < 0 else five_pence(x - 5) + two_pence(x) + + +def ten_pence(x): + return 0 if x < 0 else ten_pence(x - 10) + five_pence(x) + + +def twenty_pence(x): + return 0 if x < 0 else twenty_pence(x - 20) + ten_pence(x) + + +def fifty_pence(x): + return 0 if x < 0 else fifty_pence(x - 50) + twenty_pence(x) + + +def one_pound(x): + return 0 if x < 0 else one_pound(x - 100) + fifty_pence(x) + + +def two_pound(x): + return 0 if x < 0 else two_pound(x - 200) + one_pound(x) + + +def solution(n): + """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(str(input()).strip()))) diff --git a/project_euler/problem_31/sol2.py b/project_euler/problem_31/sol2.py new file mode 100644 index 000000000000..1f006f1a1824 --- /dev/null +++ b/project_euler/problem_31/sol2.py @@ -0,0 +1,56 @@ +"""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) -> 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_32/sol32.py b/project_euler/problem_32/sol32.py new file mode 100644 index 000000000000..393218339e9f --- /dev/null +++ b/project_euler/problem_32/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_33/__init__.py b/project_euler/problem_33/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_33/sol1.py b/project_euler/problem_33/sol1.py new file mode 100644 index 000000000000..73a49023ae41 --- /dev/null +++ b/project_euler/problem_33/sol1.py @@ -0,0 +1,55 @@ +""" +Problem: + +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. +""" + + +def isDigitCancelling(num, den): + if num != den: + if num % 10 == den // 10: + if (num // 10) / (den % 10) == num / den: + return True + + +def solve(digit_len: int) -> str: + """ + >>> solve(2) + '16/64 , 19/95 , 26/65 , 49/98' + >>> solve(3) + '16/64 , 19/95 , 26/65 , 49/98' + >>> solve(4) + '16/64 , 19/95 , 26/65 , 49/98' + >>> solve(0) + '' + >>> solve(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 isDigitCancelling(num, den): + solutions.append(f"{num}/{den}") + den += 1 + num += 1 + den = 10 + solutions = " , ".join(solutions) + return solutions + + +if __name__ == "__main__": + print(solve(2)) diff --git a/project_euler/problem_36/__init__.py b/project_euler/problem_36/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_36/sol1.py b/project_euler/problem_36/sol1.py new file mode 100644 index 000000000000..39088cf25dd4 --- /dev/null +++ b/project_euler/problem_36/sol1.py @@ -0,0 +1,53 @@ +""" +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.) +""" + + +def is_palindrome(n): + n = str(n) + + if n == n[::-1]: + return True + else: + return False + + +def solution(n): + """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_40/__init__.py b/project_euler/problem_40/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_40/sol1.py b/project_euler/problem_40/sol1.py new file mode 100644 index 000000000000..69be377723a5 --- /dev/null +++ b/project_euler/problem_40/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_42/solution42.py b/project_euler/problem_42/solution42.py new file mode 100644 index 000000000000..2380472153c6 --- /dev/null +++ b/project_euler/problem_42/solution42.py @@ -0,0 +1,48 @@ +""" +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, "r") 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_42/words.txt b/project_euler/problem_42/words.txt new file mode 100644 index 000000000000..af3aeb42f151 --- /dev/null +++ b/project_euler/problem_42/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_48/__init__.py b/project_euler/problem_48/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_48/sol1.py b/project_euler/problem_48/sol1.py new file mode 100644 index 000000000000..06ad1408dcef --- /dev/null +++ b/project_euler/problem_48/sol1.py @@ -0,0 +1,24 @@ +""" +Self Powers +Problem 48 + +The series, 11 + 22 + 33 + ... + 1010 = 10405071317. + +Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000. +""" + + +def solution(): + """Returns the last 10 digits of the series, 11 + 22 + 33 + ... + 10001000. + + >>> 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_52/__init__.py b/project_euler/problem_52/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_52/sol1.py b/project_euler/problem_52/sol1.py new file mode 100644 index 000000000000..df5c46ae05d1 --- /dev/null +++ b/project_euler/problem_52/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_53/__init__.py b/project_euler/problem_53/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_53/sol1.py b/project_euler/problem_53/sol1.py new file mode 100644 index 000000000000..0692bbe0ebb8 --- /dev/null +++ b/project_euler/problem_53/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_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..873e520cc9b4 --- /dev/null +++ b/project_euler/problem_551/sol1.py @@ -0,0 +1,204 @@ +""" +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 = 0 + for j in range(k, len(a_i)): + ds_b += a_i[j] + c = 0 + for j in range(min(len(a_i), k)): + c += a_i[j] * base[j] + + 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): + """ + 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(solution(10 ** 15)) diff --git a/project_euler/problem_56/__init__.py b/project_euler/problem_56/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_56/sol1.py b/project_euler/problem_56/sol1.py new file mode 100644 index 000000000000..d5225efcb9e5 --- /dev/null +++ b/project_euler/problem_56/sol1.py @@ -0,0 +1,32 @@ +def maximum_digital_sum(a: int, b: int) -> int: + """ + Considering natural numbers of the form, a**b, where a, b < 100, + what is the maximum digital sum? + :param a: + :param b: + :return: + >>> maximum_digital_sum(10,10) + 45 + + >>> maximum_digital_sum(100,100) + 972 + + >>> maximum_digital_sum(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_67/__init__.py b/project_euler/problem_67/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_67/sol1.py b/project_euler/problem_67/sol1.py new file mode 100644 index 000000000000..9494ff7bbabd --- /dev/null +++ b/project_euler/problem_67/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, "r") 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_67/triangle.txt b/project_euler/problem_67/triangle.txt new file mode 100644 index 000000000000..00aa2bc6382d --- /dev/null +++ b/project_euler/problem_67/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_76/__init__.py b/project_euler/problem_76/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_76/sol1.py b/project_euler/problem_76/sol1.py new file mode 100644 index 000000000000..ed0ee6b507e9 --- /dev/null +++ b/project_euler/problem_76/sol1.py @@ -0,0 +1,54 @@ +""" +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? +""" + + +def partition(m): + """Returns the number of different ways one hundred can be written as a sum + of at least two positive integers. + + >>> partition(100) + 190569291 + >>> partition(50) + 204225 + >>> partition(30) + 5603 + >>> partition(10) + 41 + >>> partition(5) + 6 + >>> partition(3) + 2 + >>> partition(2) + 1 + >>> partition(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(partition(int(str(input()).strip()))) diff --git a/project_euler/problem_99/__init__.py b/project_euler/problem_99/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_99/base_exp.txt b/project_euler/problem_99/base_exp.txt new file mode 100644 index 000000000000..abe95aa86036 --- /dev/null +++ b/project_euler/problem_99/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 \ No newline at end of file diff --git a/project_euler/problem_99/sol1.py b/project_euler/problem_99/sol1.py new file mode 100644 index 000000000000..0148a80ef481 --- /dev/null +++ b/project_euler/problem_99/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 find_largest(data_file: str = "base_exp.txt") -> int: + """ + >>> find_largest() + 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(find_largest()) diff --git a/quantum/README.md b/quantum/README.md new file mode 100644 index 000000000000..be5bd0843f4f --- /dev/null +++ b/quantum/README.md @@ -0,0 +1,8 @@ +# 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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000000..d21c13a54f1c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +beautifulsoup4 +black +fake_useragent +flake8 +keras +matplotlib +mypy +numpy>=1.17.4 +opencv-python +pandas +pillow +pytest +pytest-cov +requests +scikit-fuzzy +sklearn +sympy +tensorflow +xgboost diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py new file mode 100644 index 000000000000..f52c4243dec3 --- /dev/null +++ b/scheduling/first_come_first_served.py @@ -0,0 +1,107 @@ +# 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{turnaround_times[i]}" + ) + print(f"Average waiting time = {average_waiting_time}") + print(f"Average turn around time = {average_turnaround_time}") diff --git a/scheduling/shortest_job_first_algorithm.py b/scheduling/shortest_job_first_algorithm.py new file mode 100644 index 000000000000..18aadca0032f --- /dev/null +++ b/scheduling/shortest_job_first_algorithm.py @@ -0,0 +1,149 @@ +""" +Shortest job remainig first +Please note arrival time and burst +Please use spaces to separate times entered. +""" + +import pandas as pd +from typing import List + + +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 +): + """ + 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) + processes = list(range(1, no_of_processes + 1)) + 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/build_directory_md.py b/scripts/build_directory_md.py new file mode 100755 index 000000000000..9e26ee81a323 --- /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()): + 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/validate_filenames.py b/scripts/validate_filenames.py new file mode 100755 index 000000000000..f22fda4149f3 --- /dev/null +++ b/scripts/validate_filenames.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import os +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") + +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 + nodir_files) +if bad_files: + import sys + + sys.exit(bad_files) diff --git a/searches/binary_search.py b/searches/binary_search.py index 7df45883c09a..0d9e730258d7 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -1,5 +1,5 @@ """ -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 @@ -9,22 +9,185 @@ For manual testing run: python binary_search.py """ -from __future__ import print_function import bisect -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 + +def bisect_left(sorted_collection, item, lo=0, hi=None): + """ + 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 is None: + 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, item, lo=0, hi=None): + """ + 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 is None: + 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 insort_left(sorted_collection, item, lo=0, hi=None): + """ + Inserts a given value into a sorted array before other values with the same value. + + 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, item, lo=0, hi=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, item): """Pure implementation of binary 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 @@ -45,25 +208,24 @@ 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): """Pure implementation of binary search algorithm in Python using stdlib - 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 @@ -85,15 +247,16 @@ def binary_search_std_lib(sorted_collection, item): return index return None + def binary_search_by_recursion(sorted_collection, item, left, right): """Pure implementation of binary 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 @@ -110,24 +273,25 @@ def binary_search_by_recursion(sorted_collection, item, left, right): >>> binary_search_std_lib([0, 5, 7, 10, 15], 6) """ - 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) - + return binary_search_by_recursion(sorted_collection, item, midpoint + 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]) @@ -136,26 +300,27 @@ def __assert_sorted(collection): >>> __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() - collection = [int(item) for item in user_input.split(',')] + + 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 binary search') + 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_input = input("Enter a single number to be found in the list:\n") target = int(target_input) result = binary_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/fibonacci_search.py b/searches/fibonacci_search.py new file mode 100644 index 000000000000..67f2df505d4e --- /dev/null +++ b/searches/fibonacci_search.py @@ -0,0 +1,54 @@ +# run using python fibonacci_search.py -v + +""" +@params +arr: input array +val: the value to be searched +output: the index of element in the array or -1 if not found +return 0 if input array is empty +""" + + +def fibonacci_search(arr, val): + + """ + >>> fibonacci_search([1,6,7,0,0,0], 6) + 1 + >>> fibonacci_search([1,-1, 5, 2, 9], 10) + -1 + >>> fibonacci_search([], 9) + 0 + """ + fib_N_2 = 0 + fib_N_1 = 1 + fibNext = fib_N_1 + fib_N_2 + length = len(arr) + if length == 0: + return 0 + while fibNext < len(arr): + fib_N_2 = fib_N_1 + fib_N_1 = fibNext + fibNext = fib_N_1 + fib_N_2 + index = -1 + while fibNext > 1: + i = min(index + fib_N_2, (length - 1)) + if arr[i] < val: + fibNext = fib_N_1 + fib_N_1 = fib_N_2 + fib_N_2 = fibNext - fib_N_1 + index = i + elif arr[i] > val: + fibNext = fib_N_2 + fib_N_1 = fib_N_1 - fib_N_2 + fib_N_2 = fibNext - fib_N_1 + else: + return i + if (fib_N_1 and index < length - 1) and (arr[index + 1] == val): + return index + 1 + 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..324097ef5a24 --- /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: + import matplotlib.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 7b765c454d06..f4fa8e1203df 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -1,20 +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 -import bisect - -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 """ @@ -22,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..5ba80e9d35be 100644 --- a/searches/jump_search.py +++ b/searches/jump_search.py @@ -1,10 +1,11 @@ -from __future__ import print_function import math + + def jump_search(arr, x): 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 +20,7 @@ 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__": + arr = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] + x = 55 + print(f"Number {x} is at index {jump_search(arr, x)}") diff --git a/searches/linear_search.py b/searches/linear_search.py index 50c6eaad5e9b..155119bb4a43 100644 --- a/searches/linear_search.py +++ b/searches/linear_search.py @@ -1,5 +1,5 @@ """ -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 @@ -9,17 +9,12 @@ For manual testing run: python 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 - :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 @@ -42,14 +37,14 @@ def linear_search(sequence, target): return None -if __name__ == '__main__': - user_input = raw_input('Enter numbers separated by coma:\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 = linear_search(sequence, target) if result is not None: - print('{} found at positions: {}'.format(target, result)) + print(f"{target} found at position : {result}") else: - print('Not found') + print("Not found") diff --git a/searches/quick_select.py b/searches/quick_select.py index e5e2ce99c682..17dca395f73c 100644 --- a/searches/quick_select.py +++ b/searches/quick_select.py @@ -1,13 +1,13 @@ -import collections -import sys -import random -import time -import math """ -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 @@ -17,31 +17,46 @@ 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] - count = 0 - smaller, equal, larger =_partition(list, pivot) - count = len(equal) - m = len(smaller) - #k is the pivot - if m <= k < m + count: + +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 = random.randint(0, len(items) - 1) + pivot = items[pivot] + count = 0 + smaller, equal, larger = _partition(items, pivot) + count = len(equal) + m = len(smaller) + + # 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 - else: - return quickSelect(larger, k - (m + count)) + elif m > index: + return quick_select(smaller, index) + # must be in larger + else: + return quick_select(larger, index - (m + count)) diff --git a/searches/sentinel_linear_search.py b/searches/sentinel_linear_search.py new file mode 100644 index 000000000000..69c1cf9f351a --- /dev/null +++ b/searches/sentinel_linear_search.py @@ -0,0 +1,58 @@ +""" +This is pure Python implementation of sentinel linear search algorithm + +For doctests run following command: +python -m doctest -v sentinel_linear_search.py +or +python3 -m doctest -v sentinel_linear_search.py + +For manual testing run: +python sentinel_linear_search.py +""" + + +def sentinel_linear_search(sequence, target): + """Pure implementation of sentinel linear search algorithm in Python + + :param sequence: some sequence with comparable items + :param target: item value to search + :return: index of found item or None if item is not found + + Examples: + >>> sentinel_linear_search([0, 5, 7, 10, 15], 0) + 0 + + >>> sentinel_linear_search([0, 5, 7, 10, 15], 15) + 4 + + >>> sentinel_linear_search([0, 5, 7, 10, 15], 5) + 1 + + >>> sentinel_linear_search([0, 5, 7, 10, 15], 6) + + """ + sequence.append(target) + + index = 0 + while sequence[index] != target: + index += 1 + + sequence.pop() + + if index == len(sequence): + return None + + return index + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by comma:\n").strip() + sequence = [int(item) for item in user_input.split(",")] + + 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(f"{target} found at positions: {result}") + else: + print("Not found") diff --git a/searches/simple-binary-search.py b/searches/simple-binary-search.py new file mode 100644 index 000000000000..80e43ea346b2 --- /dev/null +++ b/searches/simple-binary-search.py @@ -0,0 +1,26 @@ +# A binary search implementation to test if a number is in a list of elements + + +def binary_search(a_list, item): + """ + >>> 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 + """ + 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__": + import doctest + + doctest.testmod() diff --git a/searches/simulated_annealing.py b/searches/simulated_annealing.py new file mode 100644 index 000000000000..c24adc1ddb41 --- /dev/null +++ b/searches/simulated_annealing.py @@ -0,0 +1,132 @@ +# https://en.wikipedia.org/wiki/Simulated_annealing +import math, 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: + import matplotlib.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 74c23f8b8cf1..9ddc5e8dee7f 100644 --- a/searches/tabu_search.py +++ b/searches/tabu_search.py @@ -1,5 +1,5 @@ """ -This is pure python implementation of Tabu search algorithm for a Travelling Salesman Problem, that the distances +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. @@ -25,7 +25,6 @@ import copy import argparse -import sys def generate_neighbours(path): @@ -38,31 +37,34 @@ def generate_neighbours(path): 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. """ - f = open(path, "r") dict_of_neighbours = {} - for line in f: - if line.split()[0] not in dict_of_neighbours: - _list = list() - _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]]) - 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]]) - f.close() + with open(path) as f: + for line in f: + if line.split()[0] not in dict_of_neighbours: + _list = list() + _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]] + ) + 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]] + ) return dict_of_neighbours @@ -84,8 +86,8 @@ def generate_first_solution(path, dict_of_neighbours): """ - f = open(path, "r") - start_node = f.read(1) + with open(path) as f: + start_node = f.read(1) end_node = start_node first_solution = [] @@ -93,7 +95,6 @@ def generate_first_solution(path, dict_of_neighbours): visiting = start_node distance_of_first_solution = 0 - f.close() while visiting not in first_solution: minim = 10000 for k in dict_of_neighbours[visiting]: @@ -113,8 +114,11 @@ 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 @@ -132,7 +136,7 @@ def find_neighborhood(solution, dict_of_neighbours): Example: - >>> find_neighborhood(['a','c','b','d','e','a']) + >>) 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]] @@ -169,7 +173,9 @@ 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. @@ -181,7 +187,7 @@ def tabu_search(first_solution, distance_of_first_solution, dict_of_neighbours, 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_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. @@ -209,8 +215,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] @@ -233,22 +241,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/tabuTestData.txt b/searches/tabu_test_data.txt similarity index 100% rename from searches/tabuTestData.txt rename to searches/tabu_test_data.txt diff --git a/searches/ternary_search.py b/searches/ternary_search.py index c610f9b3c6da..5ecc47644248 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -1,107 +1,103 @@ -''' +""" 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 - # 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): + for i in range(left, right + 1): + if A[i] == target: return i + # 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 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 bitonic_merge(a, low, cnt, dire): + if cnt > 1: + k = int(cnt / 2) + for i in range(low, low + k): + compAndSwap(a, i, i + k, dire) + bitonic_merge(a, low, k, dire) + bitonic_merge(a, low + k, k, dire) + + # 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 +def bitonic_sort(a, low, cnt, dire): + if cnt > 1: + k = int(cnt / 2) + bitonic_sort(a, low, k, 1) + bitonic_sort(a, low + k, k, 0) + bitonic_merge(a, low, cnt, dire) + + # Caller of bitonic_sort for sorting the entire array of length N + + +# in ASCENDING order +def sort(a, N, up): + bitonic_sort(a, 0, N, up) + + +if __name__ == "__main__": + + a = [] + + n = int(input().strip()) + for i in range(n): + a.append(int(input().strip())) + up = 1 + + sort(a, n, up) + print("\n\nSorted array is") + for i in range(n): + print("%d" % a[i]) 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 e28e7aa4fbe2..eb356bc7dcad 100644 --- a/sorts/bubble_sort.py +++ b/sorts/bubble_sort.py @@ -1,18 +1,3 @@ -""" -This is pure python implementation of bubble sort algorithm - -For doctests run following command: -python -m doctest -v bubble_sort.py -or -python3 -m doctest -v bubble_sort.py - -For manual testing run: -python bubble_sort.py -""" - -from __future__ import print_function - - def bubble_sort(collection): """Pure implementation of bubble sort algorithm in Python @@ -21,32 +6,38 @@ 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]) + >>> bubble_sort([-2, -45, -5]) [-45, -5, -2] + + >>> bubble_sort([-23, 0, 6, -4, 34]) + [-23, -4, 0, 6, 34] + + >>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34]) + True """ length = len(collection) - for i in range(length): + for i in range(length - 1): swapped = False - for j in range(length-1): - 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 +if __name__ == "__main__": + import time - user_input = raw_input('Enter numbers separated by a comma:\n').strip() - unsorted = [int(item) for item in user_input.split(',')] - print(bubble_sort(unsorted)) + 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 1c4b4577af61..217ee5893c4b 100644 --- a/sorts/bucket_sort.py +++ b/sorts/bucket_sort.py @@ -1,57 +1,49 @@ #!/usr/bin/env python + +"""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. +# 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 P26_InsertionSort import insertionSort -import math +# 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) 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)): - insertionSort(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) + +def bucket_sort(my_list, bucket_size=DEFAULT_BUCKET_SIZE): + if len(my_list) == 0: + raise Exception("Please add some elements in the array.") + + min_value, max_value = (min(my_list), max(my_list)) + bucket_count = (max_value - min_value) // bucket_size + 1 + buckets = [[] for _ in range(int(bucket_count))] + + for i in range(len(my_list)): + buckets[int((my_list[i] - min_value) // bucket_size)].append(my_list[i]) + + return sorted( + [buckets[i][j] for i in range(len(buckets)) for j in range(len(buckets[i]))] + ) + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:").strip() + unsorted = [float(n) for n in user_input.split(",") if len(user_input) > 0] + print(bucket_sort(unsorted)) diff --git a/sorts/cocktail_shaker_sort.py b/sorts/cocktail_shaker_sort.py index 8ad3383bbe9f..ab624421a3d6 100644 --- a/sorts/cocktail_shaker_sort.py +++ b/sorts/cocktail_shaker_sort.py @@ -1,32 +1,26 @@ -from __future__ import print_function - def cocktail_shaker_sort(unsorted): """ Pure implementation of the cocktail shaker sort algorithm in Python. """ - 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(',')] + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] cocktail_shaker_sort(unsorted) print(unsorted) diff --git a/sorts/comb_sort.py b/sorts/comb_sort.py new file mode 100644 index 000000000000..416fd4552697 --- /dev/null +++ b/sorts/comb_sort.py @@ -0,0 +1,62 @@ +""" +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 + +For doctests run following command: +python -m doctest -v comb_sort.py +or +python3 -m doctest -v comb_sort.py + +For manual testing run: +python comb_sort.py +""" + + +def comb_sort(data: list) -> list: + """Pure implementation of comb sort algorithm in Python + :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([99, 45, -7, 8, 2, 0, -15, 3]) + [-15, -7, 0, 2, 3, 8, 45, 99] + """ + shrink_factor = 1.3 + gap = len(data) + completed = False + + while not completed: + + # Update the gap value for a next comb + gap = int(gap / shrink_factor) + if gap <= 1: + completed = True + + index = 0 + while index + gap < len(data): + if data[index] > data[index + gap]: + # Swap values + data[index], data[index + gap] = data[index + gap], data[index] + completed = False + index += 1 + + return data + + +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(comb_sort(unsorted)) diff --git a/sorts/counting_sort.py b/sorts/counting_sort.py index 4ca682b13cca..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,18 +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 -if __name__ == '__main__': - try: - raw_input # Python 2 - except NameError: - raw_input = input # Python 3 +def counting_sort_string(string): + """ + >>> counting_sort_string("thisisthestring") + 'eghhiiinrsssttt' + """ + return "".join([chr(i) for i in counting_sort([ord(c) for c in string])]) + + +if __name__ == "__main__": + # Test string sort + assert "eghhiiinrsssttt" == counting_sort_string("thisisthestring") - 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/countingsort.py b/sorts/countingsort.py deleted file mode 100644 index 18ee8b851fd7..000000000000 --- a/sorts/countingsort.py +++ /dev/null @@ -1,42 +0,0 @@ -from __future__ import print_function -# Python program for counting sort - -# This is the main function that sort the given string arr[] in -# in the alphabetical order -def countSort(arr): - - # The output character array that will have sorted arr - output = [0 for i in range(256)] - - # Create a count array to store count of inidividul - # characters and initialize count array as 0 - count = [0 for i in range(256)] - - # For storing the resulting answer since the - # string is immutable - ans = ["" for _ in arr] - - # Store count of each character - for i in arr: - count[ord(i)] += 1 - - # Change count[i] so that count[i] now contains actual - # position of this character in output array - for i in range(256): - count[i] += count[i-1] - - # Build the output character array - for i in range(len(arr)): - output[count[ord(arr[i])]-1] = arr[i] - count[ord(arr[i])] -= 1 - - # Copy the output array to arr, so that arr now - # contains sorted characters - for i in range(len(arr)): - ans[i] = output[i] - return ans - -# Driver program to test above function -arr = "thisisthestring" -ans = countSort(arr) -print ("Sorted string array is %s" %("".join(ans))) diff --git a/sorts/cyclesort.py b/sorts/cycle_sort.py similarity index 74% rename from sorts/cyclesort.py rename to sorts/cycle_sort.py index ee19a1ade360..4ce6a2a0e757 100644 --- a/sorts/cyclesort.py +++ b/sorts/cycle_sort.py @@ -1,7 +1,4 @@ # Code contributed by Honey Sharma -from __future__ import print_function - - def cycle_sort(array): ans = 0 @@ -44,17 +41,12 @@ def cycle_sort(array): # 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=' ') +if __name__ == "__main__": + user_input = 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 65% rename from sorts/external-sort.py rename to sorts/external_sort.py index 6c4adc94c0f0..e5b2c045fa95 100644 --- a/sorts/external-sort.py +++ b/sorts/external_sort.py @@ -4,11 +4,11 @@ # Sort large text files in a minimum amount of memory # import os -import sys import argparse -class FileSplitter(object): - BLOCK_FILENAME_FORMAT = 'block_{0}.dat' + +class FileSplitter: + BLOCK_FILENAME_FORMAT = "block_{0}.dat" def __init__(self, filename): self.filename = filename @@ -16,37 +16,35 @@ def __init__(self, filename): def write_block(self, data, block_number): filename = self.BLOCK_FILENAME_FORMAT.format(block_number) - file = open(filename, 'w') - file.write(data) - file.close() + with open(filename, "w") as file: + file.write(data) self.block_filenames.append(filename) def get_block_filenames(self): return self.block_filenames def split(self, block_size, sort_key=None): - file = open(self.filename, 'r') i = 0 + with open(self.filename) as file: + while True: + lines = file.readlines(block_size) - while True: - lines = file.readlines(block_size) - - if lines == []: - break + if lines == []: + break - if sort_key is None: - lines.sort() - else: - lines.sort(key=sort_key) + if sort_key is None: + lines.sort() + else: + lines.sort(key=sort_key) - self.write_block(''.join(lines), i) - i += 1 + 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 @@ -58,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() @@ -66,15 +64,18 @@ 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() if len(self.empty) == self.num_buffers: return False @@ -88,29 +89,27 @@ 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): - outfile = open(outfilename, 'w', buffer_size) buffers = FilesArray(self.get_file_handles(filenames, buffer_size)) - - while buffers.refresh(): - min_index = self.merge_strategy.select(buffers.get_dict()) - outfile.write(buffers.unshift(min_index)) + 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)) 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 @@ -121,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() @@ -130,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..58a44c94da43 100644 --- a/sorts/gnome_sort.py +++ b/sorts/gnome_sort.py @@ -1,30 +1,25 @@ -from __future__ import print_function +"""Gnome Sort Algorithm.""" + def gnome_sort(unsorted): - """ - Pure implementation of the gnome sort algorithm in Python. - """ + """Pure implementation of the gnome sort algorithm in Python.""" if len(unsorted) <= 1: return unsorted - + i = 1 - + while i < len(unsorted): - if unsorted[i-1] <= unsorted[i]: + if unsorted[i - 1] <= unsorted[i]: i += 1 else: - unsorted[i-1], unsorted[i] = unsorted[i], unsorted[i-1] + unsorted[i - 1], unsorted[i] = unsorted[i], unsorted[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(',')] + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] gnome_sort(unsorted) print(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/i_sort.py b/sorts/i_sort.py new file mode 100644 index 000000000000..f6100a8d0819 --- /dev/null +++ b/sorts/i_sort.py @@ -0,0 +1,21 @@ +def insertionSort(arr): + """ + >>> a = arr[:] + >>> insertionSort(a) + >>> a == sorted(a) + True + """ + for i in range(1, len(arr)): + key = arr[i] + j = i - 1 + while j >= 0 and key < arr[j]: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + + +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +print("Sorted array is:") +for i in range(len(arr)): + print("%d" % arr[i]) diff --git a/sorts/insertion_sort.py b/sorts/insertion_sort.py index b7a4aa7a3c33..ca678381b431 100644 --- a/sorts/insertion_sort.py +++ b/sorts/insertion_sort.py @@ -1,5 +1,5 @@ """ -This is a pure python implementation of the insertion sort algorithm +This is a pure Python implementation of the insertion sort algorithm For doctests run following command: python -m doctest -v insertion_sort.py @@ -9,7 +9,6 @@ For manual testing run: python insertion_sort.py """ -from __future__ import print_function def insertion_sort(collection): @@ -29,21 +28,23 @@ def insertion_sort(collection): >>> insertion_sort([-2, -5, -45]) [-45, -5, -2] """ - for index in range(1, len(collection)): - while 0 < index and collection[index] < collection[index - 1]: - collection[index], collection[ - index - 1] = collection[index - 1], collection[index] - index -= 1 - return collection + for loop_index in range(1, len(collection)): + insertion_index = loop_index + while ( + insertion_index > 0 + and collection[insertion_index - 1] > collection[insertion_index] + ): + collection[insertion_index], collection[insertion_index - 1] = ( + collection[insertion_index - 1], + collection[insertion_index], + ) + insertion_index -= 1 + 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(insertion_sort(unsorted)) diff --git a/sorts/iterative_merge_sort.py b/sorts/iterative_merge_sort.py new file mode 100644 index 000000000000..e6e1513393e0 --- /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 typing import List + + +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_sort.py b/sorts/merge_sort.py index ca4d319fa7f1..e8031a1cb97c 100644 --- a/sorts/merge_sort.py +++ b/sorts/merge_sort.py @@ -1,5 +1,5 @@ """ -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 @@ -9,7 +9,6 @@ For manual testing run: python merge_sort.py """ -from __future__ import print_function def merge_sort(collection): @@ -29,44 +28,25 @@ def merge_sort(collection): >>> 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 - - while j < right_length: - collection[k] = right_half[j] - j += 1 - k += 1 - - 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(merge_sort(unsorted)) + def merge(left, right): + """merge left and right + :param left: left collection + :param right: right collection + :return: merge result + """ + result = [] + while left and right: + result.append((left if left[0] <= right[0] else right).pop(0)) + return result + left + right + + if len(collection) <= 1: + return collection + mid = len(collection) // 2 + return merge(merge_sort(collection[:mid]), merge_sort(collection[mid:])) + + +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/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/normal_distribution_QuickSort_README.md b/sorts/normal_distribution_quick_sort.md similarity index 100% rename from sorts/normal_distribution_QuickSort_README.md rename to sorts/normal_distribution_quick_sort.md 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..ec045d9dd08d --- /dev/null +++ b/sorts/odd_even_transposition_single_threaded.py @@ -0,0 +1,35 @@ +""" +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 OddEvenTransposition(arr): + for i in range(0, len(arr)): + for i in range(i % 2, len(arr) - 1, 2): + if arr[i + 1] < arr[i]: + arr[i], arr[i + 1] = arr[i + 1], arr[i] + print(*arr) + + return arr + + +# creates a list and sorts it +def main(): + list = [] + + for i in range(10, 0, -1): + list.append(i) + print("Initial List") + print(*list) + + list = OddEvenTransposition(list) + + print("Sorted List\n") + print(*list) + + +if __name__ == "__main__": + main() 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/pigeon_sort.py b/sorts/pigeon_sort.py new file mode 100644 index 000000000000..cf900699bc8d --- /dev/null +++ b/sorts/pigeon_sort.py @@ -0,0 +1,68 @@ +""" + 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 +""" + + +def pigeon_sort(array): + """ + 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 + + # Manually finds the minimum and maximum of the array. + min = array[0] + max = array[0] + + for i in range(len(array)): + if array[i] < min: + min = array[i] + elif array[i] > max: + max = array[i] + + # Compute the variables + holes_range = max - min + 1 + holes = [0 for _ in range(holes_range)] + holes_repeat = [0 for _ in range(holes_range)] + + # Make the sorting. + for i in range(len(array)): + index = array[i] - min + if holes[index] != array[i]: + holes[index] = array[i] + holes_repeat[index] += 1 + else: + 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__": + 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 136cbc021669..f2a55c58b437 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -1,5 +1,5 @@ """ -This is a pure python implementation of the quick sort algorithm +This is a pure Python implementation of the quick sort algorithm For doctests run following command: python -m doctest -v quick_sort.py @@ -9,10 +9,9 @@ For manual testing run: python quick_sort.py """ -from __future__ import print_function -def quick_sort(ARRAY): +def quick_sort(collection): """Pure implementation of quick sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous @@ -29,22 +28,24 @@ def quick_sort(ARRAY): >>> quick_sort([-2, -5, -45]) [-45, -5, -2] """ - ARRAY_LENGTH = len(ARRAY) - if( ARRAY_LENGTH <= 1): - return ARRAY + length = len(collection) + if length <= 1: + return collection else: - PIVOT = ARRAY[0] - GREATER = [ element for element in ARRAY[1:] if element > PIVOT ] - LESSER = [ element for element in ARRAY[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) ) + # Use the last element as the first pivot + pivot = collection.pop() + # Put elements greater than pivot in greater list + # Put elements lesser than pivot in lesser list + greater, lesser = [], [] + for element in collection: + if element > pivot: + greater.append(element) + 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_3partition.py b/sorts/quick_sort_3_partition.py similarity index 60% rename from sorts/quick_sort_3partition.py rename to sorts/quick_sort_3_partition.py index def646cdbc50..a25ac7def802 100644 --- a/sorts/quick_sort_3partition.py +++ b/sorts/quick_sort_3_partition.py @@ -1,5 +1,3 @@ -from __future__ import print_function - def quick_sort_3partition(sorting, left, right): if right <= left: return @@ -19,13 +17,9 @@ 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) +if __name__ == "__main__": + 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 b0b4b41ab24f..2990247a0ac0 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -1,28 +1,26 @@ -def radixsort(lst): - RADIX = 10 - maxLength = False - tmp , placement = -1, 1 +def radix_sort(lst): + RADIX = 10 + placement = 1 - while not maxLength: - maxLength = True - # declare and initialize buckets - buckets = [list() for _ in range( RADIX )] + # get the maximum number + max_digit = max(lst) - # split lst between lists - for i in lst: - tmp = int((i / placement) % RADIX) - buckets[tmp].append(i) + while placement < max_digit: + # declare and initialize buckets + buckets = [list() for _ in range(RADIX)] - if maxLength and tmp > 0: - maxLength = False + # 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 + # empty lists into lst array + a = 0 + for b in range(RADIX): + buck = buckets[b] + for i in buck: + lst[a] = i + a += 1 - # move to next - placement *= RADIX + # move to next + placement *= RADIX diff --git a/sorts/random_normal_distribution_quicksort.py b/sorts/random_normal_distribution_quicksort.py new file mode 100644 index 000000000000..be3b90190407 --- /dev/null +++ b/sorts/random_normal_distribution_quicksort.py @@ -0,0 +1,62 @@ +from random import randint +from tempfile import TemporaryFile +import numpy as np + + +def _inPlaceQuickSort(A, start, end): + count = 0 + if start < end: + pivot = randint(start, end) + temp = A[end] + A[end] = A[pivot] + A[pivot] = temp + + p, count = _inPlacePartition(A, start, end) + count += _inPlaceQuickSort(A, start, p - 1) + count += _inPlaceQuickSort(A, p + 1, end) + return count + + +def _inPlacePartition(A, start, end): + + count = 0 + pivot = randint(start, end) + temp = A[end] + A[end] = A[pivot] + A[pivot] = temp + newPivotIndex = start - 1 + for index in range(start, end): + + count += 1 + if A[index] < A[end]: # check if current val is less than pivot value + newPivotIndex = newPivotIndex + 1 + temp = A[newPivotIndex] + A[newPivotIndex] = A[index] + A[index] = temp + + temp = A[newPivotIndex + 1] + A[newPivotIndex + 1] = A[end] + A[end] = temp + return newPivotIndex + 1, count + + +outfile = TemporaryFile() +p = 100 # 1000 elements are to be sorted + + +mu, sigma = 0, 1 # mean and standard deviation +X = np.random.normal(mu, sigma, p) +np.save(outfile, X) +print("The array is") +print(X) + + +outfile.seek(0) # using the same array +M = np.load(outfile) +r = len(M) - 1 +z = _inPlaceQuickSort(M, 0, r) + +print( + "No of Comparisons for 100 elements selected from a standard normal distribution is :" +) +print(z) diff --git a/sorts/random_normaldistribution_quicksort.py b/sorts/random_normaldistribution_quicksort.py deleted file mode 100644 index bd730b3b1e6d..000000000000 --- a/sorts/random_normaldistribution_quicksort.py +++ /dev/null @@ -1,67 +0,0 @@ -from __future__ import print_function -from random import randint -from tempfile import TemporaryFile -import numpy as np -import math - - - -def _inPlaceQuickSort(A,start,end): - count = 0 - if start 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/recursive_bubble_sort.py b/sorts/recursive_bubble_sort.py new file mode 100644 index 000000000000..616044778a4a --- /dev/null +++ b/sorts/recursive_bubble_sort.py @@ -0,0 +1,41 @@ +def bubble_sort(list1): + """ + It is similar is bubble sort but recursive. + :param list1: mutable ordered sequence of elements + :return: the same list in ascending order + + >>> bubble_sort([0, 5, 2, 3, 2]) + [0, 2, 2, 3, 5] + + >>> bubble_sort([]) + [] + + >>> bubble_sort([-2, -45, -5]) + [-45, -5, -2] + + >>> bubble_sort([-23, 0, 6, -4, 34]) + [-23, -4, 0, 6, 34] + + >>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34]) + True + + >>> bubble_sort(['z','a','y','b','x','c']) + ['a', 'b', 'c', 'x', 'y', 'z'] + + """ + + for i, num in enumerate(list1): + try: + if list1[i + 1] < num: + list1[i] = list1[i + 1] + list1[i + 1] = num + bubble_sort(list1) + except IndexError: + pass + return list1 + + +if __name__ == "__main__": + list1 = [33, 99, 22, 11, 66] + bubble_sort(list1) + print(list1) diff --git a/sorts/recursive_insertion_sort.py b/sorts/recursive_insertion_sort.py new file mode 100644 index 000000000000..5b14c2a6c139 --- /dev/null +++ b/sorts/recursive_insertion_sort.py @@ -0,0 +1,75 @@ +""" +A recursive implementation of the insertion sort algorithm +""" + +from typing import List + + +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/selection_sort.py b/sorts/selection_sort.py index 432d14090b12..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): @@ -31,23 +30,17 @@ def selection_sort(collection): """ length = len(collection) - for i in range(length): + for i in range(length - 1): least = i 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..80d95870f95b 100644 --- a/sorts/shell_sort.py +++ b/sorts/shell_sort.py @@ -1,5 +1,5 @@ """ -This is a pure python implementation of the shell sort algorithm +This is a pure Python implementation of the shell sort algorithm For doctests run following command: python -m doctest -v shell_sort.py @@ -9,7 +9,6 @@ For manual testing run: python shell_sort.py """ -from __future__ import print_function def shell_sort(collection): @@ -43,12 +42,8 @@ def shell_sort(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(',')] +if __name__ == "__main__": + 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/sleep_sort.py b/sorts/sleep_sort.py new file mode 100644 index 000000000000..0feda9c5e038 --- /dev/null +++ b/sorts/sleep_sort.py @@ -0,0 +1,49 @@ +""" +Sleep sort is probably the wierdest of all sorting functions with time-complexity of +O(max(input)+n) which is quite different from almost all other sorting techniques. +If the number of inputs is small then the complexity can be approximated to be +O(max(input)) which is a constant + +If the number of inputs is large, the complexity is approximately O(n). + +This function uses multithreading a kind of higher order programming and calls n +functions, each with a sleep time equal to its number. Hence each of function wakes +in sorted time. + +This function is not stable for very large values. + +https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort +""" +from threading import Timer +from time import sleep +from typing import List + + +def sleep_sort(values: List[int]) -> List[int]: + """ + Sort the list using sleepsort. + >>> sleep_sort([3, 2, 4, 7, 3, 6, 9, 1]) + [1, 2, 3, 3, 4, 6, 7, 9] + >>> sleep_sort([3, 2, 1, 9, 8, 4, 2]) + [1, 2, 2, 3, 4, 8, 9] + """ + sleep_sort.result = [] + + def append_to_result(x): + sleep_sort.result.append(x) + + mx = values[0] + for value in values: + if mx < value: + mx = value + Timer(value, append_to_result, [value]).start() + sleep(mx + 1) + return sleep_sort.result + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + print(sleep_sort([3, 2, 4, 7, 3, 6, 9, 1])) diff --git a/sorts/sortinggraphs.png b/sorts/sortinggraphs.png deleted file mode 100644 index 628245f3eb39..000000000000 Binary files a/sorts/sortinggraphs.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 94cf68033b55..716170a94fd1 100644 --- a/sorts/tree_sort.py +++ b/sorts/tree_sort.py @@ -1,45 +1,53 @@ -# 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 == None: + if self.left is None: self.left = node(val) else: self.left.insert(val) elif val > self.val: - if self.right == None: + if self.right is None: self.right = node(val) else: self.right.insert(val) else: self.val = val + def inorder(root, res): - # Recursive travesal + # Recursive travesal 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..5ecc55e9cf69 --- /dev/null +++ b/sorts/unknown_sort.py @@ -0,0 +1,39 @@ +""" +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 new file mode 100644 index 000000000000..5e5220ffbf05 --- /dev/null +++ b/sorts/wiggle_sort.py @@ -0,0 +1,26 @@ +""" +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] +one possible Wiggle Sorted answer is [3, 5, 1, 6, 2, 4]. +""" + + +def wiggle_sort(nums): + """Perform Wiggle Sort.""" + 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] + + +if __name__ == "__main__": + 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) diff --git a/strings/aho-corasick.py b/strings/aho-corasick.py new file mode 100644 index 000000000000..bb6955bdd423 --- /dev/null +++ b/strings/aho-corasick.py @@ -0,0 +1,92 @@ +from collections import deque + + +class Automaton: + def __init__(self, keywords): + 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, char): + for state in self.adlist[current_state]["next_states"]: + if char == self.adlist[state]["value"]: + return state + return None + + def add_keyword(self, keyword): + 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): + 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): + """ + >>> 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..bd777c7c7e05 --- /dev/null +++ b/strings/boyer_moore_search.py @@ -0,0 +1,83 @@ +""" +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 +""" + + +class BoyerMooreSearch: + def __init__(self, text, pattern): + self.text, self.pattern = text, pattern + self.textLen, self.patLen = len(text), len(pattern) + + def match_in_pattern(self, char): + """ 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): + """ finds 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): + # 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/check_pangram.py b/strings/check_pangram.py new file mode 100644 index 000000000000..410afd8cc609 --- /dev/null +++ b/strings/check_pangram.py @@ -0,0 +1,30 @@ +# Created by sarathkaul on 12/11/19 + + +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("My name is Unknown") + False + >>> check_pangram("The quick brown fox jumps over the la_y dog") + False + """ + 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 + + +if __name__ == "main": + check_str = "INPUT STRING" + status = check_pangram(check_str) + print(f"{check_str} is {'not ' if status else ''}a pangram string") diff --git a/strings/is_palindrome.py b/strings/is_palindrome.py new file mode 100644 index 000000000000..3070970ca6d0 --- /dev/null +++ b/strings/is_palindrome.py @@ -0,0 +1,19 @@ +def is_palindrome(s): + """ + 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 + """ + 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/knuth-morris-pratt.py b/strings/knuth_morris_pratt.py similarity index 97% rename from strings/knuth-morris-pratt.py rename to strings/knuth_morris_pratt.py index 4553944284be..c7e96887c387 100644 --- a/strings/knuth-morris-pratt.py +++ b/strings/knuth_morris_pratt.py @@ -46,14 +46,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 new file mode 100644 index 000000000000..9b8793544a99 --- /dev/null +++ b/strings/levenshtein_distance.py @@ -0,0 +1,76 @@ +""" +This is a Python implementation of the levenshtein distance. +Levenshtein distance is a string metric for measuring the +difference between two sequences. + +For doctests run following command: +python -m doctest -v levenshtein-distance.py +or +python3 -m doctest -v levenshtein-distance.py + +For manual testing run: +python levenshtein-distance.py +""" + + +def levenshtein_distance(first_word, second_word): + """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. + :return: the levenshtein distance between the two words. + Examples: + >>> levenshtein_distance("planet", "planetary") + 3 + >>> levenshtein_distance("", "test") + 4 + >>> levenshtein_distance("book", "back") + 2 + >>> levenshtein_distance("book", "book") + 0 + >>> levenshtein_distance("test", "") + 4 + >>> levenshtein_distance("", "") + 0 + >>> levenshtein_distance("orchestration", "container") + 10 + """ + # The longer word should come first + if len(first_word) < len(second_word): + return levenshtein_distance(second_word, first_word) + + if len(second_word) == 0: + return len(first_word) + + previous_row = range(len(second_word) + 1) + + for i, c1 in enumerate(first_word): + + current_row = [i + 1] + + for j, c2 in enumerate(second_word): + + # Calculate insertions, deletions and substitutions + insertions = previous_row[j + 1] + 1 + deletions = current_row[j] + 1 + substitutions = previous_row[j] + (c1 != c2) + + # Get the minimum to append to the current row + current_row.append(min(insertions, deletions, substitutions)) + + # Store the previous row + previous_row = current_row + + # Returns the last element (distance) + return previous_row[-1] + + +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 + ) + ) diff --git a/strings/lower.py b/strings/lower.py new file mode 100644 index 000000000000..c3a6e598b9ea --- /dev/null +++ b/strings/lower.py @@ -0,0 +1,29 @@ +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 65 <= ord(char) <= 90 else char for char in word + ) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/strings/manacher.py b/strings/manacher.py new file mode 100644 index 000000000000..95aba1fbe65d --- /dev/null +++ b/strings/manacher.py @@ -0,0 +1,103 @@ +def palindromic_string(input_string): + """ + >>> 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]: + new_input_string += i + "|" + # 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)): + 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 + + # 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 + r = i + k - 1 + + # update max_length and start position + if max_length < length[i]: + max_length = length[i] + start = i + + # 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__": + 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 deleted file mode 100644 index 7e3298137c05..000000000000 --- a/strings/min-cost-string-conversion.py +++ /dev/null @@ -1,123 +0,0 @@ -from __future__ import print_function - -try: - xrange #Python 2 -except NameError: - xrange = range #Python 3 - -''' -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__': - from time import sleep - _, 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) - - file = open('min_cost.txt', 'w') - - string = list('Python') - i = 0 - cost = 0 - 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)) - file.close() diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py new file mode 100644 index 000000000000..abc9d2c65158 --- /dev/null +++ b/strings/min_cost_string_conversion.py @@ -0,0 +1,118 @@ +""" +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 range(n + 1)] for _ in range(m + 1)] + ops = [[0 for _ in range(n + 1)] for _ in range(m + 1)] + + for i in range(1, m + 1): + costs[i][0] = i * cD + ops[i][0] = "D%c" % X[i - 1] + + for i in range(1, n + 1): + costs[0][i] = i * cI + ops[0][i] = "I%c" % Y[i - 1] + + for i in range(1, m + 1): + for j in range(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)) diff --git a/strings/naive_string_search.py b/strings/naive_string_search.py new file mode 100644 index 000000000000..a8c2ea584399 --- /dev/null +++ b/strings/naive_string_search.py @@ -0,0 +1,32 @@ +""" +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) diff --git a/strings/rabin-karp.py b/strings/rabin_karp.py similarity index 51% rename from strings/rabin-karp.py rename to strings/rabin_karp.py index 04a849266ead..22da0de80f4c 100644 --- a/strings/rabin-karp.py +++ b/strings/rabin_karp.py @@ -1,6 +1,11 @@ +# Numbers of alphabet which we call base +alphabet_size = 256 +# Modulus to hash a string +modulus = 1000003 + + def rabin_karp(pattern, text): """ - 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. @@ -12,22 +17,42 @@ def rabin_karp(pattern, text): 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(): + """ + >>> test_rabin_karp() + Success. + """ # Test 1) pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" @@ -48,3 +73,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..0462292b78d2 --- /dev/null +++ b/strings/remove_duplicate.py @@ -0,0 +1,20 @@ +# Created by sarathkaul on 14/11/19 + + +def remove_duplicates(sentence: str) -> str: + """ + Reomove duplicates from sentence + >>> remove_duplicates("Python is great and Java is also great") + 'Java Python also and great is' + """ + sen_list = sentence.split(" ") + check = set() + + for a_word in sen_list: + check.add(a_word) + + return " ".join(sorted(check)) + + +if __name__ == "__main__": + print(remove_duplicates("INPUT_SENTENCE")) diff --git a/strings/reverse_words.py b/strings/reverse_words.py new file mode 100644 index 000000000000..6b5cc6b04039 --- /dev/null +++ b/strings/reverse_words.py @@ -0,0 +1,21 @@ +# Created by sarathkaul on 18/11/19 +# Edited by farnswj1 on 4/4/20 + + +def reverse_words(input_str: str) -> str: + """ + Reverses words in a given string + >>> sentence = "I love Python" + >>> reverse_words(sentence) == " ".join(sentence.split()[::-1]) + True + >>> reverse_words(sentence) + 'Python love I' + """ + input_str = input_str.split(" ") + new_str = list() + + return " ".join(reversed(input_str)) + + +if __name__ == "__main__": + print(reverse_words("INPUT STRING")) diff --git a/strings/split.py b/strings/split.py new file mode 100644 index 000000000000..d5bff316429f --- /dev/null +++ b/strings/split.py @@ -0,0 +1,33 @@ +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/upper.py b/strings/upper.py new file mode 100644 index 000000000000..59b16096af0b --- /dev/null +++ b/strings/upper.py @@ -0,0 +1,26 @@ +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 capital letter it is getting shift by 32 which makes it a capital case letter + return "".join( + chr(ord(char) - 32) if 97 <= ord(char) <= 122 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/traversals/binary_tree_traversals.py b/traversals/binary_tree_traversals.py index cbcaf08b7b03..c522ecebc0ff 100644 --- a/traversals/binary_tree_traversals.py +++ b/traversals/binary_tree_traversals.py @@ -1,13 +1,8 @@ """ -This is pure python implementation of tree traversal algorithms +This is pure Python implementation of tree traversal algorithms """ -from __future__ import print_function import queue - -try: - raw_input # Python 2 -except NameError: - raw_input = input # Python 3 +from typing import List class TreeNode: @@ -19,35 +14,45 @@ 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': - return None - data = int(check) - q = queue.Queue() - tree_node = TreeNode(data) + check = input("Enter the value of the root node: ").strip().lower() or "n" + if check == "n": + return None + 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=" ") @@ -55,7 +60,21 @@ def pre_order(node): 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) @@ -63,7 +82,21 @@ def in_order(node): 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) @@ -71,10 +104,24 @@ def post_order(node): print(node.data, end=" ") -def level_order(node): +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() @@ -85,22 +132,173 @@ def level_order(node): q.put(node_dequeued.right) -if __name__ == '__main__': - print("\n********* Binary Tree Traversals ************\n") +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 = queue.Queue() + q.put(node) + while not q.empty(): + list = [] + while not q.empty(): + node_dequeued = q.get() + print(node_dequeued.data, end=" ") + if node_dequeued.left: + list.append(node_dequeued.left) + if node_dequeued.right: + list.append(node_dequeued.right) + print() + for node in list: + q.put(node) + + +# iteration version +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: List[TreeNode] = [] + n = node + while n or stack: + while n: # start from root node, find its left child + print(n.data, end=" ") + stack.append(n) + n = n.left + # end of while means current node doesn't have left child + n = stack.pop() + # start to traverse its right child + n = n.right + + +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: List[TreeNode] = [] + n = node + while n or stack: + while n: + stack.append(n) + n = n.left + n = stack.pop() + print(n.data, end=" ") + n = n.right + + +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 = [], [] + n = node + stack1.append(n) + while stack1: # to find the reversed order of post order, store it in stack2 + n = stack1.pop() + if n.left: + stack1.append(n.left) + if n.right: + stack1.append(n.right) + stack2.append(n) + while stack2: # pop up from stack2 will be the post order + 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 + + 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(prompt("Actual Level Order Traversal")) + level_order_actual(node) + print("*" * 50 + "\n") + + print(prompt("Pre Order Traversal - Iteration Version")) + pre_order_iter(node) + print(prompt() + "\n") + + print(prompt("In Order Traversal - Iteration Version")) + in_order_iter(node) + print(prompt() + "\n") + + print(prompt("Post Order Traversal - Iteration Version")) + post_order_iter(node) + print(prompt()) diff --git a/web_programming/crawl_google_results.py b/web_programming/crawl_google_results.py new file mode 100644 index 000000000000..7d2be7c03c22 --- /dev/null +++ b/web_programming/crawl_google_results.py @@ -0,0 +1,25 @@ +import sys +import webbrowser + +from bs4 import BeautifulSoup +from fake_useragent import UserAgent +import requests + + +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/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/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..f6626770e833 --- /dev/null +++ b/web_programming/fetch_github_info.py @@ -0,0 +1,17 @@ +# Created by sarathkaul on 14/11/19 + +import requests + +_GITHUB_API = "https://api.github.com/user" + + +def fetch_github_info(auth_user: str, auth_pass: str) -> None: + # fetching github info using requests + info = requests.get(_GITHUB_API, auth=(auth_user, auth_pass)) + + for a_info, a_detail in info.json().items(): + print(f"{a_info}: {a_detail}") + + +if __name__ == "main": + fetch_github_info("", "") 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..811c21fb00e4 --- /dev/null +++ b/web_programming/get_imdb_top_250_movies_csv.py @@ -0,0 +1,29 @@ +import csv +from typing import Dict + +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..522e423b4eab --- /dev/null +++ b/web_programming/get_imdbtop.py @@ -0,0 +1,20 @@ +from bs4 import BeautifulSoup +import requests + + +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/slack_message.py b/web_programming/slack_message.py new file mode 100644 index 000000000000..8ea9d5d0add2 --- /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("", "")