Skip to content

Commit

Permalink
Tighten up psf/black and flake8 (TheAlgorithms#2024)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
cclauss and github-actions authored May 22, 2020
1 parent 21ed896 commit 1f8a21d
Show file tree
Hide file tree
Showing 124 changed files with 587 additions and 499 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ language: python
python: 3.8
cache: pip
before_install: pip install --upgrade pip setuptools six
install: pip install -r requirements.txt
install: pip install black flake8
before_script:
- black --check . || true
- IGNORE=E123,E203,E265,E266,E302,E401,E402,E712,E731,E741,E743,F811,F841,W291,W293,W503
- flake8 . --count --ignore=$IGNORE --max-complexity=25 --max-line-length=127 --show-source --statistics
script:
- black --check .
- flake8 --ignore=E203,W503 --max-complexity=25 --max-line-length=120 --statistics --count .
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
script:
- mypy --ignore-missing-imports .
- pytest --doctest-modules --cov-report=term-missing:skip-covered --cov=. .
after_success:
Expand Down
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
* [Bellman Ford](https://github.com/TheAlgorithms/Python/blob/master/graphs/bellman_ford.py)
* [Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs.py)
* [Bfs Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/bfs_shortest_path.py)
* [Bidirectional A Star](https://github.com/TheAlgorithms/Python/blob/master/graphs/bidirectional_a_star.py)
* [Breadth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search.py)
* [Breadth First Search Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search_shortest_path.py)
* [Check Bipartite Graph Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_bfs.py)
Expand All @@ -242,6 +243,7 @@
* [Graph List](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_list.py)
* [Graph Matrix](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_matrix.py)
* [Graphs Floyd Warshall](https://github.com/TheAlgorithms/Python/blob/master/graphs/graphs_floyd_warshall.py)
* [Greedy Best First](https://github.com/TheAlgorithms/Python/blob/master/graphs/greedy_best_first.py)
* [Kahns Algorithm Long](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_long.py)
* [Kahns Algorithm Topo](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_topo.py)
* [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py)
Expand Down Expand Up @@ -409,6 +411,7 @@
* [Fischer Yates Shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py)
* [Frequency Finder](https://github.com/TheAlgorithms/Python/blob/master/other/frequency_finder.py)
* [Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/other/game_of_life.py)
* [Gauss Easter](https://github.com/TheAlgorithms/Python/blob/master/other/gauss_easter.py)
* [Greedy](https://github.com/TheAlgorithms/Python/blob/master/other/greedy.py)
* [Integeration By Simpson Approx](https://github.com/TheAlgorithms/Python/blob/master/other/integeration_by_simpson_approx.py)
* [Largest Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/other/largest_subarray_sum.py)
Expand Down
1 change: 1 addition & 0 deletions arithmetic_analysis/newton_forward_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import math


# for calculating u value
def ucal(u, p):
"""
Expand Down
12 changes: 6 additions & 6 deletions backtracking/coloring.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def valid_coloring(
>>> neighbours = [0,1,0,1,0]
>>> colored_vertices = [0, 2, 1, 2, 0]
>>> color = 1
>>> valid_coloring(neighbours, colored_vertices, color)
True
Expand All @@ -37,11 +37,11 @@ def valid_coloring(
def util_color(
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
) -> bool:
"""
"""
Pseudo-Code
Base Case:
1. Check if coloring is complete
1. Check if coloring is complete
1.1 If complete return True (meaning that we successfully colored graph)
Recursive Step:
Expand All @@ -60,7 +60,7 @@ def util_color(
>>> max_colors = 3
>>> colored_vertices = [0, 1, 0, 0, 0]
>>> index = 3
>>> util_color(graph, max_colors, colored_vertices, index)
True
Expand All @@ -87,11 +87,11 @@ def util_color(


def color(graph: List[List[int]], max_colors: int) -> List[int]:
"""
"""
Wrapper function to call subroutine called util_color
which will either return True or False.
If True is returned colored_vertices list is filled with correct colorings
>>> graph = [[0, 1, 0, 0, 0],
... [1, 0, 1, 0, 1],
... [0, 1, 0, 1, 0],
Expand Down
30 changes: 15 additions & 15 deletions backtracking/hamiltonian_cycle.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
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.
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.
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
"""
from typing import List
Expand All @@ -18,7 +18,7 @@ def valid_connection(
2. Next vertex should not be in path
If both validations succeeds we return true saying that it is possible to connect this vertices
either we return false
Case 1:Use exact graph as in main function, with initialized values
>>> graph = [[0, 1, 0, 1, 0],
... [1, 0, 1, 1, 1],
Expand Down Expand Up @@ -56,11 +56,11 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
Recursive Step:
2. Iterate over each vertex
Check if next vertex is valid for transiting from current vertex
2.1 Remember next vertex as next transition
2.1 Remember next vertex as next transition
2.2 Do recursive call and check if going to this vertex solves problem
2.3 if next vertex leads to solution return True
2.4 else backtrack, delete remembered vertex
Case 1: Use exact graph as in main function, with initialized values
>>> graph = [[0, 1, 0, 1, 0],
... [1, 0, 1, 1, 1],
Expand Down Expand Up @@ -111,12 +111,12 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
Wrapper function to call subroutine called util_hamilton_cycle,
which will either return array of vertices indicating hamiltonian cycle
or an empty list indicating that hamiltonian cycle was not found.
Case 1:
Following graph consists of 5 edges.
Case 1:
Following graph consists of 5 edges.
If we look closely, we can see that there are multiple Hamiltonian cycles.
For example one result is when we iterate like:
For example one result is when we iterate like:
(0)->(1)->(2)->(4)->(3)->(0)
(0)---(1)---(2)
| / \ |
| / \ |
Expand All @@ -130,10 +130,10 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
... [0, 1, 1, 1, 0]]
>>> hamilton_cycle(graph)
[0, 1, 2, 4, 3, 0]
Case 2:
Case 2:
Same Graph as it was in Case 1, changed starting index from default to 3
(0)---(1)---(2)
| / \ |
| / \ |
Expand All @@ -147,11 +147,11 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
... [0, 1, 1, 1, 0]]
>>> hamilton_cycle(graph, 3)
[3, 0, 1, 2, 4, 3]
Case 3:
Following Graph is exactly what it was before, but edge 3-4 is removed.
Result is that there is no Hamiltonian Cycle anymore.
(0)---(1)---(2)
| / \ |
| / \ |
Expand Down
4 changes: 2 additions & 2 deletions backtracking/minimax.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import math

""" Minimax helps to achieve maximum score in a game by checking all possible moves
depth is current depth in game tree.
depth is current depth in game tree.
nodeIndex is index of current node in scores[].
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
"""

Expand Down
16 changes: 8 additions & 8 deletions backtracking/n_queens.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
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
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.
"""
Expand All @@ -12,7 +12,7 @@

def isSafe(board, row, column):
"""
This function returns a boolean value True if it is safe to place a queen there considering
This function returns a boolean value True if it is safe to place a queen there considering
the current state of the board.
Parameters :
Expand Down Expand Up @@ -40,13 +40,13 @@ def isSafe(board, row, column):

def solve(board, row):
"""
It creates a state space tree and calls the safe function until it receives a
False Boolean and terminates that branch and backtracks to the next
It creates a state space tree and calls the safe function until it receives a
False Boolean and terminates that branch and backtracks to the next
possible solution branch.
"""
if row >= len(board):
"""
If the row number exceeds N we have board with a successful combination
If the row number exceeds N we have board with a successful combination
and that combination is appended to the solution list and the board is printed.
"""
Expand All @@ -56,9 +56,9 @@ def solve(board, row):
return
for i in range(len(board)):
"""
For every row it iterates through each column to check if it is feasible to place a
For every row it iterates through each column to check if it is feasible to place a
queen there.
If all the combinations for that particular branch are successful the board is
If all the combinations for that particular branch are successful the board is
reinitialized for the next possible combination.
"""
if isSafe(board, row, i):
Expand Down
26 changes: 18 additions & 8 deletions ciphers/decrypt_caesar_with_chi_squared.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/usr/bin/env python3


def decrypt_caesar_with_chi_squared(
ciphertext: str,
cipher_alphabet=None,
frequencies_dict=None,
case_sensetive: bool = False,
) -> list:
) -> tuple:
"""
Basic Usage
===========
Expand Down Expand Up @@ -96,15 +99,19 @@ def decrypt_caesar_with_chi_squared(
Further Reading
================
* http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-statistic/
* http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-
statistic/
* https://en.wikipedia.org/wiki/Letter_frequency
* https://en.wikipedia.org/wiki/Chi-squared_test
* https://en.m.wikipedia.org/wiki/Caesar_cipher
Doctests
========
>>> decrypt_caesar_with_chi_squared('dof pz aol jhlzhy jpwoly zv wvwbshy? pa pz avv lhzf av jyhjr!')
(7, 3129.228005747531, 'why is the caesar cipher so popular? it is too easy to crack!')
>>> decrypt_caesar_with_chi_squared(
... 'dof pz aol jhlzhy jpwoly zv wvwbshy? pa pz avv lhzf av jyhjr!'
... ) # doctest: +NORMALIZE_WHITESPACE
(7, 3129.228005747531,
'why is the caesar cipher so popular? it is too easy to crack!')
>>> decrypt_caesar_with_chi_squared('crybd cdbsxq')
(10, 233.35343938980898, 'short string')
Expand Down Expand Up @@ -172,7 +179,7 @@ def decrypt_caesar_with_chi_squared(
# Append the character if it isn't in the alphabet
decrypted_with_shift += letter

chi_squared_statistic = 0
chi_squared_statistic = 0.0

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

# Get the excepcted amount of times the letter should appear based on letter frequencies
# Get the excepcted amount of times the letter should appear based
# on letter frequencies
expected = frequencies[letter] * occurrences

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

# Get the excepcted amount of times the letter should appear based on letter frequencies
# Get the excepcted amount of times the letter should appear based
# on letter frequencies
expected = frequencies[letter] * occurrences

# Complete the chi squared statistic formula
Expand All @@ -209,7 +218,8 @@ def decrypt_caesar_with_chi_squared(
decrypted_with_shift,
]

# Get the most likely cipher by finding the cipher with the smallest chi squared statistic
# Get the most likely cipher by finding the cipher with the smallest chi squared
# statistic
most_likely_cipher = min(
chi_squared_statistic_values, key=chi_squared_statistic_values.get
)
Expand Down
4 changes: 3 additions & 1 deletion ciphers/elgamal_key_generator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import random
import sys
import rabin_miller as rabinMiller, cryptomath_module as cryptoMath

import cryptomath_module as cryptoMath
import rabin_miller as rabinMiller

min_primitive_root = 3

Expand Down
8 changes: 4 additions & 4 deletions ciphers/mixed_keyword_cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def mixed_keyword(key="college", pt="UNIVERSITY"):
for i in key:
if i not in temp:
temp.append(i)
l = len(temp)
len_temp = len(temp)
# print(temp)
alpha = []
modalpha = []
Expand All @@ -40,17 +40,17 @@ def mixed_keyword(key="college", pt="UNIVERSITY"):
k = 0
for i in range(r):
t = []
for j in range(l):
for j in range(len_temp):
t.append(temp[k])
if not (k < 25):
break
k += 1
modalpha.append(t)
# print(modalpha)
d = dict()
d = {}
j = 0
k = 0
for j in range(l):
for j in range(len_temp):
for i in modalpha:
if not (len(i) - 1 >= j):
break
Expand Down
3 changes: 2 additions & 1 deletion ciphers/simple_substitution_cipher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys, random
import random
import sys

LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Expand Down
5 changes: 4 additions & 1 deletion ciphers/transposition_cipher_encrypt_decrypt_file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import time, os, sys
import os
import sys
import time

import transposition_cipher as transCipher


Expand Down
2 changes: 1 addition & 1 deletion conversions/decimal_to_octal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def decimal_to_octal(num: int) -> str:
"""Convert a Decimal Number to an Octal Number.
>>> all(decimal_to_octal(i) == oct(i) for i in (0, 2, 8, 64, 65, 216, 255, 256, 512))
True
"""
Expand Down
Loading

0 comments on commit 1f8a21d

Please sign in to comment.