Appearance
question:You are given a string s with a length of n consisting of lowercase English letters, and an integer k. Your task is to determine if there exists a substring of length exactly k that contains at least k/2 distinct characters. If such a substring exists, find one such substring. The first line contains an integer t (1 le t le 10^4) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer n (1 le n le 10^4) — the length of the string s. The second line contains a string s of length n consisting of lowercase English letters. The third line contains an integer k (2 le k le n) — the length of the desired substring. For each test case, if a valid substring exists, output "YES" and the substring of length k with at least k/2 distinct characters. If multiple such substrings exist, output any of them. If no such substring exists, output "NO". # Example: Input: ``` 3 7 abcdefg 3 5 aaaaa 3 10 abcdeabcde 6 ``` Output: ``` YES abc NO YES abcdea ```
answer:def find_substring(t, cases): results = [] for case in cases: n, s, k = case['n'], case['s'], case['k'] found = False for i in range(n - k + 1): substring = s[i:i+k] if len(set(substring)) >= k / 2: results.append(f"YESn{substring}") found = True break if not found: results.append("NO") return results
question:In a small town, there is a tradition of a bi-annual event called the "Town Lantern Festival." During this festival, there is a lantern competition in which participants are asked to arrange their lanterns in a row. The rows are judged based on a few criteria and one of the criteria is the "Smoothness". The "Smoothness" of a row of lanterns is defined as the maximum difference in height between any two adjacent lanterns. The goal for each participant is to minimize this value to make their lantern row as smooth as possible. You are a programmer and a participant asked for your help to rearrange his lanterns to achieve the minimum possible "Smoothness." You are given a list of integers representing the heights of the lanterns in the order they are currently arranged. You can rearrange them in any order. Your task is to find the minimum possible "Smoothness". # Input The first line contains an integer n (2 ≤ n ≤ 1000) — the number of lanterns. The second line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 1000) — the heights of the lanterns. # Output Print the minimum possible "Smoothness" of the lantern row. # Example Example 1 **Input:** ``` 5 1 5 3 9 7 ``` **Output:** ``` 2 ``` **Explanation:** One possible arrangement is [1, 3, 5, 7, 9]. The maximum difference between adjacent lanterns is at most 2 (3-1=2, 5-3=2, 7-5=2, 9-7=2). Example 2 **Input:** ``` 4 8 2 6 3 ``` **Output:** ``` 1 ``` **Explanation:** One possible arrangement is [2, 3, 6, 8]. The maximum difference between adjacent lanterns is at most 1 (3-2=1, 6-3=3, 8-6=2).
answer:def min_smoothness(n, heights): Returns the minimum possible "Smoothness" of the lantern row. heights.sort() min_diff = float('inf') for i in range(n - 1): min_diff = min(min_diff, heights[i+1] - heights[i]) return min_diff # Example usage: # n = 5 # heights = [1, 5, 3, 9, 7] # print(min_smoothness(n, heights)) # Output: 2
question:You are tasked with managing the parking lot system for a large event. The parking lot has multiple rows, each containing a specific number of parking spots. Each parking spot can be either empty or occupied. Your goal is to write a program that helps attendants find the nearest available parking spot in their assigned row. The parking lot is represented by a 2D list of integers. An empty spot is denoted by 0 and an occupied spot is denoted by 1. The attendants are provided with a row number, and they need to find the index of the first available parking spot (empty spot) in that row. If the row is fully occupied, return -1. # Input - A 2D list `parking_lot` where each sublist represents a row in the parking lot. - An integer `row` indicating the specific row number (0-indexed). # Output - The index of the first available parking spot in the given row. If there are no available spots, return -1. # Example ```python parking_lot = [ [1, 0, 1, 1], [1, 1, 1, 0], [0, 0, 1, 1] ] row = 1 ``` Expected Output: ```python 3 ``` # Constraints - The number of rows in `parking_lot` would be between 1 and 100. - Each row will have between 1 and 100 parking spots. - `row` will be a valid row index within the range of the parking lot rows. Write a function `find_parking_spot(parking_lot: List[List[int]], row: int) -> int` that implements the solution.
answer:def find_parking_spot(parking_lot, row): Returns the index of the first available parking spot (0) in the given row. If no spots are available, returns -1. :param parking_lot: List[List[int]] - a 2D list representing the parking lot. :param row: int - the row number to search for an available spot. :return: int - the index of the first available spot or -1 if no spots are available. for idx, spot in enumerate(parking_lot[row]): if spot == 0: return idx return -1
question:You are given a list of integers. Perform the following manipulations: 1. Separate the list into two sublists: one containing the even-indexed elements and the other containing the odd-indexed elements (zero-based indexing). 2. Sort the even-indexed sublist in ascending order. 3. Sort the odd-indexed sublist in descending order. 4. Merge the two sorted sublists back into one list, maintaining their respective positions (original even/odd index). Output the resulting list after these manipulations. The input consists of a single line containing a list of integers separated by spaces. The length of the list is at least 2 and at most 20. Output the list which is the result of the described manipulations.
answer:def manipulate_list(lst): Separates the list into two sublists: one containing even-indexed elements and the other containing odd-indexed elements. Sorts the even-indexed sublist in ascending order and the odd-indexed sublist in descending order, and then merges them back into one list. Args: lst (list of int): The input list of integers. Returns: list of int: The manipulated list. even_indexed = [lst[i] for i in range(len(lst)) if i % 2 == 0] odd_indexed = [lst[i] for i in range(len(lst)) if i % 2 != 0] even_indexed.sort() odd_indexed.sort(reverse=True) merged_list = [] even_index, odd_index = 0, 0 for i in range(len(lst)): if i % 2 == 0: merged_list.append(even_indexed[even_index]) even_index += 1 else: merged_list.append(odd_indexed[odd_index]) odd_index += 1 return merged_list