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
179
Write a function to find if the given number is a keith number or not.
def is_num_keith(x): terms = [] temp = x n = 0 while (temp > 0): terms.append(temp % 10) temp = int(temp / 10) n+=1 terms.reverse() next_term = 0 i = n while (next_term < x): next_term = 0 for j in range(1,n+1): next_term += terms[i - j] terms.append(next_term) i+=1 return (next_term == x)
[ "assert is_num_keith(14) == True", "assert is_num_keith(12) == False", "assert is_num_keith(197) == True" ]
[]
224
Write a python function to count set bits of a given number.
def count_Set_Bits(n): count = 0 while (n): count += n & 1 n >>= 1 return count
[ "assert count_Set_Bits(2) == 1", "assert count_Set_Bits(4) == 1", "assert count_Set_Bits(6) == 2" ]
[]
612
Write a python function to merge the first and last elements separately in a list of lists.
def merge(lst): return [list(ele) for ele in list(zip(*lst))]
[ "assert merge([['x', 'y'], ['a', 'b'], ['m', 'n']]) == [['x', 'a', 'm'], ['y', 'b', 'n']]", "assert merge([[1, 2], [3, 4], [5, 6], [7, 8]]) == [[1, 3, 5, 7], [2, 4, 6, 8]]", "assert merge([['x', 'y','z' ], ['a', 'b','c'], ['m', 'n','o']]) == [['x', 'a', 'm'], ['y', 'b', 'n'],['z', 'c','o']]" ]
[]
623
Write a function to find the n-th power of individual elements in a list using lambda function.
def nth_nums(nums,n): nth_nums = list(map(lambda x: x ** n, nums)) return nth_nums
[ "assert nth_nums([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],2)==[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]", "assert nth_nums([10,20,30],3)==([1000, 8000, 27000])", "assert nth_nums([12,15],5)==([248832, 759375])" ]
[]
448
Write a function to calculate the sum of perrin numbers.
def cal_sum(n): a = 3 b = 0 c = 2 if (n == 0): return 3 if (n == 1): return 3 if (n == 2): return 5 sum = 5 while (n > 2): d = a + b sum = sum + d a = b b = c c = d n = n-1 return sum
[ "assert cal_sum(9) == 49", "assert cal_sum(10) == 66", "assert cal_sum(11) == 88" ]
[]
290
Write a function to find the list of lists with maximum length.
def max_length(list1): max_length = max(len(x) for x in list1 ) max_list = max((x) for x in list1) return(max_length, max_list)
[ "assert max_length([[0], [1, 3], [5, 7], [9, 11], [13, 15, 17]])==(3, [13, 15, 17])", "assert max_length([[1], [5, 7], [10, 12, 14,15]])==(4, [10, 12, 14,15])", "assert max_length([[5], [15,20,25]])==(3, [15,20,25])" ]
[]
86
Write a function to find nth centered hexagonal number.
def centered_hexagonal_number(n): return 3 * n * (n - 1) + 1
[ "assert centered_hexagonal_number(10) == 271", "assert centered_hexagonal_number(2) == 7", "assert centered_hexagonal_number(9) == 217" ]
[]
77
Write a python function to find the difference between sum of even and odd digits.
def is_Diff(n): return (n % 11 == 0)
[ "assert is_Diff (12345) == False", "assert is_Diff(1212112) == True", "assert is_Diff(1212) == False" ]
[]
181
Write a function to find the longest common prefix in the given set of strings.
def common_prefix_util(str1, str2): result = ""; n1 = len(str1) n2 = len(str2) i = 0 j = 0 while i <= n1 - 1 and j <= n2 - 1: if (str1[i] != str2[j]): break result += str1[i] i += 1 j += 1 return (result) def common_prefix (arr, n): prefix = arr[0] for i in range (1, n): prefix = common_prefix_util(prefix, arr[i]) return (prefix)
[ "assert common_prefix([\"tablets\", \"tables\", \"taxi\", \"tamarind\"], 4) == 'ta'", "assert common_prefix([\"apples\", \"ape\", \"april\"], 3) == 'ap'", "assert common_prefix([\"teens\", \"teenager\", \"teenmar\"], 3) == 'teen'" ]
[]
244
Write a python function to find the next perfect square greater than a given number.
import math def next_Perfect_Square(N): nextN = math.floor(math.sqrt(N)) + 1 return nextN * nextN
[ "assert next_Perfect_Square(35) == 36", "assert next_Perfect_Square(6) == 9", "assert next_Perfect_Square(9) == 16" ]
[]
96
Write a python function to find the number of divisors of a given integer.
def divisor(n): for i in range(n): x = len([i for i in range(1,n+1) if not n % i]) return x
[ "assert divisor(15) == 4 ", "assert divisor(12) == 6", "assert divisor(9) == 3" ]
[]
655
Write a python function to find the sum of fifth power of n natural numbers.
def fifth_Power_Sum(n) : sm = 0 for i in range(1,n+1) : sm = sm + (i*i*i*i*i) return sm
[ "assert fifth_Power_Sum(2) == 33", "assert fifth_Power_Sum(4) == 1300", "assert fifth_Power_Sum(3) == 276" ]
[]
385
Write a function to find the n'th perrin number using recursion.
def get_perrin(n): if (n == 0): return 3 if (n == 1): return 0 if (n == 2): return 2 return get_perrin(n - 2) + get_perrin(n - 3)
[ "assert get_perrin(9) == 12", "assert get_perrin(4) == 2", "assert get_perrin(6) == 5" ]
[]
389
Write a function to find the n'th lucas number.
def find_lucas(n): if (n == 0): return 2 if (n == 1): return 1 return find_lucas(n - 1) + find_lucas(n - 2)
[ "assert find_lucas(9) == 76", "assert find_lucas(4) == 7", "assert find_lucas(3) == 4" ]
[]
43
Write a function to find sequences of lowercase letters joined with an underscore using regex.
import re def text_match(text): patterns = '^[a-z]+_[a-z]+$' if re.search(patterns, text): return ('Found a match!') else: return ('Not matched!')
[ "assert text_match(\"aab_cbbbc\") == 'Found a match!'", "assert text_match(\"aab_Abbbc\") == 'Not matched!'", "assert text_match(\"Aaab_abbbc\") == 'Not matched!'" ]
[ "assert text_match(\"aab-cbbbc\") == 'Not matched!'" ]
442
Write a function to find the ration of positive numbers in an array of integers.
from array import array def positive_count(nums): n = len(nums) n1 = 0 for x in nums: if x > 0: n1 += 1 else: None return round(n1/n,2)
[ "assert positive_count([0, 1, 2, -1, -5, 6, 0, -3, -2, 3, 4, 6, 8])==0.54", "assert positive_count([2, 1, 2, -1, -5, 6, 4, -3, -2, 3, 4, 6, 8])==0.69", "assert positive_count([2, 4, -6, -9, 11, -12, 14, -5, 17])==0.56" ]
[]
137
Write a function to find the ration of zeroes in an array of integers.
from array import array def zero_count(nums): n = len(nums) n1 = 0 for x in nums: if x == 0: n1 += 1 else: None return round(n1/n,2)
[ "assert zero_count([0, 1, 2, -1, -5, 6, 0, -3, -2, 3, 4, 6, 8])==0.15", "assert zero_count([2, 1, 2, -1, -5, 6, 4, -3, -2, 3, 4, 6, 8])==0.00", "assert zero_count([2, 4, -6, -9, 11, -12, 14, -5, 17])==0.00" ]
[]
797
Write a python function to find the sum of all odd natural numbers within the range l and r.
def sum_Odd(n): terms = (n + 1)//2 sum1 = terms * terms return sum1 def sum_in_Range(l,r): return sum_Odd(r) - sum_Odd(l - 1)
[ "assert sum_in_Range(2,5) == 8", "assert sum_in_Range(5,7) == 12", "assert sum_in_Range(7,13) == 40" ]
[]
726
Write a function to multiply the adjacent elements of the given tuple.
def multiply_elements(test_tup): res = tuple(i * j for i, j in zip(test_tup, test_tup[1:])) return (res)
[ "assert multiply_elements((1, 5, 7, 8, 10)) == (5, 35, 56, 80)", "assert multiply_elements((2, 4, 5, 6, 7)) == (8, 20, 30, 42)", "assert multiply_elements((12, 13, 14, 9, 15)) == (156, 182, 126, 135)" ]
[]
349
Write a python function to check whether the given string is a binary string or not.
def check(string) : p = set(string) s = {'0', '1'} if s == p or p == {'0'} or p == {'1'}: return ("Yes") else : return ("No")
[ "assert check(\"01010101010\") == \"Yes\"", "assert check(\"name0\") == \"No\"", "assert check(\"101\") == \"Yes\"" ]
[]
658
Write a function to find the item with maximum occurrences in a given list.
def max_occurrences(list1): max_val = 0 result = list1[0] for i in list1: occu = list1.count(i) if occu > max_val: max_val = occu result = i return result
[ "assert max_occurrences([2,3,8,4,7,9,8,2,6,5,1,6,1,2,3,4,6,9,1,2])==2", "assert max_occurrences([1, 3,5, 7,1, 3,13, 15, 17,5, 7,9,1, 11])==1", "assert max_occurrences([1, 2, 3,2, 4, 5,1, 1, 1])==1" ]
[]
790
Write a python function to check whether every even index contains even numbers of a given list.
def even_position(nums): return all(nums[i]%2==i%2 for i in range(len(nums)))
[ "assert even_position([3,2,1]) == False", "assert even_position([1,2,3]) == False", "assert even_position([2,1,4]) == True" ]
[]
6
Write a python function to check whether the two numbers differ at one bit position only or not.
def is_Power_Of_Two (x): return x and (not(x & (x - 1))) def differ_At_One_Bit_Pos(a,b): return is_Power_Of_Two(a ^ b)
[ "assert differ_At_One_Bit_Pos(13,9) == True", "assert differ_At_One_Bit_Pos(15,8) == False", "assert differ_At_One_Bit_Pos(2,4) == False" ]
[]
565
Write a python function to split a string into characters.
def split(word): return [char for char in word]
[ "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']" ]
[]
585
Write a function to find the n - expensive price items from a given dataset using heap queue algorithm.
import heapq def expensive_items(items,n): expensive_items = heapq.nlargest(n, items, key=lambda s: s['price']) return expensive_items
[ "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},{'name': 'Item-2', 'price': 555.22}, {'name': 'Item-3', 'price': 45.09}],2)==[{'name': 'Item-2', 'price': 555.22},{'name': 'Item-1', 'price': 101.1}]", "assert expensive_items([{'name': 'Item-1', 'price': 101.1},{'name': 'Item-2', 'price': 555.22}, {'name': 'Item-3', 'price': 45.09},{'name': 'Item-4', 'price': 22.75}],1)==[{'name': 'Item-2', 'price': 555.22}]" ]
[]
561
Write a function to assign with each element, its pair elements from other similar pairs in the given tuple.
def assign_elements(test_list): res = dict() for key, val in test_list: res.setdefault(val, []) res.setdefault(key, []).append(val) return (res)
[ "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], 6: [4], 9: [4, 5], 8: [], 3: [8], 5: []}", "assert assign_elements([(6, 2), (6, 8), (4, 9), (4, 9), (3, 7)] ) == {2: [], 6: [2, 8], 8: [], 9: [], 4: [9, 9], 7: [], 3: [7]}" ]
[]
46
Write a python function to determine whether all the numbers are different from each other are not.
def test_distinct(data): if len(data) == len(set(data)): return True else: return False;
[ "assert test_distinct([1,5,7,9]) == True", "assert test_distinct([2,4,5,5,7,9]) == False", "assert test_distinct([1,2,3]) == True" ]
[]
765
Write a function to find nth polite number.
import math def is_polite(n): n = n + 1 return (int)(n+(math.log((n + math.log(n, 2)), 2)))
[ "assert is_polite(7) == 11", "assert is_polite(4) == 7", "assert is_polite(9) == 13" ]
[]
125
Write a function to find the maximum difference between the number of 0s and number of 1s in any sub-string of the given binary string.
def find_length(string, n): current_sum = 0 max_sum = 0 for i in range(n): current_sum += (1 if string[i] == '0' else -1) if current_sum < 0: current_sum = 0 max_sum = max(current_sum, max_sum) return max_sum if max_sum else 0
[ "assert find_length(\"11000010001\", 11) == 6", "assert find_length(\"10111\", 5) == 1", "assert find_length(\"11011101100101\", 14) == 2 " ]
[]
446
Write a python function to count the occurence of all elements of list in a tuple.
from collections import Counter def count_Occurrence(tup, lst): count = 0 for item in tup: if item in lst: count+= 1 return count
[ "assert count_Occurrence(('a', 'a', 'c', 'b', 'd'),['a', 'b'] ) == 3", "assert count_Occurrence((1, 2, 3, 1, 4, 6, 7, 1, 4),[1, 4, 7]) == 6", "assert count_Occurrence((1,2,3,4,5,6),[1,2]) == 2" ]
[]
321
Write a function to find the demlo number for the given number.
def find_demlo(s): l = len(s) res = "" for i in range(1,l+1): res = res + str(i) for i in range(l-1,0,-1): res = res + str(i) return res
[ "assert find_demlo(\"111111\") == '12345654321'", "assert find_demlo(\"1111\") == '1234321'", "assert find_demlo(\"13333122222\") == '123456789101110987654321'" ]
[]
571
Write a function to find maximum possible sum of disjoint pairs for the given array of integers and a number k.
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]
[ "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, 4, 5, 6], 6, 6) == 21" ]
[]
841
Write a function to count the number of inversions in the given 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([8, 4, 2, 1], 4) == 6", "assert get_inv_count([3, 1, 2], 3) == 2" ]
[]
520
Write a function to find the lcm of the given array elements.
def find_lcm(num1, num2): if(num1>num2): num = num1 den = num2 else: num = num2 den = num1 rem = num % den while (rem != 0): num = den den = rem rem = num % den gcd = den lcm = int(int(num1 * num2)/int(gcd)) return lcm def get_lcm(l): num1 = l[0] num2 = l[1] lcm = find_lcm(num1, num2) for i in range(2, len(l)): lcm = find_lcm(lcm, l[i]) return lcm
[ "assert get_lcm([2, 7, 3, 9, 4]) == 252", "assert get_lcm([1, 2, 8, 3]) == 24", "assert get_lcm([3, 8, 4, 10, 5]) == 120" ]
[]
11
Write a python function to remove first and last occurrence of a given character from the string.
def remove_Occ(s,ch): for i in range(len(s)): if (s[i] == ch): s = s[0 : i] + s[i + 1:] break for i in range(len(s) - 1,-1,-1): if (s[i] == ch): s = s[0 : i] + s[i + 1:] break return s
[ "assert remove_Occ(\"hello\",\"l\") == \"heo\"", "assert remove_Occ(\"abcda\",\"a\") == \"bcd\"", "assert remove_Occ(\"PHP\",\"P\") == \"H\"" ]
[ "assert remove_Occ(\"hellolloll\",\"l\") == \"helollol\"", "assert remove_Occ(\"\",\"l\") == \"\"" ]
476
Write a python function to find the sum of the largest and smallest value in a given array.
def big_sum(nums): sum= max(nums)+min(nums) return sum
[ "assert big_sum([1,2,3]) == 4", "assert big_sum([-1,2,3,4]) == 3", "assert big_sum([2,3,6]) == 8" ]
[]
828
Write a function to count alphabets,digits and special charactes in a given string.
def count_alpha_dig_spl(string): alphabets=digits = special = 0 for i in range(len(string)): if(string[i].isalpha()): alphabets = alphabets + 1 elif(string[i].isdigit()): digits = digits + 1 else: special = special + 1 return (alphabets,digits,special)
[ "assert count_alpha_dig_spl(\"abc!@#123\")==(3,3,3)", "assert count_alpha_dig_spl(\"dgsuy@#$%&1255\")==(5,4,5)", "assert count_alpha_dig_spl(\"fjdsif627348#%$^&\")==(6,6,5)" ]
[]
758
Write a function to count number of unique lists within a list.
def unique_sublists(list1): result ={} for l in list1: result.setdefault(tuple(l), list()).append(1) for a, b in result.items(): result[a] = sum(b) return result
[ "assert unique_sublists([[1, 3], [5, 7], [1, 3], [13, 15, 17], [5, 7], [9, 11]] )=={(1, 3): 2, (5, 7): 2, (13, 15, 17): 1, (9, 11): 1}", "assert unique_sublists([['green', 'orange'], ['black'], ['green', 'orange'], ['white']])=={('green', 'orange'): 2, ('black',): 1, ('white',): 1}", "assert unique_sublists([[10, 20, 30, 40], [60, 70, 50, 50], [90, 100, 200]])=={(10, 20, 30, 40): 1, (60, 70, 50, 50): 1, (90, 100, 200): 1}" ]
[]
240
Write a function to replace the last element of the list with another list.
def replace_list(list1,list2): list1[-1:] = list2 replace_list=list1 return replace_list
[ "assert replace_list([1, 3, 5, 7, 9, 10],[2, 4, 6, 8])==[1, 3, 5, 7, 9, 2, 4, 6, 8]", "assert replace_list([1,2,3,4,5],[5,6,7,8])==[1,2,3,4,5,6,7,8]", "assert replace_list([\"red\",\"blue\",\"green\"],[\"yellow\"])==[\"red\",\"blue\",\"yellow\"]" ]
[]
507
Write a function to remove specific words from a given list.
def remove_words(list1, removewords): for word in list(list1): if word in removewords: list1.remove(word) return list1
[ "assert remove_words(['red', 'green', 'blue', 'white', 'black', 'orange'],['white', 'orange'])==['red', 'green', 'blue', 'black']", "assert remove_words(['red', 'green', 'blue', 'white', 'black', 'orange'],['black', 'orange'])==['red', 'green', 'blue', 'white']", "assert remove_words(['red', 'green', 'blue', 'white', 'black', 'orange'],['blue', 'white'])==['red', 'green', 'black', 'orange']" ]
[]
139
Write a function to find the circumference of a circle.
def circle_circumference(r): perimeter=2*3.1415*r return perimeter
[ "assert circle_circumference(10)==62.830000000000005", "assert circle_circumference(5)==31.415000000000003", "assert circle_circumference(4)==25.132" ]
[]
905
Write a python function to find the sum of squares of binomial co-efficients.
def factorial(start,end): res = 1 for i in range(start,end + 1): res *= i return res def sum_of_square(n): return int(factorial(n + 1, 2 * n) /factorial(1, n))
[ "assert sum_of_square(4) == 70", "assert sum_of_square(5) == 252", "assert sum_of_square(2) == 6" ]
[]
85
Write a function to find the surface area of a sphere.
import math def surfacearea_sphere(r): surfacearea=4*math.pi*r*r return surfacearea
[ "assert surfacearea_sphere(10)==1256.6370614359173", "assert surfacearea_sphere(15)==2827.4333882308138", "assert surfacearea_sphere(20)==5026.548245743669" ]
[]
875
Write a function to find the minimum difference in the tuple pairs of given tuples.
def min_difference(test_list): temp = [abs(b - a) for a, b in test_list] res = min(temp) return (res)
[ "assert min_difference([(3, 5), (1, 7), (10, 3), (1, 2)]) == 1", "assert min_difference([(4, 6), (12, 8), (11, 4), (2, 13)]) == 2", "assert min_difference([(5, 17), (3, 9), (12, 5), (3, 24)]) == 6" ]
[]
410
Write a function to find the minimum value in a given heterogeneous list.
def min_val(listval): min_val = min(i for i in listval if isinstance(i, int)) return min_val
[ "assert min_val(['Python', 3, 2, 4, 5, 'version'])==2", "assert min_val(['Python', 15, 20, 25])==15", "assert min_val(['Python', 30, 20, 40, 50, 'version'])==20" ]
[]
705
Write a function to sort a list of lists by length and value.
def sort_sublists(list1): list1.sort() list1.sort(key=len) return list1
[ "assert sort_sublists([[2], [0], [1, 3], [0, 7], [9, 11], [13, 15, 17]])==[[0], [2], [0, 7], [1, 3], [9, 11], [13, 15, 17]]", "assert sort_sublists([[1], [2, 3], [4, 5, 6], [7], [10, 11]])==[[1], [7], [2, 3], [10, 11], [4, 5, 6]]", "assert sort_sublists([[\"python\"],[\"java\",\"C\",\"C++\"],[\"DBMS\"],[\"SQL\",\"HTML\"]])==[['DBMS'], ['python'], ['SQL', 'HTML'], ['java', 'C', 'C++']]" ]
[]
170
Write a function to find sum of the numbers in a list between the indices of a specified range.
def sum_range_list(list1, m, n): sum_range = 0 for i in range(m, n+1, 1): sum_range += list1[i] return sum_range
[ "assert sum_range_list( [2,1,5,6,8,3,4,9,10,11,8,12],8,10)==29", "assert sum_range_list( [2,1,5,6,8,3,4,9,10,11,8,12],5,7)==16", "assert sum_range_list( [2,1,5,6,8,3,4,9,10,11,8,12],7,10)==38" ]
[]
287
Write a python function to find the sum of squares of first n even natural numbers.
def square_Sum(n): return int(2*n*(n+1)*(2*n+1)/3)
[ "assert square_Sum(2) == 20", "assert square_Sum(3) == 56", "assert square_Sum(4) == 120" ]
[]
927
Write a function to calculate the height of the given binary tree.
class Node: def __init__(self, data): self.data = data self.left = None self.right = None def max_height(node): if node is None: return 0 ; else : left_height = max_height(node.left) right_height = max_height(node.right) if (left_height > right_height): return left_height+1 else: return right_height+1
[ "assert (max_height(root)) == 3", "assert (max_height(root1)) == 5 ", "assert (max_height(root2)) == 4" ]
root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root1 = Node(1); root1.left = Node(2); root1.right = Node(3); root1.left.left = Node(4); root1.right.left = Node(5); root1.right.right = Node(6); root1.right.right.right= Node(7); root1.right.right.right.right = Node(8) root2 = Node(1) root2.left = Node(2) root2.right = Node(3) root2.left.left = Node(4) root2.left.right = Node(5) root2.left.left.left = Node(6) root2.left.left.right = Node(7)
[]
345
Write a function to find the difference between two consecutive numbers in a given list.
def diff_consecutivenums(nums): result = [b-a for a, b in zip(nums[:-1], nums[1:])] return result
[ "assert diff_consecutivenums([1, 1, 3, 4, 4, 5, 6, 7])==[0, 2, 1, 0, 1, 1, 1]", "assert diff_consecutivenums([4, 5, 8, 9, 6, 10])==[1, 3, 1, -3, 4]", "assert diff_consecutivenums([0, 1, 2, 3, 4, 4, 4, 4, 5, 7])==[1, 1, 1, 1, 0, 0, 0, 1, 2]" ]
[]
642
Write a function to remove similar rows from the given tuple matrix.
def remove_similar_row(test_list): res = set(sorted([tuple(sorted(set(sub))) for sub in test_list])) return (res)
[ "assert remove_similar_row([[(4, 5), (3, 2)], [(2, 2), (4, 6)], [(3, 2), (4, 5)]] ) == {((2, 2), (4, 6)), ((3, 2), (4, 5))}", "assert remove_similar_row([[(5, 6), (4, 3)], [(3, 3), (5, 7)], [(4, 3), (5, 6)]] ) == {((4, 3), (5, 6)), ((3, 3), (5, 7))}", "assert remove_similar_row([[(6, 7), (5, 4)], [(4, 4), (6, 8)], [(5, 4), (6, 7)]] ) =={((4, 4), (6, 8)), ((5, 4), (6, 7))}" ]
[]
474
Write a function to replace characters in a string.
def replace_char(str1,ch,newch): str2 = str1.replace(ch, newch) return str2
[ "assert replace_char(\"polygon\",'y','l')==(\"pollgon\")", "assert replace_char(\"character\",'c','a')==(\"aharaater\")", "assert replace_char(\"python\",'l','a')==(\"python\")" ]
[]
903
Write a python function to count the total unset bits from 1 to n.
def count_Unset_Bits(n) : cnt = 0; for i in range(1,n + 1) : temp = i; while (temp) : if (temp % 2 == 0) : cnt += 1; temp = temp // 2; return cnt;
[ "assert count_Unset_Bits(2) == 1", "assert count_Unset_Bits(5) == 4", "assert count_Unset_Bits(14) == 17" ]
[]
147
Write a function to find the maximum total path sum in the given triangle.
def max_path_sum(tri, m, n): for i in range(m-1, -1, -1): for j in range(i+1): if (tri[i+1][j] > tri[i+1][j+1]): tri[i][j] += tri[i+1][j] else: tri[i][j] += tri[i+1][j+1] return tri[0][0]
[ "assert max_path_sum([[1, 0, 0], [4, 8, 0], [1, 5, 3]], 2, 2) == 14", "assert max_path_sum([[13, 0, 0], [7, 4, 0], [2, 4, 6]], 2, 2) == 24 ", "assert max_path_sum([[2, 0, 0], [11, 18, 0], [21, 25, 33]], 2, 2) == 53" ]
[]
34
Write a python function to find the missing number in a sorted array.
def find_missing(ar,N): l = 0 r = N - 1 while (l <= r): mid = (l + r) / 2 mid= int (mid) if (ar[mid] != mid + 1 and ar[mid - 1] == mid): return (mid + 1) elif (ar[mid] != mid + 1): r = mid - 1 else: l = mid + 1 return (-1)
[ "assert find_missing([1,2,3,5],4) == 4", "assert find_missing([1,3,4,5],4) == 2", "assert find_missing([1,2,3,5,6,7],5) == 4" ]
[]
664
Write a python function to find the average of even numbers till a given even number.
def average_Even(n) : if (n% 2!= 0) : return ("Invalid Input") return -1 sm = 0 count = 0 while (n>= 2) : count = count+1 sm = sm+n n = n-2 return sm // count
[ "assert average_Even(2) == 2", "assert average_Even(4) == 3", "assert average_Even(100) == 51" ]
[]
634
Write a python function to find the sum of fourth power of first n even natural numbers.
def even_Power_Sum(n): sum = 0; for i in range(1,n + 1): j = 2*i; sum = sum + (j*j*j*j); return sum;
[ "assert even_Power_Sum(2) == 272", "assert even_Power_Sum(3) == 1568", "assert even_Power_Sum(4) == 5664" ]
[]
430
Write a function to find the directrix of a parabola.
def parabola_directrix(a, b, c): directrix=((int)(c - ((b * b) + 1) * 4 * a )) return directrix
[ "assert parabola_directrix(5,3,2)==-198", "assert parabola_directrix(9,8,4)==-2336", "assert parabola_directrix(2,4,6)==-130" ]
[]
871
Write a python function to check whether the given strings are rotations of each other or not.
def are_Rotations(string1,string2): size1 = len(string1) size2 = len(string2) temp = '' if size1 != size2: return False temp = string1 + string1 if (temp.count(string2)> 0): return True else: return False
[ "assert are_Rotations(\"abc\",\"cba\") == False", "assert are_Rotations(\"abcd\",\"cdba\") == False", "assert are_Rotations(\"abacd\",\"cdaba\") == True" ]
[]
478
Write a function to remove lowercase substrings from a given string.
import re def remove_lowercase(str1): remove_lower = lambda text: re.sub('[a-z]', '', text) result = remove_lower(str1) return result
[ "assert remove_lowercase(\"PYTHon\")==('PYTH')", "assert remove_lowercase(\"FInD\")==('FID')", "assert remove_lowercase(\"STRinG\")==('STRG')" ]
[]
185
Write a function to find the focus of a parabola.
def parabola_focus(a, b, c): focus= (((-b / (2 * a)),(((4 * a * c) - (b * b) + 1) / (4 * a)))) return focus
[ "assert parabola_focus(5,3,2)==(-0.3, 1.6)", "assert parabola_focus(9,8,4)==(-0.4444444444444444, 2.25)", "assert parabola_focus(2,4,6)==(-1.0, 4.125)" ]
[]
645
Write a function to find the product of it’s kth index in the given tuples.
def get_product(val) : res = 1 for ele in val: res *= ele return res def find_k_product(test_list, K): res = get_product([sub[K] for sub in test_list]) return (res)
[ "assert find_k_product([(5, 6, 7), (1, 3, 5), (8, 9, 19)], 2) == 665", "assert find_k_product([(6, 7, 8), (2, 4, 6), (9, 10, 20)], 1) == 280", "assert find_k_product([(7, 8, 9), (3, 5, 7), (10, 11, 21)], 0) == 210" ]
[]
550
Write a python function to find the maximum element in a sorted and rotated array.
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] if (arr[low] > arr[mid]): return find_Max(arr,low,mid - 1) else: return find_Max(arr,mid + 1,high)
[ "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" ]
[]
118
[link text](https:// [link text](https:// [link text](https://)))write a function to convert a string to a list.
def string_to_list(string): lst = list(string.split(" ")) return lst
[ "assert string_to_list(\"python programming\")==['python','programming']", "assert string_to_list(\"lists tuples strings\")==['lists','tuples','strings']", "assert string_to_list(\"write a program\")==['write','a','program']" ]
[]
81
Write a function to zip the two given tuples.
def zip_tuples(test_tup1, test_tup2): res = [] for i, j in enumerate(test_tup1): res.append((j, test_tup2[i % len(test_tup2)])) return (res)
[ "assert zip_tuples((7, 8, 4, 5, 9, 10),(1, 5, 6) ) == [(7, 1), (8, 5), (4, 6), (5, 1), (9, 5), (10, 6)]", "assert zip_tuples((8, 9, 5, 6, 10, 11),(2, 6, 7) ) == [(8, 2), (9, 6), (5, 7), (6, 2), (10, 6), (11, 7)]", "assert zip_tuples((9, 10, 6, 7, 11, 12),(3, 7, 8) ) == [(9, 3), (10, 7), (6, 8), (7, 3), (11, 7), (12, 8)]" ]
[]
647
Write a function to split a string at uppercase letters.
import re def split_upperstring(text): return (re.findall('[A-Z][^A-Z]*', text))
[ "assert split_upperstring(\"PythonProgramLanguage\")==['Python','Program','Language']", "assert split_upperstring(\"PythonProgram\")==['Python','Program']", "assert split_upperstring(\"ProgrammingLanguage\")==['Programming','Language']" ]
[]
631
Write a function to replace whitespaces with an underscore and vice versa in a given string by using regex.
import re text = 'Python Exercises' def replace_spaces(text): text =text.replace (" ", "_") return (text) text =text.replace ("_", " ") return (text)
[ "assert replace_spaces('Jumanji The Jungle') == 'Jumanji_The_Jungle'", "assert replace_spaces('The Avengers') == 'The_Avengers'", "assert replace_spaces('Fast and Furious') == 'Fast_and_Furious'" ]
[]
280
Write a function to search an element in the given array by using sequential search.
def sequential_search(dlist, item): pos = 0 found = False while pos < len(dlist) and not found: if dlist[pos] == item: found = True else: pos = pos + 1 return found, pos
[ "assert sequential_search([11,23,58,31,56,77,43,12,65,19],31) == (True, 3)", "assert sequential_search([12, 32, 45, 62, 35, 47, 44, 61],61) == (True, 7)", "assert sequential_search([9, 10, 17, 19, 22, 39, 48, 56],48) == (True, 6)" ]
[]
682
Write a function to multiply two lists using map and lambda function.
def mul_list(nums1,nums2): result = map(lambda x, y: x * y, nums1, nums2) return list(result)
[ "assert mul_list([1, 2, 3],[4,5,6])==[4,10,18]", "assert mul_list([1,2],[3,4])==[3,8]", "assert mul_list([90,120],[50,70])==[4500,8400]" ]
[]
780
Write a function to find the combinations of sums with tuples in the given tuple list.
from itertools import combinations def find_combinations(test_list): res = [(b1 + a1, b2 + a2) for (a1, a2), (b1, b2) in combinations(test_list, 2)] return (res)
[ "assert find_combinations([(2, 4), (6, 7), (5, 1), (6, 10)]) == [(8, 11), (7, 5), (8, 14), (11, 8), (12, 17), (11, 11)]", "assert find_combinations([(3, 5), (7, 8), (6, 2), (7, 11)]) == [(10, 13), (9, 7), (10, 16), (13, 10), (14, 19), (13, 13)]", "assert find_combinations([(4, 6), (8, 9), (7, 3), (8, 12)]) == [(12, 15), (11, 9), (12, 18), (15, 12), (16, 21), (15, 15)]" ]
[]
320
Write a function to calculate the difference between the squared sum of first n natural numbers and the sum of squared first n natural numbers.
def sum_difference(n): sumofsquares = 0 squareofsum = 0 for num in range(1, n+1): sumofsquares += num * num squareofsum += num squareofsum = squareofsum ** 2 return squareofsum - sumofsquares
[ "assert sum_difference(12)==5434", "assert sum_difference(20)==41230", "assert sum_difference(54)==2151270" ]
[]
898
Write a function to extract specified number of elements from a given list, which follow each other continuously.
from itertools import groupby def extract_elements(numbers, n): result = [i for i, j in groupby(numbers) if len(list(j)) == n] return result
[ "assert extract_elements([1, 1, 3, 4, 4, 5, 6, 7],2)==[1, 4]", "assert extract_elements([0, 1, 2, 3, 4, 4, 4, 4, 5, 7],4)==[4]", "assert extract_elements([0,0,0,0,0],5)==[0]" ]
[]
305
Write a function to match two words from a list of words starting with letter 'p'.
import re def start_withp(words): for w in words: m = re.match("(P\w+)\W(P\w+)", w) if m: return m.groups()
[ "assert start_withp([\"Python PHP\", \"Java JavaScript\", \"c c++\"])==('Python', 'PHP')", "assert start_withp([\"Python Programming\",\"Java Programming\"])==('Python','Programming')", "assert start_withp([\"Pqrst Pqr\",\"qrstuv\"])==('Pqrst','Pqr')" ]
[]
350
Write a python function to minimize the length of the string by removing occurrence of only one character.
def minimum_Length(s) : maxOcc = 0 n = len(s) arr = [0]*26 for i in range(n) : arr[ord(s[i]) -ord('a')] += 1 for i in range(26) : if arr[i] > maxOcc : maxOcc = arr[i] return n - maxOcc
[ "assert minimum_Length(\"mnm\") == 1", "assert minimum_Length(\"abcda\") == 3", "assert minimum_Length(\"abcb\") == 2" ]
[]
279
Write a function to find the nth decagonal number.
def is_num_decagonal(n): return 4 * n * n - 3 * n
[ "assert is_num_decagonal(3) == 27", "assert is_num_decagonal(7) == 175", "assert is_num_decagonal(10) == 370" ]
[]
92
Write a function to check whether the given number is undulating or not.
def is_undulating(n): if (len(n) <= 2): return False for i in range(2, len(n)): if (n[i - 2] != n[i]): return False return True
[ "assert is_undulating(\"1212121\") == True", "assert is_undulating(\"1991\") == False", "assert is_undulating(\"121\") == True" ]
[]
278
Write a function to find the element count that occurs before the record in the given tuple.
def count_first_elements(test_tup): for count, ele in enumerate(test_tup): if isinstance(ele, tuple): break return (count)
[ "assert count_first_elements((1, 5, 7, (4, 6), 10) ) == 3", "assert count_first_elements((2, 9, (5, 7), 11) ) == 2", "assert count_first_elements((11, 15, 5, 8, (2, 3), 8) ) == 4" ]
[]
304
Write a python function to find element at a given index after number of rotations.
def find_Element(arr,ranges,rotations,index) : for i in range(rotations - 1,-1,-1 ) : left = ranges[i][0] right = ranges[i][1] if (left <= index and right >= index) : if (index == left) : index = right else : index = index - 1 return arr[index]
[ "assert find_Element([1,2,3,4,5],[[0,2],[0,3]],2,1) == 3", "assert find_Element([1,2,3,4],[[0,1],[0,2]],1,2) == 3", "assert find_Element([1,2,3,4,5,6],[[0,1],[0,2]],1,1) == 1" ]
[]
416
Write a function to find the maximum sum we can make by dividing number in three parts recursively and summing them up together for the given number.
MAX = 1000000 def breakSum(n): dp = [0]*(n+1) dp[0] = 0 dp[1] = 1 for i in range(2, n+1): dp[i] = max(dp[int(i/2)] + dp[int(i/3)] + dp[int(i/4)], i); return dp[n]
[ "assert breakSum(12) == 13", "assert breakSum(24) == 27", "assert breakSum(23) == 23" ]
[]
210
Write a function to check that the given string contains only a certain set of characters(in this case a-z, a-z and 0-9) by using regex.
import re def is_allowed_specific_char(string): get_char = re.compile(r'[^a-zA-Z0-9.]') string = get_char.search(string) return not bool(string)
[ "assert is_allowed_specific_char(\"ABCDEFabcdef123450\") == True", "assert is_allowed_specific_char(\"*&%@#!}{\") == False", "assert is_allowed_specific_char(\"HELLOhowareyou98765\") == True" ]
[]
417
Write a function to find common first element in given list of tuple.
def group_tuples(Input): out = {} for elem in Input: try: out[elem[0]].extend(elem[1:]) except KeyError: out[elem[0]] = list(elem) return [tuple(values) for values in out.values()]
[ "assert group_tuples([('x', 'y'), ('x', 'z'), ('w', 't')]) == [('x', 'y', 'z'), ('w', 't')]", "assert group_tuples([('a', 'b'), ('a', 'c'), ('d', 'e')]) == [('a', 'b', 'c'), ('d', 'e')]", "assert group_tuples([('f', 'g'), ('f', 'g'), ('h', 'i')]) == [('f', 'g', 'g'), ('h', 'i')]" ]
[]
530
Write a function to find the ration of negative numbers in an array of integers.
from array import array def negative_count(nums): n = len(nums) n1 = 0 for x in nums: if x < 0: n1 += 1 else: None return round(n1/n,2)
[ "assert negative_count([0, 1, 2, -1, -5, 6, 0, -3, -2, 3, 4, 6, 8])==0.31", "assert negative_count([2, 1, 2, -1, -5, 6, 4, -3, -2, 3, 4, 6, 8])==0.31", "assert negative_count([2, 4, -6, -9, 11, -12, 14, -5, 17])==0.44" ]
[]
745
Write a function to find numbers within a given range where every number is divisible by every digit it contains.
def divisible_by_digits(startnum, endnum): return [n for n in range(startnum, endnum+1) \ if not any(map(lambda x: int(x) == 0 or n%int(x) != 0, str(n)))]
[ "assert divisible_by_digits(1,22)==[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]", "assert divisible_by_digits(1,15)==[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]", "assert divisible_by_digits(20,25)==[22, 24]" ]
[]
70
Write a function to find whether all the given tuples have equal length or not.
def find_equal_tuple(Input, k): flag = 1 for tuple in Input: if len(tuple) != k: flag = 0 break return flag def get_equal(Input, k): if find_equal_tuple(Input, k) == 1: return ("All tuples have same length") else: return ("All tuples do not have same length")
[ "assert get_equal([(11, 22, 33), (44, 55, 66)], 3) == 'All tuples have same length'", "assert get_equal([(1, 2, 3), (4, 5, 6, 7)], 3) == 'All tuples do not have same length'", "assert get_equal([(1, 2), (3, 4)], 2) == 'All tuples have same length'" ]
[]
145
Write a python function to find the maximum difference between any two elements in a given array.
def max_Abs_Diff(arr,n): minEle = arr[0] maxEle = arr[0] for i in range(1, n): minEle = min(minEle,arr[i]) maxEle = max(maxEle,arr[i]) return (maxEle - minEle)
[ "assert max_Abs_Diff((2,1,5,3),4) == 4", "assert max_Abs_Diff((9,3,2,5,1),5) == 8", "assert max_Abs_Diff((3,2,1),3) == 2" ]
[]
267
Write a python function to find the sum of squares of first n odd natural numbers.
def square_Sum(n): return int(n*(4*n*n-1)/3)
[ "assert square_Sum(2) == 10", "assert square_Sum(3) == 35", "assert square_Sum(4) == 84" ]
[]
219
Write a function to extract maximum and minimum k elements in the given tuple.
def extract_min_max(test_tup, K): res = [] test_tup = list(test_tup) temp = sorted(test_tup) for idx, val in enumerate(temp): if idx < K or idx >= len(temp) - K: res.append(val) res = tuple(res) return (res)
[ "assert extract_min_max((5, 20, 3, 7, 6, 8), 2) == (3, 5, 8, 20)", "assert extract_min_max((4, 5, 6, 1, 2, 7), 3) == (1, 2, 4, 5, 6, 7)", "assert extract_min_max((2, 3, 4, 8, 9, 11, 7), 4) == (2, 3, 4, 7, 8, 9, 11)" ]
[]
68
Write a python function to check whether the given array is monotonic or not.
def is_Monotonic(A): return (all(A[i] <= A[i + 1] for i in range(len(A) - 1)) or all(A[i] >= A[i + 1] for i in range(len(A) - 1)))
[ "assert is_Monotonic([6, 5, 4, 4]) == True", "assert is_Monotonic([1, 2, 2, 3]) == True", "assert is_Monotonic([1, 3, 2]) == False" ]
[]
570
Write a function to remove words from a given list of strings containing a character or string.
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
[ "assert remove_words(['Red color', 'Orange#', 'Green', 'Orange @', \"White\"],['#', 'color', '@'])==['Red', '', 'Green', 'Orange', 'White']", "assert remove_words(['Red &', 'Orange+', 'Green', 'Orange @', 'White'],['&', '+', '@'])==['Red', '', 'Green', 'Orange', 'White']", "assert remove_words(['Red &', 'Orange+', 'Green', 'Orange @', 'White'],['@'])==['Red &', 'Orange+', 'Green', 'Orange', 'White']" ]
[]
469
Write a function to find the maximum profit earned from a maximum of k stock transactions
def max_profit(price, k): n = len(price) final_profit = [[None for x in range(n)] for y in range(k + 1)] for i in range(k + 1): for j in range(n): if i == 0 or j == 0: final_profit[i][j] = 0 else: max_so_far = 0 for x in range(j): curr_price = price[j] - price[x] + final_profit[i-1][x] if max_so_far < curr_price: max_so_far = curr_price final_profit[i][j] = max(final_profit[i][j-1], max_so_far) return final_profit[k][n-1]
[ "assert max_profit([1, 5, 2, 3, 7, 6, 4, 5], 3) == 10", "assert max_profit([2, 4, 7, 5, 4, 3, 5], 2) == 7", "assert max_profit([10, 6, 8, 4, 2], 2) == 2" ]
[]
228
Write a python function to check whether all the bits are unset in the given range or not.
def all_Bits_Set_In_The_Given_Range(n,l,r): num = (((1 << r) - 1) ^ ((1 << (l - 1)) - 1)) new_num = n & num if (new_num == 0): return True return False
[ "assert all_Bits_Set_In_The_Given_Range(4,1,2) == True", "assert all_Bits_Set_In_The_Given_Range(17,2,4) == True", "assert all_Bits_Set_In_The_Given_Range(39,4,6) == False" ]
[]
627
Write a python function to find the smallest missing number from the given array.
def find_First_Missing(array,start,end): if (start > end): return end + 1 if (start != array[start]): return start; mid = int((start + end) / 2) if (array[mid] == mid): return find_First_Missing(array,mid+1,end) return find_First_Missing(array,start,mid)
[ "assert find_First_Missing([0,1,2,3],0,3) == 4", "assert find_First_Missing([0,1,2,6,9],0,4) == 3", "assert find_First_Missing([2,3,5,8,9],0,4) == 0" ]
[]
734
Write a python function to find sum of products of all possible subarrays.
def sum_Of_Subarray_Prod(arr,n): ans = 0 res = 0 i = n - 1 while (i >= 0): incr = arr[i]*(1 + res) ans += incr res = incr i -= 1 return (ans)
[ "assert sum_Of_Subarray_Prod([1,2,3],3) == 20", "assert sum_Of_Subarray_Prod([1,2],2) == 5", "assert sum_Of_Subarray_Prod([1,2,3,4],4) == 84" ]
[]
454
Write a function that matches a word containing 'z'.
import re def text_match_wordz(text): patterns = '\w*z.\w*' if re.search(patterns, text): return 'Found a match!' else: return('Not matched!')
[ "assert text_match_wordz(\"pythonz.\")==('Found a match!')", "assert text_match_wordz(\"xyz.\")==('Found a match!')", "assert text_match_wordz(\" lang .\")==('Not matched!')" ]
[]
496
Write a function to find the smallest integers from a given list of numbers using heap queue algorithm.
import heapq as hq def heap_queue_smallest(nums,n): smallest_nums = hq.nsmallest(n, nums) return smallest_nums
[ "assert heap_queue_smallest( [25, 35, 22, 85, 14, 65, 75, 25, 58],3)==[14, 22, 25] ", "assert heap_queue_smallest( [25, 35, 22, 85, 14, 65, 75, 25, 58],2)==[14, 22]", "assert heap_queue_smallest( [25, 35, 22, 85, 14, 65, 75, 22, 58],5)==[14, 22, 22, 25, 35]" ]
[]
38
Write a function to find the division of first even and odd number of a given list.
def div_even_odd(list1): first_even = next((el for el in list1 if el%2==0),-1) first_odd = next((el for el in list1 if el%2!=0),-1) return (first_even/first_odd)
[ "assert div_even_odd([1,3,5,7,4,1,6,8])==4", "assert div_even_odd([1,2,3,4,5,6,7,8,9,10])==2", "assert div_even_odd([1,5,7,9,10])==10" ]
[]
731
Write a function to find the lateral surface area of a cone.
import math def lateralsurface_cone(r,h): l = math.sqrt(r * r + h * h) LSA = math.pi * r * l return LSA
[ "assert lateralsurface_cone(5,12)==204.20352248333654", "assert lateralsurface_cone(10,15)==566.3586699569488", "assert lateralsurface_cone(19,17)==1521.8090132193388" ]
[]
624
Write a python function to convert the given string to upper case.
def is_upper(string): return (string.upper())
[ "assert is_upper(\"person\") ==\"PERSON\"", "assert is_upper(\"final\") == \"FINAL\"", "assert is_upper(\"Valid\") == \"VALID\"" ]
[]
563
Write a function to extract values between quotation marks of a string.
import re def extract_values(text): return (re.findall(r'"(.*?)"', text))
[ "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','blue','green','yellow']" ]
[]
933
Write a function to convert camel case string to snake case string by using regex.
import re def camel_to_snake(text): str1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text) return re.sub('([a-z0-9])([A-Z])', r'\1_\2', str1).lower()
[ "assert camel_to_snake('GoogleAssistant') == 'google_assistant'", "assert camel_to_snake('ChromeCast') == 'chrome_cast'", "assert camel_to_snake('QuadCore') == 'quad_core'" ]
[]