Skip to content

Commit 6acd7fb

Browse files
cclaussgithub-actions
and
github-actions
authored
Wrap lines that go beyond GitHub Editor (TheAlgorithms#1925)
* Wrap lines that go beyond GiHub Editor * flake8 --count --select=E501 --max-line-length=127 * updating DIRECTORY.md * Update strassen_matrix_multiplication.py * fixup! Format Python code with psf/black push * Update decision_tree.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent bcaa88b commit 6acd7fb

19 files changed

+161
-82
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ before_install: pip install --upgrade pip setuptools six
77
install: pip install -r requirements.txt
88
before_script:
99
- black --check . || true
10-
- flake8 . --count --select=E101,E722,E9,F4,F63,F7,F82,W191 --show-source --statistics
10+
- flake8 . --count --select=E101,E501,E722,E9,F4,F63,F7,F82,W191 --max-line-length=127 --show-source --statistics
1111
- flake8 . --count --exit-zero --max-line-length=127 --statistics
1212
script:
1313
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory

DIRECTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [All Combinations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_combinations.py)
1515
* [All Permutations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_permutations.py)
1616
* [All Subsequences](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_subsequences.py)
17+
* [Coloring](https://github.com/TheAlgorithms/Python/blob/master/backtracking/coloring.py)
1718
* [Minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py)
1819
* [N Queens](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens.py)
1920
* [Sudoku](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sudoku.py)
@@ -31,6 +32,7 @@
3132
* [One Dimensional](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/one_dimensional.py)
3233

3334
## Ciphers
35+
* [A1Z26](https://github.com/TheAlgorithms/Python/blob/master/ciphers/a1z26.py)
3436
* [Affine Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/affine_cipher.py)
3537
* [Atbash](https://github.com/TheAlgorithms/Python/blob/master/ciphers/atbash.py)
3638
* [Base16](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base16.py)
@@ -138,6 +140,8 @@
138140
## Digital Image Processing
139141
* [Change Contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py)
140142
* [Convert To Negative](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/convert_to_negative.py)
143+
* Dithering
144+
* [Burkes](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/dithering/burkes.py)
141145
* Edge Detection
142146
* [Canny](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/edge_detection/canny.py)
143147
* Filters

ciphers/rsa_cipher.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import sys, rsa_key_generator as rkg, os
1+
import os
2+
import sys
3+
4+
import rsa_key_generator as rkg
25

36
DEFAULT_BLOCK_SIZE = 128
47
BYTE_SIZE = 256
@@ -92,7 +95,9 @@ def encryptAndWriteToFile(
9295
keySize, n, e = readKeyFile(keyFilename)
9396
if keySize < blockSize * 8:
9497
sys.exit(
95-
"ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Either decrease the block size or use different keys."
98+
"ERROR: Block size is %s bits and key size is %s bits. The RSA cipher "
99+
"requires the block size to be equal to or greater than the key size. "
100+
"Either decrease the block size or use different keys."
96101
% (blockSize * 8, keySize)
97102
)
98103

@@ -117,7 +122,9 @@ def readFromFileAndDecrypt(messageFilename, keyFilename):
117122

118123
if keySize < blockSize * 8:
119124
sys.exit(
120-
"ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Did you specify the correct key file and encrypted file?"
125+
"ERROR: Block size is %s bits and key size is %s bits. The RSA cipher "
126+
"requires the block size to be equal to or greater than the key size. "
127+
"Did you specify the correct key file and encrypted file?"
121128
% (blockSize * 8, keySize)
122129
)
123130

ciphers/rsa_key_generator.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import random, sys, os
2-
import rabin_miller as rabinMiller, cryptomath_module as cryptoMath
1+
import os
2+
import random
3+
import sys
4+
5+
import cryptomath_module as cryptoMath
6+
import rabin_miller as rabinMiller
37

48

59
def main():
@@ -35,7 +39,8 @@ def makeKeyFiles(name, keySize):
3539
):
3640
print("\nWARNING:")
3741
print(
38-
'"%s_pubkey.txt" or "%s_privkey.txt" already exists. \nUse a different name or delete these files and re-run this program.'
42+
'"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n'
43+
"Use a different name or delete these files and re-run this program."
3944
% (name, name)
4045
)
4146
sys.exit()

data_structures/linked_list/doubly_linked_list.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""
2-
- A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes.
2+
- A linked list is similar to an array, it holds values. However, links in a linked
3+
list do not have indexes.
34
- This is an example of a double ended, doubly linked list.
45
- Each link references the next link and the previous one.
5-
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
6-
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficient"""
6+
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous
7+
pointer, together with next pointer and data which are there in singly linked list.
8+
- Advantages over SLL - IT can be traversed in both forward and backward direction.,
9+
Delete operation is more efficient"""
710

811

912
class LinkedList: # making main class named linked list
@@ -13,7 +16,7 @@ def __init__(self):
1316

1417
def insertHead(self, x):
1518
newLink = Link(x) # Create a new link with a value attached to it
16-
if self.isEmpty() == True: # Set the first element added to be the tail
19+
if self.isEmpty(): # Set the first element added to be the tail
1720
self.tail = newLink
1821
else:
1922
self.head.previous = newLink # newLink <-- currenthead(head)
@@ -23,7 +26,9 @@ def insertHead(self, x):
2326
def deleteHead(self):
2427
temp = self.head
2528
self.head = self.head.next # oldHead <--> 2ndElement(head)
26-
self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed
29+
# oldHead --> 2ndElement(head) nothing pointing at it so the old head will be
30+
# removed
31+
self.head.previous = None
2732
if self.head is None:
2833
self.tail = None # if empty linked list
2934
return temp

divide_and_conquer/strassen_matrix_multiplication.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@ def matrix_subtraction(matrix_a: List, matrix_b: List):
3131

3232
def split_matrix(a: List,) -> Tuple[List, List, List, List]:
3333
"""
34-
Given an even length matrix, returns the top_left, top_right, bot_left, bot_right quadrant.
34+
Given an even length matrix, returns the top_left, top_right, bot_left, bot_right
35+
quadrant.
3536
3637
>>> split_matrix([[4,3,2,4],[2,3,1,1],[6,5,4,3],[8,4,1,6]])
3738
([[4, 3], [2, 3]], [[2, 4], [1, 1]], [[6, 5], [8, 4]], [[4, 3], [1, 6]])
38-
>>> split_matrix([[4,3,2,4,4,3,2,4],[2,3,1,1,2,3,1,1],[6,5,4,3,6,5,4,3],[8,4,1,6,8,4,1,6],[4,3,2,4,4,3,2,4],[2,3,1,1,2,3,1,1],[6,5,4,3,6,5,4,3],[8,4,1,6,8,4,1,6]])
39-
([[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]])
39+
>>> split_matrix([
40+
... [4,3,2,4,4,3,2,4],[2,3,1,1,2,3,1,1],[6,5,4,3,6,5,4,3],[8,4,1,6,8,4,1,6],
41+
... [4,3,2,4,4,3,2,4],[2,3,1,1,2,3,1,1],[6,5,4,3,6,5,4,3],[8,4,1,6,8,4,1,6]
42+
... ]) # doctest: +NORMALIZE_WHITESPACE
43+
([[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4],
44+
[2, 3, 1, 1], [6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1],
45+
[6, 5, 4, 3], [8, 4, 1, 6]], [[4, 3, 2, 4], [2, 3, 1, 1], [6, 5, 4, 3],
46+
[8, 4, 1, 6]])
4047
"""
4148
if len(a) % 2 != 0 or len(a[0]) % 2 != 0:
4249
raise Exception("Odd matrices are not supported!")
@@ -66,8 +73,8 @@ def print_matrix(matrix: List) -> None:
6673

6774
def actual_strassen(matrix_a: List, matrix_b: List) -> List:
6875
"""
69-
Recursive function to calculate the product of two matrices, using the Strassen Algorithm.
70-
It only supports even length matrices.
76+
Recursive function to calculate the product of two matrices, using the Strassen
77+
Algorithm. It only supports even length matrices.
7178
"""
7279
if matrix_dimensions(matrix_a) == (2, 2):
7380
return default_matrix_multiplication(matrix_a, matrix_b)
@@ -106,7 +113,8 @@ def strassen(matrix1: List, matrix2: List) -> List:
106113
"""
107114
if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]:
108115
raise Exception(
109-
f"Unable to multiply these matrices, please check the dimensions. \nMatrix A:{matrix1} \nMatrix B:{matrix2}"
116+
f"Unable to multiply these matrices, please check the dimensions. \n"
117+
f"Matrix A:{matrix1} \nMatrix B:{matrix2}"
110118
)
111119
dimension1 = matrix_dimensions(matrix1)
112120
dimension2 = matrix_dimensions(matrix2)
@@ -119,7 +127,8 @@ def strassen(matrix1: List, matrix2: List) -> List:
119127
new_matrix1 = matrix1
120128
new_matrix2 = matrix2
121129

122-
# Adding zeros to the matrices so that the arrays dimensions are the same and also power of 2
130+
# Adding zeros to the matrices so that the arrays dimensions are the same and also
131+
# power of 2
123132
for i in range(0, maxim):
124133
if i < dimension1[0]:
125134
for j in range(dimension1[1], maxim):

dynamic_programming/bitmask.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
Here Bitmasking and DP are used for solving this.
55
66
Question :-
7-
We have N tasks and M people. Each person in M can do only certain of these tasks. Also a person can do only one task and a task is performed only by one person.
7+
We have N tasks and M people. Each person in M can do only certain of these tasks. Also
8+
a person can do only one task and a task is performed only by one person.
89
Find the total no of ways in which the tasks can be distributed.
9-
10-
1110
"""
1211
from collections import defaultdict
1312

@@ -25,7 +24,8 @@ def __init__(self, task_performed, total):
2524

2625
self.task = defaultdict(list) # stores the list of persons for each task
2726

28-
# final_mask is used to check if all persons are included by setting all bits to 1
27+
# final_mask is used to check if all persons are included by setting all bits
28+
# to 1
2929
self.final_mask = (1 << len(task_performed)) - 1
3030

3131
def CountWaysUtil(self, mask, task_no):
@@ -45,15 +45,17 @@ def CountWaysUtil(self, mask, task_no):
4545
# Number of ways when we don't this task in the arrangement
4646
total_ways_util = self.CountWaysUtil(mask, task_no + 1)
4747

48-
# now assign the tasks one by one to all possible persons and recursively assign for the remaining tasks.
48+
# now assign the tasks one by one to all possible persons and recursively
49+
# assign for the remaining tasks.
4950
if task_no in self.task:
5051
for p in self.task[task_no]:
5152

5253
# if p is already given a task
5354
if mask & (1 << p):
5455
continue
5556

56-
# assign this task to p and change the mask value. And recursively assign tasks with the new mask value.
57+
# assign this task to p and change the mask value. And recursively
58+
# assign tasks with the new mask value.
5759
total_ways_util += self.CountWaysUtil(mask | (1 << p), task_no + 1)
5860

5961
# save the value.
@@ -85,6 +87,7 @@ def countNoOfWays(self, task_performed):
8587
)
8688
"""
8789
For the particular example the tasks can be distributed as
88-
(1,2,3), (1,2,4), (1,5,3), (1,5,4), (3,1,4), (3,2,4), (3,5,4), (4,1,3), (4,2,3), (4,5,3)
90+
(1,2,3), (1,2,4), (1,5,3), (1,5,4), (3,1,4),
91+
(3,2,4), (3,5,4), (4,1,3), (4,2,3), (4,5,3)
8992
total 10
9093
"""

dynamic_programming/edit_distance.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
Author : Turfa Auliarachman
33
Date : October 12, 2016
44
5-
This is a pure Python implementation of Dynamic Programming solution to the edit distance problem.
5+
This is a pure Python implementation of Dynamic Programming solution to the edit
6+
distance problem.
67
78
The problem is :
8-
Given two strings A and B. Find the minimum number of operations to string B such that A = B. The permitted operations are removal, insertion, and substitution.
9+
Given two strings A and B. Find the minimum number of operations to string B such that
10+
A = B. The permitted operations are removal, insertion, and substitution.
911
"""
1012

1113

machine_learning/decision_tree.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@ def mean_squared_error(self, labels, prediction):
2020
mean_squared_error:
2121
@param labels: a one dimensional numpy array
2222
@param prediction: a floating point value
23-
return value: mean_squared_error calculates the error if prediction is used to estimate the labels
23+
return value: mean_squared_error calculates the error if prediction is used to
24+
estimate the labels
2425
>>> tester = Decision_Tree()
2526
>>> test_labels = np.array([1,2,3,4,5,6,7,8,9,10])
2627
>>> test_prediction = np.float(6)
27-
>>> assert tester.mean_squared_error(test_labels, test_prediction) == Test_Decision_Tree.helper_mean_squared_error_test(test_labels, test_prediction)
28+
>>> tester.mean_squared_error(test_labels, test_prediction) == (
29+
... Test_Decision_Tree.helper_mean_squared_error_test(test_labels,
30+
... test_prediction))
31+
True
2832
>>> test_labels = np.array([1,2,3])
2933
>>> test_prediction = np.float(2)
30-
>>> assert tester.mean_squared_error(test_labels, test_prediction) == Test_Decision_Tree.helper_mean_squared_error_test(test_labels, test_prediction)
31-
34+
>>> tester.mean_squared_error(test_labels, test_prediction) == (
35+
... Test_Decision_Tree.helper_mean_squared_error_test(test_labels,
36+
... test_prediction))
37+
True
3238
"""
3339
if labels.ndim != 1:
3440
print("Error: Input labels must be one dimensional")
@@ -46,7 +52,8 @@ def train(self, X, y):
4652
"""
4753

4854
"""
49-
this section is to check that the inputs conform to our dimensionality constraints
55+
this section is to check that the inputs conform to our dimensionality
56+
constraints
5057
"""
5158
if X.ndim != 1:
5259
print("Error: Input data set must be one dimensional")
@@ -72,7 +79,8 @@ def train(self, X, y):
7279
"""
7380
loop over all possible splits for the decision tree. find the best split.
7481
if no split exists that is less than 2 * error for the entire array
75-
then the data set is not split and the average for the entire array is used as the predictor
82+
then the data set is not split and the average for the entire array is used as
83+
the predictor
7684
"""
7785
for i in range(len(X)):
7886
if len(X[:i]) < self.min_leaf_size:
@@ -136,7 +144,7 @@ def helper_mean_squared_error_test(labels, prediction):
136144
helper_mean_squared_error_test:
137145
@param labels: a one dimensional numpy array
138146
@param prediction: a floating point value
139-
return value: helper_mean_squared_error_test calculates the mean squared error
147+
return value: helper_mean_squared_error_test calculates the mean squared error
140148
"""
141149
squared_error_sum = np.float(0)
142150
for label in labels:
@@ -147,9 +155,10 @@ def helper_mean_squared_error_test(labels, prediction):
147155

148156
def main():
149157
"""
150-
In this demonstration we're generating a sample data set from the sin function in numpy.
151-
We then train a decision tree on the data set and use the decision tree to predict the
152-
label of 10 different test values. Then the mean squared error over this test is displayed.
158+
In this demonstration we're generating a sample data set from the sin function in
159+
numpy. We then train a decision tree on the data set and use the decision tree to
160+
predict the label of 10 different test values. Then the mean squared error over
161+
this test is displayed.
153162
"""
154163
X = np.arange(-1.0, 1.0, 0.005)
155164
y = np.sin(X)

machine_learning/logistic_regression.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#!/usr/bin/python
22

3-
## Logistic Regression from scratch
3+
# Logistic Regression from scratch
44

55
# In[62]:
66

77
# In[63]:
88

99
# importing all the required libraries
1010

11-
""" Implementing logistic regression for classification problem
12-
Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac"""
11+
"""
12+
Implementing logistic regression for classification problem
13+
Helpful resources:
14+
Coursera ML course
15+
https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac
16+
"""
1317

1418
import numpy as np
1519
import matplotlib.pyplot as plt
@@ -21,7 +25,8 @@
2125

2226
# In[67]:
2327

24-
# sigmoid function or logistic function is used as a hypothesis function in classification problems
28+
# sigmoid function or logistic function is used as a hypothesis function in
29+
# classification problems
2530

2631

2732
def sigmoid_function(z):

machine_learning/support_vector_machines.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from sklearn.model_selection import train_test_split
44
import doctest
55

6+
67
# different functions implementing different types of SVM's
78
def NuSVC(train_x, train_y):
89
svc_NuSVC = svm.NuSVC()
@@ -17,8 +18,11 @@ def Linearsvc(train_x, train_y):
1718

1819

1920
def SVC(train_x, train_y):
20-
# svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True, probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, random_state=None)
21-
# various parameters like "kernel","gamma","C" can effectively tuned for a given machine learning model.
21+
# svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True,
22+
# probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False,
23+
# max_iter=-1, random_state=None)
24+
# various parameters like "kernel","gamma","C" can effectively tuned for a given
25+
# machine learning model.
2226
SVC = svm.SVC(gamma="auto")
2327
SVC.fit(train_x, train_y)
2428
return SVC
@@ -27,8 +31,8 @@ def SVC(train_x, train_y):
2731
def test(X_new):
2832
"""
2933
3 test cases to be passed
30-
an array containing the sepal length (cm), sepal width (cm),petal length (cm),petal width (cm)
31-
based on which the target name will be predicted
34+
an array containing the sepal length (cm), sepal width (cm), petal length (cm),
35+
petal width (cm) based on which the target name will be predicted
3236
>>> test([1,2,1,4])
3337
'virginica'
3438
>>> test([5, 2, 4, 1])

0 commit comments

Comments
 (0)