import json import logging import heapq from flask import request, jsonify from routes import app logger = logging.getLogger(__name__) @app.route('/bugfixer/p2', methods=['POST']) def max_bugs_fixed(): data = request.get_json() logging.info("Data received for evaluation: {}".format(data)) results = [] for test_case in data: bugseq = test_case.get("bugseq", []) # Sort the bugs by their escalation limits (deadlines) bugseq.sort(key=lambda x: x[1]) total_time = 0 max_heap = [] for difficulty, limit in bugseq: total_time += difficulty # Use a max heap by inserting negative difficulty heapq.heappush(max_heap, -difficulty) if total_time > limit: # Remove the bug with the largest difficulty removed_difficulty = -heapq.heappop(max_heap) total_time -= removed_difficulty logging.debug(f"Removed bug with difficulty {removed_difficulty} to meet the limit.") num_bugs_fixed = len(max_heap) results.append(num_bugs_fixed) logging.info("Computed number of bugs fixed: {}".format(num_bugs_fixed)) return jsonify(results) @app.route('/bugfixer/p1', methods=['POST']) def bugfixer_p1(): data = request.get_json() logging.info("Data received for evaluation: {}".format(data)) if data is None: logging.error("No data received") return json.dumps([]) # Ensure data is in list form if isinstance(data, dict): items = [data] elif isinstance(data, list): items = data else: logging.error("Invalid data format") return json.dumps([]) result = [] for item in items: time_list = item.get('time') or [] prerequisites_list = item.get('prerequisites') or [] total_time = compute_min_time(time_list, prerequisites_list) result.append(total_time) logging.info("My result: {}".format(result)) return jsonify(result) def compute_min_time(time_list, prerequisites_list): n = len(time_list) time_list = [0] + time_list # Adjust to 1-based indexing graph = [[] for _ in range(n + 1)] # adjacency list in_degree = [0] * (n + 1) earliest_finish_time = [0] * (n + 1) for prereq in prerequisites_list: a, b = prereq graph[a].append(b) in_degree[b] += 1 # Initialize queue with nodes with in-degree zero queue = [] for i in range(1, n + 1): if in_degree[i] == 0: earliest_finish_time[i] = time_list[i] queue.append(i) # Process nodes while queue: u = queue.pop(0) for v in graph[u]: earliest_finish_time[v] = max( earliest_finish_time[v], earliest_finish_time[u] + time_list[v] ) in_degree[v] -= 1 if in_degree[v] == 0: queue.append(v) # Maximum earliest_finish_time among all projects total_time = max(earliest_finish_time) return total_time