GeezerVison is showing github.com — Wrap lines that go beyond GitHub Editor (#1925) · jusing-es/Python@6acd7fb
File treeExpand file treeCollapse file tree Open diff view settings data_structures/linked_list
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .travis.yml +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,7 +7,7 @@ before_install: pip install --upgrade pip setuptools six | 7 | 7 | install: pip install -r requirements.txt | 8 | 8 | before_script: | 9 | 9 | - 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 | 11 | 11 | - flake8 . --count --exit-zero --max-line-length=127 --statistics | 12 | 12 | script: | 13 | 13 | - scripts/validate_filenames.py # no uppercase, no spaces, in a directory | |
Collapse file Copy file name to clipboardExpand all lines: DIRECTORY.md +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | * [All Combinations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_combinations.py) | 15 | 15 | * [All Permutations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_permutations.py) | 16 | 16 | * [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) | 17 | 18 | * [Minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py) | 18 | 19 | * [N Queens](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens.py) | 19 | 20 | * [Sudoku](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sudoku.py) | | 31 | 32 | * [One Dimensional](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/one_dimensional.py) | 32 | 33 |
| 33 | 34 | ## Ciphers | | 35 | + * [A1Z26](https://github.com/TheAlgorithms/Python/blob/master/ciphers/a1z26.py) | 34 | 36 | * [Affine Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/affine_cipher.py) | 35 | 37 | * [Atbash](https://github.com/TheAlgorithms/Python/blob/master/ciphers/atbash.py) | 36 | 38 | * [Base16](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base16.py) | | 138 | 140 | ## Digital Image Processing | 139 | 141 | * [Change Contrast](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/change_contrast.py) | 140 | 142 | * [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) | 141 | 145 | * Edge Detection | 142 | 146 | * [Canny](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/edge_detection/canny.py) | 143 | 147 | * Filters | |
Collapse file Copy file name to clipboardExpand all lines: ciphers/rsa_cipher.py +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import sys, rsa_key_generator as rkg, os | | 1 | +import os | | 2 | +import sys | | 3 | + | | 4 | +import rsa_key_generator as rkg | 2 | 5 |
| 3 | 6 | DEFAULT_BLOCK_SIZE = 128 | 4 | 7 | BYTE_SIZE = 256 | @@ -92,7 +95,9 @@ def encryptAndWriteToFile( | 92 | 95 | keySize, n, e = readKeyFile(keyFilename) | 93 | 96 | if keySize < blockSize * 8: | 94 | 97 | 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." | 96 | 101 | % (blockSize * 8, keySize) | 97 | 102 | ) | 98 | 103 |
| @@ -117,7 +122,9 @@ def readFromFileAndDecrypt(messageFilename, keyFilename): | 117 | 122 |
| 118 | 123 | if keySize < blockSize * 8: | 119 | 124 | 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?" | 121 | 128 | % (blockSize * 8, keySize) | 122 | 129 | ) | 123 | 130 |
| |
Collapse file Copy file name to clipboardExpand all lines: ciphers/rsa_key_generator.py +8-3Lines changed: 8 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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 | 3 | 7 |
| 4 | 8 |
| 5 | 9 | def main(): | @@ -35,7 +39,8 @@ def makeKeyFiles(name, keySize): | 35 | 39 | ): | 36 | 40 | print("\nWARNING:") | 37 | 41 | 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." | 39 | 44 | % (name, name) | 40 | 45 | ) | 41 | 46 | sys.exit() | |
Collapse file Copy file name to clipboardExpand all lines: data_structures/linked_list/doubly_linked_list.py +10-5Lines changed: 10 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | """ | 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. | 3 | 4 | - This is an example of a double ended, doubly linked list. | 4 | 5 | - 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""" | 7 | 10 |
| 8 | 11 |
| 9 | 12 | class LinkedList: # making main class named linked list | @@ -13,7 +16,7 @@ def __init__(self): | 13 | 16 |
| 14 | 17 | def insertHead(self, x): | 15 | 18 | 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 | 17 | 20 | self.tail = newLink | 18 | 21 | else: | 19 | 22 | self.head.previous = newLink # newLink <-- currenthead(head) | @@ -23,7 +26,9 @@ def insertHead(self, x): | 23 | 26 | def deleteHead(self): | 24 | 27 | temp = self.head | 25 | 28 | 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 | 27 | 32 | if self.head is None: | 28 | 33 | self.tail = None # if empty linked list | 29 | 34 | return temp | |
Collapse file Copy file name to clipboardExpand all lines: divide_and_conquer/strassen_matrix_multiplication.py +16-7Lines changed: 16 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,12 +31,19 @@ def matrix_subtraction(matrix_a: List, matrix_b: List): | 31 | 31 |
| 32 | 32 | def split_matrix(a: List,) -> Tuple[List, List, List, List]: | 33 | 33 | """ | 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. | 35 | 36 |
| 36 | 37 | >>> split_matrix([[4,3,2,4],[2,3,1,1],[6,5,4,3],[8,4,1,6]]) | 37 | 38 | ([[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]]) | 40 | 47 | """ | 41 | 48 | if len(a) % 2 != 0 or len(a[0]) % 2 != 0: | 42 | 49 | raise Exception("Odd matrices are not supported!") | @@ -66,8 +73,8 @@ def print_matrix(matrix: List) -> None: | 66 | 73 |
| 67 | 74 | def actual_strassen(matrix_a: List, matrix_b: List) -> List: | 68 | 75 | """ | 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. | 71 | 78 | """ | 72 | 79 | if matrix_dimensions(matrix_a) == (2, 2): | 73 | 80 | return default_matrix_multiplication(matrix_a, matrix_b) | @@ -106,7 +113,8 @@ def strassen(matrix1: List, matrix2: List) -> List: | 106 | 113 | """ | 107 | 114 | if matrix_dimensions(matrix1)[1] != matrix_dimensions(matrix2)[0]: | 108 | 115 | 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}" | 110 | 118 | ) | 111 | 119 | dimension1 = matrix_dimensions(matrix1) | 112 | 120 | dimension2 = matrix_dimensions(matrix2) | @@ -119,7 +127,8 @@ def strassen(matrix1: List, matrix2: List) -> List: | 119 | 127 | new_matrix1 = matrix1 | 120 | 128 | new_matrix2 = matrix2 | 121 | 129 |
| 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 | 123 | 132 | for i in range(0, maxim): | 124 | 133 | if i < dimension1[0]: | 125 | 134 | for j in range(dimension1[1], maxim): | |
Collapse file Copy file name to clipboardExpand all lines: dynamic_programming/bitmask.py +10-7Lines changed: 10 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | Here Bitmasking and DP are used for solving this. | 5 | 5 |
| 6 | 6 | 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. | 8 | 9 | Find the total no of ways in which the tasks can be distributed. | 9 | | -
| 10 | | -
| 11 | 10 | """ | 12 | 11 | from collections import defaultdict | 13 | 12 |
| @@ -25,7 +24,8 @@ def __init__(self, task_performed, total): | 25 | 24 |
| 26 | 25 | self.task = defaultdict(list) # stores the list of persons for each task | 27 | 26 |
| 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 | 29 | 29 | self.final_mask = (1 << len(task_performed)) - 1 | 30 | 30 |
| 31 | 31 | def CountWaysUtil(self, mask, task_no): | @@ -45,15 +45,17 @@ def CountWaysUtil(self, mask, task_no): | 45 | 45 | # Number of ways when we don't this task in the arrangement | 46 | 46 | total_ways_util = self.CountWaysUtil(mask, task_no + 1) | 47 | 47 |
| 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. | 49 | 50 | if task_no in self.task: | 50 | 51 | for p in self.task[task_no]: | 51 | 52 |
| 52 | 53 | # if p is already given a task | 53 | 54 | if mask & (1 << p): | 54 | 55 | continue | 55 | 56 |
| 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. | 57 | 59 | total_ways_util += self.CountWaysUtil(mask | (1 << p), task_no + 1) | 58 | 60 |
| 59 | 61 | # save the value. | @@ -85,6 +87,7 @@ def countNoOfWays(self, task_performed): | 85 | 87 | ) | 86 | 88 | """ | 87 | 89 | 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) | 89 | 92 | total 10 | 90 | 93 | """ |
Collapse file Copy file name to clipboardExpand all lines: dynamic_programming/edit_distance.py +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | Author : Turfa Auliarachman | 3 | 3 | Date : October 12, 2016 | 4 | 4 |
| 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. | 6 | 7 |
| 7 | 8 | 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. | 9 | 11 | """ | 10 | 12 |
| 11 | 13 |
| |
Collapse file Copy file name to clipboardExpand all lines: machine_learning/decision_tree.py +19-10Lines changed: 19 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,15 +20,21 @@ def mean_squared_error(self, labels, prediction): | 20 | 20 | mean_squared_error: | 21 | 21 | @param labels: a one dimensional numpy array | 22 | 22 | @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 | 24 | 25 | >>> tester = Decision_Tree() | 25 | 26 | >>> test_labels = np.array([1,2,3,4,5,6,7,8,9,10]) | 26 | 27 | >>> 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 | 28 | 32 | >>> test_labels = np.array([1,2,3]) | 29 | 33 | >>> 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 | 32 | 38 | """ | 33 | 39 | if labels.ndim != 1: | 34 | 40 | print("Error: Input labels must be one dimensional") | @@ -46,7 +52,8 @@ def train(self, X, y): | 46 | 52 | """ | 47 | 53 |
| 48 | 54 | """ | 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 | 50 | 57 | """ | 51 | 58 | if X.ndim != 1: | 52 | 59 | print("Error: Input data set must be one dimensional") | @@ -72,7 +79,8 @@ def train(self, X, y): | 72 | 79 | """ | 73 | 80 | loop over all possible splits for the decision tree. find the best split. | 74 | 81 | 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 | 76 | 84 | """ | 77 | 85 | for i in range(len(X)): | 78 | 86 | if len(X[:i]) < self.min_leaf_size: | @@ -136,7 +144,7 @@ def helper_mean_squared_error_test(labels, prediction): | 136 | 144 | helper_mean_squared_error_test: | 137 | 145 | @param labels: a one dimensional numpy array | 138 | 146 | @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 | 140 | 148 | """ | 141 | 149 | squared_error_sum = np.float(0) | 142 | 150 | for label in labels: | @@ -147,9 +155,10 @@ def helper_mean_squared_error_test(labels, prediction): | 147 | 155 |
| 148 | 156 | def main(): | 149 | 157 | """ | 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. | 153 | 162 | """ | 154 | 163 | X = np.arange(-1.0, 1.0, 0.005) | 155 | 164 | y = np.sin(X) | |
Collapse file Copy file name to clipboardExpand all lines: machine_learning/logistic_regression.py +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/python | 2 | 2 |
| 3 | | -## Logistic Regression from scratch | | 3 | +# Logistic Regression from scratch | 4 | 4 |
| 5 | 5 | # In[62]: | 6 | 6 |
| 7 | 7 | # In[63]: | 8 | 8 |
| 9 | 9 | # importing all the required libraries | 10 | 10 |
| 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 | +""" | 13 | 17 |
| 14 | 18 | import numpy as np | 15 | 19 | import matplotlib.pyplot as plt | | 21 | 25 |
| 22 | 26 | # In[67]: | 23 | 27 |
| 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 | 25 | 30 |
| 26 | 31 |
| 27 | 32 | def sigmoid_function(z): | |
|