Appearance
question:def classify_population_trends(population_data): Classify the population trends for each village. Parameters: population_data (List[List[int]]): A list of lists where each inner list contains population estimates for a village in chronological order. Returns: List[str]: A list of strings indicating the population trend for each village. Each string will be one of "increasing", "decreasing", "stable", or "fluctuating". >>> classify_population_trends([[100, 101, 102, 103], [200, 199, 198], [150, 150, 150], [120, 130, 125]]) ['increasing', 'decreasing', 'stable', 'fluctuating'] >>> classify_population_trends([[1, 2, 3, 4, 5]]) ['increasing'] >>> classify_population_trends([[5, 4, 3, 2, 1]]) ['decreasing'] >>> classify_population_trends([[10, 10, 10, 10, 10]]) ['stable'] >>> classify_population_trends([[3, 5, 4, 6, 2]]) ['fluctuating']
answer:def classify_population_trends(population_data): trends = [] for population in population_data: if all(population[i] == population[i+1] for i in range(len(population) - 1)): trends.append("stable") elif all(population[i] <= population[i+1] for i in range(len(population) - 1)): trends.append("increasing") elif all(population[i] >= population[i+1] for i in range(len(population) - 1)): trends.append("decreasing") else: trends.append("fluctuating") return trends
question:def max_height_difference(heights: List[int]) -> int: Finds the maximum height difference between any two non-adjacent buildings. Parameters: heights (list): List of building heights. Returns: int: The maximum height difference. >>> max_height_difference([1, 8, 3, 2, 7, 6]) 7 >>> max_height_difference([5, 5, 5, 5, 5]) 0 >>> max_height_difference([1, 3, 2, 10, 6]) 9 >>> max_height_difference([10**9, 1, 500, 300, 700, 10**6]) 999999999 >>> max_height_difference([1, 3, 1, 3, 1, 3]) 2
answer:def max_height_difference(heights): Finds the maximum height difference between any two non-adjacent buildings. Parameters: heights (list): List of building heights. Returns: int: The maximum height difference. # Find the maximum and minimum heights in the list max_height = max(heights) min_height = min(heights) # The result is the max difference between the max and min heights return max_height - min_height
question:from collections import Counter def can_form_palindrome(s): Determines if the characters of the string s can be rearranged to form a palindrome. :param s: str, input string :return: str, "YES" if string can be rearranged to form a palindrome, otherwise "NO" counts = Counter(s) odd_counts = sum(1 for count in counts.values() if count % 2 != 0) return "YES" if odd_counts <= 1 else "NO" def process_test_cases(t, test_cases): Processes multiple test cases to determine if each string can be rearranged to form a palindrome. :param t: int, number of test cases :param test_cases: list of str, each string to be tested :return: list of str, each element is "YES" or "NO" for corresponding string in test_cases results = [] for s in test_cases: results.append(can_form_palindrome(s)) return results # Example usage: # Determine if the characters of the strings can be rearranged to form palindromes t = 4 test_cases = ["aabb", "abc", "racecar", "abcdcba"] results = process_test_cases(t, test_cases) print(results) # Output: ["YES", "NO", "YES", "YES"]
answer:from collections import Counter def can_form_palindrome(s): Determines if the characters of the string s can be rearranged to form a palindrome. :param s: str, input string :return: str, "YES" if string can be rearranged to form a palindrome, otherwise "NO" counts = Counter(s) odd_counts = sum(1 for count in counts.values() if count % 2 != 0) return "YES" if odd_counts <= 1 else "NO" def process_test_cases(t, test_cases): Processes multiple test cases to determine if each string can be rearranged to form a palindrome. :param t: int, number of test cases :param test_cases: list of str, each string to be tested :return: list of str, each element is "YES" or "NO" for corresponding string in test_cases results = [] for s in test_cases: results.append(can_form_palindrome(s)) return results
question:from typing import List, Tuple def longest_common_span_seq(s: str, t: str) -> int: Find the length of the longest common spanning sequence (LCSS) of two given strings. >>> longest_common_span_seq("abcd", "abc") == 3 >>> longest_common_span_seq("a", "a") == 1 >>> longest_common_span_seq("abcde", "ace") == 3 >>> longest_common_span_seq("abc", "def") == 0 >>> longest_common_span_seq("xyzz", "xyzzy") == 4 def process_queries(grid: List[str], queries: List[Tuple[str, str]]) -> List[int]: Process the list of queries to find the LCSS for each query. def main(n: int, m: int, grid: List[str], q: int, queries: List[Tuple[str, str]]) -> List[int]: Main function to process the input and output the results for each query. def test_longest_common_span_seq(): assert longest_common_span_seq("abcd", "abc") == 3 assert longest_common_span_seq("a", "a") == 1 assert longest_common_span_seq("abcde", "ace") == 3 assert longest_common_span_seq("abc", "def") == 0 assert longest_common_span_seq("xyzz", "xyzzy") == 4 def test_process_queries(): queries = [("abcd", "abc"), ("a", "a")] assert process_queries([], queries) == [3, 1] queries = [("abcde", "ace"), ("xyz", "yxz")] assert process_queries([], queries) == [3, 2] def test_main(): n, m = 2, 2 grid = ["ab", "cd"] q = 2 queries = [("abcd", "abc"), ("a", "a")] assert main(n, m, grid, q, queries) == [3, 1] n, m = 3, 3 grid = ["abc", "def", "ghi"] q = 1 queries = [("abcdef", "aced")] assert main(n, m, grid, q, queries) == [3]
answer:def longest_common_span_seq(s, t): ns, nt = len(s), len(t) dp = [[0] * (nt + 1) for _ in range(ns + 1)] for i in range(1, ns + 1): for j in range(1, nt + 1): if s[i - 1] == t[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp[ns][nt] def process_queries(grid, queries): results = [] for s, t in queries: result = longest_common_span_seq(s, t) results.append(result) return results def main(n, m, grid, q, queries): results = process_queries(grid, queries) return results