Appearance
question:def pair_sums(lst: List[int], target: int) -> List[Tuple[int, int]]: Write a function that receives a list of integers and a target integer. The function should return a list of unique pairs of numbers from the list that add up to the target. Each pair should be in the form of a tuple (first, second), where 'first' is less than or equal to 'second'. The pairs themselves should be sorted lexicographically, and the order of the numbers in the returned tuples should reflect the order in which they appear in the input list. Examples: >>> pair_sums([1, 2, 3, 4, 3, 2, 1], 4) [(1, 3), (2, 2)] >>> pair_sums([1, 5, 2, 4, 3], 6) [(1, 5), (2, 4)] >>> pair_sums([1, 2, 2, 3, 4], 8) [] >>> pair_sums([0, -1, 1, -2, 2], 0) [(-2, 2), (-1, 1)]
answer:def pair_sums(lst, target): Returns a list of unique pairs of numbers from the input list that add up to the target. Each pair is sorted (first, second) where 'first' is <= 'second' and the pairs are sorted lexicographically. seen = set() pairs = set() for num in lst: complement = target - num if complement in seen: pair = tuple(sorted((num, complement))) pairs.add(pair) seen.add(num) return sorted(list(pairs))
question:from typing import List def count_pairs_with_sum(nums: List[int], target: int) -> int: Write a function that accepts a list of integers and an integer target value, and finds the number of pairs of integers in the list that sum up to the target value. >>> count_pairs_with_sum([1, 2, 3, 4], 5) 2 >>> count_pairs_with_sum([1, 2, 3, 4, 3], 6) 2 >>> count_pairs_with_sum([1, -1, 2, -2, 3], 1) 2
answer:def count_pairs_with_sum(nums, target): Returns the number of pairs in the list nums that sum up to the target value. seen = {} count = 0 for num in nums: complement = target - num if complement in seen: count += seen[complement] if num in seen: seen[num] += 1 else: seen[num] = 1 return count
question:def contains_nearby_duplicate(arr, k): Determines if there are two distinct indices i and j in the array such that arr[i] == arr[j] and the absolute difference between i and j is at most k. :param arr: List of integers :param k: Integer, the maximum allowed distance between duplicated values :return: "TRUE" if such indices exist, "FALSE" otherwise >>> contains_nearby_duplicate([1, 2, 3, 1, 2, 3], 2) "FALSE" >>> contains_nearby_duplicate([1, 0, 1, 1], 1) "TRUE" >>> contains_nearby_duplicate([1, 2, 3, 1, 2, 3], 3) "TRUE" >>> contains_nearby_duplicate([1], 1) "FALSE" >>> contains_nearby_duplicate([99, 99], 2) "TRUE" >>> contains_nearby_duplicate([1, 2, 3, 4, 1], 4) "TRUE" >>> contains_nearby_duplicate([], 1) "FALSE" >>> contains_nearby_duplicate([1, 2, 3, 4, 2], 1) "FALSE" >>> contains_nearby_duplicate([1, 2, 3, 2, 1], 2) "TRUE" >>> contains_nearby_duplicate([1, 2, 3, 1], 3) "TRUE"
answer:def contains_nearby_duplicate(arr, k): Determines if there are two distinct indices i and j in the array such that arr[i] == arr[j] and the absolute difference between i and j is at most k. :param arr: List of integers :param k: Integer, the maximum allowed distance between duplicated values :return: "TRUE" if such indices exist, "FALSE" otherwise num_indices = {} for i, num in enumerate(arr): if num in num_indices and i - num_indices[num] <= k: return "TRUE" num_indices[num] = i return "FALSE"
question:from typing import List def pacific_atlantic(heights: List[List[int]]) -> List[List[int]]: Given a grid of size m x n where each cell in the grid contains a non-negative integer representing the height of that cell above sea level, find all cells where water can flow to both Pacific and Atlantic oceans. Water can flow from a cell to any of its four direct neighbors (north, south, east, west) if the neighboring cell's height is less than or equal to the current cell's height. A cell is considered in the "Atlantic water" flow if it can directly or indirectly flow to the bottom or right edge of the grid, and a cell is considered in the "Pacific water" flow if it can directly or indirectly flow to the top or left edge of the grid. Parameters: - heights: A 2D array representing the height map. Returns: - A list of grid coordinates where water can flow to both the Pacific and Atlantic oceans. Examples: >>> pacific_atlantic([[1, 2, 2, 3, 5], [3, 2, 3, 4, 4], [2, 4, 5, 3, 1], [6, 7, 1, 4, 5], [5, 1, 1, 2, 4]]) [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] >>> pacific_atlantic([[10]]) [[0, 0]] >>> pacific_atlantic([]) [] >>> pacific_atlantic([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]] >>> pacific_atlantic([[1, 2, 3], [8, 9, 4], [7, 6, 5]]) [[0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
answer:def pacific_atlantic(heights): if not heights or not heights[0]: return [] m, n = len(heights), len(heights[0]) pacific_reachable = [[False] * n for _ in range(m)] atlantic_reachable = [[False] * n for _ in range(m)] def dfs(i, j, reachable): reachable[i][j] = True for x, y in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]: if 0 <= x < m and 0 <= y < n and not reachable[x][y] and heights[x][y] >= heights[i][j]: dfs(x, y, reachable) for i in range(m): dfs(i, 0, pacific_reachable) dfs(i, n - 1, atlantic_reachable) for j in range(n): dfs(0, j, pacific_reachable) dfs(m - 1, j, atlantic_reachable) result = [] for i in range(m): for j in range(n): if pacific_reachable[i][j] and atlantic_reachable[i][j]: result.append([i, j]) return result