codeparrot/xlcost-text-to-code huggingface dataset:
Lexicographically smallest string possible by performing K operations on a given string | Function to find the lexicographically smallest possible string by performing K operations on string S ; Store the size of string , s ; Check if k >= n , if true , convert every character to 'a ; Iterate in range [ 0 , n - 1 ] using i ; When k reaches 0 , break the loop ; If current character is ' a ' , continue ; Otherwise , iterate in the range [ i + 1 , n - 1 ] using j ; Check if s [ j ] > s [ i ] ; If true , set s [ j ] = s [ i ] and break out of the loop ; Check if j reaches the last index ; Update S [ i ] ; Decrement k by 1 ; Print string ; Driver Code ; Given String , s ; Given k ; Function Call
def smallestlexicographicstring ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE DEDENT ' NEW_LINE INDENT if ( k >= n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT s [ i ] = ' a ' ; NEW_LINE DEDENT print ( s , end = ' ' ) NEW_LINE return ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( k == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT if ( s [ i ] == ' a ' ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( s [ j ] > s [ i ] ) : NEW_LINE INDENT s [ j ] = s [ i ] ; NEW_LINE break ; NEW_LINE DEDENT elif ( j == n - 1 ) : NEW_LINE INDENT s [ j ] = s [ i ] ; NEW_LINE DEDENT DEDENT s [ i ] = ' a ' ; NEW_LINE k -= 1 NEW_LINE DEDENT print ( ' ' . join ( s ) , end = ' ' ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = list ( " geeksforgeeks " ) ; NEW_LINE k = 6 ; NEW_LINE smallestlexicographicstring ( s , k ) ; NEW_LINE DEDENT
Maximize palindromic strings of length 3 possible from given count of alphabets | Function to count maximum number of palindromic string of length 3 ; Stores the final count of palindromic strings ; Traverse the array ; Increment res by arr [ i ] / 3 , i . e forming string of only i + ' a ' character ; Store remainder ; Increment c1 by one , if current frequency is 1 ; Increment c2 by one , if current frequency is 2 ; Count palindromic strings of length 3 having the character at the ends different from that present in the middle ; Update c1 and c2 ; Increment res by 2 * c2 / 3 ; Finally print the result ; Driver Code ; Given array ; Function Call
def maximum_pallindromic ( arr ) : NEW_LINE INDENT res = 0 NEW_LINE c1 = 0 NEW_LINE c2 = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT res += arr [ i ] // 3 NEW_LINE arr [ i ] = arr [ i ] % 3 NEW_LINE if ( arr [ i ] == 1 ) : NEW_LINE INDENT c1 += 1 NEW_LINE DEDENT elif ( arr [ i ] == 2 ) : NEW_LINE INDENT c2 += 1 NEW_LINE DEDENT DEDENT res += min ( c1 , c2 ) NEW_LINE t = min ( c1 , c2 ) NEW_LINE c1 -= t NEW_LINE c2 -= t NEW_LINE res += 2 * ( c2 // 3 ) NEW_LINE c2 %= 3 NEW_LINE res += c2 // 2 NEW_LINE print ( res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] NEW_LINE maximum_pallindromic ( arr ) NEW_LINE DEDENT
Find the winner of game of repeatedly removing the first character to empty given string | Function to find the winner of a game of repeatedly removing the first character to empty a string ; Store characters of each string of the array arr [ ] ; Stores count of strings in arr [ ] ; Traverse the array arr [ ] ; Stores length of current string ; Traverse the string ; Insert arr [ i ] [ j ] ; 1 st Player starts the game ; Stores the player number for the next turn ; Remove 1 st character of current string ; Update player number for the next turn ; Driver Code
def find_Winner ( arr , N ) : NEW_LINE INDENT Q = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT Q [ i ] = [ ] NEW_LINE DEDENT M = len ( arr ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT Len = len ( arr [ i ] ) NEW_LINE for j in range ( Len ) : NEW_LINE INDENT Q [ i ] . append ( ord ( arr [ i ] [ j ] ) - 1 ) NEW_LINE DEDENT DEDENT player = 0 NEW_LINE while ( len ( Q [ player ] ) > 0 ) : NEW_LINE INDENT nextPlayer = Q [ player ] [ 0 ] - ord ( '0' ) NEW_LINE del Q [ player ] [ 0 ] NEW_LINE player = nextPlayer NEW_LINE DEDENT print ( " Player " , ( player + 1 ) ) NEW_LINE DEDENT N = 3 NEW_LINE arr = [ "323" , "2" , "2" ] NEW_LINE find_Winner ( arr , N ) NEW_LINE
Longest Substring that can be made a palindrome by swapping of characters | Function to find the Longest substring that can be made a palindrome by swapping of characters ; Initialize dp array of size 1024 ; Initializing mask and res ; Traverse the string ; Find the mask of the current character ; Finding the length of the longest substring in s which is a palindrome for even count ; Finding the length of the longest substring in s which is a palindrome for one odd count ; Finding maximum length of substring having one odd count ; dp [ mask ] is minimum of current i and dp [ mask ] ; Return longest length of the substring which forms a palindrome with swaps ; Driver Code ; Input String ; Function Call
def longestSubstring ( s ) : NEW_LINE INDENT dp = [ 1024 for i in range ( 1024 ) ] NEW_LINE res , mask = 0 , 0 NEW_LINE dp [ 0 ] = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT mask ^= 1 << ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE res = max ( res , i - dp [ mask ] ) NEW_LINE for j in range ( 10 ) : NEW_LINE INDENT res = max ( res , i - dp [ mask ^ ( 1 << j ) ] ) NEW_LINE DEDENT dp [ mask ] = min ( dp [ mask ] , i ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "3242415" NEW_LINE print ( longestSubstring ( s ) ) NEW_LINE DEDENT
Convert given string to a valid mobile number | Function to print valid and formatted phone number ; Length of given ; Store digits in temp ; Iterate given M ; If any digit : append it to temp ; Find new length of ; If length is not equal to 10 ; Store final result ; Make groups of 3 digits and enclose them within ( ) and separate them with " - " 0 to 2 index 1 st group ; 3 to 5 index 2 nd group ; 6 to 8 index 3 rd group ; 9 to 9 index last group ; Print final result ; Driver Code ; Given ; Function Call
def Validate ( M ) : NEW_LINE INDENT lenn = len ( M ) NEW_LINE temp = " " NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( M [ i ] . isdigit ( ) ) : NEW_LINE INDENT temp += M [ i ] NEW_LINE DEDENT DEDENT nwlenn = len ( temp ) NEW_LINE if ( nwlenn != 10 ) : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE return NEW_LINE DEDENT res = " " NEW_LINE x = temp [ 0 : 3 ] NEW_LINE res += " ( " + x + " ) - " NEW_LINE x = temp [ 3 : 3 + 3 ] NEW_LINE res += " ( " + x + " ) - " NEW_LINE x = temp [ 6 : 3 + 6 ] NEW_LINE res += " ( " + x + " ) - " NEW_LINE x = temp [ 9 : 1 + 9 ] NEW_LINE res += " ( " + x + " ) " NEW_LINE print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = "91 ▁ 234rt5%34*0 ▁ 3" NEW_LINE Validate ( M ) NEW_LINE DEDENT
Modulus of two Hexadecimal Numbers | Function to calculate modulus of two Hexadecimal numbers ; Store all possible hexadecimal digits ; Iterate over the range [ '0' , '9' ] ; Convert given string to long ; Base to get 16 power ; Store N % K ; Iterate over the digits of N ; Stores i - th digit of N ; Update ans ; Update base ; Print the answer converting into hexadecimal ; Driver Code ; Given string N and K ; Function Call
def hexaModK ( s , k ) : NEW_LINE INDENT mp = { } ; NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT mp [ chr ( i + ord ( '0' ) ) ] = i ; NEW_LINE DEDENT mp [ ' A ' ] = 10 ; NEW_LINE mp [ ' B ' ] = 11 ; NEW_LINE mp [ ' C ' ] = 12 ; NEW_LINE mp [ ' D ' ] = 13 ; NEW_LINE mp [ ' E ' ] = 14 ; NEW_LINE mp [ ' F ' ] = 15 ; NEW_LINE m = int ( k ) ; NEW_LINE base = 1 ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT n = mp [ s [ i ] ] % m ; NEW_LINE ans = ( ans + ( base % m * n % m ) % m ) % m ; NEW_LINE base = ( base % m * 16 % m ) % m ; NEW_LINE DEDENT ans = hex ( int ( ans ) ) [ - 1 ] . upper ( ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = "3E8" ; NEW_LINE k = "13" ; NEW_LINE hexaModK ( n , k ) ; NEW_LINE DEDENT
Print all combinations generated by characters of a numeric string which does not exceed N | Store the current sequence of s ; Store the all the required sequences ; Function to print all sequences of S satisfying the required condition ; Print all strings in the set ; Function to generate all sequences of string S that are at most N ; Iterate over string s ; Push ith character to combination ; Convert the string to number ; Check if the condition is true ; Push the current string to the final set of sequences ; Recursively call function ; Backtrack to its previous state ; Driver Code ; Function Call ; Print required sequences
combination = " " ; NEW_LINE combinations = [ ] ; NEW_LINE def printSequences ( combinations ) : NEW_LINE INDENT for s in ( combinations ) : NEW_LINE INDENT print ( s , end = ' ▁ ' ) ; NEW_LINE DEDENT DEDENT def generateCombinations ( s , n ) : NEW_LINE INDENT global combination ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT combination += s [ i ] ; NEW_LINE x = int ( combination ) ; NEW_LINE if ( x <= n ) : NEW_LINE INDENT combinations . append ( combination ) ; NEW_LINE generateCombinations ( s , n ) ; NEW_LINE DEDENT combination = combination [ : - 1 ] ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "124" ; NEW_LINE N = 100 ; NEW_LINE generateCombinations ( S , N ) ; NEW_LINE printSequences ( combinations ) ; NEW_LINE DEDENT
Count Distinct Strings present in an array using Polynomial rolling hash function | Function to find the hash value of a ; Traverse the ; Update hash_val ; Update mul ; Return hash_val of str ; Function to find the count of distinct strings present in the given array ; Store the hash values of the strings ; Traverse the array ; Stores hash value of arr [ i ] ; Sort hash [ ] array ; Stores count of distinct strings in the array ; Traverse hash [ ] array ; Update cntElem ; Driver Code
def compute_hash ( str ) : NEW_LINE INDENT p = 31 NEW_LINE MOD = 10 ** 9 + 7 NEW_LINE hash_val = 0 NEW_LINE mul = 1 NEW_LINE for ch in str : NEW_LINE INDENT hash_val = ( hash_val + ( ord ( ch ) - ord ( ' a ' ) + 1 ) * mul ) % MOD NEW_LINE mul = ( mul * p ) % MOD NEW_LINE DEDENT return hash_val NEW_LINE DEDENT def distinct_str ( arr , n ) : NEW_LINE INDENT hash = [ 0 ] * ( n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ i ] = compute_hash ( arr [ i ] ) NEW_LINE DEDENT hash = sorted ( hash ) NEW_LINE cntElem = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( hash [ i ] != hash [ i - 1 ] ) : NEW_LINE INDENT cntElem += 1 NEW_LINE DEDENT DEDENT return cntElem NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " abcde " , " abcce " , " abcdf " , " abcde " ] NEW_LINE N = len ( arr ) NEW_LINE print ( distinct_str ( arr , N ) ) NEW_LINE DEDENT
Remove characters from given string whose frequencies are a Prime Number | Function to perform the seive of eratosthenes algorithm ; Initialize all entries in prime [ ] as true ; Initialize 0 and 1 as non prime ; Traversing the prime array ; If i is prime ; All multiples of i must be marked false as they are non prime ; Function to remove characters which have prime frequency in the String ; Length of the String ; Create a bool array prime ; Sieve of Eratosthenes ; Stores the frequency of character ; Storing the frequencies ; New String that will be formed ; Removing the characters which have prime frequencies ; If the character has prime frequency then skip ; Else concatenate the character to the new String ; Print the modified String ; Driver Code ; Function Call
def SieveOfEratosthenes ( prime , n ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT prime [ i ] = True NEW_LINE DEDENT prime [ 0 ] = prime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT j = 2 NEW_LINE while i * j <= n : NEW_LINE INDENT prime [ i * j ] = False NEW_LINE j += 1 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def removePrimeFrequencies ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE prime = [ False ] * ( n + 1 ) NEW_LINE SieveOfEratosthenes ( prime , n ) NEW_LINE m = { } NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if s [ i ] in m : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ s [ i ] ] = 1 NEW_LINE DEDENT DEDENT new_String = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( prime [ m [ s [ i ] ] ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT new_String += s [ i ] NEW_LINE DEDENT print ( new_String , end = " " ) NEW_LINE DEDENT Str = " geeksforgeeks " NEW_LINE removePrimeFrequencies ( list ( Str ) ) NEW_LINE
Rearrange string such that no pair of adjacent characters are of the same type | Function to rearrange given alphanumeric such that no two adjacent characters are of the same type ; Stores alphabets and digits ; Store the alphabets and digits separately in the strings ; Stores the count of alphabets and digits ; If respective counts differ by 1 ; Desired arrangement not possible ; Stores the indexes ; Check if first character should be alphabet or digit ; Place alphabets and digits alternatively ; If current character needs to be alphabet ; If current character needs to be a digit ; Flip flag for alternate arrangement ; Return resultant string ; Driver Code ; Given String ; Function call
def rearrange ( s ) : NEW_LINE INDENT s1 = [ ] NEW_LINE s2 = [ ] NEW_LINE for x in s : NEW_LINE INDENT if x . isalpha ( ) : NEW_LINE INDENT s1 . append ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT s2 . append ( x ) NEW_LINE DEDENT DEDENT n = len ( s1 ) NEW_LINE m = len ( s2 ) NEW_LINE if ( abs ( n - m ) > 1 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT i = 0 NEW_LINE j = 0 NEW_LINE k = 0 NEW_LINE flag = 0 NEW_LINE if ( n >= m ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT else : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT while ( i < n and j < m ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT s [ k ] = s1 [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT s [ k ] = s2 [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT flag = not flag NEW_LINE DEDENT return " " . join ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeks2020" NEW_LINE str1 = [ i for i in str ] NEW_LINE print ( rearrange ( str1 ) ) NEW_LINE DEDENT
Find value after N operations to remove N characters of string S with given constraints | Function to find the value after N operations to remove all the N characters of String S ; Iterate till N ; Remove character at ind and decrease n ( size of String ) ; Increase count by ind + 1 ; Driver Code ; Given String str ; Function call
def charactersCount ( str , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT cur = str [ 0 ] ; NEW_LINE ind = 0 ; NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT if ( str [ j ] < cur ) : NEW_LINE INDENT cur = str [ j ] ; NEW_LINE ind = j ; NEW_LINE DEDENT DEDENT str = str [ 0 : ind ] + str [ ind + 1 : ] ; NEW_LINE n -= 1 ; NEW_LINE count += ind + 1 ; NEW_LINE DEDENT print ( count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aabbc " ; NEW_LINE n = 5 ; NEW_LINE charactersCount ( str , n ) ; NEW_LINE DEDENT
Print the middle character of a string | Function that prints the middle character of a string ; Finding string length ; Finding middle index of string ; Prthe middle character of the string ; Given string str ; Function Call
def printMiddleCharacter ( str ) : NEW_LINE INDENT length = len ( str ) ; NEW_LINE middle = length // 2 ; NEW_LINE print ( str [ middle ] ) ; NEW_LINE DEDENT str = " GeeksForGeeks " ; NEW_LINE printMiddleCharacter ( str ) ; NEW_LINE
Maximize length of the String by concatenating characters from an Array of Strings | Function to check if all the string characters are unique ; Check for repetition in characters ; Function to generate all possible strings from the given array ; Base case ; Consider every string as a starting substring and store the generated string ; Add current string to result of other strings and check if characters are unique or not ; Function to find the maximum possible length of a string ; Return max length possible ; Return the answer ; Driver Code
def check ( s ) : NEW_LINE INDENT a = set ( ) NEW_LINE for i in s : NEW_LINE INDENT if i in a : NEW_LINE INDENT return False NEW_LINE DEDENT a . add ( i ) NEW_LINE DEDENT return True NEW_LINE DEDENT def helper ( arr , ind ) : NEW_LINE INDENT if ( ind == len ( arr ) ) : NEW_LINE INDENT return [ " " ] NEW_LINE DEDENT tmp = helper ( arr , ind + 1 ) NEW_LINE ret = tmp NEW_LINE for i in tmp : NEW_LINE INDENT test = i + arr [ ind ] NEW_LINE if ( check ( test ) ) : NEW_LINE INDENT ret . append ( test ) NEW_LINE DEDENT DEDENT return ret NEW_LINE DEDENT def maxLength ( arr ) : NEW_LINE INDENT tmp = helper ( arr , 0 ) NEW_LINE l = 0 NEW_LINE for i in tmp : NEW_LINE INDENT l = l if l > len ( i ) else len ( i ) NEW_LINE DEDENT return l NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = [ ] NEW_LINE s . append ( " abcdefgh " ) NEW_LINE print ( maxLength ( s ) ) NEW_LINE DEDENT
Perform range sum queries on string as per given condition | Function to perform range sum queries on string as per the given condition ; Initialize N by string size ; Create array A [ ] for prefix sum ; Iterate till N ; Traverse the queries ; Check if if L == 1 range sum will be A [ R - 1 ] ; Condition if L > 1 range sum will be A [ R - 1 ] - A [ L - 2 ] ; Given string ; Given Queries ; Function call
def Range_sum_query ( S , Query ) : NEW_LINE INDENT N = len ( S ) NEW_LINE A = [ 0 ] * N NEW_LINE A [ 0 ] = ord ( S [ 0 ] ) - ord ( ' a ' ) + 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT A [ i ] = ord ( S [ i ] ) - ord ( ' a ' ) + 1 NEW_LINE A [ i ] = A [ i ] + A [ i - 1 ] NEW_LINE DEDENT for i in range ( len ( Query ) ) : NEW_LINE INDENT if ( Query [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT print ( A [ Query [ i ] [ 1 ] - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( A [ Query [ i ] [ 1 ] - 1 ] - A [ Query [ i ] [ 0 ] - 2 ] ) NEW_LINE DEDENT DEDENT DEDENT S = " abcd " NEW_LINE Query = [ ] NEW_LINE Query . append ( [ 2 , 4 ] ) NEW_LINE Query . append ( [ 1 , 3 ] ) NEW_LINE Range_sum_query ( S , Query ) NEW_LINE
Generate all possible permutations of a Number divisible by N | Function to generate all permutations and print the ones that are divisible by the N ; Convert string to integer ; Check for divisibility and print it ; Print all the permutations ; Swap characters ; Permute remaining characters ; Revoke the swaps ; Driver Code
def permute ( st , l , r , n ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT p = ' ' . join ( st ) NEW_LINE j = int ( p ) NEW_LINE if ( j % n == 0 ) : NEW_LINE INDENT print ( p ) NEW_LINE DEDENT return NEW_LINE DEDENT for i in range ( l , r ) : NEW_LINE INDENT st [ l ] , st [ i ] = st [ i ] , st [ l ] NEW_LINE permute ( st , l + 1 , r , n ) NEW_LINE st [ l ] , st [ i ] = st [ i ] , st [ l ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "125" NEW_LINE n = 5 NEW_LINE length = len ( st ) NEW_LINE if ( length > 0 ) : NEW_LINE p = list ( st ) NEW_LINE permute ( p , 0 , length , n ) ; NEW_LINE DEDENT
Check if binary representations of 0 to N are present as substrings in given binary string | Function to convert decimal to binary representation ; Iterate over all bits of N ; If bit is 1 ; Return binary representation ; Function to check if binary conversion of numbers from N to 1 exists in the string as a substring or not ; To store the count of number exists as a substring ; Traverse from N to 1 ; If current number is not present in map ; Store current number ; Find binary of t ; If the string s is a substring of str ; Mark t as true ; Increment the count ; Update for t / 2 ; Special judgment '0 ; If the count is N + 1 , return " yes " ; Driver Code ; Given String ; Given Number ; Function Call
def decimalToBinary ( N ) : NEW_LINE INDENT ans = " " NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT ans = '1' + ans NEW_LINE DEDENT else : NEW_LINE INDENT ans = '0' + ans NEW_LINE DEDENT N //= 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT def checkBinaryString ( str , N ) : NEW_LINE INDENT map = [ 0 ] * ( N + 10 ) NEW_LINE cnt = 0 NEW_LINE for i in range ( N , - 1 , - 1 ) : NEW_LINE INDENT if ( not map [ i ] ) : NEW_LINE INDENT t = i NEW_LINE s = decimalToBinary ( t ) NEW_LINE if ( s in str ) : NEW_LINE INDENT while ( t and not map [ t ] ) : NEW_LINE INDENT map [ t ] = 1 NEW_LINE cnt += 1 NEW_LINE t >>= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT ' NEW_LINE INDENT for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT cnt += 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( cnt == N + 1 ) : NEW_LINE INDENT return " True " NEW_LINE DEDENT else : NEW_LINE INDENT return " False " NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "0110" NEW_LINE N = 3 NEW_LINE print ( checkBinaryString ( str , N ) ) NEW_LINE DEDENT
Find the word with most anagrams in a given sentence | Function to find the word with maximum number of anagrams ; Primes assigned to 26 alphabets ; Stores the product and word mappings ; Stores the frequencies of products ; Calculate the product of primes assigned ; If product already exists ; Otherwise ; Fetch the most frequent product ; Return a string with that product ; Driver Code
def largestAnagramGrp ( arr ) : NEW_LINE INDENT prime = [ 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 , 101 ] NEW_LINE max = - 1 NEW_LINE maxpdt = - 1 NEW_LINE W = { } NEW_LINE P = { } NEW_LINE for temp in arr : NEW_LINE INDENT c = [ i for i in temp ] NEW_LINE pdt = 1 NEW_LINE for t in c : NEW_LINE INDENT pdt *= prime [ ord ( t ) - ord ( ' a ' ) ] NEW_LINE DEDENT if ( pdt in P ) : NEW_LINE INDENT P [ pdt ] = P . get ( pdt , 0 ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT W [ pdt ] = temp NEW_LINE P [ pdt ] = 1 NEW_LINE DEDENT DEDENT for e in P : NEW_LINE INDENT if ( max < P [ e ] ) : NEW_LINE INDENT max = P [ e ] NEW_LINE maxpdt = e NEW_LINE DEDENT DEDENT return W [ maxpdt ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " please ▁ be ▁ silent ▁ and ▁ listen ▁ to ▁ what ▁ the ▁ professor ▁ says " NEW_LINE arr = S . split ( " ▁ " ) NEW_LINE print ( largestAnagramGrp ( arr ) ) NEW_LINE DEDENT
Replace every vowels with lexicographically next vowel in a String | Function to replace every vowel with next vowel lexicographically ; Storing the vowels in the map with custom numbers showing their index ; Iterate over the string ; If the current character is a vowel Find the index in Hash and Replace it with next vowel from Hash ; Driver function
def print_next_vovel_string ( st ) : NEW_LINE INDENT m = { } NEW_LINE m [ ' a ' ] = 0 NEW_LINE m [ ' e ' ] = 1 NEW_LINE m [ ' i ' ] = 2 NEW_LINE m [ ' o ' ] = 3 NEW_LINE m [ ' u ' ] = 4 NEW_LINE arr = [ ' a ' , ' e ' , ' i ' , ' o ' , ' u ' ] NEW_LINE N = len ( st ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT c = st [ i ] NEW_LINE if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT index = m [ st [ i ] ] + 1 NEW_LINE newindex = index % 5 NEW_LINE st = st . replace ( st [ i ] , arr [ newindex ] , 1 ) NEW_LINE DEDENT DEDENT return st NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " geeksforgeeks " NEW_LINE print ( print_next_vovel_string ( st ) ) NEW_LINE DEDENT