codeparrot/xlcost-text-to-code huggingface dataset:
Create loops of even and odd values in a binary tree | Utility function to create a new node ; preorder traversal to place the node pointer in the respective even_ptrs or odd_ptrs list ; place node ptr in even_ptrs list if node contains even number ; else place node ptr in odd_ptrs list ; function to create the even and odd loops ; forming even loop ; for the last element ; Similarly forming odd loop ; traversing the loop from any random node in the loop ; Driver program to test above ; Binary tree formation ; traversing odd loop from any random odd node ; traversing even loop from any random even node
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE self . abtr = None NEW_LINE DEDENT DEDENT even_ptrs = [ ] NEW_LINE odd_ptrs = [ ] NEW_LINE def preorderTraversal ( root ) : NEW_LINE INDENT global even_ptrs , odd_ptrs NEW_LINE if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . data % 2 == 0 ) : NEW_LINE INDENT even_ptrs . append ( root ) NEW_LINE DEDENT else : NEW_LINE INDENT odd_ptrs . append ( root ) NEW_LINE DEDENT preorderTraversal ( root . left ) NEW_LINE preorderTraversal ( root . right ) NEW_LINE DEDENT def createLoops ( root ) : NEW_LINE INDENT preorderTraversal ( root ) NEW_LINE i = 1 NEW_LINE while i < len ( even_ptrs ) : NEW_LINE INDENT even_ptrs [ i - 1 ] . abtr = even_ptrs [ i ] NEW_LINE i += 1 NEW_LINE DEDENT even_ptrs [ i - 1 ] . abtr = even_ptrs [ 0 ] NEW_LINE i = 1 NEW_LINE while i < len ( odd_ptrs ) : NEW_LINE INDENT odd_ptrs [ i - 1 ] . abtr = odd_ptrs [ i ] NEW_LINE i += 1 NEW_LINE DEDENT odd_ptrs [ i - 1 ] . abtr = odd_ptrs [ 0 ] NEW_LINE DEDENT def traverseLoop ( start ) : NEW_LINE INDENT curr = start NEW_LINE while True and curr : NEW_LINE INDENT print ( curr . data , end = " ▁ " ) NEW_LINE curr = curr . abtr NEW_LINE if curr == start : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE 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 createLoops ( root ) NEW_LINE print ( " Odd ▁ nodes : " , end = " ▁ " ) NEW_LINE traverseLoop ( root . right ) NEW_LINE print ( " Even ▁ nodes : " , end = " ▁ " ) NEW_LINE traverseLoop ( root . left ) NEW_LINE DEDENT
Area of circle which is inscribed in equilateral triangle | Python3 program to find the area of circle which is inscribed in equilateral triangle ; Function return the area of circle inscribed in equilateral triangle ; Driver code
from math import pi NEW_LINE def circle_inscribed ( a ) : NEW_LINE INDENT return pi * ( a * a ) / 12 NEW_LINE DEDENT a = 4 NEW_LINE print ( circle_inscribed ( a ) ) NEW_LINE
Program to find the Volume of an irregular tetrahedron | Python 3 implementation of above approach ; Function to find the volume ; Steps to calculate volume of a Tetrahedron using formula ; Driver code ; edge lengths
from math import * NEW_LINE def findVolume ( u , v , w , U , V , W , b ) : NEW_LINE INDENT uPow = pow ( u , 2 ) NEW_LINE vPow = pow ( v , 2 ) NEW_LINE wPow = pow ( w , 2 ) NEW_LINE UPow = pow ( U , 2 ) NEW_LINE VPow = pow ( V , 2 ) NEW_LINE WPow = pow ( W , 2 ) NEW_LINE a = ( 4 * ( uPow * vPow * wPow ) - uPow * pow ( ( vPow + wPow - UPow ) , 2 ) - vPow * pow ( ( wPow + uPow - VPow ) , 2 ) - wPow * pow ( ( uPow + vPow - WPow ) , 2 ) + ( vPow + wPow - UPow ) * ( wPow + uPow - VPow ) * ( uPow + vPow - WPow ) ) NEW_LINE vol = sqrt ( a ) NEW_LINE vol /= b NEW_LINE print ( round ( vol , 4 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT u , v , w = 1000 , 1000 , 1000 NEW_LINE U , V , W = 3 , 4 , 5 NEW_LINE b = 12 NEW_LINE findVolume ( u , v , w , U , V , W , b ) NEW_LINE DEDENT
Check if it is possible to create a polygon with a given angle | Function to check whether it is possible to make a regular polygon with a given angle . ; N denotes the number of sides of polygons possible ; Driver Code ; function calling
def makePolygon ( a ) : NEW_LINE INDENT n = 360 / ( 180 - a ) NEW_LINE if n == int ( n ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 90 NEW_LINE makePolygon ( a ) NEW_LINE DEDENT
Finding Quadrant of a Coordinate with respect to a Circle | Python3 Program to find the quadrant of a given coordinate w . rt . the centre of a circle ; Thus function returns the quadrant number ; Coincides with center ; Outside circle ; 1 st quadrant ; 2 nd quadrant ; 3 rd quadrant ; 4 th quadrant ; Coordinates of centre ; Radius of circle ; Coordinates of the given po
import math NEW_LINE def getQuadrant ( X , Y , R , PX , PY ) : NEW_LINE INDENT if ( PX == X and PY == Y ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT val = ( math . pow ( ( PX - X ) , 2 ) + math . pow ( ( PY - Y ) , 2 ) ) ; NEW_LINE if ( val > pow ( R , 2 ) ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( PX > X and PY >= Y ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( PX <= X and PY > Y ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT if ( PX < X and PY <= Y ) : NEW_LINE INDENT return 3 ; NEW_LINE DEDENT if ( PX >= X and PY < Y ) : NEW_LINE INDENT return 4 ; NEW_LINE DEDENT DEDENT X = 0 ; NEW_LINE Y = 3 ; NEW_LINE R = 2 ; NEW_LINE PX = 1 ; NEW_LINE PY = 4 ; NEW_LINE ans = getQuadrant ( X , Y , R , PX , PY ) ; NEW_LINE if ( ans == - 1 ) : print ( " Lies ▁ Outside ▁ the ▁ circle " ) ; NEW_LINE elif ( ans == 0 ) : print ( " Coincides ▁ with ▁ centre " ) ; NEW_LINE else : print ( ans , " Quadrant " ) ; NEW_LINE
Hexadecagonal number | Function to calculate hexadecagonal number ; Driver Code
def hexadecagonalNum ( n ) : NEW_LINE INDENT return ( ( 14 * n * n ) - 12 * n ) // 2 NEW_LINE DEDENT n = 5 NEW_LINE print ( " % sth ▁ Hexadecagonal ▁ number ▁ : ▁ " % n , hexadecagonalNum ( n ) ) NEW_LINE n = 9 NEW_LINE print ( " % sth ▁ Hexadecagonal ▁ number ▁ : ▁ " % n , hexadecagonalNum ( n ) ) NEW_LINE
Find first non matching leaves in two binary trees | Utility function to create a new tree Node ; Prints the first non - matching leaf node in two trees if it exists , else prints nothing . ; If any of the tree is empty ; Create two stacks for preorder traversals ; If traversal of one tree is over and other tree still has nodes . ; Do iterative traversal of first tree and find first lead node in it as " temp1" ; pushing right childfirst so that left child comes first while popping . ; Do iterative traversal of second tree and find first lead node in it as " temp2" ; If we found leaves in both trees ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def isLeaf ( t ) : NEW_LINE INDENT return ( ( t . left == None ) and ( t . right == None ) ) NEW_LINE DEDENT def findFirstUnmatch ( root1 , root2 ) : NEW_LINE INDENT if ( root1 == None or root2 == None ) : NEW_LINE INDENT return NEW_LINE DEDENT s1 = [ ] NEW_LINE s2 = [ ] NEW_LINE s1 . insert ( 0 , root1 ) NEW_LINE s2 . insert ( 0 , root2 ) NEW_LINE while ( len ( s1 ) or len ( s2 ) ) : NEW_LINE INDENT if ( len ( s1 ) == 0 or len ( s2 ) == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT temp1 = s1 [ 0 ] NEW_LINE s1 . pop ( 0 ) NEW_LINE while ( temp1 and not isLeaf ( temp1 ) ) : NEW_LINE INDENT s1 . insert ( 0 , temp1 . right ) NEW_LINE s1 . insert ( 0 , temp1 . left ) NEW_LINE temp1 = s1 [ 0 ] NEW_LINE s1 . pop ( 0 ) NEW_LINE DEDENT temp2 = s2 [ 0 ] NEW_LINE s2 . pop ( 0 ) NEW_LINE while ( temp2 and not isLeaf ( temp2 ) ) : NEW_LINE INDENT s2 . insert ( 0 , temp2 . right ) NEW_LINE s2 . insert ( 0 , temp2 . left ) NEW_LINE temp2 = s2 [ 0 ] NEW_LINE s2 . pop ( 0 ) NEW_LINE DEDENT if ( temp1 != None and temp2 != None ) : NEW_LINE INDENT if ( temp1 . data != temp2 . data ) : NEW_LINE INDENT print ( " First ▁ non ▁ matching ▁ leaves ▁ : " , temp1 . data , " " , temp2 . data ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = newNode ( 5 ) NEW_LINE root1 . left = newNode ( 2 ) NEW_LINE root1 . right = newNode ( 7 ) NEW_LINE root1 . left . left = newNode ( 10 ) NEW_LINE root1 . left . right = newNode ( 11 ) NEW_LINE root2 = newNode ( 6 ) NEW_LINE root2 . left = newNode ( 10 ) NEW_LINE root2 . right = newNode ( 15 ) NEW_LINE findFirstUnmatch ( root1 , root2 ) NEW_LINE DEDENT
Maximum points of intersection n circles | Returns maximum number of intersections ; Driver code
def intersection ( n ) : NEW_LINE INDENT return n * ( n - 1 ) ; NEW_LINE DEDENT print ( intersection ( 3 ) ) NEW_LINE
Find the perimeter of a cylinder | Function to calculate the perimeter of a cylinder ; Driver function
def perimeter ( diameter , height ) : NEW_LINE INDENT return 2 * ( diameter + height ) NEW_LINE DEDENT diameter = 5 ; NEW_LINE height = 10 ; NEW_LINE print ( " Perimeter ▁ = ▁ " , perimeter ( diameter , height ) ) NEW_LINE
Find all possible coordinates of parallelogram | coordinates of A ; coordinates of B ; coordinates of C
ay = 0 NEW_LINE ax = 5 NEW_LINE by = 1 NEW_LINE bx = 1 NEW_LINE cy = 5 NEW_LINE cx = 2 NEW_LINE print ( ax + bx - cx , " , ▁ " , ay + by - cy ) NEW_LINE print ( ax + cx - bx , " , ▁ " , ay + cy - by ) NEW_LINE print ( cx + bx - ax , " , ▁ " , cy + by - ax ) NEW_LINE
Check whether a given point lies inside a rectangle or not | A utility function to calculate area of triangle formed by ( x1 , y1 ) , ( x2 , y2 ) and ( x3 , y3 ) ; A function to check whether point P ( x , y ) lies inside the rectangle formed by A ( x1 , y1 ) , B ( x2 , y2 ) , C ( x3 , y3 ) and D ( x4 , y4 ) ; Calculate area of rectangle ABCD ; Calculate area of triangle PAB ; Calculate area of triangle PBC ; Calculate area of triangle PCD ; Calculate area of triangle PAD ; Check if sum of A1 , A2 , A3 and A4 is same as A ; Driver Code ; Let us check whether the point P ( 10 , 15 ) lies inside the rectangle formed by A ( 0 , 10 ) , B ( 10 , 0 ) C ( 0 , - 10 ) D ( - 10 , 0 )
def area ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT return abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) NEW_LINE DEDENT def check ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 , x , y ) : NEW_LINE INDENT A = ( area ( x1 , y1 , x2 , y2 , x3 , y3 ) + area ( x1 , y1 , x4 , y4 , x3 , y3 ) ) NEW_LINE A1 = area ( x , y , x1 , y1 , x2 , y2 ) NEW_LINE A2 = area ( x , y , x2 , y2 , x3 , y3 ) NEW_LINE A3 = area ( x , y , x3 , y3 , x4 , y4 ) NEW_LINE A4 = area ( x , y , x1 , y1 , x4 , y4 ) ; NEW_LINE return ( A == A1 + A2 + A3 + A4 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT if ( check ( 0 , 10 , 10 , 0 , 0 , - 10 , - 10 , 0 , 10 , 15 ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT DEDENT
Get maximum left node in binary tree | Get max of left element using Inorder traversal ; Return maximum of three values 1 ) Recursive max in left subtree 2 ) Value in left node 3 ) Recursive max in right subtree ; Utility class to create a new tree node ; Driver Code ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1
def maxOfLeftElement ( root ) : NEW_LINE INDENT res = - 999999999999 NEW_LINE if ( root == None ) : NEW_LINE INDENT return res NEW_LINE DEDENT if ( root . left != None ) : NEW_LINE INDENT res = root . left . data NEW_LINE DEDENT return max ( { maxOfLeftElement ( root . left ) , res , maxOfLeftElement ( root . right ) } ) NEW_LINE DEDENT class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 7 ) NEW_LINE root . left = newNode ( 6 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 2 ) NEW_LINE root . right . right = newNode ( 1 ) NEW_LINE print ( maxOfLeftElement ( root ) ) NEW_LINE DEDENT
Pizza cut problem ( Or Circle Division by Lines ) | Function for finding maximum pieces with n cuts . ; Driver code
def findMaximumPieces ( n ) : NEW_LINE INDENT return int ( 1 + n * ( n + 1 ) / 2 ) NEW_LINE DEDENT print ( findMaximumPieces ( 3 ) ) NEW_LINE
Optimum location of point to minimize total distance | A Python3 program to find optimum location and total cost ; Class defining a point ; Class defining a line of ax + by + c = 0 form ; Method to get distance of point ( x , y ) from point p ; Utility method to compute total distance all points when choose point on given line has x - coordinate value as X ; Calculating Y of chosen point by line equation ; Utility method to find minimum total distance ; Loop until difference between low and high become less than EPS ; mid1 and mid2 are representative x co - ordiantes of search space ; If mid2 point gives more total distance , skip third part ; If mid1 point gives more total distance , skip first part ; Compute optimum distance cost by sending average of low and high as X ; Method to find optimum cost ; Converting 2D array input to point array ; Driver Code
import math NEW_LINE class Optimum_distance : NEW_LINE INDENT class Point : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT class Line : NEW_LINE INDENT def __init__ ( self , a , b , c ) : NEW_LINE INDENT self . a = a NEW_LINE self . b = b NEW_LINE self . c = c NEW_LINE DEDENT DEDENT def dist ( self , x , y , p ) : NEW_LINE INDENT return math . sqrt ( ( x - p . x ) ** 2 + ( y - p . y ) ** 2 ) NEW_LINE DEDENT def compute ( self , p , n , l , x ) : NEW_LINE INDENT res = 0 NEW_LINE y = - 1 * ( l . a * x + l . c ) / l . b NEW_LINE for i in range ( n ) : NEW_LINE INDENT res += self . dist ( x , y , p [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT def find_Optimum_cost_untill ( self , p , n , l ) : NEW_LINE INDENT low = - 1e6 NEW_LINE high = 1e6 NEW_LINE eps = 1e-6 + 1 NEW_LINE while ( ( high - low ) > eps ) : NEW_LINE INDENT mid1 = low + ( high - low ) / 3 NEW_LINE mid2 = high - ( high - low ) / 3 NEW_LINE dist1 = self . compute ( p , n , l , mid1 ) NEW_LINE dist2 = self . compute ( p , n , l , mid2 ) NEW_LINE if ( dist1 < dist2 ) : NEW_LINE INDENT high = mid2 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid1 NEW_LINE DEDENT DEDENT return self . compute ( p , n , l , ( low + high ) / 2 ) NEW_LINE DEDENT def find_Optimum_cost ( self , p , l ) : NEW_LINE INDENT n = len ( p ) NEW_LINE p_arr = [ None ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT p_obj = self . Point ( p [ i ] [ 0 ] , p [ i ] [ 1 ] ) NEW_LINE p_arr [ i ] = p_obj NEW_LINE DEDENT return self . find_Optimum_cost_untill ( p_arr , n , l ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT obj = Optimum_distance ( ) NEW_LINE l = obj . Line ( 1 , - 1 , - 3 ) NEW_LINE p = [ [ - 3 , - 2 ] , [ - 1 , 0 ] , [ - 1 , 2 ] , [ 1 , 2 ] , [ 3 , 4 ] ] NEW_LINE print ( obj . find_Optimum_cost ( p , l ) ) NEW_LINE DEDENT
Klee 's Algorithm (Length Of Union Of Segments of a line) | Returns sum of lengths covered by union of given segments ; Initialize empty points container ; Create a vector to store starting and ending points ; Sorting all points by point value ; Initialize result as 0 ; To keep track of counts of current open segments ( Starting point is processed , but ending point is not ) ; Traverse through all points ; If there are open points , then we add the difference between previous and current point . ; If this is an ending point , reduce , count of open points . ; Driver code
def segmentUnionLength ( segments ) : NEW_LINE INDENT n = len ( segments ) NEW_LINE points = [ None ] * ( n * 2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT points [ i * 2 ] = ( segments [ i ] [ 0 ] , False ) NEW_LINE points [ i * 2 + 1 ] = ( segments [ i ] [ 1 ] , True ) NEW_LINE DEDENT points = sorted ( points , key = lambda x : x [ 0 ] ) NEW_LINE result = 0 NEW_LINE Counter = 0 NEW_LINE for i in range ( 0 , n * 2 ) : NEW_LINE INDENT if ( i > 0 ) & ( points [ i ] [ 0 ] > points [ i - 1 ] [ 0 ] ) & ( Counter > 0 ) : NEW_LINE INDENT result += ( points [ i ] [ 0 ] - points [ i - 1 ] [ 0 ] ) NEW_LINE DEDENT if points [ i ] [ 1 ] : NEW_LINE INDENT Counter -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT Counter += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT segments = [ ( 2 , 5 ) , ( 4 , 8 ) , ( 9 , 12 ) ] NEW_LINE print ( segmentUnionLength ( segments ) ) NEW_LINE DEDENT
Find all duplicate and missing numbers in given permutation array of 1 to N | Function to find the duplicate and the missing elements over the range [ 1 , N ] ; Stores the missing and duplicate numbers in the array arr [ ] ; Traverse the given array arr [ ] ; Check if the current element is not same as the element at index arr [ i ] - 1 , then swap ; Otherwise , increment the index ; Traverse the array again ; If the element is not at its correct position ; Stores the missing and the duplicate elements ; Print the Missing Number ; Print the Duplicate Number ; Driver code
def findElements ( arr , N ) : NEW_LINE INDENT i = 0 ; NEW_LINE missing = [ ] ; NEW_LINE duplicate = set ( ) ; NEW_LINE while ( i != N ) : NEW_LINE INDENT if ( arr [ i ] != arr [ arr [ i ] - 1 ] ) : NEW_LINE INDENT t = arr [ i ] NEW_LINE arr [ i ] = arr [ arr [ i ] - 1 ] NEW_LINE arr [ t - 1 ] = t NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] != i + 1 ) : NEW_LINE INDENT missing . append ( i + 1 ) ; NEW_LINE duplicate . add ( arr [ i ] ) ; NEW_LINE DEDENT DEDENT print ( " Missing ▁ Numbers : ▁ " , end = " " ) ; NEW_LINE for it in missing : NEW_LINE INDENT print ( it , end = " ▁ " ) ; NEW_LINE DEDENT print ( " Duplicate Numbers : " , end = " " ) ; NEW_LINE for it in list ( duplicate ) : NEW_LINE INDENT print ( it , end = ' ▁ ' ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 2 , 4 , 5 , 7 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findElements ( arr , N ) ; NEW_LINE DEDENT
Check if elements of given array can be rearranged such that ( arr [ i ] + i * K ) % N = i for all values of i in range [ 0 , N | Function to check if it is possible to generate all numbers in range [ 0 , N - 1 ] using the sum of elements + in the multiset A and B mod N ; If no more pair of elements can be selected ; If the number of elements in C = N , then return true ; Otherwise return false ; Stores the value of final answer ; Iterate through all the pairs in the given multiset A and B ; Stores the set A without x ; Stores the set A without y ; Stores the set A without x + y % N ; Recursive call ; Return Answer ; Function to check if it is possible to rearrange array elements such that ( arr [ i ] + i * K ) % N = i ; Stores the values of arr [ ] modulo N ; Stores all the values of i * K modulo N ; Print Answer ; Driver code
def isPossible ( A , B , C , N ) : NEW_LINE INDENT if ( len ( A ) == 0 or len ( B ) == 0 ) : NEW_LINE INDENT if ( len ( C ) == N ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT ans = False NEW_LINE for x in A : NEW_LINE INDENT for y in B : NEW_LINE INDENT _A = A NEW_LINE _A . remove ( x ) NEW_LINE _B = B NEW_LINE _B . remove ( y ) NEW_LINE _C = C NEW_LINE _C . add ( ( x + y ) % N ) NEW_LINE ans = ( ans or isPossible ( _A , _B , _C , N ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def rearrangeArray ( arr , N , K ) : NEW_LINE INDENT A = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT A . append ( arr [ i ] % N ) NEW_LINE DEDENT A . sort ( ) NEW_LINE B = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT B . append ( ( i * K ) % N ) NEW_LINE DEDENT B . sort ( ) NEW_LINE C = set ( ) NEW_LINE if isPossible ( A , B , C , N ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 0 ] NEW_LINE K = 5 NEW_LINE N = len ( arr ) NEW_LINE rearrangeArray ( arr , N , K ) NEW_LINE
Height of Factor Tree for a given number | Python 3 program for the above approach ; Function to find the height of the Factor Tree of the integer N ; Stores the height of Factor Tree ; Loop to iterate over values of N ; Stores if there exist a factor of N or not ; Loop to find the smallest factor of N ; If i is a factor of N ; Increment the height ; If there are no factors of N i . e , N is prime , break loop ; Return Answer ; Driver Code
from math import sqrt NEW_LINE def factorTree ( N ) : NEW_LINE INDENT height = 0 NEW_LINE while ( N > 1 ) : NEW_LINE INDENT flag = False NEW_LINE for i in range ( 2 , int ( sqrt ( N ) ) + 1 , 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT N = N // i NEW_LINE flag = True NEW_LINE break NEW_LINE DEDENT DEDENT height += 1 NEW_LINE if ( flag == False ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return height NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 48 NEW_LINE print ( factorTree ( N ) ) NEW_LINE DEDENT
Maximize the largest number K such that bitwise and of K till N is 0 |  ; Function to find maximum value of k which makes bitwise AND zero . ; Finding the power less than N ; Driver Code
import math NEW_LINE def findMaxK ( N ) : NEW_LINE INDENT p = math . log ( N ) // math . log ( 2 ) ; NEW_LINE return int ( pow ( 2 , p ) ) ; NEW_LINE DEDENT N = 5 ; NEW_LINE print ( findMaxK ( N ) - 1 ) ; NEW_LINE
Find a number in minimum steps | Python program to find a number in minimum steps ; To represent data of a node in tree ; Prints level of node n ; Create a queue and insert root ; Do level order traversal ; Remove a node from queue ; q . pop ( ) To avoid infinite loop ; Check if dequeued number is same as n ; Insert children of dequeued node to queue ; Driver code
from collections import deque NEW_LINE InF = 99999 NEW_LINE class number : NEW_LINE INDENT def __init__ ( self , n , l ) : NEW_LINE INDENT self . no = n NEW_LINE self . level = l NEW_LINE DEDENT DEDENT def findnthnumber ( n ) : NEW_LINE INDENT q = deque ( ) NEW_LINE r = number ( 0 , 1 ) NEW_LINE q . append ( r ) NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT temp = q . popleft ( ) NEW_LINE if ( temp . no >= InF or temp . no <= - InF ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( temp . no == n ) : NEW_LINE INDENT print ( " Found ▁ number ▁ n ▁ at ▁ level " , temp . level - 1 ) NEW_LINE break NEW_LINE DEDENT q . append ( number ( temp . no + temp . level , temp . level + 1 ) ) NEW_LINE q . append ( number ( temp . no - temp . level , temp . level + 1 ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT findnthnumber ( 13 ) NEW_LINE DEDENT
Check if all 3 Candy bags can be emptied by removing 2 candies from any one bag and 1 from the other two repeatedly | Python code for the above approach ; If total candies are not multiple of 4 then its not possible to be left with 0 candies ; If minimum candies of three bags are less than number of operations required then the task is not possible ; Driver code
def can_empty ( a , b , c ) : NEW_LINE INDENT if ( ( a + b + c ) % 4 != 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT m = min ( a , min ( b , c ) ) ; NEW_LINE if ( m < ( a + b + c ) // 4 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT a = 4 NEW_LINE b = 2 NEW_LINE c = 2 NEW_LINE print ( " true " if can_empty ( a , b , c ) else " false " ) ; NEW_LINE a = 3 NEW_LINE b = 4 NEW_LINE c = 2 NEW_LINE print ( " true " if can_empty ( a , b , c ) else " false " ) ; NEW_LINE