Appearance
question:def is_back_to_start(movements: str) -> str: Determines if the bird returns to the starting point after the given sequence of movements. :param movements: str, sequence of movements (L, R, U, D) :return: str, "YES" if the bird returns to the starting point, "NO" otherwise. >>> is_back_to_start("LR") "YES" >>> is_back_to_start("UDLR") "YES" >>> is_back_to_start("UUDL") "NO"
answer:def is_back_to_start(movements): Determines if the bird returns to the starting point after the given sequence of movements. :param movements: str, sequence of movements (L, R, U, D) :return: str, "YES" if the bird returns to the starting point, "NO" otherwise. horizontal = 0 vertical = 0 for move in movements: if move == 'L': horizontal -= 1 elif move == 'R': horizontal += 1 elif move == 'U': vertical += 1 elif move == 'D': vertical -= 1 return "YES" if horizontal == 0 and vertical == 0 else "NO"
question:def health_management_system(H: int, A: int, actions: List[str, int]) -> int: Simulates the health management system for a player given initial health, number of actions, and a list of actions. Each action can either be 'damage' or 'heal'. Arguments: H -- initial health of the player. A -- number of actions. actions -- list of tuples where each tuple contains a string ('damage' or 'heal') and an integer value. Returns: The final health of the player after all actions. # Example test cases: # Test Case 1 - Given example # assert health_management_system(100, 4, [('damage', 30), ('heal', 20), ('damage', 50), ('heal', 10)]) == 50 # # Test Case 2 - Given example with death # assert health_management_system(200, 3, [('damage', 150), ('damage', 100), ('heal', 50)]) == 0 # # Test Case 3 - No actions # assert health_management_system(100, 0, []) == 100 # # Test Case 4 - Only healing # assert health_management_system(100, 3, [('heal', 30), ('heal', 20), ('heal', 50)]) == 100 # # Test Case 5 - Healing and damage that never kills the player # assert health_management_system(100, 3, [('damage', 30), ('heal', 20), ('damage', 50)]) == 40 # # Test Case 6 - Damage that kills the player immediately # assert health_management_system(50, 2, [('damage', 30), ('damage', 40)]) == 0 # # Test Case 7 - Composite actions that heal up to initial health # assert health_management_system(150, 4, [('damage', 50), ('heal', 20), ('damage', 50), ('heal', 100)]) == 150 # Edge Cases: # Minimal initial health # assert health_management_system(1, 1, [('damage', 1)]) == 0 # # Max initial health handling # assert health_management_system(1000000, 1, [('heal', 500000)]) == 1000000 # # High damage exceeding health # assert health_management_system(100, 1, [('damage', 150)]) == 0 # # High initial health with multiple damage and heal actions # assert health_management_system(1000000, 5, # [('damage', 999999), ('heal', 500000), # ('damage', 499999), ('heal', 499999), ('damage', 1000000)]) == 0
answer:def health_management_system(H, A, actions): Simulates the health management system for a player given initial health, number of actions, and a list of actions. Each action can either be 'damage' or 'heal'. Arguments: H -- initial health of the player. A -- number of actions. actions -- list of tuples where each tuple contains a string ('damage' or 'heal') and an integer value. Returns: The final health of the player after all actions. current_health = H for action in actions: typei, valuei = action if typei == 'damage': current_health -= valuei elif typei == 'heal': current_health = min(current_health + valuei, H) if current_health <= 0: return 0 return current_health
question:def maximum_bitwise_and(t: int, test_cases: List[Tuple[int, List[int]]]) -> List[int]: Calculate the maximum bitwise AND value of any two elements in the array for each test case. Args: t: An integer, the number of test cases. test_cases: A list of tuples, where each tuple contains an integer n (the number of elements in the array) and a list of n integers (the elements of the array). Returns: A list of integers, where each integer is the maximum bitwise AND value of any two elements in the array for the corresponding test case. pass def parse_input_to_params(input_string: str) -> Tuple[int, List[Tuple[int, List[int]]]]: Parse the input string to the required parameters for the maximum_bitwise_and function. Args: input_string: A string containing the number of test cases followed by the test cases. Returns: A tuple where the first element is the number of test cases (t) and the second element is a list of test cases. Each test case is represented as a tuple where the first element is an integer n and the second element is a list of n integers. pass from solution import maximum_bitwise_and, parse_input_to_params def test_maximum_bitwise_and_example(): input_string = 3 3 1 2 3 5 8 16 32 64 128 4 5 9 12 25 t, test_cases = parse_input_to_params(input_string) results = maximum_bitwise_and(t, test_cases) assert results == [2, 0, 9] def test_maximum_bitwise_and_small_cases(): input_string = 2 2 1 1 2 0 0 t, test_cases = parse_input_to_params(input_string) results = maximum_bitwise_and(t, test_cases) assert results == [1, 0] def test_maximum_bitwise_and_large_numbers(): input_string = 1 4 1024 2048 4096 8192 t, test_cases = parse_input_to_params(input_string) results = maximum_bitwise_and(t, test_cases) assert results == [0] def test_maximum_bitwise_and_mixed_numbers(): input_string = 1 6 15 7 9 14 12 10 t, test_cases = parse_input_to_params(input_string) results = maximum_bitwise_and(t, test_cases) assert results == [14] def test_parse_input_to_params(): input_string = 2 3 1 2 3 4 5 10 15 20 t, test_cases = parse_input_to_params(input_string) assert t == 2 assert test_cases == [ (3, [1, 2, 3]), (4, [5, 10, 15, 20]), ]
answer:def maximum_bitwise_and(t, test_cases): results = [] for i in range(t): n, arr = test_cases[i] max_and = 0 for j in range(n): for k in range(j + 1, n): max_and = max(max_and, arr[j] & arr[k]) results.append(max_and) return results def parse_input_to_params(input_string): input_lines = input_string.strip().split('n') t = int(input_lines[0]) test_cases = [] index = 1 for _ in range(t): n = int(input_lines[index]) arr = list(map(int, input_lines[index + 1].split())) test_cases.append((n, arr)) index += 2 return t, test_cases
question:def max_perimeter_of_1s(matrix: List[List[int]]) -> int: Find the maximum perimeter of a rectangle composed entirely of 1s in a given binary matrix. >>> max_perimeter_of_1s([ ... [1, 0, 1, 1], ... [1, 1, 1, 1], ... [0, 1, 1, 0], ... [1, 1, 0, 0] ... ]) == 10 >>> max_perimeter_of_1s([ ... [0, 0, 0], ... [0, 0, 0], ... [0, 0, 0] ... ]) == 0 from typing import List def test_example1(): matrix = [ [1, 0, 1, 1], [1, 1, 1, 1], [0, 1, 1, 0], [1, 1, 0, 0] ] assert max_perimeter_of_1s(matrix) == 10 def test_example2(): matrix = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ] assert max_perimeter_of_1s(matrix) == 0 def test_example3(): matrix = [ [1, 1, 1, 1, 0], [1, 1, 1, 1, 0], [0, 0, 1, 1, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1] ] assert max_perimeter_of_1s(matrix) == 14 def test_single_element_0(): matrix = [ [0] ] assert max_perimeter_of_1s(matrix) == 0 def test_single_element_1(): matrix = [ [1] ] assert max_perimeter_of_1s(matrix) == 4 def test_line_of_1s(): matrix = [ [1, 1, 1, 1, 1] ] assert max_perimeter_of_1s(matrix) == 12 def test_column_of_1s(): matrix = [ [1], [1], [1], [1], [1] ] assert max_perimeter_of_1s(matrix) == 12
answer:def max_perimeter_of_1s(matrix): n = len(matrix) if n == 0: return 0 m = len(matrix[0]) if m == 0: return 0 # Initialize height and left arrays height = [0] * m left = [0] * m right = [m] * m max_perimeter = 0 for i in range(n): cur_left, cur_right = 0, m # Update height for j in range(m): if matrix[i][j] == 1: height[j] += 1 else: height[j] = 0 # Update left for j in range(m): if matrix[i][j] == 1: left[j] = max(left[j], cur_left) else: left[j] = 0 cur_left = j + 1 # Update right for j in range(m-1, -1, -1): if matrix[i][j] == 1: right[j] = min(right[j], cur_right) else: right[j] = m cur_right = j # Calculate the maximum perimeter for j in range(m): if matrix[i][j] == 1: width = right[j] - left[j] perimeter = 2 * (width + height[j]) max_perimeter = max(max_perimeter, perimeter) return max_perimeter