codeparrot/xlcost-text-to-code huggingface dataset:
Deepest right leaf node in a binary tree | Iterative approach | Helper function that allocates a new node with the given data and None left and right poers . ; Constructor to create a new node ; utility function to return level of given node ; create a queue for level order traversal ; traverse until the queue is empty ; Since we go level by level , the last stored right leaf node is deepest one ; Driver Code ; create a binary tree
class newnode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getDeepestRightLeafNode ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return None NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE result = None NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE if ( not temp . right . left and not temp . right . right ) : NEW_LINE INDENT result = temp . right NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newnode ( 1 ) NEW_LINE root . left = newnode ( 2 ) NEW_LINE root . right = newnode ( 3 ) NEW_LINE root . left . right = newnode ( 4 ) NEW_LINE root . right . left = newnode ( 5 ) NEW_LINE root . right . right = newnode ( 6 ) NEW_LINE root . right . left . right = newnode ( 7 ) NEW_LINE root . right . right . right = newnode ( 8 ) NEW_LINE root . right . left . right . left = newnode ( 9 ) NEW_LINE root . right . right . right . right = newnode ( 10 ) NEW_LINE result = getDeepestRightLeafNode ( root ) NEW_LINE if result : NEW_LINE INDENT print ( " Deepest ▁ Right ▁ Leaf ▁ Node ▁ : : " , result . data ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ result , ▁ right ▁ leaf ▁ not ▁ found " ) NEW_LINE DEDENT DEDENT
Check if two strings can be made equal by reversing a substring of one of the strings | Function to check if the strings can be made equal or not by reversing a substring of X ; Store the first index from the left which contains unequal characters in both the strings ; Store the first element from the right which contains unequal characters in both the strings ; Checks for the first index from left in which characters in both the strings are unequal ; Store the current index ; Break out of the loop ; Checks for the first index from right in which characters in both the strings are unequal ; Store the current index ; Break out of the loop ; Reverse the subString X [ L , R ] ; If X and Y are equal ; Otherwise ; Driver Code ; Function Call
def checkString ( X , Y ) : NEW_LINE INDENT L = - 1 NEW_LINE R = - 1 NEW_LINE for i in range ( len ( X ) ) : NEW_LINE INDENT if ( X [ i ] != Y [ i ] ) : NEW_LINE INDENT L = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( len ( X ) - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( X [ i ] != Y [ i ] ) : NEW_LINE INDENT R = i NEW_LINE break NEW_LINE DEDENT DEDENT X = list ( X ) NEW_LINE X = X [ : L ] + X [ R : L - 1 : - 1 ] + X [ R + 1 : ] NEW_LINE if ( X == list ( Y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = " adcbef " NEW_LINE Y = " abcdef " NEW_LINE checkString ( X , Y ) NEW_LINE DEDENT
Maximum length of same indexed subarrays from two given arrays satisfying the given condition | Function to find maximum length of subarray such that sum of maximum element in subarray in brr [ ] and sum of subarray in [ ] arr * K is at most C ; Base Case ; Let maximum length be 0 ; Perform Binary search ; Find mid value ; Check if the current mid satisfy the given condition ; If yes , then store length ; Otherwise ; Return maximum length stored ; Function to check if it is possible to have such a subarray of length K ; Finds the maximum element in each window of size k ; Check for window of size K ; For all possible subarrays of length k ; Until deque is empty ; Calculate the total cost and check if less than equal to c ; Find sum of current subarray and the total cost ; Include the new element of current subarray ; Discard the element of last subarray ; Remove all the elements in the old window ; Calculate total cost and check <= c ; If current subarray length satisfies ; If it is not possible ; Driver Code
def maxLength ( a , b , n , c ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT max_length = 0 NEW_LINE low = 0 NEW_LINE high = n NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = int ( low + ( high - low ) / 2 ) NEW_LINE if ( possible ( a , b , n , c , mid ) ) : NEW_LINE INDENT max_length = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return max_length NEW_LINE DEDENT def possible ( a , b , n , c , k ) : NEW_LINE INDENT dq = [ ] NEW_LINE Sum = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT Sum += a [ i ] NEW_LINE while ( len ( dq ) > 0 and b [ i ] > b [ dq [ len ( dq ) - 1 ] ] ) : NEW_LINE INDENT dq . pop ( len ( dq ) - 1 ) NEW_LINE DEDENT dq . append ( i ) NEW_LINE DEDENT total_cost = Sum * k + b [ dq [ 0 ] ] NEW_LINE if ( total_cost <= c ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( k , n ) : NEW_LINE INDENT Sum += a [ i ] NEW_LINE Sum -= a [ i - k ] NEW_LINE while ( len ( dq ) > 0 and dq [ 0 ] <= i - k ) : NEW_LINE INDENT dq . pop ( 0 ) NEW_LINE DEDENT while ( len ( dq ) > 0 and b [ i ] > b [ dq [ len ( dq ) - 1 ] ] ) : NEW_LINE INDENT dq . pop ( len ( dq ) - 1 ) NEW_LINE DEDENT dq . append ( i ) NEW_LINE total_cost = Sum * k + b [ dq [ 0 ] ] NEW_LINE if ( total_cost <= c ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT A = [ 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 ] NEW_LINE B = [ 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 ] NEW_LINE N = len ( A ) NEW_LINE C = 40 NEW_LINE print ( maxLength ( A , B , N , C ) ) NEW_LINE
Program to find the Nth natural number with exactly two bits set | Set 2 | Function to find the Nth number with exactly two bits set ; Initialize variables ; Initialize the range in which the value of ' a ' is present ; Perform Binary Search ; Find the mid value ; Update the range using the mid value t ; Find b value using a and N ; Print the value 2 ^ a + 2 ^ b ; Driver Code ; Function Call
def findNthNum ( N ) : NEW_LINE INDENT last_num = 0 NEW_LINE left = 1 NEW_LINE right = N NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = left + ( right - left ) // 2 NEW_LINE t = ( mid * ( mid + 1 ) ) // 2 NEW_LINE if ( t < N ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT elif ( t == N ) : NEW_LINE INDENT a = mid NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT a = mid NEW_LINE right = mid - 1 NEW_LINE DEDENT DEDENT t = a - 1 NEW_LINE b = N - ( t * ( t + 1 ) ) // 2 - 1 NEW_LINE print ( ( 1 << a ) + ( 1 << b ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 15 NEW_LINE findNthNum ( N ) NEW_LINE DEDENT
Longest subsequence having maximum sum | Function to find the longest subsequence from the given array with maximum sum ; Stores the largest element of the array ; If Max is less than 0 ; Print the largest element of the array ; Traverse the array ; If arr [ i ] is greater than or equal to 0 ; Print elements of the subsequence ; Driver code
def longestSubWithMaxSum ( arr , N ) : NEW_LINE INDENT Max = max ( arr ) NEW_LINE if ( Max < 0 ) : NEW_LINE INDENT print ( Max ) NEW_LINE return NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] >= 0 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 2 , - 4 , - 2 , 3 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE longestSubWithMaxSum ( arr , N ) NEW_LINE
Check if string S2 can be obtained by appending subsequences of string S1 | Python3 Program to implement the above approach ; Function for finding minimum number of operations ; Stores the length of strings ; Stores frequency of characters in string s ; Update frequencies of character in s ; Traverse string s1 ; If any character in s1 is not present in s ; Stores the indices of each character in s ; Traverse string s ; Store indices of characters ; Stores index of last appended character ; Traverse string s1 ; Find the index of next character that can be appended ; Check if the current character be included in the current subsequence ; Otherwise ; Start a new subsequence ; Update index of last character appended ; Driver Code ; If S2 cannot be obtained from subsequences of S1 ; Otherwise
from bisect import bisect , bisect_left , bisect_right NEW_LINE def findMinimumOperations ( s , s1 ) : NEW_LINE INDENT n = len ( s ) NEW_LINE m = len ( s1 ) NEW_LINE frequency = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT frequency [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT if ( frequency [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT indices = [ [ ] for i in range ( 26 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT indices [ ord ( s [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE DEDENT ans = 2 NEW_LINE last = len ( indices [ ord ( s1 [ 0 ] ) - ord ( ' a ' ) ] ) - 1 NEW_LINE for i in range ( 1 , m ) : NEW_LINE INDENT ch = ord ( s1 [ i ] ) - ord ( ' a ' ) NEW_LINE it = bisect_right ( indices [ ch ] , last ) NEW_LINE if ( it != len ( indices [ ch ] ) ) : NEW_LINE INDENT last = it NEW_LINE DEDENT else : NEW_LINE INDENT ans += 1 NEW_LINE last = len ( indices [ ch ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = " acebcd " NEW_LINE S2 = " acbcde " NEW_LINE ans = findMinimumOperations ( S1 , S2 ) NEW_LINE if ( ans == - 1 ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " YES " ) NEW_LINE print ( ans ) NEW_LINE DEDENT DEDENT
Sink Odd nodes in Binary Tree | Helper function to allocates a new node ; Helper function to check if node is leaf node ; A recursive method to sink a tree with odd root This method assumes that the subtrees are already sinked . This method is similar to Heapify of Heap - Sort ; If None or is a leaf , do nothing ; if left subtree exists and left child is even ; swap root 's data with left child   and fix left subtree  ; if right subtree exists and right child is even ; swap root 's data with right child   and fix right subtree  ; Function to sink all odd nodes to the bottom of binary tree . It does a postorder traversal and calls sink ( ) if any odd node is found ; If None or is a leaf , do nothing ; Process left and right subtrees before this node ; If root is odd , sink it ; Helper function to do Level Order Traversal of Binary Tree level by level . This function is used here only for showing modified tree . ; Do Level order traversal ; Print one level at a time ; Line separator for levels ; Constructed binary tree is 1 / \ 5 8 / \ / \ 2 4 9 10
class newnode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isLeaf ( root ) : NEW_LINE INDENT return ( root . left == None and root . right == None ) NEW_LINE DEDENT def sink ( root ) : NEW_LINE INDENT if ( root == None or isLeaf ( root ) ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . left and not ( root . left . data & 1 ) ) : NEW_LINE INDENT root . data , root . left . data = root . left . data , root . data NEW_LINE sink ( root . left ) NEW_LINE DEDENT elif ( root . right and not ( root . right . data & 1 ) ) : NEW_LINE INDENT root . data , root . right . data = root . right . data , root . data NEW_LINE sink ( root . right ) NEW_LINE DEDENT DEDENT def sinkOddNodes ( root ) : NEW_LINE INDENT if ( root == None or isLeaf ( root ) ) : NEW_LINE INDENT return NEW_LINE DEDENT sinkOddNodes ( root . left ) NEW_LINE sinkOddNodes ( root . right ) NEW_LINE if ( root . data & 1 ) : NEW_LINE INDENT sink ( root ) NEW_LINE DEDENT DEDENT def printLevelOrder ( root ) : NEW_LINE INDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT nodeCount = len ( q ) NEW_LINE while ( nodeCount ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE q . pop ( 0 ) NEW_LINE if ( node . left != None ) : NEW_LINE INDENT q . append ( node . left ) NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT q . append ( node . right ) NEW_LINE DEDENT nodeCount -= 1 NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT root = newnode ( 1 ) NEW_LINE root . left = newnode ( 5 ) NEW_LINE root . right = newnode ( 8 ) NEW_LINE root . left . left = newnode ( 2 ) NEW_LINE root . left . right = newnode ( 4 ) NEW_LINE root . right . left = newnode ( 9 ) NEW_LINE root . right . right = newnode ( 10 ) NEW_LINE sinkOddNodes ( root ) NEW_LINE print ( " Level ▁ order ▁ traversal ▁ of ▁ modified ▁ tree " ) NEW_LINE printLevelOrder ( root ) NEW_LINE
Print first K distinct Moran numbers from a given array |  ; Function to calculate the sum of digits of a number ; Stores the sum of digits ; Add the digit to sum ; Remove digit ; Returns the sum of digits ; Function to check if a number is prime or not ; If r has any divisor ; Set r as non - prime ; Function to check if a number is moran number ; Calculate sum of digits ; Check if n is divisible by the sum of digits ; Calculate the quotient ; If the quotient is prime ; Function to print the first K Moran numbers from the array ; Sort the given array ; Initialise a set ; Traverse the array from the end ; If the current array element is a Moran number ; Insert into the set ; Driver Code
import math NEW_LINE def digiSum ( a ) : NEW_LINE INDENT sums = 0 NEW_LINE while ( a != 0 ) : NEW_LINE INDENT sums += a % 10 NEW_LINE a = a // 10 NEW_LINE DEDENT return sums NEW_LINE DEDENT def isPrime ( r ) : NEW_LINE INDENT s = True NEW_LINE for i in range ( 2 , int ( math . sqrt ( r ) ) + 1 ) : NEW_LINE INDENT if ( r % i == 0 ) : NEW_LINE INDENT s = False NEW_LINE break NEW_LINE DEDENT DEDENT return s NEW_LINE DEDENT def isMorannumber ( n ) : NEW_LINE INDENT dup = n NEW_LINE sums = digiSum ( dup ) NEW_LINE if ( n % sums == 0 ) : NEW_LINE INDENT c = n // sums NEW_LINE if isPrime ( c ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def FirstKMorannumber ( a , n , k ) : NEW_LINE INDENT X = k NEW_LINE a . sort ( ) NEW_LINE s = set ( ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( k <= 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( isMorannumber ( a [ i ] ) ) : NEW_LINE INDENT s . add ( a [ i ] ) NEW_LINE k -= 1 NEW_LINE DEDENT DEDENT if ( k > 0 ) : NEW_LINE INDENT print ( X , end = ' ▁ Moran ▁ numbers ▁ are ▁ not ▁ ' ' present ▁ in ▁ the ▁ array ' ) NEW_LINE return NEW_LINE DEDENT lists = sorted ( s ) NEW_LINE for i in lists : NEW_LINE INDENT print ( i , end = ' , ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 34 , 198 , 21 , 42 , 63 , 45 , 22 , 44 , 43 ] NEW_LINE K = 4 NEW_LINE N = len ( A ) NEW_LINE FirstKMorannumber ( A , N , K ) NEW_LINE DEDENT
Number of pairs whose sum is a power of 2 | Set 2 | Python3 program to implement the above approach ; Function to count all pairs whose sum is a power of two ; Stores the frequency of each element of the array ; Update frequency of array elements ; Stores count of required pairs ; Current power of 2 ; Traverse the array ; If pair does not exist ; Increment count of pairs ; Return the count of pairs ; Driver Code
from math import pow NEW_LINE def countPair ( arr , n ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] = m . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 31 ) : NEW_LINE INDENT key = int ( pow ( 2 , i ) ) NEW_LINE for j in range ( n ) : NEW_LINE INDENT k = key - arr [ j ] NEW_LINE if k not in m : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT ans += m . get ( k , 0 ) NEW_LINE DEDENT if ( k == arr [ j ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 8 , 2 , 10 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPair ( arr , n ) ) NEW_LINE DEDENT
Counting Rock Samples | TCS Codevita 2020 | Function to find the rock samples in the ranges ; Iterate over the ranges ; Driver Code ; Function Call
def findRockSample ( ranges , n , r , arr ) : NEW_LINE INDENT a = [ ] NEW_LINE for i in range ( r ) : NEW_LINE INDENT c = 0 NEW_LINE l , h = ranges [ i ] [ 0 ] , ranges [ i ] [ 1 ] NEW_LINE for val in arr : NEW_LINE INDENT if l <= val <= h : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT a . append ( c ) NEW_LINE DEDENT return a NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE r = 2 NEW_LINE arr = [ 400 , 567 , 890 , 765 , 987 ] NEW_LINE ranges = [ [ 300 , 380 ] , [ 800 , 1000 ] ] NEW_LINE print ( * findRockSample ( ranges , n , r , arr ) ) NEW_LINE DEDENT
Depth of the deepest odd level node in Binary Tree | Python3 program to find depth of the deepest odd level node Helper function that allocates a new node with the given data and None left and right poers . ; Constructor to create a new node ; Utility function which returns whether the current node is a leaf or not ; function to return the longest odd level depth if it exists otherwise 0 ; Base case return from here ; increment current level ; if curr_level is odd and its a leaf node ; A wrapper over deepestOddLevelDepth ( ) ; Driver Code ; 10 / \ 28 13 / \ 14 15 / \ 23 24 Let us create Binary Tree shown in above example
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isleaf ( curr_node ) : NEW_LINE INDENT return ( curr_node . left == None and curr_node . right == None ) NEW_LINE DEDENT def deepestOddLevelDepthUtil ( curr_node , curr_level ) : NEW_LINE INDENT if ( curr_node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT curr_level += 1 NEW_LINE if ( curr_level % 2 != 0 and isleaf ( curr_node ) ) : NEW_LINE INDENT return curr_level NEW_LINE DEDENT return max ( deepestOddLevelDepthUtil ( curr_node . left , curr_level ) , deepestOddLevelDepthUtil ( curr_node . right , curr_level ) ) NEW_LINE DEDENT def deepestOddLevelDepth ( curr_node ) : NEW_LINE INDENT return deepestOddLevelDepthUtil ( curr_node , 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 28 ) NEW_LINE root . right = newNode ( 13 ) NEW_LINE root . right . left = newNode ( 14 ) NEW_LINE root . right . right = newNode ( 15 ) NEW_LINE root . right . right . left = newNode ( 23 ) NEW_LINE root . right . right . right = newNode ( 24 ) NEW_LINE print ( deepestOddLevelDepth ( root ) ) NEW_LINE DEDENT
N digit numbers having difference between the first and last digits as K | Function to store and check the difference of digits ; Base Case ; Last digit of the number to check the difference from the first digit ; Condition to avoid repeated values ; Update the string pt ; Recursive Call ; Update the string pt ; Recursive Call ; Any number can come in between first and last except the zero ; Recursive Call ; Function to place digit of the number ; When N is 1 and K > 0 , then the single number will be the first and last digit it cannot have difference greater than 0 ; This loop place the digit at the starting ; Recursive Call ; Vector to store results ; Generate all the resultant number ; Print the result ; Driver Code ; Function Call
result = [ ] NEW_LINE def findNumbers ( st , prev , n , K ) : NEW_LINE INDENT global result NEW_LINE if ( len ( st ) == n ) : NEW_LINE INDENT result . append ( int ( st ) ) NEW_LINE return NEW_LINE DEDENT if ( len ( st ) == n - 1 ) : NEW_LINE INDENT if ( prev - K >= 0 ) : NEW_LINE INDENT pt = " " NEW_LINE pt += prev - K + 48 NEW_LINE findNumbers ( st + pt , prev - K , n , K ) NEW_LINE DEDENT if ( K != 0 and prev + K < 10 ) : NEW_LINE INDENT pt = " " NEW_LINE pt += prev + K + 48 NEW_LINE findNumbers ( st + pt , prev + K , n , K ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for j in range ( 1 , 10 , 1 ) : NEW_LINE INDENT pt = " " NEW_LINE pt += j + 48 NEW_LINE findNumbers ( st + pt , prev , n , K ) NEW_LINE DEDENT DEDENT DEDENT def numDifference ( N , K ) : NEW_LINE INDENT global result NEW_LINE st = " " NEW_LINE if ( N == 1 and K == 0 ) : NEW_LINE INDENT result . append ( 0 ) NEW_LINE DEDENT elif ( N == 1 and K > 0 ) : NEW_LINE INDENT return result NEW_LINE DEDENT for i in range ( 1 , 10 , 1 ) : NEW_LINE INDENT temp = " " NEW_LINE temp += str ( 48 + i ) NEW_LINE findNumbers ( st + temp , i , N , K ) NEW_LINE st = " " NEW_LINE DEDENT return result NEW_LINE DEDENT def numDifferenceUtil ( N , K ) : NEW_LINE INDENT res = [ ] NEW_LINE res = numDifference ( N , K ) NEW_LINE for i in range ( 1 , len ( res ) ) : NEW_LINE INDENT print ( res [ i ] + 40 , end = " ▁ " ) NEW_LINE break NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE K = 9 NEW_LINE numDifferenceUtil ( N , K ) NEW_LINE DEDENT
Minimum Number of Bullets required to penetrate all bricks | Function to find the minimum number of bullets required to penetrate all bricks ; Sort the points in ascending order ; Check if there are no points ; Iterate through all the points ; Increase the count ; Return the count ; Driver Code ; Given coordinates of bricks ; Function call
def findMinBulletShots ( points ) : NEW_LINE INDENT for i in range ( len ( points ) ) : NEW_LINE INDENT points [ i ] = points [ i ] [ : : - 1 ] NEW_LINE DEDENT points = sorted ( points ) NEW_LINE for i in range ( len ( points ) ) : NEW_LINE INDENT points [ i ] = points [ i ] [ : : - 1 ] NEW_LINE DEDENT if ( len ( points ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT cnt = 1 NEW_LINE curr = points [ 0 ] [ 1 ] NEW_LINE for j in range ( 1 , len ( points ) ) : NEW_LINE INDENT if ( curr < points [ j ] [ 0 ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE curr = points [ j ] [ 1 ] NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT bricks = [ [ 5000 , 900000 ] , [ 1 , 100 ] , [ 150 , 499 ] ] NEW_LINE print ( findMinBulletShots ( bricks ) ) NEW_LINE DEDENT
Count array elements that can be maximized by adding any permutation of first N natural numbers | Function to get the count of values that can have the maximum value ; Sort array in decreasing order ; Stores the answer ; mark stores the maximum value till each index i ; Check if arr [ i ] can be maximum ; Update the mark ; Print the total count ; Driver Code ; Given array arr ; Function call
def countMaximum ( a , n ) : NEW_LINE INDENT a . sort ( reverse = True ) ; NEW_LINE count = 0 ; NEW_LINE mark = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( a [ i ] + n >= mark ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT mark = max ( mark , a [ i ] + i + 1 ) ; NEW_LINE DEDENT print ( count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 9 , 6 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE countMaximum ( arr , N ) ; NEW_LINE DEDENT
Kth character from the Nth string obtained by the given operations | Function to return Kth character from recursive string ; If N is 1 then return A ; Iterate a loop and generate the recursive string ; Update current string ; Change A to B and B to A ; Reverse the previous string ; Return the kth character ; Driver Code
def findKthChar ( n , k ) : NEW_LINE INDENT prev = " A " NEW_LINE cur = " " NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return ' A ' NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT cur = prev + " B " NEW_LINE temp1 = [ y for y in prev ] NEW_LINE for i in range ( len ( prev ) ) : NEW_LINE INDENT if ( temp1 [ i ] == ' A ' ) : NEW_LINE INDENT temp1 [ i ] = ' B ' NEW_LINE DEDENT else : NEW_LINE INDENT temp1 [ i ] = ' A ' NEW_LINE DEDENT DEDENT temp1 = temp1 [ : : - 1 ] NEW_LINE prev = " " . join ( temp1 ) NEW_LINE cur += prev NEW_LINE prev = cur NEW_LINE DEDENT return cur [ k - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE K = 3 NEW_LINE print ( findKthChar ( N , K ) ) NEW_LINE DEDENT