From eacebd7fed6e473d12710e8fc559796e92eb5ca3 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 19:49:23 +0530 Subject: [PATCH 01/16] Create Calc.py --- other/Calc.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 other/Calc.py diff --git a/other/Calc.py b/other/Calc.py new file mode 100644 index 000000000000..a887164ed834 --- /dev/null +++ b/other/Calc.py @@ -0,0 +1,85 @@ +from flask import Flask, request, render_template_string + +app = Flask(__name__) + +def calculate(num1: float, num2: float, operation: str) -> float: + """ + Perform basic arithmetic operations: add, subtract, multiply, divide. + + >>> calculate(2, 3, 'add') + 5 + >>> calculate(5, 3, 'subtract') + 2 + >>> calculate(4, 2, 'multiply') + 8 + >>> calculate(10, 2, 'divide') + 5.0 + >>> calculate(5, 0, 'divide') + Traceback (most recent call last): + ... + ValueError: Division by zero is not allowed. + """ + if operation == "add": + return num1 + num2 + elif operation == "subtract": + return num1 - num2 + elif operation == "multiply": + return num1 * num2 + elif operation == "divide": + if num2 == 0: + raise ValueError("Division by zero is not allowed.") + return num1 / num2 + else: + raise ValueError(f"Unknown operation: {operation}") + +# HTML template for the web interface +template = """ + + + + Flask Calculator + + + +

Flask Calculator

+
+ + +
+ +
+ +
+ {% if result is not none %} +
Result: {{ result }}
+ {% endif %} + + +""" + +@app.route("/", methods=["GET", "POST"]) +def home(): + result = None + if request.method == "POST": + try: + num1 = float(request.form["num1"]) + num2 = float(request.form["num2"]) + op = request.form["operation"] + result = calculate(num1, num2, op) + except Exception as e: + result = str(e) + return render_template_string(template, result=result) + +if __name__ == "__main__": + app.run(debug=True) From 21ef5e118c8ef27375a9d37afc62d698e1a373c7 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 19:53:16 +0530 Subject: [PATCH 02/16] Rename Calc.py to calc.py --- other/{Calc.py => calc.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename other/{Calc.py => calc.py} (100%) diff --git a/other/Calc.py b/other/calc.py similarity index 100% rename from other/Calc.py rename to other/calc.py From 2df824d61bbfc926429eb44cda141e33a5f8d62f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 14:27:31 +0000 Subject: [PATCH 03/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/calc.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/other/calc.py b/other/calc.py index a887164ed834..dcf466cb48f7 100644 --- a/other/calc.py +++ b/other/calc.py @@ -2,6 +2,7 @@ app = Flask(__name__) + def calculate(num1: float, num2: float, operation: str) -> float: """ Perform basic arithmetic operations: add, subtract, multiply, divide. @@ -32,6 +33,7 @@ def calculate(num1: float, num2: float, operation: str) -> float: else: raise ValueError(f"Unknown operation: {operation}") + # HTML template for the web interface template = """ @@ -68,6 +70,7 @@ def calculate(num1: float, num2: float, operation: str) -> float: """ + @app.route("/", methods=["GET", "POST"]) def home(): result = None @@ -81,5 +84,6 @@ def home(): result = str(e) return render_template_string(template, result=result) + if __name__ == "__main__": app.run(debug=True) From 913c90ab13dacde23cfe9caf3fe4eccb40014e6c Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:00:41 +0530 Subject: [PATCH 04/16] Create rat_in_a_maze.py --- dynamic_programming/rat_in_a_maze.py | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 dynamic_programming/rat_in_a_maze.py diff --git a/dynamic_programming/rat_in_a_maze.py b/dynamic_programming/rat_in_a_maze.py new file mode 100644 index 000000000000..0a1493019bef --- /dev/null +++ b/dynamic_programming/rat_in_a_maze.py @@ -0,0 +1,29 @@ +def is_safe(maze, x, y, n): + return 0 <= x < n and 0 <= y < n and maze[x][y] == 1 + +def solve_maze_util(maze, x, y, n, solution): + if x == n - 1 and y == n - 1: + solution[x][y] = 1 + return True + if is_safe(maze, x, y, n): + solution[x][y] = 1 + if solve_maze_util(maze, x + 1, y, n, solution): + return True + if solve_maze_util(maze, x, y + 1, n, solution): + return True + solution[x][y] = 0 + return False + return False + +def solve_maze(maze, n): + solution = [[0] * n for _ in range(n)] + if not solve_maze_util(maze, 0, 0, n, solution): + print("No solution exists") + return + for row in solution: + print(*row) + +if __name__ == "__main__": + n = int(input()) + maze = [list(map(int, input().split())) for _ in range(n)] + solve_maze(maze, n) From 16b6d7855414175e9b32e5e77cb8ac334eb817eb Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:06:16 +0530 Subject: [PATCH 05/16] Delete dynamic_programming/rat_in_a_maze.py --- dynamic_programming/rat_in_a_maze.py | 29 ---------------------------- 1 file changed, 29 deletions(-) delete mode 100644 dynamic_programming/rat_in_a_maze.py diff --git a/dynamic_programming/rat_in_a_maze.py b/dynamic_programming/rat_in_a_maze.py deleted file mode 100644 index 0a1493019bef..000000000000 --- a/dynamic_programming/rat_in_a_maze.py +++ /dev/null @@ -1,29 +0,0 @@ -def is_safe(maze, x, y, n): - return 0 <= x < n and 0 <= y < n and maze[x][y] == 1 - -def solve_maze_util(maze, x, y, n, solution): - if x == n - 1 and y == n - 1: - solution[x][y] = 1 - return True - if is_safe(maze, x, y, n): - solution[x][y] = 1 - if solve_maze_util(maze, x + 1, y, n, solution): - return True - if solve_maze_util(maze, x, y + 1, n, solution): - return True - solution[x][y] = 0 - return False - return False - -def solve_maze(maze, n): - solution = [[0] * n for _ in range(n)] - if not solve_maze_util(maze, 0, 0, n, solution): - print("No solution exists") - return - for row in solution: - print(*row) - -if __name__ == "__main__": - n = int(input()) - maze = [list(map(int, input().split())) for _ in range(n)] - solve_maze(maze, n) From d0909dcd5bf123d164bae937430f9d2c80cea725 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:08:52 +0530 Subject: [PATCH 06/16] Create m-coloring-problem.py --- backtracking/m-coloring-problem.py | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 backtracking/m-coloring-problem.py diff --git a/backtracking/m-coloring-problem.py b/backtracking/m-coloring-problem.py new file mode 100644 index 000000000000..7055d8d253ff --- /dev/null +++ b/backtracking/m-coloring-problem.py @@ -0,0 +1,36 @@ +def isSafe(node, color, graph, n, col): + for k in range(n): + if graph[node][k] == 1 and col[k] == color: + return False + return True + +def solve(node, col, m, n, graph): + if node == n: + return True + for c in range(1, m + 1): + if isSafe(node, c, graph, n, col): + col[node] = c + if solve(node + 1, col, m, n, graph): + return True + col[node] = 0 + return False + +def graphColoring(graph, m, n): + col = [0] * n + if solve(0, col, m, n, graph): + return True + return False + +if __name__ == "__main__": + V = int(input()) + E = int(input()) + graph = [[0 for _ in range(V)] for _ in range(V)] + for _ in range(E): + u, v = map(int, input().split()) + graph[u][v] = 1 + graph[v][u] = 1 + m = int(input()) + if graphColoring(graph, m, V): + print("True") + else: + print("False") From 152c0c098f56fa08f60bc11b6de3c201aaa954e5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 17:42:37 +0000 Subject: [PATCH 07/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/m-coloring-problem.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backtracking/m-coloring-problem.py b/backtracking/m-coloring-problem.py index 7055d8d253ff..2c6350f20975 100644 --- a/backtracking/m-coloring-problem.py +++ b/backtracking/m-coloring-problem.py @@ -4,6 +4,7 @@ def isSafe(node, color, graph, n, col): return False return True + def solve(node, col, m, n, graph): if node == n: return True @@ -15,12 +16,14 @@ def solve(node, col, m, n, graph): col[node] = 0 return False + def graphColoring(graph, m, n): col = [0] * n if solve(0, col, m, n, graph): return True return False + if __name__ == "__main__": V = int(input()) E = int(input()) From 7647b47f030ac06665d12bb3ece0177be38302e2 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:16:38 +0530 Subject: [PATCH 08/16] Rename m-coloring-problem.py to m_coloring_problem.py --- backtracking/{m-coloring-problem.py => m_coloring_problem.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename backtracking/{m-coloring-problem.py => m_coloring_problem.py} (100%) diff --git a/backtracking/m-coloring-problem.py b/backtracking/m_coloring_problem.py similarity index 100% rename from backtracking/m-coloring-problem.py rename to backtracking/m_coloring_problem.py From 80cfe21823385ddf5772addbad2f36cbc6d0a335 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:22:29 +0530 Subject: [PATCH 09/16] Delete other/calc.py --- other/calc.py | 89 --------------------------------------------------- 1 file changed, 89 deletions(-) delete mode 100644 other/calc.py diff --git a/other/calc.py b/other/calc.py deleted file mode 100644 index dcf466cb48f7..000000000000 --- a/other/calc.py +++ /dev/null @@ -1,89 +0,0 @@ -from flask import Flask, request, render_template_string - -app = Flask(__name__) - - -def calculate(num1: float, num2: float, operation: str) -> float: - """ - Perform basic arithmetic operations: add, subtract, multiply, divide. - - >>> calculate(2, 3, 'add') - 5 - >>> calculate(5, 3, 'subtract') - 2 - >>> calculate(4, 2, 'multiply') - 8 - >>> calculate(10, 2, 'divide') - 5.0 - >>> calculate(5, 0, 'divide') - Traceback (most recent call last): - ... - ValueError: Division by zero is not allowed. - """ - if operation == "add": - return num1 + num2 - elif operation == "subtract": - return num1 - num2 - elif operation == "multiply": - return num1 * num2 - elif operation == "divide": - if num2 == 0: - raise ValueError("Division by zero is not allowed.") - return num1 / num2 - else: - raise ValueError(f"Unknown operation: {operation}") - - -# HTML template for the web interface -template = """ - - - - Flask Calculator - - - -

Flask Calculator

-
- - -
- -
- -
- {% if result is not none %} -
Result: {{ result }}
- {% endif %} - - -""" - - -@app.route("/", methods=["GET", "POST"]) -def home(): - result = None - if request.method == "POST": - try: - num1 = float(request.form["num1"]) - num2 = float(request.form["num2"]) - op = request.form["operation"] - result = calculate(num1, num2, op) - except Exception as e: - result = str(e) - return render_template_string(template, result=result) - - -if __name__ == "__main__": - app.run(debug=True) From 5dba8a180317b764ebfb8ace152031ec9257f687 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:27:01 +0530 Subject: [PATCH 10/16] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 35 ++++++++---------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 2c6350f20975..2f520825a0a3 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -1,15 +1,15 @@ -def isSafe(node, color, graph, n, col): - for k in range(n): - if graph[node][k] == 1 and col[k] == color: - return False - return True +from typing import List -def solve(node, col, m, n, graph): +def is_safe(node: int, color: int, graph: List[List[int]], n: int, col: List[int]) -> bool: + return all(not (graph[node][k] == 1 and col[k] == color) for k in range(n)) + + +def solve(node: int, col: List[int], m: int, n: int, graph: List[List[int]]) -> bool: if node == n: return True for c in range(1, m + 1): - if isSafe(node, c, graph, n, col): + if is_safe(node, c, graph, n, col): col[node] = c if solve(node + 1, col, m, n, graph): return True @@ -17,23 +17,6 @@ def solve(node, col, m, n, graph): return False -def graphColoring(graph, m, n): +def graph_coloring(graph: List[List[int]], m: int, n: int) -> bool: col = [0] * n - if solve(0, col, m, n, graph): - return True - return False - - -if __name__ == "__main__": - V = int(input()) - E = int(input()) - graph = [[0 for _ in range(V)] for _ in range(V)] - for _ in range(E): - u, v = map(int, input().split()) - graph[u][v] = 1 - graph[v][u] = 1 - m = int(input()) - if graphColoring(graph, m, V): - print("True") - else: - print("False") + return solve(0, col, m, n, graph) From ecab4cf9b11b76625ed98b6959c4db5037443a87 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:27:30 +0530 Subject: [PATCH 11/16] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 2f520825a0a3..18c409630022 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -2,10 +2,26 @@ def is_safe(node: int, color: int, graph: List[List[int]], n: int, col: List[int]) -> bool: + """ + Check if it is safe to assign a color to a node. + + >>> is_safe(0, 1, [[0,1],[1,0]], 2, [0,1]) + False + >>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1]) + True + """ return all(not (graph[node][k] == 1 and col[k] == color) for k in range(n)) def solve(node: int, col: List[int], m: int, n: int, graph: List[List[int]]) -> bool: + """ + Recursively try to color the graph using at most m colors. + + >>> solve(0, [0]*3, 3, 3, [[0,1,0],[1,0,1],[0,1,0]]) + True + >>> solve(0, [0]*3, 2, 3, [[0,1,0],[1,0,1],[0,1,0]]) + False + """ if node == n: return True for c in range(1, m + 1): @@ -18,5 +34,13 @@ def solve(node: int, col: List[int], m: int, n: int, graph: List[List[int]]) -> def graph_coloring(graph: List[List[int]], m: int, n: int) -> bool: + """ + Determine if the graph can be colored with at most m colors. + + >>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 3, 3) + True + >>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 2, 3) + False + """ col = [0] * n return solve(0, col, m, n, graph) From 8c00f5e09da85ae1c28d98c72fb1d600c2ca1086 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 18:01:02 +0000 Subject: [PATCH 12/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/m_coloring_problem.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 18c409630022..0533133e72f0 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -1,7 +1,9 @@ from typing import List -def is_safe(node: int, color: int, graph: List[List[int]], n: int, col: List[int]) -> bool: +def is_safe( + node: int, color: int, graph: List[List[int]], n: int, col: List[int] +) -> bool: """ Check if it is safe to assign a color to a node. From 67a6eb1ad5454ae79d04267d6a05dadd30794c1c Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:32:25 +0530 Subject: [PATCH 13/16] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 0533133e72f0..87dbec77edc3 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -1,9 +1,4 @@ -from typing import List - - -def is_safe( - node: int, color: int, graph: List[List[int]], n: int, col: List[int] -) -> bool: +def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int, col: list[int]) -> bool: """ Check if it is safe to assign a color to a node. @@ -12,37 +7,37 @@ def is_safe( >>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1]) True """ - return all(not (graph[node][k] == 1 and col[k] == color) for k in range(n)) + return all(not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices)) -def solve(node: int, col: List[int], m: int, n: int, graph: List[List[int]]) -> bool: +def solve(node: int, col: list[int], max_colors: int, num_vertices: int, graph: list[list[int]]) -> bool: """ - Recursively try to color the graph using at most m colors. + Recursively try to color the graph using at most max_colors. >>> solve(0, [0]*3, 3, 3, [[0,1,0],[1,0,1],[0,1,0]]) True >>> solve(0, [0]*3, 2, 3, [[0,1,0],[1,0,1],[0,1,0]]) False """ - if node == n: + if node == num_vertices: return True - for c in range(1, m + 1): - if is_safe(node, c, graph, n, col): + for c in range(1, max_colors + 1): + if is_safe(node, c, graph, num_vertices, col): col[node] = c - if solve(node + 1, col, m, n, graph): + if solve(node + 1, col, max_colors, num_vertices, graph): return True col[node] = 0 return False -def graph_coloring(graph: List[List[int]], m: int, n: int) -> bool: +def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) -> bool: """ - Determine if the graph can be colored with at most m colors. + Determine if the graph can be colored with at most max_colors. >>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 3, 3) True >>> graph_coloring([[0,1,1],[1,0,1],[1,1,0]], 2, 3) False """ - col = [0] * n - return solve(0, col, m, n, graph) + col = [0] * num_vertices + return solve(0, col, max_colors, num_vertices, graph) From b1ae4557d06eb018740b65baa22ab72d395cba67 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 18:05:49 +0000 Subject: [PATCH 14/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/m_coloring_problem.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 87dbec77edc3..88c8bc5e073e 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -1,4 +1,6 @@ -def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int, col: list[int]) -> bool: +def is_safe( + node: int, color: int, graph: list[list[int]], num_vertices: int, col: list[int] +) -> bool: """ Check if it is safe to assign a color to a node. @@ -7,10 +9,18 @@ def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int, co >>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1]) True """ - return all(not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices)) + return all( + not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices) + ) -def solve(node: int, col: list[int], max_colors: int, num_vertices: int, graph: list[list[int]]) -> bool: +def solve( + node: int, + col: list[int], + max_colors: int, + num_vertices: int, + graph: list[list[int]], +) -> bool: """ Recursively try to color the graph using at most max_colors. From b8a0a74f3821146bc59e42b534efa705ba594b87 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:37:33 +0530 Subject: [PATCH 15/16] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 43 +++++++++++++++++++----------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 88c8bc5e073e..fe5e3f726b79 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -1,6 +1,5 @@ -def is_safe( - node: int, color: int, graph: list[list[int]], num_vertices: int, col: list[int] -) -> bool: +def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int, + col: list[int]) -> bool: """ Check if it is safe to assign a color to a node. @@ -9,18 +8,12 @@ def is_safe( >>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1]) True """ - return all( - not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices) - ) - - -def solve( - node: int, - col: list[int], - max_colors: int, - num_vertices: int, - graph: list[list[int]], -) -> bool: + return all(not (graph[node][k] == 1 and col[k] == color) + for k in range(num_vertices)) + + +def solve(node: int, col: list[int], max_colors: int, num_vertices: int, + graph: list[list[int]]) -> bool: """ Recursively try to color the graph using at most max_colors. @@ -40,7 +33,8 @@ def solve( return False -def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) -> bool: +def graph_coloring(graph: list[list[int]], max_colors: int, + num_vertices: int) -> bool: """ Determine if the graph can be colored with at most max_colors. @@ -51,3 +45,20 @@ def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) - """ col = [0] * num_vertices return solve(0, col, max_colors, num_vertices, graph) + + +if __name__ == "__main__": + print("Graph Coloring Problem") + num_vertices = int(input("Enter number of vertices: ")) + num_edges = int(input("Enter number of edges: ")) + print("Enter each edge as 'u v' (0-based indexing):") + graph = [[0]*num_vertices for _ in range(num_vertices)] + for _ in range(num_edges): + u, v = map(int, input().split()) + graph[u][v] = 1 + graph[v][u] = 1 + max_colors = int(input("Enter maximum number of colors: ")) + if graph_coloring(graph, max_colors, num_vertices): + print(f"The graph can be colored with {max_colors} colors.") + else: + print(f"The graph cannot be colored with {max_colors} colors.") From a6a3db2a8d5dbb238df3e0737583cd71d2e690a1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 18:11:29 +0000 Subject: [PATCH 16/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/m_coloring_problem.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index fe5e3f726b79..eb8e12e53a3b 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -1,5 +1,6 @@ -def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int, - col: list[int]) -> bool: +def is_safe( + node: int, color: int, graph: list[list[int]], num_vertices: int, col: list[int] +) -> bool: """ Check if it is safe to assign a color to a node. @@ -8,12 +9,18 @@ def is_safe(node: int, color: int, graph: list[list[int]], num_vertices: int, >>> is_safe(0, 2, [[0,1],[1,0]], 2, [0,1]) True """ - return all(not (graph[node][k] == 1 and col[k] == color) - for k in range(num_vertices)) + return all( + not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices) + ) -def solve(node: int, col: list[int], max_colors: int, num_vertices: int, - graph: list[list[int]]) -> bool: +def solve( + node: int, + col: list[int], + max_colors: int, + num_vertices: int, + graph: list[list[int]], +) -> bool: """ Recursively try to color the graph using at most max_colors. @@ -33,8 +40,7 @@ def solve(node: int, col: list[int], max_colors: int, num_vertices: int, return False -def graph_coloring(graph: list[list[int]], max_colors: int, - num_vertices: int) -> bool: +def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) -> bool: """ Determine if the graph can be colored with at most max_colors. @@ -52,7 +58,7 @@ def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices = int(input("Enter number of vertices: ")) num_edges = int(input("Enter number of edges: ")) print("Enter each edge as 'u v' (0-based indexing):") - graph = [[0]*num_vertices for _ in range(num_vertices)] + graph = [[0] * num_vertices for _ in range(num_vertices)] for _ in range(num_edges): u, v = map(int, input().split()) graph[u][v] = 1