Appearance
question:def optimize_cluster(n: int, t: int, c: List[int], p: List[int]) -> Union[str, Tuple[str, List[Tuple[int, int]]]]: Decides if it's possible to assign exactly one task of each type to exactly one server such that the combined load on any server does not exceed its processing capacity. Parameters: n (int): Number of servers t (int): Number of task types c (list): List of servers' capacities p (list): List of task kinds' processing power Returns: str: "POSSIBLE" if a valid assignment exists, otherwise "IMPOSSIBLE" list of tuples: Each tuple contains two integers representing assignments (server, task type) pass # Example usage and test cases if __name__ == "__main__": from typing import List, Union, Tuple n = 3 t = 3 c = [10, 20, 30] p = [5, 10, 25] result = optimize_cluster(n, t, c, p) print(result) # "POSSIBLE", [(1, 1), (2, 2), (3, 3)] n = 2 t = 3 c = [15, 10] p = [5, 10, 5] result = optimize_cluster(n, t, c, p) print(result) # "IMPOSSIBLE"
answer:def optimize_cluster(n, t, c, p): Decides if it's possible to assign exactly one task of each type to exactly one server such that the combined load on any server does not exceed its processing capacity. Parameters: n (int): Number of servers t (int): Number of task types c (list): List of servers' capacities p (list): List of task kinds' processing power Returns: str: "POSSIBLE" if a valid assignment exists, otherwise "IMPOSSIBLE" list of tuples: Each tuple contains two integers representing assignments (server, task type) # Sort the capacities and tasks along with their original indices servers = sorted([(c[i], i + 1) for i in range(n)], reverse=True) tasks = sorted([(p[j], j + 1) for j in range(t)], reverse=True) if n < t: return "IMPOSSIBLE" assignments = [] for j in range(t): found = False for i in range(n): if servers[i][0] >= tasks[j][0]: assignments.append((servers[i][1], tasks[j][1])) servers[i] = (servers[i][0] - tasks[j][0], servers[i][1]) found = True break if not found: return "IMPOSSIBLE" return "POSSIBLE", assignments
question:def rabbit_behavior(n: int, m: int, k: int, record_action) -> List[List[int]]: Determine the sequence of actions performed by each rabbit species over d days. Args: n : int : Number of rabbit species (power of 2) m : int : Number of unique actions k : int : Length of observation window (power of 2) record_action : function : A function to record the actions of a given rabbit species. It takes an integer (species index) as a parameter and returns a list of length k representing the actions over k days. Returns: List[List[int]] : A nested list where each inner list contains the complete sequence of actions for a rabbit species. >>> n, m, k = 4, 5, 2 >>> mock_record_action = lambda s: {1: [1, 2], 2: [3, 4], 3: [2, 5], 4: [1, 3]}[s] >>> rabbit_behavior(n, m, k, mock_record_action) [[1, 2], [3, 4], [2, 5], [1, 3]] def mock_record_action_1(s): mock_data = { 1: [1, 2], 2: [3, 4], 3: [2, 5], 4: [1, 3] } return mock_data[s] def mock_record_action_2(s): mock_data = { 1: [5, 4], 2: [3, 2], 3: [1, 4], 4: [2, 3] } return mock_data[s] def test_rabbit_behavior_case_1(): n, m, k = 4, 5, 2 expected_output = [ [1, 2], [3, 4], [2, 5], [1, 3] ] assert rabbit_behavior(n, m, k, mock_record_action_1) == expected_output def test_rabbit_behavior_case_2(): n, m, k = 4, 5, 2 expected_output = [ [5, 4], [3, 2], [1, 4], [2, 3] ] assert rabbit_behavior(n, m, k, mock_record_action_2) == expected_output def test_rabbit_behavior_different_k(): n, m, k = 4, 5, 2 expected_output = [ [5, 4], [3, 2], [1, 4], [2, 3] ] assert rabbit_behavior(n, m, k, mock_record_action_2) == expected_output
answer:def rabbit_behavior(n, m, k, record_action): Determine the sequence of actions performed by each rabbit species over d days. Args: n : int : Number of rabbit species (power of 2) m : int : Number of unique actions k : int : Length of observation window (power of 2) record_action : function : A function to record the actions of a given rabbit species. It takes an integer (species index) as parameter and returns a list of length k representing the actions over k days. Returns: List[List[int]] : A nested list where each inner list contains the complete sequence of actions for a rabbit species. action_sequences = [None] * n for i in range(1, n+1): action_sequences[i-1] = record_action(i) return action_sequences
question:def first_repeated_number(n: int, sequence: List[int]) -> int: Finds the first number in the given sequence that appears at least twice. >>> first_repeated_number(6, [2, 3, 4, 2, 1, 3]) 2 >>> first_repeated_number(5, [1, 2, 3, 4, 5]) -1
answer:def first_repeated_number(n, sequence): Finds the first number in the given sequence that appears at least twice. :param n: Number of integers in the sequence :param sequence: List of integers in the sequence :return: The first repeating number or -1 if no such number exists seen = set() for number in sequence: if number in seen: return number seen.add(number) return -1
question:def generate_sequence(k: int) -> (List[int], int): Generates a sequence of incrementing integers starting from 1 until the sum of the sequence becomes greater than or equal to k. Arguments: k -- the threshold integer value (1 ≤ k ≤ 10^9) Returns: A tuple containing: - the sequence as a list of integers - the final sum of the sequence Examples: >>> generate_sequence(10) ([1, 2, 3, 4], 10) >>> generate_sequence(20) ([1, 2, 3, 4, 5, 6], 21) >>> generate_sequence(1) ([1], 1)
answer:def generate_sequence(k): Generates a sequence of incrementing integers starting from 1 until the sum of the sequence becomes greater than or equal to k. Arguments: k -- the threshold integer value (1 ≤ k ≤ 10^9) Returns: A tuple containing: - the sequence as a list of integers - the final sum of the sequence sequence = [] current_number = 1 current_sum = 0 while current_sum < k: sequence.append(current_number) current_sum += current_number current_number += 1 return sequence, current_sum