INSTRUCTION
stringlengths 78
1.13k
| RESPONSE
stringlengths 36
906
| SOURCE
stringclasses 2
values |
---|---|---|
Write a Python function which follows this instruction: Write a function to sort the given list. | import heapq as hq
def heap_sort(iterable):
h = []
for value in iterable:
hq.heappush(h, value)
return [hq.heappop(h) for i in range(len(h))] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check whether the given amount has no profit and no loss | def noprofit_noloss(actual_cost,sale_amount):
if(sale_amount == actual_cost):
return True
else:
return False | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to calculate the wind chill index rounded to the next integer given the wind velocity in km/h and a temperature in celsius. | import math
def wind_chill(v,t):
windchill = 13.12 + 0.6215*t - 11.37*math.pow(v, 0.16) + 0.3965*t*math.pow(v, 0.16)
return int(round(windchill, 0)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to sum the length of the names of a given list of names after removing the names that start with a lowercase letter. | def sample_nam(sample_names):
sample_names=list(filter(lambda el:el[0].isupper() and el[1:].islower(),sample_names))
return len(''.join(sample_names)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to remove the parenthesis and what is inbetween them from a string. | import re
def remove_parenthesis(items):
for item in items:
return (re.sub(r" ?\([^)]+\)", "", item)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the nth nonagonal number. | def is_nonagonal(n):
return int(n * (7 * n - 5) / 2) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that checks if a strings contains 'z', except at the start and end of the word. | import re
def text_match_wordz_middle(text):
return bool(re.search(r'\Bz\B', text)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to reverse an array upto a given position. | def reverse_Array_Upto_K(input, k):
return (input[k-1::-1] + input[k:]) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to add a dictionary to the tuple. The output should be a tuple. | def add_dict_to_tuple(test_tup, test_dict):
test_tup = list(test_tup)
test_tup.append(test_dict)
test_tup = tuple(test_tup)
return (test_tup) | mbpp_sanitized |
Write a Python function which follows this instruction: Given a square matrix of size N*N given as a list of lists, where each cell is associated with a specific cost. A path is defined as a specific sequence of cells that starts from the top-left cell move only right or down and ends on bottom right cell. We want to find a path with the maximum average over all existing paths. Average is computed as total cost divided by the number of cells visited in the path. | def maxAverageOfPath(cost):
N = len(cost)
dp = [[0 for i in range(N + 1)] for j in range(N + 1)]
dp[0][0] = cost[0][0]
for i in range(1, N):
dp[i][0] = dp[i - 1][0] + cost[i][0]
for j in range(1, N):
dp[0][j] = dp[0][j - 1] + cost[0][j]
for i in range(1, N):
for j in range(1, N):
dp[i][j] = max(dp[i - 1][j],
dp[i][j - 1]) + cost[i][j]
return dp[N - 1][N - 1] / (2 * N - 1) | mbpp_sanitized |
Write a Python function which follows this instruction: The input is given as - a dictionary with a student name as a key and a tuple of float (student_height, student_weight) as a value, - minimal height, - minimal weight. Write a function to filter students that have height and weight above the minimum. | def filter_data(students,h,w):
result = {k: s for k, s in students.items() if s[0] >=h and s[1] >=w}
return result | mbpp_sanitized |
Write a Python function which follows this instruction: The input is defined as two lists of the same length. Write a function to count indices where the lists have the same values. | from operator import eq
def count_same_pair(nums1, nums2):
result = sum(map(eq, nums1, nums2))
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes base and power as arguments and calculate the sum of all digits of the base to the specified power. | def power_base_sum(base, power):
return sum([int(i) for i in str(pow(base, power))]) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to extract values between quotation marks " " of the given string. | import re
def extract_quotation(text1):
return (re.findall(r'"(.*?)"', text1)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes as input a tuple of numbers (t_1,...,t_{N+1}) and returns a tuple of length N where the i-th element of the tuple is equal to t_i * t_{i+1}. | def multiply_elements(test_tup):
res = tuple(i * j for i, j in zip(test_tup, test_tup[1:]))
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function takes as input two lists [a_1,...,a_n], [b_1,...,b_n] and returns [a_1+b_1,...,a_n+b_n]. | def sum_list(lst1,lst2):
res_list = [lst1[i] + lst2[i] for i in range(len(lst1))]
return res_list | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to remove consecutive duplicates of a given list. | from itertools import groupby
def consecutive_duplicates(nums):
return [key for key, group in groupby(nums)] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the lateral surface area of a cone given radius r and the height h. | import math
def lateralsurface_cone(r,h):
l = math.sqrt(r * r + h * h)
LSA = math.pi * r * l
return LSA | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to replace all occurrences of spaces, commas, or dots with a colon. | import re
def replace_specialchar(text):
return (re.sub("[ ,.]", ":", text))
| mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the index of the first occurrence of a given number in a sorted array. | def find_first_occurrence(A, x):
(left, right) = (0, len(A) - 1)
result = -1
while left <= right:
mid = (left + right) // 2
if x == A[mid]:
result = mid
right = mid - 1
elif x < A[mid]:
right = mid - 1
else:
left = mid + 1
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find sum of products of all possible sublists of a given list. https://www.geeksforgeeks.org/sum-of-products-of-all-possible-subarrays/ | def sum_Of_Subarray_Prod(arr):
ans = 0
res = 0
i = len(arr) - 1
while (i >= 0):
incr = arr[i]*(1 + res)
ans += incr
res = incr
i -= 1
return (ans) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to toggle bits of the number except the first and the last bit. https://www.geeksforgeeks.org/toggle-bits-number-expect-first-last-bits/ | def set_middle_bits(n):
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return (n >> 1) ^ 1
def toggle_middle_bits(n):
if (n == 1):
return 1
return n ^ set_middle_bits(n) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to locate the left insertion point for a specified value in sorted order. https://www.w3resource.com/python-exercises/data-structures-and-algorithms/python-data-structure-exercise-24.php | import bisect
def left_insertion(a, x):
i = bisect.bisect_left(a, x)
return i | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check whether the given string is starting with a vowel or not using regex. | import re
regex = '^[aeiouAEIOU][A-Za-z0-9_]*'
def check_str(string):
return re.search(regex, string) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to calculate the geometric sum of n-1. https://www.w3resource.com/python-exercises/data-structures-and-algorithms/python-recursion-exercise-9.php | def geometric_sum(n):
if n < 0:
return 0
else:
return 1 / (pow(2, n)) + geometric_sum(n - 1) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the index of smallest triangular number with n digits. https://www.geeksforgeeks.org/index-of-smallest-triangular-number-with-n-digits/ | import math
def find_Index(n):
x = math.sqrt(2 * math.pow(10,(n - 1)))
return round(x) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert the given tuple to a key-value dictionary using adjacent elements. https://www.geeksforgeeks.org/python-convert-tuple-to-adjacent-pair-dictionary/ | def tuple_to_dict(test_tup):
res = dict(test_tup[idx : idx + 2] for idx in range(0, len(test_tup), 2))
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to check whether all the characters are same or not. | def all_Characters_Same(s) :
n = len(s)
for i in range(1,n) :
if s[i] != s[0] :
return False
return True | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to caluclate the area of a tetrahedron. | import math
def area_tetrahedron(side):
area = math.sqrt(3)*(side*side)
return area | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to rotate a given list by specified number of items to the right direction. https://www.geeksforgeeks.org/python-program-right-rotate-list-n/ | def rotate_right(list, m):
result = list[-m:] + list[:-m]
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check if the given tuple has any none value or not. | def check_none(test_tup):
res = any(map(lambda ele: ele is None, test_tup))
return res | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find numbers within a given range from startnum ti endnum where every number is divisible by every digit it contains. https://www.w3resource.com/python-exercises/lambda/python-lambda-exercise-24.php | def divisible_by_digits(startnum, endnum):
return [n for n in range(startnum, endnum+1) \
if not any(map(lambda x: int(x) == 0 or n%int(x) != 0, str(n)))] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find area of a sector. The function takes the radius and angle as inputs. Function should return None if the angle is larger than 360 degrees. | import math
def sector_area(r,a):
if a > 360:
return None
return (math.pi*r**2) * (a/360) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the longest common subsequence for the given three string sequence. https://www.geeksforgeeks.org/lcs-longest-common-subsequence-three-strings/ | def lcs_of_three(X, Y, Z):
m = len(X)
n = len(Y)
o = len(Z)
L = [[[0 for i in range(o+1)] for j in range(n+1)] for k in range(m+1)]
for i in range(m+1):
for j in range(n+1):
for k in range(o+1):
if (i == 0 or j == 0 or k == 0):
L[i][j][k] = 0
elif (X[i-1] == Y[j-1] and X[i-1] == Z[k-1]):
L[i][j][k] = L[i-1][j-1][k-1] + 1
else:
L[i][j][k] = max(max(L[i-1][j][k], L[i][j-1][k]), L[i][j][k-1])
return L[m][n][o] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to put spaces between words starting with capital letters in a given string. | import re
def capital_words_spaces(str1):
return re.sub(r"(\w)([A-Z])", r"\1 \2", str1) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to sort a given list of strings of numbers numerically. https://www.geeksforgeeks.org/python-sort-numeric-strings-in-a-list/ | def sort_numeric_strings(nums_str):
result = [int(x) for x in nums_str]
result.sort()
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to add the given tuple to the given list. | def add_tuple(test_list, test_tup):
test_list += test_tup
return test_list | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check if the given array represents min heap or not. https://www.geeksforgeeks.org/how-to-check-if-a-given-array-represents-a-binary-heap/ | def check_min_heap_helper(arr, i):
if 2 * i + 2 > len(arr):
return True
left_child = (arr[i] <= arr[2 * i + 1]) and check_min_heap_helper(arr, 2 * i + 1)
right_child = (2 * i + 2 == len(arr)) or (arr[i] <= arr[2 * i + 2]
and check_min_heap_helper(arr, 2 * i + 2))
return left_child and right_child
def check_min_heap(arr):
return check_min_heap_helper(arr, 0) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the nth jacobsthal number. https://www.geeksforgeeks.org/jacobsthal-and-jacobsthal-lucas-numbers/ 0, 1, 1, 3, 5, 11, 21, 43, 85, 171, 341, 683, 1365, 2731, ... | def jacobsthal_num(n):
dp = [0] * (n + 1)
dp[0] = 0
dp[1] = 1
for i in range(2, n+1):
dp[i] = dp[i - 1] + 2 * dp[i - 2]
return dp[n] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find minimum k records from tuple list. https://www.geeksforgeeks.org/python-find-minimum-k-records-from-tuple-list/ - in this case a verbatim copy of test cases | def min_k(test_list, K):
res = sorted(test_list, key = lambda x: x[1])[:K]
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: We say that an element is common for lists l1, l2, l3 if it appears in all three lists under the same index. Write a function to find common elements from three lists. The function should return a list. | def extract_index_list(l1, l2, l3):
result = []
for m, n, o in zip(l1, l2, l3):
if (m == n == o):
result.append(m)
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the second smallest number in a list. | def second_smallest(numbers):
unique_numbers = list(set(numbers))
unique_numbers.sort()
if len(unique_numbers) < 2:
return None
else:
return unique_numbers[1] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that matches a string that has an 'a' followed by one or more 'b's. https://www.w3resource.com/python-exercises/re/python-re-exercise-3.php | import re
def text_match_zero_one(text):
patterns = 'ab+?'
if re.search(patterns, text):
return True
else:
return False | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count the pairs of reverse strings in the given string list. https://www.geeksforgeeks.org/python-program-to-count-the-pairs-of-reverse-strings/ | def count_reverse_pairs(test_list):
res = sum([1 for idx in range(0, len(test_list)) for idxn in range(idx, len(
test_list)) if test_list[idxn] == str(''.join(list(reversed(test_list[idx]))))])
return res | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count lists within a list. The function should return a dictionary where every list is converted to a tuple and the value of such tuple is the number of its occurencies in the original list. | def unique_sublists(list1):
result ={}
for l in list1:
result.setdefault(tuple(l), list()).append(1)
for a, b in result.items():
result[a] = sum(b)
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check whether a given string is a decimal number with a precision of 2. | def is_decimal(num):
import re
dnumre = re.compile(r"""^[0-9]+(\.[0-9]{1,2})?$""")
result = dnumre.search(num)
return bool(result) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to check whether a list of numbers contains only one distinct element or not. | def unique_Element(arr):
s = set(arr)
return len(s) == 1 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check whether the given month number contains 30 days or not. Months are given as number from 1 to 12. | def check_monthnumber_number(monthnum3):
return monthnum3==4 or monthnum3==6 or monthnum3==9 or monthnum3==11 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the minimum difference between any two elements in a given array. https://www.geeksforgeeks.org/find-minimum-difference-pair/ | def find_min_diff(arr,n):
arr = sorted(arr)
diff = 10**20
for i in range(n-1):
if arr[i+1] - arr[i] < diff:
diff = arr[i+1] - arr[i]
return diff | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to count number of digits in a given string. | def number_ctr(str):
number_ctr= 0
for i in range(len(str)):
if str[i] >= '0' and str[i] <= '9': number_ctr += 1
return number_ctr | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find nth polite number. geeksforgeeks.org/n-th-polite-number/ | import math
def is_polite(n):
n = n + 1
return (int)(n+(math.log((n + math.log(n, 2)), 2))) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to return a list of all pairs of consecutive items in a given list. | def pair_wise(l1):
temp = []
for i in range(len(l1) - 1):
current_element, next_element = l1[i], l1[i + 1]
x = (current_element, next_element)
temp.append(x)
return temp | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to count the number of pairs whose sum is equal to ‘sum’. The funtion gets as input a list of numbers and the sum, | def get_pairs_count(arr, sum):
count = 0
for i in range(len(arr)):
for j in range(i + 1,len(arr)):
if arr[i] + arr[j] == sum:
count += 1
return count | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to get the difference between two lists. | def Diff(li1,li2):
return list(set(li1)-set(li2)) + list(set(li2)-set(li1))
| mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the sum of fourth power of first n odd natural numbers. | def odd_num_sum(n) :
j = 0
sm = 0
for i in range(1,n + 1) :
j = (2*i-1)
sm = sm + (j*j*j*j)
return sm | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check if the given expression is balanced or not. https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/ | from collections import deque
def check_expression(exp):
if len(exp) & 1:
return False
stack = deque()
for ch in exp:
if ch == '(' or ch == '{' or ch == '[':
stack.append(ch)
if ch == ')' or ch == '}' or ch == ']':
if not stack:
return False
top = stack.pop()
if (top == '(' and ch != ')') or (top == '{' and ch != '}' or (top == '[' and ch != ']')):
return False
return not stack | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to remove all the words with k length in the given string. | def remove_length(test_str, K):
temp = test_str.split()
res = [ele for ele in temp if len(ele) != K]
res = ' '.join(res)
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the occurrence and position of the substrings within a string. Return None if there is no match. | import re
def occurance_substring(text,pattern):
for match in re.finditer(pattern, text):
s = match.start()
e = match.end()
return (text[s:e], s, e) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to check whether every odd index contains odd numbers of a given list. | def odd_position(nums):
return all(nums[i]%2==i%2 for i in range(len(nums))) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count those characters which have vowels as their neighbors in the given string. | def count_vowels(test_str):
res = 0
vow_list = ['a', 'e', 'i', 'o', 'u']
for idx in range(1, len(test_str) - 1):
if test_str[idx] not in vow_list and (test_str[idx - 1] in vow_list or test_str[idx + 1] in vow_list):
res += 1
if test_str[0] not in vow_list and test_str[1] in vow_list:
res += 1
if test_str[-1] not in vow_list and test_str[-2] in vow_list:
res += 1
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the sum of non-repeated elements in a given list. | def find_sum(arr):
arr.sort()
sum = arr[0]
for i in range(len(arr)-1):
if (arr[i] != arr[i+1]):
sum = sum + arr[i+1]
return sum | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to pack consecutive duplicates of a given list elements into sublists. | from itertools import groupby
def pack_consecutive_duplicates(list1):
return [list(group) for key, group in groupby(list1)] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count the number of lists within a list. The function should return a dictionary, where every list is turned to a tuple, and the value of the tuple is the number of its occurrences. | def unique_sublists(list1):
result ={}
for l in list1:
result.setdefault(tuple(l), list()).append(1)
for a, b in result.items():
result[a] = sum(b)
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the combinations of sums with tuples in the given tuple list. https://www.geeksforgeeks.org/python-combinations-of-sum-with-tuples-in-tuple-list/ | from itertools import combinations
def find_combinations(test_list):
res = [(b1 + a1, b2 + a2) for (a1, a2), (b1, b2) in combinations(test_list, 2)]
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to check whether the count of divisors is even. https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-24.php | import math
def count_divisors(n) :
count = 0
for i in range(1, (int)(math.sqrt(n)) + 2) :
if (n % i == 0) :
if( n // i == i) :
count = count + 1
else :
count = count + 2
return count % 2 == 0 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the sum of all odd length subarrays. https://www.geeksforgeeks.org/sum-of-all-odd-length-subarrays/ | def odd_length_sum(arr):
Sum = 0
l = len(arr)
for i in range(l):
Sum += ((((i + 1) *(l - i) + 1) // 2) * arr[i])
return Sum | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert rgb color to hsv color. https://www.geeksforgeeks.org/program-change-rgb-color-model-hsv-color-model/ | def rgb_to_hsv(r, g, b):
r, g, b = r/255.0, g/255.0, b/255.0
mx = max(r, g, b)
mn = min(r, g, b)
df = mx-mn
if mx == mn:
h = 0
elif mx == r:
h = (60 * ((g-b)/df) + 360) % 360
elif mx == g:
h = (60 * ((b-r)/df) + 120) % 360
elif mx == b:
h = (60 * ((r-g)/df) + 240) % 360
if mx == 0:
s = 0
else:
s = (df/mx)*100
v = mx*100
return h, s, v | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the product of first even and odd number of a given list. | def mul_even_odd(list1):
first_even = next((el for el in list1 if el%2==0),-1)
first_odd = next((el for el in list1 if el%2!=0),-1)
return (first_even*first_odd) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert tuple string to integer tuple. | def tuple_str_int(test_str):
res = tuple(int(num) for num in test_str.replace('(', '').replace(')', '').replace('...', '').split(', '))
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to locate the right insertion point for a specified value in sorted order. | import bisect
def right_insertion(a, x):
return bisect.bisect_right(a, x) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that matches a string that has an a followed by three 'b'. | import re
def text_match_three(text):
patterns = 'ab{3}?'
return re.search(patterns, text) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to create a new tuple from the given string and list. | def new_tuple(test_list, test_str):
return tuple(test_list + [test_str]) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to check whether every even index contains even numbers of a given list. | def even_position(nums):
return all(nums[i]%2==i%2 for i in range(len(nums))) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to remove tuples from the given tuple. | def remove_nested(test_tup):
res = tuple()
for count, ele in enumerate(test_tup):
if not isinstance(ele, tuple):
res = res + (ele, )
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to count the number of lists in a given number of lists. | def count_list(input_list):
return len(input_list) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the last position of an element in a sorted array. | 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 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that matches a string that has an 'a' followed by anything, ending in 'b'. | import re
def text_starta_endb(text):
patterns = 'a.*?b$'
return re.search(patterns, text) | mbpp_sanitized |
Write a Python function which follows this instruction: Write function to find the sum of all items in the given dictionary. | def return_sum(dict):
sum = 0
for i in dict.values():
sum = sum + i
return sum | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the sum of all odd natural numbers within the range l and r. | def sum_odd(n):
terms = (n + 1)//2
sum1 = terms * terms
return sum1
def sum_in_range(l,r):
return sum_odd(r) - sum_odd(l - 1) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the sum of an array. | def _sum(arr):
sum=0
for i in arr:
sum = sum + i
return(sum) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to that rotate left bits by d bits a given number. We assume that the number is 32 bit. | def left_rotate(n,d):
INT_BITS = 32
return (n << d)|(n >> (INT_BITS - d)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to remove all whitespaces from a string. | import re
def remove_all_spaces(text):
return (re.sub(r'\s+', '',text)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to count the number of equal numbers from three given integers. | def test_three_equal(x,y,z):
result = set([x,y,z])
if len(result)==3:
return 0
else:
return 4-len(result) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to count the number of rotations required to generate a sorted array. https://www.geeksforgeeks.org/count-of-rotations-required-to-generate-a-sorted-array/ | def count_rotation(arr):
for i in range (1,len(arr)):
if (arr[i] < arr[i - 1]):
return i
return 0 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check whether the given number is a perfect square or not. https://www.geeksforgeeks.org/check-if-given-number-is-perfect-square-in-cpp/ | def is_perfect_square(n) :
i = 1
while (i * i<= n):
if ((n % i == 0) and (n / i == i)):
return True
i = i + 1
return False | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check whether the product of numbers in a list is even or not. | def is_product_even(arr):
for i in range(len(arr)):
if (arr[i] & 1) == 0:
return True
return False | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that returns the list in a list of lists whose sum of elements is the highest. | def max_sum_list(lists):
return max(lists, key=sum) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find maximum run of uppercase characters in the given string. | def max_run_uppercase(test_str):
cnt = 0
res = 0
for idx in range(0, len(test_str)):
if test_str[idx].isupper():
cnt += 1
else:
res = cnt
cnt = 0
if test_str[len(test_str) - 1].isupper():
res = cnt
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the first odd number in a given list of numbers. | def first_odd(nums):
first_odd = next((el for el in nums if el%2!=0),-1)
return first_odd | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check if the given tuples contain the k or not. | def check_K(test_tup, K):
res = False
for ele in test_tup:
if ele == K:
res = True
break
return res | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check if each element of second tuple is smaller than its corresponding element in the first tuple. | def check_smaller(test_tup1, test_tup2):
return all(x > y for x, y in zip(test_tup1, test_tup2)) | mbpp_sanitized |