GeezerVison is showing github.com — Improved readability (#1615) · jusing-es/Python@9eb50cc
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: arithmetic_analysis/newton_forward_interpolation.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,7 @@ def ucal(u, p): | 19 | 19 |
| 20 | 20 |
| 21 | 21 | def main(): | 22 | | - n = int(input("enter the numbers of values")) | | 22 | + n = int(input("enter the numbers of values: ")) | 23 | 23 | y = [] | 24 | 24 | for i in range(n): | 25 | 25 | y.append([]) | @@ -28,14 +28,14 @@ def main(): | 28 | 28 | y[i].append(j) | 29 | 29 | y[i][j] = 0 | 30 | 30 |
| 31 | | - print("enter the values of parameters in a list") | | 31 | + print("enter the values of parameters in a list: ") | 32 | 32 | x = list(map(int, input().split())) | 33 | 33 |
| 34 | | - print("enter the values of corresponding parameters") | | 34 | + print("enter the values of corresponding parameters: ") | 35 | 35 | for i in range(n): | 36 | 36 | y[i][0] = float(input()) | 37 | 37 |
| 38 | | - value = int(input("enter the value to interpolate")) | | 38 | + value = int(input("enter the value to interpolate: ")) | 39 | 39 | u = (value - x[0]) / (x[1] - x[0]) | 40 | 40 |
| 41 | 41 | # for calculating forward difference table | @@ -48,7 +48,7 @@ def main(): | 48 | 48 | for i in range(1, n): | 49 | 49 | summ += (ucal(u, i) * y[0][i]) / math.factorial(i) | 50 | 50 |
| 51 | | - print("the value at {} is {}".format(value, summ)) | | 51 | + print(f"the value at {value} is {summ}") | 52 | 52 |
| 53 | 53 |
| 54 | 54 | if __name__ == "__main__": | |
Collapse file Copy file name to clipboardExpand all lines: ciphers/trafid_cipher.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -117,4 +117,4 @@ def decryptMessage(message, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ.", period=5): | 117 | 117 | msg = "DEFEND THE EAST WALL OF THE CASTLE." | 118 | 118 | encrypted = encryptMessage(msg, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") | 119 | 119 | decrypted = decryptMessage(encrypted, "EPSDUCVWYM.ZLKXNBTFGORIJHAQ") | 120 | | - print("Encrypted: {}\nDecrypted: {}".format(encrypted, decrypted)) | | 120 | + print(f"Encrypted: {encrypted}\nDecrypted: {decrypted}") |
Collapse file Copy file name to clipboardExpand all lines: data_structures/linked_list/doubly_linked_list.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,4 +76,4 @@ def __init__(self, x): | 76 | 76 | self.value = x | 77 | 77 |
| 78 | 78 | def displayLink(self): | 79 | | - print("{}".format(self.value), end=" ") | | 79 | + print(f"{self.value}", end=" ") |
Collapse file Copy file name to clipboardExpand all lines: divide_and_conquer/convex_hull.py +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,7 +50,7 @@ def __init__(self, x, y): | 50 | 50 | except ValueError as e: | 51 | 51 | e.args = ( | 52 | 52 | "x and y must be both numeric types " | 53 | | - "but got {}, {} instead".format(type(x), type(y)), | | 53 | + f"but got {type(x)}, {type(y)} instead" | 54 | 54 | ) | 55 | 55 | raise | 56 | 56 |
| @@ -88,7 +88,7 @@ def __le__(self, other): | 88 | 88 | return False | 89 | 89 |
| 90 | 90 | def __repr__(self): | 91 | | - return "({}, {})".format(self.x, self.y) | | 91 | + return f"({self.x}, {self.y})" | 92 | 92 |
| 93 | 93 | def __hash__(self): | 94 | 94 | return hash(self.x) | @@ -136,8 +136,8 @@ def _construct_points(list_of_tuples): | 136 | 136 | points.append(Point(p[0], p[1])) | 137 | 137 | except (IndexError, TypeError): | 138 | 138 | print( | 139 | | - "Ignoring deformed point {}. All points" | 140 | | - " must have at least 2 coordinates.".format(p) | | 139 | + f"Ignoring deformed point {p}. All points" | | 140 | + " must have at least 2 coordinates." | 141 | 141 | ) | 142 | 142 | return points | 143 | 143 |
| @@ -184,7 +184,7 @@ def _validate_input(points): | 184 | 184 | """ | 185 | 185 |
| 186 | 186 | if not points: | 187 | | - raise ValueError("Expecting a list of points but got {}".format(points)) | | 187 | + raise ValueError(f"Expecting a list of points but got {points}") | 188 | 188 |
| 189 | 189 | if isinstance(points, set): | 190 | 190 | points = list(points) | @@ -196,12 +196,12 @@ def _validate_input(points): | 196 | 196 | else: | 197 | 197 | raise ValueError( | 198 | 198 | "Expecting an iterable of type Point, list or tuple. " | 199 | | - "Found objects of type {} instead".format(type(points[0])) | | 199 | + f"Found objects of type {type(points[0])} instead" | 200 | 200 | ) | 201 | 201 | elif not hasattr(points, "__iter__"): | 202 | 202 | raise ValueError( | 203 | 203 | "Expecting an iterable object " | 204 | | - "but got an non-iterable type {}".format(points) | | 204 | + f"but got an non-iterable type {points}" | 205 | 205 | ) | 206 | 206 | except TypeError as e: | 207 | 207 | print("Expecting an iterable of type Point, list or tuple.") | |
Collapse file Copy file name to clipboardExpand all lines: dynamic_programming/knapsack.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -81,13 +81,13 @@ def knapsack_with_example_solution(W: int, wt: list, val: list): | 81 | 81 | raise ValueError( | 82 | 82 | "The number of weights must be the " | 83 | 83 | "same as the number of values.\nBut " | 84 | | - "got {} weights and {} values".format(num_items, len(val)) | | 84 | + f"got {num_items} weights and {len(val)} values" | 85 | 85 | ) | 86 | 86 | for i in range(num_items): | 87 | 87 | if not isinstance(wt[i], int): | 88 | 88 | raise TypeError( | 89 | 89 | "All weights must be integers but " | 90 | | - "got weight of type {} at index {}".format(type(wt[i]), i) | | 90 | + f"got weight of type {type(wt[i])} at index {i}" | 91 | 91 | ) | 92 | 92 |
| 93 | 93 | optimal_val, dp_table = knapsack(W, wt, val, num_items) | |
Collapse file Copy file name to clipboardExpand all lines: graphs/dijkstra_algorithm.py +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -106,7 +106,7 @@ def show_graph(self): | 106 | 106 | print( | 107 | 107 | u, | 108 | 108 | "->", | 109 | | - " -> ".join(str("{}({})".format(v, w)) for v, w in self.adjList[u]), | | 109 | + " -> ".join(str(f"{v}({w})") for v, w in self.adjList[u]), | 110 | 110 | ) | 111 | 111 |
| 112 | 112 | def dijkstra(self, src): | @@ -139,9 +139,9 @@ def dijkstra(self, src): | 139 | 139 | self.show_distances(src) | 140 | 140 |
| 141 | 141 | def show_distances(self, src): | 142 | | - print("Distance from node: {}".format(src)) | | 142 | + print(f"Distance from node: {src}") | 143 | 143 | for u in range(self.num_nodes): | 144 | | - print("Node {} has distance: {}".format(u, self.dist[u])) | | 144 | + print(f"Node {u} has distance: {self.dist[u]}") | 145 | 145 |
| 146 | 146 | def show_path(self, src, dest): | 147 | 147 | # To show the shortest path from src to dest | @@ -161,9 +161,9 @@ def show_path(self, src, dest): | 161 | 161 | path.append(src) | 162 | 162 | path.reverse() | 163 | 163 |
| 164 | | - print("----Path to reach {} from {}----".format(dest, src)) | | 164 | + print(f"----Path to reach {dest} from {src}----") | 165 | 165 | for u in path: | 166 | | - print("{}".format(u), end=" ") | | 166 | + print(f"{u}", end=" ") | 167 | 167 | if u != dest: | 168 | 168 | print("-> ", end="") | 169 | 169 |
| |
Collapse file Copy file name to clipboardExpand all lines: graphs/edmonds_karp_multiple_source_and_sink.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -190,4 +190,4 @@ def relabel(self, vertexIndex): | 190 | 190 | # and calculate | 191 | 191 | maximumFlow = flowNetwork.findMaximumFlow() | 192 | 192 |
| 193 | | - print("maximum flow is {}".format(maximumFlow)) | | 193 | + print(f"maximum flow is {maximumFlow}") |
Collapse file Copy file name to clipboardExpand all lines: graphs/page_rank.py +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,9 +27,7 @@ def add_outbound(self, node): | 27 | 27 | self.outbound.append(node) | 28 | 28 |
| 29 | 29 | def __repr__(self): | 30 | | - return "Node {}: Inbound: {} ; Outbound: {}".format( | 31 | | - self.name, self.inbound, self.outbound | 32 | | - ) | | 30 | + return f"Node {self.name}: Inbound: {self.inbound} ; Outbound: {self.outbound}" | 33 | 31 |
| 34 | 32 |
| 35 | 33 | def page_rank(nodes, limit=3, d=0.85): | @@ -42,7 +40,7 @@ def page_rank(nodes, limit=3, d=0.85): | 42 | 40 | outbounds[node.name] = len(node.outbound) | 43 | 41 |
| 44 | 42 | for i in range(limit): | 45 | | - print("======= Iteration {} =======".format(i + 1)) | | 43 | + print(f"======= Iteration {i + 1} =======") | 46 | 44 | for j, node in enumerate(nodes): | 47 | 45 | ranks[node.name] = (1 - d) + d * sum( | 48 | 46 | [ranks[ib] / outbounds[ib] for ib in node.inbound] | |
Collapse file Copy file name to clipboardExpand all lines: machine_learning/knn_sklearn.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | iris.keys() | 8 | 8 |
| 9 | 9 |
| 10 | | -print("Target names: \n {} ".format(iris.target_names)) | 11 | | -print("\n Features: \n {}".format(iris.feature_names)) | | 10 | +print(f"Target names: \n {iris.target_names} ") | | 11 | +print(f"\n Features: \n {iris.feature_names}") | 12 | 12 |
| 13 | 13 | # Train set e Test set | 14 | 14 | X_train, X_test, y_train, y_test = train_test_split( | |
Collapse file Copy file name to clipboardExpand all lines: machine_learning/linear_discriminant_analysis.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -174,8 +174,8 @@ def accuracy(actual_y: list, predicted_y: list) -> float: | 174 | 174 | def main(): | 175 | 175 | """ This function starts execution phase """ | 176 | 176 | while True: | 177 | | - print(" Linear Discriminant Analysis ".center(100, "*")) | 178 | | - print("*" * 100, "\n") | | 177 | + print(" Linear Discriminant Analysis ".center(50, "*")) | | 178 | + print("*" * 50, "\n") | 179 | 179 | print("First of all we should specify the number of classes that") | 180 | 180 | print("we want to generate as training dataset") | 181 | 181 | # Trying to get number of classes | @@ -239,7 +239,7 @@ def main(): | 239 | 239 | else: | 240 | 240 | print( | 241 | 241 | f"Your entered value is {user_count}, Number of " | 242 | | - f"instances should be positive!" | | 242 | + "instances should be positive!" | 243 | 243 | ) | 244 | 244 | continue | 245 | 245 | except ValueError: | @@ -302,7 +302,7 @@ def main(): | 302 | 302 | # for loop iterates over number of elements in 'probabilities' list and print | 303 | 303 | # out them in separated line | 304 | 304 | for i, probability in enumerate(probabilities, 1): | 305 | | - print("Probability of class_{} is: {}".format(i, probability)) | | 305 | + print(f"Probability of class_{i} is: {probability}") | 306 | 306 | print("-" * 100) | 307 | 307 |
| 308 | 308 | # Calculating the values of variance for each class | |
|