Appearance
question:def min_cost_to_connect_houses(n: int, m: int, edges: List[Tuple[int, int, int]]) -> int: Determines the minimum cost to connect all houses in the village using a Minimum Spanning Tree (MST). Args: n (int): Number of houses. m (int): Number of possible electrical lines. edges (list of tuples): Each tuple contains three integers u, v, and w where u and v are the houses between which an electrical line can be built, and w is the cost of that line. Returns: int: The minimum cost to connect all houses. # Unit tests def test_min_cost_example_1(): n = 4 m = 5 edges = [(1, 2, 1), (1, 3, 4), (2, 3, 2), (2, 4, 3), (3, 4, 5)] assert min_cost_to_connect_houses(n, m, edges) == 6 def test_min_cost_example_2(): n = 3 m = 3 edges = [(1, 2, 5), (1, 3, 6), (2, 3, 1)] assert min_cost_to_connect_houses(n, m, edges) == 6 def test_min_cost_disconnected(): n = 4 m = 3 edges = [(1, 2, 1), (3, 4, 2), (3, 4, 3)] assert min_cost_to_connect_houses(n, m, edges) == -1 def test_min_cost_single_line(): n = 2 m = 1 edges = [(1, 2, 10)] assert min_cost_to_connect_houses(n, m, edges) == 10 def test_min_cost_multiple_connections(): n = 4 m = 6 edges = [(1, 2, 2), (1, 3, 2), (1, 4, 3), (2, 3, 1), (2, 4, 5), (3, 4, 9)] assert min_cost_to_connect_houses(n, m, edges) == 6
answer:import heapq def min_cost_to_connect_houses(n, m, edges): Determines the minimum cost to connect all houses in the village using a Minimum Spanning Tree (MST). Args: n (int): Number of houses. m (int): Number of possible electrical lines. edges (list of tuples): Each tuple contains three integers u, v, and w where u and v are the houses between which an electrical line can be built, and w is the cost of that line. Returns: int: The minimum cost to connect all houses. # Create an adjacency list representation of the graph graph = {i: [] for i in range(1, n + 1)} for u, v, w in edges: graph[u].append((w, v)) graph[v].append((w, u)) # Initialize min-heap and visited list min_heap = [(0, 1)] # (cost, node) visited = [False] * (n + 1) total_cost = 0 edges_used = 0 # Prim's algorithm to find the MST while min_heap and edges_used < n: cost, node = heapq.heappop(min_heap) if visited[node]: continue visited[node] = True total_cost += cost edges_used += 1 for edge_cost, neighbor in graph[node]: if not visited[neighbor]: heapq.heappush(min_heap, (edge_cost, neighbor)) if edges_used == n: return total_cost else: return -1 # If the graph is not connected # Example usage: # n = 4, m = 5 # edges = [(1, 2, 1), (1, 3, 4), (2, 3, 2), (2, 4, 3), (3, 4, 5)] # print(min_cost_to_connect_houses(n, m, edges)) # Output: 6
question:def countGoodStrings(words: List[str], chars: str) -> int: Returns the sum of the lengths of all good strings in the words list. A string is "good" if it can be formed by characters from chars (each character can only be used once). Example 1: >>> countGoodStrings(["cat","bt","hat","tree"], "atach") 6 Example 2: >>> countGoodStrings(["hello","world","leetcode"], "welldonehoneyr") 10
answer:def countGoodStrings(words, chars): from collections import Counter # Function to check if a word can be formed def can_form(word, chars_count): word_count = Counter(word) for char, count in word_count.items(): if count > chars_count.get(char, 0): return False return True chars_count = Counter(chars) total_length = 0 for word in words: if can_form(word, chars_count): total_length += len(word) return total_length
question:from typing import List, Tuple def determine_winner(sequence: List[int]) -> str: Determine the winner of the game given the initial sequence. >>> determine_winner([2, 4, 6]) 'Anka' >>> determine_winner([3, 5, 7, 9]) 'Boris' def game_outcome(test_cases: List[Tuple[int, List[int]]]) -> List[str]: Given a list of test cases, determine the winner for each case. >>> game_outcome([(3, [2, 4, 6]), (4, [3, 5, 7, 9])]) ['Anka', 'Boris'] >>> game_outcome([(4, [3, 5, 8, 9]), (2, [1, 2]), (5, [7, 7, 7, 7, 7])]) ['Anka', 'Anka', 'Boris']
answer:def determine_winner(sequence): has_even = any(x % 2 == 0 for x in sequence) return "Anka" if has_even else "Boris" def game_outcome(test_cases): results = [] for N, sequence in test_cases: results.append(determine_winner(sequence)) return results
question:def count_even_sum_pairs(arr): Counts the number of pairs of elements in the array whose sum is even. Parameters: arr (list): List of integers Returns: int: Number of pairs whose sum is even Examples: >>> count_even_sum_pairs([1, 2, 3]) 1 >>> count_even_sum_pairs([1, 2, 3, 4]) 2 >>> count_even_sum_pairs([2, 2, 2, 2, 2]) 10 def solve(test_cases): Solve the problem for multiple test cases. Parameters: test_cases (list of lists): List containing multiple lists of integers Returns: list: List of results for each test case Examples: >>> solve([[1, 2, 3], [1, 2, 3, 4], [2, 2, 2, 2, 2]]) [1, 2, 10]
answer:def count_even_sum_pairs(arr): Counts the number of pairs of elements in the array whose sum is even. Parameters: arr (list): List of integers Returns: int: Number of pairs whose sum is even even_count = sum(1 for x in arr if x % 2 == 0) odd_count = len(arr) - even_count return even_count * (even_count - 1) // 2 + odd_count * (odd_count - 1) // 2 def solve(test_cases): results = [] for arr in test_cases: results.append(count_even_sum_pairs(arr)) return results