codeparrot/xlcost-text-to-code huggingface dataset:
Delete all the even nodes of a Circular Linked List | Python3 program to delete all even node from a Circular singly linked list ; Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; If linked list is not None then set the next of last node ; For the first node ; Delete the node if it is even ; If node to be deleted is head node ; traverse list till not found delete node ; copy address of node ; Function to delete all even nodes from the singly circular linked list ; if node is even ; po to next node ; Function to pr nodes ; Driver code ; Initialize lists as empty ; Created linked list will be 57.11 .2 .56 .12 .61
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , data ) : NEW_LINE INDENT ptr1 = Node ( data ) NEW_LINE temp = head_ref NEW_LINE ptr1 . data = data NEW_LINE ptr1 . next = head_ref NEW_LINE if ( head_ref != None ) : NEW_LINE INDENT while ( temp . next != head_ref ) : NEW_LINE INDENT temp = temp . next NEW_LINE DEDENT temp . next = ptr1 NEW_LINE DEDENT else : NEW_LINE INDENT ptr1 . next = ptr1 NEW_LINE DEDENT head_ref = ptr1 NEW_LINE return head_ref NEW_LINE DEDENT def deleteNode ( head_ref , delete ) : NEW_LINE INDENT temp = head_ref NEW_LINE if ( head_ref == delete ) : NEW_LINE INDENT head_ref = delete . next NEW_LINE DEDENT while ( temp . next != delete ) : NEW_LINE INDENT temp = temp . next NEW_LINE DEDENT temp . next = delete . next NEW_LINE DEDENT def deleteEvenNodes ( head ) : NEW_LINE INDENT ptr = head NEW_LINE next = None NEW_LINE next = ptr . next NEW_LINE ptr = next NEW_LINE while ( ptr != head ) : NEW_LINE INDENT if ( ptr . data % 2 == 0 ) : NEW_LINE INDENT deleteNode ( head , ptr ) NEW_LINE DEDENT next = ptr . next NEW_LINE ptr = next NEW_LINE DEDENT return head NEW_LINE DEDENT def prList ( head ) : NEW_LINE INDENT temp = head NEW_LINE if ( head != None ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE while ( temp != head ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 61 ) NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 56 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 11 ) NEW_LINE head = push ( head , 57 ) NEW_LINE print ( " List ▁ after ▁ deletion ▁ : ▁ " , end = " " ) NEW_LINE head = deleteEvenNodes ( head ) NEW_LINE prList ( head ) NEW_LINE DEDENT
Sum of the nodes of a Circular Linked List | Python3 program to find the sum of all nodes of a Circular linked list ; class for a node ; Function to insert a node at the beginning of a Circular linked list ; If linked list is not NULL then set the next of last node ; For the first node ; Function to find sum of the given Circular linked list ; Driver code ; Initialize lists as empty ; Created linked list will be 11 -> 2 -> 56 -> 12
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head , data ) : NEW_LINE INDENT if not head : NEW_LINE INDENT head = Node ( data ) NEW_LINE head . next = head NEW_LINE return head NEW_LINE DEDENT lnode = head NEW_LINE while ( lnode and lnode . next is not head ) : NEW_LINE INDENT lnode = lnode . next NEW_LINE DEDENT ptr1 = Node ( data ) NEW_LINE ptr1 . next = head NEW_LINE lnode . next = ptr1 NEW_LINE head = ptr1 NEW_LINE return head NEW_LINE DEDENT def sumOfList ( head ) : NEW_LINE INDENT temp = head NEW_LINE tsum = temp . data NEW_LINE temp = temp . next NEW_LINE while ( temp is not head ) : NEW_LINE INDENT tsum += temp . data NEW_LINE temp = temp . next NEW_LINE DEDENT return tsum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 56 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 11 ) NEW_LINE print ( " Sum ▁ of ▁ circular ▁ list ▁ is ▁ = ▁ { } " . format ( sumOfList ( head ) ) ) NEW_LINE DEDENT
Convert an Array to a Circular Doubly Linked List | Node of the doubly linked list ; Utility function to create a node in memory ; Function to display the list ; Function to convert array into list ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Driver Code ; Array to be converted ; Start Pointer ; Create the List ; Display the list
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . prev = None NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getNode ( ) : NEW_LINE INDENT return ( Node ( 0 ) ) NEW_LINE DEDENT def displayList ( temp ) : NEW_LINE INDENT t = temp NEW_LINE if ( temp == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ list ▁ is : ▁ " , end = " ▁ " ) NEW_LINE while ( temp . next != t ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( temp . data ) NEW_LINE return 1 NEW_LINE DEDENT DEDENT def createList ( arr , n , start ) : NEW_LINE INDENT newNode = None NEW_LINE temp = None NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT newNode = getNode ( ) NEW_LINE newNode . data = arr [ i ] NEW_LINE if ( i == 0 ) : NEW_LINE INDENT start = newNode NEW_LINE newNode . prev = start NEW_LINE newNode . next = start NEW_LINE DEDENT else : NEW_LINE INDENT temp = ( start ) . prev NEW_LINE temp . next = newNode NEW_LINE newNode . next = start NEW_LINE newNode . prev = temp NEW_LINE temp = start NEW_LINE temp . prev = newNode NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return start NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE start = None NEW_LINE start = createList ( arr , n , start ) NEW_LINE displayList ( start ) NEW_LINE DEDENT
Lucky alive person in a circle | Code Solution to sword puzzle | Node structure ; Function to find the luckiest person ; Create a single node circular linked list . ; Starting from first soldier . ; condition for evaluating the existence of single soldier who is not killed . ; deleting soldier from the circular list who is killed in the fight . ; Returning the Luckiest soldier who remains alive . ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT node = Node ( data ) NEW_LINE return node NEW_LINE DEDENT def alivesol ( Num ) : NEW_LINE INDENT if ( Num == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT last = newNode ( 1 ) ; NEW_LINE last . next = last ; NEW_LINE for i in range ( 2 , Num + 1 ) : NEW_LINE INDENT temp = newNode ( i ) ; NEW_LINE temp . next = last . next ; NEW_LINE last . next = temp ; NEW_LINE last = temp ; NEW_LINE DEDENT curr = last . next ; NEW_LINE temp = None NEW_LINE while ( curr . next != curr ) : NEW_LINE INDENT temp = curr ; NEW_LINE curr = curr . next ; NEW_LINE temp . next = curr . next ; NEW_LINE del curr ; NEW_LINE temp = temp . next ; NEW_LINE curr = temp ; NEW_LINE DEDENT res = temp . data ; NEW_LINE del temp ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 100 ; NEW_LINE print ( alivesol ( N ) ) NEW_LINE DEDENT
Lowest Common Ancestor of the deepest leaves of a Binary Tree | Node of a Binary Tree ; Function to find the depth of the Binary Tree ; If root is not null ; Left recursive subtree ; Right recursive subtree ; Returns the maximum depth ; Function to perform the depth first search on the binary tree ; If root is null ; If curr is equal to depth ; Left recursive subtree ; Right recursive subtree ; If left and right are not null ; Return left , if left is not null Otherwise return right ; Function to find the LCA of the deepest nodes of the binary tree ; If root is null ; Stores the deepest depth of the binary tree ; Return the LCA of the nodes at level depth ; Driver Code ; Given Binary Tree
class Node : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def finddepth ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT left = finddepth ( root . left ) NEW_LINE right = finddepth ( root . right ) NEW_LINE return 1 + max ( left , right ) NEW_LINE DEDENT def dfs ( root , curr , depth ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( curr == depth ) : NEW_LINE INDENT return root NEW_LINE DEDENT left = dfs ( root . left , curr + 1 , depth ) NEW_LINE right = dfs ( root . right , curr + 1 , depth ) NEW_LINE if ( left != None and right != None ) : NEW_LINE INDENT return root NEW_LINE DEDENT return left if left else right NEW_LINE DEDENT def lcaOfDeepestLeaves ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return None NEW_LINE DEDENT depth = finddepth ( root ) - 1 NEW_LINE return dfs ( root , 0 , depth ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . right . left . left = Node ( 8 ) NEW_LINE root . right . left . right = Node ( 9 ) NEW_LINE print ( lcaOfDeepestLeaves ( root ) . data ) NEW_LINE DEDENT
Check if two nodes are on same path in a tree | Set 2 | Function to filter the return Values ; Utility function to check if nodes are on same path or not ; Condition to check if any vertex is equal to given two vertex or not ; Check if the current position has 1 ; Recursive call ; Return LCA ; Function to check if nodes lies on same path or not ; Driver code
def filter ( x , y , z ) : NEW_LINE INDENT if ( x != - 1 and y != - 1 ) : NEW_LINE INDENT return z NEW_LINE DEDENT return y if x == - 1 else x NEW_LINE DEDENT def samePathUtil ( mtrx , vrtx , v1 , v2 , i ) : NEW_LINE INDENT ans = - 1 NEW_LINE if ( i == v1 or i == v2 ) : NEW_LINE INDENT return i NEW_LINE DEDENT for j in range ( 0 , vrtx ) : NEW_LINE INDENT if ( mtrx [ i ] [ j ] == 1 ) : NEW_LINE INDENT ans = filter ( ans , samePathUtil ( mtrx , vrtx , v1 , v2 , j ) , i ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def isVertexAtSamePath ( mtrx , vrtx , v1 , v2 , i ) : NEW_LINE INDENT lca = samePathUtil ( mtrx , vrtx , v1 - 1 , v2 - 1 , i ) NEW_LINE if ( lca == v1 - 1 or lca == v2 - 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT vrtx = 7 NEW_LINE edge = 6 NEW_LINE mtrx = [ [ 0 , 1 , 1 , 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ] NEW_LINE v1 = 1 NEW_LINE v2 = 5 NEW_LINE if ( isVertexAtSamePath ( mtrx , vrtx , v1 , v2 , 0 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Sum of all odd nodes in the path connecting two given nodes | Binary Tree node ; Utitlity function to create a Binary Tree node ; Function to check if there is a path from root to the given node . It also populates arr ' with the given path ; if root is None there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr ; if it is the required node return True ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , remove current node ' s ▁ value ▁ from ▁ ▁ ' arr 'and then return False ; Function to get the sum of odd nodes in the path between any two nodes in a binary tree ; vector to store the path of first node n1 from root ; vector to store the path of second node n2 from root ; Get intersection point ; Keep moving forward until no intersection is found ; calculate sum of ODD nodes from the path ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT node = Node ( ) NEW_LINE node . data = data NEW_LINE node . left = None NEW_LINE node . right = None NEW_LINE return node NEW_LINE DEDENT def getPath ( root , arr , x ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT arr . append ( root . data ) NEW_LINE if ( root . data == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( getPath ( root . left , arr , x ) or getPath ( root . right , arr , x ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT arr . pop ( ) NEW_LINE return False NEW_LINE DEDENT def sumOddNodes ( root , n1 , n2 ) : NEW_LINE INDENT path1 = [ ] NEW_LINE path2 = [ ] NEW_LINE getPath ( root , path1 , n1 ) NEW_LINE getPath ( root , path2 , n2 ) NEW_LINE intersection = - 1 NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while ( i != len ( path1 ) or j != len ( path2 ) ) : NEW_LINE INDENT if ( i == j and path1 [ i ] == path2 [ j ] ) : NEW_LINE INDENT i = i + 1 NEW_LINE j = j + 1 NEW_LINE DEDENT else : NEW_LINE INDENT intersection = j - 1 NEW_LINE break NEW_LINE DEDENT DEDENT sum = 0 NEW_LINE i = len ( path1 ) - 1 NEW_LINE while ( i > intersection ) : NEW_LINE INDENT if ( path1 [ i ] % 2 != 0 ) : NEW_LINE INDENT sum += path1 [ i ] NEW_LINE DEDENT i = i - 1 NEW_LINE DEDENT i = intersection NEW_LINE while ( i < len ( path2 ) ) : NEW_LINE INDENT if ( path2 [ i ] % 2 != 0 ) : NEW_LINE INDENT sum += path2 [ i ] NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT 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 . left . right = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE node1 = 5 NEW_LINE node2 = 6 NEW_LINE print ( sumOddNodes ( root , node1 , node2 ) ) NEW_LINE
Lowest Common Ancestor in Parent Array Representation | Maximum value in a node ; Function to find the Lowest common ancestor ; Create a visited vector and mark all nodes as not visited . ; Moving from n1 node till root and mark every accessed node as visited ; Move to the parent of node n1 ; For second node finding the first node common ; Insert function for Binary tree ; Driver Code ; Maximum capacity of binary tree ; Root marked
MAX = 1000 NEW_LINE def findLCA ( n1 , n2 , parent ) : NEW_LINE INDENT visited = [ False for i in range ( MAX ) ] NEW_LINE visited [ n1 ] = True NEW_LINE while ( parent [ n1 ] != - 1 ) : NEW_LINE INDENT visited [ n1 ] = True NEW_LINE n1 = parent [ n1 ] NEW_LINE DEDENT visited [ n1 ] = True NEW_LINE while ( visited [ n2 ] == False ) : NEW_LINE INDENT n2 = parent [ n2 ] NEW_LINE DEDENT return n2 NEW_LINE DEDENT def insertAdj ( parent , i , j ) : NEW_LINE INDENT parent [ i ] = j NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT parent = [ 0 for i in range ( MAX ) ] NEW_LINE parent [ 20 ] = - 1 NEW_LINE insertAdj ( parent , 8 , 20 ) NEW_LINE insertAdj ( parent , 22 , 20 ) NEW_LINE insertAdj ( parent , 4 , 8 ) NEW_LINE insertAdj ( parent , 12 , 8 ) NEW_LINE insertAdj ( parent , 10 , 12 ) NEW_LINE insertAdj ( parent , 14 , 12 ) NEW_LINE print ( findLCA ( 10 , 14 , parent ) ) NEW_LINE DEDENT
LCA for general or n | Maximum number of nodes is 100000 and nodes are numbered from 1 to 100000 ; Storing root to node path ; Storing the path from root to node ; Pushing current node into the path ; Node found ; Terminating the path ; This Function compares the path from root to ' a ' & root to ' b ' and returns LCA of a and b . Time Complexity : O ( n ) ; Trivial case ; Setting root to be first element in path ; Calculating path from root to a ; Calculating path from root to b ; Runs till path 1 & path 2 mathches ; Returns the last matching node in the paths ; Driver code ; Number of nodes
MAXN = 100001 NEW_LINE tree = [ 0 ] * MAXN NEW_LINE for i in range ( MAXN ) : NEW_LINE INDENT tree [ i ] = [ ] NEW_LINE DEDENT path = [ 0 ] * 3 NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT path [ i ] = [ 0 ] * MAXN NEW_LINE DEDENT flag = False NEW_LINE def dfs ( cur : int , prev : int , pathNumber : int , ptr : int , node : int ) -> None : NEW_LINE INDENT global tree , path , flag NEW_LINE for i in range ( len ( tree [ cur ] ) ) : NEW_LINE INDENT if ( tree [ cur ] [ i ] != prev and not flag ) : NEW_LINE INDENT path [ pathNumber ] [ ptr ] = tree [ cur ] [ i ] NEW_LINE if ( tree [ cur ] [ i ] == node ) : NEW_LINE INDENT flag = True NEW_LINE path [ pathNumber ] [ ptr + 1 ] = - 1 NEW_LINE return NEW_LINE DEDENT dfs ( tree [ cur ] [ i ] , cur , pathNumber , ptr + 1 , node ) NEW_LINE DEDENT DEDENT DEDENT def LCA ( a : int , b : int ) -> int : NEW_LINE INDENT global flag NEW_LINE if ( a == b ) : NEW_LINE INDENT return a NEW_LINE DEDENT path [ 1 ] [ 0 ] = path [ 2 ] [ 0 ] = 1 NEW_LINE flag = False NEW_LINE dfs ( 1 , 0 , 1 , 1 , a ) NEW_LINE flag = False NEW_LINE dfs ( 1 , 0 , 2 , 1 , b ) NEW_LINE i = 0 NEW_LINE while ( path [ 1 ] [ i ] == path [ 2 ] [ i ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT return path [ 1 ] [ i - 1 ] NEW_LINE DEDENT def addEdge ( a : int , b : int ) -> None : NEW_LINE INDENT tree [ a ] . append ( b ) NEW_LINE tree [ b ] . append ( a ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 NEW_LINE addEdge ( 1 , 2 ) NEW_LINE addEdge ( 1 , 3 ) NEW_LINE addEdge ( 2 , 4 ) NEW_LINE addEdge ( 2 , 5 ) NEW_LINE addEdge ( 2 , 6 ) NEW_LINE addEdge ( 3 , 7 ) NEW_LINE addEdge ( 3 , 8 ) NEW_LINE print ( " LCA ( 4 , ▁ 7 ) ▁ = ▁ { } " . format ( LCA ( 4 , 7 ) ) ) NEW_LINE print ( " LCA ( 4 , ▁ 6 ) ▁ = ▁ { } " . format ( LCA ( 4 , 6 ) ) ) NEW_LINE DEDENT
Time Complexity and Space Complexity | Function to find a pair in the given array whose sum is equal to z ; Iterate through all the pairs ; Check if the sum of the pair ( a [ i ] , a [ j ] ) is equal to z ; Given Input ; Function Call
def findPair ( a , n , z ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( i != j and a [ i ] + a [ j ] == z ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT a = [ 1 , - 2 , 1 , 0 , 5 ] NEW_LINE z = 0 NEW_LINE n = len ( a ) NEW_LINE if ( findPair ( a , n , z ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT
Analysis of Algorithms | Big | Function to print all possible pairs ; Given array ; Store the size of the array ; Function Call
def printt ( a , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT print ( a [ i ] , " " , a [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT a = [ 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE printt ( a , n ) NEW_LINE
Maximum sum obtained by dividing Array into several subarrays as per given conditions | Function to find the required answer ; Stores maximum sum ; Adding the difference of elements at ends of increasing subarray to the answer ; Input ; Functio calling
def maximumSum ( arr , N ) : NEW_LINE INDENT Sum = 0 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT Sum += ( arr [ i ] - arr [ i - 1 ] ) NEW_LINE DEDENT DEDENT return Sum ; NEW_LINE DEDENT arr = [ 1 , 5 , 3 ] ; NEW_LINE N = len ( arr ) NEW_LINE print ( maximumSum ( arr , N ) ) ; NEW_LINE
The Slowest Sorting Algorithms | Function to implement stooge sort ; Base Case ; If first element is smaller than last element , swap them ; If there are more than 2 elements in the array ; Recursively sort the first 2 / 3 elements ; Recursively sort the last 2 / 3 elements ; Recursively sort the first 2 / 3 elements again ; Driver Code ; Function Call ; Display the sorted array
def stoogesort ( arr , l , h ) : NEW_LINE INDENT if ( l >= h ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( arr [ l ] > arr [ h ] ) : NEW_LINE INDENT temp = arr [ l ] NEW_LINE arr [ l ] = arr [ h ] NEW_LINE arr [ h ] = temp NEW_LINE DEDENT if ( h - l + 1 > 2 ) : NEW_LINE INDENT t = ( h - l + 1 ) // 3 NEW_LINE stoogesort ( arr , l , h - t ) NEW_LINE stoogesort ( arr , l + t , h ) NEW_LINE stoogesort ( arr , l , h - t ) NEW_LINE DEDENT DEDENT arr = [ 2 , 4 , 5 , 3 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE stoogesort ( arr , 0 , N - 1 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Determine winner of the Game by arranging balls in a row | Function to find the winner of the Game by arranging the balls in a row ; Check if small balls are greater or equal to the large ones ; X can place balls therefore scores n - 1 ; Condition if large balls are greater than small ; X can have m - 1 as a score since greater number of balls can only be adjacent ; Compare the score ; Driver Code ; Given number of small balls ( N ) and number of large balls ( M ) ; Function call
def findWinner ( n , m ) : NEW_LINE INDENT X = 0 ; NEW_LINE Y = 0 ; NEW_LINE if ( n >= m ) : NEW_LINE INDENT X = n - 1 ; NEW_LINE Y = m ; NEW_LINE DEDENT else : NEW_LINE INDENT X = m - 1 ; NEW_LINE Y = n ; NEW_LINE DEDENT if ( X > Y ) : NEW_LINE INDENT print ( " X " ) ; NEW_LINE DEDENT elif ( Y > X ) : NEW_LINE INDENT print ( " Y " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 ; NEW_LINE m = 1 ; NEW_LINE findWinner ( n , m ) ; NEW_LINE DEDENT
Check if Pascal 's Triangle is possible with a complete layer by using numbers upto N | Python3 program for the above approach ; Function to check if Pascaltriangle can be made by N integers ; Find X ; If x is integer ; Given number N ; Function call
import math NEW_LINE def checkPascaltriangle ( N ) : NEW_LINE INDENT x = ( math . sqrt ( 8 * N + 1 ) - 1 ) / 2 NEW_LINE if ( math . ceil ( x ) - x == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT N = 10 NEW_LINE checkPascaltriangle ( N ) NEW_LINE
Count subarrays having sum of elements at even and odd positions equal | Function to count subarrays in which sum of elements at even and odd positions are equal ; Initialize variables ; Iterate over the array ; Check if position is even then add to sum hen add it to sum ; else subtract it to sum ; Increment the count if the sum equals 0 ; Print the count of subarrays ; Driver Code ; Given array arr [ ] ; Size of the array ; Function call
def countSubarrays ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT if ( ( j - i ) % 2 == 0 ) : NEW_LINE INDENT sum += arr [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT sum -= arr [ j ] NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 6 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE countSubarrays ( arr , n ) NEW_LINE DEDENT
Find position of non | Function to find the count of placing non - attacking rooks on the N x N chessboard ; Count of the Non - attacking rooks ; Printing lexographically smallest configuration ; Driver Code ; Function call
def findCountRooks ( row , col , n , k ) : NEW_LINE INDENT res = n - k NEW_LINE print ( res ) NEW_LINE ri = 0 NEW_LINE ci = 0 NEW_LINE while ( res > 0 ) : NEW_LINE INDENT while ( ri < k and row [ ri ] == 1 ) : NEW_LINE INDENT ri += 1 NEW_LINE DEDENT while ( ci < k and col [ ci ] == 1 ) : NEW_LINE INDENT ci += 1 NEW_LINE DEDENT print ( ( ri + 1 ) , " " , ( ci + 1 ) ) NEW_LINE ri += 1 NEW_LINE ci += 1 NEW_LINE res -= 1 NEW_LINE DEDENT DEDENT n = 4 NEW_LINE k = 2 NEW_LINE row = [ 1 , 2 ] NEW_LINE col = [ 4 , 2 ] NEW_LINE findCountRooks ( row , col , n , k ) NEW_LINE