mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
[pre-commit.ci] pre-commit autoupdate (#11322)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
5f95d6f805
commit
bc8df6de31
|
@ -16,7 +16,7 @@ repos:
|
||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.2.2
|
rev: v0.3.2
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
- id: ruff-format
|
- id: ruff-format
|
||||||
|
@ -47,7 +47,7 @@ repos:
|
||||||
- id: validate-pyproject
|
- id: validate-pyproject
|
||||||
|
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v1.8.0
|
rev: v1.9.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
args:
|
args:
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
"""
|
"""
|
||||||
In this problem, we want to determine all possible combinations of k
|
In this problem, we want to determine all possible combinations of k
|
||||||
numbers out of 1 ... n. We use backtracking to solve this problem.
|
numbers out of 1 ... n. We use backtracking to solve this problem.
|
||||||
|
|
||||||
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))),
|
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!))),
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
"""
|
"""
|
||||||
In this problem, we want to determine all possible permutations
|
In this problem, we want to determine all possible permutations
|
||||||
of the given sequence. We use backtracking to solve this problem.
|
of the given sequence. We use backtracking to solve this problem.
|
||||||
|
|
||||||
Time complexity: O(n! * n),
|
Time complexity: O(n! * n),
|
||||||
where n denotes the length of the given sequence.
|
where n denotes the length of the given sequence.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ of the given sequence. We use backtracking to solve this problem.
|
||||||
Time complexity: O(2^n),
|
Time complexity: O(2^n),
|
||||||
where n denotes the length of the given sequence.
|
where n denotes the length of the given sequence.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
"""
|
"""
|
||||||
Graph Coloring also called "m coloring problem"
|
Graph Coloring also called "m coloring problem"
|
||||||
consists of coloring a given graph with at most m colors
|
consists of coloring a given graph with at most m colors
|
||||||
such that no adjacent vertices are assigned the same color
|
such that no adjacent vertices are assigned the same color
|
||||||
|
|
||||||
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
|
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
"""
|
"""
|
||||||
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
|
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
|
||||||
through a graph that visits each node exactly once.
|
through a graph that visits each node exactly once.
|
||||||
Determining whether such paths and cycles exist in graphs
|
Determining whether such paths and cycles exist in graphs
|
||||||
is the 'Hamiltonian path problem', which is NP-complete.
|
is the 'Hamiltonian path problem', which is NP-complete.
|
||||||
|
|
||||||
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
|
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ if move is of maximizer return true else false
|
||||||
leaves of game tree is stored in scores[]
|
leaves of game tree is stored in scores[]
|
||||||
height is maximum height of Game tree
|
height is maximum height of Game tree
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
The nqueens problem is of placing N queens on a N * N
|
The nqueens problem is of placing N queens on a N * N
|
||||||
chess board such that no queen can attack any other queens placed
|
chess board such that no queen can attack any other queens placed
|
||||||
on that chess board.
|
on that chess board.
|
||||||
This means that one queen cannot have any other queen on its horizontal, vertical and
|
This means that one queen cannot have any other queen on its horizontal, vertical and
|
||||||
diagonal lines.
|
diagonal lines.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
solution = []
|
solution = []
|
||||||
|
|
|
@ -75,6 +75,7 @@ Applying these two formulas we can check if a queen in some position is being at
|
||||||
for another one or vice versa.
|
for another one or vice versa.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ function on the next column to see if it returns True. if yes, we
|
||||||
have solved the puzzle. else, we backtrack and place another number
|
have solved the puzzle. else, we backtrack and place another number
|
||||||
in that cell and repeat this process.
|
in that cell and repeat this process.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
Matrix = list[list[int]]
|
Matrix = list[list[int]]
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
"""
|
"""
|
||||||
The sum-of-subsetsproblem states that a set of non-negative integers, and a
|
The sum-of-subsetsproblem states that a set of non-negative integers, and a
|
||||||
value M, determine all possible subsets of the given set whose summation sum
|
value M, determine all possible subsets of the given set whose summation sum
|
||||||
equal to given M.
|
equal to given M.
|
||||||
|
|
||||||
Summation of the chosen numbers must be equal to given number M and one number
|
Summation of the chosen numbers must be equal to given number M and one number
|
||||||
can be used only once.
|
can be used only once.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ Following is the truth table of a NOR Gate:
|
||||||
Code provided by Akshaj Vishwanathan
|
Code provided by Akshaj Vishwanathan
|
||||||
https://www.geeksforgeeks.org/logic-gates-in-python
|
https://www.geeksforgeeks.org/logic-gates-in-python
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
Conway's Game of Life implemented in Python.
|
Conway's Game of Life implemented in Python.
|
||||||
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
|
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
|
@ -26,7 +26,8 @@ Game-Of-Life Rules:
|
||||||
4.
|
4.
|
||||||
Any dead cell with exactly three live neighbours be-
|
Any dead cell with exactly three live neighbours be-
|
||||||
comes a live cell, as if by reproduction.
|
comes a live cell, as if by reproduction.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ Examples for doctest:
|
||||||
>>> simulate(construct_highway(5, 2, -2), 3, 0, 2)
|
>>> simulate(construct_highway(5, 2, -2), 3, 0, 2)
|
||||||
[[0, -1, 0, -1, 0], [0, -1, 0, -1, -1], [0, -1, -1, 1, -1], [-1, 1, -1, 0, -1]]
|
[[0, -1, 0, -1, 0], [0, -1, 0, -1, -1], [0, -1, -1, 1, -1], [-1, 1, -1, 0, -1]]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from random import randint, random
|
from random import randint, random
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ corresponding to the character's position in the alphabet.
|
||||||
https://www.dcode.fr/letter-number-cipher
|
https://www.dcode.fr/letter-number-cipher
|
||||||
http://bestcodes.weebly.com/a1z26.html
|
http://bestcodes.weebly.com/a1z26.html
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
""" https://en.wikipedia.org/wiki/Atbash """
|
"""https://en.wikipedia.org/wiki/Atbash"""
|
||||||
|
|
||||||
import string
|
import string
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ Base32 encoding and decoding
|
||||||
|
|
||||||
https://en.wikipedia.org/wiki/Base32
|
https://en.wikipedia.org/wiki/Base32
|
||||||
"""
|
"""
|
||||||
|
|
||||||
B32_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
|
B32_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ Module includes:
|
||||||
|
|
||||||
Created by TrapinchO
|
Created by TrapinchO
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
RotorPositionT = tuple[int, int, int]
|
RotorPositionT = tuple[int, int, int]
|
||||||
|
|
|
@ -8,6 +8,7 @@ making it more secure than substitution ciphers.
|
||||||
|
|
||||||
http://practicalcryptography.com/ciphers/fractionated-morse-cipher/
|
http://practicalcryptography.com/ciphers/fractionated-morse-cipher/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import string
|
import string
|
||||||
|
|
||||||
MORSE_CODE_DICT = {
|
MORSE_CODE_DICT = {
|
||||||
|
|
|
@ -35,6 +35,7 @@ https://www.youtube.com/watch?v=kfmNeskzs2o
|
||||||
https://www.youtube.com/watch?v=4RhLNDqcjpA
|
https://www.youtube.com/watch?v=4RhLNDqcjpA
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import string
|
import string
|
||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
|
|
|
@ -7,6 +7,7 @@ determine the order of character rearrangement.
|
||||||
|
|
||||||
For more info: https://www.nku.edu/~christensen/1402%20permutation%20ciphers.pdf
|
For more info: https://www.nku.edu/~christensen/1402%20permutation%20ciphers.pdf
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
""" https://en.wikipedia.org/wiki/Rail_fence_cipher """
|
"""https://en.wikipedia.org/wiki/Rail_fence_cipher"""
|
||||||
|
|
||||||
|
|
||||||
def encrypt(input_string: str, key: int) -> str:
|
def encrypt(input_string: str, key: int) -> str:
|
||||||
|
|
|
@ -7,6 +7,7 @@ Source: on page 3 of https://crypto.stanford.edu/~dabo/papers/RSA-survey.pdf
|
||||||
More readable source: https://www.di-mgt.com.au/rsa_factorize_n.html
|
More readable source: https://www.di-mgt.com.au/rsa_factorize_n.html
|
||||||
large number can take minutes to factor, therefore are not included in doctest.
|
large number can take minutes to factor, therefore are not included in doctest.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
"""
|
"""
|
||||||
author: Christian Bender
|
author: Christian Bender
|
||||||
date: 21.12.2017
|
date: 21.12.2017
|
||||||
class: XORCipher
|
class: XORCipher
|
||||||
|
|
||||||
This class implements the XOR-cipher algorithm and provides
|
This class implements the XOR-cipher algorithm and provides
|
||||||
some useful methods for encrypting and decrypting strings and
|
some useful methods for encrypting and decrypting strings and
|
||||||
files.
|
files.
|
||||||
|
|
||||||
Overview about methods
|
Overview about methods
|
||||||
|
|
||||||
- encrypt : list of char
|
- encrypt : list of char
|
||||||
- decrypt : list of char
|
- decrypt : list of char
|
||||||
- encrypt_string : str
|
- encrypt_string : str
|
||||||
- decrypt_string : str
|
- decrypt_string : str
|
||||||
- encrypt_file : boolean
|
- encrypt_file : boolean
|
||||||
- decrypt_file : boolean
|
- decrypt_file : boolean
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ without needing to store any additional data except the position of the first
|
||||||
original character. The BWT is thus a "free" method of improving the efficiency
|
original character. The BWT is thus a "free" method of improving the efficiency
|
||||||
of text compression algorithms, costing only some extra computation.
|
of text compression algorithms, costing only some extra computation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TypedDict
|
from typing import TypedDict
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""
|
"""
|
||||||
One of the several implementations of Lempel–Ziv–Welch compression algorithm
|
One of the several implementations of Lempel–Ziv–Welch compression algorithm
|
||||||
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
|
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""
|
"""
|
||||||
One of the several implementations of Lempel–Ziv–Welch decompression algorithm
|
One of the several implementations of Lempel–Ziv–Welch decompression algorithm
|
||||||
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
|
https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
|
@ -28,7 +28,6 @@ Sources:
|
||||||
en.wikipedia.org/wiki/LZ77_and_LZ78
|
en.wikipedia.org/wiki/LZ77_and_LZ78
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
__version__ = "0.1"
|
__version__ = "0.1"
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
https://en.wikipedia.org/wiki/Image_texture
|
https://en.wikipedia.org/wiki/Image_texture
|
||||||
https://en.wikipedia.org/wiki/Co-occurrence_matrix#Application_to_image_analysis
|
https://en.wikipedia.org/wiki/Co-occurrence_matrix#Application_to_image_analysis
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import imageio.v2 as imageio
|
import imageio.v2 as imageio
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
"""
|
"""
|
||||||
The Horn-Schunck method estimates the optical flow for every single pixel of
|
The Horn-Schunck method estimates the optical flow for every single pixel of
|
||||||
a sequence of images.
|
a sequence of images.
|
||||||
It works by assuming brightness constancy between two consecutive frames
|
It works by assuming brightness constancy between two consecutive frames
|
||||||
and smoothness in the optical flow.
|
and smoothness in the optical flow.
|
||||||
|
|
||||||
Useful resources:
|
Useful resources:
|
||||||
Wikipedia: https://en.wikipedia.org/wiki/Horn%E2%80%93Schunck_method
|
Wikipedia: https://en.wikipedia.org/wiki/Horn%E2%80%93Schunck_method
|
||||||
Paper: http://image.diku.dk/imagecanon/material/HornSchunckOptical_Flow.pdf
|
Paper: http://image.diku.dk/imagecanon/material/HornSchunckOptical_Flow.pdf
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import SupportsIndex
|
from typing import SupportsIndex
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
""" Convert Base 10 (Decimal) Values to Hexadecimal Representations """
|
"""Convert Base 10 (Decimal) Values to Hexadecimal Representations"""
|
||||||
|
|
||||||
# set decimal value for each hexadecimal digit
|
# set decimal value for each hexadecimal digit
|
||||||
values = {
|
values = {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Convert International System of Units (SI) and Binary prefixes
|
Convert International System of Units (SI) and Binary prefixes
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
""" Convert between different units of temperature """
|
"""Convert between different units of temperature"""
|
||||||
|
|
||||||
|
|
||||||
def celsius_to_fahrenheit(celsius: float, ndigits: int = 2) -> float:
|
def celsius_to_fahrenheit(celsius: float, ndigits: int = 2) -> float:
|
||||||
|
|
|
@ -6,6 +6,7 @@ elements whose sum is equal to req_sum.
|
||||||
|
|
||||||
https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0
|
https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from itertools import combinations
|
from itertools import combinations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
"""
|
"""
|
||||||
Sparse table is a data structure that allows answering range queries on
|
Sparse table is a data structure that allows answering range queries on
|
||||||
a static number list, i.e. the elements do not change throughout all the queries.
|
a static number list, i.e. the elements do not change throughout all the queries.
|
||||||
|
|
||||||
The implementation below will solve the problem of Range Minimum Query:
|
The implementation below will solve the problem of Range Minimum Query:
|
||||||
Finding the minimum value of a subset [L..R] of a static number list.
|
Finding the minimum value of a subset [L..R] of a static number list.
|
||||||
|
|
||||||
Overall time complexity: O(nlogn)
|
Overall time complexity: O(nlogn)
|
||||||
Overall space complexity: O(nlogn)
|
Overall space complexity: O(nlogn)
|
||||||
|
|
||||||
Wikipedia link: https://en.wikipedia.org/wiki/Range_minimum_query
|
Wikipedia link: https://en.wikipedia.org/wiki/Range_minimum_query
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from math import log2
|
from math import log2
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ Please do not modify this file! It is published at https://norvig.com/sudoku.ht
|
||||||
only minimal changes to work with modern versions of Python. If you have improvements,
|
only minimal changes to work with modern versions of Python. If you have improvements,
|
||||||
please make them in a separate file.
|
please make them in a separate file.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ python3 -m doctest -v avl_tree.py
|
||||||
For testing run:
|
For testing run:
|
||||||
python avl_tree.py
|
python avl_tree.py
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
|
@ -88,6 +88,7 @@ True
|
||||||
>>> not t
|
>>> not t
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterable, Iterator
|
from collections.abc import Iterable, Iterator
|
||||||
|
|
|
@ -7,6 +7,7 @@ python -m unittest binary_search_tree_recursive.py
|
||||||
To run an example:
|
To run an example:
|
||||||
python binary_search_tree_recursive.py
|
python binary_search_tree_recursive.py
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
|
@ -8,7 +8,6 @@ Python implementation:
|
||||||
frames that could be in memory is `n`
|
frames that could be in memory is `n`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
The diameter/width of a tree is defined as the number of nodes on the longest path
|
The diameter/width of a tree is defined as the number of nodes on the longest path
|
||||||
between two end nodes.
|
between two end nodes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
|
@ -10,6 +10,7 @@ https://www.geeksforgeeks.org/flatten-a-binary-tree-into-linked-list
|
||||||
Author: Arunkumar A
|
Author: Arunkumar A
|
||||||
Date: 04/09/2023
|
Date: 04/09/2023
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ https://bit.ly/46uB0a2
|
||||||
Author : Arunkumar
|
Author : Arunkumar
|
||||||
Date : 14th October 2023
|
Date : 14th October 2023
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -13,6 +13,7 @@ If n is the number of nodes in the tree then:
|
||||||
Runtime: O(n)
|
Runtime: O(n)
|
||||||
Space: O(1)
|
Space: O(1)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -3,6 +3,7 @@ Is a binary tree a sum tree where the value of every non-leaf node is equal to t
|
||||||
of the values of its left and right subtrees?
|
of the values of its left and right subtrees?
|
||||||
https://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-sumtree
|
https://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-sumtree
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -5,6 +5,7 @@ The rule for merging is that if two nodes overlap, then put the value sum of
|
||||||
both nodes to the new value of the merged node. Otherwise, the NOT null node
|
both nodes to the new value of the merged node. Otherwise, the NOT null node
|
||||||
will be used as the node of new tree.
|
will be used as the node of new tree.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ Given the root of a binary tree, mirror the tree, and return its root.
|
||||||
|
|
||||||
Leetcode problem reference: https://leetcode.com/problems/mirror-binary-tree/
|
Leetcode problem reference: https://leetcode.com/problems/mirror-binary-tree/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -35,6 +35,7 @@ https://www.geeksforgeeks.org/segment-tree-efficient-implementation/
|
||||||
>>> st.query(0, 2)
|
>>> st.query(0, 2)
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
|
|
@ -6,6 +6,7 @@ We will use the formula: t(n) = SUMMATION(i = 1 to n)t(i-1)t(n-i)
|
||||||
|
|
||||||
Further details at Wikipedia: https://en.wikipedia.org/wiki/Catalan_number
|
Further details at Wikipedia: https://en.wikipedia.org/wiki/Catalan_number
|
||||||
"""
|
"""
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Our Contribution:
|
Our Contribution:
|
||||||
Basically we Create the 2 function:
|
Basically we Create the 2 function:
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
psf/black : true
|
psf/black : true
|
||||||
ruff : passed
|
ruff : passed
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -3,6 +3,7 @@ Segment_tree creates a segment tree with a given array and function,
|
||||||
allowing queries to be done later in log(N) time
|
allowing queries to be done later in log(N) time
|
||||||
function takes 2 values and returns a same type value
|
function takes 2 values and returns a same type value
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ Given the root of a binary tree, check whether it is a mirror of itself
|
||||||
|
|
||||||
Leetcode reference: https://leetcode.com/problems/symmetric-tree/
|
Leetcode reference: https://leetcode.com/problems/symmetric-tree/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
|
@ -7,6 +7,7 @@ such as the with segment trees or fenwick trees. You can read more about them he
|
||||||
2. https://www.youtube.com/watch?v=4aSv9PcecDw&t=811s
|
2. https://www.youtube.com/watch?v=4aSv9PcecDw&t=811s
|
||||||
3. https://www.youtube.com/watch?v=CybAgVF-MMc&t=1178s
|
3. https://www.youtube.com/watch?v=CybAgVF-MMc&t=1178s
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
test_array = [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]
|
test_array = [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""
|
"""
|
||||||
Disjoint set.
|
Disjoint set.
|
||||||
Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
|
Reference: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,7 @@ The probability decreases with the number of bits in the bitarray.
|
||||||
>>> bloom.bitstring
|
>>> bloom.bitstring
|
||||||
'01100101'
|
'01100101'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from hashlib import md5, sha256
|
from hashlib import md5, sha256
|
||||||
|
|
||||||
HASH_FUNCTIONS = (sha256, md5)
|
HASH_FUNCTIONS = (sha256, md5)
|
||||||
|
|
|
@ -11,6 +11,7 @@ Where hash1() and hash2() are hash functions and TABLE_SIZE is size of hash tabl
|
||||||
|
|
||||||
Reference: https://en.wikipedia.org/wiki/Double_hashing
|
Reference: https://en.wikipedia.org/wiki/Double_hashing
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .hash_table import HashTable
|
from .hash_table import HashTable
|
||||||
from .number_theory.prime_numbers import is_prime, next_prime
|
from .number_theory.prime_numbers import is_prime, next_prime
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ Another hash map implementation, with a good explanation.
|
||||||
Modern Dictionaries by Raymond Hettinger
|
Modern Dictionaries by Raymond Hettinger
|
||||||
https://www.youtube.com/watch?v=p33CVV29OG8
|
https://www.youtube.com/watch?v=p33CVV29OG8
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Iterator, MutableMapping
|
from collections.abc import Iterator, MutableMapping
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Generic, TypeVar
|
from typing import Generic, TypeVar
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
module to operations with prime numbers
|
module to operations with prime numbers
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
|
@ -5,6 +5,7 @@ Nodes contain data and also may link to other nodes:
|
||||||
head node gives us access of the complete list
|
head node gives us access of the complete list
|
||||||
- Last node: points to null
|
- Last node: points to null
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Algorithm that merges two sorted linked lists into one sorted linked list.
|
Algorithm that merges two sorted linked lists into one sorted linked list.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterable, Iterator
|
from collections.abc import Iterable, Iterator
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
Based on "Skip Lists: A Probabilistic Alternative to Balanced Trees" by William Pugh
|
Based on "Skip Lists: A Probabilistic Alternative to Balanced Trees" by William Pugh
|
||||||
https://epaperpress.com/sortsearch/download/skiplist.pdf
|
https://epaperpress.com/sortsearch/download/skiplist.pdf
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from random import random
|
from random import random
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Implementation of double ended queue.
|
Implementation of double ended queue.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
""" A Queue using a linked list like structure """
|
"""A Queue using a linked list like structure"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
"""Queue represented by a pseudo stack (represented by a list with pop and append)"""
|
"""Queue represented by a pseudo stack (represented by a list with pop and append)"""
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ RULE 5: When the entire infix expression has been scanned, the value left on
|
||||||
|
|
||||||
NOTE: It only works with whole numbers.
|
NOTE: It only works with whole numbers.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__author__ = "Alexander Joslin"
|
__author__ = "Alexander Joslin"
|
||||||
|
|
||||||
import operator as op
|
import operator as op
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
""" A Stack using a linked list like structure """
|
"""A Stack using a linked list like structure"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Implemented an algorithm using opencv to convert a colored image into its negative
|
Implemented an algorithm using opencv to convert a colored image into its negative
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Implementation Burke's algorithm (dithering)
|
Implementation Burke's algorithm (dithering)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ Inputs:
|
||||||
Output:
|
Output:
|
||||||
img:A 2d zero padded image with values in between 0 and 1
|
img:A 2d zero padded image with values in between 0 and 1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Implementation of gaussian filter algorithm
|
Implementation of gaussian filter algorithm
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from itertools import product
|
from itertools import product
|
||||||
|
|
||||||
from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey
|
from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Implementation of median filter algorithm
|
Implementation of median filter algorithm
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey
|
from cv2 import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey
|
||||||
from numpy import divide, int8, multiply, ravel, sort, zeros_like
|
from numpy import divide, int8, multiply, ravel, sort, zeros_like
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ Created on Fri Sep 28 15:22:29 2018
|
||||||
|
|
||||||
@author: Binish125
|
@author: Binish125
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
""" Multiple image resizing techniques """
|
"""Multiple image resizing techniques"""
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
Implemented an algorithm using opencv to tone an image with sepia technique
|
Implemented an algorithm using opencv to tone an image with sepia technique
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
from cv2 import destroyAllWindows, imread, imshow, waitKey
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
PyTest's for Digital Image Processing
|
PyTest's for Digital Image Processing
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from cv2 import COLOR_BGR2GRAY, cvtColor, imread
|
from cv2 import COLOR_BGR2GRAY, cvtColor, imread
|
||||||
from numpy import array, uint8
|
from numpy import array, uint8
|
||||||
|
|
|
@ -12,6 +12,7 @@ There are other several other algorithms for the convex hull problem
|
||||||
which have not been implemented here, yet.
|
which have not been implemented here, yet.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
|
|
|
@ -8,6 +8,7 @@ This is a divide and conquer algorithm that can find a solution in O(n) time.
|
||||||
For more information of this algorithm:
|
For more information of this algorithm:
|
||||||
https://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/08/Small08.pdf
|
https://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/08/Small08.pdf
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from random import choice
|
from random import choice
|
||||||
|
|
|
@ -6,6 +6,7 @@ maximum sum within a given array of numbers. For example, given the array
|
||||||
|
|
||||||
This divide-and-conquer algorithm finds the maximum subarray in O(n log n) time.
|
This divide-and-conquer algorithm finds the maximum subarray in O(n log n) time.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
|
@ -7,6 +7,7 @@ to find the maximum of the array.
|
||||||
(From Kleinberg and Tardos. Algorithm Design.
|
(From Kleinberg and Tardos. Algorithm Design.
|
||||||
Addison Wesley 2006: Chapter 5 Solved Exercise 1)
|
Addison Wesley 2006: Chapter 5 Solved Exercise 1)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
Program to list all the ways a target string can be
|
Program to list all the ways a target string can be
|
||||||
constructed from the given list of substrings
|
constructed from the given list of substrings
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ We have N tasks and M people. Each person in M can do only certain of these task
|
||||||
a person can do only one task and a task is performed only by one person.
|
a person can do only one task and a task is performed only by one person.
|
||||||
Find the total no of ways in which the tasks can be distributed.
|
Find the total no of ways in which the tasks can be distributed.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
This program calculates the nth Fibonacci number in O(log(n)).
|
This program calculates the nth Fibonacci number in O(log(n)).
|
||||||
It's possible to calculate F(1_000_000) in less than a second.
|
It's possible to calculate F(1_000_000) in less than a second.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
|
@ -5,6 +5,7 @@ You are given a bitmask m and you want to efficiently iterate through all of
|
||||||
its submasks. The mask s is submask of m if only bits that were included in
|
its submasks. The mask s is submask of m if only bits that were included in
|
||||||
bitmask are set
|
bitmask are set
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ return it.
|
||||||
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return
|
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return
|
||||||
[10, 22, 33, 41, 60, 80] as output
|
[10, 22, 33, 41, 60, 80] as output
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@ Example input:
|
||||||
arr = [40, 20, 30, 10, 30]
|
arr = [40, 20, 30, 10, 30]
|
||||||
output: 26000
|
output: 26000
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from functools import cache
|
from functools import cache
|
||||||
|
|
|
@ -9,6 +9,7 @@ subarray sum problem in O(n) time and O(1) space.
|
||||||
|
|
||||||
Reference: https://en.wikipedia.org/wiki/Maximum_subarray_problem
|
Reference: https://en.wikipedia.org/wiki/Maximum_subarray_problem
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ represented as τ (tau). By using this RC-timeconstant we can find the voltage
|
||||||
time 't' from the initiation of charging a capacitor with the help of the exponential
|
time 't' from the initiation of charging a capacitor with the help of the exponential
|
||||||
function containing RC. Both at charging and discharging of a capacitor.
|
function containing RC. Both at charging and discharging of a capacitor.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from math import exp # value of exp = 2.718281828459…
|
from math import exp # value of exp = 2.718281828459…
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ when inductor is connected across 'AC' potential source. It starts to store the
|
||||||
in its 'magnetic field'.with the help 'RL-time-constant' we can find current at any time
|
in its 'magnetic field'.with the help 'RL-time-constant' we can find current at any time
|
||||||
in inductor while it is charging.
|
in inductor while it is charging.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from math import exp # value of exp = 2.718281828459…
|
from math import exp # value of exp = 2.718281828459…
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,7 @@ Sources :
|
||||||
https://learn.parallax.com/support/reference/resistor-color-codes
|
https://learn.parallax.com/support/reference/resistor-color-codes
|
||||||
https://byjus.com/physics/resistor-colour-codes/
|
https://byjus.com/physics/resistor-colour-codes/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
valid_colors: list = [
|
valid_colors: list = [
|
||||||
"Black",
|
"Black",
|
||||||
"Brown",
|
"Brown",
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
"""
|
"""
|
||||||
Calculate the exponential moving average (EMA) on the series of stock prices.
|
Calculate the exponential moving average (EMA) on the series of stock prices.
|
||||||
Wikipedia Reference: https://en.wikipedia.org/wiki/Exponential_smoothing
|
Wikipedia Reference: https://en.wikipedia.org/wiki/Exponential_smoothing
|
||||||
https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential
|
https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential
|
||||||
-moving-average-ema
|
-moving-average-ema
|
||||||
|
|
||||||
Exponential moving average is used in finance to analyze changes stock prices.
|
Exponential moving average is used in finance to analyze changes stock prices.
|
||||||
EMA is used in conjunction with Simple moving average (SMA), EMA reacts to the
|
EMA is used in conjunction with Simple moving average (SMA), EMA reacts to the
|
||||||
changes in the value quicker than SMA, which is one of the advantages of using EMA.
|
changes in the value quicker than SMA, which is one of the advantages of using EMA.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
|
|
@ -6,6 +6,7 @@ and identify trends.
|
||||||
|
|
||||||
Reference: https://en.wikipedia.org/wiki/Moving_average
|
Reference: https://en.wikipedia.org/wiki/Moving_average
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ Requirements (pip):
|
||||||
- numpy
|
- numpy
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import matplotlib.pyplot as plt # type: ignore
|
import matplotlib.pyplot as plt # type: ignore
|
||||||
|
|
|
@ -15,7 +15,6 @@ the boundary of the Mandelbrot set a fractal curve.
|
||||||
(see also https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set )
|
(see also https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set )
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import colorsys
|
import colorsys
|
||||||
|
|
||||||
from PIL import Image # type: ignore
|
from PIL import Image # type: ignore
|
||||||
|
|
|
@ -22,6 +22,7 @@ Credits:
|
||||||
This code was written by editing the code from
|
This code was written by editing the code from
|
||||||
https://www.riannetrujillo.com/blog/python-fractal/
|
https://www.riannetrujillo.com/blog/python-fractal/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import turtle
|
import turtle
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@ Reference: shorturl.at/exHM7
|
||||||
|
|
||||||
# Author: Swayam Singh (https://github.com/practice404)
|
# Author: Swayam Singh (https://github.com/practice404)
|
||||||
|
|
||||||
|
|
||||||
from queue import PriorityQueue
|
from queue import PriorityQueue
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
https://en.wikipedia.org/wiki/Bidirectional_search
|
https://en.wikipedia.org/wiki/Bidirectional_search
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""
|
"""
|
||||||
https://en.wikipedia.org/wiki/Bidirectional_search
|
https://en.wikipedia.org/wiki/Bidirectional_search
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
|
@ -1,29 +1,30 @@
|
||||||
"""Borůvka's algorithm.
|
"""Borůvka's algorithm.
|
||||||
|
|
||||||
Determines the minimum spanning tree (MST) of a graph using the Borůvka's algorithm.
|
Determines the minimum spanning tree (MST) of a graph using the Borůvka's algorithm.
|
||||||
Borůvka's algorithm is a greedy algorithm for finding a minimum spanning tree in a
|
Borůvka's algorithm is a greedy algorithm for finding a minimum spanning tree in a
|
||||||
connected graph, or a minimum spanning forest if a graph that is not connected.
|
connected graph, or a minimum spanning forest if a graph that is not connected.
|
||||||
|
|
||||||
The time complexity of this algorithm is O(ELogV), where E represents the number
|
The time complexity of this algorithm is O(ELogV), where E represents the number
|
||||||
of edges, while V represents the number of nodes.
|
of edges, while V represents the number of nodes.
|
||||||
O(number_of_edges Log number_of_nodes)
|
O(number_of_edges Log number_of_nodes)
|
||||||
|
|
||||||
The space complexity of this algorithm is O(V + E), since we have to keep a couple
|
The space complexity of this algorithm is O(V + E), since we have to keep a couple
|
||||||
of lists whose sizes are equal to the number of nodes, as well as keep all the
|
of lists whose sizes are equal to the number of nodes, as well as keep all the
|
||||||
edges of a graph inside of the data structure itself.
|
edges of a graph inside of the data structure itself.
|
||||||
|
|
||||||
Borůvka's algorithm gives us pretty much the same result as other MST Algorithms -
|
Borůvka's algorithm gives us pretty much the same result as other MST Algorithms -
|
||||||
they all find the minimum spanning tree, and the time complexity is approximately
|
they all find the minimum spanning tree, and the time complexity is approximately
|
||||||
the same.
|
the same.
|
||||||
|
|
||||||
One advantage that Borůvka's algorithm has compared to the alternatives is that it
|
One advantage that Borůvka's algorithm has compared to the alternatives is that it
|
||||||
doesn't need to presort the edges or maintain a priority queue in order to find the
|
doesn't need to presort the edges or maintain a priority queue in order to find the
|
||||||
minimum spanning tree.
|
minimum spanning tree.
|
||||||
Even though that doesn't help its complexity, since it still passes the edges logE
|
Even though that doesn't help its complexity, since it still passes the edges logE
|
||||||
times, it is a bit simpler to code.
|
times, it is a bit simpler to code.
|
||||||
|
|
||||||
Details: https://en.wikipedia.org/wiki/Bor%C5%AFvka%27s_algorithm
|
Details: https://en.wikipedia.org/wiki/Bor%C5%AFvka%27s_algorithm
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
""" Author: OMKAR PATHAK """
|
"""Author: OMKAR PATHAK"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user