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/27] 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/27] 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/27] [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/27] 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/27] 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/27] 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/27] [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/27] 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/27] 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/27] 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/27] 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/27] [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/27] 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/27] [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/27] 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/27] [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 From a841332de788cff3071dd9bd956b32bd7322fadb Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:43:23 +0530 Subject: [PATCH 17/27] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 38 +++++++++++------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index eb8e12e53a3b..ff39f9002f0f 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) - ) + 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. @@ -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. @@ -54,17 +48,13 @@ def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) - 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)] + num_vertices = int(input()) + num_edges = int(input()) + 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.") + max_colors = int(input()) + col = [0]*num_vertices + print(solve(0, col, max_colors, num_vertices, graph)) From 15b2b7bac27cc5e83f230fb8e63dc51fa289a9b4 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:47:38 +0530 Subject: [PATCH 18/27] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index ff39f9002f0f..62e9fb52360f 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -53,8 +53,11 @@ def graph_coloring(graph: list[list[int]], max_colors: int, 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 + if 0 <= u < num_vertices and 0 <= v < num_vertices: + graph[u][v] = 1 + graph[v][u] = 1 + else: + raise ValueError("Edge indices out of range") max_colors = int(input()) col = [0]*num_vertices print(solve(0, col, max_colors, num_vertices, graph)) From a729c20b840eb174c9fb48a9d1c6b2b8928f78b3 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:47:58 +0530 Subject: [PATCH 19/27] Update m_coloring_problem.py From 96ba61eb94b797adfbb9dab4f05bc52fcd080c86 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:51:32 +0530 Subject: [PATCH 20/27] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 62e9fb52360f..6adb197c5028 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -1,5 +1,4 @@ -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 +7,10 @@ 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 +30,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. @@ -50,7 +46,7 @@ def graph_coloring(graph: list[list[int]], max_colors: int, if __name__ == "__main__": num_vertices = int(input()) num_edges = int(input()) - 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()) if 0 <= u < num_vertices and 0 <= v < num_vertices: @@ -59,5 +55,4 @@ def graph_coloring(graph: list[list[int]], max_colors: int, else: raise ValueError("Edge indices out of range") max_colors = int(input()) - col = [0]*num_vertices - print(solve(0, col, max_colors, num_vertices, graph)) + print(graph_coloring(graph, max_colors, num_vertices)) From 0d3bd867cbf043eb8bb57bfb815630956cb6bf96 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:55:19 +0530 Subject: [PATCH 21/27] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 6adb197c5028..dba295c2ecde 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -1,4 +1,10 @@ -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 +13,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. @@ -30,7 +44,9 @@ def solve(node: int, col: list[int], max_colors: int, num_vertices: int, graph: 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. From 547da5e59cbab2a80ed70a28ddbc20a9a16e6c65 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:27:16 +0000 Subject: [PATCH 22/27] [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, 1 insertion(+), 3 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index dba295c2ecde..c9e419bca372 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -44,9 +44,7 @@ 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. From 719da796b6cfafc5889dbf0f2b9fae03c60a3fd4 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Tue, 7 Oct 2025 00:02:08 +0530 Subject: [PATCH 23/27] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index c9e419bca372..925071d09123 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -14,7 +14,8 @@ def is_safe( True """ return all( - not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices) + not (graph[node][k] == 1 and col[k] == color) + for k in range(num_vertices) ) @@ -44,7 +45,9 @@ 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. @@ -58,9 +61,14 @@ def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) - if __name__ == "__main__": - num_vertices = int(input()) - num_edges = int(input()) + import doctest + + doctest.testmod() + + num_vertices = int(input("Enter number of vertices: ")) + num_edges = int(input("Enter number of edges: ")) graph = [[0] * num_vertices for _ in range(num_vertices)] + for _ in range(num_edges): u, v = map(int, input().split()) if 0 <= u < num_vertices and 0 <= v < num_vertices: @@ -68,5 +76,6 @@ def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) - graph[v][u] = 1 else: raise ValueError("Edge indices out of range") - max_colors = int(input()) + + max_colors = int(input("Enter maximum number of colors: ")) print(graph_coloring(graph, max_colors, num_vertices)) From 4f43023edcedf9754a3b805abc425dcb82a0e84d 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:37:12 +0000 Subject: [PATCH 24/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/m_coloring_problem.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 925071d09123..1f872ff78bec 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -14,8 +14,7 @@ def is_safe( True """ return all( - not (graph[node][k] == 1 and col[k] == color) - for k in range(num_vertices) + not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices) ) @@ -45,9 +44,7 @@ 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. From 6a5897a99857f6df00f7dd34a727ac509b9f2286 Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Tue, 7 Oct 2025 00:09:15 +0530 Subject: [PATCH 25/27] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 31 ++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 1f872ff78bec..6d9f2e2b64e2 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -14,7 +14,8 @@ def is_safe( True """ return all( - not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices) + not (graph[node][k] == 1 and col[k] == color) + for k in range(num_vertices) ) @@ -31,7 +32,7 @@ def solve( >>> 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 + True """ if node == num_vertices: return True @@ -44,7 +45,9 @@ 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. @@ -66,13 +69,21 @@ def graph_coloring(graph: list[list[int]], max_colors: int, num_vertices: int) - num_edges = int(input("Enter number of edges: ")) graph = [[0] * num_vertices for _ in range(num_vertices)] + print("Enter the edges (u v):") for _ in range(num_edges): - u, v = map(int, input().split()) - if 0 <= u < num_vertices and 0 <= v < num_vertices: - graph[u][v] = 1 - graph[v][u] = 1 - else: - raise ValueError("Edge indices out of range") + try: + u, v = map(int, input().split()) + if 0 <= u < num_vertices and 0 <= v < num_vertices: + graph[u][v] = 1 + graph[v][u] = 1 + else: + print(f"Invalid edge: vertices must be between 0 and {num_vertices - 1}.") + except ValueError: + print("Invalid input format. Please enter two integers separated by a space.") max_colors = int(input("Enter maximum number of colors: ")) - print(graph_coloring(graph, max_colors, num_vertices)) + + if graph_coloring(graph, max_colors, num_vertices): + print("The graph can be colored with the given number of colors.") + else: + print("The graph cannot be colored with the given number of colors.") From 18dd29f2b1a78146e57df429d6bd818e66a7ba1b Mon Sep 17 00:00:00 2001 From: Supratim <109270340+sgindeed@users.noreply.github.com> Date: Tue, 7 Oct 2025 00:16:17 +0530 Subject: [PATCH 26/27] Update m_coloring_problem.py --- backtracking/m_coloring_problem.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index 6d9f2e2b64e2..c944109160fe 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -14,8 +14,7 @@ def is_safe( True """ return all( - not (graph[node][k] == 1 and col[k] == color) - for k in range(num_vertices) + not (graph[node][k] == 1 and col[k] == color) for k in range(num_vertices) ) @@ -65,11 +64,11 @@ def graph_coloring( doctest.testmod() - num_vertices = int(input("Enter number of vertices: ")) - num_edges = int(input("Enter number of edges: ")) + num_vertices = int(input("Enter vertices: ")) + num_edges = int(input("Enter edges: ")) graph = [[0] * num_vertices for _ in range(num_vertices)] - print("Enter the edges (u v):") + print("Enter edges (u v):") for _ in range(num_edges): try: u, v = map(int, input().split()) @@ -77,13 +76,13 @@ def graph_coloring( graph[u][v] = 1 graph[v][u] = 1 else: - print(f"Invalid edge: vertices must be between 0 and {num_vertices - 1}.") + print("Invalid edge.") except ValueError: - print("Invalid input format. Please enter two integers separated by a space.") + print("Invalid input.") + + max_colors = int(input("Enter max colors: ")) - max_colors = int(input("Enter maximum number of colors: ")) - if graph_coloring(graph, max_colors, num_vertices): - print("The graph can be colored with the given number of colors.") + print("Coloring possible.") else: - print("The graph cannot be colored with the given number of colors.") + print("Coloring not possible.") From 7e235180ba224fb28c65cfd0af43242ae96c8f94 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:52:32 +0000 Subject: [PATCH 27/27] [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, 1 insertion(+), 3 deletions(-) diff --git a/backtracking/m_coloring_problem.py b/backtracking/m_coloring_problem.py index c944109160fe..89de4c09e10b 100644 --- a/backtracking/m_coloring_problem.py +++ b/backtracking/m_coloring_problem.py @@ -44,9 +44,7 @@ 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.