codeparrot/xlcost-text-to-code huggingface dataset:
Minimum size substring to be removed to make a given string palindromic | Function to find palindromic prefix of maximum length ; Finding palindromic prefix of maximum length ; Checking if curr substring is palindrome or not . ; Condition to check if the prefix is a palindrome ; if no palindrome exist ; Function to find the maximum size palindrome such that after removing minimum size substring ; Finding prefix and suffix of same length ; Matching the ranges ; Case 1 : Length is even and whole string is palindrome ; Case 2 : Length is odd and whole string is palindrome ; Adding that mid character ; Add prefix or suffix of the remaining string or suffix , whichever is longer ; Reverse the remaining string to find the palindromic suffix ; Driver Code
def palindromePrefix ( S ) : NEW_LINE INDENT n = len ( S ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT curr = S [ 0 : i + 1 ] NEW_LINE l = 0 NEW_LINE r = len ( curr ) - 1 NEW_LINE is_palindrome = 1 NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( curr [ l ] != curr [ r ] ) : NEW_LINE INDENT is_palindrome = 0 NEW_LINE break NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT if ( is_palindrome ) : NEW_LINE INDENT return curr NEW_LINE DEDENT DEDENT return " " NEW_LINE DEDENT def maxPalindrome ( S ) : NEW_LINE INDENT n = len ( S ) NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return S NEW_LINE DEDENT pre = " " NEW_LINE suff = " " NEW_LINE i = 0 NEW_LINE j = n - 1 NEW_LINE while ( S [ i ] == S [ j ] and i < j ) : NEW_LINE INDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT i -= 1 NEW_LINE j += 1 NEW_LINE pre = S [ 0 : i + 1 ] NEW_LINE suff = S [ j : ] NEW_LINE if ( j - i == 1 ) : NEW_LINE INDENT return pre + suff NEW_LINE DEDENT if ( j - i == 2 ) : NEW_LINE INDENT mid_char = S [ i + 1 : i + 2 ] NEW_LINE return pre + mid_char + suff NEW_LINE DEDENT rem_str = S [ i + 1 : j ] NEW_LINE pre_of_rem_str = palindromePrefix ( rem_str ) NEW_LINE list1 = list ( rem_str ) NEW_LINE list1 . reverse ( ) NEW_LINE rem_str = ' ' . join ( list1 ) NEW_LINE suff_of_rem_str = palindromePrefix ( rem_str ) NEW_LINE if ( len ( pre_of_rem_str ) >= len ( suff_of_rem_str ) ) : NEW_LINE INDENT return ( pre + pre_of_rem_str + suff ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( pre + suff_of_rem_str + suff ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforskeeg " NEW_LINE print ( maxPalindrome ( S ) ) NEW_LINE DEDENT
Count numbers from a given range that contains a given number as the suffix | Python3 program of the above approach ; Function to count the number ends with given number in range ; Find number of digits in A ; Find the power of 10 ; Incrementing the A ; Driver Code ; Function call
from math import log10 NEW_LINE def countNumEnds ( A , L , R ) : NEW_LINE INDENT count = 0 NEW_LINE digits = int ( log10 ( A ) + 1 ) NEW_LINE temp = int ( pow ( 10 , digits ) ) NEW_LINE cycle = temp NEW_LINE while ( temp <= R ) : NEW_LINE INDENT if ( temp >= L ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT temp += cycle NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT A = 2 NEW_LINE L = 2 NEW_LINE R = 20 NEW_LINE countNumEnds ( A , L , R ) NEW_LINE
Minimize replacement of characters to its nearest alphabet to make a string palindromic | Function to find the minimum number of operations required to convert th given string into palindrome ; Iterate till half of the string ; Find the absolute difference between the characters ; Adding the minimum difference of the two result ; Return the result ; Given string ; Function call
def minOperations ( s ) : NEW_LINE INDENT length = len ( s ) NEW_LINE result = 0 NEW_LINE for i in range ( length // 2 ) : NEW_LINE INDENT D1 = ( ord ( max ( s [ i ] , s [ length - 1 - i ] ) ) - ord ( min ( s [ i ] , s [ length - 1 - i ] ) ) ) NEW_LINE D2 = 26 - D1 NEW_LINE result += min ( D1 , D2 ) NEW_LINE DEDENT return result NEW_LINE DEDENT s = " abccdb " NEW_LINE print ( minOperations ( s ) ) NEW_LINE
Check if all the pairs of an array are coprime with each other | Function to store and check the factors ; Check if factors are equal ; Check if the factor is already present ; Insert the factor in set ; Check if the factor is already present ; Insert the factors in set ; Function to check if all the pairs of array elements are coprime with each other ; Check if factors of A [ i ] haven 't occurred previously ; Driver code
def findFactor ( value , factors ) : NEW_LINE INDENT factors . add ( value ) NEW_LINE i = 2 NEW_LINE while ( i * i <= value ) : NEW_LINE if value % i == 0 : NEW_LINE INDENT if value // i == i : NEW_LINE if i in factors : NEW_LINE INDENT return bool ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT factors . add ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( i in factors ) or NEW_LINE ( ( value // i ) in factors ) : NEW_LINE return bool ( True ) NEW_LINE else : NEW_LINE factors . add ( i ) NEW_LINE factors . add ( value // i ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE return bool ( False ) NEW_LINE DEDENT def allCoprime ( A , n ) : NEW_LINE INDENT all_coprime = bool ( True ) NEW_LINE factors = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if A [ i ] == 1 : NEW_LINE INDENT continue NEW_LINE DEDENT if findFactor ( A [ i ] , factors ) : NEW_LINE all_coprime = bool ( False ) NEW_LINE break NEW_LINE DEDENT return bool ( all_coprime ) NEW_LINE DEDENT A = [ 3 , 5 , 11 , 7 , 19 ] NEW_LINE arr_size = len ( A ) NEW_LINE if ( allCoprime ( A , arr_size ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Count subsequences which contains both the maximum and minimum array element | Function to calculate the count of subsequences ; Find the maximum from the array ; Find the minimum from the array ; If array contains only one distinct element ; Find the count of maximum ; Find the count of minimum ; Finding the result with given condition ; Driver Code ; Function call
def countSubSequence ( arr , n ) : NEW_LINE INDENT maximum = max ( arr ) NEW_LINE minimum = min ( arr ) NEW_LINE if maximum == minimum : NEW_LINE INDENT return pow ( 2 , n ) - 1 NEW_LINE DEDENT i = arr . count ( maximum ) NEW_LINE j = arr . count ( minimum ) NEW_LINE res = ( pow ( 2 , i ) - 1 ) * ( pow ( 2 , j ) - 1 ) * pow ( 2 , n - i - j ) NEW_LINE return res NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countSubSequence ( arr , n ) ) NEW_LINE
Minimize elements to be added to a given array such that it contains another given array as its subsequence | Function that finds the minimum number of the element must be added to make A as a subsequence in B ; Base Case ; dp [ i ] [ j ] indicates the length of LCS of A of length i & B of length j ; If there are no elements either in A or B then the length of lcs is 0 ; If the element present at ith and jth index of A and B are equal then include in LCS ; If they are not equal then take the max ; Return difference of length of A and lcs of A and B ; Driver Code ; Given Sequence A and B ; Function Call
def transformSubsequence ( n , m , A , B ) : NEW_LINE INDENT if B is None or len ( B ) == 0 : NEW_LINE INDENT return n NEW_LINE DEDENT dp = [ [ 0 for col in range ( m + 1 ) ] for row in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( m + 1 ) : NEW_LINE INDENT if i == 0 or j == 0 : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE DEDENT elif A [ i - 1 ] == B [ j - 1 ] : NEW_LINE INDENT dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return n - dp [ n ] [ m ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE M = 6 NEW_LINE A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE B = [ 2 , 5 , 6 , 4 , 9 , 12 ] NEW_LINE print ( transformSubsequence ( N , M , A , B ) ) NEW_LINE DEDENT
Check if the sum of a subarray within a given range is a perfect square or not | Function to calculate the square root of the sum of a subarray in a given range ; Calculate the sum of array elements within a given range ; Finding the square root ; If a perfect square is found ; Reduce the search space if the value is greater than sum ; Reduce the search space if the value if smaller than sum ; Driver Code ; Given Array ; Given range ; Function call
def checkForPerfectSquare ( arr , i , j ) : NEW_LINE INDENT mid , sum = 0 , 0 NEW_LINE for m in range ( i , j + 1 ) : NEW_LINE INDENT sum += arr [ m ] NEW_LINE DEDENT low = 0 NEW_LINE high = sum // 2 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( mid * mid == sum ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( mid * mid > sum ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 19 , 33 , 48 , 90 , 100 ] NEW_LINE L = 1 NEW_LINE R = 3 NEW_LINE print ( checkForPerfectSquare ( arr , L , R ) ) NEW_LINE DEDENT
Count of subarrays forming an Arithmetic Progression ( AP ) | Function to find the total count of subarrays ; Iterate over each subarray ; Difference between first two terms of subarray ; Iterate over the subarray from i to j ; Check if the difference of all adjacent elements is same ; Driver Code ; Given array ; Function call
def calcSubarray ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT flag = True NEW_LINE comm_diff = A [ i + 1 ] - A [ i ] NEW_LINE for k in range ( i , j ) : NEW_LINE INDENT if ( A [ k + 1 ] - A [ k ] == comm_diff ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 8 , 7 , 4 , 1 , 0 ] NEW_LINE N = len ( A ) NEW_LINE print ( calcSubarray ( A , N ) ) NEW_LINE DEDENT
Largest Sum Contiguous Subarray having unique elements | Function to calculate required maximum subarray sum ; Initialize two pointers ; Stores the unique elements ; Insert the first element ; Current max sum ; Global maximum sum ; Update sum & increment j ; Add the current element ; Update sum and increment i and remove arr [ i ] from set ; Remove the element from start position ; Return the maximum sum ; Driver Code ; Given array arr [ ] ; Function call ; Print the maximum sum
def maxSumSubarray ( arr ) : NEW_LINE INDENT i = 0 NEW_LINE j = 1 NEW_LINE set = { } NEW_LINE set [ arr [ 0 ] ] = 1 NEW_LINE sum = arr [ 0 ] NEW_LINE maxsum = sum NEW_LINE while ( i < len ( arr ) - 1 and j < len ( arr ) ) : NEW_LINE INDENT if arr [ j ] not in set : NEW_LINE INDENT sum = sum + arr [ j ] NEW_LINE maxsum = max ( sum , maxsum ) NEW_LINE set [ arr [ j ] ] = 1 NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum -= arr [ i ] NEW_LINE del set [ arr [ i ] ] NEW_LINE i += 1 NEW_LINE DEDENT DEDENT return maxsum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 1 , 5 ] NEW_LINE ans = maxSumSubarray ( arr ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Count of numbers having only one unset bit in a range [ L , R ] | Python3 program for the above approach ; Function to count numbers in the range [ l , r ] having exactly one unset bit ; Stores the required count ; Iterate over the range ; Calculate number of bits ; Calculate number of set bits ; If count of unset bits is 1 ; Increment answer ; Return the answer ; Driver Code ; Function call
from math import log2 NEW_LINE def count_numbers ( L , R ) : NEW_LINE INDENT ans = 0 NEW_LINE for n in range ( L , R + 1 ) : NEW_LINE INDENT no_of_bits = int ( log2 ( n ) + 1 ) NEW_LINE no_of_set_bits = bin ( n ) . count ( '1' ) NEW_LINE if ( no_of_bits - no_of_set_bits == 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 4 NEW_LINE R = 9 NEW_LINE print ( count_numbers ( L , R ) ) NEW_LINE DEDENT
Count of numbers having only one unset bit in a range [ L , R ] | Python3 program for the above approach ; Function to count numbers in the range [ L , R ] having exactly one unset bit ; Stores the count elements having one zero in binary ; Stores the maximum number of bits needed to represent number ; Loop over for zero bit position ; Number having zero_bit as unset and remaining bits set ; Sets all bits before zero_bit ; Set the bit at position j ; Set the bit position at j ; If cur is in the range [ L , R ] , then increment ans ; Return ans ; Driver Code ; Function Call
import math NEW_LINE def count_numbers ( L , R ) : NEW_LINE INDENT ans = 0 ; NEW_LINE LogR = ( int ) ( math . log ( R ) + 1 ) ; NEW_LINE for zero_bit in range ( LogR ) : NEW_LINE INDENT cur = 0 ; NEW_LINE for j in range ( zero_bit ) : NEW_LINE INDENT cur |= ( 1 << j ) ; NEW_LINE DEDENT for j in range ( zero_bit + 1 , LogR ) : NEW_LINE INDENT cur |= ( 1 << j ) ; NEW_LINE if ( cur >= L and cur <= R ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 4 ; NEW_LINE R = 9 ; NEW_LINE print ( count_numbers ( L , R ) ) ; NEW_LINE DEDENT
Sum of all distances between occurrences of same characters in a given string | Function to calculate the sum of distances between occurrences of same characters in a string ; If similar characters are found ; Add the difference of their positions ; Return the answer ; Driver Code
def findSum ( s ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == s [ j ] ) : NEW_LINE INDENT sum += ( j - i ) NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT s = " ttt " NEW_LINE print ( findSum ( s ) ) NEW_LINE
Count array elements that can be represented as sum of at least two consecutive array elements | Function to find the number of array elements that can be represented as the sum of two or more consecutive array elements ; Stores the frequencies of array elements ; Stores required count ; Update frequency of each array element ; Find sum of all subarrays ; Increment ans by cnt [ sum ] ; Reset cnt [ sum ] by 0 ; Return ans ; Driver Code ; Given array ; Function call
def countElements ( a , n ) : NEW_LINE INDENT cnt = [ 0 ] * ( n + 1 ) NEW_LINE ans = 0 NEW_LINE for k in a : NEW_LINE INDENT cnt [ k ] += 1 NEW_LINE DEDENT for l in range ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for r in range ( l , n ) : NEW_LINE INDENT sum += a [ r ] NEW_LINE if ( l == r ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( sum <= n ) : NEW_LINE INDENT ans += cnt [ sum ] NEW_LINE cnt [ sum ] = 0 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 1 , 1 , 1 , 1 ] NEW_LINE print ( countElements ( a , len ( a ) ) ) NEW_LINE DEDENT
Maximum GCD among all pairs ( i , j ) of first N natural numbers | Python3 program for the above approach ; Function to find maximum gcd of all pairs possible from first n natural numbers ; Stores maximum gcd ; Iterate over all possible pairs ; Update maximum GCD ; Driver Code
def __gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT else : NEW_LINE INDENT return __gcd ( b , a % b ) ; NEW_LINE DEDENT DEDENT def maxGCD ( n ) : NEW_LINE INDENT maxHcf = - 2391734235435 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n + 1 ) : NEW_LINE INDENT maxHcf = max ( maxHcf , __gcd ( i , j ) ) ; NEW_LINE DEDENT DEDENT return maxHcf ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE print ( maxGCD ( n ) ) ; NEW_LINE DEDENT
Maximum GCD among all pairs ( i , j ) of first N natural numbers | Function to find the maximum GCD among all the pairs from first n natural numbers ; Return max GCD ; Driver code
def maxGCD ( n ) : NEW_LINE INDENT return ( n // 2 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE print ( maxGCD ( n ) ) ; NEW_LINE DEDENT
Remove odd indexed characters from a given string | Function to remove the odd indexed characters from a given string ; Stores the resultant string ; If the current index is odd ; Skip the character ; Otherwise , append the character ; Return the modified string ; Driver Code ; Remove the characters which have odd index
def removeOddIndexCharacters ( s ) : NEW_LINE INDENT new_s = " " NEW_LINE i = 0 NEW_LINE while i < len ( s ) : NEW_LINE INDENT if ( i % 2 == 1 ) : NEW_LINE INDENT i += 1 NEW_LINE continue NEW_LINE DEDENT new_s += s [ i ] NEW_LINE i += 1 NEW_LINE DEDENT return new_s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " abcdef " NEW_LINE str = removeOddIndexCharacters ( str ) NEW_LINE print ( str ) NEW_LINE DEDENT
Longest subarray forming a Geometic Progression ( GP ) | Function to return the length of the longest subarray forming a GP in a sorted array ; Base Case ; Stores the length of GP and the common ratio ; Stores the maximum length of the GP ; Traverse the array ; Check if the common ratio is valid for GP ; If the current common ratio is equal to previous common ratio ; Increment the length of the GP ; Store the max length of GP ; Otherwise ; Update the common ratio ; Update the length of GP ; Store the max length of GP ; Update the length of GP ; Store the max length of GP ; Return the max length of GP ; Given array ; Length of the array ; Function call
def longestGP ( A , N ) : NEW_LINE INDENT if ( N < 2 ) : NEW_LINE INDENT return N NEW_LINE DEDENT length = 1 NEW_LINE common_ratio = 1 NEW_LINE maxlength = 1 NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( A [ i + 1 ] % A [ i ] == 0 ) : NEW_LINE INDENT if ( A [ i + 1 ] // A [ i ] == common_ratio ) : NEW_LINE INDENT length = length + 1 NEW_LINE maxlength = max ( maxlength , length ) NEW_LINE DEDENT else : NEW_LINE INDENT common_ratio = A [ i + 1 ] // A [ i ] NEW_LINE length = 2 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT maxlength = max ( maxlength , length ) NEW_LINE length = 1 NEW_LINE DEDENT DEDENT maxlength = max ( maxlength , length ) NEW_LINE return maxlength NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 7 , 14 , 28 , 56 , 89 ] NEW_LINE N = len ( arr ) NEW_LINE print ( longestGP ( arr , N ) ) NEW_LINE
Maximum number of uncrossed lines between two given arrays | Function to count maximum number of uncrossed lines between the two given arrays ; Stores the length of lcs obtained upto every index ; Iterate over first array ; Iterate over second array ; Update value in dp table ; If both characters are equal ; Update the length of lcs ; If both characters are not equal ; Update the table ; Return the answer ; Driver Code ; Given array A [ ] and B [ ] ; Function Call
def uncrossedLines ( a , b , n , m ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( m + 1 ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE DEDENT elif ( a [ i - 1 ] == b [ j - 1 ] ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ n ] [ m ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 9 , 2 ] NEW_LINE B = [ 3 , 2 , 9 ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE print ( uncrossedLines ( A , B , N , M ) ) NEW_LINE DEDENT
Minimize flips required to make all shortest paths from top | Function to count the minimum number of flips required ; Dimensions of matrix ; Stores the count the flips ; Check if element is same or not ; Return the final count ; Given Matrix ; Given path as a string ; Function call
def minFlips ( mat , s ) : NEW_LINE INDENT N = len ( mat ) NEW_LINE M = len ( mat [ 0 ] ) NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( mat [ i ] [ j ] != ord ( s [ i + j ] ) - ord ( '0' ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT mat = [ [ 1 , 0 , 1 ] , [ 0 , 1 , 1 ] , [ 0 , 0 , 0 ] ] NEW_LINE s = "10001" NEW_LINE print ( minFlips ( mat , s ) ) NEW_LINE
Generate a pair of integers from a range [ L , R ] whose LCM also lies within the range | Python3 implementation of the above approach ; Checking if any pair is possible or not in range ( l , r ) ; If not possible print ( - 1 ) ; Print LCM pair ; Driver Code ; Function call
def lcmpair ( l , r ) : NEW_LINE INDENT x = l NEW_LINE y = 2 * l NEW_LINE if ( y > r ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " X ▁ = ▁ { } ▁ Y ▁ = ▁ { } " . format ( x , y ) ) NEW_LINE DEDENT DEDENT l = 13 NEW_LINE r = 69 NEW_LINE lcmpair ( l , r ) NEW_LINE
Print all possible shortest chains to reach a target word | Python Program to implement the above approach ; Function to print all possible shortest sequences starting from start to target . ; Find words differing by a single character with word ; Find next word in dict by changing each element from ' a ' to 'z ; Function to get all the shortest possible sequences starting from ' start ' to 'target ; Store all the shortest path . ; Store visited words in list ; Queue used to find the shortest path ; Stores the distinct words from given list ; Stores whether the shortest path is found or not ; Explore the next level ; Find words differing by a single character ; Add words to the path . ; Found the target ; If already reached target ; Erase all visited words . ; Driver Code
from collections import deque NEW_LINE from typing import Deque , List , Set NEW_LINE def displaypath ( res : List [ List [ str ] ] ) : NEW_LINE INDENT for i in res : NEW_LINE INDENT print ( " [ ▁ " , end = " " ) for j in i : print ( j , end = " , ▁ " ) print ( " ] " ) NEW_LINE DEDENT DEDENT def addWord ( word : str , Dict : Set ) : NEW_LINE INDENT res : List [ str ] = [ ] NEW_LINE wrd = list ( word ) NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( len ( wrd ) ) : NEW_LINE INDENT s = wrd [ i ] NEW_LINE c = ' a ' NEW_LINE while c <= ' z ' : NEW_LINE INDENT wrd [ i ] = c NEW_LINE if ' ' . join ( wrd ) in Dict : NEW_LINE INDENT res . append ( ' ' . join ( wrd ) ) NEW_LINE DEDENT c = chr ( ord ( c ) + 1 ) NEW_LINE DEDENT wrd [ i ] = s NEW_LINE DEDENT return res NEW_LINE DEDENT ' NEW_LINE def findLadders ( Dictt : List [ str ] , beginWord : str , endWord : str ) : NEW_LINE INDENT res : List [ List [ str ] ] = [ ] NEW_LINE visit = set ( ) NEW_LINE q : Deque [ List [ str ] ] = deque ( ) NEW_LINE Dict = set ( ) NEW_LINE for i in Dictt : NEW_LINE INDENT Dict . add ( i ) NEW_LINE DEDENT q . append ( [ beginWord ] ) NEW_LINE flag = False NEW_LINE while q : NEW_LINE INDENT size = len ( q ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT cur = q [ 0 ] NEW_LINE q . popleft ( ) NEW_LINE newadd = [ ] NEW_LINE newadd = addWord ( cur [ - 1 ] , Dict ) NEW_LINE for j in range ( len ( newadd ) ) : NEW_LINE INDENT newline = cur . copy ( ) NEW_LINE newline . append ( newadd [ j ] ) NEW_LINE if ( newadd [ j ] == endWord ) : NEW_LINE INDENT flag = True NEW_LINE res . append ( newline ) NEW_LINE DEDENT visit . add ( newadd [ j ] ) NEW_LINE q . append ( newline ) NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT break NEW_LINE DEDENT for it in visit : NEW_LINE INDENT Dict . remove ( it ) NEW_LINE DEDENT visit . clear ( ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = [ " ted " , " tex " , " red " , " tax " , " tad " , " den " , " rex " , " pee " ] NEW_LINE beginWord = " red " NEW_LINE endWord = " tax " NEW_LINE res = findLadders ( string , beginWord , endWord ) NEW_LINE displaypath ( res ) NEW_LINE DEDENT