codeparrot/xlcost-text-to-code huggingface dataset:
Given two strings check which string makes a palindrome first | Given two strings , check which string makes palindrome first . ; returns winner of two strings ; Count frequencies of characters in both given strings ; Check if there is a character that appears more than once in A and does not appear in B ; Driver Code
MAX_CHAR = 26 NEW_LINE def stringPalindrome ( A , B ) : NEW_LINE INDENT countA = [ 0 ] * MAX_CHAR NEW_LINE countB = [ 0 ] * MAX_CHAR NEW_LINE l1 = len ( A ) NEW_LINE l2 = len ( B ) NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT countA [ ord ( A [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( l2 ) : NEW_LINE INDENT countB [ ord ( B [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( ( countA [ i ] > 1 and countB [ i ] == 0 ) ) : NEW_LINE INDENT return ' A ' NEW_LINE DEDENT DEDENT return ' B ' NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = " abcdea " NEW_LINE b = " bcdesg " NEW_LINE print ( stringPalindrome ( a , b ) ) NEW_LINE DEDENT
Print the longest common substring | function to find and print the longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Note that LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; To store the index of the cell which contains the maximum value . This cell 's index helps in building  up the longest common substring  from right to left. ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion . ; if true , then no common substring exists ; allocate space for the longest common substring ; traverse up diagonally form the ( row , col ) cell until LCSuff [ row ] [ col ] != 0 ; move diagonally up to previous cell ; required longest common substring ; Driver Code
def printLCSSubStr ( X : str , Y : str , m : int , n : int ) : NEW_LINE INDENT LCSuff = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ] NEW_LINE length = 0 NEW_LINE row , col = 0 , 0 NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT if i == 0 or j == 0 : NEW_LINE INDENT LCSuff [ i ] [ j ] = 0 NEW_LINE DEDENT elif X [ i - 1 ] == Y [ j - 1 ] : NEW_LINE INDENT LCSuff [ i ] [ j ] = LCSuff [ i - 1 ] [ j - 1 ] + 1 NEW_LINE if length < LCSuff [ i ] [ j ] : NEW_LINE INDENT length = LCSuff [ i ] [ j ] NEW_LINE row = i NEW_LINE col = j NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT LCSuff [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT if length == 0 : NEW_LINE INDENT print ( " No ▁ Common ▁ Substring " ) NEW_LINE return NEW_LINE DEDENT resultStr = [ '0' ] * length NEW_LINE while LCSuff [ row ] [ col ] != 0 : NEW_LINE INDENT length -= 1 NEW_LINE row -= 1 NEW_LINE col -= 1 NEW_LINE DEDENT print ( ' ' . join ( resultStr ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = " OldSite : GeeksforGeeks . org " NEW_LINE Y = " NewSite : GeeksQuiz . com " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE printLCSSubStr ( X , Y , m , n ) NEW_LINE DEDENT
Convert all substrings of length ' k ' from base ' b ' to decimal | Simple Python3 program to convert all substrings from decimal to given base . ; Saving substring in sub ; Evaluating decimal for current substring and printing it . ; Driver code
import math NEW_LINE def substringConversions ( s , k , b ) : NEW_LINE INDENT l = len ( s ) ; NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( ( i + k ) < l + 1 ) : NEW_LINE INDENT sub = s [ i : i + k ] ; NEW_LINE sum , counter = 0 , 0 ; NEW_LINE for i in range ( len ( sub ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum = sum + ( ( ord ( sub [ i ] ) - ord ( '0' ) ) * pow ( b , counter ) ) ; NEW_LINE counter += 1 ; NEW_LINE DEDENT print ( sum , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT s = "12212" ; NEW_LINE b , k = 3 , 3 ; NEW_LINE substringConversions ( s , b , k ) ; NEW_LINE
Longest Possible Chunked Palindrome | Here curr_str is the string whose LCP is needed leng is length of string evaluated till now and s is original string ; If there is nothing at all ! ! ; If a single letter is left out ; For each length of substring chunk in string ; If left side chunk and right side chunk are same ; Call LCP for the part between the chunks and add 2 to the result . Length of string evaluated till now is increased by ( i + 1 ) * 2 ; Wrapper over LPCRec ( ) ; Driver code
def LPCRec ( curr_str , count , leng , s ) : NEW_LINE INDENT if not curr_str : NEW_LINE INDENT return 0 NEW_LINE DEDENT if len ( curr_str ) <= 1 : NEW_LINE INDENT if count != 0 and len ( s ) - leng <= 1 : NEW_LINE INDENT return ( count + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT n = len ( curr_str ) NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT if ( curr_str [ 0 : i + 1 ] == curr_str [ n - 1 - i : n ] ) : NEW_LINE INDENT return LPCRec ( curr_str [ i + 1 : n - 1 - i ] , count + 2 , leng + ( i + 1 ) * 2 , s ) NEW_LINE DEDENT DEDENT return count + 1 NEW_LINE DEDENT def LPC ( s ) : NEW_LINE INDENT return LPCRec ( s , 0 , 0 , s ) NEW_LINE DEDENT print ( " V ▁ : " , LPC ( " V " ) ) NEW_LINE print ( " VOLVO ▁ : " , LPC ( " VOLVO " ) ) NEW_LINE print ( " VOLVOV ▁ : " , LPC ( " VOLVOV " ) ) NEW_LINE print ( " ghiabcdefhelloadamhelloabcdefghi ▁ : " , LPC ( " ghiabcdefhelloadamhelloabcdefghi " ) ) NEW_LINE print ( " ghiabcdefhelloadamhelloabcdefghik ▁ : " , LPC ( " ghiabcdefhelloadamhelloabcdefghik " ) ) NEW_LINE print ( " antaprezatepzapreanta ▁ : " , LPC ( " antaprezatepzapreanta " ) ) NEW_LINE
Find numbers of balancing positions in string | Python3 program to find number of balancing points in string ; function to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; Driver Code
MAX_CHAR = 256 NEW_LINE def countBalance ( string ) : NEW_LINE INDENT leftVisited = [ 0 ] * ( MAX_CHAR ) NEW_LINE rightVisited = [ 0 ] * ( MAX_CHAR ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT rightVisited [ ord ( string [ i ] ) ] += 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT leftVisited [ ord ( string [ i ] ) ] += 1 NEW_LINE rightVisited [ ord ( string [ i ] ) ] -= 1 NEW_LINE j = 0 NEW_LINE while j < MAX_CHAR : NEW_LINE INDENT if ( ( leftVisited [ j ] == 0 and rightVisited [ j ] != 0 ) or ( leftVisited [ j ] != 0 and rightVisited [ j ] == 0 ) ) : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if j == MAX_CHAR : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abaababa " NEW_LINE print ( countBalance ( string ) ) NEW_LINE DEDENT
Min flips of continuous characters to make all characters same in a string | To find min number of flips in binary string ; If last character is not equal to str [ i ] increase res ; To return min flips ; Driver Code
def findFlips ( str , n ) : NEW_LINE INDENT last = ' ▁ ' NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( last != str [ i ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT last = str [ i ] NEW_LINE DEDENT return res // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "00011110001110" NEW_LINE n = len ( str ) NEW_LINE print ( findFlips ( str , n ) ) NEW_LINE DEDENT
Maximum length substring having all same characters after k changes | function to find the maximum length of substring having character ch ; traverse the whole string ; if character is not same as ch increase count ; While count > k traverse the string again until count becomes less than k and decrease the count when characters are not same ; length of substring will be rightIndex - leftIndex + 1. Compare this with the maximum length and return maximum length ; function which returns maximum length of substring ; Driver Code
def findLen ( A , n , k , ch ) : NEW_LINE INDENT maxlen = 1 NEW_LINE cnt = 0 NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE while r < n : NEW_LINE INDENT if A [ r ] != ch : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT while cnt > k : NEW_LINE INDENT if A [ l ] != ch : NEW_LINE INDENT cnt -= 1 NEW_LINE DEDENT l += 1 NEW_LINE DEDENT maxlen = max ( maxlen , r - l + 1 ) NEW_LINE r += 1 NEW_LINE DEDENT return maxlen NEW_LINE DEDENT def answer ( A , n , k ) : NEW_LINE INDENT maxlen = 1 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT maxlen = max ( maxlen , findLen ( A , n , k , chr ( i + ord ( ' A ' ) ) ) ) NEW_LINE maxlen = max ( maxlen , findLen ( A , n , k , chr ( i + ord ( ' a ' ) ) ) ) NEW_LINE DEDENT return maxlen NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE k = 2 NEW_LINE A = " ABABA " NEW_LINE print ( " Maximum ▁ length ▁ = " , answer ( A , n , k ) ) NEW_LINE n = 6 NEW_LINE k = 4 NEW_LINE B = " HHHHHH " NEW_LINE print ( " Maximum ▁ length ▁ = " , answer ( B , n , k ) ) NEW_LINE DEDENT
Given a sequence of words , print all anagrams together using STL | Python3 program for finding all anagram pairs in the given array ; Utility function for printing anagram list ; Utility function for storing the vector of strings into HashMap ; Check for sorted string if it already exists ; Push new string to already existing key ; Print utility function for printing all the anagrams ; Driver code ; Initialize vector of strings ; Utility function for storing strings into hashmap
from collections import defaultdict NEW_LINE def printAnagram ( store : dict ) -> None : NEW_LINE INDENT for ( k , v ) in store . items ( ) : NEW_LINE INDENT temp_vec = v NEW_LINE size = len ( temp_vec ) NEW_LINE if ( size > 1 ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( temp_vec [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT DEDENT def storeInMap ( vec : list ) -> None : NEW_LINE INDENT store = defaultdict ( lambda : list ) NEW_LINE for i in range ( len ( vec ) ) : NEW_LINE INDENT tempString = vec [ i ] NEW_LINE tempString = ' ' . join ( sorted ( tempString ) ) NEW_LINE if ( tempString not in store ) : NEW_LINE INDENT temp_vec = [ ] NEW_LINE temp_vec . append ( vec [ i ] ) NEW_LINE store [ tempString ] = temp_vec NEW_LINE DEDENT else : NEW_LINE INDENT temp_vec = store [ tempString ] NEW_LINE temp_vec . append ( vec [ i ] ) NEW_LINE store [ tempString ] = temp_vec NEW_LINE DEDENT DEDENT printAnagram ( store ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ ] NEW_LINE arr . append ( " geeksquiz " ) NEW_LINE arr . append ( " geeksforgeeks " ) NEW_LINE arr . append ( " abcd " ) NEW_LINE arr . append ( " forgeeksgeeks " ) NEW_LINE arr . append ( " zuiqkeegs " ) NEW_LINE arr . append ( " cat " ) NEW_LINE arr . append ( " act " ) NEW_LINE arr . append ( " tca " ) NEW_LINE storeInMap ( arr ) NEW_LINE DEDENT
Quick way to check if all the characters of a string are same | Function to check is all the characters in string are or not ; Insert characters in the set ; If all characters are same Size of set will always be 1 ; Driver code
def allCharactersSame ( s ) : NEW_LINE INDENT s1 = [ ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT s1 . append ( s [ i ] ) NEW_LINE DEDENT s1 = list ( set ( s1 ) ) NEW_LINE if ( len ( s1 ) == 1 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT Str = " nnnn " NEW_LINE allCharactersSame ( Str ) NEW_LINE
Check if both halves of the string have same set of characters | Python3 program to check if it is possible to split string or not ; Function to check if we can split string or not ; Counter array initialized with 0 ; Length of the string ; Traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; String to be checked
MAX_CHAR = 26 NEW_LINE def checkCorrectOrNot ( s ) : NEW_LINE INDENT global MAX_CHAR NEW_LINE count1 = [ 0 ] * MAX_CHAR NEW_LINE count2 = [ 0 ] * MAX_CHAR NEW_LINE n = len ( s ) NEW_LINE if n == 1 : NEW_LINE INDENT return true NEW_LINE DEDENT i = 0 ; j = n - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT count1 [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE count2 [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE i += 1 ; j -= 1 NEW_LINE DEDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT if count1 [ i ] != count2 [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT s = " ababc " NEW_LINE print ( " Yes " if checkCorrectOrNot ( s ) else " No " ) NEW_LINE
Extract maximum numeric value from a given string | Set 1 ( General approach ) | Utility function to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Function to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Driver Code
def maximumNum ( curr_num , res ) : NEW_LINE INDENT len1 = len ( curr_num ) ; NEW_LINE len2 = len ( res ) ; NEW_LINE if ( len1 == len2 ) : NEW_LINE INDENT i = 0 ; NEW_LINE while ( curr_num [ i ] == res [ i ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( curr_num [ i ] < res [ i ] ) : NEW_LINE INDENT return res ; NEW_LINE DEDENT else : NEW_LINE INDENT return curr_num ; NEW_LINE DEDENT DEDENT return res if ( len1 < len2 ) else curr_num ; NEW_LINE DEDENT def extractMaximum ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE curr_num = " " ; NEW_LINE res = " " ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( i < n and str [ i ] == '0' ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT while ( i < n and str [ i ] >= '0' and str [ i ] <= '9' ) : NEW_LINE INDENT curr_num += str [ i ] ; NEW_LINE i += 1 ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT if ( len ( curr_num ) > 0 ) : NEW_LINE INDENT i -= 1 ; NEW_LINE DEDENT res = maximumNum ( curr_num , res ) ; NEW_LINE curr_num = " " ; NEW_LINE DEDENT if ( len ( curr_num ) == 0 and len ( res ) == 0 ) : NEW_LINE INDENT res += '0' ; NEW_LINE DEDENT return maximumNum ( curr_num , res ) ; NEW_LINE DEDENT str = "100klh564abc365bg " ; NEW_LINE print ( extractMaximum ( str ) ) ; NEW_LINE
To check divisibility of any large number by 999 | function to check divisibility ; Append required 0 s at the beginning . ; add digits in group of three in gSum ; group saves 3 - digit group ; calculate result till 3 digit sum ; Driver code
def isDivisible999 ( num ) : NEW_LINE INDENT n = len ( num ) ; NEW_LINE if ( n == 0 or num [ 0 ] == '0' ) : NEW_LINE INDENT return true NEW_LINE DEDENT if ( ( n % 3 ) == 1 ) : NEW_LINE INDENT num = "00" + num NEW_LINE DEDENT if ( ( n % 3 ) == 2 ) : NEW_LINE INDENT num = "0" + num NEW_LINE DEDENT gSum = 0 NEW_LINE for i in range ( 0 , n , 3 ) : NEW_LINE INDENT group = 0 NEW_LINE group += ( ord ( num [ i ] ) - 48 ) * 100 NEW_LINE group += ( ord ( num [ i + 1 ] ) - 48 ) * 10 NEW_LINE group += ( ord ( num [ i + 2 ] ) - 48 ) NEW_LINE gSum += group NEW_LINE DEDENT if ( gSum > 1000 ) : NEW_LINE INDENT num = str ( gSum ) NEW_LINE n = len ( num ) NEW_LINE gSum = isDivisible999 ( num ) NEW_LINE DEDENT return ( gSum == 999 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT num = "1998" NEW_LINE n = len ( num ) NEW_LINE if ( isDivisible999 ( num ) ) : NEW_LINE INDENT print ( " Divisible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ divisible " ) NEW_LINE DEDENT DEDENT
Rearrange a string in sorted order followed by the integer sum | Python3 program for above implementation ; Function to return string in lexicographic order followed by integers sum ; Traverse the string ; Count occurrence of uppercase alphabets ; Store sum of integers ; Traverse for all characters A to Z ; Append the current character in the string no . of times it occurs in the given string ; Append the sum of integers ; return resultant string ; Driver code
MAX_CHAR = 26 NEW_LINE def arrangeString ( string ) : NEW_LINE INDENT char_count = [ 0 ] * MAX_CHAR NEW_LINE s = 0 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if string [ i ] >= " A " and string [ i ] <= " Z " : NEW_LINE INDENT char_count [ ord ( string [ i ] ) - ord ( " A " ) ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT s += ord ( string [ i ] ) - ord ( "0" ) NEW_LINE DEDENT DEDENT res = " " NEW_LINE for i in range ( MAX_CHAR ) : NEW_LINE INDENT ch = chr ( ord ( " A " ) + i ) NEW_LINE while char_count [ i ] : NEW_LINE INDENT res += ch NEW_LINE char_count [ i ] -= 1 NEW_LINE DEDENT DEDENT if s > 0 : NEW_LINE INDENT res += str ( s ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " ACCBA10D2EW30" NEW_LINE print ( arrangeString ( string ) ) NEW_LINE DEDENT
URLify a given string ( Replace spaces is % 20 ) | Instantiate the string ; Trim the given string ; Replace All space ( unicode is \\ s ) to % 20 ; Display the result
s = " Mr ▁ John ▁ Smith ▁ " NEW_LINE s = s . strip ( ) NEW_LINE s = s . replace ( ' ▁ ' , " % 20" ) NEW_LINE print ( s ) NEW_LINE
Program to print all substrings of a given string | Function to print all sub strings ; Pick starting point ; Pick ending point ; Print characters from current starting point to current ending point . ; Driver program to test above function
def subString ( Str , n ) : NEW_LINE INDENT for Len in range ( 1 , n + 1 ) : NEW_LINE INDENT for i in range ( n - Len + 1 ) : NEW_LINE INDENT j = i + Len - 1 NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT print ( Str [ k ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT DEDENT Str = " abc " NEW_LINE subString ( Str , len ( Str ) ) NEW_LINE
Reverse a string preserving space positions | Function to reverse the string and preserve the space position ; Mark spaces in result ; Traverse input string from beginning and put characters in result from end ; Ignore spaces in input string ; Ignore spaces in result . ; Driver code
def reverses ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE result = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( st [ i ] == ' ▁ ' ) : NEW_LINE INDENT result [ i ] = ' ▁ ' NEW_LINE DEDENT DEDENT j = n - 1 NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT if ( st [ i ] != ' ▁ ' ) : NEW_LINE INDENT if ( result [ j ] == ' ▁ ' ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT result [ j ] = st [ i ] NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT return ' ' . join ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " internship ▁ at ▁ geeks ▁ for ▁ geeks " NEW_LINE print ( reverses ( st ) ) NEW_LINE DEDENT
Find uncommon characters of the two strings | size of the hash table ; function to find the uncommon characters of the two strings ; mark presence of each character as 0 in the hash table 'present[] ; for each character of str1 , mark its presence as 1 in 'present[] ; for each character of str2 ; if a character of str2 is also present in str1 , then mark its presence as - 1 ; else mark its presence as 2 ; print all the uncommon characters ; Driver Code
MAX_CHAR = 26 NEW_LINE def findAndPrintUncommonChars ( str1 , str2 ) : NEW_LINE ' NEW_LINE INDENT present = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT present [ i ] = 0 NEW_LINE DEDENT l1 = len ( str1 ) NEW_LINE l2 = len ( str2 ) NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( 0 , l1 ) : NEW_LINE INDENT present [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] = 1 NEW_LINE DEDENT for i in range ( 0 , l2 ) : NEW_LINE INDENT if ( present [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] == 1 or present [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] == - 1 ) : NEW_LINE INDENT present [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT present [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] = 2 NEW_LINE DEDENT DEDENT for i in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT if ( present [ i ] == 1 or present [ i ] == 2 ) : NEW_LINE INDENT print ( chr ( i + ord ( ' a ' ) ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " characters " NEW_LINE str2 = " alphabets " NEW_LINE findAndPrintUncommonChars ( str1 , str2 ) NEW_LINE DEDENT
Length Of Last Word in a String | Python3 program for implementation of simple approach to find length of last word ; String a is ' final ' -- can not be modified So , create a copy and trim the spaces from both sides ; Driver code
def lengthOfLastWord ( a ) : NEW_LINE INDENT l = 0 NEW_LINE x = a . strip ( ) NEW_LINE for i in range ( len ( x ) ) : NEW_LINE INDENT if x [ i ] == " ▁ " : NEW_LINE INDENT l = 0 NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 NEW_LINE DEDENT DEDENT return l NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT inp = " Geeks ▁ For ▁ Geeks ▁ " NEW_LINE print ( " The ▁ length ▁ of ▁ last ▁ word ▁ is " , lengthOfLastWord ( inp ) ) NEW_LINE DEDENT
Length Of Last Word in a String | Python3 program for implementation of efficient approach to find length of last word ; Split by space and converting String to list and ; Driver code
def length ( str ) : NEW_LINE INDENT lis = list ( str . split ( " ▁ " ) ) NEW_LINE return len ( lis [ - 1 ] ) NEW_LINE DEDENT str = " Geeks ▁ for ▁ Geeks " NEW_LINE print ( " The ▁ length ▁ of ▁ last ▁ word ▁ is " , length ( str ) ) NEW_LINE
Program to count vowels in a string ( Iterative and Recursive ) | Function to check the Vowel ; Returns count of vowels in str ; Check for vowel ; string object ; Total number of Vowels
def isVowel ( ch ) : NEW_LINE INDENT return ch . upper ( ) in [ ' A ' , ' E ' , ' I ' , ' O ' , ' U ' ] NEW_LINE DEDENT def countVowels ( str ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if isVowel ( str [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT str = ' abc ▁ de ' NEW_LINE print ( countVowels ( str ) ) NEW_LINE
Toggle case of a string using Bitwise Operators | Python3 program to get toggle case of a string ; tOGGLE cASE = swaps CAPS to lower case and lower case to CAPS ; Bitwise EXOR with 32 ; Driver Code
x = 32 ; NEW_LINE def toggleCase ( a ) : NEW_LINE INDENT for i in range ( len ( a ) ) : NEW_LINE INDENT a = a [ : i ] + chr ( ord ( a [ i ] ) ^ 32 ) + a [ i + 1 : ] ; NEW_LINE DEDENT return a ; NEW_LINE DEDENT str = " CheRrY " ; NEW_LINE print ( " Toggle ▁ case : ▁ " , end = " " ) ; NEW_LINE str = toggleCase ( str ) ; NEW_LINE print ( str ) ; NEW_LINE print ( " Original ▁ string : ▁ " , end = " " ) ; NEW_LINE str = toggleCase ( str ) ; NEW_LINE print ( str ) ; NEW_LINE
Determine if a string has all Unique Characters | Python3 program to illustrate String with unique characters ; Converting string to set ; If length of set is equal to len of string then it will have unique characters ; Driver Code
def uniqueCharacters ( str ) : NEW_LINE INDENT setstring = set ( str ) NEW_LINE if ( len ( setstring ) == len ( str ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT input = " GeeksforGeeks " NEW_LINE if ( uniqueCharacters ( input ) ) : NEW_LINE INDENT print ( " The ▁ String ▁ " + input + " ▁ has ▁ all ▁ unique ▁ characters " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ String ▁ " + input + " ▁ has ▁ duplicate ▁ characters " ) NEW_LINE DEDENT DEDENT