[ { "task_id": 1, "mbpp_task_id": 776, "description": "Write a Python function to count characters which have vowels as their neighbors in the given string.", "cot": "Given the vowel list vowels = ['a', 'e', 'i', 'o', 'u']. Given a string s with len(s) characters. If len(s) is less than 2, then there are no neighboring characters, so return 0. Otherwise: All characters except for the first and the last have a preceding and a following neighbor. Use a for loop to iterate over the indices idx of s except for the first and last index, i.e. starting from the second and ending with the second to last character. Check if the idx-th character is a vowel. If not, check if the character preceding or the following the current character is a vowel and if it is, then increment the vowel counter res. After the loop ends, the first and last characters still need to be checked. If the first character is not a vowel and the second character is a vowel, increment res by 1. If the last character is not a vowel and the second-to-last character is a vowel, increment res by 1. Then return res.", "imports": "", "function_head": "def count_vowels(s):\n", "function_body": " res = 0\n vowels = ['a', 'e', 'i', 'o', 'u']\n for idx in range(1, len(s) - 1):\n if s[idx] not in vowels and (s[idx - 1] in vowels or s[idx + 1] in vowels):\n res += 1\n if s[0] not in vowels and s[1] in vowels:\n res += 1\n if s[-1] not in vowels and s[-2] in vowels:\n res += 1\n return (res) " }, { "task_id": 2, "mbpp_task_id": 639, "description": "Write a Python function to sum the length of the strings of a given list after removing the strings that start with a lowercase letter.", "cot": "The function can be implemented using a lambda function. Given the list containing strings starting with a lowecase or a uppercase letter. The sum of the lengths of each string starting with an uppercase letter can be determined as follows: First remove all the strings that start with a lowercase letter using the filter function. The filtering condition can be dynamically created using a lambda function which iterates over the strings and checks whether a string starts with an uppercase or a lowercase letter keeping only the strings starting with an uppercase letter. The filter function returns an iterator with the elements that pass the filtering condition. To convert it to a list, you can use the list(...) constructor. Finally, join each string of the filtered list and return its length to count the sum of the lengths of each string starting with an uppercase letter.", "imports": "", "function_head": "def sum_str_len(s):\n", "function_body": " s=list(filter(lambda el:el[0].isupper() and el[1:].islower(),s))\n return len(''.join(s))" }, { "task_id": 3, "mbpp_task_id": 793, "description": "Write a Python function to find the position of last occurrences of an element in a sorted array.", "cot": "The function can be implemented using a binary search algorithm. Given a sorted array of elements arr of length n. Given the target element x for which we want to find the last occurrence in the array and return its position. The last occurrence of the target element x can be determined as follows: First initialize the bottom border to 0, i.e. low = 0, and the top border to n - 1, i.e. high = n-1, to track the left and right ends of the current search interval. By the end of the loop, if we did not find the target value, the function returns -1. Then enter the loop and repeatedly divide the search interval in half to find the part of the array that contains the element x. This can be accomplished by adding the bottom and top border of the current search interval and dividing the sum by 2 using the floor division operator, i.e. mid = (low + high) // 2. Now compare the value at mid with the target. If the value at mid is greater than the target x, then we need to search the last occurrence in the left part because all the values in the right part are less than the target. Update the top border, i.e. high= mid - 1. Else, if the value at mid is less than the target x, then we need to search for the first occurrence in the right part and update the bottom border, i.e. low = mid +1. Else the value at mid is the target element and the bottom border is updated, i.e. low=mid+1. The loop ends when the bottom border is greater than the top border, i.e. low > high.", "imports": "", "function_head": "def last(arr,x):\n", "function_body": " n = len(arr)\n low = 0\n high = n - 1\n res = -1 \n while (low <= high):\n mid = (low + high) // 2 \n if arr[mid] > x:\n high = mid - 1\n elif arr[mid] < x:\n low = mid + 1\n else:\n res = mid\n low = mid + 1\n return res" }, { "task_id": 4, "mbpp_task_id": null, "description": "Write a Python function to lowercase the input text.", "cot": "Given an input text. The function can be implemented with the lower() method, which returns a string in which all characters are lowercase. Symbols and Numbers are ignored.", "imports": "", "function_head": "def text_lowercase(text):\n", "function_body": " return text.lower()" }, { "task_id": 5, "mbpp_task_id": 725, "description": "Write a Python function to extract values between quotation marks \" \" of the given string.", "cot": "The function can be implemented using a regular expression. Given an input string s containing substrings between quotation. To extract strings in between the quotations the method findall() from re library can be used. The method findall() returns all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found.", "imports": "import re\n", "function_head": "def extract_quotation(s):\n", "function_body": " return (re.findall(r'\"(.*?)\"', s))" }, { "task_id": 6, "mbpp_task_id": 619, "description": "Write a Python function to move all the numbers to the end of the given string.", "cot": "\"The function can be implemented using a for loop. Given an input string s. Initialize two empty strings to append the digits and the substrings found in the input string, i.e. dig='' and res=''. Iterate over each character in the string and check if it is a digit using the method isdigit(). If it is a digit append it to the digits string i.e. dig += ele, else append it to the substring string res i.e. res += ele. After the loop terminates, append the digits string to the substring string res, i.e. res += dig.", "imports": "", "function_head": "def move_num(s):\n", "function_body": " res = ''\n dig = ''\n for ele in s:\n if ele.isdigit():\n dig += ele\n else:\n res += ele\n res += dig\n return (res) " }, { "task_id": 7, "mbpp_task_id": 602, "description": "Write a Python function to find the first repeated character in a given string.", "cot": "The function can be implemented using a for loop. Given an input string s. Iterate over each character in the string using enumerate objects in the loop, i.e. enumerate(s). The enumerate() function adds a counter to an iterable and returns it in the form of an enumeration object, i.e. each character c is accompanied by a position index. Use this position index to count the occurrences of a character up to its position index, i.e. s[:index+1].count(c). Returns the first character whose counter is greater than 1.", "imports": "", "function_head": "def first_repeated_char(s):\n", "function_body": " for index,c in enumerate(s):\n if s[:index+1].count(c) > 1:\n return c" }, { "task_id": 8, "mbpp_task_id": 616, "description": "Write a Python function which takes two tuples of the same length and performs the element wise modulo.", "cot": "The function can be implemented using a generator expression. Given two tuples i.e. tup1, tup2 of same length. The element wise modulo of tuples can be realized using a generator expression and the zip() function. The zip() function returns pairs of elements, where the first element is from the first tuple and the second element is from the second tuple.", "imports": "", "function_head": "def tuple_modulo(tup1, tup2):\n", "function_body": " res = tuple(el1 % el2 for el1, el2 in zip(tup1, tup2)) \n return (res)" }, { "task_id": 9, "mbpp_task_id": 641, "description": "Write a Python function to find the nth nonagonal number.", "cot": "The function can be implemented using the formula: n*(7*n - 5)/2.", "imports": "", "function_head": "def is_nonagonal(n):\n", "function_body": " return int(n * (7 * n - 5) / 2)" }, { "task_id": 10, "mbpp_task_id": 784, "description": "Write a Python function to find the first even and odd number of a given list.", "cot": "The function can be implemented using a generator expression and the next() function. Given a list l of numbers. Iterate over the list and find the first even number from the list, i.e. el%2==0. Then iterate over the list to find the first odd number from the list, i.e. el%2!=0. Return a tuple containing the first even and odd number. If there is no even or odd number on the list then return -1.", "function_head": "def even_odd(l):\n", "function_body": " first_even = next((el for el in l if el%2==0),-1)\n first_odd = next((el for el in l if el%2!=0),-1)\n return first_even, first_odd" }, { "task_id": 11, "mbpp_task_id": 785, "description": "Write a Python function to convert tuple string to integer tuple.", "cot": "The function can be implemented using the functions tuple(), int(), replace() and split(). Given a tuple of integers as a string. Iterate over the string characters and use the replace() and split() function to extract the digits. Use the int() function to convert the characters to integers and use the tuple() method to create a tuple from the integer values.", "imports": "", "function_head": "def tuple_str_int(test_str):\n", "function_body": " res = tuple(int(num) for num in test_str.replace('(', '').replace(')', '').replace('...', '').split(', '))\n return (res)" }, { "task_id": 12, "mbpp_task_id": 574, "description": "Write a Python function to compute the surface area of a cylinder.", "cot": "Given the height and the radius of a cylinder. The surface of a cylinder can be implemented using the following formula: ((2*pi*radian) * height) + ((pi*radian**2)*2). With pi = 3.1415.", "imports": "", "function_head": "def surfacearea_cylinder(r,h):\n", "function_body": " surfacearea=((2*3.1415*r)*h) + ((3.1415*r**2)*2)\n return surfacearea" }, { "task_id": 13, "mbpp_task_id": 786, "description": "Write a Python function to locate the right insertion point for a specified value in sorted order.", "cot": "Given a list l and a value x to be inserted. Use the bisect_right function of the bisect library to find the position in the list where the input value x can be inserted to keep the list sorted. The bisect_right function returns the position in the sorted list where the input value x can be inserted to keep the resulting list sorted. If the element already exists in the list, the rightmost position where the element must be inserted is returned.\n", "imports": "import bisect\n", "function_head": "def right_insertion(l, x):\n", "function_body": " return bisect.bisect_right(l, x)" }, { "task_id": 14, "mbpp_task_id": 566, "description": "Write a Python function to get the sum of the digits of a non-negative integer.", "cot": "Given an non-negative integer n. The sum can be implemented by using a recursive function and the modulo operator. The modulo operation returns the rest of the division of n by 10. By casting the result of the division of n by 10, the digits before the decimal point are obtained. With the casted result, the function can be called again to determine the next digit and add it to the intermediate result.", "imports": "", "function_head": "def sum_digits(n):\n", "function_body": " if n == 0:\n return 0\n else:\n return n % 10 + sum_digits(int(n / 10))" }, { "task_id": 15, "mbpp_task_id": 603, "description": "Write a Python function to get lucid numbers smaller than or equal to a given integer.", "cot": "Given an non-negative integer number n. Ludic numbers smaller or equal than n are obtained by considering list of natural numbers and removing i-th number in i-th iteration where i begins with 2. In every iteration, the first removed number is Ludic. 1 is considered as Ludic.", "imports": "", "function_head": "def get_ludic(n):\n", "function_body": " ludics = []\n for i in range(1, n + 1):\n ludics.append(i)\n index = 1\n while(index != len(ludics)):\n first_ludic = ludics[index]\n remove_index = index + first_ludic\n while(remove_index < len(ludics)):\n ludics.remove(ludics[remove_index])\n remove_index = remove_index + first_ludic - 1\n index += 1\n return ludics" }, { "task_id": 16, "mbpp_task_id": 760, "description": "Write a Python function to check whether a list of numbers contains only one distinct element or not.", "cot":"Given is a list of numbers l. The check whether a list of numbers contains only one element or not can be realized using a set, since no repetitions are allowed in a set. Insert the elements of the list into the set. If there is only one element, the length of the set should be 1. In this case return True else False.", "imports": "", "function_head": "def unique_element(l):\n", "function_body": " s = set(l)\n return len(s) == 1" }, { "task_id": 17, "mbpp_task_id": 744, "description": "Write a Python function to check if the given tuple has any none value or not.", "cot":"Given a tuple t. To check if the given tuple has any none value or not can be implemented by using the functions any(), map() and lambda. The map() function iterates over each element of the given tuple and returns an iterable of the results after applying the lambda function to each item of the given tuple. The lambda function checks if an item is None. The any() function returns True if any of the elements of the returned map results are True else it returns False.", "imports": "", "function_head": "def check_none(test_tup):\n", "function_body": " res = any(map(lambda ele: ele is None, test_tup))\n return res" }, { "task_id": 18, "mbpp_task_id": 772, "description": "Write a Python function to remove all the words with k length in the given string.", "cot":"Given a string s and the length k. To remove k length words in the given string s can be realized using a list comprehension and the functions split(), join() and len(). The split() function splits a string into a list of strings after breaking the given string by the specified separator. If is not provided then any white space is a separator. Iterate over the splits with a list comprehension and omit all elements whose length differs from k, i.e. len(ele) != k. Then use the join() function to concatenate the elements of length k separated by the space character. The joined elements of length k are returned as a string.", "imports": "", "function_head": "def remove_length(s, k):\n", "function_body": " temp = s.split()\n res = [ele for ele in temp if len(ele) != k]\n res = ' '.join(res)\n return (res)" }, { "task_id": 19, "mbpp_task_id": 775, "description": "Write a Python function to check whether every odd index contains odd numbers of a given list.", "cot":"Given a list of integer numbers. To check whether every even index contains an even number and every odd index contains odd number of the input list num can be implemented using a generator expression and the modulo operator. Iterate over the indexes of the list and compare whether the index and the value stored under the index is an even or odd number. If each even index has an even value and each odd index has an odd value then return True otherwise False. Use the all() function for this. The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function also returns True.", "imports": "", "function_head": "def odd_position(nums):\n", "function_body": " return all(nums[i]%2==i%2 for i in range(len(nums)))" }, { "task_id": 20, "mbpp_task_id": 597, "description": "Write a Python function to find kth element from the given two sorted arrays.", "cot":"Given two sorted lists of size m and n respectively and a value k. Finding the k-th element from the given two lists of integers can be realized by using a technique based on while-loops to merge two lists. From the merged (and sorted) list the element from the k-th position can be easily determined.", "imports": "", "function_head": "def find_kth(arr1, arr2, k):\n", "function_body": " m = len(arr1)\n n = len(arr2)\n sorted1 = [0] * (m + n)\n i = 0\n j = 0\n d = 0\n while (i < m and j < n):\n if (arr1[i] < arr2[j]):\n sorted1[d] = arr1[i]\n i += 1\n else:\n sorted1[d] = arr2[j]\n j += 1\n d += 1\n while (i < m):\n sorted1[d] = arr1[i]\n d += 1\n i += 1\n while (j < n):\n sorted1[d] = arr2[j]\n d += 1\n j += 1\n return sorted1[k - 1]" } ]