mr44/mbsearch huggingface dataset:
Task: Write a function to flatten the given tuple matrix into the tuple list with each tuple representing each column.

Solution:
def matrix_to_list(test_list):
  temp = [ele for sub in test_list for ele in sub]
  res = list(zip(*temp))
  return (str(res))


Tests:
assert matrix_to_list([[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]) == '[(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]'
assert matrix_to_list([[(5, 6), (8, 9)], [(11, 14), (19, 18)], [(1, 5), (11, 2)]]) == '[(5, 8, 11, 19, 1, 11), (6, 9, 14, 18, 5, 2)]'
assert matrix_to_list([[(6, 7), (9, 10)], [(12, 15), (20, 21)], [(23, 7), (15, 8)]]) == '[(6, 9, 12, 20, 23, 15), (7, 10, 15, 21, 7, 8)]'
Task: Write a function to group a sequence of key-value pairs into a dictionary of lists using collections module.

Solution:
from collections import defaultdict
def grouping_dictionary(l):
    d = defaultdict(list)
    for k, v in l:
        d[k].append(v)
    return d


Tests:
assert grouping_dictionary([('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)])== ({'yellow': [1, 3], 'blue': [2, 4], 'red': [1]})
assert grouping_dictionary([('yellow', 10), ('blue', 20), ('yellow', 30), ('blue', 40), ('red', 10)])== ({'yellow': [10, 30], 'blue': [20, 40], 'red': [10]})
assert grouping_dictionary([('yellow', 15), ('blue', 25), ('yellow', 35), ('blue', 45), ('red', 15)])== ({'yellow': [15, 35], 'blue': [25, 45], 'red': [15]})
Task: Write a function to find the perimeter of a rectangle.

Solution:
def rectangle_perimeter(l,b):
  perimeter=2*(l+b)
  return perimeter


Tests:
assert rectangle_perimeter(10,20)==60
assert rectangle_perimeter(10,5)==30
assert rectangle_perimeter(4,2)==12
Task: Write a python function to find the sum of fifth power of n natural numbers.

Solution:
def fifth_Power_Sum(n) : 
    sm = 0 
    for i in range(1,n+1) : 
        sm = sm + (i*i*i*i*i) 
    return sm 


Tests:
assert fifth_Power_Sum(2) == 33
assert fifth_Power_Sum(4) == 1300
assert fifth_Power_Sum(3) == 276
Task: Write a python function to find the minimum sum of absolute differences of two arrays.

Solution:
def find_Min_Sum(a,b,n): 
    a.sort() 
    b.sort() 
    sum = 0  
    for i in range(n): 
        sum = sum + abs(a[i] - b[i]) 
    return sum


Tests:
assert find_Min_Sum([3,2,1],[2,1,3],3) == 0
assert find_Min_Sum([1,2,3],[4,5,6],3) == 9
assert find_Min_Sum([4,1,8,7],[2,3,6,5],4) == 6
Task: Write a python function to find the first digit in factorial of a given number.

Solution:
import math 
def first_Digit(n) : 
    fact = 1
    for i in range(2,n + 1) : 
        fact = fact * i 
        while (fact % 10 == 0) :  
            fact = int(fact / 10) 
    while (fact >= 10) : 
        fact = int(fact / 10) 
    return math.floor(fact) 


Tests:
assert first_Digit(5) == 1
assert first_Digit(10) == 3
assert first_Digit(7) == 5
Task: Write a function to find the item with maximum occurrences in a given list.

Solution:
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


Tests:
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
Task: Write a python function to print duplicants from a list of integers.

Solution:
def Repeat(x): 
    _size = len(x) 
    repeated = [] 
    for i in range(_size): 
        k = i + 1
        for j in range(k, _size): 
            if x[i] == x[j] and x[i] not in repeated: 
                repeated.append(x[i]) 
    return repeated 


Tests:
assert Repeat([10, 20, 30, 20, 20, 30, 40, 50, -20, 60, 60, -20, -20]) == [20, 30, -20, 60]
assert Repeat([-1, 1, -1, 8]) == [-1]
assert Repeat([1, 2, 3, 1, 2,]) == [1, 2]
Task: Write a python function to choose points from two ranges such that no point lies in both the ranges.

Solution:
def find_Points(l1,r1,l2,r2): 
    x = min(l1,l2) if (l1 != l2) else -1
    y = max(r1,r2) if (r1 != r2) else -1
    return (x,y)


Tests:
assert find_Points(5,10,1,5) == (1,10)
assert find_Points(3,5,7,9) == (3,9)
assert find_Points(1,5,2,8) == (1,8)
Task: Write a function to find the maximum sum that can be formed which has no three consecutive elements present.

Solution:
def max_sum_of_three_consecutive(arr, n): 
	sum = [0 for k in range(n)] 
	if n >= 1: 
		sum[0] = arr[0] 
	if n >= 2: 
		sum[1] = arr[0] + arr[1] 
	if n > 2: 
		sum[2] = max(sum[1], max(arr[1] + arr[2], arr[0] + arr[2])) 
	for i in range(3, n): 
		sum[i] = max(max(sum[i-1], sum[i-2] + arr[i]), arr[i] + arr[i-1] + sum[i-3]) 
	return sum[n-1]


Tests:
assert max_sum_of_three_consecutive([100, 1000, 100, 1000, 1], 5) == 2101
assert max_sum_of_three_consecutive([3000, 2000, 1000, 3, 10], 5) == 5013
assert max_sum_of_three_consecutive([1, 2, 3, 4, 5, 6, 7, 8], 8) == 27
Task: Write a function to sort a list in a dictionary.

Solution:
def sorted_dict(dict1):
  sorted_dict = {x: sorted(y) for x, y in dict1.items()}
  return sorted_dict


Tests:
assert sorted_dict({'n1': [2, 3, 1], 'n2': [5, 1, 2], 'n3': [3, 2, 4]})=={'n1': [1, 2, 3], 'n2': [1, 2, 5], 'n3': [2, 3, 4]}
assert sorted_dict({'n1': [25,37,41], 'n2': [41,54,63], 'n3': [29,38,93]})=={'n1': [25, 37, 41], 'n2': [41, 54, 63], 'n3': [29, 38, 93]}
assert sorted_dict({'n1': [58,44,56], 'n2': [91,34,58], 'n3': [100,200,300]})=={'n1': [44, 56, 58], 'n2': [34, 58, 91], 'n3': [100, 200, 300]}
Task: Write a function to find the largest possible value of k such that k modulo x is y.

Solution:
import sys 
def find_max_val(n, x, y): 
	ans = -sys.maxsize 
	for k in range(n + 1): 
		if (k % x == y): 
			ans = max(ans, k) 
	return (ans if (ans >= 0 and
					ans <= n) else -1) 


Tests:
assert find_max_val(15, 10, 5) == 15
assert find_max_val(187, 10, 5) == 185
assert find_max_val(16, 11, 1) == 12
Task: Write a python function to find the average of even numbers till a given even number.

Solution:
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 


Tests:
assert average_Even(2) == 2
assert average_Even(4) == 3
assert average_Even(100) == 51
Task: Write a python function to shift first element to the end of given list.

Solution:
def move_last(num_list):
    a = [num_list[0] for i in range(num_list.count(num_list[0]))]
    x = [ i for i in num_list if i != num_list[0]]
    x.extend(a)
    return (x)


Tests:
assert move_last([1,2,3,4]) == [2,3,4,1]
assert move_last([2,3,4,1,5,0]) == [3,4,1,5,0,2]
assert move_last([5,4,3,2,1]) == [4,3,2,1,5]
Task: Write a function to count occurrence of a character in a string.

Solution:
def count_char(string,char):
 count = 0
 for i in range(len(string)):
    if(string[i] == char):
        count = count + 1
 return count


Tests:
assert count_char("Python",'o')==1
assert count_char("little",'t')==2
assert count_char("assert",'s')==2
Task: Write a python function to count number of vowels in the string.

Solution:
def Check_Vow(string, vowels): 
    final = [each for each in string if each in vowels] 
    return(len(final)) 



Tests:
assert Check_Vow('corner','AaEeIiOoUu') == 2
assert Check_Vow('valid','AaEeIiOoUu') == 2
assert Check_Vow('true','AaEeIiOoUu') ==2
Task: Write a python function to replace multiple occurence of character by single.

Solution:
import re 
def replace(string, char): 
    pattern = char + '{2,}'
    string = re.sub(pattern, char, string) 
    return string 


Tests:
assert replace('peep','e') == 'pep'
assert replace('Greek','e') == 'Grek'
assert replace('Moon','o') == 'Mon'
Task: Write a function to check whether the given ip address is valid or not using regex.

Solution:
import re 
regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
			25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
			25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
			25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$'''
def check_IP(Ip): 
	if(re.search(regex, Ip)): 
		return ("Valid IP address") 
	else: 
		return ("Invalid IP address") 


Tests:
assert check_IP("192.168.0.1") == 'Valid IP address'
assert check_IP("110.234.52.124") == 'Valid IP address'
assert check_IP("366.1.2.2") == 'Invalid IP address'
Task: Write a python function to check whether a sequence of numbers has a decreasing trend or not.

Solution:
def decreasing_trend(nums):
    if (sorted(nums)== nums):
        return True
    else:
        return False


Tests:
assert decreasing_trend([-4,-3,-2,-1]) == True
assert decreasing_trend([1,2,3]) == True
assert decreasing_trend([3,2,1]) == False
Task: Write a python function to set the right most unset bit.

Solution:
import math 
def get_Pos_Of_Right_most_Set_Bit(n): 
    return int(math.log2(n&-n)+1)   
def set_Right_most_Unset_Bit(n): 
    if (n == 0): 
        return 1
    if ((n & (n + 1)) == 0):     
        return n 
    pos = get_Pos_Of_Right_most_Set_Bit(~n)      
    return ((1 << (pos - 1)) | n) 


Tests:
assert set_Right_most_Unset_Bit(21) == 23
assert set_Right_most_Unset_Bit(11) == 15
assert set_Right_most_Unset_Bit(15) == 15
Task: Write a function to find maximum of three numbers.

Solution:
def max_of_three(num1,num2,num3): 
    if (num1 >= num2) and (num1 >= num3):
       lnum = num1
    elif (num2 >= num1) and (num2 >= num3):
       lnum = num2
    else:
       lnum = num3
    return lnum


Tests:
assert max_of_three(10,20,30)==30
assert max_of_three(55,47,39)==55
assert max_of_three(10,49,30)==49
Task: Write a python function to convert a list of multiple integers into a single integer.

Solution:
def convert(list): 
    s = [str(i) for i in list] 
    res = int("".join(s))  
    return (res) 


Tests:
assert convert([1,2,3]) == 123
assert convert([4,5,6]) == 456
assert convert([7,8,9]) == 789
Task: Write a function to remove duplicate words from a given string using collections module.

Solution:
from collections import OrderedDict
def remove_duplicate(string):
  result = ' '.join(OrderedDict((w,w) for w in string.split()).keys())
  return result


Tests:
assert remove_duplicate("Python Exercises Practice Solution Exercises")==("Python Exercises Practice Solution")
assert remove_duplicate("Python Exercises Practice Solution Python")==("Python Exercises Practice Solution")
assert remove_duplicate("Python Exercises Practice Solution Practice")==("Python Exercises Practice Solution")
Task: Write a function to add two integers. however, if the sum is between the given range it will return 20.

Solution:
def sum_nums(x, y,m,n):
    sum_nums= x + y
    if sum_nums in range(m, n):
        return 20
    else:
        return sum_nums


Tests:
assert sum_nums(2,10,11,20)==20
assert sum_nums(15,17,1,10)==32
assert sum_nums(10,15,5,30)==20
Task: Write a function to remove everything except alphanumeric characters from the given string by using regex.

Solution:
import re
def remove_extra_char(text1):
  pattern = re.compile('[\W_]+')
  return (pattern.sub('', text1))


Tests:
assert remove_extra_char('**//Google Android// - 12. ') == 'GoogleAndroid12'
assert remove_extra_char('****//Google Flutter//*** - 36. ') == 'GoogleFlutter36'
assert remove_extra_char('**//Google Firebase// - 478. ') == 'GoogleFirebase478'
Task: Write a function to check if the triangle is valid or not.

Solution:
def validity_triangle(a,b,c):
 total = a + b + c
 if total == 180:
    return True
 else:
    return False


Tests:
assert validity_triangle(60,50,90)==False
assert validity_triangle(45,75,60)==True
assert validity_triangle(30,50,100)==True
Task: Write a python function to remove spaces from a given string.

Solution:
def remove_spaces(str1):
  str1 = str1.replace(' ','')
  return str1


Tests:
assert remove_spaces("a b c") == "abc"
assert remove_spaces("1 2 3") == "123"
assert remove_spaces(" b c") == "bc"
Task: Write a function to access dictionary key’s element by index.

Solution:
def access_key(ditionary,key):
  return list(ditionary)[key]


Tests:
assert access_key({'physics': 80, 'math': 90, 'chemistry': 86},0)== 'physics'
assert access_key({'python':10, 'java': 20, 'C++':30},2)== 'C++'
assert access_key({'program':15,'computer':45},1)== 'computer'
Task: Write a python function to check whether a sequence of numbers has an increasing trend or not.

Solution:
def increasing_trend(nums):
    if (sorted(nums)== nums):
        return True
    else:
        return False


Tests:
assert increasing_trend([1,2,3,4]) == True
assert increasing_trend([4,3,2,1]) == False
assert increasing_trend([0,1,4,9]) == True
Task: Write a python function to find the smallest prime divisor of a number.

Solution:
def smallest_Divisor(n): 
    if (n % 2 == 0): 
        return 2; 
    i = 3;  
    while (i*i <= n): 
        if (n % i == 0): 
            return i; 
        i += 2; 
    return n; 


Tests:
assert smallest_Divisor(10) == 2
assert smallest_Divisor(25) == 5
assert smallest_Divisor(31) == 31
Task: Write a function to multiply two lists using map and lambda function.

Solution:
def mul_list(nums1,nums2):
  result = map(lambda x, y: x * y, nums1, nums2)
  return list(result)


Tests:
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]
Task: Write a python function to check whether the given number can be represented by sum of two squares or not.

Solution:
def sum_Square(n) : 
    i = 1 
    while i*i <= n : 
        j = 1
        while (j*j <= n) : 
            if (i*i+j*j == n) : 
                return True
            j = j+1
        i = i+1     
    return False


Tests:
assert sum_Square(25) == True
assert sum_Square(24) == False
assert sum_Square(17) == True
Task: Write a python function to count occurences of a character in a repeated string.

Solution:
def count_Char(str,x): 
    count = 0
    for i in range(len(str)):  
        if (str[i] == x) : 
            count += 1
    n = 10
    repititions = n // len(str)  
    count = count * repititions  
    l = n % len(str)  
    for i in range(l): 
        if (str[i] == x):  
            count += 1
    return count  


Tests:
assert count_Char("abcac",'a') == 4
assert count_Char("abca",'c') == 2
assert count_Char("aba",'a') == 7
Task: Write a python function to find sum of prime numbers between 1 to n.

Solution:
def sum_Of_Primes(n): 
    prime = [True] * (n + 1)  
    p = 2
    while p * p <= n: 
        if prime[p] == True:  
            i = p * 2
            while i <= n: 
                prime[i] = False
                i += p 
        p += 1    
    sum = 0
    for i in range (2,n + 1): 
        if(prime[i]): 
            sum += i 
    return sum


Tests:
assert sum_Of_Primes(10) == 17
assert sum_Of_Primes(20) == 77
assert sum_Of_Primes(5) == 10
Task: Write a function to find the frequency of each element in the given list.

Solution:
from collections import defaultdict 
def freq_element(test_tup):
  res = defaultdict(int)
  for ele in test_tup:
    res[ele] += 1
  return (str(dict(res))) 


Tests:
assert freq_element((4, 5, 4, 5, 6, 6, 5, 5, 4) ) == '{4: 3, 5: 4, 6: 2}'
assert freq_element((7, 8, 8, 9, 4, 7, 6, 5, 4) ) == '{7: 2, 8: 2, 9: 1, 4: 2, 6: 1, 5: 1}'
assert freq_element((1, 4, 3, 1, 4, 5, 2, 6, 2, 7) ) == '{1: 2, 4: 2, 3: 1, 5: 1, 2: 2, 6: 1, 7: 1}'
Task: Write a function to find the greatest common divisor (gcd) of two integers by using recursion.

Solution:
def recur_gcd(a, b):
	low = min(a, b)
	high = max(a, b)
	if low == 0:
		return high
	elif low == 1:
		return 1
	else:
		return recur_gcd(low, high%low)


Tests:
assert recur_gcd(12,14) == 2
assert recur_gcd(13,17) == 1
assert recur_gcd(9, 3) == 3
Task: Write a function to get the length of a complex number.

Solution:
import cmath
def len_complex(a,b):
  cn=complex(a,b)
  length=abs(cn)
  return length


Tests:
assert len_complex(3,4)==5.0
assert len_complex(9,10)==13.45362404707371
assert len_complex(7,9)==11.40175425099138
Task: ## write a function to find the minimum number of jumps to reach the end of the array for the given array of integers where each element represents the max number of steps that can be made forward from that element. > indented block > indented block

Solution:
def min_jumps(arr, n):
	jumps = [0 for i in range(n)]
	if (n == 0) or (arr[0] == 0):
		return float('inf')
	jumps[0] = 0
	for i in range(1, n):
		jumps[i] = float('inf')
		for j in range(i):
			if (i <= j + arr[j]) and (jumps[j] != float('inf')):
				jumps[i] = min(jumps[i], jumps[j] + 1)
				break
	return jumps[n-1]


Tests:
assert min_jumps([1, 3, 6, 1, 0, 9], 6) == 3
assert min_jumps([1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9], 11) == 3
assert min_jumps([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 11) == 10
Task: Write a function to multiply consecutive numbers of a given list.

Solution:
def mul_consecutive_nums(nums):
    result = [b*a for a, b in zip(nums[:-1], nums[1:])]
    return result


Tests:
assert mul_consecutive_nums([1, 1, 3, 4, 4, 5, 6, 7])==[1, 3, 12, 16, 20, 30, 42]
assert mul_consecutive_nums([4, 5, 8, 9, 6, 10])==[20, 40, 72, 54, 60]
assert mul_consecutive_nums([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])==[2, 6, 12, 20, 30, 42, 56, 72, 90]
Task: Write a function to group the 1st elements on the basis of 2nd elements in the given tuple list.

Solution:
from itertools import groupby 
def group_element(test_list):
  res = dict()
  for key, val in groupby(sorted(test_list, key = lambda ele: ele[1]), key = lambda ele: ele[1]):
    res[key] = [ele[0] for ele in val] 
  return (res)



Tests:
assert group_element([(6, 5), (2, 7), (2, 5), (8, 7), (9, 8), (3, 7)]) == {5: [6, 2], 7: [2, 8, 3], 8: [9]}
assert group_element([(7, 6), (3, 8), (3, 6), (9, 8), (10, 9), (4, 8)]) == {6: [7, 3], 8: [3, 9, 4], 9: [10]}
assert group_element([(8, 7), (4, 9), (4, 7), (10, 9), (11, 10), (5, 9)]) == {7: [8, 4], 9: [4, 10, 5], 10: [11]}
Task: Write a python function to find the last two digits in factorial of a given number.

Solution:
def last_Two_Digits(N): 
    if (N >= 10): 
        return
    fac = 1
    for i in range(1,N + 1): 
        fac = (fac * i) % 100
    return (fac) 


Tests:
assert last_Two_Digits(7) == 40
assert last_Two_Digits(5) == 20
assert last_Two_Digits(2) == 2
Task: Write a function to remove multiple spaces in a string by using regex.

Solution:
import re
def remove_multiple_spaces(text1):
  return (re.sub(' +',' ',text1))


Tests:
assert remove_multiple_spaces('Google      Assistant') == 'Google Assistant'
assert remove_multiple_spaces('Quad      Core') == 'Quad Core'
assert remove_multiple_spaces('ChromeCast      Built-in') == 'ChromeCast Built-in'
Task: Write a function to extract unique values from the given dictionary values.

Solution:
def extract_unique(test_dict):
  res = list(sorted({ele for val in test_dict.values() for ele in val}))
  return res


Tests:
assert extract_unique({'msm' : [5, 6, 7, 8],'is' : [10, 11, 7, 5],'best' : [6, 12, 10, 8],'for' : [1, 2, 5]} ) == [1, 2, 5, 6, 7, 8, 10, 11, 12]
assert extract_unique({'Built' : [7, 1, 9, 4],'for' : [11, 21, 36, 14, 9],'ISP' : [4, 1, 21, 39, 47],'TV' : [1, 32, 38]} ) == [1, 4, 7, 9, 11, 14, 21, 32, 36, 38, 39, 47]
assert extract_unique({'F' : [11, 13, 14, 17],'A' : [12, 11, 15, 18],'N' : [19, 21, 15, 36],'G' : [37, 36, 35]}) == [11, 12, 13, 14, 15, 17, 18, 19, 21, 35, 36, 37]
Task: Write a function to check if each element of the second tuple is greater than its corresponding index in the first tuple.

Solution:
def check_greater(test_tup1, test_tup2):
  res = all(x < y for x, y in zip(test_tup1, test_tup2))
  return (res) 


Tests:
assert check_greater((10, 4, 5), (13, 5, 18)) == True
assert check_greater((1, 2, 3), (2, 1, 4)) == False
assert check_greater((4, 5, 6), (5, 6, 7)) == True
Task: Write a function to zip two given lists of lists.

Solution:
def zip_list(list1,list2):  
 result = list(map(list.__add__, list1, list2)) 
 return result


Tests:
assert zip_list([[1, 3], [5, 7], [9, 11]] ,[[2, 4], [6, 8], [10, 12, 14]] )==[[1, 3, 2, 4], [5, 7, 6, 8], [9, 11, 10, 12, 14]]
assert zip_list([[1, 2], [3, 4], [5, 6]] ,[[7, 8], [9, 10], [11, 12]] )==[[1, 2, 7, 8], [3, 4, 9, 10], [5, 6, 11, 12]]
assert zip_list([['a','b'],['c','d']] , [['e','f'],['g','h']] )==[['a','b','e','f'],['c','d','g','h']]
Task: Write a function to find number of even elements in the given list using lambda function.

Solution:
def count_even(array_nums):
   count_even = len(list(filter(lambda x: (x%2 == 0) , array_nums)))
   return count_even


Tests:
assert count_even([1, 2, 3, 5, 7, 8, 9, 10])==3
assert count_even([10,15,14,13,-18,12,-20])==5
assert count_even([1, 2, 4, 8, 9])==3
Task: Write a function to sort dictionary items by tuple product of keys for the given dictionary with tuple keys.

Solution:
def sort_dict_item(test_dict):
  res = {key: test_dict[key] for key in sorted(test_dict.keys(), key = lambda ele: ele[1] * ele[0])}
  return  (res) 



Tests:
assert sort_dict_item({(5, 6) : 3, (2, 3) : 9, (8, 4): 10, (6, 4): 12} ) == {(2, 3): 9, (6, 4): 12, (5, 6): 3, (8, 4): 10}
assert sort_dict_item({(6, 7) : 4, (3, 4) : 10, (9, 5): 11, (7, 5): 13} ) == {(3, 4): 10, (7, 5): 13, (6, 7): 4, (9, 5): 11}
assert sort_dict_item({(7, 8) : 5, (4, 5) : 11, (10, 6): 12, (8, 6): 14} ) == {(4, 5): 11, (8, 6): 14, (7, 8): 5, (10, 6): 12}
Task: Write a python function to find the minimum number of swaps required to convert one binary string to another.

Solution:
def min_Swaps(str1,str2) : 
    count = 0
    for i in range(len(str1)) : 
        if str1[i] != str2[i] : 
            count += 1
    if count % 2 == 0 : 
        return (count // 2) 
    else : 
        return ("Not Possible") 


Tests:
assert min_Swaps("1101","1110") == 1
assert min_Swaps("1111","0100") == "Not Possible"
assert min_Swaps("1110000","0001101") == 3
Task: Write a function to count the number of elements in a list which are within a specific range.

Solution:
def count_range_in_list(li, min, max):
	ctr = 0
	for x in li:
		if min <= x <= max:
			ctr += 1
	return ctr


Tests:
assert count_range_in_list([10,20,30,40,40,40,70,80,99],40,100)==6
assert count_range_in_list(['a','b','c','d','e','f'],'a','e')==5
assert count_range_in_list([7,8,9,15,17,19,45],15,20)==3
Task: Write a function to find the equilibrium index of the given array.

Solution:
def equilibrium_index(arr):
  total_sum = sum(arr)
  left_sum=0
  for i, num in enumerate(arr):
    total_sum -= num
    if left_sum == total_sum:
      return i
    left_sum += num
  return -1


Tests:
assert equilibrium_index([1, 2, 3, 4, 1, 2, 3]) == 3
assert equilibrium_index([-7, 1, 5, 2, -4, 3, 0]) == 3
assert equilibrium_index([1, 2, 3]) == -1
Task: Write a function to find the minimum number of elements that should be removed such that amax-amin<=k.

Solution:
def find_ind(key, i, n, 
			k, arr):
	ind = -1
	start = i + 1
	end = n - 1;
	while (start < end):
		mid = int(start +
				(end - start) / 2)
		if (arr[mid] - key <= k):
			ind = mid
			start = mid + 1
		else:
			end = mid
	return ind
def removals(arr, n, k):
	ans = n - 1
	arr.sort()
	for i in range(0, n):
		j = find_ind(arr[i], i, 
					n, k, arr)
		if (j != -1):
			ans = min(ans, n -
						(j - i + 1))
	return ans


Tests:
assert removals([1, 3, 4, 9, 10,11, 12, 17, 20], 9, 4) == 5
assert removals([1, 5, 6, 2, 8], 5, 2) == 3
assert removals([1, 2, 3 ,4, 5, 6], 6, 3) == 2
Task: Write a function to check whether the given key is present in the dictionary or not.

Solution:
def is_key_present(d,x):
  if x in d:
    return True
  else:
     return False


Tests:
assert is_key_present({1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60},5)==True
assert is_key_present({1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60},6)==True
assert is_key_present({1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60},10)==False
Task: Write a function to calculate the harmonic sum of n-1.

Solution:
def harmonic_sum(n):
  if n < 2:
    return 1
  else:
    return 1 / n + (harmonic_sum(n - 1))


Tests:
assert harmonic_sum(10)==2.9289682539682538
assert harmonic_sum(4)==2.083333333333333
assert harmonic_sum(7)==2.5928571428571425