ShwStone/humaneval-leetcode huggingface dataset:
def twoSum(nums: List[int], target: int) -> List[int]:
    """
    Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
    You may assume that each input would have exactly one solution, and you may not use the same element twice.
    You can return the answer in any order.
    
    Example 1:
    
    >>> twoSum(nums = [2,7,11,15], target = 9)
    >>> [0,1]
    Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
    
    Example 2:
    
    >>> twoSum(nums = [3,2,4], target = 6)
    >>> [1,2]
    
    Example 3:
    
    >>> twoSum(nums = [3,3], target = 6)
    >>> [0,1]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
    """
    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
    You may assume the two numbers do not contain any leading zero, except the number 0 itself.
    
    Example 1:
    
    
    >>> __init__(l1 = [2,4,3], l2 = [5,6,4])
    >>> [7,0,8]
    Explanation: 342 + 465 = 807.
    
    Example 2:
    
    >>> __init__(l1 = [0], l2 = [0])
    >>> [0]
    
    Example 3:
    
    >>> __init__(l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9])
    >>> [8,9,9,9,0,0,0,1]
    """
def lengthOfLongestSubstring(s: str) -> int:
    """
    Given a string s, find the length of the longest substring without repeating characters.
    
    Example 1:
    
    >>> lengthOfLongestSubstring(s = "abcabcbb")
    >>> 3
    Explanation: The answer is "abc", with the length of 3.
    
    Example 2:
    
    >>> lengthOfLongestSubstring(s = "bbbbb")
    >>> 1
    Explanation: The answer is "b", with the length of 1.
    
    Example 3:
    
    >>> lengthOfLongestSubstring(s = "pwwkew")
    >>> 3
    Explanation: The answer is "wke", with the length of 3.
    Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
    """
def findMedianSortedArrays(nums1: List[int], nums2: List[int]) -> float:
    """
    Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
    The overall run time complexity should be O(log (m+n)).
    
    Example 1:
    
    >>> findMedianSortedArrays(nums1 = [1,3], nums2 = [2])
    >>> 2.00000
    Explanation: merged array = [1,2,3] and median is 2.
    
    Example 2:
    
    >>> findMedianSortedArrays(nums1 = [1,2], nums2 = [3,4])
    >>> 2.50000
    Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
    """
def longestPalindrome(s: str) -> str:
    """
    Given a string s, return the longest palindromic substring in s.
    
    Example 1:
    
    >>> longestPalindrome(s = "babad")
    >>> "bab"
    Explanation: "aba" is also a valid answer.
    
    Example 2:
    
    >>> longestPalindrome(s = "cbbd")
    >>> "bb"
    """
def convert(s: str, numRows: int) -> str:
    """
    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
    
    P   A   H   N
    A P L S I I G
    Y   I   R
    
    And then read line by line: "PAHNAPLSIIGYIR"
    Write the code that will take a string and make this conversion given a number of rows:
    
    string convert(string s, int numRows);
    
    
    Example 1:
    
    >>> convert(s = "PAYPALISHIRING", numRows = 3)
    >>> "PAHNAPLSIIGYIR"
    
    Example 2:
    
    >>> convert(s = "PAYPALISHIRING", numRows = 4)
    >>> "PINALSIGYAHRPI"
    Explanation:
    P     I    N
    A   L S  I G
    Y A   H R
    P     I
    
    Example 3:
    
    >>> convert(s = "A", numRows = 1)
    >>> "A"
    """
def reverse(x: int) -> int:
    """
    Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
    Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
    
    Example 1:
    
    >>> reverse(x = 123)
    >>> 321
    
    Example 2:
    
    >>> reverse(x = -123)
    >>> -321
    
    Example 3:
    
    >>> reverse(x = 120)
    >>> 21
    """
def myAtoi(s: str) -> int:
    """
    Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.
    The algorithm for myAtoi(string s) is as follows:
    
    Whitespace: Ignore any leading whitespace (" ").
    Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present.
    Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.
    Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1.
    
    Return the integer as the final result.
    
    Example 1:
    
    >>> myAtoi(s = "42")
    >>> 42
    Explanation:
    
    The underlined characters are what is read in and the caret is the current reader position.
    Step 1: "42" (no characters read because there is no leading whitespace)
             ^
    Step 2: "42" (no characters read because there is neither a '-' nor '+')
             ^
    Step 3: "42" ("42" is read in)
               ^
    
    
    Example 2:
    
    >>> myAtoi(s = " -042")
    >>> -42
    Explanation:
    
    Step 1: "   -042" (leading whitespace is read and ignored)
                ^
    Step 2: "   -042" ('-' is read, so the result should be negative)
                 ^
    Step 3: "   -042" ("042" is read in, leading zeros ignored in the result)
                   ^
    
    
    Example 3:
    
    >>> myAtoi(s = "1337c0d3")
    >>> 1337
    Explanation:
    
    Step 1: "1337c0d3" (no characters read because there is no leading whitespace)
             ^
    Step 2: "1337c0d3" (no characters read because there is neither a '-' nor '+')
             ^
    Step 3: "1337c0d3" ("1337" is read in; reading stops because the next character is a non-digit)
                 ^
    
    
    Example 4:
    
    >>> myAtoi(s = "0-1")
    >>> 0
    Explanation:
    
    Step 1: "0-1" (no characters read because there is no leading whitespace)
             ^
    Step 2: "0-1" (no characters read because there is neither a '-' nor '+')
             ^
    Step 3: "0-1" ("0" is read in; reading stops because the next character is a non-digit)
              ^
    
    
    Example 5:
    
    >>> myAtoi(s = "words and 987")
    >>> 0
    Explanation:
    Reading stops at the first non-digit character 'w'.
    """
def isPalindrome(x: int) -> bool:
    """
    Given an integer x, return true if x is a palindrome, and false otherwise.
    
    Example 1:
    
    >>> isPalindrome(x = 121)
    >>> true
    Explanation: 121 reads as 121 from left to right and from right to left.
    
    Example 2:
    
    >>> isPalindrome(x = -121)
    >>> false
    Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
    
    Example 3:
    
    >>> isPalindrome(x = 10)
    >>> false
    Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
    """
def isMatch(s: str, p: str) -> bool:
    """
    Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
    
    '.' Matches any single character.​​​​
    '*' Matches zero or more of the preceding element.
    
    The matching should cover the entire input string (not partial).
    
    Example 1:
    
    >>> isMatch(s = "aa", p = "a")
    >>> false
    Explanation: "a" does not match the entire string "aa".
    
    Example 2:
    
    >>> isMatch(s = "aa", p = "a*")
    >>> true
    Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
    
    Example 3:
    
    >>> isMatch(s = "ab", p = ".*")
    >>> true
    Explanation: ".*" means "zero or more (*) of any character (.)".
    """
def maxArea(height: List[int]) -> int:
    """
    You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
    Find two lines that together with the x-axis form a container, such that the container contains the most water.
    Return the maximum amount of water a container can store.
    Notice that you may not slant the container.
    
    Example 1:
    
    
    >>> maxArea(height = [1,8,6,2,5,4,8,3,7])
    >>> 49
    Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
    
    Example 2:
    
    >>> maxArea(height = [1,1])
    >>> 1
    """
def intToRoman(num: int) -> str:
    """
    Seven different symbols represent Roman numerals with the following values:
    
    
    
    Symbol
    Value
    
    
    
    
    I
    1
    
    
    V
    5
    
    
    X
    10
    
    
    L
    50
    
    
    C
    100
    
    
    D
    500
    
    
    M
    1000
    
    
    
    Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
    
    If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
    If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol, for example, 4 is 1 (I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Only the following subtractive forms are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM).
    Only powers of 10 (I, X, C, M) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (V), 50 (L), or 500 (D) multiple times. If you need to append a symbol 4 times use the subtractive form.
    
    Given an integer, convert it to a Roman numeral.
    
    Example 1:
    
    >>> intToRoman(num = 3749)
    >>> "MMMDCCXLIX"
    Explanation:
    
    3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M)
     700 = DCC as 500 (D) + 100 (C) + 100 (C)
      40 = XL as 10 (X) less of 50 (L)
       9 = IX as 1 (I) less of 10 (X)
    Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places
    
    
    Example 2:
    
    >>> intToRoman(num = 58)
    >>> "LVIII"
    Explanation:
    
    50 = L
     8 = VIII
    
    
    Example 3:
    
    >>> intToRoman(num = 1994)
    >>> "MCMXCIV"
    Explanation:
    
    1000 = M
     900 = CM
      90 = XC
       4 = IV
    """
def romanToInt(s: str) -> int:
    """
    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
    
    Symbol       Value
    I             1
    V             5
    X             10
    L             50
    C             100
    D             500
    M             1000
    For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
    Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
    
    I can be placed before V (5) and X (10) to make 4 and 9.
    X can be placed before L (50) and C (100) to make 40 and 90.
    C can be placed before D (500) and M (1000) to make 400 and 900.
    
    Given a roman numeral, convert it to an integer.
    
    Example 1:
    
    >>> romanToInt(s = "III")
    >>> 3
    Explanation: III = 3.
    
    Example 2:
    
    >>> romanToInt(s = "LVIII")
    >>> 58
    Explanation: L = 50, V= 5, III = 3.
    
    Example 3:
    
    >>> romanToInt(s = "MCMXCIV")
    >>> 1994
    Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
    """
def longestCommonPrefix(strs: List[str]) -> str:
    """
    Write a function to find the longest common prefix string amongst an array of strings.
    If there is no common prefix, return an empty string "".
    
    Example 1:
    
    >>> longestCommonPrefix(strs = ["flower","flow","flight"])
    >>> "fl"
    
    Example 2:
    
    >>> longestCommonPrefix(strs = ["dog","racecar","car"])
    >>> ""
    Explanation: There is no common prefix among the input strings.
    """
def threeSum(nums: List[int]) -> List[List[int]]:
    """
    Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
    Notice that the solution set must not contain duplicate triplets.
    
    Example 1:
    
    >>> threeSum(nums = [-1,0,1,2,-1,-4])
    >>> [[-1,-1,2],[-1,0,1]]
    Explanation:
    nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
    nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
    nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
    The distinct triplets are [-1,0,1] and [-1,-1,2].
    Notice that the order of the output and the order of the triplets does not matter.
    
    Example 2:
    
    >>> threeSum(nums = [0,1,1])
    >>> []
    Explanation: The only possible triplet does not sum up to 0.
    
    Example 3:
    
    >>> threeSum(nums = [0,0,0])
    >>> [[0,0,0]]
    Explanation: The only possible triplet sums up to 0.
    """
def threeSumClosest(nums: List[int], target: int) -> int:
    """
    Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
    Return the sum of the three integers.
    You may assume that each input would have exactly one solution.
    
    Example 1:
    
    >>> threeSumClosest(nums = [-1,2,1,-4], target = 1)
    >>> 2
    Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
    
    Example 2:
    
    >>> threeSumClosest(nums = [0,0,0], target = 1)
    >>> 0
    Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
    """
def letterCombinations(digits: str) -> List[str]:
    """
    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
    A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
    
    
    Example 1:
    
    >>> letterCombinations(digits = "23")
    >>> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
    
    Example 2:
    
    >>> letterCombinations(digits = "")
    >>> []
    
    Example 3:
    
    >>> letterCombinations(digits = "2")
    >>> ["a","b","c"]
    """
def fourSum(nums: List[int], target: int) -> List[List[int]]:
    """
    Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
    
    0 <= a, b, c, d < n
    a, b, c, and d are distinct.
    nums[a] + nums[b] + nums[c] + nums[d] == target
    
    You may return the answer in any order.
    
    Example 1:
    
    >>> fourSum(nums = [1,0,-1,0,-2,2], target = 0)
    >>> [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
    
    Example 2:
    
    >>> fourSum(nums = [2,2,2,2,2], target = 8)
    >>> [[2,2,2,2]]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(head: Optional[ListNode], n: int) -> Optional[ListNode]:
    """
    Given the head of a linked list, remove the nth node from the end of the list and return its head.
    
    Example 1:
    
    
    >>> __init__(head = [1,2,3,4,5], n = 2)
    >>> [1,2,3,5]
    
    Example 2:
    
    >>> __init__(head = [1], n = 1)
    >>> []
    
    Example 3:
    
    >>> __init__(head = [1,2], n = 1)
    >>> [1]
    """
def isValid(s: str) -> bool:
    """
    Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
    An input string is valid if:
    
    Open brackets must be closed by the same type of brackets.
    Open brackets must be closed in the correct order.
    Every close bracket has a corresponding open bracket of the same type.
    
    
    Example 1:
    
    >>> isValid(s = "()")
    >>> true
    
    Example 2:
    
    >>> isValid(s = "()[]{}")
    >>> true
    
    Example 3:
    
    >>> isValid(s = "(]")
    >>> false
    
    Example 4:
    
    >>> isValid(s = "([])")
    >>> true
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
    """
    You are given the heads of two sorted linked lists list1 and list2.
    Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
    Return the head of the merged linked list.
    
    Example 1:
    
    
    >>> __init__(list1 = [1,2,4], list2 = [1,3,4])
    >>> [1,1,2,3,4,4]
    
    Example 2:
    
    >>> __init__(list1 = [], list2 = [])
    >>> []
    
    Example 3:
    
    >>> __init__(list1 = [], list2 = [0])
    >>> [0]
    """
def generateParenthesis(n: int) -> List[str]:
    """
    Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
    
    Example 1:
    >>> generateParenthesis(n = 3)
    >>> ["((()))","(()())","(())()","()(())","()()()"]
    Example 2:
    >>> generateParenthesis(n = 1)
    >>> ["()"]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeKLists(lists: List[Optional[ListNode]]) -> Optional[ListNode]:
    """
    You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
    Merge all the linked-lists into one sorted linked-list and return it.
    
    Example 1:
    
    >>> __init__(lists = [[1,4,5],[1,3,4],[2,6]])
    >>> [1,1,2,3,4,4,5,6]
    Explanation: The linked-lists are:
    [
      1->4->5,
      1->3->4,
      2->6
    ]
    merging them into one sorted list:
    1->1->2->3->4->4->5->6
    
    Example 2:
    
    >>> __init__(lists = [])
    >>> []
    
    Example 3:
    
    >>> __init__(lists = [[]])
    >>> []
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(head: Optional[ListNode]) -> Optional[ListNode]:
    """
    Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
    
    Example 1:
    
    >>> __init__(head = [1,2,3,4])
    >>> [2,1,4,3]
    Explanation:
    
    
    Example 2:
    
    >>> __init__(head = [])
    >>> []
    
    Example 3:
    
    >>> __init__(head = [1])
    >>> [1]
    
    Example 4:
    
    >>> __init__(head = [1,2,3])
    >>> [2,1,3]
    """
# class ListNode:
#     def __init__(val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseKGroup(head: Optional[ListNode], k: int) -> Optional[ListNode]:
    """
    Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
    k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
    You may not alter the values in the list's nodes, only nodes themselves may be changed.
    
    Example 1:
    
    
    >>> __init__(head = [1,2,3,4,5], k = 2)
    >>> [2,1,4,3,5]
    
    Example 2:
    
    
    >>> __init__(head = [1,2,3,4,5], k = 3)
    >>> [3,2,1,4,5]
    """
def removeDuplicates(nums: List[int]) -> int:
    """
    Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
    Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
    
    Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
    Return k.
    
    Custom Judge:
    The judge will test your solution with the following code:
    
    int[] nums = [...]; // Input array
    int[] expectedNums = [...]; // The expected answer with correct length
    
    int k = removeDuplicates(nums); // Calls your implementation
    
    assert k == expectedNums.length;
    for (int i = 0; i < k; i++) {
        assert nums[i] == expectedNums[i];
    }
    
    If all assertions pass, then your solution will be accepted.
    
    Example 1:
    
    >>> removeDuplicates(nums = [1,1,2])
    >>> 2, nums = [1,2,_]
    Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
    It does not matter what you leave beyond the returned k (hence they are underscores).
    
    Example 2:
    
    >>> removeDuplicates(nums = [0,0,1,1,1,2,2,3,3,4])
    >>> 5, nums = [0,1,2,3,4,_,_,_,_,_]
    Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
    It does not matter what you leave beyond the returned k (hence they are underscores).
    """
def removeElement(nums: List[int], val: int) -> int:
    """
    Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.
    Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:
    
    Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
    Return k.
    
    Custom Judge:
    The judge will test your solution with the following code:
    
    int[] nums = [...]; // Input array
    int val = ...; // Value to remove
    int[] expectedNums = [...]; // The expected answer with correct length.
                                // It is sorted with no values equaling val.
    
    int k = removeElement(nums, val); // Calls your implementation
    
    assert k == expectedNums.length;
    sort(nums, 0, k); // Sort the first k elements of nums
    for (int i = 0; i < actualLength; i++) {
        assert nums[i] == expectedNums[i];
    }
    
    If all assertions pass, then your solution will be accepted.
    
    Example 1:
    
    >>> removeElement(nums = [3,2,2,3], val = 3)
    >>> 2, nums = [2,2,_,_]
    Explanation: Your function should return k = 2, with the first two elements of nums being 2.
    It does not matter what you leave beyond the returned k (hence they are underscores).
    
    Example 2:
    
    >>> removeElement(nums = [0,1,2,2,3,0,4,2], val = 2)
    >>> 5, nums = [0,1,4,0,3,_,_,_]
    Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
    Note that the five elements can be returned in any order.
    It does not matter what you leave beyond the returned k (hence they are underscores).
    """