codeparrot/xlcost-text-to-code huggingface dataset:
Find k 'th character of decrypted string | Set 1 | Function to find K 'th character in Encoded String ; expand string variable is used to store final string after decompressing string str ; Current substring freq = 0 Count of current substring ; read characters until you find a number or end of string ; push character in temp ; read number for how many times string temp will be repeated in decompressed string ; generating frequency of temp ; now append string temp into expand equal to its frequency ; this condition is to handle the case when string str is ended with alphabets not with numeric value ; Driver Code
def encodedChar ( str , k ) : NEW_LINE INDENT expand = " " NEW_LINE i = 0 NEW_LINE while ( i < len ( str ) ) : NEW_LINE INDENT while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( ' a ' ) and ord ( str [ i ] ) <= ord ( ' z ' ) ) : NEW_LINE INDENT temp += str [ i ] NEW_LINE i += 1 NEW_LINE DEDENT while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( '1' ) and ord ( str [ i ] ) <= ord ( '9' ) ) : NEW_LINE INDENT freq = freq * 10 + ord ( str [ i ] ) - ord ( '0' ) NEW_LINE i += 1 NEW_LINE DEDENT for j in range ( 1 , freq + 1 , 1 ) : NEW_LINE INDENT expand += temp NEW_LINE DEDENT DEDENT if ( freq == 0 ) : NEW_LINE INDENT expand += temp NEW_LINE DEDENT return expand [ k - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ab4c12ed3" NEW_LINE k = 21 NEW_LINE print ( encodedChar ( str , k ) ) NEW_LINE DEDENT
Converting Decimal Number lying between 1 to 3999 to Roman Numerals | Function to calculate roman equivalent ; Storing roman values of digits from 0 - 9 when placed at different places ; Converting to roman ; Driver code
def intToRoman ( num ) : NEW_LINE INDENT m = [ " " , " M " , " MM " , " MMM " ] NEW_LINE c = [ " " , " C " , " CC " , " CCC " , " CD " , " D " , " DC " , " DCC " , " DCCC " , " CM ▁ " ] NEW_LINE x = [ " " , " X " , " XX " , " XXX " , " XL " , " L " , " LX " , " LXX " , " LXXX " , " XC " ] NEW_LINE i = [ " " , " I " , " II " , " III " , " IV " , " V " , " VI " , " VII " , " VIII " , " IX " ] NEW_LINE thousands = m [ num // 1000 ] NEW_LINE hundereds = c [ ( num % 1000 ) // 100 ] NEW_LINE tens = x [ ( num % 100 ) // 10 ] NEW_LINE ones = i [ num % 10 ] NEW_LINE ans = ( thousands + hundereds + tens + ones ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT number = 3549 NEW_LINE print ( intToRoman ( number ) ) NEW_LINE DEDENT
Longest Common Prefix using Binary Search | A Function to find the string having the minimum length and returns that length ; A Function that returns the longest common prefix from the array of strings ; We will do an in - place binary search on the first string of the array in the range 0 to index ; Same as ( low + high ) / 2 , but avoids overflow for large low and high ; If all the strings in the input array contains this prefix then append this substring to our answer ; And then go for the right part ; Go for the left part ; Driver Code
def findMinLength ( strList ) : NEW_LINE INDENT return len ( min ( arr , key = len ) ) NEW_LINE DEDENT def allContainsPrefix ( strList , str , start , end ) : NEW_LINE INDENT for i in range ( 0 , len ( strList ) ) : NEW_LINE INDENT word = strList [ i ] NEW_LINE for j in range ( start , end + 1 ) : NEW_LINE INDENT if word [ j ] != str [ j ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def CommonPrefix ( strList ) : NEW_LINE INDENT index = findMinLength ( strList ) NEW_LINE low , high = 0 , index - 1 NEW_LINE while low <= high : NEW_LINE INDENT mid = int ( low + ( high - low ) / 2 ) NEW_LINE if allContainsPrefix ( strList , strList [ 0 ] , low , mid ) : NEW_LINE INDENT prefix = prefix + strList [ 0 ] [ low : mid + 1 ] NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return prefix NEW_LINE DEDENT arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ] NEW_LINE lcp = CommonPrefix ( arr ) NEW_LINE if len ( lcp ) > 0 : NEW_LINE INDENT print ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " + str ( lcp ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ is ▁ no ▁ common ▁ prefix " ) NEW_LINE DEDENT
Lower case to upper case | Converts a string to uppercase ; Driver code
def to_upper ( s ) : NEW_LINE INDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ' a ' <= s [ i ] <= ' z ' ) : NEW_LINE INDENT s = s [ 0 : i ] + chr ( ord ( s [ i ] ) & ( ~ ( 1 << 5 ) ) ) + s [ i + 1 : ] ; NEW_LINE DEDENT DEDENT return s ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE print ( to_upper ( string ) ) ; NEW_LINE DEDENT
Longest Common Prefix using Divide and Conquer Algorithm | A Utility Function to find the common prefix between strings - str1 and str2 ; A Divide and Conquer based function to find the longest common prefix . This is similar to the merge sort technique ; Same as ( low + high ) / 2 , but avoids overflow for large low and high ; Driver Code
def commonPrefixUtil ( str1 , str2 ) : NEW_LINE INDENT result = " " NEW_LINE n1 , n2 = len ( str1 ) , len ( str2 ) NEW_LINE i , j = 0 , 0 NEW_LINE while i <= n1 - 1 and j <= n2 - 1 : NEW_LINE INDENT if str1 [ i ] != str2 [ j ] : NEW_LINE INDENT break NEW_LINE DEDENT result += str1 [ i ] NEW_LINE i , j = i + 1 , j + 1 NEW_LINE DEDENT return result NEW_LINE DEDENT def commonPrefix ( arr , low , high ) : NEW_LINE INDENT if low == high : NEW_LINE INDENT return arr [ low ] NEW_LINE DEDENT if high > low : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE str1 = commonPrefix ( arr , low , mid ) NEW_LINE str2 = commonPrefix ( arr , mid + 1 , high ) NEW_LINE return commonPrefixUtil ( str1 , str2 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ] NEW_LINE n = len ( arr ) NEW_LINE ans = commonPrefix ( arr , 0 , n - 1 ) NEW_LINE if len ( ans ) : NEW_LINE INDENT print ( " The ▁ longest ▁ common ▁ prefix ▁ is " , ans ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ is ▁ no ▁ common ▁ prefix " ) NEW_LINE DEDENT DEDENT
Longest Common Prefix using Word by Word Matching | A Utility Function to find the common prefix between strings - str1 and str2 ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; Driver Code
def commonPrefixUtil ( str1 , str2 ) : NEW_LINE INDENT result = " " ; NEW_LINE n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while i <= n1 - 1 and j <= n2 - 1 : NEW_LINE INDENT if ( str1 [ i ] != str2 [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT result += str1 [ i ] NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT return ( result ) NEW_LINE DEDENT def commonPrefix ( arr , n ) : NEW_LINE INDENT prefix = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix = commonPrefixUtil ( prefix , arr [ i ] ) NEW_LINE DEDENT return ( prefix ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ] NEW_LINE n = len ( arr ) NEW_LINE ans = commonPrefix ( arr , n ) NEW_LINE if ( len ( ans ) ) : NEW_LINE INDENT print ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ - " , ans ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ is ▁ no ▁ common ▁ prefix " ) NEW_LINE DEDENT DEDENT
Find all strings formed from characters mapped to digits of a number | Function to find all strings formed from a given number where each digit maps to given characters . ; vector of strings to store output ; stores index of first occurrence of the digits in input ; maintains index of current digit considered ; for each digit ; store index of first occurrence of the digit in the map ; clear vector contents for future use ; do for each character thats maps to the digit ; for first digit , simply push all its mapped characters in the output list ; from second digit onwards ; for each string in output list append current character to it . ; convert current character to string ; Imp - If this is not the first occurrence of the digit , use same character as used in its first occurrence ; store strings formed by current digit ; nothing more needed to be done if this is not the first occurrence of the digit ; replace contents of output list with temp list ; Driver Code ; vector to store the mappings ; vector to store input number ; print all possible strings
def findCombinations ( input : list , table : list ) -> list : NEW_LINE INDENT out , temp = [ ] , [ ] NEW_LINE mp = dict ( ) NEW_LINE index = 0 NEW_LINE for d in input : NEW_LINE INDENT if d not in mp : NEW_LINE INDENT mp [ d ] = index NEW_LINE DEDENT temp . clear ( ) NEW_LINE for i in range ( len ( table [ d - 1 ] ) ) : NEW_LINE INDENT if index == 0 : NEW_LINE INDENT s = table [ d - 1 ] [ i ] NEW_LINE out . append ( s ) NEW_LINE DEDENT if index > 0 : NEW_LINE INDENT for string in out : NEW_LINE INDENT s = table [ d - 1 ] [ i ] NEW_LINE if mp [ d ] != index : NEW_LINE INDENT s = string [ mp [ d ] ] NEW_LINE DEDENT string = string + s NEW_LINE temp . append ( string ) NEW_LINE DEDENT if mp [ d ] != index : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if index > 0 : NEW_LINE INDENT out = temp . copy ( ) NEW_LINE DEDENT index += 1 NEW_LINE DEDENT return out NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT table = [ [ ' A ' , ' B ' , ' C ' ] , [ ' D ' , ' E ' , ' F ' ] , [ ' G ' , ' H ' , ' I ' ] , [ ' J ' , ' K ' , ' L ' ] , [ ' M ' , ' N ' , ' O ' ] , [ ' P ' , ' Q ' , ' R ' ] , [ ' S ' , ' T ' , ' U ' ] , [ ' V ' , ' W ' , ' X ' ] , [ ' Y ' , ' Z ' ] ] NEW_LINE input = [ 1 , 2 , 1 ] NEW_LINE out = findCombinations ( input , table ) NEW_LINE for it in out : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT
1 ' s ▁ and ▁ 2' s complement of a Binary Number | Returns '0' for '1' and '1' for '0 ; Print 1 ' s ▁ and ▁ 2' s complement of binary number represented by " bin " ; for ones complement flip every bit ; for two 's complement go from right  to left in ones complement and if  we get 1 make, we make them 0 and  keep going left when we get first  0, make that 1 and go out of loop ; If No break : all are 1 as in 111 or 11111 in such case , add extra 1 at beginning ; Driver Code
' NEW_LINE def flip ( c ) : NEW_LINE INDENT return '1' if ( c == '0' ) else '0' NEW_LINE DEDENT def printOneAndTwosComplement ( bin ) : NEW_LINE INDENT n = len ( bin ) NEW_LINE ones = " " NEW_LINE twos = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT ones += flip ( bin [ i ] ) NEW_LINE DEDENT ones = list ( ones . strip ( " " ) ) NEW_LINE twos = list ( ones ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( ones [ i ] == '1' ) : NEW_LINE INDENT twos [ i ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT twos [ i ] = '1' NEW_LINE break NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE if ( i == - 1 ) : NEW_LINE INDENT twos . insert ( 0 , '1' ) NEW_LINE DEDENT print ( "1 ' s ▁ complement : ▁ " , * ones , sep = " " ) NEW_LINE print ( "2 ' s ▁ complement : ▁ " , * twos , sep = " " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT bin = "1100" NEW_LINE printOneAndTwosComplement ( bin . strip ( " " ) ) NEW_LINE DEDENT
Print Concatenation of Zig | Function for zig - zag Concatenation ; Check is n is less or equal to 1 ; Iterate rowNum from 0 to n - 1 ; Iterate i till s . length ( ) - 1 ; Check is rowNum is 0 or n - 1 ; Driver Code
def zigZagConcat ( s , n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return s NEW_LINE DEDENT result = " " NEW_LINE for rowNum in range ( n ) : NEW_LINE INDENT i = rowNum NEW_LINE up = True NEW_LINE while ( i < len ( s ) ) : NEW_LINE INDENT result += s [ i ] NEW_LINE if ( rowNum == 0 or rowNum == n - 1 ) : NEW_LINE INDENT i += ( 2 * n - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( up ) : NEW_LINE INDENT i += ( 2 * ( n - rowNum ) - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT i += rowNum * 2 NEW_LINE DEDENT up ^= True NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT str = " GEEKSFORGEEKS " NEW_LINE n = 3 NEW_LINE print ( zigZagConcat ( str , n ) ) NEW_LINE
Print string of odd length in ' X ' format | Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; driver code
def pattern ( st , length ) : NEW_LINE INDENT for i in range ( length ) : NEW_LINE INDENT j = length - 1 - i NEW_LINE for k in range ( length ) : NEW_LINE INDENT if ( k == i or k == j ) : NEW_LINE INDENT print ( st [ k ] , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " geeksforgeeks " NEW_LINE length = len ( st ) NEW_LINE pattern ( st , length ) NEW_LINE DEDENT
Check if characters of a given string can be rearranged to form a palindrome | Python3 implementation to check if characters of a given string can be rearranged to form a palindrome ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver code
NO_OF_CHARS = 256 NEW_LINE def canFormPalindrome ( st ) : NEW_LINE INDENT count = [ 0 ] * ( NO_OF_CHARS ) NEW_LINE for i in range ( 0 , len ( st ) ) : NEW_LINE INDENT count [ ord ( st [ i ] ) ] = count [ ord ( st [ i ] ) ] + 1 NEW_LINE DEDENT odd = 0 NEW_LINE for i in range ( 0 , NO_OF_CHARS ) : NEW_LINE INDENT if ( count [ i ] & 1 ) : NEW_LINE INDENT odd = odd + 1 NEW_LINE DEDENT if ( odd > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if ( canFormPalindrome ( " geeksforgeeks " ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT if ( canFormPalindrome ( " geeksogeeks " ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Generate n | This function generates all n bit Gray codes and prints the generated codes ; Base case ; Recursive case ; Append 0 to the first half ; Append 1 to the second half ; Function to generate the Gray code of N bits ; Print contents of arr ; Driver Code
def generateGray ( n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return [ "0" ] NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return [ "0" , "1" ] NEW_LINE DEDENT recAns = generateGray ( n - 1 ) NEW_LINE mainAns = [ ] NEW_LINE for i in range ( len ( recAns ) ) : NEW_LINE INDENT s = recAns [ i ] NEW_LINE mainAns . append ( "0" + s ) NEW_LINE DEDENT for i in range ( len ( recAns ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT s = recAns [ i ] NEW_LINE mainAns . append ( "1" + s ) NEW_LINE DEDENT return mainAns NEW_LINE DEDENT def generateGrayarr ( n ) : NEW_LINE INDENT arr = generateGray ( n ) NEW_LINE print ( * arr , sep =   " " ) NEW_LINE DEDENT generateGrayarr ( 3 ) NEW_LINE
Check whether two strings are anagram of each other | Python program to check if two strings are anagrams of each other ; Function to check whether two strings are anagram of each other ; Create two count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; Compare count arrays ; Driver code ; Function call
NO_OF_CHARS = 256 NEW_LINE def areAnagram ( str1 , str2 ) : NEW_LINE INDENT count1 = [ 0 ] * NO_OF_CHARS NEW_LINE count2 = [ 0 ] * NO_OF_CHARS NEW_LINE for i in str1 : NEW_LINE INDENT count1 [ ord ( i ) ] += 1 NEW_LINE DEDENT for i in str2 : NEW_LINE INDENT count2 [ ord ( i ) ] += 1 NEW_LINE DEDENT if len ( str1 ) != len ( str2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in xrange ( NO_OF_CHARS ) : NEW_LINE INDENT if count1 [ i ] != count2 [ i ] : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT str1 = " geeksforgeeks " NEW_LINE str2 = " forgeeksgeeks " NEW_LINE if areAnagram ( str1 , str2 ) : NEW_LINE INDENT print " The ▁ two ▁ strings ▁ are ▁ anagram ▁ of ▁ each ▁ other " NEW_LINE DEDENT else : NEW_LINE INDENT print " The ▁ two ▁ strings ▁ are ▁ not ▁ anagram ▁ of ▁ each ▁ other " NEW_LINE DEDENT
Find the smallest window in a string containing all characters of another string | Python3 program to find the smallest window containing all characters of a pattern . ; Function to find smallest window containing all characters of 'pat ; Check if string ' s ▁ length ▁ is ▁ ▁ less ▁ than ▁ pattern ' s length . If yes then no such window can exist ; Store occurrence ofs characters of pattern ; Start traversing the string count = 0 count of characters ; count occurrence of characters of string ; If string ' s ▁ char ▁ matches ▁ with ▁ ▁ pattern ' s char then increment count ; if all the characters are matched ; Try to minimize the window ; update window size ; If no window found ; Return substring starting from start_index and length min_len ; Driver code
no_of_chars = 256 NEW_LINE ' NEW_LINE def findSubString ( string , pat ) : NEW_LINE INDENT len1 = len ( string ) NEW_LINE len2 = len ( pat ) NEW_LINE if len1 < len2 : NEW_LINE INDENT print ( " No ▁ such ▁ window ▁ exists " ) NEW_LINE return " " NEW_LINE DEDENT hash_pat = [ 0 ] * no_of_chars NEW_LINE hash_str = [ 0 ] * no_of_chars NEW_LINE for i in range ( 0 , len2 ) : NEW_LINE INDENT hash_pat [ ord ( pat [ i ] ) ] += 1 NEW_LINE DEDENT start , start_index , min_len = 0 , - 1 , float ( ' inf ' ) NEW_LINE for j in range ( 0 , len1 ) : NEW_LINE INDENT hash_str [ ord ( string [ j ] ) ] += 1 NEW_LINE if ( hash_str [ ord ( string [ j ] ) ] <= hash_pat [ ord ( string [ j ] ) ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count == len2 : NEW_LINE INDENT while ( hash_str [ ord ( string [ start ] ) ] > hash_pat [ ord ( string [ start ] ) ] or hash_pat [ ord ( string [ start ] ) ] == 0 ) : NEW_LINE INDENT if ( hash_str [ ord ( string [ start ] ) ] > hash_pat [ ord ( string [ start ] ) ] ) : NEW_LINE INDENT hash_str [ ord ( string [ start ] ) ] -= 1 NEW_LINE DEDENT start += 1 NEW_LINE DEDENT len_window = j - start + 1 NEW_LINE if min_len > len_window : NEW_LINE INDENT min_len = len_window NEW_LINE start_index = start NEW_LINE DEDENT DEDENT DEDENT if start_index == - 1 : NEW_LINE INDENT print ( " No ▁ such ▁ window ▁ exists " ) NEW_LINE return " " NEW_LINE DEDENT return string [ start_index : start_index + min_len ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " this ▁ is ▁ a ▁ test ▁ string " NEW_LINE pat = " tist " NEW_LINE print ( " Smallest ▁ window ▁ is ▁ : ▁ " ) NEW_LINE print ( findSubString ( string , pat ) ) NEW_LINE DEDENT
Remove characters from the first string which are present in the second string | Python 3 program to remove duplicates ; we extract every character of string string 2 ; we find char exit or not ; if char exit we simply remove that char ; Driver Code
def removeChars ( string1 , string2 ) : NEW_LINE INDENT for i in string2 : NEW_LINE INDENT while i in string1 : NEW_LINE INDENT itr = string1 . find ( i ) NEW_LINE string1 = string1 . replace ( i , ' ' ) NEW_LINE DEDENT DEDENT return string1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string1 = " geeksforgeeks " NEW_LINE string2 = " mask " NEW_LINE print ( removeChars ( string1 , string2 ) ) NEW_LINE DEDENT
Remove duplicates from a given string | Python3 program to remove duplicate character from character array and prin sorted order ; Used as index in the modified string ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
def removeDuplicate ( str , n ) : NEW_LINE INDENT index = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , i + 1 ) : NEW_LINE INDENT if ( str [ i ] == str [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == i ) : NEW_LINE INDENT str [ index ] = str [ i ] NEW_LINE index += 1 NEW_LINE DEDENT DEDENT return " " . join ( str [ : index ] ) NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE n = len ( str ) NEW_LINE print ( removeDuplicate ( list ( str ) , n ) ) NEW_LINE
Check if it ’ s possible to completely fill every container with same ball | A boolean function that returns true if it 's possible to completely fill the container else return false ; Base Case ; Backtracking ; Function to check the conditions ; Storing frequencies ; Function Call for backtracking ; Driver Code ; Given Input ; Function Call
def getans ( i , v , q ) : NEW_LINE INDENT if ( i == len ( q ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for j in range ( len ( v ) ) : NEW_LINE INDENT if ( v [ j ] >= q [ i ] ) : NEW_LINE INDENT v [ j ] -= q [ i ] NEW_LINE if ( getans ( i + 1 , v , q ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT v [ j ] += q [ i ] NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def Check ( c , b ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT if b [ i ] in m : NEW_LINE INDENT m [ b [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ b [ i ] ] = 1 NEW_LINE DEDENT DEDENT v = [ ] NEW_LINE for key , value in m . items ( ) : NEW_LINE INDENT v . append ( value ) NEW_LINE DEDENT check = getans ( 0 , v , c ) NEW_LINE if ( check ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT c = [ 1 , 3 , 3 ] NEW_LINE b = [ 2 , 2 , 2 , 2 , 4 , 4 , 4 ] NEW_LINE Check ( c , b ) NEW_LINE DEDENT
Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges | Function to find the maximum Bitwise XOR of any subset of the array of size K ; Number of node must K + 1 for K edges ; Stores the maximum Bitwise XOR ; Generate all subsets of the array ; __builtin_popcount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update the maximum Bitwise XOR obtained so far ; Return the maximum XOR ; Driver Code
def maximumXOR ( arr , n , K ) : NEW_LINE INDENT K += 1 NEW_LINE maxXor = - 10 ** 9 NEW_LINE for i in range ( 1 << n ) : NEW_LINE INDENT if ( bin ( i ) . count ( '1' ) == K ) : NEW_LINE INDENT cur_xor = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT cur_xor = cur_xor ^ arr [ j ] NEW_LINE DEDENT DEDENT maxXor = max ( maxXor , cur_xor ) NEW_LINE DEDENT DEDENT return maxXor NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE M = 2 NEW_LINE print ( maximumXOR ( arr , N , M ) ) NEW_LINE DEDENT
Tower of Hanoi | Set 2 | Python program for the above approach ; Function to print order of movement of N disks across three rods to place all disks on the third rod from the first - rod using binary representation ; Iterate over the range [ 0 , 2 ^ N - 1 ] ; Print the movement of the current rod ; Driver Code
import math NEW_LINE def TowerOfHanoi ( N ) : NEW_LINE INDENT for x in range ( 1 , int ( math . pow ( 2 , N ) ) ) : NEW_LINE INDENT print ( " Move ▁ from ▁ Rod ▁ " , ( ( x & x - 1 ) % 3 + 1 ) , " ▁ to ▁ Rod ▁ " , ( ( ( x x - 1 ) + 1 ) % 3 + 1 ) ) NEW_LINE DEDENT DEDENT N = 3 NEW_LINE TowerOfHanoi ( N ) NEW_LINE