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
|
---|---|---|---|---|---|
835 | Write a python function to find the slope of a line. | def slope(x1,y1,x2,y2):
return (float)(y2-y1)/(x2-x1) | [
"assert slope(4,2,2,5) == -1.5",
"assert slope(2,4,4,6) == 1",
"assert slope(1,2,4,2) == 0"
] | [] |
|
471 | Write a python function to find remainder of array multiplication divided by n. | def find_remainder(arr, lens, n):
mul = 1
for i in range(lens):
mul = (mul * (arr[i] % n)) % n
return mul % n | [
"assert find_remainder([ 100, 10, 5, 25, 35, 14 ],6,11) ==9",
"assert find_remainder([1,1,1],3,1) == 0",
"assert find_remainder([1,2,1],3,2) == 0"
] | [] |
|
31 | 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 | [
"assert func([[1, 2, 6], [1, 3, 4, 5, 7, 8], [1, 3, 5, 6, 8, 9], [2, 5, 7, 11], [1, 4, 7, 8, 12]],3)==[5, 7, 1]",
"assert func([[1, 2, 6], [1, 3, 4, 5, 7, 8], [1, 3, 5, 6, 8, 9], [2, 5, 7, 11], [1, 4, 7, 8, 12]],1)==[1]",
"assert func([[1, 2, 6], [1, 3, 4, 5, 7, 8], [1, 3, 5, 6, 8, 9], [2, 5, 7, 11], [1, 4, 7, 8, 12]],5)==[6, 5, 7, 8, 1]"
] | [] |
|
87 | 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 | [
"assert merge_dictionaries_three({ \"R\": \"Red\", \"B\": \"Black\", \"P\": \"Pink\" }, { \"G\": \"Green\", \"W\": \"White\" },{ \"O\": \"Orange\", \"W\": \"White\", \"B\": \"Black\" })=={'B': 'Black', 'R': 'Red', 'P': 'Pink', 'G': 'Green', 'W': 'White', 'O': 'Orange'}",
"assert merge_dictionaries_three({ \"R\": \"Red\", \"B\": \"Black\", \"P\": \"Pink\" }, { \"G\": \"Green\", \"W\": \"White\" },{\"L\":\"lavender\",\"B\":\"Blue\"})=={'W': 'White', 'P': 'Pink', 'B': 'Black', 'R': 'Red', 'G': 'Green', 'L': 'lavender'}",
"assert merge_dictionaries_three({ \"R\": \"Red\", \"B\": \"Black\", \"P\": \"Pink\" },{\"L\":\"lavender\",\"B\":\"Blue\"},{ \"G\": \"Green\", \"W\": \"White\" })=={'B': 'Black', 'P': 'Pink', 'R': 'Red', 'G': 'Green', 'L': 'lavender', 'W': 'White'}"
] | [] |
|
649 | Write a python function to calculate the sum of the numbers in a list between the indices of a specified range. | def sum_Range_list(nums, m, n):
sum_range = 0
for i in range(m, n+1, 1):
sum_range += nums[i]
return sum_range | [
"assert sum_Range_list([2, 1, 5, 6, 8, 3, 4, 9, 10, 11, 8, 12],8,10) == 29",
"assert sum_Range_list([1,2,3,4,5],1,2) == 5",
"assert sum_Range_list([1,0,1,2,5,6],4,5) == 11"
] | [] |
|
808 | Write a function to check if the given tuples contain the k or not. | def check_K(test_tup, K):
res = False
for ele in test_tup:
if ele == K:
res = True
break
return (res) | [
"assert check_K((10, 4, 5, 6, 8), 6) == True",
"assert check_K((1, 2, 3, 4, 5, 6), 7) == False",
"assert check_K((7, 8, 9, 44, 11, 12), 11) == True"
] | [] |
|
528 | Write a function to find the list of lists with minimum length. | def min_length(list1):
min_length = min(len(x) for x in list1 )
min_list = min((x) for x in list1)
return(min_length, min_list) | [
"assert min_length([[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]])==(1, [0])",
"assert min_length([[1], [5, 7], [10, 12, 14,15]])==(1, [1])",
"assert min_length([[5], [15,20,25]])==(1, [5])"
] | [] |
|
429 | Write a function to extract the elementwise and tuples from the given two tuples. | def and_tuples(test_tup1, test_tup2):
res = tuple(ele1 & ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
return (res) | [
"assert and_tuples((10, 4, 6, 9), (5, 2, 3, 3)) == (0, 0, 2, 1)",
"assert and_tuples((1, 2, 3, 4), (5, 6, 7, 8)) == (1, 2, 3, 0)",
"assert and_tuples((8, 9, 11, 12), (7, 13, 14, 17)) == (0, 9, 10, 0)"
] | [] |
|
132 | Write a function to convert tuple to a string. | def tup_string(tup1):
str = ''.join(tup1)
return str | [
"assert tup_string(('e', 'x', 'e', 'r', 'c', 'i', 's', 'e', 's'))==(\"exercises\")",
"assert tup_string(('p','y','t','h','o','n'))==(\"python\")",
"assert tup_string(('p','r','o','g','r','a','m'))==(\"program\")"
] | [] |
|
194 | Write a python function to convert octal number to decimal number. | def octal_To_Decimal(n):
num = n;
dec_value = 0;
base = 1;
temp = num;
while (temp):
last_digit = temp % 10;
temp = int(temp / 10);
dec_value += last_digit*base;
base = base * 8;
return dec_value; | [
"assert octal_To_Decimal(25) == 21",
"assert octal_To_Decimal(30) == 24",
"assert octal_To_Decimal(40) == 32"
] | [] |
|
276 | Write a function to find the volume of a cylinder. | def volume_cylinder(r,h):
volume=3.1415*r*r*h
return volume | [
"assert volume_cylinder(10,5)==1570.7500000000002",
"assert volume_cylinder(4,5)==251.32000000000002",
"assert volume_cylinder(4,10)==502.64000000000004"
] | [] |
|
114 | Write a function to assign frequency to each tuple in the given tuple list. | from collections import Counter
def assign_freq(test_list):
res = [(*key, val) for key, val in Counter(test_list).items()]
return (str(res)) | [
"assert assign_freq([(6, 5, 8), (2, 7), (6, 5, 8), (6, 5, 8), (9, ), (2, 7)] ) == '[(6, 5, 8, 3), (2, 7, 2), (9, 1)]'",
"assert assign_freq([(4, 2, 4), (7, 1), (4, 8), (4, 2, 4), (9, 2), (7, 1)] ) == '[(4, 2, 4, 2), (7, 1, 2), (4, 8, 1), (9, 2, 1)]'",
"assert assign_freq([(11, 13, 10), (17, 21), (4, 2, 3), (17, 21), (9, 2), (4, 2, 3)] ) == '[(11, 13, 10, 1), (17, 21, 2), (4, 2, 3, 2), (9, 2, 1)]'"
] | [] |
|
330 | Write a function to find all three, four, five characters long words in the given string by using regex. | import re
def find_char(text):
return (re.findall(r"\b\w{3,5}\b", text)) | [
"assert find_char('For the four consumer complaints contact manager AKR reddy') == ['For', 'the', 'four', 'AKR', 'reddy']",
"assert find_char('Certain service are subject to change MSR') == ['are', 'MSR']",
"assert find_char('Third party legal desclaimers') == ['Third', 'party', 'legal']"
] | [] |
|
231 | Write a function to find the maximum sum in the given right triangle of numbers. | def max_sum(tri, n):
if n > 1:
tri[1][1] = tri[1][1]+tri[0][0]
tri[1][0] = tri[1][0]+tri[0][0]
for i in range(2, n):
tri[i][0] = tri[i][0] + tri[i-1][0]
tri[i][i] = tri[i][i] + tri[i-1][i-1]
for j in range(1, i):
if tri[i][j]+tri[i-1][j-1] >= tri[i][j]+tri[i-1][j]:
tri[i][j] = tri[i][j] + tri[i-1][j-1]
else:
tri[i][j] = tri[i][j]+tri[i-1][j]
return (max(tri[n-1])) | [
"assert max_sum([[1], [2,1], [3,3,2]], 3) == 6",
"assert max_sum([[1], [1, 2], [4, 1, 12]], 3) == 15 ",
"assert max_sum([[2], [3,2], [13,23,12]], 3) == 28"
] | [] |
|
929 | Write a function to count repeated items of a tuple. | def count_tuplex(tuplex,value):
count = tuplex.count(value)
return count | [
"assert count_tuplex((2, 4, 5, 6, 2, 3, 4, 4, 7),4)==3",
"assert count_tuplex((2, 4, 5, 6, 2, 3, 4, 4, 7),2)==2",
"assert count_tuplex((2, 4, 7, 7, 7, 3, 4, 4, 7),7)==4"
] | [] |
|
198 | Write a function to find the largest triangle that can be inscribed in an ellipse. | import math
def largest_triangle(a,b):
if (a < 0 or b < 0):
return -1
area = (3 * math.sqrt(3) * pow(a, 2)) / (4 * b);
return area | [
"assert largest_triangle(4,2)==10.392304845413264",
"assert largest_triangle(5,7)==4.639421805988064",
"assert largest_triangle(9,1)==105.2220865598093"
] | [] |
|
526 | Write a python function to capitalize first and last letters of each word of a given string. | def capitalize_first_last_letters(str1):
str1 = result = str1.title()
result = ""
for word in str1.split():
result += word[:-1] + word[-1].upper() + " "
return result[:-1] | [
"assert capitalize_first_last_letters(\"python\") == \"PythoN\"",
"assert capitalize_first_last_letters(\"bigdata\") == \"BigdatA\"",
"assert capitalize_first_last_letters(\"Hadoop\") == \"HadooP\""
] | [] |
|
848 | Write a function to find the area of a trapezium. | def area_trapezium(base1,base2,height):
area = 0.5 * (base1 + base2) * height
return area | [
"assert area_trapezium(6,9,4)==30",
"assert area_trapezium(10,20,30)==450",
"assert area_trapezium(15,25,35)==700"
] | [] |
|
877 | Write a python function to sort the given string. | def sort_String(str) :
str = ''.join(sorted(str))
return (str) | [
"assert sort_String(\"cba\") == \"abc\"",
"assert sort_String(\"data\") == \"aadt\"",
"assert sort_String(\"zxy\") == \"xyz\""
] | [] |
|
799 | Write a python function to left rotate the bits of a given number. | INT_BITS = 32
def left_Rotate(n,d):
return (n << d)|(n >> (INT_BITS - d)) | [
"assert left_Rotate(16,2) == 64",
"assert left_Rotate(10,2) == 40",
"assert left_Rotate(99,3) == 792"
] | [] |
|
659 | Write a python function to print duplicants from a list of integers. | def Repeat(x):
_size = len(x)
repeated = []
for i in range(_size):
k = i + 1
for j in range(k, _size):
if x[i] == x[j] and x[i] not in repeated:
repeated.append(x[i])
return repeated | [
"assert Repeat([10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]) == [20, 30, -20, 60]",
"assert Repeat([-1, 1, -1, 8]) == [-1]",
"assert Repeat([1, 2, 3, 1, 2,]) == [1, 2]"
] | [] |
|
667 | Write a python function to count number of vowels in the string. | def Check_Vow(string, vowels):
final = [each for each in string if each in vowels]
return(len(final))
| [
"assert Check_Vow('corner','AaEeIiOoUu') == 2",
"assert Check_Vow('valid','AaEeIiOoUu') == 2",
"assert Check_Vow('true','AaEeIiOoUu') ==2"
] | [] |
|
859 | Write a function to generate all sublists of a given list. | from itertools import combinations
def sub_lists(my_list):
subs = []
for i in range(0, len(my_list)+1):
temp = [list(x) for x in combinations(my_list, i)]
if len(temp)>0:
subs.extend(temp)
return subs | [
"assert sub_lists([10, 20, 30, 40])==[[], [10], [20], [30], [40], [10, 20], [10, 30], [10, 40], [20, 30], [20, 40], [30, 40], [10, 20, 30], [10, 20, 40], [10, 30, 40], [20, 30, 40], [10, 20, 30, 40]]",
"assert sub_lists(['X', 'Y', 'Z'])==[[], ['X'], ['Y'], ['Z'], ['X', 'Y'], ['X', 'Z'], ['Y', 'Z'], ['X', 'Y', 'Z']]",
"assert sub_lists([1,2,3])==[[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]"
] | [] |
|
56 | 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) | [
"assert check(70) == False",
"assert check(23) == False",
"assert check(73) == True"
] | [] |
|
127 | Write a function to multiply two integers without using the * operator in python. | 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) | [
"assert multiply_int(10,20)==200",
"assert multiply_int(5,10)==50",
"assert multiply_int(4,8)==32"
] | [] |
|
291 | Write a function to find out the number of ways of painting the fence such that at most 2 adjacent posts have the same color for the given fence with n posts and k colors. | def count_no_of_ways(n, k):
dp = [0] * (n + 1)
total = k
mod = 1000000007
dp[1] = k
dp[2] = k * k
for i in range(3,n+1):
dp[i] = ((k - 1) * (dp[i - 1] + dp[i - 2])) % mod
return dp[n] | [
"assert count_no_of_ways(2, 4) == 16",
"assert count_no_of_ways(3, 2) == 6",
"assert count_no_of_ways(4, 4) == 228"
] | [] |
|
188 | Write a python function to check whether the given number can be represented by product of two squares or not. | def prod_Square(n):
for i in range(2,(n) + 1):
if (i*i < (n+1)):
for j in range(2,n + 1):
if ((i*i*j*j) == n):
return True;
return False; | [
"assert prod_Square(25) == False",
"assert prod_Square(30) == False",
"assert prod_Square(16) == True"
] | [] |
|
636 | Write a python function to check if roots of a quadratic equation are reciprocal of each other or not. | def Check_Solution(a,b,c):
if (a == c):
return ("Yes");
else:
return ("No"); | [
"assert Check_Solution(2,0,2) == \"Yes\"",
"assert Check_Solution(2,-5,2) == \"Yes\"",
"assert Check_Solution(1,2,3) == \"No\""
] | [] |
|
493 | Write a function to calculate a grid of hexagon coordinates where function returns a list of lists containing 6 tuples of x, y point coordinates. | import math
def calculate_polygons(startx, starty, endx, endy, radius):
sl = (2 * radius) * math.tan(math.pi / 6)
p = sl * 0.5
b = sl * math.cos(math.radians(30))
w = b * 2
h = 2 * sl
startx = startx - w
starty = starty - h
endx = endx + w
endy = endy + h
origx = startx
origy = starty
xoffset = b
yoffset = 3 * p
polygons = []
row = 1
counter = 0
while starty < endy:
if row % 2 == 0:
startx = origx + xoffset
else:
startx = origx
while startx < endx:
p1x = startx
p1y = starty + p
p2x = startx
p2y = starty + (3 * p)
p3x = startx + b
p3y = starty + h
p4x = startx + w
p4y = starty + (3 * p)
p5x = startx + w
p5y = starty + p
p6x = startx + b
p6y = starty
poly = [
(p1x, p1y),
(p2x, p2y),
(p3x, p3y),
(p4x, p4y),
(p5x, p5y),
(p6x, p6y),
(p1x, p1y)]
polygons.append(poly)
counter += 1
startx += w
starty += yoffset
row += 1
return polygons | [
"assert calculate_polygons(1,1, 4, 4, 3)==[[(-5.0, -4.196152422706632), (-5.0, -0.7320508075688767), (-2.0, 1.0), (1.0, -0.7320508075688767), (1.0, -4.196152422706632), (-2.0, -5.928203230275509), (-5.0, -4.196152422706632)], [(1.0, -4.196152422706632), (1.0, -0.7320508075688767), (4.0, 1.0), (7.0, -0.7320508075688767), (7.0, -4.196152422706632), (4.0, -5.928203230275509), (1.0, -4.196152422706632)], [(7.0, -4.196152422706632), (7.0, -0.7320508075688767), (10.0, 1.0), (13.0, -0.7320508075688767), (13.0, -4.196152422706632), (10.0, -5.928203230275509), (7.0, -4.196152422706632)], [(-2.0, 1.0000000000000004), (-2.0, 4.464101615137755), (1.0, 6.196152422706632), (4.0, 4.464101615137755), (4.0, 1.0000000000000004), (1.0, -0.7320508075688767), (-2.0, 1.0000000000000004)], [(4.0, 1.0000000000000004), (4.0, 4.464101615137755), (7.0, 6.196152422706632), (10.0, 4.464101615137755), (10.0, 1.0000000000000004), (7.0, -0.7320508075688767), (4.0, 1.0000000000000004)], [(-5.0, 6.196152422706632), (-5.0, 9.660254037844387), (-2.0, 11.392304845413264), (1.0, 9.660254037844387), (1.0, 6.196152422706632), (-2.0, 4.464101615137755), (-5.0, 6.196152422706632)], [(1.0, 6.196152422706632), (1.0, 9.660254037844387), (4.0, 11.392304845413264), (7.0, 9.660254037844387), (7.0, 6.196152422706632), (4.0, 4.464101615137755), (1.0, 6.196152422706632)], [(7.0, 6.196152422706632), (7.0, 9.660254037844387), (10.0, 11.392304845413264), (13.0, 9.660254037844387), (13.0, 6.196152422706632), (10.0, 4.464101615137755), (7.0, 6.196152422706632)], [(-2.0, 11.392304845413264), (-2.0, 14.85640646055102), (1.0, 16.588457268119896), (4.0, 14.85640646055102), (4.0, 11.392304845413264), (1.0, 9.660254037844387), (-2.0, 11.392304845413264)], [(4.0, 11.392304845413264), (4.0, 14.85640646055102), (7.0, 16.588457268119896), (10.0, 14.85640646055102), (10.0, 11.392304845413264), (7.0, 9.660254037844387), (4.0, 11.392304845413264)]]",
"assert calculate_polygons(5,4,7,9,8)==[[(-11.0, -9.856406460551018), (-11.0, -0.6188021535170058), (-3.0, 4.0), (5.0, -0.6188021535170058), (5.0, -9.856406460551018), (-3.0, -14.475208614068023), (-11.0, -9.856406460551018)], [(5.0, -9.856406460551018), (5.0, -0.6188021535170058), (13.0, 4.0), (21.0, -0.6188021535170058), (21.0, -9.856406460551018), (13.0, -14.475208614068023), (5.0, -9.856406460551018)], [(21.0, -9.856406460551018), (21.0, -0.6188021535170058), (29.0, 4.0), (37.0, -0.6188021535170058), (37.0, -9.856406460551018), (29.0, -14.475208614068023), (21.0, -9.856406460551018)], [(-3.0, 4.0), (-3.0, 13.237604307034012), (5.0, 17.856406460551018), (13.0, 13.237604307034012), (13.0, 4.0), (5.0, -0.6188021535170058), (-3.0, 4.0)], [(13.0, 4.0), (13.0, 13.237604307034012), (21.0, 17.856406460551018), (29.0, 13.237604307034012), (29.0, 4.0), (21.0, -0.6188021535170058), (13.0, 4.0)], [(-11.0, 17.856406460551018), (-11.0, 27.09401076758503), (-3.0, 31.712812921102035), (5.0, 27.09401076758503), (5.0, 17.856406460551018), (-3.0, 13.237604307034012), (-11.0, 17.856406460551018)], [(5.0, 17.856406460551018), (5.0, 27.09401076758503), (13.0, 31.712812921102035), (21.0, 27.09401076758503), (21.0, 17.856406460551018), (13.0, 13.237604307034012), (5.0, 17.856406460551018)], [(21.0, 17.856406460551018), (21.0, 27.09401076758503), (29.0, 31.712812921102035), (37.0, 27.09401076758503), (37.0, 17.856406460551018), (29.0, 13.237604307034012), (21.0, 17.856406460551018)], [(-3.0, 31.712812921102035), (-3.0, 40.95041722813605), (5.0, 45.569219381653056), (13.0, 40.95041722813605), (13.0, 31.712812921102035), (5.0, 27.09401076758503), (-3.0, 31.712812921102035)], [(13.0, 31.712812921102035), (13.0, 40.95041722813605), (21.0, 45.569219381653056), (29.0, 40.95041722813605), (29.0, 31.712812921102035), (21.0, 27.09401076758503), (13.0, 31.712812921102035)]]",
"assert calculate_polygons(9,6,4,3,2)==[[(5.0, 2.5358983848622456), (5.0, 4.8452994616207485), (7.0, 6.0), (9.0, 4.8452994616207485), (9.0, 2.5358983848622456), (7.0, 1.3811978464829942), (5.0, 2.5358983848622456)], [(7.0, 6.0), (7.0, 8.309401076758503), (9.0, 9.464101615137753), (11.0, 8.309401076758503), (11.0, 6.0), (9.0, 4.8452994616207485), (7.0, 6.0)]]"
] | [] |
|
964 | Write a python function to check whether the length of the word is even or not. | def word_len(s):
s = s.split(' ')
for word in s:
if len(word)%2==0:
return True
else:
return False | [
"assert word_len(\"program\") == False",
"assert word_len(\"solution\") == True",
"assert word_len(\"data\") == True"
] | [] |
|
328 | Write a function to rotate a given list by specified number of items to the left direction. | def rotate_left(list1,m,n):
result = list1[m:]+list1[:n]
return result | [
"assert rotate_left([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],3,4)==[4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4]",
"assert rotate_left([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],2,2)==[3, 4, 5, 6, 7, 8, 9, 10, 1, 2]",
"assert rotate_left([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],5,2)==[6, 7, 8, 9, 10, 1, 2]"
] | [] |
|
260 | 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) | [
"assert newman_prime(3) == 7 ",
"assert newman_prime(4) == 17",
"assert newman_prime(5) == 41"
] | [] |
|
820 | Write a function to check whether the given month number contains 28 days or not. | def check_monthnum_number(monthnum1):
if monthnum1 == 2:
return True
else:
return False | [
"assert check_monthnum_number(2)==True",
"assert check_monthnum_number(1)==False",
"assert check_monthnum_number(3)==False"
] | [] |
|
108 | Write a function to merge multiple sorted inputs into a single sorted iterator using heap queue algorithm. | 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) | [
"assert merge_sorted_list([25, 24, 15, 4, 5, 29, 110],[19, 20, 11, 56, 25, 233, 154],[24, 26, 54, 48])==[4, 5, 11, 15, 19, 20, 24, 24, 25, 25, 26, 29, 48, 54, 56, 110, 154, 233]",
"assert merge_sorted_list([1, 3, 5, 6, 8, 9], [2, 5, 7, 11], [1, 4, 7, 8, 12])==[1, 1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 9, 11, 12]",
"assert merge_sorted_list([18, 14, 10, 9, 8, 7, 9, 3, 2, 4, 1],[25, 35, 22, 85, 14, 65, 75, 25, 58],[12, 74, 9, 50, 61, 41])==[1, 2, 3, 4, 7, 8, 9, 9, 9, 10, 12, 14, 14, 18, 22, 25, 25, 35, 41, 50, 58, 61, 65, 74, 75, 85]"
] | [] |
|
381 | Write a function to sort a list of lists by a given index of the inner list. | from operator import itemgetter
def index_on_inner_list(list_data, index_no):
result = sorted(list_data, key=itemgetter(index_no))
return result | [
"assert index_on_inner_list([('Greyson Fulton', 98, 99), ('Brady Kent', 97, 96), ('Wyatt Knott', 91, 94), ('Beau Turnbull', 94, 98)] ,0)==[('Beau Turnbull', 94, 98), ('Brady Kent', 97, 96), ('Greyson Fulton', 98, 99), ('Wyatt Knott', 91, 94)]",
"assert index_on_inner_list([('Greyson Fulton', 98, 99), ('Brady Kent', 97, 96), ('Wyatt Knott', 91, 94), ('Beau Turnbull', 94, 98)] ,1)==[('Wyatt Knott', 91, 94), ('Beau Turnbull', 94, 98), ('Brady Kent', 97, 96), ('Greyson Fulton', 98, 99)]",
"assert index_on_inner_list([('Greyson Fulton', 98, 99), ('Brady Kent', 97, 96), ('Wyatt Knott', 91, 94), ('Beau Turnbull', 94, 98)] ,2)==[('Wyatt Knott', 91, 94), ('Brady Kent', 97, 96), ('Beau Turnbull', 94, 98), ('Greyson Fulton', 98, 99)]"
] | [] |
|
747 | Write a function to find the longest common subsequence for the given three string sequence. | def lcs_of_three(X, Y, Z, m, n, o):
L = [[[0 for i in range(o+1)] for j in range(n+1)]
for k in range(m+1)]
for i in range(m+1):
for j in range(n+1):
for k in range(o+1):
if (i == 0 or j == 0 or k == 0):
L[i][j][k] = 0
elif (X[i-1] == Y[j-1] and
X[i-1] == Z[k-1]):
L[i][j][k] = L[i-1][j-1][k-1] + 1
else:
L[i][j][k] = max(max(L[i-1][j][k],
L[i][j-1][k]),
L[i][j][k-1])
return L[m][n][o] | [
"assert lcs_of_three('AGGT12', '12TXAYB', '12XBA', 6, 7, 5) == 2",
"assert lcs_of_three('Reels', 'Reelsfor', 'ReelsforReels', 5, 8, 13) == 5 ",
"assert lcs_of_three('abcd1e2', 'bc12ea', 'bd1ea', 7, 6, 5) == 3"
] | [] |
|
283 | Write a python function to check whether the frequency of each digit is less than or equal to the digit itself. | def validate(n):
for i in range(10):
temp = n;
count = 0;
while (temp):
if (temp % 10 == i):
count+=1;
if (count > i):
return False
temp //= 10;
return True | [
"assert validate(1234) == True",
"assert validate(51241) == False",
"assert validate(321) == True"
] | [] |
|
609 | Write a python function to find minimum possible value for the given periodic function. | def floor_Min(A,B,N):
x = max(B - 1,N)
return (A*x) // B | [
"assert floor_Min(10,20,30) == 15",
"assert floor_Min(1,2,1) == 0",
"assert floor_Min(11,10,9) == 9"
] | [] |
|
826 | Write a python function to find the type of triangle from the given sides. | def check_Type_Of_Triangle(a,b,c):
sqa = pow(a,2)
sqb = pow(b,2)
sqc = pow(c,2)
if (sqa == sqa + sqb or sqb == sqa + sqc or sqc == sqa + sqb):
return ("Right-angled Triangle")
elif (sqa > sqc + sqb or sqb > sqa + sqc or sqc > sqa + sqb):
return ("Obtuse-angled Triangle")
else:
return ("Acute-angled Triangle") | [
"assert check_Type_Of_Triangle(1,2,3) == \"Obtuse-angled Triangle\"",
"assert check_Type_Of_Triangle(2,2,2) == \"Acute-angled Triangle\"",
"assert check_Type_Of_Triangle(1,0,1) == \"Right-angled Triangle\""
] | [] |
|
134 | Write a python function to check whether the last element of given array is even or odd after performing an operation p times. | def check_last (arr,n,p):
_sum = 0
for i in range(n):
_sum = _sum + arr[i]
if p == 1:
if _sum % 2 == 0:
return "ODD"
else:
return "EVEN"
return "EVEN"
| [
"assert check_last([5,7,10],3,1) == \"ODD\"",
"assert check_last([2,3],2,3) == \"EVEN\"",
"assert check_last([1,2,3],3,1) == \"ODD\""
] | [] |
|
162 | Write a function to calculate the sum of the positive integers of 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) | [
"assert sum_series(6)==12",
"assert sum_series(10)==30",
"assert sum_series(9)==25"
] | [] |
|
312 | Write a function to find the volume of a cone. | import math
def volume_cone(r,h):
volume = (1.0/3) * math.pi * r * r * h
return volume | [
"assert volume_cone(5,12)==314.15926535897927",
"assert volume_cone(10,15)==1570.7963267948965",
"assert volume_cone(19,17)==6426.651371693521"
] | [] |
|
199 | Write a python function to find highest power of 2 less than or equal to given number. | def highest_Power_of_2(n):
res = 0;
for i in range(n, 0, -1):
if ((i & (i - 1)) == 0):
res = i;
break;
return res; | [
"assert highest_Power_of_2(10) == 8",
"assert highest_Power_of_2(19) == 16",
"assert highest_Power_of_2(32) == 32"
] | [] |
|
869 | Write a function to remove sublists from a given list of lists, which are outside a given range. | def remove_list_range(list1, leftrange, rigthrange):
result = [i for i in list1 if (min(i)>=leftrange and max(i)<=rigthrange)]
return result | [
"assert remove_list_range([[2], [0], [1, 2, 3], [0, 1, 2, 3, 6, 7], [9, 11], [13, 14, 15, 17]],13,17)==[[13, 14, 15, 17]]",
"assert remove_list_range([[2], [0], [1, 2, 3], [0, 1, 2, 3, 6, 7], [9, 11], [13, 14, 15, 17]],1,3)==[[2], [1, 2, 3]]",
"assert remove_list_range([[2], [0], [1, 2, 3], [0, 1, 2, 3, 6, 7], [9, 11], [13, 14, 15, 17]],0,7)==[[2], [0], [1, 2, 3], [0, 1, 2, 3, 6, 7]]"
] | [] |
|
135 | Write a function to find the nth hexagonal number. | def hexagonal_num(n):
return n*(2*n - 1) | [
"assert hexagonal_num(10) == 190",
"assert hexagonal_num(5) == 45",
"assert hexagonal_num(7) == 91"
] | [] |
|
45 | 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 | [
"assert get_gcd([2, 4, 6, 8, 16]) == 2",
"assert get_gcd([1, 2, 3]) == 1",
"assert get_gcd([2, 4, 6, 8]) == 2 "
] | [] |
|
318 | Write a python function to find the maximum volume of a cuboid with given sum of sides. | def max_volume (s):
maxvalue = 0
i = 1
for i in range(s - 1):
j = 1
for j in range(s):
k = s - i - j
maxvalue = max(maxvalue, i * j * k)
return maxvalue | [
"assert max_volume(8) == 18",
"assert max_volume(4) == 2",
"assert max_volume(1) == 0"
] | [] |
|
29 | 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 | [
"assert get_Odd_Occurrence([1,2,3,1,2,3,1],7) == 1",
"assert get_Odd_Occurrence([1,2,3,2,3,1,3],7) == 3",
"assert get_Odd_Occurrence([2,3,5,4,5,2,4,3,5,2,4,4,2],13) == 5"
] | [] |
|
343 | Write a function to calculate the number of digits and letters in a string. | def dig_let(s):
d=l=0
for c in s:
if c.isdigit():
d=d+1
elif c.isalpha():
l=l+1
else:
pass
return (l,d) | [
"assert dig_let(\"python\")==(6,0)",
"assert dig_let(\"program\")==(7,0)",
"assert dig_let(\"python3.0\")==(6,2)"
] | [] |
|
736 | Write a function to locate the left insertion point for a specified value in sorted order. | import bisect
def left_insertion(a, x):
i = bisect.bisect_left(a, x)
return i | [
"assert left_insertion([1,2,4,5],6)==4",
"assert left_insertion([1,2,4,5],3)==2",
"assert left_insertion([1,2,4,5],7)==4"
] | [] |
|
248 | Write a function to calculate the harmonic sum of n-1. | def harmonic_sum(n):
if n < 2:
return 1
else:
return 1 / n + (harmonic_sum(n - 1)) | [
"assert harmonic_sum(7) == 2.5928571428571425",
"assert harmonic_sum(4) == 2.083333333333333",
"assert harmonic_sum(19) == 3.547739657143682"
] | [] |
|
711 | Write a python function to check whether the product of digits of a number at even and odd places is equal or not. | def product_Equal(n):
if n < 10:
return False
prodOdd = 1; prodEven = 1
while n > 0:
digit = n % 10
prodOdd *= digit
n = n//10
if n == 0:
break;
digit = n % 10
prodEven *= digit
n = n//10
if prodOdd == prodEven:
return True
return False | [
"assert product_Equal(2841) == True",
"assert product_Equal(1234) == False",
"assert product_Equal(1212) == False"
] | [] |
|
894 | Write a function to convert the given string of float type into tuple. | def float_to_tuple(test_str):
res = tuple(map(float, test_str.split(', ')))
return (res) | [
"assert float_to_tuple(\"1.2, 1.3, 2.3, 2.4, 6.5\") == (1.2, 1.3, 2.3, 2.4, 6.5)",
"assert float_to_tuple(\"2.3, 2.4, 5.6, 5.4, 8.9\") == (2.3, 2.4, 5.6, 5.4, 8.9)",
"assert float_to_tuple(\"0.3, 0.5, 7.8, 9.4\") == (0.3, 0.5, 7.8, 9.4)"
] | [] |
|
590 | Write a function to convert polar coordinates to rectangular coordinates. | import cmath
def polar_rect(x,y):
cn = complex(x,y)
cn=cmath.polar(cn)
cn1 = cmath.rect(2, cmath.pi)
return (cn,cn1) | [
"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, 0.8478169733934057), (-2+2.4492935982947064e-16j))"
] | [] |
|
365 | Write a python function to count the number of digits of a given number. | def count_Digit(n):
count = 0
while n != 0:
n //= 10
count += 1
return count | [
"assert count_Digit(12345) == 5",
"assert count_Digit(11223305) == 8",
"assert count_Digit(4123459) == 7"
] | [] |
|
400 | Write a function to extract the frequency of unique tuples in the given list order irrespective. | def extract_freq(test_list):
res = len(list(set(tuple(sorted(sub)) for sub in test_list)))
return (res) | [
"assert extract_freq([(3, 4), (1, 2), (4, 3), (5, 6)] ) == 3",
"assert extract_freq([(4, 15), (2, 3), (5, 4), (6, 7)] ) == 4",
"assert extract_freq([(5, 16), (2, 3), (6, 5), (6, 9)] ) == 4"
] | [] |
|
707 | Write a python function to count the total set bits from 1 to n. | def count_Set_Bits(n) :
n += 1;
powerOf2 = 2;
cnt = n // 2;
while (powerOf2 <= n) :
totalPairs = n // powerOf2;
cnt += (totalPairs // 2) * powerOf2;
if (totalPairs & 1) :
cnt += (n % powerOf2)
else :
cnt += 0
powerOf2 <<= 1;
return cnt; | [
"assert count_Set_Bits(16) == 33",
"assert count_Set_Bits(2) == 2",
"assert count_Set_Bits(14) == 28"
] | [] |
|
953 | Write a python function to find the minimun number of subsets with distinct elements. | def subset(ar, n):
res = 0
ar.sort()
for i in range(0, n) :
count = 1
for i in range(n - 1):
if ar[i] == ar[i + 1]:
count+=1
else:
break
res = max(res, count)
return res | [
"assert subset([1, 2, 3, 4],4) == 1",
"assert subset([5, 6, 9, 3, 4, 3, 4],7) == 2",
"assert subset([1, 2, 3 ],3) == 1"
] | [] |
|
422 | Write a python function to find the average of cubes of first n natural numbers. | def find_Average_Of_Cube(n):
sum = 0
for i in range(1, n + 1):
sum += i * i * i
return round(sum / n, 6) | [
"assert find_Average_Of_Cube(2) == 4.5",
"assert find_Average_Of_Cube(3) == 12",
"assert find_Average_Of_Cube(1) == 1"
] | [] |
|
483 | Write a python function to find the first natural number whose factorial is divisible by x. | def first_Factorial_Divisible_Number(x):
i = 1;
fact = 1;
for i in range(1,x):
fact = fact * i
if (fact % x == 0):
break
return i | [
"assert first_Factorial_Divisible_Number(10) == 5",
"assert first_Factorial_Divisible_Number(15) == 5",
"assert first_Factorial_Divisible_Number(5) == 4"
] | [] |
|
282 | Write a function to substaract two lists using map and lambda function. | def sub_list(nums1,nums2):
result = map(lambda x, y: x - y, nums1, nums2)
return list(result) | [
"assert sub_list([1, 2, 3],[4,5,6])==[-3,-3,-3]",
"assert sub_list([1,2],[3,4])==[-2,-2]",
"assert sub_list([90,120],[50,70])==[40,50]"
] | [] |
|
399 | Write a function to perform the mathematical bitwise xor operation across the given tuples. | def bitwise_xor(test_tup1, test_tup2):
res = tuple(ele1 ^ ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
return (res) | [
"assert bitwise_xor((10, 4, 6, 9), (5, 2, 3, 3)) == (15, 6, 5, 10)",
"assert bitwise_xor((11, 5, 7, 10), (6, 3, 4, 4)) == (13, 6, 3, 14)",
"assert bitwise_xor((12, 6, 8, 11), (7, 4, 5, 6)) == (11, 2, 13, 13)"
] | [] |
|
742 | Write a function to caluclate the area of a tetrahedron. | import math
def area_tetrahedron(side):
area = math.sqrt(3)*(side*side)
return area | [
"assert area_tetrahedron(3)==15.588457268119894",
"assert area_tetrahedron(20)==692.8203230275509",
"assert area_tetrahedron(10)==173.20508075688772"
] | [] |
|
491 | Write a function to find the sum of geometric progression series. | import math
def sum_gp(a,n,r):
total = (a * (1 - math.pow(r, n ))) / (1- r)
return total | [
"assert sum_gp(1,5,2)==31",
"assert sum_gp(1,5,4)==341",
"assert sum_gp(2,6,3)==728"
] | [] |
|
699 | Write a python function to find the 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(\"1111\",\"0100\") == \"Not Possible\"",
"assert min_Swaps(\"1110000\",\"0001101\") == 3"
] | [] |
|
368 | Write a function to repeat the given tuple n times. | def repeat_tuples(test_tup, N):
res = ((test_tup, ) * N)
return (res) | [
"assert repeat_tuples((1, 3), 4) == ((1, 3), (1, 3), (1, 3), (1, 3))",
"assert repeat_tuples((1, 2), 3) == ((1, 2), (1, 2), (1, 2))",
"assert repeat_tuples((3, 4), 5) == ((3, 4), (3, 4), (3, 4), (3, 4), (3, 4))"
] | [] |
|
882 | Write a function to caluclate perimeter of a parallelogram. | def parallelogram_perimeter(b,h):
perimeter=2*(b*h)
return perimeter | [
"assert parallelogram_perimeter(10,20)==400",
"assert parallelogram_perimeter(15,20)==600",
"assert parallelogram_perimeter(8,9)==144"
] | [] |
|
589 | Write a function to find perfect squares between two given numbers. | 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 | [
"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]"
] | [] |
|
80 | Write a function to find the nth tetrahedral number. | def tetrahedral_number(n):
return (n * (n + 1) * (n + 2)) / 6 | [
"assert tetrahedral_number(5) == 35.0",
"assert tetrahedral_number(6) == 56.0",
"assert tetrahedral_number(7) == 84.0"
] | [] |
|
796 | Write function to find the sum of all items in the given dictionary. | def return_sum(dict):
sum = 0
for i in dict.values():
sum = sum + i
return sum | [
"assert return_sum({'a': 100, 'b':200, 'c':300}) == 600",
"assert return_sum({'a': 25, 'b':18, 'c':45}) == 88",
"assert return_sum({'a': 36, 'b':39, 'c':49}) == 124"
] | [] |
|
136 | Write a function to calculate electricity bill. | def cal_electbill(units):
if(units < 50):
amount = units * 2.60
surcharge = 25
elif(units <= 100):
amount = 130 + ((units - 50) * 3.25)
surcharge = 35
elif(units <= 200):
amount = 130 + 162.50 + ((units - 100) * 5.26)
surcharge = 45
else:
amount = 130 + 162.50 + 526 + ((units - 200) * 8.45)
surcharge = 75
total = amount + surcharge
return total | [
"assert cal_electbill(75)==246.25",
"assert cal_electbill(265)==1442.75",
"assert cal_electbill(100)==327.5"
] | [] |
|
810 | Write a function to iterate over elements repeating each as many times as its count. | from collections import Counter
def count_variable(a,b,c,d):
c = Counter(p=a, q=b, r=c, s=d)
return list(c.elements()) | [
"assert count_variable(4,2,0,-2)==['p', 'p', 'p', 'p', 'q', 'q'] ",
"assert count_variable(0,1,2,3)==['q', 'r', 'r', 's', 's', 's'] ",
"assert count_variable(11,15,12,23)==['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'q', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's', 's']"
] | [] |
|
540 | Write a python function to find the difference between highest and least frequencies in a given array. | 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)
count = 0
return max_count - min_count | [
"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"
] | [] |
|
604 | Write a function to reverse words in a given string. | def reverse_words(s):
return ' '.join(reversed(s.split())) | [
"assert reverse_words(\"python program\")==(\"program python\")",
"assert reverse_words(\"java language\")==(\"language java\")",
"assert reverse_words(\"indian man\")==(\"man indian\")"
] | [] |
|
3 | 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 | [
"assert is_not_prime(2) == False",
"assert is_not_prime(10) == True",
"assert is_not_prime(35) == True"
] | [] |
|
883 | Write a function to find numbers divisible by m and n from a list of numbers using lambda function. | def div_of_nums(nums,m,n):
result = list(filter(lambda x: (x % m == 0 and x % n == 0), nums))
return result | [
"assert div_of_nums([19, 65, 57, 39, 152, 639, 121, 44, 90, 190],2,4)==[ 152,44]",
"assert div_of_nums([1, 2, 3, 5, 7, 8, 10],2,5)==[10]",
"assert div_of_nums([10,15,14,13,18,12,20],10,5)==[10,20]"
] | [] |
|
791 | Write a function to remove the nested record from the given tuple. | def remove_nested(test_tup):
res = tuple()
for count, ele in enumerate(test_tup):
if not isinstance(ele, tuple):
res = res + (ele, )
return (res) | [
"assert remove_nested((1, 5, 7, (4, 6), 10)) == (1, 5, 7, 10)",
"assert remove_nested((2, 6, 8, (5, 7), 11)) == (2, 6, 8, 11)",
"assert remove_nested((3, 7, 9, (6, 8), 12)) == (3, 7, 9, 12)"
] | [] |
|
672 | Write a function to find maximum of three numbers. | def max_of_three(num1,num2,num3):
if (num1 >= num2) and (num1 >= num3):
lnum = num1
elif (num2 >= num1) and (num2 >= num3):
lnum = num2
else:
lnum = num3
return lnum | [
"assert max_of_three(10,20,30)==30",
"assert max_of_three(55,47,39)==55",
"assert max_of_three(10,49,30)==49"
] | [] |
|
762 | Write a function to check whether the given month number contains 30 days or not. | def check_monthnumber_number(monthnum3):
if(monthnum3==4 or monthnum3==6 or monthnum3==9 or monthnum3==11):
return True
else:
return False | [
"assert check_monthnumber_number(6)==True",
"assert check_monthnumber_number(2)==False",
"assert check_monthnumber_number(12)==False"
] | [] |
|
663 | Write a function to find the largest possible value of k such that k modulo x is y. | import sys
def find_max_val(n, x, y):
ans = -sys.maxsize
for k in range(n + 1):
if (k % x == y):
ans = max(ans, k)
return (ans if (ans >= 0 and
ans <= n) else -1) | [
"assert find_max_val(15, 10, 5) == 15",
"assert find_max_val(187, 10, 5) == 185",
"assert find_max_val(16, 11, 1) == 12"
] | [] |
|
296 | Write a python function to count inversions in an array. | def get_Inv_Count(arr,n):
inv_count = 0
for i in range(n):
for j in range(i + 1,n):
if (arr[i] > arr[j]):
inv_count += 1
return inv_count | [
"assert get_Inv_Count([1,20,6,4,5],5) == 5",
"assert get_Inv_Count([1,2,1],3) == 1",
"assert get_Inv_Count([1,2,5,6,1],5) == 3"
] | [] |
|
727 | Write a function to remove all characters except letters and numbers using regex | import re
def remove_char(S):
result = re.sub('[\W_]+', '', S)
return result | [
"assert remove_char(\"123abcjw:, .@! eiw\") == '123abcjweiw'",
"assert remove_char(\"Hello1234:, ! Howare33u\") == 'Hello1234Howare33u'",
"assert remove_char(\"Cool543Triks@:, Make@987Trips\") == 'Cool543TriksMake987Trips' "
] | [] |
|
913 | Write a function to check for a number at the end of a string. | import re
def end_num(string):
text = re.compile(r".*[0-9]$")
if text.match(string):
return True
else:
return False | [
"assert end_num('abcdef')==False",
"assert end_num('abcdef7')==True",
"assert end_num('abc')==False"
] | [] |
|
748 | Write a function to put spaces between words starting with capital letters in a given string by using regex. | import re
def capital_words_spaces(str1):
return re.sub(r"(\w)([A-Z])", r"\1 \2", str1) | [
"assert capital_words_spaces(\"Python\") == 'Python'",
"assert capital_words_spaces(\"PythonProgrammingExamples\") == 'Python Programming Examples'",
"assert capital_words_spaces(\"GetReadyToBeCodingFreak\") == 'Get Ready To Be Coding Freak'"
] | [] |
|
562 | Write a python function to find the maximum length of sublist. | def Find_Max_Length(lst):
maxLength = max(len(x) for x in lst )
return maxLength | [
"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"
] | [] |
|
752 | Write a function to find the nth jacobsthal number. | def jacobsthal_num(n):
dp = [0] * (n + 1)
dp[0] = 0
dp[1] = 1
for i in range(2, n+1):
dp[i] = dp[i - 1] + 2 * dp[i - 2]
return dp[n] | [
"assert jacobsthal_num(5) == 11",
"assert jacobsthal_num(2) == 1",
"assert jacobsthal_num(4) == 5"
] | [] |
|
613 | Write a function to find the maximum value in record list as tuple attribute in the given tuple list. | def maximum_value(test_list):
res = [(key, max(lst)) for key, lst in test_list]
return (res) | [
"assert maximum_value([('key1', [3, 4, 5]), ('key2', [1, 4, 2]), ('key3', [9, 3])]) == [('key1', 5), ('key2', 4), ('key3', 9)]",
"assert maximum_value([('key1', [4, 5, 6]), ('key2', [2, 5, 3]), ('key3', [10, 4])]) == [('key1', 6), ('key2', 5), ('key3', 10)]",
"assert maximum_value([('key1', [5, 6, 7]), ('key2', [3, 6, 4]), ('key3', [11, 5])]) == [('key1', 7), ('key2', 6), ('key3', 11)]"
] | [] |
|
675 | Write a function to add two integers. however, if the sum is between the given range it will return 20. | def sum_nums(x, y,m,n):
sum_nums= x + y
if sum_nums in range(m, n):
return 20
else:
return sum_nums | [
"assert sum_nums(2,10,11,20)==20",
"assert sum_nums(15,17,1,10)==32",
"assert sum_nums(10,15,5,30)==20"
] | [] |
|
313 | Write a python function to print positive numbers in a list. | def pos_nos(list1):
for num in list1:
if num >= 0:
return num | [
"assert pos_nos([-1,-2,1,2]) == 1,2",
"assert pos_nos([3,4,-5]) == 3,4",
"assert pos_nos([-2,-3,1]) == 1"
] | [] |
|
832 | Write a function to extract the maximum numeric value from a string by using regex. | import re
def extract_max(input):
numbers = re.findall('\d+',input)
numbers = map(int,numbers)
return max(numbers) | [
"assert extract_max('100klh564abc365bg') == 564",
"assert extract_max('hello300how546mer231') == 546",
"assert extract_max('its233beenalong343journey234') == 343"
] | [] |
|
912 | Write a function to find ln, m lobb number. | def binomial_coeff(n, k):
C = [[0 for j in range(k + 1)]
for i in range(n + 1)]
for i in range(0, n + 1):
for j in range(0, min(i, k) + 1):
if (j == 0 or j == i):
C[i][j] = 1
else:
C[i][j] = (C[i - 1][j - 1]
+ C[i - 1][j])
return C[n][k]
def lobb_num(n, m):
return (((2 * m + 1) *
binomial_coeff(2 * n, m + n))
/ (m + n + 1)) | [
"assert int(lobb_num(5, 3)) == 35",
"assert int(lobb_num(3, 2)) == 5",
"assert int(lobb_num(4, 2)) == 20"
] | [] |
|
117 | Write a function to convert all possible convertible elements in the list to float. | 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 (str(res)) | [
"assert list_to_float( [(\"3\", \"4\"), (\"1\", \"26.45\"), (\"7.32\", \"8\"), (\"4\", \"8\")] ) == '[(3.0, 4.0), (1.0, 26.45), (7.32, 8.0), (4.0, 8.0)]'",
"assert list_to_float( [(\"4\", \"4\"), (\"2\", \"27\"), (\"4.12\", \"9\"), (\"7\", \"11\")] ) == '[(4.0, 4.0), (2.0, 27.0), (4.12, 9.0), (7.0, 11.0)]'",
"assert list_to_float( [(\"6\", \"78\"), (\"5\", \"26.45\"), (\"1.33\", \"4\"), (\"82\", \"13\")] ) == '[(6.0, 78.0), (5.0, 26.45), (1.33, 4.0), (82.0, 13.0)]'"
] | [] |
|
938 | Write a function to find three closest elements from three sorted arrays. | import sys
def find_closet(A, B, C, p, q, r):
diff = sys.maxsize
res_i = 0
res_j = 0
res_k = 0
i = 0
j = 0
k = 0
while(i < p and j < q and k < r):
minimum = min(A[i], min(B[j], C[k]))
maximum = max(A[i], max(B[j], C[k]));
if maximum-minimum < diff:
res_i = i
res_j = j
res_k = k
diff = maximum - minimum;
if diff == 0:
break
if A[i] == minimum:
i = i+1
elif B[j] == minimum:
j = j+1
else:
k = k+1
return A[res_i],B[res_j],C[res_k] | [
"assert find_closet([1, 4, 10],[2, 15, 20],[10, 12],3,3,2) == (10, 15, 10)",
"assert find_closet([20, 24, 100],[2, 19, 22, 79, 800],[10, 12, 23, 24, 119],3,5,5) == (24, 22, 23)",
"assert find_closet([2, 5, 11],[3, 16, 21],[11, 13],3,3,2) == (11, 16, 11)"
] | [] |
|
90 | 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 | [
"assert len_log([\"python\",\"PHP\",\"bigdata\"]) == 7",
"assert len_log([\"a\",\"ab\",\"abc\"]) == 3",
"assert len_log([\"small\",\"big\",\"tall\"]) == 5"
] | [] |
|
814 | Write a function to find the area of a rombus. | def rombus_area(p,q):
area=(p*q)/2
return area | [
"assert rombus_area(10,20)==100",
"assert rombus_area(10,5)==25",
"assert rombus_area(4,2)==4"
] | [] |
|
606 | Write a function to convert degrees to radians. | import math
def radian_degree(degree):
radian = degree*(math.pi/180)
return radian | [
"assert radian_degree(90)==1.5707963267948966",
"assert radian_degree(60)==1.0471975511965976",
"assert radian_degree(120)==2.0943951023931953"
] | [] |
|
543 | Write a function to add two numbers and print number of digits of sum. | def count_digits(num1,num2):
number=num1+num2
count = 0
while(number > 0):
number = number // 10
count = count + 1
return count | [
"assert count_digits(9875,10)==(4)",
"assert count_digits(98759853034,100)==(11)",
"assert count_digits(1234567,500)==(7)"
] | [] |
|
358 | Write a function to find modulo division of two lists using map and lambda function. | def moddiv_list(nums1,nums2):
result = map(lambda x, y: x % y, nums1, nums2)
return list(result) | [
"assert moddiv_list([4,5,6],[1, 2, 3])==[0, 1, 0]",
"assert moddiv_list([3,2],[1,4])==[0, 2]",
"assert moddiv_list([90,120],[50,70])==[40, 50]"
] | [] |
|
621 | Write a function to increment the numeric values in the given strings by k. | def increment_numerics(test_list, K):
res = [str(int(ele) + K) if ele.isdigit() else ele for ele in test_list]
return res | [
"assert increment_numerics([\"MSM\", \"234\", \"is\", \"98\", \"123\", \"best\", \"4\"] , 6) == ['MSM', '240', 'is', '104', '129', 'best', '10']",
"assert increment_numerics([\"Dart\", \"356\", \"is\", \"88\", \"169\", \"Super\", \"6\"] , 12) == ['Dart', '368', 'is', '100', '181', 'Super', '18']",
"assert increment_numerics([\"Flutter\", \"451\", \"is\", \"44\", \"96\", \"Magnificent\", \"12\"] , 33) == ['Flutter', '484', 'is', '77', '129', 'Magnificent', '45']"
] | [] |
|
900 | Write a function where a string will start with a specific number. | import re
def match_num(string):
text = re.compile(r"^5")
if text.match(string):
return True
else:
return False | [
"assert match_num('5-2345861')==True",
"assert match_num('6-2345861')==False",
"assert match_num('78910')==False"
] | [] |
End of preview. Expand
in Dataset Viewer.
README.md exists but content is empty.
- Downloads last month
- 37