Appearance
question:You are given an array A of N distinct integers. Define the distance between two integers A[i] and A[j] as abs(A[i] - A[j]), where 0 ≤ i < j < N. You are required to find two integers in the array such that their distance is maximum, as well as two integers such that their distance is minimum. If multiple pairs have the same maximum or minimum distance, any pair can be returned. ------ Input Format ------ - The first line contains a single integer T, the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains a single integer N, the number of elements in array A. - The second line of each test case contains N space-separated integers denoting the elements of the array A. ------ Output Format ------ For each test case, output two lines: - The first line should contain the two integers that form the pair with the maximum distance in the array. - The second line should contain the two integers that form the pair with the minimum distance in the array. If multiple pairs have the same maximum or minimum distance, any such pair can be printed. ------ Constraints ------ 1 ≤ T ≤ 100 2 ≤ N ≤ 10^{5} -10^{5} ≤ A_{i} ≤ 10^{5} ------ Sample Input 1 ------ 2 6 7 1 3 10 5 2 3 1 2 3 ------ Sample Output 1 ------ 1 10 7 5 1 3 2 1 ------ Explanation ------ Test case 1: The pair with the maximum distance is (1, 10) with a distance of abs(1 - 10) = 9. The pair with the minimum distance is (7, 5) with a distance of abs(7 - 5) = 2. Test case 2: The pair with the maximum distance is (1, 3) with a distance of abs(1 - 3) = 2. The pair with the minimum distance is (2, 1) with a distance of abs(2 - 1) = 1.
answer:def find_max_min_distance_pairs(T, test_cases): results = [] for t in range(T): N = test_cases[t][0] A = test_cases[t][1] A_sorted = sorted(A) max_dist_pair = (A_sorted[0], A_sorted[-1]) max_distance = abs(A_sorted[0] - A_sorted[-1]) min_distance = float('inf') min_dist_pair = None for i in range(1, N): dist = abs(A_sorted[i] - A_sorted[i-1]) if dist < min_distance: min_distance = dist min_dist_pair = (A_sorted[i-1], A_sorted[i]) results.append((max_dist_pair, min_dist_pair)) return results # Example usage: # T = 2 # test_cases = [ # (6, [7, 1, 3, 10, 5, 2]), # (3, [1, 2, 3]) # ] # print(find_max_min_distance_pairs(T, test_cases)) # Should output: [((1, 10), (1, 2)), ((1, 3), (2, 1))]
question:Developer Dan is creating a game where he needs to analyze areas of rectangles for game levels. Given n rectangles, Dan wants to calculate the area for each rectangle based on provided width and height. Can you help Dan with this task? -----Input:----- - First-line will contain n, the number of rectangles. Then the rectangles follow. - Each rectangle is defined by two integers: width and height. -----Output:----- For each rectangle, output the area in a single line. -----Constraints----- - 1 leq n leq 1000000 - 1 leq width, height leq 1000 -----Sample Input:----- 3 5 4 7 2 6 6 -----Sample Output:----- 20 14 36 -----EXPLANATION:----- The areas of the rectangles are calculated by multiplying the width by the height for each given rectangle.
answer:def calculate_areas(rectangles): Given a list of tuples representing rectangles, where each tuple contains the width and height of the rectangles, this function returns a list of their areas. Parameters: rectangles (list of tuples): List of tuples where each tuple contains two integers (width, height) Returns: list of int: List containing the area of each rectangle areas = [] for width, height in rectangles: areas.append(width * height) return areas
question:A robotics company is designing a delivery system where autonomous robots deliver packages within a building. The building consists of a series of rooms connected by hallways. The number of rooms is represented by N, and the hallways are represented as a matrix where the element at the i-th row and j-th column denotes the time it takes to traverse from room i to room j. If there is no direct hallway between room i and room j, the time is represented as a large integer value (for example, 1000000). Your task is to find the shortest time a robot needs to deliver a package from a given starting room (S) to a target room (T). You need to write a program that inputs the number of rooms, the hallway matrix, the starting room, and the target room, and then outputs the shortest delivery time. For simplicity, number the rooms from 0 to N-1. Input The input consists of multiple datasets. The end of the input is indicated by a line containing a single minus one (`-1`). Each dataset starts with an integer N (2 ≤ N ≤ 20), the number of rooms. The next N lines contain N integers each, forming the hallway matrix. The subsequent line contains two integers, S (starting room) and T (target room). Output For each dataset, print the shortest delivery time from room S to room T on one line. If delivery is impossible, print "Impossible". Example Input 3 0 2 1000000 2 0 3 1000000 3 0 0 2 4 0 3 1000000 7 3 0 1 8 1000000 1 0 1000000 7 8 1000000 0 0 3 -1 Output 5 7
answer:def floyd_warshall(matrix): Implements the Floyd-Warshall algorithm to find the shortest paths between all pairs of nodes. Parameters: matrix (list of list of int): The adjacency matrix representing the time between rooms. Returns: list of list of int: The matrix of the shortest paths between all pairs of nodes. N = len(matrix) dist = [row[:] for row in matrix] # Initialize the distance matrix for k in range(N): for i in range(N): for j in range(N): if dist[i][j] > dist[i][k] + dist[k][j]: dist[i][j] = dist[i][k] + dist[k][j] return dist def find_shortest_delivery(N, matrix, S, T): shortest_paths = floyd_warshall(matrix) return shortest_paths[S][T] if shortest_paths[S][T] < 1000000 else "Impossible" def process_input_data(input_data): results = [] idx = 0 while idx < len(input_data): if input_data[idx] == '-1': break N = int(input_data[idx]) idx += 1 matrix = [] for _ in range(N): matrix.append(list(map(int, input_data[idx].split()))) idx += 1 S, T = map(int, input_data[idx].split()) idx += 1 result = find_shortest_delivery(N, matrix, S, T) results.append(result) return results # Example usage: # input_data = [ # "3", # "0 2 1000000", # "2 0 3", # "1000000 3 0", # "0 2", # "4", # "0 3 1000000 7", # "3 0 1 8", # "1000000 1 0 1000000", # "7 8 1000000 0", # "0 3", # "-1" # ] # results = process_input_data(input_data) # for result in results: # print(result)
question:Fiona is working on a text editor and she wants to implement a feature to auto-correct a document containing multiple sentences. The auto-correction involves two operations: converting all characters in a sentence to lowercase and capitalizing the first letter of the sentence. Each sentence ends with a period ('.') and sentences are separated by a single space. -----Input----- The first line of input contains a string S (1 ≤ |S| ≤ 10^6) representing the document. -----Output----- Output the corrected document such that each sentence is properly formatted with lowercase letters, and the first letter of each sentence is capitalized. -----Example----- Input: "hello world. this is an example. can you fix it?" Output: "Hello world. This is an example. Can you fix it?" -----Explanation----- Example case 1: The sentences "hello world.", "this is an example.", and "can you fix it?" are each corrected by making all characters lowercase and then capitalizing the first letter.
answer:def auto_correct_document(document): Auto-corrects a document by converting all characters to lowercase and capitalizing the first letter of each sentence. Sentences are separated by a single space and end with a period. Args: document (str): The input document string. Returns: str: The auto-corrected document. sentences = document.lower().split('. ') corrected_sentences = [sentence.capitalize() for sentence in sentences] corrected_document = '. '.join(corrected_sentences) return corrected_document