codeparrot/xlcost-text-to-code huggingface dataset:
Finite Automata algorithm for Pattern Searching | Python program for Finite Automata Pattern searching Algorithm ; If the character c is same as next character in pattern , then simply increment state ; Start from the largest possible value and stop when you find a prefix which is also suffix ; This function builds the TF table which represents Finite Automata for a given pattern ; Prints all occurrences of pat in txt ; Process txt over FA . ; Driver program to test above function
NO_OF_CHARS = 256 NEW_LINE def getNextState ( pat , M , state , x ) : NEW_LINE INDENT if state < M and x == ord ( pat [ state ] ) : NEW_LINE INDENT return state + 1 NEW_LINE DEDENT i = 0 NEW_LINE for ns in range ( state , 0 , - 1 ) : NEW_LINE INDENT if ord ( pat [ ns - 1 ] ) == x : NEW_LINE INDENT while ( i < ns - 1 ) : NEW_LINE INDENT if pat [ i ] != pat [ state - ns + 1 + i ] : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if i == ns - 1 : NEW_LINE INDENT return ns NEW_LINE DEDENT DEDENT DEDENT return 0 NEW_LINE DEDENT def computeTF ( pat , M ) : NEW_LINE INDENT global NO_OF_CHARS NEW_LINE TF = [ [ 0 for i in range ( NO_OF_CHARS ) ] \ for _ in range ( M + 1 ) ] NEW_LINE for state in range ( M + 1 ) : NEW_LINE INDENT for x in range ( NO_OF_CHARS ) : NEW_LINE INDENT z = getNextState ( pat , M , state , x ) NEW_LINE TF [ state ] [ x ] = z NEW_LINE DEDENT DEDENT return TF NEW_LINE DEDENT def search ( pat , txt ) : NEW_LINE INDENT global NO_OF_CHARS NEW_LINE M = len ( pat ) NEW_LINE N = len ( txt ) NEW_LINE TF = computeTF ( pat , M ) NEW_LINE state = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT state = TF [ state ] [ ord ( txt [ i ] ) ] NEW_LINE if state == M : NEW_LINE INDENT print ( " Pattern ▁ found ▁ at ▁ index : ▁ { } " . format ( i - M + 1 ) ) NEW_LINE DEDENT DEDENT DEDENT def main ( ) : NEW_LINE INDENT txt = " AABAACAADAABAAABAA " NEW_LINE pat = " AABA " NEW_LINE search ( pat , txt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Longest Substring containing '1' | Function to find length of longest substring containing '1 ; Count the number of contiguous 1 's ; Driver Code
' NEW_LINE def maxlength ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT count = 1 NEW_LINE j = i + 1 NEW_LINE while ( j <= n - 1 and s [ j ] == '1' ) : NEW_LINE INDENT count += 1 NEW_LINE j += 1 NEW_LINE DEDENT ans = max ( ans , count ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT s = "11101110" ; NEW_LINE print ( maxlength ( s ) ) NEW_LINE
Generate all possible strings formed by replacing letters with given respective symbols | Function to generate all possible string by replacing the characters with mapped symbols ; Base Case ; Function call with the P - th character not replaced ; Replace the P - th character ; Function call with the P - th character replaced ; Driver Code ; Function Call
def generateLetters ( S , P , M ) : NEW_LINE INDENT if ( P == len ( S ) ) : NEW_LINE INDENT print ( S ) ; NEW_LINE return NEW_LINE DEDENT generateLetters ( S , P + 1 , M ) ; NEW_LINE S = S . replace ( S [ P ] , M [ S [ P ] ] ) NEW_LINE generateLetters ( S , P + 1 , M ) ; NEW_LINE DEDENT S = " aBc " ; NEW_LINE M = { } ; NEW_LINE M [ ' a ' ] = ' $ ' NEW_LINE M [ ' B ' ] = ' # ' NEW_LINE M [ ' c ' ] = ' ^ ' NEW_LINE M [ ' d ' ] = ' & ' NEW_LINE M [ '1' ] = ' * ' NEW_LINE M [ '2' ] = ' ! ' NEW_LINE M [ ' E ' ] = ' @ ' NEW_LINE generateLetters ( S , 0 , M ) ; NEW_LINE
Minimize swaps of pairs of characters required such that no two adjacent characters in the string are same | Python3 program for the above approach ; Function to check if S contains any pair of adjacent characters that are same ; Traverse the String S ; If current pair of adjacent characters are the same ; Return true ; Utility function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Check if the required String is formed already ; Traverse the String S ; Swap the characters at i and j position ; Swap for Backtracking Step ; Function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Stores the resultant minimum number of swaps required ; Function call to find the minimum swaps required ; Prvar the result ; Driver code
import sys NEW_LINE ansSwaps = 0 NEW_LINE def check ( S ) : NEW_LINE INDENT for i in range ( 1 , len ( S ) ) : NEW_LINE INDENT if ( S [ i - 1 ] == S [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def minimumSwaps ( S , swaps , idx ) : NEW_LINE INDENT global ansSwaps NEW_LINE if ( check ( S ) ) : NEW_LINE INDENT ansSwaps = 1 + min ( ansSwaps , swaps ) NEW_LINE DEDENT for i in range ( idx , len ( S ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( S ) ) : NEW_LINE INDENT swap ( S , i , j ) NEW_LINE minimumSwaps ( S , swaps + 1 , i + 1 ) NEW_LINE S = swap ( S , i , j ) NEW_LINE DEDENT DEDENT DEDENT def swap ( arr , i , j ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE arr [ i ] = arr [ j ] NEW_LINE arr [ j ] = temp NEW_LINE return arr NEW_LINE DEDENT def findMinimumSwaps ( S ) : NEW_LINE INDENT global ansSwaps NEW_LINE ansSwaps = sys . maxsize NEW_LINE minimumSwaps ( S , 0 , 0 ) NEW_LINE if ( ansSwaps == sys . maxsize ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ansSwaps ) NEW_LINE DEDENT DEDENT S = " ABAACD " NEW_LINE findMinimumSwaps ( S . split ( ) ) NEW_LINE
Get level of a node in binary tree | iterative approach | Python3 program to find closest value in Binary search Tree ; Helper function that allocates a new node with the given data and None left and right poers . ; utility function to return level of given node ; extra None is appended to keep track of all the nodes to be appended before level is incremented by 1 ; Driver Code ; create a binary tree ; return level of node
_MIN = - 2147483648 NEW_LINE _MAX = 2147483648 NEW_LINE class getnode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getlevel ( root , data ) : NEW_LINE INDENT q = [ ] NEW_LINE level = 1 NEW_LINE q . append ( root ) NEW_LINE q . append ( None ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( temp == None ) : NEW_LINE INDENT if len ( q ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( q [ 0 ] != None ) : NEW_LINE INDENT q . append ( None ) NEW_LINE DEDENT level += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( temp . data == data ) : NEW_LINE INDENT return level NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getnode ( 20 ) NEW_LINE root . left = getnode ( 10 ) NEW_LINE root . right = getnode ( 30 ) NEW_LINE root . left . left = getnode ( 5 ) NEW_LINE root . left . right = getnode ( 15 ) NEW_LINE root . left . right . left = getnode ( 12 ) NEW_LINE root . right . left = getnode ( 25 ) NEW_LINE root . right . right = getnode ( 40 ) NEW_LINE level = getlevel ( root , 30 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 30 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 30 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 12 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 12 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 12 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 25 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 25 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 25 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 27 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 27 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 27 ▁ not ▁ found " ) NEW_LINE DEDENT DEDENT
Lexicographically smallest Palindromic Path in a Binary Tree | Struct binary tree node ; Function to check if the is palindrome or not ; Function to find the lexicographically smallest palindromic path in the Binary Tree ; Base case ; Append current node 's  data to the string ; Check if a node is leaf or not ; Check for the 1 st Palindromic Path ; Store lexicographically the smallest palindromic path ; Recursively traverse left subtree ; Recursively traverse right subtree ; Function to get smallest lexographical palindromic path ; Variable which stores the final result ; Function call to compute lexicographically smallest palindromic Path ; Driver Code ; Construct binary tree
class Node : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def checkPalindrome ( s ) : NEW_LINE INDENT low , high = 0 , len ( s ) - 1 NEW_LINE while ( low < high ) : NEW_LINE INDENT if ( s [ low ] != s [ high ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT low += 1 NEW_LINE high -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def lexicographicallySmall ( root , s ) : NEW_LINE INDENT global finalAns NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT s += root . data NEW_LINE if ( not root . left and not root . right ) : NEW_LINE INDENT if ( checkPalindrome ( s ) ) : NEW_LINE INDENT if ( finalAns == " $ " ) : NEW_LINE INDENT finalAns = s NEW_LINE DEDENT else : NEW_LINE INDENT finalAns = min ( finalAns , s ) NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT lexicographicallySmall ( root . left , s ) NEW_LINE lexicographicallySmall ( root . right , s ) NEW_LINE DEDENT def getPalindromePath ( root ) : NEW_LINE INDENT global finalAns NEW_LINE finalAns = " $ " NEW_LINE lexicographicallySmall ( root , " " ) NEW_LINE if ( finalAns == " $ " ) : NEW_LINE INDENT print ( " No ▁ Palindromic ▁ Path ▁ exists " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( finalAns ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT finalAns = " " NEW_LINE root = Node ( ' a ' ) NEW_LINE root . left = Node ( ' c ' ) NEW_LINE root . left . left = Node ( ' a ' ) NEW_LINE root . left . right = Node ( ' g ' ) NEW_LINE root . right = Node ( ' b ' ) NEW_LINE root . right . left = Node ( ' b ' ) NEW_LINE root . right . right = Node ( ' x ' ) NEW_LINE root . right . left . right = Node ( ' a ' ) NEW_LINE getPalindromePath ( root ) NEW_LINE DEDENT
Find mirror of a given node in Binary tree | Python3 program to find the mirror node in Binary tree ; A binary tree node has data , reference to left child and a reference to right child ; recursive function to find mirror ; If any of the node is none then node itself and decendent have no mirror , so return none , no need to further explore ! ; if left node is target node , then return right 's key (that is mirror) and vice versa ; first recur external nodes ; if no mirror found , recur internal nodes ; interface for mirror search ; Driver ; target node whose mirror have to be searched
class Node : NEW_LINE INDENT def __init__ ( self , key , lchild = None , rchild = None ) : NEW_LINE INDENT self . key = key NEW_LINE self . lchild = None NEW_LINE self . rchild = None NEW_LINE DEDENT DEDENT def findMirrorRec ( target , left , right ) : NEW_LINE INDENT if left == None or right == None : NEW_LINE INDENT return None NEW_LINE DEDENT if left . key == target : NEW_LINE INDENT return right . key NEW_LINE DEDENT if right . key == target : NEW_LINE INDENT return left . key NEW_LINE DEDENT mirror_val = findMirrorRec ( target , left . lchild , right . rchild ) NEW_LINE if mirror_val != None : NEW_LINE INDENT return mirror_val NEW_LINE DEDENT findMirrorRec ( target , left . rchild , right . lchild ) NEW_LINE DEDENT def findMirror ( root , target ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return None NEW_LINE DEDENT if root . key == target : NEW_LINE INDENT return target NEW_LINE DEDENT return findMirrorRec ( target , root . lchild , root . rchild ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE n1 = Node ( 2 ) NEW_LINE n2 = Node ( 3 ) NEW_LINE root . lchild = n1 NEW_LINE root . rchild = n2 NEW_LINE n3 = Node ( 4 ) NEW_LINE n4 = Node ( 5 ) NEW_LINE n5 = Node ( 6 ) NEW_LINE n1 . lchild = n3 NEW_LINE n2 . lchild = n4 NEW_LINE n2 . rchild = n5 NEW_LINE n6 = Node ( 7 ) NEW_LINE n7 = Node ( 8 ) NEW_LINE n8 = Node ( 9 ) NEW_LINE n3 . rchild = n6 NEW_LINE n4 . lchild = n7 NEW_LINE n4 . rchild = n8 NEW_LINE target = n3 . key NEW_LINE mirror = findMirror ( root , target ) NEW_LINE print ( " Mirror ▁ of ▁ node ▁ { } ▁ is ▁ node ▁ { } " . format ( target , mirror ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Convert given string to another by minimum replacements of subsequences by its smallest character | Function to return the minimum number of operation ; Storing data ; Initialize both arrays ; Stores the index of character ; Filling str1array , convChar and hashmap convertMap . ; Not possible to convert ; Calculate result Initializing return values ; Iterating the character from the end ; Increment the number of operations ; Not possible to convert ; To check whether the final element has been added in set S or not . ; Check if v1 [ j ] is present in hashmap or not ; Already converted then then continue ; Not possible to convert ; Print the result ; Given strings ; Function call
def transformString ( str1 , str2 ) : NEW_LINE INDENT N = len ( str1 ) NEW_LINE convChar = [ ] NEW_LINE str1array = [ ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT convChar . append ( [ ] ) NEW_LINE str1array . append ( [ ] ) NEW_LINE DEDENT convertMap = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT str1array [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( str1 [ i ] < str2 [ i ] ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT elif ( str1 [ i ] == str2 [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT convChar [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE convertMap [ i ] = str2 [ i ] NEW_LINE DEDENT DEDENT ret = 0 NEW_LINE retv = [ ] NEW_LINE for i in range ( 25 , - 1 , - 1 ) : NEW_LINE INDENT v = convChar [ i ] NEW_LINE if ( len ( v ) == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ret += 1 ; NEW_LINE v1 = str1array [ i ] NEW_LINE if ( len ( v1 ) == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT isScompleted = False NEW_LINE for j in range ( len ( v1 ) ) : NEW_LINE INDENT if ( v1 [ j ] in convertMap ) : NEW_LINE INDENT a = v1 [ j ] NEW_LINE if ( a > i + ord ( ' a ' ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( v1 [ j ] ) NEW_LINE isScompleted = True NEW_LINE retv . append ( v ) NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT v . append ( v1 [ j ] ) NEW_LINE isScompleted = True NEW_LINE retv . append ( v ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( isScompleted == False ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( ret ) NEW_LINE DEDENT A = " abcab " NEW_LINE B = " aabab " NEW_LINE transformString ( A , B ) NEW_LINE
Find largest subtree having identical left and right subtrees | Helper class that allocates a new node with the given data and None left and right pointers . ; Sets maxSize to size of largest subtree with identical left and right . maxSize is set with size of the maximum sized subtree . It returns size of subtree rooted with current node . This size is used to keep track of maximum size . ; string to store structure of left and right subtrees ; traverse left subtree and finds its size ; traverse right subtree and finds its size ; if left and right subtrees are similar update maximum subtree if needed ( Note that left subtree may have a bigger value than right and vice versa ) ; append left subtree data ; append current node data ; append right subtree data ; function to find the largest subtree having identical left and right subtree ; Driver Code ; Let us construct the following Tree 50 / \ 10 60 / \ / \ 5 20 70 70 / \ / \ 65 80 65 80
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def largestSubtreeUtil ( root , Str , maxSize , maxNode ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT left = [ " " ] NEW_LINE right = [ " " ] NEW_LINE ls = largestSubtreeUtil ( root . left , left , maxSize , maxNode ) NEW_LINE rs = largestSubtreeUtil ( root . right , right , maxSize , maxNode ) NEW_LINE size = ls + rs + 1 NEW_LINE if ( left [ 0 ] == right [ 0 ] ) : NEW_LINE INDENT if ( size > maxSize [ 0 ] ) : NEW_LINE INDENT maxSize [ 0 ] = size NEW_LINE maxNode [ 0 ] = root NEW_LINE DEDENT DEDENT Str [ 0 ] = Str [ 0 ] + " | " + left [ 0 ] + " | " NEW_LINE Str [ 0 ] = Str [ 0 ] + " | " + str ( root . data ) + " | " NEW_LINE Str [ 0 ] = Str [ 0 ] + " | " + right [ 0 ] + " | " NEW_LINE return size NEW_LINE DEDENT def largestSubtree ( node , maxNode ) : NEW_LINE INDENT maxSize = [ 0 ] NEW_LINE Str = [ " " ] NEW_LINE largestSubtreeUtil ( node , Str , maxSize , maxNode ) NEW_LINE return maxSize NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 50 ) NEW_LINE root . left = newNode ( 10 ) NEW_LINE root . right = newNode ( 60 ) NEW_LINE root . left . left = newNode ( 5 ) NEW_LINE root . left . right = newNode ( 20 ) NEW_LINE root . right . left = newNode ( 70 ) NEW_LINE root . right . left . left = newNode ( 65 ) NEW_LINE root . right . left . right = newNode ( 80 ) NEW_LINE root . right . right = newNode ( 70 ) NEW_LINE root . right . right . left = newNode ( 65 ) NEW_LINE root . right . right . right = newNode ( 80 ) NEW_LINE maxNode = [ None ] NEW_LINE maxSize = largestSubtree ( root , maxNode ) NEW_LINE print ( " Largest ▁ Subtree ▁ is ▁ rooted ▁ at ▁ node ▁ " , maxNode [ 0 ] . data ) NEW_LINE print ( " and ▁ its ▁ size ▁ is ▁ " , maxSize ) NEW_LINE DEDENT
Smallest number containing all possible N length permutations using digits 0 to D | Initialize set to see if all the possible permutations are present in the min length string ; To keep min length string ; Generate the required string ; Iterate over all possible character ; Append to make a new string ; If the new string is not visited ; Add in set ; Call the dfs function on the last d characters ; Base case ; Append '0' n - 1 times ; Call the DFS Function ; Driver Code
visited = set ( ) NEW_LINE ans = [ ] NEW_LINE def dfs ( curr , D ) : NEW_LINE INDENT for c in range ( D ) : NEW_LINE INDENT c = str ( c ) NEW_LINE neighbour = curr + c NEW_LINE if neighbour not in visited : NEW_LINE INDENT visited . add ( neighbour ) NEW_LINE dfs ( neighbour [ 1 : ] , D ) NEW_LINE ans . append ( c ) NEW_LINE DEDENT DEDENT DEDENT def reqString ( N , D ) : NEW_LINE INDENT if ( N == 1 and D == 1 ) : NEW_LINE INDENT return "0" NEW_LINE DEDENT start = ' ' . join ( [ '0' ] * ( N - 1 ) ) NEW_LINE dfs ( start , D ) NEW_LINE ans . extend ( [ '0' ] * ( N - 1 ) ) NEW_LINE return ' ' . join ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , D = 2 , 2 NEW_LINE print ( reqString ( N , D ) ) NEW_LINE DEDENT
Check if a string is a scrambled form of another string | Python3 program to check if a given string is a scrambled form of another string ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Check for the condition of anagram ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; If none of the above conditions are satisfied ; Driver Code
def isScramble ( S1 : str , S2 : str ) : NEW_LINE INDENT if len ( S1 ) != len ( S2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = len ( S1 ) NEW_LINE if not n : NEW_LINE INDENT return True NEW_LINE DEDENT if S1 == S2 : NEW_LINE INDENT return True NEW_LINE DEDENT if sorted ( S1 ) != sorted ( S2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( isScramble ( S1 [ : i ] , S2 [ : i ] ) and isScramble ( S1 [ i : ] , S2 [ i : ] ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( isScramble ( S1 [ - i : ] , S2 [ : i ] ) and isScramble ( S1 [ : - i ] , S2 [ i : ] ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S1 = " coder " NEW_LINE S2 = " ocred " NEW_LINE if ( isScramble ( S1 , S2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Smallest number possible by swapping adjacent even odd pairs | Function to return the smallest number possible ; Arrays to store odd and even digits in the order of their appearance in the given string ; Insert the odd and even digits ; pointer to odd digit ; pointer to even digit ; In case number of even and odd digits are not equal If odd digits are remaining ; If even digits are remaining ; Removal of leading 0 's  ; Driver Code
def findAns ( s ) : NEW_LINE INDENT odd = [ ] NEW_LINE even = [ ] NEW_LINE for c in s : NEW_LINE INDENT digit = int ( c ) NEW_LINE if ( digit & 1 ) : NEW_LINE INDENT odd . append ( digit ) NEW_LINE DEDENT else : NEW_LINE INDENT even . append ( digit ) NEW_LINE DEDENT DEDENT i = 0 NEW_LINE j = 0 NEW_LINE ans = " " NEW_LINE while ( i < len ( odd ) and j < len ( even ) ) : NEW_LINE INDENT if ( odd [ i ] < even [ j ] ) : NEW_LINE INDENT ans += str ( odd [ i ] ) NEW_LINE i = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += str ( even [ j ] ) NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT while ( i < len ( odd ) ) : NEW_LINE INDENT ans += str ( odd [ i ] ) NEW_LINE i = i + 1 NEW_LINE DEDENT while ( j < len ( even ) ) : NEW_LINE INDENT ans += str ( even [ j ] ) NEW_LINE j = j + 1 NEW_LINE DEDENT while ( ans [ 0 ] == '0' ) : NEW_LINE INDENT ans = ans [ 1 : ] NEW_LINE DEDENT return ans NEW_LINE DEDENT s = "894687536" NEW_LINE print ( findAns ( s ) ) NEW_LINE
Program to Convert BCD number into Decimal number | Function to convert BCD to Decimal ; Iterating through the bits backwards ; Forming the equivalent digit ( 0 to 9 ) from the group of 4. ; Reinitialize all variables and compute the number ; Update the answer ; Reverse the number formed . ; Driver Code ; Function Call
def bcdToDecimal ( s ) : NEW_LINE INDENT length = len ( s ) ; NEW_LINE check = 0 ; NEW_LINE check0 = 0 ; NEW_LINE num = 0 ; NEW_LINE sum = 0 ; NEW_LINE mul = 1 ; NEW_LINE rev = 0 ; NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum += ( ord ( s [ i ] ) - ord ( '0' ) ) * mul ; NEW_LINE mul *= 2 ; NEW_LINE check += 1 ; NEW_LINE if ( check == 4 or i == 0 ) : NEW_LINE INDENT if ( sum == 0 and check0 == 0 ) : NEW_LINE INDENT num = 1 ; NEW_LINE check0 = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT num = num * 10 + sum ; NEW_LINE DEDENT check = 0 ; NEW_LINE sum = 0 ; NEW_LINE mul = 1 ; NEW_LINE DEDENT DEDENT while ( num > 0 ) : NEW_LINE INDENT rev = rev * 10 + ( num % 10 ) ; NEW_LINE num //= 10 ; NEW_LINE DEDENT if ( check0 == 1 ) : NEW_LINE INDENT return rev - 1 ; NEW_LINE DEDENT return rev ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "100000101000" ; NEW_LINE print ( bcdToDecimal ( s ) ) ; NEW_LINE DEDENT