Skip to content
🤔prompts chat🧠
🔍
question:def calculate_total_age(tree: dict) -> int: Calculate the total age of relatives in the given genealogical tree. Args: tree (dict): A dictionary representing the genealogical tree where each key is a string representing a person's name, and each value is another dictionary with keys "age" (an integer) and "children" (a dictionary structured similarly). Returns: int: The total age of all individuals in the tree. >>> tree = { ... "John": { ... "age": 50, ... "children": { ... "Michael": { ... "age": 30, ... "children": { ... "Sarah": { ... "age": 10, ... "children": {} ... }, ... "Tom": { ... "age": 8, ... "children": {} ... } ... } ... }, ... "Anna": { ... "age": 28, ... "children": { ... "James": { ... "age": 3, ... "children": {} ... } ... } ... } ... } ... } ... } >>> calculate_total_age(tree) 129

answer:def calculate_total_age(tree: dict) -> int: Recursively calculates the total age of all individuals in the genealogical tree. Args: tree (dict): A dictionary representing the genealogical tree. Returns: int: The total age of all individuals in the tree. total_age = 0 # Recursive function to traverse and sum ages def traverse(node): nonlocal total_age age = node['age'] total_age += age for child in node['children'].values(): traverse(child) # Start traversal from the root of the tree for person in tree.values(): traverse(person) return total_age

question:def relative_ordering_sort(arr): Sorts an array such that all negative numbers appear before all positive numbers while maintaining their relative order. >>> relative_ordering_sort([1, 2, 3, 4]) [1, 2, 3, 4] >>> relative_ordering_sort([-1, -2, -3, -4]) [-1, -2, -3, -4] >>> relative_ordering_sort([-1, 2, -3, 4, 5]) [-1, -3, 2, 4, 5] >>> relative_ordering_sort([0, -1, 2, -3, 1, -4, -2, 3]) [-1, -3, -4, -2, 0, 2, 1, 3] >>> relative_ordering_sort([4, -3, -2, 1, 0, -1, 5]) [-3, -2, -1, 4, 1, 0, 5] >>> relative_ordering_sort([0, 1, 2, 3]) [0, 1, 2, 3] >>> relative_ordering_sort([-1, -2, -3, -4]) [-1, -2, -3, -4] >>> relative_ordering_sort([]) [] >>> relative_ordering_sort([0]) [0] >>> relative_ordering_sort([-1]) [-1] >>> relative_ordering_sort([2]) [2] >>> relative_ordering_sort([0, 0, 0, 0]) [0, 0, 0, 0]

answer:def relative_ordering_sort(arr): Sorts an array such that all negative numbers appear before all positive numbers while maintaining their relative order. negative = [x for x in arr if x < 0] non_negative = [x for x in arr if x >= 0] return negative + non_negative

question:from typing import List def min_operations(garden_row: List[int]) -> int: Calculate the minimum number of operations to ensure no two adjacent plants are of the same type. :param garden_row: List of integers representing the garden row including its length :return: Integer, minimum number of operations needed pass def process_input(data: List[List[int]]) -> List[int]: Process multiple datasets to determine the minimum number of operations for each garden row :param data: List of datasets :return: List of integers, each representing the minimum number of operations for corresponding garden row pass def test_min_operations_single_test_case(): assert min_operations([3, 1, 2, 2]) == 1 def test_min_operations_single_type_all_adjacent(): assert min_operations([5, 1, 1, 1, 2, 2]) == 2 def test_min_operations_no_replacements_needed(): assert min_operations([4, 1, 2, 1, 2]) == 0 def test_min_operations_alternating_replacements(): assert min_operations([5, 1, 1, 2, 2, 2]) == 2 def test_process_input_multiple_rows(): data = [[3, 1, 2, 2], [5, 1, 1, 1, 2, 2], [0]] assert process_input(data) == [1, 2] def test_process_input_single_row(): data = [[4, 1, 2, 1, 1], [0]] assert process_input(data) == [1] def test_process_input_empty(): data = [[0]] assert process_input(data) == []

answer:def min_operations(garden_row): Calculate the minimum number of operations to ensure no two adjacent plants are of the same type. :param garden_row: List of integers representing the garden row including its length :return: Integer, minimum number of operations needed if not garden_row or garden_row[0] == 0: return 0 n = garden_row[0] plants = garden_row[1:] moves = 0 for i in range(1, n): if plants[i] == plants[i - 1]: moves += 1 # Choose an arbitrary plant type (that is different from current and previous if possible) plants[i] = plants[i - 1] + 1 if plants[i - 1] + 1 != plants[i - 2] else plants[i - 1] + 2 return moves def process_input(data): results = [] for row in data: if row[0] == 0: break results.append(min_operations(row)) return results

question:def max_sum_of_differences(t: int, test_cases: List[Tuple[int, List[int]]]) -> List[int]: Determine the maximum sum of differences between the pairs of two groups if Wallace splits them in the most optimal way. >>> max_sum_of_differences(2, [(2, [1, 1, 2, 2]), (3, [1, 2, 3, 4, 5, 6])]) [2, 9] >>> max_sum_of_differences(1, [(1, [3, 5])]) [2] >>> max_sum_of_differences(1, [(3, [1, 1, 1, 1, 1, 1])]) [0] >>> max_sum_of_differences(1, [(4, [1, 3, 5, 7, 2, 4, 6, 8])]) [16] >>> max_sum_of_differences(1, [(50, [x for x in range(1, 101)])]) [2500]

answer:def max_sum_of_differences(t, test_cases): results = [] for case in test_cases: n, coins = case # Sort the coin values coins.sort() # Calculate the maximum sum of differences max_diff_sum = sum(coins[n:]) - sum(coins[:n]) results.append(max_diff_sum) return results # Example usage: # t = 2 # test_cases = [ # (2, [1, 1, 2, 2]), # (3, [1, 2, 3, 4, 5, 6]) # ] # print(max_sum_of_differences(t, test_cases))

Released under the chat License.

has loaded