codeparrot/xlcost-text-to-code huggingface dataset:
Split a string in equal parts such that all parts are palindromes | Function to return the frequency array for the given string ; Function to return the required count ; Add frequencies of the even appearing characters ; Count of the characters that appeared odd number of times ; If there are no characters with odd frequency ; If there are no characters with even frequency ; Only a single character with odd frequency ; More than 1 character with odd frequency string isn 't a palindrome ; All odd appearing characters can also contribute to the even length palindrome if one character is removed from the frequency leaving it as even ; If k palindromes are possible where k is the number of characters with odd frequency ; Current character can no longer be an element in a string other than the mid character ; If current character has odd frequency > 1 take two characters which can be used in any of the parts ; Update the frequency ; If not possible , then every character of the string will act as a separate palindrome ; Driver code
def getFrequencies ( string ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT return freq NEW_LINE DEDENT def countMinParts ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE freq = getFrequencies ( string ) NEW_LINE oddFreq = [ ] NEW_LINE evenFreq = [ ] NEW_LINE sumEven = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if freq [ i ] == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT if freq [ i ] % 2 == 0 : NEW_LINE INDENT evenFreq . append ( freq [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT oddFreq . append ( freq [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( evenFreq ) ) : NEW_LINE INDENT sumEven += evenFreq [ i ] NEW_LINE DEDENT if len ( oddFreq ) == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if sumEven == 0 : NEW_LINE INDENT if len ( oddFreq ) == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT i = 0 NEW_LINE while ( i < len ( oddFreq ) ) : NEW_LINE INDENT if ( ( sumEven / 2 ) % len ( oddFreq ) == 0 ) : NEW_LINE INDENT return len ( oddFreq ) NEW_LINE DEDENT if ( oddFreq [ i ] == 1 ) : NEW_LINE INDENT i += 1 NEW_LINE continue NEW_LINE DEDENT sumEven += 2 NEW_LINE oddFreq [ i ] = oddFreq [ i ] - 2 NEW_LINE DEDENT return n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " noonpeep " NEW_LINE print ( countMinParts ( s ) ) NEW_LINE DEDENT
Substring Reverse Pattern | Function to print the required pattern ; Print the unmodified string ; Reverse the string ; Replace the first and last character by ' * ' then second and second last character and so on until the string has characters remaining ; Driver code
def printPattern ( s , n ) : NEW_LINE INDENT print ( ' ' . join ( s ) ) NEW_LINE i , j = 0 , n - 1 NEW_LINE while i < j : NEW_LINE INDENT s [ i ] , s [ j ] = s [ j ] , s [ i ] NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT i , j = 0 , n - 1 NEW_LINE while j - i > 1 : NEW_LINE INDENT s [ i ] , s [ j ] = ' * ' , ' * ' NEW_LINE print ( ' ' . join ( s ) ) NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeks " NEW_LINE n = len ( s ) NEW_LINE printPattern ( list ( s ) , n ) NEW_LINE DEDENT
Check if the string satisfies the given condition | Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if c is a vowel ; Function that returns true if the count of vowels in word is prime ; If count of vowels is prime ; Driver code
def prime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def isVowel ( c ) : NEW_LINE INDENT c = c . lower ( ) NEW_LINE 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 isValidString ( word ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( len ( word ) ) : NEW_LINE INDENT if ( isVowel ( word [ i ] ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT if ( prime ( cnt ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE if ( isValidString ( s ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Concatenate suffixes of a String | Function to print the expansion of the string ; Take sub - string from i to n - 1 ; Print the sub - string ; Driver code
def printExpansion ( str ) : NEW_LINE INDENT suff = " " NEW_LINE for i in range ( len ( str ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT suff = suff + str [ i ] NEW_LINE print ( suff , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeks " NEW_LINE printExpansion ( str ) NEW_LINE DEDENT
Solve the Logical Expression given by string | Python3 program to solve the logical expression . ; Function to evaluate the logical expression ; traversing string from the end . ; for NOT operation ; for AND and OR operation ; Driver code
import math as mt NEW_LINE def logicalExpressionEvaluation ( string ) : NEW_LINE INDENT arr = list ( ) NEW_LINE n = len ( string ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( string [ i ] == " [ " ) : NEW_LINE INDENT s = list ( ) NEW_LINE while ( arr [ - 1 ] != " ] " ) : NEW_LINE INDENT s . append ( arr [ - 1 ] ) NEW_LINE arr . pop ( ) NEW_LINE DEDENT arr . pop ( ) NEW_LINE if ( len ( s ) == 3 ) : NEW_LINE INDENT if s [ 2 ] == "1" : NEW_LINE INDENT arr . append ( "0" ) NEW_LINE DEDENT else : NEW_LINE INDENT arr . append ( "1" ) NEW_LINE DEDENT DEDENT elif ( len ( s ) == 5 ) : NEW_LINE INDENT a = int ( s [ 0 ] ) - 48 NEW_LINE b = int ( s [ 4 ] ) - 48 NEW_LINE c = 0 NEW_LINE if s [ 2 ] == " & " : NEW_LINE INDENT c = a & b NEW_LINE DEDENT else : NEW_LINE INDENT c = a | b NEW_LINE DEDENT arr . append ( ( c ) + 48 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT arr . append ( string [ i ] ) NEW_LINE DEDENT DEDENT return arr [ - 1 ] NEW_LINE DEDENT string = " [ | , [ & ,1 , [ ! , 0 ] ] , [ ! , [ | , [ ▁ , 1,0 ] , [ ! ,1 ] ] ] ] " NEW_LINE print ( logicalExpressionEvaluation ( string ) ) NEW_LINE
Find number of substrings of length k whose sum of ASCII value of characters is divisible by k | Python3 program to find number of substrings of length k whose sum of ASCII value of characters is divisible by k ; Finding length of string ; finding sum of ASCII value of first substring ; Using sliding window technique to find sum of ASCII value of rest of the substring ; checking if sum is divisible by k ; Driver code
def count ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE d , count = 0 , 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT d += ord ( s [ i ] ) NEW_LINE if ( d % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT prev = ord ( s [ i - k ] ) NEW_LINE d -= prev NEW_LINE d += ord ( s [ i ] ) NEW_LINE if ( d % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE return count NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT s = " bcgabc " NEW_LINE k = 3 NEW_LINE ans = count ( s , k ) NEW_LINE print ( ans ) NEW_LINE
String which when repeated exactly K times gives a permutation of S | Function to return a string which when repeated exactly k times gives a permutation of s ; size of string ; to frequency of each character ; get frequency of each character ; to store final answer ; check if frequency is divisible by k ; add to answer ; if frequency is not divisible by k ; Driver code ; function call
def K_String ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE fre = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT fre [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT str = " " NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( fre [ i ] % k == 0 ) : NEW_LINE INDENT x = fre [ i ] // k NEW_LINE while ( x ) : NEW_LINE INDENT str += chr ( i + ord ( ' a ' ) ) NEW_LINE x -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT DEDENT return str NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aabb " NEW_LINE k = 2 NEW_LINE print ( K_String ( s , k ) ) NEW_LINE DEDENT
Maximum even length sub | Python3 code to find the maximum length of sub - string ( of even length ) which can be arranged into a Palindrome ; function that returns true if the given sub - string can be arranged into a Palindrome ; This function returns the maximum length of the sub - string ( of even length ) which can be arranged into a Palindrome ; If we reach end of the string ; if string is of even length ; if string can be arranged into a palindrome ; Even length sub - string ; Check if current sub - string can be arranged into a palindrome ; Odd length sub - string ; Driver code
from collections import defaultdict NEW_LINE def canBePalindrome ( count ) : NEW_LINE INDENT for key in count : NEW_LINE INDENT if count [ key ] % 2 != 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def maxPal ( string , count , start , end ) : NEW_LINE INDENT if end == len ( string ) : NEW_LINE INDENT if ( end - start ) % 2 == 0 : NEW_LINE INDENT if canBePalindrome ( count ) == True : NEW_LINE INDENT return end - start NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT if ( end - start ) % 2 == 0 : NEW_LINE INDENT if canBePalindrome ( count ) == True : NEW_LINE INDENT count [ string [ end ] ] += 1 NEW_LINE return max ( end - start , maxPal ( string , count , start , end + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT count [ string [ end ] ] += 1 NEW_LINE return maxPal ( string , count , start , end + 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT count [ string [ end ] ] += 1 NEW_LINE length = maxPal ( string , count . copy ( ) , start , end + 1 ) NEW_LINE count [ string [ end ] ] -= 1 NEW_LINE count [ string [ start ] ] -= 1 NEW_LINE return max ( length , maxPal ( string , count , start + 1 , end ) ) NEW_LINE DEDENT DEDENT DEDENT string = '124565463' NEW_LINE start , end = 0 , 0 NEW_LINE count = defaultdict ( lambda : 0 ) NEW_LINE print ( maxPal ( string , count , start , end ) ) NEW_LINE
Minimum deletions from string to reduce it to string with at most 2 unique characters | Function to find the minimum deletions ; Array to store the occurrences of each characters ; Length of the string ; ASCII of the character ; Increasing the frequency for this character ; Choosing two character ; Finding the minimum deletion ; Driver code
def check ( s ) : NEW_LINE INDENT fr = [ 0 ] * 26 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = ord ( s [ i ] ) NEW_LINE fr [ x - 97 ] += 1 NEW_LINE DEDENT minimum = 99999999999 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT for j in range ( i + 1 , 26 ) : NEW_LINE INDENT z = fr [ i ] + fr [ j ] NEW_LINE minimum = min ( minimum , n - z ) NEW_LINE DEDENT DEDENT return minimum NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE print ( check ( s ) ) NEW_LINE
Count and Print the alphabets having ASCII value not in the range [ l , r ] | Function to count the number of characters whose ascii value not in range [ l , r ] ; Initializing the count to 0 ; using map to pra character only once ; Increment the count if the value is less ; return the count ; Driver Code
def CountCharacters ( str , l , r ) : NEW_LINE INDENT cnt = 0 NEW_LINE m = { } NEW_LINE length = len ( str ) NEW_LINE for i in range ( 0 , length ) : NEW_LINE INDENT if ( not ( l <= ord ( str [ i ] ) and ord ( str [ i ] ) <= r ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE if ord ( str [ i ] ) not in m : NEW_LINE INDENT m [ ord ( str [ i ] ) ] = 0 NEW_LINE print ( str [ i ] , end = " ▁ " ) NEW_LINE DEDENT m [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE str = str . strip ( ) NEW_LINE l = 102 NEW_LINE r = 111 NEW_LINE print ( " Characters ▁ with ▁ ASCII ▁ values " , end = " " ) NEW_LINE print ( " not in the range [ l , r ] " , ▁ " in the given string are : " , ▁ end ▁ = ▁ " " ) NEW_LINE print ( " and their count is   " , CountCharacters ( str , l , r ) ) NEW_LINE DEDENT
Final state of the string after modification | Function to return final positions of the boxes ; Populate forces going from left to right ; Populate forces going from right to left ; return final state of boxes ; Driver code ; Function call to print answer
def pushBoxes ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE force = [ 0 ] * N NEW_LINE f = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if S [ i ] == ' R ' : NEW_LINE INDENT f = N NEW_LINE DEDENT elif S [ i ] == ' L ' : NEW_LINE INDENT f = 0 NEW_LINE DEDENT else : NEW_LINE INDENT f = max ( f - 1 , 0 ) NEW_LINE DEDENT force [ i ] += f NEW_LINE DEDENT f = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if S [ i ] == ' L ' : NEW_LINE INDENT f = N NEW_LINE DEDENT elif S [ i ] == ' R ' : NEW_LINE INDENT f = 0 NEW_LINE DEDENT else : NEW_LINE INDENT f = max ( f - 1 , 0 ) NEW_LINE DEDENT force [ i ] -= f NEW_LINE DEDENT return " " . join ( ' . ' if f == 0 else ' R ' if f > 0 else ' L ' for f in force ) NEW_LINE DEDENT S = " . L . R . . . LR . . L . . " NEW_LINE print ( pushBoxes ( S ) ) NEW_LINE
Count the number of words having sum of ASCII values less than and greater than k | Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; Add the ascii value to sum ; Handling the Last word separately ; Driver code
def CountWords ( str , k ) : NEW_LINE INDENT sum = 0 NEW_LINE NumberOfWords = 0 NEW_LINE counter = 0 NEW_LINE l = len ( str ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( str [ i ] == ' ▁ ' ) : NEW_LINE INDENT if ( sum < k ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT sum = 0 NEW_LINE NumberOfWords += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum += ord ( str [ i ] ) NEW_LINE DEDENT DEDENT NumberOfWords += 1 NEW_LINE if ( sum < k ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT print ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII " , " values ▁ less ▁ than ▁ k ▁ = " , counter ) NEW_LINE print ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII ▁ values " , " greater ▁ than ▁ or ▁ equal ▁ to ▁ k ▁ = " , NumberOfWords - counter ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " Learn ▁ how ▁ to ▁ code " NEW_LINE k = 400 NEW_LINE CountWords ( str , k ) NEW_LINE DEDENT
Rearrange a string in the form of integer sum followed by the minimized character | function to return maximum volume ; separate digits and alphabets ; change digit sum to string ; change alphabet sum to string ; concatenate sum to alphabets string ; Driver code
def separateChar ( str__ ) : NEW_LINE INDENT n = len ( str__ ) NEW_LINE digitSum = 0 NEW_LINE alphabetSum = 0 NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ord ( str__ [ i ] ) >= 48 and ord ( str__ [ i ] ) <= 56 ) : NEW_LINE INDENT digitSum += ord ( str__ [ i ] ) - ord ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT alphabetSum += ord ( str__ [ i ] ) - ord ( ' a ' ) + 1 NEW_LINE alphabetSum %= 26 NEW_LINE DEDENT DEDENT sumStr = str ( digitSum ) NEW_LINE alphabetStr = chr ( alphabetSum + ord ( ' a ' ) - 1 ) NEW_LINE sumStr += alphabetStr NEW_LINE return sumStr NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str__ = "3652adyz3423" NEW_LINE print ( separateChar ( str__ ) ) NEW_LINE DEDENT
Find the count of palindromic sub | Python3 program to find the count of palindromic sub - string of a string in it 's ascending form ; function to return count of palindromic sub - string ; calculate frequency ; calculate count of palindromic sub - string ; return result ; Driver Code
MAX_CHAR = 26 NEW_LINE def countPalindrome ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE sum = 0 NEW_LINE hashTable = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT hashTable [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( hashTable [ i ] ) : NEW_LINE INDENT sum += ( hashTable [ i ] * ( hashTable [ i ] + 1 ) // 2 ) NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " ananananddd " NEW_LINE print ( countPalindrome ( str ) ) NEW_LINE DEDENT
Minimum number of elements to be removed so that pairwise consecutive elements are same | Function to count the minimum number of elements to remove from a number so that pairwise two consecutive digits are same . ; initialize counting variable ; check if two consecutive digits are same ; Driver code
def countConsecutive ( s ) : NEW_LINE INDENT count = - 1 NEW_LINE for i in range ( len ( s ) - 1 ) : NEW_LINE INDENT if ( i <= len ( s ) ) : NEW_LINE INDENT if ( s [ i ] is s [ i + 1 ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "44522255" NEW_LINE print ( countConsecutive ( str ) ) NEW_LINE DEDENT
Smallest odd digits number not less than N | function to check if all digits are odd of a given number ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the smallest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code
def check_digits ( n ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT if ( ( n % 10 ) % 2 == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT n = int ( n / 10 ) NEW_LINE DEDENT return 1 NEW_LINE DEDENT def smallest_number ( n ) : NEW_LINE INDENT i = n NEW_LINE while ( 1 ) : NEW_LINE INDENT if ( check_digits ( i ) ) : NEW_LINE INDENT return i NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2397 NEW_LINE print ( smallest_number ( N ) ) NEW_LINE DEDENT
Smallest odd digits number not less than N | function to return the smallest number with all digits odd ; convert the number to string to perform operations ; find out the first even number ; if no even numbers are there , than n is the answer ; add all digits till first even ; increase the even digit by 1 ; add 1 to the right of the even number ; Driver Code
def smallestNumber ( n ) : NEW_LINE INDENT num = 0 NEW_LINE s = " " NEW_LINE duplicate = n NEW_LINE while ( n ) : NEW_LINE INDENT s = chr ( n % 10 + 48 ) + s NEW_LINE n //= 10 NEW_LINE DEDENT index = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT digit = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( ( digit & 1 ) == 0 ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return duplicate NEW_LINE DEDENT for i in range ( index ) : NEW_LINE INDENT num = num * 10 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT num = num * 10 + ( ord ( s [ index ] ) - ord ( '0' ) + 1 ) NEW_LINE for i in range ( index + 1 , len ( s ) ) : NEW_LINE INDENT num = num * 10 + 1 NEW_LINE DEDENT return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2397 NEW_LINE print ( smallestNumber ( N ) ) NEW_LINE DEDENT
Count and Print the alphabets having ASCII value in the range [ l , r ] | Function to count the number of characters whose ascii value is in range [ l , r ] ; Initializing the count to 0 ; Increment the count if the value is less ; return the count ; Driver code
def CountCharacters ( str1 , l , r ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE len1 = len ( str1 ) NEW_LINE for i in str1 : NEW_LINE INDENT if ( l <= ord ( i ) and ord ( i ) <= r ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geeksforgeeks " NEW_LINE l = 102 NEW_LINE r = 111 NEW_LINE print ( " Characters ▁ with ▁ ASCII ▁ values ▁ " + " in ▁ the ▁ range ▁ [ l , ▁ r ] ▁ are " ) NEW_LINE print ( " and their count is   " , CountCharacters ( str1 , l , r ) ) NEW_LINE DEDENT
Minimum steps to remove substring 010 from a binary string | Function to find the minimum steps ; substring "010" found ; Get the binary string ; Find the minimum steps
def minSteps ( str ) : NEW_LINE INDENT count = 0 NEW_LINE i = 0 NEW_LINE while i < len ( str ) - 2 : NEW_LINE INDENT if str [ i ] == '0' : NEW_LINE INDENT if ( str [ i + 1 ] == '1' ) : NEW_LINE INDENT if ( str [ i + 2 ] == '0' ) : NEW_LINE INDENT count = count + 1 NEW_LINE i = i + 2 NEW_LINE DEDENT DEDENT DEDENT i = i + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT str = "0101010" NEW_LINE print ( minSteps ( str ) ) NEW_LINE
XOR of Prime Frequencies of Characters in a String | Python3 program to find XOR of Prime Frequencies of Characters in a String ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find XOR of prime frequencies ; map is used to store character frequencies ; Traverse the map ; Calculate XOR of all prime frequencies ; Driver code
from collections import defaultdict NEW_LINE def SieveOfEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= p_size : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT for i in range ( p * 2 , p_size + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def xorOfPrime ( s ) : NEW_LINE INDENT prime = [ True ] * 100005 NEW_LINE SieveOfEratosthenes ( prime , 10005 ) NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE DEDENT result = flag = 0 NEW_LINE for it in m : NEW_LINE INDENT if prime [ m [ it ] ] : NEW_LINE INDENT result ^= m [ it ] NEW_LINE flag = 1 NEW_LINE DEDENT DEDENT if not flag : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " gggggeeekkkks " NEW_LINE print ( xorOfPrime ( s ) ) NEW_LINE DEDENT
Count of alphabets having ASCII value less than and greater than k | Function to count the number of characters whose ascii value is less than k ; Initialising the count to 0 ; Incrementing the count if the value is less ; return the count ; Driver code
def CountCharacters ( str , k ) : NEW_LINE INDENT cnt = 0 NEW_LINE l = len ( str ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( ord ( str [ i ] ) < k ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " GeeksForGeeks " NEW_LINE k = 90 NEW_LINE count = CountCharacters ( str , k ) NEW_LINE print ( " Characters ▁ with ▁ ASCII ▁ values " , " less ▁ than ▁ K ▁ are " , count ) NEW_LINE print ( " Characters ▁ with ▁ ASCII ▁ values " , " greater ▁ than ▁ or ▁ equal ▁ to ▁ K ▁ are " , len ( str ) - count ) NEW_LINE DEDENT
De Bruijn sequence | Set 1 | Python3 implementation of the above approach ; Modified DFS in which no edge is traversed twice ; Function to find a de Bruijn sequence of order n on k characters ; Clearing global variables ; Number of edges ; Driver code
import math NEW_LINE seen = set ( ) NEW_LINE edges = [ ] NEW_LINE def dfs ( node , k , A ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT str = node + A [ i ] NEW_LINE if ( str not in seen ) : NEW_LINE INDENT seen . add ( str ) NEW_LINE dfs ( str [ 1 : ] , k , A ) NEW_LINE edges . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def deBruijn ( n , k , A ) : NEW_LINE INDENT seen . clear ( ) NEW_LINE edges . clear ( ) NEW_LINE startingNode = A [ 0 ] * ( n - 1 ) NEW_LINE dfs ( startingNode , k , A ) NEW_LINE S = " " NEW_LINE l = int ( math . pow ( k , n ) ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT S += A [ edges [ i ] ] NEW_LINE DEDENT S += startingNode NEW_LINE return S NEW_LINE DEDENT n = 3 NEW_LINE k = 2 NEW_LINE A = "01" NEW_LINE print ( deBruijn ( n , k , A ) ) NEW_LINE