Appearance
question:from typing import List def plant_flowers(n: int, m: int, garden: List[str]) -> List[str]: Given a garden grid with empty spaces ('.') and rocks ('#'), compute the maximum number of flowers that can be planted such that no two flowers are adjacent (in all 8 possible directions). Args: n (int): number of rows in the garden grid. m (int): number of columns in the garden grid. garden (List[str]): list of strings representing the garden grid. Returns: List[str]: the modified garden grid with maximum flowers planted. pass def test_plant_flowers_example_case(): n, m = 4, 4 garden = [ "....", ".#..", "..#.", "...." ] expected = [ "*.*.", ".#.*", "*.#.", ".*.*" ] assert plant_flowers(n, m, garden) == expected def test_plant_flowers_all_empty(): n, m = 2, 2 garden = [ "..", ".." ] expected = [ "*.", ".*" ] assert plant_flowers(n, m, garden) == expected def test_plant_flowers_all_rocks(): n, m = 2, 2 garden = [ "", "" ] expected = [ "", "" ] assert plant_flowers(n, m, garden) == expected def test_plant_flowers_mixed_case(): n, m = 3, 3 garden = [ "#", "#.#", "#" ] expected = [ "#", "#*#", "#" ] assert plant_flowers(n, m, garden) == expected def test_plant_flowers_one_row(): n, m = 1, 4 garden = [ "...." ] expected = [ "*.*." ] assert plant_flowers(n, m, garden) == expected def test_plant_flowers_one_column(): n, m = 4, 1 garden = [ ".", ".", ".", "." ] expected = [ "*", ".", "*", "." ] assert plant_flowers(n, m, garden) == expected
answer:def plant_flowers(n, m, garden): Given a garden grid with empty spaces ('.') and rocks ('#'), compute the maximum number of flowers that can be planted such that no two flowers are adjacent (in all 8 possible directions). Args: n (int): number of rows in the garden grid. m (int): number of columns in the garden grid. garden (List[str]): list of strings representing the garden grid. Returns: List[str]: the modified garden grid with maximum flowers planted. # Making a copy of the garden to modify result_garden = [list(row) for row in garden] # Iterate through the cells of the grid for i in range(n): for j in range(m): # We can plant a flower if the cell is empty and the sum of the row and column index is even if garden[i][j] == '.' and (i + j) % 2 == 0: result_garden[i][j] = '*' # Convert resulting list of lists back to list of strings return [''.join(row) for row in result_garden]
question:def can_all_scores_become_equal(n, scores): Determines if all participants can achieve the same score through the operations described. Parameters: n (int): The number of participants. scores (list of int): The initial scores of the participants. Returns: str: "Yes" if it's possible for all participants to achieve the same score, otherwise "No". Example: >>> can_all_scores_become_equal(3, [3, 6, 9]) "Yes" >>> can_all_scores_become_equal(3, [4, 5, 6]) "No" from solution import can_all_scores_become_equal def test_basic_cases(): assert can_all_scores_become_equal(3, [3, 6, 9]) == "Yes" assert can_all_scores_become_equal(3, [4, 5, 6]) == "No" def test_identical_scores(): assert can_all_scores_become_equal(4, [7, 7, 7, 7]) == "Yes" def test_zero_scores(): assert can_all_scores_become_equal(3, [0, 0, 0]) == "Yes" assert can_all_scores_become_equal(3, [0, 1, 2]) == "No" def test_large_numbers(): assert can_all_scores_become_equal(5, [1000000000, 500000000, 250000000, 125000000, 62500000]) == "Yes" def test_mixed_gcd(): assert can_all_scores_become_equal(4, [8, 16, 24, 32]) == "Yes" assert can_all_scores_become_equal(4, [8, 16, 25, 32]) == "No"
answer:def can_all_scores_become_equal(n, scores): Determines if all participants can achieve the same score through the operations described. Parameters: n (int): The number of participants. scores (list of int): The initial scores of the participants. Returns: str: "Yes" if it's possible for all participants to achieve the same score, otherwise "No". min_score = min(scores) gcd_value = min_score def gcd(a, b): while b: a, b = b, a % b return a for score in scores: gcd_value = gcd(gcd_value, score) if gcd_value == 1: return "No" return "Yes"
question:def max_books(n: int, W: int, weights: List[int]) -> int: Returns the maximum number of books that can fit on a shelf without exceeding the weight limit. Arguments: n -- number of books W -- weight limit of the shelf weights -- list of weights of the books (list of integers) Returns: int -- maximum number of books that can be placed on the shelf >>> max_books(5, 10, [2, 3, 6, 4, 1]) 4 >>> max_books(1, 5, [10]) 0 >>> max_books(1, 5, [3]) 1 >>> max_books(4, 15, [1, 2, 3, 4]) 4 >>> max_books(3, 5, [6, 7, 8]) 0 >>> max_books(4, 10, [5, 5, 5, 5]) 2 >>> max_books(10000, 5000, list(range(1, 10001))) 99 >>> max_books(1, 1, [1]) 1 >>> max_books(10, 10**9, [10**9] * 10) 1 >>> max_books(10, 10**9 - 1, [10**9] * 10) 0 pass
answer:def max_books(n, W, weights): Returns the maximum number of books that can fit on a shelf without exceeding the weight limit. Arguments: n -- number of books W -- weight limit of the shelf weights -- list of weights of the books Returns: int -- maximum number of books that can be placed on the shelf weights.sort() total_weight = 0 count = 0 for weight in weights: if total_weight + weight <= W: total_weight += weight count += 1 else: break return count
question:def process_transactions(n: int, transactions: List[str]) -> Tuple[str, int, int, int]: Process a series of transactions and compute the balance at each step, identifying specific moments when the balance reaches its highest and lowest points, and counting the number of times the account went into overdraft. >>> process_transactions(6, ["credit 100", "debit 50", "credit 200", "debit 300", "debit 100", "credit 500"]) ("100 50 250 -50 -150 350", 350, -150, 2) >>> process_transactions(3, ["credit 100", "credit 200", "credit 300"]) ("100 300 600", 600, 100, 0) >>> process_transactions(3, ["debit 50", "debit 150", "debit 200"]) ("-50 -200 -400", -50, -400, 3) >>> process_transactions(6, ["credit 300", "debit 100", "debit 50", "credit 200", "debit 450", "credit 500"]) ("300 200 150 350 -100 400", 400, -100, 1) >>> process_transactions(1, ["credit 500"]) ("500", 500, 500, 0) >>> process_transactions(1, ["debit 500"]) ("-500", -500, -500, 1)
answer:def process_transactions(n, transactions): balances = [] current_balance = 0 highest_balance = float('-inf') lowest_balance = float('inf') overdraft_count = 0 for transaction in transactions: type_, amount = transaction.split() amount = int(amount) if type_ == 'credit': current_balance += amount else: # type_ == 'debit' current_balance -= amount balances.append(current_balance) if current_balance > highest_balance: highest_balance = current_balance if current_balance < lowest_balance: lowest_balance = current_balance if current_balance < 0: overdraft_count += 1 # Join balances into a space-separated string balances_str = ' '.join(map(str, balances)) return balances_str, highest_balance, lowest_balance, overdraft_count # Example usage: transactions = ["credit 100", "debit 50", "credit 200", "debit 300", "debit 100", "credit 500"] result = process_transactions(6, transactions) print(result)