Skip to content
🤔prompts chat🧠
🔍
question:def shortest_subarray_with_sum(N: int, X: int, prices: List[int]) -> int: Finds the length of the shortest contiguous subarray whose sum equals X. If there are multiple such subarrays, the one with the smallest starting index is chosen. If no such subarray is found, return -1. Args: N (int): The number of gadgets X (int): The exact sum to be matched prices (List[int]): List of gadget prices Returns: int: Length of the shortest subarray with sum equal to X, or -1 if no such subarray exists. Examples: >>> shortest_subarray_with_sum(6, 9, [1, 2, 3, 4, 5, 6]) 2 >>> shortest_subarray_with_sum(5, 50, [1, 2, 3, 4, 5]) -1 from typing import List def test_shortest_subarray_with_sum(): # Sample test case assert shortest_subarray_with_sum(6, 9, [1, 2, 3, 4, 5, 6]) == 2 # Test case where no subarray sums to X assert shortest_subarray_with_sum(5, 50, [1, 2, 3, 4, 5]) == -1 # Test case where entire array sums to X assert shortest_subarray_with_sum(3, 6, [1, 2, 3]) == 3 # Test having multiple subarrays with sum = X assert shortest_subarray_with_sum(7, 15, [1, 2, 3, 4, 5, 5, 10]) == 2 # Test case with smallest subarray at the beginning assert shortest_subarray_with_sum(5, 3, [3, 1, 1, 1, 1]) == 1 # Test case with smallest subarray at the end assert shortest_subarray_with_sum(5, 5, [1, 2, 1, 1, 5]) == 1 # Test for large numbers assert shortest_subarray_with_sum(10, 15, [1000000]*10) == -1 # Test with repeated numbers but with valid subarray in the middle assert shortest_subarray_with_sum(8, 20, [5, 5, 5, 5, 1, 2, 3, 4]) == 4 # Test with multiple valid subarrays assert shortest_subarray_with_sum(6, 5, [1, 1, 1, 1, 1, 1]) == 5 # Edge case with a single element subarray that equals X assert shortest_subarray_with_sum(5, 2, [1, 2, 3, 4, 5]) == 1 if __name__ == "__main__": test_shortest_subarray_with_sum()

answer:def shortest_subarray_with_sum(N, X, prices): Finds the length of the shortest contiguous subarray whose sum equals X. If there are multiple such subarrays, the one with the smallest starting index is chosen. If no such subarray is found, return -1. from collections import deque min_length = float('inf') current_sum = 0 start = 0 subarray_indices = (-1, -1) for end in range(N): current_sum += prices[end] while current_sum >= X: if current_sum == X: if (end - start + 1) < min_length: min_length = end - start + 1 subarray_indices = (start, end) current_sum -= prices[start] start += 1 return min_length if min_length != float('inf') else -1

question:def convert_to_title_case(s: str) -> str: Converts a given string into Title Case, where the first letter of each word is capitalized and the remaining letters are in lowercase. Parameters: s (str): Input string containing only alphabetic characters and spaces. Returns: str: The string converted to Title Case. Examples: >>> convert_to_title_case("hello world") 'Hello World' >>> convert_to_title_case("python programming is fun") 'Python Programming Is Fun' >>> convert_to_title_case("This Is a Test") 'This Is A Test' >>> convert_to_title_case("hELLo woRLd") 'Hello World' >>> convert_to_title_case("PyTHoN PrOgRaMmInG Is FuN") 'Python Programming Is Fun' >>> convert_to_title_case("HELLO WORLD") 'Hello World' >>> convert_to_title_case("PYTHON PROGRAMMING IS FUN") 'Python Programming Is Fun'

answer:def convert_to_title_case(s): Converts a given string into Title Case. Parameters: s (str): Input string containing only alphabetic characters and spaces. Returns: str: The string converted to Title Case. return s.title()

question:def minimum_spanning_tree(towns: int, road_data: List[List[int]]) -> int: Find the total length of the minimum spanning tree in the given road network. >>> minimum_spanning_tree(5, [[1, 2, 4], [1, 3, 2], [2, 3, 1], [2, 4, 7], [3, 4, 3], [3, 5, 5], [4, 5, 6]]) == 11 >>> minimum_spanning_tree(4, [[1, 2, 1], [1, 3, 2], [1, 4, 3], [2, 3, 4], [3, 4, 5]]) == 6 >>> minimum_spanning_tree(3, [[1, 2, 1], [1, 3, 3], [2, 3, 4]]) == 4 >>> minimum_spanning_tree(4, [[1, 2, 3], [1, 3, 1], [2, 3, 3], [3, 4, 1], [2, 4, 8]]) == 5

answer:import heapq def find(parent, i): A function to find the set of an element i (uses path compression technique) if parent[i] == i: return i else: parent[i] = find(parent, parent[i]) return parent[i] def union(parent, rank, x, y): A function that does union of two sets of x and y (uses union by rank) xroot = find(parent, x) yroot = find(parent, y) if rank[xroot] < rank[yroot]: parent[xroot] = yroot elif rank[xroot] > rank[yroot]: parent[yroot] = xroot else: parent[yroot] = xroot rank[xroot] += 1 def kruskal_mst(towns, roads): Function to construct MST using Kruskal's algorithm result = [] # Step 1: Sort all the edges in non-decreasing order of their weight. roads = sorted(roads, key=lambda item: item[2]) parent = [] rank = [] # Create V subsets with single elements for node in range(towns): parent.append(node) rank.append(0) # Number of edges to be taken is equal to V-1 e = 0 i = 0 # Step 2: Pick the smallest edge. Check if it forms a cycle with the spanning tree # formed so far. If cycle is not formed, include this edge. Else, discard it. while e < towns - 1: # Step 2.1: Pick the smallest edge and increment the index for next iteration u, v, w = roads[i] i = i + 1 x = find(parent, u - 1) y = find(parent, v - 1) # If including this edge does't cause cycle, include it in result and increment # the index of result for next edge if x != y: e = e + 1 result.append([u, v, w]) union(parent, rank, x, y) # Else discard the edge # Print the contents of result[] to display the built MST minimum_cost = sum([w for u, v, w in result]) return minimum_cost def minimum_spanning_tree(towns, road_data): return kruskal_mst(towns, road_data)

question:from typing import List import math from functools import reduce def gcd(a: int, b: int) -> int: return math.gcd(a, b) def gcd_of_list(lst: List[int]) -> int: return reduce(gcd, lst) def gcd_of_subarray(arr: List[int], l: int, r: int) -> int: Given a list of integers, find the GCD of the subarray specified by the range [l, r]. >>> gcd_of_subarray([2, 3, 6, 9, 5], 1, 3) 1 >>> gcd_of_subarray([2, 3, 6, 9, 5], 2, 5) 1 >>> gcd_of_subarray([2, 3, 6, 9, 5], 1, 5) 1 subarray = arr[l-1:r] return gcd_of_list(subarray) # Unit tests def test_gcd_of_subarray_case1(): arr = [2, 3, 6, 9, 5] assert gcd_of_subarray(arr, 1, 3) == 1 def test_gcd_of_subarray_case2(): arr = [2, 3, 6, 9, 5] assert gcd_of_subarray(arr, 2, 5) == 1 def test_gcd_of_subarray_case3(): arr = [2, 3, 6, 9, 5] assert gcd_of_subarray(arr, 1, 5) == 1 def test_gcd_of_subarray_single_element(): arr = [2, 3, 6, 9, 5] assert gcd_of_subarray(arr, 2, 2) == 3 def test_gcd_of_subarray_entire_array(): arr = [2, 4, 6, 8] assert gcd_of_subarray(arr, 1, 4) == 2 def test_gcd_of_subarray_with_trivial_gcd(): arr = [5, 10, 15, 20] assert gcd_of_subarray(arr, 1, 4) == 5 def test_gcd_of_subarray_all_same_elements(): arr = [7, 7, 7, 7] assert gcd_of_subarray(arr, 1, 4) == 7 def test_gcd_of_subarray_subarray_with_common_divisor(): arr = [2, 4, 8, 16] assert gcd_of_subarray(arr, 2, 3) == 4

answer:import math from functools import reduce def gcd(a, b): return math.gcd(a, b) def gcd_of_list(lst): return reduce(gcd, lst) def gcd_of_subarray(arr, l, r): subarray = arr[l-1:r] return gcd_of_list(subarray)

Released under the chat License.

has loaded