codeparrot/xlcost-text-to-code huggingface dataset:
Smallest element present in every subarray of all possible lengths | Function to print the common elements for all subarray lengths ; Function to find and store the minimum element present in all subarrays of all lengths from 1 to n ; Skip lengths for which answer [ i ] is - 1 ; Initialize minimum as the first element where answer [ i ] is not - 1 ; Updating the answer array ; If answer [ i ] is - 1 , then minimum can be substituted in that place ; Find minimum answer ; Function to find the minimum number corresponding to every subarray of length K , for every K from 1 to N ; Stores the minimum common elements for all subarray lengths Initialize with - 1. ; Find for every element , the minimum length such that the number is present in every subsequence of that particular length or more ; To store first occurence and gaps between occurences ; To cover the distance between last occurence and the end of the array ; To find the distance between any two occurences ; Update and store the answer ; Print the required answer ; Function to find the smallest element common in all subarrays for every possible subarray lengths ; Initializing indices array ; Store the numbers present in the array ; Push the index in the indices [ A [ i ] ] and also store the numbers in set to get the numbers present in input array ; Function call to calculate length of subarray for which a number is present in every subarray of that length ; Given array ; Size of array ; Function Call
def printAnswer ( answer , N ) : NEW_LINE INDENT for i in range ( N + 1 ) : NEW_LINE INDENT print ( answer [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def updateAnswerArray ( answer , N ) : NEW_LINE INDENT i = 0 NEW_LINE while ( answer [ i ] == - 1 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT minimum = answer [ i ] NEW_LINE while ( i <= N ) : NEW_LINE INDENT if ( answer [ i ] == - 1 ) : NEW_LINE INDENT answer [ i ] = minimum NEW_LINE DEDENT else : NEW_LINE INDENT answer [ i ] = min ( minimum , answer [ i ] ) NEW_LINE DEDENT minimum = min ( minimum , answer [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def lengthOfSubarray ( indices , st , N ) : NEW_LINE INDENT answer = [ - 1 for i in range ( N + 1 ) ] NEW_LINE for itr in st : NEW_LINE INDENT start = - 1 NEW_LINE gap = - 1 NEW_LINE indices [ itr ] . append ( N ) NEW_LINE for i in range ( len ( indices [ itr ] ) ) : NEW_LINE INDENT gap = max ( gap , indices [ itr ] [ i ] - start ) NEW_LINE start = indices [ itr ] [ i ] NEW_LINE DEDENT if ( answer [ gap ] == - 1 ) : NEW_LINE INDENT answer [ gap ] = itr NEW_LINE DEDENT DEDENT updateAnswerArray ( answer , N ) NEW_LINE printAnswer ( answer , N ) NEW_LINE DEDENT def smallestPresentNumber ( arr , N ) : NEW_LINE INDENT indices = [ [ ] for i in range ( N + 1 ) ] NEW_LINE elements = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT indices [ arr [ i ] ] . append ( i ) NEW_LINE elements . append ( arr [ i ] ) NEW_LINE DEDENT elements = list ( set ( elements ) ) NEW_LINE lengthOfSubarray ( indices , elements , N ) NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE smallestPresentNumber ( arr , N ) NEW_LINE
Minimize remaining array element by removing pairs and replacing them by their absolute difference | function to find the smallest element left in the array by the given operations ; Base Case ; If this subproblem has occurred previously ; Including i - th array element into the first subset ; If i - th array element is not selected ; Update dp [ i ] [ sum ] ; Utility function to find smallest element left in the array by the given operations ; Stores sum of the array elements ; Traverse the array ; Update total ; Stores overlapping subproblems ; Driver Code
def smallestLeft ( arr , total , sum , i , dp ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return abs ( total - 2 * sum ) NEW_LINE DEDENT if ( dp [ i ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ sum ] NEW_LINE DEDENT X = smallestLeft ( arr , total , sum + arr [ i - 1 ] , i - 1 , dp ) NEW_LINE Y = smallestLeft ( arr , total , sum , i - 1 , dp ) NEW_LINE dp [ i ] [ sum ] = min ( X , Y ) NEW_LINE return dp [ i ] [ sum ] NEW_LINE DEDENT def UtilSmallestElement ( arr , N ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT total += arr [ i ] NEW_LINE DEDENT dp = [ [ - 1 for y in range ( total ) ] for x in range ( N + 1 ) ] NEW_LINE print ( smallestLeft ( arr , total , 0 , N , dp ) ) NEW_LINE DEDENT arr = [ 2 , 7 , 4 , 1 , 8 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE UtilSmallestElement ( arr , N ) NEW_LINE
Minimize remaining array element by removing pairs and replacing them by their absolute difference | Function to find minimize the remaining array element by removing pairs and replacing them by their absolute difference ; Stores sum of array elements ; Traverse the array ; Update totalSum ; Stores half of totalSum ; dp [ i ] : True if sum i can be obtained as a subset sum ; Base case ; Stores closest sum that can be obtained as a subset sum ; Traverse the array ; Iterate over all possible value of sum ; Update dp [ j ] ; If sum i can be obtained from array elements ; Update reach ; Driver Code
def SmallestElementLeft ( arr , N ) : NEW_LINE INDENT totalSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE DEDENT req = totalSum // 2 NEW_LINE dp = [ False for i in range ( req + 1 ) ] NEW_LINE memset ( dp , false , sizeof ( dp ) ) ; NEW_LINE dp [ 0 ] = True NEW_LINE reach = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT j = req NEW_LINE while j >= arr [ i ] : NEW_LINE INDENT dp [ j ] = dp [ j ] or dp [ j - arr [ i ] ] NEW_LINE if ( dp [ j ] ) : NEW_LINE INDENT reach = max ( reach , j ) NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT DEDENT return totalSum - ( 2 * reach ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 2 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( SmallestElementLeft ( arr , N ) ) NEW_LINE DEDENT
Replace even | Function to count the minimum number of substrings of str1 such that replacing even - indexed characters of those substrings converts the str1 to str2 ; Stores length of str1 ; Stores minimum count of operations to convert str1 to str2 ; Traverse both the given string ; If current character in both the strings are equal ; Stores current index of the string ; If current character in both the strings are not equal ; Replace str1 [ ptr ] by str2 [ ptr ] ; Update ptr ; Update cntOp ; Driver Code
def minOperationsReq ( str11 , str22 ) : NEW_LINE INDENT str1 = list ( str11 ) NEW_LINE str2 = list ( str22 ) NEW_LINE N = len ( str1 ) NEW_LINE cntOp = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( str1 [ i ] == str2 [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT ptr = i NEW_LINE while ( ptr < N and str1 [ ptr ] != str2 [ ptr ] ) : NEW_LINE INDENT str1 [ ptr ] = str2 [ ptr ] NEW_LINE ptr += 2 NEW_LINE DEDENT cntOp += 1 NEW_LINE DEDENT return cntOp NEW_LINE DEDENT str1 = " abcdef " NEW_LINE str2 = " ffffff " NEW_LINE print ( minOperationsReq ( str1 , str2 ) ) NEW_LINE
Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not | Python3 program for the above approach ; Function to check if a number is composite or not ; Corner cases ; Check if the number is divisible by 2 or 3 or not ; Check if n is a multiple of any other prime number ; Function to check if a number is a Perfect Square or not ; Function to check if a number is a Fibonacci number or not ; If 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both are perfect square ; Function to check if GCD of composite numbers from the array a [ ] which are divisible by k is a Fibonacci number or not ; Traverse the array ; If array element is composite and divisible by k ; Calculate GCD of all elements in compositeset ; If GCD is Fibonacci ; Driver Code
import math NEW_LINE def isComposite ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n <= 3 : NEW_LINE INDENT return False NEW_LINE DEDENT if n % 2 == 0 or n % 3 == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT i = 5 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( ( n % i == 0 ) or ( n % ( i + 2 ) == 0 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return False NEW_LINE DEDENT def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( math . sqrt ( x ) ) NEW_LINE return ( s * s == x ) NEW_LINE DEDENT def isFibonacci ( n ) : NEW_LINE INDENT return ( isPerfectSquare ( 5 * n * n + 4 ) or isPerfectSquare ( 5 * n * n - 4 ) ) NEW_LINE DEDENT def ifgcdFibonacci ( a , n , k ) : NEW_LINE INDENT compositeset = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isComposite ( a [ i ] ) and a [ i ] % k == 0 ) : NEW_LINE INDENT compositeset . append ( a [ i ] ) NEW_LINE DEDENT DEDENT gcd = compositeset [ 0 ] NEW_LINE for i in range ( 1 , len ( compositeset ) , 1 ) : NEW_LINE INDENT gcd = math . gcd ( gcd , compositeset [ i ] ) NEW_LINE if gcd == 1 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( isFibonacci ( gcd ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 34 , 2 , 4 , 8 , 5 , 7 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE ifgcdFibonacci ( arr , n , k ) NEW_LINE DEDENT
Longest subarray in which all elements are a factor of K | Function to find the length of the longest subarray in which all elements are a factor of K ; Stores length of the longest subarray in which all elements are a factor of K ; Stores length of the current subarray ; Traverse the array arr [ ] ; If A [ i ] is a factor of K ; Update Len ; Update MaxLen ; If A [ i ] is not a factor of K ; Reset Len ; Driver Code
def find_longest_subarray ( A , N , K ) : NEW_LINE INDENT MaxLen = 0 NEW_LINE Len = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( K % A [ i ] == 0 ) : NEW_LINE INDENT Len += 1 NEW_LINE MaxLen = max ( MaxLen , Len ) NEW_LINE DEDENT else : NEW_LINE INDENT Len = 0 NEW_LINE DEDENT DEDENT return MaxLen NEW_LINE DEDENT A = [ 2 , 8 , 3 , 10 , 6 , 7 , 4 , 9 ] NEW_LINE N = len ( A ) NEW_LINE K = 60 NEW_LINE print ( find_longest_subarray ( A , N , K ) ) NEW_LINE
Length of smallest meeting that can be attended | Python3 program to implement the above approach ; Function to find the minimum time to attend exactly one meeting ; Stores minimum time to attend exactly one meeting ; Sort entrance [ ] array ; Sort exit [ ] time ; Traverse meeting [ ] [ ] ; Stores start time of current meeting ; Stores end time of current meeting ; Find just greater value of u in entrance [ ] ; Find just greater or equal value of u in entrance [ ] ; Stores enter time to attend the current meeting ; Stores exist time after attending the meeting ; Update start lies in range [ 0 , m - 1 ] and end lies in the range [ 0 , p - 1 ] ; Update ans ; Return answer ; Driver Code ; Stores interval of meeting ; Stores entrance timings ; Stores exit timings ; Stores total count of meetings ; Stores total entrance timings ; Stores total exit timings ; Minimum time
from bisect import bisect_left , bisect_right NEW_LINE import sys NEW_LINE def minTime ( meeting , n , entrance , m , exit , p ) : NEW_LINE INDENT ans = sys . maxsize NEW_LINE entrance = sorted ( entrance ) NEW_LINE exit = sorted ( exit ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT u = meeting [ i ] [ 0 ] NEW_LINE v = meeting [ i ] [ 1 ] NEW_LINE it1 = bisect_right ( entrance , u ) NEW_LINE it2 = bisect_left ( exit , v ) NEW_LINE start = it1 - 1 NEW_LINE end = it2 NEW_LINE if ( start >= 0 and start < m and end >= 0 and end < p ) : NEW_LINE INDENT ans = min ( ans , exit [ end ] - entrance [ start ] ) NEW_LINE DEDENT DEDENT if ans >= sys . maxsize : NEW_LINE INDENT ans = - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT meeting = [ [ 15 , 19 ] , [ 5 , 10 ] , [ 7 , 25 ] ] NEW_LINE entrance = [ 4 , 13 , 25 , 2 ] NEW_LINE exit = [ 10 , 25 ] NEW_LINE n = len ( meeting ) NEW_LINE m = len ( entrance ) NEW_LINE p = len ( exit ) NEW_LINE print ( minTime ( meeting , n , entrance , m , exit , p ) ) NEW_LINE DEDENT
Divide array into two arrays which does not contain any pair with sum K | Function to split the given array into two separate arrays satisfying given condition ; Stores resultant arrays ; Traverse the array ; If a [ i ] is smaller than or equal to k / 2 ; Print first array ; Print second array ; Driver Code ; Given K ; Given array ; Given size
def splitArray ( a , n , k ) : NEW_LINE INDENT first = [ ] NEW_LINE second = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] <= k // 2 ) : NEW_LINE INDENT first . append ( a [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT second . append ( a [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( first ) ) : NEW_LINE INDENT print ( first [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , ▁ end ▁ = ▁ " " ) NEW_LINE for i in range ( len ( second ) ) : NEW_LINE INDENT print ( second [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 5 NEW_LINE a = [ 0 , 1 , 3 , 2 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] NEW_LINE n = len ( a ) NEW_LINE splitArray ( a , n , k ) NEW_LINE DEDENT
Count numbers in a given range whose count of prime factors is a Prime Number | Python3 program to implement the above approach ; Function to find the smallest prime factor of all the numbers in range [ 0 , MAX ] ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; No smallest prime factor of 0 and 1 exists ; Traverse all the numbers in the range [ 1 , MAX ] ; Update spf [ i ] ; Update all the numbers whose smallest prime factor is 2 ; Traverse all the numbers in the range [ 1 , sqrt ( MAX ) ] ; Check if i is a prime number ; Update all the numbers whose smallest prime factor is i ; Check if j is a prime number ; Function to find count of prime factor of num ; Stores count of prime factor of num ; Calculate count of prime factor ; Update count ; Update num ; Function to precalculate the count of numbers in the range [ 0 , i ] whose count of prime factors is a prime number ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; Traverse all the numbers in the range [ 1 , MAX ] ; Stores count of prime factor of i ; If count of prime factor is a prime number ; Update sum [ i ] ; Update sum [ i ] ; Driver code ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; N = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ;
MAX = 1001 NEW_LINE def sieve ( ) : NEW_LINE INDENT global MAX NEW_LINE spf = [ 0 ] * MAX NEW_LINE spf [ 0 ] = spf [ 1 ] = - 1 NEW_LINE for i in range ( 2 , MAX ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAX , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT for i in range ( 3 , MAX ) : NEW_LINE INDENT if ( spf [ i ] == i ) : NEW_LINE INDENT for j in range ( i * i , MAX ) : NEW_LINE INDENT if ( spf [ j ] == j ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT return spf NEW_LINE DEDENT def countFactors ( spf , num ) : NEW_LINE INDENT count = 0 NEW_LINE while ( num > 1 ) : NEW_LINE INDENT count += 1 NEW_LINE num = num // spf [ num ] NEW_LINE DEDENT return count NEW_LINE DEDENT def precalculateSum ( spf ) : NEW_LINE INDENT sum = [ 0 ] * MAX NEW_LINE for i in range ( 1 , MAX ) : NEW_LINE INDENT prime_factor = countFactors ( spf , i ) NEW_LINE if ( spf [ prime_factor ] == prime_factor ) : NEW_LINE INDENT sum [ i ] = sum [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum [ i ] = sum [ i - 1 ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT spf = sieve ( ) NEW_LINE sum = precalculateSum ( spf ) NEW_LINE Q = [ [ 4 , 8 ] , [ 30 , 32 ] ] NEW_LINE sum [ Q [ 0 ] [ 1 ] ] += 1 NEW_LINE for i in range ( 0 , 2 ) : NEW_LINE INDENT print ( ( sum [ Q [ i ] [ 1 ] ] - sum [ Q [ i ] [ 0 ] ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Maximum number of Perfect Numbers present in a subarray of size K | Function to check a number is Perfect Number or not ; Stores sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to N ; Function to return maximum sum of a subarray of size K ; If k is greater than N ; Compute sum of first window of size K ; Compute sums of remaining windows by removing first element of previous window and adding last element of current window ; Return the answer ; Function to find all the perfect numbers in the array ; The given array is converted into binary array ; Driver Code
def isPerfect ( N ) : NEW_LINE INDENT sum = 1 NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if i * i > N : NEW_LINE INDENT break NEW_LINE DEDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( i == N // i ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT else : NEW_LINE INDENT sum += i + N // i NEW_LINE DEDENT DEDENT DEDENT if ( sum == N and N != 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def maxSum ( arr , N , K ) : NEW_LINE INDENT if ( N < K ) : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE return - 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT res += arr [ i ] NEW_LINE DEDENT curr_sum = res NEW_LINE for i in range ( K , N ) : NEW_LINE INDENT curr_sum += arr [ i ] - arr [ i - K ] NEW_LINE res = max ( res , curr_sum ) NEW_LINE DEDENT return res NEW_LINE DEDENT def max_PerfectNumbers ( arr , N , K ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if isPerfect ( arr [ i ] ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = 0 NEW_LINE DEDENT DEDENT return maxSum ( arr , N , K ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 28 , 2 , 3 , 6 , 496 , 99 , 8128 , 24 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE print ( max_PerfectNumbers ( arr , N , K ) ) NEW_LINE DEDENT
Count greater elements on the left side of every array element | Function to print the count of greater elements on left of each array element ; Function to get the count of greater elements on left of each array element ; Store distinct array elements in sorted order ; Stores the count of greater elements on the left side ; Traverse the array ; Insert array elements into the set ; Find previous greater element ; Find the distance between the previous greater element of arr [ i ] and last element of the set ; Driver code
def display ( countLeftGreater , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( countLeftGreater [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def countGreater ( arr , N ) : NEW_LINE INDENT St = set ( ) NEW_LINE countLeftGreater = [ 0 ] * ( N ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT St . add ( arr [ i ] ) NEW_LINE it = 0 NEW_LINE for st in St : NEW_LINE INDENT if ( arr [ i ] < st ) : NEW_LINE INDENT break NEW_LINE DEDENT it += 1 NEW_LINE DEDENT countLeftGreater [ i ] = abs ( it - len ( St ) ) NEW_LINE DEDENT display ( countLeftGreater , N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 1 , 2 , 3 , 0 , 11 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE countGreater ( arr , N ) NEW_LINE DEDENT
Count characters to be shifted from the start or end of a string to obtain another string | Function to find the minimum cost to convert string A to string B ; Length of string ; Initialize maxlen as 0 ; Traverse the string A ; Stores the length of substrings of string A ; Traversing string B for each character of A ; Shift i pointer towards right and increment length , if A [ i ] equals B [ j ] ; If traverse till end ; Update maxlen ; Return minimum cost ; Driver Code ; Given two strings A and B ; Function call
def minCost ( A , B ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE i = 0 ; NEW_LINE maxlen = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT length = 0 ; NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( A [ i ] == B [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE length += 1 ; NEW_LINE if ( i == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT maxlen = max ( maxlen , length ) ; NEW_LINE DEDENT return n - maxlen ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " edacb " ; NEW_LINE B = " abcde " ; NEW_LINE print ( minCost ( A , B ) ) ; NEW_LINE DEDENT
Lexicographic rank of a string among all its substrings | Function to find lexicographic rank of among all its substring ; Length of string ; Traverse the given string and store the indices of each character ; Extract the index ; Store it in the vector ; Traverse the alphaIndex array lesser than the index of first character of given string ; If alphaIndex [ i ] size exceeds 0 ; Traverse over the indices ; Add count of substring equal to n - alphaIndex [ i ] [ j ] ; Store all substrings in a vector strr starting with the first character of the given string ; Insert the current character to substring ; Store the subformed ; Sort the substring in the lexicographical order ; Find the rank of given string ; Increase the rank until the given is same ; If substring is same as the given string ; Add 1 to rank of the given string ; Driver Code ; Given string ; Function call
def lexicographicRank ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE alphaIndex = [ [ ] for i in range ( 26 ) ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT x = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE alphaIndex [ x ] . append ( i ) NEW_LINE DEDENT rank = - 1 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ord ( ' a ' ) + i >= ord ( s [ 0 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT if len ( alphaIndex [ i ] ) > 0 : NEW_LINE INDENT for j in range ( len ( alphaIndex [ i ] ) ) : NEW_LINE INDENT rank = rank + ( n - alphaIndex [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT strr = [ ] NEW_LINE for i in range ( len ( alphaIndex [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] ) ) : NEW_LINE INDENT substring = [ ] NEW_LINE jj = alphaIndex [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] [ i ] NEW_LINE for j in range ( jj , n ) : NEW_LINE INDENT substring . append ( s [ j ] ) NEW_LINE strr . append ( substring ) NEW_LINE DEDENT DEDENT strr = sorted ( strr ) NEW_LINE for i in range ( len ( strr ) ) : NEW_LINE INDENT if ( strr [ i ] != s ) : NEW_LINE INDENT rank += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return rank + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " enren " NEW_LINE print ( lexicographicRank ( strr ) ) NEW_LINE DEDENT
Find H | Function to find the H - index ; Set the range for binary search ; Check if current citations is possible ; Check to the right of mid ; Update h - index ; Since current value is not possible , check to the left of mid ; Print the h - index ; citations
def hIndex ( citations , n ) : NEW_LINE INDENT hindex = 0 NEW_LINE low = 0 NEW_LINE high = n - 1 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( citations [ mid ] >= ( mid + 1 ) ) : NEW_LINE INDENT low = mid + 1 NEW_LINE hindex = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT print ( hindex ) NEW_LINE return hindex NEW_LINE DEDENT n = 5 NEW_LINE citations = [ 5 , 3 , 3 , 2 , 2 ] NEW_LINE hIndex ( citations , n ) NEW_LINE
Check if all strings of an array can be made same by interchanging characters | Function to check if all strings are equal after swap operations ; Stores the frequency of characters ; Stores the length of string ; Traverse the array ; Traverse each string ; Check if frequency of character is divisible by N ; Driver Code
def checkEqual ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * 256 NEW_LINE M = len ( arr [ 0 ] ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT hash [ ord ( arr [ i ] [ j ] ) ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 256 ) : NEW_LINE INDENT if ( hash [ i ] % N != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ " fdd " , " fhh " ] NEW_LINE N = len ( arr ) NEW_LINE if ( checkEqual ( arr , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT