Skip to content
🤔prompts chat🧠
🔍
question:def manage_inventory(input_data: List[str]) -> List[Tuple[str, int]]: Simulates the inventory changes handled via several operations in a supermarket inventory system. Args: - input_data: List of strings containing initial inventory statuses and operations. Returns: - List of tuples containing final inventory statuses sorted by item IDs. >>> manage_inventory([ "a1 10", "b2 5", "c3 0", "END_OF_INITIAL_INVENTORY", "ADD a1 5", "REMOVE b2 3", "SET c3 10", "DELETE b2", "ADD d4 12", "END_OF_OPERATIONS", "END" ]) [("a1 15", "c3 10", "d4 12")] pass def format_inventory(inventory: List[Tuple[str, int]]) -> List[str]: Formats the inventory list to match the specified output format. Args: - inventory: List of tuples containing item IDs and their quantities. Returns: - List of strings where each string is formatted as "id quantity". >>> format_inventory([("a1", 15), ("c3", 10), ("d4", 12)]) ["a1 15", "c3 10", "d4 12"] pass # Unit Test Cases import pytest from solution import manage_inventory, format_inventory def test_example_case(): input_data = [ "a1 10", "b2 5", "c3 0", "END_OF_INITIAL_INVENTORY", "ADD a1 5", "REMOVE b2 3", "SET c3 10", "DELETE b2", "ADD d4 12", "END_OF_OPERATIONS", "END" ] result = manage_inventory(input_data) assert format_inventory(result) == ["a1 15", "c3 10", "d4 12"] def test_empty_inventory(): input_data = [ "END_OF_INITIAL_INVENTORY", "END_OF_OPERATIONS", "END" ] result = manage_inventory(input_data) assert format_inventory(result) == [] def test_adding_new_item(): input_data = [ "a1 10", "END_OF_INITIAL_INVENTORY", "ADD b2 7", "END_OF_OPERATIONS", "END" ] result = manage_inventory(input_data) assert format_inventory(result) == ["a1 10", "b2 7"] def test_removing_item_to_zero(): input_data = [ "a1 10", "END_OF_INITIAL_INVENTORY", "REMOVE a1 10", "END_OF_OPERATIONS", "END" ] result = manage_inventory(input_data) assert format_inventory(result) == [] def test_setting_item_to_zero(): input_data = [ "a1 10", "END_OF_INITIAL_INVENTORY", "SET a1 0", "END_OF_OPERATIONS", "END" ] result = manage_inventory(input_data) assert format_inventory(result) == [] def test_no_operations(): input_data = [ "a1 10", "b2 5", "END_OF_INITIAL_INVENTORY", "END_OF_OPERATIONS", "END" ] result = manage_inventory(input_data) assert format_inventory(result) == ["a1 10", "b2 5"] def test_multiple_operations_on_same_item(): input_data = [ "a1 10", "END_OF_INITIAL_INVENTORY", "ADD a1 5", "REMOVE a1 3", "SET a1 20", "DELETE a1", "ADD a1 8", "END_OF_OPERATIONS", "END" ] result = manage_inventory(input_data) assert format_inventory(result) == ["a1 8"]

answer:def manage_inventory(input_data): inventory = {} processing_initial_inventory = True processing_operations = False for line in input_data: line = line.strip() if line == "END_OF_INITIAL_INVENTORY": processing_initial_inventory = False processing_operations = True continue elif line == "END_OF_OPERATIONS": processing_operations = False continue elif line == "END": break if processing_initial_inventory: item_id, quantity = line.split() inventory[item_id] = int(quantity) elif processing_operations: parts = line.split() command = parts[0] if command == "ADD": item_id, quantity = parts[1], int(parts[2]) if item_id in inventory: inventory[item_id] += quantity else: inventory[item_id] = quantity elif command == "REMOVE": item_id, quantity = parts[1], int(parts[2]) if item_id in inventory: inventory[item_id] -= quantity if inventory[item_id] <= 0: del inventory[item_id] elif command == "SET": item_id, quantity = parts[1], int(parts[2]) if quantity > 0: inventory[item_id] = quantity else: if item_id in inventory: del inventory[item_id] elif command == "DELETE": item_id = parts[1] if item_id in inventory: del inventory[item_id] sorted_inventory = sorted(inventory.items()) return sorted_inventory def format_inventory(inventory): return [f"{item_id} {quantity}" for item_id, quantity in inventory]

question:def number_of_subsets(test_cases: List[int]) -> List[int]: Given a list of integers representing the number of distinct items for each test case, return a list of integers where each integer represents the number of subsets that can be formed from the respective number of distinct items. >>> number_of_subsets([3, 4, 1]) [8, 16, 2] >>> number_of_subsets([0]) [1] >>> number_of_subsets([2]) [4] >>> number_of_subsets([50]) [1125899906842624] >>> number_of_subsets([5, 6, 7]) [32, 64, 128] >>> number_of_subsets([0, 1, 2, 10, 60]) [1, 2, 4, 1024, 1152921504606846976]

answer:def number_of_subsets(test_cases): Given a list of integers representing the number of distinct items for each test case, return a list of integers where each integer represents the number of subsets that can be formed from the respective number of distinct items. results = [] for N in test_cases: # The number of subsets of a set with N elements is 2^N results.append(2 ** N) return results

question:def calculate_final_inventory(s: int, initial_inventory: List[int], m: int, moves: List[Tuple[int, int, int]]) -> List[int]: Calculate the final inventory of items in each sector after a series of moves. :param s: Number of sectors. :param initial_inventory: List of initial inventory in each sector. :param m: Number of moves recorded. :param moves: List of moves where each move is a tuple (u, v, k) representing `k` items moved from sector `u` to sector `v`. :return: List of final inventory in each sector. >>> calculate_final_inventory(5, [10, 20, 30, 40, 50], 3, [(1, 2, 5), (2, 3, 10), (5, 4, 15)]) [5, 15, 40, 55, 35] >>> calculate_final_inventory(3, [15, 25, 35], 0, []) [15, 25, 35] >>> calculate_final_inventory(4, [10, 20, 30, 40], 1, [(1, 1, 10)]) [10, 20, 30, 40]

answer:def calculate_final_inventory(s, initial_inventory, m, moves): Calculate the final inventory of items in each sector after a series of moves. :param s: Number of sectors. :param initial_inventory: List of initial inventory in each sector. :param m: Number of moves recorded. :param moves: List of moves where each move is a tuple (u, v, k) representing `k` items moved from sector `u` to sector `v`. :return: List of final inventory in each sector. # Adjust the moves for zero-based indexing for move in moves: u, v, k = move[0] - 1, move[1] - 1, move[2] initial_inventory[u] -= k initial_inventory[v] += k return initial_inventory

question:def tree_heights(initial_heights: List[int], growth_rates: List[int], D: int) -> List[int]: Calculate the height of each tree after D days. :param initial_heights: List of initial heights of the trees. :param growth_rates: List of daily growth rates of the trees. :param D: Number of days. :return: List of heights of the trees after D days. >>> tree_heights([2, 3, 5], [1, 2, 1], 10) [12, 23, 15] >>> tree_heights([1, 2, 3], [0, 0, 0], 10) [1, 2, 3] >>> tree_heights([10, 20, 30], [5, 5, 5], 0) [10, 20, 30] >>> tree_heights([1, 1, 1], [10000, 20000, 30000], 1) [10001, 20001, 30001] >>> tree_heights([1, 2, 3], [1, 1, 1], 1000000) [1000001, 1000002, 1000003]

answer:def tree_heights(initial_heights, growth_rates, D): Calculate the height of each tree after D days. :param initial_heights: List of initial heights of the trees. :param growth_rates: List of daily growth rates of the trees. :param D: Number of days. :return: List of heights of the trees after D days. # Compute final heights by adding the growth over D days to the initial heights return [initial_heights[i] + growth_rates[i] * D for i in range(len(initial_heights))]

Released under the chat License.

has loaded