Skip to content
Permalink
Browse files

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>
  • Loading branch information
cclauss and github-actions committed May 22, 2020
1 parent 21ed896 commit 1f8a21d7276a05442e02570dbbfe27cd4f0365a5
Showing with 587 additions and 499 deletions.
  1. +5 βˆ’5 .travis.yml
  2. +3 βˆ’0 DIRECTORY.md
  3. +1 βˆ’0 arithmetic_analysis/newton_forward_interpolation.py
  4. +6 βˆ’6 backtracking/coloring.py
  5. +15 βˆ’15 backtracking/hamiltonian_cycle.py
  6. +2 βˆ’2 backtracking/minimax.py
  7. +8 βˆ’8 backtracking/n_queens.py
  8. +18 βˆ’8 ciphers/decrypt_caesar_with_chi_squared.py
  9. +3 βˆ’1 ciphers/elgamal_key_generator.py
  10. +4 βˆ’4 ciphers/mixed_keyword_cypher.py
  11. +2 βˆ’1 ciphers/simple_substitution_cipher.py
  12. +4 βˆ’1 ciphers/transposition_cipher_encrypt_decrypt_file.py
  13. +1 βˆ’1 conversions/decimal_to_octal.py
  14. +9 βˆ’9 data_structures/binary_tree/avl_tree.py
  15. +6 βˆ’1 data_structures/binary_tree/basic_binary_tree.py
  16. +19 βˆ’14 data_structures/binary_tree/lazy_segment_tree.py
  17. +9 βˆ’6 data_structures/binary_tree/non_recursive_segment_tree.py
  18. +12 βˆ’10 data_structures/binary_tree/segment_tree.py
  19. +4 βˆ’1 data_structures/binary_tree/treap.py
  20. +1 βˆ’1 data_structures/hashing/quadratic_probing.py
  21. +2 βˆ’0 data_structures/heap/binomial_heap.py
  22. +2 βˆ’2 data_structures/heap/min_heap.py
  23. +4 βˆ’4 data_structures/linked_list/deque_doubly.py
  24. +1 βˆ’1 data_structures/linked_list/middle_element_of_linked_list.py
  25. +9 βˆ’11 data_structures/stacks/postfix_evaluation.py
  26. +1 βˆ’1 data_structures/trie/trie.py
  27. +3 βˆ’3 digital_image_processing/dithering/burkes.py
  28. +2 βˆ’2 digital_image_processing/edge_detection/canny.py
  29. +40 βˆ’32 digital_image_processing/index_calculation.py
  30. +14 βˆ’14 divide_and_conquer/max_subarray_sum.py
  31. +1 βˆ’1 divide_and_conquer/mergesort.py
  32. +3 βˆ’3 dynamic_programming/factorial.py
  33. +2 βˆ’2 dynamic_programming/iterating_through_submasks.py
  34. +1 βˆ’1 dynamic_programming/longest_common_subsequence.py
  35. +11 βˆ’6 dynamic_programming/longest_increasing_subsequence.py
  36. +6 βˆ’5 dynamic_programming/longest_increasing_subsequence_o(nlogn).py
  37. +3 βˆ’3 dynamic_programming/max_sub_array.py
  38. +1 βˆ’1 dynamic_programming/minimum_partition.py
  39. +7 βˆ’6 dynamic_programming/optimal_binary_search_tree.py
  40. +10 βˆ’7 dynamic_programming/subset_generation.py
  41. +2 βˆ’2 geodesy/lamberts_ellipsoidal_distance.py
  42. +4 βˆ’4 graphs/articulation_points.py
  43. +6 βˆ’5 graphs/basic_graphs.py
  44. +1 βˆ’1 graphs/bellman_ford.py
  45. +6 βˆ’5 graphs/breadth_first_search_shortest_path.py
  46. +10 βˆ’9 graphs/check_bipartite_graph_bfs.py
  47. +10 βˆ’9 graphs/check_bipartite_graph_dfs.py
  48. +4 βˆ’4 graphs/depth_first_search.py
  49. +2 βˆ’2 graphs/depth_first_search_2.py
  50. +2 βˆ’3 graphs/dijkstra.py
  51. +1 βˆ’1 graphs/dinic.py
  52. +24 βˆ’24 graphs/directed_and_undirected_(weighted)_graph.py
  53. +1 βˆ’1 graphs/eulerian_path_and_circuit_for_undirected_graph.py
  54. +5 βˆ’5 graphs/finding_bridges.py
  55. +1 βˆ’1 graphs/frequent_pattern_graph_miner.py
  56. +7 βˆ’7 graphs/kahns_algorithm_long.py
  57. +11 βˆ’8 graphs/kahns_algorithm_topo.py
  58. +2 βˆ’2 graphs/minimum_spanning_tree_prims.py
  59. +20 βˆ’21 hashes/chaos_machine.py
  60. +1 βˆ’0 hashes/hamming_code.py
  61. +9 βˆ’9 linear_algebra/src/lib.py
  62. +1 βˆ’1 linear_algebra/src/test_linear_algebra.py
  63. +16 βˆ’11 machine_learning/k_means_clust.py
  64. +3 βˆ’2 machine_learning/linear_discriminant_analysis.py
  65. +7 βˆ’7 machine_learning/polymonial_regression.py
  66. +1 βˆ’0 machine_learning/scoring_functions.py
  67. +1 βˆ’1 maths/aliquot_sum.py
  68. +2 βˆ’2 maths/allocation_number.py
  69. +5 βˆ’5 maths/bailey_borwein_plouffe.py
  70. +3 βˆ’2 maths/collatz_sequence.py
  71. +1 βˆ’1 maths/find_max_recursion.py
  72. +6 βˆ’6 maths/gamma.py
  73. +2 βˆ’2 maths/gaussian.py
  74. +2 βˆ’2 maths/is_square_free.py
  75. +4 βˆ’4 maths/kth_lexicographic_permutation.py
  76. +5 βˆ’5 maths/lucas_lehmer_primality_test.py
  77. +1 βˆ’1 maths/matrix_exponentiation.py
  78. +1 βˆ’1 maths/modular_exponential.py
  79. +5 βˆ’5 maths/monte_carlo.py
  80. +2 βˆ’2 maths/newton_raphson.py
  81. +1 βˆ’1 maths/prime_factors.py
  82. +4 βˆ’2 maths/prime_sieve_eratosthenes.py
  83. +2 βˆ’2 maths/radians.py
  84. +15 βˆ’15 maths/radix2_fft.py
  85. +2 βˆ’2 maths/sieve_of_eratosthenes.py
  86. +2 βˆ’2 maths/square_root.py
  87. +6 βˆ’6 matrix/sherman_morrison.py
  88. +1 βˆ’1 networking_flow/ford_fulkerson.py
  89. +1 βˆ’1 networking_flow/minimum_cut.py
  90. +6 βˆ’4 other/activity_selection.py
  91. +4 βˆ’1 other/anagrams.py
  92. +3 βˆ’2 other/detecting_english_programmatically.py
  93. +1 βˆ’1 other/dijkstra_bankers_algorithm.py
  94. +5 βˆ’3 other/game_of_life.py
  95. +4 βˆ’4 other/integeration_by_simpson_approx.py
  96. +1 βˆ’0 other/magicdiamondpattern.py
  97. +2 βˆ’2 other/sdes.py
  98. +1 βˆ’1 project_euler/problem_02/sol4.py
  99. +1 βˆ’1 project_euler/problem_03/sol1.py
  100. +1 βˆ’1 project_euler/problem_03/sol2.py
  101. +1 βˆ’1 project_euler/problem_05/sol1.py
  102. +1 βˆ’1 project_euler/problem_07/sol2.py
  103. +1 βˆ’1 project_euler/problem_08/sol1.py
  104. +1 βˆ’1 project_euler/problem_08/sol2.py
  105. +1 βˆ’1 project_euler/problem_08/sol3.py
  106. +1 βˆ’1 project_euler/problem_11/sol2.py
  107. +1 βˆ’1 project_euler/problem_16/sol2.py
  108. +1 βˆ’1 project_euler/problem_234/sol1.py
  109. +1 βˆ’1 project_euler/problem_30/soln.py
  110. +1 βˆ’1 project_euler/problem_31/sol2.py
  111. +1 βˆ’1 project_euler/problem_551/sol1.py
  112. +1 βˆ’1 scheduling/first_come_first_served.py
  113. +3 βˆ’1 searches/simulated_annealing.py
  114. +1 βˆ’0 searches/ternary_search.py
  115. +2 βˆ’1 sorts/bitonic_sort.py
  116. +2 βˆ’2 sorts/pigeon_sort.py
  117. +1 βˆ’1 sorts/recursive_bubble_sort.py
  118. +12 βˆ’12 strings/boyer_moore_search.py
  119. +4 βˆ’4 strings/lower.py
  120. +23 βˆ’19 strings/manacher.py
  121. +1 βˆ’4 strings/reverse_words.py
  122. +4 βˆ’4 strings/split.py
  123. +4 βˆ’4 strings/upper.py
  124. +3 βˆ’1 traversals/binary_tree_traversals.py
@@ -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:
@@ -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)
@@ -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)
@@ -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)
@@ -2,6 +2,7 @@

import math


# for calculating u value
def ucal(u, p):
"""
@@ -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
@@ -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:
@@ -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
@@ -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],
@@ -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
@@ -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],
@@ -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],
@@ -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)
| / \ |
| / \ |
@@ -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)
| / \ |
| / \ |
@@ -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)
| / \ |
| / \ |
@@ -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
"""

@@ -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.
"""
@@ -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 :
@@ -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.
"""
@@ -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):
@@ -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
===========
@@ -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')
@@ -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:
@@ -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
@@ -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
@@ -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
)
@@ -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

@@ -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 = []
@@ -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
@@ -1,4 +1,5 @@
import sys, random
import random
import sys

LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

@@ -1,4 +1,7 @@
import time, os, sys
import os
import sys
import time

import transposition_cipher as transCipher


@@ -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
"""

0 comments on commit 1f8a21d

Please sign in to comment.
You can’t perform that action at this time.