Appearance
question:def max_resources(n: int, m: int, k: int, grid: List[List[int]]) -> int: Calculate the maximum resources that can be collected by starting at any cell in the first row and moving to the last row, moving only downward or diagonally downward adjacent. Args: n (int): The number of rows in the grid. m (int): The number of columns in the grid. k (int): The maximum number of resources in any cell. grid (List[List[int]]): The grid representation with resource values. Returns: int: The maximum number of resources that can be collected. Example: >>> max_resources(3, 3, 5, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 18
answer:def max_resources(n, m, k, grid): # Initialize a dp array with same dimensions as grid dp = [[0]*m for _ in range(n)] # Fill the dp array for the first row for j in range(m): dp[0][j] = grid[0][j] # Fill the dp array for subsequent rows for i in range(1, n): for j in range(m): # Current cell max_val = dp[i-1][j] # Diagonal left if j > 0: max_val = max(max_val, dp[i-1][j-1]) # Diagonal right if j < m-1: max_val = max(max_val, dp[i-1][j+1]) dp[i][j] = grid[i][j] + max_val # The result will be the maximum value in the last row return max(dp[n-1]) # Example usage: n, m, k = 3, 3, 5 grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(max_resources(n, m, k, grid)) # Output: 18
question:def isPythagoreanPrime(N: int) -> int: Given a number N, check if it is a Pythagorean prime. A Pythagorean prime is a prime number that can be expressed in the form 4k + 1. >>> isPythagoreanPrime(5) 1 >>> isPythagoreanPrime(7) 0 >>> isPythagoreanPrime(10) 0 pass def test_isPythagoreanPrime(): assert isPythagoreanPrime(5) == 1 # Pythagorean prime assert isPythagoreanPrime(7) == 0 # Prime, but not Pythagorean prime assert isPythagoreanPrime(10) == 0 # Not a prime assert isPythagoreanPrime(13) == 1 # Pythagorean prime assert isPythagoreanPrime(1) == 0 # Not a prime assert isPythagoreanPrime(0) == 0 # Not a prime assert isPythagoreanPrime(2) == 0 # Prime, but not Pythagorean prime assert isPythagoreanPrime(3) == 0 # Prime, but not Pythagorean prime assert isPythagoreanPrime(17) == 1 # Pythagorean prime assert isPythagoreanPrime(19) == 0 # Prime, but not Pythagorean prime assert isPythagoreanPrime(25) == 0 # Not a prime assert isPythagoreanPrime(37) == 1 # Pythagorean prime test_isPythagoreanPrime()
answer:def is_prime(num): if num <= 1: return False if num <= 3: return True if num % 2 == 0 or num % 3 == 0: return False i = 5 while i * i <= num: if num % i == 0 or num % (i + 2) == 0: return False i += 6 return True def isPythagoreanPrime(N): if is_prime(N) and (N % 4 == 1): return 1 else: return 0
question:from typing import List, Union def arrange_plants(R: int, C: int, T: int, grid: List[List[int]]) -> Union[List[List[int]], str]: Arrange plant species on the grid such that no two neighboring cells contain plants with growth factors differing by more than a threshold T and each plant has at least one neighbor. >>> arrange_plants(4, 4, 2, [ ... [1, 2, 3, 4], ... [2, 3, 4, 5], ... [3, 4, 5, 6], ... [4, 5, 6, 7] ... ]) == [ ... [1, 2, 3, 4], ... [2, 3, 4, 5], ... [3, 4, 5, 6], ... [4, 5, 6, 7] ... ] >>> arrange_plants(3, 3, 1, [ ... [9, 10, 12], ... [11, 13, 14], ... [13, 14, 15] ... ]) == "Impossible"
answer:def arrange_plants(R, C, T, grid): def is_valid_arrangement(grid, R, C, T): for i in range(R): for j in range(C): if i > 0 and abs(grid[i][j] - grid[i - 1][j]) > T: return False if j > 0 and abs(grid[i][j] - grid[i][j - 1]) > T: return False return True if is_valid_arrangement(grid, R, C, T): return grid else: return "Impossible"
question:def find_initial_marbles(n: int, m: int, l: int) -> list: Determines the initial configuration of marbles in the jars given the final number of marbles in the last remaining jar. Parameters: n (int): The initial number of jars (2 ≤ n ≤ 100). m (int): The number of marbles in the final jar after all operations (0 ≤ m < l). l (int): The maximum number of marbles that can be initially present in any jar (2 ≤ l ≤ 100). Returns: list: The initial configuration of marbles in each jar if possible, otherwise -1. Examples: >>> find_initial_marbles(4, 3, 5) [2, 1, 0, 0] # or [3, 0, 0, 0] >>> find_initial_marbles(3, 0, 4) [0, 0, 0] >>> find_initial_marbles(2, 1, 3) [1, 0] # or [0, 1] >>> find_initial_marbles(4, 8, 2) -1 >>> find_initial_marbles(5, 20, 3) -1 >>> find_initial_marbles(3, 99, 100) [99, 0, 0] # or [0, 99, 0] or [0, 0, 99] >>> find_initial_marbles(2, 1, 2) [1, 0] # or [0, 1]
answer:def find_initial_marbles(n, m, l): Determines the initial configuration of marbles in the jars given the final number of marbles in the last remaining jar. Parameters: n (int): The initial number of jars. m (int): The number of marbles in the final jar after all operations. l (int): The maximum number of marbles that can be initially present in any jar. Returns: list: The initial configuration of marbles in each jar if possible, otherwise -1. # Case when n * (l-1) < m which makes the final number impossible if m > n * (l - 1): return -1 # Try to distribute marbles to make it sum to `m` initial_config = [] total_marbles = m remaining_jars = n # Initial value for each jar for i in range(n): if i < n - 1: initial_value = min(l-1, total_marbles) initial_config.append(initial_value) total_marbles -= initial_value remaining_jars -= 1 else: # Fill the last jar with the remaining marbles initial_config.append(total_marbles) if sum(initial_config) % l != m % l: return -1 return initial_config