codeparrot/xlcost-text-to-code huggingface dataset:
Minimize steps defined by a string required to reach the destination from a given source | Function to find the minimum length of string required to reach from source to destination ; Size of the string ; Stores the index of the four directions E , W , N , S ; If destination reached ; Iterate over the string ; Move east ; Change x1 according to direction E ; Move west ; Change x1 according to direction W ; Move north ; Change y1 according to direction N ; Move south ; Change y1 according to direction S ; Store the max of all positions ; Print the minimum length of string required ; Otherwise , it is impossible ; Given string ; Given source and destination ; Function call
def minimum_length ( x1 , y1 , x2 , y2 , str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE pos1 = - 1 NEW_LINE pos2 = - 1 NEW_LINE pos3 = - 1 NEW_LINE pos4 = - 1 NEW_LINE if ( x1 == x2 and y1 == y2 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( x2 > x1 ) : NEW_LINE INDENT if ( str [ i ] == ' E ' ) : NEW_LINE INDENT x1 = x1 + 1 NEW_LINE if ( x1 == x2 ) : NEW_LINE INDENT pos1 = i NEW_LINE DEDENT DEDENT DEDENT if ( x2 < x1 ) : NEW_LINE INDENT if ( str [ i ] == ' W ' ) : NEW_LINE INDENT x1 = x1 - 1 NEW_LINE if ( x1 == x2 ) : NEW_LINE INDENT pos2 = i NEW_LINE DEDENT DEDENT DEDENT if ( y2 > y1 ) : NEW_LINE INDENT if ( str [ i ] == ' N ' ) : NEW_LINE INDENT y1 = y1 + 1 NEW_LINE if ( y1 == y2 ) : NEW_LINE INDENT pos3 = i NEW_LINE DEDENT DEDENT DEDENT if ( y2 < y1 ) : NEW_LINE INDENT if ( str [ i ] == ' S ' ) : NEW_LINE INDENT y1 = y1 - 1 NEW_LINE if ( y1 == y2 ) : NEW_LINE INDENT pos4 = i NEW_LINE DEDENT DEDENT DEDENT DEDENT z = 0 NEW_LINE z = max ( pos1 , max ( max ( pos2 , pos3 ) , pos4 ) ) NEW_LINE if ( x1 == x2 and y1 == y2 ) : NEW_LINE INDENT print ( z + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT DEDENT str = " SESNW " NEW_LINE x1 = 0 NEW_LINE x2 = 1 NEW_LINE y1 = 0 NEW_LINE y2 = 1 NEW_LINE minimum_length ( x1 , y1 , x2 , y2 , str ) NEW_LINE
Remove all duplicate adjacent characters from a string using Stack | Function to remove adjacent duplicate elements ; Store the string without duplicate elements ; Store the index of str ; Traverse the string str ; Checks if stack is empty or top of the stack is not equal to current character ; If top element of the stack is equal to the current character ; If stack is empty ; If stack is not Empty ; Driver Code
def ShortenString ( str1 ) : NEW_LINE INDENT st = [ ] NEW_LINE i = 0 NEW_LINE while i < len ( str1 ) : NEW_LINE INDENT if len ( st ) == 0 or str1 [ i ] != st [ - 1 ] : NEW_LINE INDENT st . append ( str1 [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT st . pop ( ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if len ( st ) == 0 : NEW_LINE INDENT return ( " Empty ▁ String " ) NEW_LINE DEDENT else : NEW_LINE INDENT short_string = " " NEW_LINE for i in st : NEW_LINE INDENT short_string += str ( i ) NEW_LINE DEDENT return ( short_string ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " azzxzy " NEW_LINE print ( ShortenString ( str1 ) ) NEW_LINE DEDENT
Count ways to place all the characters of two given strings alternately | Function to get the factorial of N ; Function to get the total number of distinct ways ; Length of str1 ; Length of str2 ; If both strings have equal length ; If both strings do not have equal length ; Driver code
def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def distinctWays ( str1 , str2 ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE m = len ( str2 ) NEW_LINE if ( n == m ) : NEW_LINE INDENT return 2 * fact ( n ) * fact ( m ) NEW_LINE DEDENT return fact ( n ) * fact ( m ) NEW_LINE DEDENT str1 = " aegh " NEW_LINE str2 = " rsw " NEW_LINE print ( distinctWays ( str1 , str2 ) ) NEW_LINE
Smallest string without any multiplication sign that represents the product of two given numbers | Python3 program for the above approach ; Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ logg <= B && 2 ^ ( logg + 1 ) > B ; Update res to res += A X 2 ^ logg ; Update res to res += A X 2 ^ 0 ; Find the remainder ; If remainder is not equal to 0 ; Return the resultant string ; Function to find the minimum length representation of A * B ; Find representation of form A << k1 + A << k2 + ... + A << kn ; Find representation of form B << k1 + B << k2 + ... + B << kn ; Compare the length of the representations ; Driver Code ; Product A X B ; Function call
from math import log NEW_LINE def lenn ( A , B ) : NEW_LINE INDENT res = " " NEW_LINE logg = 0 NEW_LINE while True : NEW_LINE INDENT logg = log ( B ) // log ( 2 ) NEW_LINE if ( logg != 0 ) : NEW_LINE INDENT res += ( str ( A ) + " < < " + str ( int ( logg ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT res += A NEW_LINE break NEW_LINE DEDENT B = B - pow ( 2 , logg ) NEW_LINE if ( B != 0 ) : NEW_LINE INDENT res += " + " NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT if logg == 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def minimumString ( A , B ) : NEW_LINE INDENT res1 = lenn ( A , B ) NEW_LINE res2 = lenn ( B , A ) NEW_LINE if ( len ( res1 ) > len ( res2 ) ) : NEW_LINE INDENT print ( res2 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( res1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 6 NEW_LINE B = 10 NEW_LINE minimumString ( A , B ) NEW_LINE DEDENT
Count substrings of a given string whose anagram is a palindrome | Function to prcount of subStrings whose anagrams are palindromic ; Stores the answer ; Iterate over the String ; Set the current character ; Parity update ; Print final count ; Driver Code ; Function Call
def countSubString ( s ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT x = 0 ; NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT temp = 1 << ord ( s [ j ] ) - ord ( ' a ' ) ; NEW_LINE x ^= temp ; NEW_LINE if ( ( x & ( x - 1 ) ) == 0 ) : NEW_LINE INDENT res += 1 ; NEW_LINE DEDENT DEDENT DEDENT print ( res ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aaa " ; NEW_LINE countSubString ( str ) ; NEW_LINE DEDENT
Count substrings of a given string whose anagram is a palindrome | Python3 program for the above approach ; Function to get the count of substrings whose anagrams are palindromic ; Store the answer ; Map to store the freq of masks ; Set frequency for mask 00. . .00 to 1 ; Store mask in x from 0 to i ; Update answer ; Update frequency ; Print the answer ; Driver Code ; Function call
from collections import defaultdict NEW_LINE def countSubstring ( s ) : NEW_LINE INDENT answer = 0 NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE m [ 0 ] = 1 NEW_LINE x = 0 NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT x ^= 1 << ( ord ( s [ j ] ) - ord ( ' a ' ) ) NEW_LINE answer += m [ x ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT answer += m [ x ^ ( 1 << i ) ] NEW_LINE DEDENT m [ x ] += 1 NEW_LINE DEDENT print ( answer ) NEW_LINE DEDENT str = " abab " NEW_LINE countSubstring ( str ) NEW_LINE
Sum of an array of large numbers | Function to prthe result of the summation of numbers having K - digit ; Reverse the array to obtain the result ; Print every digit of the answer ; Function to calculate the total sum ; Stores the array of large numbers in integer format ; Convert each element from character to integer ; Stores the carry ; Stores the result of summation ; Initialize the sum ; Calculate sum ; Update the sum by adding existing carry ; Store the number of digits ; Increase count of digits ; If the number exceeds 9 , Store the unit digit in carry ; Store the rest of the sum ; Append digit by digit into result array ; Append result until carry is 0 ; Print the result ; Driver Code ; Given N array of large numbers
def printResult ( result ) : NEW_LINE INDENT result = result [ : : - 1 ] NEW_LINE i = 0 NEW_LINE while ( i < len ( result ) ) : NEW_LINE INDENT print ( result [ i ] , end = " " ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def sumOfLargeNumbers ( v , k , N ) : NEW_LINE INDENT x = [ [ ] for i in range ( 1000 ) ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT x [ i ] . append ( ord ( v [ i ] [ j ] ) - ord ( '0' ) ) NEW_LINE DEDENT DEDENT carry = 0 NEW_LINE result = [ ] NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( k ) : NEW_LINE INDENT sum += x [ j ] [ i ] NEW_LINE DEDENT sum += carry NEW_LINE temp = sum NEW_LINE count = 0 NEW_LINE while ( temp > 9 ) : NEW_LINE INDENT temp = temp % 10 NEW_LINE count += 1 NEW_LINE DEDENT l = pow ( 10 , count ) NEW_LINE if ( l != 1 ) : NEW_LINE INDENT carry = sum / l NEW_LINE DEDENT sum = sum % 10 NEW_LINE result . append ( sum ) NEW_LINE DEDENT while ( carry != 0 ) : NEW_LINE INDENT a = carry % 10 NEW_LINE result . append ( a ) NEW_LINE carry = carry // 10 NEW_LINE DEDENT printResult ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 10 NEW_LINE N = 5 NEW_LINE arr = [ "1111111111" , "1111111111" , "1111111111" , "1111111111" , "1111111111" ] NEW_LINE sumOfLargeNumbers ( arr , N , K ) NEW_LINE DEDENT
Reverse words in a given string | Set 2 | Function to reverse the words of a given string ; Stack to store each word of the string ; Store the whole string in stream ; Push each word of the into the stack ; Print the in reverse order of the words ; Driver Code
def printRev ( strr ) : NEW_LINE INDENT strr = strr . split ( " ▁ " ) NEW_LINE st = [ ] NEW_LINE for i in strr : NEW_LINE INDENT st . append ( i ) NEW_LINE DEDENT while len ( st ) > 0 : NEW_LINE INDENT print ( st [ - 1 ] , end = " ▁ " ) NEW_LINE del st [ - 1 ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " geeks ▁ quiz ▁ practice ▁ code " NEW_LINE printRev ( strr ) NEW_LINE DEDENT
Count of substrings of a Binary string containing only 1 s | Function to find the total number of substring having only ones ; Driver Code
def countOfSubstringWithOnlyOnes ( s ) : NEW_LINE INDENT count = 0 NEW_LINE res = 0 NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if s [ i ] == '1' : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = 0 ; NEW_LINE DEDENT res = res + count NEW_LINE DEDENT return res NEW_LINE DEDENT s = "0110111" NEW_LINE print ( countOfSubstringWithOnlyOnes ( s ) ) NEW_LINE
Count of Distinct strings possible by inserting K characters in the original string | Python3 program for the above approach ; Function to calculate and return x ^ n in log ( n ) time using Binary Exponentiation ; Function to calculate the factorial of a number ; Function to calculate combination ; nCi = ( n ! ) / ( ( n - i ) ! * i ! ) ; Using Euler 's theorem of Modular  multiplicative inverse to find  the inverse of a number.  (1/a)%mod=a^(m?2)%mod ; Function to find the count of possible strings ; Number of ways to form all possible strings ; Number of ways to form strings that don 't contain the input  string as a subsequence ; Checking for all prefix length from 0 to | S | - 1. ; To calculate nCi ; Select the remaining characters 25 ^ ( N - i ) ; Add the answer for this prefix length to the final answer ; Answer is the difference of allWays and noWays ; Print the answer ; Driver Code
mod = 1000000007 NEW_LINE def binExp ( base , power ) : NEW_LINE INDENT x = 1 NEW_LINE while ( power ) : NEW_LINE INDENT if ( power % 2 == 1 ) : NEW_LINE INDENT x = ( ( ( x % mod ) * ( base % mod ) ) % mod ) NEW_LINE DEDENT base = ( ( ( base % mod ) * ( base % mod ) ) % mod ) NEW_LINE power = power // 2 NEW_LINE DEDENT return x NEW_LINE DEDENT def fact ( num ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 1 , num + 1 ) : NEW_LINE INDENT result = ( ( ( result % mod ) * ( i % mod ) ) % mod ) NEW_LINE DEDENT return result NEW_LINE DEDENT def calculate_nCi ( N , i ) : NEW_LINE INDENT nfact = fact ( N ) NEW_LINE ifact = fact ( i ) NEW_LINE dfact = fact ( N - i ) NEW_LINE inv_ifact = binExp ( ifact , mod - 2 ) NEW_LINE inv_dfact = binExp ( dfact , mod - 2 ) NEW_LINE denm = ( ( ( inv_ifact % mod ) * ( inv_dfact % mod ) ) % mod ) NEW_LINE answer = ( ( ( nfact % mod ) * ( denm % mod ) ) % mod ) NEW_LINE return answer NEW_LINE DEDENT def countSubstring ( N , s , k ) : NEW_LINE INDENT allWays = binExp ( 26 , N ) NEW_LINE noWays = 0 NEW_LINE for i in range ( s ) : NEW_LINE INDENT nCi = calculate_nCi ( N , i ) NEW_LINE remaining = binExp ( 25 , N - i ) NEW_LINE multiply = ( ( ( nCi % mod ) * ( remaining % mod ) ) % mod ) NEW_LINE noWays = ( ( ( noWays % mod ) + ( multiply % mod ) ) % mod ) NEW_LINE DEDENT answer = ( ( ( allWays % mod ) - ( noWays % mod ) ) % mod ) NEW_LINE if ( answer < 0 ) : NEW_LINE INDENT answer += mod NEW_LINE DEDENT print ( answer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " abc " NEW_LINE k = 2 NEW_LINE s = len ( st ) NEW_LINE N = s + k NEW_LINE countSubstring ( N , s , k ) NEW_LINE DEDENT
Calculate Sum of ratio of special characters to length of substrings of the given string | Python3 program to implement the above approach ; Stores frequency of special characters in the array ; Stores prefix sum ; Function to check whether a character is special or not ; If current character is special ; Otherwise ; Function to find sum of ratio of count of special characters and length of substrings ; Calculate the prefix sum of special nodes ; Generate prefix sum array ; Calculate ratio for substring ; Driver Code
N = 100005 NEW_LINE prefix = [ 0 ] * N NEW_LINE sum = [ 0 ] * N NEW_LINE def isSpecial ( c , special ) : NEW_LINE INDENT for i in special : NEW_LINE INDENT if ( i == c ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def countRatio ( s , special ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix [ i ] = int ( isSpecial ( s [ i ] , special ) ) NEW_LINE if ( i > 0 ) : NEW_LINE INDENT prefix [ i ] += prefix [ i - 1 ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT sum [ i ] = prefix [ i ] NEW_LINE if ( i > 0 ) : NEW_LINE INDENT sum [ i ] += sum [ i - 1 ] NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if i > 1 : NEW_LINE INDENT count = sum [ n - 1 ] - sum [ i - 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT count = sum [ n - 1 ] NEW_LINE DEDENT if i < n : NEW_LINE INDENT count -= sum [ n - i - 1 ] NEW_LINE DEDENT ans += count / i NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abcd " NEW_LINE special = [ ' b ' , ' c ' ] NEW_LINE ans = countRatio ( s , special ) NEW_LINE print ( ' % .6f ' % ans ) NEW_LINE DEDENT
Minimum replacements in a string to make adjacent characters unequal | Function which counts the minimum number of required operations ; n stores the length of the string s ; ans will store the required ans ; i is the current index in the string ; Move j until characters s [ i ] & s [ j ] are equal or the end of the string is reached ; diff stores the length of the substring such that all the characters are equal in it ; We need atleast diff / 2 operations for this substring ; Driver code
def count_minimum ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT j = i NEW_LINE while j < n and ( s [ j ] == s [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT diff = j - i NEW_LINE ans += diff // 2 NEW_LINE i = j NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " caaab " NEW_LINE count_minimum ( str ) NEW_LINE DEDENT
Minimum cost to convert given string to consist of only vowels | Function to find the minimum cost ; Store vowels ; Loop for iteration of string ; Loop to calculate the cost ; Add minimum cost ; Given String ; Function Call
def min_cost ( st ) : NEW_LINE INDENT vow = " aeiou " NEW_LINE cost = 0 NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT costs = [ ] NEW_LINE for j in range ( 5 ) : NEW_LINE INDENT costs . append ( abs ( ord ( st [ i ] ) - ord ( vow [ j ] ) ) ) NEW_LINE DEDENT cost += min ( costs ) NEW_LINE DEDENT return cost NEW_LINE DEDENT str = " abcde " NEW_LINE print ( min_cost ( str ) ) NEW_LINE
Check if a given string is Even | Function to check if the string str is palindromic or not ; Pointers to iterate the string from both ends ; If characters are found to be distinct ; Return true if the string is palindromic ; Function to generate string from characters at odd indices ; Function to generate string from characters at even indices ; Functions to checks if string is Even - Odd Palindrome or not ; Generate odd indexed string ; Generate even indexed string ; Check for Palindrome ; Driver code
def isPalindrome ( Str ) : NEW_LINE INDENT l = 0 NEW_LINE h = len ( Str ) - 1 NEW_LINE while ( h > l ) : NEW_LINE INDENT if ( Str [ l ] != Str [ h ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT l += 1 NEW_LINE h -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def makeOddString ( Str ) : NEW_LINE INDENT odd = " " NEW_LINE for i in range ( 1 , len ( Str ) , 2 ) : NEW_LINE INDENT odd += Str [ i ] NEW_LINE DEDENT return odd NEW_LINE DEDENT def makeevenString ( Str ) : NEW_LINE INDENT even = " " NEW_LINE for i in range ( 0 , len ( Str ) , 2 ) : NEW_LINE INDENT even += Str [ i ] NEW_LINE DEDENT return even NEW_LINE DEDENT def checkevenOddPalindrome ( Str ) : NEW_LINE INDENT odd = makeOddString ( Str ) NEW_LINE even = makeevenString ( Str ) NEW_LINE if ( isPalindrome ( odd ) and isPalindrome ( even ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT Str = " abzzab " NEW_LINE checkevenOddPalindrome ( Str ) NEW_LINE
Check given string is oddly palindrome or not | Function to check if the string str is palindromic or not ; Iterate the string str from left and right pointers ; Keep comparing characters while they are same ; If they are not same then return false ; Return true if the string is palindromic ; Function to make string using odd indices of string str ; Functions checks if characters at odd index of the string forms palindrome or not ; Make odd indexed string ; Check for Palindrome ; Given string ; Function call
def isPalindrome ( str ) : NEW_LINE INDENT l = 0 ; NEW_LINE h = len ( str ) - 1 ; NEW_LINE while ( h > l ) : NEW_LINE INDENT if ( str [ l ] != str [ h ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT l += 1 NEW_LINE h -= 1 NEW_LINE DEDENT return True ; NEW_LINE DEDENT def makeOddString ( str ) : NEW_LINE INDENT odd = " " ; NEW_LINE for i in range ( 1 , len ( str ) , 2 ) : NEW_LINE INDENT odd += str [ i ] ; NEW_LINE DEDENT return odd ; NEW_LINE DEDENT def checkOddlyPalindrome ( str ) : NEW_LINE INDENT odd = makeOddString ( str ) ; NEW_LINE if ( isPalindrome ( odd ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT str = " ddwfefwde " ; NEW_LINE checkOddlyPalindrome ( str ) ; NEW_LINE
Count of binary strings of given length consisting of at least one 1 | Function to return the count of Strings ; Calculate pow ( 2 , n ) ; Return pow ( 2 , n ) - 1 ; Driver Code
def count_Strings ( n ) : NEW_LINE INDENT x = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT x = ( 1 << x ) ; NEW_LINE DEDENT return x - 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 ; NEW_LINE print ( count_Strings ( n ) ) ; NEW_LINE DEDENT
String formed with middle character of every right substring followed by left sequentially | Function to decrypt and print the new string ; If the whole string has been traversed ; To calculate middle index of the string ; Print the character at middle index ; Recursively call for right - substring ; Recursive call for left - substring ; Driver Code
def decrypt ( Str , Start , End ) : NEW_LINE INDENT if ( Start > End ) : NEW_LINE INDENT return ; NEW_LINE DEDENT mid = ( Start + End ) >> 1 ; NEW_LINE print ( Str [ mid ] , end = " " ) ; NEW_LINE decrypt ( Str , mid + 1 , End ) ; NEW_LINE decrypt ( Str , Start , mid - 1 ) ; NEW_LINE DEDENT N = 4 ; NEW_LINE Str = " abcd " ; NEW_LINE decrypt ( Str , 0 , N - 1 ) ; NEW_LINE print ( ) ; NEW_LINE N = 6 ; NEW_LINE Str = " gyuitp " ; NEW_LINE decrypt ( Str , 0 , N - 1 ) ; NEW_LINE
Check whether the binary equivalent of a number ends with given string or not | Function returns true if s1 is suffix of s2 ; Function to check if binary equivalent of a number ends in "111" or not ; To store the binary number ; Count used to store exponent value ; Driver code
def isSuffix ( s1 , s2 ) : NEW_LINE INDENT n1 = len ( s1 ) NEW_LINE n2 = len ( s2 ) NEW_LINE if ( n1 > n2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( n1 ) : NEW_LINE INDENT if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def CheckBinaryEquivalent ( N , s ) : NEW_LINE INDENT B_Number = 0 ; NEW_LINE cnt = 0 ; NEW_LINE while ( N != 0 ) : NEW_LINE INDENT rem = N % 2 ; NEW_LINE c = pow ( 10 , cnt ) ; NEW_LINE B_Number += rem * c ; NEW_LINE N //= 2 ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT bin = str ( B_Number ) ; NEW_LINE return isSuffix ( s , bin ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 23 ; NEW_LINE s = "111" ; NEW_LINE if ( CheckBinaryEquivalent ( N , s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of substrings consisting only of vowels | Function to check if a character is vowel or not ; Function to check whether string contains only vowel ; Check if the character is not vowel then invalid ; Function to Count all substrings in a string which contains only vowels ; Generate all substring of s ; If temp contains only vowels ; Increment the count ; Driver code
def isvowel ( ch ) : NEW_LINE INDENT return ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) NEW_LINE DEDENT def isvalid ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not isvowel ( s [ i ] ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def CountTotal ( s ) : NEW_LINE INDENT ans = 0 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp = " " NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT temp += s [ j ] NEW_LINE if ( isvalid ( temp ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aeoibsddaaeiouudb " NEW_LINE print ( CountTotal ( s ) ) NEW_LINE DEDENT
Longest substring of vowels with no two adjacent alphabets same | Function to check a character is vowel or not ; Function to check a substring is valid or not ; If size is 1 then check only first character ; If 0 'th character is  not vowel then invalid ; If two adjacent characters are same or i 'th char is  not vowel then invalid ; Function to find length of longest substring consisting only of vowels and no similar adjacent alphabets ; Stores max length of valid substring ; For current substring ; Check if substring is valid ; Size of substring is ( j - i + 1 ) ; Driver code
def isVowel ( c ) : NEW_LINE INDENT return ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) NEW_LINE DEDENT def isValid ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return ( isVowel ( s [ 0 ] ) ) NEW_LINE DEDENT if ( isVowel ( s [ 0 ] ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == s [ i - 1 ] or not isVowel ( s [ i ] ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findMaxLen ( s ) : NEW_LINE INDENT maxLen = 0 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp = " " NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT temp = temp + s [ j ] NEW_LINE if ( isValid ( temp ) ) : NEW_LINE INDENT maxLen = ( max ( maxLen , ( j - i + 1 ) ) ) NEW_LINE DEDENT DEDENT DEDENT return maxLen NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Str = " aeoibsddaeiouudb " NEW_LINE print ( findMaxLen ( Str ) ) NEW_LINE DEDENT