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 ind... | 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 filte... | 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... | 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.... | 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 ... | 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... | 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 seco... | 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... | 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 t... | 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 resulti... | 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 ca... | 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 ... |
No dataset card yet
- Downloads last month
- 48