codeparrot/xlcost-text-to-code huggingface dataset:
Maximum segment value after putting k breakpoints in a number | Function to Find Maximum Number ; Maximum segment length ; Find value of first segment of seg_len ; Find value of remaining segments using sliding window ; To find value of current segment , first remove leading digit from previous value ; Then add trailing digit ; Driver Code
def findMaxSegment ( s , k ) : NEW_LINE INDENT seg_len = len ( s ) - k NEW_LINE res = 0 NEW_LINE for i in range ( seg_len ) : NEW_LINE INDENT res = res * 10 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT seg_len_pow = pow ( 10 , seg_len - 1 ) NEW_LINE curr_val = res NEW_LINE for i in range ( 1 , len ( s ) - seg_len ) : NEW_LINE INDENT curr_val = curr_val - ( ord ( s [ i - 1 ] ) - ord ( '0' ) ) * seg_len_pow NEW_LINE curr_val = ( curr_val * 10 + ( ord ( s [ i + seg_len - 1 ] ) - ord ( '0' ) ) ) NEW_LINE res = max ( res , curr_val ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "8754" NEW_LINE k = 2 NEW_LINE print ( " Maximum ▁ number ▁ = ▁ " , findMaxSegment ( s , k ) ) NEW_LINE DEDENT
Converting one string to other using append and delete last operations | Returns true if it is possible to convert str1 to str2 using k operations . ; Case A ( i ) ; finding common length of both string ; Case A ( ii ) - ; Case B - ; Driver Code
def isConvertible ( str1 , str2 , k ) : NEW_LINE INDENT if ( ( len ( str1 ) + len ( str2 ) ) < k ) : NEW_LINE INDENT return True NEW_LINE DEDENT commonLength = 0 NEW_LINE for i in range ( 0 , min ( len ( str1 ) , len ( str2 ) ) , 1 ) : NEW_LINE INDENT if ( str1 [ i ] == str2 [ i ] ) : NEW_LINE INDENT commonLength += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( ( k - len ( str1 ) - len ( str2 ) + 2 * commonLength ) % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geek " NEW_LINE str2 = " geek " NEW_LINE k = 7 NEW_LINE if ( isConvertible ( str1 , str2 , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT str1 = " geeks " NEW_LINE str2 = " geek " NEW_LINE k = 5 NEW_LINE if ( isConvertible ( str1 , str2 , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Maximum distinct lowercase alphabets between two uppercase | Python3 Program to find maximum lowercase alphabets present between two uppercase alphabets ; Function which computes the maximum number of distinct lowercase alphabets between two uppercase alphabets ; Ignoring lowercase characters in the beginning . ; We start from next of first capital letter and traverse through remaining character . ; If character is in uppercase , ; Count all distinct lower case characters ; Update maximum count ; Reset count array ; If character is in lowercase ; Driver function
MAX_CHAR = 26 NEW_LINE def maxLower ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE i = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if str [ i ] >= ' A ' and str [ i ] <= ' Z ' : NEW_LINE INDENT i += 1 NEW_LINE break NEW_LINE DEDENT DEDENT maxCount = 0 NEW_LINE count = [ ] NEW_LINE for j in range ( MAX_CHAR ) : NEW_LINE INDENT count . append ( 0 ) NEW_LINE DEDENT for j in range ( i , n ) : NEW_LINE INDENT if str [ j ] >= ' A ' and str [ j ] <= ' Z ' : NEW_LINE INDENT currCount = 0 NEW_LINE for k in range ( MAX_CHAR ) : NEW_LINE INDENT if count [ k ] > 0 : NEW_LINE INDENT currCount += 1 NEW_LINE DEDENT DEDENT maxCount = max ( maxCount , currCount ) NEW_LINE for y in count : NEW_LINE INDENT y = 0 NEW_LINE DEDENT DEDENT if str [ j ] >= ' a ' and str [ j ] <= ' z ' : NEW_LINE INDENT count [ ord ( str [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT return maxCount NEW_LINE DEDENT str = " zACaAbbaazzC " ; NEW_LINE print ( maxLower ( str ) ) NEW_LINE
Maximum distinct lowercase alphabets between two uppercase | Function which computes the maximum number of distinct lowercase alphabets between two uppercase alphabets ; Ignoring lowercase characters in the beginning . ; We start from next of first capital letter and traverse through remaining character . ; If character is in uppercase , ; Update maximum count if lowercase character before this is more . ; clear the set ; If character is in lowercase ; Driver Code
def maxLower ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE i = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) : NEW_LINE INDENT i += 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT maxCount = 3 ; NEW_LINE s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) : NEW_LINE INDENT maxCount = max ( maxCount , len ( s ) ) ; NEW_LINE s . clear ( ) ; NEW_LINE DEDENT if ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) : NEW_LINE INDENT s . add ( str [ i ] ) ; NEW_LINE return maxCount ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " zACaAbbaazzC " ; NEW_LINE print ( maxLower ( str ) ) ; NEW_LINE DEDENT
First uppercase letter in a string ( Iterative and Recursive ) | Function to find string which has first character of each word . ; Driver code
def first ( str ) : NEW_LINE INDENT for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT if ( str [ i ] . istitle ( ) ) : NEW_LINE INDENT return str [ i ] NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT str = " geeksforGeeKS " NEW_LINE res = first ( str ) NEW_LINE if ( res == 0 ) : NEW_LINE INDENT print ( " No ▁ uppercase ▁ letter " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( res ) NEW_LINE DEDENT
Count maximum | Python3 implementation for counting maximum length palindromes ; factorial of a number ; function to count maximum length palindromes . ; Count number of occurrence of a charterer in the string ; k = 0 Count of singles num = 0 numerator of result den = 1 denominator of result ; if frequency is even fi = ci / 2 ; if frequency is odd fi = ci - 1 / 2. ; sum of all frequencies ; product of factorial of every frequency ; if all character are unique so there will be no pallindrome , so if num != 0 then only we are finding the factorial ; k are the single elements that can be placed in middle ; Driver Code
import math as mt NEW_LINE def fact ( n ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = ans * i NEW_LINE DEDENT return ( ans ) NEW_LINE DEDENT def numberOfPossiblePallindrome ( string , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if string [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ string [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ string [ i ] ] = 1 NEW_LINE DEDENT DEDENT fi = 0 NEW_LINE for it in mp : NEW_LINE INDENT if ( mp [ it ] % 2 == 0 ) : NEW_LINE INDENT fi = mp [ it ] // 2 NEW_LINE DEDENT else : NEW_LINE INDENT fi = ( mp [ it ] - 1 ) // 2 NEW_LINE k += 1 NEW_LINE DEDENT num = num + fi NEW_LINE den = den * fact ( fi ) NEW_LINE DEDENT if ( num != 0 ) : NEW_LINE INDENT num = fact ( num ) NEW_LINE DEDENT ans = num // den NEW_LINE if ( k != 0 ) : NEW_LINE INDENT ans = ans * k NEW_LINE DEDENT return ( ans ) NEW_LINE DEDENT string = " ababab " NEW_LINE n = len ( string ) NEW_LINE print ( numberOfPossiblePallindrome ( string , n ) ) NEW_LINE
Counting even decimal value substrings in a binary string | Python3 Program to count all even decimal value substring ; Generate all substring in arr [ 0. . n - 1 ] ; Store the count ; Pick starting point ; Pick ending point ; Substring between current starting and ending points ; increment power of 2 by one ; Driver code
import math NEW_LINE def evenDecimalValue ( str , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT decimalValue = 0 ; NEW_LINE powerOf2 = 1 ; NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT decimalValue += ( ( int ( str [ k ] ) - 0 ) * powerOf2 ) NEW_LINE powerOf2 *= 2 NEW_LINE DEDENT if ( decimalValue % 2 == 0 ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT str = "10010" NEW_LINE n = 5 NEW_LINE print ( evenDecimalValue ( str , n ) ) NEW_LINE
Create a new string by alternately combining the characters of two halves of the string in reverse | Function performing calculations ; Calculating the two halves of string s as first and second . The final string p ; It joins the characters to final string in reverse order ; It joins the characters to final string in reverse order ; Driver code ; Calling function
def solve ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE x = l // 2 NEW_LINE y = l NEW_LINE p = " " NEW_LINE while ( x > 0 and y > l / 2 ) : NEW_LINE INDENT p = p + s [ x - 1 ] NEW_LINE x = x - 1 NEW_LINE p = p + s [ y - 1 ] NEW_LINE y = y - 1 NEW_LINE DEDENT if ( y > l // 2 ) : NEW_LINE INDENT p = p + s [ y - 1 ] NEW_LINE y = y - 1 NEW_LINE DEDENT print ( p ) NEW_LINE DEDENT s = " sunshine " NEW_LINE solve ( s ) NEW_LINE
Make a lexicographically smallest palindrome with minimal changes | Function to create a palindrome ; Count the occurrences of every character in the string ; Create a string of characters with odd occurrences ; Change the created string upto middle element and update count to make sure that only one odd character exists . ; decrease the count of character updated ; Create three strings to make first half second half and middle one . ; characters with even occurrences ; fill the first half . ; character with odd occurrence ; fill the first half with half of occurrence except one ; For middle element ; create the second half by reversing the first half . ; Driver Code
def palindrome ( s : str , n : int ) -> int : NEW_LINE INDENT cnt = dict ( ) NEW_LINE R = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a = s [ i ] NEW_LINE if a in cnt : NEW_LINE INDENT cnt [ a ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ a ] = 1 NEW_LINE DEDENT DEDENT i = ' a ' NEW_LINE while i <= ' z ' : NEW_LINE INDENT if i in cnt and cnt [ i ] % 2 != 0 : NEW_LINE INDENT R += i NEW_LINE DEDENT i = chr ( ord ( i ) + 1 ) NEW_LINE DEDENT l = len ( R ) NEW_LINE j = 0 NEW_LINE for i in range ( l - 1 , ( l // 2 ) - 1 , - 1 ) : NEW_LINE INDENT if R [ i ] in cnt : NEW_LINE INDENT cnt [ R [ i ] ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ R [ i ] ] = - 1 NEW_LINE DEDENT R [ i ] = R [ j ] NEW_LINE if R [ j ] in cnt : NEW_LINE INDENT cnt [ R [ j ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ R [ j ] ] = 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT first , middle , second = " " , " " , " " NEW_LINE i = ' a ' NEW_LINE while i <= ' z ' : NEW_LINE INDENT if i in cnt : NEW_LINE INDENT if cnt [ i ] % 2 == 0 : NEW_LINE INDENT j = 0 NEW_LINE while j < cnt [ i ] // 2 : NEW_LINE INDENT first += i NEW_LINE j += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT j = 0 NEW_LINE while j < ( cnt [ i ] - 1 ) // 2 : NEW_LINE INDENT first += i NEW_LINE j += 1 NEW_LINE DEDENT middle += i NEW_LINE DEDENT DEDENT i = chr ( ord ( i ) + 1 ) NEW_LINE DEDENT second = first NEW_LINE second = ' ' . join ( reversed ( second ) ) NEW_LINE resultant = first + middle + second NEW_LINE print ( resultant ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE n = len ( S ) NEW_LINE palindrome ( S , n ) NEW_LINE DEDENT
Program for length of a string using recursion | Python program to calculate length of a string using recursion ; Function to calculate length ; if we reach at the end of the string ; Driver Code
str = " GeeksforGeeks " NEW_LINE def string_length ( str ) : NEW_LINE INDENT if str == ' ' : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + string_length ( str [ 1 : ] ) NEW_LINE DEDENT DEDENT print ( string_length ( str ) ) NEW_LINE
Count consonants in a string ( Iterative and recursive methods ) | Function to check for consonant ; To handle lower case ; To check is character is Consonant ; Driver code
def isConsonant ( ch ) : NEW_LINE INDENT ch = ch . upper ( ) NEW_LINE return not ( ch == ' A ' or ch == ' E ' or ch == ' I ' or ch == ' O ' or ch == ' U ' ) and ord ( ch ) >= 65 and ord ( ch ) <= 90 NEW_LINE DEDENT def totalConsonants ( string ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( isConsonant ( string [ i ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT string = " abc ▁ de " NEW_LINE print ( totalConsonants ( string ) ) NEW_LINE
Count consonants in a string ( Iterative and recursive methods ) | Function to check for consonant ; To handle lower case ; To count total number of consonants from 0 to n - 1 ; Driver code
def isConsonant ( ch ) : NEW_LINE INDENT ch = ch . upper ( ) NEW_LINE return not ( ch == ' A ' or ch == ' E ' or ch == ' I ' or ch == ' O ' or ch == ' U ' ) and ord ( ch ) >= 65 and ord ( ch ) <= 90 NEW_LINE DEDENT def totalConsonants ( string , n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return isConsonant ( string [ 0 ] ) NEW_LINE DEDENT return totalConsonants ( string , n - 1 ) + isConsonant ( string [ n - 1 ] ) NEW_LINE DEDENT string = " abc ▁ de " NEW_LINE print ( totalConsonants ( string , len ( string ) ) ) NEW_LINE
Reduce Hamming distance by swapping two characters | Python 3 code to decrease hamming distance using swap . ; Function to return the swapped indexes to get minimum hamming distance . ; Find the initial hamming distance ; Case - I : To decrease distance by two ; ASCII values of present character . ; If two same letters appear in different positions print their indexes ; Store the index of letters which is in wrong position ; Case : II ; If misplaced letter is found , print its original index and its new index ; Store the index of letters in wrong position ; Case - III ; Driver code
MAX = 26 NEW_LINE def Swap ( s , t , n ) : NEW_LINE INDENT dp = [ [ - 1 for x in range ( MAX ) ] for y in range ( MAX ) ] NEW_LINE tot = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] != t [ i ] ) : NEW_LINE INDENT tot += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT a = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE b = ord ( t [ i ] ) - ord ( ' a ' ) NEW_LINE if ( a == b ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( dp [ a ] [ b ] != - 1 ) : NEW_LINE INDENT print ( i + 1 , " ▁ " , dp [ a ] [ b ] + 1 ) NEW_LINE return NEW_LINE DEDENT dp [ b ] [ a ] = i NEW_LINE DEDENT A = [ - 1 ] * MAX NEW_LINE B = [ - 1 ] * MAX NEW_LINE for i in range ( n ) : NEW_LINE INDENT a = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE b = ord ( t [ i ] ) - ord ( ' a ' ) NEW_LINE if ( a == b ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( A [ b ] != - 1 ) : NEW_LINE INDENT print ( i + 1 , A [ b ] + 1 ) NEW_LINE return NEW_LINE DEDENT if ( B [ a ] != - 1 ) : NEW_LINE INDENT print ( i + 1 , B [ a ] + 1 ) NEW_LINE return NEW_LINE DEDENT A [ a ] = i NEW_LINE B [ b ] = i NEW_LINE DEDENT print ( " - 1" ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " permanent " NEW_LINE T = " pergament " NEW_LINE n = len ( S ) NEW_LINE if ( S == " " or T == " " ) : NEW_LINE INDENT print ( " Required ▁ string ▁ is ▁ empty . " ) NEW_LINE DEDENT else : NEW_LINE INDENT Swap ( S , T , n ) NEW_LINE DEDENT DEDENT
Convert string X to an anagram of string Y with minimum replacements | Python3 program to convert string X to string Y which minimum number of changes . ; Function that converts string X into lexicographically smallest anagram of string Y with minimal changes ; Counting frequency of characters in each string . ; We maintain two more counter arrays ctrx [ ] and ctry [ ] Ctrx [ ] maintains the count of extra elements present in string X than string Y Ctry [ ] maintains the count of characters missing from string X which should be present in string Y . ; This means that we cannot edit the current character as it 's frequency  in string X is equal to or less  than the frequency in string Y.  Thus, we go to the next position ; Here , we try to find that character , which has more frequency in string Y and less in string X . We try to find this character in lexicographical order so that we get lexicographically smaller string ; This portion deals with the lexicographical property . Now , we put a character in string X when either this character has smaller value than the character present there right now or if this is the last position for it to exchange , else we fix the character already present here in this position . ; Driver Code
MAX = 26 NEW_LINE def printAnagramAndChanges ( x , y ) : NEW_LINE INDENT x = list ( x ) NEW_LINE y = list ( y ) NEW_LINE countx , county = [ 0 ] * MAX , [ 0 ] * MAX NEW_LINE ctrx , ctry = [ 0 ] * MAX , [ 0 ] * MAX NEW_LINE change = 0 NEW_LINE l = len ( x ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT countx [ ord ( x [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE county [ ord ( y [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE DEDENT for i in range ( MAX ) : NEW_LINE INDENT if countx [ i ] > county [ i ] : NEW_LINE INDENT ctrx [ i ] += ( countx [ i ] - county [ i ] ) NEW_LINE DEDENT elif countx [ i ] < county [ i ] : NEW_LINE INDENT ctry [ i ] += ( county [ i ] - countx [ i ] ) NEW_LINE DEDENT change += abs ( county [ i ] - countx [ i ] ) NEW_LINE DEDENT for i in range ( l ) : NEW_LINE INDENT if ctrx [ ord ( x [ i ] ) - ord ( ' A ' ) ] == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT j = 0 NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT if ctry [ j ] > 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if countx [ ord ( x [ i ] ) - ord ( ' A ' ) ] == ctrx [ ord ( x [ i ] ) - ord ( ' A ' ) ] or ord ( x [ i ] ) - ord ( ' A ' ) > j : NEW_LINE INDENT countx [ ord ( x [ i ] ) - ord ( ' A ' ) ] -= 1 NEW_LINE ctrx [ ord ( x [ i ] ) - ord ( ' A ' ) ] -= 1 NEW_LINE ctry [ j ] -= 1 NEW_LINE x [ i ] = chr ( ord ( ' A ' ) + j ) NEW_LINE DEDENT else : NEW_LINE INDENT countx [ ord ( x [ i ] ) - ord ( ' A ' ) ] -= 1 NEW_LINE DEDENT DEDENT print ( " Anagram ▁ : " , ' ' . join ( x ) ) NEW_LINE print ( " Number ▁ of ▁ changes ▁ made ▁ : " , change // 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = " CDBABC " NEW_LINE y = " ADCABD " NEW_LINE printAnagramAndChanges ( x , y ) NEW_LINE DEDENT
Count number of equal pairs in a string | Python3 program to count the number of pairs ; Function to count the number of equal pairs ; Hash table ; Traverse the string and count occurrence ; Stores the answer ; Traverse and check the occurrence of every character ; Driver code
MAX = 256 NEW_LINE def countPairs ( s ) : NEW_LINE INDENT cnt = [ 0 for i in range ( 0 , MAX ) ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT cnt [ ord ( s [ i ] ) - 97 ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 0 , MAX ) : NEW_LINE INDENT ans += cnt [ i ] * cnt [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE print ( countPairs ( s ) ) NEW_LINE DEDENT
Find a string in lexicographic order which is in between given two strings | Function to find the lexicographically next string ; Iterate from last character ; If not ' z ' , increase by one ; if ' z ' , change it to 'a ; Driver Code ; If not equal , print the resultant string
def lexNext ( s , n ) : NEW_LINE INDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if s [ i ] != ' z ' : NEW_LINE INDENT k = ord ( s [ i ] ) NEW_LINE s [ i ] = chr ( k + 1 ) NEW_LINE return ' ' . join ( s ) NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT s [ i ] = ' a ' NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abcdeg " NEW_LINE T = " abcfgh " NEW_LINE n = len ( S ) NEW_LINE S = list ( S ) NEW_LINE res = lexNext ( S , n ) NEW_LINE if res != T : NEW_LINE INDENT print ( res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT
Find the arrangement of queue at given time | prints the arrangement at time = t ; Checking the entire queue for every moment from time = 1 to time = t . ; If current index contains ' B ' and next index contains ' G ' then swap ; Driver code
def solve ( n , t , p ) : NEW_LINE INDENT s = list ( p ) NEW_LINE for i in range ( 0 , t ) : NEW_LINE INDENT for j in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( s [ j ] == ' B ' and s [ j + 1 ] == ' G ' ) : NEW_LINE INDENT temp = s [ j ] ; NEW_LINE s [ j ] = s [ j + 1 ] ; NEW_LINE s [ j + 1 ] = temp ; NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT DEDENT print ( ' ' . join ( s ) ) NEW_LINE DEDENT n = 6 NEW_LINE t = 2 NEW_LINE p = " BBGBBG " NEW_LINE solve ( n , t , p ) NEW_LINE
Add two numbers represented by two arrays | Return sum of two number represented by the arrays . Size of a [ ] is greater than b [ ] . It is made sure be the wrapper function ; array to store sum . ; Until we reach beginning of array . we are comparing only for second array because we have already compare the size of array in wrapper function . ; find sum of corresponding element of both array . ; Finding carry for next sum . ; If second array size is less the first array size . ; Add carry to first array elements . ; If there is carry on adding 0 index elements . append 1 to total sum . ; Converting array into number . ; Wrapper Function ; Making first array which have greater number of element ; Driven Code
def calSumUtil ( a , b , n , m ) : NEW_LINE INDENT sum = [ 0 ] * n NEW_LINE i = n - 1 NEW_LINE j = m - 1 NEW_LINE k = n - 1 NEW_LINE carry = 0 NEW_LINE s = 0 NEW_LINE while j >= 0 : NEW_LINE INDENT s = a [ i ] + b [ j ] + carry NEW_LINE sum [ k ] = ( s % 10 ) NEW_LINE carry = s // 10 NEW_LINE k -= 1 NEW_LINE i -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT while i >= 0 : NEW_LINE INDENT s = a [ i ] + carry NEW_LINE sum [ k ] = ( s % 10 ) NEW_LINE carry = s // 10 NEW_LINE i -= 1 NEW_LINE k -= 1 NEW_LINE DEDENT ans = 0 NEW_LINE if carry : NEW_LINE INDENT ans = 10 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT ans += sum [ i ] NEW_LINE ans *= 10 NEW_LINE DEDENT return ans // 10 NEW_LINE DEDENT def calSum ( a , b , n , m ) : NEW_LINE INDENT if n >= m : NEW_LINE INDENT return calSumUtil ( a , b , n , m ) NEW_LINE DEDENT else : NEW_LINE INDENT return calSumUtil ( b , a , m , n ) NEW_LINE DEDENT DEDENT a = [ 9 , 3 , 9 ] NEW_LINE b = [ 6 , 1 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( calSum ( a , b , n , m ) ) NEW_LINE
Longest Common Anagram Subsequence | Python 3 implementation to find the length of the longest common anagram subsequence ; function to find the length of the longest common anagram subsequence ; List for storing frequencies of each character ; calculate frequency of each character of 'str1[] ; calculate frequency of each character of 'str2[] ; for each character add its minimum frequency out of the two strings in 'len ; required length ; Driver Code
SIZE = 26 NEW_LINE def longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) : NEW_LINE INDENT freq1 = [ 0 ] * SIZE NEW_LINE freq2 = [ 0 ] * SIZE NEW_LINE l = 0 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( n1 ) : NEW_LINE INDENT freq1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT for i in range ( n2 ) : NEW_LINE INDENT freq2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT for i in range ( SIZE ) : NEW_LINE INDENT l += min ( freq1 [ i ] , freq2 [ i ] ) NEW_LINE DEDENT return l NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " abdacp " NEW_LINE str2 = " ckamb " NEW_LINE n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE print ( " Length ▁ = ▁ " , longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) ) NEW_LINE DEDENT
Panalphabetic window in a string | Return if given string contain panalphabetic window . ; traversing the string ; if character of string is equal to ch , increment ch . ; if all characters are found , return true . ; Driver Code
def isPanalphabeticWindow ( s , n ) : NEW_LINE INDENT ch = ' a ' NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( s [ i ] == ch ) : NEW_LINE INDENT ch = chr ( ord ( ch ) + 1 ) NEW_LINE DEDENT if ( ch == ' z ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT s = " abujm ▁ zvcd ▁ acefc ▁ deghf ▁ gijkle ▁ m ▁ n ▁ o ▁ p ▁ pafqrstuvwxyzfap " NEW_LINE n = len ( s ) NEW_LINE if ( isPanalphabeticWindow ( s , n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT