Appearance
question:Design a function to manage an inventory of items in a warehouse. The inventory should be represented as a list of dictionaries, where each dictionary contains 'item_id', 'name', 'quantity', and 'price'. Implement the following functionalities: 1. Add a new item to the inventory. Ensure that 'item_id' is unique and handle the case where an item with the same 'item_id' already exists. 2. Remove an item from the inventory based on 'item_id'. Handle the case where the 'item_id' does not exist. 3. Update the quantity and price of an existing item based on 'item_id'. Handle the case where the 'item_id' does not exist. 4. Search for an item based on 'item_id' or 'name'. Return all matching items. Your function should properly manage and maintain the inventory while providing appropriate error messages for the above cases.
answer:class InventoryItem: def __init__(self, item_id, name, quantity, price): self.item_id = item_id self.name = name self.quantity = quantity self.price = price class WarehouseInventory: def __init__(self): self.inventory = [] def add_item(self, item_id, name, quantity, price): for item in self.inventory: if item.item_id == item_id: return "Error: Item with this ID already exists." new_item = InventoryItem(item_id, name, quantity, price) self.inventory.append(new_item) return "Item added successfully." def remove_item(self, item_id): for item in self.inventory: if item.item_id == item_id: self.inventory.remove(item) return "Item removed successfully." return "Error: Item ID does not exist." def update_item(self, item_id, quantity, price): for item in self.inventory: if item.item_id == item_id: item.quantity = quantity item.price = price return "Item updated successfully." return "Error: Item ID does not exist." def search_item(self, **kwargs): search_results = [] for item in self.inventory: for key, value in kwargs.items(): if hasattr(item, key) and getattr(item, key) == value: search_results.append(item) return search_results
question:Write a Python function `find_longest_word` that takes a list of strings as an input and returns the longest string from the list. If there are multiple strings with the same maximum length, return the first one that appears in the list. If the input list is empty, return an empty string. Example: ```python print(find_longest_word(["apple", "banana", "cherry", "date"])) # Output: "banana" print(find_longest_word([])) # Output: "" ```
answer:def find_longest_word(words): Returns the longest word from the list of words. If multiple words have the same maximum length, the first one is returned. If the input list is empty, an empty string is returned. if not words: return "" longest_word = words[0] for word in words[1:]: if len(word) > len(longest_word): longest_word = word return longest_word
question:Design and implement a function that uses NumPy to generate an m x n matrix, where each element is the sum of its row and column indices. Example: ``` Input: m = 3, n = 4 Output: [[0, 1, 2, 3], [1, 2, 3, 4], [2, 3, 4, 5]] ``` import numpy as np def generate_matrix(m, n): matrix = np.zeros((m,n), dtype=int) for i in range(m): for j in range(n): matrix[i, j] = i + j return matrix # Test the function print(generate_matrix(3, 4))
answer:import numpy as np def generate_matrix(m, n): Generates an m x n matrix where each element is the sum of its row and column indices. Parameters: m (int): Number of rows n (int): Number of columns Returns: np.ndarray: Generated matrix matrix = np.zeros((m, n), dtype=int) for i in range(m): for j in range(n): matrix[i, j] = i + j return matrix
question:Create a function that takes a string containing only lowercase alphabets and returns the length of the longest substring that contains no repeated characters. Your solution should have a time complexity of O(n). Consider edge cases where the input string may be empty or have all identical characters.
answer:def longest_unique_substring(s): Returns the length of the longest substring that contains no repeated characters. n = len(s) if n == 0: return 0 char_index_map = {} max_length = 0 start = 0 for end in range(n): if s[end] in char_index_map: start = max(start, char_index_map[s[end]] + 1) char_index_map[s[end]] = end max_length = max(max_length, end - start + 1) return max_length