codeparrot/xlcost-text-to-code huggingface dataset:
Minimum characters to be added at front to make string palindrome | function for checking string is palindrome or not ; Driver code ; if string becomes palindrome then break ; erase the last element of the string ; print the number of insertion at front
def ispalindrome ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE i = 0 NEW_LINE j = l - 1 NEW_LINE while i <= j : NEW_LINE INDENT if ( s [ i ] != s [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " BABABAA " NEW_LINE cnt = 0 NEW_LINE flag = 0 NEW_LINE while ( len ( s ) > 0 ) : NEW_LINE INDENT if ( ispalindrome ( s ) ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE s = s [ : - 1 ] NEW_LINE s . erase ( s . begin ( ) + s . length ( ) - 1 ) ; NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( cnt ) NEW_LINE DEDENT DEDENT
Count characters at same position as in English alphabet | Function to count the number of characters at same position as in English alphabets ; Traverse the input string ; Check that index of characters of string is same as of English alphabets by using ASCII values and the fact that all lower case alphabetic characters come together in same order in ASCII table . And same is true for upper case . ; Driver Code
def findCount ( str ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( ( i == ord ( str [ i ] ) - ord ( ' a ' ) ) or ( i == ord ( str [ i ] ) - ord ( ' A ' ) ) ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT str = ' AbgdeF ' NEW_LINE print ( findCount ( str ) ) NEW_LINE
Remove a character from a string to make it a palindrome | Utility method to check if substring from low to high is palindrome or not . ; This method returns - 1 if it is not possible to make string a palindrome . It returns - 2 if string is already a palindrome . Otherwise it returns index of character whose removal can make the whole string palindrome . ; Initialize low and right by both the ends of the string ; loop until low and high cross each other ; If both characters are equal then move both pointer towards end ; If removing str [ low ] makes the whole string palindrome . We basically check if substring str [ low + 1. . high ] is palindrome or not . ; If removing str [ high ] makes the whole string palindrome We basically check if substring str [ low + 1. . high ] is palindrome or not ; We reach here when complete string will be palindrome if complete string is palindrome then return mid character ; Driver Code
def isPalindrome ( string : str , low : int , high : int ) -> bool : NEW_LINE INDENT while low < high : NEW_LINE INDENT if string [ low ] != string [ high ] : NEW_LINE INDENT return False NEW_LINE DEDENT low += 1 NEW_LINE high -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def possiblepalinByRemovingOneChar ( string : str ) -> int : NEW_LINE INDENT low = 0 NEW_LINE high = len ( string ) - 1 NEW_LINE while low < high : NEW_LINE INDENT if string [ low ] == string [ high ] : NEW_LINE INDENT low += 1 NEW_LINE high -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT if isPalindrome ( string , low + 1 , high ) : NEW_LINE INDENT return low NEW_LINE DEDENT if isPalindrome ( string , low , high - 1 ) : NEW_LINE INDENT return high NEW_LINE DEDENT return - 1 NEW_LINE DEDENT DEDENT return - 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abecbea " NEW_LINE idx = possiblepalinByRemovingOneChar ( string ) NEW_LINE if idx == - 1 : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT elif idx == - 2 : NEW_LINE INDENT print ( " Possible ▁ without ▁ removing ▁ any ▁ character " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Possible ▁ by ▁ removing ▁ character ▁ at ▁ index " , idx ) NEW_LINE DEDENT DEDENT
Generate all binary strings from given pattern | Recursive function to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; replace ' ? ' by '0' and recurse ; replace ' ? ' by '1' and recurse ; NOTE : Need to backtrack as string is passed by reference to the function ; Driver code
def _print ( string , index ) : NEW_LINE INDENT if index == len ( string ) : NEW_LINE INDENT print ( ' ' . join ( string ) ) NEW_LINE return NEW_LINE DEDENT if string [ index ] == " ? " : NEW_LINE INDENT string [ index ] = '0' NEW_LINE _print ( string , index + 1 ) NEW_LINE string [ index ] = '1' NEW_LINE _print ( string , index + 1 ) NEW_LINE string [ index ] = ' ? ' NEW_LINE DEDENT else : NEW_LINE INDENT _print ( string , index + 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "1 ? ? 0?101" NEW_LINE string = list ( string ) NEW_LINE _print ( string , 0 ) NEW_LINE DEDENT
Find the most frequent digit without using array / string | Simple function to count occurrences of digit d in x ; count = 0 ; Initialize count of digit d ; Increment count if current digit is same as d ; Returns the max occurring digit in x ; Handle negative number ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver Code
def countOccurrences ( x , d ) : NEW_LINE INDENT while ( x ) : NEW_LINE INDENT if ( x % 10 == d ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT x = int ( x / 10 ) ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT def maxOccurring ( x ) : NEW_LINE INDENT if ( x < 0 ) : NEW_LINE INDENT x = - x ; NEW_LINE DEDENT for d in range ( 10 ) : NEW_LINE INDENT count = countOccurrences ( x , d ) ; NEW_LINE if ( count >= max_count ) : NEW_LINE INDENT max_count = count ; NEW_LINE result = d ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT x = 1223355 ; NEW_LINE print ( " Max ▁ occurring ▁ digit ▁ is " , maxOccurring ( x ) ) ; NEW_LINE
Remove recurring digits in a given number | Removes recurring digits in num [ ] ; Index in modified string ; Traverse digits of given number one by one ; Copy the first occurrence of new digit ; Remove repeating occurrences of digit ; Driver code
def removeRecurringDigits ( num ) : NEW_LINE INDENT l = len ( num ) NEW_LINE ( i , j ) = ( 0 , 0 ) NEW_LINE str = ' ' NEW_LINE while i < l : NEW_LINE INDENT str += num [ i ] NEW_LINE j += 1 NEW_LINE while ( i + 1 < l and num [ i ] == num [ i + 1 ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return str NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = '1299888833' NEW_LINE print ( ' Modified ▁ number ▁ is ▁ { } ' . format ( removeRecurringDigits ( num ) ) ) NEW_LINE DEDENT
Find the maximum subarray XOR in a given array | A simple Python program to find max subarray XOR ; Initialize result ; Pick starting points of subarrays ; to store xor of current subarray ; Pick ending points of subarrays starting with i ; Driver code
def maxSubarrayXOR ( arr , n ) : NEW_LINE INDENT ans = - 2147483648 NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr_xor = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT curr_xor = curr_xor ^ arr [ j ] NEW_LINE ans = max ( ans , curr_xor ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 8 , 1 , 2 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Max ▁ subarray ▁ XOR ▁ is ▁ " , maxSubarrayXOR ( arr , n ) ) NEW_LINE
Recursive Implementation of atoi ( ) | Recursive function to compute atoi ( ) ; base case , we 've hit the end of the string,  we just return the last value ; If more than 1 digits , recur for ( n - 1 ) , multiplu result with 10 and add last digit ; Driver Code
def myAtoiRecursive ( string , num ) : NEW_LINE INDENT if len ( string ) == 1 : NEW_LINE INDENT return int ( string ) + ( num * 10 ) NEW_LINE DEDENT num = int ( string [ 0 : 1 ] ) + ( num * 10 ) NEW_LINE return myAtoiRecursive ( string [ 1 : ] , num ) NEW_LINE DEDENT string = "112" NEW_LINE print ( myAtoiRecursive ( string , 0 ) ) NEW_LINE
Print string of odd length in ' X ' format | Function to print the given string in respective pattern ; Print characters at corresponding places satisfying the two conditions ; Print blank space at rest of places ; Driver code
def printPattern ( Str , Len ) : NEW_LINE INDENT for i in range ( Len ) : NEW_LINE INDENT for j in range ( Len ) : NEW_LINE INDENT if ( ( i == j ) or ( i + j == Len - 1 ) ) : NEW_LINE INDENT print ( Str [ j ] , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT Str = " geeksforgeeks " NEW_LINE Len = len ( Str ) NEW_LINE printPattern ( Str , Len ) NEW_LINE
Find the longest substring with k unique characters in a given string | Python program to find the longest substring with k unique characters in a given string ; This function calculates number of unique characters using a associative array count [ ] . Returns true if no . of characters are less than required else returns false . ; Return true if k is greater than or equal to val ; Finds the maximum substring with exactly k unique characters ; Associative array to store the count ; Tranverse the string , fills the associative array count [ ] and count number of unique characters ; If there are not enough unique characters , show an error message . ; Otherwise take a window with first element in it . start and end variables . ; Also initialize values for result longest window ; Initialize associative array count [ ] with zero ; put the first character ; Start from the second character and add characters in window according to above explanation ; Add the character ' s [ i ] ' to current window ; If there are more than k unique characters in current window , remove from left side ; Update the max window size if required ; Driver function
MAX_CHARS = 26 NEW_LINE def isValid ( count , k ) : NEW_LINE INDENT val = 0 NEW_LINE for i in range ( MAX_CHARS ) : NEW_LINE INDENT if count [ i ] > 0 : NEW_LINE INDENT val += 1 NEW_LINE DEDENT DEDENT return ( k >= val ) NEW_LINE DEDENT def kUniques ( s , k ) : NEW_LINE INDENT count = [ 0 ] * MAX_CHARS NEW_LINE for i in range ( n ) : NEW_LINE INDENT if count [ ord ( s [ i ] ) - ord ( ' a ' ) ] == 0 : NEW_LINE INDENT u += 1 NEW_LINE DEDENT count [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT if u < k : NEW_LINE INDENT print ( " Not ▁ enough ▁ unique ▁ characters " ) NEW_LINE return NEW_LINE DEDENT curr_start = 0 NEW_LINE curr_end = 0 NEW_LINE max_window_size = 1 NEW_LINE max_window_start = 0 NEW_LINE count = [ 0 ] * len ( count ) NEW_LINE count [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] += 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT count [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE curr_end += 1 NEW_LINE while not isValid ( count , k ) : NEW_LINE INDENT count [ ord ( s [ curr_start ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE curr_start += 1 NEW_LINE DEDENT if curr_end - curr_start + 1 > max_window_size : NEW_LINE INDENT max_window_size = curr_end - curr_start + 1 NEW_LINE max_window_start = curr_start NEW_LINE DEDENT DEDENT print ( " Max ▁ substring ▁ is ▁ : ▁ " + s [ max_window_start : max_window_start + max_window_size ] + " ▁ with ▁ length ▁ " + str ( max_window_size ) ) NEW_LINE DEDENT s = " aabacbebebe " NEW_LINE k = 3 NEW_LINE kUniques ( s , k ) NEW_LINE
Check if characters of a given string can be rearranged to form a palindrome | Python3 implementation of above approach . ; bitvector to store the record of which character appear odd and even number of times ; Driver Code
def canFormPalindrome ( s ) : NEW_LINE INDENT bitvector = 0 NEW_LINE for str in s : NEW_LINE INDENT bitvector ^= 1 << ord ( str ) NEW_LINE DEDENT return bitvector == 0 or bitvector & ( bitvector - 1 ) == 0 NEW_LINE DEDENT if canFormPalindrome ( " geeksforgeeks " ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT
Print all pairs of anagrams in a given array of strings | Python3 program to find best meeting point in 2D array ; 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 " ; See if there is any non - zero value in count array ; This function prints all anagram pairs in a given array of strings ; Driver Code
NO_OF_CHARS = 256 NEW_LINE def areAnagram ( str1 : str , str2 : str ) -> bool : NEW_LINE INDENT count = [ 0 ] * NO_OF_CHARS NEW_LINE i = 0 NEW_LINE while i < len ( str1 ) and i < len ( str2 ) : NEW_LINE INDENT count [ ord ( str1 [ i ] ) ] += 1 NEW_LINE count [ ord ( str2 [ i ] ) ] -= 1 NEW_LINE i += 1 NEW_LINE DEDENT if len ( str1 ) != len ( str2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( NO_OF_CHARS ) : NEW_LINE INDENT if count [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT DEDENT def findAllAnagrams ( arr : list , n : int ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if areAnagram ( arr [ i ] , arr [ j ] ) : NEW_LINE INDENT print ( arr [ i ] , " is ▁ anagram ▁ of " , arr [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " geeksquiz " , " geeksforgeeks " , " abcd " , " forgeeksgeeks " , " zuiqkeegs " ] NEW_LINE n = len ( arr ) NEW_LINE findAllAnagrams ( arr , n ) NEW_LINE DEDENT
Program to print all palindromes in a given range | A function to check if n is palindrome ; Find reverse of n ; If n and rev are same , then n is palindrome ; prints palindrome between min and max ; Driver Code
def isPalindrome ( n : int ) -> bool : NEW_LINE INDENT rev = 0 NEW_LINE i = n NEW_LINE while i > 0 : NEW_LINE INDENT rev = rev * 10 + i % 10 NEW_LINE i //= 10 NEW_LINE DEDENT return ( n == rev ) NEW_LINE DEDENT def countPal ( minn : int , maxx : int ) -> None : NEW_LINE INDENT for i in range ( minn , maxx + 1 ) : NEW_LINE INDENT if isPalindrome ( i ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT countPal ( 100 , 2000 ) NEW_LINE DEDENT
Length of the longest substring without repeating characters | Python3 program to find the length of the longest substring without repeating characters ; Result ; Note : Default values in visited are false ; If current character is visited Break the loop ; Else update the result if this window is larger , and mark current character as visited . ; Remove the first character of previous window ; Driver code
def longestUniqueSubsttr ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT visited = [ 0 ] * 256 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT if ( visited [ ord ( str [ j ] ) ] == True ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT res = max ( res , j - i + 1 ) NEW_LINE visited [ ord ( str [ j ] ) ] = True NEW_LINE DEDENT DEDENT visited [ ord ( str [ i ] ) ] = False NEW_LINE DEDENT return res NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE print ( " The ▁ input ▁ is ▁ " , str ) NEW_LINE len = longestUniqueSubsttr ( str ) NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ longest ▁ " " non - repeating ▁ character ▁ substring ▁ is ▁ " , len ) NEW_LINE
Length of the longest substring without repeating characters | Creating a set to store the last positions of occurrence ; starting the initial point of window to index 0 ; Checking if we have already seen the element or not ; If we have seen the number , move the start pointer to position after the last occurrence ; Updating the last seen value of the character ; Driver Code
def longestUniqueSubsttr ( string ) : NEW_LINE INDENT seen = { } NEW_LINE maximum_length = 0 NEW_LINE start = 0 NEW_LINE for end in range ( len ( string ) ) : NEW_LINE INDENT if string [ end ] in seen : NEW_LINE INDENT start = max ( start , seen [ string [ end ] ] + 1 ) NEW_LINE DEDENT seen [ string [ end ] ] = end NEW_LINE maximum_length = max ( maximum_length , end - start + 1 ) NEW_LINE DEDENT return maximum_length NEW_LINE DEDENT string = " geeksforgeeks " NEW_LINE print ( " The ▁ input ▁ string ▁ is " , string ) NEW_LINE length = longestUniqueSubsttr ( string ) NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ character ▁ substring ▁ is " , length ) NEW_LINE
Find the smallest window in a string containing all characters of another string | Python solution ; Starting index of ans ; Answer Length of ans ; creating map ; References of Window ; Traversing the window ; Calculating ; Condition matching ; calculating answer . ; Sliding I Calculation for removing I ; Driver code
def smallestWindow ( s , p ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if n < len ( p ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mp = [ 0 ] * 256 NEW_LINE start = 0 NEW_LINE ans = n + 1 NEW_LINE cnt = 0 NEW_LINE for i in p : NEW_LINE INDENT mp [ ord ( i ) ] += 1 NEW_LINE if mp [ ord ( i ) ] == 1 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT j = 0 NEW_LINE i = 0 NEW_LINE while ( j < n ) : NEW_LINE INDENT mp [ ord ( s [ j ] ) ] -= 1 NEW_LINE if mp [ ord ( s [ j ] ) ] == 0 : NEW_LINE INDENT cnt -= 1 NEW_LINE while cnt == 0 : NEW_LINE INDENT if ans > j - i + 1 : NEW_LINE INDENT ans = j - i + 1 NEW_LINE start = i NEW_LINE DEDENT mp [ ord ( s [ i ] ) ] += 1 NEW_LINE if mp [ ord ( s [ i ] ) ] > 0 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT if ans > n : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT return s [ start : start + ans ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ADOBECODEBANC " NEW_LINE p = " ABC " NEW_LINE result = smallestWindow ( s , p ) NEW_LINE print ( " - - > Smallest ▁ window ▁ that ▁ contain ▁ all ▁ character ▁ : " , result ) NEW_LINE DEDENT
Run Length Encoding | Python3 program to implement run length encoding ; Count occurrences of current character ; Print character and its count ; Driver code
def printRLE ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE i = 0 NEW_LINE while i < n - 1 : NEW_LINE INDENT count = 1 NEW_LINE while ( i < n - 1 and st [ i ] == st [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE print ( st [ i - 1 ] + str ( count ) , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " wwwwaaadexxxxxxywww " NEW_LINE printRLE ( st ) NEW_LINE DEDENT
Print list items containing all characters of a given word | Python program to print the list items containing all characters of a given word ; Prints list items having all characters of word ; Set the values in map ; Get the length of given word ; Check each item of list if has all characters of words ; unset the bit so that strings like sss not printed ; Set the values in map for next item ; Driver program to test the above function
NO_OF_CHARS = 256 NEW_LINE def printList ( list , word , list_size ) : NEW_LINE INDENT map = [ 0 ] * NO_OF_CHARS NEW_LINE for i in word : NEW_LINE INDENT map [ ord ( i ) ] = 1 NEW_LINE DEDENT word_size = len ( word ) NEW_LINE for i in list : NEW_LINE INDENT count = 0 NEW_LINE for j in i : NEW_LINE INDENT if map [ ord ( j ) ] : NEW_LINE INDENT count += 1 NEW_LINE map [ ord ( j ) ] = 0 NEW_LINE DEDENT DEDENT if count == word_size : NEW_LINE INDENT print i NEW_LINE DEDENT for j in xrange ( len ( word ) ) : NEW_LINE INDENT map [ ord ( word [ j ] ) ] = 1 NEW_LINE DEDENT DEDENT DEDENT string = " sun " NEW_LINE list = [ " geeksforgeeks " , " unsorted " , " sunday " , " just " , " sss " ] NEW_LINE printList ( list , string , 5 ) NEW_LINE
Given a string , find its first non | Python program to print the first non - repeating character ; Returns an array of size 256 containing count of characters in the passed char array ; The function returns index of first non - repeating character in a string . If all characters are repeating then returns - 1 ; Driver program to test above function
NO_OF_CHARS = 256 NEW_LINE def getCharCountArray ( string ) : NEW_LINE INDENT count = [ 0 ] * NO_OF_CHARS NEW_LINE for i in string : NEW_LINE INDENT count [ ord ( i ) ] += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def firstNonRepeating ( string ) : NEW_LINE INDENT count = getCharCountArray ( string ) NEW_LINE index = - 1 NEW_LINE k = 0 NEW_LINE for i in string : NEW_LINE INDENT if count [ ord ( i ) ] == 1 : NEW_LINE INDENT index = k NEW_LINE break NEW_LINE DEDENT k += 1 NEW_LINE DEDENT return index NEW_LINE DEDENT string = " geeksforgeeks " NEW_LINE index = firstNonRepeating ( string ) NEW_LINE if index == 1 : NEW_LINE INDENT print " Either ▁ all ▁ characters ▁ are ▁ repeating ▁ or ▁ string ▁ is ▁ empty " NEW_LINE DEDENT else : NEW_LINE INDENT print " First ▁ non - repeating ▁ character ▁ is ▁ " + string [ index ] NEW_LINE DEDENT
Divide a string in N equal parts | Function to print n equal parts of string ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division points ; Driver program to test the above function Length of string is 28 ;  ; Print 4 equal parts of the string
def divideString ( string , n ) : NEW_LINE INDENT str_size = len ( string ) NEW_LINE if str_size % n != 0 : NEW_LINE INDENT print " Invalid ▁ Input : ▁ String ▁ size ▁ is ▁ not ▁ divisible ▁ by ▁ n " NEW_LINE return NEW_LINE DEDENT part_size = str_size / n NEW_LINE k = 0 NEW_LINE for i in string : NEW_LINE INDENT if k % part_size == 0 : NEW_LINE INDENT print   " NEW_LINE DEDENT DEDENT DEDENT " , NEW_LINE INDENT print i , NEW_LINE k += 1 NEW_LINE DEDENT string = " a _ simple _ divide _ string _ quest " NEW_LINE / * length od string is 28 * / NEW_LINE divideString ( string , 4 ) NEW_LINE
Print all paths from a source point to all the 4 corners of a Matrix | Function to check if we reached on of the entry / exit ( corner ) point . ; Function to check if the index is within the matrix boundary . ; Recursive helper function ; If any corner is reached push the string t into ans and return ; For all the four directions ; The new ith index ; The new jth index ; The direction R / L / U / D ; If the new cell is within the matrix boundary and it is not previously visited in same path ; mark the new cell visited ; Store the direction ; Backtrack to explore other paths ; Function to find all possible paths ; Create a direction array for all the four directions ; stores the result ; Initialise variable ; function call ; Print the result
def isCorner ( i , j , M , N ) : NEW_LINE INDENT if ( ( i == 0 and j == 0 ) or ( i == 0 and j == N - 1 ) or ( i == M - 1 and j == N - 1 ) or ( i == M - 1 and j == 0 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isValid ( i , j , M , N ) : NEW_LINE INDENT if ( i < 0 or i >= M or j < 0 or j >= N ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def solve ( i , j , M , N , Dir , maze , t , ans ) : NEW_LINE INDENT if ( isCorner ( i , j , M , N ) ) : NEW_LINE INDENT ans . append ( t ) NEW_LINE return NEW_LINE DEDENT for k in range ( 4 ) : NEW_LINE INDENT x = i + Dir [ k ] [ 0 ] NEW_LINE y = j + Dir [ k ] [ 1 ] NEW_LINE c = Dir [ k ] [ 2 ] NEW_LINE if ( isValid ( x , y , M , N ) and maze [ x ] [ y ] == 1 ) : NEW_LINE INDENT maze [ x ] [ y ] = 0 NEW_LINE t += c NEW_LINE solve ( x , y , M , N , Dir , maze , t , ans ) NEW_LINE t = t [ : len ( t ) - 1 ] NEW_LINE maze [ x ] [ y ] = 1 NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT def possiblePaths ( src , maze ) : NEW_LINE INDENT Dir = [ [ - 1 , 0 , ' U ' ] , [ 0 , 1 , ' R ' ] , [ 1 , 0 , ' D ' ] , [ 0 , - 1 , ' L ' ] ] NEW_LINE temp = " " NEW_LINE ans = [ ] NEW_LINE solve ( src [ 0 ] , src [ 1 ] , len ( maze ) , len ( maze [ 0 ] ) , Dir , maze , temp , ans ) NEW_LINE return ans NEW_LINE DEDENT maze = [ [ 1 , 0 , 0 , 1 , 0 , 0 , 1 , 1 ] , [ 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 ] , [ 1 , 0 , 1 , 1 , 1 , 1 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] , [ 1 , 0 , 1 , 0 , 1 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 ] , [ 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 ] ] NEW_LINE src = [ 4 , 2 ] NEW_LINE paths = possiblePaths ( src , maze ) NEW_LINE if ( len ( paths ) == 0 ) : NEW_LINE INDENT print ( " No ▁ Possible ▁ Paths " ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in paths : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT DEDENT