Appearance
question:def product_except_self(nums: List[int]) -> List[int]: Given an array of integers, create another array where each element at index `i` is the product of all integers in the input array except the one at `i`. >>> product_except_self([1, 2, 3, 4]) [24, 12, 8, 6] >>> product_except_self([1, 0, 3, 4]) [0, 12, 0, 0] >>> product_except_self([-1, -2, -3, -4]) [-24, -12, -8, -6] >>> product_except_self([1, -2, 3, -4]) [24, -12, 8, -6]
answer:def product_except_self(nums): Returns an array where each element at index `i` is the product of all integers in the input array except the one at index `i`. n = len(nums) if n == 0: return [] result = [1] * n left_product = 1 right_product = 1 # Calculate left products for each element for i in range(n): result[i] = left_product left_product *= nums[i] # Multiply with right products for each element for i in range(n-1, -1, -1): result[i] *= right_product right_product *= nums[i] return result
question:from typing import List, Tuple def shortest_path_length(n: int, m: int, edges: List[Tuple[int, int]], s: int, t: int) -> int: Find the shortest path between nodes S and T in an undirected graph. Parameters: n: The number of nodes (1-indexed). m: The number of edges. edges: List of edges where each edge is represented as a tuple (u, v). s: The start node. t: The target node. Returns: length of the shortest path between S and T, or -1 if no path exists. Example: >>> shortest_path_length(5, 6, [(1, 2), (2, 3), (3, 4), (4, 5), (2, 4), (5, 1)], 1, 5) 1 >>> shortest_path_length(4, 2, [(1, 2), (3, 4)], 1, 3) -1
answer:from collections import deque, defaultdict def shortest_path_length(n, m, edges, s, t): Find the shortest path between nodes S and T in an undirected graph. Parameters: n (int): Number of nodes m (int): Number of edges edges (list[tuple[int, int]]): List of edges represented as tuples (u, v) s (int): Start node t (int): Target node Returns: int: Length of the shortest path between S and T, or -1 if no path exists. if s == t: return 0 # Create adjacency list for the graph graph = defaultdict(list) for u, v in edges: graph[u].append(v) graph[v].append(u) # Perform BFS to find the shortest path queue = deque([(s, 0)]) visited = set([s]) while queue: current, distance = queue.popleft() for neighbor in graph[current]: if neighbor == t: return distance + 1 if neighbor not in visited: visited.add(neighbor) queue.append((neighbor, distance + 1)) # If we reach here, there is no path between S and T return -1
question:def count_unique_chars(s: str) -> int: Returns the count of unique alphabetic characters in the string `s`, ignoring case, spaces, and punctuation. >>> count_unique_chars("Hello, World!") == 7 >>> count_unique_chars("A") == 1 >>> count_unique_chars("AaBbCc!") == 3 >>> count_unique_chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") == 26 >>> count_unique_chars("!!!aaaBBBccc???") == 3 >>> count_unique_chars(" ") == 0 >>> count_unique_chars("") == 0 >>> count_unique_chars("!?,.") == 0 >>> count_unique_chars("abc123ABC") == 3 >>> max_length_string = "aaBBcc" * 5333 + "d" >>> count_unique_chars(max_length_string) == 4
answer:def count_unique_chars(s): Returns the count of unique alphabetic characters in the string `s`, ignoring case, spaces, and punctuation. # Initialize an empty set to store unique characters unique_chars = set() # Iterate through each character in the string for char in s: # Check if the character is an alphabet letter if char.isalpha(): # Convert to lowercase and add to the set unique_chars.add(char.lower()) # Return the size of the set return len(unique_chars)
question:def curve_scores(scores: List[int], max_score: int) -> None: Adjusts the scores of students based on a curved grading system. Parameters: scores (list of int): List of current scores of students. max_score (int): The maximum possible score in the curved grading system. Returns: None, but mutates the input list 'scores'. >>> scores = [72, 88, 95, 100] >>> max_score = 120 >>> curve_scores(scores, max_score) >>> scores [86, 106, 114, 120] >>> scores = [60, 70, 80, 90, 100] >>> max_score = 200 >>> curve_scores(scores, max_score) >>> scores [120, 140, 160, 180, 200]
answer:def curve_scores(scores, max_score): Adjusts the scores of students based on a curved grading system. Parameters: scores (list of int): List of current scores of students. max_score (int): The maximum possible score in the curved grading system. Returns: None, but mutates the input list 'scores'. if not scores: return current_max_score = max(scores) # If the maximum provided score is less than or equal to the highest score in the list, # do not change the scores. if max_score <= current_max_score: return # Calculate the proportion factor to adjust the scores factor = max_score / current_max_score # Mutate the input list with the adjusted scores for i in range(len(scores)): scores[i] = round(scores[i] * factor)