codeparrot/xlcost-text-to-code huggingface dataset:
Efficiently check if a string has all unique characters without using any additional data structure | Returns true if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits ; If bit corresponding to current character is already set ; set bit in checker ; Driver code
def areCharactersUnique ( s ) : NEW_LINE INDENT checker = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE if ( checker & ( 1 << val ) ) > 0 : NEW_LINE INDENT return False NEW_LINE DEDENT checker |= ( 1 << val ) NEW_LINE DEDENT return True NEW_LINE DEDENT s = " aaabbccdaa " NEW_LINE if areCharactersUnique ( s ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Count of words whose i | Return the count of words . ; If word contain single letter , return 1. ; Checking for first letter . ; Traversing the string and multiplying for combinations . ; If all three letters are same . ; If two letter are distinct . ; If all three letter are distinct . ; Checking for last letter . ; Driven Program
def countWords ( str , l ) : NEW_LINE INDENT count = 1 ; NEW_LINE if ( l == 1 ) : NEW_LINE INDENT return count NEW_LINE DEDENT if ( str [ 0 ] == str [ 1 ] ) : NEW_LINE INDENT count *= 1 NEW_LINE DEDENT else : NEW_LINE INDENT count *= 2 NEW_LINE DEDENT for j in range ( 1 , l - 1 ) : NEW_LINE INDENT if ( str [ j ] == str [ j - 1 ] and str [ j ] == str [ j + 1 ] ) : NEW_LINE INDENT count *= 1 NEW_LINE DEDENT elif ( str [ j ] == str [ j - 1 ] or str [ j ] == str [ j + 1 ] or str [ j - 1 ] == str [ j + 1 ] ) : NEW_LINE INDENT count *= 2 NEW_LINE DEDENT else : NEW_LINE INDENT count *= 3 NEW_LINE DEDENT DEDENT if ( str [ l - 1 ] == str [ l - 2 ] ) : NEW_LINE INDENT count *= 1 NEW_LINE DEDENT else : NEW_LINE INDENT count *= 2 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " abc " NEW_LINE l = len ( str ) NEW_LINE print ( countWords ( str , l ) ) NEW_LINE DEDENT
Maximum and minimum sums from two numbers with digit replacements | Find new value of x after replacing digit " from " to " to " ; Required digit found , replace it ; Returns maximum and minimum possible sums of x1 and x2 if digit replacements are allowed . ; We always get minimum sum if we replace 6 with 5. ; We always get maximum sum if we replace 5 with 6. ; Driver code
def replaceDig ( x , from1 , to ) : NEW_LINE INDENT result = 0 NEW_LINE multiply = 1 NEW_LINE while ( x > 0 ) : NEW_LINE INDENT reminder = x % 10 NEW_LINE if ( reminder == from1 ) : NEW_LINE INDENT result = result + to * multiply NEW_LINE DEDENT else : NEW_LINE INDENT result = result + reminder * multiply NEW_LINE DEDENT multiply *= 10 NEW_LINE x = int ( x / 10 ) NEW_LINE DEDENT return result NEW_LINE DEDENT def calculateMinMaxSum ( x1 , x2 ) : NEW_LINE INDENT minSum = replaceDig ( x1 , 6 , 5 ) + replaceDig ( x2 , 6 , 5 ) NEW_LINE maxSum = replaceDig ( x1 , 5 , 6 ) + replaceDig ( x2 , 5 , 6 ) NEW_LINE print ( " Minimum ▁ sum ▁ = " , minSum ) NEW_LINE print ( " Maximum ▁ sum ▁ = " , maxSum , end = " ▁ " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x1 = 5466 NEW_LINE x2 = 4555 NEW_LINE calculateMinMaxSum ( x1 , x2 ) NEW_LINE DEDENT
Queries on substring palindrome formation | Query type 1 : update str1ing position i with character x . ; Pr " Yes " if range [ L . . R ] can form palindrome , else pr " No " . ; Find the frequency of each character in S [ L ... R ] . ; Checking if more than one character have frequency greater than 1. ; Driver Code
def qType1 ( l , x , str1 ) : NEW_LINE INDENT str1 [ l - 1 ] = x NEW_LINE DEDENT def qType2 ( l , r , str1 ) : NEW_LINE INDENT freq = [ 0 for i in range ( 27 ) ] NEW_LINE for i in range ( l - 1 , r ) : NEW_LINE INDENT freq [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT if ( freq [ j ] % 2 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if count <= 1 : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT str1 = " geeksforgeeks " NEW_LINE str2 = [ i for i in str1 ] NEW_LINE n = len ( str2 ) NEW_LINE qType1 ( 4 , ' g ' , str2 ) NEW_LINE qType2 ( 1 , 4 , str2 ) NEW_LINE qType2 ( 2 , 3 , str2 ) NEW_LINE qType1 ( 10 , ' t ' , str2 ) NEW_LINE qType2 ( 10 , 11 , str2 ) NEW_LINE
Queries on substring palindrome formation | Python3 program to Queries on substr1ing palindrome formation . ; Return the frequency of the character in the i - th prefix . ; Updating the BIT ; Query to update the character in the str1ing . ; Adding - 1 at L position ; Updating the character ; Adding + 1 at R position ; Query to find if rearrangement of character in range L ... R can form palindrome ; Checking on the first character of the str1ing S . ; Checking if frequency of character is even or odd . ; Creating the Binary Index Tree of all aphabet ; Driver code
max = 1000 ; NEW_LINE def getFrequency ( tree , idx , i ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( idx > 0 ) : NEW_LINE INDENT sum += tree [ idx ] [ i ] ; NEW_LINE idx -= ( idx & - idx ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def update ( tree , idx , val , i ) : NEW_LINE INDENT while ( idx <= max ) : NEW_LINE INDENT tree [ idx ] [ i ] += val ; NEW_LINE idx += ( idx & - idx ) ; NEW_LINE DEDENT DEDENT def qType1 ( tree , l , x , str1 ) : NEW_LINE INDENT update ( tree , l , - 1 , ord ( str1 [ l - 1 ] ) - 97 + 1 ) ; NEW_LINE list1 = list ( str1 ) NEW_LINE list1 [ l - 1 ] = x ; NEW_LINE str1 = ' ' . join ( list1 ) ; NEW_LINE update ( tree , l , 1 , ord ( str1 [ l - 1 ] ) - 97 + 1 ) ; NEW_LINE DEDENT def qType2 ( tree , l , r , str1 ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( 1 , 27 ) : NEW_LINE INDENT if ( l == 1 ) : NEW_LINE INDENT if ( getFrequency ( tree , r , i ) % 2 == 1 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( ( getFrequency ( tree , r , i ) - getFrequency ( tree , l - 1 , i ) ) % 2 == 1 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT print ( " Yes " ) if ( count <= 1 ) else print ( " No " ) ; NEW_LINE DEDENT def buildBIT ( tree , str1 , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT update ( tree , i + 1 , 1 , ord ( str1 [ i ] ) - 97 + 1 ) ; NEW_LINE DEDENT DEDENT str1 = " geeksforgeeks " ; NEW_LINE n = len ( str1 ) ; NEW_LINE tree = [ [ 0 for x in range ( 27 ) ] for y in range ( max ) ] ; NEW_LINE buildBIT ( tree , str1 , n ) ; NEW_LINE qType1 ( tree , 4 , ' g ' , str1 ) ; NEW_LINE qType2 ( tree , 1 , 4 , str1 ) ; NEW_LINE qType2 ( tree , 2 , 3 , str1 ) ; NEW_LINE qType1 ( tree , 10 , ' t ' , str1 ) ; NEW_LINE qType2 ( tree , 10 , 11 , str1 ) ; NEW_LINE
Partition a number into two divisible parts | Finds if it is possible to partition str into two parts such that first part is divisible by a and second part is divisible by b . ; Create an array of size lenn + 1 and initialize it with 0. Store remainders from left to right when divided by 'a ; Compute remainders from right to left when divided by 'b ; Find a pothat can partition a number ; If split is not possible at this point ; We can split at i if one of the following two is true . a ) All characters after str [ i ] are 0 b ) after str [ i ] is divisible by b , i . e . , str [ i + 1. . n - 1 ] is divisible by b . ; Driver code
def findDivision ( str , a , b ) : NEW_LINE INDENT lenn = len ( str ) NEW_LINE DEDENT ' NEW_LINE INDENT lr = [ 0 ] * ( lenn + 1 ) NEW_LINE lr [ 0 ] = ( int ( str [ 0 ] ) ) % a NEW_LINE for i in range ( 1 , lenn ) : NEW_LINE INDENT lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + int ( str [ i ] ) ) % a NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT rl = [ 0 ] * ( lenn + 1 ) NEW_LINE rl [ lenn - 1 ] = int ( str [ lenn - 1 ] ) % b NEW_LINE power10 = 10 NEW_LINE for i in range ( lenn - 2 , - 1 , - 1 ) : NEW_LINE INDENT rl [ i ] = ( rl [ i + 1 ] + int ( str [ i ] ) * power10 ) % b NEW_LINE power10 = ( power10 * 10 ) % b NEW_LINE DEDENT for i in range ( 0 , lenn - 1 ) : NEW_LINE INDENT if ( lr [ i ] != 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( rl [ i + 1 ] == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE for k in range ( 0 , i + 1 ) : NEW_LINE INDENT print ( str [ k ] , end = " " ) NEW_LINE DEDENT print ( " , " , end = " ▁ " ) NEW_LINE for i in range ( i + 1 , lenn ) : NEW_LINE INDENT print ( str [ k ] , end = " " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " NO " ) NEW_LINE DEDENT str = "123" NEW_LINE a , b = 12 , 3 NEW_LINE findDivision ( str , a , b ) NEW_LINE
Efficient method for 2 's complement of a binary string | Function to find two 's complement ; Traverse the string to get first '1' from the last of string ; If there exists no '1' concatenate 1 at the starting of string ; Continue traversal after the position of first '1 ; Just flip the values ; return the modified string ; Driver code
def findTwoscomplement ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( str [ i ] == '1' ) : NEW_LINE INDENT break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( i == - 1 ) : NEW_LINE INDENT return '1' + str NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT k = i - 1 NEW_LINE while ( k >= 0 ) : NEW_LINE INDENT if ( str [ k ] == '1' ) : NEW_LINE INDENT str = list ( str ) NEW_LINE str [ k ] = '0' NEW_LINE str = ' ' . join ( str ) NEW_LINE DEDENT else : NEW_LINE INDENT str = list ( str ) NEW_LINE str [ k ] = '1' NEW_LINE str = ' ' . join ( str ) NEW_LINE DEDENT k -= 1 NEW_LINE DEDENT return str NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "00000101" NEW_LINE print ( findTwoscomplement ( str ) ) NEW_LINE DEDENT
Check length of a string is equal to the number appended at its last | Function to find if given number is equal to length or not ; Traverse string from end and find the number stored at the end . x is used to store power of 10. ; Check if number is equal to string length except that number 's digits ; Driver Code
def isequal ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE num = 0 NEW_LINE x = 1 NEW_LINE i = n - 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( '0' <= str [ i ] and str [ i ] <= '9' ) : NEW_LINE INDENT num = ( ord ( str [ i ] ) - ord ( '0' ) ) * x + num NEW_LINE x = x * 10 NEW_LINE if ( num >= n ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return num == i + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks13" NEW_LINE print ( " Yes " ) if isequal ( str ) else print ( " No " ) NEW_LINE DEDENT
Check if two strings are k | Python 3 program for the above approach ; Function to check k anagram of two strings ; First Condition : If both the strings have different length , then they cannot form anagram ; Converting str1 to Character Array arr1 ; Converting str2 to Character Array arr2 ; Sort arr1 in increasing order ; Sort arr2 in increasing order ; Iterate till str1 . length ( ) ; Condition if arr1 [ i ] is not equal to arr2 [ i ] then add it to list ; Condition to check if strings for K - anagram or not ; Driver Code ; Function Call
import sys NEW_LINE def kAnagrams ( str1 , str2 , k ) : NEW_LINE INDENT flag = 0 NEW_LINE list1 = [ ] NEW_LINE if ( len ( str1 ) != len ( str2 ) ) : NEW_LINE INDENT sys . exit ( ) NEW_LINE DEDENT arr1 = list ( str1 ) NEW_LINE arr2 = list ( str2 ) NEW_LINE arr1 . sort ( ) NEW_LINE arr2 . sort ( ) NEW_LINE for i in range ( len ( str1 ) ) : NEW_LINE INDENT if ( arr1 [ i ] != arr2 [ i ] ) : NEW_LINE INDENT list1 . append ( arr2 [ i ] ) NEW_LINE DEDENT DEDENT if ( len ( list1 ) <= k ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT if ( flag == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " fodr " NEW_LINE str2 = " gork " NEW_LINE k = 2 NEW_LINE kAnagrams ( str1 , str2 , k ) NEW_LINE if ( kAnagrams ( str1 , str2 , k ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimum number of characters to be removed to make a binary string alternate | Returns count of minimum characters to be removed to make s alternate . ; if two alternating characters of string are same ; result += 1 then need to delete a character ; Driver code
def countToMake0lternate ( s ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( len ( s ) - 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( countToMake0lternate ( "000111" ) ) NEW_LINE print ( countToMake0lternate ( "11111" ) ) NEW_LINE print ( countToMake0lternate ( "01010101" ) ) NEW_LINE DEDENT
Convert to a string that is repetition of a substring of k length | Returns True if S can be converted to a with k repeated subs after replacing k characters . ; Length of must be a multiple of k ; Map to store s of length k and their counts ; If is already a repetition of k subs , return True . ; If number of distinct subs is not 2 , then not possible to replace a . ; One of the two distinct must appear exactly once . Either the first entry appears once , or it appears n / k - 1 times to make other sub appear once . ; Driver code
def check ( S , k ) : NEW_LINE INDENT n = len ( S ) NEW_LINE if ( n % k != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT mp = { } NEW_LINE for i in range ( 0 , n , k ) : NEW_LINE INDENT mp [ S [ i : k ] ] = mp . get ( S [ i : k ] , 0 ) + 1 NEW_LINE DEDENT if ( len ( mp ) == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( len ( mp ) != 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in mp : NEW_LINE INDENT if i == ( n // k - 1 ) or mp [ i ] == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if check ( " abababcd " , 2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Group all occurrences of characters according to first appearance | Since only lower case characters are there ; Function to print the string ; Initialize counts of all characters as 0 ; Count occurrences of all characters in string ; Starts traversing the string ; Print the character till its count in hash array ; Make this character 's count value as 0. ; Driver code
MAX_CHAR = 26 NEW_LINE def printGrouped ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE count = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ ord ( string [ i ] ) - ord ( " a " ) ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT while count [ ord ( string [ i ] ) - ord ( " a " ) ] : NEW_LINE INDENT print ( string [ i ] , end = " " ) NEW_LINE count [ ord ( string [ i ] ) - ord ( " a " ) ] -= 1 NEW_LINE DEDENT count [ ord ( string [ i ] ) - ord ( " a " ) ] = 0 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " NEW_LINE printGrouped ( string ) NEW_LINE DEDENT
Sort a string according to the order defined by another string | Python3 program to sort a string according to the order defined by a pattern string ; Sort str according to the order defined by pattern . ; Create a count array store count of characters in str . ; Count number of occurrences of each character in str . ; Traverse the pattern and print every characters same number of times as it appears in str . This loop takes O ( m + n ) time where m is length of pattern and n is length of str . ; Driver code
MAX_CHAR = 26 NEW_LINE def sortByPattern ( str , pat ) : NEW_LINE INDENT global MAX_CHAR NEW_LINE count = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT count [ ord ( str [ i ] ) - 97 ] += 1 NEW_LINE DEDENT index = 0 ; NEW_LINE str = " " NEW_LINE for i in range ( 0 , len ( pat ) ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < count [ ord ( pat [ i ] ) - ord ( ' a ' ) ] ) : NEW_LINE INDENT str += pat [ i ] NEW_LINE j = j + 1 NEW_LINE index += 1 NEW_LINE DEDENT DEDENT return str NEW_LINE DEDENT pat = " bca " NEW_LINE str = " abc " NEW_LINE print ( sortByPattern ( str , pat ) ) NEW_LINE
Number of flips to make binary string alternate | Set 1 | Utility method to flip a character ; Utility method to get minimum flips when alternate string starts with expected char ; if current character is not expected , increase flip count ; flip expected character each time ; method return minimum flip to make binary string alternate ; return minimum of following two 1 ) flips when alternate string starts with 0 2 ) flips when alternate string starts with 1 ; Driver code to test above method
def flip ( ch ) : NEW_LINE INDENT return '1' if ( ch == '0' ) else '0' NEW_LINE DEDENT def getFlipWithStartingCharcter ( str , expected ) : NEW_LINE INDENT flipCount = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] != expected ) : NEW_LINE INDENT flipCount += 1 NEW_LINE DEDENT expected = flip ( expected ) NEW_LINE DEDENT return flipCount NEW_LINE DEDENT def minFlipToMakeStringAlternate ( str ) : NEW_LINE INDENT return min ( getFlipWithStartingCharcter ( str , '0' ) , getFlipWithStartingCharcter ( str , '1' ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "0001010111" NEW_LINE print ( minFlipToMakeStringAlternate ( str ) ) NEW_LINE DEDENT
Remainder with 7 for large numbers | Function which return Remainder after dividing the number by 7 ; This series is used to find remainder with 7 ; Index of next element in series ; Initialize result ; Traverse num from end ; Find current digit of num ; Add next term to result ; Move to next term in series ; Make sure that result never goes beyond 7. ; Make sure that remainder is positive ; Driver Code
def remainderWith7 ( num ) : NEW_LINE INDENT series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ] ; NEW_LINE series_index = 0 ; NEW_LINE result = 0 ; NEW_LINE for i in range ( ( len ( num ) - 1 ) , - 1 , - 1 ) : NEW_LINE INDENT digit = ord ( num [ i ] ) - 48 ; NEW_LINE result += digit * series [ series_index ] ; NEW_LINE series_index = ( series_index + 1 ) % 6 ; NEW_LINE result %= 7 ; NEW_LINE DEDENT if ( result < 0 ) : NEW_LINE INDENT result = ( result + 7 ) % 7 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT str = "12345" ; NEW_LINE print ( " Remainder ▁ with ▁ 7 ▁ is " , remainderWith7 ( str ) ) ; NEW_LINE
Check if a string can become empty by recursively deleting a given sub | Returns true if str can be made empty by recursively removing sub_str . ; idx : to store starting index of sub - string found in the original string ; Erasing the found sub - string from the original string ; Driver code
def canBecomeEmpty ( string , sub_str ) : NEW_LINE INDENT while len ( string ) > 0 : NEW_LINE INDENT idx = string . find ( sub_str ) NEW_LINE if idx == - 1 : NEW_LINE INDENT break NEW_LINE DEDENT string = string . replace ( sub_str , " " , 1 ) NEW_LINE DEDENT return ( len ( string ) == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GEEGEEKSKS " NEW_LINE sub_str = " GEEKS " NEW_LINE if canBecomeEmpty ( string , sub_str ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Perfect reversible string | This function basically checks if string is palindrome or not ; iterate from left and right ; Driver Code
def isReversible ( str ) : NEW_LINE INDENT i = 0 ; j = len ( str ) - 1 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( str [ i ] != str [ j ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT i += 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT str = " aba " ; NEW_LINE if ( isReversible ( str ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT
Check if string follows order of characters defined by a pattern or not | Set 3 | Python3 program to find if a string follows order defined by a given pattern ; Returns true if characters of str follow order defined by a given ptr . ; Initialize all orders as - 1 ; Assign an order to pattern characters according to their appearance in pattern ; Give the pattern characters order ; Increment the order ; Now one by one check if string characters follow above order ; If order of this character is less than order of previous , return false ; Update last_order for next iteration ; return that str followed pat ; Driver Code
CHAR_SIZE = 256 NEW_LINE def checkPattern ( Str , pat ) : NEW_LINE INDENT label = [ - 1 ] * CHAR_SIZE NEW_LINE order = 1 NEW_LINE for i in range ( len ( pat ) ) : NEW_LINE INDENT label [ ord ( pat [ i ] ) ] = order NEW_LINE order += 1 NEW_LINE DEDENT last_order = - 1 NEW_LINE for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( label [ ord ( Str [ i ] ) ] != - 1 ) : NEW_LINE INDENT if ( label [ ord ( Str [ i ] ) ] < last_order ) : NEW_LINE INDENT return False NEW_LINE DEDENT last_order = label [ ord ( Str [ i ] ) ] NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Str = " engineers ▁ rock " NEW_LINE pattern = " gsr " NEW_LINE print ( checkPattern ( Str , pattern ) ) NEW_LINE DEDENT
Check if string follows order of characters defined by a pattern or not | Set 2 | Python3 program to check if characters of a string follow pattern defined by given pattern . ; Insert all characters of pattern in a hash set , ; Build modified string ( string with characters only from pattern are taken ) ; Remove more than one consecutive occurrences of pattern characters from modified string . ; After above modifications , the length of modified string must be same as pattern length ; And pattern characters must also be same as modified string characters ; Driver Code
def followsPattern ( string , pattern ) : NEW_LINE INDENT patternSet = set ( ) NEW_LINE for i in range ( len ( pattern ) ) : NEW_LINE INDENT patternSet . add ( pattern [ i ] ) NEW_LINE DEDENT modifiedString = string NEW_LINE for i in range ( len ( string ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if not modifiedString [ i ] in patternSet : NEW_LINE INDENT modifiedString = modifiedString [ : i ] + modifiedString [ i + 1 : ] NEW_LINE DEDENT DEDENT for i in range ( len ( modifiedString ) - 1 , 0 , - 1 ) : NEW_LINE INDENT if modifiedString [ i ] == modifiedString [ i - 1 ] : NEW_LINE INDENT modifiedString = modifiedString [ : i ] + modifiedString [ i + 1 : ] NEW_LINE DEDENT DEDENT if len ( pattern ) != len ( modifiedString ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( len ( pattern ) ) : NEW_LINE INDENT if pattern [ i ] != modifiedString [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " engineers ▁ rock " NEW_LINE pattern = " er " NEW_LINE print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " egr " NEW_LINE print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " gsr " NEW_LINE print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " eger " NEW_LINE print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE DEDENT