From b6d37bfb83c4cd5f879105b38d17c0700bc47008 Mon Sep 17 00:00:00 2001 From: eliott veyrier Date: Sat, 12 Sep 2020 21:20:37 +0200 Subject: [PATCH 1/5] Fix spelling in docstrings --- backtracking/hamiltonian_cycle.py | 6 +++--- backtracking/rat_in_maze.py | 22 +++++++++++----------- boolean_algebra/quine_mc_cluskey.py | 5 +++-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/backtracking/hamiltonian_cycle.py b/backtracking/hamiltonian_cycle.py index 3bd61fc667d9..7be1ea350d7c 100644 --- a/backtracking/hamiltonian_cycle.py +++ b/backtracking/hamiltonian_cycle.py @@ -51,7 +51,7 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) """ Pseudo-Code Base Case: - 1. Chceck if we visited all of vertices + 1. Check if we visited all of vertices 1.1 If last visited vertex has path to starting vertex return True either return False Recursive Step: @@ -59,8 +59,8 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) Check if next vertex is valid for transiting from current vertex 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 + 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], diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index ba96d6a52214..da8bd3206e5a 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -1,13 +1,13 @@ def solve_maze(maze: list) -> bool: """ - This method solves rat in maze algorithm. - In this problem we have n by n matrix and we have start point and end point - we want to go from source to distination. In this matrix 0 are block paths - 1 are open paths we can use. + This method solves the "rat in maze" problem. + In this problem we have some n by n matrix, a start point and an end point. + We want to go from the start to the end. In this matrix zeroes represent walls + and ones paths we can use. Parameters : maze(2D matrix) : maze Returns: - Return: True is maze has a solution or False if it does not. + Return: True if the maze has a solution or False if it does not. >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [1, 0, 1, 0, 1], @@ -47,13 +47,13 @@ def solve_maze(maze: list) -> bool: ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze) - Solution does not exists! + Solution does not exist! False >>> maze = [[0, 1], ... [1, 0]] >>> solve_maze(maze) - Solution does not exists! + Solution does not exist! False """ size = len(maze) @@ -69,10 +69,10 @@ def solve_maze(maze: list) -> bool: def run_maze(maze, i, j, solutions): """ - This method is recursive method which starts from i and j - and goes with 4 direction option up, down, left, right - if path found to destination it breaks and return True - otherwise False + This method is recursive : it starts from (i, j) + and goes in one of these four directions: up, down, left, right + if a path is found to destination it breaks and returns True + otherwise it returns False Parameters: maze(2D matrix) : maze i, j : coordinates of matrix diff --git a/boolean_algebra/quine_mc_cluskey.py b/boolean_algebra/quine_mc_cluskey.py index 036cfbe63e79..11cb66e50365 100644 --- a/boolean_algebra/quine_mc_cluskey.py +++ b/boolean_algebra/quine_mc_cluskey.py @@ -131,7 +131,8 @@ def prime_implicant_chart(prime_implicants, binary): >>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5']) [[1]] """ - chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))] + chart = [[0 for x in range(len(binary))] + for x in range(len(prime_implicants))] for i in range(len(prime_implicants)): count = prime_implicants[i].count("_") for j in range(len(binary)): @@ -146,7 +147,7 @@ def main(): minterms = [ int(x) for x in input( - "Enter the decimal representation of Minterms 'Spaces Seprated'\n" + "Enter the decimal representation of Minterms 'Spaces Separated'\n" ).split() ] binary = decimal_to_binary(no_of_variable, minterms) From 17e0c8a81d1447ac3f573cf62a1bfcc82929abdd Mon Sep 17 00:00:00 2001 From: eliott veyrier Date: Sat, 12 Sep 2020 21:30:01 +0200 Subject: [PATCH 2/5] Improve comments and formatting --- cellular_automata/one_dimensional.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cellular_automata/one_dimensional.py b/cellular_automata/one_dimensional.py index 7819088c8cff..1e23e04c4695 100644 --- a/cellular_automata/one_dimensional.py +++ b/cellular_automata/one_dimensional.py @@ -32,10 +32,14 @@ def new_generation(cells: List[List[int]], rule: List[int], time: int) -> List[i next_generation = [] for i in range(population): # Get the neighbors of each cell - left_neighbor = 0 if i == 0 else cells[time][i - 1] # special: leftmost cell - right_neighbor = 0 if i == population - 1 else cells[time][i + 1] # rightmost + # Handle neighbours outside bounds by using 0 as their value + left_neighbor = 0 if i == 0 else cells[time][i - 1] + right_neighbor = 0 if i == population - 1 else cells[time][i + 1] # Define a new cell and add it to the new generation - situation = 7 - int(f"{left_neighbor}{cells[time][i]}{right_neighbor}", 2) + situation = 7 - int( + f"{left_neighbor}{cells[time][i]}{right_neighbor}", + 2 + ) next_generation.append(rule[situation]) return next_generation From 707f62a04747ab7a4dd986b09672cb5c04e6f90e Mon Sep 17 00:00:00 2001 From: eliott veyrier Date: Sat, 12 Sep 2020 22:43:50 +0200 Subject: [PATCH 3/5] Update print statement to reflect doctest change --- backtracking/rat_in_maze.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index da8bd3206e5a..996e7d6b52be 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -63,7 +63,7 @@ def solve_maze(maze: list) -> bool: if solved: print("\n".join(str(row) for row in solutions)) else: - print("Solution does not exists!") + print("Solution does not exist!") return solved From f405493494ccdc5efff6543b298a0cb2b94ae6f8 Mon Sep 17 00:00:00 2001 From: eliott veyrier Date: Sun, 13 Sep 2020 22:15:31 +0200 Subject: [PATCH 4/5] improve phrasing and apply black --- backtracking/rat_in_maze.py | 6 +++--- boolean_algebra/quine_mc_cluskey.py | 3 +-- cellular_automata/one_dimensional.py | 5 +---- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 996e7d6b52be..553d0e0505c4 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -47,13 +47,13 @@ def solve_maze(maze: list) -> bool: ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze) - Solution does not exist! + No solution exists! False >>> maze = [[0, 1], ... [1, 0]] >>> solve_maze(maze) - Solution does not exist! + No solution exists! False """ size = len(maze) @@ -63,7 +63,7 @@ def solve_maze(maze: list) -> bool: if solved: print("\n".join(str(row) for row in solutions)) else: - print("Solution does not exist!") + print("No solution exists!") return solved diff --git a/boolean_algebra/quine_mc_cluskey.py b/boolean_algebra/quine_mc_cluskey.py index 11cb66e50365..a55b624483ca 100644 --- a/boolean_algebra/quine_mc_cluskey.py +++ b/boolean_algebra/quine_mc_cluskey.py @@ -131,8 +131,7 @@ def prime_implicant_chart(prime_implicants, binary): >>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5']) [[1]] """ - chart = [[0 for x in range(len(binary))] - for x in range(len(prime_implicants))] + chart = [[0 for x in range(len(binary))] for x in range(len(prime_implicants))] for i in range(len(prime_implicants)): count = prime_implicants[i].count("_") for j in range(len(binary)): diff --git a/cellular_automata/one_dimensional.py b/cellular_automata/one_dimensional.py index 1e23e04c4695..a6229dd9096f 100644 --- a/cellular_automata/one_dimensional.py +++ b/cellular_automata/one_dimensional.py @@ -36,10 +36,7 @@ def new_generation(cells: List[List[int]], rule: List[int], time: int) -> List[i left_neighbor = 0 if i == 0 else cells[time][i - 1] right_neighbor = 0 if i == population - 1 else cells[time][i + 1] # Define a new cell and add it to the new generation - situation = 7 - int( - f"{left_neighbor}{cells[time][i]}{right_neighbor}", - 2 - ) + situation = 7 - int(f"{left_neighbor}{cells[time][i]}{right_neighbor}", 2) next_generation.append(rule[situation]) return next_generation From 36b0913a69dd46c58a72eb552202abeffa9a89d7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 14 Sep 2020 07:26:56 +0200 Subject: [PATCH 5/5] Update rat_in_maze.py This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. If a path is found to destination it returns True otherwise it returns False. --- backtracking/rat_in_maze.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 553d0e0505c4..788aeac13c09 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -69,10 +69,9 @@ def solve_maze(maze: list) -> bool: def run_maze(maze, i, j, solutions): """ - This method is recursive : it starts from (i, j) - and goes in one of these four directions: up, down, left, right - if a path is found to destination it breaks and returns True - otherwise it returns False + This method is recursive starting from (i, j) and going in one of four directions: + up, down, left, right. + If a path is found to destination it returns True otherwise it returns False. Parameters: maze(2D matrix) : maze i, j : coordinates of matrix