mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Tighten up psf/black and flake8 (#2024)
* Tighten up psf/black and flake8
* Fix some tests
* Fix some E741
* Fix some E741
* updating DIRECTORY.md
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -9,7 +9,7 @@ def aliquot_sum(input_num: int) -> int:
|
||||
@return: the aliquot sum of input_num, if input_num is positive.
|
||||
Otherwise, raise a ValueError
|
||||
Wikipedia Explanation: https://en.wikipedia.org/wiki/Aliquot_sum
|
||||
|
||||
|
||||
>>> aliquot_sum(15)
|
||||
9
|
||||
>>> aliquot_sum(6)
|
||||
|
||||
@@ -4,8 +4,8 @@ from typing import List
|
||||
def allocation_num(number_of_bytes: int, partitions: int) -> List[str]:
|
||||
"""
|
||||
Divide a number of bytes into x partitions.
|
||||
|
||||
In a multi-threaded download, this algorithm could be used to provide
|
||||
|
||||
In a multi-threaded download, this algorithm could be used to provide
|
||||
each worker thread with a block of non-overlapping bytes to download.
|
||||
For example:
|
||||
for i in allocation_list:
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
def bailey_borwein_plouffe(digit_position: int, precision: int = 1000) -> str:
|
||||
"""
|
||||
Implement a popular pi-digit-extraction algorithm known as the
|
||||
Implement a popular pi-digit-extraction algorithm known as the
|
||||
Bailey-Borwein-Plouffe (BBP) formula to calculate the nth hex digit of pi.
|
||||
Wikipedia page:
|
||||
https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula
|
||||
@param digit_position: a positive integer representing the position of the digit to extract.
|
||||
@param digit_position: a positive integer representing the position of the digit to extract.
|
||||
The digit immediately after the decimal point is located at position 1.
|
||||
@param precision: number of terms in the second summation to calculate.
|
||||
A higher number reduces the chance of an error but increases the runtime.
|
||||
@return: a hexadecimal digit representing the digit at the nth position
|
||||
in pi's decimal expansion.
|
||||
|
||||
|
||||
>>> "".join(bailey_borwein_plouffe(i) for i in range(1, 11))
|
||||
'243f6a8885'
|
||||
>>> bailey_borwein_plouffe(5, 10000)
|
||||
@@ -59,11 +59,11 @@ def _subsum(
|
||||
# only care about first digit of fractional part; don't need decimal
|
||||
"""
|
||||
Private helper function to implement the summation
|
||||
functionality.
|
||||
functionality.
|
||||
@param digit_pos_to_extract: digit position to extract
|
||||
@param denominator_addend: added to denominator of fractions in the formula
|
||||
@param precision: same as precision in main function
|
||||
@return: floating-point number whose integer part is not important
|
||||
@return: floating-point number whose integer part is not important
|
||||
"""
|
||||
sum = 0.0
|
||||
for sum_index in range(digit_pos_to_extract + precision):
|
||||
|
||||
@@ -18,8 +18,9 @@ def collatz_sequence(n: int) -> List[int]:
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
Exception: Sequence only defined for natural numbers
|
||||
>>> collatz_sequence(43)
|
||||
[43, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
|
||||
>>> collatz_sequence(43) # doctest: +NORMALIZE_WHITESPACE
|
||||
[43, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56, 28, 14, 7,
|
||||
22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
|
||||
"""
|
||||
|
||||
if not isinstance(n, int) or n < 1:
|
||||
|
||||
@@ -6,7 +6,7 @@ def find_max(nums, left, right):
|
||||
:param left: index of first element
|
||||
:param right: index of last element
|
||||
:return: max in nums
|
||||
|
||||
|
||||
>>> nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
|
||||
>>> find_max(nums, 0, len(nums) - 1) == max(nums)
|
||||
True
|
||||
|
||||
@@ -6,8 +6,8 @@ from numpy import inf
|
||||
def gamma(num: float) -> float:
|
||||
"""
|
||||
https://en.wikipedia.org/wiki/Gamma_function
|
||||
In mathematics, the gamma function is one commonly
|
||||
used extension of the factorial function to complex numbers.
|
||||
In mathematics, the gamma function is one commonly
|
||||
used extension of the factorial function to complex numbers.
|
||||
The gamma function is defined for all complex numbers except the non-positive integers
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ def gamma(num: float) -> float:
|
||||
...
|
||||
ValueError: math domain error
|
||||
|
||||
|
||||
|
||||
|
||||
>>> gamma(0)
|
||||
Traceback (most recent call last):
|
||||
@@ -27,12 +27,12 @@ def gamma(num: float) -> float:
|
||||
>>> gamma(9)
|
||||
40320.0
|
||||
|
||||
>>> from math import gamma as math_gamma
|
||||
>>> from math import gamma as math_gamma
|
||||
>>> all(gamma(i)/math_gamma(i) <= 1.000000001 and abs(gamma(i)/math_gamma(i)) > .99999999 for i in range(1, 50))
|
||||
True
|
||||
|
||||
|
||||
>>> from math import gamma as math_gamma
|
||||
>>> from math import gamma as math_gamma
|
||||
>>> gamma(-1)/math_gamma(-1) <= 1.000000001
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
@@ -40,7 +40,7 @@ def gamma(num: float) -> float:
|
||||
|
||||
|
||||
>>> from math import gamma as math_gamma
|
||||
>>> gamma(3.3) - math_gamma(3.3) <= 0.00000001
|
||||
>>> gamma(3.3) - math_gamma(3.3) <= 0.00000001
|
||||
True
|
||||
"""
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
|
||||
"""
|
||||
>>> gaussian(1)
|
||||
0.24197072451914337
|
||||
|
||||
|
||||
>>> gaussian(24)
|
||||
3.342714441794458e-126
|
||||
|
||||
@@ -25,7 +25,7 @@ def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
|
||||
1.33830226e-04, 1.48671951e-06, 6.07588285e-09, 9.13472041e-12,
|
||||
5.05227108e-15, 1.02797736e-18, 7.69459863e-23, 2.11881925e-27,
|
||||
2.14638374e-32, 7.99882776e-38, 1.09660656e-43])
|
||||
|
||||
|
||||
>>> gaussian(15)
|
||||
5.530709549844416e-50
|
||||
|
||||
|
||||
@@ -13,12 +13,12 @@ def is_square_free(factors: List[int]) -> bool:
|
||||
returns True if the factors are square free.
|
||||
>>> is_square_free([1, 1, 2, 3, 4])
|
||||
False
|
||||
|
||||
|
||||
These are wrong but should return some value
|
||||
it simply checks for repition in the numbers.
|
||||
>>> is_square_free([1, 3, 4, 'sd', 0.0])
|
||||
True
|
||||
|
||||
|
||||
>>> is_square_free([1, 0.5, 2, 0.0])
|
||||
True
|
||||
>>> is_square_free([1, 2, 2, 5])
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
def kthPermutation(k, n):
|
||||
"""
|
||||
Finds k'th lexicographic permutation (in increasing order) of
|
||||
Finds k'th lexicographic permutation (in increasing order) of
|
||||
0,1,2,...n-1 in O(n^2) time.
|
||||
|
||||
|
||||
Examples:
|
||||
First permutation is always 0,1,2,...n
|
||||
>>> kthPermutation(0,5)
|
||||
[0, 1, 2, 3, 4]
|
||||
|
||||
|
||||
The order of permutation of 0,1,2,3 is [0,1,2,3], [0,1,3,2], [0,2,1,3],
|
||||
[0,2,3,1], [0,3,1,2], [0,3,2,1], [1,0,2,3], [1,0,3,2], [1,2,0,3],
|
||||
[0,2,3,1], [0,3,1,2], [0,3,2,1], [1,0,2,3], [1,0,3,2], [1,2,0,3],
|
||||
[1,2,3,0], [1,3,0,2]
|
||||
>>> kthPermutation(10,4)
|
||||
[1, 3, 0, 2]
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
"""
|
||||
In mathematics, the Lucas–Lehmer test (LLT) is a primality test for Mersenne numbers.
|
||||
https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test
|
||||
|
||||
|
||||
A Mersenne number is a number that is one less than a power of two.
|
||||
That is M_p = 2^p - 1
|
||||
https://en.wikipedia.org/wiki/Mersenne_prime
|
||||
|
||||
The Lucas–Lehmer test is the primality test used by the
|
||||
|
||||
The Lucas–Lehmer test is the primality test used by the
|
||||
Great Internet Mersenne Prime Search (GIMPS) to locate large primes.
|
||||
"""
|
||||
|
||||
@@ -17,10 +17,10 @@ def lucas_lehmer_test(p: int) -> bool:
|
||||
"""
|
||||
>>> lucas_lehmer_test(p=7)
|
||||
True
|
||||
|
||||
|
||||
>>> lucas_lehmer_test(p=11)
|
||||
False
|
||||
|
||||
|
||||
# M_11 = 2^11 - 1 = 2047 = 23 * 89
|
||||
"""
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import timeit
|
||||
|
||||
"""
|
||||
Matrix Exponentiation is a technique to solve linear recurrences in logarithmic time.
|
||||
You read more about it here:
|
||||
You read more about it here:
|
||||
http://zobayer.blogspot.com/2010/11/matrix-exponentiation.html
|
||||
https://www.hackerearth.com/practice/notes/matrix-exponentiation-1/
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
Modular Exponential.
|
||||
Modular exponentiation is a type of exponentiation performed over a modulus.
|
||||
Modular exponentiation is a type of exponentiation performed over a modulus.
|
||||
For more explanation, please check https://en.wikipedia.org/wiki/Modular_exponentiation
|
||||
"""
|
||||
|
||||
|
||||
@@ -45,13 +45,13 @@ def area_under_curve_estimator(
|
||||
) -> float:
|
||||
"""
|
||||
An implementation of the Monte Carlo method to find area under
|
||||
a single variable non-negative real-valued continuous function,
|
||||
say f(x), where x lies within a continuous bounded interval,
|
||||
say [min_value, max_value], where min_value and max_value are
|
||||
a single variable non-negative real-valued continuous function,
|
||||
say f(x), where x lies within a continuous bounded interval,
|
||||
say [min_value, max_value], where min_value and max_value are
|
||||
finite numbers
|
||||
1. Let x be a uniformly distributed random variable between min_value to
|
||||
1. Let x be a uniformly distributed random variable between min_value to
|
||||
max_value
|
||||
2. Expected value of f(x) =
|
||||
2. Expected value of f(x) =
|
||||
(integrate f(x) from min_value to max_value)/(max_value - min_value)
|
||||
3. Finding expected value of f(x):
|
||||
a. Repeatedly draw x from uniform distribution
|
||||
|
||||
@@ -24,7 +24,7 @@ def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=Fa
|
||||
a = x0 # set the initial guess
|
||||
steps = [a]
|
||||
error = abs(f(a))
|
||||
f1 = lambda x: calc_derivative(f, x, h=step) # Derivative of f(x)
|
||||
f1 = lambda x: calc_derivative(f, x, h=step) # noqa: E731 Derivative of f(x)
|
||||
for _ in range(maxiter):
|
||||
if f1(a) == 0:
|
||||
raise ValueError("No converging solution found")
|
||||
@@ -44,7 +44,7 @@ def newton_raphson(f, x0=0, maxiter=100, step=0.0001, maxerror=1e-6, logsteps=Fa
|
||||
if __name__ == "__main__":
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
f = lambda x: m.tanh(x) ** 2 - m.exp(3 * x)
|
||||
f = lambda x: m.tanh(x) ** 2 - m.exp(3 * x) # noqa: E731
|
||||
solution, error, steps = newton_raphson(
|
||||
f, x0=10, maxiter=1000, step=1e-6, logsteps=True
|
||||
)
|
||||
|
||||
@@ -7,7 +7,7 @@ from typing import List
|
||||
def prime_factors(n: int) -> List[int]:
|
||||
"""
|
||||
Returns prime factors of n as a list.
|
||||
|
||||
|
||||
>>> prime_factors(0)
|
||||
[]
|
||||
>>> prime_factors(100)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
# flake8: noqa
|
||||
|
||||
"""
|
||||
Sieve of Eratosthenes
|
||||
|
||||
Input : n =10
|
||||
Output : 2 3 5 7
|
||||
Output: 2 3 5 7
|
||||
|
||||
Input : n = 20
|
||||
Output: 2 3 5 7 11 13 17 19
|
||||
Output: 2 3 5 7 11 13 17 19
|
||||
|
||||
you can read in detail about this at
|
||||
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
||||
|
||||
@@ -14,8 +14,8 @@ def radians(degree: float) -> float:
|
||||
4.782202150464463
|
||||
>>> radians(109.82)
|
||||
1.9167205845401725
|
||||
|
||||
>>> from math import radians as math_radians
|
||||
|
||||
>>> from math import radians as math_radians
|
||||
>>> all(abs(radians(i)-math_radians(i)) <= 0.00000001 for i in range(-2, 361))
|
||||
True
|
||||
"""
|
||||
|
||||
@@ -12,36 +12,36 @@ class FFT:
|
||||
|
||||
Reference:
|
||||
https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm#The_radix-2_DIT_case
|
||||
|
||||
For polynomials of degree m and n the algorithms has complexity
|
||||
|
||||
For polynomials of degree m and n the algorithms has complexity
|
||||
O(n*logn + m*logm)
|
||||
|
||||
|
||||
The main part of the algorithm is split in two parts:
|
||||
1) __DFT: We compute the discrete fourier transform (DFT) of A and B using a
|
||||
bottom-up dynamic approach -
|
||||
1) __DFT: We compute the discrete fourier transform (DFT) of A and B using a
|
||||
bottom-up dynamic approach -
|
||||
2) __multiply: Once we obtain the DFT of A*B, we can similarly
|
||||
invert it to obtain A*B
|
||||
|
||||
The class FFT takes two polynomials A and B with complex coefficients as arguments;
|
||||
The class FFT takes two polynomials A and B with complex coefficients as arguments;
|
||||
The two polynomials should be represented as a sequence of coefficients starting
|
||||
from the free term. Thus, for instance x + 2*x^3 could be represented as
|
||||
[0,1,0,2] or (0,1,0,2). The constructor adds some zeros at the end so that the
|
||||
polynomials have the same length which is a power of 2 at least the length of
|
||||
their product.
|
||||
|
||||
from the free term. Thus, for instance x + 2*x^3 could be represented as
|
||||
[0,1,0,2] or (0,1,0,2). The constructor adds some zeros at the end so that the
|
||||
polynomials have the same length which is a power of 2 at least the length of
|
||||
their product.
|
||||
|
||||
Example:
|
||||
|
||||
|
||||
Create two polynomials as sequences
|
||||
>>> A = [0, 1, 0, 2] # x+2x^3
|
||||
>>> B = (2, 3, 4, 0) # 2+3x+4x^2
|
||||
|
||||
|
||||
Create an FFT object with them
|
||||
>>> x = FFT(A, B)
|
||||
|
||||
|
||||
Print product
|
||||
>>> print(x.product) # 2x + 3x^2 + 8x^3 + 4x^4 + 6x^5
|
||||
[(-0+0j), (2+0j), (3+0j), (8+0j), (6+0j), (8+0j)]
|
||||
|
||||
|
||||
__str__ test
|
||||
>>> print(x)
|
||||
A = 0*x^0 + 1*x^1 + 2*x^0 + 3*x^2
|
||||
|
||||
@@ -16,7 +16,7 @@ import math
|
||||
def sieve(n):
|
||||
"""
|
||||
Returns a list with all prime numbers up to n.
|
||||
|
||||
|
||||
>>> sieve(50)
|
||||
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
||||
>>> sieve(25)
|
||||
@@ -31,7 +31,7 @@ def sieve(n):
|
||||
[]
|
||||
"""
|
||||
|
||||
l = [True] * (n + 1)
|
||||
l = [True] * (n + 1) # noqa: E741
|
||||
prime = []
|
||||
start = 2
|
||||
end = int(math.sqrt(n))
|
||||
|
||||
@@ -24,10 +24,10 @@ def square_root_iterative(
|
||||
"""
|
||||
Square root is aproximated using Newtons method.
|
||||
https://en.wikipedia.org/wiki/Newton%27s_method
|
||||
|
||||
|
||||
>>> all(abs(square_root_iterative(i)-math.sqrt(i)) <= .00000000000001 for i in range(0, 500))
|
||||
True
|
||||
|
||||
|
||||
>>> square_root_iterative(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
|
||||
Reference in New Issue
Block a user