codeparrot/xlcost-text-to-code huggingface dataset:
Count strings that end with the given pattern | Function that return true if str1 ends with pat ; Pattern is larger in length than the str1ing ; We match starting from the end while patLen is greater than or equal to 0. ; If at any index str1 doesn 't match  with pattern ; If str1 ends with the given pattern ; Function to return the count of required str1ings ; If current str1ing ends with the given pattern ; Driver code
def endsWith ( str1 , pat ) : NEW_LINE INDENT patLen = len ( pat ) NEW_LINE str1Len = len ( str1 ) NEW_LINE if ( patLen > str1Len ) : NEW_LINE INDENT return False NEW_LINE DEDENT patLen -= 1 NEW_LINE str1Len -= 1 NEW_LINE while ( patLen >= 0 ) : NEW_LINE INDENT if ( pat [ patLen ] != str1 [ str1Len ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT patLen -= 1 NEW_LINE str1Len -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def countOfstr1ings ( pat , n , sArr ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( endsWith ( sArr [ i ] , pat ) == True ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT pat = " ks " NEW_LINE n = 4 NEW_LINE sArr = [ " geeks " , " geeksforgeeks " , " games " , " unit " ] NEW_LINE print ( countOfstr1ings ( pat , n , sArr ) ) NEW_LINE
Length of the longest substring with consecutive characters | Function to return the ending index for the largest valid sub - str1ing starting from index i ; If the current character appears after the previous character according to the given circular alphabetical order ; Function to return the length of the longest sub - str1ing of consecutive characters from str1 ; Valid sub - str1ing exists from index i to end ; Update the Length ; Driver code
def getEndingIndex ( str1 , n , i ) : NEW_LINE INDENT i += 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT curr = str1 [ i ] NEW_LINE prev = str1 [ i - 1 ] NEW_LINE if ( ( curr == ' a ' and prev == ' z ' ) or ( ord ( curr ) - ord ( prev ) == 1 ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i - 1 NEW_LINE DEDENT def largestSubstr1 ( str1 , n ) : NEW_LINE INDENT Len = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT end = getEndingIndex ( str1 , n , i ) NEW_LINE Len = max ( end - i + 1 , Len ) NEW_LINE i = end + 1 NEW_LINE DEDENT return Len NEW_LINE DEDENT str1 = " abcabcdefabc " NEW_LINE n = len ( str1 ) NEW_LINE print ( largestSubstr1 ( str1 , n ) ) NEW_LINE
Sum of integers upto N with given unit digit ( Set 2 ) | Function to return the required sum ; Decrement N ; Driver code
def getSum ( n , d ) : NEW_LINE INDENT if ( n < d ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( n % 10 != d ) : NEW_LINE INDENT n -= 1 NEW_LINE DEDENT k = n // 10 NEW_LINE return ( ( k + 1 ) * d + ( k * 10 + 10 * k * k ) // 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 30 NEW_LINE d = 3 NEW_LINE print ( getSum ( n , d ) ) NEW_LINE DEDENT
Maximum possible time that can be formed from four digits | Python3 implementation of the approach ; Function to return the updated frequency map for the array passed as argument ; Function that returns true if the passed digit is present in the map after decrementing it 's frequency by 1 ; If map contains the digit ; Decrement the frequency of the digit by 1 ; True here indicates that the digit was found in the map ; Digit not found ; Function to return the maximum possible time_value in 24 - Hours format ; First digit of hours can be from the range [ 0 , 2 ] ; If no valid digit found ; If first digit of hours was chosen as 2 then the second digit of hours can be from the range [ 0 , 3 ] ; Else it can be from the range [ 0 , 9 ] ; Hours and minutes separator ; First digit of minutes can be from the range [ 0 , 5 ] ; Second digit of minutes can be from the range [ 0 , 9 ] ; Return the maximum possible time_value ; Driver code
from collections import defaultdict NEW_LINE def getFrequencyMap ( arr , n ) : NEW_LINE INDENT hashMap = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hashMap [ arr [ i ] ] += 1 NEW_LINE DEDENT return hashMap NEW_LINE DEDENT def hasDigit ( hashMap , digit ) : NEW_LINE INDENT if hashMap [ digit ] > 0 : NEW_LINE INDENT hashMap [ digit ] -= 1 NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE DEDENT def getMaxtime_value ( arr , n ) : NEW_LINE INDENT hashMap = getFrequencyMap ( arr , n ) NEW_LINE flag = False NEW_LINE time_value = " " NEW_LINE for i in range ( 2 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT if not flag : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT flag = False NEW_LINE if ( time_value [ 0 ] == '2' ) : NEW_LINE INDENT for i in range ( 3 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 9 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if not flag : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT time_value += " : " NEW_LINE flag = False NEW_LINE for i in range ( 5 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT if not flag : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT flag = False NEW_LINE for i in range ( 9 , - 1 , - 1 ) : NEW_LINE INDENT if hasDigit ( hashMap , i ) == True : NEW_LINE INDENT flag = True NEW_LINE time_value += str ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT if not flag : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT return time_value NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getMaxtime_value ( arr , n ) ) NEW_LINE DEDENT
Most frequent word in first String which is not present in second String | Python3 implementation of above approach ; Function to return frequent word from S1 that isn 't present in S2 ; create map of banned words ; find smallest and most frequent word ; check if word is not banned ; return answer ; Driver Code
from collections import defaultdict NEW_LINE def smallestFreq ( S1 , S2 ) : NEW_LINE INDENT banned = defaultdict ( lambda : 0 ) NEW_LINE i = 0 NEW_LINE while i < len ( S2 ) : NEW_LINE INDENT s = " " NEW_LINE while i < len ( S2 ) and S2 [ i ] != ' ▁ ' : NEW_LINE INDENT s += S2 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE banned [ s ] += 1 NEW_LINE DEDENT result = defaultdict ( lambda : 0 ) NEW_LINE ans = " " NEW_LINE freq = 0 NEW_LINE i = 0 NEW_LINE while i < len ( S1 ) : NEW_LINE INDENT s = " " NEW_LINE while i < len ( S1 ) and S1 [ i ] != ' ▁ ' : NEW_LINE INDENT s += S1 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE if banned [ s ] == 0 : NEW_LINE INDENT result [ s ] += 1 NEW_LINE if ( result [ s ] > freq or ( result [ s ] == freq and s < ans ) ) : NEW_LINE INDENT ans = s NEW_LINE freq = result [ s ] NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S1 = " geeks ▁ for ▁ geeks ▁ is ▁ best ▁ place ▁ to ▁ learn " NEW_LINE S2 = " bad ▁ place " NEW_LINE print ( smallestFreq ( S1 , S2 ) ) NEW_LINE DEDENT
Minimum characters to be replaced to remove the given substring | Function to calculate minimum characters to replace ; mismatch occurs ; If all characters matched , i . e , there is a substring of ' a ' which is same as string ' b ' ; increment i to index m - 1 such that minimum characters are replaced in ' a ' ; Driver Code
def replace ( A , B ) : NEW_LINE INDENT n , m = len ( A ) , len ( B ) NEW_LINE count , i = 0 , 0 NEW_LINE while i < n : NEW_LINE INDENT j = 0 NEW_LINE while j < m : NEW_LINE INDENT if i + j >= n or A [ i + j ] != B [ j ] : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if j == m : NEW_LINE INDENT count += 1 NEW_LINE i += m - 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " aaaaaaaa " NEW_LINE str2 = " aaa " NEW_LINE print ( replace ( str1 , str2 ) ) NEW_LINE DEDENT
Check if a string is substring of another | Returns true if s1 is substring of s2 ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; Driver Code
def isSubstring ( s1 , s2 ) : NEW_LINE INDENT M = len ( s1 ) NEW_LINE N = len ( s2 ) NEW_LINE for i in range ( N - M + 1 ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( s2 [ i + j ] != s1 [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if j + 1 == M : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " for " NEW_LINE s2 = " geeksforgeeks " NEW_LINE res = isSubstring ( s1 , s2 ) NEW_LINE if res == - 1 : NEW_LINE INDENT print ( " Not ▁ present " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Present ▁ at ▁ index ▁ " + str ( res ) ) NEW_LINE DEDENT DEDENT
Splitting a Numeric String | Function accepts a string and checks if string can be split . ; if there is only 1 number in the string then it is not possible to split it ; storing the substring from 0 to i + 1 to form initial number of the increasing sequence ; convert string to integer and add 1 and again convert back to string s2 ; if s2 is not a substring of number than not possible ; if s2 is the next substring of the numeric string ; Incearse num2 by 1 i . e the next number to be looked for ; check if string is fully traversed then break ; If next string doesnot occurs in a given numeric string then it is not possible ; if the string was fully traversed and conditions were satisfied ; if conditions failed to hold ; Driver code ; Call the split function for splitting the string
def split ( Str ) : NEW_LINE INDENT Len = len ( Str ) NEW_LINE if ( Len == 1 ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT s1 , s2 = " " , " " NEW_LINE for i in range ( ( Len // 2 ) + 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE s1 = Str [ 0 : i + 1 ] NEW_LINE num1 = int ( s1 ) NEW_LINE num2 = num1 + 1 NEW_LINE s2 = str ( num2 ) NEW_LINE k = i + 1 NEW_LINE while ( flag == 0 ) : NEW_LINE INDENT l = len ( s2 ) NEW_LINE if ( k + l > Len ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT if ( ( Str [ k : k + l ] == s2 ) ) : NEW_LINE INDENT flag = 0 NEW_LINE num2 += 1 NEW_LINE k = k + l NEW_LINE if ( k == Len ) : NEW_LINE INDENT break NEW_LINE DEDENT s2 = str ( num2 ) NEW_LINE l = len ( s2 ) NEW_LINE if ( k + 1 > len ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Possible " , s1 ) NEW_LINE break NEW_LINE DEDENT elif ( flag == 1 and i > ( Len // 2 ) - 1 ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT Str = "99100" NEW_LINE split ( Str ) NEW_LINE
Count of number of given string in 2D character array | utility function to search complete string from any given index of 2d array ; through Backtrack searching in every directions ; Function to search the string in 2d array ; Driver code
def internalSearch ( ii , needle , row , col , hay , row_max , col_max ) : NEW_LINE INDENT found = 0 NEW_LINE if ( row >= 0 and row <= row_max and col >= 0 and col <= col_max and needle [ ii ] == hay [ row ] [ col ] ) : NEW_LINE INDENT match = needle [ ii ] NEW_LINE ii += 1 NEW_LINE hay [ row ] [ col ] = 0 NEW_LINE if ( ii == len ( needle ) ) : NEW_LINE INDENT found = 1 NEW_LINE DEDENT else : NEW_LINE INDENT found += internalSearch ( ii , needle , row , col + 1 , hay , row_max , col_max ) NEW_LINE found += internalSearch ( ii , needle , row , col - 1 , hay , row_max , col_max ) NEW_LINE found += internalSearch ( ii , needle , row + 1 , col , hay , row_max , col_max ) NEW_LINE found += internalSearch ( ii , needle , row - 1 , col , hay , row_max , col_max ) NEW_LINE DEDENT hay [ row ] [ col ] = match NEW_LINE DEDENT return found NEW_LINE DEDENT def searchString ( needle , row , col , strr , row_count , col_count ) : NEW_LINE INDENT found = 0 NEW_LINE for r in range ( row_count ) : NEW_LINE INDENT for c in range ( col_count ) : NEW_LINE INDENT found += internalSearch ( 0 , needle , r , c , strr , row_count - 1 , col_count - 1 ) NEW_LINE DEDENT DEDENT return found NEW_LINE DEDENT needle = " MAGIC " NEW_LINE inputt = [ " BBABBM " , " CBMBBA " , " IBABBG " , " GOZBBI " , " ABBBBC " , " MCIGAM " ] NEW_LINE strr = [ 0 ] * len ( inputt ) NEW_LINE for i in range ( len ( inputt ) ) : NEW_LINE INDENT strr [ i ] = list ( inputt [ i ] ) NEW_LINE DEDENT print ( " count : ▁ " , searchString ( needle , 0 , 0 , strr , len ( strr ) , len ( strr [ 0 ] ) ) ) NEW_LINE
Find minimum shift for longest common prefix | function for KMP search ; preprocessing of longest proper prefix ; find out the longest prefix and position ; for new position with longer prefix in str2 update pos and Len ; prresult ; Driver Code
def KMP ( m , n , str2 , str1 ) : NEW_LINE INDENT pos = 0 NEW_LINE Len = 0 NEW_LINE p = [ 0 for i in range ( m + 1 ) ] NEW_LINE k = 0 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT while ( k > 0 and str1 [ k ] != str1 [ i - 1 ] ) : NEW_LINE INDENT k = p [ k ] NEW_LINE DEDENT if ( str1 [ k ] == str1 [ i - 1 ] ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT p [ i ] = k NEW_LINE DEDENT j = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT while ( j > 0 and j < n and str1 [ j ] != str2 [ i ] ) : NEW_LINE INDENT j = p [ j ] NEW_LINE DEDENT if ( j < n and str1 [ j ] == str2 [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( j > Len ) : NEW_LINE INDENT Len = j NEW_LINE pos = i - j + 1 NEW_LINE DEDENT DEDENT print ( " Shift ▁ = ▁ " , pos ) NEW_LINE print ( " Prefix ▁ = ▁ " , str1 [ : Len ] ) NEW_LINE DEDENT str1 = " geeksforgeeks " NEW_LINE str2 = " forgeeksgeeks " NEW_LINE n = len ( str1 ) NEW_LINE str2 = str2 + str2 NEW_LINE KMP ( 2 * n , n , str2 , str1 ) NEW_LINE
Find all the patterns of "1(0 + ) 1" in a given string | SET 1 ( General Approach ) | Function to count patterns ; Variable to store the last character ; We found 0 and last character was '1' , state change ; After the stream of 0 ' s , ▁ we ▁ got ▁ a ▁ ' 1 ',  counter incremented ; Last character stored ; Driver Code
def patternCount ( str ) : NEW_LINE INDENT last = str [ 0 ] NEW_LINE i = 1 ; counter = 0 NEW_LINE while ( i < len ( str ) ) : NEW_LINE INDENT if ( str [ i ] == '0' and last == '1' ) : NEW_LINE INDENT while ( str [ i ] == '0' ) : NEW_LINE INDENT i += 1 NEW_LINE if ( str [ i ] == '1' ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT DEDENT DEDENT last = str [ i ] NEW_LINE i += 1 NEW_LINE DEDENT return counter NEW_LINE DEDENT str = "1001ab010abc01001" NEW_LINE ans = patternCount ( str ) NEW_LINE print ( ans ) NEW_LINE
Boyer Moore Algorithm | Good Suffix heuristic | preprocessing for strong good suffix rule ; m is the length of pattern ; if character at position i - 1 is not equivalent to character at j - 1 , then continue searching to right of the pattern for border ; the character preceding the occurrence of t in pattern P is different than the mismatching character in P , we stop skipping the occurrences and shift the pattern from i to j ; Update the position of next border ; p [ i - 1 ] matched with p [ j - 1 ] , border is found . store the beginning position of border ; Preprocessing for case 2 ; set the border position of the first character of the pattern to all indices in array shift having shift [ i ] = 0 ; suffix becomes shorter than bpos [ 0 ] , use the position of next widest border as value of j ; Search for a pattern in given text using Boyer Moore algorithm with Good suffix rule ; s is shift of the pattern with respect to text ; initialize all occurrence of shift to 0 ; do preprocessing ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at the current shift , then index j will become - 1 after the above loop ; pat [ i ] != pat [ s + j ] so shift the pattern shift [ j + 1 ] times ; Driver Code
def preprocess_strong_suffix ( shift , bpos , pat , m ) : NEW_LINE INDENT i = m NEW_LINE j = m + 1 NEW_LINE bpos [ i ] = j NEW_LINE while i > 0 : NEW_LINE INDENT while j <= m and pat [ i - 1 ] != pat [ j - 1 ] : NEW_LINE INDENT if shift [ j ] == 0 : NEW_LINE INDENT shift [ j ] = j - i NEW_LINE DEDENT j = bpos [ j ] NEW_LINE DEDENT i -= 1 NEW_LINE j -= 1 NEW_LINE bpos [ i ] = j NEW_LINE DEDENT DEDENT def preprocess_case2 ( shift , bpos , pat , m ) : NEW_LINE INDENT j = bpos [ 0 ] NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT if shift [ i ] == 0 : NEW_LINE INDENT shift [ i ] = j NEW_LINE DEDENT if i == j : NEW_LINE INDENT j = bpos [ j ] NEW_LINE DEDENT DEDENT DEDENT def search ( text , pat ) : NEW_LINE INDENT s = 0 NEW_LINE m = len ( pat ) NEW_LINE n = len ( text ) NEW_LINE bpos = [ 0 ] * ( m + 1 ) NEW_LINE shift = [ 0 ] * ( m + 1 ) NEW_LINE preprocess_strong_suffix ( shift , bpos , pat , m ) NEW_LINE preprocess_case2 ( shift , bpos , pat , m ) NEW_LINE while s <= n - m : NEW_LINE INDENT j = m - 1 NEW_LINE while j >= 0 and pat [ j ] == text [ s + j ] : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT if j < 0 : NEW_LINE INDENT print ( " pattern ▁ occurs ▁ at ▁ shift ▁ = ▁ % d " % s ) NEW_LINE s += shift [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT s += shift [ j + 1 ] NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT text = " ABAAAABAACD " NEW_LINE pat = " ABA " NEW_LINE search ( text , pat ) NEW_LINE DEDENT
Maximum length prefix of one string that occurs as subsequence in another | Return the maximum length prefix which is subsequence . ; Iterating string T . ; If end of string S . ; If character match , increment counter . ; Driver Code
def maxPrefix ( s , t ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , len ( t ) ) : NEW_LINE INDENT if ( count == len ( s ) ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( t [ i ] == s [ count ] ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT S = " digger " NEW_LINE T = " biggerdiagram " NEW_LINE print ( maxPrefix ( S , T ) ) NEW_LINE
Replace all occurrences of string AB with C without using extra space | Python 3 program to replace all occurrences of " AB " with " C " ; Start traversing from second character ; If previous character is ' A ' and current character is 'B" ; Replace previous character with ' C ' and move all subsequent characters one position back ; Driver code
def translate ( st ) : NEW_LINE INDENT for i in range ( 1 , len ( st ) ) : NEW_LINE INDENT if ( st [ i - 1 ] == ' A ' and st [ i ] == ' B ' ) : NEW_LINE INDENT st [ i - 1 ] = ' C ' NEW_LINE for j in range ( i , len ( st ) - 1 ) : NEW_LINE INDENT st [ j ] = st [ j + 1 ] NEW_LINE DEDENT st [ len ( st ) - 1 ] = ' ▁ ' NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT st = list ( " helloABworldABGfG " ) NEW_LINE translate ( st ) NEW_LINE print ( " The ▁ modified ▁ string ▁ is ▁ : " ) NEW_LINE print ( ' ' . join ( st ) ) NEW_LINE
Find all occurrences of a given word in a matrix | Python3 Program to find all occurrences of the word in a matrix ; check whether given cell ( row , col ) is a valid cell or not . ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 8 neighboursof a given cell ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; return if current character doesn 't match with  the next character in the word ; append current character position to path ; current character matches with the last character \ in the word ; Recur for all connected neighbours ; The main function to find all occurrences of the word in a matrix ; traverse through the all cells of given matrix ; occurrence of first character in matrix ; check and prif path exists ; Driver code
ROW = 3 NEW_LINE COL = 5 NEW_LINE def isvalid ( row , col , prevRow , prevCol ) : NEW_LINE INDENT return ( row >= 0 ) and ( row < ROW ) and ( col >= 0 ) and ( col < COL ) and not ( row == prevRow and col == prevCol ) NEW_LINE DEDENT rowNum = [ - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 ] NEW_LINE colNum = [ - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 ] NEW_LINE def DFS ( mat , row , col , prevRow , prevCol , word , path , index , n ) : NEW_LINE INDENT if ( index > n or mat [ row ] [ col ] != word [ index ] ) : NEW_LINE INDENT return NEW_LINE DEDENT path += word [ index ] + " ( " + str ( row ) + " , ▁ " + str ( col ) + " ) ▁ " NEW_LINE if ( index == n ) : NEW_LINE INDENT print ( path ) NEW_LINE return NEW_LINE DEDENT for k in range ( 8 ) : NEW_LINE INDENT if ( isvalid ( row + rowNum [ k ] , col + colNum [ k ] , prevRow , prevCol ) ) : NEW_LINE INDENT DFS ( mat , row + rowNum [ k ] , col + colNum [ k ] , row , col , word , path , index + 1 , n ) NEW_LINE DEDENT DEDENT DEDENT def findWords ( mat , word , n ) : NEW_LINE INDENT for i in range ( ROW ) : NEW_LINE INDENT for j in range ( COL ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == word [ 0 ] ) : NEW_LINE INDENT DFS ( mat , i , j , - 1 , - 1 , word , " " , 0 , n ) NEW_LINE DEDENT DEDENT DEDENT DEDENT mat = [ [ ' B ' , ' N ' , ' E ' , ' Y ' , ' S ' ] , [ ' H ' , ' E ' , ' D ' , ' E ' , ' S ' ] , [ ' S ' , ' G ' , ' N ' , ' D ' , ' E ' ] ] NEW_LINE word = list ( " DES " ) NEW_LINE findWords ( mat , word , len ( word ) - 1 ) NEW_LINE
Generate N Random Hexadecimal Numbers | Python3 program for the above approach ; Maximum length of the random integer ; Function to generate N Hexadecimal integers ; Stores all the possible charcters in the Hexadecimal notation ; Loop to print N integers ; Randomly select length of the int in the range [ 1 , maxSize ] ; Print len charcters ; Print a randomly selected character ; Driver Code
import random , math NEW_LINE maxSize = 10 ; NEW_LINE def randomHexInt ( N ) : NEW_LINE INDENT hexChar = [ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' ] ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT Len = math . floor ( random . random ( ) * ( maxSize - 1 ) + 1 ) NEW_LINE for j in range ( Len ) : NEW_LINE INDENT print ( hexChar [ math . floor ( random . random ( ) * 16 ) ] , end = " " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 ; NEW_LINE randomHexInt ( N ) ; NEW_LINE DEDENT
Check if two array of string are equal by performing swapping operations | Function to check whether brr [ ] can be made equal to arr [ ] by doing swapping operations ; siz variable to store size of string ; iterate till N to sort strings ; sort each string in arr [ ] ; sort each string in brr [ ] ; Sort both the vectors so that all the comparable strings will be arranged ; iterate till N to compare string in arr [ ] and brr [ ] ; Compare each string ; Return false becaues if atleast one string is not equal then brr [ ] cannot be converted to arr [ ] ; All the strings are compared so at last return true . ; Driver code ; Store the answer in variable
def checkEqualArrays ( arr , brr , N ) : NEW_LINE INDENT siz = len ( arr [ 0 ] ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp1 = list ( arr [ i ] ) ; NEW_LINE temp1 . sort ( ) ; NEW_LINE arr [ i ] = " " . join ( temp1 ) NEW_LINE temp2 = list ( brr [ i ] ) ; NEW_LINE temp2 . sort ( ) ; NEW_LINE brr [ i ] = " " . join ( temp2 ) ; NEW_LINE DEDENT arr . sort ( ) NEW_LINE brr . sort ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] != brr [ i ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE arr = [ " bcd " , " aac " ] ; NEW_LINE brr = [ " aca " , " dbc " ] ; NEW_LINE ans = checkEqualArrays ( arr , brr , N ) ; NEW_LINE if ( ans ) : NEW_LINE INDENT print ( " true " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " false " ) ; NEW_LINE DEDENT DEDENT