codeparrot/xlcost-text-to-code huggingface dataset:
Group consecutive characters of same type in a string | Function to return the modified string ; Store original string ; Remove all white spaces ; To store the resultant string ; Traverse the string ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant string ; Driver code
def groupCharacters ( s , l ) : NEW_LINE INDENT temp = " " NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( s [ i ] != ' ▁ ' ) : NEW_LINE INDENT temp = temp + s [ i ] NEW_LINE DEDENT DEDENT l = len ( temp ) NEW_LINE ans = " " NEW_LINE i = 0 NEW_LINE while ( i < l ) : NEW_LINE INDENT if ( ord ( temp [ i ] ) >= ord ( ' A ' ) and ord ( temp [ i ] ) <= ord ( ' Z ' ) ) : NEW_LINE INDENT while ( i < l and ord ( temp [ i ] ) >= ord ( ' A ' ) and ord ( temp [ i ] ) <= ord ( ' Z ' ) ) : NEW_LINE INDENT ans = ans + temp [ i ] NEW_LINE i += 1 NEW_LINE DEDENT ans = ans + " ▁ " NEW_LINE DEDENT elif ( ord ( temp [ i ] ) >= ord ( '0' ) and ord ( temp [ i ] ) <= ord ( '9' ) ) : NEW_LINE INDENT while ( i < l and ord ( temp [ i ] ) >= ord ( '0' ) and ord ( temp [ i ] ) <= ord ( '9' ) ) : NEW_LINE INDENT ans = ans + temp [ i ] NEW_LINE i += 1 NEW_LINE DEDENT ans = ans + " ▁ " NEW_LINE DEDENT else : NEW_LINE INDENT while ( i < l and ord ( temp [ i ] ) >= ord ( ' * ' ) and ord ( temp [ i ] ) <= ord ( ' / ' ) ) : NEW_LINE INDENT ans = ans + temp [ i ] NEW_LINE i += 1 NEW_LINE DEDENT ans = ans + " ▁ " NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "34FTG234 + ▁ + - ▁ * " NEW_LINE l = len ( s ) NEW_LINE print ( groupCharacters ( s , l ) ) NEW_LINE DEDENT
Find the minimum number of preprocess moves required to make two strings equal | Function to return the minimum number of pre - processing moves required on string A ; Length of the given strings ; To store the required answer ; Run a loop upto n / 2 ; To store frequency of 4 characters ; If size is 4 ; If size is 3 ; If size is 2 ; If n is odd ; Driver code
def Preprocess ( A , B ) : NEW_LINE INDENT n = len ( A ) NEW_LINE ans = 0 NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE mp [ A [ i ] ] = 1 NEW_LINE if A [ i ] == A [ n - i - 1 ] : NEW_LINE INDENT mp [ A [ n - i - 1 ] ] += 1 NEW_LINE DEDENT if B [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ B [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ B [ i ] ] = 1 NEW_LINE DEDENT if B [ n - i - 1 ] in mp . keys ( ) : NEW_LINE INDENT mp [ B [ n - 1 - i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ B [ n - 1 - i ] ] = 1 NEW_LINE DEDENT sz = len ( mp ) NEW_LINE if ( sz == 4 ) : NEW_LINE INDENT ans += 2 NEW_LINE DEDENT elif ( sz == 3 ) : NEW_LINE INDENT ans += 1 + ( A [ i ] == A [ n - i - 1 ] ) NEW_LINE DEDENT elif ( sz == 2 ) : NEW_LINE INDENT ans += mp [ A [ i ] ] != 2 NEW_LINE DEDENT DEDENT if ( n % 2 == 1 and A [ n // 2 ] != B [ n // 2 ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT A = " abacaba " NEW_LINE B = " bacabaa " NEW_LINE print ( Preprocess ( A , B ) ) NEW_LINE
Check whether two strings are equivalent or not according to given condition | This function returns the least lexicogr aphical string obtained from its two halves ; Base Case - If string size is 1 ; Divide the string into its two halves ; Form least lexicographical string ; Driver Code
def leastLexiString ( s ) : NEW_LINE INDENT if ( len ( s ) & 1 != 0 ) : NEW_LINE INDENT return s NEW_LINE DEDENT x = leastLexiString ( s [ 0 : int ( len ( s ) / 2 ) ] ) NEW_LINE y = leastLexiString ( s [ int ( len ( s ) / 2 ) : len ( s ) ] ) NEW_LINE return min ( x + y , y + x ) NEW_LINE DEDENT def areEquivalent ( a , b ) : NEW_LINE INDENT return ( leastLexiString ( a ) == leastLexiString ( b ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = " aaba " NEW_LINE b = " abaa " NEW_LINE if ( areEquivalent ( a , b ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT a = " aabb " NEW_LINE b = " abab " NEW_LINE if ( areEquivalent ( a , b ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Check if a string can be converted to another string by replacing vowels and consonants | Function to check if the character is vowel or not ; Function that checks if a string can be converted to another string ; Find length of string ; If length is not same ; Iterate for every character ; If both vowel ; Both are consonants ; Driver Code
def isVowel ( c ) : NEW_LINE INDENT if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def checkPossibility ( s1 , s2 ) : NEW_LINE INDENT l1 = len ( s1 ) NEW_LINE l2 = len ( s2 ) NEW_LINE if ( l1 != l2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( l1 ) : NEW_LINE INDENT if ( isVowel ( s1 [ i ] ) and isVowel ( s2 [ i ] ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( ( isVowel ( s1 [ i ] ) ) == False and ( isVowel ( s2 [ i ] ) == False ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT S1 , S2 = " abcgle " , " ezggli " NEW_LINE if ( checkPossibility ( S1 , S2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Generate a string consisting of characters ' a ' and ' b ' that satisfy the given conditions | Function to generate and print the required string ; More ' b ' , append " bba " ; More ' a ' , append " aab " ; Equal number of ' a ' and ' b ' append " ab " ; Driver code
def generateString ( A , B ) : NEW_LINE INDENT rt = " " NEW_LINE while ( 0 < A or 0 < B ) : NEW_LINE INDENT if ( A < B ) : NEW_LINE INDENT if ( 0 < B ) : NEW_LINE INDENT rt = rt + ' b ' NEW_LINE B -= 1 NEW_LINE DEDENT if ( 0 < B ) : NEW_LINE INDENT rt += ' b ' NEW_LINE B -= 1 NEW_LINE DEDENT if ( 0 < A ) : NEW_LINE INDENT rt += ' a ' NEW_LINE A -= 1 NEW_LINE DEDENT DEDENT elif ( B < A ) : NEW_LINE INDENT if ( 0 < A ) : NEW_LINE INDENT rt += ' a ' NEW_LINE A -= 1 NEW_LINE DEDENT if ( 0 < A ) : NEW_LINE INDENT rt += ' a ' NEW_LINE A -= 1 NEW_LINE DEDENT if ( 0 < B ) : NEW_LINE INDENT rt += ' b ' NEW_LINE B -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( 0 < A ) : NEW_LINE INDENT rt += ' a ' NEW_LINE A -= 1 NEW_LINE DEDENT if ( 0 < B ) : NEW_LINE INDENT rt += ' b ' NEW_LINE B -= 1 NEW_LINE DEDENT DEDENT DEDENT print ( rt ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 2 NEW_LINE B = 6 NEW_LINE generateString ( A , B ) NEW_LINE DEDENT
Number of strings that satisfy the given condition | Function to return the count of valid strings ; Set to store indices of valid strings ; Find the maximum digit for current position ; Add indices of all the strings in the set that contain maximal digit ; Return number of strings in the set ; Driver code
def countStrings ( n , m , s ) : NEW_LINE INDENT ind = dict ( ) NEW_LINE for j in range ( m ) : NEW_LINE INDENT mx = 0 NEW_LINE str1 = s [ j ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT mx = max ( mx , int ( str1 [ i ] ) ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if int ( str1 [ i ] ) == mx : NEW_LINE INDENT ind [ i ] = 1 NEW_LINE DEDENT DEDENT DEDENT return len ( ind ) NEW_LINE DEDENT s = [ "223" , "232" , "112" ] NEW_LINE m = len ( s [ 0 ] ) NEW_LINE n = len ( s ) NEW_LINE print ( countStrings ( n , m , s ) ) NEW_LINE
Lexicographically largest sub | Function to return the lexicographically largest sub - sequence of s ; Get the max character from the string ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining string ; Driver code
def getSubSeq ( s , n ) : NEW_LINE INDENT res = " " NEW_LINE cr = 0 NEW_LINE while ( cr < n ) : NEW_LINE INDENT mx = s [ cr ] NEW_LINE for i in range ( cr + 1 , n ) : NEW_LINE INDENT mx = max ( mx , s [ i ] ) NEW_LINE DEDENT lst = cr NEW_LINE for i in range ( cr , n ) : NEW_LINE INDENT if ( s [ i ] == mx ) : NEW_LINE INDENT res += s [ i ] NEW_LINE lst = i NEW_LINE DEDENT DEDENT cr = lst + 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE n = len ( s ) NEW_LINE print ( getSubSeq ( s , n ) ) NEW_LINE DEDENT
Validation of Equation Given as String | Function that returns true if the equation is valid ; If it is an integer then add it to another string array ; Evaluation of 1 st operator ; Evaluation of 2 nd operator ; Evaluation of 3 rd operator ; If the LHS result is equal to the RHS ; Driver code
def isValid ( string ) : NEW_LINE INDENT k = 0 ; NEW_LINE operands = [ " " ] * 5 ; NEW_LINE operators = [ " " ] * 4 ; NEW_LINE ans = 0 ; ans1 = 0 ; ans2 = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] != ' + ' and string [ i ] != ' = ' and string [ i ] != ' - ' ) : NEW_LINE INDENT operands [ k ] += string [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT operators [ k ] = string [ i ] ; NEW_LINE if ( k == 1 ) : NEW_LINE INDENT if ( operators [ k - 1 ] == ' + ' ) : NEW_LINE INDENT ans += int ( operands [ k - 1 ] ) + int ( operands [ k ] ) ; NEW_LINE DEDENT if ( operators [ k - 1 ] == ' - ' ) : NEW_LINE INDENT ans += int ( operands [ k - 1 ] ) - int ( operands [ k ] ) ; NEW_LINE DEDENT DEDENT if ( k == 2 ) : NEW_LINE INDENT if ( operators [ k - 1 ] == ' + ' ) : NEW_LINE INDENT ans1 += ans + int ( operands [ k ] ) ; NEW_LINE DEDENT if ( operators [ k - 1 ] == ' - ' ) : NEW_LINE INDENT ans1 -= ans - int ( operands [ k ] ) ; NEW_LINE DEDENT DEDENT if ( k == 3 ) : NEW_LINE INDENT if ( operators [ k - 1 ] == ' + ' ) : NEW_LINE INDENT ans2 += ans1 + int ( operands [ k ] ) ; NEW_LINE DEDENT if ( operators [ k - 1 ] == ' - ' ) : NEW_LINE INDENT ans2 -= ans1 - int ( operands [ k ] ) ; NEW_LINE DEDENT DEDENT k += 1 NEW_LINE DEDENT DEDENT if ( ans2 == int ( operands [ 4 ] ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "2 ▁ + ▁ 5 ▁ + ▁ 3 ▁ + ▁ 1 ▁ = ▁ 11" ; NEW_LINE if ( isValid ( string ) ) : NEW_LINE INDENT print ( " Valid " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) ; NEW_LINE DEDENT DEDENT
Count of sub | Function to return the count of sub - strings of str that are divisible by k ; Take all sub - strings starting from i ; If current sub - string is divisible by k ; Return the required count ; Driver code
def countSubStr ( str , l , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( l ) : NEW_LINE INDENT n = 0 NEW_LINE for j in range ( i , l , 1 ) : NEW_LINE INDENT n = n * 10 + ( ord ( str [ j ] ) - ord ( '0' ) ) NEW_LINE if ( n % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "33445" NEW_LINE l = len ( str ) NEW_LINE k = 11 NEW_LINE print ( countSubStr ( str , l , k ) ) NEW_LINE DEDENT
Find the resulting Colour Combination | Function to return Colour Combination ; Check for B * G = Y ; Check for B * Y = G ; Check for Y * G = B ; Driver Code
def Colour_Combination ( s ) : NEW_LINE INDENT temp = s [ 0 ] NEW_LINE for i in range ( 1 , len ( s ) , 1 ) : NEW_LINE INDENT if ( temp != s [ i ] ) : NEW_LINE INDENT if ( ( temp == ' B ' or temp == ' G ' ) and ( s [ i ] == ' G ' or s [ i ] == ' B ' ) ) : NEW_LINE INDENT temp = ' Y ' NEW_LINE DEDENT elif ( ( temp == ' B ' or temp == ' Y ' ) and ( s [ i ] == ' Y ' or s [ i ] == ' B ' ) ) : NEW_LINE INDENT temp = ' G ' NEW_LINE DEDENT else : NEW_LINE INDENT temp = ' B ' NEW_LINE DEDENT DEDENT DEDENT return temp NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " GBYGB " NEW_LINE print ( Colour_Combination ( s ) ) NEW_LINE DEDENT
Reverse Middle X Characters | Function to reverse the middle x characters in a str1ing ; Find the position from where the characters have to be reversed ; Print the first n characters ; Print the middle x characters in reverse ; Print the last n characters ; Driver code
def reverse ( str1 , x ) : NEW_LINE INDENT n = ( len ( str1 ) - x ) // 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT for i in range ( n + x - 1 , n - 1 , - 1 ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT for i in range ( n + x , len ( str1 ) ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT DEDENT str1 = " geeksforgeeks " NEW_LINE x = 3 NEW_LINE reverse ( str1 , x ) NEW_LINE
Minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it | Function to count numbers ; Count the number of ' a ' , ' b ' and ' c ' in string ; If equal previously ; If not a multiple of 3 ; Increase the number of a ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' b ' and c ; Check if it is ' b ' and it more than n / 3 ; Check if it is ' c ' and it more than n / 3 ; Increase the number of b ' s ▁ by ▁ ▁ removing ▁ extra ▁ ' c ; Check if it is ' c ' and it more than n / 3 ; Increase the number of c 's from back ; Check if it is ' a ' and it more than n / 3 ; Increase the number of b 's from back ; Check if it is ' a ' and it more than n / 3 ; Increase the number of c 's from back ; Check if it is ' b ' and it more than n / 3 ; Driver Code
def lexoSmallest ( s , n ) : NEW_LINE INDENT ca = 0 NEW_LINE cb = 0 NEW_LINE cc = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' a ' ) : NEW_LINE INDENT ca += 1 NEW_LINE DEDENT elif ( s [ i ] == ' b ' ) : NEW_LINE INDENT cb += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cc += 1 NEW_LINE DEDENT DEDENT if ( ca == cb and cb == cc ) : NEW_LINE INDENT return s NEW_LINE DEDENT cnt = n // 3 NEW_LINE if ( cnt * 3 != n ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT i = 0 NEW_LINE while ( ca < cnt and i < n ) : NEW_LINE INDENT if ( s [ i ] == ' b ' and cb > cnt ) : NEW_LINE INDENT cb -= 1 NEW_LINE s [ i ] = ' a ' NEW_LINE ca += 1 NEW_LINE DEDENT elif ( s [ i ] == ' c ' and cc > cnt ) : NEW_LINE INDENT cc -= 1 NEW_LINE s [ i ] = ' a ' NEW_LINE ca += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT i = 0 NEW_LINE DEDENT ' NEW_LINE INDENT while ( cb < cnt and i < n ) : NEW_LINE INDENT if ( s [ i ] == ' c ' and cc > cnt ) : NEW_LINE INDENT cc -= 1 NEW_LINE s [ i ] = '1' NEW_LINE cb += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT i = n - 1 NEW_LINE while ( cc < cnt and i >= 0 ) : NEW_LINE INDENT if ( s [ i ] == ' a ' and ca > cnt ) : NEW_LINE INDENT ca -= 1 NEW_LINE s [ i ] = ' c ' NEW_LINE cc += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT i = n - 1 NEW_LINE while ( cb < cnt and i >= 0 ) : NEW_LINE INDENT if ( s [ i ] == ' a ' and ca > cnt ) : NEW_LINE INDENT ca -= 1 NEW_LINE s [ i ] = ' b ' NEW_LINE cb += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT i = n - 1 NEW_LINE while ( cc < cnt and i >= 0 ) : NEW_LINE INDENT if ( s [ i ] == ' b ' and cb > cnt ) : NEW_LINE INDENT cb -= 1 NEW_LINE s [ i ] = ' c ' NEW_LINE cc += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return s NEW_LINE DEDENT s = " aaaaaa " NEW_LINE n = len ( s ) NEW_LINE print ( * lexoSmallest ( list ( s ) , n ) , sep = " " ) NEW_LINE
Minimum moves to reach from i to j in a cyclic string | Function to return the count of steps required to move from i to j ; Starting from i + 1 ; Count of steps ; Current character ; If current character is different from previous ; Increment steps ; Update current character ; Return total steps ; Function to return the minimum number of steps required to reach j from i ; Swap the values so that i <= j ; Steps to go from i to j ( left to right ) ; While going from i to j ( right to left ) First go from i to 0 then from ( n - 1 ) to j ; If first and last character is different then it 'll add a step to stepsToLeft ; Return the minimum of two paths ; Driver code
def getSteps ( str , i , j , n ) : NEW_LINE INDENT k = i + 1 NEW_LINE steps = 0 NEW_LINE ch = str [ i ] NEW_LINE while ( k <= j ) : NEW_LINE INDENT if ( str [ k ] != ch ) : NEW_LINE INDENT steps = steps + 1 NEW_LINE ch = str [ k ] NEW_LINE DEDENT k = k + 1 NEW_LINE DEDENT return steps NEW_LINE DEDENT def getMinSteps ( str , i , j , n ) : NEW_LINE INDENT if ( j < i ) : NEW_LINE INDENT temp = i NEW_LINE i = j NEW_LINE j = temp NEW_LINE DEDENT stepsToRight = getSteps ( str , i , j , n ) NEW_LINE stepsToLeft = getSteps ( str , 0 , i , n ) + getSteps ( str , j , n - 1 , n ) NEW_LINE if ( str [ 0 ] != str [ n - 1 ] ) : NEW_LINE INDENT stepsToLeft = stepsToLeft + 1 NEW_LINE DEDENT return min ( stepsToLeft , stepsToRight ) NEW_LINE DEDENT str = " SSNSS " NEW_LINE n = len ( str ) NEW_LINE i = 0 NEW_LINE j = 3 NEW_LINE print ( getMinSteps ( str , i , j , n ) ) NEW_LINE
Count distinct substrings that contain some characters at most k times | Python3 implementation of the approach ; Function to return the count of valid sub - strings ; Store all characters of anotherStr in a direct index table for quick lookup . ; To store distinct output substrings ; Traverse through the given string and one by one generate substrings beginning from s [ i ] . ; One by one generate substrings ending with s [ j ] ; If character is illegal ; If current substring is valid ; If current substring is invalid , adding more characters would not help . ; Return the count of distinct sub - strings ; Driver code
MAX_CHAR = 256 NEW_LINE def countSubStrings ( s , anotherStr , k ) : NEW_LINE INDENT illegal = [ False ] * MAX_CHAR NEW_LINE for i in range ( len ( anotherStr ) ) : NEW_LINE INDENT illegal [ ord ( anotherStr [ i ] ) ] = True NEW_LINE DEDENT us = set ( ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT ss = " " NEW_LINE count = 0 NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT if ( illegal [ ord ( s [ j ] ) ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT ss = ss + s [ j ] NEW_LINE if ( count <= k ) : NEW_LINE INDENT us . add ( ss ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return len ( us ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " acbacbacaa " NEW_LINE anotherStr = " abcdefghijklmnopqrstuvwxyz " NEW_LINE k = 2 NEW_LINE print ( countSubStrings ( string , anotherStr , k ) ) NEW_LINE DEDENT
Count pairs of parentheses sequences such that parentheses are balanced | Python3 program to count the number of pairs of balanced parentheses ; Function to count the number of pairs ; Hashing function to count the opening and closing brackets ; Traverse for all bracket sequences ; Get the string ; Counts the opening and closing required ; Traverse in the string ; If it is a opening bracket ; If openings are there , then close it ; else : Else increase count of closing ; If requirements of openings are there and no closing ; If requirements of closing are there and no opening ; Perfect ; Divide by two since two perfect makes one pair ; Traverse in the open and find corresponding minimum ; Driver Code
import math as mt NEW_LINE def countPairs ( bracks , num ) : NEW_LINE INDENT openn = dict ( ) NEW_LINE close = dict ( ) NEW_LINE cnt = 0 NEW_LINE for i in range ( num ) : NEW_LINE INDENT s = bracks [ i ] NEW_LINE l = len ( s ) NEW_LINE op , cl = 0 , 0 NEW_LINE for j in range ( l ) : NEW_LINE INDENT if ( s [ j ] == ' ( ' ) : NEW_LINE INDENT op += 1 NEW_LINE if ( op ) : NEW_LINE INDENT op -= 1 NEW_LINE cl += 1 NEW_LINE DEDENT DEDENT DEDENT if ( op and cl == 0 ) : NEW_LINE INDENT if op in openn . keys ( ) : NEW_LINE INDENT openn [ op ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT openn [ op ] = 1 NEW_LINE DEDENT DEDENT if ( cl and op == 0 ) : NEW_LINE INDENT if cl in openn . keys ( ) : NEW_LINE INDENT close [ cl ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT close [ cl ] = 1 NEW_LINE DEDENT DEDENT if ( op == 0 and cl == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT cnt = cnt // 2 NEW_LINE for it in openn : NEW_LINE INDENT cnt += min ( openn [ it ] , close [ it ] ) NEW_LINE DEDENT return cnt NEW_LINE DEDENT bracks = [ " ) ( ) ) " , " ) " , " ( ( " , " ( ( " , " ( " , " ) " , " ) " ] NEW_LINE num = len ( bracks ) NEW_LINE print ( countPairs ( bracks , num ) ) NEW_LINE
Decrypt a string according to given rules | Function to return the original string after decryption ; Stores the decrypted string ; If length is odd ; Step counter ; Starting and ending index ; Iterate till all characters are decrypted ; Even step ; Odd step ; If length is even ; Step counter ; Starting and ending index ; Even step ; Odd step ; Reverse the decrypted string ; Driver Code
def decrypt ( s , l ) : NEW_LINE INDENT ans = " " NEW_LINE if ( l % 2 ) : NEW_LINE INDENT cnt = 0 NEW_LINE indl = 0 NEW_LINE indr = l - 1 NEW_LINE while ( len ( ans ) != l ) : NEW_LINE INDENT if ( cnt % 2 == 0 ) : NEW_LINE INDENT ans += s [ indl ] NEW_LINE indl += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += s [ indr ] NEW_LINE indr -= 1 NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT cnt = 0 NEW_LINE indl = 0 NEW_LINE indr = l - 1 NEW_LINE while ( len ( ans ) != l ) : NEW_LINE INDENT if ( cnt % 2 == 0 ) : NEW_LINE INDENT ans += s [ indr ] NEW_LINE indr -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += s [ indl ] NEW_LINE indl += 1 NEW_LINE DEDENT cnt += 1 NEW_LINE DEDENT DEDENT string = list ( ans ) NEW_LINE string . reverse ( ) NEW_LINE ans = ' ' . join ( string ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " segosegekfrek " NEW_LINE l = len ( s ) NEW_LINE print ( decrypt ( s , l ) ) NEW_LINE DEDENT
Remove consecutive alphabets which are in same case | Function to return the modified string ; Traverse through the remaining characters in the string ; If the current and the previous characters are not in the same case then take the character ; Driver code
def removeChars ( s ) : NEW_LINE INDENT modifiedStr = " " NEW_LINE modifiedStr += s [ 0 ] NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] . isupper ( ) and s [ i - 1 ] . islower ( ) or s [ i ] . islower ( ) and s [ i - 1 ] . isupper ( ) ) : NEW_LINE INDENT modifiedStr += s [ i ] NEW_LINE DEDENT DEDENT return modifiedStr NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " GeeksForGeeks " NEW_LINE print ( removeChars ( s ) ) NEW_LINE DEDENT
Cost to make a string Panagram | Function to return the total cost required to make the string Pangram ; Mark all the alphabets that occurred in the string ; Calculate the total cost for the missing alphabets ; Driver Code
def pangramCost ( arr , string ) : NEW_LINE INDENT cost = 0 NEW_LINE occurred = [ False ] * 26 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT occurred [ ord ( string [ i ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( not occurred [ i ] ) : NEW_LINE INDENT cost += arr [ i ] NEW_LINE DEDENT DEDENT return cost NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 ] NEW_LINE string = " abcdefghijklmopqrstuvwz " NEW_LINE print ( pangramCost ( arr , string ) ) NEW_LINE DEDENT
Recursive program to insert a star between pair of identical characters | Function to insert * at desired position ; Append current character ; If we reached last character ; If next character is same , append '* ; Driver code
def pairStar ( Input , Output , i = 0 ) : NEW_LINE INDENT Output = Output + Input [ i ] NEW_LINE if ( i == len ( Input ) - 1 ) : NEW_LINE INDENT print ( Output ) NEW_LINE return ; NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( Input [ i ] == Input [ i + 1 ] ) : NEW_LINE INDENT Output = Output + ' * ' ; NEW_LINE DEDENT pairStar ( Input , Output , i + 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Input = " geeks " NEW_LINE Output = " " NEW_LINE pairStar ( Input , Output ) ; NEW_LINE DEDENT