Unnamed: 0
int64
0
973
source
stringlengths
37
249
target
stringlengths
30
1.33k
program_id
stringlengths
6
8
0
Write a function to find the minimum cost path to reach (m, n) from (0, 0) for the given cost matrix cost[][] and a position (m, n) in cost[][].
R = 3 C = 3 def min_cost(cost, m, n): tc = [[0 for x in range(C)] for x in range(R)] tc[0][0] = cost[0][0] for i in range(1, m+1): tc[i][0] = tc[i-1][0] + cost[i][0] for j in range(1, n+1): tc[0][j] = tc[0][j-1] + cost[0][j] for i in range(1, m+1): for j in range(1, n+1): tc[i][j] = min(tc[i-1][j-1], tc[i-1][j], tc[i][j-1]) + cost[i][j] return tc[m][n]
MBPP_0
1
Write a function to find the similar elements from the given two tuple lists.
def similar_elements(test_tup1, test_tup2): res = tuple(set(test_tup1) & set(test_tup2)) return (res)
MBPP_1
2
Write a python function to identify non-prime numbers.
import math def is_not_prime(n): result = False for i in range(2,int(math.sqrt(n)) + 1): if n % i == 0: result = True return result
MBPP_2
3
Write a function to find the largest integers from a given list of numbers using heap queue algorithm.
import heapq as hq def heap_queue_largest(nums,n): largest_nums = hq.nlargest(n, nums) return largest_nums
MBPP_3
4
Write a function to find the number of ways to fill it with 2 x 1 dominoes for the given 3 x n board.
def count_ways(n): A = [0] * (n + 1) B = [0] * (n + 1) A[0] = 1 A[1] = 0 B[0] = 0 B[1] = 1 for i in range(2, n+1): A[i] = A[i - 2] + 2 * B[i - 1] B[i] = A[i - 1] + B[i - 2] return A[n]
MBPP_4
5
Write a python function to check whether the two numbers differ at one bit position only or not.
def is_Power_Of_Two (x): return x and (not(x & (x - 1))) def differ_At_One_Bit_Pos(a,b): return is_Power_Of_Two(a ^ b)
MBPP_5
6
Write a function to find all words which are at least 4 characters long in a string by using regex.
import re def find_char_long(text): return (re.findall(r"\b\w{4,}\b", text))
MBPP_6
7
Write a function to find squares of individual elements in a list using lambda function.
def square_nums(nums): square_nums = list(map(lambda x: x ** 2, nums)) return square_nums
MBPP_7
8
Write a python function to find the minimum number of rotations required to get the same string.
def find_Rotations(str): tmp = str + str n = len(str) for i in range(1,n + 1): substring = tmp[i: i+n] if (str == substring): return i return n
MBPP_8
9
Write a function to get the n smallest items from a dataset.
import heapq def small_nnum(list1,n): smallest=heapq.nsmallest(n,list1) return smallest
MBPP_9
10
Write a python function to remove first and last occurrence of a given character from the string.
def remove_Occ(s,ch): for i in range(len(s)): if (s[i] == ch): s = s[0 : i] + s[i + 1:] break for i in range(len(s) - 1,-1,-1): if (s[i] == ch): s = s[0 : i] + s[i + 1:] break return s
MBPP_10
11
Write a function to sort a given matrix in ascending order according to the sum of its rows.
def sort_matrix(M): result = sorted(M, key=sum) return result
MBPP_11
12
Write a function to count the most common words in a dictionary.
from collections import Counter def count_common(words): word_counts = Counter(words) top_four = word_counts.most_common(4) return (top_four)
MBPP_12
13
Write a python function to find the volume of a triangular prism.
def find_Volume(l,b,h) : return ((l * b * h) / 2)
MBPP_13
14
Write a function to split a string at lowercase letters.
import re def split_lowerstring(text): return (re.findall('[a-z][^a-z]*', text))
MBPP_14
15
Write a function to find sequences of lowercase letters joined with an underscore.
import re def text_lowercase_underscore(text): patterns = '^[a-z]+_[a-z]+$' if re.search(patterns, text): return 'Found a match!' else: return('Not matched!')
MBPP_15
16
Write a function to find the perimeter of a square.
def square_perimeter(a): perimeter=4*a return perimeter
MBPP_16
17
Write a function to remove characters from the first string which are present in the second string.
NO_OF_CHARS = 256 def str_to_list(string): temp = [] for x in string: temp.append(x) return temp def lst_to_string(List): return ''.join(List) def get_char_count_array(string): count = [0] * NO_OF_CHARS for i in string: count[ord(i)] += 1 return count def remove_dirty_chars(string, second_string): count = get_char_count_array(second_string) ip_ind = 0 res_ind = 0 temp = '' str_list = str_to_list(string) while ip_ind != len(str_list): temp = str_list[ip_ind] if count[ord(temp)] == 0: str_list[res_ind] = str_list[ip_ind] res_ind += 1 ip_ind+=1 return lst_to_string(str_list[0:res_ind])
MBPP_17
18
Write a function to find whether a given array of integers contains any duplicate element.
def test_duplicate(arraynums): nums_set = set(arraynums) return len(arraynums) != len(nums_set)
MBPP_18
19
Write a function to check if the given number is woodball or not.
def is_woodall(x): if (x % 2 == 0): return False if (x == 1): return True x = x + 1 p = 0 while (x % 2 == 0): x = x/2 p = p + 1 if (p == x): return True return False
MBPP_19
20
Write a function to find m number of multiples of n.
def multiples_of_num(m,n): multiples_of_num= list(range(n,(m+1)*n, n)) return list(multiples_of_num)
MBPP_20
21
Write a function to find the first duplicate element in a given array of integers.
def find_first_duplicate(nums): num_set = set() no_duplicate = -1 for i in range(len(nums)): if nums[i] in num_set: return nums[i] else: num_set.add(nums[i]) return no_duplicate
MBPP_21
22
Write a python function to find the maximum sum of elements of list in a list of lists.
def maximum_Sum(list1): maxi = -100000 for x in list1: sum = 0 for y in x: sum+= y maxi = max(sum,maxi) return maxi
MBPP_22
23
Write a function to convert the given binary number to its decimal equivalent.
def binary_to_decimal(binary): binary1 = binary decimal, i, n = 0, 0, 0 while(binary != 0): dec = binary % 10 decimal = decimal + dec * pow(2, i) binary = binary//10 i += 1 return (decimal)
MBPP_23
24
Write a python function to find the product of non-repeated elements in a given array.
def find_Product(arr,n): arr.sort() prod = 1 for i in range(0,n,1): if (arr[i - 1] != arr[i]): prod = prod * arr[i] return prod;
MBPP_24
25
Write a function to check if the given tuple list has all k elements.
def check_k_elements(test_list, K): res = True for tup in test_list: for ele in tup: if ele != K: res = False return (res)
MBPP_25
26
Write a python function to remove all digits from a list of strings.
import re def remove(list): pattern = '[0-9]' list = [re.sub(pattern, '', i) for i in list] return list
MBPP_26
27
Write a python function to find binomial co-efficient.
def binomial_Coeff(n,k): if k > n : return 0 if k==0 or k ==n : return 1 return binomial_Coeff(n-1,k-1) + binomial_Coeff(n-1,k)
MBPP_27
28
Write a python function to find the element occurring odd number of times.
def get_Odd_Occurrence(arr,arr_size): for i in range(0,arr_size): count = 0 for j in range(0,arr_size): if arr[i] == arr[j]: count+=1 if (count % 2 != 0): return arr[i] return -1
MBPP_28
29
Write a python function to count all the substrings starting and ending with same characters.
def check_Equality(s): return (ord(s[0]) == ord(s[len(s) - 1])); def count_Substring_With_Equal_Ends(s): result = 0; n = len(s); for i in range(n): for j in range(1,n-i+1): if (check_Equality(s[i:i+j])): result+=1; return result;
MBPP_29
30
Write a function to find the top k integers that occur most frequently from given lists of sorted and distinct integers using heap queue algorithm.
def func(nums, k): import collections d = collections.defaultdict(int) for row in nums: for i in row: d[i] += 1 temp = [] import heapq for key, v in d.items(): if len(temp) < k: temp.append((v, key)) if len(temp) == k: heapq.heapify(temp) else: if v > temp[0][0]: heapq.heappop(temp) heapq.heappush(temp, (v, key)) result = [] while temp: v, key = heapq.heappop(temp) result.append(key) return result
MBPP_30
31
Write a python function to find the largest prime factor of a given number.
import math def max_Prime_Factors (n): maxPrime = -1 while n%2 == 0: maxPrime = 2 n >>= 1 for i in range(3,int(math.sqrt(n))+1,2): while n % i == 0: maxPrime = i n = n / i if n > 2: maxPrime = n return int(maxPrime)
MBPP_31
32
Write a python function to convert a decimal number to binary number.
def decimal_To_Binary(N): B_Number = 0 cnt = 0 while (N != 0): rem = N % 2 c = pow(10,cnt) B_Number += rem*c N //= 2 cnt += 1 return B_Number
MBPP_32
33
Write a python function to find the missing number in a sorted array.
def find_missing(ar,N): l = 0 r = N - 1 while (l <= r): mid = (l + r) / 2 mid= int (mid) if (ar[mid] != mid + 1 and ar[mid - 1] == mid): return (mid + 1) elif (ar[mid] != mid + 1): r = mid - 1 else: l = mid + 1 return (-1)
MBPP_33
34
Write a function to find the n-th rectangular number.
def find_rect_num(n): return n*(n + 1)
MBPP_34
35
Write a python function to find the nth digit in the proper fraction of two given numbers.
def find_Nth_Digit(p,q,N) : while (N > 0) : N -= 1; p *= 10; res = p // q; p %= q; return res;
MBPP_35
36
Write a function to sort a given mixed list of integers and strings.
def sort_mixed_list(mixed_list): int_part = sorted([i for i in mixed_list if type(i) is int]) str_part = sorted([i for i in mixed_list if type(i) is str]) return int_part + str_part
MBPP_36
37
Write a function to find the division of first even and odd number of a given list.
def div_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_37
38
Write a function to check if the letters of a given string can be rearranged so that two characters that are adjacent to each other are different.
import heapq from collections import Counter def rearange_string(S): ctr = Counter(S) heap = [(-value, key) for key, value in ctr.items()] heapq.heapify(heap) if (-heap[0][0]) * 2 > len(S) + 1: return "" ans = [] while len(heap) >= 2: nct1, char1 = heapq.heappop(heap) nct2, char2 = heapq.heappop(heap) ans.extend([char1, char2]) if nct1 + 1: heapq.heappush(heap, (nct1 + 1, char1)) if nct2 + 1: heapq.heappush(heap, (nct2 + 1, char2)) return "".join(ans) + (heap[0][1] if heap else "")
MBPP_38
39
Write a function to find frequency of the elements in a given list of lists using collections module.
from collections import Counter from itertools import chain def freq_element(nums): result = Counter(chain.from_iterable(nums)) return result
MBPP_39
40
Write a function to filter even numbers using lambda function.
def filter_evennumbers(nums): even_nums = list(filter(lambda x: x%2 == 0, nums)) return even_nums
MBPP_40
41
Write a python function to find the sum of repeated elements in a given array.
def find_Sum(arr,n): return sum([x for x in arr if arr.count(x) > 1])
MBPP_41
42
Write a function to find sequences of lowercase letters joined with an underscore using regex.
import re def text_match(text): patterns = '^[a-z]+_[a-z]+$' if re.search(patterns, text): return ('Found a match!') else: return ('Not matched!')
MBPP_42
43
Write a function that matches a word at the beginning of a string.
import re def text_match_string(text): patterns = '^\w+' if re.search(patterns, text): return 'Found a match!' else: return 'Not matched!'
MBPP_43
44
Write a function to find the gcd of the given array elements.
def find_gcd(x, y): while(y): x, y = y, x % y return x def get_gcd(l): num1 = l[0] num2 = l[1] gcd = find_gcd(num1, num2) for i in range(2, len(l)): gcd = find_gcd(gcd, l[i]) return gcd
MBPP_44
45
Write a python function to determine whether all the numbers are different from each other are not.
def test_distinct(data): if len(data) == len(set(data)): return True else: return False;
MBPP_45
46
Write a python function to find the last digit when factorial of a divides factorial of b.
def compute_Last_Digit(A,B): variable = 1 if (A == B): return 1 elif ((B - A) >= 5): return 0 else: for i in range(A + 1,B + 1): variable = (variable * (i % 10)) % 10 return variable % 10
MBPP_46
47
Write a python function to set all odd bits of a given number.
def odd_bit_set_number(n): count = 0;res = 0;temp = n while temp > 0: if count % 2 == 0: res |= (1 << count) count += 1 temp >>= 1 return (n | res)
MBPP_47
48
Write a function to extract every first or specified element from a given two-dimensional list.
def specified_element(nums, N): result = [i[N] for i in nums] return result
MBPP_48
49
Write a function to find the list with minimum length using lambda function.
def min_length_list(input_list): min_length = min(len(x) for x in input_list ) min_list = min(input_list, key = lambda i: len(i)) return(min_length, min_list)
MBPP_49
50
Write a function to print check if the triangle is equilateral or not.
def check_equilateral(x,y,z): if x == y == z: return True else: return False
MBPP_50
51
Write a function to caluclate area of a parallelogram.
def parallelogram_area(b,h): area=b*h return area
MBPP_51
52
Write a python function to check whether the first and last characters of a given string are equal or not.
def check_Equality(str): if (str[0] == str[-1]): return ("Equal") else: return ("Not Equal")
MBPP_52
53
Write a function to sort the given array by using counting sort.
def counting_sort(my_list): max_value = 0 for i in range(len(my_list)): if my_list[i] > max_value: max_value = my_list[i] buckets = [0] * (max_value + 1) for i in my_list: buckets[i] += 1 i = 0 for j in range(max_value + 1): for a in range(buckets[j]): my_list[i] = j i += 1 return my_list
MBPP_53
54
Write a function to find t-nth term of geometric series.
import math def tn_gp(a,n,r): tn = a * (math.pow(r, n - 1)) return tn
MBPP_54
55
Write a python function to check if a given number is one less than twice its reverse.
def rev(num): rev_num = 0 while (num > 0): rev_num = (rev_num * 10 + num % 10) num = num // 10 return rev_num def check(n): return (2 * rev(n) == n + 1)
MBPP_55
56
Write a python function to find the largest number that can be formed with the given digits.
def find_Max_Num(arr,n) : arr.sort(reverse = True) num = arr[0] for i in range(1,n) : num = num * 10 + arr[i] return num
MBPP_56
57
Write a python function to check whether the given two integers have opposite sign or not.
def opposite_Signs(x,y): return ((x ^ y) < 0);
MBPP_57
58
Write a function to find the nth octagonal number.
def is_octagonal(n): return 3 * n * n - 2 * n
MBPP_58
59
Write a function to find the maximum length of the subsequence with difference between adjacent elements for the given array.
def max_len_sub( arr, n): mls=[] max = 0 for i in range(n): mls.append(1) for i in range(n): for j in range(i): if (abs(arr[i] - arr[j]) <= 1 and mls[i] < mls[j] + 1): mls[i] = mls[j] + 1 for i in range(n): if (max < mls[i]): max = mls[i] return max
MBPP_59
60
Write a python function to count number of substrings with the sum of digits equal to their length.
from collections import defaultdict def count_Substrings(s,n): count,sum = 0,0 mp = defaultdict(lambda : 0) mp[0] += 1 for i in range(n): sum += ord(s[i]) - ord('0') count += mp[sum - (i + 1)] mp[sum - (i + 1)] += 1 return count
MBPP_60
61
Write a python function to find smallest number in a list.
def smallest_num(xs): return min(xs)
MBPP_61
62
Write a function to find the maximum difference between available pairs in the given tuple list.
def max_difference(test_list): temp = [abs(b - a) for a, b in test_list] res = max(temp) return (res)
MBPP_62
63
Write a function to sort a list of tuples using lambda.
def subject_marks(subjectmarks): #subject_marks = [('English', 88), ('Science', 90), ('Maths', 97), ('Social sciences', 82)]) subjectmarks.sort(key = lambda x: x[1]) return subjectmarks
MBPP_63
64
Write a function of recursion list sum.
def recursive_list_sum(data_list): total = 0 for element in data_list: if type(element) == type([]): total = total + recursive_list_sum(element) else: total = total + element return total
MBPP_64
65
Write a python function to count positive numbers in a list.
def pos_count(list): pos_count= 0 for num in list: if num >= 0: pos_count += 1 return pos_count
MBPP_65
66
Write a function to find the number of ways to partition a set of bell numbers.
def bell_number(n): bell = [[0 for i in range(n+1)] for j in range(n+1)] bell[0][0] = 1 for i in range(1, n+1): bell[i][0] = bell[i-1][i-1] for j in range(1, i+1): bell[i][j] = bell[i-1][j-1] + bell[i][j-1] return bell[n][0]
MBPP_66
67
Write a python function to check whether the given array is monotonic or not.
def is_Monotonic(A): return (all(A[i] <= A[i + 1] for i in range(len(A) - 1)) or all(A[i] >= A[i + 1] for i in range(len(A) - 1)))
MBPP_67
68
Write a function to check whether a list contains the given sublist or not.
def is_sublist(l, s): sub_set = False if s == []: sub_set = True elif s == l: sub_set = True elif len(s) > len(l): sub_set = False else: for i in range(len(l)): if l[i] == s[0]: n = 1 while (n < len(s)) and (l[i+n] == s[n]): n += 1 if n == len(s): sub_set = True return sub_set
MBPP_68
69
Write a function to find whether all the given tuples have equal length or not.
def find_equal_tuple(Input, k): flag = 1 for tuple in Input: if len(tuple) != k: flag = 0 break return flag def get_equal(Input, k): if find_equal_tuple(Input, k) == 1: return ("All tuples have same length") else: return ("All tuples do not have same length")
MBPP_69
70
Write a function to sort a list of elements using comb sort.
def comb_sort(nums): shrink_fact = 1.3 gaps = len(nums) swapped = True i = 0 while gaps > 1 or swapped: gaps = int(float(gaps) / shrink_fact) swapped = False i = 0 while gaps + i < len(nums): if nums[i] > nums[i+gaps]: nums[i], nums[i+gaps] = nums[i+gaps], nums[i] swapped = True i += 1 return nums
MBPP_70
71
Write a python function to check whether the given number can be represented as difference of two squares or not.
def dif_Square(n): if (n % 4 != 2): return True return False
MBPP_71
72
Write a function to split the given string with multiple delimiters by using regex.
import re def multiple_split(text): return (re.split('; |, |\*|\n',text))
MBPP_72
73
Write a function to check whether it follows the sequence given in the patterns array.
def is_samepatterns(colors, patterns): if len(colors) != len(patterns): return False sdict = {} pset = set() sset = set() for i in range(len(patterns)): pset.add(patterns[i]) sset.add(colors[i]) if patterns[i] not in sdict.keys(): sdict[patterns[i]] = [] keys = sdict[patterns[i]] keys.append(colors[i]) sdict[patterns[i]] = keys if len(pset) != len(sset): return False for values in sdict.values(): for i in range(len(values) - 1): if values[i] != values[i+1]: return False return True
MBPP_73
74
Write a function to find tuples which have all elements divisible by k from the given list of tuples.
def find_tuples(test_list, K): res = [sub for sub in test_list if all(ele % K == 0 for ele in sub)] return (str(res))
MBPP_74
75
Write a python function to count the number of squares in a rectangle.
def count_Squares(m,n): if(n < m): temp = m m = n n = temp return ((m * (m + 1) * (2 * m + 1) / 6 + (n - m) * m * (m + 1) / 2))
MBPP_75
76
Write a python function to find the difference between sum of even and odd digits.
def is_Diff(n): return (n % 11 == 0)
MBPP_76
77
Write a python function to find number of integers with odd number of set bits.
def count_With_Odd_SetBits(n): if (n % 2 != 0): return (n + 1) / 2 count = bin(n).count('1') ans = n / 2 if (count % 2 != 0): ans += 1 return ans
MBPP_77
78
Write a python function to check whether the length of the word is odd or not.
def word_len(s): s = s.split(' ') for word in s: if len(word)%2!=0: return True else: return False
MBPP_78
79
Write a function to find the nth tetrahedral number.
def tetrahedral_number(n): return (n * (n + 1) * (n + 2)) / 6
MBPP_79
80
Write a function to zip the two given tuples.
def zip_tuples(test_tup1, test_tup2): res = [] for i, j in enumerate(test_tup1): res.append((j, test_tup2[i % len(test_tup2)])) return (res)
MBPP_80
81
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_81
82
Write a python function to find the character made by adding all the characters of the given string.
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_82
83
Write a function to find the n-th number in 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_83
84
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_84
85
Write a function to find nth centered hexagonal number.
def centered_hexagonal_number(n): return 3 * n * (n - 1) + 1
MBPP_85
86
Write a function to merge three dictionaries into a single expression.
import collections as ct def merge_dictionaries_three(dict1,dict2, dict3): merged_dict = dict(ct.ChainMap({},dict1,dict2,dict3)) return merged_dict
MBPP_86
87
Write a function to get the frequency of the elements in a list.
import collections def freq_count(list1): freq_count= collections.Counter(list1) return freq_count
MBPP_87
88
Write a function to find the closest smaller number than n.
def closest_num(N): return (N - 1)
MBPP_88
89
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_89
90
Write a function to check if a substring is present 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_90
91
Write a function to check whether the given number is undulating or not.
def is_undulating(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_91
92
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_92
93
Write a function to extract the index minimum value record from the given tuples.
from operator import itemgetter def index_minimum(test_list): res = min(test_list, key = itemgetter(1))[0] return (res)
MBPP_93
94
Write a python function to find the minimum length of sublist.
def Find_Min_Length(lst): minLength = min(len(x) for x in lst ) return minLength
MBPP_94
95
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_95
96
Write a function to find frequency count of list of lists.
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_96
97
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_97
98
Write a function to convert the given decimal number to its binary equivalent.
def decimal_to_binary(n): return bin(n).replace("0b","")
MBPP_98
99
Write a function to find the next smallest palindrome of a specified number.
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_99