Skip to content
🤔prompts chat🧠
🔍
question:# Coding Assessment Question **Problem Statement:** A text editor features a find-and-replace function where you can find all occurrences of a word in a given text and replace them with another word. Implementing this function involves two main operations: finding all occurrences of a word and then replacing them. This can be particularly challenging when trying to efficiently handle large texts and numerous replacements. **Objective:** Write a function `find_and_replace` that takes a list of operations where each operation involves finding and replacing words in a text. **Function Signature:** ```python def find_and_replace(operations: List[Tuple[str, str, str]]) -> List[str]: ``` **Input:** * `operations`: A list of tuples where each tuple contains: - A string representing the current text. - A string representing the word to find. - A string representing the word to replace it with. **Output:** * A list of strings where each string is the text after performing the find-and-replace operation. **Constraints:** * Each text in the operations list may contain spaces and punctuation marks. * Words will be case-sensitive. * No word will be a substring of another word (i.e., each occurrence of a word is a full word separated by non-alphanumeric characters). **Example:** ```python operations = [ ("the quick brown fox jumps over the lazy dog", "the", "a"), ("hello world, hello everyone", "hello", "hi"), ("find the needle in the haystack", "needle", "pin") ] result = find_and_replace(operations) print(result) # Output: ["a quick brown fox jumps over a lazy dog", "hi world, hi everyone", "find the pin in the haystack"] ``` **Requirements:** 1. Implement the function `find_and_replace`. 2. Ensure efficient handling and replacement within the text. 3. Handle edge cases such as punctuations, multiple spaces, and case sensitivity. 4. Thoroughly test the function with various inputs representing different edge cases.

answer:import re from typing import List, Tuple def find_and_replace(operations: List[Tuple[str, str, str]]) -> List[str]: This function takes a list of tuples where each tuple contains a string (text), a string to find, and a string to replace it with, and returns a list of strings where each string is the text after performing the find-and-replace operation. outputs = [] for text, find_word, replace_word in operations: # Using regex to find whole words pattern = r'b{}b'.format(re.escape(find_word)) replaced_text = re.sub(pattern, replace_word, text) outputs.append(replaced_text) return outputs

question:# Coding Assessment Question Graph traversal algorithms are crucial for understanding the structure and properties of graph-based systems, like social networks, transportation grids, and web crawlers. Depth-First Search (DFS) is one such algorithm that explores as far as possible along each branch before backtracking. **Objective**: Implement a recursive Depth-First Search (DFS) algorithm to traverse a graph starting from a given node. Demonstrate your solution by finding the connected components in an undirected graph. Requirements: 1. **Graph Representation**: * Represent the graph using an adjacency list. * Assume the input graph is undirected and connected. 2. **DFS Implementation**: * Define a recursive function that performs the DFS traversal. * Ensure that your implementation correctly handles edge cases (e.g., cycles and disjoint subsets of a graph). 3. **Connected Components**: * Extend your DFS function to identify all connected components in the graph. * Return the list of connected components, where each component is represented as a list of nodes. Function Signature ```python from typing import List, Dict def depth_first_search(graph: Dict[int, List[int]], start: int, visited: set) -> List[int]: pass def find_connected_components(graph: Dict[int, List[int]]) -> List[List[int]]: pass ``` Example Usage: ```python graph = { 0: [1, 2], 1: [0, 3], 2: [0, 3], 3: [1, 2, 4, 5], 4: [3], 5: [3, 6, 7], 6: [5], 7: [5] } connected_components = find_connected_components(graph) print(connected_components) # Output: [[0, 1, 3, 2, 4, 5, 6, 7]] since the graph is fully connected ``` Constraints: 1. The graph is represented as a dictionary where keys are node labels (integers) and values are lists of adjacent nodes. 2. The graph can have up to 10^3 nodes. 3. Ensure the implementation can handle graphs with cycles and small isolated components. 4. Adhere to the principles of recursion and handle large graphs efficiently in terms of time and space complexity. Context: Understandably handling graph traversal algorithms like DFS is fundamental in computer science for exploring the structure of graphs and solving problems related to connectivity and pathfinding. Implementing DFS recursively emphasizes understanding both the algorithm and recursion principles, essential for solving complex graph-related problems.

answer:from typing import List, Dict, Set def depth_first_search(graph: Dict[int, List[int]], start: int, visited: Set[int]) -> List[int]: visited.add(start) components = [start] for neighbor in graph[start]: if neighbor not in visited: components.extend(depth_first_search(graph, neighbor, visited)) return components def find_connected_components(graph: Dict[int, List[int]]) -> List[List[int]]: visited = set() connected_components = [] for node in graph: if node not in visited: component = depth_first_search(graph, node, visited) connected_components.append(component) return connected_components

question:# Data Processing and Analytics Pipeline Development You are to design and implement a data processing pipeline that ingests raw data from a CSV file, processes it to extract meaningful insights, and outputs a summarized report. You need to demonstrate your ability to handle various data processing tasks including data cleaning, transformation, and summarization using Python. Specific Requirements 1. **Input**: A CSV file containing raw data. The CSV file has columns: `timestamp`, `value`, `category`. 2. **Output**: A summarized report in the form of a dictionary with the following structure: * `total_entries`: Total number of data entries. * `average_value`: Average of the `value` column. * `max_value`: Maximum value in the `value` column and its associated timestamp. * `min_value`: Minimum value in the `value` column and its associated timestamp. * `entries_per_category`: A dictionary with categories as keys and the count of entries per category as values. 3. **Constraints**: * The `timestamp` column should be parsed as datetime objects. * Handle missing or malformed data appropriately. * Ensure the results are accurate and efficiently calculated. Performance Requirements * The script should handle large CSV files efficiently. * Use appropriate data structures and libraries (e.g., pandas) to optimize performance. Scenario Your organization has accumulated a large dataset in a CSV format. You are tasked with developing a robust data processing pipeline to transform this raw data into a summarized report for decision-making purposes. You are tasked with implementing the following function: ```python def process_data(file_path: str) -> dict: Given the file path to a CSV file containing raw data, process the data and return a summarized report. Parameters: file_path (str): The path to the CSV file. Returns: dict: A dictionary containing the summarized report. pass ``` **Example Usage:** ```python report = process_data("data.csv") print(report) # Output: # { # 'total_entries': 1000, # 'average_value': 50.5, # 'max_value': {'timestamp': '2023-01-01 12:00:00', 'value': 100.0}, # 'min_value': {'timestamp': '2023-01-01 12:00:00', 'value': 0.0}, # 'entries_per_category': {'A': 300, 'B': 500, 'C': 200} # } ``` Ensure your solution handles the CSV file appropriately, processes the data efficiently, and returns the summarized report accurately.

answer:import pandas as pd def process_data(file_path: str) -> dict: Given the file path to a CSV file containing raw data, process the data and return a summarized report. Parameters: file_path (str): The path to the CSV file. Returns: dict: A dictionary containing the summarized report. # Read the CSV file data = pd.read_csv(file_path, parse_dates=['timestamp']) # Drop rows with missing or malformed data data.dropna(subset=['timestamp', 'value', 'category'], inplace=True) # Total number of data entries total_entries = data.shape[0] # Average of the `value` column average_value = data['value'].mean() # Maximum value in the `value` column and its associated timestamp max_row = data.loc[data['value'].idxmax()] max_value = {'timestamp': max_row['timestamp'], 'value': max_row['value']} # Minimum value in the `value` column and its associated timestamp min_row = data.loc[data['value'].idxmin()] min_value = {'timestamp': min_row['timestamp'], 'value': min_row['value']} # Count of entries per category entries_per_category = data['category'].value_counts().to_dict() # Return the summarized report return { 'total_entries': total_entries, 'average_value': average_value, 'max_value': max_value, 'min_value': min_value, 'entries_per_category': entries_per_category }

question:# Sum of Powers Given a positive integer `n`, your task is to determine whether `n` can be expressed as the sum of two distinct integers (a) and (b), where both (a) and (b) are powers of 2. Return a list containing the tuple ((a, b)), or an empty list if no such pair exists. The integers (a) and (b) must satisfy (a < b). Input: - A single integer `n` (1 ≤ n ≤ 10^6). Output: - A list containing a single tuple ((a, b)) such that (a) and (b) are powers of 2 and (a + b = n). Return an empty list if no such pair exists. Constraints: - Both (a) and (b) must be distinct powers of 2. - Ensure the solution checks possible pairs efficiently. Example: ```python >>> find_sum_of_powers(10) [(2, 8)] >>> find_sum_of_powers(18) [(2, 16)] >>> find_sum_of_powers(15) [] ``` > **Scenario**: > Your younger sibling is learning about powers of 2 in their math class and wants to practice recognizing which numbers can be represented as the sum of two powers of 2. Assist them by implementing a function that helps identify these pairs. Function Signature ```python def find_sum_of_powers(n: int) -> list: pass ```

answer:def find_sum_of_powers(n: int) -> list: Determines whether n can be expressed as the sum of two distinct integers a and b, where both a and b are powers of 2. Parameters: n (int): A positive integer to be represented as a sum of two distinct powers of 2. Returns: list: A list containing a single tuple (a, b) such that a and b are distinct powers of 2 and a + b = n, or an empty list if no such pair exists. # Generate powers of 2 that are less than n powers_of_2 = [] power = 1 while power < n: powers_of_2.append(power) power *= 2 # Check pairs of powers of 2 to see if their sum equals n power_set = set(powers_of_2) for a in powers_of_2: if n - a in power_set and n - a != a: return [(a, n - a)] return []

Released under the chat License.

has loaded