codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number is in given base or not | Python3 program to check if given number is in given base or not . ; Allowed bases are till 16 ( Hexadecimal ) ; If base is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from 'A ; Driver code
def isInGivenBase ( Str , base ) : NEW_LINE INDENT if ( base > 16 ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( base <= 10 ) : NEW_LINE INDENT for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( Str [ i ] . isnumeric ( ) and ( ord ( Str [ i ] ) >= ord ( '0' ) and ord ( Str [ i ] ) < ( ord ( '0' ) + base ) ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT ' NEW_LINE INDENT else : NEW_LINE INDENT for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( Str [ i ] . isnumeric ( ) and ( ( ord ( Str [ i ] ) >= ord ( '0' ) and ord ( Str [ i ] ) < ( ord ( '0' ) + base ) ) or ( ord ( Str [ i ] ) >= ord ( ' A ' ) and ord ( Str [ i ] ) < ( ord ( ' A ' ) + base - 10 ) ) ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT Str = " AF87" NEW_LINE if ( isInGivenBase ( Str , 16 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find indices of all occurrence of one string in other | Python program to find indices of all occurrences of one String in other . ; Driver code
def printIndex ( str , s ) : NEW_LINE INDENT flag = False ; NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i : i + len ( s ) ] == s ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE flag = True ; NEW_LINE DEDENT DEDENT if ( flag == False ) : NEW_LINE INDENT print ( " NONE " ) ; NEW_LINE DEDENT DEDENT str1 = " GeeksforGeeks " ; NEW_LINE str2 = " Geeks " ; NEW_LINE printIndex ( str1 , str2 ) ; NEW_LINE
Alternatively Merge two Strings in Java | Function for alternatively merging two strings ; To store the final string ; For every index in the strings ; First choose the ith character of the first string if it exists ; Then choose the ith character of the second string if it exists ; Driver Code
def merge ( s1 , s2 ) : NEW_LINE INDENT result = " " NEW_LINE i = 0 NEW_LINE while ( i < len ( s1 ) ) or ( i < len ( s2 ) ) : NEW_LINE INDENT if ( i < len ( s1 ) ) : NEW_LINE INDENT result += s1 [ i ] NEW_LINE DEDENT if ( i < len ( s2 ) ) : NEW_LINE INDENT result += s2 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return result NEW_LINE DEDENT s1 = " geeks " NEW_LINE s2 = " forgeeks " NEW_LINE print ( merge ( s1 , s2 ) ) NEW_LINE
Maximum occurring character in an input string | Set | function to find the maximum occurring character in an input string which is lexicographically first ; freq [ ] used as hash table ; to store maximum frequency ; length of str ; get frequency of each character of 'str ; for each character , where character is obtained by ( i + ' a ' ) check whether it is the maximum character so far and accodingly update 'result ; maximum occurring character ; Driver Code
def getMaxOccurringChar ( str ) : NEW_LINE INDENT freq = [ 0 for i in range ( 100 ) ] NEW_LINE max = - 1 NEW_LINE len__ = len ( str ) NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( 0 , len__ , 1 ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT for i in range ( 26 ) : NEW_LINE INDENT if ( max < freq [ i ] ) : NEW_LINE INDENT max = freq [ i ] NEW_LINE result = chr ( ord ( ' a ' ) + i ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " sample ▁ program " NEW_LINE print ( " Maximum ▁ occurring ▁ character ▁ = " , getMaxOccurringChar ( str ) ) NEW_LINE DEDENT
Given two numbers as strings , find if one is a power of other | Python3 program to check if one number is a power of other ; Multiply the numbers . It multiplies each digit of second string to each digit of first and stores the result . ; If the digit exceeds 9 , add the cumulative carry to previous digit . ; If all zeroes , return "0" . ; Remove starting zeroes . ; Removes Extra zeroes from front of a string . ; Make sure there are no leading zeroes in the string . ; Making sure that s1 is smaller . If it is greater , we recur we reversed parameters . ; Driver Code
def isGreaterThanEqualTo ( s1 , s2 ) : NEW_LINE INDENT if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return s1 == s2 NEW_LINE DEDENT def multiply ( s1 , s2 ) : NEW_LINE INDENT n , m = len ( s1 ) , len ( s2 ) NEW_LINE result = [ 0 ] * ( n + m ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( m - 1 , - 1 , - 1 ) : NEW_LINE INDENT result [ i + j + 1 ] += ( int ( s1 [ i ] ) * int ( s2 [ j ] ) ) NEW_LINE DEDENT DEDENT size = len ( result ) NEW_LINE for i in range ( size - 1 , 0 , - 1 ) : NEW_LINE INDENT if result [ i ] >= 10 : NEW_LINE INDENT result [ i - 1 ] += result [ i ] // 10 NEW_LINE result [ i ] = result [ i ] % 10 NEW_LINE DEDENT DEDENT i = 0 NEW_LINE while i < size and result [ i ] == 0 : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if i == size : NEW_LINE INDENT return "0" NEW_LINE DEDENT temp = " " NEW_LINE while i < size : NEW_LINE INDENT temp += str ( result [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT return temp NEW_LINE DEDENT def removeLeadingZeores ( s ) : NEW_LINE INDENT n , i = len ( s ) , 0 NEW_LINE while i < n and s [ i ] == '0' : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if i == n : NEW_LINE INDENT return "0" NEW_LINE DEDENT temp = " " NEW_LINE while i < n : NEW_LINE INDENT temp += s [ i ] NEW_LINE i += 1 NEW_LINE DEDENT return temp NEW_LINE DEDENT def isPower ( s1 , s2 ) : NEW_LINE INDENT s1 = removeLeadingZeores ( s1 ) NEW_LINE s2 = removeLeadingZeores ( s2 ) NEW_LINE if s1 == "0" or s2 == "0" : NEW_LINE INDENT return False NEW_LINE DEDENT if s1 == "1" and s2 == "1" : NEW_LINE INDENT return True NEW_LINE DEDENT if s1 == "1" and s2 == "1" : NEW_LINE INDENT return True NEW_LINE DEDENT if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT return isPower ( s2 , s1 ) NEW_LINE DEDENT temp = s1 NEW_LINE while not isGreaterThanEqualTo ( s1 , s2 ) : NEW_LINE INDENT s1 = multiply ( s1 , temp ) NEW_LINE DEDENT return s1 == s2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 , s2 = "374747" , "52627712618930723" NEW_LINE if isPower ( s1 , s2 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT s1 , s2 = "4099" , "2" NEW_LINE if isPower ( s1 , s2 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Sorting array with conditional swapping | Function to check if it is possible to sort the array ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driver Code
def possibleToSort ( arr , n , str ) : NEW_LINE INDENT max_element = - 1 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT max_element = max ( max_element , arr [ i ] ) NEW_LINE if ( str [ i ] == '0' ) : NEW_LINE INDENT if ( max_element > i + 1 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT DEDENT return " Yes " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 3 , 4 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE str = "01110" NEW_LINE print ( possibleToSort ( arr , n , str ) ) NEW_LINE DEDENT
Prime String | Function that checks if sum is prime or not ; corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver code
def isPrimeString ( str1 ) : NEW_LINE INDENT len1 = len ( str1 ) NEW_LINE n = 0 NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT n += ord ( str1 [ i ] ) NEW_LINE DEDENT 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 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 str1 = " geekRam " NEW_LINE if ( isPrimeString ( str1 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Ways to split string such that each partition starts with distinct character | Returns the number of we can split the string ; Finding the frequency of each character . ; making frequency of first character of string equal to 1. ; Finding the product of frequency of occurrence of each character . ; Driver Code
def countWays ( s ) : NEW_LINE INDENT count = [ 0 ] * 26 ; NEW_LINE for x in s : NEW_LINE INDENT count [ ord ( x ) - ord ( ' a ' ) ] = ( count [ ord ( x ) - ord ( ' a ' ) ] ) + 1 ; NEW_LINE DEDENT count [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] = 1 ; NEW_LINE ans = 1 ; NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( count [ i ] != 0 ) : NEW_LINE INDENT ans *= count [ i ] ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " acbbcc " ; NEW_LINE print ( countWays ( s ) ) ; NEW_LINE DEDENT
Lexicographically next greater string using same character set | function to print output ; to store unique characters of the string ; to check uniqueness ; if mp [ s [ i ] ] = 0 then it is first time ; sort the unique characters ; simply add n - k smallest characters ; searching the first character left of index k and not equal to greatest character of the string ; finding the just next greater character than s [ i ] ; suffix with smallest character ; if we reach here then all indices to the left of k had the greatest character ; Driver code ; Function call
def lexoString ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE v = [ ] NEW_LINE mp = { s [ i ] : 0 for i in range ( len ( s ) ) } NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( mp [ s [ i ] ] == 0 ) : NEW_LINE INDENT mp [ s [ i ] ] = 1 NEW_LINE v . append ( s [ i ] ) NEW_LINE DEDENT DEDENT v . sort ( reverse = False ) NEW_LINE if ( k > n ) : NEW_LINE INDENT print ( s , end = " " ) NEW_LINE for i in range ( n , k , 1 ) : NEW_LINE INDENT print ( v [ 0 ] , end = " " ) NEW_LINE DEDENT return ; NEW_LINE DEDENT i = k - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( s [ i ] != v [ len ( v ) - 1 ] ) : NEW_LINE INDENT for j in range ( 0 , i , 1 ) : NEW_LINE INDENT print ( s [ j ] , end = " ▁ " ) NEW_LINE DEDENT for j in range ( 0 , len ( v ) , 1 ) : NEW_LINE INDENT if ( v [ j ] > s [ i ] ) : NEW_LINE INDENT print ( v [ j ] , end = " ▁ " ) NEW_LINE break NEW_LINE DEDENT DEDENT for j in range ( i + 1 , k , 1 ) : NEW_LINE INDENT print ( v [ 0 ] , end = " ▁ " ) NEW_LINE DEDENT re1turn NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT print ( " No ▁ lexicographically ▁ greater " , " string ▁ of ▁ length " , k , " possible ▁ here . " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " gi " NEW_LINE k = 3 NEW_LINE lexoString ( s , k ) NEW_LINE DEDENT
Number of palindromic permutations | Set 1 | Python3 program to find number of palindromic permutations of a given string ; Returns factorial of n ; Returns count of palindromic permutations of str . ; Count frequencies of all characters ; Since half of the characters decide count of palindromic permutations , we take ( n / 2 ) ! ; To make sure that there is at most one odd occurring char ; Traverse through all counts ; To make sure that the string can permute to form a palindrome ; If there are more than one odd occurring chars ; Divide all permutations with repeated characters ; Driver code
MAX = 256 NEW_LINE def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def countPalinPermutations ( str ) : NEW_LINE INDENT global MAX NEW_LINE n = len ( str ) NEW_LINE freq = [ 0 ] * MAX ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) ] = freq [ ord ( str [ i ] ) ] + 1 ; NEW_LINE DEDENT res = fact ( int ( n / 2 ) ) NEW_LINE oddFreq = False NEW_LINE for i in range ( 0 , MAX ) : NEW_LINE INDENT half = int ( freq [ i ] / 2 ) NEW_LINE if ( freq [ i ] % 2 != 0 ) : NEW_LINE INDENT if ( oddFreq == True ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT oddFreq = True NEW_LINE DEDENT res = int ( res / fact ( half ) ) NEW_LINE DEDENT return res NEW_LINE DEDENT str = " gffg " NEW_LINE print ( countPalinPermutations ( str ) ) NEW_LINE
Rearrange characters to form palindrome if possible | Python3 program to rearrange a string to make palindrome . ; Store counts of characters ; Find the number of odd elements . Takes O ( n ) ; odd_cnt = 1 only if the length of str is odd ; Generate first halh of palindrome ; Build a string of floor ( count / 2 ) occurrences of current character ; Attach the built string to end of and begin of second half ; Insert odd character if there is any ; Driver code
from collections import defaultdict NEW_LINE def getPalindrome ( st ) : NEW_LINE INDENT hmap = defaultdict ( int ) NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT hmap [ st [ i ] ] += 1 NEW_LINE DEDENT oddCount = 0 NEW_LINE for x in hmap : NEW_LINE INDENT if ( hmap [ x ] % 2 != 0 ) : NEW_LINE INDENT oddCount += 1 NEW_LINE oddChar = x NEW_LINE DEDENT DEDENT if ( oddCount > 1 or oddCount == 1 and len ( st ) % 2 == 0 ) : NEW_LINE INDENT return " NO ▁ PALINDROME " NEW_LINE DEDENT firstHalf = " " NEW_LINE secondHalf = " " NEW_LINE for x in sorted ( hmap . keys ( ) ) : NEW_LINE INDENT s = ( hmap [ x ] // 2 ) * x NEW_LINE firstHalf = firstHalf + s NEW_LINE secondHalf = s + secondHalf NEW_LINE DEDENT if ( oddCount == 1 ) : NEW_LINE INDENT return ( firstHalf + oddChar + secondHalf ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( firstHalf + secondHalf ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " mdaam " NEW_LINE print ( getPalindrome ( s ) ) NEW_LINE DEDENT
Substrings starting with vowel and ending with consonants and vice versa | Returns true if ch is vowel ; Function to check consonant ; in case of empty string , we can 't fullfill the  required condition, hence we return ans as 0. ; co [ i ] is going to store counts of consonants from str [ len - 1 ] to str [ i ] . vo [ i ] is going to store counts of vowels from str [ len - 1 ] to str [ i ] . ; Counting consonants and vowels from end of string . ; Now we traverse string from beginning ; If vowel , then count of substrings starting with str [ i ] is equal to count of consonants after it . ; If consonant , then count of substrings starting with str [ i ] is equal to count of vowels after it . ; 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 isCons ( ch ) : NEW_LINE INDENT return ( ch != ' a ' and ch != ' e ' and ch != ' i ' and ch != ' o ' and ch != ' u ' ) NEW_LINE DEDENT def countSpecial ( str ) : NEW_LINE INDENT lent = len ( str ) NEW_LINE if lent == 0 : NEW_LINE return 0 ; NEW_LINE co = [ ] NEW_LINE vo = [ ] NEW_LINE for i in range ( 0 , lent + 1 ) : NEW_LINE INDENT co . append ( 0 ) NEW_LINE DEDENT for i in range ( 0 , lent + 1 ) : NEW_LINE INDENT vo . append ( 0 ) NEW_LINE DEDENT if isCons ( str [ lent - 1 ] ) == 1 : NEW_LINE INDENT co [ lent - 1 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT vo [ lent - 1 ] = 1 NEW_LINE DEDENT for i in range ( lent - 2 , - 1 , - 1 ) : NEW_LINE INDENT if isCons ( str [ i ] ) == 1 : NEW_LINE INDENT co [ i ] = co [ i + 1 ] + 1 NEW_LINE vo [ i ] = vo [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT co [ i ] = co [ i + 1 ] NEW_LINE vo [ i ] = vo [ i + 1 ] + 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( lent ) : NEW_LINE INDENT if isVowel ( str [ i ] ) : NEW_LINE INDENT ans = ans + co [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans + vo [ i + 1 ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT str = " adceba " NEW_LINE print ( countSpecial ( str ) ) NEW_LINE
Convert the string into palindrome string by changing only one character | Function to check if it is possible to convert the string into palindrome ; Counting number of characters that should be changed . ; If count of changes is less than or equal to 1 ; Driver Code
def checkPalindrome ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE count = 0 NEW_LINE for i in range ( 0 , int ( n / 2 ) ) : NEW_LINE INDENT if ( str [ i ] != str [ n - i - 1 ] ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT if ( count <= 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT str = " abccaa " NEW_LINE if ( checkPalindrome ( str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Number of substrings with odd decimal value in a binary string | python program to count substrings with odd decimal value ; function to count number of substrings with odd decimal representation ; auxiliary array to store count of 1 's before ith index ; store count of 1 's before  i-th index ; variable to store answer ; traverse the string reversely to calculate number of odd substrings before i - th index ; Driver method
import math NEW_LINE def countSubstr ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE auxArr = [ 0 for i in range ( n ) ] NEW_LINE if ( s [ 0 ] == '1' ) : NEW_LINE INDENT auxArr [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE if ( s [ i ] == '1' ) : NEW_LINE INDENT auxArr [ i ] = auxArr [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT auxArr [ i ] = auxArr [ i - 1 ] NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT count += auxArr [ i ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT s = "1101" NEW_LINE print ( countSubstr ( s ) ) NEW_LINE
Find the starting indices of the substrings in string ( S ) which is made by concatenating all words from a list ( L ) | Returns an integer vector consisting of starting indices of substrings present inside the string S ; Number of a characters of a word in list L . ; Number of words present inside list L . ; Total characters present in list L . ; Resultant vector which stores indices . ; If the total number of characters in list L is more than length of string S itself . ; Map stores the words present in list L against it 's occurrences inside list L ; Traverse the substring ; Extract the word ; If word not found or if frequency of current word is more than required simply break . ; Else decrement the count of word from hash_map ; Store the starting index of that substring when all the words in the list are in substring ; Driver Code
def findSubStringIndices ( s , L ) : NEW_LINE INDENT size_word = len ( L [ 0 ] ) NEW_LINE word_count = len ( L ) NEW_LINE size_L = size_word * word_count NEW_LINE res = [ ] NEW_LINE if size_L > len ( s ) : NEW_LINE INDENT return res NEW_LINE DEDENT hash_map = dict ( ) NEW_LINE for i in range ( word_count ) : NEW_LINE INDENT if L [ i ] in hash_map : NEW_LINE INDENT hash_map [ L [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT hash_map [ L [ i ] ] = 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , len ( s ) - size_L + 1 , 1 ) : NEW_LINE INDENT temp_hash_map = hash_map . copy ( ) NEW_LINE j = i NEW_LINE count = word_count NEW_LINE while j < i + size_L : NEW_LINE INDENT word = s [ j : j + size_word ] NEW_LINE if ( word not in hash_map or temp_hash_map [ word ] == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT temp_hash_map [ word ] -= 1 NEW_LINE count -= 1 NEW_LINE DEDENT j += size_word NEW_LINE DEDENT if count == 0 : NEW_LINE INDENT res . append ( i ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " barfoothefoobarman " NEW_LINE L = [ " foo " , " bar " ] NEW_LINE indices = findSubStringIndices ( s , L ) NEW_LINE print ( * indices ) NEW_LINE DEDENT
Check whether second string can be formed from characters of first string | Python program to check whether second string can be formed from first string ; Create a count array and count frequencies characters in s1 ; Now traverse through str2 to check if every character has enough counts ; Driver Code
def canMakeStr2 ( s1 , s2 ) : NEW_LINE INDENT count = { s1 [ i ] : 0 for i in range ( len ( s1 ) ) } NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT count [ s1 [ i ] ] += 1 NEW_LINE DEDENT for i in range ( len ( s2 ) ) : NEW_LINE INDENT if ( count . get ( s2 [ i ] ) == None or count [ s2 [ i ] ] == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT count [ s2 [ i ] ] -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT s1 = " geekforgeeks " NEW_LINE s2 = " for " NEW_LINE if canMakeStr2 ( s1 , s2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Position of robot after given movements | function to find final position of robot after the complete movement ; traverse the instruction string 'move ; for each movement increment its respective counter ; required final position of robot ; Driver code
def finalPosition ( move ) : NEW_LINE INDENT l = len ( move ) NEW_LINE countUp , countDown = 0 , 0 NEW_LINE countLeft , countRight = 0 , 0 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( l ) : NEW_LINE INDENT if ( move [ i ] == ' U ' ) : NEW_LINE INDENT countUp += 1 NEW_LINE DEDENT elif ( move [ i ] == ' D ' ) : NEW_LINE INDENT countDown += 1 NEW_LINE DEDENT elif ( move [ i ] == ' L ' ) : NEW_LINE INDENT countLeft += 1 NEW_LINE DEDENT elif ( move [ i ] == ' R ' ) : NEW_LINE INDENT countRight += 1 NEW_LINE DEDENT DEDENT print ( " Final ▁ Position : ▁ ( " , ( countRight - countLeft ) , " , ▁ " , ( countUp - countDown ) , " ) " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT move = " UDDLLRUUUDUURUDDUULLDRRRR " NEW_LINE finalPosition ( move ) NEW_LINE DEDENT
Length of longest balanced parentheses prefix | Function to return the length of longest balanced parentheses prefix . ; Traversing the string . ; If open bracket add 1 to sum . ; If closed bracket subtract 1 from sum ; if first bracket is closing bracket then this condition would help ; If sum is 0 , store the index value . ; Driver Code
def maxbalancedprefix ( str , n ) : NEW_LINE INDENT _sum = 0 NEW_LINE maxi = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if str [ i ] == ' ( ' : NEW_LINE INDENT _sum += 1 NEW_LINE DEDENT else : NEW_LINE INDENT _sum -= 1 NEW_LINE DEDENT if _sum < 0 : NEW_LINE INDENT break NEW_LINE DEDENT if _sum == 0 : NEW_LINE INDENT maxi = i + 1 NEW_LINE DEDENT DEDENT return maxi NEW_LINE DEDENT str = ' ( ( ( ) ( ) ) ( ) ) ( ( ' NEW_LINE n = len ( str ) NEW_LINE print ( maxbalancedprefix ( str , n ) ) NEW_LINE
Minimum cost to convert string into palindrome | Function to return cost ; length of string ; Iterate from both sides of string . If not equal , a cost will be there ; Driver code
def cost ( st ) : NEW_LINE INDENT l = len ( st ) NEW_LINE res = 0 NEW_LINE j = l - 1 NEW_LINE i = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( st [ i ] != st [ j ] ) : NEW_LINE INDENT res += ( min ( ord ( st [ i ] ) , ord ( st [ j ] ) ) - ord ( ' a ' ) + 1 ) NEW_LINE DEDENT i = i + 1 NEW_LINE j = j - 1 NEW_LINE DEDENT return res NEW_LINE DEDENT st = " abcdef " ; NEW_LINE print ( cost ( st ) ) NEW_LINE