Appearance
question:I have a Python library that contains a number of functions that are decorated with a custom decorator. The decorator adds some common functionality to each function, but it also takes some parameters that are used to determine which functions to call. I'd like to be able to list all the functions in the library that have this decorator, along with their corresponding decorator parameters. My first thought was to use the `inspect` module, but after playing around with it, I couldn't figure out how to get the decorator parameters. Is there a way to do this?
answer:import inspect def custom_decorator(param1, param2): def decorator(func): # Attach metadata to the function func._decorator_params = { 'param1': param1, 'param2': param2 } return func return decorator def list_decorated_functions(module): Lists all functions in the given module that have the custom decorator and returns their names along with the decorator parameters. functions_list = [] for name, obj in inspect.getmembers(module, inspect.isfunction): if hasattr(obj, '_decorator_params'): functions_list.append({ 'name': name, 'params': obj._decorator_params }) return functions_list
question:I have a list of IDs, and I want to create a new list that contains each ID exactly twice. Is there a way to do this in Python?
answer:def duplicate_ids(ids): Returns a list containing each ID from the input list exactly twice. Args: ids (list): A list of IDs. Returns: list: A new list with each ID appearing twice. return [id for id in ids for _ in range(2)]
question:# Stack Implementation Using Linked List Problem Statement Implement a stack using a linked list. The stack should have the following methods: - `push(element)`: adds an element to the top of the stack - `pop()`: removes an element from the top of the stack - `peek()`: returns the element at the top of the stack without removing it - `is_empty()`: checks if the stack is empty - `size()`: returns the number of elements in the stack Example Use Cases * Create a new stack and push elements onto it. * Pop elements from the stack. * Peek at the top element of the stack. * Check if the stack is empty. * Get the size of the stack. Code ```python class Node: Node class to represent each element in the linked list. def __init__(self, data=None): self.data = data self.next = None class Stack: Stack class implemented using a linked list. def __init__(self): self.top = None self.num_elements = 0 def push(self, element): Adds an element to the top of the stack. # Create a new node with the given element new_node = Node(element) # If the stack is empty, set the new node as the top if self.top is None: self.top = new_node # Otherwise, set the new node's next pointer to the current top and update the top else: new_node.next = self.top self.top = new_node # Increment the number of elements self.num_elements += 1 def pop(self): Removes an element from the top of the stack. # Check if the stack is empty if self.top is None: return None # Store the top element top_element = self.top.data # Update the top to the next node self.top = self.top.next # Decrement the number of elements self.num_elements -= 1 # Return the removed top element return top_element def peek(self): Returns the element at the top of the stack without removing it. # Check if the stack is empty if self.top is None: return None # Return the top element return self.top.data def is_empty(self): Checks if the stack is empty. return self.top is None def size(self): Returns the number of elements in the stack. return self.num_elements # Example usage if __name__ == "__main__": stack = Stack() # Push elements onto the stack stack.push(1) stack.push(2) stack.push(3) # Print the size of the stack print("Stack size:", stack.size()) # Output: 3 # Peek at the top element print("Top element:", stack.peek()) # Output: 3 # Pop elements from the stack print("Popped element:", stack.pop()) # Output: 3 print("Popped element:", stack.pop()) # Output: 2 # Check if the stack is empty print("Is stack empty?
answer:class Node: Node class to represent each element in the linked list. def __init__(self, data=None): self.data = data self.next = None class Stack: Stack class implemented using a linked list. def __init__(self): self.top = None self.num_elements = 0 def push(self, element): Adds an element to the top of the stack. # Create a new node with the given element new_node = Node(element) # If the stack is empty, set the new node as the top if self.top is None: self.top = new_node # Otherwise, set the new node's next pointer to the current top and update the top else: new_node.next = self.top self.top = new_node # Increment the number of elements self.num_elements += 1 def pop(self): Removes an element from the top of the stack. # Check if the stack is empty if self.top is None: return None # Store the top element top_element = self.top.data # Update the top to the next node self.top = self.top.next # Decrement the number of elements self.num_elements -= 1 # Return the removed top element return top_element def peek(self): Returns the element at the top of the stack without removing it. # Check if the stack is empty if self.top is None: return None # Return the top element return self.top.data def is_empty(self): Checks if the stack is empty. return self.top is None def size(self): Returns the number of elements in the stack. return self.num_elements
question:I have a list of items (strings) of variable length. I want to find the common prefix that is the longest among all these items. Here is my list of strings: ["flower","flow","flight"]. I can see that "fl" is the common prefix of all items. How do I write a Python function to do this?
answer:def longest_common_prefix(strs): Returns the longest common prefix string amongst an array of strings. If there is no common prefix, returns an empty string. if not strs: return "" # Sort the list, the common prefix of the first and last element will be the overall common prefix in the list strs.sort() first = strs[0] last = strs[-1] # Compare the characters of the first and last strings i = 0 while i < len(first) and i < len(last) and first[i] == last[i]: i += 1 return first[:i]