codeparrot/xlcost-text-to-code huggingface dataset:
Compare two strings considering only alphanumeric characters | Function to check alphanumeric equality of both strings ; variable declaration ; Length of first string ; Length of second string ; To check each and every character of both string ; If the current character of the first string is not an alphanumeric character , increase the pointer i ; If the current character of the second string is not an alphanumeric character , increase the pointer j ; if all alphanumeric characters of both strings are same , then return true ; if any alphanumeric characters of both strings are not same , then return false ; If current character matched , increase both pointers to check the next character ; If not same , then return false ; Function to print Equal or Unequal if strings are same or not ; check alphanumeric equality of both strings ; if both are alphanumeric equal , print Equal ; otherwise print Unequal ; Driver code
def CompareAlphanumeric ( str1 , str2 ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE len1 = len ( str1 ) NEW_LINE len2 = len ( str2 ) NEW_LINE while ( i <= len1 and j <= len2 ) : NEW_LINE INDENT while ( i < len1 and ( ( ( str1 [ i ] >= ' a ' and str1 [ i ] <= ' z ' ) or ( str1 [ i ] >= ' A ' and str1 [ i ] <= ' Z ' ) or ( str1 [ i ] >= '0' and str1 [ i ] <= '9' ) ) == False ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while ( j < len2 and ( ( ( str2 [ j ] >= ' a ' and str2 [ j ] <= ' z ' ) or ( str2 [ j ] >= ' A ' and str2 [ j ] <= ' Z ' ) or ( str2 [ j ] >= '0' and str2 [ j ] <= '9' ) ) == False ) ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( i == len1 and j == len2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( str1 [ i ] != str2 [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def CompareAlphanumericUtil ( str1 , str2 ) : NEW_LINE INDENT res = CompareAlphanumeric ( str1 , str2 ) NEW_LINE if ( res == True ) : NEW_LINE INDENT print ( " Equal " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Unequal " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " Ram , ▁ Shyam " NEW_LINE str2 = " ▁ Ram ▁ - ▁ Shyam . " NEW_LINE CompareAlphanumericUtil ( str1 , str2 ) NEW_LINE str1 = " abc123" NEW_LINE str2 = "123abc " NEW_LINE CompareAlphanumericUtil ( str1 , str2 ) NEW_LINE DEDENT
Queries to print the character that occurs the maximum number of times in a given range | Function that answers all the queries ; Length of the String ; Number of queries ; Prefix array ; Iterate for all the characters ; Increase the count of the character ; Presum array for all 26 characters ; Update the prefix array ; Answer every query ; Range ; Iterate for all characters ; Times the lowercase character j occurs till r - th index ; Subtract the times it occurred till ( l - 1 ) th index ; Max times it occurs ; Print the answer ; Driver Code
def solveQueries ( Str , query ) : NEW_LINE INDENT ll = len ( Str ) NEW_LINE Q = len ( query ) NEW_LINE pre = [ [ 0 for i in range ( 256 ) ] for i in range ( ll ) ] NEW_LINE for i in range ( ll ) : NEW_LINE INDENT pre [ i ] [ ord ( Str [ i ] ) ] += 1 NEW_LINE if ( i ) : NEW_LINE INDENT for j in range ( 256 ) : NEW_LINE INDENT pre [ i ] [ j ] += pre [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( Q ) : NEW_LINE INDENT l = query [ i ] [ 0 ] NEW_LINE r = query [ i ] [ 1 ] NEW_LINE maxi = 0 NEW_LINE c = ' a ' NEW_LINE for j in range ( 256 ) : NEW_LINE INDENT times = pre [ r ] [ j ] NEW_LINE if ( l ) : NEW_LINE INDENT times -= pre [ l - 1 ] [ j ] NEW_LINE DEDENT if ( times > maxi ) : NEW_LINE INDENT maxi = times NEW_LINE c = chr ( j ) NEW_LINE DEDENT DEDENT print ( " Query ▁ " , i + 1 , " : ▁ " , c ) NEW_LINE DEDENT DEDENT Str = " striver " NEW_LINE query = [ [ 0 , 1 ] , [ 1 , 6 ] , [ 5 , 6 ] ] NEW_LINE solveQueries ( Str , query ) NEW_LINE
Check whether given string can be generated after concatenating given strings | Function that return true if pre is a prefix of str ; While there are characters to match ; If characters differ at any position ; str starts with pre ; Function that return true if suff is a suffix of str ; While there are characters to match ; If characters differ at any position ; str ends with suff ; Function that returns true if str = a + b or str = b + a ; str cannot be generated by concatenating a and b ; If str starts with a i . e . a is a prefix of str ; Check if the rest of the characters are equal to b i . e . b is a suffix of str ; If str starts with b i . e . b is a prefix of str ; Check if the rest of the characters are equal to a i . e . a is a suffix of str ; Driver code
def startsWith ( str , pre ) : NEW_LINE INDENT strLen = len ( str ) NEW_LINE preLen = len ( pre ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while ( i < strLen and j < preLen ) : NEW_LINE INDENT if ( str [ i ] != pre [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def endsWith ( str , suff ) : NEW_LINE INDENT i = len ( str ) - 1 NEW_LINE j = len ( suff ) - 1 NEW_LINE while ( i >= 0 and j >= 0 ) : NEW_LINE INDENT if ( str [ i ] != suff [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def checkString ( str , a , b ) : NEW_LINE INDENT if ( len ( str ) != len ( a ) + len ( b ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( startsWith ( str , a ) ) : NEW_LINE INDENT if ( endsWith ( str , b ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT if ( startsWith ( str , b ) ) : NEW_LINE INDENT if ( endsWith ( str , a ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT str = " GeeksforGeeks " NEW_LINE a = " Geeksfo " NEW_LINE b = " rGeeks " NEW_LINE if ( checkString ( str , a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Print characters having odd frequencies in order of occurrence | Python3 implementation of the approach ; Function to print the odd frequency characters in the order of their occurrence ; To store the frequency of each of the character of the string and Initialize all elements of freq [ ] to 0 ; Update the frequency of each character ; Traverse str character by character ; If frequency of current character is odd ; Driver code
import sys NEW_LINE import math NEW_LINE def printChar ( str_ , n ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( str_ [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( freq [ ord ( str_ [ i ] ) - ord ( ' a ' ) ] ) % 2 == 1 : NEW_LINE INDENT print ( " { } " . format ( str_ [ i ] ) , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str_ = " geeksforgeeks " NEW_LINE n = len ( str_ ) NEW_LINE printChar ( str_ , n ) NEW_LINE DEDENT
Minimum number of operations to move all uppercase characters before all lower case characters | Function to return the minimum number of operations required ; To store the indices of the last uppercase and the first lowercase character ; Find the last uppercase character ; Find the first lowercase character ; If all the characters are either uppercase or lowercase ; Count of uppercase characters that appear after the first lowercase character ; Count of lowercase characters that appear before the last uppercase character ; Return the minimum operations required ; Driver Code
def minOperations ( str , n ) : NEW_LINE INDENT lastUpper = - 1 NEW_LINE firstLower = - 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] . isupper ( ) ) : NEW_LINE INDENT lastUpper = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] . islower ( ) ) : NEW_LINE INDENT firstLower = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( lastUpper == - 1 or firstLower == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT countUpper = 0 NEW_LINE for i in range ( firstLower , n ) : NEW_LINE INDENT if ( str [ i ] . isupper ( ) ) : NEW_LINE INDENT countUpper += 1 NEW_LINE DEDENT DEDENT countLower = 0 NEW_LINE for i in range ( lastUpper ) : NEW_LINE INDENT if ( str [ i ] . islower ( ) ) : NEW_LINE INDENT countLower += 1 NEW_LINE DEDENT DEDENT return min ( countLower , countUpper ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geEksFOrGEekS " NEW_LINE n = len ( str ) NEW_LINE print ( minOperations ( str , n ) ) NEW_LINE DEDENT
Find the sum of all Betrothed numbers up to N | Python3 program to find the Sum of the all Betrothed numbers up to N ; Function to find the Sum of the all Betrothed numbers ; To store the Betrothed numbers ; Calculate sum of number_1 's divisors ; i = 2 because we don 't  want to include  1 as a divisor. ; Sum all Betrothed numbers up to N ; Driver Code
import math NEW_LINE def Betrothed_Sum ( n ) : NEW_LINE INDENT Set = [ ] NEW_LINE for number_1 in range ( 1 , n ) : NEW_LINE INDENT sum_divisor_1 = 1 NEW_LINE i = 2 NEW_LINE while i * i <= number_1 : NEW_LINE INDENT if ( number_1 % i == 0 ) : NEW_LINE INDENT sum_divisor_1 = sum_divisor_1 + i NEW_LINE if ( i * i != number_1 ) : NEW_LINE INDENT sum_divisor_1 += number_1 // i NEW_LINE DEDENT DEDENT i = i + 1 NEW_LINE DEDENT if ( sum_divisor_1 > number_1 ) : NEW_LINE INDENT number_2 = sum_divisor_1 - 1 NEW_LINE sum_divisor_2 = 1 NEW_LINE j = 2 NEW_LINE while j * j <= number_2 : NEW_LINE INDENT if ( number_2 % j == 0 ) : NEW_LINE INDENT sum_divisor_2 += j NEW_LINE if ( j * j != number_2 ) : NEW_LINE INDENT sum_divisor_2 += number_2 // j NEW_LINE DEDENT DEDENT j = j + 1 NEW_LINE DEDENT if ( sum_divisor_2 == number_1 + 1 and number_1 <= n and number_2 <= n ) : NEW_LINE INDENT Set . append ( number_1 ) NEW_LINE Set . append ( number_2 ) NEW_LINE DEDENT DEDENT DEDENT Summ = 0 NEW_LINE for i in Set : NEW_LINE INDENT if i <= n : NEW_LINE INDENT Summ += i NEW_LINE DEDENT DEDENT return Summ NEW_LINE DEDENT n = 78 NEW_LINE print ( Betrothed_Sum ( n ) ) NEW_LINE
Probability of rain on N + 1 th day | Function to find the probability ; count occurence of 1 ; find probability ; Driver code
def rainDayProbability ( a , n ) : NEW_LINE INDENT count = a . count ( 1 ) NEW_LINE m = count / n NEW_LINE return m NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( rainDayProbability ( a , n ) ) NEW_LINE DEDENT
Program to find the sum of a Series 1 + 1 / 2 ^ 2 + 1 / 3 ^ 3 + â €¦ . . + 1 / n ^ n | Function to calculate the following series ; Driver Code
def Series ( n ) : NEW_LINE INDENT sums = 0.0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ser = 1 / ( i ** i ) NEW_LINE sums += ser NEW_LINE DEDENT return sums NEW_LINE DEDENT n = 3 NEW_LINE res = round ( Series ( n ) , 5 ) NEW_LINE print ( res ) NEW_LINE
Lexicographically largest string formed in minimum moves by replacing characters of given String | Function to print the lexicographically the largest string obtained in process of obtaining a string containing first N lower case english alphabtes ; Store the frequency of each character ; Traverse the string S ; Stores the characters which are not appearing in S ; Stores the index of the largest character in the array V , that need to be replaced ; Traverse the string , S ; If frequency of S [ i ] is greater than 1 or it is outside the range ; Decrement its frequency by 1 ; Update S [ i ] ; Decrement j by 1 ; Traverse the string , S ; Decrement its frequency by 1 ; Update S [ i ] ; Increment l by 1 ; Return S ; Given Input ; Function Call
def lexicographicallyMaximum ( S , N ) : NEW_LINE INDENT M = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if S [ i ] in M : NEW_LINE M [ S [ i ] ] += 1 NEW_LINE else : NEW_LINE INDENT M [ S [ i ] ] = 1 NEW_LINE DEDENT DEDENT V = [ ] NEW_LINE for i in range ( ord ( ' a ' ) , ord ( ' a ' ) + min ( N , 25 ) ) : NEW_LINE INDENT if i not in M : NEW_LINE INDENT V . append ( chr ( i ) ) NEW_LINE DEDENT DEDENT j = len ( V ) - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( ord ( S [ i ] ) >= ( ord ( ' a ' ) + min ( N , 25 ) ) or ( S [ i ] in M and M [ S [ i ] ] > 1 ) ) : NEW_LINE INDENT if ( ord ( V [ j ] ) < ord ( S [ i ] ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT M [ S [ i ] ] -= 1 NEW_LINE S = S [ 0 : i ] + V [ j ] + S [ ( i + 1 ) : ] NEW_LINE j -= 1 NEW_LINE DEDENT if ( j < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT l = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( l > j ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( ord ( S [ i ] ) >= ( ord ( ' a ' ) + min ( N , 25 ) ) or S [ i ] in M and M [ S [ i ] ] > 1 ) : NEW_LINE INDENT M [ S [ i ] ] -= 1 NEW_LINE S = S [ 0 : i ] + V [ l ] + S [ ( i + 1 ) : ] NEW_LINE l += 1 NEW_LINE DEDENT DEDENT s = list ( S ) NEW_LINE s [ len ( s ) - 1 ] = ' d ' NEW_LINE S = " " . join ( s ) NEW_LINE return S NEW_LINE DEDENT S = " abccefghh " NEW_LINE N = len ( S ) NEW_LINE print ( lexicographicallyMaximum ( S , N ) ) NEW_LINE
Check if any subarray can be made palindromic by replacing less than half of its elements | A Utility Function to check if a subarray can be palindromic by replacing less than half of the elements present in it ; Stores frequency of array elements ; Traverse the array ; Update frequency of each array element ; Iterator over the Map ; If frequency of any element exceeds 1 ; If no repetition is found ; Function to check and print if any subarray can be made palindromic by replacing less than half of its elements ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
def isConsistingSubarrayUtil ( arr , n ) : NEW_LINE INDENT mp = { } ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in mp : NEW_LINE INDENT mp [ arr [ i ] ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 ; NEW_LINE DEDENT DEDENT for it in mp : NEW_LINE INDENT if ( mp [ it ] > 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT def isConsistingSubarray ( arr , N ) : NEW_LINE INDENT if ( isConsistingSubarrayUtil ( arr , N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 1 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE isConsistingSubarray ( arr , N ) ; NEW_LINE DEDENT
Count composite fibonacci numbers from given array | Python3 program to implement the above approach ; Function to find all Fibonacci numbers up to Max ; Store all Fibonacci numbers upto Max ; Stores previous element of Fibonacci sequence ; Stores previous element of Fibonacci sequence ; Insert prev into hashmap ; Insert all the Fibonacci numbers up to Max ; Insert curr into hashmap ; Stores curr into temp ; Update curr ; Update prev ; Function to find all Composite numbers up to Max ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Function to find the numbers which is both a composite and Fibonacci number ; Stores the largest element of the array ; Traverse the array arr [ ] ; Update Max ; isPrim [ i ] check i is a prime number or not ; Stores all the Fibonacci numbers ; Traverse the array arr [ ] ; Current element is not a composite number ; If current element is a Fibonacci and composite number ; Print current element ; Driver Code
import math NEW_LINE def createhashmap ( Max ) : NEW_LINE INDENT hashmap = { " " } NEW_LINE curr = 1 NEW_LINE prev = 0 NEW_LINE hashmap . add ( prev ) NEW_LINE while ( curr <= Max ) : NEW_LINE INDENT hashmap . add ( curr ) NEW_LINE temp = curr NEW_LINE curr = curr + prev NEW_LINE prev = temp NEW_LINE DEDENT return hashmap NEW_LINE DEDENT def SieveOfEratosthenes ( Max ) : NEW_LINE INDENT isPrime = [ 1 for x in range ( Max + 1 ) ] NEW_LINE isPrime [ 0 ] = 0 NEW_LINE isPrime [ 1 ] = 0 NEW_LINE for p in range ( 0 , int ( math . sqrt ( Max ) ) ) : NEW_LINE INDENT if ( isPrime [ p ] ) : NEW_LINE INDENT for i in range ( 2 * p , Max , p ) : NEW_LINE INDENT isPrime [ i ] = 0 NEW_LINE DEDENT DEDENT DEDENT return isPrime NEW_LINE DEDENT def cntFibonacciPrime ( arr , N ) : NEW_LINE INDENT Max = arr [ 0 ] NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT Max = max ( Max , arr [ i ] ) NEW_LINE DEDENT isPrime = SieveOfEratosthenes ( Max ) NEW_LINE hashmap = createhashmap ( Max ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if arr [ i ] == 1 : NEW_LINE INDENT continue NEW_LINE DEDENT if ( ( arr [ i ] in hashmap ) and ( not ( isPrime [ arr [ i ] ] ) ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 13 , 55 , 7 , 3 , 5 , 21 , 233 , 144 , 89 ] NEW_LINE N = len ( arr ) NEW_LINE cntFibonacciPrime ( arr , N ) NEW_LINE
Reduce a given number to form a key by the given operations | Python3 program of the above approach ; Function to find the key of the given number ; Convert the integer to String ; Iterate the num - string to get the result ; Check if digit is even or odd ; Iterate until odd sum is obtained by adding consecutive digits ; Check if sum becomes odd ; Add the result in ans ; Assign the digit index to num string ; If the number is odd ; Iterate until odd sum is obtained by adding consecutive digits ; Check if sum becomes even ; Add the result in ans ; assign the digit index to main numstring ; Check if all digits are visited or not ;
import math NEW_LINE def key ( N ) : NEW_LINE INDENT num = " " + str ( N ) NEW_LINE ans = 0 NEW_LINE j = 0 NEW_LINE while j < len ( num ) : NEW_LINE INDENT if ( ( ord ( num [ j ] ) - 48 ) % 2 == 0 ) : NEW_LINE INDENT add = 0 NEW_LINE i = j NEW_LINE while j < len ( num ) : NEW_LINE INDENT add += ord ( num [ j ] ) - 48 NEW_LINE if ( add % 2 == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( add == 0 ) : NEW_LINE INDENT ans *= 10 NEW_LINE DEDENT else : NEW_LINE INDENT digit = int ( math . floor ( math . log10 ( add ) + 1 ) ) NEW_LINE ans *= ( pow ( 10 , digit ) ) NEW_LINE ans += add NEW_LINE DEDENT i = j NEW_LINE DEDENT else : NEW_LINE INDENT add = 0 NEW_LINE i = j NEW_LINE while j < len ( num ) : NEW_LINE INDENT add += ord ( num [ j ] ) - 48 NEW_LINE if ( add % 2 == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( add == 0 ) : NEW_LINE INDENT ans *= 10 NEW_LINE DEDENT else : NEW_LINE INDENT digit = int ( math . floor ( math . log10 ( add ) + 1 ) ) NEW_LINE ans *= ( pow ( 10 , digit ) ) NEW_LINE ans += add NEW_LINE DEDENT i = j NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j + 1 ) >= len ( num ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT ans += ord ( num [ len ( num ) - 1 ] ) - 48 NEW_LINE return ans NEW_LINE DEDENT DEDENT / * Driver Code * / NEW_LINE N = 1667848271 NEW_LINE print ( key ( N ) ) NEW_LINE
Sentinel Linear Search | Python3 implementation of the approach Function to search key in the given array ; Function to search x in the given array ; Last element of the array ; Element to be searched is placed at the last index ; Put the last element back ; Driver code
def sentinelSearch ( arr , n , key ) : NEW_LINE def sentinelSearch ( arr , n , key ) : NEW_LINE INDENT last = arr [ n - 1 ] NEW_LINE arr [ n - 1 ] = key NEW_LINE i = 0 NEW_LINE while ( arr [ i ] != key ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT arr [ n - 1 ] = last NEW_LINE if ( ( i < n - 1 ) or ( arr [ n - 1 ] == key ) ) : NEW_LINE INDENT print ( key , " is ▁ present ▁ at ▁ index " , i ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Element ▁ Not ▁ found " ) NEW_LINE DEDENT DEDENT arr = [ 10 , 20 , 180 , 30 , 60 , 50 , 110 , 100 , 70 ] NEW_LINE n = len ( arr ) NEW_LINE key = 180 NEW_LINE sentinelSearch ( arr , n , key ) NEW_LINE
Maximum possible middle element of the array after deleting exactly k elements | Function to calculate maximum possible middle value of the array after deleting exactly k elements ; Initialize answer as - 1 ; Calculate range of elements that can give maximum possible middle value of the array since index of maximum possible middle value after deleting exactly k elements from array will lie in between low and high ; Find maximum element of the array in range low and high ; since indexing is 1 based so check element at index i - 1 ; Return the maximum possible middle value of the array after deleting exactly k elements from the array ; Driver Code
def maximum_middle_value ( n , k , arr ) : NEW_LINE INDENT ans = - 1 NEW_LINE low = ( n + 1 - k ) // 2 NEW_LINE high = ( n + 1 - k ) // 2 + k NEW_LINE for i in range ( low , high + 1 ) : NEW_LINE INDENT ans = max ( ans , arr [ i - 1 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 5 , 2 NEW_LINE arr = [ 9 , 5 , 3 , 7 , 10 ] NEW_LINE print ( maximum_middle_value ( n , k , arr ) ) NEW_LINE n , k = 9 , 3 NEW_LINE arr1 = [ 2 , 4 , 3 , 9 , 5 , 8 , 7 , 6 , 10 ] NEW_LINE print ( maximum_middle_value ( n , k , arr1 ) ) NEW_LINE DEDENT
Ternary Search | Python3 program to illustrate recursive approach to ternary search ; Function to perform Ternary Search ; Find the mid1 and mid2 ; Check if key is present at any mid ; Since key is not present at mid , check in which region it is present then repeat the Search operation in that region ; The key lies in between l and mid1 ; The key lies in between mid2 and r ; The key lies in between mid1 and mid2 ; Key not found ; Driver code ; Get the array Sort the array if not sorted ; Starting index ; length of array ; Key to be searched in the array ; Search the key using ternarySearch ; Print the result ; Key to be searched in the array ; Search the key using ternarySearch ; Print the result
import math as mt NEW_LINE def ternarySearch ( l , r , key , ar ) : NEW_LINE INDENT if ( r >= l ) : NEW_LINE INDENT mid1 = l + ( r - l ) // 3 NEW_LINE mid2 = r - ( r - l ) // 3 NEW_LINE if ( ar [ mid1 ] == key ) : NEW_LINE INDENT return mid1 NEW_LINE DEDENT if ( ar [ mid2 ] == key ) : NEW_LINE INDENT return mid2 NEW_LINE DEDENT if ( key < ar [ mid1 ] ) : NEW_LINE INDENT return ternarySearch ( l , mid1 - 1 , key , ar ) NEW_LINE DEDENT elif ( key > ar [ mid2 ] ) : NEW_LINE INDENT return ternarySearch ( mid2 + 1 , r , key , ar ) NEW_LINE DEDENT else : NEW_LINE INDENT return ternarySearch ( mid1 + 1 , mid2 - 1 , key , ar ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT l , r , p = 0 , 9 , 5 NEW_LINE ar = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] NEW_LINE l = 0 NEW_LINE r = 9 NEW_LINE key = 5 NEW_LINE p = ternarySearch ( l , r , key , ar ) NEW_LINE print ( " Index ▁ of " , key , " is " , p ) NEW_LINE key = 50 NEW_LINE p = ternarySearch ( l , r , key , ar ) NEW_LINE print ( " Index ▁ of " , key , " is " , p ) NEW_LINE
Minimum number of points to be removed to get remaining points on one side of axis | Function to find the minimum number of points ; Number of points on the left of Y - axis . ; Number of points on the right of Y - axis . ; Number of points above X - axis . ; Number of points below X - axis . ; Driver Code
def findmin ( p , n ) : NEW_LINE INDENT a , b , c , d = 0 , 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( p [ i ] [ 0 ] <= 0 ) : NEW_LINE INDENT a += 1 NEW_LINE DEDENT elif ( p [ i ] [ 0 ] >= 0 ) : NEW_LINE INDENT b += 1 NEW_LINE DEDENT if ( p [ i ] [ 1 ] >= 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT elif ( p [ i ] [ 1 ] <= 0 ) : NEW_LINE INDENT d += 1 NEW_LINE DEDENT DEDENT return min ( [ a , b , c , d ] ) NEW_LINE DEDENT p = [ [ 1 , 1 ] , [ 2 , 2 ] , [ - 1 , - 1 ] , [ - 2 , 2 ] ] NEW_LINE n = len ( p ) NEW_LINE print ( findmin ( p , n ) ) NEW_LINE
Maximum number of pair reductions possible on a given triplet | Function to count the maximum number of pair reductions possible on a given triplet ; Convert them into an array ; Stores count of operations ; Sort the array ; If the first two array elements reduce to 0 ; Apply the operations ; Increment count ; Print the maximum count ; Given triplet
def maxOps ( a , b , c ) : NEW_LINE INDENT arr = [ a , b , c ] NEW_LINE count = 0 NEW_LINE while True : NEW_LINE INDENT arr . sort ( ) NEW_LINE if not arr [ 0 ] and not arr [ 1 ] : NEW_LINE break NEW_LINE arr [ 1 ] -= 1 NEW_LINE arr [ 2 ] -= 1 NEW_LINE count += 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT a , b , c = 4 , 3 , 2 NEW_LINE maxOps ( a , b , c ) NEW_LINE