Skip to content

Commit 9316e7c

Browse files
cclaussgithub-actions
and
github-actions
authored
Set the Python file maximum line length to 88 characters (TheAlgorithms#2122)
* flake8 --max-line-length=88 * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 9438c6b commit 9316e7c

File tree

90 files changed

+474
-321
lines changed

Some content is hidden

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

90 files changed

+474
-321
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ notifications:
1010
on_success: never
1111
before_script:
1212
- black --check . || true
13-
- flake8 --ignore=E203,W503 --max-complexity=25 --max-line-length=120 --statistics --count .
13+
- flake8 --ignore=E203,W503 --max-complexity=25 --max-line-length=88 --statistics --count .
1414
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
1515
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
1616
script:

arithmetic_analysis/intersection.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import math
22

33

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
4+
def intersection(function, x0, x1):
5+
"""
6+
function is the f we want to find its root
7+
x0 and x1 are two random starting points
8+
"""
79
x_n = x0
810
x_n1 = x1
911
while True:

backtracking/hamiltonian_cycle.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def valid_connection(
1616
Checks whether it is possible to add next into path by validating 2 statements
1717
1. There should be path between current and next vertex
1818
2. Next vertex should not be in path
19-
If both validations succeeds we return true saying that it is possible to connect this vertices
20-
either we return false
19+
If both validations succeeds we return True saying that it is possible to connect
20+
this vertices either we return False
2121
2222
Case 1:Use exact graph as in main function, with initialized values
2323
>>> graph = [[0, 1, 0, 1, 0],
@@ -52,7 +52,8 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
5252
Pseudo-Code
5353
Base Case:
5454
1. Chceck if we visited all of vertices
55-
1.1 If last visited vertex has path to starting vertex return True either return False
55+
1.1 If last visited vertex has path to starting vertex return True either
56+
return False
5657
Recursive Step:
5758
2. Iterate over each vertex
5859
Check if next vertex is valid for transiting from current vertex
@@ -74,7 +75,8 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
7475
>>> print(path)
7576
[0, 1, 2, 4, 3, 0]
7677
77-
Case 2: Use exact graph as in previous case, but in the properties taken from middle of calculation
78+
Case 2: Use exact graph as in previous case, but in the properties taken from
79+
middle of calculation
7880
>>> graph = [[0, 1, 0, 1, 0],
7981
... [1, 0, 1, 1, 1],
8082
... [0, 1, 0, 0, 1],

backtracking/n_queens.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
def isSafe(board, row, column):
1414
"""
15-
This function returns a boolean value True if it is safe to place a queen there considering
16-
the current state of the board.
15+
This function returns a boolean value True if it is safe to place a queen there
16+
considering the current state of the board.
1717
1818
Parameters :
1919
board(2D matrix) : board
@@ -56,8 +56,8 @@ def solve(board, row):
5656
return
5757
for i in range(len(board)):
5858
"""
59-
For every row it iterates through each column to check if it is feasible to place a
60-
queen there.
59+
For every row it iterates through each column to check if it is feasible to
60+
place a queen there.
6161
If all the combinations for that particular branch are successful the board is
6262
reinitialized for the next possible combination.
6363
"""

backtracking/sum_of_subsets.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""
2-
The sum-of-subsetsproblem states that a set of non-negative integers, and a value M,
3-
determine all possible subsets of the given set whose summation sum equal to given M.
2+
The sum-of-subsetsproblem states that a set of non-negative integers, and a
3+
value M, determine all possible subsets of the given set whose summation sum
4+
equal to given M.
45
5-
Summation of the chosen numbers must be equal to given number M and one number can
6-
be used only once.
6+
Summation of the chosen numbers must be equal to given number M and one number
7+
can be used only once.
78
"""
89

910

@@ -21,7 +22,8 @@ def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nu
2122
Creates a state space tree to iterate through each branch using DFS.
2223
It terminates the branching of a node when any of the two conditions
2324
given below satisfy.
24-
This algorithm follows depth-fist-search and backtracks when the node is not branchable.
25+
This algorithm follows depth-fist-search and backtracks when the node is not
26+
branchable.
2527
2628
"""
2729
if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum:

blockchain/chinese_remainder_theorem.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Chinese Remainder Theorem:
22
# GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
33

4-
# If GCD(a,b) = 1, then for any remainder ra modulo a and any remainder rb modulo b there exists integer n,
5-
# such that n = ra (mod a) and n = ra(mod b). If n1 and n2 are two such integers, then n1=n2(mod ab)
4+
# If GCD(a,b) = 1, then for any remainder ra modulo a and any remainder rb modulo b
5+
# there exists integer n, such that n = ra (mod a) and n = ra(mod b). If n1 and n2 are
6+
# two such integers, then n1=n2(mod ab)
67

78
# Algorithm :
89

blockchain/diophantine_equation.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Diophantine Equation : Given integers a,b,c ( at least one of a and b != 0), the diophantine equation
2-
# a*x + b*y = c has a solution (where x and y are integers) iff gcd(a,b) divides c.
1+
# Diophantine Equation : Given integers a,b,c ( at least one of a and b != 0), the
2+
# diophantine equation a*x + b*y = c has a solution (where x and y are integers)
3+
# iff gcd(a,b) divides c.
34

45
# GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
56

@@ -29,8 +30,9 @@ def diophantine(a, b, c):
2930

3031
# Finding All solutions of Diophantine Equations:
3132

32-
# Theorem : Let gcd(a,b) = d, a = d*p, b = d*q. If (x0,y0) is a solution of Diophantine Equation a*x + b*y = c.
33-
# a*x0 + b*y0 = c, then all the solutions have the form a(x0 + t*q) + b(y0 - t*p) = c, where t is an arbitrary integer.
33+
# Theorem : Let gcd(a,b) = d, a = d*p, b = d*q. If (x0,y0) is a solution of Diophantine
34+
# Equation a*x + b*y = c. a*x0 + b*y0 = c, then all the solutions have the form
35+
# a(x0 + t*q) + b(y0 - t*p) = c, where t is an arbitrary integer.
3436

3537
# n is the number of solution you want, n = 2 by default
3638

@@ -75,8 +77,9 @@ def greatest_common_divisor(a, b):
7577
>>> greatest_common_divisor(7,5)
7678
1
7779
78-
Note : In number theory, two integers a and b are said to be relatively prime, mutually prime, or co-prime
79-
if the only positive integer (factor) that divides both of them is 1 i.e., gcd(a,b) = 1.
80+
Note : In number theory, two integers a and b are said to be relatively prime,
81+
mutually prime, or co-prime if the only positive integer (factor) that
82+
divides both of them is 1 i.e., gcd(a,b) = 1.
8083
8184
>>> greatest_common_divisor(121, 11)
8285
11
@@ -91,7 +94,8 @@ def greatest_common_divisor(a, b):
9194
return b
9295

9396

94-
# Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x and y, then d = gcd(a,b)
97+
# Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers
98+
# x and y, then d = gcd(a,b)
9599

96100

97101
def extended_gcd(a, b):

blockchain/modular_division.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
# GCD ( Greatest Common Divisor ) or HCF ( Highest Common Factor )
55

6-
# Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should return an integer x such that
7-
# 0≤x≤n−1, and b/a=x(modn) (that is, b=ax(modn)).
6+
# Given three integers a, b, and n, such that gcd(a,n)=1 and n>1, the algorithm should
7+
# return an integer x such that 0≤x≤n−1, and b/a=x(modn) (that is, b=ax(modn)).
88

99
# Theorem:
1010
# a has a multiplicative inverse modulo n iff gcd(a,n) = 1
@@ -68,7 +68,8 @@ def modular_division2(a, b, n):
6868
return x
6969

7070

71-
# Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x and y, then d = gcd(a,b)
71+
# Extended Euclid's Algorithm : If d divides a and b and d = a*x + b*y for integers x
72+
# and y, then d = gcd(a,b)
7273

7374

7475
def extended_gcd(a, b):
@@ -123,8 +124,9 @@ def greatest_common_divisor(a, b):
123124
>>> greatest_common_divisor(7,5)
124125
1
125126
126-
Note : In number theory, two integers a and b are said to be relatively prime, mutually prime, or co-prime
127-
if the only positive integer (factor) that divides both of them is 1 i.e., gcd(a,b) = 1.
127+
Note : In number theory, two integers a and b are said to be relatively prime,
128+
mutually prime, or co-prime if the only positive integer (factor) that divides
129+
both of them is 1 i.e., gcd(a,b) = 1.
128130
129131
>>> greatest_common_divisor(121, 11)
130132
11

ciphers/affine_cipher.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def check_keys(keyA, keyB, mode):
5555

5656
def encrypt_message(key: int, message: str) -> str:
5757
"""
58-
>>> encrypt_message(4545, 'The affine cipher is a type of monoalphabetic substitution cipher.')
58+
>>> encrypt_message(4545, 'The affine cipher is a type of monoalphabetic '
59+
... 'substitution cipher.')
5960
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
6061
"""
6162
keyA, keyB = divmod(key, len(SYMBOLS))
@@ -72,7 +73,8 @@ def encrypt_message(key: int, message: str) -> str:
7273

7374
def decrypt_message(key: int, message: str) -> str:
7475
"""
75-
>>> decrypt_message(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi')
76+
>>> decrypt_message(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF'
77+
... '{xIp~{HL}Gi')
7678
'The affine cipher is a type of monoalphabetic substitution cipher.'
7779
"""
7880
keyA, keyB = divmod(key, len(SYMBOLS))

ciphers/base64_cipher.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def decode_base64(text):
3939
'WELCOME to base64 encoding 😁'
4040
>>> decode_base64('QcOF4ZCD8JCAj/CfpJM=')
4141
'AÅᐃ𐀏🤓'
42-
>>> decode_base64("QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\r\nQUFB")
42+
>>> decode_base64("QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUF"
43+
... "BQUFBQUFBQUFB\r\nQUFB")
4344
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
4445
"""
4546
base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

ciphers/elgamal_key_generator.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def main():
1616

1717
# I have written my code naively same as definition of primitive root
1818
# however every time I run this program, memory exceeded...
19-
# so I used 4.80 Algorithm in Handbook of Applied Cryptography(CRC Press, ISBN : 0-8493-8523-7, October 1996)
19+
# so I used 4.80 Algorithm in
20+
# Handbook of Applied Cryptography(CRC Press, ISBN : 0-8493-8523-7, October 1996)
2021
# and it seems to run nicely!
2122
def primitiveRoot(p_val):
2223
print("Generating primitive root of p")

ciphers/hill_cipher.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def check_determinant(self) -> None:
106106
req_l = len(self.key_string)
107107
if greatest_common_divisor(det, len(self.key_string)) != 1:
108108
raise ValueError(
109-
f"determinant modular {req_l} of encryption key({det}) is not co prime w.r.t {req_l}.\nTry another key."
109+
f"determinant modular {req_l} of encryption key({det}) is not co prime "
110+
f"w.r.t {req_l}.\nTry another key."
110111
)
111112

112113
def process_text(self, text: str) -> str:

ciphers/shuffled_shift_cipher.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,30 @@ def __make_key_list(self) -> list:
8383
Shuffling only 26 letters of the english alphabet can generate 26!
8484
combinations for the shuffled list. In the program we consider, a set of
8585
97 characters (including letters, digits, punctuation and whitespaces),
86-
thereby creating a possibility of 97! combinations (which is a 152 digit number in itself),
87-
thus diminishing the possibility of a brute force approach. Moreover,
88-
shift keys even introduce a multiple of 26 for a brute force approach
86+
thereby creating a possibility of 97! combinations (which is a 152 digit number
87+
in itself), thus diminishing the possibility of a brute force approach.
88+
Moreover, shift keys even introduce a multiple of 26 for a brute force approach
8989
for each of the already 97! combinations.
9090
"""
91-
# key_list_options contain nearly all printable except few elements from string.whitespace
91+
# key_list_options contain nearly all printable except few elements from
92+
# string.whitespace
9293
key_list_options = (
9394
string.ascii_letters + string.digits + string.punctuation + " \t\n"
9495
)
9596

9697
keys_l = []
9798

98-
# creates points known as breakpoints to break the key_list_options at those points and pivot each substring
99+
# creates points known as breakpoints to break the key_list_options at those
100+
# points and pivot each substring
99101
breakpoints = sorted(set(self.__passcode))
100102
temp_list = []
101103

102104
# algorithm for creating a new shuffled list, keys_l, out of key_list_options
103105
for i in key_list_options:
104106
temp_list.extend(i)
105107

106-
# checking breakpoints at which to pivot temporary sublist and add it into keys_l
108+
# checking breakpoints at which to pivot temporary sublist and add it into
109+
# keys_l
107110
if i in breakpoints or i == key_list_options[-1]:
108111
keys_l.extend(temp_list[::-1])
109112
temp_list = []
@@ -131,7 +134,8 @@ def decrypt(self, encoded_message: str) -> str:
131134
"""
132135
decoded_message = ""
133136

134-
# decoding shift like Caesar cipher algorithm implementing negative shift or reverse shift or left shift
137+
# decoding shift like Caesar cipher algorithm implementing negative shift or
138+
# reverse shift or left shift
135139
for i in encoded_message:
136140
position = self.__key_list.index(i)
137141
decoded_message += self.__key_list[
@@ -152,7 +156,8 @@ def encrypt(self, plaintext: str) -> str:
152156
"""
153157
encoded_message = ""
154158

155-
# encoding shift like Caesar cipher algorithm implementing positive shift or forward shift or right shift
159+
# encoding shift like Caesar cipher algorithm implementing positive shift or
160+
# forward shift or right shift
156161
for i in plaintext:
157162
position = self.__key_list.index(i)
158163
encoded_message += self.__key_list[

compression/peak_signal_to_noise_ratio.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
2-
Peak signal-to-noise ratio - PSNR - https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
3-
Source: https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python/
2+
Peak signal-to-noise ratio - PSNR
3+
https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
4+
Source:
5+
https://tutorials.techonical.com/how-to-calculate-psnr-value-of-two-images-using-python
46
"""
57

68
import math

conversions/decimal_to_any.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ def decimal_to_any(num: int, base: int) -> str:
6666
if base > 36:
6767
raise ValueError("base must be <= 36")
6868
# fmt: off
69-
ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', '16': 'G', '17': 'H',
70-
'18': 'I', '19': 'J', '20': 'K', '21': 'L', '22': 'M', '23': 'N', '24': 'O', '25': 'P',
71-
'26': 'Q', '27': 'R', '28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X',
69+
ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F',
70+
'16': 'G', '17': 'H', '18': 'I', '19': 'J', '20': 'K', '21': 'L',
71+
'22': 'M', '23': 'N', '24': 'O', '25': 'P', '26': 'Q', '27': 'R',
72+
'28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X',
7273
'34': 'Y', '35': 'Z'}
7374
# fmt: on
7475
new_value = ""

conversions/decimal_to_hexadecimal.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
def decimal_to_hexadecimal(decimal):
2525
"""
26-
take integer decimal value, return hexadecimal representation as str beginning with 0x
26+
take integer decimal value, return hexadecimal representation as str beginning
27+
with 0x
2728
>>> decimal_to_hexadecimal(5)
2829
'0x5'
2930
>>> decimal_to_hexadecimal(15)

conversions/decimal_to_octal.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
def decimal_to_octal(num: int) -> str:
1010
"""Convert a Decimal Number to an Octal Number.
1111
12-
>>> all(decimal_to_octal(i) == oct(i) for i in (0, 2, 8, 64, 65, 216, 255, 256, 512))
12+
>>> all(decimal_to_octal(i) == oct(i) for i
13+
... in (0, 2, 8, 64, 65, 216, 255, 256, 512))
1314
True
1415
"""
1516
octal = 0

data_structures/data_structures/heap/heap_generic.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Heap:
2-
"""A generic Heap class, can be used as min or max by passing the key function accordingly.
2+
"""
3+
A generic Heap class, can be used as min or max by passing the key function
4+
accordingly.
35
"""
46

57
def __init__(self, key=None):
@@ -9,7 +11,8 @@ def __init__(self, key=None):
911
self.pos_map = {}
1012
# Stores current size of heap.
1113
self.size = 0
12-
# Stores function used to evaluate the score of an item on which basis ordering will be done.
14+
# Stores function used to evaluate the score of an item on which basis ordering
15+
# will be done.
1316
self.key = key or (lambda x: x)
1417

1518
def _parent(self, i):
@@ -41,7 +44,10 @@ def _cmp(self, i, j):
4144
return self.arr[i][1] < self.arr[j][1]
4245

4346
def _get_valid_parent(self, i):
44-
"""Returns index of valid parent as per desired ordering among given index and both it's children"""
47+
"""
48+
Returns index of valid parent as per desired ordering among given index and
49+
both it's children
50+
"""
4551
left = self._left(i)
4652
right = self._right(i)
4753
valid_parent = i
@@ -87,8 +93,8 @@ def delete_item(self, item):
8793
self.arr[index] = self.arr[self.size - 1]
8894
self.pos_map[self.arr[self.size - 1][0]] = index
8995
self.size -= 1
90-
# Make sure heap is right in both up and down direction.
91-
# Ideally only one of them will make any change- so no performance loss in calling both.
96+
# Make sure heap is right in both up and down direction. Ideally only one
97+
# of them will make any change- so no performance loss in calling both.
9298
if self.size > index:
9399
self._heapify_up(index)
94100
self._heapify_down(index)
@@ -109,7 +115,10 @@ def get_top(self):
109115
return self.arr[0] if self.size else None
110116

111117
def extract_top(self):
112-
"""Returns top item tuple (Calculated value, item) from heap and removes it as well if present"""
118+
"""
119+
Return top item tuple (Calculated value, item) from heap and removes it as well
120+
if present
121+
"""
113122
top_item_tuple = self.get_top()
114123
if top_item_tuple:
115124
self.delete_item(top_item_tuple[0])

0 commit comments

Comments
 (0)