Dataset Viewer
source_file
string | task_id
int64 | prompt
string | code
string | test_imports
sequence | test_list
sequence |
---|---|---|---|---|---|
Benchmark Questions Verification V2.ipynb
| 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\""
] |
Benchmark Questions Verification V2.ipynb
| 12 |
Write a function to sort a given matrix in ascending order according to the sum of its rows.
|
def sort_matrix(M):
result = sorted(M, key=sum)
return result
|
[] |
[
"assert sort_matrix([[1, 2, 3], [2, 4, 5], [1, 1, 1]])==[[1, 1, 1], [1, 2, 3], [2, 4, 5]]",
"assert sort_matrix([[1, 2, 3], [-2, 4, -5], [1, -1, 1]])==[[-2, 4, -5], [1, -1, 1], [1, 2, 3]]",
"assert sort_matrix([[5,8,9],[6,4,3],[2,1,4]])==[[2, 1, 4], [6, 4, 3], [5, 8, 9]]"
] |
Benchmark Questions Verification V2.ipynb
| 16 |
Write a function to that returns true if the input string contains sequences of lowercase letters joined with an underscore and false otherwise.
|
import re
def text_lowercase_underscore(text):
patterns = '^[a-z]+_[a-z]+$'
if re.search(patterns, text):
return True
else:
return False
|
[] |
[
"assert text_lowercase_underscore(\"aab_cbbbc\")==(True)",
"assert text_lowercase_underscore(\"aab_Abbbc\")==(False)",
"assert text_lowercase_underscore(\"Aaab_abbbc\")==(False)"
] |
Benchmark Questions Verification V2.ipynb
| 18 |
Write a function to remove characters from the first string which are present in the second string.
|
NO_OF_CHARS = 256
def str_to_list(string):
temp = []
for x in string:
temp.append(x)
return temp
def lst_to_string(List):
return ''.join(List)
def get_char_count_array(string):
count = [0] * NO_OF_CHARS
for i in string:
count[ord(i)] += 1
return count
def remove_dirty_chars(string, second_string):
count = get_char_count_array(second_string)
ip_ind = 0
res_ind = 0
temp = ''
str_list = str_to_list(string)
while ip_ind != len(str_list):
temp = str_list[ip_ind]
if count[ord(temp)] == 0:
str_list[res_ind] = str_list[ip_ind]
res_ind += 1
ip_ind+=1
return lst_to_string(str_list[0:res_ind])
|
[] |
[
"assert remove_dirty_chars(\"probasscurve\", \"pros\") == 'bacuve'",
"assert remove_dirty_chars(\"digitalindia\", \"talent\") == 'digiidi'",
"assert remove_dirty_chars(\"exoticmiles\", \"toxic\") == 'emles'"
] |
Benchmark Questions Verification V2.ipynb
| 19 |
Write a function to find whether a given array of integers contains any duplicate element.
|
def test_duplicate(arraynums):
nums_set = set(arraynums)
return len(arraynums) != len(nums_set)
|
[] |
[
"assert test_duplicate(([1,2,3,4,5]))==False",
"assert test_duplicate(([1,2,3,4, 4]))==True",
"assert test_duplicate([1,1,2,2,3,3,4,4,5])==True"
] |
Benchmark Questions Verification V2.ipynb
| 20 |
Write a function to check if the given number is woodball or not.
|
def is_woodall(x):
if (x % 2 == 0):
return False
if (x == 1):
return True
x = x + 1
p = 0
while (x % 2 == 0):
x = x/2
p = p + 1
if (p == x):
return True
return False
|
[] |
[
"assert is_woodall(383) == True",
"assert is_woodall(254) == False",
"assert is_woodall(200) == False"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 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"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 58 |
Write a python function to check whether the given two integers have opposite sign or not.
|
def opposite_Signs(x,y):
return ((x ^ y) < 0);
|
[] |
[
"assert opposite_Signs(1,-2) == True",
"assert opposite_Signs(3,2) == False",
"assert opposite_Signs(-10,-10) == False",
"assert opposite_Signs(-2,2) == True"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 59 |
Write a function to find the nth octagonal number.
|
def is_octagonal(n):
return 3 * n * n - 2 * n
|
[] |
[
"assert is_octagonal(5) == 65",
"assert is_octagonal(10) == 280",
"assert is_octagonal(15) == 645"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 61 |
Write a python function to count the number of substrings with the sum of digits equal to their length.
|
from collections import defaultdict
def count_Substrings(s):
n = len(s)
count,sum = 0,0
mp = defaultdict(lambda : 0)
mp[0] += 1
for i in range(n):
sum += ord(s[i]) - ord('0')
count += mp[sum - (i + 1)]
mp[sum - (i + 1)] += 1
return count
|
[] |
[
"assert count_Substrings('112112') == 6",
"assert count_Substrings('111') == 6",
"assert count_Substrings('1101112') == 12"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 65 |
Write a function to flatten a list and sum all of its elements.
|
def recursive_list_sum(data_list):
total = 0
for element in data_list:
if type(element) == type([]):
total = total + recursive_list_sum(element)
else:
total = total + element
return total
|
[] |
[
"assert recursive_list_sum(([1, 2, [3,4],[5,6]]))==21",
"assert recursive_list_sum(([7, 10, [15,14],[19,41]]))==106",
"assert recursive_list_sum(([10, 20, [30,40],[50,60]]))==210"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 67 |
Write a function to find the number of ways to partition a set of Bell numbers.
|
def bell_number(n):
bell = [[0 for i in range(n+1)] for j in range(n+1)]
bell[0][0] = 1
for i in range(1, n+1):
bell[i][0] = bell[i-1][i-1]
for j in range(1, i+1):
bell[i][j] = bell[i-1][j-1] + bell[i][j-1]
return bell[n][0]
|
[] |
[
"assert bell_number(2)==2",
"assert bell_number(10)==115975",
"assert bell_number(56)==6775685320645824322581483068371419745979053216268760300"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 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"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 69 |
Write a function to check whether a list contains the given sublist or not.
|
def is_sublist(l, s):
sub_set = False
if s == []:
sub_set = True
elif s == l:
sub_set = True
elif len(s) > len(l):
sub_set = False
else:
for i in range(len(l)):
if l[i] == s[0]:
n = 1
while (n < len(s)) and (l[i+n] == s[n]):
n += 1
if n == len(s):
sub_set = True
return sub_set
|
[] |
[
"assert is_sublist([2,4,3,5,7],[3,7])==False",
"assert is_sublist([2,4,3,5,7],[4,3])==True",
"assert is_sublist([2,4,3,5,7],[1,6])==False"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 71 |
Write a function to sort a list of elements.
|
def comb_sort(nums):
shrink_fact = 1.3
gaps = len(nums)
swapped = True
i = 0
while gaps > 1 or swapped:
gaps = int(float(gaps) / shrink_fact)
swapped = False
i = 0
while gaps + i < len(nums):
if nums[i] > nums[i+gaps]:
nums[i], nums[i+gaps] = nums[i+gaps], nums[i]
swapped = True
i += 1
return nums
|
[] |
[
"assert comb_sort([5, 15, 37, 25, 79]) == [5, 15, 25, 37, 79]",
"assert comb_sort([41, 32, 15, 19, 22]) == [15, 19, 22, 32, 41]",
"assert comb_sort([99, 15, 13, 47]) == [13, 15, 47, 99]"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 72 |
Write a python function to check whether the given number can be represented as the 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"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 74 |
Write a function to check whether it follows the sequence given in the patterns array.
|
def is_samepatterns(colors, patterns):
if len(colors) != len(patterns):
return False
sdict = {}
pset = set()
sset = set()
for i in range(len(patterns)):
pset.add(patterns[i])
sset.add(colors[i])
if patterns[i] not in sdict.keys():
sdict[patterns[i]] = []
keys = sdict[patterns[i]]
keys.append(colors[i])
sdict[patterns[i]] = keys
if len(pset) != len(sset):
return False
for values in sdict.values():
for i in range(len(values) - 1):
if values[i] != values[i+1]:
return False
return True
|
[] |
[
"assert is_samepatterns([\"red\",\"green\",\"green\"], [\"a\", \"b\", \"b\"])==True",
"assert is_samepatterns([\"red\",\"green\",\"greenn\"], [\"a\",\"b\",\"b\"])==False",
"assert is_samepatterns([\"red\",\"green\",\"greenn\"], [\"a\",\"b\"])==False"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 77 |
Write a python function to find whether a number is divisible by 11.
|
def is_Diff(n):
return (n % 11 == 0)
|
[] |
[
"assert is_Diff (12345) == False",
"assert is_Diff(1212112) == True",
"assert is_Diff(1212) == False"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 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",
"assert tetrahedral_number(6) == 56",
"assert tetrahedral_number(7) == 84"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 83 |
Write a python function to find the character made by adding the ASCII value of all the characters of the given string modulo 26.
|
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\""
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 84 |
Write a function to find the nth number in the newman conway sequence.
|
def sequence(n):
if n == 1 or n == 2:
return 1
else:
return sequence(sequence(n-1)) + sequence(n-sequence(n-1))
|
[] |
[
"assert sequence(10) == 6",
"assert sequence(2) == 1",
"assert sequence(3) == 2"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 87 |
Write a function to merge three dictionaries into a single dictionary.
|
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'}"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 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"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 92 |
Write a function to check whether the given number is undulating or not.
|
def is_undulating(n):
n = str(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"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 93 |
Write a function to calculate the value of 'a' to the power 'b'.
|
def power(a,b):
if b==0:
return 1
elif a==0:
return 0
elif b==1:
return a
else:
return a*power(a,b-1)
|
[] |
[
"assert power(3,4) == 81",
"assert power(2,3) == 8",
"assert power(5,5) == 3125"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 95 |
Write a python function to find the length of the smallest list in a list of lists.
|
def Find_Min_Length(lst):
minLength = min(len(x) for x in lst )
return minLength
|
[] |
[
"assert Find_Min_Length([[1],[1,2]]) == 1",
"assert Find_Min_Length([[1,2],[1,2,3],[1,2,3,4]]) == 2",
"assert Find_Min_Length([[3,3,3],[4,4,4,4]]) == 3"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 97 |
Write a function to find frequency of each element in a flattened list of lists, returned in a dictionary.
|
def frequency_lists(list1):
list1 = [item for sublist in list1 for item in sublist]
dic_data = {}
for num in list1:
if num in dic_data.keys():
dic_data[num] += 1
else:
key = num
value = 1
dic_data[key] = value
return dic_data
|
[] |
[
"assert frequency_lists([[1, 2, 3, 2], [4, 5, 6, 2], [7, 8, 9, 5]])=={1: 1, 2: 3, 3: 1, 4: 1, 5: 2, 6: 1, 7: 1, 8: 1, 9: 1}",
"assert frequency_lists([[1,2,3,4],[5,6,7,8],[9,10,11,12]])=={1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1,10:1,11:1,12:1}",
"assert frequency_lists([[20,30,40,17],[18,16,14,13],[10,20,30,40]])=={20:2,30:2,40:2,17: 1,18:1, 16: 1,14: 1,13: 1, 10: 1}"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 98 |
Write a function to multiply all the numbers in a list and divide with the length of the list.
|
def multiply_num(numbers):
total = 1
for x in numbers:
total *= x
return total/len(numbers)
|
[
"import math"
] |
[
"assert math.isclose(multiply_num((8, 2, 3, -1, 7)), -67.2, rel_tol=0.001)",
"assert math.isclose(multiply_num((-10,-20,-30)), -2000.0, rel_tol=0.001)",
"assert math.isclose(multiply_num((19,15,18)), 1710.0, rel_tol=0.001)"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 99 |
Write a function to convert the given decimal number to its binary equivalent, represented as a string with no leading zeros.
|
def decimal_to_binary(n):
return bin(n).replace("0b","")
|
[] |
[
"assert decimal_to_binary(8) == '1000'",
"assert decimal_to_binary(18) == '10010'",
"assert decimal_to_binary(7) == '111'"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 100 |
Write a function to find the next smallest palindrome of a specified integer, returned as an integer.
|
import sys
def next_smallest_palindrome(num):
numstr = str(num)
for i in range(num+1,sys.maxsize):
if str(i) == str(i)[::-1]:
return i
|
[] |
[
"assert next_smallest_palindrome(99)==101",
"assert next_smallest_palindrome(1221)==1331",
"assert next_smallest_palindrome(120)==121"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 102 |
Write a function to convert a snake case string to camel case string.
|
def snake_to_camel(word):
import re
return ''.join(x.capitalize() or '_' for x in word.split('_'))
|
[] |
[
"assert snake_to_camel('python_program')=='PythonProgram'",
"assert snake_to_camel('python_language')==('PythonLanguage')",
"assert snake_to_camel('programming_language')==('ProgrammingLanguage')"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 103 |
Write a function to find the Eulerian number a(n, m).
|
def eulerian_num(n, m):
if (m >= n or n == 0):
return 0
if (m == 0):
return 1
return ((n - m) * eulerian_num(n - 1, m - 1) +(m + 1) * eulerian_num(n - 1, m))
|
[] |
[
"assert eulerian_num(3, 1) == 4",
"assert eulerian_num(4, 1) == 11",
"assert eulerian_num(5, 3) == 26"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 104 |
Write a function to sort each sublist of strings in a given list of lists.
|
def sort_sublists(input_list):
result = [sorted(x, key = lambda x:x[0]) for x in input_list]
return result
|
[] |
[
"assert sort_sublists(([\"green\", \"orange\"], [\"black\", \"white\"], [\"white\", \"black\", \"orange\"]))==[['green', 'orange'], ['black', 'white'], ['black', 'orange', 'white']]",
"assert sort_sublists(([\" red \",\"green\" ],[\"blue \",\" black\"],[\" orange\",\"brown\"]))==[[' red ', 'green'], [' black', 'blue '], [' orange', 'brown']]",
"assert sort_sublists(([\"zilver\",\"gold\"], [\"magnesium\",\"aluminium\"], [\"steel\", \"bronze\"]))==[['gold', 'zilver'],['aluminium', 'magnesium'], ['bronze', 'steel']]"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 105 |
Write a python function to count true booleans in the given list.
|
def count(lst):
return sum(lst)
|
[] |
[
"assert count([True,False,True]) == 2",
"assert count([False,False]) == 0",
"assert count([True,True,True]) == 3"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 109 |
Write a python function to find the number of numbers with an odd value when rotating a binary string the given number of times.
|
def odd_Equivalent(s,n):
count=0
for i in range(0,n):
if (s[i] == '1'):
count = count + 1
return count
|
[] |
[
"assert odd_Equivalent(\"011001\",6) == 3",
"assert odd_Equivalent(\"11011\",5) == 4",
"assert odd_Equivalent(\"1010\",4) == 2"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 111 |
Write a function to find the common elements in given nested lists.
|
def common_in_nested_lists(nestedlist):
result = list(set.intersection(*map(set, nestedlist)))
return result
|
[] |
[
"assert set(common_in_nested_lists([[12, 18, 23, 25, 45], [7, 12, 18, 24, 28], [1, 5, 8, 12, 15, 16, 18]]))==set([18, 12])",
"assert set(common_in_nested_lists([[12, 5, 23, 25, 45], [7, 11, 5, 23, 28], [1, 5, 8, 18, 23, 16]]))==set([5,23])",
"assert set(common_in_nested_lists([[2, 3,4, 1], [4, 5], [6,4, 8],[4, 5], [6, 8,4]]))==set([4])"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 116 |
Write a function to convert a given tuple of positive integers into a single integer.
|
def tuple_to_int(nums):
result = int(''.join(map(str,nums)))
return result
|
[] |
[
"assert tuple_to_int((1,2,3))==123",
"assert tuple_to_int((4,5,6))==456",
"assert tuple_to_int((5,6,7))==567"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 118 |
Write a function to convert a string to a list of strings split on the space character.
|
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']"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 120 |
Write a function to find the maximum absolute product between numbers in pairs of tuples within a given list.
|
def max_product_tuple(list1):
result_max = max([abs(x * y) for x, y in list1] )
return result_max
|
[] |
[
"assert max_product_tuple([(2, 7), (2, 6), (1, 8), (4, 9)] )==36",
"assert max_product_tuple([(10,20), (15,2), (5,10)] )==200",
"assert max_product_tuple([(11,44), (10,15), (20,5), (12, 9)] )==484"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 123 |
Write a function to sum all amicable numbers from 1 to a specified number.
|
def amicable_numbers_sum(limit):
if not isinstance(limit, int):
return "Input is not an integer!"
if limit < 1:
return "Input must be bigger than 0!"
amicables = set()
for num in range(2, limit+1):
if num in amicables:
continue
sum_fact = sum([fact for fact in range(1, num) if num % fact == 0])
sum_fact2 = sum([fact for fact in range(1, sum_fact) if sum_fact % fact == 0])
if num == sum_fact2 and num != sum_fact:
amicables.add(num)
amicables.add(sum_fact2)
return sum(amicables)
|
[] |
[
"assert amicable_numbers_sum(999)==504",
"assert amicable_numbers_sum(9999)==31626",
"assert amicable_numbers_sum(99)==0"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 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 = len(string)
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\") == 6",
"assert find_length(\"10111\") == 1",
"assert find_length(\"11011101100101\") == 2"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 126 |
Write a python function to find the sum of common divisors of two given numbers.
|
def sum(a,b):
sum = 0
for i in range (1,min(a,b)):
if (a % i == 0 and b % i == 0):
sum += i
return sum
|
[] |
[
"assert sum(10,15) == 6",
"assert sum(100,150) == 93",
"assert sum(4,6) == 3"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 127 |
Write a function to multiply two integers.
|
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"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 128 |
Write a function to find words that are longer than n characters 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']"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 129 |
Write a function to calculate whether the matrix is a magic square.
|
def magic_square_test(my_matrix):
iSize = len(my_matrix[0])
sum_list = []
sum_list.extend([sum (lines) for lines in my_matrix])
for col in range(iSize):
sum_list.append(sum(row[col] for row in my_matrix))
result1 = 0
for i in range(0,iSize):
result1 +=my_matrix[i][i]
sum_list.append(result1)
result2 = 0
for i in range(iSize-1,-1,-1):
result2 +=my_matrix[i][i]
sum_list.append(result2)
if len(set(sum_list))>1:
return False
return True
|
[] |
[
"assert magic_square_test([[7, 12, 1, 14], [2, 13, 8, 11], [16, 3, 10, 5], [9, 6, 15, 4]])==True",
"assert magic_square_test([[2, 7, 6], [9, 5, 1], [4, 3, 8]])==True",
"assert magic_square_test([[2, 7, 6], [9, 5, 1], [4, 3, 7]])==False"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 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[0]
|
[] |
[
"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",
"assert max_occurrences([2,3,8,4,7,9,8,7,9,15,14,10,12,13,16,18])==8",
"assert max_occurrences([10,20,20,30,40,90,80,50,30,20,50,10])==20"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 131 |
Write a python function to reverse only the vowels of a given string (where y is not a vowel).
|
def reverse_vowels(str1):
vowels = ""
for char in str1:
if char in "aeiouAEIOU":
vowels += char
result_string = ""
for char in str1:
if char in "aeiouAEIOU":
result_string += vowels[-1]
vowels = vowels[:-1]
else:
result_string += char
return result_string
|
[] |
[
"assert reverse_vowels(\"Python\") == \"Python\"",
"assert reverse_vowels(\"USA\") == \"ASU\"",
"assert reverse_vowels(\"ab\") == \"ab\""
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 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"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 138 |
Write a python function to check whether the given number can be represented as sum of non-zero powers of 2 or not.
|
def is_Sum_Of_Powers_Of_Two(n):
if (n % 2 == 1):
return False
else:
return True
|
[] |
[
"assert is_Sum_Of_Powers_Of_Two(10) == True",
"assert is_Sum_Of_Powers_Of_Two(7) == False",
"assert is_Sum_Of_Powers_Of_Two(14) == True"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 141 |
Write a function to sort a list of elements.
|
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]"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 142 |
Write a function to count number items that are identical in the same position of three given lists.
|
def count_samepair(list1,list2,list3):
result = sum(m == n == o for m, n, o in zip(list1,list2,list3))
return result
|
[] |
[
"assert count_samepair([1,2,3,4,5,6,7,8],[2,2,3,1,2,6,7,9],[2,1,3,1,2,6,7,9])==3",
"assert count_samepair([1,2,3,4,5,6,7,8],[2,2,3,1,2,6,7,8],[2,1,3,1,2,6,7,8])==4",
"assert count_samepair([1,2,3,4,2,6,7,8],[2,2,3,1,2,6,7,8],[2,1,3,1,2,6,7,8])==5"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 143 |
Write a function to find number of lists present in the given tuple.
|
def find_lists(Input):
if isinstance(Input, list):
return 1
else:
return len(Input)
|
[] |
[
"assert find_lists(([1, 2, 3, 4], [5, 6, 7, 8])) == 2",
"assert find_lists(([1, 2], [3, 4], [5, 6])) == 3",
"assert find_lists(([9, 8, 7, 6, 5, 4, 3, 2, 1])) == 1"
] |
Mike's Copy of Benchmark Questions Verification V2.ipynb
| 145 |
Write a python function to find the maximum difference between any two elements in a given array.
|
def max_Abs_Diff(arr):
n = len(arr)
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",
"assert max_Abs_Diff((9,3,2,5,1)) == 8",
"assert max_Abs_Diff((3,2,1)) == 2"
] |
Benchmark Questions Verification V2.ipynb
| 160 |
Write a function that returns integers x and y that satisfy ax + by = n as a tuple, or return None if no solution exists.
|
def find_solution(a, b, n):
i = 0
while i * a <= n:
if (n - (i * a)) % b == 0:
return (i, (n - (i * a)) // b)
i = i + 1
return None
|
[] |
[
"assert find_solution(2, 3, 7) == (2, 1)",
"assert find_solution(4, 2, 7) == None",
"assert find_solution(1, 13, 17) == (4, 1)"
] |
Benchmark Questions Verification V2.ipynb
| 161 |
Write a function to remove all elements from a given list present in another list.
|
def remove_elements(list1, list2):
result = [x for x in list1 if x not in list2]
return result
|
[] |
[
"assert remove_elements([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [2, 4, 6, 8]) == [1, 3, 5, 7, 9, 10]",
"assert remove_elements([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 3, 5, 7]) == [2, 4, 6, 8, 9, 10]",
"assert remove_elements([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [5, 7]) == [1, 2, 3, 4, 6, 8, 9, 10]"
] |
Benchmark Questions Verification V2.ipynb
| 165 |
Write a function to count the number of characters in a string that occur at the same position in the string as in the English alphabet (case insensitive).
|
def count_char_position(str1):
count_chars = 0
for i in range(len(str1)):
if ((i == ord(str1[i]) - ord('A')) or
(i == ord(str1[i]) - ord('a'))):
count_chars += 1
return count_chars
|
[] |
[
"assert count_char_position(\"xbcefg\") == 2",
"assert count_char_position(\"ABcED\") == 3",
"assert count_char_position(\"AbgdeF\") == 5"
] |
Benchmark Questions Verification V2.ipynb
| 170 |
Write a function to find the sum of numbers in a list within a range specified by two indices.
|
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"
] |
Benchmark Questions Verification V2.ipynb
| 223 |
Write a function that takes in a sorted array, its length (n), and an element and returns whether the element is the majority element in the given sorted array. (The majority element is the element that occurs more than n/2 times.)
|
def is_majority(arr, n, x):
i = binary_search(arr, 0, n-1, x)
if i == -1:
return False
if ((i + n//2) <= (n -1)) and arr[i + n//2] == x:
return True
else:
return False
def binary_search(arr, low, high, x):
if high >= low:
mid = (low + high)//2
if (mid == 0 or x > arr[mid-1]) and (arr[mid] == x):
return mid
elif x > arr[mid]:
return binary_search(arr, (mid + 1), high, x)
else:
return binary_search(arr, low, (mid -1), x)
return -1
|
[] |
[
"assert is_majority([1, 2, 3, 3, 3, 3, 10], 7, 3) == True",
"assert is_majority([1, 1, 2, 4, 4, 4, 6, 6], 8, 4) == False",
"assert is_majority([1, 1, 1, 2, 2], 5, 1) == True",
"assert is_majority([1, 1, 2, 2], 5, 1) == False"
] |
Benchmark Questions Verification V2.ipynb
| 224 |
Write a python function to count the number of set bits (binary digits with value 1) in 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"
] |
Benchmark Questions Verification V2.ipynb
| 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"
] |
Benchmark Questions Verification V2.ipynb
| 235 |
Write a python function to set all even bits of a given number.
|
def even_bit_set_number(n):
count = 0;res = 0;temp = n
while(temp > 0):
if (count % 2 == 1):
res |= (1 << count)
count+=1
temp >>= 1
return (n | res)
|
[] |
[
"assert even_bit_set_number(10) == 10",
"assert even_bit_set_number(20) == 30",
"assert even_bit_set_number(30) == 30"
] |
Benchmark Questions Verification V2.ipynb
| 238 |
Write a python function to count the number of non-empty substrings of a given string.
|
def number_of_substrings(str):
str_len = len(str);
return int(str_len * (str_len + 1) / 2);
|
[] |
[
"assert number_of_substrings(\"abc\") == 6",
"assert number_of_substrings(\"abcd\") == 10",
"assert number_of_substrings(\"abcde\") == 15"
] |
Benchmark Questions Verification V2.ipynb
| 239 |
Write a function that takes in positive integers m and n and finds the number of possible sequences of length n, such that each element is a positive integer and is greater than or equal to twice the previous element but less than or equal to m.
|
def get_total_number_of_sequences(m,n):
T=[[0 for i in range(n+1)] for i in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i==0 or j==0:
T[i][j]=0
elif i<j:
T[i][j]=0
elif j==1:
T[i][j]=i
else:
T[i][j]=T[i-1][j]+T[i//2][j-1]
return T[m][n]
|
[] |
[
"assert get_total_number_of_sequences(10, 4) == 4",
"assert get_total_number_of_sequences(5, 2) == 6",
"assert get_total_number_of_sequences(16, 3) == 84"
] |
Benchmark Questions Verification V2.ipynb
| 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"
] |
Benchmark Questions Verification V2.ipynb
| 245 |
Write a function that takes an array and finds the maximum sum of a bitonic subsequence for the given array, where a sequence is bitonic if it is first increasing and then decreasing.
|
def max_sum(arr):
MSIBS = arr[:]
for i in range(len(arr)):
for j in range(0, i):
if arr[i] > arr[j] and MSIBS[i] < MSIBS[j] + arr[i]:
MSIBS[i] = MSIBS[j] + arr[i]
MSDBS = arr[:]
for i in range(1, len(arr) + 1):
for j in range(1, i):
if arr[-i] > arr[-j] and MSDBS[-i] < MSDBS[-j] + arr[-i]:
MSDBS[-i] = MSDBS[-j] + arr[-i]
max_sum = float("-Inf")
for i, j, k in zip(MSIBS, MSDBS, arr):
max_sum = max(max_sum, i + j - k)
return max_sum
|
[] |
[
"assert max_sum([1, 15, 51, 45, 33, 100, 12, 18, 9]) == 194",
"assert max_sum([80, 60, 30, 40, 20, 10]) == 210",
"assert max_sum([2, 3 ,14, 16, 21, 23, 29, 30]) == 138"
] |
Benchmark Questions Verification V2.ipynb
| 246 |
Write a function for computing square roots using the babylonian method.
|
def babylonian_squareroot(number):
if(number == 0):
return 0;
g = number/2.0;
g2 = g + 1;
while(g != g2):
n = number/ g;
g2 = g;
g = (g + n)/2;
return g;
|
[
"import math"
] |
[
"assert math.isclose(babylonian_squareroot(10), 3.162277660168379, rel_tol=0.001)",
"assert math.isclose(babylonian_squareroot(2), 1.414213562373095, rel_tol=0.001)",
"assert math.isclose(babylonian_squareroot(9), 3.0, rel_tol=0.001)"
] |
Benchmark Questions Verification V2.ipynb
| 247 |
Write a function to find the length of the longest palindromic subsequence in the given string.
|
def lps(str):
n = len(str)
L = [[0 for x in range(n)] for x in range(n)]
for i in range(n):
L[i][i] = 1
for cl in range(2, n+1):
for i in range(n-cl+1):
j = i+cl-1
if str[i] == str[j] and cl == 2:
L[i][j] = 2
elif str[i] == str[j]:
L[i][j] = L[i+1][j-1] + 2
else:
L[i][j] = max(L[i][j-1], L[i+1][j]);
return L[0][n-1]
|
[] |
[
"assert lps(\"TENS FOR TENS\") == 5",
"assert lps(\"CARDIO FOR CARDS\") == 7",
"assert lps(\"PART OF THE JOURNEY IS PART\") == 9"
] |
Benchmark Questions Verification V2.ipynb
| 250 |
Write a python function that takes in a tuple and an element and counts the occcurences of the element in the tuple.
|
def count_X(tup, x):
count = 0
for ele in tup:
if (ele == x):
count = count + 1
return count
|
[] |
[
"assert count_X((10, 8, 5, 2, 10, 15, 10, 8, 5, 8, 8, 2),4) == 0",
"assert count_X((10, 8, 5, 2, 10, 15, 10, 8, 5, 8, 8, 2),10) == 3",
"assert count_X((10, 8, 5, 2, 10, 15, 10, 8, 5, 8, 8, 2),8) == 4"
] |
Benchmark Questions Verification V2.ipynb
| 253 |
Write a python function that returns the number of integer elements in a given list.
|
def count_integer(list1):
ctr = 0
for i in list1:
if isinstance(i, int):
ctr = ctr + 1
return ctr
|
[] |
[
"assert count_integer([1,2,'abc',1.2]) == 2",
"assert count_integer([1,2,3]) == 3",
"assert count_integer([1,1.2,4,5.1]) == 2"
] |
Benchmark Questions Verification V2.ipynb
| 255 |
Write a function that takes in a list and length n, and generates all combinations (with repetition) of the elements of the list and returns a list with a tuple for each combination.
|
from itertools import combinations_with_replacement
def combinations_colors(l, n):
return list(combinations_with_replacement(l,n))
|
[] |
[
"assert combinations_colors( [\"Red\",\"Green\",\"Blue\"],1)==[('Red',), ('Green',), ('Blue',)]",
"assert combinations_colors( [\"Red\",\"Green\",\"Blue\"],2)==[('Red', 'Red'), ('Red', 'Green'), ('Red', 'Blue'), ('Green', 'Green'), ('Green', 'Blue'), ('Blue', 'Blue')]",
"assert combinations_colors( [\"Red\",\"Green\",\"Blue\"],3)==[('Red', 'Red', 'Red'), ('Red', 'Red', 'Green'), ('Red', 'Red', 'Blue'), ('Red', 'Green', 'Green'), ('Red', 'Green', 'Blue'), ('Red', 'Blue', 'Blue'), ('Green', 'Green', 'Green'), ('Green', 'Green', 'Blue'), ('Green', 'Blue', 'Blue'), ('Blue', 'Blue', 'Blue')]"
] |
Benchmark Questions Verification V2.ipynb
| 256 |
Write a python function that takes in a non-negative number and returns the number of prime numbers less than the given non-negative number.
|
def count_Primes_nums(n):
ctr = 0
for num in range(n):
if num <= 1:
continue
for i in range(2,num):
if (num % i) == 0:
break
else:
ctr += 1
return ctr
|
[] |
[
"assert count_Primes_nums(5) == 2",
"assert count_Primes_nums(10) == 4",
"assert count_Primes_nums(100) == 25"
] |
Benchmark Questions Verification V2.ipynb
| 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"
] |
Benchmark Questions Verification V2.ipynb
| 262 |
Write a function that takes in a list and an integer L and splits the given list into two parts where the length of the first part of the list is L, and returns the resulting lists in a tuple.
|
def split_two_parts(list1, L):
return list1[:L], list1[L:]
|
[] |
[
"assert split_two_parts([1,1,2,3,4,4,5,1],3)==([1, 1, 2], [3, 4, 4, 5, 1])",
"assert split_two_parts(['a', 'b', 'c', 'd'],2)==(['a', 'b'], ['c', 'd'])",
"assert split_two_parts(['p', 'y', 't', 'h', 'o', 'n'],4)==(['p', 'y', 't', 'h'], ['o', 'n'])"
] |
Benchmark Questions Verification V2.ipynb
| 265 |
Write a function that takes in a list and an integer n and splits a list for every nth element, returning a list of the resulting lists.
|
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']]"
] |
Benchmark Questions Verification V2.ipynb
| 267 |
Write a python function that takes in an integer n and returns the sum of the squares of the 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"
] |
Benchmark Questions Verification V2.ipynb
| 268 |
Write a function to find the n'th star number.
|
def find_star_num(n):
return (6 * n * (n - 1) + 1)
|
[] |
[
"assert find_star_num(3) == 37",
"assert find_star_num(4) == 73",
"assert find_star_num(5) == 121"
] |
Benchmark Questions Verification V2.ipynb
| 271 |
Write a python function that takes in an integer n and finds the sum of the first n even natural numbers that are raised to the fifth power.
|
def even_Power_Sum(n):
sum = 0;
for i in range(1,n+1):
j = 2*i;
sum = sum + (j*j*j*j*j);
return sum;
|
[] |
[
"assert even_Power_Sum(2) == 1056",
"assert even_Power_Sum(3) == 8832",
"assert even_Power_Sum(1) == 32"
] |
Benchmark Questions Verification V2.ipynb
| 274 |
Write a python function that takes in a positive integer n and finds the sum of even index binomial coefficients.
|
import math
def even_binomial_Coeff_Sum( n):
return (1 << (n - 1))
|
[] |
[
"assert even_binomial_Coeff_Sum(4) == 8",
"assert even_binomial_Coeff_Sum(6) == 32",
"assert even_binomial_Coeff_Sum(2) == 2"
] |
Benchmark Questions Verification V2.ipynb
| 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"
] |
Benchmark Questions Verification V2.ipynb
| 280 |
Write a function that takes in an array and element and returns a tuple containing a boolean that indicates if the element is in the array and the index position of the element (or -1 if the element is not found).
|
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)"
] |
Benchmark Questions Verification V2.ipynb
| 281 |
Write a python function to check if the elements of a given list are unique or not.
|
def all_unique(test_list):
if len(test_list) > len(set(test_list)):
return False
return True
|
[] |
[
"assert all_unique([1,2,3]) == True",
"assert all_unique([1,2,1,2]) == False",
"assert all_unique([1,2,3,4,5]) == True"
] |
Benchmark Questions Verification V2.ipynb
| 283 |
Write a python function takes in an integer and check whether the frequency of each digit in the integer 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"
] |
Benchmark Questions Verification V2.ipynb
| 285 |
Write a function that checks whether a string contains the 'a' character followed by two or three 'b' characters.
|
import re
def text_match_two_three(text):
patterns = 'ab{2,3}'
if re.search(patterns, text):
return True
else:
return False
|
[] |
[
"assert text_match_two_three(\"ac\")==(False)",
"assert text_match_two_three(\"dc\")==(False)",
"assert text_match_two_three(\"abbbba\")==(True)"
] |
Benchmark Questions Verification V2.ipynb
| 286 |
Write a function to find the largest sum of a contiguous array in the modified array which is formed by repeating the given array k times.
|
def max_sub_array_sum_repeated(a, n, k):
max_so_far = -2147483648
max_ending_here = 0
for i in range(n*k):
max_ending_here = max_ending_here + a[i%n]
if (max_so_far < max_ending_here):
max_so_far = max_ending_here
if (max_ending_here < 0):
max_ending_here = 0
return max_so_far
|
[] |
[
"assert max_sub_array_sum_repeated([10, 20, -30, -1], 4, 3) == 30",
"assert max_sub_array_sum_repeated([-1, 10, 20], 3, 2) == 59",
"assert max_sub_array_sum_repeated([-1, -2, -3], 3, 3) == -1"
] |
Benchmark Questions Verification V2.ipynb
| 287 |
Write a python function takes in an integer n and returns 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"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 290 |
Write a function to find the list of maximum length in a list of lists.
|
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])"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 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"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 292 |
Write a python function to find quotient of two numbers (rounded down to the nearest integer).
|
def find(n,m):
q = n//m
return (q)
|
[] |
[
"assert find(10,3) == 3",
"assert find(4,2) == 2",
"assert find(20,5) == 4"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 295 |
Write a function to return the sum of all divisors of a number.
|
def sum_div(number):
divisors = [1]
for i in range(2, number):
if (number % i)==0:
divisors.append(i)
return sum(divisors)
|
[] |
[
"assert sum_div(8)==7",
"assert sum_div(12)==16",
"assert sum_div(7)==1"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 296 |
Write a python function to count inversions in an array.
|
def get_Inv_Count(arr):
inv_count = 0
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
if (arr[i] > arr[j]):
inv_count += 1
return inv_count
|
[] |
[
"assert get_Inv_Count([1,20,6,4,5]) == 5",
"assert get_Inv_Count([1,2,1]) == 1",
"assert get_Inv_Count([1,2,5,6,1]) == 3"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 297 |
Write a function to flatten a given nested list structure.
|
def flatten_list(list1):
result_list = []
if not list1: return result_list
stack = [list(list1)]
while stack:
c_num = stack.pop()
next = c_num.pop()
if c_num: stack.append(c_num)
if isinstance(next, list):
if next: stack.append(list(next))
else: result_list.append(next)
result_list.reverse()
return result_list
|
[] |
[
"assert flatten_list([0, 10, [20, 30], 40, 50, [60, 70, 80], [90, 100, 110, 120]])==[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]",
"assert flatten_list([[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]])==[10, 20, 40, 30, 56, 25, 10, 20, 33, 40]",
"assert flatten_list([[1,2,3], [4,5,6], [10,11,12], [7,8,9]])==[1, 2, 3, 4, 5, 6, 10, 11, 12, 7, 8, 9]"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 299 |
Write a function to calculate the maximum aggregate from the list of tuples.
|
from collections import defaultdict
def max_aggregate(stdata):
temp = defaultdict(int)
for name, marks in stdata:
temp[name] += marks
return max(temp.items(), key=lambda x: x[1])
|
[] |
[
"assert max_aggregate([('Juan Whelan',90),('Sabah Colley',88),('Peter Nichols',7),('Juan Whelan',122),('Sabah Colley',84)])==('Juan Whelan', 212)",
"assert max_aggregate([('Juan Whelan',50),('Sabah Colley',48),('Peter Nichols',37),('Juan Whelan',22),('Sabah Colley',14)])==('Juan Whelan', 72)",
"assert max_aggregate([('Juan Whelan',10),('Sabah Colley',20),('Peter Nichols',30),('Juan Whelan',40),('Sabah Colley',50)])==('Sabah Colley', 70)"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 300 |
Write a function to find the count of all binary sequences of length 2n such that sum of first n bits is same as sum of last n bits.
|
def count_binary_seq(n):
nCr = 1
res = 1
for r in range(1, n + 1):
nCr = (nCr * (n + 1 - r)) / r
res += nCr * nCr
return res
|
[
"import math"
] |
[
"assert math.isclose(count_binary_seq(1), 2.0, rel_tol=0.001)",
"assert math.isclose(count_binary_seq(2), 6.0, rel_tol=0.001)",
"assert math.isclose(count_binary_seq(3), 20.0, rel_tol=0.001)"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 301 |
Write a function to find the depth of a dictionary.
|
def dict_depth(d):
if isinstance(d, dict):
return 1 + (max(map(dict_depth, d.values())) if d else 0)
return 0
|
[] |
[
"assert dict_depth({'a':1, 'b': {'c': {'d': {}}}})==4",
"assert dict_depth({'a':1, 'b': {'c':'python'}})==2",
"assert dict_depth({1: 'Sun', 2: {3: {4:'Mon'}}})==3"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 306 |
Write a function to find the maximum sum of increasing subsequence from prefix until 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"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 308 |
Write a function to find the specified number of largest products from two given lists, selecting one factor from each list.
|
def large_product(nums1, nums2, N):
result = sorted([x*y for x in nums1 for y in nums2], reverse=True)[:N]
return result
|
[] |
[
"assert large_product([1, 2, 3, 4, 5, 6],[3, 6, 8, 9, 10, 6],3)==[60, 54, 50]",
"assert large_product([1, 2, 3, 4, 5, 6],[3, 6, 8, 9, 10, 6],4)==[60, 54, 50, 48]",
"assert large_product([1, 2, 3, 4, 5, 6],[3, 6, 8, 9, 10, 6],5)==[60, 54, 50, 48, 45]"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 309 |
Write a python function to find the maximum of two numbers.
|
def maximum(a,b):
if a >= b:
return a
else:
return b
|
[] |
[
"assert maximum(5,10) == 10",
"assert maximum(-1,-2) == -1",
"assert maximum(9,7) == 9"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 310 |
Write a function to convert a given string to a tuple of characters.
|
def string_to_tuple(str1):
result = tuple(x for x in str1 if not x.isspace())
return result
|
[] |
[
"assert string_to_tuple(\"python 3.0\")==('p', 'y', 't', 'h', 'o', 'n', '3', '.', '0')",
"assert string_to_tuple(\"item1\")==('i', 't', 'e', 'm', '1')",
"assert string_to_tuple(\"15.10\")==('1', '5', '.', '1', '0')"
] |
Ellen's Copy of Benchmark Questions Verification V2.ipynb
| 311 |
Write a python function to set the left most unset bit.
|
def set_left_most_unset_bit(n):
if not (n & (n + 1)):
return n
pos, temp, count = 0, n, 0
while temp:
if not (temp & 1):
pos = count
count += 1; temp>>=1
return (n | (1 << (pos)))
|
[] |
[
"assert set_left_most_unset_bit(10) == 14",
"assert set_left_most_unset_bit(12) == 14",
"assert set_left_most_unset_bit(15) == 15"
] |
Benchmark Questions Verification V2.ipynb
| 388 |
Write a python function to find the highest power of 2 that is less than or equal to n.
|
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"
] |
End of preview. Expand
in Data Studio
README.md exists but content is empty.
- Downloads last month
- 3