Appearance
question:A large square field of size n × n is divided into a grid of 1 × 1 cells. The cell at the top-left corner of the field has coordinates (1, 1) and the cell at the bottom-right corner has coordinates (n, n). In this square field, there are k obstacles each positioned at unique coordinates (x_i, y_i) (1 ≤ x_i, y_i ≤ n), and there is a drone starting at (1, 1). The drone wants to reach the bottom-right corner of the field (n, n). Each time the drone tries to move it can move: - Right (increase the y coordinate by 1), - Down (increase the x coordinate by 1) However, the drone cannot move onto a cell that has an obstacle. You need to determine whether it is possible for the drone to reach (n, n) from (1, 1) without hitting any obstacle. Input The first line of input contains two integers n and k (2 ≤ n ≤ 100, 0 ≤ k ≤ n*n - 1) — the size of the field and the number of obstacles. The next k lines each contain two integers x_i and y_i (1 ≤ x_i, y_i ≤ n), indicating the coordinates of an obstacle. Output If it is possible for the drone to reach (n, 1) from (1, 1) print "YES", otherwise print "NO". Examples Input 4 3 2 2 3 3 4 2 Output YES Input 3 3 2 1 2 2 2 3 Output NO Input 3 1 3 2 Output YES
answer:def can_reach_destination(n, k, obstacles): from collections import deque # Initialize the grid grid = [[True] * n for _ in range(n)] for x, y in obstacles: grid[x - 1][y - 1] = False # BFS to find path from (1,1) to (n,n) directions = [(0, 1), (1, 0)] queue = deque([(0, 0)]) visited = set([(0, 0)]) while queue: x, y = queue.popleft() if (x, y) == (n - 1, n - 1): return "YES" for dx, dy in directions: nx, ny = x + dx, y + dy if 0 <= nx < n and 0 <= ny < n and grid[nx][ny] and (nx, ny) not in visited: queue.append((nx, ny)) visited.add((nx, ny)) return "NO" # Example Usage # n = 3, k = 3 # obstacles = [(2, 1), (2, 2), (2, 3)] # print(can_reach_destination(n, k, obstacles)) # Should return "NO"
question:Create a program that helps a landowner manage the irrigation of her rectangular field. The irrigation system can water a rectangular sub-area of the field, with its sides parallel to the field's sides. However, the sub-area can be placed anywhere within the whole field, including along the borders. Given the dimensions of the field and the desired amount of water to use, the landowner wants to know the maximum depth of water that can be achieved if the irrigation area is optimized. The field is defined by its length L and width W, and the irrigation system can cover any rectangular sub-area of the field with sides parallel to the main field's sides. You are given the total volume of water V to be used for irrigation. Your task is to write a program that calculates the maximum possible depth of water (in units) that can be achieved if the sub-area is adjusted optimally. Input: - A sequence of multiple datasets. - The end of the input is indicated by three zeroes on a single line. - Each dataset consists of: - The first line with three integers L, W, and V (1 ≤ L, W ≤ 1000; 1 ≤ V ≤ 1000000) representing the length, width, and volume of water, respectively. Output: - For each dataset, output the maximum depth of water that the irrigation sub-area can have, with precision up to 6 decimal places. Example: Input: 10 5 50 20 10 5000 0 0 0 Output: 10.000000 25.000000 Explanation: For the first dataset, the total water volume is 50. The optimal sub-area is the entire field (10x5), and the depth is 50 / (10 * 5) = 1.000000 units. For the second dataset, the total water volume is 5000. The optimal sub-area can still be the entire field (20x10), and the depth is 5000 / (20 * 10) = 25.000000 units. Consider all configurations where the sub-area dimensions are optimized to maximize the depth of water within the given constraints.
answer:def max_water_depth(L, W, V): Calculate the maximum depth of water that can be achieved if the sub-area is adjusted optimally. Parameters: L (int): Length of the field. W (int): Width of the field. V (int): Volume of water to be used for irrigation. Returns: float: Maximum depth of water achievable with precision up to 6 decimal places. return round(V / (L * W), 6) def process_datasets(datasets): Process multiple datasets to calculate the maximum water depth for each dataset. Parameters: datasets (list of tuple): List of tuples, where each tuple contains (L, W, V). Returns: list of float: List of maximum water depths for each dataset. results = [] for L, W, V in datasets: if L == 0 and W == 0 and V == 0: break results.append(max_water_depth(L, W, V)) return results
question:Write a function that takes a positive integer n and returns a list of all prime numbers less than or equal to n. Constraints * 1 leq n leq 10^6 Input The input is given as a single integer n. Output Return a list of all prime numbers less than or equal to n in ascending order. Example Input 10 Output [2, 3, 5, 7] Input 20 Output [2, 3, 5, 7, 11, 13, 17, 19] Explanation For the input n = 10, the prime numbers less than or equal to 10 are 2, 3, 5, and 7. For the input n = 20, the prime numbers less than or equal to 20 are 2, 3, 5, 7, 11, 13, 17, and 19.
answer:def sieve_of_eratosthenes(n): Returns a list of all prime numbers less than or equal to n. if n < 2: return [] is_prime = [True] * (n + 1) is_prime[0] = is_prime[1] = False p = 2 while p * p <= n: if is_prime[p]: for i in range(p * p, n + 1, p): is_prime[i] = False p += 1 return [p for p in range(2, n + 1) if is_prime[p]] # Example usage: # result = sieve_of_eratosthenes(20) # print(result) # Output: [2, 3, 5, 7, 11, 13, 17, 19]
question:# BACKGROUND: Emma loves to read and recently, she has joined an online reading club where the club keeps track of each member's reading habits and recommends books based on their preferences. Each member has their own set of reading logs that record the time they spent reading each book. Emma wants a program that can process these logs and provide her with a summary of her reading habits. # TECHNICAL DETAILS: Emma’s reading log is stored in a list of dictionaries, where each dictionary represents a single reading session. Each dictionary contains the name of the book, the number of pages read, and the amount of time spent (in hours) during that session. Emma wants to know the total number of books read, the total pages read, the total reading time, and the average reading speed (pages per hour). # TASK: Write a function that accepts a list of reading session logs. Each log is a dictionary with the following keys: ``` - 'book' : a string representing the name of the book, - 'pages' : an integer representing the number of pages read in that session, - 'time' : a float representing the time spent reading in hours. ``` The function should return a dictionary containing: ``` - 'total_books' : the total number of unique books read, - 'total_pages' : the total number of pages read, - 'total_time' : the total amount of time spent reading, - 'avg_speed' : the average reading speed in pages per hour (rounded to the nearest tenth). ``` # EXAMPLE: Suppose Emma reads according to the following logs: ``` logs = [ {'book': 'Book A', 'pages': 150, 'time': 2.5}, {'book': 'Book B', 'pages': 200, 'time': 3.0}, {'book': 'Book A', 'pages': 100, 'time': 1.5}, {'book': 'Book C', 'pages': 300, 'time': 5.0} ] ``` The function call to process these logs would look like: `summarize_reading(logs)` and its output should be: ``` { 'total_books': 3, 'total_pages': 750, 'total_time': 12.0, 'avg_speed': 62.5 } ``` # NOTES: - If the input list is empty, the function should return zeros for all values. - Ensure to handle divisions by zero where necessary. For example, if the total time is zero, then the average reading speed should also be zero.
answer:def summarize_reading(logs): Summarizes the reading habits from the logs. Args: logs (list): List of dictionaries with each dictionary containing 'book' (str), 'pages' (int), and 'time' (float). Returns: dict: A dictionary with summary of total books, total pages, total time, and average speed. if not logs: return { 'total_books': 0, 'total_pages': 0, 'total_time': 0.0, 'avg_speed': 0.0 } unique_books = set() total_pages = 0 total_time = 0.0 for log in logs: unique_books.add(log['book']) total_pages += log['pages'] total_time += log['time'] total_books = len(unique_books) avg_speed = total_pages / total_time if total_time > 0 else 0 return { 'total_books': total_books, 'total_pages': total_pages, 'total_time': round(total_time, 1), 'avg_speed': round(avg_speed, 1) }