Skip to content
🤔prompts chat🧠
🔍
question:def organize_grades(students): Organizes a list of tuples representing students and their grades into a dictionary where the keys are the grade categories (A, B, C, D, F) and the values are lists of student names. Parameters: students (list of tuples): A list where each tuple contains a student's name and their grade. Returns: dict: A dictionary where keys are grade categories ('A', 'B', 'C', 'D', 'F') and values are lists of student names. Example: >>> organize_grades([("John", 85), ("Jane", 92), ("Dave", 78), ("Sara", 62), ("Tim", 55)]) { 'A': ['Jane'], 'B': ['John'], 'C': ['Dave'], 'D': ['Sara'], 'F': ['Tim'] } def test_organize_grades_typical_case(): result = organize_grades([("John", 85), ("Jane", 92), ("Dave", 78), ("Sara", 62), ("Tim", 55)]) expected = { 'A': ['Jane'], 'B': ['John'], 'C': ['Dave'], 'D': ['Sara'], 'F': ['Tim'] } assert result == expected def test_organize_grades_all_grades_present(): result = organize_grades([("Alice", 95), ("Bob", 88), ("Charlie", 73), ("Diana", 67), ("Eve", 50)]) expected = { 'A': ['Alice'], 'B': ['Bob'], 'C': ['Charlie'], 'D': ['Diana'], 'F': ['Eve'] } assert result == expected def test_organize_grades_empty_input(): result = organize_grades([]) expected = { 'A': [], 'B': [], 'C': [], 'D': [], 'F': [] } assert result == expected def test_organize_grades_no_A_students(): result = organize_grades([("Fiona", 75), ("George", 82), ("Harry", 68)]) expected = { 'A': [], 'B': ['George'], 'C': ['Fiona'], 'D': ['Harry'], 'F': [] } assert result == expected def test_organize_grades_multiple_students_same_grade(): result = organize_grades([("Ivy", 91), ("Jack", 91), ("Kyle", 50), ("Lydia", 50)]) expected = { 'A': ['Ivy', 'Jack'], 'B': [], 'C': [], 'D': [], 'F': ['Kyle', 'Lydia'] } assert result == expected

answer:def organize_grades(students): Organizes a list of tuples representing students and their grades into a dictionary. Parameters: students (list of tuples): A list where each tuple contains a student's name and their grade. Returns: dict: A dictionary where keys are grade categories ('A', 'B', 'C', 'D', 'F') and values are lists of student names. # Initialize the dictionary with keys for each grade category grade_categories = {'A': [], 'B': [], 'C': [], 'D': [], 'F': []} for name, grade in students: if 90 <= grade <= 100: grade_categories['A'].append(name) elif 80 <= grade <= 89: grade_categories['B'].append(name) elif 70 <= grade <= 79: grade_categories['C'].append(name) elif 60 <= grade <= 69: grade_categories['D'].append(name) elif 0 <= grade <= 59: grade_categories['F'].append(name) return grade_categories

question:from typing import List def find_divisibles(numbers: List[int], target: int) -> List[int]: Write a function that takes a list of integers and a target integer. The function should return a new list consisting of all integers from the original list that are divisible by the target integer. If no integers in the list are divisible by the target integer, return an empty list. If the target integer is zero, raise a ValueError. >>> find_divisibles([10, 15, 20, 25, 30], 5) == [10, 15, 20, 25, 30] >>> find_divisibles([10, 12, 15, 16, 20], 5) == [10, 15, 20] >>> find_divisibles([1, 2, 3, 4], 5) == [] >>> find_divisibles([-10, -20, 15, 25, 30], 5) == [-10, -20, 15, 25, 30] >>> find_divisibles([0, 10, 20, 30], 10) == [0, 10, 20, 30] >>> find_divisibles([0, 10, 20, 30], 0) Traceback (most recent call last): ... ValueError: Target integer cannot be zero.

answer:def find_divisibles(numbers, target): Returns a list of integers from the original list that are divisible by the target integer. Parameters: numbers (list): A list of integers. target (int): The target integer. Returns: list: A list of integers divisible by the target integer. if target == 0: raise ValueError("Target integer cannot be zero.") return [num for num in numbers if num % target == 0]

question:def count_frequencies(numbers: List[int]) -> List[Tuple[int, int]]: Takes a list of integers and returns a list of tuples, where each tuple contains an integer from the input list and its frequency of occurrence. The returned list of tuples is sorted by the integers in ascending order. >>> count_frequencies([]) [] >>> count_frequencies([1]) [(1, 1)] >>> count_frequencies([1, 2, 2, 3, 3, 3]) [(1, 1), (2, 2), (3, 3)] >>> count_frequencies([3, 1, 2]) [(1, 1), (2, 1), (3, 1)] >>> count_frequencies([4, 4, 4, 4]) [(4, 4)] >>> count_frequencies([-1, -1, -2]) [(-2, 1), (-1, 2)]

answer:def count_frequencies(numbers): Takes a list of integers and returns a list of tuples, where each tuple contains an integer from the input list and its frequency of occurrence. The returned list of tuples is sorted by the integers in ascending order. from collections import Counter frequency_dict = Counter(numbers) sorted_items = sorted(frequency_dict.items()) return sorted_items

question:def longest_subsequence_k_times(s: str, k: int) -> int: Returns the length of the longest possible subsequence in which every character appears at least 'k' times. This function should leverage a divide and conquer strategy to achieve the result efficiently. It avoids the use of Python's built-in functions that would make this task trivial. >>> longest_subsequence_k_times("aaabb", 3) 3 >>> longest_subsequence_k_times("ababbc", 2) 5 pass def test_empty_string(): assert longest_subsequence_k_times('', 1) == 0 def test_all_characters_meet_threshold(): assert longest_subsequence_k_times('aaabb', 3) == 3 def test_mixed_threshold_characters(): assert longest_subsequence_k_times('ababbc', 2) == 5 def test_no_characters_meet_threshold(): assert longest_subsequence_k_times('abcdef', 2) == 0 def test_all_characters_of_k_frequency(): assert longest_subsequence_k_times('aabbcc', 2) == 6 def test_single_character_string(): assert longest_subsequence_k_times('a', 1) == 1 assert longest_subsequence_k_times('a', 2) == 0 def test_repeating_characters_exceed_threshold(): assert longest_subsequence_k_times('aaabbbccc', 3) == 9

answer:def longest_subsequence_k_times(s: str, k: int) -> int: Returns the length of the longest possible subsequence in which every character appears at least 'k' times. This function employs a divide and conquer strategy. def helper(start, end): if end - start < k: return 0 count = {} for i in range(start, end): if s[i] in count: count[s[i]] += 1 else: count[s[i]] = 1 for mid in range(start, end): if count[s[mid]] < k: left = helper(start, mid) right = helper(mid + 1, end) return max(left, right) return end - start return helper(0, len(s))

Released under the chat License.

has loaded