Create a programming task related to common algorithms and data…
- User
Assistant: User: Create a programming task related to common algorithms and data structures. The task should follow this format:
{ "problem_description": "You are given an array of integers. Write a function to find the maximum sum of a subarray within the array.", "input": "An array of integers.", "output": "The maximum sum of a subarray within the input array.", "examples": [ { "input": "[-2, 1, -3, 4, -1, 2, 1, -5, 4]", "output": "6", "explanation": "The subarray [4, -1, 2, 1] has the maximum sum of 6." }, { "input": "[1, 2, 3, 4, -10, 5, 6]", "output": "15", "explanation": "The subarray [1, 2, 3, 4, -10, 5, 6] has the maximum sum of 15." }, { "input": "[5, 4, -1, 7, 8]", "output": "23", "explanation": "The subarray [5, 4, -1, 7, 8] has the maximum sum of 23." }, { "input": "[-1, -2, -3, -4]", "output": "-1", "explanation": "The subarray [-1] has the maximum sum of -1." } ], "function_signature": "def max_subarray_sum(arr):", "solution": { "code": " max_sum = float('-inf') current_sum = 0 for num in arr: current_sum += num if current_sum > max_sum: max_sum = current_sum if current_sum < 0: current_sum = 0 return max_sum" } }
Assistant:
- Assistant
{ "problem_description": "You are given a string containing digits from 2-9 inclusive, which represents a sequence of letters on a telephone keypad. Write a function to return all possible letter combinations that the number could represent. Map the digits to their respective letters similar to the T9 keypad input method.", "input": "A string containing digits between 2 and 9.", "output": "A list of all possible letter combinations that the input numbers could represent.", "examples": [ { "input": ""23"", "output": "["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]", "explanation": "The digit 2 maps to 'abc' and the digit 3 maps to 'def'. Combining these gives the possible letter combinations." }, { "input": """", "output": "[]", "explanation": "An empty input results in no combinations." }, { "input": ""9"", "output": "["w", "x", "y", "z"]", "explanation": "The digit 9 maps to 'wxyz'. Hence all single character combinations are ['w', 'x', 'y', 'z']." }, { "input": ""78"", "output": "["pt", "pu", "pv", "qt", "qu", "qv", "rt", "ru", "rv", "st", "su", "sv"]", "explanation": "The digit 7 maps to 'pqrs' and the digit 8 maps to 'tuv'. Combining these gives the possible letter combinations." } ], "function_signature": "def letter_combinations(digits):", "solution": { "code": "def letter_combinations(digits):\n if not digits:\n return []\n phone_mapping = {\n "2": "abc",\n "3": "def",\n "4": "ghi",\n "5": "jkl",\n "6": "mno",\n "7": "pqrs",\n "8": "tuv",\n "9": "wxyz"\n }\n combinations = [""]\n for digit in digits:\n new_combinations = []\n for combination in combinations:\n for letter in phone_mapping[digit]:\n new_combinations.append(combination + letter)\n combinations = new_combinations\n return combinations" } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.