prompt,canonical_solution,test
def find_missing_num(nums): """ You are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in the list. One of the integers is missing in the list. Write an efficient code to find the missing integer. Example: >>> find_missing_num([1, 2, 4, 6, 3, 7, 8]) 5 """ return sum(range(1, len(nums)+2)) - sum(nums),def check(candidate): assert candidate([1, 2, 4, 6, 3, 7, 8]) == 5, "Test Case 1" assert candidate([3, 7, 1, 2, 8, 4, 5]) == 6, "Test Case 2" assert candidate([1, 2, 3, 5]) == 4, "Test Case 3" assert candidate([1]) == 2, "Test Case 4"
def merge_intervals(intervals): """ Given a collection of intervals, merge all overlapping intervals. Example: >>> merge_intervals([[1,3],[2,6],[8,10],[15,18]]) [[1,6],[8,10],[15,18]] """ intervals.sort(key=lambda x: x[0]) merged = [] for interval in intervals: if not merged or merged[-1][1] < interval[0]: merged.append(interval) else: merged[-1][1] = max(merged[-1][1], interval[1]) return merged,def check(candidate): assert candidate([[1,3],[2,6],[8,10],[15,18]]) == [[1,6],[8,10],[15,18]], "Test Case 1" assert candidate([[1,4],[4,5]]) == [[1,5]], "Test Case 2" assert candidate([[1,4],[2,3]]) == [[1,4]], "Test Case 3"
def rotate_matrix(matrix): """ Given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Example: >>> rotate_matrix([[1,2,3],[4,5,6],[7,8,9]]) [[7, 4, 1], [8, 5, 2], [9, 6, 3]] """ return [list(row)[::-1] for row in zip(*matrix)],def check(candidate): assert candidate([[1,2,3],[4,5,6],[7,8,9]]) == [[7, 4, 1], [8, 5, 2], [9, 6, 3]], "Test Case 1" assert candidate([[1,2],[3,4]]) == [[3, 1], [4, 2]], "Test Case 2" assert candidate([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) == [[10, 7, 4, 1], [11, 8, 5, 2], [12, 9, 6, 3]], "Test Case 3"
def remove_duplicates(nums): """ Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once and returns the new length. Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory. Example: >>> remove_duplicates([1,1,2]) 2 """ i = 0 for j in range(1, len(nums)): if nums[j] != nums[i]: i += 1 nums[i] = nums[j] return i+1,def check(candidate): assert candidate([1,1,2]) == 2, "Test Case 1" assert candidate([0,0,1,1,1,2,2,3,3,4]) == 5, "Test Case 2" assert candidate([1,1,1,1,1]) == 1, "Test Case 3"
def three_sum(nums): """ Given an integer array nums, return all the unique triplets in the array which gives the sum of zero. You may return the triplets in any order. Example: >>> three_sum([-1,0,1,2,-1,-4]) [[-1, -1, 2], [-1, 0, 1]] """ res = [] nums.sort() for i in range(len(nums)-2): if i > 0 and nums[i] == nums[i-1]: continue l, r = i+1, len(nums)-1 while l < r: total = nums[i] + nums[l] + nums[r] if total < 0: l += 1 elif total > 0: r -= 1 else: res.append([nums[i], nums[l], nums[r]]) while l < r and nums[l] == nums[l+1]: l += 1 l += 1 r -= 1 return res,def check(candidate): assert candidate([-1,0,1,2,-1,-4]) == [[-1, -1, 2], [-1, 0, 1]], "Test Case 1" assert candidate([]) == [], "Test Case 2" assert candidate([0]) == [], "Test Case 3"