task_id
int32
3
974
text
stringlengths
40
171
code
stringlengths
30
1.33k
test_list
sequencelengths
3
3
test_setup_code
stringclasses
2 values
challenge_test_list
sequencelengths
0
3
265
Write a function to split a list for every nth element.
def list_split(S, step): return [S[i::step] for i in range(step)]
[ "assert list_split(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'],3)==[['a', 'd', 'g', 'j', 'm'], ['b', 'e', 'h', 'k', 'n'], ['c', 'f', 'i', 'l']] ", "assert list_split([1,2,3,4,5,6,7,8,9,10,11,12,13,14],3)==[[1,4,7,10,13], [2,5,8,11,14], [3,6,9,12]] ", "assert list_split(['python','java','C','C++','DBMS','SQL'],2)==[['python', 'C', 'DBMS'], ['java', 'C++', 'SQL']] " ]
[]
352
Write a python function to check whether all the characters in a given string are unique.
def unique_Characters(str): for i in range(len(str)): for j in range(i + 1,len(str)): if (str[i] == str[j]): return False; return True;
[ "assert unique_Characters('aba') == False", "assert unique_Characters('abc') == True", "assert unique_Characters('abab') == False" ]
[]
361
Write a function to remove empty lists from a given list of lists.
def remove_empty(list1): remove_empty = [x for x in list1 if x] return remove_empty
[ "assert remove_empty([[], [], [], 'Red', 'Green', [1,2], 'Blue', [], []])==['Red', 'Green', [1, 2], 'Blue']", "assert remove_empty([[], [], [],[],[], 'Green', [1,2], 'Blue', [], []])==[ 'Green', [1, 2], 'Blue']", "assert remove_empty([[], [], [], 'Python',[],[], 'programming', 'language',[],[],[], [], []])==['Python', 'programming', 'language']" ]
[]
554
Write a python function to find odd numbers from a mixed list.
def Split(list): od_li = [] for i in list: if (i % 2 != 0): od_li.append(i) return od_li
[ "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]" ]
[]
638
Write a function to calculate wind chill index.
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))
[ "assert wind_chill(120,35)==40", "assert wind_chill(40,70)==86", "assert wind_chill(10,100)==116" ]
[]
934
Write a function to find the nth delannoy number.
def dealnnoy_num(n, m): if (m == 0 or n == 0) : return 1 return dealnnoy_num(m - 1, n) + dealnnoy_num(m - 1, n - 1) + dealnnoy_num(m, n - 1)
[ "assert dealnnoy_num(3, 4) == 129", "assert dealnnoy_num(3, 3) == 63", "assert dealnnoy_num(4, 5) == 681" ]
[]
595
Write a python function to count minimum number of swaps required to convert one binary string to another.
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")
[ "assert min_Swaps(\"1101\",\"1110\") == 1", "assert min_Swaps(\"111\",\"000\") == \"Not Possible\"", "assert min_Swaps(\"111\",\"110\") == \"Not Possible\"" ]
[]
418
Write a python function to find the sublist having maximum length.
def Find_Max(lst): maxList = max((x) for x in lst) return maxList
[ "assert Find_Max([['A'],['A','B'],['A','B','C']]) == ['A','B','C']", "assert Find_Max([[1],[1,2],[1,2,3]]) == [1,2,3]", "assert Find_Max([[1,1],[1,2,3],[1,5,6,1]]) == [1,5,6,1]" ]
[]
414
Write a python function to check whether the value exists in a sequence or not.
def overlapping(list1,list2): c=0 d=0 for i in list1: c+=1 for i in list2: d+=1 for i in range(0,c): for j in range(0,d): if(list1[i]==list2[j]): return 1 return 0
[ "assert overlapping([1,2,3,4,5],[6,7,8,9]) == False", "assert overlapping([1,2,3],[4,5,6]) == False", "assert overlapping([1,4,5],[1,4,5]) == True" ]
[]
694
Write a function to extract unique values from the given dictionary values.
def extract_unique(test_dict): res = list(sorted({ele for val in test_dict.values() for ele in val})) return res
[ "assert extract_unique({'msm' : [5, 6, 7, 8],'is' : [10, 11, 7, 5],'best' : [6, 12, 10, 8],'for' : [1, 2, 5]} ) == [1, 2, 5, 6, 7, 8, 10, 11, 12]", "assert extract_unique({'Built' : [7, 1, 9, 4],'for' : [11, 21, 36, 14, 9],'ISP' : [4, 1, 21, 39, 47],'TV' : [1, 32, 38]} ) == [1, 4, 7, 9, 11, 14, 21, 32, 36, 38, 39, 47]", "assert extract_unique({'F' : [11, 13, 14, 17],'A' : [12, 11, 15, 18],'N' : [19, 21, 15, 36],'G' : [37, 36, 35]}) == [11, 12, 13, 14, 15, 17, 18, 19, 21, 35, 36, 37]" ]
[]
858
Write a function to count number of lists in a given list of lists and square the count.
def count_list(input_list): return (len(input_list))**2
[ "assert count_list([[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]])==25", "assert count_list([[1, 3], [5, 7], [9, 11], [13, 15, 17]] )==16", "assert count_list([[2, 4], [[6,8], [4,5,8]], [10, 12, 14]])==9" ]
[]
130
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
[ "assert max_occurrences([2,3,8,4,7,9,8,2,6,5,1,6,1,2,3,2,4,6,9,1,2])==(2, 5)", "assert max_occurrences([2,3,8,4,7,9,8,7,9,15,14,10,12,13,16,16,18])==(8, 2)", "assert max_occurrences([10,20,20,30,40,90,80,50,30,20,50,10])==(20, 3)" ]
[]
292
Write a python function to find quotient of two numbers.
def find(n,m): q = n//m return (q)
[ "assert find(10,3) == 3", "assert find(4,2) == 2", "assert find(20,5) == 4" ]
[]
83
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)
[ "assert get_Char(\"abc\") == \"f\"", "assert get_Char(\"gfg\") == \"t\"", "assert get_Char(\"ab\") == \"c\"" ]
[]
754
Write a function to find common index elements from three lists.
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
[ "assert extract_index_list([1, 1, 3, 4, 5, 6, 7],[0, 1, 2, 3, 4, 5, 7],[0, 1, 2, 3, 4, 5, 7])==[1, 7]", "assert extract_index_list([1, 1, 3, 4, 5, 6, 7],[0, 1, 2, 3, 4, 6, 5],[0, 1, 2, 3, 4, 6, 7])==[1, 6]", "assert extract_index_list([1, 1, 3, 4, 6, 5, 6],[0, 1, 2, 3, 4, 5, 7],[0, 1, 2, 3, 4, 5, 7])==[1, 5]" ]
[]
724
Write a function to 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))])
[ "assert power_base_sum(2,100)==115", "assert power_base_sum(8,10)==37", "assert power_base_sum(8,15)==62" ]
[]
633
Write a python function to find the sum of xor of all pairs of numbers in the given array.
def pair_OR_Sum(arr,n) : ans = 0 for i in range(0,n) : for j in range(i + 1,n) : ans = ans + (arr[i] ^ arr[j]) return ans
[ "assert pair_OR_Sum([5,9,7,6],4) == 47", "assert pair_OR_Sum([7,3,5],3) == 12", "assert pair_OR_Sum([7,3],2) == 4" ]
[]
692
Write a python function to find the last two digits in factorial of a given number.
def last_Two_Digits(N): if (N >= 10): return fac = 1 for i in range(1,N + 1): fac = (fac * i) % 100 return (fac)
[ "assert last_Two_Digits(7) == 40", "assert last_Two_Digits(5) == 20", "assert last_Two_Digits(2) == 2" ]
[]
712
Write a function to remove duplicates from a list of lists.
import itertools def remove_duplicate(list1): list.sort(list1) remove_duplicate = list(list1 for list1,_ in itertools.groupby(list1)) return remove_duplicate
[ "assert remove_duplicate([[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]])==[[10, 20], [30, 56, 25], [33], [40]] ", "assert remove_duplicate([\"a\", \"b\", \"a\", \"c\", \"c\"] )==[\"a\", \"b\", \"c\"]", "assert remove_duplicate([1, 3, 5, 6, 3, 5, 6, 1] )==[1, 3, 5, 6]" ]
[]
728
Write a function to sum elements in two lists.
def sum_list(lst1,lst2): res_list = [lst1[i] + lst2[i] for i in range(len(lst1))] return res_list
[ "assert sum_list([10,20,30],[15,25,35])==[25,45,65]", "assert sum_list([1,2,3],[5,6,7])==[6,8,10]", "assert sum_list([15,20,30],[15,45,75])==[30,65,105]" ]
[]
60
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
[ "assert max_len_sub([2, 5, 6, 3, 7, 6, 5, 8], 8) == 5", "assert max_len_sub([-2, -1, 5, -1, 4, 0, 3], 7) == 4", "assert max_len_sub([9, 11, 13, 15, 18], 5) == 1" ]
[]
306
Write a function to find the maximum sum of increasing subsequence from prefix till ith index and also including a given kth element which is after i, i.e., k > i .
def max_sum_increasing_subseq(a, n, index, k): dp = [[0 for i in range(n)] for i in range(n)] for i in range(n): if a[i] > a[0]: dp[0][i] = a[i] + a[0] else: dp[0][i] = a[i] for i in range(1, n): for j in range(n): if a[j] > a[i] and j > i: if dp[i - 1][i] + a[j] > dp[i - 1][j]: dp[i][j] = dp[i - 1][i] + a[j] else: dp[i][j] = dp[i - 1][j] else: dp[i][j] = dp[i - 1][j] return dp[index][k]
[ "assert max_sum_increasing_subseq([1, 101, 2, 3, 100, 4, 5 ], 7, 4, 6) == 11", "assert max_sum_increasing_subseq([1, 101, 2, 3, 100, 4, 5 ], 7, 2, 5) == 7", "assert max_sum_increasing_subseq([11, 15, 19, 21, 26, 28, 31], 7, 2, 4) == 71" ]
[]
72
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
[ "assert dif_Square(5) == True", "assert dif_Square(10) == False", "assert dif_Square(15) == True" ]
[]
852
Write a python function to remove negative numbers from a list.
def remove_negs(num_list): for item in num_list: if item < 0: num_list.remove(item) return num_list
[ "assert remove_negs([1,-2,3,-4]) == [1,3]", "assert remove_negs([1,2,3,-4]) == [1,2,3]", "assert remove_negs([4,5,-6,7,-8]) == [4,5,7]" ]
[]
158
Write a python function to find k number of operations required to make all elements equal.
def min_Ops(arr,n,k): max1 = max(arr) res = 0 for i in range(0,n): if ((max1 - arr[i]) % k != 0): return -1 else: res += (max1 - arr[i]) / k return int(res)
[ "assert min_Ops([2,2,2,2],4,3) == 0", "assert min_Ops([4,2,6,8],4,3) == -1", "assert min_Ops([21,33,9,45,63],5,6) == 24" ]
[]
856
Write a python function to find minimum adjacent swaps required to sort binary array.
def find_Min_Swaps(arr,n) : noOfZeroes = [0] * n count = 0 noOfZeroes[n - 1] = 1 - arr[n - 1] for i in range(n-2,-1,-1) : noOfZeroes[i] = noOfZeroes[i + 1] if (arr[i] == 0) : noOfZeroes[i] = noOfZeroes[i] + 1 for i in range(0,n) : if (arr[i] == 1) : count = count + noOfZeroes[i] return count
[ "assert find_Min_Swaps([1,0,1,0],4) == 3", "assert find_Min_Swaps([0,1,0],3) == 1", "assert find_Min_Swaps([0,0,1,1,0],5) == 2" ]
[]
375
Write a function to round the given number to the nearest multiple of a specific number.
def round_num(n,m): a = (n //m) * m b = a + m return (b if n - a > b - n else a)
[ "assert round_num(4722,10)==4720", "assert round_num(1111,5)==1110", "assert round_num(219,2)==218" ]
[]
437
Write a function to remove odd characters in a string.
def remove_odd(str1): str2 = '' for i in range(1, len(str1) + 1): if(i % 2 == 0): str2 = str2 + str1[i - 1] return str2
[ "assert remove_odd(\"python\")==(\"yhn\")", "assert remove_odd(\"program\")==(\"rga\")", "assert remove_odd(\"language\")==(\"agae\")" ]
[]
357
Write a function to find the maximum element of all the given tuple records.
def find_max(test_list): res = max(int(j) for i in test_list for j in i) return (res)
[ "assert find_max([(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]) == 10", "assert find_max([(3, 5), (7, 8), (6, 2), (7, 11), (9, 8)]) == 11", "assert find_max([(4, 6), (8, 9), (7, 3), (8, 12), (10, 9)]) == 12" ]
[]
539
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.
def basesnum_coresspondingnum(bases_num,index): result = list(map(pow, bases_num, index)) return result
[ "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, 8235430000000, 1677721600000000, 387420489000000000, 100000000000000000000]", "assert basesnum_coresspondingnum([1, 2, 3, 4, 5, 6, 7],[10, 20, 30, 40, 50, 60, 70])==[1, 1048576, 205891132094649, 1208925819614629174706176, 88817841970012523233890533447265625, 48873677980689257489322752273774603865660850176, 143503601609868434285603076356671071740077383739246066639249]", "assert basesnum_coresspondingnum([4, 8, 12, 16, 20, 24, 28],[3, 6, 9, 12, 15, 18, 21])==[64, 262144, 5159780352, 281474976710656, 32768000000000000000, 6979147079584381377970176, 2456510688823056210273111113728]" ]
[]
577
Write a python function to find the last digit in factorial of a given number.
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
[ "assert last_Digit_Factorial(4) == 4", "assert last_Digit_Factorial(21) == 0", "assert last_Digit_Factorial(30) == 0" ]
[]
583
Write a function for nth catalan number.
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
[ "assert catalan_number(10)==16796", "assert catalan_number(9)==4862", "assert catalan_number(7)==429" ]
[]
575
Write a python function to find nth number in a sequence which is not a multiple of a given number.
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)
[ "assert count_no(2,3,1,10) == 5", "assert count_no(3,6,4,20) == 11", "assert count_no(5,10,4,20) == 16" ]
[]
952
Write a function to compute the value of ncr mod p.
def nCr_mod_p(n, r, p): if (r > n- r): r = n - r C = [0 for i in range(r + 1)] C[0] = 1 for i in range(1, n + 1): for j in range(min(i, r), 0, -1): C[j] = (C[j] + C[j-1]) % p return C[r]
[ "assert nCr_mod_p(10, 2, 13) == 6", "assert nCr_mod_p(11, 3, 14) == 11", "assert nCr_mod_p(18, 14, 19) == 1" ]
[]
697
Write a function to find number of even elements in the given list using lambda function.
def count_even(array_nums): count_even = len(list(filter(lambda x: (x%2 == 0) , array_nums))) return count_even
[ "assert count_even([1, 2, 3, 5, 7, 8, 9, 10])==3", "assert count_even([10,15,14,13,-18,12,-20])==5", "assert count_even([1, 2, 4, 8, 9])==3" ]
[]
809
Write a function to check if each element of second tuple is smaller than its corresponding index in first tuple.
def check_smaller(test_tup1, test_tup2): res = all(x > y for x, y in zip(test_tup1, test_tup2)) return (res)
[ "assert check_smaller((1, 2, 3), (2, 3, 4)) == False", "assert check_smaller((4, 5, 6), (3, 4, 5)) == True", "assert check_smaller((11, 12, 13), (10, 11, 12)) == True" ]
[]
303
Write a python function to check whether the count of inversion of two types are same or not.
import sys def solve(a,n): mx = -sys.maxsize - 1 for j in range(1,n): if (mx > a[j]): return False mx = max(mx,a[j - 1]) return True
[ "assert solve([1,0,2],3) == True", "assert solve([1,2,0],3) == False", "assert solve([1,2,1],3) == True" ]
[]
206
Write a function to perform the adjacent element concatenation in the given tuples.
def concatenate_elements(test_tup): res = tuple(i + j for i, j in zip(test_tup, test_tup[1:])) return (res)
[ "assert concatenate_elements((\"DSP \", \"IS \", \"BEST \", \"FOR \", \"ALL \", \"UTS\")) == ('DSP IS ', 'IS BEST ', 'BEST FOR ', 'FOR ALL ', 'ALL UTS')", "assert concatenate_elements((\"RES \", \"IS \", \"BEST \", \"FOR \", \"ALL \", \"QESR\")) == ('RES IS ', 'IS BEST ', 'BEST FOR ', 'FOR ALL ', 'ALL QESR')", "assert concatenate_elements((\"MSAM\", \"IS \", \"BEST \", \"FOR \", \"ALL \", \"SKD\")) == ('MSAMIS ', 'IS BEST ', 'BEST FOR ', 'FOR ALL ', 'ALL SKD')" ]
[]
921
Write a function to perform chunking of tuples each of size n.
def chunk_tuples(test_tup, N): res = [test_tup[i : i + N] for i in range(0, len(test_tup), N)] return (res)
[ "assert chunk_tuples((10, 4, 5, 6, 7, 6, 8, 3, 4), 3) == [(10, 4, 5), (6, 7, 6), (8, 3, 4)]", "assert chunk_tuples((1, 2, 3, 4, 5, 6, 7, 8, 9), 2) == [(1, 2), (3, 4), (5, 6), (7, 8), (9,)]", "assert chunk_tuples((11, 14, 16, 17, 19, 21, 22, 25), 4) == [(11, 14, 16, 17), (19, 21, 22, 25)]" ]
[]
845
Write a python function to count the number of digits in factorial of a given number.
import math def find_Digits(n): if (n < 0): return 0; if (n <= 1): return 1; x = ((n * math.log10(n / math.e) + math.log10(2 * math.pi * n) /2.0)); return math.floor(x) + 1;
[ "assert find_Digits(7) == 4", "assert find_Digits(5) == 3", "assert find_Digits(4) == 2" ]
[]
353
Write a function to remove a specified column from a given nested list.
def remove_column(list1, n): for i in list1: del i[n] return list1
[ "assert remove_column([[1, 2, 3], [2, 4, 5], [1, 1, 1]],0)==[[2, 3], [4, 5], [1, 1]]", "assert remove_column([[1, 2, 3], [-2, 4, -5], [1, -1, 1]],2)==[[1, 2], [-2, 4], [1, -1]]", "assert remove_column([[1, 3], [5, 7], [1, 3], [13, 15, 17], [5, 7], [9, 11]],0)==[[3], [7], [3], [15, 17], [7], [11]]" ]
[]
459
Write a function to remove uppercase substrings from a given string by using regex.
import re def remove_uppercase(str1): remove_upper = lambda text: re.sub('[A-Z]', '', text) result = remove_upper(str1) return (result)
[ "assert remove_uppercase('cAstyoUrFavoRitETVshoWs') == 'cstyoravoitshos'", "assert remove_uppercase('wAtchTheinTernEtrAdIo') == 'wtchheinerntrdo'", "assert remove_uppercase('VoicESeaRchAndreComMendaTionS') == 'oiceachndreomendaion'" ]
[]
128
Write a function to shortlist words that are longer than n 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
[ "assert long_words(3,\"python is a programming language\")==['python','programming','language']", "assert long_words(2,\"writing a program\")==['writing','program']", "assert long_words(5,\"sorting list\")==['sorting']" ]
[]
263
Write a function to merge two dictionaries.
def merge_dict(d1,d2): d = d1.copy() d.update(d2) return d
[ "assert merge_dict({'a': 100, 'b': 200},{'x': 300, 'y': 200})=={'x': 300, 'y': 200, 'a': 100, 'b': 200}", "assert merge_dict({'a':900,'b':900,'d':900},{'a':900,'b':900,'d':900})=={'a':900,'b':900,'d':900,'a':900,'b':900,'d':900}", "assert merge_dict({'a':10,'b':20},{'x':30,'y':40})=={'x':30,'y':40,'a':10,'b':20}" ]
[]
174
Write a function to group a sequence of key-value pairs into a dictionary of lists.
def group_keyvalue(l): result = {} for k, v in l: result.setdefault(k, []).append(v) return result
[ "assert group_keyvalue([('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)])=={'yellow': [1, 3], 'blue': [2, 4], 'red': [1]}", "assert group_keyvalue([('python', 1), ('python', 2), ('python', 3), ('python', 4), ('python', 5)])=={'python': [1,2,3,4,5]}", "assert group_keyvalue([('yellow',100), ('blue', 200), ('yellow', 300), ('blue', 400), ('red', 100)])=={'yellow': [100, 300], 'blue': [200, 400], 'red': [100]}" ]
[]
766
Write a function to iterate over 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
[ "assert pair_wise([1,1,2,3,3,4,4,5])==[(1, 1), (1, 2), (2, 3), (3, 3), (3, 4), (4, 4), (4, 5)]", "assert pair_wise([1,5,7,9,10])==[(1, 5), (5, 7), (7, 9), (9, 10)]", "assert pair_wise([1,2,3,4,5,6,7,8,9,10])==[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)]" ]
[]
971
Write a function to find the maximum number of segments of lengths a, b and c that can be formed from n.
def maximum_segments(n, a, b, c) : dp = [-1] * (n + 10) dp[0] = 0 for i in range(0, n) : if (dp[i] != -1) : if(i + a <= n ): dp[i + a] = max(dp[i] + 1, dp[i + a]) if(i + b <= n ): dp[i + b] = max(dp[i] + 1, dp[i + b]) if(i + c <= n ): dp[i + c] = max(dp[i] + 1, dp[i + c]) return dp[n]
[ "assert maximum_segments(7, 5, 2, 5) == 2", "assert maximum_segments(17, 2, 1, 3) == 17", "assert maximum_segments(18, 16, 3, 6) == 6" ]
[]
73
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))
[ "assert multiple_split('Forces of the \\ndarkness*are coming into the play.') == ['Forces of the ', 'darkness', 'are coming into the play.']", "assert multiple_split('Mi Box runs on the \\n Latest android*which has google assistance and chromecast.') == ['Mi Box runs on the ', ' Latest android', 'which has google assistance and chromecast.']", "assert multiple_split('Certain services\\nare subjected to change*over the seperate subscriptions.') == ['Certain services', 'are subjected to change', 'over the seperate subscriptions.']" ]
[]
432
Write a function to find the median of a trapezium.
def median_trapezium(base1,base2,height): median = 0.5 * (base1+ base2) return median
[ "assert median_trapezium(15,25,35)==20", "assert median_trapezium(10,20,30)==15", "assert median_trapezium(6,9,4)==7.5" ]
[]
428
Write a function to sort the given array by using shell sort.
def shell_sort(my_list): gap = len(my_list) // 2 while gap > 0: for i in range(gap, len(my_list)): current_item = my_list[i] j = i while j >= gap and my_list[j - gap] > current_item: my_list[j] = my_list[j - gap] j -= gap my_list[j] = current_item gap //= 2 return my_list
[ "assert shell_sort([12, 23, 4, 5, 3, 2, 12, 81, 56, 95]) == [2, 3, 4, 5, 12, 12, 23, 56, 81, 95]", "assert shell_sort([24, 22, 39, 34, 87, 73, 68]) == [22, 24, 34, 39, 68, 73, 87]", "assert shell_sort([32, 30, 16, 96, 82, 83, 74]) == [16, 30, 32, 74, 82, 83, 96]" ]
[]
509
Write a python function to find the average of odd numbers till a given odd number.
def average_Odd(n) : if (n%2==0) : return ("Invalid Input") return -1 sm =0 count =0 while (n>=1) : count=count+1 sm = sm + n n = n-2 return sm//count
[ "assert average_Odd(9) == 5", "assert average_Odd(5) == 3", "assert average_Odd(11) == 6" ]
[]
837
Write a python function to find the cube sum of first n odd natural numbers.
def cube_Sum(n): sum = 0 for i in range(0,n) : sum += (2*i+1)*(2*i+1)*(2*i+1) return sum
[ "assert cube_Sum(2) == 28", "assert cube_Sum(3) == 153", "assert cube_Sum(4) == 496" ]
[]
504
Write a python function to find the cube sum of first n natural numbers.
def sum_Of_Series(n): sum = 0 for i in range(1,n + 1): sum += i * i*i return sum
[ "assert sum_Of_Series(5) == 225", "assert sum_Of_Series(2) == 9", "assert sum_Of_Series(3) == 36" ]
[]
760
Write a python function to check whether an array contains only one distinct element or not.
def unique_Element(arr,n): s = set(arr) if (len(s) == 1): return ('YES') else: return ('NO')
[ "assert unique_Element([1,1,1],3) == 'YES'", "assert unique_Element([1,2,1,2],4) == 'NO'", "assert unique_Element([1,2,3,4,5],5) == 'NO'" ]
[]
730
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)]
[ "assert consecutive_duplicates([0, 0, 1, 2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 4, 4 ])==[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4]", "assert consecutive_duplicates([10, 10, 15, 19, 18, 18, 17, 26, 26, 17, 18, 10])==[10, 15, 19, 18, 17, 26, 17, 18, 10]", "assert consecutive_duplicates(['a', 'a', 'b', 'c', 'd', 'd'])==['a', 'b', 'c', 'd']" ]
[]
16
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!')
[ "assert text_lowercase_underscore(\"aab_cbbbc\")==('Found a match!')", "assert text_lowercase_underscore(\"aab_Abbbc\")==('Not matched!')", "assert text_lowercase_underscore(\"Aaab_abbbc\")==('Not matched!')" ]
[ "assert text_lowercase_underscore(\"aab-cbbbc\")==('Not matched!')" ]
447
Write a function to find cubes of individual elements in a list using lambda function.
def cube_nums(nums): cube_nums = list(map(lambda x: x ** 3, nums)) return cube_nums
[ "assert cube_nums([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])==[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]", "assert cube_nums([10,20,30])==([1000, 8000, 27000])", "assert cube_nums([12,15])==([1728, 3375])" ]
[]
141
Write a function to sort a list of elements using pancake sort.
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
[ "assert pancake_sort([15, 79, 25, 38, 69]) == [15, 25, 38, 69, 79]", "assert pancake_sort([98, 12, 54, 36, 85]) == [12, 36, 54, 85, 98]", "assert pancake_sort([41, 42, 32, 12, 23]) == [12, 23, 32, 41, 42]" ]
[]
941
Write a function to count the elements in a list until an element is a tuple.
def count_elim(num): count_elim = 0 for n in num: if isinstance(n, tuple): break count_elim += 1 return count_elim
[ "assert count_elim([10,20,30,(10,20),40])==3", "assert count_elim([10,(20,30),(10,20),40])==1", "assert count_elim([(10,(20,30,(10,20),40))])==0" ]
[]
690
Write a function to multiply consecutive numbers of a given list.
def mul_consecutive_nums(nums): result = [b*a for a, b in zip(nums[:-1], nums[1:])] return result
[ "assert mul_consecutive_nums([1, 1, 3, 4, 4, 5, 6, 7])==[1, 3, 12, 16, 20, 30, 42]", "assert mul_consecutive_nums([4, 5, 8, 9, 6, 10])==[20, 40, 72, 54, 60]", "assert mul_consecutive_nums([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])==[2, 6, 12, 20, 30, 42, 56, 72, 90]" ]
[]
243
Write a function to sort the given list based on the occurrence of first element of tuples.
def sort_on_occurence(lst): dct = {} for i, j in lst: dct.setdefault(i, []).append(j) return ([(i, *dict.fromkeys(j), len(j)) for i, j in dct.items()])
[ "assert sort_on_occurence([(1, 'Jake'), (2, 'Bob'), (1, 'Cara')]) == [(1, 'Jake', 'Cara', 2), (2, 'Bob', 1)]", "assert sort_on_occurence([('b', 'ball'), ('a', 'arm'), ('b', 'b'), ('a', 'ant')]) == [('b', 'ball', 'b', 2), ('a', 'arm', 'ant', 2)]", "assert sort_on_occurence([(2, 'Mark'), (3, 'Maze'), (2, 'Sara')]) == [(2, 'Mark', 'Sara', 2), (3, 'Maze', 1)]" ]
[]
512
Write a function to count the element frequency in the mixed nested tuple.
def flatten(test_tuple): for tup in test_tuple: if isinstance(tup, tuple): yield from flatten(tup) else: yield tup def count_element_freq(test_tuple): res = {} for ele in flatten(test_tuple): if ele not in res: res[ele] = 0 res[ele] += 1 return (res)
[ "assert count_element_freq((5, 6, (5, 6), 7, (8, 9), 9) ) == {5: 2, 6: 2, 7: 1, 8: 1, 9: 2}", "assert count_element_freq((6, 7, (6, 7), 8, (9, 10), 10) ) == {6: 2, 7: 2, 8: 1, 9: 1, 10: 2}", "assert count_element_freq((7, 8, (7, 8), 9, (10, 11), 11) ) == {7: 2, 8: 2, 9: 1, 10: 1, 11: 2}" ]
[]
41
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
[ "assert filter_evennumbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])==[2, 4, 6, 8, 10]", "assert filter_evennumbers([10,20,45,67,84,93])==[10,20,84]", "assert filter_evennumbers([5,7,9,8,6,4,3])==[8,6,4]" ]
[]
395
Write a python function to find the first non-repeated character in a given string.
def first_non_repeating_character(str1): char_order = [] ctr = {} for c in str1: if c in ctr: ctr[c] += 1 else: ctr[c] = 1 char_order.append(c) for c in char_order: if ctr[c] == 1: return c return None
[ "assert first_non_repeating_character(\"abcabc\") == None", "assert first_non_repeating_character(\"abc\") == \"a\"", "assert first_non_repeating_character(\"ababc\") == \"c\"" ]
[]
630
Write a function to extract all the adjacent coordinates of the given coordinate tuple.
def adjac(ele, sub = []): if not ele: yield sub else: yield from [idx for j in range(ele[0] - 1, ele[0] + 2) for idx in adjac(ele[1:], sub + [j])] def get_coordinates(test_tup): res = list(adjac(test_tup)) return (res)
[ "assert get_coordinates((3, 4)) == [[2, 3], [2, 4], [2, 5], [3, 3], [3, 4], [3, 5], [4, 3], [4, 4], [4, 5]]", "assert get_coordinates((4, 5)) ==[[3, 4], [3, 5], [3, 6], [4, 4], [4, 5], [4, 6], [5, 4], [5, 5], [5, 6]]", "assert get_coordinates((5, 6)) == [[4, 5], [4, 6], [4, 7], [5, 5], [5, 6], [5, 7], [6, 5], [6, 6], [6, 7]]" ]
[]
896
Write a function to sort a list in increasing order by the last element in each tuple from a given list of non-empty tuples.
def last(n): return n[-1] def sort_list_last(tuples): return sorted(tuples, key=last)
[ "assert sort_list_last([(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)])==[(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)] ", "assert sort_list_last([(9,8), (4, 7), (3,5), (7,9), (1,2)])==[(1,2), (3,5), (4,7), (9,8), (7,9)] ", "assert sort_list_last([(20,50), (10,20), (40,40)])==[(10,20),(40,40),(20,50)] " ]
[]
444
Write a function to trim each tuple by k in the given tuple list.
def trim_tuple(test_list, K): res = [] for ele in test_list: N = len(ele) res.append(tuple(list(ele)[K: N - K])) return (str(res))
[ "assert trim_tuple([(5, 3, 2, 1, 4), (3, 4, 9, 2, 1),(9, 1, 2, 3, 5), (4, 8, 2, 1, 7)], 2) == '[(2,), (9,), (2,), (2,)]'", "assert trim_tuple([(5, 3, 2, 1, 4), (3, 4, 9, 2, 1), (9, 1, 2, 3, 5), (4, 8, 2, 1, 7)], 1) == '[(3, 2, 1), (4, 9, 2), (1, 2, 3), (8, 2, 1)]'", "assert trim_tuple([(7, 8, 4, 9), (11, 8, 12, 4),(4, 1, 7, 8), (3, 6, 9, 7)], 1) == '[(8, 4), (8, 12), (1, 7), (6, 9)]'" ]
[]
167
Write a python function to find smallest power of 2 greater than or equal to n.
def next_Power_Of_2(n): count = 0; if (n and not(n & (n - 1))): return n while( n != 0): n >>= 1 count += 1 return 1 << count;
[ "assert next_Power_Of_2(0) == 1", "assert next_Power_Of_2(5) == 8", "assert next_Power_Of_2(17) == 32" ]
[]
523
Write a function to check whether a given string has a capital letter, a lower case letter, a number and specified length using lambda function.
def check_string(str1): messg = [ lambda str1: any(x.isupper() for x in str1) or 'String must have 1 upper case character.', lambda str1: any(x.islower() for x in str1) or 'String must have 1 lower case character.', lambda str1: any(x.isdigit() for x in str1) or 'String must have 1 number.', lambda str1: len(str1) >= 7 or 'String length should be atleast 8.',] result = [x for x in [i(str1) for i in messg] if x != True] if not result: result.append('Valid string.') return result
[ "assert check_string('python')==['String must have 1 upper case character.', 'String must have 1 number.', 'String length should be atleast 8.']", "assert check_string('123python')==['String must have 1 upper case character.']", "assert check_string('123Python')==['Valid string.']" ]
[]
584
Write a function to find all adverbs and their positions in a given sentence by using regex.
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)))
[ "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 quickly\") == '18-25: quickly'" ]
[]
49
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
[ "assert specified_element([[1, 2, 3, 2], [4, 5, 6, 2], [7, 1, 9, 5]],0)==[1, 4, 7]", "assert specified_element([[1, 2, 3, 2], [4, 5, 6, 2], [7, 1, 9, 5]],2)==[3, 6, 9]", "assert specified_element([[1, 2, 3, 2], [4, 5, 6, 2], [7, 1, 9, 5]],1)==[2,5,1]" ]
[]
967
Write a python function to accept the strings which contains all vowels.
def check(string): if len(set(string).intersection("AEIOUaeiou"))>=5: return ('accepted') else: return ("not accepted")
[ "assert check(\"SEEquoiaL\") == 'accepted'", "assert check('program') == \"not accepted\"", "assert check('fine') == \"not accepted\"" ]
[]
477
Write a python function to convert the given string to lower case.
def is_lower(string): return (string.lower())
[ "assert is_lower(\"InValid\") == \"invalid\"", "assert is_lower(\"TruE\") == \"true\"", "assert is_lower(\"SenTenCE\") == \"sentence\"" ]
[]
616
Write a function to perfom the modulo of tuple elements in the given two tuples.
def tuple_modulo(test_tup1, test_tup2): res = tuple(ele1 % ele2 for ele1, ele2 in zip(test_tup1, test_tup2)) return (res)
[ "assert tuple_modulo((10, 4, 5, 6), (5, 6, 7, 5)) == (0, 4, 5, 1)", "assert tuple_modulo((11, 5, 6, 7), (6, 7, 8, 6)) == (5, 5, 6, 1)", "assert tuple_modulo((12, 6, 7, 8), (7, 8, 9, 7)) == (5, 6, 7, 1)" ]
[]