task_id
int32
511
974
source
stringlengths
149
914
task_source
stringclasses
1 value
task_name
stringclasses
1 value
target
stringlengths
30
908
split
stringclasses
2 values
537
Instruct: Write a python function to find the first repeated word in a given string. >>> assert first_repeated_word("ab ca bc ab") == "ab" >>> assert first_repeated_word("ab ca bc") == 'None' >>> assert first_repeated_word("ab ca bc ca ab bc") == "ca" Answer:
mbpp
mbpp
def first_repeated_word(str1): temp = set() for word in str1.split(): if word in temp: return word; else: temp.add(word) return 'None'
validation
538
Instruct: Write a python function to convert a given string list to a tuple. >>> assert string_list_to_tuple(("python 3.0")) == ('p', 'y', 't', 'h', 'o', 'n', '3', '.', '0') >>> assert string_list_to_tuple(("bigdata")) == ('b', 'i', 'g', 'd', 'a', 't', 'a') >>> assert string_list_to_tuple(("language")) == ('l', 'a', 'n...
mbpp
mbpp
def string_list_to_tuple(str1): result = tuple(x for x in str1 if not x.isspace()) return result
validation
539
Instruct: Write a function to create a list containing the power of said number in bases raised to the corresponding number in the index using map function. >>> assert basesnum_coresspondingnum([10, 20, 30, 40, 50, 60, 70, 80, 90, 100],[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])==[10, 400, 27000, 2560000, 312500000, 46656000000, ...
mbpp
mbpp
def basesnum_coresspondingnum(bases_num,index): result = list(map(pow, bases_num, index)) return result
validation
540
Instruct: Write a python function to find the difference between highest and least frequencies in a given array. >>> assert find_Diff([1,1,2,2,7,8,4,5,1,4],10) == 2 >>> assert find_Diff([1,7,9,2,3,3,1,3,3],9) == 3 >>> assert find_Diff([1,2,1,2],4) == 0 Answer:
mbpp
mbpp
def find_Diff(arr,n): arr.sort() count = 0; max_count = 0; min_count = n for i in range(0,(n-1)): if arr[i] == arr[i + 1]: count += 1 continue else: max_count = max(max_count,count) min_count = min(min_count,count) ...
validation
541
Instruct: Write a function to find if the given number is abundant or not. >>> assert check_abundant(12) == True >>> assert check_abundant(15) == False >>> assert check_abundant(18) == True Answer:
mbpp
mbpp
import math def get_sum(n): sum = 0 i = 1 while i <= (math.sqrt(n)): if n%i == 0: if n/i == i : sum = sum + i else: sum = sum + i sum = sum + (n / i ) i = i + 1 sum = sum - n return sum def check_abundant(n): if (get_sum(n) > n): return True else: return F...
validation
542
Instruct: Write a function to replace all occurrences of spaces, commas, or dots with a colon in the given string by using regex. >>> assert fill_spaces('Boult Curve Wireless Neckband') == 'Boult:Curve:Wireless:Neckband' >>> assert fill_spaces('Stereo Sound Sweatproof') == 'Stereo:Sound:Sweatproof' >>> assert fill_spac...
mbpp
mbpp
import re def fill_spaces(text): return (re.sub("[ ,.]", ":", text))
validation
543
Instruct: Write a function to add two numbers and print number of digits of sum. >>> assert count_digits(9875,10)==(4) >>> assert count_digits(98759853034,100)==(11) >>> assert count_digits(1234567,500)==(7) Answer:
mbpp
mbpp
def count_digits(num1,num2): number=num1+num2 count = 0 while(number > 0): number = number // 10 count = count + 1 return count
validation
544
Instruct: Write a function to flatten the tuple list to a string. >>> assert flatten_tuple([('1', '4', '6'), ('5', '8'), ('2', '9'), ('1', '10')]) == '1 4 6 5 8 2 9 1 10' >>> assert flatten_tuple([('2', '3', '4'), ('6', '9'), ('3', '2'), ('2', '11')]) == '2 3 4 6 9 3 2 2 11' >>> assert flatten_tuple([('14', '21', '9'),...
mbpp
mbpp
def flatten_tuple(test_list): res = ' '.join([idx for tup in test_list for idx in tup]) return (res)
validation
545
Instruct: Write a python function to toggle only first and last bits of a given number. >>> assert toggle_F_and_L_bits(10) == 3 >>> assert toggle_F_and_L_bits(15) == 6 >>> assert toggle_F_and_L_bits(20) == 5 Answer:
mbpp
mbpp
def take_L_and_F_set_bits(n) : n = n | n >> 1 n = n | n >> 2 n = n | n >> 4 n = n | n >> 8 n = n | n >> 16 return ((n + 1) >> 1) + 1 def toggle_F_and_L_bits(n) : if (n == 1) : return 0 return n ^ take_L_and_F_set_bits(n)
validation
546
Instruct: Write a function to find the last occurrence of a character in a string. >>> assert last_occurence_char("hello world",'l')==10 >>> assert last_occurence_char("language",'g')==7 >>> assert last_occurence_char("little",'y')==None Answer:
mbpp
mbpp
def last_occurence_char(string,char): flag = -1 for i in range(len(string)): if(string[i] == char): flag = i if(flag == -1): return None else: return flag + 1
validation
547
Instruct: Write a python function to find the sum of hamming distances of all consecutive numbers from o to n. >>> assert Total_Hamming_Distance(4) == 7 >>> assert Total_Hamming_Distance(2) == 3 >>> assert Total_Hamming_Distance(5) == 8 Answer:
mbpp
mbpp
def Total_Hamming_Distance(n): i = 1 sum = 0 while (n // i > 0): sum = sum + n // i i = i * 2 return sum
validation
548
Instruct: Write a function to find the length of the longest increasing subsequence of the given sequence. >>> assert longest_increasing_subsequence([10, 22, 9, 33, 21, 50, 41, 60]) == 5 >>> assert longest_increasing_subsequence([3, 10, 2, 1, 20]) == 3 >>> assert longest_increasing_subsequence([50, 3, 10, 7, 40, 80]) =...
mbpp
mbpp
def longest_increasing_subsequence(arr): n = len(arr) longest_increasing_subsequence = [1]*n for i in range (1 , n): for j in range(0 , i): if arr[i] > arr[j] and longest_increasing_subsequence[i]< longest_increasing_subsequence[j] + 1 : longest_increasing_subsequence[i] = longest_increasing_sub...
validation
549
Instruct: Write a python function to find the sum of fifth power of first n odd natural numbers. >>> assert odd_Num_Sum(1) == 1 >>> assert odd_Num_Sum(2) == 244 >>> assert odd_Num_Sum(3) == 3369 Answer:
mbpp
mbpp
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*j) return sm
validation
550
Instruct: Write a python function to find the maximum element in a sorted and rotated array. >>> assert find_Max([2,3,5,6,9],0,4) == 9 >>> assert find_Max([3,4,5,2,1],0,4) == 5 >>> assert find_Max([1,2,3],0,2) == 3 Answer:
mbpp
mbpp
def find_Max(arr,low,high): if (high < low): return arr[0] if (high == low): return arr[low] mid = low + (high - low) // 2 if (mid < high and arr[mid + 1] < arr[mid]): return arr[mid] if (mid > low and arr[mid] < arr[mid - 1]): return arr[mid - 1] ...
validation
551
Instruct: Write a function to extract a specified column from a given nested list. >>> assert extract_column([[1, 2, 3], [2, 4, 5], [1, 1, 1]],0)==[1, 2, 1] >>> assert extract_column([[1, 2, 3], [-2, 4, -5], [1, -1, 1]],2)==[3, -5, 1] >>> assert extract_column([[1, 3], [5, 7], [1, 3], [13, 15, 17], [5, 7], [9, 11]],0)=...
mbpp
mbpp
def extract_column(list1, n): result = [i.pop(n) for i in list1] return result
validation
552
Instruct: Write a python function to check whether a given sequence is linear or not. >>> assert Seq_Linear([0,2,4,6,8,10]) == "Linear Sequence" >>> assert Seq_Linear([1,2,3]) == "Linear Sequence" >>> assert Seq_Linear([1,5,2]) == "Non Linear Sequence" Answer:
mbpp
mbpp
def Seq_Linear(seq_nums): seq_nums = [seq_nums[x] - seq_nums[x-1] for x in range(1, len(seq_nums))] if len(set(seq_nums)) == 1: return "Linear Sequence" else: return "Non Linear Sequence"
validation
553
Instruct: Write a function to convert the given tuple to a floating-point number. >>> assert tuple_to_float((4, 56)) == 4.56 >>> assert tuple_to_float((7, 256)) == 7.256 >>> assert tuple_to_float((8, 123)) == 8.123 Answer:
mbpp
mbpp
def tuple_to_float(test_tup): res = float('.'.join(str(ele) for ele in test_tup)) return (res)
validation
554
Instruct: Write a python function to find odd numbers from a mixed list. >>> assert Split([1,2,3,4,5,6]) == [1,3,5] >>> assert Split([10,11,12,13]) == [11,13] >>> assert Split([7,8,9,1]) == [7,9,1] Answer:
mbpp
mbpp
def Split(list): od_li = [] for i in list: if (i % 2 != 0): od_li.append(i) return od_li
validation
555
Instruct: Write a python function to find the difference between sum of cubes of first n natural numbers and the sum of first n natural numbers. >>> assert difference(3) == 30 >>> assert difference(5) == 210 >>> assert difference(2) == 6 Answer:
mbpp
mbpp
def difference(n) : S = (n*(n + 1))//2; res = S*(S-1); return res;
validation
556
Instruct: Write a python function to count the pairs with xor as an odd number. >>> assert find_Odd_Pair([5,4,7,2,1],5) == 6 >>> assert find_Odd_Pair([7,2,8,1,0,5,11],7) == 12 >>> assert find_Odd_Pair([1,2,3],3) == 2 Answer:
mbpp
mbpp
def find_Odd_Pair(A,N) : oddPair = 0 for i in range(0,N) : for j in range(i+1,N) : if ((A[i] ^ A[j]) % 2 != 0): oddPair+=1 return oddPair
validation
557
Instruct: Write a function to toggle characters case in a string. >>> assert toggle_string("Python")==("pYTHON") >>> assert toggle_string("Pangram")==("pANGRAM") >>> assert toggle_string("LIttLE")==("liTTle") Answer:
mbpp
mbpp
def toggle_string(string): string1 = string.swapcase() return string1
validation
558
Instruct: Write a python function to find the digit distance between two integers. >>> assert digit_distance_nums(1,2) == 1 >>> assert digit_distance_nums(23,56) == 6 >>> assert digit_distance_nums(123,256) == 7 Answer:
mbpp
mbpp
def digit_distance_nums(n1, n2): return sum(map(int,str(abs(n1-n2))))
validation
559
Instruct: Write a function to find the largest sum of contiguous subarray in the given array. >>> assert max_sub_array_sum([-2, -3, 4, -1, -2, 1, 5, -3], 8) == 7 >>> assert max_sub_array_sum([-3, -4, 5, -2, -3, 2, 6, -4], 8) == 8 >>> assert max_sub_array_sum([-4, -5, 6, -3, -4, 3, 7, -5], 8) == 10 Answer:
mbpp
mbpp
def max_sub_array_sum(a, size): max_so_far = 0 max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if max_ending_here < 0: max_ending_here = 0 elif (max_so_far < max_ending_here): max_so_far = max_ending_here return max_so_far
validation
560
Instruct: Write a function to find the union of elements of the given tuples. >>> assert union_elements((3, 4, 5, 6),(5, 7, 4, 10) ) == (3, 4, 5, 6, 7, 10) >>> assert union_elements((1, 2, 3, 4),(3, 4, 5, 6) ) == (1, 2, 3, 4, 5, 6) >>> assert union_elements((11, 12, 13, 14),(13, 15, 16, 17) ) == (11, 12, 13, 14, 15, 16...
mbpp
mbpp
def union_elements(test_tup1, test_tup2): res = tuple(set(test_tup1 + test_tup2)) return (res)
validation
561
Instruct: Write a function to assign with each element, its pair elements from other similar pairs in the given tuple. >>> assert assign_elements([(5, 3), (7, 5), (2, 7), (3, 8), (8, 4)] ) == {3: [8], 5: [3], 7: [5], 2: [7], 8: [4], 4: []} >>> assert assign_elements([(6, 4), (9, 4), (3, 8), (4, 9), (9, 5)] ) == {4: [9]...
mbpp
mbpp
def assign_elements(test_list): res = dict() for key, val in test_list: res.setdefault(val, []) res.setdefault(key, []).append(val) return (res)
validation
562
Instruct: Write a python function to find the maximum length of sublist. >>> assert Find_Max_Length([[1],[1,4],[5,6,7,8]]) == 4 >>> assert Find_Max_Length([[0,1],[2,2,],[3,2,1]]) == 3 >>> assert Find_Max_Length([[7],[22,23],[13,14,15],[10,20,30,40,50]]) == 5 Answer:
mbpp
mbpp
def Find_Max_Length(lst): maxLength = max(len(x) for x in lst ) return maxLength
validation
563
Instruct: Write a function to extract values between quotation marks of a string. >>> assert extract_values('"Python", "PHP", "Java"')==['Python', 'PHP', 'Java'] >>> assert extract_values('"python","program","language"')==['python','program','language'] >>> assert extract_values('"red","blue","green","yellow"')==['red'...
mbpp
mbpp
import re def extract_values(text): return (re.findall(r'"(.*?)"', text))
validation
564
Instruct: Write a python function to count unequal element pairs from the given array. >>> assert count_Pairs([1,2,1],3) == 2 >>> assert count_Pairs([1,1,1,1],4) == 0 >>> assert count_Pairs([1,2,3,4,5],5) == 10 Answer:
mbpp
mbpp
def count_Pairs(arr,n): cnt = 0; for i in range(n): for j in range(i + 1,n): if (arr[i] != arr[j]): cnt += 1; return cnt;
validation
565
Instruct: Write a python function to split a string into characters. >>> assert split('python') == ['p','y','t','h','o','n'] >>> assert split('Name') == ['N','a','m','e'] >>> assert split('program') == ['p','r','o','g','r','a','m'] Answer:
mbpp
mbpp
def split(word): return [char for char in word]
validation
566
Instruct: Write a function to get the sum of a non-negative integer. >>> assert sum_digits(345)==12 >>> assert sum_digits(12)==3 >>> assert sum_digits(97)==16 Answer:
mbpp
mbpp
def sum_digits(n): if n == 0: return 0 else: return n % 10 + sum_digits(int(n / 10))
validation
567
Instruct: Write a function to check whether a specified list is sorted or not. >>> assert issort_list([1,2,4,6,8,10,12,14,16,17])==True >>> assert issort_list([1, 2, 4, 6, 8, 10, 12, 14, 20, 17])==False >>> assert issort_list([1, 2, 4, 6, 8, 10,15,14,20])==False Answer:
mbpp
mbpp
def issort_list(list1): result = all(list1[i] <= list1[i+1] for i in range(len(list1)-1)) return result
validation
568
Instruct: Write a function to create a list of empty dictionaries. >>> assert empty_list(5)==[{},{},{},{},{}] >>> assert empty_list(6)==[{},{},{},{},{},{}] >>> assert empty_list(7)==[{},{},{},{},{},{},{}] Answer:
mbpp
mbpp
def empty_list(length): empty_list = [{} for _ in range(length)] return empty_list
validation
569
Instruct: Write a function to sort each sublist of strings in a given list of lists. >>> assert sort_sublists([['green', 'orange'], ['black', 'white'], ['white', 'black', 'orange']])==[['green', 'orange'], ['black', 'white'], ['black', 'orange', 'white']] >>> assert sort_sublists([['green', 'orange'], ['black'], ['gree...
mbpp
mbpp
def sort_sublists(list1): result = list(map(sorted,list1)) return result
validation
570
Instruct: Write a function to remove words from a given list of strings containing a character or string. >>> assert remove_words(['Red color', 'Orange#', 'Green', 'Orange @', "White"],['#', 'color', '@'])==['Red', '', 'Green', 'Orange', 'White'] >>> assert remove_words(['Red &', 'Orange+', 'Green', 'Orange @', 'White'...
mbpp
mbpp
def remove_words(list1, charlist): new_list = [] for line in list1: new_words = ' '.join([word for word in line.split() if not any([phrase in word for phrase in charlist])]) new_list.append(new_words) return new_list
validation
571
Instruct: Write a function to find maximum possible sum of disjoint pairs for the given array of integers and a number k. >>> assert max_sum_pair_diff_lessthan_K([3, 5, 10, 15, 17, 12, 9], 7, 4) == 62 >>> assert max_sum_pair_diff_lessthan_K([5, 15, 10, 300], 4, 12) == 25 >>> assert max_sum_pair_diff_lessthan_K([1, 2, 3...
mbpp
mbpp
def max_sum_pair_diff_lessthan_K(arr, N, K): arr.sort() dp = [0] * N dp[0] = 0 for i in range(1, N): dp[i] = dp[i-1] if (arr[i] - arr[i-1] < K): if (i >= 2): dp[i] = max(dp[i], dp[i-2] + arr[i] + arr[i-1]); else: dp[i] = max(dp[i], arr[i] + arr[i-1]); return dp[N - 1]
validation
572
Instruct: Write a python function to remove two duplicate numbers from a given number of lists. >>> assert two_unique_nums([1,2,3,2,3,4,5]) == [1, 4, 5] >>> assert two_unique_nums([1,2,3,2,4,5]) == [1, 3, 4, 5] >>> assert two_unique_nums([1,2,3,4,5]) == [1, 2, 3, 4, 5] Answer:
mbpp
mbpp
def two_unique_nums(nums): return [i for i in nums if nums.count(i)==1]
validation
573
Instruct: Write a python function to calculate the product of the unique numbers of a given list. >>> assert unique_product([10, 20, 30, 40, 20, 50, 60, 40]) == 720000000 >>> assert unique_product([1, 2, 3, 1,]) == 6 >>> assert unique_product([7, 8, 9, 0, 1, 1]) == 0 Answer:
mbpp
mbpp
def unique_product(list_data): temp = list(set(list_data)) p = 1 for i in temp: p *= i return p
validation
574
Instruct: Write a function to find the surface area of a cylinder. >>> assert surfacearea_cylinder(10,5)==942.45 >>> assert surfacearea_cylinder(4,5)==226.18800000000002 >>> assert surfacearea_cylinder(4,10)==351.848 Answer:
mbpp
mbpp
def surfacearea_cylinder(r,h): surfacearea=((2*3.1415*r*r) +(2*3.1415*r*h)) return surfacearea
validation
575
Instruct: Write a python function to find nth number in a sequence which is not a multiple of a given number. >>> assert count_no(2,3,1,10) == 5 >>> assert count_no(3,6,4,20) == 11 >>> assert count_no(5,10,4,20) == 16 Answer:
mbpp
mbpp
def count_no (A,N,L,R): count = 0 for i in range (L,R + 1): if (i % A != 0): count += 1 if (count == N): break return (i)
validation
576
Instruct: Write a python function to check whether an array is subarray of another or not. >>> assert is_Sub_Array([1,4,3,5],[1,2],4,2) == False >>> assert is_Sub_Array([1,2,1],[1,2,1],3,3) == True >>> assert is_Sub_Array([1,0,2,2],[2,2,0],4,3) ==False Answer:
mbpp
mbpp
def is_Sub_Array(A,B,n,m): i = 0; j = 0; while (i < n and j < m): if (A[i] == B[j]): i += 1; j += 1; if (j == m): return True; else: i = i - j + 1; j = 0; return False;
validation
577
Instruct: Write a python function to find the last digit in factorial of a given number. >>> assert last_Digit_Factorial(4) == 4 >>> assert last_Digit_Factorial(21) == 0 >>> assert last_Digit_Factorial(30) == 0 Answer:
mbpp
mbpp
def last_Digit_Factorial(n): if (n == 0): return 1 elif (n <= 2): return n elif (n == 3): return 6 elif (n == 4): return 4 else: return 0
validation
578
Instruct: Write a function to interleave lists of the same length. >>> assert interleave_lists([1,2,3,4,5,6,7],[10,20,30,40,50,60,70],[100,200,300,400,500,600,700])==[1, 10, 100, 2, 20, 200, 3, 30, 300, 4, 40, 400, 5, 50, 500, 6, 60, 600, 7, 70, 700] >>> assert interleave_lists([10,20],[15,2],[5,10])==[10,15,5,20,2,10]...
mbpp
mbpp
def interleave_lists(list1,list2,list3): result = [el for pair in zip(list1, list2, list3) for el in pair] return result
validation
579
Instruct: Write a function to find the dissimilar elements in the given two tuples. >>> assert find_dissimilar((3, 4, 5, 6), (5, 7, 4, 10)) == (3, 6, 7, 10) >>> assert find_dissimilar((1, 2, 3, 4), (7, 2, 3, 9)) == (1, 4, 7, 9) >>> assert find_dissimilar((21, 11, 25, 26), (26, 34, 21, 36)) == (34, 36, 11, 25) Answer:
mbpp
mbpp
def find_dissimilar(test_tup1, test_tup2): res = tuple(set(test_tup1) ^ set(test_tup2)) return (res)
validation
580
Instruct: Write a function to extract the even elements in the nested mixed tuple. >>> assert extract_even((4, 5, (7, 6, (2, 4)), 6, 8)) == (4, (6, (2, 4)), 6, 8) >>> assert extract_even((5, 6, (8, 7, (4, 8)), 7, 9)) == (6, (8, (4, 8))) >>> assert extract_even((5, 6, (9, 8, (4, 6)), 8, 10)) == (6, (8, (4, 6)), 8, 10) A...
mbpp
mbpp
def even_ele(test_tuple, even_fnc): res = tuple() for ele in test_tuple: if isinstance(ele, tuple): res += (even_ele(ele, even_fnc), ) elif even_fnc(ele): res += (ele, ) return res def extract_even(test_tuple): res = even_ele(test_tuple, lambda x: x % 2 == 0) return (res)
validation
581
Instruct: Write a python function to find the surface area of the square pyramid. >>> assert surface_Area(3,4) == 33 >>> assert surface_Area(4,5) == 56 >>> assert surface_Area(1,2) == 5 Answer:
mbpp
mbpp
def surface_Area(b,s): return 2 * b * s + pow(b,2)
validation
582
Instruct: Write a function to check if a dictionary is empty or not. >>> assert my_dict({10})==False >>> assert my_dict({11})==False >>> assert my_dict({})==True Answer:
mbpp
mbpp
def my_dict(dict1): if bool(dict1): return False else: return True
validation
583
Instruct: Write a function for nth catalan number. >>> assert catalan_number(10)==16796 >>> assert catalan_number(9)==4862 >>> assert catalan_number(7)==429 Answer:
mbpp
mbpp
def catalan_number(num): if num <=1: return 1 res_num = 0 for i in range(num): res_num += catalan_number(i) * catalan_number(num-i-1) return res_num
validation
584
Instruct: Write a function to find all adverbs and their positions in a given sentence by using regex. >>> assert find_adverbs("Clearly, he has no excuse for such behavior.") == '0-7: Clearly' >>> assert find_adverbs("Please handle the situation carefuly") == '28-36: carefuly' >>> assert find_adverbs("Complete the task...
mbpp
mbpp
import re def find_adverbs(text): for m in re.finditer(r"\w+ly", text): return ('%d-%d: %s' % (m.start(), m.end(), m.group(0)))
validation
585
Instruct: Write a function to find the n - expensive price items from a given dataset using heap queue algorithm. >>> assert expensive_items([{'name': 'Item-1', 'price': 101.1},{'name': 'Item-2', 'price': 555.22}],1)==[{'name': 'Item-2', 'price': 555.22}] >>> assert expensive_items([{'name': 'Item-1', 'price': 101.1},{...
mbpp
mbpp
import heapq def expensive_items(items,n): expensive_items = heapq.nlargest(n, items, key=lambda s: s['price']) return expensive_items
validation
586
Instruct: Write a python function to split the array and add the first part to the end. >>> assert split_Arr([12,10,5,6,52,36],6,2) == [5,6,52,36,12,10] >>> assert split_Arr([1,2,3,4],4,1) == [2,3,4,1] >>> assert split_Arr([0,1,2,3,4,5,6,7],8,3) == [3,4,5,6,7,0,1,2] Answer:
mbpp
mbpp
def split_Arr(a,n,k): b = a[:k] return (a[k::]+b[::])
validation
587
Instruct: Write a function to convert a list to a tuple. >>> assert list_tuple([5, 10, 7, 4, 15, 3])==(5, 10, 7, 4, 15, 3) >>> assert list_tuple([2, 4, 5, 6, 2, 3, 4, 4, 7])==(2, 4, 5, 6, 2, 3, 4, 4, 7) >>> assert list_tuple([58,44,56])==(58,44,56) Answer:
mbpp
mbpp
def list_tuple(listx): tuplex = tuple(listx) return tuplex
validation
588
Instruct: Write a python function to find the difference between largest and smallest value in a given array. >>> assert big_diff([1,2,3,4]) == 3 >>> assert big_diff([4,5,12]) == 8 >>> assert big_diff([9,2,3]) == 7 Answer:
mbpp
mbpp
def big_diff(nums): diff= max(nums)-min(nums) return diff
validation
589
Instruct: Write a function to find perfect squares between two given numbers. >>> assert perfect_squares(1,30)==[1, 4, 9, 16, 25] >>> assert perfect_squares(50,100)==[64, 81, 100] >>> assert perfect_squares(100,200)==[100, 121, 144, 169, 196] Answer:
mbpp
mbpp
def perfect_squares(a, b): lists=[] for i in range (a,b+1): j = 1; while j*j <= i: if j*j == i: lists.append(i) j = j+1 i = i+1 return lists
validation
590
Instruct: Write a function to convert polar coordinates to rectangular coordinates. >>> assert polar_rect(3,4)==((5.0, 0.9272952180016122), (-2+2.4492935982947064e-16j)) >>> assert polar_rect(4,7)==((8.06225774829855, 1.0516502125483738), (-2+2.4492935982947064e-16j)) >>> assert polar_rect(15,17)==((22.67156809750927, ...
mbpp
mbpp
import cmath def polar_rect(x,y): cn = complex(x,y) cn=cmath.polar(cn) cn1 = cmath.rect(2, cmath.pi) return (cn,cn1)
validation
591
Instruct: Write a python function to interchange the first and last elements in a list. >>> assert swap_List([12, 35, 9, 56, 24]) == [24, 35, 9, 56, 12] >>> assert swap_List([1, 2, 3]) == [3, 2, 1] >>> assert swap_List([4, 5, 6]) == [6, 5, 4] Answer:
mbpp
mbpp
def swap_List(newList): size = len(newList) temp = newList[0] newList[0] = newList[size - 1] newList[size - 1] = temp return newList
validation
592
Instruct: Write a python function to find sum of product of binomial co-efficients. >>> assert sum_Of_product(3) == 15 >>> assert sum_Of_product(4) == 56 >>> assert sum_Of_product(1) == 1 Answer:
mbpp
mbpp
def binomial_Coeff(n,k): C = [0] * (k + 1); C[0] = 1; # nC0 is 1 for i in range(1,n + 1): for j in range(min(i, k),0,-1): C[j] = C[j] + C[j - 1]; return C[k]; def sum_Of_product(n): return binomial_Coeff(2 * n,n - 1);
validation
593
Instruct: Write a function to remove leading zeroes from an ip address. >>> assert removezero_ip("216.08.094.196")==('216.8.94.196') >>> assert removezero_ip("12.01.024")==('12.1.24') >>> assert removezero_ip("216.08.094.0196")==('216.8.94.196') Answer:
mbpp
mbpp
import re def removezero_ip(ip): string = re.sub('\.[0]*', '.', ip) return string
validation
594
Instruct: Write a function to find the difference of first even and odd number of a given list. >>> assert diff_even_odd([1,3,5,7,4,1,6,8])==3 >>> assert diff_even_odd([1,2,3,4,5,6,7,8,9,10])==1 >>> assert diff_even_odd([1,5,7,9,10])==9 Answer:
mbpp
mbpp
def diff_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)
validation
595
Instruct: Write a python function to count minimum number of swaps required to convert one binary string to another. >>> assert min_Swaps("1101","1110") == 1 >>> assert min_Swaps("111","000") == "Not Possible" >>> assert min_Swaps("111","110") == "Not Possible" Answer:
mbpp
mbpp
def min_Swaps(str1,str2) : count = 0 for i in range(len(str1)) : if str1[i] != str2[i] : count += 1 if count % 2 == 0 : return (count // 2) else : return ("Not Possible")
validation
596
Instruct: Write a function to find the size of the given tuple. >>> assert tuple_size(("A", 1, "B", 2, "C", 3) ) == sys.getsizeof(("A", 1, "B", 2, "C", 3)) >>> assert tuple_size((1, "Raju", 2, "Nikhil", 3, "Deepanshu") ) == sys.getsizeof((1, "Raju", 2, "Nikhil", 3, "Deepanshu")) >>> assert tuple_size(((1, "Lion"), ( 2,...
mbpp
mbpp
import sys def tuple_size(tuple_list): return (sys.getsizeof(tuple_list))
validation
597
Instruct: Write a function to find kth element from the given two sorted arrays. >>> assert find_kth([2, 3, 6, 7, 9], [1, 4, 8, 10], 5, 4, 5) == 6 >>> assert find_kth([100, 112, 256, 349, 770], [72, 86, 113, 119, 265, 445, 892], 5, 7, 7) == 256 >>> assert find_kth([3, 4, 7, 8, 10], [2, 5, 9, 11], 5, 4, 6) == 8 Answer:
mbpp
mbpp
def find_kth(arr1, arr2, m, n, k): sorted1 = [0] * (m + n) i = 0 j = 0 d = 0 while (i < m and j < n): if (arr1[i] < arr2[j]): sorted1[d] = arr1[i] i += 1 else: sorted1[d] = arr2[j] j += 1 d += 1 while (i < m): sorted1[d] = arr1[i] d += 1 i += 1 while (j < n): sorted1[d...
validation
598
Instruct: Write a function to check whether the given number is armstrong or not. >>> assert armstrong_number(153)==True >>> assert armstrong_number(259)==False >>> assert armstrong_number(4458)==False Answer:
mbpp
mbpp
def armstrong_number(number): sum = 0 times = 0 temp = number while temp > 0: times = times + 1 temp = temp // 10 temp = number while temp > 0: reminder = temp % 10 sum = sum + (reminder ** times) temp //= 10 if number == sum: return ...
validation
599
Instruct: Write a function to find sum and average of first n natural numbers. >>> assert sum_average(10)==(55, 5.5) >>> assert sum_average(15)==(120, 8.0) >>> assert sum_average(20)==(210, 10.5) Answer:
mbpp
mbpp
def sum_average(number): total = 0 for value in range(1, number + 1): total = total + value average = total / number return (total,average)
validation
600
Instruct: Write a python function to check whether the given number is even or not using bitwise operator. >>> assert is_Even(1) == False >>> assert is_Even(2) == True >>> assert is_Even(3) == False Answer:
mbpp
mbpp
def is_Even(n) : if (n^1 == n+1) : return True; else : return False;
validation