Skip to content

Commit 1f8a21d

Browse files
cclaussgithub-actions
and
github-actions
authored
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>
1 parent 21ed896 commit 1f8a21d

File tree

124 files changed

+587
-499
lines changed

Some content is hidden

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

124 files changed

+587
-499
lines changed

.travis.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ language: python
44
python: 3.8
55
cache: pip
66
before_install: pip install --upgrade pip setuptools six
7-
install: pip install -r requirements.txt
7+
install: pip install black flake8
88
before_script:
9-
- black --check . || true
10-
- IGNORE=E123,E203,E265,E266,E302,E401,E402,E712,E731,E741,E743,F811,F841,W291,W293,W503
11-
- flake8 . --count --ignore=$IGNORE --max-complexity=25 --max-line-length=127 --show-source --statistics
12-
script:
9+
- black --check .
10+
- flake8 --ignore=E203,W503 --max-complexity=25 --max-line-length=120 --statistics --count .
1311
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
12+
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
13+
script:
1414
- mypy --ignore-missing-imports .
1515
- pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=. .
1616
after_success:

DIRECTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
* [Bellman Ford](https://github.com/TheAlgorithms/Python/blob/master/graphs/bellman_ford.py)
223223
* [Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs.py)
224224
* [Bfs Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs_shortest_path.py)
225+
* [Bidirectional A Star](https://github.com/TheAlgorithms/Python/blob/master/graphs/bidirectional_a_star.py)
225226
* [Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search.py)
226227
* [Breadth First Search Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search_shortest_path.py)
227228
* [Check Bipartite Graph Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_bfs.py)
@@ -242,6 +243,7 @@
242243
* [Graph List](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_list.py)
243244
* [Graph Matrix](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_matrix.py)
244245
* [Graphs Floyd Warshall](https://github.com/TheAlgorithms/Python/blob/master/graphs/graphs_floyd_warshall.py)
246+
* [Greedy Best First](https://github.com/TheAlgorithms/Python/blob/master/graphs/greedy_best_first.py)
245247
* [Kahns Algorithm Long](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_long.py)
246248
* [Kahns Algorithm Topo](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_topo.py)
247249
* [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py)
@@ -409,6 +411,7 @@
409411
* [Fischer Yates Shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py)
410412
* [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py)
411413
* [Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/other/game_of_life.py)
414+
* [Gauss Easter](https://github.com/TheAlgorithms/Python/blob/master/other/gauss_easter.py)
412415
* [Greedy](https://github.com/TheAlgorithms/Python/blob/master/other/greedy.py)
413416
* [Integeration By Simpson Approx](https://github.com/TheAlgorithms/Python/blob/master/other/integeration_by_simpson_approx.py)
414417
* [Largest Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/other/largest_subarray_sum.py)

arithmetic_analysis/newton_forward_interpolation.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import math
44

5+
56
# for calculating u value
67
def ucal(u, p):
78
"""

backtracking/coloring.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def valid_coloring(
1818
1919
>>> neighbours = [0,1,0,1,0]
2020
>>> colored_vertices = [0, 2, 1, 2, 0]
21-
21+
2222
>>> color = 1
2323
>>> valid_coloring(neighbours, colored_vertices, color)
2424
True
@@ -37,11 +37,11 @@ def valid_coloring(
3737
def util_color(
3838
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
3939
) -> bool:
40-
"""
40+
"""
4141
Pseudo-Code
4242
4343
Base Case:
44-
1. Check if coloring is complete
44+
1. Check if coloring is complete
4545
1.1 If complete return True (meaning that we successfully colored graph)
4646
4747
Recursive Step:
@@ -60,7 +60,7 @@ def util_color(
6060
>>> max_colors = 3
6161
>>> colored_vertices = [0, 1, 0, 0, 0]
6262
>>> index = 3
63-
63+
6464
>>> util_color(graph, max_colors, colored_vertices, index)
6565
True
6666
@@ -87,11 +87,11 @@ def util_color(
8787

8888

8989
def color(graph: List[List[int]], max_colors: int) -> List[int]:
90-
"""
90+
"""
9191
Wrapper function to call subroutine called util_color
9292
which will either return True or False.
9393
If True is returned colored_vertices list is filled with correct colorings
94-
94+
9595
>>> graph = [[0, 1, 0, 0, 0],
9696
... [1, 0, 1, 0, 1],
9797
... [0, 1, 0, 1, 0],

backtracking/hamiltonian_cycle.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
2+
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
33
through a graph that visits each node exactly once.
4-
Determining whether such paths and cycles exist in graphs
4+
Determining whether such paths and cycles exist in graphs
55
is the 'Hamiltonian path problem', which is NP-complete.
6-
6+
77
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
88
"""
99
from typing import List
@@ -18,7 +18,7 @@ def valid_connection(
1818
2. Next vertex should not be in path
1919
If both validations succeeds we return true saying that it is possible to connect this vertices
2020
either we return false
21-
21+
2222
Case 1:Use exact graph as in main function, with initialized values
2323
>>> graph = [[0, 1, 0, 1, 0],
2424
... [1, 0, 1, 1, 1],
@@ -56,11 +56,11 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
5656
Recursive Step:
5757
2. Iterate over each vertex
5858
Check if next vertex is valid for transiting from current vertex
59-
2.1 Remember next vertex as next transition
59+
2.1 Remember next vertex as next transition
6060
2.2 Do recursive call and check if going to this vertex solves problem
6161
2.3 if next vertex leads to solution return True
6262
2.4 else backtrack, delete remembered vertex
63-
63+
6464
Case 1: Use exact graph as in main function, with initialized values
6565
>>> graph = [[0, 1, 0, 1, 0],
6666
... [1, 0, 1, 1, 1],
@@ -111,12 +111,12 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
111111
Wrapper function to call subroutine called util_hamilton_cycle,
112112
which will either return array of vertices indicating hamiltonian cycle
113113
or an empty list indicating that hamiltonian cycle was not found.
114-
Case 1:
115-
Following graph consists of 5 edges.
114+
Case 1:
115+
Following graph consists of 5 edges.
116116
If we look closely, we can see that there are multiple Hamiltonian cycles.
117-
For example one result is when we iterate like:
117+
For example one result is when we iterate like:
118118
(0)->(1)->(2)->(4)->(3)->(0)
119-
119+
120120
(0)---(1)---(2)
121121
| / \ |
122122
| / \ |
@@ -130,10 +130,10 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
130130
... [0, 1, 1, 1, 0]]
131131
>>> hamilton_cycle(graph)
132132
[0, 1, 2, 4, 3, 0]
133-
134-
Case 2:
133+
134+
Case 2:
135135
Same Graph as it was in Case 1, changed starting index from default to 3
136-
136+
137137
(0)---(1)---(2)
138138
| / \ |
139139
| / \ |
@@ -147,11 +147,11 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
147147
... [0, 1, 1, 1, 0]]
148148
>>> hamilton_cycle(graph, 3)
149149
[3, 0, 1, 2, 4, 3]
150-
150+
151151
Case 3:
152152
Following Graph is exactly what it was before, but edge 3-4 is removed.
153153
Result is that there is no Hamiltonian Cycle anymore.
154-
154+
155155
(0)---(1)---(2)
156156
| / \ |
157157
| / \ |

backtracking/minimax.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import math
22

33
""" Minimax helps to achieve maximum score in a game by checking all possible moves
4-
depth is current depth in game tree.
4+
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
7-
leaves of game tree is stored in scores[]
7+
leaves of game tree is stored in scores[]
88
height is maximum height of Game tree
99
"""
1010

backtracking/n_queens.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
3-
The nqueens problem is of placing N queens on a N * N
3+
The nqueens problem is of placing N queens on a N * N
44
chess board such that no queen can attack any other queens placed
55
on that chess board.
6-
This means that one queen cannot have any other queen on its horizontal, vertical and
6+
This means that one queen cannot have any other queen on its horizontal, vertical and
77
diagonal lines.
88
99
"""
@@ -12,7 +12,7 @@
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
15+
This function returns a boolean value True if it is safe to place a queen there considering
1616
the current state of the board.
1717
1818
Parameters :
@@ -40,13 +40,13 @@ def isSafe(board, row, column):
4040

4141
def solve(board, row):
4242
"""
43-
It creates a state space tree and calls the safe function until it receives a
44-
False Boolean and terminates that branch and backtracks to the next
43+
It creates a state space tree and calls the safe function until it receives a
44+
False Boolean and terminates that branch and backtracks to the next
4545
possible solution branch.
4646
"""
4747
if row >= len(board):
4848
"""
49-
If the row number exceeds N we have board with a successful combination
49+
If the row number exceeds N we have board with a successful combination
5050
and that combination is appended to the solution list and the board is printed.
5151
5252
"""
@@ -56,9 +56,9 @@ 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
59+
For every row it iterates through each column to check if it is feasible to place a
6060
queen there.
61-
If all the combinations for that particular branch are successful the board is
61+
If all the combinations for that particular branch are successful the board is
6262
reinitialized for the next possible combination.
6363
"""
6464
if isSafe(board, row, i):

ciphers/decrypt_caesar_with_chi_squared.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
#!/usr/bin/env python3
2+
3+
14
def decrypt_caesar_with_chi_squared(
25
ciphertext: str,
36
cipher_alphabet=None,
47
frequencies_dict=None,
58
case_sensetive: bool = False,
6-
) -> list:
9+
) -> tuple:
710
"""
811
Basic Usage
912
===========
@@ -96,15 +99,19 @@ def decrypt_caesar_with_chi_squared(
9699
Further Reading
97100
================
98101
99-
* http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-statistic/
102+
* http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-
103+
statistic/
100104
* https://en.wikipedia.org/wiki/Letter_frequency
101105
* https://en.wikipedia.org/wiki/Chi-squared_test
102106
* https://en.m.wikipedia.org/wiki/Caesar_cipher
103107
104108
Doctests
105109
========
106-
>>> decrypt_caesar_with_chi_squared('dof pz aol jhlzhy jpwoly zv wvwbshy? pa pz avv lhzf av jyhjr!')
107-
(7, 3129.228005747531, 'why is the caesar cipher so popular? it is too easy to crack!')
110+
>>> decrypt_caesar_with_chi_squared(
111+
... 'dof pz aol jhlzhy jpwoly zv wvwbshy? pa pz avv lhzf av jyhjr!'
112+
... ) # doctest: +NORMALIZE_WHITESPACE
113+
(7, 3129.228005747531,
114+
'why is the caesar cipher so popular? it is too easy to crack!')
108115
109116
>>> decrypt_caesar_with_chi_squared('crybd cdbsxq')
110117
(10, 233.35343938980898, 'short string')
@@ -172,7 +179,7 @@ def decrypt_caesar_with_chi_squared(
172179
# Append the character if it isn't in the alphabet
173180
decrypted_with_shift += letter
174181

175-
chi_squared_statistic = 0
182+
chi_squared_statistic = 0.0
176183

177184
# Loop through each letter in the decoded message with the shift
178185
for letter in decrypted_with_shift:
@@ -181,7 +188,8 @@ def decrypt_caesar_with_chi_squared(
181188
# Get the amount of times the letter occurs in the message
182189
occurrences = decrypted_with_shift.count(letter)
183190

184-
# Get the excepcted amount of times the letter should appear based on letter frequencies
191+
# Get the excepcted amount of times the letter should appear based
192+
# on letter frequencies
185193
expected = frequencies[letter] * occurrences
186194

187195
# Complete the chi squared statistic formula
@@ -194,7 +202,8 @@ def decrypt_caesar_with_chi_squared(
194202
# Get the amount of times the letter occurs in the message
195203
occurrences = decrypted_with_shift.count(letter)
196204

197-
# Get the excepcted amount of times the letter should appear based on letter frequencies
205+
# Get the excepcted amount of times the letter should appear based
206+
# on letter frequencies
198207
expected = frequencies[letter] * occurrences
199208

200209
# Complete the chi squared statistic formula
@@ -209,7 +218,8 @@ def decrypt_caesar_with_chi_squared(
209218
decrypted_with_shift,
210219
]
211220

212-
# Get the most likely cipher by finding the cipher with the smallest chi squared statistic
221+
# Get the most likely cipher by finding the cipher with the smallest chi squared
222+
# statistic
213223
most_likely_cipher = min(
214224
chi_squared_statistic_values, key=chi_squared_statistic_values.get
215225
)

ciphers/elgamal_key_generator.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import random
33
import sys
4-
import rabin_miller as rabinMiller, cryptomath_module as cryptoMath
4+
5+
import cryptomath_module as cryptoMath
6+
import rabin_miller as rabinMiller
57

68
min_primitive_root = 3
79

ciphers/mixed_keyword_cypher.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def mixed_keyword(key="college", pt="UNIVERSITY"):
2525
for i in key:
2626
if i not in temp:
2727
temp.append(i)
28-
l = len(temp)
28+
len_temp = len(temp)
2929
# print(temp)
3030
alpha = []
3131
modalpha = []
@@ -40,17 +40,17 @@ def mixed_keyword(key="college", pt="UNIVERSITY"):
4040
k = 0
4141
for i in range(r):
4242
t = []
43-
for j in range(l):
43+
for j in range(len_temp):
4444
t.append(temp[k])
4545
if not (k < 25):
4646
break
4747
k += 1
4848
modalpha.append(t)
4949
# print(modalpha)
50-
d = dict()
50+
d = {}
5151
j = 0
5252
k = 0
53-
for j in range(l):
53+
for j in range(len_temp):
5454
for i in modalpha:
5555
if not (len(i) - 1 >= j):
5656
break

ciphers/simple_substitution_cipher.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import sys, random
1+
import random
2+
import sys
23

34
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45

ciphers/transposition_cipher_encrypt_decrypt_file.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import time, os, sys
1+
import os
2+
import sys
3+
import time
4+
25
import transposition_cipher as transCipher
36

47

conversions/decimal_to_octal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def decimal_to_octal(num: int) -> str:
1010
"""Convert a Decimal Number to an Octal Number.
11-
11+
1212
>>> all(decimal_to_octal(i) == oct(i) for i in (0, 2, 8, 64, 65, 216, 255, 256, 512))
1313
True
1414
"""

0 commit comments

Comments
 (0)