mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 10:28:39 +00:00
Merge branch 'master' into fix_bucket_sort
This commit is contained in:
commit
4a376c6002
@ -1,4 +0,0 @@
|
|||||||
[report]
|
|
||||||
sort = Cover
|
|
||||||
omit =
|
|
||||||
.env/*
|
|
8
.devcontainer/Dockerfile
Normal file
8
.devcontainer/Dockerfile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# https://github.com/microsoft/vscode-dev-containers/blob/main/containers/python-3/README.md
|
||||||
|
ARG VARIANT=3.11-bookworm
|
||||||
|
FROM mcr.microsoft.com/vscode/devcontainers/python:${VARIANT}
|
||||||
|
COPY requirements.txt /tmp/pip-tmp/
|
||||||
|
RUN python3 -m pip install --upgrade pip \
|
||||||
|
&& python3 -m pip install --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
|
||||||
|
&& pipx install pre-commit ruff \
|
||||||
|
&& pre-commit install
|
42
.devcontainer/devcontainer.json
Normal file
42
.devcontainer/devcontainer.json
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"name": "Python 3",
|
||||||
|
"build": {
|
||||||
|
"dockerfile": "Dockerfile",
|
||||||
|
"context": "..",
|
||||||
|
"args": {
|
||||||
|
// Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6
|
||||||
|
// Append -bullseye or -buster to pin to an OS version.
|
||||||
|
// Use -bullseye variants on local on arm64/Apple Silicon.
|
||||||
|
"VARIANT": "3.11-bookworm",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Configure tool-specific properties.
|
||||||
|
"customizations": {
|
||||||
|
// Configure properties specific to VS Code.
|
||||||
|
"vscode": {
|
||||||
|
// Set *default* container specific settings.json values on container create.
|
||||||
|
"settings": {
|
||||||
|
"python.defaultInterpreterPath": "/usr/local/bin/python",
|
||||||
|
"python.linting.enabled": true,
|
||||||
|
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
|
||||||
|
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy"
|
||||||
|
},
|
||||||
|
|
||||||
|
// Add the IDs of extensions you want installed when the container is created.
|
||||||
|
"extensions": [
|
||||||
|
"ms-python.python",
|
||||||
|
"ms-python.vscode-pylance"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||||
|
// "forwardPorts": [],
|
||||||
|
|
||||||
|
// Use 'postCreateCommand' to run commands after the container is created.
|
||||||
|
// "postCreateCommand": "pip3 install --user -r requirements.txt",
|
||||||
|
|
||||||
|
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
|
||||||
|
"remoteUser": "vscode"
|
||||||
|
}
|
10
.github/CODEOWNERS
vendored
10
.github/CODEOWNERS
vendored
@ -31,11 +31,11 @@
|
|||||||
|
|
||||||
# /data_structures/ @cclauss # TODO: Uncomment this line after Hacktoberfest
|
# /data_structures/ @cclauss # TODO: Uncomment this line after Hacktoberfest
|
||||||
|
|
||||||
/digital_image_processing/ @mateuszz0000
|
# /digital_image_processing/
|
||||||
|
|
||||||
# /divide_and_conquer/
|
# /divide_and_conquer/
|
||||||
|
|
||||||
/dynamic_programming/ @Kush1101
|
# /dynamic_programming/
|
||||||
|
|
||||||
# /file_transfer/
|
# /file_transfer/
|
||||||
|
|
||||||
@ -59,7 +59,7 @@
|
|||||||
|
|
||||||
# /machine_learning/
|
# /machine_learning/
|
||||||
|
|
||||||
/maths/ @Kush1101
|
# /maths/
|
||||||
|
|
||||||
# /matrix/
|
# /matrix/
|
||||||
|
|
||||||
@ -69,7 +69,7 @@
|
|||||||
|
|
||||||
# /other/ @cclauss # TODO: Uncomment this line after Hacktoberfest
|
# /other/ @cclauss # TODO: Uncomment this line after Hacktoberfest
|
||||||
|
|
||||||
/project_euler/ @dhruvmanila @Kush1101
|
/project_euler/ @dhruvmanila
|
||||||
|
|
||||||
# /quantum/
|
# /quantum/
|
||||||
|
|
||||||
@ -79,7 +79,7 @@
|
|||||||
|
|
||||||
# /searches/
|
# /searches/
|
||||||
|
|
||||||
/sorts/ @mateuszz0000
|
# /sorts/
|
||||||
|
|
||||||
# /strings/ @cclauss # TODO: Uncomment this line after Hacktoberfest
|
# /strings/ @cclauss # TODO: Uncomment this line after Hacktoberfest
|
||||||
|
|
||||||
|
54
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
Normal file
54
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
name: Bug report
|
||||||
|
description: Create a bug report to help us address errors in the repository
|
||||||
|
labels: [bug]
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: >
|
||||||
|
Before requesting please search [existing issues](https://github.com/TheAlgorithms/Python/labels/bug).
|
||||||
|
Usage questions such as "How do I...?" belong on the
|
||||||
|
[Discord](https://discord.gg/c7MnfGFGa6) and will be closed.
|
||||||
|
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: "Repository commit"
|
||||||
|
description: >
|
||||||
|
The commit hash for `TheAlgorithms/Python` repository. You can get this
|
||||||
|
by running the command `git rev-parse HEAD` locally.
|
||||||
|
placeholder: "a0b0f414ae134aa1772d33bb930e5a960f9979e8"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: "Python version (python --version)"
|
||||||
|
placeholder: "Python 3.10.7"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: "Dependencies version (pip freeze)"
|
||||||
|
description: >
|
||||||
|
This is the output of the command `pip freeze --all`. Note that the
|
||||||
|
actual output might be different as compared to the placeholder text.
|
||||||
|
placeholder: |
|
||||||
|
appnope==0.1.3
|
||||||
|
asttokens==2.0.8
|
||||||
|
backcall==0.2.0
|
||||||
|
...
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: "Expected behavior"
|
||||||
|
description: "Describe the behavior you expect. May include images or videos."
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: "Actual behavior"
|
||||||
|
validations:
|
||||||
|
required: true
|
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: Discord community
|
||||||
|
url: https://discord.gg/c7MnfGFGa6
|
||||||
|
about: Have any questions or need any help? Please contact us via Discord
|
19
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
Normal file
19
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
name: Feature request
|
||||||
|
description: Suggest features, propose improvements, discuss new ideas.
|
||||||
|
labels: [enhancement]
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: >
|
||||||
|
Before requesting please search [existing issues](https://github.com/TheAlgorithms/Python/labels/enhancement).
|
||||||
|
Usage questions such as "How do I...?" belong on the
|
||||||
|
[Discord](https://discord.gg/c7MnfGFGa6) and will be closed.
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: "Feature description"
|
||||||
|
description: >
|
||||||
|
This could be new algorithms, data structures or improving any existing
|
||||||
|
implementations.
|
||||||
|
validations:
|
||||||
|
required: true
|
19
.github/ISSUE_TEMPLATE/other.yml
vendored
Normal file
19
.github/ISSUE_TEMPLATE/other.yml
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
name: Other
|
||||||
|
description: Use this for any other issues. PLEASE do not create blank issues
|
||||||
|
labels: ["awaiting triage"]
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
id: issuedescription
|
||||||
|
attributes:
|
||||||
|
label: What would you like to share?
|
||||||
|
description: Provide a clear and concise explanation of your issue.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
id: extrainfo
|
||||||
|
attributes:
|
||||||
|
label: Additional information
|
||||||
|
description: Is there anything else we should know about this issue?
|
||||||
|
validations:
|
||||||
|
required: false
|
4
.github/pull_request_template.md
vendored
4
.github/pull_request_template.md
vendored
@ -16,5 +16,5 @@
|
|||||||
* [ ] All functions and variable names follow Python naming conventions.
|
* [ ] 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 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 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.
|
* [ ] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
|
||||||
* [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
|
* [ ] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a [closing keyword](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue): "Fixes #ISSUE-NUMBER".
|
||||||
|
4
.github/stale.yml
vendored
4
.github/stale.yml
vendored
@ -45,7 +45,7 @@ pulls:
|
|||||||
closeComment: >
|
closeComment: >
|
||||||
Please reopen this pull request once you commit the changes requested
|
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
|
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)
|
some help, feel free to seek help from our [Gitter](https://gitter.im/TheAlgorithms/community)
|
||||||
or ping one of the reviewers. Thank you for your contributions!
|
or ping one of the reviewers. Thank you for your contributions!
|
||||||
|
|
||||||
issues:
|
issues:
|
||||||
@ -59,5 +59,5 @@ issues:
|
|||||||
closeComment: >
|
closeComment: >
|
||||||
Please reopen this issue once you add more information and updates here.
|
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
|
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
|
from our [Gitter](https://gitter.im/TheAlgorithms/community) or ping one of the
|
||||||
reviewers. Thank you for your contributions!
|
reviewers. Thank you for your contributions!
|
||||||
|
19
.github/workflows/build.yml
vendored
19
.github/workflows/build.yml
vendored
@ -9,20 +9,25 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-python@v2
|
- uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
python-version: "3.9"
|
python-version: 3.11
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/.cache/pip
|
path: ~/.cache/pip
|
||||||
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
|
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip setuptools six wheel
|
python -m pip install --upgrade pip setuptools six wheel
|
||||||
python -m pip install mypy pytest-cov -r requirements.txt
|
python -m pip install pytest-cov -r requirements.txt
|
||||||
- run: mypy . # See `mypy.ini` for configuration settings.
|
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/validate_solutions.py --cov-report=term-missing:skip-covered --cov=. .
|
# TODO: #8818 Re-enable quantum tests
|
||||||
|
run: pytest
|
||||||
|
--ignore=quantum/q_fourier_transform.py
|
||||||
|
--ignore=project_euler/
|
||||||
|
--ignore=scripts/validate_solutions.py
|
||||||
|
--cov-report=term-missing:skip-covered
|
||||||
|
--cov=. .
|
||||||
- if: ${{ success() }}
|
- if: ${{ success() }}
|
||||||
run: scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
|
run: scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
|
||||||
|
6
.github/workflows/directory_writer.yml
vendored
6
.github/workflows/directory_writer.yml
vendored
@ -6,8 +6,10 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1 # v1, NOT v2
|
- uses: actions/checkout@v1 # v1, NOT v2 or v3
|
||||||
- uses: actions/setup-python@v2
|
- uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 3.x
|
||||||
- name: Write DIRECTORY.md
|
- name: Write DIRECTORY.md
|
||||||
run: |
|
run: |
|
||||||
scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
|
scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
|
||||||
|
22
.github/workflows/pre-commit.yml
vendored
22
.github/workflows/pre-commit.yml
vendored
@ -1,22 +0,0 @@
|
|||||||
name: pre-commit
|
|
||||||
|
|
||||||
on: [push, pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
pre-commit:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- uses: actions/cache@v2
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.cache/pre-commit
|
|
||||||
~/.cache/pip
|
|
||||||
key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
|
|
||||||
- uses: actions/setup-python@v2
|
|
||||||
- uses: psf/black@21.4b0
|
|
||||||
- name: Install pre-commit
|
|
||||||
run: |
|
|
||||||
python -m pip install --upgrade pip
|
|
||||||
python -m pip install --upgrade pre-commit
|
|
||||||
- run: pre-commit run --verbose --all-files --show-diff-on-failure
|
|
12
.github/workflows/project_euler.yml
vendored
12
.github/workflows/project_euler.yml
vendored
@ -14,8 +14,10 @@ jobs:
|
|||||||
project-euler:
|
project-euler:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-python@v2
|
- uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 3.x
|
||||||
- name: Install pytest and pytest-cov
|
- name: Install pytest and pytest-cov
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
@ -24,8 +26,10 @@ jobs:
|
|||||||
validate-solutions:
|
validate-solutions:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-python@v2
|
- uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: 3.x
|
||||||
- name: Install pytest and requests
|
- name: Install pytest and requests
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
|
16
.github/workflows/ruff.yml
vendored
Normal file
16
.github/workflows/ruff.yml
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# https://beta.ruff.rs
|
||||||
|
name: ruff
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
jobs:
|
||||||
|
ruff:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- run: pip install --user ruff
|
||||||
|
- run: ruff --format=github .
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -107,3 +107,4 @@ venv.bak/
|
|||||||
.idea
|
.idea
|
||||||
.try
|
.try
|
||||||
.vscode/
|
.vscode/
|
||||||
|
.vs/
|
||||||
|
@ -1,62 +1,42 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v3.4.0
|
rev: v4.4.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: check-executables-have-shebangs
|
- id: check-executables-have-shebangs
|
||||||
|
- id: check-toml
|
||||||
- id: check-yaml
|
- id: check-yaml
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
types: [python]
|
types: [python]
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
exclude: |
|
|
||||||
(?x)^(
|
|
||||||
data_structures/heap/binomial_heap.py
|
|
||||||
)$
|
|
||||||
- id: requirements-txt-fixer
|
- id: requirements-txt-fixer
|
||||||
|
|
||||||
|
- repo: https://github.com/MarcoGorelli/auto-walrus
|
||||||
|
rev: v0.2.2
|
||||||
|
hooks:
|
||||||
|
- id: auto-walrus
|
||||||
|
|
||||||
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
|
rev: v0.0.284
|
||||||
|
hooks:
|
||||||
|
- id: ruff
|
||||||
|
|
||||||
- repo: https://github.com/psf/black
|
- repo: https://github.com/psf/black
|
||||||
rev: 21.4b0
|
rev: 23.7.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: black
|
- id: black
|
||||||
- repo: https://github.com/PyCQA/isort
|
|
||||||
rev: 5.8.0
|
|
||||||
hooks:
|
|
||||||
- id: isort
|
|
||||||
args:
|
|
||||||
- --profile=black
|
|
||||||
- repo: https://github.com/asottile/pyupgrade
|
|
||||||
rev: v2.29.0
|
|
||||||
hooks:
|
|
||||||
- id: pyupgrade
|
|
||||||
args:
|
|
||||||
- --py39-plus
|
|
||||||
- repo: https://gitlab.com/pycqa/flake8
|
|
||||||
rev: 3.9.1
|
|
||||||
hooks:
|
|
||||||
- id: flake8
|
|
||||||
args:
|
|
||||||
- --ignore=E203,W503
|
|
||||||
- --max-complexity=25
|
|
||||||
- --max-line-length=88
|
|
||||||
# FIXME: fix mypy errors and then uncomment this
|
|
||||||
# - repo: https://github.com/pre-commit/mirrors-mypy
|
|
||||||
# rev: v0.782
|
|
||||||
# hooks:
|
|
||||||
# - id: mypy
|
|
||||||
# args:
|
|
||||||
# - --ignore-missing-imports
|
|
||||||
- repo: https://github.com/codespell-project/codespell
|
- repo: https://github.com/codespell-project/codespell
|
||||||
rev: v2.0.0
|
rev: v2.2.5
|
||||||
hooks:
|
hooks:
|
||||||
- id: codespell
|
- id: codespell
|
||||||
args:
|
additional_dependencies:
|
||||||
- --ignore-words-list=ans,crate,fo,followings,hist,iff,mater,secant,som,tim
|
- tomli
|
||||||
- --skip="./.*,./strings/dictionary.txt,./strings/words.txt,./project_euler/problem_022/p022_names.txt"
|
|
||||||
- --quiet-level=2
|
- repo: https://github.com/tox-dev/pyproject-fmt
|
||||||
exclude: |
|
rev: "0.13.1"
|
||||||
(?x)^(
|
hooks:
|
||||||
strings/dictionary.txt |
|
- id: pyproject-fmt
|
||||||
strings/words.txt |
|
|
||||||
project_euler/problem_022/p022_names.txt
|
|
||||||
)$
|
|
||||||
- repo: local
|
- repo: local
|
||||||
hooks:
|
hooks:
|
||||||
- id: validate-filenames
|
- id: validate-filenames
|
||||||
@ -64,3 +44,18 @@ repos:
|
|||||||
entry: ./scripts/validate_filenames.py
|
entry: ./scripts/validate_filenames.py
|
||||||
language: script
|
language: script
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
|
|
||||||
|
- repo: https://github.com/abravalheri/validate-pyproject
|
||||||
|
rev: v0.13
|
||||||
|
hooks:
|
||||||
|
- id: validate-pyproject
|
||||||
|
|
||||||
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
|
rev: v1.5.0
|
||||||
|
hooks:
|
||||||
|
- id: mypy
|
||||||
|
args:
|
||||||
|
- --ignore-missing-imports
|
||||||
|
- --install-types # See mirrors-mypy README.md
|
||||||
|
- --non-interactive
|
||||||
|
additional_dependencies: [types-requests]
|
||||||
|
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"githubPullRequests.ignoredPullRequestBranches": [
|
||||||
|
"master"
|
||||||
|
]
|
||||||
|
}
|
@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
## Before contributing
|
## 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).
|
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/community).
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
### Contributor
|
### Contributor
|
||||||
|
|
||||||
We are very happy that you consider implementing algorithms and data structures 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:
|
We are very happy that you are considering implementing algorithms and data structures 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
|
- You did your work - no plagiarism allowed
|
||||||
- Any plagiarized work will not be merged.
|
- Any plagiarized work will not be merged.
|
||||||
@ -25,7 +25,14 @@ We appreciate any contribution, from fixing a grammar mistake in a comment to im
|
|||||||
|
|
||||||
Your contribution will be tested by our [automated testing on GitHub Actions](https://github.com/TheAlgorithms/Python/actions) to save time and mental energy. After you have submitted your pull request, you should see the GitHub Actions 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 GitHub Actions 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.
|
Your contribution will be tested by our [automated testing on GitHub Actions](https://github.com/TheAlgorithms/Python/actions) to save time and mental energy. After you have submitted your pull request, you should see the GitHub Actions 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 GitHub Actions 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.
|
If you are interested in resolving an [open issue](https://github.com/TheAlgorithms/Python/issues), simply make a pull request with your proposed fix. __We do not assign issues in this repo__ so please do not ask for permission to work on an issue.
|
||||||
|
|
||||||
|
Please help us keep our issue list small by adding `Fixes #{$ISSUE_NUMBER}` to the description of pull requests that resolve open issues.
|
||||||
|
For example, if your pull request fixes issue #10, then please add the following to its description:
|
||||||
|
```
|
||||||
|
Fixes #10
|
||||||
|
```
|
||||||
|
GitHub will use this tag to [auto-close the issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) if and when the PR is merged.
|
||||||
|
|
||||||
#### What is an Algorithm?
|
#### What is an Algorithm?
|
||||||
|
|
||||||
@ -66,7 +73,7 @@ pre-commit run --all-files --show-diff-on-failure
|
|||||||
|
|
||||||
We want your work to be readable by others; therefore, we encourage you to note the following:
|
We want your work to be readable by others; therefore, we encourage you to note the following:
|
||||||
|
|
||||||
- Please write in Python 3.9+. For instance: `print()` is a function in Python 3 so `print "Hello"` will *not* work but `print("Hello")` will.
|
- Please write in Python 3.11+. For instance: `print()` is a function in Python 3 so `print "Hello"` will *not* work but `print("Hello")` will.
|
||||||
- Please focus hard on the naming of functions, classes, and variables. Help your reader by using __descriptive names__ that can help you to remove redundant comments.
|
- Please focus hard on the 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.
|
- 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.
|
- Expand acronyms because `gcd()` is hard to understand but `greatest_common_divisor()` is not.
|
||||||
@ -81,11 +88,11 @@ We want your work to be readable by others; therefore, we encourage you to note
|
|||||||
black .
|
black .
|
||||||
```
|
```
|
||||||
|
|
||||||
- All submissions will need to pass the test `flake8 . --ignore=E203,W503 --max-line-length=88` before they will be accepted so if possible, try this test locally on your Python file(s) before submitting your pull request.
|
- All submissions will need to pass the test `ruff .` before they will be accepted so if possible, try this test locally on your Python file(s) before submitting your pull request.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python3 -m pip install flake8 # only required the first time
|
python3 -m pip install ruff # only required the first time
|
||||||
flake8 . --ignore=E203,W503 --max-line-length=88 --show-source
|
ruff .
|
||||||
```
|
```
|
||||||
|
|
||||||
- Original code submission require docstrings or comments to describe your work.
|
- Original code submission require docstrings or comments to describe your work.
|
||||||
@ -176,7 +183,7 @@ We want your work to be readable by others; therefore, we encourage you to note
|
|||||||
|
|
||||||
- Most importantly,
|
- Most importantly,
|
||||||
- __Be consistent in the use of these guidelines when submitting.__
|
- __Be consistent in the use of these guidelines when submitting.__
|
||||||
- __Join__ [Gitter](https://gitter.im/TheAlgorithms) __now!__
|
- __Join__ us on [Discord](https://discord.com/invite/c7MnfGFGa6) and [Gitter](https://gitter.im/TheAlgorithms/community) __now!__
|
||||||
- Happy coding!
|
- Happy coding!
|
||||||
|
|
||||||
Writer [@poyea](https://github.com/poyea), Jun 2019.
|
Writer [@poyea](https://github.com/poyea), Jun 2019.
|
||||||
|
1826
DIRECTORY.md
1826
DIRECTORY.md
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2016-2021 The Algorithms
|
Copyright (c) 2016-2022 TheAlgorithms and contributors
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
18
README.md
18
README.md
@ -12,23 +12,17 @@
|
|||||||
<a href="https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md">
|
<a href="https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md">
|
||||||
<img src="https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square" height="20" alt="Contributions Welcome">
|
<img src="https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square" height="20" alt="Contributions Welcome">
|
||||||
</a>
|
</a>
|
||||||
<a href="https://www.paypal.me/TheAlgorithms/100">
|
|
||||||
<img src="https://img.shields.io/badge/Donate-PayPal-green.svg?logo=paypal&style=flat-square" height="20" alt="Donate">
|
|
||||||
</a>
|
|
||||||
<img src="https://img.shields.io/github/repo-size/TheAlgorithms/Python.svg?label=Repo%20size&style=flat-square" height="20">
|
<img src="https://img.shields.io/github/repo-size/TheAlgorithms/Python.svg?label=Repo%20size&style=flat-square" height="20">
|
||||||
<a href="https://discord.gg/c7MnfGFGa6">
|
<a href="https://the-algorithms.com/discord">
|
||||||
<img src="https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA&style=flat-square" height="20" alt="Discord chat">
|
<img src="https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA&style=flat-square" height="20" alt="Discord chat">
|
||||||
</a>
|
</a>
|
||||||
<a href="https://gitter.im/TheAlgorithms">
|
<a href="https://gitter.im/TheAlgorithms/community">
|
||||||
<img src="https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square" height="20" alt="Gitter chat">
|
<img src="https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square" height="20" alt="Gitter chat">
|
||||||
</a>
|
</a>
|
||||||
<!-- Second row: -->
|
<!-- Second row: -->
|
||||||
<br>
|
<br>
|
||||||
<a href="https://github.com/TheAlgorithms/Python/actions">
|
<a href="https://github.com/TheAlgorithms/Python/actions">
|
||||||
<img src="https://img.shields.io/github/workflow/status/TheAlgorithms/Python/build?label=CI&logo=github&style=flat-square" height="20" alt="GitHub Workflow Status">
|
<img src="https://img.shields.io/github/actions/workflow/status/TheAlgorithms/Python/build.yml?branch=master&label=CI&logo=github&style=flat-square" height="20" alt="GitHub Workflow Status">
|
||||||
</a>
|
|
||||||
<a href="https://lgtm.com/projects/g/TheAlgorithms/Python/alerts">
|
|
||||||
<img src="https://img.shields.io/lgtm/alerts/github/TheAlgorithms/Python.svg?label=LGTM&logo=LGTM&style=flat-square" height="20" alt="LGTM">
|
|
||||||
</a>
|
</a>
|
||||||
<a href="https://github.com/pre-commit/pre-commit">
|
<a href="https://github.com/pre-commit/pre-commit">
|
||||||
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" height="20" alt="pre-commit">
|
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" height="20" alt="pre-commit">
|
||||||
@ -40,7 +34,7 @@
|
|||||||
<h3>All algorithms implemented in Python - for education</h3>
|
<h3>All algorithms implemented in Python - for education</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
Implementations are for learning purposes only. As they may be less efficient than the implementations in the Python standard library, use them at your discretion.
|
Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library. Use them at your discretion.
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
@ -48,8 +42,8 @@ Read through our [Contribution Guidelines](CONTRIBUTING.md) before you contribut
|
|||||||
|
|
||||||
## Community Channels
|
## Community Channels
|
||||||
|
|
||||||
We're on [Discord](https://discord.gg/c7MnfGFGa6) and [Gitter](https://gitter.im/TheAlgorithms)! Community channels are great for you to ask questions and get help. Please join us!
|
We are on [Discord](https://the-algorithms.com/discord) and [Gitter](https://gitter.im/TheAlgorithms/community)! Community channels are a great way for you to ask questions and get help. Please join us!
|
||||||
|
|
||||||
## List of Algorithms
|
## List of Algorithms
|
||||||
|
|
||||||
See our [directory](DIRECTORY.md) for easier navigation and better overview of the project.
|
See our [directory](DIRECTORY.md) for easier navigation and a better overview of the project.
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from typing import Callable
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
|
||||||
def bisection(function: Callable[[float], float], a: float, b: float) -> float:
|
def bisection(function: Callable[[float], float], a: float, b: float) -> float:
|
||||||
@ -8,7 +8,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float:
|
|||||||
1.0000000149011612
|
1.0000000149011612
|
||||||
>>> bisection(lambda x: x ** 3 - 1, 2, 1000)
|
>>> bisection(lambda x: x ** 3 - 1, 2, 1000)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: could not find root in given interval.
|
ValueError: could not find root in given interval.
|
||||||
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 0, 2)
|
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 0, 2)
|
||||||
1.0
|
1.0
|
||||||
@ -16,7 +16,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float:
|
|||||||
3.0
|
3.0
|
||||||
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)
|
>>> bisection(lambda x: x ** 2 - 4 * x + 3, 4, 1000)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: could not find root in given interval.
|
ValueError: could not find root in given interval.
|
||||||
"""
|
"""
|
||||||
start: float = a
|
start: float = a
|
||||||
@ -32,7 +32,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float:
|
|||||||
raise ValueError("could not find root in given interval.")
|
raise ValueError("could not find root in given interval.")
|
||||||
else:
|
else:
|
||||||
mid: float = start + (end - start) / 2.0
|
mid: float = start + (end - start) / 2.0
|
||||||
while abs(start - mid) > 10 ** -7: # until precisely equals to 10^-7
|
while abs(start - mid) > 10**-7: # until precisely equals to 10^-7
|
||||||
if function(mid) == 0:
|
if function(mid) == 0:
|
||||||
return mid
|
return mid
|
||||||
elif function(mid) * function(start) < 0:
|
elif function(mid) * function(start) < 0:
|
||||||
@ -44,7 +44,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float:
|
|||||||
|
|
||||||
|
|
||||||
def f(x: float) -> float:
|
def f(x: float) -> float:
|
||||||
return x ** 3 - 2 * x - 5
|
return x**3 - 2 * x - 5
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -5,9 +5,13 @@ Gaussian elimination - https://en.wikipedia.org/wiki/Gaussian_elimination
|
|||||||
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from numpy import float64
|
||||||
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
|
|
||||||
def retroactive_resolution(coefficients: np.matrix, vector: np.ndarray) -> np.ndarray:
|
def retroactive_resolution(
|
||||||
|
coefficients: NDArray[float64], vector: NDArray[float64]
|
||||||
|
) -> NDArray[float64]:
|
||||||
"""
|
"""
|
||||||
This function performs a retroactive linear system resolution
|
This function performs a retroactive linear system resolution
|
||||||
for triangular matrix
|
for triangular matrix
|
||||||
@ -27,18 +31,20 @@ def retroactive_resolution(coefficients: np.matrix, vector: np.ndarray) -> np.nd
|
|||||||
|
|
||||||
rows, columns = np.shape(coefficients)
|
rows, columns = np.shape(coefficients)
|
||||||
|
|
||||||
x = np.zeros((rows, 1), dtype=float)
|
x: NDArray[float64] = np.zeros((rows, 1), dtype=float)
|
||||||
for row in reversed(range(rows)):
|
for row in reversed(range(rows)):
|
||||||
sum = 0
|
total = 0
|
||||||
for col in range(row + 1, columns):
|
for col in range(row + 1, columns):
|
||||||
sum += coefficients[row, col] * x[col]
|
total += coefficients[row, col] * x[col]
|
||||||
|
|
||||||
x[row, 0] = (vector[row] - sum) / coefficients[row, row]
|
x[row, 0] = (vector[row] - total) / coefficients[row, row]
|
||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
def gaussian_elimination(coefficients: np.matrix, vector: np.ndarray) -> np.ndarray:
|
def gaussian_elimination(
|
||||||
|
coefficients: NDArray[float64], vector: NDArray[float64]
|
||||||
|
) -> NDArray[float64]:
|
||||||
"""
|
"""
|
||||||
This function performs Gaussian elimination method
|
This function performs Gaussian elimination method
|
||||||
|
|
||||||
@ -60,7 +66,7 @@ def gaussian_elimination(coefficients: np.matrix, vector: np.ndarray) -> np.ndar
|
|||||||
return np.array((), dtype=float)
|
return np.array((), dtype=float)
|
||||||
|
|
||||||
# augmented matrix
|
# augmented matrix
|
||||||
augmented_mat = np.concatenate((coefficients, vector), axis=1)
|
augmented_mat: NDArray[float64] = np.concatenate((coefficients, vector), axis=1)
|
||||||
augmented_mat = augmented_mat.astype("float64")
|
augmented_mat = augmented_mat.astype("float64")
|
||||||
|
|
||||||
# scale the matrix leaving it triangular
|
# scale the matrix leaving it triangular
|
||||||
|
@ -3,7 +3,8 @@ Checks if a system of forces is in static equilibrium.
|
|||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from numpy import array, cos, cross, ndarray, radians, sin
|
from numpy import array, cos, cross, float64, radians, sin
|
||||||
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
|
|
||||||
def polar_force(
|
def polar_force(
|
||||||
@ -12,10 +13,17 @@ def polar_force(
|
|||||||
"""
|
"""
|
||||||
Resolves force along rectangular components.
|
Resolves force along rectangular components.
|
||||||
(force, angle) => (force_x, force_y)
|
(force, angle) => (force_x, force_y)
|
||||||
>>> polar_force(10, 45)
|
>>> import math
|
||||||
[7.0710678118654755, 7.071067811865475]
|
>>> force = polar_force(10, 45)
|
||||||
>>> polar_force(10, 3.14, radian_mode=True)
|
>>> math.isclose(force[0], 7.071067811865477)
|
||||||
[-9.999987317275394, 0.01592652916486828]
|
True
|
||||||
|
>>> math.isclose(force[1], 7.0710678118654755)
|
||||||
|
True
|
||||||
|
>>> force = polar_force(10, 3.14, radian_mode=True)
|
||||||
|
>>> math.isclose(force[0], -9.999987317275396)
|
||||||
|
True
|
||||||
|
>>> math.isclose(force[1], 0.01592652916486828)
|
||||||
|
True
|
||||||
"""
|
"""
|
||||||
if radian_mode:
|
if radian_mode:
|
||||||
return [magnitude * cos(angle), magnitude * sin(angle)]
|
return [magnitude * cos(angle), magnitude * sin(angle)]
|
||||||
@ -23,7 +31,7 @@ def polar_force(
|
|||||||
|
|
||||||
|
|
||||||
def in_static_equilibrium(
|
def in_static_equilibrium(
|
||||||
forces: ndarray, location: ndarray, eps: float = 10 ** -1
|
forces: NDArray[float64], location: NDArray[float64], eps: float = 10**-1
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
Check if a system is in equilibrium.
|
Check if a system is in equilibrium.
|
||||||
@ -42,7 +50,7 @@ def in_static_equilibrium(
|
|||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
# summation of moments is zero
|
# summation of moments is zero
|
||||||
moments: ndarray = cross(location, forces)
|
moments: NDArray[float64] = cross(location, forces)
|
||||||
sum_moments: float = sum(moments)
|
sum_moments: float = sum(moments)
|
||||||
return abs(sum_moments) < eps
|
return abs(sum_moments) < eps
|
||||||
|
|
||||||
@ -50,10 +58,14 @@ def in_static_equilibrium(
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Test to check if it works
|
# Test to check if it works
|
||||||
forces = array(
|
forces = array(
|
||||||
[polar_force(718.4, 180 - 30), polar_force(879.54, 45), polar_force(100, -90)]
|
[
|
||||||
|
polar_force(718.4, 180 - 30),
|
||||||
|
polar_force(879.54, 45),
|
||||||
|
polar_force(100, -90),
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
location = array([[0, 0], [0, 0], [0, 0]])
|
location: NDArray[float64] = array([[0, 0], [0, 0], [0, 0]])
|
||||||
|
|
||||||
assert in_static_equilibrium(forces, location)
|
assert in_static_equilibrium(forces, location)
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import math
|
import math
|
||||||
from typing import Callable
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
|
||||||
def intersection(function: Callable[[float], float], x0: float, x1: float) -> float:
|
def intersection(function: Callable[[float], float], x0: float, x1: float) -> float:
|
||||||
@ -10,7 +10,7 @@ def intersection(function: Callable[[float], float], x0: float, x1: float) -> fl
|
|||||||
0.9999999999954654
|
0.9999999999954654
|
||||||
>>> intersection(lambda x: x ** 3 - 1, 5, 5)
|
>>> intersection(lambda x: x ** 3 - 1, 5, 5)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ZeroDivisionError: float division by zero, could not find root
|
ZeroDivisionError: float division by zero, could not find root
|
||||||
>>> intersection(lambda x: x ** 3 - 1, 100, 200)
|
>>> intersection(lambda x: x ** 3 - 1, 100, 200)
|
||||||
1.0000000000003888
|
1.0000000000003888
|
||||||
@ -24,7 +24,7 @@ def intersection(function: Callable[[float], float], x0: float, x1: float) -> fl
|
|||||||
0.0
|
0.0
|
||||||
>>> intersection(math.cos, -math.pi, math.pi)
|
>>> intersection(math.cos, -math.pi, math.pi)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ZeroDivisionError: float division by zero, could not find root
|
ZeroDivisionError: float division by zero, could not find root
|
||||||
"""
|
"""
|
||||||
x_n: float = x0
|
x_n: float = x0
|
||||||
@ -35,7 +35,7 @@ def intersection(function: Callable[[float], float], x0: float, x1: float) -> fl
|
|||||||
x_n2: float = x_n1 - (
|
x_n2: float = x_n1 - (
|
||||||
function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n))
|
function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n))
|
||||||
)
|
)
|
||||||
if abs(x_n2 - x_n1) < 10 ** -5:
|
if abs(x_n2 - x_n1) < 10**-5:
|
||||||
return x_n2
|
return x_n2
|
||||||
x_n = x_n1
|
x_n = x_n1
|
||||||
x_n1 = x_n2
|
x_n1 = x_n2
|
||||||
|
173
arithmetic_analysis/jacobi_iteration_method.py
Normal file
173
arithmetic_analysis/jacobi_iteration_method.py
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
"""
|
||||||
|
Jacobi Iteration Method - https://en.wikipedia.org/wiki/Jacobi_method
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from numpy import float64
|
||||||
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
|
|
||||||
|
# Method to find solution of system of linear equations
|
||||||
|
def jacobi_iteration_method(
|
||||||
|
coefficient_matrix: NDArray[float64],
|
||||||
|
constant_matrix: NDArray[float64],
|
||||||
|
init_val: list[int],
|
||||||
|
iterations: int,
|
||||||
|
) -> list[float]:
|
||||||
|
"""
|
||||||
|
Jacobi Iteration Method:
|
||||||
|
An iterative algorithm to determine the solutions of strictly diagonally dominant
|
||||||
|
system of linear equations
|
||||||
|
|
||||||
|
4x1 + x2 + x3 = 2
|
||||||
|
x1 + 5x2 + 2x3 = -6
|
||||||
|
x1 + 2x2 + 4x3 = -4
|
||||||
|
|
||||||
|
x_init = [0.5, -0.5 , -0.5]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
|
||||||
|
>>> constant = np.array([[2], [-6], [-4]])
|
||||||
|
>>> init_val = [0.5, -0.5, -0.5]
|
||||||
|
>>> iterations = 3
|
||||||
|
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations)
|
||||||
|
[0.909375, -1.14375, -0.7484375]
|
||||||
|
|
||||||
|
|
||||||
|
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2]])
|
||||||
|
>>> constant = np.array([[2], [-6], [-4]])
|
||||||
|
>>> init_val = [0.5, -0.5, -0.5]
|
||||||
|
>>> iterations = 3
|
||||||
|
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Coefficient matrix dimensions must be nxn but received 2x3
|
||||||
|
|
||||||
|
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
|
||||||
|
>>> constant = np.array([[2], [-6]])
|
||||||
|
>>> init_val = [0.5, -0.5, -0.5]
|
||||||
|
>>> iterations = 3
|
||||||
|
>>> jacobi_iteration_method(
|
||||||
|
... coefficient, constant, init_val, iterations
|
||||||
|
... ) # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Coefficient and constant matrices dimensions must be nxn and nx1 but
|
||||||
|
received 3x3 and 2x1
|
||||||
|
|
||||||
|
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
|
||||||
|
>>> constant = np.array([[2], [-6], [-4]])
|
||||||
|
>>> init_val = [0.5, -0.5]
|
||||||
|
>>> iterations = 3
|
||||||
|
>>> jacobi_iteration_method(
|
||||||
|
... coefficient, constant, init_val, iterations
|
||||||
|
... ) # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Number of initial values must be equal to number of rows in coefficient
|
||||||
|
matrix but received 2 and 3
|
||||||
|
|
||||||
|
>>> coefficient = np.array([[4, 1, 1], [1, 5, 2], [1, 2, 4]])
|
||||||
|
>>> constant = np.array([[2], [-6], [-4]])
|
||||||
|
>>> init_val = [0.5, -0.5, -0.5]
|
||||||
|
>>> iterations = 0
|
||||||
|
>>> jacobi_iteration_method(coefficient, constant, init_val, iterations)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Iterations must be at least 1
|
||||||
|
"""
|
||||||
|
|
||||||
|
rows1, cols1 = coefficient_matrix.shape
|
||||||
|
rows2, cols2 = constant_matrix.shape
|
||||||
|
|
||||||
|
if rows1 != cols1:
|
||||||
|
msg = f"Coefficient matrix dimensions must be nxn but received {rows1}x{cols1}"
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
if cols2 != 1:
|
||||||
|
msg = f"Constant matrix must be nx1 but received {rows2}x{cols2}"
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
if rows1 != rows2:
|
||||||
|
msg = (
|
||||||
|
"Coefficient and constant matrices dimensions must be nxn and nx1 but "
|
||||||
|
f"received {rows1}x{cols1} and {rows2}x{cols2}"
|
||||||
|
)
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
if len(init_val) != rows1:
|
||||||
|
msg = (
|
||||||
|
"Number of initial values must be equal to number of rows in coefficient "
|
||||||
|
f"matrix but received {len(init_val)} and {rows1}"
|
||||||
|
)
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
if iterations <= 0:
|
||||||
|
raise ValueError("Iterations must be at least 1")
|
||||||
|
|
||||||
|
table: NDArray[float64] = np.concatenate(
|
||||||
|
(coefficient_matrix, constant_matrix), axis=1
|
||||||
|
)
|
||||||
|
|
||||||
|
rows, cols = table.shape
|
||||||
|
|
||||||
|
strictly_diagonally_dominant(table)
|
||||||
|
|
||||||
|
# Iterates the whole matrix for given number of times
|
||||||
|
for _ in range(iterations):
|
||||||
|
new_val = []
|
||||||
|
for row in range(rows):
|
||||||
|
temp = 0
|
||||||
|
for col in range(cols):
|
||||||
|
if col == row:
|
||||||
|
denom = table[row][col]
|
||||||
|
elif col == cols - 1:
|
||||||
|
val = table[row][col]
|
||||||
|
else:
|
||||||
|
temp += (-1) * table[row][col] * init_val[col]
|
||||||
|
temp = (temp + val) / denom
|
||||||
|
new_val.append(temp)
|
||||||
|
init_val = new_val
|
||||||
|
|
||||||
|
return [float(i) for i in new_val]
|
||||||
|
|
||||||
|
|
||||||
|
# Checks if the given matrix is strictly diagonally dominant
|
||||||
|
def strictly_diagonally_dominant(table: NDArray[float64]) -> bool:
|
||||||
|
"""
|
||||||
|
>>> table = np.array([[4, 1, 1, 2], [1, 5, 2, -6], [1, 2, 4, -4]])
|
||||||
|
>>> strictly_diagonally_dominant(table)
|
||||||
|
True
|
||||||
|
|
||||||
|
>>> table = np.array([[4, 1, 1, 2], [1, 5, 2, -6], [1, 2, 3, -4]])
|
||||||
|
>>> strictly_diagonally_dominant(table)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Coefficient matrix is not strictly diagonally dominant
|
||||||
|
"""
|
||||||
|
|
||||||
|
rows, cols = table.shape
|
||||||
|
|
||||||
|
is_diagonally_dominant = True
|
||||||
|
|
||||||
|
for i in range(0, rows):
|
||||||
|
total = 0
|
||||||
|
for j in range(0, cols - 1):
|
||||||
|
if i == j:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
total += table[i][j]
|
||||||
|
|
||||||
|
if table[i][i] <= total:
|
||||||
|
raise ValueError("Coefficient matrix is not strictly diagonally dominant")
|
||||||
|
|
||||||
|
return is_diagonally_dominant
|
||||||
|
|
||||||
|
|
||||||
|
# Test Cases
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
@ -1,7 +1,19 @@
|
|||||||
"""Lower-Upper (LU) Decomposition.
|
"""
|
||||||
|
Lower–upper (LU) decomposition factors a matrix as a product of a lower
|
||||||
|
triangular matrix and an upper triangular matrix. A square matrix has an LU
|
||||||
|
decomposition under the following conditions:
|
||||||
|
- If the matrix is invertible, then it has an LU decomposition if and only
|
||||||
|
if all of its leading principal minors are non-zero (see
|
||||||
|
https://en.wikipedia.org/wiki/Minor_(linear_algebra) for an explanation of
|
||||||
|
leading principal minors of a matrix).
|
||||||
|
- If the matrix is singular (i.e., not invertible) and it has a rank of k
|
||||||
|
(i.e., it has k linearly independent columns), then it has an LU
|
||||||
|
decomposition if its first k leading principal minors are non-zero.
|
||||||
|
|
||||||
Reference:
|
This algorithm will simply attempt to perform LU decomposition on any square
|
||||||
- https://en.wikipedia.org/wiki/LU_decomposition
|
matrix and raise an error if no such decomposition exists.
|
||||||
|
|
||||||
|
Reference: https://en.wikipedia.org/wiki/LU_decomposition
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@ -9,50 +21,82 @@ import numpy as np
|
|||||||
|
|
||||||
|
|
||||||
def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
||||||
"""Lower-Upper (LU) Decomposition
|
"""
|
||||||
|
Perform LU decomposition on a given matrix and raises an error if the matrix
|
||||||
Example:
|
isn't square or if no such decomposition exists
|
||||||
|
|
||||||
>>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
|
>>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
|
||||||
>>> outcome = lower_upper_decomposition(matrix)
|
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||||
>>> outcome[0]
|
>>> lower_mat
|
||||||
array([[1. , 0. , 0. ],
|
array([[1. , 0. , 0. ],
|
||||||
[0. , 1. , 0. ],
|
[0. , 1. , 0. ],
|
||||||
[2.5, 8. , 1. ]])
|
[2.5, 8. , 1. ]])
|
||||||
>>> outcome[1]
|
>>> upper_mat
|
||||||
array([[ 2. , -2. , 1. ],
|
array([[ 2. , -2. , 1. ],
|
||||||
[ 0. , 1. , 2. ],
|
[ 0. , 1. , 2. ],
|
||||||
[ 0. , 0. , -17.5]])
|
[ 0. , 0. , -17.5]])
|
||||||
|
|
||||||
|
>>> matrix = np.array([[4, 3], [6, 3]])
|
||||||
|
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||||
|
>>> lower_mat
|
||||||
|
array([[1. , 0. ],
|
||||||
|
[1.5, 1. ]])
|
||||||
|
>>> upper_mat
|
||||||
|
array([[ 4. , 3. ],
|
||||||
|
[ 0. , -1.5]])
|
||||||
|
|
||||||
|
# Matrix is not square
|
||||||
>>> matrix = np.array([[2, -2, 1], [0, 1, 2]])
|
>>> matrix = np.array([[2, -2, 1], [0, 1, 2]])
|
||||||
>>> lower_upper_decomposition(matrix)
|
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: 'table' has to be of square shaped array but got a 2x3 array:
|
ValueError: 'table' has to be of square shaped array but got a 2x3 array:
|
||||||
[[ 2 -2 1]
|
[[ 2 -2 1]
|
||||||
[ 0 1 2]]
|
[ 0 1 2]]
|
||||||
|
|
||||||
|
# Matrix is invertible, but its first leading principal minor is 0
|
||||||
|
>>> matrix = np.array([[0, 1], [1, 0]])
|
||||||
|
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ArithmeticError: No LU decomposition exists
|
||||||
|
|
||||||
|
# Matrix is singular, but its first leading principal minor is 1
|
||||||
|
>>> matrix = np.array([[1, 0], [1, 0]])
|
||||||
|
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||||
|
>>> lower_mat
|
||||||
|
array([[1., 0.],
|
||||||
|
[1., 1.]])
|
||||||
|
>>> upper_mat
|
||||||
|
array([[1., 0.],
|
||||||
|
[0., 0.]])
|
||||||
|
|
||||||
|
# Matrix is singular, but its first leading principal minor is 0
|
||||||
|
>>> matrix = np.array([[0, 1], [0, 1]])
|
||||||
|
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ArithmeticError: No LU decomposition exists
|
||||||
"""
|
"""
|
||||||
# Table that contains our data
|
# Ensure that table is a square array
|
||||||
# Table has to be a square array so we need to check first
|
|
||||||
rows, columns = np.shape(table)
|
rows, columns = np.shape(table)
|
||||||
if rows != columns:
|
if rows != columns:
|
||||||
raise ValueError(
|
msg = (
|
||||||
f"'table' has to be of square shaped array but got a {rows}x{columns} "
|
"'table' has to be of square shaped array but got a "
|
||||||
+ f"array:\n{table}"
|
f"{rows}x{columns} array:\n{table}"
|
||||||
)
|
)
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
lower = np.zeros((rows, columns))
|
lower = np.zeros((rows, columns))
|
||||||
upper = np.zeros((rows, columns))
|
upper = np.zeros((rows, columns))
|
||||||
for i in range(columns):
|
for i in range(columns):
|
||||||
for j in range(i):
|
for j in range(i):
|
||||||
total = 0
|
total = sum(lower[i][k] * upper[k][j] for k in range(j))
|
||||||
for k in range(j):
|
if upper[j][j] == 0:
|
||||||
total += lower[i][k] * upper[k][j]
|
raise ArithmeticError("No LU decomposition exists")
|
||||||
lower[i][j] = (table[i][j] - total) / upper[j][j]
|
lower[i][j] = (table[i][j] - total) / upper[j][j]
|
||||||
lower[i][i] = 1
|
lower[i][i] = 1
|
||||||
for j in range(i, columns):
|
for j in range(i, columns):
|
||||||
total = 0
|
total = sum(lower[i][k] * upper[k][j] for k in range(j))
|
||||||
for k in range(i):
|
|
||||||
total += lower[i][k] * upper[k][j]
|
|
||||||
upper[i][j] = table[i][j] - total
|
upper[i][j] = table[i][j] - total
|
||||||
return lower, upper
|
return lower, upper
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ def ucal(u: float, p: int) -> float:
|
|||||||
def main() -> None:
|
def main() -> None:
|
||||||
n = int(input("enter the numbers of values: "))
|
n = int(input("enter the numbers of values: "))
|
||||||
y: list[list[float]] = []
|
y: list[list[float]] = []
|
||||||
for i in range(n):
|
for _ in range(n):
|
||||||
y.append([])
|
y.append([])
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
for j in range(n):
|
for j in range(n):
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"""Newton's Method."""
|
"""Newton's Method."""
|
||||||
|
|
||||||
# Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method
|
# Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method
|
||||||
from typing import Callable
|
from collections.abc import Callable
|
||||||
|
|
||||||
RealFunc = Callable[[float], float] # type alias for a real -> real function
|
RealFunc = Callable[[float], float] # type alias for a real -> real function
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ def newton(
|
|||||||
1.5707963267948966
|
1.5707963267948966
|
||||||
>>> newton(math.cos, lambda x: -math.sin(x), 0)
|
>>> newton(math.cos, lambda x: -math.sin(x), 0)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ZeroDivisionError: Could not find root
|
ZeroDivisionError: Could not find root
|
||||||
"""
|
"""
|
||||||
prev_guess = float(starting_int)
|
prev_guess = float(starting_int)
|
||||||
@ -37,17 +37,17 @@ def newton(
|
|||||||
next_guess = prev_guess - function(prev_guess) / derivative(prev_guess)
|
next_guess = prev_guess - function(prev_guess) / derivative(prev_guess)
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
raise ZeroDivisionError("Could not find root") from None
|
raise ZeroDivisionError("Could not find root") from None
|
||||||
if abs(prev_guess - next_guess) < 10 ** -5:
|
if abs(prev_guess - next_guess) < 10**-5:
|
||||||
return next_guess
|
return next_guess
|
||||||
prev_guess = next_guess
|
prev_guess = next_guess
|
||||||
|
|
||||||
|
|
||||||
def f(x: float) -> float:
|
def f(x: float) -> float:
|
||||||
return (x ** 3) - (2 * x) - 5
|
return (x**3) - (2 * x) - 5
|
||||||
|
|
||||||
|
|
||||||
def f1(x: float) -> float:
|
def f1(x: float) -> float:
|
||||||
return 3 * (x ** 2) - 2
|
return 3 * (x**2) - 2
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -5,13 +5,13 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from math import * # noqa: F401, F403
|
from math import * # noqa: F403
|
||||||
|
|
||||||
from sympy import diff
|
from sympy import diff
|
||||||
|
|
||||||
|
|
||||||
def newton_raphson(
|
def newton_raphson(
|
||||||
func: str, a: float | Decimal, precision: float = 10 ** -10
|
func: str, a: float | Decimal, precision: float = 10**-10
|
||||||
) -> float:
|
) -> float:
|
||||||
"""Finds root from the point 'a' onwards by Newton-Raphson method
|
"""Finds root from the point 'a' onwards by Newton-Raphson method
|
||||||
>>> newton_raphson("sin(x)", 2)
|
>>> newton_raphson("sin(x)", 2)
|
||||||
@ -25,9 +25,11 @@ def newton_raphson(
|
|||||||
"""
|
"""
|
||||||
x = a
|
x = a
|
||||||
while True:
|
while True:
|
||||||
x = Decimal(x) - (Decimal(eval(func)) / Decimal(eval(str(diff(func)))))
|
x = Decimal(x) - (
|
||||||
|
Decimal(eval(func)) / Decimal(eval(str(diff(func)))) # noqa: S307
|
||||||
|
)
|
||||||
# This number dictates the accuracy of the answer
|
# This number dictates the accuracy of the answer
|
||||||
if abs(eval(func)) < precision:
|
if abs(eval(func)) < precision: # noqa: S307
|
||||||
return float(x)
|
return float(x)
|
||||||
|
|
||||||
|
|
||||||
|
83
arithmetic_analysis/newton_raphson_new.py
Normal file
83
arithmetic_analysis/newton_raphson_new.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
# Implementing Newton Raphson method in Python
|
||||||
|
# Author: Saksham Gupta
|
||||||
|
#
|
||||||
|
# The Newton-Raphson method (also known as Newton's method) is a way to
|
||||||
|
# quickly find a good approximation for the root of a functreal-valued ion
|
||||||
|
# The method can also be extended to complex functions
|
||||||
|
#
|
||||||
|
# Newton's Method - https://en.wikipedia.org/wiki/Newton's_method
|
||||||
|
|
||||||
|
from sympy import diff, lambdify, symbols
|
||||||
|
from sympy.functions import * # noqa: F403
|
||||||
|
|
||||||
|
|
||||||
|
def newton_raphson(
|
||||||
|
function: str,
|
||||||
|
starting_point: complex,
|
||||||
|
variable: str = "x",
|
||||||
|
precision: float = 10**-10,
|
||||||
|
multiplicity: int = 1,
|
||||||
|
) -> complex:
|
||||||
|
"""Finds root from the 'starting_point' onwards by Newton-Raphson method
|
||||||
|
Refer to https://docs.sympy.org/latest/modules/functions/index.html
|
||||||
|
for usable mathematical functions
|
||||||
|
|
||||||
|
>>> newton_raphson("sin(x)", 2)
|
||||||
|
3.141592653589793
|
||||||
|
>>> newton_raphson("x**4 -5", 0.4 + 5j)
|
||||||
|
(-7.52316384526264e-37+1.4953487812212207j)
|
||||||
|
>>> newton_raphson('log(y) - 1', 2, variable='y')
|
||||||
|
2.7182818284590455
|
||||||
|
>>> newton_raphson('exp(x) - 1', 10, precision=0.005)
|
||||||
|
1.2186556186174883e-10
|
||||||
|
>>> newton_raphson('cos(x)', 0)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ZeroDivisionError: Could not find root
|
||||||
|
"""
|
||||||
|
|
||||||
|
x = symbols(variable)
|
||||||
|
func = lambdify(x, function)
|
||||||
|
diff_function = lambdify(x, diff(function, x))
|
||||||
|
|
||||||
|
prev_guess = starting_point
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if diff_function(prev_guess) != 0:
|
||||||
|
next_guess = prev_guess - multiplicity * func(prev_guess) / diff_function(
|
||||||
|
prev_guess
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise ZeroDivisionError("Could not find root") from None
|
||||||
|
|
||||||
|
# Precision is checked by comparing the difference of consecutive guesses
|
||||||
|
if abs(next_guess - prev_guess) < precision:
|
||||||
|
return next_guess
|
||||||
|
|
||||||
|
prev_guess = next_guess
|
||||||
|
|
||||||
|
|
||||||
|
# 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
|
||||||
|
# Find fourth Root of 5
|
||||||
|
print(f"The root of x**4 - 5 = 0 is {newton_raphson('x**4 -5', 0.4 +5j)}")
|
||||||
|
|
||||||
|
# Find value of e
|
||||||
|
print(
|
||||||
|
"The root of log(y) - 1 = 0 is ",
|
||||||
|
f"{newton_raphson('log(y) - 1', 2, variable='y')}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Exponential Roots
|
||||||
|
print(
|
||||||
|
"The root of exp(x) - 1 = 0 is",
|
||||||
|
f"{newton_raphson('exp(x) - 1', 10, precision=0.005)}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Find root of cos(x)
|
||||||
|
print(f"The root of cos(x) = 0 is {newton_raphson('cos(x)', 0)}")
|
@ -20,7 +20,7 @@ def secant_method(lower_bound: float, upper_bound: float, repeats: int) -> float
|
|||||||
"""
|
"""
|
||||||
x0 = lower_bound
|
x0 = lower_bound
|
||||||
x1 = upper_bound
|
x1 = upper_bound
|
||||||
for i in range(0, repeats):
|
for _ in range(0, repeats):
|
||||||
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
|
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
|
||||||
return x1
|
return x1
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ Alternatively you can use scipy.signal.butter, which should yield the same resul
|
|||||||
|
|
||||||
|
|
||||||
def make_lowpass(
|
def make_lowpass(
|
||||||
frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2)
|
frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) # noqa: B008
|
||||||
) -> IIRFilter:
|
) -> IIRFilter:
|
||||||
"""
|
"""
|
||||||
Creates a low-pass filter
|
Creates a low-pass filter
|
||||||
@ -39,7 +39,7 @@ def make_lowpass(
|
|||||||
|
|
||||||
|
|
||||||
def make_highpass(
|
def make_highpass(
|
||||||
frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2)
|
frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) # noqa: B008
|
||||||
) -> IIRFilter:
|
) -> IIRFilter:
|
||||||
"""
|
"""
|
||||||
Creates a high-pass filter
|
Creates a high-pass filter
|
||||||
@ -67,7 +67,7 @@ def make_highpass(
|
|||||||
|
|
||||||
|
|
||||||
def make_bandpass(
|
def make_bandpass(
|
||||||
frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2)
|
frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) # noqa: B008
|
||||||
) -> IIRFilter:
|
) -> IIRFilter:
|
||||||
"""
|
"""
|
||||||
Creates a band-pass filter
|
Creates a band-pass filter
|
||||||
@ -96,7 +96,7 @@ def make_bandpass(
|
|||||||
|
|
||||||
|
|
||||||
def make_allpass(
|
def make_allpass(
|
||||||
frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2)
|
frequency: int, samplerate: int, q_factor: float = 1 / sqrt(2) # noqa: B008
|
||||||
) -> IIRFilter:
|
) -> IIRFilter:
|
||||||
"""
|
"""
|
||||||
Creates an all-pass filter
|
Creates an all-pass filter
|
||||||
@ -121,7 +121,10 @@ def make_allpass(
|
|||||||
|
|
||||||
|
|
||||||
def make_peak(
|
def make_peak(
|
||||||
frequency: int, samplerate: int, gain_db: float, q_factor: float = 1 / sqrt(2)
|
frequency: int,
|
||||||
|
samplerate: int,
|
||||||
|
gain_db: float,
|
||||||
|
q_factor: float = 1 / sqrt(2), # noqa: B008
|
||||||
) -> IIRFilter:
|
) -> IIRFilter:
|
||||||
"""
|
"""
|
||||||
Creates a peak filter
|
Creates a peak filter
|
||||||
@ -150,7 +153,10 @@ def make_peak(
|
|||||||
|
|
||||||
|
|
||||||
def make_lowshelf(
|
def make_lowshelf(
|
||||||
frequency: int, samplerate: int, gain_db: float, q_factor: float = 1 / sqrt(2)
|
frequency: int,
|
||||||
|
samplerate: int,
|
||||||
|
gain_db: float,
|
||||||
|
q_factor: float = 1 / sqrt(2), # noqa: B008
|
||||||
) -> IIRFilter:
|
) -> IIRFilter:
|
||||||
"""
|
"""
|
||||||
Creates a low-shelf filter
|
Creates a low-shelf filter
|
||||||
@ -184,7 +190,10 @@ def make_lowshelf(
|
|||||||
|
|
||||||
|
|
||||||
def make_highshelf(
|
def make_highshelf(
|
||||||
frequency: int, samplerate: int, gain_db: float, q_factor: float = 1 / sqrt(2)
|
frequency: int,
|
||||||
|
samplerate: int,
|
||||||
|
gain_db: float,
|
||||||
|
q_factor: float = 1 / sqrt(2), # noqa: B008
|
||||||
) -> IIRFilter:
|
) -> IIRFilter:
|
||||||
"""
|
"""
|
||||||
Creates a high-shelf filter
|
Creates a high-shelf filter
|
||||||
|
61
audio_filters/equal_loudness_filter.py.broken.txt
Normal file
61
audio_filters/equal_loudness_filter.py.broken.txt
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
from json import loads
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from yulewalker import yulewalk
|
||||||
|
|
||||||
|
from audio_filters.butterworth_filter import make_highpass
|
||||||
|
from audio_filters.iir_filter import IIRFilter
|
||||||
|
|
||||||
|
data = loads((Path(__file__).resolve().parent / "loudness_curve.json").read_text())
|
||||||
|
|
||||||
|
|
||||||
|
class EqualLoudnessFilter:
|
||||||
|
r"""
|
||||||
|
An equal-loudness filter which compensates for the human ear's non-linear response
|
||||||
|
to sound.
|
||||||
|
This filter corrects this by cascading a yulewalk filter and a butterworth filter.
|
||||||
|
|
||||||
|
Designed for use with samplerate of 44.1kHz and above. If you're using a lower
|
||||||
|
samplerate, use with caution.
|
||||||
|
|
||||||
|
Code based on matlab implementation at https://bit.ly/3eqh2HU
|
||||||
|
(url shortened for ruff)
|
||||||
|
|
||||||
|
Target curve: https://i.imgur.com/3g2VfaM.png
|
||||||
|
Yulewalk response: https://i.imgur.com/J9LnJ4C.png
|
||||||
|
Butterworth and overall response: https://i.imgur.com/3g2VfaM.png
|
||||||
|
|
||||||
|
Images and original matlab implementation by David Robinson, 2001
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, samplerate: int = 44100) -> None:
|
||||||
|
self.yulewalk_filter = IIRFilter(10)
|
||||||
|
self.butterworth_filter = make_highpass(150, samplerate)
|
||||||
|
|
||||||
|
# pad the data to nyquist
|
||||||
|
curve_freqs = np.array(data["frequencies"] + [max(20000.0, samplerate / 2)])
|
||||||
|
curve_gains = np.array(data["gains"] + [140])
|
||||||
|
|
||||||
|
# Convert to angular frequency
|
||||||
|
freqs_normalized = curve_freqs / samplerate * 2
|
||||||
|
# Invert the curve and normalize to 0dB
|
||||||
|
gains_normalized = np.power(10, (np.min(curve_gains) - curve_gains) / 20)
|
||||||
|
|
||||||
|
# Scipy's `yulewalk` function is a stub, so we're using the
|
||||||
|
# `yulewalker` library instead.
|
||||||
|
# This function computes the coefficients using a least-squares
|
||||||
|
# fit to the specified curve.
|
||||||
|
ya, yb = yulewalk(10, freqs_normalized, gains_normalized)
|
||||||
|
self.yulewalk_filter.set_coefficients(ya, yb)
|
||||||
|
|
||||||
|
def process(self, sample: float) -> float:
|
||||||
|
"""
|
||||||
|
Process a single sample through both filters
|
||||||
|
|
||||||
|
>>> filt = EqualLoudnessFilter()
|
||||||
|
>>> filt.process(0.0)
|
||||||
|
0.0
|
||||||
|
"""
|
||||||
|
tmp = self.yulewalk_filter.process(sample)
|
||||||
|
return self.butterworth_filter.process(tmp)
|
@ -47,19 +47,21 @@ class IIRFilter:
|
|||||||
>>> filt.set_coefficients(a_coeffs, b_coeffs)
|
>>> filt.set_coefficients(a_coeffs, b_coeffs)
|
||||||
"""
|
"""
|
||||||
if len(a_coeffs) < self.order:
|
if len(a_coeffs) < self.order:
|
||||||
a_coeffs = [1.0] + a_coeffs
|
a_coeffs = [1.0, *a_coeffs]
|
||||||
|
|
||||||
if len(a_coeffs) != self.order + 1:
|
if len(a_coeffs) != self.order + 1:
|
||||||
raise ValueError(
|
msg = (
|
||||||
f"Expected a_coeffs to have {self.order + 1} elements for {self.order}"
|
f"Expected a_coeffs to have {self.order + 1} elements "
|
||||||
f"-order filter, got {len(a_coeffs)}"
|
f"for {self.order}-order filter, got {len(a_coeffs)}"
|
||||||
)
|
)
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
if len(b_coeffs) != self.order + 1:
|
if len(b_coeffs) != self.order + 1:
|
||||||
raise ValueError(
|
msg = (
|
||||||
f"Expected b_coeffs to have {self.order + 1} elements for {self.order}"
|
f"Expected b_coeffs to have {self.order + 1} elements "
|
||||||
f"-order filter, got {len(a_coeffs)}"
|
f"for {self.order}-order filter, got {len(a_coeffs)}"
|
||||||
)
|
)
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
self.a_coeffs = a_coeffs
|
self.a_coeffs = a_coeffs
|
||||||
self.b_coeffs = b_coeffs
|
self.b_coeffs = b_coeffs
|
||||||
|
76
audio_filters/loudness_curve.json
Normal file
76
audio_filters/loudness_curve.json
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
{
|
||||||
|
"_comment": "The following is a representative average of the Equal Loudness Contours as measured by Robinson and Dadson, 1956",
|
||||||
|
"_doi": "10.1088/0508-3443/7/5/302",
|
||||||
|
"frequencies": [
|
||||||
|
0,
|
||||||
|
20,
|
||||||
|
30,
|
||||||
|
40,
|
||||||
|
50,
|
||||||
|
60,
|
||||||
|
70,
|
||||||
|
80,
|
||||||
|
90,
|
||||||
|
100,
|
||||||
|
200,
|
||||||
|
300,
|
||||||
|
400,
|
||||||
|
500,
|
||||||
|
600,
|
||||||
|
700,
|
||||||
|
800,
|
||||||
|
900,
|
||||||
|
1000,
|
||||||
|
1500,
|
||||||
|
2000,
|
||||||
|
2500,
|
||||||
|
3000,
|
||||||
|
3700,
|
||||||
|
4000,
|
||||||
|
5000,
|
||||||
|
6000,
|
||||||
|
7000,
|
||||||
|
8000,
|
||||||
|
9000,
|
||||||
|
10000,
|
||||||
|
12000,
|
||||||
|
15000,
|
||||||
|
20000
|
||||||
|
],
|
||||||
|
"gains": [
|
||||||
|
120,
|
||||||
|
113,
|
||||||
|
103,
|
||||||
|
97,
|
||||||
|
93,
|
||||||
|
91,
|
||||||
|
89,
|
||||||
|
87,
|
||||||
|
86,
|
||||||
|
85,
|
||||||
|
78,
|
||||||
|
76,
|
||||||
|
76,
|
||||||
|
76,
|
||||||
|
76,
|
||||||
|
77,
|
||||||
|
78,
|
||||||
|
79.5,
|
||||||
|
80,
|
||||||
|
79,
|
||||||
|
77,
|
||||||
|
74,
|
||||||
|
71.5,
|
||||||
|
70,
|
||||||
|
70.5,
|
||||||
|
74,
|
||||||
|
79,
|
||||||
|
84,
|
||||||
|
86,
|
||||||
|
86,
|
||||||
|
85,
|
||||||
|
95,
|
||||||
|
110,
|
||||||
|
125
|
||||||
|
]
|
||||||
|
}
|
@ -34,7 +34,7 @@ def get_bounds(
|
|||||||
return lowest, highest
|
return lowest, highest
|
||||||
|
|
||||||
|
|
||||||
def show_frequency_response(filter: FilterType, samplerate: int) -> None:
|
def show_frequency_response(filter_type: FilterType, samplerate: int) -> None:
|
||||||
"""
|
"""
|
||||||
Show frequency response of a filter
|
Show frequency response of a filter
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ def show_frequency_response(filter: FilterType, samplerate: int) -> None:
|
|||||||
|
|
||||||
size = 512
|
size = 512
|
||||||
inputs = [1] + [0] * (size - 1)
|
inputs = [1] + [0] * (size - 1)
|
||||||
outputs = [filter.process(item) for item in inputs]
|
outputs = [filter_type.process(item) for item in inputs]
|
||||||
|
|
||||||
filler = [0] * (samplerate - size) # zero-padding
|
filler = [0] * (samplerate - size) # zero-padding
|
||||||
outputs += filler
|
outputs += filler
|
||||||
@ -66,7 +66,7 @@ def show_frequency_response(filter: FilterType, samplerate: int) -> None:
|
|||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
def show_phase_response(filter: FilterType, samplerate: int) -> None:
|
def show_phase_response(filter_type: FilterType, samplerate: int) -> None:
|
||||||
"""
|
"""
|
||||||
Show phase response of a filter
|
Show phase response of a filter
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ def show_phase_response(filter: FilterType, samplerate: int) -> None:
|
|||||||
|
|
||||||
size = 512
|
size = 512
|
||||||
inputs = [1] + [0] * (size - 1)
|
inputs = [1] + [0] * (size - 1)
|
||||||
outputs = [filter.process(item) for item in inputs]
|
outputs = [filter_type.process(item) for item in inputs]
|
||||||
|
|
||||||
filler = [0] * (samplerate - size) # zero-padding
|
filler = [0] * (samplerate - size) # zero-padding
|
||||||
outputs += filler
|
outputs += filler
|
||||||
|
66
backtracking/combination_sum.py
Normal file
66
backtracking/combination_sum.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
"""
|
||||||
|
In the Combination Sum problem, we are given a list consisting of distinct integers.
|
||||||
|
We need to find all the combinations whose sum equals to target given.
|
||||||
|
We can use an element more than one.
|
||||||
|
|
||||||
|
Time complexity(Average Case): O(n!)
|
||||||
|
|
||||||
|
Constraints:
|
||||||
|
1 <= candidates.length <= 30
|
||||||
|
2 <= candidates[i] <= 40
|
||||||
|
All elements of candidates are distinct.
|
||||||
|
1 <= target <= 40
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def backtrack(
|
||||||
|
candidates: list, path: list, answer: list, target: int, previous_index: int
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
A recursive function that searches for possible combinations. Backtracks in case
|
||||||
|
of a bigger current combination value than the target value.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
previous_index: Last index from the previous search
|
||||||
|
target: The value we need to obtain by summing our integers in the path list.
|
||||||
|
answer: A list of possible combinations
|
||||||
|
path: Current combination
|
||||||
|
candidates: A list of integers we can use.
|
||||||
|
"""
|
||||||
|
if target == 0:
|
||||||
|
answer.append(path.copy())
|
||||||
|
else:
|
||||||
|
for index in range(previous_index, len(candidates)):
|
||||||
|
if target >= candidates[index]:
|
||||||
|
path.append(candidates[index])
|
||||||
|
backtrack(candidates, path, answer, target - candidates[index], index)
|
||||||
|
path.pop(len(path) - 1)
|
||||||
|
|
||||||
|
|
||||||
|
def combination_sum(candidates: list, target: int) -> list:
|
||||||
|
"""
|
||||||
|
>>> combination_sum([2, 3, 5], 8)
|
||||||
|
[[2, 2, 2, 2], [2, 3, 3], [3, 5]]
|
||||||
|
>>> combination_sum([2, 3, 6, 7], 7)
|
||||||
|
[[2, 2, 3], [7]]
|
||||||
|
>>> combination_sum([-8, 2.3, 0], 1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
RecursionError: maximum recursion depth exceeded in comparison
|
||||||
|
"""
|
||||||
|
path = [] # type: list[int]
|
||||||
|
answer = [] # type: list[int]
|
||||||
|
backtrack(candidates, path, answer, target, 0)
|
||||||
|
return answer
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
print(combination_sum([-8, 2.3, 0], 1))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
main()
|
@ -71,7 +71,7 @@ def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int)
|
|||||||
>>> curr_ind = 1
|
>>> curr_ind = 1
|
||||||
>>> util_hamilton_cycle(graph, path, curr_ind)
|
>>> util_hamilton_cycle(graph, path, curr_ind)
|
||||||
True
|
True
|
||||||
>>> print(path)
|
>>> path
|
||||||
[0, 1, 2, 4, 3, 0]
|
[0, 1, 2, 4, 3, 0]
|
||||||
|
|
||||||
Case 2: Use exact graph as in previous case, but in the properties taken from
|
Case 2: Use exact graph as in previous case, but in the properties taken from
|
||||||
@ -85,7 +85,7 @@ def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int)
|
|||||||
>>> curr_ind = 3
|
>>> curr_ind = 3
|
||||||
>>> util_hamilton_cycle(graph, path, curr_ind)
|
>>> util_hamilton_cycle(graph, path, curr_ind)
|
||||||
True
|
True
|
||||||
>>> print(path)
|
>>> path
|
||||||
[0, 1, 2, 4, 3, 0]
|
[0, 1, 2, 4, 3, 0]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -95,10 +95,10 @@ def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int)
|
|||||||
return graph[path[curr_ind - 1]][path[0]] == 1
|
return graph[path[curr_ind - 1]][path[0]] == 1
|
||||||
|
|
||||||
# Recursive Step
|
# Recursive Step
|
||||||
for next in range(0, len(graph)):
|
for next_ver in range(0, len(graph)):
|
||||||
if valid_connection(graph, next, curr_ind, path):
|
if valid_connection(graph, next_ver, curr_ind, path):
|
||||||
# Insert current vertex into path as next transition
|
# Insert current vertex into path as next transition
|
||||||
path[curr_ind] = next
|
path[curr_ind] = next_ver
|
||||||
# Validate created path
|
# Validate created path
|
||||||
if util_hamilton_cycle(graph, path, curr_ind + 1):
|
if util_hamilton_cycle(graph, path, curr_ind + 1):
|
||||||
return True
|
return True
|
||||||
|
@ -78,7 +78,7 @@ def open_knight_tour(n: int) -> list[list[int]]:
|
|||||||
|
|
||||||
>>> open_knight_tour(2)
|
>>> open_knight_tour(2)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: Open Kight Tour cannot be performed on a board of size 2
|
ValueError: Open Kight Tour cannot be performed on a board of size 2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -91,7 +91,8 @@ def open_knight_tour(n: int) -> list[list[int]]:
|
|||||||
return board
|
return board
|
||||||
board[i][j] = 0
|
board[i][j] = 0
|
||||||
|
|
||||||
raise ValueError(f"Open Kight Tour cannot be performed on a board of size {n}")
|
msg = f"Open Kight Tour cannot be performed on a board of size {n}"
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
69
backtracking/minmax.py
Normal file
69
backtracking/minmax.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
"""
|
||||||
|
Minimax helps to achieve maximum score in a game by checking all possible moves.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def minimax(
|
||||||
|
depth: int, node_index: int, is_max: bool, scores: list[int], height: float
|
||||||
|
) -> int:
|
||||||
|
"""
|
||||||
|
depth is current depth in game tree.
|
||||||
|
node_index is index of current node in scores[].
|
||||||
|
scores[] contains the leaves of game tree.
|
||||||
|
height is maximum height of game tree.
|
||||||
|
|
||||||
|
>>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]
|
||||||
|
>>> height = math.log(len(scores), 2)
|
||||||
|
>>> minimax(0, 0, True, scores, height)
|
||||||
|
65
|
||||||
|
>>> minimax(-1, 0, True, scores, height)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Depth cannot be less than 0
|
||||||
|
>>> minimax(0, 0, True, [], 2)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Scores cannot be empty
|
||||||
|
>>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
|
||||||
|
>>> height = math.log(len(scores), 2)
|
||||||
|
>>> minimax(0, 0, True, scores, height)
|
||||||
|
12
|
||||||
|
"""
|
||||||
|
|
||||||
|
if depth < 0:
|
||||||
|
raise ValueError("Depth cannot be less than 0")
|
||||||
|
|
||||||
|
if not scores:
|
||||||
|
raise ValueError("Scores cannot be empty")
|
||||||
|
|
||||||
|
if depth == height:
|
||||||
|
return scores[node_index]
|
||||||
|
|
||||||
|
return (
|
||||||
|
max(
|
||||||
|
minimax(depth + 1, node_index * 2, False, scores, height),
|
||||||
|
minimax(depth + 1, node_index * 2 + 1, False, scores, height),
|
||||||
|
)
|
||||||
|
if is_max
|
||||||
|
else min(
|
||||||
|
minimax(depth + 1, node_index * 2, True, scores, height),
|
||||||
|
minimax(depth + 1, node_index * 2 + 1, True, scores, height),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
|
||||||
|
height = math.log(len(scores), 2)
|
||||||
|
print(f"Optimal value : {minimax(0, 0, True, scores, height)}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
main()
|
@ -12,7 +12,7 @@ from __future__ import annotations
|
|||||||
solution = []
|
solution = []
|
||||||
|
|
||||||
|
|
||||||
def isSafe(board: list[list[int]], row: int, column: int) -> bool:
|
def is_safe(board: list[list[int]], row: int, column: int) -> bool:
|
||||||
"""
|
"""
|
||||||
This function returns a boolean value True if it is safe to place a queen there
|
This function returns a boolean value True if it is safe to place a queen there
|
||||||
considering the current state of the board.
|
considering the current state of the board.
|
||||||
@ -63,7 +63,7 @@ def solve(board: list[list[int]], row: int) -> bool:
|
|||||||
If all the combinations for that particular branch are successful the board is
|
If all the combinations for that particular branch are successful the board is
|
||||||
reinitialized for the next possible combination.
|
reinitialized for the next possible combination.
|
||||||
"""
|
"""
|
||||||
if isSafe(board, row, i):
|
if is_safe(board, row, i):
|
||||||
board[row][i] = 1
|
board[row][i] = 1
|
||||||
solve(board, row + 1)
|
solve(board, row + 1)
|
||||||
board[row][i] = 0
|
board[row][i] = 0
|
||||||
|
@ -107,7 +107,6 @@ def depth_first_search(
|
|||||||
|
|
||||||
# We iterate each column in the row to find all possible results in each row
|
# We iterate each column in the row to find all possible results in each row
|
||||||
for col in range(n):
|
for col in range(n):
|
||||||
|
|
||||||
# We apply that we learned previously. First we check that in the current board
|
# We apply that we learned previously. First we check that in the current board
|
||||||
# (possible_board) there are not other same value because if there is it means
|
# (possible_board) there are not other same value because if there is it means
|
||||||
# that there are a collision in vertical. Then we apply the two formulas we
|
# that there are a collision in vertical. Then we apply the two formulas we
|
||||||
@ -130,9 +129,9 @@ def depth_first_search(
|
|||||||
|
|
||||||
# If it is False we call dfs function again and we update the inputs
|
# If it is False we call dfs function again and we update the inputs
|
||||||
depth_first_search(
|
depth_first_search(
|
||||||
possible_board + [col],
|
[*possible_board, col],
|
||||||
diagonal_right_collisions + [row - col],
|
[*diagonal_right_collisions, row - col],
|
||||||
diagonal_left_collisions + [row + col],
|
[*diagonal_left_collisions, row + col],
|
||||||
boards,
|
boards,
|
||||||
n,
|
n,
|
||||||
)
|
)
|
||||||
|
93
backtracking/power_sum.py
Normal file
93
backtracking/power_sum.py
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
"""
|
||||||
|
Problem source: https://www.hackerrank.com/challenges/the-power-sum/problem
|
||||||
|
Find the number of ways that a given integer X, can be expressed as the sum
|
||||||
|
of the Nth powers of unique, natural numbers. For example, if X=13 and N=2.
|
||||||
|
We have to find all combinations of unique squares adding up to 13.
|
||||||
|
The only solution is 2^2+3^2. Constraints: 1<=X<=1000, 2<=N<=10.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from math import pow
|
||||||
|
|
||||||
|
|
||||||
|
def backtrack(
|
||||||
|
needed_sum: int,
|
||||||
|
power: int,
|
||||||
|
current_number: int,
|
||||||
|
current_sum: int,
|
||||||
|
solutions_count: int,
|
||||||
|
) -> tuple[int, int]:
|
||||||
|
"""
|
||||||
|
>>> backtrack(13, 2, 1, 0, 0)
|
||||||
|
(0, 1)
|
||||||
|
>>> backtrack(100, 2, 1, 0, 0)
|
||||||
|
(0, 3)
|
||||||
|
>>> backtrack(100, 3, 1, 0, 0)
|
||||||
|
(0, 1)
|
||||||
|
>>> backtrack(800, 2, 1, 0, 0)
|
||||||
|
(0, 561)
|
||||||
|
>>> backtrack(1000, 10, 1, 0, 0)
|
||||||
|
(0, 0)
|
||||||
|
>>> backtrack(400, 2, 1, 0, 0)
|
||||||
|
(0, 55)
|
||||||
|
>>> backtrack(50, 1, 1, 0, 0)
|
||||||
|
(0, 3658)
|
||||||
|
"""
|
||||||
|
if current_sum == needed_sum:
|
||||||
|
# If the sum of the powers is equal to needed_sum, then we have a solution.
|
||||||
|
solutions_count += 1
|
||||||
|
return current_sum, solutions_count
|
||||||
|
|
||||||
|
i_to_n = int(pow(current_number, power))
|
||||||
|
if current_sum + i_to_n <= needed_sum:
|
||||||
|
# If the sum of the powers is less than needed_sum, then continue adding powers.
|
||||||
|
current_sum += i_to_n
|
||||||
|
current_sum, solutions_count = backtrack(
|
||||||
|
needed_sum, power, current_number + 1, current_sum, solutions_count
|
||||||
|
)
|
||||||
|
current_sum -= i_to_n
|
||||||
|
if i_to_n < needed_sum:
|
||||||
|
# If the power of i is less than needed_sum, then try with the next power.
|
||||||
|
current_sum, solutions_count = backtrack(
|
||||||
|
needed_sum, power, current_number + 1, current_sum, solutions_count
|
||||||
|
)
|
||||||
|
return current_sum, solutions_count
|
||||||
|
|
||||||
|
|
||||||
|
def solve(needed_sum: int, power: int) -> int:
|
||||||
|
"""
|
||||||
|
>>> solve(13, 2)
|
||||||
|
1
|
||||||
|
>>> solve(100, 2)
|
||||||
|
3
|
||||||
|
>>> solve(100, 3)
|
||||||
|
1
|
||||||
|
>>> solve(800, 2)
|
||||||
|
561
|
||||||
|
>>> solve(1000, 10)
|
||||||
|
0
|
||||||
|
>>> solve(400, 2)
|
||||||
|
55
|
||||||
|
>>> solve(50, 1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Invalid input
|
||||||
|
needed_sum must be between 1 and 1000, power between 2 and 10.
|
||||||
|
>>> solve(-10, 5)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Invalid input
|
||||||
|
needed_sum must be between 1 and 1000, power between 2 and 10.
|
||||||
|
"""
|
||||||
|
if not (1 <= needed_sum <= 1000 and 2 <= power <= 10):
|
||||||
|
raise ValueError(
|
||||||
|
"Invalid input\n"
|
||||||
|
"needed_sum must be between 1 and 1000, power between 2 and 10."
|
||||||
|
)
|
||||||
|
|
||||||
|
return backtrack(needed_sum, power, 1, 0, 0)[1] # Return the solutions_count
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
@ -88,12 +88,12 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
|
|||||||
solutions[i][j] = 1
|
solutions[i][j] = 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
lower_flag = (not (i < 0)) and (not (j < 0)) # Check lower bounds
|
lower_flag = (not i < 0) and (not j < 0) # Check lower bounds
|
||||||
upper_flag = (i < size) and (j < size) # Check upper bounds
|
upper_flag = (i < size) and (j < size) # Check upper bounds
|
||||||
|
|
||||||
if lower_flag and upper_flag:
|
if lower_flag and upper_flag:
|
||||||
# check for already visited and block points.
|
# check for already visited and block points.
|
||||||
block_flag = (not (solutions[i][j])) and (not (maze[i][j]))
|
block_flag = (not solutions[i][j]) and (not maze[i][j])
|
||||||
if block_flag:
|
if block_flag:
|
||||||
# check visited
|
# check visited
|
||||||
solutions[i][j] = 1
|
solutions[i][j] = 1
|
||||||
|
@ -39,14 +39,14 @@ def create_state_space_tree(
|
|||||||
if sum(path) == max_sum:
|
if sum(path) == max_sum:
|
||||||
result.append(path)
|
result.append(path)
|
||||||
return
|
return
|
||||||
for num_index in range(num_index, len(nums)):
|
for index in range(num_index, len(nums)):
|
||||||
create_state_space_tree(
|
create_state_space_tree(
|
||||||
nums,
|
nums,
|
||||||
max_sum,
|
max_sum,
|
||||||
num_index + 1,
|
index + 1,
|
||||||
path + [nums[num_index]],
|
[*path, nums[index]],
|
||||||
result,
|
result,
|
||||||
remaining_nums_sum - nums[num_index],
|
remaining_nums_sum - nums[index],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
168
backtracking/word_search.py
Normal file
168
backtracking/word_search.py
Normal file
@ -0,0 +1,168 @@
|
|||||||
|
"""
|
||||||
|
Author : Alexander Pantyukhin
|
||||||
|
Date : November 24, 2022
|
||||||
|
|
||||||
|
Task:
|
||||||
|
Given an m x n grid of characters board and a string word,
|
||||||
|
return true if word exists in the grid.
|
||||||
|
|
||||||
|
The word can be constructed from letters of sequentially adjacent cells,
|
||||||
|
where adjacent cells are horizontally or vertically neighboring.
|
||||||
|
The same letter cell may not be used more than once.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
Matrix:
|
||||||
|
---------
|
||||||
|
|A|B|C|E|
|
||||||
|
|S|F|C|S|
|
||||||
|
|A|D|E|E|
|
||||||
|
---------
|
||||||
|
|
||||||
|
Word:
|
||||||
|
"ABCCED"
|
||||||
|
|
||||||
|
Result:
|
||||||
|
True
|
||||||
|
|
||||||
|
Implementation notes: Use backtracking approach.
|
||||||
|
At each point, check all neighbors to try to find the next letter of the word.
|
||||||
|
|
||||||
|
leetcode: https://leetcode.com/problems/word-search/
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def get_point_key(len_board: int, len_board_column: int, row: int, column: int) -> int:
|
||||||
|
"""
|
||||||
|
Returns the hash key of matrix indexes.
|
||||||
|
|
||||||
|
>>> get_point_key(10, 20, 1, 0)
|
||||||
|
200
|
||||||
|
"""
|
||||||
|
|
||||||
|
return len_board * len_board_column * row + column
|
||||||
|
|
||||||
|
|
||||||
|
def exits_word(
|
||||||
|
board: list[list[str]],
|
||||||
|
word: str,
|
||||||
|
row: int,
|
||||||
|
column: int,
|
||||||
|
word_index: int,
|
||||||
|
visited_points_set: set[int],
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
Return True if it's possible to search the word suffix
|
||||||
|
starting from the word_index.
|
||||||
|
|
||||||
|
>>> exits_word([["A"]], "B", 0, 0, 0, set())
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
|
||||||
|
if board[row][column] != word[word_index]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if word_index == len(word) - 1:
|
||||||
|
return True
|
||||||
|
|
||||||
|
traverts_directions = [(0, 1), (0, -1), (-1, 0), (1, 0)]
|
||||||
|
len_board = len(board)
|
||||||
|
len_board_column = len(board[0])
|
||||||
|
for direction in traverts_directions:
|
||||||
|
next_i = row + direction[0]
|
||||||
|
next_j = column + direction[1]
|
||||||
|
if not (0 <= next_i < len_board and 0 <= next_j < len_board_column):
|
||||||
|
continue
|
||||||
|
|
||||||
|
key = get_point_key(len_board, len_board_column, next_i, next_j)
|
||||||
|
if key in visited_points_set:
|
||||||
|
continue
|
||||||
|
|
||||||
|
visited_points_set.add(key)
|
||||||
|
if exits_word(board, word, next_i, next_j, word_index + 1, visited_points_set):
|
||||||
|
return True
|
||||||
|
|
||||||
|
visited_points_set.remove(key)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def word_exists(board: list[list[str]], word: str) -> bool:
|
||||||
|
"""
|
||||||
|
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED")
|
||||||
|
True
|
||||||
|
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE")
|
||||||
|
True
|
||||||
|
>>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB")
|
||||||
|
False
|
||||||
|
>>> word_exists([["A"]], "A")
|
||||||
|
True
|
||||||
|
>>> word_exists([["A","A","A","A","A","A"],
|
||||||
|
... ["A","A","A","A","A","A"],
|
||||||
|
... ["A","A","A","A","A","A"],
|
||||||
|
... ["A","A","A","A","A","A"],
|
||||||
|
... ["A","A","A","A","A","B"],
|
||||||
|
... ["A","A","A","A","B","A"]],
|
||||||
|
... "AAAAAAAAAAAAABB")
|
||||||
|
False
|
||||||
|
>>> word_exists([["A"]], 123)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: The word parameter should be a string of length greater than 0.
|
||||||
|
>>> word_exists([["A"]], "")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: The word parameter should be a string of length greater than 0.
|
||||||
|
>>> word_exists([[]], "AB")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: The board should be a non empty matrix of single chars strings.
|
||||||
|
>>> word_exists([], "AB")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: The board should be a non empty matrix of single chars strings.
|
||||||
|
>>> word_exists([["A"], [21]], "AB")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: The board should be a non empty matrix of single chars strings.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Validate board
|
||||||
|
board_error_message = (
|
||||||
|
"The board should be a non empty matrix of single chars strings."
|
||||||
|
)
|
||||||
|
|
||||||
|
len_board = len(board)
|
||||||
|
if not isinstance(board, list) or len(board) == 0:
|
||||||
|
raise ValueError(board_error_message)
|
||||||
|
|
||||||
|
for row in board:
|
||||||
|
if not isinstance(row, list) or len(row) == 0:
|
||||||
|
raise ValueError(board_error_message)
|
||||||
|
|
||||||
|
for item in row:
|
||||||
|
if not isinstance(item, str) or len(item) != 1:
|
||||||
|
raise ValueError(board_error_message)
|
||||||
|
|
||||||
|
# Validate word
|
||||||
|
if not isinstance(word, str) or len(word) == 0:
|
||||||
|
raise ValueError(
|
||||||
|
"The word parameter should be a string of length greater than 0."
|
||||||
|
)
|
||||||
|
|
||||||
|
len_board_column = len(board[0])
|
||||||
|
for i in range(len_board):
|
||||||
|
for j in range(len_board_column):
|
||||||
|
if exits_word(
|
||||||
|
board, word, i, j, 0, {get_point_key(len_board, len_board_column, i, j)}
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
@ -1,7 +1,7 @@
|
|||||||
def get_1s_count(number: int) -> int:
|
def get_1s_count(number: int) -> int:
|
||||||
"""
|
"""
|
||||||
Count the number of set bits in a 32 bit integer using Brian Kernighan's way.
|
Count the number of set bits in a 32 bit integer using Brian Kernighan's way.
|
||||||
Ref - http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
|
Ref - https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
|
||||||
>>> get_1s_count(25)
|
>>> get_1s_count(25)
|
||||||
3
|
3
|
||||||
>>> get_1s_count(37)
|
>>> get_1s_count(37)
|
||||||
@ -17,16 +17,19 @@ def get_1s_count(number: int) -> int:
|
|||||||
>>> get_1s_count(-1)
|
>>> get_1s_count(-1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: the value of input must be positive
|
ValueError: Input must be a non-negative integer
|
||||||
>>> get_1s_count(0.8)
|
>>> get_1s_count(0.8)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
TypeError: Input value must be an 'int' type
|
ValueError: Input must be a non-negative integer
|
||||||
|
>>> get_1s_count("25")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Input must be a non-negative integer
|
||||||
"""
|
"""
|
||||||
if number < 0:
|
if not isinstance(number, int) or number < 0:
|
||||||
raise ValueError("the value of input must be positive")
|
raise ValueError("Input must be a non-negative integer")
|
||||||
elif isinstance(number, float):
|
|
||||||
raise TypeError("Input value must be an 'int' type")
|
|
||||||
count = 0
|
count = 0
|
||||||
while number:
|
while number:
|
||||||
# This way we arrive at next set bit (next 1) instead of looping
|
# This way we arrive at next set bit (next 1) instead of looping
|
||||||
|
@ -1,34 +1,91 @@
|
|||||||
def get_set_bits_count(number: int) -> int:
|
from timeit import timeit
|
||||||
|
|
||||||
|
|
||||||
|
def get_set_bits_count_using_brian_kernighans_algorithm(number: int) -> int:
|
||||||
"""
|
"""
|
||||||
Count the number of set bits in a 32 bit integer
|
Count the number of set bits in a 32 bit integer
|
||||||
>>> get_set_bits_count(25)
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(25)
|
||||||
3
|
3
|
||||||
>>> get_set_bits_count(37)
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(37)
|
||||||
3
|
3
|
||||||
>>> get_set_bits_count(21)
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(21)
|
||||||
3
|
3
|
||||||
>>> get_set_bits_count(58)
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(58)
|
||||||
4
|
4
|
||||||
>>> get_set_bits_count(0)
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(0)
|
||||||
0
|
0
|
||||||
>>> get_set_bits_count(256)
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(256)
|
||||||
1
|
1
|
||||||
>>> get_set_bits_count(-1)
|
>>> get_set_bits_count_using_brian_kernighans_algorithm(-1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: the value of input must be positive
|
ValueError: the value of input must not be negative
|
||||||
"""
|
"""
|
||||||
if number < 0:
|
if number < 0:
|
||||||
raise ValueError("the value of input must be positive")
|
raise ValueError("the value of input must not be negative")
|
||||||
|
result = 0
|
||||||
|
while number:
|
||||||
|
number &= number - 1
|
||||||
|
result += 1
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def get_set_bits_count_using_modulo_operator(number: int) -> int:
|
||||||
|
"""
|
||||||
|
Count the number of set bits in a 32 bit integer
|
||||||
|
>>> get_set_bits_count_using_modulo_operator(25)
|
||||||
|
3
|
||||||
|
>>> get_set_bits_count_using_modulo_operator(37)
|
||||||
|
3
|
||||||
|
>>> get_set_bits_count_using_modulo_operator(21)
|
||||||
|
3
|
||||||
|
>>> get_set_bits_count_using_modulo_operator(58)
|
||||||
|
4
|
||||||
|
>>> get_set_bits_count_using_modulo_operator(0)
|
||||||
|
0
|
||||||
|
>>> get_set_bits_count_using_modulo_operator(256)
|
||||||
|
1
|
||||||
|
>>> get_set_bits_count_using_modulo_operator(-1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: the value of input must not be negative
|
||||||
|
"""
|
||||||
|
if number < 0:
|
||||||
|
raise ValueError("the value of input must not be negative")
|
||||||
result = 0
|
result = 0
|
||||||
while number:
|
while number:
|
||||||
if number % 2 == 1:
|
if number % 2 == 1:
|
||||||
result += 1
|
result += 1
|
||||||
number = number >> 1
|
number >>= 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def benchmark() -> None:
|
||||||
|
"""
|
||||||
|
Benchmark code for comparing 2 functions, with different length int values.
|
||||||
|
Brian Kernighan's algorithm is consistently faster than using modulo_operator.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def do_benchmark(number: int) -> None:
|
||||||
|
setup = "import __main__ as z"
|
||||||
|
print(f"Benchmark when {number = }:")
|
||||||
|
print(f"{get_set_bits_count_using_modulo_operator(number) = }")
|
||||||
|
timing = timeit("z.get_set_bits_count_using_modulo_operator(25)", setup=setup)
|
||||||
|
print(f"timeit() runs in {timing} seconds")
|
||||||
|
print(f"{get_set_bits_count_using_brian_kernighans_algorithm(number) = }")
|
||||||
|
timing = timeit(
|
||||||
|
"z.get_set_bits_count_using_brian_kernighans_algorithm(25)",
|
||||||
|
setup=setup,
|
||||||
|
)
|
||||||
|
print(f"timeit() runs in {timing} seconds")
|
||||||
|
|
||||||
|
for number in (25, 37, 58, 0):
|
||||||
|
do_benchmark(number)
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
benchmark()
|
||||||
|
34
bit_manipulation/highest_set_bit.py
Normal file
34
bit_manipulation/highest_set_bit.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
def get_highest_set_bit_position(number: int) -> int:
|
||||||
|
"""
|
||||||
|
Returns position of the highest set bit of a number.
|
||||||
|
Ref - https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
|
||||||
|
>>> get_highest_set_bit_position(25)
|
||||||
|
5
|
||||||
|
>>> get_highest_set_bit_position(37)
|
||||||
|
6
|
||||||
|
>>> get_highest_set_bit_position(1)
|
||||||
|
1
|
||||||
|
>>> get_highest_set_bit_position(4)
|
||||||
|
3
|
||||||
|
>>> get_highest_set_bit_position(0)
|
||||||
|
0
|
||||||
|
>>> get_highest_set_bit_position(0.8)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: Input value must be an 'int' type
|
||||||
|
"""
|
||||||
|
if not isinstance(number, int):
|
||||||
|
raise TypeError("Input value must be an 'int' type")
|
||||||
|
|
||||||
|
position = 0
|
||||||
|
while number:
|
||||||
|
position += 1
|
||||||
|
number >>= 1
|
||||||
|
|
||||||
|
return position
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
51
bit_manipulation/index_of_rightmost_set_bit.py
Normal file
51
bit_manipulation/index_of_rightmost_set_bit.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
# Reference: https://www.geeksforgeeks.org/position-of-rightmost-set-bit/
|
||||||
|
|
||||||
|
|
||||||
|
def get_index_of_rightmost_set_bit(number: int) -> int:
|
||||||
|
"""
|
||||||
|
Take in a positive integer 'number'.
|
||||||
|
Returns the zero-based index of first set bit in that 'number' from right.
|
||||||
|
Returns -1, If no set bit found.
|
||||||
|
|
||||||
|
>>> get_index_of_rightmost_set_bit(0)
|
||||||
|
-1
|
||||||
|
>>> get_index_of_rightmost_set_bit(5)
|
||||||
|
0
|
||||||
|
>>> get_index_of_rightmost_set_bit(36)
|
||||||
|
2
|
||||||
|
>>> get_index_of_rightmost_set_bit(8)
|
||||||
|
3
|
||||||
|
>>> get_index_of_rightmost_set_bit(-18)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Input must be a non-negative integer
|
||||||
|
>>> get_index_of_rightmost_set_bit('test')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Input must be a non-negative integer
|
||||||
|
>>> get_index_of_rightmost_set_bit(1.25)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Input must be a non-negative integer
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not isinstance(number, int) or number < 0:
|
||||||
|
raise ValueError("Input must be a non-negative integer")
|
||||||
|
|
||||||
|
intermediate = number & ~(number - 1)
|
||||||
|
index = 0
|
||||||
|
while intermediate:
|
||||||
|
intermediate >>= 1
|
||||||
|
index += 1
|
||||||
|
return index - 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
"""
|
||||||
|
Finding the index of rightmost set bit has some very peculiar use-cases,
|
||||||
|
especially in finding missing or/and repeating numbers in a list of
|
||||||
|
positive integers.
|
||||||
|
"""
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod(verbose=True)
|
37
bit_manipulation/is_even.py
Normal file
37
bit_manipulation/is_even.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
def is_even(number: int) -> bool:
|
||||||
|
"""
|
||||||
|
return true if the input integer is even
|
||||||
|
Explanation: Lets take a look at the following deicmal to binary conversions
|
||||||
|
2 => 10
|
||||||
|
14 => 1110
|
||||||
|
100 => 1100100
|
||||||
|
3 => 11
|
||||||
|
13 => 1101
|
||||||
|
101 => 1100101
|
||||||
|
from the above examples we can observe that
|
||||||
|
for all the odd integers there is always 1 set bit at the end
|
||||||
|
also, 1 in binary can be represented as 001, 00001, or 0000001
|
||||||
|
so for any odd integer n => n&1 is always equals 1 else the integer is even
|
||||||
|
|
||||||
|
>>> is_even(1)
|
||||||
|
False
|
||||||
|
>>> is_even(4)
|
||||||
|
True
|
||||||
|
>>> is_even(9)
|
||||||
|
False
|
||||||
|
>>> is_even(15)
|
||||||
|
False
|
||||||
|
>>> is_even(40)
|
||||||
|
True
|
||||||
|
>>> is_even(100)
|
||||||
|
True
|
||||||
|
>>> is_even(101)
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
return number & 1 == 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
57
bit_manipulation/is_power_of_two.py
Normal file
57
bit_manipulation/is_power_of_two.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
"""
|
||||||
|
Author : Alexander Pantyukhin
|
||||||
|
Date : November 1, 2022
|
||||||
|
|
||||||
|
Task:
|
||||||
|
Given a positive int number. Return True if this number is power of 2
|
||||||
|
or False otherwise.
|
||||||
|
|
||||||
|
Implementation notes: Use bit manipulation.
|
||||||
|
For example if the number is the power of two it's bits representation:
|
||||||
|
n = 0..100..00
|
||||||
|
n - 1 = 0..011..11
|
||||||
|
|
||||||
|
n & (n - 1) - no intersections = 0
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def is_power_of_two(number: int) -> bool:
|
||||||
|
"""
|
||||||
|
Return True if this number is power of 2 or False otherwise.
|
||||||
|
|
||||||
|
>>> is_power_of_two(0)
|
||||||
|
True
|
||||||
|
>>> is_power_of_two(1)
|
||||||
|
True
|
||||||
|
>>> is_power_of_two(2)
|
||||||
|
True
|
||||||
|
>>> is_power_of_two(4)
|
||||||
|
True
|
||||||
|
>>> is_power_of_two(6)
|
||||||
|
False
|
||||||
|
>>> is_power_of_two(8)
|
||||||
|
True
|
||||||
|
>>> is_power_of_two(17)
|
||||||
|
False
|
||||||
|
>>> is_power_of_two(-1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: number must not be negative
|
||||||
|
>>> is_power_of_two(1.2)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: unsupported operand type(s) for &: 'float' and 'float'
|
||||||
|
|
||||||
|
# Test all powers of 2 from 0 to 10,000
|
||||||
|
>>> all(is_power_of_two(int(2 ** i)) for i in range(10000))
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
if number < 0:
|
||||||
|
raise ValueError("number must not be negative")
|
||||||
|
return number & (number - 1) == 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
39
bit_manipulation/numbers_different_signs.py
Normal file
39
bit_manipulation/numbers_different_signs.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
"""
|
||||||
|
Author : Alexander Pantyukhin
|
||||||
|
Date : November 30, 2022
|
||||||
|
|
||||||
|
Task:
|
||||||
|
Given two int numbers. Return True these numbers have opposite signs
|
||||||
|
or False otherwise.
|
||||||
|
|
||||||
|
Implementation notes: Use bit manipulation.
|
||||||
|
Use XOR for two numbers.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def different_signs(num1: int, num2: int) -> bool:
|
||||||
|
"""
|
||||||
|
Return True if numbers have opposite signs False otherwise.
|
||||||
|
|
||||||
|
>>> different_signs(1, -1)
|
||||||
|
True
|
||||||
|
>>> different_signs(1, 1)
|
||||||
|
False
|
||||||
|
>>> different_signs(1000000000000000000000000000, -1000000000000000000000000000)
|
||||||
|
True
|
||||||
|
>>> different_signs(-1000000000000000000000000000, 1000000000000000000000000000)
|
||||||
|
True
|
||||||
|
>>> different_signs(50, 278)
|
||||||
|
False
|
||||||
|
>>> different_signs(0, 2)
|
||||||
|
False
|
||||||
|
>>> different_signs(2, 0)
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
return num1 ^ num2 < 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
@ -14,10 +14,11 @@ def get_reverse_bit_string(number: int) -> str:
|
|||||||
TypeError: operation can not be conducted on a object of type str
|
TypeError: operation can not be conducted on a object of type str
|
||||||
"""
|
"""
|
||||||
if not isinstance(number, int):
|
if not isinstance(number, int):
|
||||||
raise TypeError(
|
msg = (
|
||||||
"operation can not be conducted on a object of type "
|
"operation can not be conducted on a object of type "
|
||||||
f"{type(number).__name__}"
|
f"{type(number).__name__}"
|
||||||
)
|
)
|
||||||
|
raise TypeError(msg)
|
||||||
bit_string = ""
|
bit_string = ""
|
||||||
for _ in range(0, 32):
|
for _ in range(0, 32):
|
||||||
bit_string += str(number % 2)
|
bit_string += str(number % 2)
|
||||||
|
45
blockchain/README.md
Normal file
45
blockchain/README.md
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# Blockchain
|
||||||
|
|
||||||
|
A Blockchain is a type of **distributed ledger** technology (DLT) that consists of growing list of records, called **blocks**, that are securely linked together using **cryptography**.
|
||||||
|
|
||||||
|
Let's breakdown the terminologies in the above definition. We find below terminologies,
|
||||||
|
|
||||||
|
- Digital Ledger Technology (DLT)
|
||||||
|
- Blocks
|
||||||
|
- Cryptography
|
||||||
|
|
||||||
|
## Digital Ledger Technology
|
||||||
|
|
||||||
|
It is otherwise called as distributed ledger technology. It is simply the opposite of centralized database. Firstly, what is a **ledger**? A ledger is a book or collection of accounts that records account transactions.
|
||||||
|
|
||||||
|
*Why is Blockchain addressed as digital ledger if it can record more than account transactions? What other transaction details and information can it hold?*
|
||||||
|
|
||||||
|
Digital Ledger Technology is just a ledger which is shared among multiple nodes. This way there exist no need for central authority to hold the info. Okay, how is it differentiated from central database and what are their benefits?
|
||||||
|
|
||||||
|
There is an organization which has 4 branches whose data are stored in a centralized database. So even if one branch needs any data from ledger they need an approval from database in charge. And if one hacks the central database he gets to tamper and control all the data.
|
||||||
|
|
||||||
|
Now lets assume every branch has a copy of the ledger and then once anything is added to the ledger by anyone branch it is gonna automatically reflect in all other ledgers available in other branch. This is done using Peer-to-peer network.
|
||||||
|
|
||||||
|
So this means even if information is tampered in one branch we can find out. If one branch is hacked we can be alerted ,so we can safeguard other branches. Now, assume these branches as computers or nodes and the ledger is a transaction record or digital receipt. If one ledger is hacked in a node we can detect since there will be a mismatch in comparison with other node information. So this is the concept of Digital Ledger Technology.
|
||||||
|
|
||||||
|
*Is it required for all nodes to have access to all information in other nodes? Wouldn't this require enormous storage space in each node?*
|
||||||
|
|
||||||
|
## Blocks
|
||||||
|
|
||||||
|
In short a block is nothing but collections of records with a labelled header. These are connected cryptographically. Once a new block is added to a chain, the previous block is connected, more precisely said as locked and hence, will remain unaltered. We can understand this concept once we get a clear understanding of working mechanism of blockchain.
|
||||||
|
|
||||||
|
## Cryptography
|
||||||
|
|
||||||
|
It is the practice and study of secure communication techniques in the midst of adversarial behavior. More broadly, cryptography is the creation and analysis of protocols that prevent third parties or the general public from accessing private messages.
|
||||||
|
|
||||||
|
*Which cryptography technology is most widely used in blockchain and why?*
|
||||||
|
|
||||||
|
So, in general, blockchain technology is a distributed record holder which records the information about ownership of an asset. To define precisely,
|
||||||
|
> Blockchain is a distributed, immutable ledger that makes it easier to record transactions and track assets in a corporate network.
|
||||||
|
An asset could be tangible (such as a house, car, cash, or land) or intangible (such as a business) (intellectual property, patents, copyrights, branding). A blockchain network can track and sell almost anything of value, lowering risk and costs for everyone involved.
|
||||||
|
|
||||||
|
So this is all about introduction to blockchain technology. To learn more about the topic refer below links....
|
||||||
|
* <https://en.wikipedia.org/wiki/Blockchain>
|
||||||
|
* <https://en.wikipedia.org/wiki/Chinese_remainder_theorem>
|
||||||
|
* <https://en.wikipedia.org/wiki/Diophantine_equation>
|
||||||
|
* <https://www.geeksforgeeks.org/modular-division/>
|
@ -53,6 +53,7 @@ def chinese_remainder_theorem(n1: int, r1: int, n2: int, r2: int) -> int:
|
|||||||
|
|
||||||
# ----------SAME SOLUTION USING InvertModulo instead ExtendedEuclid----------------
|
# ----------SAME SOLUTION USING InvertModulo instead ExtendedEuclid----------------
|
||||||
|
|
||||||
|
|
||||||
# This function find the inverses of a i.e., a^(-1)
|
# This function find the inverses of a i.e., a^(-1)
|
||||||
def invert_modulo(a: int, n: int) -> int:
|
def invert_modulo(a: int, n: int) -> int:
|
||||||
"""
|
"""
|
||||||
|
50
boolean_algebra/and_gate.py
Normal file
50
boolean_algebra/and_gate.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
"""
|
||||||
|
An AND Gate is a logic gate in boolean algebra which results to 1 (True) if both the
|
||||||
|
inputs are 1, and 0 (False) otherwise.
|
||||||
|
|
||||||
|
Following is the truth table of an AND Gate:
|
||||||
|
------------------------------
|
||||||
|
| Input 1 | Input 2 | Output |
|
||||||
|
------------------------------
|
||||||
|
| 0 | 0 | 0 |
|
||||||
|
| 0 | 1 | 0 |
|
||||||
|
| 1 | 0 | 0 |
|
||||||
|
| 1 | 1 | 1 |
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def and_gate(input_1: int, input_2: int) -> int:
|
||||||
|
"""
|
||||||
|
Calculate AND of the input values
|
||||||
|
|
||||||
|
>>> and_gate(0, 0)
|
||||||
|
0
|
||||||
|
>>> and_gate(0, 1)
|
||||||
|
0
|
||||||
|
>>> and_gate(1, 0)
|
||||||
|
0
|
||||||
|
>>> and_gate(1, 1)
|
||||||
|
1
|
||||||
|
"""
|
||||||
|
return int((input_1, input_2).count(0) == 0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_and_gate() -> None:
|
||||||
|
"""
|
||||||
|
Tests the and_gate function
|
||||||
|
"""
|
||||||
|
assert and_gate(0, 0) == 0
|
||||||
|
assert and_gate(0, 1) == 0
|
||||||
|
assert and_gate(1, 0) == 0
|
||||||
|
assert and_gate(1, 1) == 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_and_gate()
|
||||||
|
print(and_gate(1, 0))
|
||||||
|
print(and_gate(0, 0))
|
||||||
|
print(and_gate(0, 1))
|
||||||
|
print(and_gate(1, 1))
|
47
boolean_algebra/nand_gate.py
Normal file
47
boolean_algebra/nand_gate.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
"""
|
||||||
|
A NAND Gate is a logic gate in boolean algebra which results to 0 (False) if both
|
||||||
|
the inputs are 1, and 1 (True) otherwise. It's similar to adding
|
||||||
|
a NOT gate along with an AND gate.
|
||||||
|
Following is the truth table of a NAND Gate:
|
||||||
|
------------------------------
|
||||||
|
| Input 1 | Input 2 | Output |
|
||||||
|
------------------------------
|
||||||
|
| 0 | 0 | 1 |
|
||||||
|
| 0 | 1 | 1 |
|
||||||
|
| 1 | 0 | 1 |
|
||||||
|
| 1 | 1 | 0 |
|
||||||
|
------------------------------
|
||||||
|
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def nand_gate(input_1: int, input_2: int) -> int:
|
||||||
|
"""
|
||||||
|
Calculate NAND of the input values
|
||||||
|
>>> nand_gate(0, 0)
|
||||||
|
1
|
||||||
|
>>> nand_gate(0, 1)
|
||||||
|
1
|
||||||
|
>>> nand_gate(1, 0)
|
||||||
|
1
|
||||||
|
>>> nand_gate(1, 1)
|
||||||
|
0
|
||||||
|
"""
|
||||||
|
return int((input_1, input_2).count(0) != 0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_nand_gate() -> None:
|
||||||
|
"""
|
||||||
|
Tests the nand_gate function
|
||||||
|
"""
|
||||||
|
assert nand_gate(0, 0) == 1
|
||||||
|
assert nand_gate(0, 1) == 1
|
||||||
|
assert nand_gate(1, 0) == 1
|
||||||
|
assert nand_gate(1, 1) == 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(nand_gate(0, 0))
|
||||||
|
print(nand_gate(0, 1))
|
||||||
|
print(nand_gate(1, 0))
|
||||||
|
print(nand_gate(1, 1))
|
48
boolean_algebra/nor_gate.py
Normal file
48
boolean_algebra/nor_gate.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
"""
|
||||||
|
A NOR Gate is a logic gate in boolean algebra which results to false(0)
|
||||||
|
if any of the input is 1, and True(1) if both the inputs are 0.
|
||||||
|
Following is the truth table of a NOR Gate:
|
||||||
|
| Input 1 | Input 2 | Output |
|
||||||
|
| 0 | 0 | 1 |
|
||||||
|
| 0 | 1 | 0 |
|
||||||
|
| 1 | 0 | 0 |
|
||||||
|
| 1 | 1 | 0 |
|
||||||
|
|
||||||
|
Following is the code implementation of the NOR Gate
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def nor_gate(input_1: int, input_2: int) -> int:
|
||||||
|
"""
|
||||||
|
>>> nor_gate(0, 0)
|
||||||
|
1
|
||||||
|
>>> nor_gate(0, 1)
|
||||||
|
0
|
||||||
|
>>> nor_gate(1, 0)
|
||||||
|
0
|
||||||
|
>>> nor_gate(1, 1)
|
||||||
|
0
|
||||||
|
>>> nor_gate(0.0, 0.0)
|
||||||
|
1
|
||||||
|
>>> nor_gate(0, -7)
|
||||||
|
0
|
||||||
|
"""
|
||||||
|
return int(input_1 == input_2 == 0)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
print("Truth Table of NOR Gate:")
|
||||||
|
print("| Input 1 | Input 2 | Output |")
|
||||||
|
print(f"| 0 | 0 | {nor_gate(0, 0)} |")
|
||||||
|
print(f"| 0 | 1 | {nor_gate(0, 1)} |")
|
||||||
|
print(f"| 1 | 0 | {nor_gate(1, 0)} |")
|
||||||
|
print(f"| 1 | 1 | {nor_gate(1, 1)} |")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
main()
|
||||||
|
"""Code provided by Akshaj Vishwanathan"""
|
||||||
|
"""Reference: https://www.geeksforgeeks.org/logic-gates-in-python/"""
|
37
boolean_algebra/not_gate.py
Normal file
37
boolean_algebra/not_gate.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
"""
|
||||||
|
A NOT Gate is a logic gate in boolean algebra which results to 0 (False) if the
|
||||||
|
input is high, and 1 (True) if the input is low.
|
||||||
|
Following is the truth table of a XOR Gate:
|
||||||
|
------------------------------
|
||||||
|
| Input | Output |
|
||||||
|
------------------------------
|
||||||
|
| 0 | 1 |
|
||||||
|
| 1 | 0 |
|
||||||
|
------------------------------
|
||||||
|
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def not_gate(input_1: int) -> int:
|
||||||
|
"""
|
||||||
|
Calculate NOT of the input values
|
||||||
|
>>> not_gate(0)
|
||||||
|
1
|
||||||
|
>>> not_gate(1)
|
||||||
|
0
|
||||||
|
"""
|
||||||
|
|
||||||
|
return 1 if input_1 == 0 else 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_not_gate() -> None:
|
||||||
|
"""
|
||||||
|
Tests the not_gate function
|
||||||
|
"""
|
||||||
|
assert not_gate(0) == 1
|
||||||
|
assert not_gate(1) == 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(not_gate(0))
|
||||||
|
print(not_gate(1))
|
46
boolean_algebra/or_gate.py
Normal file
46
boolean_algebra/or_gate.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
"""
|
||||||
|
An OR Gate is a logic gate in boolean algebra which results to 0 (False) if both the
|
||||||
|
inputs are 0, and 1 (True) otherwise.
|
||||||
|
Following is the truth table of an AND Gate:
|
||||||
|
------------------------------
|
||||||
|
| Input 1 | Input 2 | Output |
|
||||||
|
------------------------------
|
||||||
|
| 0 | 0 | 0 |
|
||||||
|
| 0 | 1 | 1 |
|
||||||
|
| 1 | 0 | 1 |
|
||||||
|
| 1 | 1 | 1 |
|
||||||
|
------------------------------
|
||||||
|
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def or_gate(input_1: int, input_2: int) -> int:
|
||||||
|
"""
|
||||||
|
Calculate OR of the input values
|
||||||
|
>>> or_gate(0, 0)
|
||||||
|
0
|
||||||
|
>>> or_gate(0, 1)
|
||||||
|
1
|
||||||
|
>>> or_gate(1, 0)
|
||||||
|
1
|
||||||
|
>>> or_gate(1, 1)
|
||||||
|
1
|
||||||
|
"""
|
||||||
|
return int((input_1, input_2).count(1) != 0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_or_gate() -> None:
|
||||||
|
"""
|
||||||
|
Tests the or_gate function
|
||||||
|
"""
|
||||||
|
assert or_gate(0, 0) == 0
|
||||||
|
assert or_gate(0, 1) == 1
|
||||||
|
assert or_gate(1, 0) == 1
|
||||||
|
assert or_gate(1, 1) == 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(or_gate(0, 1))
|
||||||
|
print(or_gate(1, 0))
|
||||||
|
print(or_gate(0, 0))
|
||||||
|
print(or_gate(1, 1))
|
@ -1,25 +1,28 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Sequence
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
def compare_string(string1: str, string2: str) -> str:
|
|
||||||
|
def compare_string(string1: str, string2: str) -> str | Literal[False]:
|
||||||
"""
|
"""
|
||||||
>>> compare_string('0010','0110')
|
>>> compare_string('0010','0110')
|
||||||
'0_10'
|
'0_10'
|
||||||
|
|
||||||
>>> compare_string('0110','1101')
|
>>> compare_string('0110','1101')
|
||||||
'X'
|
False
|
||||||
"""
|
"""
|
||||||
l1 = list(string1)
|
list1 = list(string1)
|
||||||
l2 = list(string2)
|
list2 = list(string2)
|
||||||
count = 0
|
count = 0
|
||||||
for i in range(len(l1)):
|
for i in range(len(list1)):
|
||||||
if l1[i] != l2[i]:
|
if list1[i] != list2[i]:
|
||||||
count += 1
|
count += 1
|
||||||
l1[i] = "_"
|
list1[i] = "_"
|
||||||
if count > 1:
|
if count > 1:
|
||||||
return "X"
|
return False
|
||||||
else:
|
else:
|
||||||
return "".join(l1)
|
return "".join(list1)
|
||||||
|
|
||||||
|
|
||||||
def check(binary: list[str]) -> list[str]:
|
def check(binary: list[str]) -> list[str]:
|
||||||
@ -28,16 +31,16 @@ def check(binary: list[str]) -> list[str]:
|
|||||||
['0.00.01.5']
|
['0.00.01.5']
|
||||||
"""
|
"""
|
||||||
pi = []
|
pi = []
|
||||||
while 1:
|
while True:
|
||||||
check1 = ["$"] * len(binary)
|
check1 = ["$"] * len(binary)
|
||||||
temp = []
|
temp = []
|
||||||
for i in range(len(binary)):
|
for i in range(len(binary)):
|
||||||
for j in range(i + 1, len(binary)):
|
for j in range(i + 1, len(binary)):
|
||||||
k = compare_string(binary[i], binary[j])
|
k = compare_string(binary[i], binary[j])
|
||||||
if k != "X":
|
if k is False:
|
||||||
check1[i] = "*"
|
check1[i] = "*"
|
||||||
check1[j] = "*"
|
check1[j] = "*"
|
||||||
temp.append(k)
|
temp.append("X")
|
||||||
for i in range(len(binary)):
|
for i in range(len(binary)):
|
||||||
if check1[i] == "$":
|
if check1[i] == "$":
|
||||||
pi.append(binary[i])
|
pi.append(binary[i])
|
||||||
@ -46,19 +49,18 @@ def check(binary: list[str]) -> list[str]:
|
|||||||
binary = list(set(temp))
|
binary = list(set(temp))
|
||||||
|
|
||||||
|
|
||||||
def decimal_to_binary(no_of_variable: int, minterms: list[float]) -> list[str]:
|
def decimal_to_binary(no_of_variable: int, minterms: Sequence[float]) -> list[str]:
|
||||||
"""
|
"""
|
||||||
>>> decimal_to_binary(3,[1.5])
|
>>> decimal_to_binary(3,[1.5])
|
||||||
['0.00.01.5']
|
['0.00.01.5']
|
||||||
"""
|
"""
|
||||||
temp = []
|
temp = []
|
||||||
s = ""
|
for minterm in minterms:
|
||||||
for m in minterms:
|
string = ""
|
||||||
for i in range(no_of_variable):
|
for _ in range(no_of_variable):
|
||||||
s = str(m % 2) + s
|
string = str(minterm % 2) + string
|
||||||
m //= 2
|
minterm //= 2
|
||||||
temp.append(s)
|
temp.append(string)
|
||||||
s = ""
|
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
|
|
||||||
@ -70,16 +72,10 @@ def is_for_table(string1: str, string2: str, count: int) -> bool:
|
|||||||
>>> is_for_table('01_','001',1)
|
>>> is_for_table('01_','001',1)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
l1 = list(string1)
|
list1 = list(string1)
|
||||||
l2 = list(string2)
|
list2 = list(string2)
|
||||||
count_n = 0
|
count_n = sum(item1 != item2 for item1, item2 in zip(list1, list2))
|
||||||
for i in range(len(l1)):
|
return count_n == count
|
||||||
if l1[i] != l2[i]:
|
|
||||||
count_n += 1
|
|
||||||
if count_n == count:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
|
def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
|
||||||
@ -93,40 +89,34 @@ def selection(chart: list[list[int]], prime_implicants: list[str]) -> list[str]:
|
|||||||
temp = []
|
temp = []
|
||||||
select = [0] * len(chart)
|
select = [0] * len(chart)
|
||||||
for i in range(len(chart[0])):
|
for i in range(len(chart[0])):
|
||||||
count = 0
|
count = sum(row[i] == 1 for row in chart)
|
||||||
rem = -1
|
|
||||||
for j in range(len(chart)):
|
|
||||||
if chart[j][i] == 1:
|
|
||||||
count += 1
|
|
||||||
rem = j
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
|
rem = max(j for j, row in enumerate(chart) if row[i] == 1)
|
||||||
select[rem] = 1
|
select[rem] = 1
|
||||||
for i in range(len(select)):
|
for i, item in enumerate(select):
|
||||||
if select[i] == 1:
|
if item != 1:
|
||||||
for j in range(len(chart[0])):
|
continue
|
||||||
if chart[i][j] == 1:
|
for j in range(len(chart[0])):
|
||||||
for k in range(len(chart)):
|
if chart[i][j] != 1:
|
||||||
chart[k][j] = 0
|
continue
|
||||||
temp.append(prime_implicants[i])
|
for row in chart:
|
||||||
while 1:
|
row[j] = 0
|
||||||
max_n = 0
|
temp.append(prime_implicants[i])
|
||||||
rem = -1
|
while True:
|
||||||
count_n = 0
|
counts = [chart[i].count(1) for i in range(len(chart))]
|
||||||
for i in range(len(chart)):
|
max_n = max(counts)
|
||||||
count_n = chart[i].count(1)
|
rem = counts.index(max_n)
|
||||||
if count_n > max_n:
|
|
||||||
max_n = count_n
|
|
||||||
rem = i
|
|
||||||
|
|
||||||
if max_n == 0:
|
if max_n == 0:
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
temp.append(prime_implicants[rem])
|
temp.append(prime_implicants[rem])
|
||||||
|
|
||||||
for i in range(len(chart[0])):
|
for j in range(len(chart[0])):
|
||||||
if chart[rem][i] == 1:
|
if chart[rem][j] != 1:
|
||||||
for j in range(len(chart)):
|
continue
|
||||||
chart[j][i] = 0
|
for i in range(len(chart)):
|
||||||
|
chart[i][j] = 0
|
||||||
|
|
||||||
|
|
||||||
def prime_implicant_chart(
|
def prime_implicant_chart(
|
||||||
|
48
boolean_algebra/xnor_gate.py
Normal file
48
boolean_algebra/xnor_gate.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
"""
|
||||||
|
A XNOR Gate is a logic gate in boolean algebra which results to 0 (False) if both the
|
||||||
|
inputs are different, and 1 (True), if the inputs are same.
|
||||||
|
It's similar to adding a NOT gate to an XOR gate
|
||||||
|
|
||||||
|
Following is the truth table of a XNOR Gate:
|
||||||
|
------------------------------
|
||||||
|
| Input 1 | Input 2 | Output |
|
||||||
|
------------------------------
|
||||||
|
| 0 | 0 | 1 |
|
||||||
|
| 0 | 1 | 0 |
|
||||||
|
| 1 | 0 | 0 |
|
||||||
|
| 1 | 1 | 1 |
|
||||||
|
------------------------------
|
||||||
|
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def xnor_gate(input_1: int, input_2: int) -> int:
|
||||||
|
"""
|
||||||
|
Calculate XOR of the input values
|
||||||
|
>>> xnor_gate(0, 0)
|
||||||
|
1
|
||||||
|
>>> xnor_gate(0, 1)
|
||||||
|
0
|
||||||
|
>>> xnor_gate(1, 0)
|
||||||
|
0
|
||||||
|
>>> xnor_gate(1, 1)
|
||||||
|
1
|
||||||
|
"""
|
||||||
|
return 1 if input_1 == input_2 else 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_xnor_gate() -> None:
|
||||||
|
"""
|
||||||
|
Tests the xnor_gate function
|
||||||
|
"""
|
||||||
|
assert xnor_gate(0, 0) == 1
|
||||||
|
assert xnor_gate(0, 1) == 0
|
||||||
|
assert xnor_gate(1, 0) == 0
|
||||||
|
assert xnor_gate(1, 1) == 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(xnor_gate(0, 0))
|
||||||
|
print(xnor_gate(0, 1))
|
||||||
|
print(xnor_gate(1, 0))
|
||||||
|
print(xnor_gate(1, 1))
|
46
boolean_algebra/xor_gate.py
Normal file
46
boolean_algebra/xor_gate.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
"""
|
||||||
|
A XOR Gate is a logic gate in boolean algebra which results to 1 (True) if only one of
|
||||||
|
the two inputs is 1, and 0 (False) if an even number of inputs are 1.
|
||||||
|
Following is the truth table of a XOR Gate:
|
||||||
|
------------------------------
|
||||||
|
| Input 1 | Input 2 | Output |
|
||||||
|
------------------------------
|
||||||
|
| 0 | 0 | 0 |
|
||||||
|
| 0 | 1 | 1 |
|
||||||
|
| 1 | 0 | 1 |
|
||||||
|
| 1 | 1 | 0 |
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def xor_gate(input_1: int, input_2: int) -> int:
|
||||||
|
"""
|
||||||
|
calculate xor of the input values
|
||||||
|
|
||||||
|
>>> xor_gate(0, 0)
|
||||||
|
0
|
||||||
|
>>> xor_gate(0, 1)
|
||||||
|
1
|
||||||
|
>>> xor_gate(1, 0)
|
||||||
|
1
|
||||||
|
>>> xor_gate(1, 1)
|
||||||
|
0
|
||||||
|
"""
|
||||||
|
return (input_1, input_2).count(0) % 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_xor_gate() -> None:
|
||||||
|
"""
|
||||||
|
Tests the xor_gate function
|
||||||
|
"""
|
||||||
|
assert xor_gate(0, 0) == 0
|
||||||
|
assert xor_gate(0, 1) == 1
|
||||||
|
assert xor_gate(1, 0) == 1
|
||||||
|
assert xor_gate(1, 1) == 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(xor_gate(0, 0))
|
||||||
|
print(xor_gate(0, 1))
|
@ -1,4 +1,8 @@
|
|||||||
# Cellular Automata
|
# Cellular Automata
|
||||||
|
|
||||||
* https://en.wikipedia.org/wiki/Cellular_automaton
|
Cellular automata are a way to simulate the behavior of "life", no matter if it is a robot or cell.
|
||||||
* https://mathworld.wolfram.com/ElementaryCellularAutomaton.html
|
They usually follow simple rules but can lead to the creation of complex forms.
|
||||||
|
The most popular cellular automaton is Conway's [Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life).
|
||||||
|
|
||||||
|
* <https://en.wikipedia.org/wiki/Cellular_automaton>
|
||||||
|
* <https://mathworld.wolfram.com/ElementaryCellularAutomaton.html>
|
||||||
|
@ -10,7 +10,7 @@ Python:
|
|||||||
- 3.5
|
- 3.5
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
- $python3 game_o_life <canvas_size:int>
|
- $python3 game_of_life <canvas_size:int>
|
||||||
|
|
||||||
Game-Of-Life Rules:
|
Game-Of-Life Rules:
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ import numpy as np
|
|||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
from matplotlib.colors import ListedColormap
|
from matplotlib.colors import ListedColormap
|
||||||
|
|
||||||
usage_doc = "Usage of script: script_nama <size_of_canvas:int>"
|
usage_doc = "Usage of script: script_name <size_of_canvas:int>"
|
||||||
|
|
||||||
choice = [0] * 100 + [1] * 10
|
choice = [0] * 100 + [1] * 10
|
||||||
random.shuffle(choice)
|
random.shuffle(choice)
|
||||||
@ -52,7 +52,8 @@ def seed(canvas: list[list[bool]]) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def run(canvas: list[list[bool]]) -> list[list[bool]]:
|
def run(canvas: list[list[bool]]) -> list[list[bool]]:
|
||||||
"""This function runs the rules of game through all points, and changes their
|
"""
|
||||||
|
This function runs the rules of game through all points, and changes their
|
||||||
status accordingly.(in the same canvas)
|
status accordingly.(in the same canvas)
|
||||||
@Args:
|
@Args:
|
||||||
--
|
--
|
||||||
@ -60,21 +61,17 @@ def run(canvas: list[list[bool]]) -> list[list[bool]]:
|
|||||||
|
|
||||||
@returns:
|
@returns:
|
||||||
--
|
--
|
||||||
None
|
canvas of population after one step
|
||||||
"""
|
"""
|
||||||
current_canvas = np.array(canvas)
|
current_canvas = np.array(canvas)
|
||||||
next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
|
next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
|
||||||
for r, row in enumerate(current_canvas):
|
for r, row in enumerate(current_canvas):
|
||||||
for c, pt in enumerate(row):
|
for c, pt in enumerate(row):
|
||||||
# print(r-1,r+2,c-1,c+2)
|
|
||||||
next_gen_canvas[r][c] = __judge_point(
|
next_gen_canvas[r][c] = __judge_point(
|
||||||
pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2]
|
pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2]
|
||||||
)
|
)
|
||||||
|
|
||||||
current_canvas = next_gen_canvas
|
return next_gen_canvas.tolist()
|
||||||
del next_gen_canvas # cleaning memory as we move on.
|
|
||||||
return_canvas: list[list[bool]] = current_canvas.tolist()
|
|
||||||
return return_canvas
|
|
||||||
|
|
||||||
|
|
||||||
def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
|
def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
|
||||||
@ -99,7 +96,7 @@ def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
|
|||||||
if pt:
|
if pt:
|
||||||
if alive < 2:
|
if alive < 2:
|
||||||
state = False
|
state = False
|
||||||
elif alive == 2 or alive == 3:
|
elif alive in {2, 3}:
|
||||||
state = True
|
state = True
|
||||||
elif alive > 3:
|
elif alive > 3:
|
||||||
state = False
|
state = False
|
||||||
|
@ -45,8 +45,7 @@ def construct_highway(
|
|||||||
|
|
||||||
highway = [[-1] * number_of_cells] # Create a highway without any car
|
highway = [[-1] * number_of_cells] # Create a highway without any car
|
||||||
i = 0
|
i = 0
|
||||||
if initial_speed < 0:
|
initial_speed = max(initial_speed, 0)
|
||||||
initial_speed = 0
|
|
||||||
while i < number_of_cells:
|
while i < number_of_cells:
|
||||||
highway[0][i] = (
|
highway[0][i] = (
|
||||||
randint(0, max_speed) if random_speed else initial_speed
|
randint(0, max_speed) if random_speed else initial_speed
|
||||||
|
550
cellular_automata/wa_tor.py
Normal file
550
cellular_automata/wa_tor.py
Normal file
@ -0,0 +1,550 @@
|
|||||||
|
"""
|
||||||
|
Wa-Tor algorithm (1984)
|
||||||
|
|
||||||
|
@ https://en.wikipedia.org/wiki/Wa-Tor
|
||||||
|
@ https://beltoforion.de/en/wator/
|
||||||
|
@ https://beltoforion.de/en/wator/images/wator_medium.webm
|
||||||
|
|
||||||
|
This solution aims to completely remove any systematic approach
|
||||||
|
to the Wa-Tor planet, and utilise fully random methods.
|
||||||
|
|
||||||
|
The constants are a working set that allows the Wa-Tor planet
|
||||||
|
to result in one of the three possible results.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from random import randint, shuffle
|
||||||
|
from time import sleep
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
WIDTH = 50 # Width of the Wa-Tor planet
|
||||||
|
HEIGHT = 50 # Height of the Wa-Tor planet
|
||||||
|
|
||||||
|
PREY_INITIAL_COUNT = 30 # The initial number of prey entities
|
||||||
|
PREY_REPRODUCTION_TIME = 5 # The chronons before reproducing
|
||||||
|
|
||||||
|
PREDATOR_INITIAL_COUNT = 50 # The initial number of predator entities
|
||||||
|
# The initial energy value of predator entities
|
||||||
|
PREDATOR_INITIAL_ENERGY_VALUE = 15
|
||||||
|
# The energy value provided when consuming prey
|
||||||
|
PREDATOR_FOOD_VALUE = 5
|
||||||
|
PREDATOR_REPRODUCTION_TIME = 20 # The chronons before reproducing
|
||||||
|
|
||||||
|
MAX_ENTITIES = 500 # The max number of organisms on the board
|
||||||
|
# The number of entities to delete from the unbalanced side
|
||||||
|
DELETE_UNBALANCED_ENTITIES = 50
|
||||||
|
|
||||||
|
|
||||||
|
class Entity:
|
||||||
|
"""
|
||||||
|
Represents an entity (either prey or predator).
|
||||||
|
|
||||||
|
>>> e = Entity(True, coords=(0, 0))
|
||||||
|
>>> e.prey
|
||||||
|
True
|
||||||
|
>>> e.coords
|
||||||
|
(0, 0)
|
||||||
|
>>> e.alive
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, prey: bool, coords: tuple[int, int]) -> None:
|
||||||
|
self.prey = prey
|
||||||
|
# The (row, col) pos of the entity
|
||||||
|
self.coords = coords
|
||||||
|
|
||||||
|
self.remaining_reproduction_time = (
|
||||||
|
PREY_REPRODUCTION_TIME if prey else PREDATOR_REPRODUCTION_TIME
|
||||||
|
)
|
||||||
|
self.energy_value = None if prey is True else PREDATOR_INITIAL_ENERGY_VALUE
|
||||||
|
self.alive = True
|
||||||
|
|
||||||
|
def reset_reproduction_time(self) -> None:
|
||||||
|
"""
|
||||||
|
>>> e = Entity(True, coords=(0, 0))
|
||||||
|
>>> e.reset_reproduction_time()
|
||||||
|
>>> e.remaining_reproduction_time == PREY_REPRODUCTION_TIME
|
||||||
|
True
|
||||||
|
>>> e = Entity(False, coords=(0, 0))
|
||||||
|
>>> e.reset_reproduction_time()
|
||||||
|
>>> e.remaining_reproduction_time == PREDATOR_REPRODUCTION_TIME
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
self.remaining_reproduction_time = (
|
||||||
|
PREY_REPRODUCTION_TIME if self.prey is True else PREDATOR_REPRODUCTION_TIME
|
||||||
|
)
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
"""
|
||||||
|
>>> Entity(prey=True, coords=(1, 1))
|
||||||
|
Entity(prey=True, coords=(1, 1), remaining_reproduction_time=5)
|
||||||
|
>>> Entity(prey=False, coords=(2, 1)) # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
Entity(prey=False, coords=(2, 1),
|
||||||
|
remaining_reproduction_time=20, energy_value=15)
|
||||||
|
"""
|
||||||
|
repr_ = (
|
||||||
|
f"Entity(prey={self.prey}, coords={self.coords}, "
|
||||||
|
f"remaining_reproduction_time={self.remaining_reproduction_time}"
|
||||||
|
)
|
||||||
|
if self.energy_value is not None:
|
||||||
|
repr_ += f", energy_value={self.energy_value}"
|
||||||
|
return f"{repr_})"
|
||||||
|
|
||||||
|
|
||||||
|
class WaTor:
|
||||||
|
"""
|
||||||
|
Represents the main Wa-Tor algorithm.
|
||||||
|
|
||||||
|
:attr time_passed: A function that is called every time
|
||||||
|
time passes (a chronon) in order to visually display
|
||||||
|
the new Wa-Tor planet. The time_passed function can block
|
||||||
|
using time.sleep to slow the algorithm progression.
|
||||||
|
|
||||||
|
>>> wt = WaTor(10, 15)
|
||||||
|
>>> wt.width
|
||||||
|
10
|
||||||
|
>>> wt.height
|
||||||
|
15
|
||||||
|
>>> len(wt.planet)
|
||||||
|
15
|
||||||
|
>>> len(wt.planet[0])
|
||||||
|
10
|
||||||
|
>>> len(wt.get_entities()) == PREDATOR_INITIAL_COUNT + PREY_INITIAL_COUNT
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
|
||||||
|
time_passed: Callable[["WaTor", int], None] | None
|
||||||
|
|
||||||
|
def __init__(self, width: int, height: int) -> None:
|
||||||
|
self.width = width
|
||||||
|
self.height = height
|
||||||
|
self.time_passed = None
|
||||||
|
|
||||||
|
self.planet: list[list[Entity | None]] = [[None] * width for _ in range(height)]
|
||||||
|
|
||||||
|
# Populate planet with predators and prey randomly
|
||||||
|
for _ in range(PREY_INITIAL_COUNT):
|
||||||
|
self.add_entity(prey=True)
|
||||||
|
for _ in range(PREDATOR_INITIAL_COUNT):
|
||||||
|
self.add_entity(prey=False)
|
||||||
|
self.set_planet(self.planet)
|
||||||
|
|
||||||
|
def set_planet(self, planet: list[list[Entity | None]]) -> None:
|
||||||
|
"""
|
||||||
|
Ease of access for testing
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> planet = [
|
||||||
|
... [None, None, None],
|
||||||
|
... [None, Entity(True, coords=(1, 1)), None]
|
||||||
|
... ]
|
||||||
|
>>> wt.set_planet(planet)
|
||||||
|
>>> wt.planet == planet
|
||||||
|
True
|
||||||
|
>>> wt.width
|
||||||
|
3
|
||||||
|
>>> wt.height
|
||||||
|
2
|
||||||
|
"""
|
||||||
|
self.planet = planet
|
||||||
|
self.width = len(planet[0])
|
||||||
|
self.height = len(planet)
|
||||||
|
|
||||||
|
def add_entity(self, prey: bool) -> None:
|
||||||
|
"""
|
||||||
|
Adds an entity, making sure the entity does
|
||||||
|
not override another entity
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> wt.set_planet([[None, None], [None, None]])
|
||||||
|
>>> wt.add_entity(True)
|
||||||
|
>>> len(wt.get_entities())
|
||||||
|
1
|
||||||
|
>>> wt.add_entity(False)
|
||||||
|
>>> len(wt.get_entities())
|
||||||
|
2
|
||||||
|
"""
|
||||||
|
while True:
|
||||||
|
row, col = randint(0, self.height - 1), randint(0, self.width - 1)
|
||||||
|
if self.planet[row][col] is None:
|
||||||
|
self.planet[row][col] = Entity(prey=prey, coords=(row, col))
|
||||||
|
return
|
||||||
|
|
||||||
|
def get_entities(self) -> list[Entity]:
|
||||||
|
"""
|
||||||
|
Returns a list of all the entities within the planet.
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> len(wt.get_entities()) == PREDATOR_INITIAL_COUNT + PREY_INITIAL_COUNT
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
return [entity for column in self.planet for entity in column if entity]
|
||||||
|
|
||||||
|
def balance_predators_and_prey(self) -> None:
|
||||||
|
"""
|
||||||
|
Balances predators and preys so that prey
|
||||||
|
can not dominate the predators, blocking up
|
||||||
|
space for them to reproduce.
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> for i in range(2000):
|
||||||
|
... row, col = i // HEIGHT, i % WIDTH
|
||||||
|
... wt.planet[row][col] = Entity(True, coords=(row, col))
|
||||||
|
>>> entities = len(wt.get_entities())
|
||||||
|
>>> wt.balance_predators_and_prey()
|
||||||
|
>>> len(wt.get_entities()) == entities
|
||||||
|
False
|
||||||
|
"""
|
||||||
|
entities = self.get_entities()
|
||||||
|
shuffle(entities)
|
||||||
|
|
||||||
|
if len(entities) >= MAX_ENTITIES - MAX_ENTITIES / 10:
|
||||||
|
prey = [entity for entity in entities if entity.prey]
|
||||||
|
predators = [entity for entity in entities if not entity.prey]
|
||||||
|
|
||||||
|
prey_count, predator_count = len(prey), len(predators)
|
||||||
|
|
||||||
|
entities_to_purge = (
|
||||||
|
prey[:DELETE_UNBALANCED_ENTITIES]
|
||||||
|
if prey_count > predator_count
|
||||||
|
else predators[:DELETE_UNBALANCED_ENTITIES]
|
||||||
|
)
|
||||||
|
for entity in entities_to_purge:
|
||||||
|
self.planet[entity.coords[0]][entity.coords[1]] = None
|
||||||
|
|
||||||
|
def get_surrounding_prey(self, entity: Entity) -> list[Entity]:
|
||||||
|
"""
|
||||||
|
Returns all the prey entities around (N, S, E, W) a predator entity.
|
||||||
|
|
||||||
|
Subtly different to the try_to_move_to_unoccupied square.
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> wt.set_planet([
|
||||||
|
... [None, Entity(True, (0, 1)), None],
|
||||||
|
... [None, Entity(False, (1, 1)), None],
|
||||||
|
... [None, Entity(True, (2, 1)), None]])
|
||||||
|
>>> wt.get_surrounding_prey(
|
||||||
|
... Entity(False, (1, 1))) # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
[Entity(prey=True, coords=(0, 1), remaining_reproduction_time=5),
|
||||||
|
Entity(prey=True, coords=(2, 1), remaining_reproduction_time=5)]
|
||||||
|
>>> wt.set_planet([[Entity(False, (0, 0))]])
|
||||||
|
>>> wt.get_surrounding_prey(Entity(False, (0, 0)))
|
||||||
|
[]
|
||||||
|
>>> wt.set_planet([
|
||||||
|
... [Entity(True, (0, 0)), Entity(False, (1, 0)), Entity(False, (2, 0))],
|
||||||
|
... [None, Entity(False, (1, 1)), Entity(True, (2, 1))],
|
||||||
|
... [None, None, None]])
|
||||||
|
>>> wt.get_surrounding_prey(Entity(False, (1, 0)))
|
||||||
|
[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5)]
|
||||||
|
"""
|
||||||
|
row, col = entity.coords
|
||||||
|
adjacent: list[tuple[int, int]] = [
|
||||||
|
(row - 1, col), # North
|
||||||
|
(row + 1, col), # South
|
||||||
|
(row, col - 1), # West
|
||||||
|
(row, col + 1), # East
|
||||||
|
]
|
||||||
|
|
||||||
|
return [
|
||||||
|
ent
|
||||||
|
for r, c in adjacent
|
||||||
|
if 0 <= r < self.height
|
||||||
|
and 0 <= c < self.width
|
||||||
|
and (ent := self.planet[r][c]) is not None
|
||||||
|
and ent.prey
|
||||||
|
]
|
||||||
|
|
||||||
|
def move_and_reproduce(
|
||||||
|
self, entity: Entity, direction_orders: list[Literal["N", "E", "S", "W"]]
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Attempts to move to an unoccupied neighbouring square
|
||||||
|
in either of the four directions (North, South, East, West).
|
||||||
|
If the move was successful and the remaining_reproduction time is
|
||||||
|
equal to 0, then a new prey or predator can also be created
|
||||||
|
in the previous square.
|
||||||
|
|
||||||
|
:param direction_orders: Ordered list (like priority queue) depicting
|
||||||
|
order to attempt to move. Removes any systematic
|
||||||
|
approach of checking neighbouring squares.
|
||||||
|
|
||||||
|
>>> planet = [
|
||||||
|
... [None, None, None],
|
||||||
|
... [None, Entity(True, coords=(1, 1)), None],
|
||||||
|
... [None, None, None]
|
||||||
|
... ]
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> wt.set_planet(planet)
|
||||||
|
>>> wt.move_and_reproduce(Entity(True, coords=(1, 1)), direction_orders=["N"])
|
||||||
|
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
[[None, Entity(prey=True, coords=(0, 1), remaining_reproduction_time=4), None],
|
||||||
|
[None, None, None],
|
||||||
|
[None, None, None]]
|
||||||
|
>>> wt.planet[0][0] = Entity(True, coords=(0, 0))
|
||||||
|
>>> wt.move_and_reproduce(Entity(True, coords=(0, 1)),
|
||||||
|
... direction_orders=["N", "W", "E", "S"])
|
||||||
|
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
[[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5), None,
|
||||||
|
Entity(prey=True, coords=(0, 2), remaining_reproduction_time=4)],
|
||||||
|
[None, None, None],
|
||||||
|
[None, None, None]]
|
||||||
|
>>> wt.planet[0][1] = wt.planet[0][2]
|
||||||
|
>>> wt.planet[0][2] = None
|
||||||
|
>>> wt.move_and_reproduce(Entity(True, coords=(0, 1)),
|
||||||
|
... direction_orders=["N", "W", "S", "E"])
|
||||||
|
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
[[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5), None, None],
|
||||||
|
[None, Entity(prey=True, coords=(1, 1), remaining_reproduction_time=4), None],
|
||||||
|
[None, None, None]]
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> reproducable_entity = Entity(False, coords=(0, 1))
|
||||||
|
>>> reproducable_entity.remaining_reproduction_time = 0
|
||||||
|
>>> wt.planet = [[None, reproducable_entity]]
|
||||||
|
>>> wt.move_and_reproduce(reproducable_entity,
|
||||||
|
... direction_orders=["N", "W", "S", "E"])
|
||||||
|
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
[[Entity(prey=False, coords=(0, 0),
|
||||||
|
remaining_reproduction_time=20, energy_value=15),
|
||||||
|
Entity(prey=False, coords=(0, 1), remaining_reproduction_time=20,
|
||||||
|
energy_value=15)]]
|
||||||
|
"""
|
||||||
|
row, col = coords = entity.coords
|
||||||
|
|
||||||
|
adjacent_squares: dict[Literal["N", "E", "S", "W"], tuple[int, int]] = {
|
||||||
|
"N": (row - 1, col), # North
|
||||||
|
"S": (row + 1, col), # South
|
||||||
|
"W": (row, col - 1), # West
|
||||||
|
"E": (row, col + 1), # East
|
||||||
|
}
|
||||||
|
# Weight adjacent locations
|
||||||
|
adjacent: list[tuple[int, int]] = []
|
||||||
|
for order in direction_orders:
|
||||||
|
adjacent.append(adjacent_squares[order])
|
||||||
|
|
||||||
|
for r, c in adjacent:
|
||||||
|
if (
|
||||||
|
0 <= r < self.height
|
||||||
|
and 0 <= c < self.width
|
||||||
|
and self.planet[r][c] is None
|
||||||
|
):
|
||||||
|
# Move entity to empty adjacent square
|
||||||
|
self.planet[r][c] = entity
|
||||||
|
self.planet[row][col] = None
|
||||||
|
entity.coords = (r, c)
|
||||||
|
break
|
||||||
|
|
||||||
|
# (2.) See if it possible to reproduce in previous square
|
||||||
|
if coords != entity.coords and entity.remaining_reproduction_time <= 0:
|
||||||
|
# Check if the entities on the planet is less than the max limit
|
||||||
|
if len(self.get_entities()) < MAX_ENTITIES:
|
||||||
|
# Reproduce in previous square
|
||||||
|
self.planet[row][col] = Entity(prey=entity.prey, coords=coords)
|
||||||
|
entity.reset_reproduction_time()
|
||||||
|
else:
|
||||||
|
entity.remaining_reproduction_time -= 1
|
||||||
|
|
||||||
|
def perform_prey_actions(
|
||||||
|
self, entity: Entity, direction_orders: list[Literal["N", "E", "S", "W"]]
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Performs the actions for a prey entity
|
||||||
|
|
||||||
|
For prey the rules are:
|
||||||
|
1. At each chronon, a prey moves randomly to one of the adjacent unoccupied
|
||||||
|
squares. If there are no free squares, no movement takes place.
|
||||||
|
2. Once a prey has survived a certain number of chronons it may reproduce.
|
||||||
|
This is done as it moves to a neighbouring square,
|
||||||
|
leaving behind a new prey in its old position.
|
||||||
|
Its reproduction time is also reset to zero.
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> reproducable_entity = Entity(True, coords=(0, 1))
|
||||||
|
>>> reproducable_entity.remaining_reproduction_time = 0
|
||||||
|
>>> wt.planet = [[None, reproducable_entity]]
|
||||||
|
>>> wt.perform_prey_actions(reproducable_entity,
|
||||||
|
... direction_orders=["N", "W", "S", "E"])
|
||||||
|
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
[[Entity(prey=True, coords=(0, 0), remaining_reproduction_time=5),
|
||||||
|
Entity(prey=True, coords=(0, 1), remaining_reproduction_time=5)]]
|
||||||
|
"""
|
||||||
|
self.move_and_reproduce(entity, direction_orders)
|
||||||
|
|
||||||
|
def perform_predator_actions(
|
||||||
|
self,
|
||||||
|
entity: Entity,
|
||||||
|
occupied_by_prey_coords: tuple[int, int] | None,
|
||||||
|
direction_orders: list[Literal["N", "E", "S", "W"]],
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Performs the actions for a predator entity
|
||||||
|
|
||||||
|
:param occupied_by_prey_coords: Move to this location if there is prey there
|
||||||
|
|
||||||
|
For predators the rules are:
|
||||||
|
1. At each chronon, a predator moves randomly to an adjacent square occupied
|
||||||
|
by a prey. If there is none, the predator moves to a random adjacent
|
||||||
|
unoccupied square. If there are no free squares, no movement takes place.
|
||||||
|
2. At each chronon, each predator is deprived of a unit of energy.
|
||||||
|
3. Upon reaching zero energy, a predator dies.
|
||||||
|
4. If a predator moves to a square occupied by a prey,
|
||||||
|
it eats the prey and earns a certain amount of energy.
|
||||||
|
5. Once a predator has survived a certain number of chronons
|
||||||
|
it may reproduce in exactly the same way as the prey.
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> wt.set_planet([[Entity(True, coords=(0, 0)), Entity(False, coords=(0, 1))]])
|
||||||
|
>>> wt.perform_predator_actions(Entity(False, coords=(0, 1)), (0, 0), [])
|
||||||
|
>>> wt.planet # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
[[Entity(prey=False, coords=(0, 0),
|
||||||
|
remaining_reproduction_time=20, energy_value=19), None]]
|
||||||
|
"""
|
||||||
|
assert entity.energy_value is not None # [type checking]
|
||||||
|
|
||||||
|
# (3.) If the entity has 0 energy, it will die
|
||||||
|
if entity.energy_value == 0:
|
||||||
|
self.planet[entity.coords[0]][entity.coords[1]] = None
|
||||||
|
return
|
||||||
|
|
||||||
|
# (1.) Move to entity if possible
|
||||||
|
if occupied_by_prey_coords is not None:
|
||||||
|
# Kill the prey
|
||||||
|
prey = self.planet[occupied_by_prey_coords[0]][occupied_by_prey_coords[1]]
|
||||||
|
assert prey is not None
|
||||||
|
prey.alive = False
|
||||||
|
|
||||||
|
# Move onto prey
|
||||||
|
self.planet[occupied_by_prey_coords[0]][occupied_by_prey_coords[1]] = entity
|
||||||
|
self.planet[entity.coords[0]][entity.coords[1]] = None
|
||||||
|
|
||||||
|
entity.coords = occupied_by_prey_coords
|
||||||
|
# (4.) Eats the prey and earns energy
|
||||||
|
entity.energy_value += PREDATOR_FOOD_VALUE
|
||||||
|
else:
|
||||||
|
# (5.) If it has survived the certain number of chronons it will also
|
||||||
|
# reproduce in this function
|
||||||
|
self.move_and_reproduce(entity, direction_orders)
|
||||||
|
|
||||||
|
# (2.) Each chronon, the predator is deprived of a unit of energy
|
||||||
|
entity.energy_value -= 1
|
||||||
|
|
||||||
|
def run(self, *, iteration_count: int) -> None:
|
||||||
|
"""
|
||||||
|
Emulate time passing by looping iteration_count times
|
||||||
|
|
||||||
|
>>> wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
>>> wt.run(iteration_count=PREDATOR_INITIAL_ENERGY_VALUE - 1)
|
||||||
|
>>> len(list(filter(lambda entity: entity.prey is False,
|
||||||
|
... wt.get_entities()))) >= PREDATOR_INITIAL_COUNT
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
for iter_num in range(iteration_count):
|
||||||
|
# Generate list of all entities in order to randomly
|
||||||
|
# pop an entity at a time to simulate true randomness
|
||||||
|
# This removes the systematic approach of iterating
|
||||||
|
# through each entity width by height
|
||||||
|
all_entities = self.get_entities()
|
||||||
|
|
||||||
|
for __ in range(len(all_entities)):
|
||||||
|
entity = all_entities.pop(randint(0, len(all_entities) - 1))
|
||||||
|
if entity.alive is False:
|
||||||
|
continue
|
||||||
|
|
||||||
|
directions: list[Literal["N", "E", "S", "W"]] = ["N", "E", "S", "W"]
|
||||||
|
shuffle(directions) # Randomly shuffle directions
|
||||||
|
|
||||||
|
if entity.prey:
|
||||||
|
self.perform_prey_actions(entity, directions)
|
||||||
|
else:
|
||||||
|
# Create list of surrounding prey
|
||||||
|
surrounding_prey = self.get_surrounding_prey(entity)
|
||||||
|
surrounding_prey_coords = None
|
||||||
|
|
||||||
|
if surrounding_prey:
|
||||||
|
# Again, randomly shuffle directions
|
||||||
|
shuffle(surrounding_prey)
|
||||||
|
surrounding_prey_coords = surrounding_prey[0].coords
|
||||||
|
|
||||||
|
self.perform_predator_actions(
|
||||||
|
entity, surrounding_prey_coords, directions
|
||||||
|
)
|
||||||
|
|
||||||
|
# Balance out the predators and prey
|
||||||
|
self.balance_predators_and_prey()
|
||||||
|
|
||||||
|
if self.time_passed is not None:
|
||||||
|
# Call time_passed function for Wa-Tor planet
|
||||||
|
# visualisation in a terminal or a graph.
|
||||||
|
self.time_passed(self, iter_num)
|
||||||
|
|
||||||
|
|
||||||
|
def visualise(wt: WaTor, iter_number: int, *, colour: bool = True) -> None:
|
||||||
|
"""
|
||||||
|
Visually displays the Wa-Tor planet using
|
||||||
|
an ascii code in terminal to clear and re-print
|
||||||
|
the Wa-Tor planet at intervals.
|
||||||
|
|
||||||
|
Uses ascii colour codes to colourfully display
|
||||||
|
the predators and prey.
|
||||||
|
|
||||||
|
(0x60f197) Prey = #
|
||||||
|
(0xfffff) Predator = x
|
||||||
|
|
||||||
|
>>> wt = WaTor(30, 30)
|
||||||
|
>>> wt.set_planet([
|
||||||
|
... [Entity(True, coords=(0, 0)), Entity(False, coords=(0, 1)), None],
|
||||||
|
... [Entity(False, coords=(1, 0)), None, Entity(False, coords=(1, 2))],
|
||||||
|
... [None, Entity(True, coords=(2, 1)), None]
|
||||||
|
... ])
|
||||||
|
>>> visualise(wt, 0, colour=False) # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
# x .
|
||||||
|
x . x
|
||||||
|
. # .
|
||||||
|
<BLANKLINE>
|
||||||
|
Iteration: 0 | Prey count: 2 | Predator count: 3 |
|
||||||
|
"""
|
||||||
|
if colour:
|
||||||
|
__import__("os").system("")
|
||||||
|
print("\x1b[0;0H\x1b[2J\x1b[?25l")
|
||||||
|
|
||||||
|
reprint = "\x1b[0;0H" if colour else ""
|
||||||
|
ansi_colour_end = "\x1b[0m " if colour else " "
|
||||||
|
|
||||||
|
planet = wt.planet
|
||||||
|
output = ""
|
||||||
|
|
||||||
|
# Iterate over every entity in the planet
|
||||||
|
for row in planet:
|
||||||
|
for entity in row:
|
||||||
|
if entity is None:
|
||||||
|
output += " . "
|
||||||
|
else:
|
||||||
|
if colour is True:
|
||||||
|
output += (
|
||||||
|
"\x1b[38;2;96;241;151m"
|
||||||
|
if entity.prey
|
||||||
|
else "\x1b[38;2;255;255;15m"
|
||||||
|
)
|
||||||
|
output += f" {'#' if entity.prey else 'x'}{ansi_colour_end}"
|
||||||
|
|
||||||
|
output += "\n"
|
||||||
|
|
||||||
|
entities = wt.get_entities()
|
||||||
|
prey_count = sum(entity.prey for entity in entities)
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"{output}\n Iteration: {iter_number} | Prey count: {prey_count} | "
|
||||||
|
f"Predator count: {len(entities) - prey_count} | {reprint}"
|
||||||
|
)
|
||||||
|
# Block the thread to be able to visualise seeing the algorithm
|
||||||
|
sleep(0.05)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
|
||||||
|
wt = WaTor(WIDTH, HEIGHT)
|
||||||
|
wt.time_passed = visualise
|
||||||
|
wt.run(iteration_count=100_000)
|
7
ciphers/README.md
Normal file
7
ciphers/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# Ciphers
|
||||||
|
|
||||||
|
Ciphers are used to protect data from people that are not allowed to have it. They are everywhere on the internet to protect your connections.
|
||||||
|
|
||||||
|
* <https://en.wikipedia.org/wiki/Cipher>
|
||||||
|
* <http://practicalcryptography.com/ciphers/>
|
||||||
|
* <https://practicalcryptography.com/ciphers/classical-era/>
|
@ -9,26 +9,26 @@ SYMBOLS = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def check_keys(keyA: int, keyB: int, mode: str) -> None:
|
def check_keys(key_a: int, key_b: int, mode: str) -> None:
|
||||||
if mode == "encrypt":
|
if mode == "encrypt":
|
||||||
if keyA == 1:
|
if key_a == 1:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"The affine cipher becomes weak when key "
|
"The affine cipher becomes weak when key "
|
||||||
"A is set to 1. Choose different key"
|
"A is set to 1. Choose different key"
|
||||||
)
|
)
|
||||||
if keyB == 0:
|
if key_b == 0:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"The affine cipher becomes weak when key "
|
"The affine cipher becomes weak when key "
|
||||||
"B is set to 0. Choose different key"
|
"B is set to 0. Choose different key"
|
||||||
)
|
)
|
||||||
if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1:
|
if key_a < 0 or key_b < 0 or key_b > len(SYMBOLS) - 1:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"Key A must be greater than 0 and key B must "
|
"Key A must be greater than 0 and key B must "
|
||||||
f"be between 0 and {len(SYMBOLS) - 1}."
|
f"be between 0 and {len(SYMBOLS) - 1}."
|
||||||
)
|
)
|
||||||
if cryptomath.gcd(keyA, len(SYMBOLS)) != 1:
|
if cryptomath.gcd(key_a, len(SYMBOLS)) != 1:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
f"Key A {keyA} and the symbol set size {len(SYMBOLS)} "
|
f"Key A {key_a} and the symbol set size {len(SYMBOLS)} "
|
||||||
"are not relatively prime. Choose a different key."
|
"are not relatively prime. Choose a different key."
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,16 +39,16 @@ def encrypt_message(key: int, message: str) -> str:
|
|||||||
... 'substitution cipher.')
|
... 'substitution cipher.')
|
||||||
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
|
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
|
||||||
"""
|
"""
|
||||||
keyA, keyB = divmod(key, len(SYMBOLS))
|
key_a, key_b = divmod(key, len(SYMBOLS))
|
||||||
check_keys(keyA, keyB, "encrypt")
|
check_keys(key_a, key_b, "encrypt")
|
||||||
cipherText = ""
|
cipher_text = ""
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
if symbol in SYMBOLS:
|
if symbol in SYMBOLS:
|
||||||
symIndex = SYMBOLS.find(symbol)
|
sym_index = SYMBOLS.find(symbol)
|
||||||
cipherText += SYMBOLS[(symIndex * keyA + keyB) % len(SYMBOLS)]
|
cipher_text += SYMBOLS[(sym_index * key_a + key_b) % len(SYMBOLS)]
|
||||||
else:
|
else:
|
||||||
cipherText += symbol
|
cipher_text += symbol
|
||||||
return cipherText
|
return cipher_text
|
||||||
|
|
||||||
|
|
||||||
def decrypt_message(key: int, message: str) -> str:
|
def decrypt_message(key: int, message: str) -> str:
|
||||||
@ -57,25 +57,27 @@ def decrypt_message(key: int, message: str) -> str:
|
|||||||
... '{xIp~{HL}Gi')
|
... '{xIp~{HL}Gi')
|
||||||
'The affine cipher is a type of monoalphabetic substitution cipher.'
|
'The affine cipher is a type of monoalphabetic substitution cipher.'
|
||||||
"""
|
"""
|
||||||
keyA, keyB = divmod(key, len(SYMBOLS))
|
key_a, key_b = divmod(key, len(SYMBOLS))
|
||||||
check_keys(keyA, keyB, "decrypt")
|
check_keys(key_a, key_b, "decrypt")
|
||||||
plainText = ""
|
plain_text = ""
|
||||||
modInverseOfkeyA = cryptomath.find_mod_inverse(keyA, len(SYMBOLS))
|
mod_inverse_of_key_a = cryptomath.find_mod_inverse(key_a, len(SYMBOLS))
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
if symbol in SYMBOLS:
|
if symbol in SYMBOLS:
|
||||||
symIndex = SYMBOLS.find(symbol)
|
sym_index = SYMBOLS.find(symbol)
|
||||||
plainText += SYMBOLS[(symIndex - keyB) * modInverseOfkeyA % len(SYMBOLS)]
|
plain_text += SYMBOLS[
|
||||||
|
(sym_index - key_b) * mod_inverse_of_key_a % len(SYMBOLS)
|
||||||
|
]
|
||||||
else:
|
else:
|
||||||
plainText += symbol
|
plain_text += symbol
|
||||||
return plainText
|
return plain_text
|
||||||
|
|
||||||
|
|
||||||
def get_random_key() -> int:
|
def get_random_key() -> int:
|
||||||
while True:
|
while True:
|
||||||
keyA = random.randint(2, len(SYMBOLS))
|
key_b = random.randint(2, len(SYMBOLS))
|
||||||
keyB = random.randint(2, len(SYMBOLS))
|
key_b = random.randint(2, len(SYMBOLS))
|
||||||
if cryptomath.gcd(keyA, len(SYMBOLS)) == 1 and keyB % len(SYMBOLS) != 0:
|
if cryptomath.gcd(key_b, len(SYMBOLS)) == 1 and key_b % len(SYMBOLS) != 0:
|
||||||
return keyA * len(SYMBOLS) + keyB
|
return key_b * len(SYMBOLS) + key_b
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
@ -38,26 +38,13 @@ def atbash(sequence: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def benchmark() -> None:
|
def benchmark() -> None:
|
||||||
"""Let's benchmark them side-by-side..."""
|
"""Let's benchmark our functions side-by-side..."""
|
||||||
from timeit import timeit
|
from timeit import timeit
|
||||||
|
|
||||||
print("Running performance benchmarks...")
|
print("Running performance benchmarks...")
|
||||||
print(
|
setup = "from string import printable ; from __main__ import atbash, atbash_slow"
|
||||||
"> atbash_slow()",
|
print(f"> atbash_slow(): {timeit('atbash_slow(printable)', setup=setup)} seconds")
|
||||||
timeit(
|
print(f"> atbash(): {timeit('atbash(printable)', setup=setup)} seconds")
|
||||||
"atbash_slow(printable)",
|
|
||||||
setup="from string import printable ; from __main__ import atbash_slow",
|
|
||||||
),
|
|
||||||
"seconds",
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
"> atbash()",
|
|
||||||
timeit(
|
|
||||||
"atbash(printable)",
|
|
||||||
setup="from string import printable ; from __main__ import atbash",
|
|
||||||
),
|
|
||||||
"seconds",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
131
ciphers/autokey.py
Normal file
131
ciphers/autokey.py
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
"""
|
||||||
|
https://en.wikipedia.org/wiki/Autokey_cipher
|
||||||
|
An autokey cipher (also known as the autoclave cipher) is a cipher that
|
||||||
|
incorporates the message (the plaintext) into the key.
|
||||||
|
The key is generated from the message in some automated fashion,
|
||||||
|
sometimes by selecting certain letters from the text or, more commonly,
|
||||||
|
by adding a short primer key to the front of the message.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def encrypt(plaintext: str, key: str) -> str:
|
||||||
|
"""
|
||||||
|
Encrypt a given plaintext (string) and key (string), returning the
|
||||||
|
encrypted ciphertext.
|
||||||
|
>>> encrypt("hello world", "coffee")
|
||||||
|
'jsqqs avvwo'
|
||||||
|
>>> encrypt("coffee is good as python", "TheAlgorithms")
|
||||||
|
'vvjfpk wj ohvp su ddylsv'
|
||||||
|
>>> encrypt("coffee is good as python", 2)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: key must be a string
|
||||||
|
>>> encrypt("", "TheAlgorithms")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: plaintext is empty
|
||||||
|
"""
|
||||||
|
if not isinstance(plaintext, str):
|
||||||
|
raise TypeError("plaintext must be a string")
|
||||||
|
if not isinstance(key, str):
|
||||||
|
raise TypeError("key must be a string")
|
||||||
|
|
||||||
|
if not plaintext:
|
||||||
|
raise ValueError("plaintext is empty")
|
||||||
|
if not key:
|
||||||
|
raise ValueError("key is empty")
|
||||||
|
|
||||||
|
key += plaintext
|
||||||
|
plaintext = plaintext.lower()
|
||||||
|
key = key.lower()
|
||||||
|
plaintext_iterator = 0
|
||||||
|
key_iterator = 0
|
||||||
|
ciphertext = ""
|
||||||
|
while plaintext_iterator < len(plaintext):
|
||||||
|
if (
|
||||||
|
ord(plaintext[plaintext_iterator]) < 97
|
||||||
|
or ord(plaintext[plaintext_iterator]) > 122
|
||||||
|
):
|
||||||
|
ciphertext += plaintext[plaintext_iterator]
|
||||||
|
plaintext_iterator += 1
|
||||||
|
elif ord(key[key_iterator]) < 97 or ord(key[key_iterator]) > 122:
|
||||||
|
key_iterator += 1
|
||||||
|
else:
|
||||||
|
ciphertext += chr(
|
||||||
|
(
|
||||||
|
(ord(plaintext[plaintext_iterator]) - 97 + ord(key[key_iterator]))
|
||||||
|
- 97
|
||||||
|
)
|
||||||
|
% 26
|
||||||
|
+ 97
|
||||||
|
)
|
||||||
|
key_iterator += 1
|
||||||
|
plaintext_iterator += 1
|
||||||
|
return ciphertext
|
||||||
|
|
||||||
|
|
||||||
|
def decrypt(ciphertext: str, key: str) -> str:
|
||||||
|
"""
|
||||||
|
Decrypt a given ciphertext (string) and key (string), returning the decrypted
|
||||||
|
ciphertext.
|
||||||
|
>>> decrypt("jsqqs avvwo", "coffee")
|
||||||
|
'hello world'
|
||||||
|
>>> decrypt("vvjfpk wj ohvp su ddylsv", "TheAlgorithms")
|
||||||
|
'coffee is good as python'
|
||||||
|
>>> decrypt("vvjfpk wj ohvp su ddylsv", "")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: key is empty
|
||||||
|
>>> decrypt(527.26, "TheAlgorithms")
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: ciphertext must be a string
|
||||||
|
"""
|
||||||
|
if not isinstance(ciphertext, str):
|
||||||
|
raise TypeError("ciphertext must be a string")
|
||||||
|
if not isinstance(key, str):
|
||||||
|
raise TypeError("key must be a string")
|
||||||
|
|
||||||
|
if not ciphertext:
|
||||||
|
raise ValueError("ciphertext is empty")
|
||||||
|
if not key:
|
||||||
|
raise ValueError("key is empty")
|
||||||
|
|
||||||
|
key = key.lower()
|
||||||
|
ciphertext_iterator = 0
|
||||||
|
key_iterator = 0
|
||||||
|
plaintext = ""
|
||||||
|
while ciphertext_iterator < len(ciphertext):
|
||||||
|
if (
|
||||||
|
ord(ciphertext[ciphertext_iterator]) < 97
|
||||||
|
or ord(ciphertext[ciphertext_iterator]) > 122
|
||||||
|
):
|
||||||
|
plaintext += ciphertext[ciphertext_iterator]
|
||||||
|
else:
|
||||||
|
plaintext += chr(
|
||||||
|
(ord(ciphertext[ciphertext_iterator]) - ord(key[key_iterator])) % 26
|
||||||
|
+ 97
|
||||||
|
)
|
||||||
|
key += chr(
|
||||||
|
(ord(ciphertext[ciphertext_iterator]) - ord(key[key_iterator])) % 26
|
||||||
|
+ 97
|
||||||
|
)
|
||||||
|
key_iterator += 1
|
||||||
|
ciphertext_iterator += 1
|
||||||
|
return plaintext
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
operation = int(input("Type 1 to encrypt or 2 to decrypt:"))
|
||||||
|
if operation == 1:
|
||||||
|
plaintext = input("Typeplaintext to be encrypted:\n")
|
||||||
|
key = input("Type the key:\n")
|
||||||
|
print(encrypt(plaintext, key))
|
||||||
|
elif operation == 2:
|
||||||
|
ciphertext = input("Type the ciphertext to be decrypted:\n")
|
||||||
|
key = input("Type the key:\n")
|
||||||
|
print(decrypt(ciphertext, key))
|
||||||
|
decrypt("jsqqs avvwo", "coffee")
|
@ -1,34 +1,63 @@
|
|||||||
import base64
|
def base16_encode(data: bytes) -> str:
|
||||||
|
|
||||||
|
|
||||||
def base16_encode(inp: str) -> bytes:
|
|
||||||
"""
|
"""
|
||||||
Encodes a given utf-8 string into base-16.
|
Encodes the given bytes into base16.
|
||||||
|
|
||||||
>>> base16_encode('Hello World!')
|
>>> base16_encode(b'Hello World!')
|
||||||
b'48656C6C6F20576F726C6421'
|
'48656C6C6F20576F726C6421'
|
||||||
>>> base16_encode('HELLO WORLD!')
|
>>> base16_encode(b'HELLO WORLD!')
|
||||||
b'48454C4C4F20574F524C4421'
|
'48454C4C4F20574F524C4421'
|
||||||
>>> base16_encode('')
|
>>> base16_encode(b'')
|
||||||
b''
|
|
||||||
"""
|
|
||||||
# encode the input into a bytes-like object and then encode b16encode that
|
|
||||||
return base64.b16encode(inp.encode("utf-8"))
|
|
||||||
|
|
||||||
|
|
||||||
def base16_decode(b16encoded: bytes) -> str:
|
|
||||||
"""
|
|
||||||
Decodes from base-16 to a utf-8 string.
|
|
||||||
|
|
||||||
>>> base16_decode(b'48656C6C6F20576F726C6421')
|
|
||||||
'Hello World!'
|
|
||||||
>>> base16_decode(b'48454C4C4F20574F524C4421')
|
|
||||||
'HELLO WORLD!'
|
|
||||||
>>> base16_decode(b'')
|
|
||||||
''
|
''
|
||||||
"""
|
"""
|
||||||
# b16decode the input into bytes and decode that into a human readable string
|
# Turn the data into a list of integers (where each integer is a byte),
|
||||||
return base64.b16decode(b16encoded).decode("utf-8")
|
# Then turn each byte into its hexadecimal representation, make sure
|
||||||
|
# it is uppercase, and then join everything together and return it.
|
||||||
|
return "".join([hex(byte)[2:].zfill(2).upper() for byte in list(data)])
|
||||||
|
|
||||||
|
|
||||||
|
def base16_decode(data: str) -> bytes:
|
||||||
|
"""
|
||||||
|
Decodes the given base16 encoded data into bytes.
|
||||||
|
|
||||||
|
>>> base16_decode('48656C6C6F20576F726C6421')
|
||||||
|
b'Hello World!'
|
||||||
|
>>> base16_decode('48454C4C4F20574F524C4421')
|
||||||
|
b'HELLO WORLD!'
|
||||||
|
>>> base16_decode('')
|
||||||
|
b''
|
||||||
|
>>> base16_decode('486')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Base16 encoded data is invalid:
|
||||||
|
Data does not have an even number of hex digits.
|
||||||
|
>>> base16_decode('48656c6c6f20576f726c6421')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Base16 encoded data is invalid:
|
||||||
|
Data is not uppercase hex or it contains invalid characters.
|
||||||
|
>>> base16_decode('This is not base64 encoded data.')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Base16 encoded data is invalid:
|
||||||
|
Data is not uppercase hex or it contains invalid characters.
|
||||||
|
"""
|
||||||
|
# Check data validity, following RFC3548
|
||||||
|
# https://www.ietf.org/rfc/rfc3548.txt
|
||||||
|
if (len(data) % 2) != 0:
|
||||||
|
raise ValueError(
|
||||||
|
"""Base16 encoded data is invalid:
|
||||||
|
Data does not have an even number of hex digits."""
|
||||||
|
)
|
||||||
|
# Check the character set - the standard base16 alphabet
|
||||||
|
# is uppercase according to RFC3548 section 6
|
||||||
|
if not set(data) <= set("0123456789ABCDEF"):
|
||||||
|
raise ValueError(
|
||||||
|
"""Base16 encoded data is invalid:
|
||||||
|
Data is not uppercase hex or it contains invalid characters."""
|
||||||
|
)
|
||||||
|
# For every two hexadecimal digits (= a byte), turn it into an integer.
|
||||||
|
# Then, string the result together into bytes, and return it.
|
||||||
|
return bytes(int(data[i] + data[i + 1], 16) for i in range(0, len(data), 2))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -34,9 +34,8 @@ def base64_encode(data: bytes) -> bytes:
|
|||||||
"""
|
"""
|
||||||
# Make sure the supplied data is a bytes-like object
|
# Make sure the supplied data is a bytes-like object
|
||||||
if not isinstance(data, bytes):
|
if not isinstance(data, bytes):
|
||||||
raise TypeError(
|
msg = f"a bytes-like object is required, not '{data.__class__.__name__}'"
|
||||||
f"a bytes-like object is required, not '{data.__class__.__name__}'"
|
raise TypeError(msg)
|
||||||
)
|
|
||||||
|
|
||||||
binary_stream = "".join(bin(byte)[2:].zfill(8) for byte in data)
|
binary_stream = "".join(bin(byte)[2:].zfill(8) for byte in data)
|
||||||
|
|
||||||
@ -88,10 +87,11 @@ def base64_decode(encoded_data: str) -> bytes:
|
|||||||
"""
|
"""
|
||||||
# Make sure encoded_data is either a string or a bytes-like object
|
# Make sure encoded_data is either a string or a bytes-like object
|
||||||
if not isinstance(encoded_data, bytes) and not isinstance(encoded_data, str):
|
if not isinstance(encoded_data, bytes) and not isinstance(encoded_data, str):
|
||||||
raise TypeError(
|
msg = (
|
||||||
"argument should be a bytes-like object or ASCII string, not "
|
"argument should be a bytes-like object or ASCII string, "
|
||||||
f"'{encoded_data.__class__.__name__}'"
|
f"not '{encoded_data.__class__.__name__}'"
|
||||||
)
|
)
|
||||||
|
raise TypeError(msg)
|
||||||
|
|
||||||
# In case encoded_data is a bytes-like object, make sure it contains only
|
# In case encoded_data is a bytes-like object, make sure it contains only
|
||||||
# ASCII characters so we convert it to a string object
|
# ASCII characters so we convert it to a string object
|
||||||
|
@ -5,7 +5,7 @@ Author: Mohit Radadiya
|
|||||||
from string import ascii_uppercase
|
from string import ascii_uppercase
|
||||||
|
|
||||||
dict1 = {char: i for i, char in enumerate(ascii_uppercase)}
|
dict1 = {char: i for i, char in enumerate(ascii_uppercase)}
|
||||||
dict2 = {i: char for i, char in enumerate(ascii_uppercase)}
|
dict2 = dict(enumerate(ascii_uppercase))
|
||||||
|
|
||||||
|
|
||||||
# This function generates the key in
|
# This function generates the key in
|
||||||
|
@ -9,16 +9,17 @@ https://www.braingle.com/brainteasers/codes/bifid.php
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
SQUARE = [
|
||||||
|
["a", "b", "c", "d", "e"],
|
||||||
|
["f", "g", "h", "i", "k"],
|
||||||
|
["l", "m", "n", "o", "p"],
|
||||||
|
["q", "r", "s", "t", "u"],
|
||||||
|
["v", "w", "x", "y", "z"],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class BifidCipher:
|
class BifidCipher:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
SQUARE = [
|
|
||||||
["a", "b", "c", "d", "e"],
|
|
||||||
["f", "g", "h", "i", "k"],
|
|
||||||
["l", "m", "n", "o", "p"],
|
|
||||||
["q", "r", "s", "t", "u"],
|
|
||||||
["v", "w", "x", "y", "z"],
|
|
||||||
]
|
|
||||||
self.SQUARE = np.array(SQUARE)
|
self.SQUARE = np.array(SQUARE)
|
||||||
|
|
||||||
def letter_to_numbers(self, letter: str) -> np.ndarray:
|
def letter_to_numbers(self, letter: str) -> np.ndarray:
|
||||||
@ -32,7 +33,7 @@ class BifidCipher:
|
|||||||
>>> np.array_equal(BifidCipher().letter_to_numbers('u'), [4,5])
|
>>> np.array_equal(BifidCipher().letter_to_numbers('u'), [4,5])
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
index1, index2 = np.where(self.SQUARE == letter)
|
index1, index2 = np.where(letter == self.SQUARE)
|
||||||
indexes = np.concatenate([index1 + 1, index2 + 1])
|
indexes = np.concatenate([index1 + 1, index2 + 1])
|
||||||
return indexes
|
return indexes
|
||||||
|
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import string
|
||||||
|
|
||||||
|
|
||||||
def decrypt(message: str) -> None:
|
def decrypt(message: str) -> None:
|
||||||
"""
|
"""
|
||||||
>>> decrypt('TMDETUX PMDVU')
|
>>> decrypt('TMDETUX PMDVU')
|
||||||
@ -28,16 +31,15 @@ def decrypt(message: str) -> None:
|
|||||||
Decryption using Key #24: VOFGVWZ ROFXW
|
Decryption using Key #24: VOFGVWZ ROFXW
|
||||||
Decryption using Key #25: UNEFUVY QNEWV
|
Decryption using Key #25: UNEFUVY QNEWV
|
||||||
"""
|
"""
|
||||||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
for key in range(len(string.ascii_uppercase)):
|
||||||
for key in range(len(LETTERS)):
|
|
||||||
translated = ""
|
translated = ""
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
if symbol in LETTERS:
|
if symbol in string.ascii_uppercase:
|
||||||
num = LETTERS.find(symbol)
|
num = string.ascii_uppercase.find(symbol)
|
||||||
num = num - key
|
num = num - key
|
||||||
if num < 0:
|
if num < 0:
|
||||||
num = num + len(LETTERS)
|
num = num + len(string.ascii_uppercase)
|
||||||
translated = translated + LETTERS[num]
|
translated = translated + string.ascii_uppercase[num]
|
||||||
else:
|
else:
|
||||||
translated = translated + symbol
|
translated = translated + symbol
|
||||||
print(f"Decryption using Key #{key}: {translated}")
|
print(f"Decryption using Key #{key}: {translated}")
|
||||||
|
@ -27,7 +27,7 @@ def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
|
|||||||
=========================
|
=========================
|
||||||
The caesar cipher is named after Julius Caesar who used it when sending
|
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
|
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
|
where every character in the plain-text is shifted by a certain number known
|
||||||
as the "key" or "shift".
|
as the "key" or "shift".
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
@ -6,7 +6,8 @@ def gcd(a: int, b: int) -> int:
|
|||||||
|
|
||||||
def find_mod_inverse(a: int, m: int) -> int:
|
def find_mod_inverse(a: int, m: int) -> int:
|
||||||
if gcd(a, m) != 1:
|
if gcd(a, m) != 1:
|
||||||
raise ValueError(f"mod inverse of {a!r} and {m!r} does not exist")
|
msg = f"mod inverse of {a!r} and {m!r} does not exist"
|
||||||
|
raise ValueError(msg)
|
||||||
u1, u2, u3 = 1, 0, a
|
u1, u2, u3 = 1, 0, a
|
||||||
v1, v2, v3 = 0, 1, m
|
v1, v2, v3 = 0, 1, m
|
||||||
while v3 != 0:
|
while v3 != 0:
|
||||||
|
@ -73,7 +73,7 @@ def miller_rabin(n: int, allow_probable: bool = False) -> bool:
|
|||||||
for prime in plist:
|
for prime in plist:
|
||||||
pr = False
|
pr = False
|
||||||
for r in range(s):
|
for r in range(s):
|
||||||
m = pow(prime, d * 2 ** r, n)
|
m = pow(prime, d * 2**r, n)
|
||||||
# see article for analysis explanation for m
|
# see article for analysis explanation for m
|
||||||
if (r == 0 and m == 1) or ((m + 1) % n == 0):
|
if (r == 0 and m == 1) or ((m + 1) % n == 0):
|
||||||
pr = True
|
pr = True
|
||||||
|
@ -10,13 +10,13 @@ primes = {
|
|||||||
5: {
|
5: {
|
||||||
"prime": int(
|
"prime": int(
|
||||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||||
+ "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||||
+ "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||||
+ "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||||
+ "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||||
+ "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||||
+ "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||||
+ "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF",
|
"670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF",
|
||||||
base=16,
|
base=16,
|
||||||
),
|
),
|
||||||
"generator": 2,
|
"generator": 2,
|
||||||
@ -25,16 +25,16 @@ primes = {
|
|||||||
14: {
|
14: {
|
||||||
"prime": int(
|
"prime": int(
|
||||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||||
+ "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||||
+ "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||||
+ "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||||
+ "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||||
+ "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||||
+ "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||||
+ "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||||
+ "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||||
+ "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||||
+ "15728E5A8AACAA68FFFFFFFFFFFFFFFF",
|
"15728E5A8AACAA68FFFFFFFFFFFFFFFF",
|
||||||
base=16,
|
base=16,
|
||||||
),
|
),
|
||||||
"generator": 2,
|
"generator": 2,
|
||||||
@ -43,21 +43,21 @@ primes = {
|
|||||||
15: {
|
15: {
|
||||||
"prime": int(
|
"prime": int(
|
||||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||||
+ "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||||
+ "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||||
+ "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||||
+ "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||||
+ "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||||
+ "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||||
+ "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||||
+ "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||||
+ "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||||
+ "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
"15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
||||||
+ "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
"ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
||||||
+ "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
"ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
||||||
+ "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
"F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
||||||
+ "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
"BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
||||||
+ "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF",
|
"43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF",
|
||||||
base=16,
|
base=16,
|
||||||
),
|
),
|
||||||
"generator": 2,
|
"generator": 2,
|
||||||
@ -66,27 +66,27 @@ primes = {
|
|||||||
16: {
|
16: {
|
||||||
"prime": int(
|
"prime": int(
|
||||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||||
+ "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||||
+ "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||||
+ "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||||
+ "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||||
+ "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||||
+ "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||||
+ "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||||
+ "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||||
+ "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||||
+ "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
"15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
||||||
+ "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
"ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
||||||
+ "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
"ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
||||||
+ "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
"F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
||||||
+ "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
"BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
||||||
+ "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
|
"43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
|
||||||
+ "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
|
"88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
|
||||||
+ "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
|
"2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
|
||||||
+ "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
|
"287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
|
||||||
+ "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
|
"1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
|
||||||
+ "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
|
"93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
|
||||||
+ "FFFFFFFFFFFFFFFF",
|
"FFFFFFFFFFFFFFFF",
|
||||||
base=16,
|
base=16,
|
||||||
),
|
),
|
||||||
"generator": 2,
|
"generator": 2,
|
||||||
@ -95,33 +95,33 @@ primes = {
|
|||||||
17: {
|
17: {
|
||||||
"prime": int(
|
"prime": int(
|
||||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
|
||||||
+ "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
|
"8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
|
||||||
+ "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
|
"302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
|
||||||
+ "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
|
"A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
|
||||||
+ "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
|
"49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
|
||||||
+ "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
"FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||||
+ "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
|
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
|
||||||
+ "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
|
"180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
|
||||||
+ "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
|
"3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
|
||||||
+ "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
|
"04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
|
||||||
+ "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
|
"B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
|
||||||
+ "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
"1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
||||||
+ "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
|
"BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
|
||||||
+ "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
|
"E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
|
||||||
+ "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
|
"99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
|
||||||
+ "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
|
"04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
|
||||||
+ "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
|
"233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
|
||||||
+ "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
|
"D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
|
||||||
+ "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406"
|
"36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406"
|
||||||
+ "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918"
|
"AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918"
|
||||||
+ "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151"
|
"DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151"
|
||||||
+ "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03"
|
"2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03"
|
||||||
+ "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F"
|
"F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F"
|
||||||
+ "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
|
"BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
|
||||||
+ "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B"
|
"CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B"
|
||||||
+ "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632"
|
"B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632"
|
||||||
+ "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E"
|
"387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E"
|
||||||
+ "6DCC4024FFFFFFFFFFFFFFFF",
|
"6DCC4024FFFFFFFFFFFFFFFF",
|
||||||
base=16,
|
base=16,
|
||||||
),
|
),
|
||||||
"generator": 2,
|
"generator": 2,
|
||||||
@ -130,48 +130,48 @@ primes = {
|
|||||||
18: {
|
18: {
|
||||||
"prime": int(
|
"prime": int(
|
||||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
|
||||||
+ "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
"29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
|
||||||
+ "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
"EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
|
||||||
+ "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
"E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
|
||||||
+ "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
|
||||||
+ "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
"C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
|
||||||
+ "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
"83655D23DCA3AD961C62F356208552BB9ED529077096966D"
|
||||||
+ "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
"670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
|
||||||
+ "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
|
||||||
+ "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
"DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
|
||||||
+ "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
"15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64"
|
||||||
+ "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
"ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7"
|
||||||
+ "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
"ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B"
|
||||||
+ "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
"F12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
|
||||||
+ "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
"BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31"
|
||||||
+ "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
|
"43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7"
|
||||||
+ "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
|
"88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA"
|
||||||
+ "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
|
"2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6"
|
||||||
+ "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
|
"287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED"
|
||||||
+ "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
|
"1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9"
|
||||||
+ "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
|
"93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
|
||||||
+ "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD"
|
"36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD"
|
||||||
+ "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831"
|
"F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831"
|
||||||
+ "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
|
"179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
|
||||||
+ "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF"
|
"DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF"
|
||||||
+ "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6"
|
"5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6"
|
||||||
+ "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3"
|
"D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3"
|
||||||
+ "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
|
"23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
|
||||||
+ "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328"
|
"CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328"
|
||||||
+ "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C"
|
"06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C"
|
||||||
+ "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE"
|
"DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE"
|
||||||
+ "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4"
|
"12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4"
|
||||||
+ "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300"
|
"38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300"
|
||||||
+ "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568"
|
"741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568"
|
||||||
+ "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
|
"3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
|
||||||
+ "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B"
|
"22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B"
|
||||||
+ "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A"
|
"4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A"
|
||||||
+ "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36"
|
"062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36"
|
||||||
+ "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1"
|
"4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1"
|
||||||
+ "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92"
|
"B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92"
|
||||||
+ "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47"
|
"4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47"
|
||||||
+ "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
|
"9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
|
||||||
+ "60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
|
"60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
|
||||||
base=16,
|
base=16,
|
||||||
),
|
),
|
||||||
"generator": 2,
|
"generator": 2,
|
||||||
@ -228,10 +228,10 @@ class DiffieHellman:
|
|||||||
|
|
||||||
def is_valid_public_key(self, key: int) -> bool:
|
def is_valid_public_key(self, key: int) -> bool:
|
||||||
# check if the other public key is valid based on NIST SP800-56
|
# check if the other public key is valid based on NIST SP800-56
|
||||||
if 2 <= key and key <= self.prime - 2:
|
return (
|
||||||
if pow(key, (self.prime - 1) // 2, self.prime) == 1:
|
2 <= key <= self.prime - 2
|
||||||
return True
|
and pow(key, (self.prime - 1) // 2, self.prime) == 1
|
||||||
return False
|
)
|
||||||
|
|
||||||
def generate_shared_key(self, other_key_str: str) -> str:
|
def generate_shared_key(self, other_key_str: str) -> str:
|
||||||
other_key = int(other_key_str, base=16)
|
other_key = int(other_key_str, base=16)
|
||||||
@ -243,10 +243,10 @@ class DiffieHellman:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def is_valid_public_key_static(remote_public_key_str: int, prime: int) -> bool:
|
def is_valid_public_key_static(remote_public_key_str: int, prime: int) -> bool:
|
||||||
# check if the other public key is valid based on NIST SP800-56
|
# check if the other public key is valid based on NIST SP800-56
|
||||||
if 2 <= remote_public_key_str and remote_public_key_str <= prime - 2:
|
return (
|
||||||
if pow(remote_public_key_str, (prime - 1) // 2, prime) == 1:
|
2 <= remote_public_key_str <= prime - 2
|
||||||
return True
|
and pow(remote_public_key_str, (prime - 1) // 2, prime) == 1
|
||||||
return False
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def generate_shared_key_static(
|
def generate_shared_key_static(
|
||||||
|
@ -26,7 +26,7 @@ def primitive_root(p_val: int) -> int:
|
|||||||
|
|
||||||
def generate_key(key_size: int) -> tuple[tuple[int, int, int, int], tuple[int, int]]:
|
def generate_key(key_size: int) -> tuple[tuple[int, int, int, int], tuple[int, int]]:
|
||||||
print("Generating prime p...")
|
print("Generating prime p...")
|
||||||
p = rabin_miller.generateLargePrime(key_size) # select large prime number.
|
p = rabin_miller.generate_large_prime(key_size) # select large prime number.
|
||||||
e_1 = primitive_root(p) # one primitive root on modulo p.
|
e_1 = primitive_root(p) # one primitive root on modulo p.
|
||||||
d = random.randrange(3, p) # private_key -> have to be greater than 2 for safety.
|
d = random.randrange(3, p) # private_key -> have to be greater than 2 for safety.
|
||||||
e_2 = cryptomath.find_mod_inverse(pow(e_1, d, p), p)
|
e_2 = cryptomath.find_mod_inverse(pow(e_1, d, p), p)
|
||||||
@ -37,28 +37,23 @@ def generate_key(key_size: int) -> tuple[tuple[int, int, int, int], tuple[int, i
|
|||||||
return public_key, private_key
|
return public_key, private_key
|
||||||
|
|
||||||
|
|
||||||
def make_key_files(name: str, keySize: int) -> None:
|
def make_key_files(name: str, key_size: int) -> None:
|
||||||
if os.path.exists("%s_pubkey.txt" % name) or os.path.exists(
|
if os.path.exists(f"{name}_pubkey.txt") or os.path.exists(f"{name}_privkey.txt"):
|
||||||
"%s_privkey.txt" % name
|
|
||||||
):
|
|
||||||
print("\nWARNING:")
|
print("\nWARNING:")
|
||||||
print(
|
print(
|
||||||
'"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n'
|
f'"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n'
|
||||||
"Use a different name or delete these files and re-run this program."
|
"Use a different name or delete these files and re-run this program."
|
||||||
% (name, name)
|
|
||||||
)
|
)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
publicKey, privateKey = generate_key(keySize)
|
public_key, private_key = generate_key(key_size)
|
||||||
print("\nWriting public key to file %s_pubkey.txt..." % name)
|
print(f"\nWriting public key to file {name}_pubkey.txt...")
|
||||||
with open("%s_pubkey.txt" % name, "w") as fo:
|
with open(f"{name}_pubkey.txt", "w") as fo:
|
||||||
fo.write(
|
fo.write(f"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}")
|
||||||
"%d,%d,%d,%d" % (publicKey[0], publicKey[1], publicKey[2], publicKey[3])
|
|
||||||
)
|
|
||||||
|
|
||||||
print("Writing private key to file %s_privkey.txt..." % name)
|
print(f"Writing private key to file {name}_privkey.txt...")
|
||||||
with open("%s_privkey.txt" % name, "w") as fo:
|
with open(f"{name}_privkey.txt", "w") as fo:
|
||||||
fo.write("%d,%d" % (privateKey[0], privateKey[1]))
|
fo.write(f"{private_key[0]},{private_key[1]}")
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
@ -8,7 +8,7 @@ the famous Enigma machine from WWII.
|
|||||||
Module includes:
|
Module includes:
|
||||||
- enigma function
|
- enigma function
|
||||||
- showcase of function usage
|
- showcase of function usage
|
||||||
- 9 randnomly generated rotors
|
- 9 randomly generated rotors
|
||||||
- reflector (aka static rotor)
|
- reflector (aka static rotor)
|
||||||
- original alphabet
|
- original alphabet
|
||||||
|
|
||||||
@ -86,24 +86,21 @@ def _validator(
|
|||||||
"""
|
"""
|
||||||
# Checks if there are 3 unique rotors
|
# Checks if there are 3 unique rotors
|
||||||
|
|
||||||
unique_rotsel = len(set(rotsel))
|
if (unique_rotsel := len(set(rotsel))) < 3:
|
||||||
if unique_rotsel < 3:
|
msg = f"Please use 3 unique rotors (not {unique_rotsel})"
|
||||||
raise Exception(f"Please use 3 unique rotors (not {unique_rotsel})")
|
raise Exception(msg)
|
||||||
|
|
||||||
# Checks if rotor positions are valid
|
# Checks if rotor positions are valid
|
||||||
rotorpos1, rotorpos2, rotorpos3 = rotpos
|
rotorpos1, rotorpos2, rotorpos3 = rotpos
|
||||||
if not 0 < rotorpos1 <= len(abc):
|
if not 0 < rotorpos1 <= len(abc):
|
||||||
raise ValueError(
|
msg = f"First rotor position is not within range of 1..26 ({rotorpos1}"
|
||||||
f"First rotor position is not within range of 1..26 (" f"{rotorpos1}"
|
raise ValueError(msg)
|
||||||
)
|
|
||||||
if not 0 < rotorpos2 <= len(abc):
|
if not 0 < rotorpos2 <= len(abc):
|
||||||
raise ValueError(
|
msg = f"Second rotor position is not within range of 1..26 ({rotorpos2})"
|
||||||
f"Second rotor position is not within range of 1..26 (" f"{rotorpos2})"
|
raise ValueError(msg)
|
||||||
)
|
|
||||||
if not 0 < rotorpos3 <= len(abc):
|
if not 0 < rotorpos3 <= len(abc):
|
||||||
raise ValueError(
|
msg = f"Third rotor position is not within range of 1..26 ({rotorpos3})"
|
||||||
f"Third rotor position is not within range of 1..26 (" f"{rotorpos3})"
|
raise ValueError(msg)
|
||||||
)
|
|
||||||
|
|
||||||
# Validates string and returns dict
|
# Validates string and returns dict
|
||||||
pbdict = _plugboard(pb)
|
pbdict = _plugboard(pb)
|
||||||
@ -131,9 +128,11 @@ def _plugboard(pbstring: str) -> dict[str, str]:
|
|||||||
# a) is type string
|
# a) is type string
|
||||||
# b) has even length (so pairs can be made)
|
# b) has even length (so pairs can be made)
|
||||||
if not isinstance(pbstring, str):
|
if not isinstance(pbstring, str):
|
||||||
raise TypeError(f"Plugboard setting isn't type string ({type(pbstring)})")
|
msg = f"Plugboard setting isn't type string ({type(pbstring)})"
|
||||||
|
raise TypeError(msg)
|
||||||
elif len(pbstring) % 2 != 0:
|
elif len(pbstring) % 2 != 0:
|
||||||
raise Exception(f"Odd number of symbols ({len(pbstring)})")
|
msg = f"Odd number of symbols ({len(pbstring)})"
|
||||||
|
raise Exception(msg)
|
||||||
elif pbstring == "":
|
elif pbstring == "":
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@ -143,9 +142,11 @@ def _plugboard(pbstring: str) -> dict[str, str]:
|
|||||||
tmppbl = set()
|
tmppbl = set()
|
||||||
for i in pbstring:
|
for i in pbstring:
|
||||||
if i not in abc:
|
if i not in abc:
|
||||||
raise Exception(f"'{i}' not in list of symbols")
|
msg = f"'{i}' not in list of symbols"
|
||||||
|
raise Exception(msg)
|
||||||
elif i in tmppbl:
|
elif i in tmppbl:
|
||||||
raise Exception(f"Duplicate symbol ({i})")
|
msg = f"Duplicate symbol ({i})"
|
||||||
|
raise Exception(msg)
|
||||||
else:
|
else:
|
||||||
tmppbl.add(i)
|
tmppbl.add(i)
|
||||||
del tmppbl
|
del tmppbl
|
||||||
@ -231,7 +232,6 @@ def enigma(
|
|||||||
# encryption/decryption process --------------------------
|
# encryption/decryption process --------------------------
|
||||||
for symbol in text:
|
for symbol in text:
|
||||||
if symbol in abc:
|
if symbol in abc:
|
||||||
|
|
||||||
# 1st plugboard --------------------------
|
# 1st plugboard --------------------------
|
||||||
if symbol in plugboard:
|
if symbol in plugboard:
|
||||||
symbol = plugboard[symbol]
|
symbol = plugboard[symbol]
|
||||||
|
@ -62,7 +62,7 @@ class HillCipher:
|
|||||||
# take x and return x % len(key_string)
|
# take x and return x % len(key_string)
|
||||||
modulus = numpy.vectorize(lambda x: x % 36)
|
modulus = numpy.vectorize(lambda x: x % 36)
|
||||||
|
|
||||||
to_int = numpy.vectorize(lambda x: round(x))
|
to_int = numpy.vectorize(round)
|
||||||
|
|
||||||
def __init__(self, encrypt_key: numpy.ndarray) -> None:
|
def __init__(self, encrypt_key: numpy.ndarray) -> None:
|
||||||
"""
|
"""
|
||||||
@ -104,10 +104,11 @@ class HillCipher:
|
|||||||
|
|
||||||
req_l = len(self.key_string)
|
req_l = len(self.key_string)
|
||||||
if greatest_common_divisor(det, len(self.key_string)) != 1:
|
if greatest_common_divisor(det, len(self.key_string)) != 1:
|
||||||
raise ValueError(
|
msg = (
|
||||||
f"determinant modular {req_l} of encryption key({det}) is not co prime "
|
f"determinant modular {req_l} of encryption key({det}) "
|
||||||
f"w.r.t {req_l}.\nTry another key."
|
f"is not co prime w.r.t {req_l}.\nTry another key."
|
||||||
)
|
)
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
def process_text(self, text: str) -> str:
|
def process_text(self, text: str) -> str:
|
||||||
"""
|
"""
|
||||||
@ -201,11 +202,11 @@ class HillCipher:
|
|||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
N = int(input("Enter the order of the encryption key: "))
|
n = int(input("Enter the order of the encryption key: "))
|
||||||
hill_matrix = []
|
hill_matrix = []
|
||||||
|
|
||||||
print("Enter each row of the encryption key with space separated integers")
|
print("Enter each row of the encryption key with space separated integers")
|
||||||
for _ in range(N):
|
for _ in range(n):
|
||||||
row = [int(x) for x in input().split()]
|
row = [int(x) for x in input().split()]
|
||||||
hill_matrix.append(row)
|
hill_matrix.append(row)
|
||||||
|
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str:
|
from string import ascii_uppercase
|
||||||
"""
|
|
||||||
|
|
||||||
For key:hello
|
|
||||||
|
def mixed_keyword(
|
||||||
|
keyword: str, plaintext: str, verbose: bool = False, alphabet: str = ascii_uppercase
|
||||||
|
) -> str:
|
||||||
|
"""
|
||||||
|
For keyword: hello
|
||||||
|
|
||||||
H E L O
|
H E L O
|
||||||
A B C D
|
A B C D
|
||||||
@ -12,57 +16,60 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str:
|
|||||||
Y Z
|
Y Z
|
||||||
and map vertically
|
and map vertically
|
||||||
|
|
||||||
>>> mixed_keyword("college", "UNIVERSITY") # doctest: +NORMALIZE_WHITESPACE
|
>>> mixed_keyword("college", "UNIVERSITY", True) # doctest: +NORMALIZE_WHITESPACE
|
||||||
{'A': 'C', 'B': 'A', 'C': 'I', 'D': 'P', 'E': 'U', 'F': 'Z', 'G': 'O', 'H': 'B',
|
{'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',
|
'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',
|
'Q': 'E', 'R': 'F', 'S': 'M', 'T': 'S', 'U': 'X', 'V': 'G', 'W': 'H', 'X': 'N',
|
||||||
'Y': 'T', 'Z': 'Y'}
|
'Y': 'T', 'Z': 'Y'}
|
||||||
'XKJGUFMJST'
|
'XKJGUFMJST'
|
||||||
|
|
||||||
|
>>> mixed_keyword("college", "UNIVERSITY", False) # doctest: +NORMALIZE_WHITESPACE
|
||||||
|
'XKJGUFMJST'
|
||||||
"""
|
"""
|
||||||
key = key.upper()
|
keyword = keyword.upper()
|
||||||
pt = pt.upper()
|
plaintext = plaintext.upper()
|
||||||
temp = []
|
alphabet_set = set(alphabet)
|
||||||
for i in key:
|
|
||||||
if i not in temp:
|
# create a list of unique characters in the keyword - their order matters
|
||||||
temp.append(i)
|
# it determines how we will map plaintext characters to the ciphertext
|
||||||
len_temp = len(temp)
|
unique_chars = []
|
||||||
# print(temp)
|
for char in keyword:
|
||||||
alpha = []
|
if char in alphabet_set and char not in unique_chars:
|
||||||
modalpha = []
|
unique_chars.append(char)
|
||||||
for j in range(65, 91):
|
# the number of those unique characters will determine the number of rows
|
||||||
t = chr(j)
|
num_unique_chars_in_keyword = len(unique_chars)
|
||||||
alpha.append(t)
|
|
||||||
if t not in temp:
|
# create a shifted version of the alphabet
|
||||||
temp.append(t)
|
shifted_alphabet = unique_chars + [
|
||||||
# print(temp)
|
char for char in alphabet if char not in unique_chars
|
||||||
r = int(26 / 4)
|
]
|
||||||
# print(r)
|
|
||||||
k = 0
|
# create a modified alphabet by splitting the shifted alphabet into rows
|
||||||
for _ in range(r):
|
modified_alphabet = [
|
||||||
s = []
|
shifted_alphabet[k : k + num_unique_chars_in_keyword]
|
||||||
for j in range(len_temp):
|
for k in range(0, 26, num_unique_chars_in_keyword)
|
||||||
s.append(temp[k])
|
]
|
||||||
if not (k < 25):
|
|
||||||
|
# map the alphabet characters to the modified alphabet characters
|
||||||
|
# going 'vertically' through the modified alphabet - consider columns first
|
||||||
|
mapping = {}
|
||||||
|
letter_index = 0
|
||||||
|
for column in range(num_unique_chars_in_keyword):
|
||||||
|
for row in modified_alphabet:
|
||||||
|
# if current row (the last one) is too short, break out of loop
|
||||||
|
if len(row) <= column:
|
||||||
break
|
break
|
||||||
k += 1
|
|
||||||
modalpha.append(s)
|
# map current letter to letter in modified alphabet
|
||||||
# print(modalpha)
|
mapping[alphabet[letter_index]] = row[column]
|
||||||
d = {}
|
letter_index += 1
|
||||||
j = 0
|
|
||||||
k = 0
|
if verbose:
|
||||||
for j in range(len_temp):
|
print(mapping)
|
||||||
for m in modalpha:
|
# create the encrypted text by mapping the plaintext to the modified alphabet
|
||||||
if not (len(m) - 1 >= j):
|
return "".join(mapping[char] if char in mapping else char for char in plaintext)
|
||||||
break
|
|
||||||
d[alpha[k]] = m[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"))
|
if __name__ == "__main__":
|
||||||
|
# example use
|
||||||
|
print(mixed_keyword("college", "UNIVERSITY"))
|
||||||
|
@ -22,7 +22,7 @@ class Onepad:
|
|||||||
for i in range(len(key)):
|
for i in range(len(key)):
|
||||||
p = int((cipher[i] - (key[i]) ** 2) / key[i])
|
p = int((cipher[i] - (key[i]) ** 2) / key[i])
|
||||||
plain.append(chr(p))
|
plain.append(chr(p))
|
||||||
return "".join([i for i in plain])
|
return "".join(plain)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import itertools
|
import itertools
|
||||||
import string
|
import string
|
||||||
from typing import Generator, Iterable
|
from collections.abc import Generator, Iterable
|
||||||
|
|
||||||
|
|
||||||
def chunker(seq: Iterable[str], size: int) -> Generator[tuple[str, ...], None, None]:
|
def chunker(seq: Iterable[str], size: int) -> Generator[tuple[str, ...], None, None]:
|
||||||
@ -39,7 +39,6 @@ def prepare_input(dirty: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def generate_table(key: str) -> list[str]:
|
def generate_table(key: str) -> list[str]:
|
||||||
|
|
||||||
# I and J are used interchangeably to allow
|
# I and J are used interchangeably to allow
|
||||||
# us to use a 5x5 table (25 letters)
|
# us to use a 5x5 table (25 letters)
|
||||||
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
|
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
|
||||||
|
@ -8,16 +8,17 @@ https://www.braingle.com/brainteasers/codes/polybius.php
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
SQUARE = [
|
||||||
|
["a", "b", "c", "d", "e"],
|
||||||
|
["f", "g", "h", "i", "k"],
|
||||||
|
["l", "m", "n", "o", "p"],
|
||||||
|
["q", "r", "s", "t", "u"],
|
||||||
|
["v", "w", "x", "y", "z"],
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class PolybiusCipher:
|
class PolybiusCipher:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
SQUARE = [
|
|
||||||
["a", "b", "c", "d", "e"],
|
|
||||||
["f", "g", "h", "i", "k"],
|
|
||||||
["l", "m", "n", "o", "p"],
|
|
||||||
["q", "r", "s", "t", "u"],
|
|
||||||
["v", "w", "x", "y", "z"],
|
|
||||||
]
|
|
||||||
self.SQUARE = np.array(SQUARE)
|
self.SQUARE = np.array(SQUARE)
|
||||||
|
|
||||||
def letter_to_numbers(self, letter: str) -> np.ndarray:
|
def letter_to_numbers(self, letter: str) -> np.ndarray:
|
||||||
@ -30,7 +31,7 @@ class PolybiusCipher:
|
|||||||
>>> np.array_equal(PolybiusCipher().letter_to_numbers('u'), [4,5])
|
>>> np.array_equal(PolybiusCipher().letter_to_numbers('u'), [4,5])
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
index1, index2 = np.where(self.SQUARE == letter)
|
index1, index2 = np.where(letter == self.SQUARE)
|
||||||
indexes = np.concatenate([index1 + 1, index2 + 1])
|
indexes = np.concatenate([index1 + 1, index2 + 1])
|
||||||
return indexes
|
return indexes
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
def rabinMiller(num: int) -> bool:
|
def rabin_miller(num: int) -> bool:
|
||||||
s = num - 1
|
s = num - 1
|
||||||
t = 0
|
t = 0
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ def rabinMiller(num: int) -> bool:
|
|||||||
s = s // 2
|
s = s // 2
|
||||||
t += 1
|
t += 1
|
||||||
|
|
||||||
for trials in range(5):
|
for _ in range(5):
|
||||||
a = random.randrange(2, num - 1)
|
a = random.randrange(2, num - 1)
|
||||||
v = pow(a, s, num)
|
v = pow(a, s, num)
|
||||||
if v != 1:
|
if v != 1:
|
||||||
@ -21,15 +21,15 @@ def rabinMiller(num: int) -> bool:
|
|||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
i = i + 1
|
i = i + 1
|
||||||
v = (v ** 2) % num
|
v = (v**2) % num
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def isPrime(num: int) -> bool:
|
def is_prime_low_num(num: int) -> bool:
|
||||||
if num < 2:
|
if num < 2:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
lowPrimes = [
|
low_primes = [
|
||||||
2,
|
2,
|
||||||
3,
|
3,
|
||||||
5,
|
5,
|
||||||
@ -200,24 +200,24 @@ def isPrime(num: int) -> bool:
|
|||||||
997,
|
997,
|
||||||
]
|
]
|
||||||
|
|
||||||
if num in lowPrimes:
|
if num in low_primes:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
for prime in lowPrimes:
|
for prime in low_primes:
|
||||||
if (num % prime) == 0:
|
if (num % prime) == 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return rabinMiller(num)
|
return rabin_miller(num)
|
||||||
|
|
||||||
|
|
||||||
def generateLargePrime(keysize: int = 1024) -> int:
|
def generate_large_prime(keysize: int = 1024) -> int:
|
||||||
while True:
|
while True:
|
||||||
num = random.randrange(2 ** (keysize - 1), 2 ** (keysize))
|
num = random.randrange(2 ** (keysize - 1), 2 ** (keysize))
|
||||||
if isPrime(num):
|
if is_prime_low_num(num):
|
||||||
return num
|
return num
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
num = generateLargePrime()
|
num = generate_large_prime()
|
||||||
print(("Prime number:", num))
|
print(("Prime number:", num))
|
||||||
print(("isPrime:", isPrime(num)))
|
print(("is_prime_low_num:", is_prime_low_num(num)))
|
||||||
|
@ -72,7 +72,7 @@ def decrypt(input_string: str, key: int) -> str:
|
|||||||
counter = 0
|
counter = 0
|
||||||
for row in temp_grid: # fills in the characters
|
for row in temp_grid: # fills in the characters
|
||||||
splice = input_string[counter : counter + len(row)]
|
splice = input_string[counter : counter + len(row)]
|
||||||
grid.append([character for character in splice])
|
grid.append(list(splice))
|
||||||
counter += len(row)
|
counter += len(row)
|
||||||
|
|
||||||
output_string = "" # reads as zigzag
|
output_string = "" # reads as zigzag
|
||||||
|
@ -29,20 +29,20 @@ def get_text_from_blocks(
|
|||||||
block_message: list[str] = []
|
block_message: list[str] = []
|
||||||
for i in range(block_size - 1, -1, -1):
|
for i in range(block_size - 1, -1, -1):
|
||||||
if len(message) + i < message_length:
|
if len(message) + i < message_length:
|
||||||
ascii_number = block_int // (BYTE_SIZE ** i)
|
ascii_number = block_int // (BYTE_SIZE**i)
|
||||||
block_int = block_int % (BYTE_SIZE ** i)
|
block_int = block_int % (BYTE_SIZE**i)
|
||||||
block_message.insert(0, chr(ascii_number))
|
block_message.insert(0, chr(ascii_number))
|
||||||
message.extend(block_message)
|
message.extend(block_message)
|
||||||
return "".join(message)
|
return "".join(message)
|
||||||
|
|
||||||
|
|
||||||
def encrypt_message(
|
def encrypt_message(
|
||||||
message: str, key: tuple[int, int], blockSize: int = DEFAULT_BLOCK_SIZE
|
message: str, key: tuple[int, int], block_size: int = DEFAULT_BLOCK_SIZE
|
||||||
) -> list[int]:
|
) -> list[int]:
|
||||||
encrypted_blocks = []
|
encrypted_blocks = []
|
||||||
n, e = key
|
n, e = key
|
||||||
|
|
||||||
for block in get_blocks_from_text(message, blockSize):
|
for block in get_blocks_from_text(message, block_size):
|
||||||
encrypted_blocks.append(pow(block, e, n))
|
encrypted_blocks.append(pow(block, e, n))
|
||||||
return encrypted_blocks
|
return encrypted_blocks
|
||||||
|
|
||||||
@ -63,8 +63,8 @@ def decrypt_message(
|
|||||||
def read_key_file(key_filename: str) -> tuple[int, int, int]:
|
def read_key_file(key_filename: str) -> tuple[int, int, int]:
|
||||||
with open(key_filename) as fo:
|
with open(key_filename) as fo:
|
||||||
content = fo.read()
|
content = fo.read()
|
||||||
key_size, n, EorD = content.split(",")
|
key_size, n, eor_d = content.split(",")
|
||||||
return (int(key_size), int(n), int(EorD))
|
return (int(key_size), int(n), int(eor_d))
|
||||||
|
|
||||||
|
|
||||||
def encrypt_and_write_to_file(
|
def encrypt_and_write_to_file(
|
||||||
@ -76,10 +76,11 @@ def encrypt_and_write_to_file(
|
|||||||
key_size, n, e = read_key_file(key_filename)
|
key_size, n, e = read_key_file(key_filename)
|
||||||
if key_size < block_size * 8:
|
if key_size < block_size * 8:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"ERROR: Block size is %s bits and key size is %s bits. The RSA cipher "
|
"ERROR: Block size is {} bits and key size is {} bits. The RSA cipher "
|
||||||
"requires the block size to be equal to or greater than the key size. "
|
"requires the block size to be equal to or greater than the key size. "
|
||||||
"Either decrease the block size or use different keys."
|
"Either decrease the block size or use different keys.".format(
|
||||||
% (block_size * 8, key_size)
|
block_size * 8, key_size
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
encrypted_blocks = [str(i) for i in encrypt_message(message, (n, e), block_size)]
|
encrypted_blocks = [str(i) for i in encrypt_message(message, (n, e), block_size)]
|
||||||
@ -101,10 +102,11 @@ def read_from_file_and_decrypt(message_filename: str, key_filename: str) -> str:
|
|||||||
|
|
||||||
if key_size < block_size * 8:
|
if key_size < block_size * 8:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"ERROR: Block size is %s bits and key size is %s bits. The RSA cipher "
|
"ERROR: Block size is {} bits and key size is {} bits. The RSA cipher "
|
||||||
"requires the block size to be equal to or greater than the key size. "
|
"requires the block size to be equal to or greater than the key size. "
|
||||||
"Did you specify the correct key file and encrypted file?"
|
"Did you specify the correct key file and encrypted file?".format(
|
||||||
% (block_size * 8, key_size)
|
block_size * 8, key_size
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
encrypted_blocks = []
|
encrypted_blocks = []
|
||||||
@ -125,19 +127,19 @@ def main() -> None:
|
|||||||
|
|
||||||
if mode == "encrypt":
|
if mode == "encrypt":
|
||||||
if not os.path.exists("rsa_pubkey.txt"):
|
if not os.path.exists("rsa_pubkey.txt"):
|
||||||
rkg.makeKeyFiles("rsa", 1024)
|
rkg.make_key_files("rsa", 1024)
|
||||||
|
|
||||||
message = input("\nEnter message: ")
|
message = input("\nEnter message: ")
|
||||||
pubkey_filename = "rsa_pubkey.txt"
|
pubkey_filename = "rsa_pubkey.txt"
|
||||||
print("Encrypting and writing to %s..." % (filename))
|
print(f"Encrypting and writing to {filename}...")
|
||||||
encryptedText = encrypt_and_write_to_file(filename, pubkey_filename, message)
|
encrypted_text = encrypt_and_write_to_file(filename, pubkey_filename, message)
|
||||||
|
|
||||||
print("\nEncrypted text:")
|
print("\nEncrypted text:")
|
||||||
print(encryptedText)
|
print(encrypted_text)
|
||||||
|
|
||||||
elif mode == "decrypt":
|
elif mode == "decrypt":
|
||||||
privkey_filename = "rsa_privkey.txt"
|
privkey_filename = "rsa_privkey.txt"
|
||||||
print("Reading from %s and decrypting..." % (filename))
|
print(f"Reading from {filename} and decrypting...")
|
||||||
decrypted_text = read_from_file_and_decrypt(filename, privkey_filename)
|
decrypted_text = read_from_file_and_decrypt(filename, privkey_filename)
|
||||||
print("writing decryption to rsa_decryption.txt...")
|
print("writing decryption to rsa_decryption.txt...")
|
||||||
with open("rsa_decryption.txt", "w") as dec:
|
with open("rsa_decryption.txt", "w") as dec:
|
||||||
|
@ -13,7 +13,7 @@ import math
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
def rsafactor(d: int, e: int, N: int) -> list[int]:
|
def rsafactor(d: int, e: int, n: int) -> list[int]:
|
||||||
"""
|
"""
|
||||||
This function returns the factors of N, where p*q=N
|
This function returns the factors of N, where p*q=N
|
||||||
Return: [p, q]
|
Return: [p, q]
|
||||||
@ -35,16 +35,16 @@ def rsafactor(d: int, e: int, N: int) -> list[int]:
|
|||||||
p = 0
|
p = 0
|
||||||
q = 0
|
q = 0
|
||||||
while p == 0:
|
while p == 0:
|
||||||
g = random.randint(2, N - 1)
|
g = random.randint(2, n - 1)
|
||||||
t = k
|
t = k
|
||||||
while True:
|
while True:
|
||||||
if t % 2 == 0:
|
if t % 2 == 0:
|
||||||
t = t // 2
|
t = t // 2
|
||||||
x = (g ** t) % N
|
x = (g**t) % n
|
||||||
y = math.gcd(x - 1, N)
|
y = math.gcd(x - 1, n)
|
||||||
if x > 1 and y > 1:
|
if x > 1 and y > 1:
|
||||||
p = y
|
p = y
|
||||||
q = N // y
|
q = n // y
|
||||||
break # find the correct factors
|
break # find the correct factors
|
||||||
else:
|
else:
|
||||||
break # t is not divisible by 2, break and choose another g
|
break # t is not divisible by 2, break and choose another g
|
||||||
|
@ -2,57 +2,59 @@ import os
|
|||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from . import cryptomath_module as cryptoMath
|
from . import cryptomath_module, rabin_miller
|
||||||
from . import rabin_miller as rabinMiller
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
print("Making key files...")
|
print("Making key files...")
|
||||||
makeKeyFiles("rsa", 1024)
|
make_key_files("rsa", 1024)
|
||||||
print("Key files generation successful.")
|
print("Key files generation successful.")
|
||||||
|
|
||||||
|
|
||||||
def generateKey(keySize: int) -> tuple[tuple[int, int], tuple[int, int]]:
|
def generate_key(key_size: int) -> tuple[tuple[int, int], tuple[int, int]]:
|
||||||
print("Generating prime p...")
|
"""
|
||||||
p = rabinMiller.generateLargePrime(keySize)
|
>>> random.seed(0) # for repeatability
|
||||||
print("Generating prime q...")
|
>>> public_key, private_key = generate_key(8)
|
||||||
q = rabinMiller.generateLargePrime(keySize)
|
>>> public_key
|
||||||
|
(26569, 239)
|
||||||
|
>>> private_key
|
||||||
|
(26569, 2855)
|
||||||
|
"""
|
||||||
|
p = rabin_miller.generate_large_prime(key_size)
|
||||||
|
q = rabin_miller.generate_large_prime(key_size)
|
||||||
n = p * q
|
n = p * q
|
||||||
|
|
||||||
print("Generating e that is relatively prime to (p - 1) * (q - 1)...")
|
# Generate e that is relatively prime to (p - 1) * (q - 1)
|
||||||
while True:
|
while True:
|
||||||
e = random.randrange(2 ** (keySize - 1), 2 ** (keySize))
|
e = random.randrange(2 ** (key_size - 1), 2 ** (key_size))
|
||||||
if cryptoMath.gcd(e, (p - 1) * (q - 1)) == 1:
|
if cryptomath_module.gcd(e, (p - 1) * (q - 1)) == 1:
|
||||||
break
|
break
|
||||||
|
|
||||||
print("Calculating d that is mod inverse of e...")
|
# Calculate d that is mod inverse of e
|
||||||
d = cryptoMath.find_mod_inverse(e, (p - 1) * (q - 1))
|
d = cryptomath_module.find_mod_inverse(e, (p - 1) * (q - 1))
|
||||||
|
|
||||||
publicKey = (n, e)
|
public_key = (n, e)
|
||||||
privateKey = (n, d)
|
private_key = (n, d)
|
||||||
return (publicKey, privateKey)
|
return (public_key, private_key)
|
||||||
|
|
||||||
|
|
||||||
def makeKeyFiles(name: str, keySize: int) -> None:
|
def make_key_files(name: str, key_size: int) -> None:
|
||||||
if os.path.exists("%s_pubkey.txt" % (name)) or os.path.exists(
|
if os.path.exists(f"{name}_pubkey.txt") or os.path.exists(f"{name}_privkey.txt"):
|
||||||
"%s_privkey.txt" % (name)
|
|
||||||
):
|
|
||||||
print("\nWARNING:")
|
print("\nWARNING:")
|
||||||
print(
|
print(
|
||||||
'"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n'
|
f'"{name}_pubkey.txt" or "{name}_privkey.txt" already exists. \n'
|
||||||
"Use a different name or delete these files and re-run this program."
|
"Use a different name or delete these files and re-run this program."
|
||||||
% (name, name)
|
|
||||||
)
|
)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
publicKey, privateKey = generateKey(keySize)
|
public_key, private_key = generate_key(key_size)
|
||||||
print("\nWriting public key to file %s_pubkey.txt..." % name)
|
print(f"\nWriting public key to file {name}_pubkey.txt...")
|
||||||
with open("%s_pubkey.txt" % name, "w") as out_file:
|
with open(f"{name}_pubkey.txt", "w") as out_file:
|
||||||
out_file.write(f"{keySize},{publicKey[0]},{publicKey[1]}")
|
out_file.write(f"{key_size},{public_key[0]},{public_key[1]}")
|
||||||
|
|
||||||
print("Writing private key to file %s_privkey.txt..." % name)
|
print(f"Writing private key to file {name}_privkey.txt...")
|
||||||
with open("%s_privkey.txt" % name, "w") as out_file:
|
with open(f"{name}_privkey.txt", "w") as out_file:
|
||||||
out_file.write(f"{keySize},{privateKey[0]},{privateKey[1]}")
|
out_file.write(f"{key_size},{private_key[0]},{private_key[1]}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -9,7 +9,7 @@ class ShuffledShiftCipher:
|
|||||||
This algorithm uses the Caesar Cipher algorithm but removes the option to
|
This algorithm uses the Caesar Cipher algorithm but removes the option to
|
||||||
use brute force to decrypt the message.
|
use brute force to decrypt the message.
|
||||||
|
|
||||||
The passcode is a a random password from the selection buffer of
|
The passcode is a random password from the selection buffer of
|
||||||
1. uppercase letters of the English alphabet
|
1. uppercase letters of the English alphabet
|
||||||
2. lowercase letters of the English alphabet
|
2. lowercase letters of the English alphabet
|
||||||
3. digits from 0 to 9
|
3. digits from 0 to 9
|
||||||
@ -42,7 +42,7 @@ class ShuffledShiftCipher:
|
|||||||
"""
|
"""
|
||||||
:return: passcode of the cipher object
|
:return: passcode of the cipher object
|
||||||
"""
|
"""
|
||||||
return "Passcode is: " + "".join(self.__passcode)
|
return "".join(self.__passcode)
|
||||||
|
|
||||||
def __neg_pos(self, iterlist: list[int]) -> list[int]:
|
def __neg_pos(self, iterlist: list[int]) -> list[int]:
|
||||||
"""
|
"""
|
||||||
|
@ -21,7 +21,7 @@ def create_cipher_map(key: str) -> dict[str, str]:
|
|||||||
:param key: keyword to use
|
:param key: keyword to use
|
||||||
:return: dictionary cipher map
|
:return: dictionary cipher map
|
||||||
"""
|
"""
|
||||||
# Create alphabet list
|
# Create a list of the letters in the alphabet
|
||||||
alphabet = [chr(i + 65) for i in range(26)]
|
alphabet = [chr(i + 65) for i in range(26)]
|
||||||
# Remove duplicate characters from key
|
# Remove duplicate characters from key
|
||||||
key = remove_duplicates(key.upper())
|
key = remove_duplicates(key.upper())
|
||||||
|
@ -9,66 +9,66 @@ def main() -> None:
|
|||||||
key = "LFWOAYUISVKMNXPBDCRJTQEGHZ"
|
key = "LFWOAYUISVKMNXPBDCRJTQEGHZ"
|
||||||
resp = input("Encrypt/Decrypt [e/d]: ")
|
resp = input("Encrypt/Decrypt [e/d]: ")
|
||||||
|
|
||||||
checkValidKey(key)
|
check_valid_key(key)
|
||||||
|
|
||||||
if resp.lower().startswith("e"):
|
if resp.lower().startswith("e"):
|
||||||
mode = "encrypt"
|
mode = "encrypt"
|
||||||
translated = encryptMessage(key, message)
|
translated = encrypt_message(key, message)
|
||||||
elif resp.lower().startswith("d"):
|
elif resp.lower().startswith("d"):
|
||||||
mode = "decrypt"
|
mode = "decrypt"
|
||||||
translated = decryptMessage(key, message)
|
translated = decrypt_message(key, message)
|
||||||
|
|
||||||
print(f"\n{mode.title()}ion: \n{translated}")
|
print(f"\n{mode.title()}ion: \n{translated}")
|
||||||
|
|
||||||
|
|
||||||
def checkValidKey(key: str) -> None:
|
def check_valid_key(key: str) -> None:
|
||||||
keyList = list(key)
|
key_list = list(key)
|
||||||
lettersList = list(LETTERS)
|
letters_list = list(LETTERS)
|
||||||
keyList.sort()
|
key_list.sort()
|
||||||
lettersList.sort()
|
letters_list.sort()
|
||||||
|
|
||||||
if keyList != lettersList:
|
if key_list != letters_list:
|
||||||
sys.exit("Error in the key or symbol set.")
|
sys.exit("Error in the key or symbol set.")
|
||||||
|
|
||||||
|
|
||||||
def encryptMessage(key: str, message: str) -> str:
|
def encrypt_message(key: str, message: str) -> str:
|
||||||
"""
|
"""
|
||||||
>>> encryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Harshil Darji')
|
>>> encrypt_message('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Harshil Darji')
|
||||||
'Ilcrism Olcvs'
|
'Ilcrism Olcvs'
|
||||||
"""
|
"""
|
||||||
return translateMessage(key, message, "encrypt")
|
return translate_message(key, message, "encrypt")
|
||||||
|
|
||||||
|
|
||||||
def decryptMessage(key: str, message: str) -> str:
|
def decrypt_message(key: str, message: str) -> str:
|
||||||
"""
|
"""
|
||||||
>>> decryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Ilcrism Olcvs')
|
>>> decrypt_message('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Ilcrism Olcvs')
|
||||||
'Harshil Darji'
|
'Harshil Darji'
|
||||||
"""
|
"""
|
||||||
return translateMessage(key, message, "decrypt")
|
return translate_message(key, message, "decrypt")
|
||||||
|
|
||||||
|
|
||||||
def translateMessage(key: str, message: str, mode: str) -> str:
|
def translate_message(key: str, message: str, mode: str) -> str:
|
||||||
translated = ""
|
translated = ""
|
||||||
charsA = LETTERS
|
chars_a = LETTERS
|
||||||
charsB = key
|
chars_b = key
|
||||||
|
|
||||||
if mode == "decrypt":
|
if mode == "decrypt":
|
||||||
charsA, charsB = charsB, charsA
|
chars_a, chars_b = chars_b, chars_a
|
||||||
|
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
if symbol.upper() in charsA:
|
if symbol.upper() in chars_a:
|
||||||
symIndex = charsA.find(symbol.upper())
|
sym_index = chars_a.find(symbol.upper())
|
||||||
if symbol.isupper():
|
if symbol.isupper():
|
||||||
translated += charsB[symIndex].upper()
|
translated += chars_b[sym_index].upper()
|
||||||
else:
|
else:
|
||||||
translated += charsB[symIndex].lower()
|
translated += chars_b[sym_index].lower()
|
||||||
else:
|
else:
|
||||||
translated += symbol
|
translated += symbol
|
||||||
|
|
||||||
return translated
|
return translated
|
||||||
|
|
||||||
|
|
||||||
def getRandomKey() -> str:
|
def get_random_key() -> str:
|
||||||
key = list(LETTERS)
|
key = list(LETTERS)
|
||||||
random.shuffle(key)
|
random.shuffle(key)
|
||||||
return "".join(key)
|
return "".join(key)
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
def __encryptPart(messagePart: str, character2Number: dict[str, str]) -> str:
|
def __encrypt_part(message_part: str, character_to_number: dict[str, str]) -> str:
|
||||||
one, two, three = "", "", ""
|
one, two, three = "", "", ""
|
||||||
tmp = []
|
tmp = []
|
||||||
|
|
||||||
for character in messagePart:
|
for character in message_part:
|
||||||
tmp.append(character2Number[character])
|
tmp.append(character_to_number[character])
|
||||||
|
|
||||||
for each in tmp:
|
for each in tmp:
|
||||||
one += each[0]
|
one += each[0]
|
||||||
@ -17,18 +17,18 @@ def __encryptPart(messagePart: str, character2Number: dict[str, str]) -> str:
|
|||||||
return one + two + three
|
return one + two + three
|
||||||
|
|
||||||
|
|
||||||
def __decryptPart(
|
def __decrypt_part(
|
||||||
messagePart: str, character2Number: dict[str, str]
|
message_part: str, character_to_number: dict[str, str]
|
||||||
) -> tuple[str, str, str]:
|
) -> tuple[str, str, str]:
|
||||||
tmp, thisPart = "", ""
|
tmp, this_part = "", ""
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
for character in messagePart:
|
for character in message_part:
|
||||||
thisPart += character2Number[character]
|
this_part += character_to_number[character]
|
||||||
|
|
||||||
for digit in thisPart:
|
for digit in this_part:
|
||||||
tmp += digit
|
tmp += digit
|
||||||
if len(tmp) == len(messagePart):
|
if len(tmp) == len(message_part):
|
||||||
result.append(tmp)
|
result.append(tmp)
|
||||||
tmp = ""
|
tmp = ""
|
||||||
|
|
||||||
@ -79,51 +79,57 @@ def __prepare(
|
|||||||
"332",
|
"332",
|
||||||
"333",
|
"333",
|
||||||
)
|
)
|
||||||
character2Number = {}
|
character_to_number = {}
|
||||||
number2Character = {}
|
number_to_character = {}
|
||||||
for letter, number in zip(alphabet, numbers):
|
for letter, number in zip(alphabet, numbers):
|
||||||
character2Number[letter] = number
|
character_to_number[letter] = number
|
||||||
number2Character[number] = letter
|
number_to_character[number] = letter
|
||||||
|
|
||||||
return message, alphabet, character2Number, number2Character
|
return message, alphabet, character_to_number, number_to_character
|
||||||
|
|
||||||
|
|
||||||
def encryptMessage(
|
def encrypt_message(
|
||||||
message: str, alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period: int = 5
|
message: str, alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period: int = 5
|
||||||
) -> str:
|
) -> str:
|
||||||
message, alphabet, character2Number, number2Character = __prepare(message, alphabet)
|
message, alphabet, character_to_number, number_to_character = __prepare(
|
||||||
|
message, alphabet
|
||||||
|
)
|
||||||
encrypted, encrypted_numeric = "", ""
|
encrypted, encrypted_numeric = "", ""
|
||||||
|
|
||||||
for i in range(0, len(message) + 1, period):
|
for i in range(0, len(message) + 1, period):
|
||||||
encrypted_numeric += __encryptPart(message[i : i + period], character2Number)
|
encrypted_numeric += __encrypt_part(
|
||||||
|
message[i : i + period], character_to_number
|
||||||
|
)
|
||||||
|
|
||||||
for i in range(0, len(encrypted_numeric), 3):
|
for i in range(0, len(encrypted_numeric), 3):
|
||||||
encrypted += number2Character[encrypted_numeric[i : i + 3]]
|
encrypted += number_to_character[encrypted_numeric[i : i + 3]]
|
||||||
|
|
||||||
return encrypted
|
return encrypted
|
||||||
|
|
||||||
|
|
||||||
def decryptMessage(
|
def decrypt_message(
|
||||||
message: str, alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period: int = 5
|
message: str, alphabet: str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period: int = 5
|
||||||
) -> str:
|
) -> str:
|
||||||
message, alphabet, character2Number, number2Character = __prepare(message, alphabet)
|
message, alphabet, character_to_number, number_to_character = __prepare(
|
||||||
|
message, alphabet
|
||||||
|
)
|
||||||
decrypted_numeric = []
|
decrypted_numeric = []
|
||||||
decrypted = ""
|
decrypted = ""
|
||||||
|
|
||||||
for i in range(0, len(message) + 1, period):
|
for i in range(0, len(message) + 1, period):
|
||||||
a, b, c = __decryptPart(message[i : i + period], character2Number)
|
a, b, c = __decrypt_part(message[i : i + period], character_to_number)
|
||||||
|
|
||||||
for j in range(0, len(a)):
|
for j in range(0, len(a)):
|
||||||
decrypted_numeric.append(a[j] + b[j] + c[j])
|
decrypted_numeric.append(a[j] + b[j] + c[j])
|
||||||
|
|
||||||
for each in decrypted_numeric:
|
for each in decrypted_numeric:
|
||||||
decrypted += number2Character[each]
|
decrypted += number_to_character[each]
|
||||||
|
|
||||||
return decrypted
|
return decrypted
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
msg = "DEFEND THE EAST WALL OF THE CASTLE."
|
msg = "DEFEND THE EAST WALL OF THE CASTLE."
|
||||||
encrypted = encryptMessage(msg, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
|
encrypted = encrypt_message(msg, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
|
||||||
decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
|
decrypted = decrypt_message(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ")
|
||||||
print(f"Encrypted: {encrypted}\nDecrypted: {decrypted}")
|
print(f"Encrypted: {encrypted}\nDecrypted: {decrypted}")
|
||||||
|
@ -10,57 +10,57 @@ text. The type of transposition cipher demonstrated under is the ROUTE cipher.
|
|||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
message = input("Enter message: ")
|
message = input("Enter message: ")
|
||||||
key = int(input("Enter key [2-%s]: " % (len(message) - 1)))
|
key = int(input(f"Enter key [2-{len(message) - 1}]: "))
|
||||||
mode = input("Encryption/Decryption [e/d]: ")
|
mode = input("Encryption/Decryption [e/d]: ")
|
||||||
|
|
||||||
if mode.lower().startswith("e"):
|
if mode.lower().startswith("e"):
|
||||||
text = encryptMessage(key, message)
|
text = encrypt_message(key, message)
|
||||||
elif mode.lower().startswith("d"):
|
elif mode.lower().startswith("d"):
|
||||||
text = decryptMessage(key, message)
|
text = decrypt_message(key, message)
|
||||||
|
|
||||||
# Append pipe symbol (vertical bar) to identify spaces at the end.
|
# Append pipe symbol (vertical bar) to identify spaces at the end.
|
||||||
print("Output:\n%s" % (text + "|"))
|
print(f"Output:\n{text + '|'}")
|
||||||
|
|
||||||
|
|
||||||
def encryptMessage(key: int, message: str) -> str:
|
def encrypt_message(key: int, message: str) -> str:
|
||||||
"""
|
"""
|
||||||
>>> encryptMessage(6, 'Harshil Darji')
|
>>> encrypt_message(6, 'Harshil Darji')
|
||||||
'Hlia rDsahrij'
|
'Hlia rDsahrij'
|
||||||
"""
|
"""
|
||||||
cipherText = [""] * key
|
cipher_text = [""] * key
|
||||||
for col in range(key):
|
for col in range(key):
|
||||||
pointer = col
|
pointer = col
|
||||||
while pointer < len(message):
|
while pointer < len(message):
|
||||||
cipherText[col] += message[pointer]
|
cipher_text[col] += message[pointer]
|
||||||
pointer += key
|
pointer += key
|
||||||
return "".join(cipherText)
|
return "".join(cipher_text)
|
||||||
|
|
||||||
|
|
||||||
def decryptMessage(key: int, message: str) -> str:
|
def decrypt_message(key: int, message: str) -> str:
|
||||||
"""
|
"""
|
||||||
>>> decryptMessage(6, 'Hlia rDsahrij')
|
>>> decrypt_message(6, 'Hlia rDsahrij')
|
||||||
'Harshil Darji'
|
'Harshil Darji'
|
||||||
"""
|
"""
|
||||||
numCols = math.ceil(len(message) / key)
|
num_cols = math.ceil(len(message) / key)
|
||||||
numRows = key
|
num_rows = key
|
||||||
numShadedBoxes = (numCols * numRows) - len(message)
|
num_shaded_boxes = (num_cols * num_rows) - len(message)
|
||||||
plainText = [""] * numCols
|
plain_text = [""] * num_cols
|
||||||
col = 0
|
col = 0
|
||||||
row = 0
|
row = 0
|
||||||
|
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
plainText[col] += symbol
|
plain_text[col] += symbol
|
||||||
col += 1
|
col += 1
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(col == numCols)
|
(col == num_cols)
|
||||||
or (col == numCols - 1)
|
or (col == num_cols - 1)
|
||||||
and (row >= numRows - numShadedBoxes)
|
and (row >= num_rows - num_shaded_boxes)
|
||||||
):
|
):
|
||||||
col = 0
|
col = 0
|
||||||
row += 1
|
row += 1
|
||||||
|
|
||||||
return "".join(plainText)
|
return "".join(plain_text)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user