GeezerVison is showing github.com — Optimized recursive_bubble_sort (#2410) · jusing-es/Python@4d0a8f2
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: arithmetic_analysis/newton_method.py +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 |
| 9 | 9 | # function is the f(x) and derivative is the f'(x) | 10 | | -def newton(function: RealFunc, derivative: RealFunc, starting_int: int,) -> float: | | 10 | +def newton( | | 11 | + function: RealFunc, | | 12 | + derivative: RealFunc, | | 13 | + starting_int: int, | | 14 | +) -> float: | 11 | 15 | """ | 12 | 16 | >>> newton(lambda x: x ** 3 - 2 * x - 5, lambda x: 3 * x ** 2 - 2, 3) | 13 | 17 | 2.0945514815423474 | |
Collapse file Copy file name to clipboardExpand all lines: arithmetic_analysis/newton_raphson.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 |
| 11 | 11 | def newton_raphson(func: str, a: int, precision: int = 10 ** -10) -> float: | 12 | | - """ Finds root from the point 'a' onwards by Newton-Raphson method | | 12 | + """Finds root from the point 'a' onwards by Newton-Raphson method | 13 | 13 | >>> newton_raphson("sin(x)", 2) | 14 | 14 | 3.1415926536808043 | 15 | 15 | >>> newton_raphson("x**2 - 5*x +2", 0.4) | |
Collapse file Copy file name to clipboardExpand all lines: backtracking/all_permutations.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,10 +13,10 @@ def generate_all_permutations(sequence): | 13 | 13 |
| 14 | 14 | def create_state_space_tree(sequence, current_sequence, index, index_used): | 15 | 15 | """ | 16 | | - Creates a state space tree to iterate through each branch using DFS. | 17 | | - We know that each state has exactly len(sequence) - index children. | 18 | | - It terminates when it reaches the end of the given sequence. | 19 | | - """ | | 16 | + Creates a state space tree to iterate through each branch using DFS. | | 17 | + We know that each state has exactly len(sequence) - index children. | | 18 | + It terminates when it reaches the end of the given sequence. | | 19 | + """ | 20 | 20 |
| 21 | 21 | if index == len(sequence): | 22 | 22 | print(current_sequence) | |
Collapse file Copy file name to clipboardExpand all lines: backtracking/all_subsequences.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,10 +13,10 @@ def generate_all_subsequences(sequence): | 13 | 13 |
| 14 | 14 | def create_state_space_tree(sequence, current_subsequence, index): | 15 | 15 | """ | 16 | | - Creates a state space tree to iterate through each branch using DFS. | 17 | | - We know that each state has exactly two children. | 18 | | - It terminates when it reaches the end of the given sequence. | 19 | | - """ | | 16 | + Creates a state space tree to iterate through each branch using DFS. | | 17 | + We know that each state has exactly two children. | | 18 | + It terminates when it reaches the end of the given sequence. | | 19 | + """ | 20 | 20 |
| 21 | 21 | if index == len(sequence): | 22 | 22 | print(current_subsequence) | |
Collapse file Copy file name to clipboardExpand all lines: backtracking/sudoku.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -105,7 +105,7 @@ def sudoku(grid): | 105 | 105 | [7, 4, 5, 2, 8, 6, 3, 1, 9]] | 106 | 106 | >>> sudoku(no_solution) | 107 | 107 | False | 108 | | - """ | | 108 | + """ | 109 | 109 |
| 110 | 110 | if is_completed(grid): | 111 | 111 | return grid | |
Collapse file Copy file name to clipboardExpand all lines: backtracking/sum_of_subsets.py +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,13 +19,13 @@ def generate_sum_of_subsets_soln(nums, max_sum): | 19 | 19 |
| 20 | 20 | def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum): | 21 | 21 | """ | 22 | | - Creates a state space tree to iterate through each branch using DFS. | 23 | | - It terminates the branching of a node when any of the two conditions | 24 | | - given below satisfy. | 25 | | - This algorithm follows depth-fist-search and backtracks when the node is not | 26 | | - branchable. | | 22 | + Creates a state space tree to iterate through each branch using DFS. | | 23 | + It terminates the branching of a node when any of the two conditions | | 24 | + given below satisfy. | | 25 | + This algorithm follows depth-fist-search and backtracks when the node is not | | 26 | + branchable. | 27 | 27 |
| 28 | | - """ | | 28 | + """ | 29 | 29 | if sum(path) > max_sum or (remaining_nums_sum + sum(path)) < max_sum: | 30 | 30 | return | 31 | 31 | if sum(path) == max_sum: | |
Collapse file Copy file name to clipboardExpand all lines: blockchain/modular_division.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,13 +74,13 @@ def modular_division2(a, b, n): | 74 | 74 |
| 75 | 75 | def extended_gcd(a, b): | 76 | 76 | """ | 77 | | - >>> extended_gcd(10, 6) | 78 | | - (2, -1, 2) | | 77 | + >>> extended_gcd(10, 6) | | 78 | + (2, -1, 2) | 79 | 79 |
| 80 | | - >>> extended_gcd(7, 5) | 81 | | - (1, -2, 3) | | 80 | + >>> extended_gcd(7, 5) | | 81 | + (1, -2, 3) | 82 | 82 |
| 83 | | - ** extended_gcd function is used when d = gcd(a,b) is required in output | | 83 | + ** extended_gcd function is used when d = gcd(a,b) is required in output | 84 | 84 |
| 85 | 85 | """ | 86 | 86 | assert a >= 0 and b >= 0 | |
Collapse file Copy file name to clipboardExpand all lines: ciphers/enigma_machine2.py +67-35Lines changed: 67 additions & 35 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 |
| 18 | 18 | # used alphabet -------------------------- | 19 | 19 | # from string.ascii_uppercase | 20 | | -abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | | 20 | +abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 21 | 21 |
| 22 | 22 | # -------------------------- default selection -------------------------- | 23 | 23 | # rotors -------------------------- | 24 | | -rotor1 = 'EGZWVONAHDCLFQMSIPJBYUKXTR' | 25 | | -rotor2 = 'FOBHMDKEXQNRAULPGSJVTYICZW' | 26 | | -rotor3 = 'ZJXESIUQLHAVRMDOYGTNFWPBKC' | | 24 | +rotor1 = "EGZWVONAHDCLFQMSIPJBYUKXTR" | | 25 | +rotor2 = "FOBHMDKEXQNRAULPGSJVTYICZW" | | 26 | +rotor3 = "ZJXESIUQLHAVRMDOYGTNFWPBKC" | 27 | 27 | # reflector -------------------------- | 28 | | -reflector = {'A': 'N', 'N': 'A', 'B': 'O', 'O': 'B', 'C': 'P', 'P': 'C', 'D': 'Q', | 29 | | - 'Q': 'D', 'E': 'R', 'R': 'E', 'F': 'S', 'S': 'F', 'G': 'T', 'T': 'G', | 30 | | - 'H': 'U', 'U': 'H', 'I': 'V', 'V': 'I', 'J': 'W', 'W': 'J', 'K': 'X', | 31 | | - 'X': 'K', 'L': 'Y', 'Y': 'L', 'M': 'Z', 'Z': 'M'} | | 28 | +reflector = { | | 29 | + "A": "N", | | 30 | + "N": "A", | | 31 | + "B": "O", | | 32 | + "O": "B", | | 33 | + "C": "P", | | 34 | + "P": "C", | | 35 | + "D": "Q", | | 36 | + "Q": "D", | | 37 | + "E": "R", | | 38 | + "R": "E", | | 39 | + "F": "S", | | 40 | + "S": "F", | | 41 | + "G": "T", | | 42 | + "T": "G", | | 43 | + "H": "U", | | 44 | + "U": "H", | | 45 | + "I": "V", | | 46 | + "V": "I", | | 47 | + "J": "W", | | 48 | + "W": "J", | | 49 | + "K": "X", | | 50 | + "X": "K", | | 51 | + "L": "Y", | | 52 | + "Y": "L", | | 53 | + "M": "Z", | | 54 | + "Z": "M", | | 55 | +} | 32 | 56 |
| 33 | 57 | # -------------------------- extra rotors -------------------------- | 34 | | -rotor4 = 'RMDJXFUWGISLHVTCQNKYPBEZOA' | 35 | | -rotor5 = 'SGLCPQWZHKXAREONTFBVIYJUDM' | 36 | | -rotor6 = 'HVSICLTYKQUBXDWAJZOMFGPREN' | 37 | | -rotor7 = 'RZWQHFMVDBKICJLNTUXAGYPSOE' | 38 | | -rotor8 = 'LFKIJODBEGAMQPXVUHYSTCZRWN' | 39 | | -rotor9 = 'KOAEGVDHXPQZMLFTYWJNBRCIUS' | | 58 | +rotor4 = "RMDJXFUWGISLHVTCQNKYPBEZOA" | | 59 | +rotor5 = "SGLCPQWZHKXAREONTFBVIYJUDM" | | 60 | +rotor6 = "HVSICLTYKQUBXDWAJZOMFGPREN" | | 61 | +rotor7 = "RZWQHFMVDBKICJLNTUXAGYPSOE" | | 62 | +rotor8 = "LFKIJODBEGAMQPXVUHYSTCZRWN" | | 63 | +rotor9 = "KOAEGVDHXPQZMLFTYWJNBRCIUS" | 40 | 64 |
| 41 | 65 |
| 42 | 66 | def _validator(rotpos: tuple, rotsel: tuple, pb: str) -> tuple: | @@ -57,19 +81,22 @@ def _validator(rotpos: tuple, rotsel: tuple, pb: str) -> tuple: | 57 | 81 |
| 58 | 82 | unique_rotsel = len(set(rotsel)) | 59 | 83 | if unique_rotsel < 3: | 60 | | - raise Exception(f'Please use 3 unique rotors (not {unique_rotsel})') | | 84 | + raise Exception(f"Please use 3 unique rotors (not {unique_rotsel})") | 61 | 85 |
| 62 | 86 | # Checks if rotor positions are valid | 63 | 87 | rotorpos1, rotorpos2, rotorpos3 = rotpos | 64 | 88 | if not 0 < rotorpos1 <= len(abc): | 65 | | - raise ValueError(f'First rotor position is not within range of 1..26 (' | 66 | | - f'{rotorpos1}') | | 89 | + raise ValueError( | | 90 | + f"First rotor position is not within range of 1..26 (" f"{rotorpos1}" | | 91 | + ) | 67 | 92 | if not 0 < rotorpos2 <= len(abc): | 68 | | - raise ValueError(f'Second rotor position is not within range of 1..26 (' | 69 | | - f'{rotorpos2})') | | 93 | + raise ValueError( | | 94 | + f"Second rotor position is not within range of 1..26 (" f"{rotorpos2})" | | 95 | + ) | 70 | 96 | if not 0 < rotorpos3 <= len(abc): | 71 | | - raise ValueError(f'Third rotor position is not within range of 1..26 (' | 72 | | - f'{rotorpos3})') | | 97 | + raise ValueError( | | 98 | + f"Third rotor position is not within range of 1..26 (" f"{rotorpos3})" | | 99 | + ) | 73 | 100 |
| 74 | 101 | # Validates string and returns dict | 75 | 102 | pb = _plugboard(pb) | @@ -97,21 +124,21 @@ def _plugboard(pbstring: str) -> dict: | 97 | 124 | # a) is type string | 98 | 125 | # b) has even length (so pairs can be made) | 99 | 126 | if not isinstance(pbstring, str): | 100 | | - raise TypeError(f'Plugboard setting isn\'t type string ({type(pbstring)})') | | 127 | + raise TypeError(f"Plugboard setting isn't type string ({type(pbstring)})") | 101 | 128 | elif len(pbstring) % 2 != 0: | 102 | | - raise Exception(f'Odd number of symbols ({len(pbstring)})') | 103 | | - elif pbstring == '': | | 129 | + raise Exception(f"Odd number of symbols ({len(pbstring)})") | | 130 | + elif pbstring == "": | 104 | 131 | return {} | 105 | 132 |
| 106 | | - pbstring.replace(' ', '') | | 133 | + pbstring.replace(" ", "") | 107 | 134 |
| 108 | 135 | # Checks if all characters are unique | 109 | 136 | tmppbl = set() | 110 | 137 | for i in pbstring: | 111 | 138 | if i not in abc: | 112 | | - raise Exception(f'\'{i}\' not in list of symbols') | | 139 | + raise Exception(f"'{i}' not in list of symbols") | 113 | 140 | elif i in tmppbl: | 114 | | - raise Exception(f'Duplicate symbol ({i})') | | 141 | + raise Exception(f"Duplicate symbol ({i})") | 115 | 142 | else: | 116 | 143 | tmppbl.add(i) | 117 | 144 | del tmppbl | @@ -125,8 +152,12 @@ def _plugboard(pbstring: str) -> dict: | 125 | 152 | return pb | 126 | 153 |
| 127 | 154 |
| 128 | | -def enigma(text: str, rotor_position: tuple, | 129 | | - rotor_selection: tuple = (rotor1, rotor2, rotor3), plugb: str = '') -> str: | | 155 | +def enigma( | | 156 | + text: str, | | 157 | + rotor_position: tuple, | | 158 | + rotor_selection: tuple = (rotor1, rotor2, rotor3), | | 159 | + plugb: str = "", | | 160 | +) -> str: | 130 | 161 | """ | 131 | 162 | The only difference with real-world enigma is that I allowed string input. | 132 | 163 | All characters are converted to uppercase. (non-letter symbol are ignored) | @@ -179,7 +210,8 @@ def enigma(text: str, rotor_position: tuple, | 179 | 210 |
| 180 | 211 | text = text.upper() | 181 | 212 | rotor_position, rotor_selection, plugboard = _validator( | 182 | | - rotor_position, rotor_selection, plugb.upper()) | | 213 | + rotor_position, rotor_selection, plugb.upper() | | 214 | + ) | 183 | 215 |
| 184 | 216 | rotorpos1, rotorpos2, rotorpos3 = rotor_position | 185 | 217 | rotor1, rotor2, rotor3 = rotor_selection | @@ -245,12 +277,12 @@ def enigma(text: str, rotor_position: tuple, | 245 | 277 | return "".join(result) | 246 | 278 |
| 247 | 279 |
| 248 | | -if __name__ == '__main__': | 249 | | - message = 'This is my Python script that emulates the Enigma machine from WWII.' | | 280 | +if __name__ == "__main__": | | 281 | + message = "This is my Python script that emulates the Enigma machine from WWII." | 250 | 282 | rotor_pos = (1, 1, 1) | 251 | | - pb = 'pictures' | | 283 | + pb = "pictures" | 252 | 284 | rotor_sel = (rotor2, rotor4, rotor8) | 253 | 285 | en = enigma(message, rotor_pos, rotor_sel, pb) | 254 | 286 |
| 255 | | - print('Encrypted message:', en) | 256 | | - print('Decrypted message:', enigma(en, rotor_pos, rotor_sel, pb)) | | 287 | + print("Encrypted message:", en) | | 288 | + print("Decrypted message:", enigma(en, rotor_pos, rotor_sel, pb)) |
Collapse file Copy file name to clipboardExpand all lines: ciphers/xor_cipher.py +35-35Lines changed: 35 additions & 35 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | class XORCipher: | 22 | 22 | def __init__(self, key=0): | 23 | 23 | """ | 24 | | - simple constructor that receives a key or uses | 25 | | - default key = 0 | 26 | | - """ | | 24 | + simple constructor that receives a key or uses | | 25 | + default key = 0 | | 26 | + """ | 27 | 27 |
| 28 | 28 | # private field | 29 | 29 | self.__key = key | 30 | 30 |
| 31 | 31 | def encrypt(self, content, key): | 32 | 32 | """ | 33 | | - input: 'content' of type string and 'key' of type int | 34 | | - output: encrypted string 'content' as a list of chars | 35 | | - if key not passed the method uses the key by the constructor. | 36 | | - otherwise key = 1 | 37 | | - """ | | 33 | + input: 'content' of type string and 'key' of type int | | 34 | + output: encrypted string 'content' as a list of chars | | 35 | + if key not passed the method uses the key by the constructor. | | 36 | + otherwise key = 1 | | 37 | + """ | 38 | 38 |
| 39 | 39 | # precondition | 40 | 40 | assert isinstance(key, int) and isinstance(content, str) | @@ -55,11 +55,11 @@ def encrypt(self, content, key): | 55 | 55 |
| 56 | 56 | def decrypt(self, content, key): | 57 | 57 | """ | 58 | | - input: 'content' of type list and 'key' of type int | 59 | | - output: decrypted string 'content' as a list of chars | 60 | | - if key not passed the method uses the key by the constructor. | 61 | | - otherwise key = 1 | 62 | | - """ | | 58 | + input: 'content' of type list and 'key' of type int | | 59 | + output: decrypted string 'content' as a list of chars | | 60 | + if key not passed the method uses the key by the constructor. | | 61 | + otherwise key = 1 | | 62 | + """ | 63 | 63 |
| 64 | 64 | # precondition | 65 | 65 | assert isinstance(key, int) and isinstance(content, list) | @@ -80,11 +80,11 @@ def decrypt(self, content, key): | 80 | 80 |
| 81 | 81 | def encrypt_string(self, content, key=0): | 82 | 82 | """ | 83 | | - input: 'content' of type string and 'key' of type int | 84 | | - output: encrypted string 'content' | 85 | | - if key not passed the method uses the key by the constructor. | 86 | | - otherwise key = 1 | 87 | | - """ | | 83 | + input: 'content' of type string and 'key' of type int | | 84 | + output: encrypted string 'content' | | 85 | + if key not passed the method uses the key by the constructor. | | 86 | + otherwise key = 1 | | 87 | + """ | 88 | 88 |
| 89 | 89 | # precondition | 90 | 90 | assert isinstance(key, int) and isinstance(content, str) | @@ -105,11 +105,11 @@ def encrypt_string(self, content, key=0): | 105 | 105 |
| 106 | 106 | def decrypt_string(self, content, key=0): | 107 | 107 | """ | 108 | | - input: 'content' of type string and 'key' of type int | 109 | | - output: decrypted string 'content' | 110 | | - if key not passed the method uses the key by the constructor. | 111 | | - otherwise key = 1 | 112 | | - """ | | 108 | + input: 'content' of type string and 'key' of type int | | 109 | + output: decrypted string 'content' | | 110 | + if key not passed the method uses the key by the constructor. | | 111 | + otherwise key = 1 | | 112 | + """ | 113 | 113 |
| 114 | 114 | # precondition | 115 | 115 | assert isinstance(key, int) and isinstance(content, str) | @@ -130,12 +130,12 @@ def decrypt_string(self, content, key=0): | 130 | 130 |
| 131 | 131 | def encrypt_file(self, file, key=0): | 132 | 132 | """ | 133 | | - input: filename (str) and a key (int) | 134 | | - output: returns true if encrypt process was | 135 | | - successful otherwise false | 136 | | - if key not passed the method uses the key by the constructor. | 137 | | - otherwise key = 1 | 138 | | - """ | | 133 | + input: filename (str) and a key (int) | | 134 | + output: returns true if encrypt process was | | 135 | + successful otherwise false | | 136 | + if key not passed the method uses the key by the constructor. | | 137 | + otherwise key = 1 | | 138 | + """ | 139 | 139 |
| 140 | 140 | # precondition | 141 | 141 | assert isinstance(file, str) and isinstance(key, int) | @@ -155,12 +155,12 @@ def encrypt_file(self, file, key=0): | 155 | 155 |
| 156 | 156 | def decrypt_file(self, file, key): | 157 | 157 | """ | 158 | | - input: filename (str) and a key (int) | 159 | | - output: returns true if decrypt process was | 160 | | - successful otherwise false | 161 | | - if key not passed the method uses the key by the constructor. | 162 | | - otherwise key = 1 | 163 | | - """ | | 158 | + input: filename (str) and a key (int) | | 159 | + output: returns true if decrypt process was | | 160 | + successful otherwise false | | 161 | + if key not passed the method uses the key by the constructor. | | 162 | + otherwise key = 1 | | 163 | + """ | 164 | 164 |
| 165 | 165 | # precondition | 166 | 166 | assert isinstance(file, str) and isinstance(key, int) | |
|