codeparrot/xlcost-text-to-code huggingface dataset:
Count non | Function to reverse a number ; Store the reverse of N ; Return reverse of N ; Function to get the count of non - palindromic numbers having same first and last digit ; Store the required count ; Traverse the array ; Store reverse of arr [ i ] ; Check for palindrome ; IF non - palindromic ; Check if first and last digits are equal ; Driver Code
def revNum ( N ) : NEW_LINE INDENT x = 0 NEW_LINE while ( N ) : NEW_LINE INDENT x = x * 10 + N % 10 NEW_LINE N = N // 10 NEW_LINE DEDENT return x NEW_LINE DEDENT def ctNonPalin ( arr , N ) : NEW_LINE INDENT Res = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = revNum ( arr [ i ] ) NEW_LINE if ( x == arr [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT Res += ( arr [ i ] % 10 == N % 10 ) NEW_LINE DEDENT DEDENT return Res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 121 , 134 , 2342 , 4514 ] NEW_LINE N = len ( arr ) NEW_LINE print ( ctNonPalin ( arr , N ) ) NEW_LINE DEDENT
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | Python3 program for the above approach ; Function to find the smallest subarray with sum greater than or equal target ; DP table to store the computed subproblems ; Initialize first column with 0 ; Initialize first row with 0 ; Check for invalid condition ; Fill up the dp table ; Print the minimum length ; Driver Code
import sys NEW_LINE def minlt ( arr , target , n ) : NEW_LINE INDENT dp = [ [ - 1 for _ in range ( target + 1 ) ] \ for _ in range ( len ( arr ) + 1 ) ] NEW_LINE for i in range ( len ( arr ) + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 0 NEW_LINE DEDENT for j in range ( target + 1 ) : NEW_LINE INDENT dp [ 0 ] [ j ] = sys . maxsize NEW_LINE DEDENT for i in range ( 1 , len ( arr ) + 1 ) : NEW_LINE INDENT for j in range ( 1 , target + 1 ) : NEW_LINE INDENT if arr [ i - 1 ] > j : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i - 1 ] [ j ] ,   \ 1 + dp [ i ] [ j - arr [ i - 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ - 1 ] [ - 1 ] NEW_LINE if dp [ - 1 ] [ - 1 ] == sys . maxsize : NEW_LINE INDENT return ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return dp [ - 1 ] [ - 1 ] NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 5 , 4 , 1 ] NEW_LINE target = 11 NEW_LINE n = len ( arr ) NEW_LINE print ( minlt ( arr , target , n ) ) NEW_LINE
Minimum absolute difference of server loads | Function which returns the minimum difference of loads ; Compute the overall server load ; Stores the results of subproblems ; Fill the partition table in bottom up manner ; If i - th server is included ; If i - th server is excluded ; Server A load : total_sum - ans Server B load : ans Diff : abs ( total_sum - 2 * ans ) ; Driver Code ; Function Call
def minServerLoads ( n , servers ) : NEW_LINE INDENT totalLoad = sum ( servers ) NEW_LINE requiredLoad = totalLoad // 2 NEW_LINE dp = [ [ 0 for col in range ( requiredLoad + 1 ) ] for row in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , requiredLoad + 1 ) : NEW_LINE INDENT if servers [ i - 1 ] > j : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , servers [ i - 1 ] + dp [ i - 1 ] [ j - servers [ i - 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT return totalLoad - 2 * dp [ n ] [ requiredLoad ] NEW_LINE DEDENT N = 5 ; NEW_LINE servers = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE print ( minServerLoads ( N , servers ) ) NEW_LINE
Length of longest subarray with positive product | Function to find the length of longest subarray whose product is positive ; Stores the length of current subarray with positive product ; Stores the length of current subarray with negative product ; Stores the length of the longest subarray with positive product ; Reset the value ; If current element is positive ; Increment the length of subarray with positive product ; If at least one element is present in the subarray with negative product ; Update res ; If current element is negative ; Increment the length of subarray with negative product ; If at least one element is present in the subarray with positive product ; Update res ; Driver Code
def maxLenSub ( arr , N ) : NEW_LINE INDENT Pos = 0 NEW_LINE Neg = 0 NEW_LINE res = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT Pos = Neg = 0 NEW_LINE DEDENT elif ( arr [ i ] > 0 ) : NEW_LINE INDENT Pos += 1 NEW_LINE if ( Neg != 0 ) : NEW_LINE INDENT Neg += 1 NEW_LINE DEDENT res = max ( res , Pos ) NEW_LINE DEDENT else : NEW_LINE INDENT Pos , Neg = Neg , Pos NEW_LINE Neg += 1 NEW_LINE if ( Pos != 0 ) : NEW_LINE INDENT Pos += 1 NEW_LINE DEDENT res = max ( res , Pos ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 1 , - 2 , - 3 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxLenSub ( arr , N ) ) NEW_LINE DEDENT
Print all strings of maximum length from an array of strings | Python3 program to implement the above approach ; Function to find the length of the longest string from the given array of strings ; Traverse the array ; Stores the length of current string ; Update maximum length ; Return the maximum length ; Function to print the longest strings from the array ; Find the strings having length equals to lenn ; Print the resultant vector of strings ; Function to print all the longest strings from the array ; Find the length of longest string ; Find and print all the strings having length equals to max ; Driver Code
import sys NEW_LINE def maxLength ( arr ) : NEW_LINE INDENT lenn = - sys . maxsize - 1 NEW_LINE N = len ( arr ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT l = len ( arr [ i ] ) NEW_LINE if ( lenn < l ) : NEW_LINE INDENT lenn = l NEW_LINE DEDENT DEDENT return lenn NEW_LINE DEDENT def maxStrings ( arr , lenn ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE ans = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( lenn == len ( arr [ i ] ) ) : NEW_LINE INDENT ans . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def printStrings ( arr ) : NEW_LINE INDENT max = maxLength ( arr ) NEW_LINE maxStrings ( arr , max ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " aba " , " aa " , " ad " , " vcd " , " aba " ] NEW_LINE printStrings ( arr ) NEW_LINE DEDENT
Minimize count of flips required such that no substring of 0 s have length exceeding K | Function to return minimum number of flips required ; Base Case ; Stores the count of minimum number of flips ; Stores the count of zeros in current sub ; If current character is 0 ; Continue ongoing sub ; Start a new sub ; If k consecutive zeroes are obtained ; End segment ; Return the result ; Driver Code ; Function call
def min_flips ( strr , k ) : NEW_LINE INDENT if ( len ( strr ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 0 NEW_LINE cnt_zeros = 0 NEW_LINE for ch in strr : NEW_LINE INDENT if ( ch == '0' ) : NEW_LINE INDENT cnt_zeros += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt_zeros = 0 NEW_LINE DEDENT if ( cnt_zeros == k ) : NEW_LINE INDENT ans += 1 NEW_LINE cnt_zeros = 0 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = "11100000011" NEW_LINE k = 3 NEW_LINE print ( min_flips ( strr , k ) ) NEW_LINE DEDENT
Find all possible subarrays having product less than or equal to K | Function to return all possible subarrays having product less than or equal to K ; Store the required subarrays ; Stores the product of current subarray ; Stores the starting index of the current subarray ; Check for empty array ; Iterate over the array ; Calculate product ; If product exceeds K ; Reduce product ; Increase starting index of current subarray ; Stores the subarray elements ; Store the subarray elements ; Add the subarrays to the li ; Return the final li of subarrays ; Driver Code
def maxSubArray ( arr , n , K ) : NEW_LINE INDENT solution = [ ] NEW_LINE multi = 1 NEW_LINE start = 0 NEW_LINE if ( n <= 1 or K < 0 ) : NEW_LINE INDENT return solution NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT multi = multi * arr [ i ] NEW_LINE while ( multi > K ) : NEW_LINE INDENT multi = multi // arr [ start ] NEW_LINE start += 1 NEW_LINE DEDENT li = [ ] NEW_LINE j = i NEW_LINE while ( j >= start ) : NEW_LINE INDENT li . insert ( 0 , arr [ j ] ) NEW_LINE solution . append ( list ( li ) ) NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT return solution NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 7 , 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE K = 7 NEW_LINE v = maxSubArray ( arr , n , K ) NEW_LINE print ( v ) NEW_LINE DEDENT
Count of collisions at a point ( X , Y ) | Function to find the count of possible pairs of collisions ; Stores the time at which points reach the origin ; Calculate time for each point ; Sort the times ; Counting total collisions ; Count of elements arriving at a given point at the same time ; Driver Code ; Given set of points with speed ; Function Call
def solve ( D , N , X , Y ) : NEW_LINE INDENT T = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = D [ i ] [ 0 ] NEW_LINE y = D [ i ] [ 1 ] NEW_LINE speed = D [ i ] [ 2 ] NEW_LINE time = ( ( x * x - X * X ) + ( y * y - Y * Y ) ) / ( speed * speed ) NEW_LINE T . append ( time ) NEW_LINE DEDENT T . sort ( ) NEW_LINE i = 0 NEW_LINE total = 0 NEW_LINE while i < len ( T ) - 1 : NEW_LINE INDENT count = 1 NEW_LINE while i < len ( T ) - 1 and T [ i ] == T [ i + 1 ] : NEW_LINE INDENT count += 1 NEW_LINE i += 1 NEW_LINE DEDENT total += ( count * ( count - 1 ) ) / 2 NEW_LINE i += 1 NEW_LINE DEDENT return total NEW_LINE DEDENT N = 5 NEW_LINE D = [ [ 5 , 12 , 1 ] , [ 16 , 63 , 5 ] , [ - 10 , 24 , 2 ] , [ 7 , 24 , 2 ] , [ - 24 , 7 , 2 ] ] NEW_LINE X = 0 NEW_LINE Y = 0 NEW_LINE print ( solve ( D , N , X , Y ) ) NEW_LINE
Longest subarray forming an Arithmetic Progression ( AP ) | Function to return the length of longest subarray forming an AP ; Minimum possible length of required subarray is 2 ; Stores the length of the current subarray ; Stores the common difference of the current AP ; Stores the common difference of the previous AP ; If the common differences are found to be equal ; Continue the previous subarray ; Start a new subarray ; Update the length to store maximum length ; Update the length to store maximum length ; Return the length of the longest subarray ; Driver Code
def getMaxLength ( arr , N ) : NEW_LINE INDENT res = 2 NEW_LINE dist = 2 NEW_LINE curradj = ( arr [ 1 ] - arr [ 0 ] ) NEW_LINE prevadj = ( arr [ 1 ] - arr [ 0 ] ) NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT curradj = arr [ i ] - arr [ i - 1 ] NEW_LINE if ( curradj == prevadj ) : NEW_LINE INDENT dist += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prevadj = curradj NEW_LINE res = max ( res , dist ) NEW_LINE dist = 2 NEW_LINE DEDENT DEDENT res = max ( res , dist ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 7 , 4 , 6 , 8 , 10 , 11 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getMaxLength ( arr , N ) ) NEW_LINE DEDENT
Minimize cost to empty a given string by removing characters alphabetically | Function to find the minimum cost required to remove each character of the string in alphabetical order ; Stores the frequency of characters of the string ; Iterate through the string ; Count the number of characters smaller than the present character ; If no smaller character precedes current character ; Increase the frequency of the current character ; Return the total cost ; Given string str ; Function call
def minSteps ( str , N ) : NEW_LINE INDENT cost = 0 NEW_LINE f = [ 0 ] * 26 NEW_LINE for i in range ( N ) : NEW_LINE INDENT curr_ele = ord ( str [ i ] ) - ord ( ' a ' ) NEW_LINE smaller = 0 NEW_LINE for j in range ( curr_ele + 1 ) : NEW_LINE INDENT if ( f [ j ] ) : NEW_LINE INDENT smaller += f [ j ] NEW_LINE DEDENT DEDENT if ( smaller == 0 ) : NEW_LINE INDENT cost += ( i + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT cost += ( i - smaller + 1 ) NEW_LINE DEDENT f [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT return cost NEW_LINE DEDENT str = " abcab " NEW_LINE N = len ( str ) NEW_LINE print ( minSteps ( str , N ) ) NEW_LINE
Count of Rectangles with area K made up of only 1 s from given Binary Arrays | Function to find the subarrays of all possible lengths made up of only 1 s ; Stores the frequency of the subarrays ; Check if the previous value was also 0 ; If the previous value was 1 ; Find the subarrays of each size from 1 to count ; If A [ ] is of the form ... .111 ; Function to find the count of all possible rectangles ; Size of each of the arrays ; Stores the count of subarrays of each size consisting of only 1 s from array A [ ] ; Stores the count of subarrays of each size consisting of only 1 s from array B [ ] ; Iterating over all subarrays consisting of only 1 s in A [ ] ; If i is a factor of K , then there is a subarray of size K / i in B [ ] ; Driver Code
def findSubarrays ( a ) : NEW_LINE INDENT n = len ( a ) NEW_LINE freq = [ 0 ] * ( n + 1 ) NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT if ( count == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT value = count NEW_LINE for j in range ( 1 , count + 1 ) : NEW_LINE INDENT freq [ j ] += value NEW_LINE value -= 1 NEW_LINE DEDENT count = 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count > 0 ) : NEW_LINE INDENT value = count NEW_LINE for j in range ( 1 , count + 1 ) : NEW_LINE INDENT freq [ j ] += value NEW_LINE value -= 1 NEW_LINE DEDENT DEDENT return freq NEW_LINE DEDENT def countRectangles ( a , b , K ) : NEW_LINE INDENT n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE subA = [ ] NEW_LINE subA = findSubarrays ( a ) NEW_LINE subB = [ ] NEW_LINE subB = findSubarrays ( b ) NEW_LINE total = 0 NEW_LINE for i in range ( 1 , len ( subA ) ) : NEW_LINE INDENT if ( K % i == 0 and ( K // i ) <= m ) : NEW_LINE INDENT total = total + subA [ i ] * subB [ K // i ] NEW_LINE DEDENT DEDENT print ( total ) NEW_LINE DEDENT a = [ 0 , 0 , 1 , 1 ] NEW_LINE b = [ 1 , 0 , 1 ] NEW_LINE K = 2 NEW_LINE countRectangles ( a , b , K ) NEW_LINE
Print Longest Bitonic subsequence ( Space Optimized Approach ) | Function to print the longest bitonic subsequence ; Function to generate the longest bitonic subsequence ; Store the lengths of LIS ending at every index ; Store the lengths of LDS ending at every index ; Compute LIS for all indices ; Compute LDS for all indices ; Find the index having maximum value of lis [ i ] + lds [ i ] + 1 ; Stores the count of elements in increasing order in Bitonic subsequence ; Store the increasing subsequence ; Sort the bitonic subsequence to arrange smaller elements at the beginning ; Stores the count of elements in decreasing order in Bitonic subsequence ; Print the longest bitonic sequence ; Driver code
def printRes ( res ) : NEW_LINE INDENT n = len ( res ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def printLBS ( arr , N ) : NEW_LINE INDENT lis = [ 0 ] * N NEW_LINE lds = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT lis [ i ] = lds [ i ] = 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if arr [ j ] < arr [ i ] : NEW_LINE INDENT if lis [ i ] < lis [ j ] + 1 : NEW_LINE INDENT lis [ i ] = lis [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( N - 1 , i , - 1 ) : NEW_LINE INDENT if arr [ j ] < arr [ i ] : NEW_LINE INDENT if lds [ i ] < lds [ j ] + 1 : NEW_LINE INDENT lds [ i ] = lds [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT MaxVal = arr [ 0 ] NEW_LINE inx = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if MaxVal < lis [ i ] + lds [ i ] - 1 : NEW_LINE INDENT MaxVal = lis [ i ] + lds [ i ] - 1 NEW_LINE inx = i NEW_LINE DEDENT DEDENT ct1 = lis [ inx ] NEW_LINE res = [ ] NEW_LINE i = inx NEW_LINE while i >= 0 and ct1 > 0 : NEW_LINE INDENT if lis [ i ] == ct1 : NEW_LINE INDENT res . append ( arr [ i ] ) NEW_LINE ct1 -= 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT res . reverse ( ) NEW_LINE ct2 = lds [ inx ] - 1 NEW_LINE i = inx NEW_LINE while i < N and ct2 > 0 : NEW_LINE INDENT if lds [ i ] == ct2 : NEW_LINE INDENT res . append ( arr [ i ] ) NEW_LINE ct2 -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT printRes ( res ) NEW_LINE DEDENT arr = [ 80 , 60 , 30 , 40 , 20 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE printLBS ( arr , N ) NEW_LINE
Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible | Function to check if the array can be partitioned into atmost d subarray with sum atmost T ; Initial partition ; Current sum ; If current sum exceeds T ; Create a new partition ; If count of partitions exceed d ; Function to find the minimum possible value of T ; Stores the maximum and total sum of elements ; Maximum element ; Sum of all elements ; Calculate median T ; If atmost D partitions possible ; Check for smaller T ; Otherwise ; Check for larger T ; Print the minimum T required ; Driver code ; Function call
def possible ( T , arr , n , d ) : NEW_LINE INDENT partition = 1 ; NEW_LINE total = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT total = total + arr [ i ] ; NEW_LINE if ( total > T ) : NEW_LINE INDENT partition = partition + 1 ; NEW_LINE total = arr [ i ] ; NEW_LINE if ( partition > d ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT DEDENT return True ; NEW_LINE DEDENT def calcT ( n , d , arr ) : NEW_LINE INDENT mx = - 1 ; sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mx = max ( mx , arr [ i ] ) ; NEW_LINE sum = sum + arr [ i ] ; NEW_LINE DEDENT lb = mx ; NEW_LINE ub = sum ; NEW_LINE while ( lb < ub ) : NEW_LINE INDENT T_mid = lb + ( ub - lb ) // 2 ; NEW_LINE if ( possible ( T_mid , arr , n , d ) == True ) : NEW_LINE INDENT ub = T_mid ; NEW_LINE DEDENT else : NEW_LINE INDENT lb = T_mid + 1 ; NEW_LINE DEDENT DEDENT print ( lb ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT d = 2 ; NEW_LINE arr = [ 1 , 1 , 1 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE calcT ( n , d , arr ) ; NEW_LINE DEDENT
Minimize splits to generate monotonous Substrings from given String | Function to return final result ; Initialize variable to keep track of ongoing sequence ; If current sequence is neither increasing nor decreasing ; If prev char is greater ; If prev char is same ; Otherwise ; If current sequence is Non - Decreasing ; If prev char is smaller ; If prev char is same ; Update ongoing ; Otherwise ; Update ongoing ; Increase count ; If current sequence is Non - Increasing ; If prev char is greater , then update ongoing with D ; If prev char is equal , then update current with D ; Otherwise , update ongoing with N and increment count ; Return count + 1 ; Driver code
def minReqSubstring ( s , n ) : NEW_LINE INDENT ongoing = ' N ' NEW_LINE count , l = 0 , len ( s ) NEW_LINE for i in range ( 1 , l ) : NEW_LINE INDENT if ongoing == ' N ' : NEW_LINE INDENT if s [ i ] < s [ i - 1 ] : NEW_LINE INDENT ongoing = ' D ' NEW_LINE DEDENT elif s [ i ] == s [ i - 1 ] : NEW_LINE INDENT ongoing = ' N ' NEW_LINE DEDENT else : NEW_LINE INDENT ongoing = ' I ' NEW_LINE DEDENT DEDENT elif ongoing == ' I ' : NEW_LINE INDENT if s [ i ] > s [ i - 1 ] : NEW_LINE INDENT ongoing = ' I ' NEW_LINE DEDENT elif s [ i ] == s [ i - 1 ] : NEW_LINE INDENT ongoing = ' I ' NEW_LINE DEDENT else : NEW_LINE INDENT ongoing = ' N ' NEW_LINE count += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if s [ i ] < s [ i - 1 ] : NEW_LINE INDENT ongoing = ' D ' NEW_LINE DEDENT elif s [ i ] == s [ i - 1 ] : NEW_LINE INDENT ongoing = ' D ' NEW_LINE DEDENT else : NEW_LINE INDENT ongoing = ' N ' NEW_LINE count += 1 NEW_LINE DEDENT DEDENT DEDENT return count + 1 NEW_LINE DEDENT S = " aeccdhba " NEW_LINE n = len ( S ) NEW_LINE print ( minReqSubstring ( S , n ) ) NEW_LINE
Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K | Function to calculate the minimum number of operations required ; Stores the total number of operations ; Iterate over the array ; If the sum of pair of adjacent elements exceed k . ; If current element exceeds k ; Reduce arr [ i ] to k ; Update arr [ i + 1 ] accordingly ; Update answer ; Driver Code
def minimum_required_operations ( arr , n , k ) : NEW_LINE INDENT answer = 0 NEW_LINE mod = 10 ** 9 + 7 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if arr [ i ] + arr [ i + 1 ] > k : NEW_LINE INDENT if arr [ i ] > k : NEW_LINE INDENT answer += ( arr [ i ] - k ) NEW_LINE arr [ i ] = k NEW_LINE DEDENT answer += ( arr [ i ] + arr [ i + 1 ] ) - k NEW_LINE arr [ i + 1 ] = ( k - arr [ i ] ) NEW_LINE answer %= mod NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT a = [ 9 , 50 , 4 , 14 , 42 , 89 ] NEW_LINE k = 10 NEW_LINE print ( minimum_required_operations ( a , len ( a ) , k ) ) NEW_LINE
Lexicographic smallest permutation of a String containing the second String as a Substring | Function to print the desired lexicographic smaller string ; Calculate length of the string ; Stores the frequencies of characters of string str1 ; Stores the frequencies of characters of string str2 ; Decrease the frequency of second string from that of of the first string ; To store the resultant string ; To find the index of first character of the string str2 ; Append the characters in lexicographical order ; Append all the current character ( i + ' a ' ) ; If we reach first character of string str2 append str2 ; Return the resultant string ; Driver code
def findSmallestString ( str1 , str2 ) : NEW_LINE INDENT freq1 = [ 0 ] * 26 NEW_LINE freq2 = [ 0 ] * 26 NEW_LINE n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT freq1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( n2 ) : NEW_LINE INDENT freq2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT freq1 [ i ] -= freq2 [ i ] NEW_LINE DEDENT res = " " NEW_LINE minIndex = ord ( str2 [ 0 ] ) - ord ( ' a ' ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT for j in range ( freq1 [ i ] ) : NEW_LINE INDENT res += chr ( i + ord ( ' a ' ) ) NEW_LINE DEDENT if i == minIndex : NEW_LINE INDENT res += str2 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT str1 = " geeksforgeeksfor " NEW_LINE str2 = " for " NEW_LINE print ( findSmallestString ( str1 , str2 ) ) NEW_LINE
Restore original String from given Encrypted String by the given operations | Function to decrypt and print the original strings ; If length is exceeded ; Reverse the string ; Driver Code
def decryptString ( s , N ) : NEW_LINE INDENT for i in range ( 0 , len ( s ) , 2 * N ) : NEW_LINE INDENT if ( i + N < len ( s ) ) : NEW_LINE INDENT end = s [ i + N ] NEW_LINE DEDENT if ( i + N > len ( s ) ) : NEW_LINE INDENT end = s [ - 1 ] NEW_LINE DEDENT if ( i == 0 ) : NEW_LINE INDENT s = s [ i + N - 1 : : - 1 ] + s [ i + N : ] NEW_LINE DEDENT else : NEW_LINE INDENT s = s [ : i ] + s [ i + N - 1 : i - 1 : - 1 ] + s [ i + N : ] NEW_LINE DEDENT DEDENT print ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ihTs ▁ suohld ▁ ebeas ! y " NEW_LINE N = 3 NEW_LINE decryptString ( s , N ) NEW_LINE DEDENT