codeparrot/xlcost-text-to-code huggingface dataset:
Partition problem | DP | A utility function that returns true if there is a subset of arr [ ] with sun equal to given sum ; Base Cases ; If last element is greater than sum , then ignore it ; else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element ; Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of the elements in array ; If sum is odd , there cannot be two subsets with equal sum ; Find if there is subset with sum equal to half of total sum ; Driver code ; Function call
def isSubsetSum ( arr , n , sum ) : NEW_LINE INDENT if sum == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT if n == 0 and sum != 0 : NEW_LINE INDENT return False NEW_LINE DEDENT if arr [ n - 1 ] > sum : NEW_LINE INDENT return isSubsetSum ( arr , n - 1 , sum ) NEW_LINE DEDENT return isSubsetSum ( arr , n - 1 , sum ) or isSubsetSum ( arr , n - 1 , sum - arr [ n - 1 ] ) NEW_LINE DEDENT def findPartion ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if sum % 2 != 0 : NEW_LINE INDENT return false NEW_LINE DEDENT return isSubsetSum ( arr , n , sum // 2 ) NEW_LINE DEDENT arr = [ 3 , 1 , 5 , 9 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE if findPartion ( arr , n ) == True : NEW_LINE INDENT print ( " Can ▁ be ▁ divided ▁ into ▁ two ▁ subsets ▁ of ▁ equal ▁ sum " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Can ▁ not ▁ be ▁ divided ▁ into ▁ two ▁ subsets ▁ of ▁ equal ▁ sum " ) NEW_LINE DEDENT
Partition problem | DP | Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false ; Calculate sum of all elements ; Initialize the part array as 0 ; Fill the partition table in bottom up manner ; the element to be included in the sum cannot be greater than the sum ; check if sum - arr [ i ] could be formed from a subset using elements before index i ; Drive code ; Function call
def findPartiion ( arr , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE DEDENT if ( Sum % 2 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT part = [ 0 ] * ( ( Sum // 2 ) + 1 ) NEW_LINE for i in range ( ( Sum // 2 ) + 1 ) : NEW_LINE INDENT part [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( Sum // 2 , arr [ i ] - 1 , - 1 ) : NEW_LINE INDENT if ( part [ j - arr [ i ] ] == 1 or j == arr [ i ] ) : NEW_LINE INDENT part [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT return part [ Sum // 2 ] NEW_LINE DEDENT arr = [ 1 , 3 , 3 , 2 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( findPartiion ( arr , n ) == 1 ) : NEW_LINE INDENT print ( " Can ▁ be ▁ divided ▁ into ▁ two ▁ subsets ▁ of ▁ equal ▁ sum " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Can ▁ not ▁ be ▁ divided ▁ into ▁ two ▁ subsets ▁ of ▁ equal ▁ sum " ) NEW_LINE DEDENT
Binomial Coefficient | DP | Function to find binomial coefficient ; Getting the modular inversion for all the numbers from 2 to r with respect to m here m = 1000000007 ; for 1 / ( r ! ) part ; for ( n ) * ( n - 1 ) * ( n - 2 ) * ... * ( n - r + 1 ) part ; Driver code
def binomialCoeff ( n , r ) : NEW_LINE INDENT if ( r > n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT m = 1000000007 NEW_LINE inv = [ 0 for i in range ( r + 1 ) ] NEW_LINE inv [ 0 ] = 1 ; NEW_LINE if ( r + 1 >= 2 ) NEW_LINE inv [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , r + 1 ) : NEW_LINE INDENT inv [ i ] = m - ( m // i ) * inv [ m % i ] % m NEW_LINE DEDENT ans = 1 NEW_LINE for i in range ( 2 , r + 1 ) : NEW_LINE INDENT ans = ( ( ans % m ) * ( inv [ i ] % m ) ) % m NEW_LINE DEDENT for i in range ( n , n - r , - 1 ) : NEW_LINE INDENT ans = ( ( ans % m ) * ( i % m ) ) % m NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 5 NEW_LINE r = 2 NEW_LINE print ( " Value ▁ of ▁ C ( " , n , " , ▁ " , r , " ) ▁ is ▁ " , binomialCoeff ( n , r ) ) NEW_LINE
Check if it is possible to reach ( X , Y ) from ( 1 , 1 ) by given steps | Function to find the gcd of two numbers ; Base case ; Recurse ; Function to print the answer ; GCD of X and Y ; If GCD is power of 2 ; Driver code ; Given X and Y ; Function call
def gcd ( a , b ) : NEW_LINE INDENT if ( a < b ) : NEW_LINE INDENT t = a NEW_LINE a = b NEW_LINE b = t NEW_LINE DEDENT if ( a % b == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def printAnswer ( x , y ) : NEW_LINE INDENT val = gcd ( x , y ) NEW_LINE if ( ( val & ( val - 1 ) ) == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 4 NEW_LINE y = 7 NEW_LINE printAnswer ( x , y ) NEW_LINE DEDENT
Find the element in the matrix generated by given rules | Function to return the element in the rth row and cth column from the required matrix ; Condition for lower half of matrix ; Condition if element is in first row ; Starting element of AP in row r ; Common difference of AP in row r ; Position of element to find in AP in row r ; Driver Code
def getElement ( N , r , c ) : NEW_LINE INDENT if ( r > c ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( r == 1 ) : NEW_LINE INDENT return c ; NEW_LINE DEDENT a = ( r + 1 ) * pow ( 2 , r - 2 ) ; NEW_LINE d = pow ( 2 , r - 1 ) ; NEW_LINE c = c - r ; NEW_LINE element = a + d * c ; NEW_LINE return element ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; R = 3 ; C = 4 ; NEW_LINE print ( getElement ( N , R , C ) ) ; NEW_LINE DEDENT
Find smallest number formed by inserting given digit | Function to insert X in N and return the minimum value string ; Variable to store length of string N ; Variable to denote the position where X must be added ; If the given string N represent a negative value ; X must be place at the last index where is greater than N [ i ] ; For positive numbers , X must be placed at the last index where it is smaller than N [ i ] ; Insert X at that position ; Return the string ; Given Input ; Function Call
def MinValue ( N , X ) : NEW_LINE INDENT N = list ( N ) ; NEW_LINE ln = len ( N ) NEW_LINE position = ln + 1 NEW_LINE if ( N [ 0 ] == ' - ' ) : NEW_LINE INDENT for i in range ( ln - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( ( ord ( N [ i ] ) - ord ( '0' ) ) < X ) : NEW_LINE INDENT position = i NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( ln - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( ( ord ( N [ i ] ) - ord ( '0' ) ) > X ) : NEW_LINE INDENT position = i NEW_LINE DEDENT DEDENT DEDENT c = chr ( X + ord ( '0' ) ) NEW_LINE str = N . insert ( position , c ) ; NEW_LINE return ' ' . join ( N ) NEW_LINE DEDENT N = "89" NEW_LINE X = 1 NEW_LINE print ( MinValue ( N , X ) ) NEW_LINE
Check if Decimal representation of given Binary String is divisible by K or not | Function to check the binary number is divisible by K ; Array poweroftwo will store pow ( 2 , i ) % k ; Initializing the first element in Array ; Storing every pow ( 2 , i ) % k value in the array ; To store the remaining ; Iterating till N ; If current bit is 1 ; Updating rem ; If completely divisible ; If not Completely divisible ; Driver Code ; Given Input ; length of string s ; Function Call
def divisibleByk ( s , n , k ) : NEW_LINE INDENT poweroftwo = [ 0 for i in range ( n ) ] NEW_LINE poweroftwo [ 0 ] = 1 % k NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT poweroftwo [ i ] = ( poweroftwo [ i - 1 ] * ( 2 % k ) ) % k NEW_LINE DEDENT rem = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ n - i - 1 ] == '1' ) : NEW_LINE INDENT rem += ( poweroftwo [ i ] ) NEW_LINE rem %= k NEW_LINE DEDENT DEDENT if ( rem == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "1010001" NEW_LINE k = 9 NEW_LINE n = len ( s ) NEW_LINE print ( divisibleByk ( s , n , k ) ) NEW_LINE DEDENT
Count removal of pairs required to be empty all Balanced Parenthesis subsequences | Function to find the maximum count of pairs required to be removed such that subsequence of string does not contain any valid parenthesis ; Stores count of pairs of balanced parenthesis ; Stores count of curly balanced parenthesis ; Stores count of small balanced parenthesis ; Stores count of square balanced parenthesis ; Iterate over characters of the string ; Update cntCurly ; Update cntSml ; Update cntSqr ; Update cntCurly ; Update cntPairs ; Update cntSml ; Update cntPairs ; Update cntSml ; Update cntPairs ; Driver Code ; Given String ; Function call
def cntBalancedParenthesis ( s , N ) : NEW_LINE INDENT cntPairs = 0 ; NEW_LINE cntCurly = 0 ; NEW_LINE cntSml = 0 ; NEW_LINE cntSqr = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( ord ( s [ i ] ) == ord ( ' { ' ) ) : NEW_LINE INDENT cntCurly += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' ( ' ) ) : NEW_LINE INDENT cntSml += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' [ ' ) ) : NEW_LINE INDENT cntSqr += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' } ' ) and cntCurly > 0 ) : NEW_LINE INDENT cntCurly -= 1 ; NEW_LINE cntPairs += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' ) ' ) and cntSml > 0 ) : NEW_LINE INDENT cntSml -= 1 ; NEW_LINE cntPairs += 1 ; NEW_LINE DEDENT elif ( ord ( s [ i ] ) == ord ( ' ] ' ) and cntSqr > 0 ) : NEW_LINE INDENT cntSqr -= 1 ; NEW_LINE cntPairs += 1 ; NEW_LINE DEDENT DEDENT print ( cntPairs ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " { ( } ) " ; NEW_LINE N = len ( s ) ; NEW_LINE cntBalancedParenthesis ( s , N ) ; NEW_LINE DEDENT
Count of strings that does not contain Arc intersection | Function to check if there is arc intersection or not ; Traverse the string S ; Insert all the elements in the stack one by one ; Extract the top element ; Pop out the top element ; Check if the top element is same as the popped element ; Otherwise ; If the stack is empty ; Function to check if there is arc intersection or not for the given array of strings ; Stores count of string not having arc intersection ; Iterate through array ; Length of every string ; Function Call ; Print the desired count ; Driver Code ; Function Call
def arcIntersection ( S , lenn ) : NEW_LINE INDENT stk = [ ] NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT stk . append ( S [ i ] ) NEW_LINE if ( len ( stk ) >= 2 ) : NEW_LINE INDENT temp = stk [ - 1 ] NEW_LINE del stk [ - 1 ] NEW_LINE if ( stk [ - 1 ] == temp ) : NEW_LINE INDENT del stk [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT stk . append ( temp ) NEW_LINE DEDENT DEDENT DEDENT if ( len ( stk ) == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def countString ( arr , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT lenn = len ( arr [ i ] ) NEW_LINE count += arcIntersection ( arr [ i ] , lenn ) NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ "0101" , "0011" , "0110" ] NEW_LINE N = len ( arr ) NEW_LINE countString ( arr , N ) NEW_LINE DEDENT
Check if decimal representation of Binary String is divisible by 9 or not | Function to convert the binary string into octal representation ; Stores binary representation of the decimal value [ 0 - 7 ] ; Stores the decimal values of binary strings [ 0 - 7 ] ; Stores length of S ; Update S ; Update S ; Update N ; Stores octal representation of the binary string ; Traverse the binary string ; Stores 3 consecutive characters of the binary string ; Append octal representation of temp ; Function to check if binary string is divisible by 9 or not ; Stores octal representation of S ; Stores sum of elements present at odd positions of oct ; Stores sum of elements present at odd positions of oct ; Stores length of oct ; Traverse the string oct ; Update oddSum ; Traverse the string oct ; Update evenSum ; Stores cotal representation of 9 ; If absolute value of ( oddSum - evenSum ) is divisible by Oct_9 ; Driver Code
def ConvertequivalentBase8 ( S ) : NEW_LINE INDENT mp = { } NEW_LINE mp [ "000" ] = '0' NEW_LINE mp [ "001" ] = '1' NEW_LINE mp [ "010" ] = '2' NEW_LINE mp [ "011" ] = '3' NEW_LINE mp [ "100" ] = '4' NEW_LINE mp [ "101" ] = '5' NEW_LINE mp [ "110" ] = '6' NEW_LINE mp [ "111" ] = '7' NEW_LINE N = len ( S ) NEW_LINE if ( N % 3 == 2 ) : NEW_LINE INDENT S = "0" + S NEW_LINE DEDENT elif ( N % 3 == 1 ) : NEW_LINE INDENT S = "00" + S NEW_LINE DEDENT N = len ( S ) NEW_LINE octal = " " NEW_LINE for i in range ( 0 , N , 3 ) : NEW_LINE INDENT temp = S [ i : i + 3 ] NEW_LINE if temp in mp : NEW_LINE octal += ( mp [ temp ] ) NEW_LINE DEDENT return octal NEW_LINE DEDENT def binString_div_9 ( S , N ) : NEW_LINE INDENT octal = ConvertequivalentBase8 ( S ) NEW_LINE oddSum = 0 NEW_LINE evenSum = 0 NEW_LINE M = len ( octal ) NEW_LINE for i in range ( 0 , M , 2 ) : NEW_LINE INDENT oddSum += ord ( octal [ i ] ) - ord ( '0' ) NEW_LINE DEDENT for i in range ( 1 , M , 2 ) : NEW_LINE INDENT evenSum += ord ( octal [ i ] ) - ord ( '0' ) NEW_LINE DEDENT Oct_9 = 11 NEW_LINE if ( abs ( oddSum - evenSum ) % Oct_9 == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "1010001" NEW_LINE N = len ( S ) NEW_LINE print ( binString_div_9 ( S , N ) ) NEW_LINE DEDENT
Minimum cost to remove the spaces between characters of a String by rearranging the characters | Function to calculate the minimum cost ; Stores the minimum cost ; Stores the count of characters found ; Stores the count of blank spaces found ; Stores the count of total characters ; If the count of characters is equal to 1 ; Iterate over the string ; Consider the previous character together with current character ; If not together already ; Add the cost to group them together ; Increase count of characters found ; Otherwise ; Increase count of spaces found ; Return the total cost obtained ; Driver Code
def min_cost ( S ) : NEW_LINE INDENT cost = 0 NEW_LINE F = 0 NEW_LINE B = 0 NEW_LINE n = len ( S ) - S . count ( ' ▁ ' ) NEW_LINE if n == 1 : NEW_LINE INDENT return cost NEW_LINE DEDENT for char in S : NEW_LINE INDENT if char != ' ▁ ' : NEW_LINE INDENT if B != 0 : NEW_LINE INDENT cost += min ( n - F , F ) * B NEW_LINE B = 0 NEW_LINE DEDENT F += 1 NEW_LINE DEDENT else : NEW_LINE INDENT B += 1 NEW_LINE DEDENT DEDENT return cost NEW_LINE DEDENT S = " ▁ @ TABSYMBOL $ " NEW_LINE print ( min_cost ( S ) ) NEW_LINE
Minimize cost to replace all the vowels of a given String by a single vowel | Function that return true if the given character is a vowel ; Function to return the minimum cost to convert all the vowels of a string to a single one ; Stores count of respective vowels ; Iterate through the string ; If a vowel is encountered ; Calculate the cost ; Return the minimum cost ; Driver code
def isVowel ( ch ) : NEW_LINE INDENT if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def minCost ( S ) : NEW_LINE INDENT cA = 0 ; NEW_LINE cE = 0 ; NEW_LINE cI = 0 ; NEW_LINE cO = 0 ; NEW_LINE cU = 0 ; NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( isVowel ( S [ i ] ) ) : NEW_LINE INDENT cA += abs ( ord ( S [ i ] ) - ord ( ' a ' ) ) ; NEW_LINE cE += abs ( ord ( S [ i ] ) - ord ( ' e ' ) ) ; NEW_LINE cI += abs ( ord ( S [ i ] ) - ord ( ' i ' ) ) ; NEW_LINE cO += abs ( ord ( S [ i ] ) - ord ( ' o ' ) ) ; NEW_LINE cU += abs ( ord ( S [ i ] ) - ord ( ' u ' ) ) ; NEW_LINE DEDENT DEDENT return min ( min ( min ( min ( cA , cE ) , cI ) , cO ) , cU ) ; NEW_LINE DEDENT S = " geeksforgeeks " ; NEW_LINE print ( minCost ( S ) ) NEW_LINE
Generate a string whose all K | Function to return the required required string ; Iterate the given string ; Append the first character of every substring of length K ; Consider all characters from the last substring ; Driver code
def decode_String ( st , K ) : NEW_LINE INDENT ans = " " NEW_LINE for i in range ( 0 , len ( st ) , K ) : NEW_LINE INDENT ans += st [ i ] NEW_LINE DEDENT for i in range ( len ( st ) - ( K - 1 ) , len ( st ) ) : NEW_LINE INDENT ans += st [ i ] NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 3 NEW_LINE st = " abcbcscsesesesd " NEW_LINE decode_String ( st , K ) NEW_LINE DEDENT
Lexicographically smallest K | Function that prints the lexicographically smallest K - length substring containing maximum number of vowels ; Store the length of the string ; Initialize a prefix sum array ; Loop through the string to create the prefix sum array ; Store 1 at the index if it is a vowel ; Otherwise , store 0 ; Process the prefix array ; Initialize the variable to store maximum count of vowels ; Initialize the variable to store substring with maximum count of vowels ; Loop through the prefix array ; Store the current count of vowels ; Update the result if current count is greater than maximum count ; Update lexicographically smallest substring if the current count is equal to the maximum count ; Return the result ; Driver code
def maxVowelSubString ( str1 , K ) : NEW_LINE INDENT N = len ( str1 ) NEW_LINE pref = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( str1 [ i ] == ' a ' or str1 [ i ] == ' e ' or str1 [ i ] == ' i ' or str1 [ i ] == ' o ' or str1 [ i ] == ' u ' ) : NEW_LINE INDENT pref [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT pref [ i ] = 0 NEW_LINE DEDENT if ( i ) : NEW_LINE INDENT pref [ i ] += pref [ i - 1 ] NEW_LINE DEDENT DEDENT maxCount = pref [ K - 1 ] NEW_LINE res = str1 [ 0 : K ] NEW_LINE for i in range ( K , N ) : NEW_LINE INDENT currCount = pref [ i ] - pref [ i - K ] NEW_LINE if ( currCount > maxCount ) : NEW_LINE INDENT maxCount = currCount NEW_LINE res = str1 [ i - K + 1 : i + 1 ] NEW_LINE DEDENT elif ( currCount == maxCount ) : NEW_LINE INDENT temp = str1 [ i - K + 1 : i + 1 ] NEW_LINE if ( temp < res ) : NEW_LINE INDENT res = temp NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " ceebbaceeffo " NEW_LINE K = 3 NEW_LINE print ( maxVowelSubString ( str1 , K ) ) NEW_LINE DEDENT
Decode the string encoded with the given algorithm | Function to decode and print the original string ; To store the decoded string ; Getting the mid element ; Storing the first element of the string at the median position ; If the length is even then store the second element also ; k represents the number of characters that are already stored in the c [ ] ; If string length is odd ; If it is even ; Print the decoded string ; Driver code
def decodeStr ( str , len ) : NEW_LINE INDENT c = [ " " for i in range ( len ) ] NEW_LINE pos = 1 NEW_LINE if ( len % 2 == 1 ) : NEW_LINE INDENT med = int ( len / 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT med = int ( len / 2 - 1 ) NEW_LINE DEDENT c [ med ] = str [ 0 ] NEW_LINE if ( len % 2 == 0 ) : NEW_LINE INDENT c [ med + 1 ] = str [ 1 ] NEW_LINE DEDENT if ( len & 1 ) : NEW_LINE INDENT k = 1 NEW_LINE DEDENT else : NEW_LINE INDENT k = 2 NEW_LINE DEDENT for i in range ( k , len , 2 ) : NEW_LINE INDENT c [ med - pos ] = str [ i ] NEW_LINE if ( len % 2 == 1 ) : NEW_LINE INDENT c [ med + pos ] = str [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT c [ med + pos + 1 ] = str [ i + 1 ] NEW_LINE DEDENT pos += 1 NEW_LINE DEDENT print ( * c , sep = " " ) NEW_LINE DEDENT str = " ofrsgkeeeekgs " NEW_LINE len = len ( str ) NEW_LINE decodeStr ( str , len ) NEW_LINE
Count of distinct characters in a substring by given range for Q queries | Python3 program for Naive Approach ; Counter to count distinct char ; Initializing frequency array to count characters as the appear in substring S [ L : R ] ; Iterating over S [ L ] to S [ R ] ; Incrementing the count of s [ i ] character in frequency array ; If frequency of any character is > 0 then increment the counter ; Driver code
def findCount ( s , L , R ) : NEW_LINE INDENT distinct = 0 NEW_LINE frequency = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( L , R + 1 , 1 ) : NEW_LINE INDENT frequency [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( frequency [ i ] > 0 ) : NEW_LINE INDENT distinct += 1 NEW_LINE DEDENT DEDENT print ( distinct ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeksisacomputerscienceportal " NEW_LINE queries = 3 NEW_LINE Q = [ [ 0 , 10 ] , [ 15 , 18 ] , [ 12 , 20 ] ] NEW_LINE for i in range ( queries ) : NEW_LINE INDENT findCount ( s , Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT
String obtained by reversing and complementing a Binary string K times | Function to perform K operations upon the string and find modified string ; Number of reverse operations ; Number of complement operations ; If rev is odd parity ; If complement is odd parity ; Complementing each position ; Return the modified string ; Driver Code ; Function call
def ReverseComplement ( s , n , k ) : NEW_LINE INDENT rev = ( k + 1 ) // 2 NEW_LINE complement = k - rev NEW_LINE if ( rev % 2 ) : NEW_LINE INDENT s = s [ : : - 1 ] NEW_LINE DEDENT if ( complement % 2 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT s [ i ] = '1' NEW_LINE DEDENT else : NEW_LINE INDENT s [ i ] = '0' NEW_LINE DEDENT DEDENT DEDENT return s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = "10011" NEW_LINE k = 5 NEW_LINE n = len ( str1 ) NEW_LINE print ( ReverseComplement ( str1 , n , k ) ) NEW_LINE DEDENT
Check if any permutation of string is a K times repeated string | Function to check that permutation of the given string is a K times repeating String ; If length of string is not divisible by K ; Frequency Array ; Initially frequency of each character is 0 ; Computing the frequency of each character in the string ; Loop to check that frequency of every character of the string is divisible by K ; Driver Code
def repeatingString ( s , n , k ) : NEW_LINE INDENT if ( n % k != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT frequency = [ 0 for i in range ( 123 ) ] NEW_LINE for i in range ( 123 ) : NEW_LINE INDENT frequency [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT frequency [ s [ i ] ] += 1 NEW_LINE DEDENT repeat = n // k NEW_LINE for i in range ( 123 ) : NEW_LINE INDENT if ( frequency [ i ] % repeat != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " abcdcba " NEW_LINE n = len ( s ) NEW_LINE k = 3 NEW_LINE if ( repeatingString ( s , n , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find the last two missing digits of the given phone number | Function to find the last two digits of the number and print the complete number ; Sum of the first eight digits of the number ; if sum < 10 , then the two digits are '0' and the value of sum ; if sum > 10 , then the two digits are the value of sum ; Driver code
def findPhoneNumber ( n ) : NEW_LINE INDENT temp = n NEW_LINE sum = 0 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT sum += temp % 10 NEW_LINE temp = temp // 10 NEW_LINE DEDENT if ( sum < 10 ) : NEW_LINE INDENT print ( n , "0" , sum ) NEW_LINE DEDENT else : NEW_LINE INDENT n = str ( n ) NEW_LINE sum = str ( sum ) NEW_LINE n += sum NEW_LINE print ( n ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 98765432 NEW_LINE findPhoneNumber ( n ) NEW_LINE DEDENT
Number of ways to split a binary number such that every part is divisible by 2 | Function to return the required count ; If the splitting is not possible ; To store the count of zeroes ; Counting the number of zeroes ; Return the final answer ; Driver code
def cntSplits ( s ) : NEW_LINE INDENT if ( s [ len ( s ) - 1 ] == '1' ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT c_zero = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT c_zero += ( s [ i ] == '0' ) ; NEW_LINE DEDENT return int ( pow ( 2 , c_zero - 1 ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "10010" ; NEW_LINE print ( cntSplits ( s ) ) ; NEW_LINE DEDENT