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 and two characters. Write a function to determine the minimum distance between the two given characters in the string. The distance is defined as the number of characters in between the two characters.", "input": "A string and two characters.", "output": "An integer representing the minimum distance between the two characters in the string.", "examples": [ { "input": "\"hello world\", 'h', 'r'", "output": "7", "explanation": "The distance between 'h' at index 0 and 'r' at index 8 is 7." }, { "input": "\"abcdefg\", 'a', 'g'", "output": "5", "explanation": "The distance between 'a' at index 0 and 'g' at index 6 is 5." }, { "input": "\"abcabcabc\", 'a', 'c'", "output": "1", "explanation": "The minimum distance between 'a' and 'c' is between indices 0 and 2, which is 1." }, { "input": "\"zzzabczzz\", 'z', 'a'", "output": "1", "explanation": "The minimum distance between 'z' and 'a' is between indices 2 and 3, which is 1." } ], "function_signature": "def min_distance_between_chars(s, char1, char2):", "solution": { "code": "def min_distance_between_chars(s, char1, char2):\n last_pos_char1 = -1\n last_pos_char2 = -1\n min_distance = float('inf')\n for i, char in enumerate(s):\n if char == char1:\n last_pos_char1 = i\n if last_pos_char2 != -1:\n min_distance = min(min_distance, last_pos_char1 - last_pos_char2 - 1)\n elif char == char2:\n last_pos_char2 = i\n if last_pos_char1 != -1:\n min_distance = min(min_distance, last_pos_char2 - last_pos_char1 - 1)\n return min_distance if min_distance != float('inf') else -1" } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.