Skip to content
🤔prompts chat🧠
🔍
question:Administrators need your assistance in managing a university's online examination system. You need to create solutions for two tasks. First, write a function that, given an array of students' scores and the passing score, returns an array of booleans where each boolean indicates whether the corresponding student has passed or not. Second, write a function that, given an array of students' scores, returns an object containing: 1. The highest score `highest`. 2. The lowest score `lowest`. 3. The average score rounded to 2 decimals `average`. 4. The total number of students who scored above average `aboveAverageCount`. Ensure that both functions handle edge cases such as empty arrays or all students having the same score.

answer:def did_students_pass(scores, passing_score): Given an array of students' scores and the passing score, returns an array of booleans indicating whether each student has passed. Parameters: scores (list): List of students' scores (integers). passing_score (int): The passing score. Returns: list: List of booleans indicating pass (True) or fail (False) for each student. return [score >= passing_score for score in scores] def score_statistics(scores): Given an array of students' scores, returns an object containing: - The highest score `highest`. - The lowest score `lowest`. - The average score rounded to 2 decimals `average`. - The total number of students who scored above average `aboveAverageCount`. Parameters: scores (list): List of students' scores (integers). Returns: dict: Dictionary containing highest, lowest, average, aboveAverageCount. if not scores: return {"highest": None, "lowest": None, "average": None, "aboveAverageCount": 0} highest = max(scores) lowest = min(scores) average = round(sum(scores) / len(scores), 2) above_average_count = sum(1 for score in scores if score > average) return { "highest": highest, "lowest": lowest, "average": average, "aboveAverageCount": above_average_count }

question:There is a string ( s ) consisting of ( N ) lowercase English letters. You need to find the length of the longest substring of ( s ) that contains at most ( k ) distinct characters. -----Input----- - The first line of the input contains a single integer ( t ) denoting the number of test cases. The description of ( t ) test cases follows. - The first line of each test case contains an integer ( k ). - The second line of each test case contains a string ( s ). -----Output----- For each test case, print a single line containing one integer: the length of the longest substring with at most ( k ) distinct characters. -----Constraints----- - ( 1 leq t leq 10^4 ) - ( 1 leq k leq 26 ) - ( 1 leq |s| leq 10^5 ) - The sum of the lengths of all strings over all test cases doesn't exceed ( 10^6 ) -----Example Input----- 3 2 abcba 1 aa 3 abaccc -----Example Output----- 3 2 5 -----Explanation----- Example case 1: The longest substring with at most two distinct characters in "abcba" is "bcb", which has length 3. Example case 2: The longest substring with at most one distinct character in "aa" is "aa", which has length 2. Example case 3: The longest substring with at most three distinct characters in "abaccc" is "abaccc", which has length 6.

answer:def longest_substring_with_k_distinct(s, k): Returns the length of the longest substring with at most k distinct characters. from collections import defaultdict char_count = defaultdict(int) left = 0 max_length = 0 for right in range(len(s)): char_count[s[right]] += 1 while len(char_count) > k: char_count[s[left]] -= 1 if char_count[s[left]] == 0: del char_count[s[left]] left += 1 max_length = max(max_length, right - left + 1) return max_length def process_test_cases(t, test_cases): results = [] for i in range(t): k, s = test_cases[i] results.append(longest_substring_with_k_distinct(s, k)) return results

question:Alice loves collecting rare gemstones, and she has recently acquired a collection of n gemstones, each of different values. She wants to display these gemstones in a special sequence such that the overall value of the collection, when viewed from left to right, forms a strictly increasing subsequence. However, arranging the gemstones in this way is not trivial, and Alice needs your help. You are given the values of the gemstones, and your task is to determine two things: 1. The length of the longest strictly increasing subsequence of gemstone values. 2. One example of such a subsequence. -----Input----- The first line contains an integer n (1 leq n leq 3,000) — the number of gemstones in the collection. The second line contains n space-separated integers a_1, a_2, dots, a_n (1 leq a_i leq 10,000) — the values of the gemstones. -----Output----- On the first line, print a single integer — the length of the longest strictly increasing subsequence. On the second line, print the values of an example of such a subsequence in the order they appear. -----Examples----- Input 6 5 2 8 6 3 6 Output 3 2 6 8 Input 8 10 9 2 5 3 7 101 18 Output 4 2 3 7 101 -----Note----- In the first sample, one of the longest strictly increasing subsequences is 2, 6, 8, which has length 3. In the second sample, one of the longest strictly increasing subsequences is 2, 3, 7, 101, which has length 4.

answer:def longest_increasing_subsequence(arr): if not arr: return 0, [] n = len(arr) dp = [1] * n prev = [-1] * n for i in range(1, n): for j in range(i): if arr[i] > arr[j] and dp[i] < dp[j] + 1: dp[i] = dp[j] + 1 prev[i] = j max_len = max(dp) index = dp.index(max_len) lis = [] while index != -1: lis.append(arr[index]) index = prev[index] lis.reverse() return max_len, lis

question:# Task: Write a function that flattens a nested list of integers and strings while preserving the order of the elements. The function should be able to handle nesting of arbitrary depth, but only the innermost elements that are integers should be added to the final list. Strings should be ignored. If the input is not a list, return an empty list. # Function Signature: ```python def flatten_list(nested_list: list) -> list: ``` # Input: - `nested_list`: A list, which may contain other lists, integers, and strings at arbitrary depths. # Output: - A single list containing all the integers from the input list, in the same order they appeared, regardless of depth. # Example: ```python flatten_list([1, 'a', [2, 'b', [3, [4, 'c']], 5], ['d'], 6]) # Output: [1, 2, 3, 4, 5, 6] flatten_list(['a', ['b', ['c']]]) # Output: [] flatten_list(5) # Output: [] flatten_list([]) # Output: [] ``` # Constraints: - Elements can be integers, strings, or lists. - Non-list input should return an empty list. - Maintain the order of the integers as they appear in the input. - The function should handle deeply nested structures efficiently. # Hints: - Consider using a recursive approach to navigate through the nested lists. - Use helper functions if necessary to keep the code clean and readable.

answer:def flatten_list(nested_list): Flattens a nested list of integers and strings while preserving the order of the integers. Strings are ignored. Only the innermost integers are added to the final list. If the input is not a list, an empty list is returned. :param nested_list: list containing integers, strings, and sublists :return: a list containing all the integers from the input list in order def flatten(nested): for elem in nested: if isinstance(elem, list): yield from flatten(elem) elif isinstance(elem, int): yield elem if not isinstance(nested_list, list): return [] return list(flatten(nested_list))

Released under the chat License.

has loaded