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/11] 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/11] 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/11] [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/11] 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/11] 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/11] 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/11] [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/11] 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/11] 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/11] 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/11] 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)