codeparrot/xlcost-text-to-code huggingface dataset:
Check if a given integer is the product of K consecutive integers | Function to check if N can be expressed as the product of K consecutive integers ; Stores the K - th root of N ; Stores the product of K consecutive integers ; Traverse over the range [ 1 , K ] ; Update the product ; If product is N , then return " Yes " ; Otherwise , traverse over the range [ 2 , Kthroot ] ; Update the value of product ; If product is equal to N ; Otherwise , return " No " ; Driver Code
def checkPro ( n , k ) : NEW_LINE INDENT KthRoot = int ( n ** ( 1 / k ) ) NEW_LINE product = 1 NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT product = product * i NEW_LINE DEDENT print ( product ) NEW_LINE if ( product == N ) : NEW_LINE INDENT return ( " Yes " ) NEW_LINE DEDENT for i in range ( 2 , KthRoot + 1 ) : NEW_LINE INDENT product = product * ( i + k - 1 ) NEW_LINE product = product / ( i - 1 ) NEW_LINE print ( product ) NEW_LINE if ( product == N ) : NEW_LINE INDENT return ( " Yes " ) NEW_LINE DEDENT DEDENT return ( " No " ) NEW_LINE DEDENT N = 210 NEW_LINE K = 3 NEW_LINE print ( checkPro ( N , K ) ) NEW_LINE
Replace ' ? ' in a string such that no two adjacent characters are same | Function that replace all ' ? ' with lowercase alphabets such that each adjacent character is different ; Store the given String ; If the first character is '? ; Traverse the String [ 1 , N - 1 ] ; If the current character is '? ; Change the character ; Check equality with the previous character ; Check equality with the next character ; Check equality with the previous character ; If the last character is '? ; Change character ; Check with previous character ; Return the resultant String ; Driver Code ; Given String S ; Function Call
def changeString ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE s = [ ' ▁ ' ] * ( len ( S ) ) NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT s [ i ] = S [ i ] NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( s [ 0 ] == ' ? ' ) : NEW_LINE INDENT s [ 0 ] = ' a ' NEW_LINE if ( s [ 0 ] == s [ 1 ] ) : NEW_LINE INDENT s [ 0 ] = chr ( ord ( s [ 0 ] ) + 1 ) NEW_LINE DEDENT DEDENT for i in range ( 1 , N - 1 ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( s [ i ] == ' ? ' ) : NEW_LINE INDENT s [ i ] = ' a ' NEW_LINE if ( s [ i ] == s [ i - 1 ] ) : NEW_LINE INDENT s [ i ] = chr ( ord ( s [ i ] ) + 1 ) NEW_LINE DEDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE INDENT s [ i ] = chr ( ord ( s [ i ] ) + 1 ) NEW_LINE DEDENT if ( s [ i ] == s [ i - 1 ] ) : NEW_LINE INDENT s [ i ] = chr ( ord ( s [ i ] ) + 1 ) NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( s [ N - 1 ] == ' ? ' ) : NEW_LINE INDENT s [ N - 1 ] = ' a ' NEW_LINE if ( s [ N - 1 ] == s [ N - 2 ] ) : NEW_LINE INDENT s [ N - 1 ] += 1 NEW_LINE DEDENT DEDENT ans = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT ans += s [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ? a ? a " NEW_LINE print ( changeString ( S ) ) NEW_LINE DEDENT
Check if a given string is a Reverse Bitonic String or not | Function to check if the given string is reverse bitonic ; Check for decreasing sequence ; If end of string has been reached ; Check for increasing sequence ; If the end of string hasn 't  been reached ; If reverse bitonic ; Driver Code
def checkReverseBitonic ( s ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] < s [ i - 1 ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( s [ i ] >= s [ i - 1 ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( i == len ( s ) - 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT for j in range ( i + 1 , len ( s ) ) : NEW_LINE INDENT if ( s [ j ] > s [ j - 1 ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( s [ j ] <= s [ j - 1 ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT i = j ; NEW_LINE if ( i != len ( s ) ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT return 1 ; NEW_LINE DEDENT s = " abcdwef " NEW_LINE if ( checkReverseBitonic ( s ) == 1 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Smallest number whose square has N digits | Python3 Program to find the smallest number whose square has N digits ; Function to return smallest number whose square has N digits ; Calculate N - th term of the series ; Driver Code
import math ; NEW_LINE def smallestNum ( N ) : NEW_LINE INDENT x = pow ( 10.0 , ( N - 1 ) / 2.0 ) ; NEW_LINE return math . ceil ( x ) ; NEW_LINE DEDENT N = 4 ; NEW_LINE print ( smallestNum ( N ) ) ; NEW_LINE
Count of K | Function to to count the number of K - countdowns for multiple queries ; flag which stores the current value of value in the countdown ; count of K - countdowns ; Loop to iterate over the elements of the array ; condition check if the elements of the array is equal to K ; condition check if the elements of the array is in continuous order ; condition check if the elements of the array are not in continuous order ; condition check to increment the counter if the there is a K - countdown present in the array ; returning the count of K - countdowns ; Driver Code ; Function Call
def countKCountdown ( arr , N , K ) : NEW_LINE INDENT flag = - 1 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( arr [ i ] == K ) : NEW_LINE INDENT flag = K ; NEW_LINE DEDENT if ( arr [ i ] == flag ) : NEW_LINE INDENT flag -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT flag = - 1 ; NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT N = 8 ; NEW_LINE K = 3 ; NEW_LINE arr = [ 4 , 3 , 2 , 1 , 5 , 3 , 2 , 1 ] ; NEW_LINE print ( countKCountdown ( arr , N , K ) ) NEW_LINE
Convert the given RGB color code to Hex color code | Function to convert decimal to hexadecimal ; char array to store hexadecimal number ; Counter for hexadecimal number array ; Temporary variable to store remainder ; Storing remainder in temp variable . ; Check if temp < 10 ; Return the equivalent hexadecimal color code ; Function to convert the RGB code to Hex color code ; The hex color code doesn 't exist ; Driver Code
def decToHexa ( n ) : NEW_LINE INDENT hexaDeciNum = [ '0' ] * 100 NEW_LINE i = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT temp = 0 NEW_LINE temp = n % 16 NEW_LINE if ( temp < 10 ) : NEW_LINE INDENT hexaDeciNum [ i ] = chr ( temp + 48 ) NEW_LINE i = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hexaDeciNum [ i ] = chr ( temp + 55 ) NEW_LINE i = i + 1 NEW_LINE DEDENT n = int ( n / 16 ) NEW_LINE DEDENT hexCode = " " NEW_LINE if ( i == 2 ) : NEW_LINE INDENT hexCode = hexCode + hexaDeciNum [ 0 ] NEW_LINE hexCode = hexCode + hexaDeciNum [ 1 ] NEW_LINE DEDENT elif ( i == 1 ) : NEW_LINE INDENT hexCode = "0" NEW_LINE hexCode = hexCode + hexaDeciNum [ 0 ] NEW_LINE DEDENT elif ( i == 0 ) : NEW_LINE INDENT hexCode = "00" NEW_LINE DEDENT return hexCode NEW_LINE DEDENT def convertRGBtoHex ( R , G , B ) : NEW_LINE INDENT if ( ( R >= 0 and R <= 255 ) and ( G >= 0 and G <= 255 ) and ( B >= 0 and B <= 255 ) ) : NEW_LINE INDENT hexCode = hexCode + decToHexa ( R ) NEW_LINE hexCode = hexCode + decToHexa ( G ) NEW_LINE hexCode = hexCode + decToHexa ( B ) NEW_LINE return hexCode NEW_LINE DEDENT else : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT DEDENT R = 0 NEW_LINE G = 0 NEW_LINE B = 0 NEW_LINE print ( convertRGBtoHex ( R , G , B ) ) NEW_LINE R = 255 NEW_LINE G = 255 NEW_LINE B = 255 NEW_LINE print ( convertRGBtoHex ( R , G , B ) ) NEW_LINE R = 25 NEW_LINE G = 56 NEW_LINE B = 123 NEW_LINE print ( convertRGBtoHex ( R , G , B ) ) NEW_LINE R = 2 NEW_LINE G = 3 NEW_LINE B = 4 NEW_LINE print ( convertRGBtoHex ( R , G , B ) ) NEW_LINE R = 255 NEW_LINE G = 255 NEW_LINE B = 256 NEW_LINE print ( convertRGBtoHex ( R , G , B ) ) NEW_LINE
Strings formed from given characters without any consecutive repeating characters | Function to print the strings which satisfy the mentioned conditions ; Iterate through all the strings in the array . ; check function to check the conditions for every string ; Function to check whether the string contains any consecutive repetitive characters and any characters other than those in str ; Valid characters check ; Nonrepetitive check ; Driver code
def getStrings ( strr , arr ) : NEW_LINE INDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( check ( arr [ i ] , strr ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT def check ( s , strr ) : NEW_LINE INDENT chars = s NEW_LINE for c in chars : NEW_LINE INDENT if c not in strr : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT for i in range ( len ( chars ) - 1 ) : NEW_LINE INDENT if ( chars [ i ] == chars [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT strr = " ABCD " NEW_LINE arr = [ " AABCDA " , " ABCDZADC " , " ABCDBCA " , " ABCDABDCA " ] NEW_LINE getStrings ( strr , arr ) NEW_LINE
Program to build a DFA to accept strings that start and end with same character | Function for the state Q1 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q2 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q2 ; Function for the state Q3 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q4 ; Condition to check end of string ; State transitions ' a ' takes to q4 , and ' b ' takes to q3 ; Function for the state Q0 ; Condition to check end of string ; State transitions ' a ' takes to q1 , and ' b ' takes to q3 ; Driver Code ; Since q0 is the starting state Send the string to q0
def q1 ( s , i ) : NEW_LINE INDENT if ( i == len ( s ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == ' a ' ) : NEW_LINE INDENT q1 ( s , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT q2 ( s , i + 1 ) ; NEW_LINE DEDENT DEDENT def q2 ( s , i ) : NEW_LINE INDENT if ( i == len ( s ) ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == ' a ' ) : NEW_LINE INDENT q1 ( s , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT q2 ( s , i + 1 ) ; NEW_LINE DEDENT DEDENT def q3 ( s , i ) : NEW_LINE INDENT if ( i == len ( s ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == ' a ' ) : NEW_LINE INDENT q4 ( s , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT q3 ( s , i + 1 ) ; NEW_LINE DEDENT DEDENT def q4 ( s , i ) : NEW_LINE INDENT if ( i == s . length ( ) ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == ' a ' ) : NEW_LINE INDENT q4 ( s , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT q3 ( s , i + 1 ) ; NEW_LINE DEDENT DEDENT def q0 ( s , i ) : NEW_LINE INDENT if ( i == len ( s ) ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == ' a ' ) : NEW_LINE INDENT q1 ( s , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT q3 ( s , i + 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " abbaabb " ; NEW_LINE q0 ( s , 0 ) ; NEW_LINE DEDENT
Longest prefix in a string with highest frequency | Function to find Longest prefix string with the highest frequency ; storing all indices where first element is found ; if the first letter in the string does not occur again then answer will be the whole string ; loop till second appearance of the first element ; check one letter after every stored index ; If there is no mismatch we move forward ; otherwise we stop ; Driver Code
def prefix ( string ) : NEW_LINE INDENT k = 1 ; NEW_LINE n = len ( string ) ; NEW_LINE g = [ ] ; NEW_LINE flag = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( string [ i ] == string [ 0 ] ) : NEW_LINE INDENT g . append ( i ) ; NEW_LINE flag = 1 ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( string ) ; NEW_LINE DEDENT else : NEW_LINE INDENT length = len ( g ) ; NEW_LINE while ( k < g [ 0 ] ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for j in range ( length ) : NEW_LINE INDENT if ( string [ g [ j ] + k ] == string [ k ] ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT if ( cnt == len ) : NEW_LINE INDENT k += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT for i in range ( k + 1 ) : NEW_LINE INDENT print ( string [ i ] , end = " " ) ; NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abcab " ; NEW_LINE prefix ( string ) ; NEW_LINE DEDENT
Generate permutation of 1 to N such that absolute difference of consecutive numbers give K distinct integers | Function to generate a permutation of integers from 1 to N such that the absolute difference of all the two consecutive integers give K distinct integers ; To store the permutation ; For sequence 1 2 3. . . ; For sequence N , N - 1 , N - 2. . . ; Flag is used to alternate between the above if else statements ; If last element added was r + 1 ; If last element added was l - 1 ; Print the permutation ; Driver code
def printPermutation ( N , K ) : NEW_LINE INDENT res = list ( ) ; NEW_LINE l , r , flag = 1 , N , 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT if flag == False : NEW_LINE INDENT res . append ( l ) NEW_LINE l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT res . append ( r ) ; NEW_LINE r -= 1 ; NEW_LINE DEDENT flag = flag ^ 1 ; NEW_LINE DEDENT if flag == False : NEW_LINE INDENT for i in range ( r , 2 , - 1 ) : NEW_LINE INDENT res . append ( i ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( l , r ) : NEW_LINE INDENT res . append ( i ) NEW_LINE DEDENT DEDENT for i in res : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT N , K = 10 , 4 NEW_LINE printPermutation ( N , K ) NEW_LINE
Find Nth term of the series 1 , 8 , 54 , 384. . . | calculate factorial of N ; calculate Nth term of series ; Driver Code
def fact ( N ) : NEW_LINE INDENT product = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT product = product * i NEW_LINE DEDENT return product NEW_LINE DEDENT def nthTerm ( N ) : NEW_LINE INDENT return ( N * N ) * fact ( N ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT
Rabin | d is the number of characters in the input alphabet ; pat -> pattern txt -> text q -> A prime number ; hash value for pattern t = 0 hash value for txt ; The value of h would be " pow ( d , ▁ M - 1 ) % q " ; Calculate the hash value of pattern and first window of text ; Slide the pattern over text one by one ; Check the hash values of current window of text and pattern if the hash values match then only check for characters on by one ; Check for characters one by one ; if p == t and pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Calculate hash value for next window of text : Remove leading digit , add trailing digit ; We might get negative values of t , converting it to positive ; Driver Code ; A prime number ; Function Call
d = 256 NEW_LINE def search ( pat , txt , q ) : NEW_LINE INDENT M = len ( pat ) NEW_LINE N = len ( txt ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE h = 1 NEW_LINE for i in xrange ( M - 1 ) : NEW_LINE INDENT h = ( h * d ) % q NEW_LINE DEDENT for i in xrange ( M ) : NEW_LINE INDENT p = ( d * p + ord ( pat [ i ] ) ) % q NEW_LINE t = ( d * t + ord ( txt [ i ] ) ) % q NEW_LINE DEDENT for i in xrange ( N - M + 1 ) : NEW_LINE INDENT if p == t : NEW_LINE INDENT for j in xrange ( M ) : NEW_LINE INDENT if txt [ i + j ] != pat [ j ] : NEW_LINE INDENT break NEW_LINE DEDENT else : j += 1 NEW_LINE DEDENT if j == M : NEW_LINE INDENT print " Pattern ▁ found ▁ at ▁ index ▁ " + str ( i ) NEW_LINE DEDENT DEDENT if i < N - M : NEW_LINE INDENT t = ( d * ( t - ord ( txt [ i ] ) * h ) + ord ( txt [ i + M ] ) ) % q NEW_LINE if t < 0 : NEW_LINE INDENT t = t + q NEW_LINE DEDENT DEDENT DEDENT DEDENT txt = " GEEKS ▁ FOR ▁ GEEKS " NEW_LINE pat = " GEEK " NEW_LINE q = 101 NEW_LINE search ( pat , txt , q ) NEW_LINE
Find the final string after flipping bits at the indices that are multiple of prime factors of array elements | Python program for the above approach ; Stores smallest prime factor ; Function to find the smallest prime factor for every number till MAXN ; Marking smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Function to find all the distinct prime factors of the given number x ; Find the prime factors for X ; Find the spf [ ] of x ; Return the prime factors for x ; Function to find string after flipping the characters at indices of prime factors of array elements arr [ ] ; Precalculating Smallest Prime Factor ; Stores the frequency of each prime factor ; Iterate over all elements of the array arr [ ] ; Stores prime factors of arr [ i ] ; Increment the frequency of all prime factors of arr [ i ] ; Iterate over all elements of the array frequency [ ] ; If frequency [ i ] is odd ; Flip bits of all indices that are multiple of i ; Return Answer ; Driver Code
MAXN = 100001 NEW_LINE spf = [ 0 ] * MAXN NEW_LINE def sieve ( ) : NEW_LINE INDENT spf [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAXN ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAXN , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT i = 3 NEW_LINE while ( i * 8 < MAXN ) : NEW_LINE INDENT i += 1 NEW_LINE if ( spf [ i ] == i ) : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE INDENT if ( spf [ j ] == j ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def getFactorization ( x ) : NEW_LINE INDENT ret = [ ] NEW_LINE while ( x != 1 ) : NEW_LINE INDENT ret . append ( spf [ x ] ) NEW_LINE value = spf [ x ] NEW_LINE while ( x % value == 0 ) : NEW_LINE INDENT x = x // value NEW_LINE DEDENT DEDENT return ret NEW_LINE DEDENT def flipString ( S , arr , M ) : NEW_LINE INDENT sieve ( ) ; NEW_LINE frequency = [ 0 ] * MAXN NEW_LINE for i in range ( 0 , M ) : NEW_LINE INDENT primeFactors = getFactorization ( arr [ i ] ) NEW_LINE for factors in primeFactors : NEW_LINE INDENT frequency [ factors ] += 1 NEW_LINE frequency [ factors ] %= 2 NEW_LINE DEDENT DEDENT N = len ( S ) NEW_LINE for i in range ( 0 , MAXN ) : NEW_LINE INDENT if ( frequency [ i ] & 1 ) : NEW_LINE INDENT for j in range ( i , N + 1 , i ) : NEW_LINE INDENT S [ j - 1 ] = ( '0' if S [ j - 1 ] == '1' else '1' ) NEW_LINE DEDENT DEDENT DEDENT return S NEW_LINE DEDENT S = "000000" NEW_LINE S = list ( S ) NEW_LINE arr = [ 2 , 4 , 6 ] NEW_LINE M = len ( arr ) NEW_LINE print ( " " . join ( flipString ( S , arr , M ) ) ) NEW_LINE
Check if count of 1 s can be made greater in a Binary string by changing 0 s adjacent to 1 s | Function to check whether in a given binary string can we make number of 1 ' s ▁ greater ▁ than ▁ the ▁ number ▁ of ▁ 0' s by doing the given operation ; Stores the count of 0 's ; Stores the count of 1 's ; Traverse through the string S ; Check current character is 1 ; Update cnt1 ; Update cnt0 ; Traverse through the string S ; Check curretn character is 1 ; Check if left adjacent character is 0 ; Change the left adjacent character to _ ; Update the cnt0 ; Check if right adjacent character is 0 ; Change the right adjacent character to _ ; Update the cnt0 ; Check count of 1 ' s ▁ is ▁ greater ▁ ▁ than ▁ the ▁ count ▁ of ▁ 0' s ; Driver Code
def isOnesGreater ( S , N ) : NEW_LINE INDENT S = list ( S ) NEW_LINE cnt0 = 0 NEW_LINE cnt1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT if ( i > 0 and S [ i - 1 ] == '0' ) : NEW_LINE INDENT S [ i - 1 ] = ' _ ' NEW_LINE cnt0 -= 1 NEW_LINE DEDENT elif ( i < N and S [ i + 1 ] == '0' ) : NEW_LINE INDENT S [ i + 1 ] = ' _ ' NEW_LINE cnt0 -= 1 NEW_LINE DEDENT DEDENT DEDENT if ( cnt1 > cnt0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT S = "01" NEW_LINE N = len ( S ) NEW_LINE isOnesGreater ( S , N ) NEW_LINE
Minimize flips to make binary string as all 1 s by flipping characters in substring of size K repeatedly | Function to find the minimum number of operations required to convert all the characters to 1 by flipping the substrings of size K ; Stores the minimum number of operations required ; Traverse the string S ; If the character is 0 ; Flip the substrings of size K starting from i ; Increment the minimum count of operations required ; After performing the operations check if string S contains any 0 s ; If S contains only 1 's ; Driver Code
def minOperation ( St , K , N ) : NEW_LINE INDENT S = list ( St ) NEW_LINE min = 0 NEW_LINE i = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] == '0' and i + K <= N ) : NEW_LINE INDENT j = i NEW_LINE while ( j < i + K ) : NEW_LINE INDENT if ( S [ j ] == '1' ) : NEW_LINE INDENT S [ j ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT S [ j ] = '1' NEW_LINE DEDENT j += 1 NEW_LINE DEDENT min += 1 NEW_LINE DEDENT DEDENT temp = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT temp += 1 NEW_LINE if ( S [ i ] == '0' ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( temp == N ) : NEW_LINE INDENT print ( min ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT S = "00010110" NEW_LINE K = 3 NEW_LINE N = len ( S ) NEW_LINE minOperation ( S , K , N ) NEW_LINE
Check if string S1 can be formed using repeated insertions of another string S2 | Function to check a valid insertion ; Store the size of string ; Maintain a stack for characters ; Iterate through the string ; push the current character on top of the stack ; If the current character is the last character of string S2 then pop characters until S2 is not formed ; index of last character of the string S2 ; pop characters till 0 - th index ; Check if stack in non - empty ; Driver Code
def validInsertionstring ( S1 , S2 ) : NEW_LINE INDENT N = len ( S1 ) NEW_LINE M = len ( S2 ) NEW_LINE st = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT st . append ( S1 [ i ] ) NEW_LINE if ( S1 [ i ] == S2 [ M - 1 ] ) : NEW_LINE INDENT idx = M - 1 NEW_LINE while ( idx >= 0 ) : NEW_LINE INDENT if ( len ( st ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT c = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE if ( c != S2 [ idx ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT idx -= 1 NEW_LINE DEDENT DEDENT DEDENT if ( len ( st ) != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT S1 = " aabb " NEW_LINE S2 = " ab " NEW_LINE if validInsertionstring ( S1 , S2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if rearranging Array elements can form a Palindrome or not | Function to check whether elements of an array can form a palindrome ; create an empty string to append elements of an array ; append each element to the string str to form a string so that we can solve it in easy way ; Create a freq array and initialize all values as 0 ; For each character in formed string , increment freq in the corresponding freq array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver Code
def can_form_palindrome ( arr , n ) : NEW_LINE INDENT MAX = 256 NEW_LINE s = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = s + str ( arr [ i ] ) NEW_LINE DEDENT freq = [ 0 ] * MAX NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT if ( freq [ i ] & 1 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if ( count > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE if ( can_form_palindrome ( arr , N ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT