task_id
int32
1
15
mbpp_task_id
int32
-1
793
description
stringclasses
15 values
cot
stringclasses
15 values
imports
stringclasses
3 values
function_head
stringclasses
15 values
function_body
stringclasses
15 values
1
776
Write a Python function to count characters which have vowels as their neighbors in the given string.
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.
def count_vowels(s):
res = 0 vowels = ['a', 'e', 'i', 'o', 'u'] for idx in range(1, len(s) - 1): if s[idx] not in vowels and (s[idx - 1] in vowels or s[idx + 1] in vowels): res += 1 if s[0] not in vowels and s[1] in vowels: res += 1 if s[-1] not in vowels and s[-2] in vowels: res += 1 return (res)
2
639
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.
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.
def sum_str_len(s):
s=list(filter(lambda el:el[0].isupper() and el[1:].islower(),s)) return len(''.join(s))
3
793
Write a Python function to find the position of last occurrences of an element in a sorted array.
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.
def last(arr,x):
n = len(arr) low = 0 high = n - 1 res = -1 while (low <= high): mid = (low + high) // 2 if arr[mid] > x: high = mid - 1 elif arr[mid] < x: low = mid + 1 else: res = mid low = mid + 1 return res
4
-1
Write a Python function to lowercase the input text.
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.
def text_lowercase(text):
return text.lower()
5
725
Write a Python function to extract values between quotation marks " " of the given string.
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.
import re
def extract_quotation(s):
return (re.findall(r'"(.*?)"', s))
6
619
Write a Python function to move all the numbers to the end of the given string.
"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.
def move_num(s):
res = '' dig = '' for ele in s: if ele.isdigit(): dig += ele else: res += ele res += dig return (res)
7
602
Write a Python function to find the first repeated character in a given string.
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.
def first_repeated_char(s):
for index,c in enumerate(s): if s[:index+1].count(c) > 1: return c
8
616
Write a Python function which takes two tuples of the same length and performs the element wise modulo.
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.
def tuple_modulo(tup1, tup2):
res = tuple(el1 % el2 for el1, el2 in zip(tup1, tup2)) return (res)
9
641
Write a Python function to find the nth nonagonal number.
The function can be implemented using the formula: n*(7*n - 5)/2.
def is_nonagonal(n):
return int(n * (7 * n - 5) / 2)
10
784
Write a Python function to find the first even and odd number of a given list.
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.
def even_odd(l):
first_even = next((el for el in l if el%2==0),-1) first_odd = next((el for el in l if el%2!=0),-1) return first_even, first_odd
11
785
Write a Python function to convert tuple string to integer tuple.
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.
def tuple_str_int(test_str):
res = tuple(int(num) for num in test_str.replace('(', '').replace(')', '').replace('...', '').split(', ')) return (res)
12
574
Write a Python function to compute the surface area of a cylinder.
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.
def surfacearea_cylinder(r,h):
surfacearea=((2*3.1415*r)*h) + ((3.1415*r**2)*2) return surfacearea
13
786
Write a Python function to locate the right insertion point for a specified value in sorted order.
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.
import bisect
def right_insertion(l, x):
return bisect.bisect_right(l, x)
14
566
Write a Python function to get the sum of the digits of a non-negative integer.
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.
def sum_digits(n):
if n == 0: return 0 else: return n % 10 + sum_digits(int(n / 10))
15
603
Write a Python function to get lucid numbers smaller than or equal to a given integer.
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.
def get_ludic(n):
ludics = [] for i in range(1, n + 1): ludics.append(i) index = 1 while(index != len(ludics)): first_ludic = ludics[index] remove_index = index + first_ludic while(remove_index < len(ludics)): ludics.remove(ludics[remove_index]) remove_index = remove_index + first_ludic - 1 index += 1 return ludics

No dataset card yet

New: Create and edit this dataset card directly on the website!

Contribute a Dataset Card
Downloads last month
63
Add dataset card