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 find the volume of a sphere. | import math
def volume_sphere(r):
volume=(4/3)*math.pi*r*r*r
return volume | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the character made by adding the ASCII value of all the characters of the given string modulo 26. | def get_Char(strr):
summ = 0
for i in range(len(strr)):
summ += (ord(strr[i]) - ord('a') + 1)
if (summ % 26 == 0):
return ord('z')
else:
summ = summ % 26
return chr(ord('a') + summ - 1) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the nth number in the newman conway sequence. | def sequence(n):
if n == 1 or n == 2:
return 1
else:
return sequence(sequence(n-1)) + sequence(n-sequence(n-1)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the surface area of a sphere. | import math
def surfacearea_sphere(r):
surfacearea=4*math.pi*r*r
return surfacearea | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find nth centered hexagonal number. | def centered_hexagonal_number(n):
return 3 * n * (n - 1) + 1 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to merge three dictionaries into a single dictionary. | import collections as ct
def merge_dictionaries_three(dict1,dict2, dict3):
merged_dict = dict(ct.ChainMap({},dict1,dict2,dict3))
return merged_dict | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to get the frequency of all the elements in a list, returned as a dictionary. | import collections
def freq_count(list1):
freq_count= collections.Counter(list1)
return freq_count | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the closest smaller number than n. | def closest_num(N):
return (N - 1) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the length of the longest word. | def len_log(list1):
max=len(list1[0])
for i in list1:
if len(i)>max:
max=len(i)
return max | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check if a string is present as a substring in a given list of string values. | def find_substring(str1, sub_str):
if any(sub_str in s for s in str1):
return True
return False | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check whether the given number is undulating or not. | def is_undulating(n):
n = str(n)
if (len(n) <= 2):
return False
for i in range(2, len(n)):
if (n[i - 2] != n[i]):
return False
return True | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to calculate the value of 'a' to the power 'b'. | def power(a,b):
if b==0:
return 1
elif a==0:
return 0
elif b==1:
return a
else:
return a*power(a,b-1) | mbpp_sanitized |
Write a Python function which follows this instruction: Given a list of tuples, write a function that returns the first value of the tuple with the smallest second value. | from operator import itemgetter
def index_minimum(test_list):
res = min(test_list, key = itemgetter(1))[0]
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the length of the smallest list in a list of lists. | def Find_Min_Length(lst):
minLength = min(len(x) for x in lst )
return minLength | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the number of divisors of a given integer. | def divisor(n):
for i in range(n):
x = len([i for i in range(1,n+1) if not n % i])
return x | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find frequency of each element in a flattened list of lists, returned in a dictionary. | def frequency_lists(list1):
list1 = [item for sublist in list1 for item in sublist]
dic_data = {}
for num in list1:
if num in dic_data.keys():
dic_data[num] += 1
else:
key = num
value = 1
dic_data[key] = value
return dic_data
| mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to multiply all the numbers in a list and divide with the length of the list. | def multiply_num(numbers):
total = 1
for x in numbers:
total *= x
return total/len(numbers) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert the given decimal number to its binary equivalent, represented as a string with no leading zeros. | def decimal_to_binary(n):
return bin(n).replace("0b","") | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the next smallest palindrome of a specified integer, returned as an integer. | import sys
def next_smallest_palindrome(num):
numstr = str(num)
for i in range(num+1,sys.maxsize):
if str(i) == str(i)[::-1]:
return i | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the kth element in the given array using 1-based indexing. | def kth_element(arr, k):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] == arr[j+1], arr[j]
return arr[k-1] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert a snake case string to camel case string. | def snake_to_camel(word):
import re
return ''.join(x.capitalize() or '_' for x in word.split('_')) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the Eulerian number a(n, m). | def eulerian_num(n, m):
if (m >= n or n == 0):
return 0
if (m == 0):
return 1
return ((n - m) * eulerian_num(n - 1, m - 1) +(m + 1) * eulerian_num(n - 1, m)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to sort each sublist of strings in a given list of lists. | def sort_sublists(input_list):
result = [sorted(x, key = lambda x:x[0]) for x in input_list]
return result
| mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to count true booleans in the given list. | def count(lst):
return sum(lst) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to append the given list to the given tuples. | def add_lists(test_list, test_tup):
res = tuple(list(test_tup) + test_list)
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to merge three lists into a single sorted list. | import heapq
def merge_sorted_list(num1,num2,num3):
num1=sorted(num1)
num2=sorted(num2)
num3=sorted(num3)
result = heapq.merge(num1,num2,num3)
return list(result) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the number of numbers with an odd value when rotating a binary string the given number of times. | def odd_Equivalent(s,n):
count=0
for i in range(0,n):
if (s[i] == '1'):
count = count + 1
return count | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the common elements in given nested lists. | def common_in_nested_lists(nestedlist):
result = list(set.intersection(*map(set, nestedlist)))
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check if a string represents an integer or not. | def check_integer(text):
text = text.strip()
if len(text) < 1:
return None
else:
if all(text[i] in "0123456789" for i in range(len(text))):
return True
elif (text[0] in "+-") and \
all(text[i] in "0123456789" for i in range(1,len(text))):
return True
else:
return False | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check whether all dictionaries in a list are empty or not. | def empty_dit(list1):
empty_dit=all(not d for d in list1)
return empty_dit | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert a given tuple of positive integers into a single integer. | def tuple_to_int(nums):
result = int(''.join(map(str,nums)))
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert all possible convertible elements in a list of lists to floats. | def list_to_float(test_list):
res = []
for tup in test_list:
temp = []
for ele in tup:
if ele.isalpha():
temp.append(ele)
else:
temp.append(float(ele))
res.append((temp[0],temp[1]))
return res | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert a string to a list of strings split on the space character. | def string_to_list(string):
lst = list(string.split(" "))
return lst | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the element that appears only once in a sorted array. | def search(arr):
n = len(arr)
XOR = 0
for i in range(n) :
XOR = XOR ^ arr[i]
return (XOR) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the maximum absolute product between numbers in pairs of tuples within a given list. | def max_product_tuple(list1):
result_max = max([abs(x * y) for x, y in list1] )
return result_max | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to sum all amicable numbers from 1 to a specified number. | def amicable_numbers_sum(limit):
if not isinstance(limit, int):
return "Input is not an integer!"
if limit < 1:
return "Input must be bigger than 0!"
amicables = set()
for num in range(2, limit+1):
if num in amicables:
continue
sum_fact = sum([fact for fact in range(1, num) if num % fact == 0])
sum_fact2 = sum([fact for fact in range(1, sum_fact) if sum_fact % fact == 0])
if num == sum_fact2 and num != sum_fact:
amicables.add(num)
amicables.add(sum_fact2)
return sum(amicables) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to get the angle of a complex number. | import cmath
def angle_complex(a,b):
cn=complex(a,b)
angle=cmath.phase(a+b)
return angle | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the maximum difference between the number of 0s and number of 1s in any sub-string of the given binary string. | def find_length(string):
n = len(string)
current_sum = 0
max_sum = 0
for i in range(n):
current_sum += (1 if string[i] == '0' else -1)
if current_sum < 0:
current_sum = 0
max_sum = max(current_sum, max_sum)
return max_sum if max_sum else 0 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the sum of common divisors of two given numbers. | def sum(a,b):
sum = 0
for i in range (1,min(a,b)):
if (a % i == 0 and b % i == 0):
sum += i
return sum | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to multiply two integers. | def multiply_int(x, y):
if y < 0:
return -multiply_int(x, -y)
elif y == 0:
return 0
elif y == 1:
return x
else:
return x + multiply_int(x, y - 1) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find words that are longer than n characters from a given list of words. | def long_words(n, str):
word_len = []
txt = str.split(" ")
for x in txt:
if len(x) > n:
word_len.append(x)
return word_len | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to calculate whether the matrix is a magic square. | def magic_square_test(my_matrix):
iSize = len(my_matrix[0])
sum_list = []
sum_list.extend([sum (lines) for lines in my_matrix])
for col in range(iSize):
sum_list.append(sum(row[col] for row in my_matrix))
result1 = 0
for i in range(0,iSize):
result1 +=my_matrix[i][i]
sum_list.append(result1)
result2 = 0
for i in range(iSize-1,-1,-1):
result2 +=my_matrix[i][i]
sum_list.append(result2)
if len(set(sum_list))>1:
return False
return True | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the item with maximum frequency in a given list. | from collections import defaultdict
def max_occurrences(nums):
dict = defaultdict(int)
for i in nums:
dict[i] += 1
result = max(dict.items(), key=lambda x: x[1])
return result[0] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to reverse only the vowels of a given string (where y is not a vowel). | def reverse_vowels(str1):
vowels = ""
for char in str1:
if char in "aeiouAEIOU":
vowels += char
result_string = ""
for char in str1:
if char in "aeiouAEIOU":
result_string += vowels[-1]
vowels = vowels[:-1]
else:
result_string += char
return result_string | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to convert a tuple to a string. | def tup_string(tup1):
str = ''.join(tup1)
return str | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to calculate the sum of the negative numbers of a given list of numbers. | def sum_negativenum(nums):
sum_negativenum = list(filter(lambda nums:nums<0,nums))
return sum(sum_negativenum) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the nth hexagonal number. | def hexagonal_num(n):
return n*(2*n - 1) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the ratio of zeroes to non-zeroes in an array of integers. | from array import array
def zero_count(nums):
n = len(nums)
n1 = 0
for x in nums:
if x == 0:
n1 += 1
else:
None
return n1/(n-n1) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to check whether the given number can be represented as sum of non-zero powers of 2 or not. | def is_Sum_Of_Powers_Of_Two(n):
if (n % 2 == 1):
return False
else:
return True | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the circumference of a circle. | def circle_circumference(r):
perimeter=2*3.1415*r
return perimeter | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to flatten the list of lists into a single set of numbers. | def extract_singly(test_list):
res = []
temp = set()
for inner in test_list:
for ele in inner:
if not ele in temp:
temp.add(ele)
res.append(ele)
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to sort a list of elements. | def pancake_sort(nums):
arr_len = len(nums)
while arr_len > 1:
mi = nums.index(max(nums[0:arr_len]))
nums = nums[mi::-1] + nums[mi+1:len(nums)]
nums = nums[arr_len-1::-1] + nums[arr_len:len(nums)]
arr_len -= 1
return nums | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count number items that are identical in the same position of three given lists. | def count_samepair(list1,list2,list3):
result = sum(m == n == o for m, n, o in zip(list1,list2,list3))
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find number of lists present in the given tuple. | def find_lists(Input):
if isinstance(Input, list):
return 1
else:
return len(Input) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the maximum difference between any two elements in a given array. | def max_Abs_Diff(arr):
n = len(arr)
minEle = arr[0]
maxEle = arr[0]
for i in range(1, n):
minEle = min(minEle,arr[i])
maxEle = max(maxEle,arr[i])
return (maxEle - minEle) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that returns integers x and y that satisfy ax + by = n as a tuple, or return None if no solution exists. | def find_solution(a, b, n):
i = 0
while i * a <= n:
if (n - (i * a)) % b == 0:
return (i, (n - (i * a)) // b)
i = i + 1
return None | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to remove all elements from a given list present in another list. | def remove_elements(list1, list2):
result = [x for x in list1 if x not in list2]
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to calculate the sum (n - 2*i) from i=0 to n // 2, for instance n + (n-2) + (n-4)... (until n-x =< 0). | def sum_series(n):
if n < 1:
return 0
else:
return n + sum_series(n - 2) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to calculate the area of a regular polygon given the length and number of its sides. | from math import tan, pi
def area_polygon(s, l):
area = s * (l ** 2) / (4 * tan(pi / s))
return area | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to determine if the sum of the divisors of two integers are the same. | import math
def div_sum(n):
total = 1
i = 2
while i * i <= n:
if (n % i == 0):
total = (total + i + math.floor(n / i))
i += 1
return total
def are_equivalent(num1, num2):
return div_sum(num1) == div_sum(num2); | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count the number of characters in a string that occur at the same position in the string as in the English alphabet (case insensitive). | def count_char_position(str1):
count_chars = 0
for i in range(len(str1)):
if ((i == ord(str1[i]) - ord('A')) or
(i == ord(str1[i]) - ord('a'))):
count_chars += 1
return count_chars | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that counts the number of pairs of integers in a list that xor to an even number. | def find_even_pair(A):
count = 0
for i in range(0, len(A)):
for j in range(i+1, len(A)):
if ((A[i] ^ A[j]) % 2 == 0):
count += 1
return count | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the smallest power of 2 greater than or equal to n. | def next_power_of_2(n):
if n and not n & (n - 1):
return n
count = 0
while n != 0:
n >>= 1
count += 1
return 1 << count; | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count the number of occurrences of a number in a given list. | def frequency(a,x):
count = 0
for i in a:
if i == x:
count += 1
return count | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the sum of numbers in a list within a range specified by two indices. | def sum_range_list(list1, m, n):
sum_range = 0
for i in range(m, n+1, 1):
sum_range += list1[i]
return sum_range | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the perimeter of a regular pentagon from the length of its sides. | import math
def perimeter_pentagon(a):
perimeter=(5*a)
return perimeter | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count the number of occurence of the string 'std' in a given string. | def count_occurance(s):
count = 0
for i in range(len(s) - 2):
if (s[i] == 's' and s[i+1] == 't' and s[i+2] == 'd'):
count = count + 1
return count | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to check if all the elements in tuple have same data type or not. | def check_type(test_tuple):
res = True
for ele in test_tuple:
if not isinstance(ele, type(test_tuple[0])):
res = False
break
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in a sorted array, its length (n), and an element and returns whether the element is the majority element in the given sorted array. (The majority element is the element that occurs more than n/2 times.) | def is_majority(arr, n, x):
i = binary_search(arr, 0, n-1, x)
if i == -1:
return False
if ((i + n//2) <= (n -1)) and arr[i + n//2] == x:
return True
else:
return False
def binary_search(arr, low, high, x):
if high >= low:
mid = (low + high)//2
if (mid == 0 or x > arr[mid-1]) and (arr[mid] == x):
return mid
elif x > arr[mid]:
return binary_search(arr, (mid + 1), high, x)
else:
return binary_search(arr, low, (mid -1), x)
return -1 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to count the number of set bits (binary digits with value 1) in a given number. | def count_Set_Bits(n):
count = 0
while (n):
count += n & 1
n >>= 1
return count | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to remove the characters which have odd index values of a given string. | def odd_values_string(str):
result = ""
for i in range(len(str)):
if i % 2 == 0:
result = result + str[i]
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find minimum of three numbers. | def min_of_three(a,b,c):
if (a <= b) and (a <= c):
smallest = a
elif (b <= a) and (b <= c):
smallest = b
else:
smallest = c
return smallest | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to check whether all the bits are unset in the given range or not. | def all_Bits_Set_In_The_Given_Range(n,l,r):
num = (((1 << r) - 1) ^ ((1 << (l - 1)) - 1))
new_num = n & num
if (new_num == 0):
return True
return False | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in an array and an integer n, and re-arranges the first n elements of the given array so that all negative elements appear before positive ones, and where the relative order among negative and positive elements is preserved. | def re_arrange_array(arr, n):
j=0
for i in range(0, n):
if (arr[i] < 0):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
j = j + 1
return arr | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in a string and character, replaces blank spaces in the string with the character, and returns the string. | def replace_blank(str1,char):
str2 = str1.replace(' ', char)
return str2 | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in a list and an integer n and returns a list containing the n largest items from the list. | import heapq
def larg_nnum(list1,n):
largest=heapq.nlargest(n,list1)
return largest | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the lateral surface area of a cylinder. | def lateralsuface_cylinder(r,h):
lateralsurface= 2*3.1415*r*h
return lateralsurface | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the volume of a cube given its side length. | def volume_cube(l):
volume = l * l * l
return volume | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to set all even bits of a given number. | def even_bit_set_number(n):
count = 0;res = 0;temp = n
while(temp > 0):
if (count % 2 == 1):
res |= (1 << count)
count+=1
temp >>= 1
return (n | res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in a list of tuples and returns a dictionary mapping each unique tuple to the number of times it occurs in the list. | from collections import Counter
def check_occurences(test_list):
res = dict(Counter(tuple(ele) for ele in map(sorted, test_list)))
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to count the number of non-empty substrings of a given string. | def number_of_substrings(str):
str_len = len(str);
return int(str_len * (str_len + 1) / 2); | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in positive integers m and n and finds the number of possible sequences of length n, such that each element is a positive integer and is greater than or equal to twice the previous element but less than or equal to m. | def get_total_number_of_sequences(m,n):
T=[[0 for i in range(n+1)] for i in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i==0 or j==0:
T[i][j]=0
elif i<j:
T[i][j]=0
elif j==1:
T[i][j]=i
else:
T[i][j]=T[i-1][j]+T[i//2][j-1]
return T[m][n] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in two lists and replaces the last element of the first list with the elements of the second list. | def replace_list(list1,list2):
list1[-1:] = list2
replace_list=list1
return replace_list
| mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to count the total number of characters in a string. | def count_charac(str1):
total = 0
for i in str1:
total = total + 1
return total | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to find the next perfect square greater than a given number. | import math
def next_Perfect_Square(N):
nextN = math.floor(math.sqrt(N)) + 1
return nextN * nextN | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes an array and finds the maximum sum of a bitonic subsequence for the given array, where a sequence is bitonic if it is first increasing and then decreasing. | def max_sum(arr):
MSIBS = arr[:]
for i in range(len(arr)):
for j in range(0, i):
if arr[i] > arr[j] and MSIBS[i] < MSIBS[j] + arr[i]:
MSIBS[i] = MSIBS[j] + arr[i]
MSDBS = arr[:]
for i in range(1, len(arr) + 1):
for j in range(1, i):
if arr[-i] > arr[-j] and MSDBS[-i] < MSDBS[-j] + arr[-i]:
MSDBS[-i] = MSDBS[-j] + arr[-i]
max_sum = float("-Inf")
for i, j, k in zip(MSIBS, MSDBS, arr):
max_sum = max(max_sum, i + j - k)
return max_sum | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function for computing square roots using the babylonian method. | def babylonian_squareroot(number):
if(number == 0):
return 0;
g = number/2.0;
g2 = g + 1;
while(g != g2):
n = number/ g;
g2 = g;
g = (g + n)/2;
return g; | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the length of the longest palindromic subsequence in the given string. | def lps(str):
n = len(str)
L = [[0 for x in range(n)] for x in range(n)]
for i in range(n):
L[i][i] = 1
for cl in range(2, n+1):
for i in range(n-cl+1):
j = i+cl-1
if str[i] == str[j] and cl == 2:
L[i][j] = 2
elif str[i] == str[j]:
L[i][j] = L[i+1][j-1] + 2
else:
L[i][j] = max(L[i][j-1], L[i+1][j]);
return L[0][n-1] | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in an integer n and calculates the harmonic sum of n-1. | def harmonic_sum(n):
if n < 2:
return 1
else:
return 1 / n + (harmonic_sum(n - 1)) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the intersection of two arrays. | def intersection_array(array_nums1,array_nums2):
result = list(filter(lambda x: x in array_nums1, array_nums2))
return result | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function that takes in a tuple and an element and counts the occcurences of the element in the tuple. | def count_X(tup, x):
count = 0
for ele in tup:
if (ele == x):
count = count + 1
return count | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in a list and an element and inserts the element before each element in the list, and returns the resulting list. | def insert_element(list,element):
list = [v for elt in list for v in (element, elt)]
return list | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function to convert complex numbers to polar coordinates. | import cmath
def convert(numbers):
num = cmath.polar(numbers)
return (num) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function that returns the number of integer elements in a given list. | def count_integer(list1):
ctr = 0
for i in list1:
if isinstance(i, int):
ctr = ctr + 1
return ctr | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in a list and length n, and generates all combinations (with repetition) of the elements of the list and returns a list with a tuple for each combination. | from itertools import combinations_with_replacement
def combinations_colors(l, n):
return list(combinations_with_replacement(l,n))
| mbpp_sanitized |
Write a Python function which follows this instruction: Write a python function that takes in a non-negative number and returns the number of prime numbers less than the given non-negative number. | def count_Primes_nums(n):
ctr = 0
for num in range(n):
if num <= 1:
continue
for i in range(2,num):
if (num % i) == 0:
break
else:
ctr += 1
return ctr | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in two numbers and returns a tuple with the second number and then the first number. | def swap_numbers(a,b):
temp = a
a = b
b = temp
return (a,b) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to maximize the given two tuples. | def maximize_elements(test_tup1, test_tup2):
res = tuple(tuple(max(a, b) for a, b in zip(tup1, tup2))
for tup1, tup2 in zip(test_tup1, test_tup2))
return (res) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function to find the nth newman–shanks–williams prime number. | def newman_prime(n):
if n == 0 or n == 1:
return 1
return 2 * newman_prime(n - 1) + newman_prime(n - 2) | mbpp_sanitized |
Write a Python function which follows this instruction: Write a function that takes in two tuples and performs mathematical division operation element-wise across the given tuples. | def division_elements(test_tup1, test_tup2):
res = tuple(ele1 // ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
return (res) | mbpp_sanitized |