codeparrot/xlcost-text-to-code huggingface dataset:
How to validate PAN Card number using Regular Expression | Python3 program to validate the PAN Card number using Regular Expression ; Function to validate the PAN Card number . ; Regex to check valid PAN Card number ; Compile the ReGex ; If the PAN Card number is empty return false ; Return if the PAN Card number matched the ReGex ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
import re NEW_LINE def isValidPanCardNo ( panCardNo ) : NEW_LINE INDENT regex = " [ A - Z ] {5 } [ 0-9 ] { 4 } [ A - Z ] {1 } " NEW_LINE p = re . compile ( regex ) NEW_LINE if ( panCardNo == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( re . search ( p , panCardNo ) and len ( panCardNo ) == 10 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT str1 = " BNZAA2318J " NEW_LINE print ( isValidPanCardNo ( str1 ) ) NEW_LINE str2 = "23ZAABN18J " NEW_LINE print ( isValidPanCardNo ( str2 ) ) NEW_LINE str3 = " BNZAA2318JM " NEW_LINE print ( isValidPanCardNo ( str3 ) ) NEW_LINE str4 = " BNZAA23184" NEW_LINE print ( isValidPanCardNo ( str4 ) ) NEW_LINE str5 = " BNZAA ▁ 23184" NEW_LINE print ( isValidPanCardNo ( str5 ) ) NEW_LINE
How to validate time in 12 | Python3 program to validate the time in 12 - hour format using Regular Expression . ; Function to validate the time in 12 - hour format . ; Regex to check valid time in 12 - hour format . ; Compile the ReGex ; If the time is empty return false ; Return if the time matched the ReGex ; Driver Code . ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
import re NEW_LINE def isValidTime ( time ) : NEW_LINE INDENT regexPattern = " ( 1[012 ] ▁ [ 1-9 ] ) : " + " [ 0-5 ] [ 0-9 ] ( \\s ) " + " ? ( ? i ) ( am ▁ pm ) " ; NEW_LINE compiledPattern = re . compile ( regexPattern ) ; NEW_LINE if ( time == None ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if re . search ( compiledPattern , time ) : 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 = "12:15 ▁ AM " ; NEW_LINE print ( isValidTime ( str1 ) ) ; NEW_LINE str2 = "9:45PM " ; NEW_LINE print ( isValidTime ( str2 ) ) ; NEW_LINE str3 = "1:15" ; NEW_LINE print ( isValidTime ( str3 ) ) ; NEW_LINE str4 = "17:30" ; NEW_LINE print ( isValidTime ( str4 ) ) ; NEW_LINE DEDENT
Program to build a DFA that checks if a string ends with "01" or "10" | End position is checked using the string length value . q0 is the starting state . q1 and q2 are intermediate states . q3 and q4 are final states . ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q1 , 1 takes to q2 ; Driver Code ; all state transitions are printed . if string is accpetable , YES is printed . else NO is printed
def q1 ( s , i ) : NEW_LINE INDENT print ( " q1 - > " , end = " " ) ; NEW_LINE if ( i == len ( s ) ) : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT q1 ( s , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT q3 ( s , i + 1 ) ; NEW_LINE DEDENT DEDENT def q2 ( s , i ) : NEW_LINE INDENT print ( " q2 - > " , end = " " ) ; NEW_LINE if ( i == len ( s ) ) : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT q4 ( 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 print ( " q3 - > " , end = " " ) ; NEW_LINE if ( i == len ( s ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT q4 ( s , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT q2 ( s , i + 1 ) ; NEW_LINE DEDENT DEDENT def q4 ( s , i ) : NEW_LINE INDENT print ( " q4 - > " , end = " " ) ; NEW_LINE if ( i == len ( s ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT q1 ( 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 print ( " q0 - > " , end = " " ) ; NEW_LINE if ( i == len ( s ) ) : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE return ; NEW_LINE DEDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT q1 ( s , i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT q2 ( s , i + 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "010101" ; NEW_LINE print ( " State ▁ transitions ▁ are " , end = " ▁ " ) ; NEW_LINE q0 ( s , 0 ) ; NEW_LINE DEDENT
Check whether two strings contain same characters in same order | Python3 implementation of approach ; if length of the b = 0 then we return true ; if length of a = 0 that means b is not present in a so we return false ; Driver code
def checkSequence ( a , b ) : NEW_LINE INDENT if len ( b ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT if len ( a ) == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT if ( a [ 0 ] == b [ 0 ] ) : NEW_LINE INDENT return checkSequence ( a [ 1 : ] , b [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT return checkSequence ( a [ 1 : ] , b ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " Geeks " NEW_LINE s2 = " Geks " NEW_LINE if ( checkSequence ( s1 , s2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
String matching with * ( that matches with any ) in any of the two strings | Function to check if the two strings can be matched or not ; if the string don 't have *  then character t that position  must be same. ; Driver code
def doMatch ( A , B ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT if A [ i ] != ' * ' and B [ i ] != ' * ' : NEW_LINE INDENT if A [ i ] != B [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " gee * sforgeeks " NEW_LINE B = " geeksforgeeks " NEW_LINE print ( int ( doMatch ( A , B ) ) ) NEW_LINE DEDENT
Find Nth term of the series 0 , 2 , 4 , 8 , 12 , 18. . . | Calculate Nth term of series ; Driver Code
def nthTerm ( N ) : NEW_LINE INDENT return ( N + N * ( N - 1 ) ) // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Function for finding factorial of N ; return factorial of N ; Function for calculating Nth term of series ; Driver code
def factorial ( N ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE DEDENT return fact NEW_LINE DEDENT def nthTerm ( N ) : NEW_LINE INDENT return ( factorial ( N ) * ( N + 2 ) // 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT
Sum of nodes at maximum depth of a Binary Tree | Python3 code to find the sum of the nodes which are present at the maximum depth . ; Binary tree node ; Function to find the sum of the node which are present at the maximum depth . While traversing the nodes compare the level of the node with max_level ( Maximum level till the current node ) . If the current level exceeds the maximum level , update the max_level as current level . If the max level and current level are same , add the root data to current sum . ; Driver Code
sum = [ 0 ] NEW_LINE max_level = [ - ( 2 ** 32 ) ] NEW_LINE class createNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . d = data NEW_LINE self . l = None NEW_LINE self . r = None NEW_LINE DEDENT DEDENT def sumOfNodesAtMaxDepth ( ro , level ) : NEW_LINE INDENT if ( ro == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( level > max_level [ 0 ] ) : NEW_LINE INDENT sum [ 0 ] = ro . d NEW_LINE max_level [ 0 ] = level NEW_LINE DEDENT elif ( level == max_level [ 0 ] ) : NEW_LINE INDENT sum [ 0 ] = sum [ 0 ] + ro . d NEW_LINE DEDENT sumOfNodesAtMaxDepth ( ro . l , level + 1 ) NEW_LINE sumOfNodesAtMaxDepth ( ro . r , level + 1 ) NEW_LINE DEDENT root = createNode ( 1 ) NEW_LINE root . l = createNode ( 2 ) NEW_LINE root . r = createNode ( 3 ) NEW_LINE root . l . l = createNode ( 4 ) NEW_LINE root . l . r = createNode ( 5 ) NEW_LINE root . r . l = createNode ( 6 ) NEW_LINE root . r . r = createNode ( 7 ) NEW_LINE sumOfNodesAtMaxDepth ( root , 0 ) NEW_LINE print ( sum [ 0 ] ) NEW_LINE
Sum of nodes at maximum depth of a Binary Tree | Constructor ; Function to find the sum of nodes at maximum depth arguments are node and max , where Max is to match the depth of node at every call to node , if Max will be equal to 1 , means we are at deepest node . ; base case ; Max == 1 to track the node at deepest level ; recursive call to left and right nodes ; maxDepth function to find the max depth of the tree ; base case ; Either leftDepth of rightDepth is greater add 1 to include height of node at which call is ; call to function to calculate max depth ; Driver code ; Constructing tree ; call to calculate required sum
class Node : 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 sumMaxLevelRec ( node , Max ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if Max == 1 : NEW_LINE INDENT return node . data NEW_LINE DEDENT return ( sumMaxLevelRec ( node . left , Max - 1 ) + sumMaxLevelRec ( node . right , Max - 1 ) ) NEW_LINE DEDENT def maxDepth ( node ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 1 + max ( maxDepth ( node . left ) , maxDepth ( node . right ) ) NEW_LINE DEDENT def sumMaxLevel ( root ) : NEW_LINE INDENT MaxDepth = maxDepth ( root ) NEW_LINE return sumMaxLevelRec ( root , MaxDepth ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE print ( sumMaxLevel ( root ) ) NEW_LINE DEDENT
Sum of Manhattan distances between repetitions in a String | Function to find the sum of the manhattan distances between same characters in string ; Vector to store indices for each unique character of the string ; Append the position of each character in their respective vectors ; Store the required result ; Iterate over all the characters ; Calculate sum of all elements present in the vector ; Traverse the current vector ; Find suffix [ i + 1 ] ; Adding distance of all pairs whose first element is i and second element belongs to [ i + 1 , n ] ; Prthe result ; Driver Code ; Given Input ; Function Call
def SumofDistances ( s ) : NEW_LINE INDENT v = [ [ ] for i in range ( 26 ) ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT v [ ord ( s [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( len ( v [ i ] ) ) : NEW_LINE INDENT sum += v [ i ] [ j ] NEW_LINE DEDENT for j in range ( len ( v [ i ] ) ) : NEW_LINE INDENT sum -= v [ i ] [ j ] NEW_LINE ans += ( sum - ( len ( v [ i ] ) - 1 - j ) * ( v [ i ] [ j ] ) ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " ababa " NEW_LINE SumofDistances ( s ) NEW_LINE DEDENT
Minimum prefixes required to be flipped to convert a Binary String to another | Function to count flips required to make strings A and B equal ; Stores the count of the number of operations ; Stores the length of the chosen prefixes ; Stores if operations are performed even or odd times ; Traverse the given string ; If current characters in the two strings are unequal ; If A [ i ] is not flipped ; Increment count of operations ; Insert the length of the chosen prefix ; Update invert to true ; If A [ i ] is flipped ; Increment count of operations ; Insert length of the chosen prefix ; Update invert to false ; Print the number of operations required ; Print the chosen prefix length in each operation ; Driver Code ; Given binary strings
def findOperations ( A , B , N ) : NEW_LINE INDENT operations = 0 NEW_LINE ops = [ ] NEW_LINE invert = False NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ i ] != B [ i ] ) : NEW_LINE INDENT if ( not invert ) : NEW_LINE INDENT operations += 1 NEW_LINE ops . append ( i + 1 ) NEW_LINE invert = True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( invert ) : NEW_LINE INDENT operations += 1 NEW_LINE ops . append ( i + 1 ) NEW_LINE invert = False NEW_LINE DEDENT DEDENT DEDENT print ( operations ) NEW_LINE if ( operations != 0 ) : NEW_LINE INDENT for x in ops : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B = "001" , "000" NEW_LINE N = len ( A ) NEW_LINE findOperations ( A , B , N ) NEW_LINE DEDENT
Maximize cost of forming a set of words using given set of characters | Function to find the maximum cost of any valid set of words formed by using the given letters ; Stores frequency of characters ; Find the maximum cost ; Utility function to find maximum cost of generating any possible subsets of strings ; Base Case ; Stores the current cost ; Stores the cost of by the current word ; Create a copy of the letterCounts array ; Traverse the current word ; Store the current index & check if its frequency is 0 or not ; If true , then update wordScore to - 1 ; Otherwise , add the cost of the current index to wordScore ; Decrease its frequency ; If wordScore > 0 , then recursively call for next index ; Find the cost by not including current word ; Return the maximum score ; Given arrays ; Function Call
def maxScoreWords ( words , letters , score ) : NEW_LINE INDENT letterCounts = [ 0 ] * ( 26 ) NEW_LINE for letter in letters : NEW_LINE INDENT letterCounts [ ord ( letter ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT return helper ( words , 0 , letterCounts , score ) NEW_LINE DEDENT def helper ( words , start , letterCounts , score ) : NEW_LINE INDENT if ( start == len ( words ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT currScore = 0 NEW_LINE wordScore = 1 NEW_LINE nextCounts = letterCounts NEW_LINE for i in range ( len ( words [ start ] ) ) : NEW_LINE INDENT idx = ord ( words [ start ] [ i ] ) - ord ( ' a ' ) NEW_LINE if ( nextCounts [ idx ] == 0 ) : NEW_LINE INDENT wordScore = - 1 NEW_LINE break NEW_LINE DEDENT wordScore += score [ idx ] NEW_LINE nextCounts [ idx ] -= 1 NEW_LINE DEDENT if ( wordScore > 0 ) : NEW_LINE INDENT currScore = helper ( words , start + 1 , nextCounts , score ) + wordScore NEW_LINE DEDENT currScore = max ( currScore , helper ( words , start + 1 , letterCounts , score ) ) NEW_LINE return currScore NEW_LINE DEDENT words = [ " dog " , " cat " , " dad " , " good " ] NEW_LINE letters = [ ' a ' , ' a ' , ' c ' , ' d ' , ' d ' , ' d ' , ' g ' , ' o ' , ' o ' ] NEW_LINE score = [ 1 , 0 , 9 , 5 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] NEW_LINE print ( maxScoreWords ( words , letters , score ) ) NEW_LINE
Modify string by sorting characters after removal of characters whose frequency is not equal to power of 2 | Python3 program for the above approach ; Function to remove all the characters from a that whose frequencies are not equal to a perfect power of 2 ; Stores the frequency of each character in S ; Iterate over characters of string ; Update frequency of current character in the array freq [ ] ; Traverse the array freq [ ] ; Check if the i - th letter is absent from S ; Calculate log of frequency of the current character in the S ; Calculate power of 2 of lg ; Check if freq [ i ] is a power of 2 ; Print letter i + ' a ' freq [ i ] times ; Driver Code
from math import log2 NEW_LINE def countFrequency ( S , N ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq [ ord ( S [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT lg = int ( log2 ( freq [ i ] ) ) NEW_LINE a = pow ( 2 , lg ) NEW_LINE if ( a == freq [ i ] ) : NEW_LINE INDENT while ( freq [ i ] ) : NEW_LINE INDENT print ( chr ( i + ord ( ' a ' ) ) , end = " " ) NEW_LINE freq [ i ] -= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aaacbb " NEW_LINE N = len ( S ) NEW_LINE countFrequency ( S , N ) NEW_LINE DEDENT
Longest Common Subsequence ( LCS ) by repeatedly swapping characters of a string with characters of another string | Function to find the length of LCS possible by swapping any character of a with that of another string ; Store the size of the strings ; Stores frequency of characters ; Iterate over characters of the A ; Update frequency of character A [ i ] ; Iterate over characters of the B ; Update frequency of character B [ i ] ; Store the count of all pairs of similar characters ; Traverse the array freq [ ] ; Update cnt ; Print the minimum of cnt , N and M ; Driver Code ; Given strings
def lcsBySwapping ( A , B ) : NEW_LINE INDENT N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE freq = [ 0 ] * 26 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT freq [ ord ( A [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( len ( B ) ) : NEW_LINE INDENT freq [ ord ( B [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT cnt += freq [ i ] // 2 NEW_LINE DEDENT print ( min ( cnt , min ( N , M ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " abdeff " NEW_LINE B = " abbet " NEW_LINE lcsBySwapping ( A , B ) NEW_LINE DEDENT
Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code
def maximumLengthSubsequence ( str ) : NEW_LINE INDENT mp = { } NEW_LINE for ch in str : NEW_LINE INDENT if ch in mp . keys ( ) : NEW_LINE INDENT mp [ ch ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ ch ] = 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for ch in str : NEW_LINE INDENT if chr ( ord ( ch ) - 1 ) in mp . keys ( ) : NEW_LINE INDENT curr_max = mp [ ch ] NEW_LINE if chr ( ord ( ch ) - 1 ) in mp . keys ( ) : NEW_LINE INDENT curr_max += mp [ chr ( ord ( ch ) - 1 ) ] NEW_LINE DEDENT ans = max ( ans , curr_max ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT S = " acbbebcg " NEW_LINE maximumLengthSubsequence ( S ) NEW_LINE
Minimum increments by 1 or K required to convert a string into another given string | Function to count minimum increments by 1 or K required to convert X to Y ; Traverse the string X ; Case 1 ; Case 2 ; Add the difference / K to the count ; Add the difference % K to the count ; Case 3 ; Add the difference / K to the count ; Add the difference % K to the count ; Print the answer ; Driver Code
def countOperations ( X , Y , K ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( X ) ) : NEW_LINE INDENT c = 0 NEW_LINE if ( X [ i ] == Y [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( X [ i ] < Y [ i ] ) : NEW_LINE INDENT if ( ( ord ( Y [ i ] ) - ord ( X [ i ] ) ) >= K ) : NEW_LINE INDENT c = ( ord ( Y [ i ] ) - ord ( X [ i ] ) ) // K NEW_LINE DEDENT c += ( ord ( Y [ i ] ) - ord ( X [ i ] ) ) % K NEW_LINE DEDENT else : NEW_LINE INDENT t = 90 - ord ( X [ i ] ) NEW_LINE t += ord ( Y [ i ] ) - 65 + 1 NEW_LINE if ( t >= K ) : NEW_LINE INDENT c = t // K NEW_LINE DEDENT c += ( t % K ) NEW_LINE DEDENT count += c NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT X = " ABCT " NEW_LINE Y = " PBDI " NEW_LINE K = 6 NEW_LINE countOperations ( X , Y , K ) NEW_LINE
Minimum number of swaps required such that a given substring consists of exactly K 1 s | Function to find the minimum number of swaps required such that the substring { s [ l ] , . . , s [ r ] } consists of exactly k 1 s ; Store the size of the string ; Store the total number of 1 s and 0 s in the entire string ; Traverse the string S to find the frequency of 1 and 0 ; Store the number of 1 s and 0 s in the substring s [ l , r ] ; Traverse the substring S [ l , r ] to find the frequency of 1 s and 0 s in it ; Store the count of 1 s and 0 s outside substring s [ l , r ] ; Check if the sum of the substring is at most K ; Store number of 1 s required ; Check if there are enough 1 s remaining to be swapped ; If the count of 1 s in the substring exceeds k ; Store the number of 0 s required ; Check if there are enough 0 s remaining to be swapped ; In all other cases , print - 1 ; Driver Code
def minimumSwaps ( s , l , r , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE tot_ones , tot_zeros = 0 , 0 NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT tot_ones += 1 NEW_LINE DEDENT else : NEW_LINE INDENT tot_zeros += 1 NEW_LINE DEDENT DEDENT ones , zeros , Sum = 0 , 0 , 0 NEW_LINE for i in range ( l - 1 , r ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT ones += 1 NEW_LINE Sum += 1 NEW_LINE DEDENT else : NEW_LINE INDENT zeros += 1 NEW_LINE DEDENT DEDENT rem_ones = tot_ones - ones NEW_LINE rem_zeros = tot_zeros - zeros NEW_LINE if ( k >= Sum ) : NEW_LINE INDENT rem = k - Sum NEW_LINE if ( zeros >= rem and rem_ones >= rem ) : NEW_LINE INDENT return rem NEW_LINE DEDENT DEDENT elif ( k < Sum ) : NEW_LINE INDENT rem = Sum - k NEW_LINE if ( ones >= rem and rem_zeros >= rem ) : NEW_LINE INDENT return rem NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT S = "110011111000101" NEW_LINE L , R , K = 5 , 8 , 2 NEW_LINE print ( minimumSwaps ( S , L , R , K ) ) NEW_LINE
Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | Python3 program to implement the above approach ; Function to generate all numbers having "01" and "10" as a substring ; Insert 2 and 5 ; Iterate till x is 10 ^ 15 ; Multiply x by 2 ; Update x as x * 2 + 1 ; Function to check if binary representation of N contains only "01" and "10" as substring ; Function Call to generate all such numbers ; Check if a number N exists in Ans [ ] or not ; If the number exists ; Driver Code
Ans = [ ] NEW_LINE def populateNumber ( ) : NEW_LINE INDENT Ans . append ( 2 ) NEW_LINE Ans . append ( 5 ) NEW_LINE x = 5 NEW_LINE while ( x < 1000000000001 ) : NEW_LINE INDENT x *= 2 NEW_LINE Ans . append ( x ) NEW_LINE x = x * 2 + 1 NEW_LINE Ans . append ( x ) NEW_LINE DEDENT DEDENT def checkString ( N ) : NEW_LINE INDENT populateNumber ( ) NEW_LINE for it in Ans : NEW_LINE INDENT if ( it == N ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT return " No " NEW_LINE DEDENT N = 5 NEW_LINE print ( checkString ( N ) ) NEW_LINE