Skip to content

Commit 9eac17a

Browse files
WilliamHYZhangcclauss
authored andcommitted
psf/black code formatting (TheAlgorithms#1277)
1 parent 07f04a2 commit 9eac17a

File tree

291 files changed

+6100
-4657
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

291 files changed

+6100
-4657
lines changed

arithmetic_analysis/bisection.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import math
22

33

4-
def bisection(function, a, b): # finds where the function becomes 0 in [a,b] using bolzano
4+
def bisection(
5+
function, a, b
6+
): # finds where the function becomes 0 in [a,b] using bolzano
57

68
start = a
79
end = b
810
if function(a) == 0: # one of the a or b is a root for the function
911
return a
1012
elif function(b) == 0:
1113
return b
12-
elif function(a) * function(b) > 0: # if none of these are root and they are both positive or negative,
14+
elif (
15+
function(a) * function(b) > 0
16+
): # if none of these are root and they are both positive or negative,
1317
# then his algorithm can't find the root
1418
print("couldn't find root in [a,b]")
1519
return
1620
else:
1721
mid = start + (end - start) / 2.0
18-
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
22+
while abs(start - mid) > 10 ** -7: # until we achieve precise equals to 10^-7
1923
if function(mid) == 0:
2024
return mid
2125
elif function(mid) * function(start) < 0:
@@ -27,7 +31,8 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
2731

2832

2933
def f(x):
30-
return math.pow(x, 3) - 2*x - 5
34+
return math.pow(x, 3) - 2 * x - 5
35+
3136

3237
if __name__ == "__main__":
3338
print(bisection(f, 1, 1000))

arithmetic_analysis/in_static_equilibrium.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ def in_static_equilibrium(
5454
if __name__ == "__main__":
5555
# Test to check if it works
5656
forces = array(
57-
[
58-
polar_force(718.4, 180 - 30),
59-
polar_force(879.54, 45),
60-
polar_force(100, -90)
61-
])
57+
[polar_force(718.4, 180 - 30), polar_force(879.54, 45), polar_force(100, -90)]
58+
)
6259

6360
location = array([[0, 0], [0, 0], [0, 0]])
6461

arithmetic_analysis/intersection.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import math
22

3-
def intersection(function,x0,x1): #function is the f we want to find its root and x0 and x1 are two random starting points
3+
4+
def intersection(
5+
function, x0, x1
6+
): # function is the f we want to find its root and x0 and x1 are two random starting points
47
x_n = x0
58
x_n1 = x1
69
while True:
7-
x_n2 = x_n1-(function(x_n1)/((function(x_n1)-function(x_n))/(x_n1-x_n)))
8-
if abs(x_n2 - x_n1) < 10**-5:
10+
x_n2 = x_n1 - (
11+
function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n))
12+
)
13+
if abs(x_n2 - x_n1) < 10 ** -5:
914
return x_n2
10-
x_n=x_n1
11-
x_n1=x_n2
15+
x_n = x_n1
16+
x_n1 = x_n2
17+
1218

1319
def f(x):
14-
return math.pow(x , 3) - (2 * x) -5
20+
return math.pow(x, 3) - (2 * x) - 5
21+
1522

1623
if __name__ == "__main__":
17-
print(intersection(f,3,3.5))
24+
print(intersection(f, 3, 3.5))

arithmetic_analysis/lu_decomposition.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def LUDecompose(table):
2828

2929

3030
if __name__ == "__main__":
31-
matrix = numpy.array([[2, -2, 1],
32-
[0, 1, 2],
33-
[5, 3, 1]])
31+
matrix = numpy.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
3432
L, U = LUDecompose(matrix)
3533
print(L)
3634
print(U)

arithmetic_analysis/newton_method.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ def newton(function, function1, startingInt):
88
x_n = startingInt
99
while True:
1010
x_n1 = x_n - function(x_n) / function1(x_n)
11-
if abs(x_n - x_n1) < 10**-5:
11+
if abs(x_n - x_n1) < 10 ** -5:
1212
return x_n1
1313
x_n = x_n1
1414

1515

1616
def f(x):
17-
return (x**3) - (2 * x) - 5
17+
return (x ** 3) - (2 * x) - 5
1818

1919

2020
def f1(x):
21-
return 3 * (x**2) - 2
21+
return 3 * (x ** 2) - 2
2222

2323

2424
if __name__ == "__main__":
+12-11
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
# Implementing Newton Raphson method in Python
22
# Author: Syed Haseeb Shah (github.com/QuantumNovice)
3-
#The Newton-Raphson method (also known as Newton's method) is a way to
4-
#quickly find a good approximation for the root of a real-valued function
3+
# The Newton-Raphson method (also known as Newton's method) is a way to
4+
# quickly find a good approximation for the root of a real-valued function
55
from sympy import diff
66
from decimal import Decimal
77

8+
89
def NewtonRaphson(func, a):
9-
''' Finds root from the point 'a' onwards by Newton-Raphson method '''
10+
""" Finds root from the point 'a' onwards by Newton-Raphson method """
1011
while True:
11-
c = Decimal(a) - ( Decimal(eval(func)) / Decimal(eval(str(diff(func)))) )
12+
c = Decimal(a) - (Decimal(eval(func)) / Decimal(eval(str(diff(func)))))
1213

1314
a = c
1415

1516
# This number dictates the accuracy of the answer
16-
if abs(eval(func)) < 10**-15:
17-
return c
17+
if abs(eval(func)) < 10 ** -15:
18+
return c
1819

1920

2021
# Let's Execute
21-
if __name__ == '__main__':
22+
if __name__ == "__main__":
2223
# Find root of trigonometric function
2324
# Find value of pi
24-
print('sin(x) = 0', NewtonRaphson('sin(x)', 2))
25+
print("sin(x) = 0", NewtonRaphson("sin(x)", 2))
2526

2627
# Find root of polynomial
27-
print('x**2 - 5*x +2 = 0', NewtonRaphson('x**2 - 5*x +2', 0.4))
28+
print("x**2 - 5*x +2 = 0", NewtonRaphson("x**2 - 5*x +2", 0.4))
2829

2930
# Find Square Root of 5
30-
print('x**2 - 5 = 0', NewtonRaphson('x**2 - 5', 0.1))
31+
print("x**2 - 5 = 0", NewtonRaphson("x**2 - 5", 0.1))
3132

3233
# Exponential Roots
33-
print('exp(x) - 1 = 0', NewtonRaphson('exp(x) - 1', 0))
34+
print("exp(x) - 1 = 0", NewtonRaphson("exp(x) - 1", 0))

backtracking/all_combinations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def generate_all_combinations(n: int, k: int) -> [[int]]:
1212
>>> generate_all_combinations(n=4, k=2)
1313
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
1414
"""
15-
15+
1616
result = []
1717
create_all_state(1, n, k, [], result)
1818
return result
@@ -34,7 +34,7 @@ def print_all_state(total_list):
3434
print(*i)
3535

3636

37-
if __name__ == '__main__':
37+
if __name__ == "__main__":
3838
n = 4
3939
k = 2
4040
total_list = generate_all_combinations(n, k)

backtracking/all_permutations.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
'''
1+
"""
22
In this problem, we want to determine all possible permutations
33
of the given sequence. We use backtracking to solve this problem.
44
55
Time complexity: O(n! * n),
66
where n denotes the length of the given sequence.
7-
'''
7+
"""
88

99

1010
def generate_all_permutations(sequence):
11-
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
11+
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
1212

1313

1414
def create_state_space_tree(sequence, current_sequence, index, index_used):
15-
'''
15+
"""
1616
Creates a state space tree to iterate through each branch using DFS.
1717
We know that each state has exactly len(sequence) - index children.
1818
It terminates when it reaches the end of the given sequence.
19-
'''
19+
"""
2020

21-
if index == len(sequence):
22-
print(current_sequence)
23-
return
21+
if index == len(sequence):
22+
print(current_sequence)
23+
return
2424

25-
for i in range(len(sequence)):
26-
if not index_used[i]:
27-
current_sequence.append(sequence[i])
28-
index_used[i] = True
29-
create_state_space_tree(sequence, current_sequence, index + 1, index_used)
30-
current_sequence.pop()
31-
index_used[i] = False
25+
for i in range(len(sequence)):
26+
if not index_used[i]:
27+
current_sequence.append(sequence[i])
28+
index_used[i] = True
29+
create_state_space_tree(sequence, current_sequence, index + 1, index_used)
30+
current_sequence.pop()
31+
index_used[i] = False
3232

3333

34-
'''
34+
"""
3535
remove the comment to take an input from the user
3636
3737
print("Enter the elements")
3838
sequence = list(map(int, input().split()))
39-
'''
39+
"""
4040

4141
sequence = [3, 1, 2, 4]
4242
generate_all_permutations(sequence)

backtracking/all_subsequences.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
'''
1+
"""
22
In this problem, we want to determine all possible subsequences
33
of the given sequence. We use backtracking to solve this problem.
44
55
Time complexity: O(2^n),
66
where n denotes the length of the given sequence.
7-
'''
7+
"""
88

99

1010
def generate_all_subsequences(sequence):
11-
create_state_space_tree(sequence, [], 0)
11+
create_state_space_tree(sequence, [], 0)
1212

1313

1414
def create_state_space_tree(sequence, current_subsequence, index):
15-
'''
15+
"""
1616
Creates a state space tree to iterate through each branch using DFS.
1717
We know that each state has exactly two children.
1818
It terminates when it reaches the end of the given sequence.
19-
'''
19+
"""
2020

21-
if index == len(sequence):
22-
print(current_subsequence)
23-
return
21+
if index == len(sequence):
22+
print(current_subsequence)
23+
return
2424

25-
create_state_space_tree(sequence, current_subsequence, index + 1)
26-
current_subsequence.append(sequence[index])
27-
create_state_space_tree(sequence, current_subsequence, index + 1)
28-
current_subsequence.pop()
25+
create_state_space_tree(sequence, current_subsequence, index + 1)
26+
current_subsequence.append(sequence[index])
27+
create_state_space_tree(sequence, current_subsequence, index + 1)
28+
current_subsequence.pop()
2929

3030

31-
'''
31+
"""
3232
remove the comment to take an input from the user
3333
3434
print("Enter the elements")
3535
sequence = list(map(int, input().split()))
36-
'''
36+
"""
3737

3838
sequence = [3, 1, 2, 4]
3939
generate_all_subsequences(sequence)

backtracking/minimax.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1-
import math
1+
import math
22

3-
''' Minimax helps to achieve maximum score in a game by checking all possible moves
3+
""" Minimax helps to achieve maximum score in a game by checking all possible moves
44
depth is current depth in game tree.
55
nodeIndex is index of current node in scores[].
66
if move is of maximizer return true else false
77
leaves of game tree is stored in scores[]
88
height is maximum height of Game tree
9-
'''
9+
"""
1010

11-
def minimax (Depth, nodeIndex, isMax, scores, height):
1211

13-
if Depth == height:
14-
return scores[nodeIndex]
12+
def minimax(Depth, nodeIndex, isMax, scores, height):
1513

16-
if isMax:
17-
return (max(minimax(Depth + 1, nodeIndex * 2, False, scores, height),
18-
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)))
19-
return (min(minimax(Depth + 1, nodeIndex * 2, True, scores, height),
20-
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)))
14+
if Depth == height:
15+
return scores[nodeIndex]
2116

22-
if __name__ == "__main__":
23-
24-
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
25-
height = math.log(len(scores), 2)
17+
if isMax:
18+
return max(
19+
minimax(Depth + 1, nodeIndex * 2, False, scores, height),
20+
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height),
21+
)
22+
return min(
23+
minimax(Depth + 1, nodeIndex * 2, True, scores, height),
24+
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height),
25+
)
2626

27-
print("Optimal value : ", end = "")
28-
print(minimax(0, 0, True, scores, height))
27+
28+
if __name__ == "__main__":
29+
30+
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
31+
height = math.log(len(scores), 2)
32+
33+
print("Optimal value : ", end="")
34+
print(minimax(0, 0, True, scores, height))

0 commit comments

Comments
 (0)