diff --git a/backtracking/m-coloring-problem.py b/backtracking/m-coloring-problem.py new file mode 100644 index 000000000000..2c6350f20975 --- /dev/null +++ b/backtracking/m-coloring-problem.py @@ -0,0 +1,39 @@ +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") diff --git a/other/calc.py b/other/calc.py new file mode 100644 index 000000000000..dcf466cb48f7 --- /dev/null +++ b/other/calc.py @@ -0,0 +1,89 @@ +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)