Skip to content
🤔prompts chat🧠
🔍
question:def reverse_words_in_sentence(T: int, sentences: List[str]) -> List[str]: Reverses the order of words in each sentence. Parameters: T (int): The number of test cases. sentences (list of str): Each element is a sentence to be processed. Returns: list of str: Sentences with the word order reversed. >>> reverse_words_in_sentence(3, ["Hello World", "The quick brown fox", "Coding is fun"]) ["World Hello", "fox brown quick The", "fun is Coding"] >>> reverse_words_in_sentence(1, ["Python is awesome"]) ["awesome is Python"]

answer:def reverse_words_in_sentence(T, sentences): Reverses the order of words in each sentence. Parameters: T (int): The number of test cases. sentences (list of str): Each element is a sentence to be processed. Returns: list of str: Sentences with the word order reversed. result = [] for sentence in sentences: # Split the sentence into words and reverse the order reversed_sentence = ' '.join(sentence.split()[::-1]) result.append(reversed_sentence) return result

question:def can_be_sorted_by_reversing_subarray(arr): Given a list of integers, determine whether the list can be sorted into ascending order by reversing exactly one contiguous subarray of the list. >>> can_be_sorted_by_reversing_subarray([1, 3, 2, 4, 5]) == "YES" >>> can_be_sorted_by_reversing_subarray([1, 4, 3, 2]) == "YES" >>> can_be_sorted_by_reversing_subarray([1, 5, 3, 3, 2]) == "NO" >>> can_be_sorted_by_reversing_subarray([1, 2, 3, 4, 5]) == "YES" >>> can_be_sorted_by_reversing_subarray([5, 4, 3, 2, 1]) == "YES" >>> can_be_sorted_by_reversing_subarray([2]) == "YES" >>> can_be_sorted_by_reversing_subarray([2, 1]) == "YES" pass

answer:def can_be_sorted_by_reversing_subarray(arr): n = len(arr) if n == 1: return "YES" # Find the first dip in the array start = 0 while start < n - 1 and arr[start] < arr[start + 1]: start += 1 # If the array is already sorted if start == n - 1: return "YES" # Find the end of the dip end = start while end < n - 1 and arr[end] > arr[end + 1]: end += 1 # Reverse the subarray start to end arr[start:end + 1] = reversed(arr[start:end + 1]) # Check if the array is sorted now for i in range(1, n): if arr[i - 1] > arr[i]: return "NO" return "YES"

question:def max_challenges_solved(N: int, M: int, skill_levels: List[int], challenge_difficulties: List[int]) -> List[int]: Calculates the number of challenges each participant can solve. Parameters: N (int): Number of participants. M (int): Number of challenges. skill_levels (list of int): List of skill levels of participants. challenge_difficulties (list of int): List of difficulty levels of challenges. Returns: list of int: List containing the number of challenges each participant can solve. Example: >>> max_challenges_solved(5, 4, [50, 60, 70, 80, 90], [40, 50, 60, 70]) [2, 3, 4, 4, 4] >>> max_challenges_solved(3, 5, [30, 60, 90], [10, 20, 30, 40, 50]) [3, 5, 5] def test_max_challenges_solved(): assert max_challenges_solved(5, 4, [50, 60, 70, 80, 90], [40, 50, 60, 70]) == [2, 3, 4, 4, 4] assert max_challenges_solved(3, 5, [30, 60, 90], [10, 20, 30, 40, 50]) == [3, 5, 5] assert max_challenges_solved(4, 4, [40, 50, 50, 60], [10, 10, 10, 10]) == [4, 4, 4, 4] assert max_challenges_solved(1, 4, [100], [10, 20, 30, 40]) == [4] assert max_challenges_solved(2, 2, [1, 100], [100, 1]) == [1, 2]

answer:def max_challenges_solved(N, M, skill_levels, challenge_difficulties): Calculates the number of challenges each participant can solve. Parameters: N (int): Number of participants. M (int): Number of challenges. skill_levels (list of int): List of skill levels of participants. challenge_difficulties (list of int): List of difficulty levels of challenges. Returns: list of int: List containing the number of challenges each participant can solve. challenge_difficulties.sort() result = [] for skill in skill_levels: count = 0 for difficulty in challenge_difficulties: if difficulty <= skill: count += 1 else: break result.append(count) return result

question:from collections import deque, defaultdict def min_intermediate_nodes(n, m, edges, s, t): Determine the minimum number of intermediate nodes required to forward a message from the source node s to the destination node t. >>> min_intermediate_nodes(6, 7, [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (1, 3), (4, 6)], 1, 6) 3 >>> min_intermediate_nodes(4, 2, [(1, 2), (2, 3)], 1, 4) -1 >>> min_intermediate_nodes(5, 5, [(1, 2), (2, 3), (3, 4), (4, 5)], 1, 5) 4 >>> min_intermediate_nodes(4, 3, [(1, 2), (2, 3), (3, 4)], 1, 4) 3 >>> min_intermediate_nodes(1, 0, [], 1, 1) 0

answer:from collections import deque, defaultdict def min_intermediate_nodes(n, m, edges, s, t): graph = defaultdict(list) # Build the graph for u, v in edges: graph[u].append(v) graph[v].append(u) # BFS initialization queue = deque([(s, 0)]) # (current_node, distance) visited = set() visited.add(s) while queue: current_node, distance = queue.popleft() # Check if we've reached the destination if current_node == t: return distance # Process neighbors for neighbor in graph[current_node]: if neighbor not in visited: visited.add(neighbor) queue.append((neighbor, distance + 1)) # If we exit the loop without finding the destination return -1

Released under the chat License.

has loaded