codeparrot/xlcost-text-to-code huggingface dataset:
Find the Deepest Node in a Binary Tree | A tree node with constructor ; Utility function to find height of a tree , rooted at ' root ' . ; levels : current Level Utility function to print all nodes at a given level . ; Driver Code ; Calculating height of tree ; Printing the deepest node
class new_Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def height ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT leftHt = height ( root . left ) NEW_LINE rightHt = height ( root . right ) NEW_LINE return max ( leftHt , rightHt ) + 1 NEW_LINE DEDENT def deepestNode ( root , levels ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( levels == 1 ) : NEW_LINE INDENT print ( root . data ) NEW_LINE DEDENT elif ( levels > 1 ) : NEW_LINE INDENT deepestNode ( root . left , levels - 1 ) NEW_LINE deepestNode ( root . right , levels - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = new_Node ( 1 ) NEW_LINE root . left = new_Node ( 2 ) NEW_LINE root . right = new_Node ( 3 ) NEW_LINE root . left . left = new_Node ( 4 ) NEW_LINE root . right . left = new_Node ( 5 ) NEW_LINE root . right . right = new_Node ( 6 ) NEW_LINE root . right . left . right = new_Node ( 7 ) NEW_LINE root . right . right . right = new_Node ( 8 ) NEW_LINE root . right . left . right . left = new_Node ( 9 ) NEW_LINE levels = height ( root ) NEW_LINE deepestNode ( root , levels ) NEW_LINE DEDENT
Check if an array is sorted and rotated using Binary Search | Function to return the index of the pivot ; Base cases ; Check if element at ( mid - 1 ) is pivot Consider the cases like { 4 , 5 , 1 , 2 , 3 } ; Decide whether we need to go to the left half or the right half ; Function to check if a given array is sorted rotated or not ; To check if the elements to the left of the pivot are in descending or not ; To check if the elements to the right of the pivot are in ascending or not ; If any of the above if or else is true Then the array is sorted rotated ; Else the array is not sorted rotated ; Driver code
def findPivot ( arr , low , high ) : NEW_LINE INDENT if ( high < low ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( high == low ) : NEW_LINE INDENT return low ; NEW_LINE DEDENT mid = ( low + high ) // 2 ; NEW_LINE if ( mid < high and arr [ mid + 1 ] < arr [ mid ] ) : NEW_LINE INDENT return mid ; NEW_LINE DEDENT if ( mid > low and arr [ mid ] < arr [ mid - 1 ] ) : NEW_LINE INDENT return mid - 1 ; NEW_LINE DEDENT if ( arr [ low ] > arr [ mid ] ) : NEW_LINE INDENT return findPivot ( arr , low , mid - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return findPivot ( arr , mid + 1 , high ) ; NEW_LINE DEDENT DEDENT def isRotated ( arr , n ) : NEW_LINE INDENT l = 0 ; NEW_LINE r = n - 1 ; NEW_LINE pivot = - 1 ; NEW_LINE if ( arr [ l ] > arr [ r ] ) : NEW_LINE INDENT pivot = findPivot ( arr , l , r ) ; NEW_LINE temp = pivot NEW_LINE if ( l < pivot ) : NEW_LINE INDENT while ( pivot > l ) : NEW_LINE INDENT if ( arr [ pivot ] < arr [ pivot - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT pivot -= 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT pivot = temp NEW_LINE pivot += 1 ; NEW_LINE while ( pivot < r ) : NEW_LINE INDENT if ( arr [ pivot ] > arr [ pivot + 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT pivot + + 1 ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 4 , 5 , 1 , 2 ] ; NEW_LINE if ( isRotated ( arr , 5 ) ) : NEW_LINE INDENT print ( " True " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) ; NEW_LINE DEDENT DEDENT
Find the Deepest Node in a Binary Tree | A Python3 program to find value of the deepest node in a given binary tree by method 3 ; A tree node with constructor ; constructor ; Funtion to return the deepest node ; Creating a Queue ; Iterates untill queue become empty ; Driver Code
from collections import deque NEW_LINE class new_Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def deepestNode ( root ) : NEW_LINE INDENT node = None NEW_LINE if root == None : NEW_LINE INDENT return 0 NEW_LINE DEDENT q = deque ( ) NEW_LINE q . append ( root ) NEW_LINE while len ( q ) != 0 : NEW_LINE INDENT node = q . popleft ( ) NEW_LINE if node . left is not None : NEW_LINE INDENT q . append ( node . left ) NEW_LINE DEDENT if node . right is not None : NEW_LINE INDENT q . append ( node . right ) NEW_LINE DEDENT DEDENT return node . data NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = new_Node ( 1 ) NEW_LINE root . left = new_Node ( 2 ) NEW_LINE root . right = new_Node ( 3 ) NEW_LINE root . left . left = new_Node ( 4 ) NEW_LINE root . right . left = new_Node ( 5 ) NEW_LINE root . right . right = new_Node ( 6 ) NEW_LINE root . right . left . right = new_Node ( 7 ) NEW_LINE root . right . right . right = new_Node ( 8 ) NEW_LINE root . right . left . right . left = new_Node ( 9 ) NEW_LINE levels = deepestNode ( root ) NEW_LINE print ( levels ) NEW_LINE DEDENT
Find a distinct pair ( x , y ) in given range such that x divides y | Function to return the possible pair ; ans1 , ans2 store value of x and y respectively ; Driver Code
def findpair ( l , r ) : NEW_LINE INDENT ans1 = l NEW_LINE ans2 = 2 * l NEW_LINE print ( ans1 , " , ▁ " , ans2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l , r = 1 , 10 NEW_LINE findpair ( l , r ) NEW_LINE DEDENT
Jump in rank of a student after updating marks | Function to print the name of student who stood first after updation in rank ; Array of students ; Store the name of the student ; Update the marks of the student ; Store the current rank of the student ; Print the name and jump in rank ; Names of the students ; Marks of the students ; Updates that are to be done ; Number of students
def nameRank ( names , marks , updates , n ) : NEW_LINE INDENT x = [ [ 0 for j in range ( 3 ) ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT x [ i ] [ 0 ] = names [ i ] NEW_LINE x [ i ] [ 1 ] = marks [ i ] + updates [ i ] NEW_LINE x [ i ] [ 2 ] = i + 1 NEW_LINE DEDENT highest = x [ 0 ] NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT if ( x [ j ] [ 1 ] >= highest [ 1 ] ) : NEW_LINE INDENT highest = x [ j ] NEW_LINE DEDENT DEDENT print ( " Name : ▁ " , highest [ 0 ] , " , ▁ Jump : ▁ " , abs ( highest [ 2 ] - 1 ) , sep = " " ) NEW_LINE DEDENT names = [ " sam " , " ram " , " geek " ] NEW_LINE marks = [ 80 , 79 , 75 ] NEW_LINE updates = [ 0 , 5 , - 9 ] NEW_LINE n = len ( marks ) NEW_LINE nameRank ( names , marks , updates , n ) NEW_LINE
Integers from the range that are composed of a single distinct digit | Function to return the count of digits of a number ; Function to return a number that contains only digit ' d ' repeated exactly count times ; Function to return the count of integers that are composed of a single distinct digit only ; Count of digits in L and R ; First digits of L and R ; If L has lesser number of digits than R ; If the number that starts with firstDigitL and has number of digits = countDigitsL is within the range include the number ; Exclude the number ; If the number that starts with firstDigitR and has number of digits = countDigitsR is within the range include the number ; Exclude the number ; If both L and R have equal number of digits ; Include the number greater than L upto the maximum number whose digit = coutDigitsL ; Exclude the numbers which are greater than R ; Return the count ; Driver code
def countDigits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE n //= 10 NEW_LINE DEDENT return count NEW_LINE DEDENT def getDistinct ( d , count ) : NEW_LINE INDENT num = 0 NEW_LINE count = pow ( 10 , count - 1 ) NEW_LINE while ( count > 0 ) : NEW_LINE INDENT num += ( count * d ) NEW_LINE count //= 10 NEW_LINE DEDENT return num NEW_LINE DEDENT def findCount ( L , R ) : NEW_LINE INDENT count = 0 NEW_LINE countDigitsL = countDigits ( L ) NEW_LINE countDigitsR = countDigits ( R ) NEW_LINE firstDigitL = ( L // pow ( 10 , countDigitsL - 1 ) ) NEW_LINE firstDigitR = ( R // pow ( 10 , countDigitsR - 1 ) ) NEW_LINE if ( countDigitsL < countDigitsR ) : NEW_LINE INDENT count += ( 9 * ( countDigitsR - countDigitsL - 1 ) ) NEW_LINE if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) : NEW_LINE INDENT count += ( 9 - firstDigitL + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT count += ( 9 - firstDigitL ) NEW_LINE DEDENT if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) : NEW_LINE INDENT count += firstDigitR NEW_LINE DEDENT else : NEW_LINE INDENT count += ( firstDigitR - 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( getDistinct ( firstDigitL , countDigitsL ) >= L ) : NEW_LINE INDENT count += ( 9 - firstDigitL + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT count += ( 9 - firstDigitL ) NEW_LINE DEDENT if ( getDistinct ( firstDigitR , countDigitsR ) <= R ) : NEW_LINE INDENT count -= ( 9 - firstDigitR ) NEW_LINE DEDENT else : NEW_LINE INDENT count -= ( 9 - firstDigitR + 1 ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT L = 10 NEW_LINE R = 50 NEW_LINE print ( findCount ( L , R ) ) NEW_LINE
Check if a pair with given absolute difference exists in a Matrix | Python 3 program to check for pairs with given difference exits in the matrix or not ; Function to check if a pair with given difference exist in the matrix ; Store elements in a hash ; Loop to iterate over the elements of the matrix ; Input matrix ; given difference
N = 4 NEW_LINE M = 4 NEW_LINE def isPairWithDiff ( mat , k ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if mat [ i ] [ j ] > k : NEW_LINE INDENT m = mat [ i ] [ j ] - k NEW_LINE if m in s : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT m = k - mat [ i ] [ j ] NEW_LINE if m in s : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT s . add ( mat [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT n , m = 4 , 4 NEW_LINE mat = [ [ 5 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 100 ] ] NEW_LINE k = 85 NEW_LINE if isPairWithDiff ( mat , k ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Next Smaller Element | prints element and NSE pair for all elements of arr [ ] of size n ; push the first element to stack ; iterate for rest of the elements ; if stack is not empty , then pop an element from stack . If the popped element is greater than next , then a ) print the pair b ) keep popping while elements are greater and stack is not empty ; push next to stack so that we can find next smaller for it ; After iterating over the loop , the remaining elements in stack do not have the next smaller element , so print - 1 for them ; Driver Code ;
def printNSE ( arr , n ) : NEW_LINE INDENT s = [ ] NEW_LINE mp = { } NEW_LINE s . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT s . append ( arr [ i ] ) NEW_LINE continue NEW_LINE DEDENT while ( len ( s ) != 0 and s [ - 1 ] > arr [ i ] ) : NEW_LINE INDENT mp [ s [ - 1 ] ] = arr [ i ] NEW_LINE s . pop ( ) NEW_LINE DEDENT s . append ( arr [ i ] ) NEW_LINE DEDENT while ( len ( s ) != 0 ) : NEW_LINE INDENT mp [ s [ - 1 ] ] = - 1 NEW_LINE s . pop ( ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , " - - - > " , mp [ arr [ i ] ] ) NEW_LINE DEDENT DEDENT arr = [ 11 , 13 , 21 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE printNSE ( arr , n ) NEW_LINE s NEW_LINE
Deepest left 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 deepest left leaf 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 getDeepestLeftLeafNode ( 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 if ( not temp . left . left and not temp . left . right ) : NEW_LINE INDENT result = temp . left NEW_LINE DEDENT DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE 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 . Left = 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 = getDeepestLeftLeafNode ( root ) NEW_LINE if result : NEW_LINE INDENT print ( " Deepest ▁ Left ▁ Leaf ▁ Node ▁ : : " , result . data ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ result , ▁ Left ▁ leaf ▁ not ▁ found " ) NEW_LINE DEDENT DEDENT
Find all possible binary trees with given Inorder Traversal | Node Structure ; Utility to create a new node ; A utility function to do preorder traversal of BST ; Function for constructing all possible trees with given inorder traversal stored in an array from arr [ start ] to arr [ end ] . This function returns a vector of trees . ; List to store all possible trees ; if start > end then subtree will be empty so returning NULL in the list ; Iterating through all values from start to end for constructing left and right subtree recursively ; Constructing left subtree ; Constructing right subtree ; Looping through all left and right subtrees and connecting to ith root below ; Making arr [ i ] as root ; Connecting left subtree ; Connecting right subtree ; Adding this tree to list ; Driver program to test above function
class Node : NEW_LINE INDENT def __init__ ( self , item ) : NEW_LINE INDENT self . key = item NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def preorder ( root ) : NEW_LINE INDENT if root is not None : NEW_LINE INDENT print root . key , NEW_LINE preorder ( root . left ) NEW_LINE preorder ( root . right ) NEW_LINE DEDENT DEDENT def getTrees ( arr , start , end ) : NEW_LINE INDENT trees = [ ] NEW_LINE if start > end : NEW_LINE INDENT trees . append ( None ) NEW_LINE return trees NEW_LINE DEDENT for i in range ( start , end + 1 ) : NEW_LINE INDENT ltrees = getTrees ( arr , start , i - 1 ) NEW_LINE rtrees = getTrees ( arr , i + 1 , end ) NEW_LINE for j in ltrees : NEW_LINE INDENT for k in rtrees : NEW_LINE INDENT node = Node ( arr [ i ] ) NEW_LINE node . left = j NEW_LINE node . right = k NEW_LINE trees . append ( node ) NEW_LINE DEDENT DEDENT DEDENT return trees NEW_LINE DEDENT inp = [ 4 , 5 , 7 ] NEW_LINE n = len ( inp ) NEW_LINE trees = getTrees ( inp , 0 , n - 1 ) NEW_LINE print " Preorder ▁ traversals ▁ of ▁ different ▁ possible\ STRNEWLINE ▁ Binary ▁ Trees ▁ are ▁ " NEW_LINE for i in trees : NEW_LINE INDENT preorder ( i ) ; NEW_LINE print " " NEW_LINE DEDENT
Check if a string is suffix of another | Python3 program to find if a string is suffix of another ; Test case - sensitive implementation of endsWith function
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " geeks " ; NEW_LINE s2 = " geeksforgeeks " ; NEW_LINE result = s2 . endswith ( s1 ) ; NEW_LINE if ( result ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Maximum elements that can be made equal with k updates | Function to calculate the maximum number of equal elements possible with atmost K increment of values . Here we have done sliding window to determine that whether there are x number of elements present which on increment will become equal . The loop here will run in fashion like 0. . . x - 1 , 1. . . x , 2. . . x + 1 , ... . , n - x - 1. . . n - 1 ; It can be explained with the reasoning that if for some x number of elements we can update the values then the increment to the segment ( i to j having length -> x ) so that all will be equal is ( x * maxx [ j ] ) this is the total sum of segment and ( pre [ j ] - pre [ i ] ) is present sum So difference of them should be less than k if yes , then that segment length ( x ) can be possible return true ; sort the array in ascending order ; Initializing the prefix array and maximum array ; Calculating prefix sum of the array ; Calculating max value upto that position in the array ; Binary search applied for computation here ; printing result ; Driver code
def ElementsCalculationFunc ( pre , maxx , x , k , n ) : NEW_LINE INDENT i = 0 NEW_LINE j = x NEW_LINE while j <= n : NEW_LINE INDENT if ( x * maxx [ j ] - ( pre [ j ] - pre [ i ] ) <= k ) : NEW_LINE INDENT return True NEW_LINE DEDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT def MaxNumberOfElements ( a , n , k ) : NEW_LINE INDENT a . sort ( ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT pre [ i ] = 0 NEW_LINE maxx [ i ] = 0 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + a [ i - 1 ] NEW_LINE maxx [ i ] = max ( maxx [ i - 1 ] , a [ i - 1 ] ) NEW_LINE DEDENT l = 1 NEW_LINE r = n NEW_LINE while ( l < r ) : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if ( ElementsCalculationFunc ( pre , maxx , mid - 1 , k , n ) ) : NEW_LINE INDENT ans = mid NEW_LINE l = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE MaxNumberOfElements ( arr , n , k ) NEW_LINE DEDENT
Print number in ascending order which contains 1 , 2 and 3 in their digits . | convert all numbers to strings ; check if each number in the list has 1 , 2 and 3 ; sort all the numbers ; Driver Code
def printNumbers ( numbers ) : NEW_LINE INDENT numbers = map ( str , numbers ) NEW_LINE result = [ ] NEW_LINE for num in numbers : NEW_LINE INDENT if ( '1' in num and '2' in num and '3' in num ) : NEW_LINE INDENT result . append ( num ) NEW_LINE DEDENT DEDENT if not result : NEW_LINE INDENT result = [ ' - 1' ] NEW_LINE DEDENT return sorted ( result ) ; NEW_LINE DEDENT numbers = [ 123 , 1232 , 456 , 234 , 32145 ] NEW_LINE result = printNumbers ( numbers ) NEW_LINE print ' , ▁ ' . join ( num for num in result ) NEW_LINE
Find k | Structure to store the start and end point ; Function to find Kth smallest number in a vector of merged intervals ; Traverse merged [ ] to find Kth smallest element using Linear search . ; To combined both type of ranges , overlapping as well as non - overlapping . ; Sorting intervals according to start time ; Merging all intervals into merged ; To check if starting point of next range is lying between the previous range and ending point of next range is greater than the Ending point of previous range then update ending point of previous range by ending point of next range . ; If starting point of next range is greater than the ending point of previous range then store next range in merged [ ] . ; Driver Code ; Merge all intervals into merged [ ] ; Processing all queries on merged intervals
class Interval : NEW_LINE INDENT def __init__ ( self , s , e ) : NEW_LINE INDENT self . s = s NEW_LINE self . e = e NEW_LINE DEDENT DEDENT def kthSmallestNum ( merged : list , k : int ) -> int : NEW_LINE INDENT n = len ( merged ) NEW_LINE for j in range ( n ) : NEW_LINE INDENT if k <= abs ( merged [ j ] . e - merged [ j ] . s + 1 ) : NEW_LINE INDENT return merged [ j ] . s + k - 1 NEW_LINE DEDENT k = k - abs ( merged [ j ] . e - merged [ j ] . s + 1 ) NEW_LINE DEDENT if k : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT def mergeIntervals ( merged : list , arr : list , n : int ) : NEW_LINE INDENT arr . sort ( key = lambda a : a . s ) NEW_LINE merged . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prev = merged [ - 1 ] NEW_LINE curr = arr [ i ] NEW_LINE if curr . s >= prev . s and curr . s <= prev . e and curr . e > prev . e : NEW_LINE INDENT merged [ - 1 ] . e = curr . e NEW_LINE DEDENT else : NEW_LINE INDENT if curr . s > prev . e : NEW_LINE INDENT merged . append ( curr ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ Interval ( 2 , 6 ) , Interval ( 4 , 7 ) ] NEW_LINE n = len ( arr ) NEW_LINE query = [ 5 , 8 ] NEW_LINE q = len ( query ) NEW_LINE merged = [ ] NEW_LINE mergeIntervals ( merged , arr , n ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( kthSmallestNum ( merged , query [ i ] ) ) NEW_LINE DEDENT DEDENT
Grouping Countries | Python3 program to count no of distinct countries from a given group of people ; Answer is valid if adjacent sitting num people give same answer ; someone gives different answer ; check next person ; one valid country group has been found ; Driven code
def countCountries ( ans , N ) : NEW_LINE INDENT total_countries = 0 NEW_LINE i = 0 NEW_LINE invalid = 0 NEW_LINE while ( i < N ) : NEW_LINE INDENT curr_size = ans [ i ] NEW_LINE num = ans [ i ] NEW_LINE while ( num > 0 ) : NEW_LINE INDENT if ( ans [ i ] != curr_size ) : NEW_LINE INDENT print ( " Invalid ▁ Answer " ) NEW_LINE return ; NEW_LINE DEDENT else : NEW_LINE INDENT num = num - 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT total_countries = total_countries + 1 ; NEW_LINE DEDENT print ( " There ▁ are ▁ " , total_countries , " ▁ distinct ▁ companies ▁ in ▁ the ▁ group . " ) NEW_LINE DEDENT ans = [ 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 ] ; NEW_LINE n = len ( ans ) ; NEW_LINE countCountries ( ans , n ) ; NEW_LINE