File size: 13,758 Bytes
fcf6b5e 301b108 fcf6b5e 301b108 fcf6b5e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
[
{
"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": -1,
"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.",
"imports": "",
"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"
}
]
|