text
stringlengths
17
3.65k
code
stringlengths
70
5.84k
Angle of intersection of two circles having their centers D distance apart | Function to find the cosine of the angle of the intersection of two circles with radius R1 and R2 ; Return the cosine of the angle ; Driver Code
def angle ( R1 , R2 , D ) : NEW_LINE INDENT ans = ( ( R1 * R1 + R2 * R2 - D * D ) / ( 2 * R1 * R2 ) ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT R1 = 3 NEW_LINE R2 = 4 NEW_LINE D = 5 NEW_LINE print ( angle ( R1 , R2 , D ) ) NEW_LINE DEDENT
Check if any point exists in a plane whose Manhattan distance is at most K from N given points | Function to check if there exists any point with at most K distance from N given points ; Traverse the given n points ; Stores the count of pairs of coordinates having Manhattan distance <= K ; For the same coordinate ; Calculate Manhattan distance ; If Manhattan distance <= K ; If all coordinates can meet ; If all coordinates can 't meet ; Driver code
def find ( a , b , N , K ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT continue NEW_LINE DEDENT dis = abs ( a [ i ] - a [ j ] ) + abs ( b [ i ] - b [ j ] ) NEW_LINE if ( dis <= K ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if ( count == N - 1 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT return " No " NEW_LINE DEDENT DEDENT N = 5 NEW_LINE A = [ 1 , 0 , 2 , 1 , 1 ] NEW_LINE B = [ 1 , 1 , 1 , 0 , 2 ] NEW_LINE K = 1 NEW_LINE print ( find ( A , B , N , K ) ) NEW_LINE
Minimum area of the triangle formed by any tangent to an ellipse with the coordinate axes | Function to find the minimum area of triangle formed by any tangent to ellipse with the coordinate axes ; Stores the minimum area ; Print the calculated area ; Driver Code
def minimumTriangleArea ( a , b ) : NEW_LINE INDENT area = a * b NEW_LINE print ( area ) NEW_LINE DEDENT a = 1 NEW_LINE b = 2 NEW_LINE minimumTriangleArea ( a , b ) NEW_LINE
Find interior angles for each side of a given Cyclic Quadrilateral | Python3 program for the above approach ; Function to find the interior angles of the cyclic quadrilateral ; Stores the numerator and the denominator to find angle A ; Stores the numerator and the denominator to find angle B ; Stores the numerator and the denominator to find angle C : ; Stores the numerator and the denominator to find angle D : ; Driver Code
import math NEW_LINE def findAngles ( a , b , c , d ) : NEW_LINE INDENT numerator = a * a + d * d - b * b - c * c NEW_LINE denominator = 2 * ( a * b + c * d ) NEW_LINE x = numerator / denominator NEW_LINE print ( " A : ▁ " , ' % .2f ' % ( ( math . acos ( x ) * 180 ) / 3.141592 ) , " ▁ degrees " ) NEW_LINE numerator = a * a + b * b - c * c - d * d NEW_LINE x = numerator / denominator NEW_LINE print ( " B : ▁ " , ' % .2f ' % ( ( math . acos ( x ) * 180 ) / 3.141592 ) , " ▁ degrees " ) NEW_LINE numerator = c * c + b * b - a * a - d * d NEW_LINE x = numerator / denominator NEW_LINE print ( " C : ▁ " , ' % .2f ' % ( ( math . acos ( x ) * 180 ) / 3.141592 ) , " ▁ degrees " ) NEW_LINE numerator = d * d + c * c - a * a - b * b NEW_LINE x = numerator / denominator NEW_LINE print ( " D : ▁ " , ' % .2f ' % ( ( math . acos ( x ) * 180 ) / 3.141592 ) , " ▁ degrees " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 10 NEW_LINE B = 15 NEW_LINE C = 20 NEW_LINE D = 25 NEW_LINE findAngles ( A , B , C , D ) NEW_LINE DEDENT
Number of smaller circles that can be inscribed in a larger circle | Python3 program for the above approach ; Function to count number of smaller circles that can be inscribed in the larger circle touching its boundary ; If R2 is greater than R1 ; Stores the angle made by the smaller circle ; Stores the ratio of R2 / ( R1 - R2 ) ; Stores the count of smaller circles that can be inscribed ; Stores the ratio ; If the diameter of smaller circle is greater than the radius of the larger circle ; Otherwise ; Find the angle using formula ; Divide 360 with angle and take the floor value ; Return the final result ; Driver Code
import math NEW_LINE def countInscribed ( R1 , R2 ) : NEW_LINE INDENT if ( R2 > R1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT angle = 0 NEW_LINE ratio = 0 NEW_LINE number_of_circles = 0 NEW_LINE ratio = R2 / ( R1 - R2 ) NEW_LINE if ( R1 < 2 * R2 ) : NEW_LINE INDENT number_of_circles = 1 NEW_LINE DEDENT else : NEW_LINE INDENT angle = ( abs ( math . asin ( ratio ) * 180 ) / 3.14159265 ) NEW_LINE number_of_circles = ( 360 / ( 2 * math . floor ( angle ) ) ) NEW_LINE DEDENT return number_of_circles NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT R1 = 3 NEW_LINE R2 = 1 NEW_LINE print ( int ( countInscribed ( R1 , R2 ) ) ) NEW_LINE DEDENT
Count pairs of coordinates connected by a line with slope in the range [ | Function to find the number of pairs of points such that the line passing through them has a slope in the range [ - k , k ] ; Store the result ; Traverse through all the combination of points ; If pair satisfies the given condition ; Increment ans by 1 ; Print the result ; Driver Code ; Function Call
def findPairs ( x , y , K ) : NEW_LINE INDENT n = len ( x ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( K * abs ( x [ i ] - x [ j ] ) >= abs ( y [ i ] - y [ j ] ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ 2 , 1 , 0 ] NEW_LINE Y = [ 1 , 2 , 0 ] NEW_LINE K = 1 NEW_LINE findPairs ( X , Y , K ) NEW_LINE DEDENT
Number of largest circles that can be inscribed in a rectangle | Function to count the number of largest circles in a rectangle ; If length exceeds breadth ; Swap to reduce length to smaller than breadth ; Return total count of circles inscribed ; Driver Code
def totalCircles ( L , B ) : NEW_LINE INDENT if ( L > B ) : NEW_LINE INDENT temp = L NEW_LINE L = B NEW_LINE B = temp NEW_LINE DEDENT return B // L NEW_LINE DEDENT L = 3 NEW_LINE B = 8 NEW_LINE print ( totalCircles ( L , B ) ) NEW_LINE
Program to calculate length of diagonal of a square | Python3 program for the above approach ; Function to find the length of the diagonal of a square of a given side ; Driver Code
import math NEW_LINE def findDiagonal ( s ) : NEW_LINE INDENT return math . sqrt ( 2 ) * s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = 10 NEW_LINE print ( findDiagonal ( S ) ) NEW_LINE DEDENT
Area of largest isosceles triangle that can be inscribed in an Ellipse whose vertex coincides with one extremity of the major axis | Python 3 program for the above approach ; Function to calculate area of the isosceles triangle ; If a and b are negative ; Stores the area of the triangle ; Print the area ; Driver code ; Given value of a & b ; Function call to find the area of the isosceles triangle
from math import sqrt NEW_LINE def triangleArea ( a , b ) : NEW_LINE INDENT if ( a < 0 or b < 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT area = ( 3 * sqrt ( 3 ) * a * b ) / ( 4 ) ; NEW_LINE print ( " { : . 5f } " . format ( area ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 1 NEW_LINE b = 2 NEW_LINE triangleArea ( a , b ) NEW_LINE DEDENT
Queries to count points lying on or inside an isosceles Triangle with given length of equal sides | Python3 implementation of above approach ; Function to find answer of each query ; Stores the count of points with sum less than or equal to their indices ; Traverse the array ; ` If both x and y - coordinate < 0 ; Stores the sum of co - ordinates ; Increment count of sum by 1 ; Prefix array ; Perform queries ; Drivers Code
MAX = 10 ** 6 + 5 NEW_LINE from math import ceil NEW_LINE def query ( arr , Q ) : NEW_LINE INDENT pre = [ 0 ] * ( MAX ) NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] [ 0 ] < 0 or arr [ i ] [ 1 ] < 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT sum = ceil ( ( arr [ i ] [ 0 ] + arr [ i ] [ 1 ] ) ) ; NEW_LINE pre [ sum ] += 1 NEW_LINE DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT pre [ i ] += pre [ i - 1 ] NEW_LINE DEDENT for i in range ( len ( Q ) ) : NEW_LINE INDENT print ( pre [ Q [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 2.1 , 3.0 ] , [ 3.7 , 1.2 ] , [ 1.5 , 6.5 ] , [ 1.2 , 0.0 ] ] NEW_LINE Q = [ 2 , 8 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( Q ) NEW_LINE query ( arr , Q ) NEW_LINE DEDENT
Perimeter of the Union of Two Rectangles | Function to check if two rectangles are intersecting or not ; If one rectangle is to the right of other 's right edge ; If one rectangle is on the top of other 's top edge ; Function to return the perimeter of the Union of Two Rectangles ; Stores the resultant perimeter ; If rectangles do not interesect ; Perimeter of Rectangle 1 ; Perimeter of Rectangle 2 ; If the rectangles intersect ; Get width of combined figure ; Get length of combined figure ; Return the perimeter ; Driver Code
def doIntersect ( X , Y ) : NEW_LINE INDENT if ( X [ 0 ] > X [ 3 ] or X [ 2 ] > X [ 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( Y [ 0 ] > Y [ 3 ] or Y [ 2 ] > Y [ 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def getUnionPerimeter ( X , Y ) : NEW_LINE INDENT perimeter = 0 NEW_LINE if ( not doIntersect ( X , Y ) ) : NEW_LINE INDENT perimeter += 2 * ( abs ( X [ 1 ] - X [ 0 ] ) + abs ( Y [ 1 ] - Y [ 0 ] ) ) NEW_LINE perimeter += 2 * ( abs ( X [ 3 ] - X [ 2 ] ) + abs ( Y [ 3 ] - Y [ 2 ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT w = max ( X ) - min ( X ) NEW_LINE l = max ( Y ) - min ( Y ) NEW_LINE perimeter = 2 * ( l + w ) NEW_LINE DEDENT return perimeter NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ - 1 , 2 , 4 , 6 ] NEW_LINE Y = [ 2 , 5 , 3 , 7 ] NEW_LINE print ( getUnionPerimeter ( X , Y ) ) NEW_LINE DEDENT
Check whether two straight lines are parallel or not | Function to check if two lines are parallel or not ; If slopes are equal ; Driver Code ; Function Call
def parallel ( a1 , b1 , c1 , a2 , b2 , c2 ) : NEW_LINE INDENT if ( ( - ( a1 / b1 ) ) == ( - ( a2 / b2 ) ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a1 = - 2 ; b1 = 4 ; c1 = 5 ; NEW_LINE a2 = - 6 ; b2 = 12 ; c2 = 6 ; NEW_LINE parallel ( a1 , b1 , c1 , a2 , b2 , c2 ) ; NEW_LINE DEDENT
Calculate area and height of an isosceles triangle whose sides are radii of a circle | Python3 program for the above approach ; Function to convert given angle from degree to radian ; Function to calculate height and area of the triangle OAB ; Stores the angle OAB and OBA ; Stores the angle in radians ; Stores the height ; Print height of the triangle ; Stores the base of triangle OAB ; Stores the area of the triangle ; Print the area of triangle OAB ; Driver Code
from math import sin , cos NEW_LINE def Convert ( degree ) : NEW_LINE INDENT pi = 3.14159265359 NEW_LINE return ( degree * ( pi / 180 ) ) NEW_LINE DEDENT def areaAndHeightOfTraingle ( radius , a ) : NEW_LINE INDENT if ( a >= 180 or a == 0 ) : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE return NEW_LINE DEDENT base_angle = ( 180 - a ) / 2 NEW_LINE radians = Convert ( base_angle ) NEW_LINE height = sin ( radians ) * radius NEW_LINE print ( " Height ▁ of ▁ triangle ▁ " , round ( height , 1 ) ) NEW_LINE base = cos ( radians ) * radius NEW_LINE area = base * height NEW_LINE print ( " Area ▁ of ▁ triangle ▁ " , round ( area , 4 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT R , angle = 5 , 120 NEW_LINE areaAndHeightOfTraingle ( R , angle ) NEW_LINE DEDENT
Radius of a circle having area equal to the sum of area of the circles having given radii | Function to calculate radius of the circle having area equal to sum of the area of two circles with given radii ; Area of first circle ; Area of second circle ; Area of third circle ; Radius of third circle ; Driver code ; Given radius ; Prints the radius of the required circle
def findRadius ( r1 , r2 ) : NEW_LINE INDENT a1 , a2 , a3 , r3 = 0 , 0 , 0 , 0 ; NEW_LINE a1 = 3.14 * r1 * r1 ; NEW_LINE a2 = 3.14 * r2 * r2 ; NEW_LINE a3 = a1 + a2 ; NEW_LINE r3 = ( ( a3 / 3.14 ) ** ( 1 / 2 ) ) ; NEW_LINE return r3 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r1 = 8 ; r2 = 6 ; NEW_LINE print ( int ( findRadius ( r1 , r2 ) ) ) ; NEW_LINE DEDENT
Minimum number of Cuboids required to form a Cube | Function to calculate and return LCM of a , b , and c ; Find GCD of a and b ; Find LCM of a and b ; LCM ( a , b , c ) = LCM ( LCM ( a , b ) , c ) ; Finding LCM of a , b , c ; return LCM ( a , b , c ) ; Function to find the minimum number of cuboids required to make the volume of a valid cube ; Find the LCM of L , B , H ; Volume of the cube ; Volume of the cuboid ; Minimum number cuboids required to form a cube ; Driver Code ; Given dimensions of cuboid ; Function Call
def find_lcm ( a , b , c ) : NEW_LINE INDENT g = __gcd ( a , b ) ; NEW_LINE LCM1 = ( a * b ) // g ; NEW_LINE g = __gcd ( LCM1 , c ) ; NEW_LINE LCM = ( LCM1 * c ) // g ; NEW_LINE return LCM ; NEW_LINE DEDENT def minimumCuboids ( L , B , H ) : NEW_LINE INDENT lcm = find_lcm ( L , B , H ) ; NEW_LINE volume_cube = lcm * lcm * lcm ; NEW_LINE volume_cuboid = L * B * H ; NEW_LINE print ( ( volume_cube // volume_cuboid ) ) ; NEW_LINE DEDENT def __gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT else : NEW_LINE INDENT return __gcd ( b , a % b ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 1 ; B = 1 ; H = 2 ; NEW_LINE minimumCuboids ( L , B , H ) ; NEW_LINE DEDENT
Check if given polygon is a convex polygon or not | Utility function to find cross product of two vectors ; Stores coefficient of X direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of X direction of vector A [ 2 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 2 ] A [ 0 ] ; Return cross product ; Function to check if the polygon is convex polygon or not ; Stores count of edges in polygon ; Stores direction of cross product of previous traversed edges ; Stores direction of cross product of current traversed edges ; Traverse the array ; Stores three adjacent edges of the polygon ; Update curr ; If curr is not equal to 0 ; If direction of cross product of all adjacent edges are not same ; Update curr ; Driver code
def CrossProduct ( A ) : NEW_LINE INDENT X1 = ( A [ 1 ] [ 0 ] - A [ 0 ] [ 0 ] ) NEW_LINE Y1 = ( A [ 1 ] [ 1 ] - A [ 0 ] [ 1 ] ) NEW_LINE X2 = ( A [ 2 ] [ 0 ] - A [ 0 ] [ 0 ] ) NEW_LINE Y2 = ( A [ 2 ] [ 1 ] - A [ 0 ] [ 1 ] ) NEW_LINE return ( X1 * Y2 - Y1 * X2 ) NEW_LINE DEDENT def isConvex ( points ) : NEW_LINE INDENT N = len ( points ) NEW_LINE prev = 0 NEW_LINE curr = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp = [ points [ i ] , points [ ( i + 1 ) % N ] , points [ ( i + 2 ) % N ] ] NEW_LINE curr = CrossProduct ( temp ) NEW_LINE if ( curr != 0 ) : NEW_LINE INDENT if ( curr * prev < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT prev = curr NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT points = [ [ 0 , 0 ] , [ 0 , 1 ] , [ 1 , 1 ] , [ 1 , 0 ] ] NEW_LINE if ( isConvex ( points ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Program to find all possible triangles having same Area and Perimeter | Function to print sides of all the triangles having same perimeter & area ; Stores unique sides of triangles ; i + j + k values cannot exceed 256 ; Find the value of 2 * s ; Find the value of 2 * ( s - a ) ; Find the value of 2 * ( s - b ) ; Find the value of 2 * ( s - c ) ; If triplets have same area and perimeter ; Store sides of triangle ; Sort the triplets ; Inserting in set to avoid duplicate sides ; Print sides of all desired triangles ; Driver Code ; Function call
def samePerimeterAndArea ( ) : NEW_LINE INDENT se = [ ] NEW_LINE for i in range ( 1 , 256 , 1 ) : NEW_LINE INDENT for j in range ( 1 , 256 , 1 ) : NEW_LINE INDENT for k in range ( 1 , 256 , 1 ) : NEW_LINE INDENT peri = i + j + k NEW_LINE mul1 = - i + j + k NEW_LINE if ( k > 100 ) : NEW_LINE break NEW_LINE if ( j > 100 ) : NEW_LINE break NEW_LINE if ( i > 100 ) : NEW_LINE break NEW_LINE mul2 = i - j + k NEW_LINE mul3 = i + j - k NEW_LINE if ( 16 * peri == mul1 * mul2 * mul3 ) : NEW_LINE INDENT v = [ i , j , k ] NEW_LINE v . sort ( reverse = False ) NEW_LINE se . append ( v ) NEW_LINE se . sort ( reverse = False ) NEW_LINE DEDENT DEDENT DEDENT DEDENT temp = [ ] NEW_LINE temp . append ( se [ 0 ] ) NEW_LINE temp . append ( se [ 6 ] ) NEW_LINE temp . append ( se [ 12 ] ) NEW_LINE temp . append ( se [ 18 ] ) NEW_LINE temp . append ( se [ 24 ] ) NEW_LINE for it in temp : NEW_LINE INDENT print ( it [ 0 ] , it [ 1 ] , it [ 2 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT samePerimeterAndArea ( ) NEW_LINE DEDENT
Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points | Function to get the count of ractangles ; Store distinct horizontal lines ; Store distinct Vertical lines ; Insert horizontal line passing through 0 ; Insert vertical line passing through 0. ; Insert horizontal line passing through rectangle [ 3 ] [ 0 ] ; Insert vertical line passing through rectangle [ 3 ] [ 1 ] ; Insert all horizontal and vertical lines passing through the given array ; Insert all horizontal lines ; Insert all vertical lines ; Driver Code
def cntRect ( points , N , rectangle ) : NEW_LINE INDENT cntHor = set ( [ ] ) NEW_LINE cntVer = set ( [ ] ) NEW_LINE cntHor . add ( 0 ) NEW_LINE cntVer . add ( 0 ) NEW_LINE cntHor . add ( rectangle [ 3 ] [ 0 ] ) NEW_LINE cntVer . add ( rectangle [ 3 ] [ 1 ] ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT cntHor . add ( points [ i ] [ 0 ] ) NEW_LINE cntVer . add ( points [ i ] [ 1 ] ) NEW_LINE DEDENT return ( ( len ( cntHor ) - 1 ) * ( len ( cntVer ) - 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT rectangle = [ [ 0 , 0 ] , [ 0 , 5 ] , [ 5 , 0 ] , [ 5 , 5 ] ] NEW_LINE points = [ [ 1 , 2 ] , [ 3 , 4 ] ] NEW_LINE N = len ( points ) NEW_LINE print ( cntRect ( points , N , rectangle ) ) NEW_LINE DEDENT
Count squares possible from M and N straight lines parallel to X and Y axis respectively | Function to count all the possible squares with given lines parallel to both the X and Y axis ; Stores the count of all possible distances in X [ ] & Y [ ] respectively ; Find distance between all pairs in the array X [ ] ; Add the count to m1 ; Find distance between all pairs in the array Y [ ] ; Add the count to m2 ; Find sum of m1 [ i ] * m2 [ i ] for same distance ; Find current count in m2 ; Add to the total count ; Return the final count ; Driver Code ; Given lines ; Function call
def numberOfSquares ( X , Y , N , M ) : NEW_LINE INDENT m1 = { } NEW_LINE m2 = { } NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT dist = abs ( X [ i ] - X [ j ] ) NEW_LINE if dist in m1 : NEW_LINE INDENT m1 [ dist ] = m1 [ dist ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT m1 [ dist ] = 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , M ) : NEW_LINE INDENT for j in range ( i + 1 , M ) : NEW_LINE INDENT dist = abs ( Y [ i ] - Y [ j ] ) NEW_LINE if dist in m2 : NEW_LINE INDENT m2 [ dist ] = m2 [ dist ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT m2 [ dist ] = 1 NEW_LINE DEDENT DEDENT DEDENT for key in m1 : NEW_LINE INDENT if key in m2 : NEW_LINE INDENT ans = ans + ( m1 [ key ] * m2 [ key ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = [ 1 , 3 , 7 ] NEW_LINE Y = [ 2 , 4 , 6 , 1 ] NEW_LINE N = len ( X ) NEW_LINE M = len ( Y ) NEW_LINE print ( numberOfSquares ( X , Y , N , M ) ) NEW_LINE DEDENT
Program to calculate area of a parallelogram | Python3 program for the above approach ; Function to return the area of parallelogram using sides and angle at the intersection of diagonal ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using sides and angle at the intersection of sides ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using diagonals and angle at the intersection of diagonals ; Calculate area of parallelogram ; Return the answer ; Given diagonal and angle ; Function Call ; Print the area
import math NEW_LINE def Area_Parallelogram1 ( a , b , theta ) : NEW_LINE INDENT area = ( abs ( math . tan ( math . radians ( theta ) ) ) / 2 ) * abs ( a ** 2 - b ** 2 ) NEW_LINE return area NEW_LINE DEDENT def Area_Parallelogram2 ( a , b , gamma ) : NEW_LINE INDENT area = ( abs ( math . sin ( math . radians ( gamma ) ) ) ) * abs ( a * b ) NEW_LINE return area NEW_LINE DEDENT def Area_Parallelogram3 ( d1 , d2 , theta ) : NEW_LINE INDENT area = ( abs ( math . sin ( math . radians ( theta ) ) ) / 2 ) * abs ( d1 * d2 ) NEW_LINE return area NEW_LINE DEDENT d1 = 3 NEW_LINE d2 = 5 NEW_LINE theta = 90 NEW_LINE area = Area_Parallelogram3 ( d1 , d2 , theta ) NEW_LINE print ( round ( area , 2 ) ) NEW_LINE
Count squares of size K inscribed in a square of size N | Function to calculate the number of squares of size K in a square of size N ; Stores the number of squares ; Driver Code ; Size of the bigger square ; Size of smaller square
def No_of_squares ( N , K ) : NEW_LINE INDENT no_of_squares = 0 ; NEW_LINE no_of_squares = ( N - K + 1 ) * ( N - K + 1 ) ; NEW_LINE return no_of_squares ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE K = 3 ; NEW_LINE print ( No_of_squares ( N , K ) ) ; NEW_LINE DEDENT
Count cubes of size K inscribed in a cube of size N | Function to find the number of the cubes of the size K ; Stores the number of cubes ; Stores the number of cubes of size k ; Size of the bigger cube ; Size of the smaller cube
def No_of_cubes ( N , K ) : NEW_LINE INDENT No = 0 NEW_LINE No = ( N - K + 1 ) NEW_LINE No = pow ( No , 3 ) NEW_LINE return No NEW_LINE DEDENT N = 5 NEW_LINE K = 2 NEW_LINE print ( No_of_cubes ( N , K ) ) NEW_LINE
Count of smaller rectangles that can be placed inside a bigger rectangle | Function to count smaller rectangles within the larger rectangle ; If the dimension of the smaller rectangle is greater than the bigger one ; Return the number of smaller rectangles possible ; Driver code ; Dimension of bigger rectangle ; Dimension of smaller rectangle ; Function call
def No_of_rectangles ( L , B , l , b ) : NEW_LINE INDENT if ( l > L ) or ( b > B ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( L - l + 1 ) * ( B - b + 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 5 NEW_LINE B = 3 NEW_LINE l = 4 NEW_LINE b = 1 NEW_LINE print ( No_of_rectangles ( L , B , l , b ) ) NEW_LINE DEDENT
Program To Check whether a Triangle is Equilateral , Isosceles or Scalene | Function to check if the triangle is equilateral or isosceles or scalene ; _Check for equilateral triangle ; Check for isosceles triangle ; Otherwise scalene triangle ; Given sides of triangle ; Function Call
def checkTriangle ( x , y , z ) : NEW_LINE INDENT if x == y == z : NEW_LINE INDENT print ( " Equilateral ▁ Triangle " ) NEW_LINE DEDENT elif x == y or y == z or z == x : NEW_LINE INDENT print ( " Isosceles ▁ Triangle " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Scalene ▁ Triangle " ) NEW_LINE DEDENT DEDENT x = 8 NEW_LINE y = 7 NEW_LINE z = 9 NEW_LINE checkTriangle ( x , y , z ) NEW_LINE
Find the remaining vertices of a square from two given vertices | Function to find the remaining vertices of a square ; Check if the x - coordinates are equal ; Check if the y - coordinates are equal ; If the the given coordinates forms a diagonal of the square ; Otherwise ; Square does not exist ; Driver Code ; Given two vertices
def findVertices ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT if ( x1 == x2 ) : NEW_LINE INDENT print ( ( x1 + y2 - y1 ) , " , " , y1 ) NEW_LINE print ( ( x2 + y2 - y1 ) , " , " , y2 ) NEW_LINE DEDENT elif ( y1 == y2 ) : NEW_LINE INDENT print ( x1 , " , " , ( y1 + x2 - x1 ) ) NEW_LINE print ( x2 , " , " , ( y2 + x2 - x1 ) ) NEW_LINE DEDENT elif ( abs ( x2 - x1 ) == abs ( y2 - y1 ) ) : NEW_LINE INDENT print ( x1 , " , " , y2 ) NEW_LINE print ( x2 , " , " , y1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x1 = 1 NEW_LINE y1 = 2 NEW_LINE x2 = 3 NEW_LINE y2 = 4 NEW_LINE findVertices ( x1 , y1 , x2 , y2 ) NEW_LINE DEDENT
Find the area of rhombus from given Angle and Side length | Python3 Program to calculate area of rhombus from given angle and side length ; Function to return the area of rhombus using one angle and side . ; Driver Code ; Function Call ; Print the final answer
import math NEW_LINE def Area_of_Rhombus ( a , theta ) : NEW_LINE INDENT area = ( a ** 2 ) * math . sin ( math . radians ( theta ) ) NEW_LINE return area NEW_LINE DEDENT a = 4 NEW_LINE theta = 60 NEW_LINE ans = Area_of_Rhombus ( a , theta ) NEW_LINE print ( round ( ans , 2 ) ) NEW_LINE
Count of Equilateral Triangles of unit length possible from a given Hexagon | Function to calculate the the number of Triangles possible ; Regular Hexagon ; Irregular Hexagon
def calculateTriangles ( sides ) : NEW_LINE INDENT count = pow ( sides [ 0 ] + sides [ 1 ] + sides [ 2 ] , 2 ) NEW_LINE count -= pow ( sides [ 0 ] , 2 ) NEW_LINE count -= pow ( sides [ 2 ] , 2 ) NEW_LINE count -= pow ( sides [ 4 ] , 2 ) NEW_LINE return int ( count ) NEW_LINE DEDENT sides = [ 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE print ( calculateTriangles ( sides ) ) NEW_LINE sides = [ 2 , 2 , 1 , 3 , 1 , 2 ] NEW_LINE print ( calculateTriangles ( sides ) ) NEW_LINE
Length of diagonal of a parallelogram using adjacent sides and angle between them | Python3 Program to find length Of diagonal of a parallelogram Using sides and angle between them . ; Function to return the length Of diagonal of a parallelogram using sides and angle between them . ; Given Sides ; Given Angle ; Function Call ; Print the final answer
import math NEW_LINE def Length_Diagonal ( a , b , theta ) : NEW_LINE INDENT diagonal = math . sqrt ( ( ( a ** 2 ) + ( b ** 2 ) ) - 2 * a * b * math . cos ( math . radians ( theta ) ) ) NEW_LINE return diagonal NEW_LINE DEDENT a = 3 NEW_LINE b = 5 NEW_LINE theta = 45 NEW_LINE ans = Length_Diagonal ( a , b , theta ) NEW_LINE print ( round ( ans , 2 ) ) NEW_LINE
Maximum number of tiles required to cover the floor of given size using 2 x1 size tiles | Function to find the maximum number of tiles required to cover the floor of size m x n using 2 x 1 size tiles ; Prthe answer ; Driver code ; Given M and N ; Function call
def maximumTiles ( n , m ) : NEW_LINE INDENT print ( int ( ( m * n ) / 2 ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 3 ; NEW_LINE N = 4 ; NEW_LINE maximumTiles ( N , M ) ; NEW_LINE DEDENT
Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal | Python Program to implement the above approach ; Function to calculate the length of the diagonal of a parallelogram using two sides and other diagonal ; Driver Code ; Function Call ; Print the final answer
import math NEW_LINE def Length_Diagonal ( a , b , d ) : NEW_LINE INDENT diagonal = math . sqrt ( 2 * ( ( a ** 2 ) + ( b ** 2 ) ) - ( d ** 2 ) ) NEW_LINE return diagonal NEW_LINE DEDENT A = 10 NEW_LINE B = 30 NEW_LINE D = 20 NEW_LINE ans = Length_Diagonal ( A , B , D ) NEW_LINE print ( round ( ans , 2 ) ) NEW_LINE
Length of Diagonals of a Cyclic Quadrilateral using the length of Sides . | Python3 program to implement the above approach ; Function to calculate the length of diagonals of a cyclic quadrilateral ; Driver Code ; Function Call ; Print the final answer
import math NEW_LINE def Diagonals ( a , b , c , d ) : NEW_LINE INDENT p = math . sqrt ( ( ( a * c ) + ( b * d ) ) * ( ( a * d ) + ( b * c ) ) / ( ( a * b ) + ( c * d ) ) ) NEW_LINE q = math . sqrt ( ( ( a * c ) + ( b * d ) ) * ( ( a * b ) + ( c * d ) ) / ( ( a * d ) + ( b * c ) ) ) NEW_LINE return [ p , q ] NEW_LINE DEDENT A = 10 NEW_LINE B = 15 NEW_LINE C = 20 NEW_LINE D = 25 NEW_LINE ans = Diagonals ( A , B , C , D ) NEW_LINE print ( round ( ans [ 0 ] , 2 ) , round ( ans [ 1 ] , 2 ) ) NEW_LINE
Minimum Sum of Euclidean Distances to all given Points | Python3 program to implement the above approach ; Function to calculate Euclidean distance ; Function to calculate the minimum sum of the euclidean distances to all points ; Calculate the centroid ; Calculate distance of all points ; Driver Code ; Initializing the points
from math import sqrt NEW_LINE def find ( x , y , p ) : NEW_LINE INDENT mind = 0 NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT a = p [ i ] [ 0 ] NEW_LINE b = p [ i ] [ 1 ] NEW_LINE mind += sqrt ( ( x - a ) * ( x - a ) + ( y - b ) * ( y - b ) ) NEW_LINE DEDENT return mind NEW_LINE DEDENT def getMinDistSum ( p ) : NEW_LINE INDENT x = 0 NEW_LINE y = 0 NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT x += p [ i ] [ 0 ] NEW_LINE y += p [ i ] [ 1 ] NEW_LINE DEDENT x = x // len ( p ) NEW_LINE y = y // len ( p ) NEW_LINE mind = find ( x , y , p ) NEW_LINE return mind NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT vec = [ [ 0 , 1 ] , [ 1 , 0 ] , [ 1 , 2 ] , [ 2 , 1 ] ] NEW_LINE d = getMinDistSum ( vec ) NEW_LINE print ( int ( d ) ) NEW_LINE DEDENT
Coplanarity of Two Lines in 3D Geometry | Function to generate determinant ; Return the sum ; Driver Code ; Position vector of first line ; Direction ratios of line to which first line is parallel ; Position vectors of second line ; Direction ratios of line to which second line is parallel ; Determinant to check coplanarity ; If determinant is zero ; Otherwise
def det ( d ) : NEW_LINE INDENT Sum = d [ 0 ] [ 0 ] * ( ( d [ 1 ] [ 1 ] * d [ 2 ] [ 2 ] ) - ( d [ 2 ] [ 1 ] * d [ 1 ] [ 2 ] ) ) NEW_LINE Sum -= d [ 0 ] [ 1 ] * ( ( d [ 1 ] [ 0 ] * d [ 2 ] [ 2 ] ) - ( d [ 1 ] [ 2 ] * d [ 2 ] [ 0 ] ) ) NEW_LINE Sum += d [ 0 ] [ 2 ] * ( ( d [ 0 ] [ 1 ] * d [ 1 ] [ 2 ] ) - ( d [ 0 ] [ 2 ] * d [ 1 ] [ 1 ] ) ) NEW_LINE return Sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x1 , y1 , z1 = - 3 , 1 , 5 NEW_LINE a1 , b1 , c1 = - 3 , 1 , 5 NEW_LINE x2 , y2 , z2 = - 1 , 2 , 5 NEW_LINE a2 , b2 , c2 = - 1 , 2 , 5 NEW_LINE det_list = [ [ x2 - x1 , y2 - y1 , z2 - z1 ] , [ a1 , b1 , c1 ] , [ a2 , b2 , c2 ] ] NEW_LINE if ( det ( det_list ) == 0 ) : NEW_LINE INDENT print ( " Lines ▁ are ▁ coplanar " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Lines ▁ are ▁ non ▁ coplanar " ) NEW_LINE DEDENT DEDENT
Distance between Incenter and Circumcenter of a triangle using Inradius and Circumradius | Python3 program for the above approach ; Function returns the required distance ; Length of Inradius ; Length of Circumradius
import math NEW_LINE def distance ( r , R ) : NEW_LINE INDENT d = math . sqrt ( ( R ** 2 ) - ( 2 * r * R ) ) NEW_LINE return d NEW_LINE DEDENT r = 2 NEW_LINE R = 5 NEW_LINE print ( round ( distance ( r , R ) , 2 ) ) NEW_LINE
Check if an N | Function to check if the polygon exists or not ; Initialize a variable to store the sum of angles ; Loop through the array and calculate the sum of angles ; Check the condition for an N - side polygon ; Driver Code ; Given array arr [ ] ; Function Call
def checkValidPolygon ( arr , N ) : NEW_LINE INDENT Sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE DEDENT if Sum == 180 * ( N - 2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT N = 3 NEW_LINE arr = [ 60 , 60 , 60 ] NEW_LINE checkValidPolygon ( arr , N ) NEW_LINE
Find the angle of Rotational Symmetry of an N | Function to find required minimum angle of rotation ; Calculating the angle of rotation and type - casting the integer N to type ; Driver code
def minAnglRot ( N ) : NEW_LINE INDENT res = 360 // N NEW_LINE return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 ; NEW_LINE print ( " Angle ▁ of ▁ Rotational ▁ Symmetry : ▁ " , minAnglRot ( N ) ) NEW_LINE DEDENT
Area of a Triangle from the given lengths of medians | Python3 program to calculate area of a triangle from the given lengths of medians ; Function to return the area of triangle using medians ; Driver Code ; Function Call ; Print the final answer
import math NEW_LINE def Area_of_Triangle ( a , b , c ) : NEW_LINE INDENT s = ( a + b + c ) // 2 NEW_LINE x = s * ( s - a ) NEW_LINE x = x * ( s - b ) NEW_LINE x = x * ( s - c ) NEW_LINE area = ( 4 / 3 ) * math . sqrt ( x ) NEW_LINE return area NEW_LINE DEDENT a = 9 NEW_LINE b = 12 NEW_LINE c = 15 NEW_LINE ans = Area_of_Triangle ( a , b , c ) NEW_LINE print ( round ( ans , 2 ) ) NEW_LINE
Total number of unit cells covered by all given Rectangles | Python3 program to find the number of cells enclosed by the given rectangles ; Update the coordinates lying within the rectangle ; Update arr [ i ] [ j ] for all ( i , j ) lying within the rectangle ; Function to return the total area covered by rectangles ; Stores the number of cells ; arr [ i ] ] [ [ j ] == 1 means that grid is filled by some rectangle ; Driver code ; ( A [ i ] [ 0 ] , A [ i ] [ 1 ] ) denotes the coordinate of the bottom left of the rectangle ( A [ i ] [ 2 ] , A [ i ] [ 3 ] ) denotes the coordinate of upper right of the rectangle ; Update the coordinates that lie within the rectangle
MAX = 1001 NEW_LINE arr = [ [ False for i in range ( MAX ) ] for j in range ( MAX ) ] NEW_LINE def updateArray ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT for i in range ( x1 , x2 ) : NEW_LINE INDENT for j in range ( y1 , y2 ) : NEW_LINE INDENT arr [ i ] [ j ] = True NEW_LINE DEDENT DEDENT DEDENT def findAreaCovered ( ) : NEW_LINE INDENT area = 0 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT for j in range ( MAX ) : NEW_LINE INDENT if arr [ i ] [ j ] : NEW_LINE INDENT area += 1 NEW_LINE DEDENT DEDENT DEDENT return area NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE A = [ [ 1 , 3 , 4 , 5 ] , [ 3 , 1 , 7 , 4 ] , [ 5 , 3 , 8 , 6 ] ] ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT updateArray ( A [ i ] [ 0 ] , A [ i ] [ 1 ] , A [ i ] [ 2 ] , A [ i ] [ 3 ] ) ; NEW_LINE DEDENT area = findAreaCovered ( ) ; NEW_LINE print ( area ) NEW_LINE DEDENT
Find the equation of plane which passes through two points and parallel to a given axis | Python3 implementation to find the equation of plane which passes through two points and parallel to a given axis ; Find direction vector of points ( x1 , y1 , z1 ) and ( x2 , y2 , z2 ) ; Values that are calculated and simplified from the cross product ; Print the equation of plane ; Driver Code ; Point A ; Point B ; Given axis ; Function Call
def findEquation ( x1 , y1 , z1 , x2 , y2 , z2 , d , e , f ) : NEW_LINE INDENT a = x2 - x1 NEW_LINE b = y2 - y1 NEW_LINE c = z2 - z1 NEW_LINE A = ( b * f - c * e ) NEW_LINE B = ( a * f - c * d ) NEW_LINE C = ( a * e - b * d ) NEW_LINE D = - ( A * d - B * e + C * f ) NEW_LINE print ( A , " x ▁ + ▁ " , B , " y ▁ + ▁ " , C , " z ▁ + ▁ " , D , " = ▁ 0" ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x1 = 2 NEW_LINE y1 = 3 NEW_LINE z1 = 5 ; NEW_LINE x2 = 6 NEW_LINE y2 = 7 NEW_LINE z2 = 8 NEW_LINE a = 11 NEW_LINE b = 23 NEW_LINE c = 10 NEW_LINE findEquation ( x1 , y1 , z1 , x2 , y2 , z2 , a , b , c ) NEW_LINE DEDENT
Find the length of the median of a Triangle if length of sides are given | Python3 implementation to Find the length of the median using sides of the triangle ; Function to return the length of the median using sides of triangle . ; Driver Code ; Function Call ; Print the final answer
import math NEW_LINE def median ( a , b , c ) : NEW_LINE INDENT n = ( 1 / 2 ) * math . sqrt ( 2 * ( b ** 2 ) + 2 * ( c ** 2 ) - a ** 2 ) NEW_LINE return n NEW_LINE DEDENT a = 4 NEW_LINE b = 3 NEW_LINE c = 5 NEW_LINE ans = median ( a , b , c ) NEW_LINE print ( round ( ans , 2 ) ) NEW_LINE
Number of quadrilateral formed with N distinct points on circumference of Circle | Function to find the number of combinations in the N ; Function to find the factorial of the given number N ; Loop to find the factorial of the given number ; Driver Code ; Function Call
def nCr ( n , r ) : NEW_LINE INDENT return ( fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ) NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE print ( int ( nCr ( n , 4 ) ) ) NEW_LINE DEDENT
Length of remaining two sides of a Triangle from a given side and its adjacent angles | Python3 program for above approach ; Function for computing other 2 side of the trianlgle ; computing angle C ; converting A in to radian ; converting B in to radian ; converting C in to radian ; computing length of side b ; computing length of side c ; driver program ; calling function
import math NEW_LINE def findSide ( a , B , C ) : NEW_LINE INDENT A = 180 - C - B NEW_LINE radA = math . pi * ( A / 180 ) NEW_LINE radB = math . pi * ( B / 180 ) NEW_LINE radC = math . pi * ( C / 180 ) NEW_LINE b = a / math . sin ( radA ) * math . sin ( radB ) NEW_LINE c = a / math . sin ( radA ) * math . sin ( radC ) NEW_LINE return b , c NEW_LINE DEDENT a = 12 NEW_LINE B = 60 NEW_LINE C = 30 NEW_LINE b , c = findSide ( a , B , C ) NEW_LINE print ( b , c ) NEW_LINE
Minimum area of square holding two identical rectangles | Function to find the area of the square ; Larger side of rectangle ; Smaller side of the rectangle ; Driver code
def areaSquare ( L , B ) : NEW_LINE INDENT large = max ( L , B ) NEW_LINE small = min ( L , B ) NEW_LINE if ( large >= 2 * small ) : NEW_LINE INDENT return large * large NEW_LINE DEDENT else : NEW_LINE INDENT return ( 2 * small ) * ( 2 * small ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 7 NEW_LINE B = 4 NEW_LINE print ( areaSquare ( L , B ) ) NEW_LINE DEDENT
Minimum side of square embedded in Regular polygon with N sides | Python 3 program to find the minimum side of the square in which a regular polygon with even sides can completely embed ; PI value in Python 3 using acos function ; Function to find the minimum side of the square in which a regular polygon with even sides can completely embed ; Projection angle variation from axes ; Projection angle variation when the number of sides are in multiple of 4 ; Distance between the end points ; Projection from all N points on X - axis ; Projection from all N points on Y - axis ; Maximum side ; Return the portion of side forming the square ; Driver code
import math NEW_LINE pi = math . acos ( - 1.0 ) NEW_LINE def nGon ( N ) : NEW_LINE INDENT proAngleVar = 0 NEW_LINE if ( N % 4 == 0 ) : NEW_LINE INDENT proAngleVar = ( pi * ( 180.0 / N ) / 180 ) NEW_LINE DEDENT else : NEW_LINE INDENT proAngleVar = ( pi * ( 180.0 / ( 2 * N ) ) / 180 ) NEW_LINE DEDENT negX = 1.0e+99 NEW_LINE posX = - 1.0e+99 NEW_LINE negY = 1.0e+99 NEW_LINE posY = - 1.0e+99 NEW_LINE for j in range ( N ) : NEW_LINE INDENT px = math . cos ( 2 * pi * j / N + proAngleVar ) NEW_LINE py = math . sin ( 2 * pi * j / N + proAngleVar ) NEW_LINE negX = min ( negX , px ) NEW_LINE posX = max ( posX , px ) NEW_LINE negY = min ( negY , py ) NEW_LINE posY = max ( posY , py ) NEW_LINE DEDENT opt2 = max ( posX - negX , posY - negY ) NEW_LINE return ( opt2 / math . sin ( pi / N ) / 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE print ( ' % .5f ' % nGon ( N ) ) NEW_LINE DEDENT
Heptacontagon Number | Finding the nth heptacontagon Number ; Driver code
def heptacontagonNum ( n ) : NEW_LINE INDENT return ( 68 * n * n - 66 * n ) // 2 ; NEW_LINE DEDENT N = 3 ; NEW_LINE print ( "3rd ▁ heptacontagon ▁ Number ▁ is ▁ = " , heptacontagonNum ( N ) ) ; NEW_LINE
Program to check if N is a Icositetragonal number | Python3 implementation to check that a number is icositetragonal number or not ; Function to check that the number is a icositetragonal number ; Condition to check if the number is a icositetragonal number ; Driver Code ; Function call
import math NEW_LINE def isicositetragonal ( N ) : NEW_LINE INDENT n = ( 10 + math . sqrt ( 44 * N + 100 ) ) / 22 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT i = 24 NEW_LINE if ( isicositetragonal ( i ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to check if N is a Tridecagonal Number or not | Python3 implementation to check that a number is a tridecagon number or not ; Function to check that the number is a tridecagon number ; Condition to check if the number is a tridecagon number ; Driver Code ; Function call
import math NEW_LINE def isTridecagon ( N ) : NEW_LINE INDENT n = ( 9 + math . sqrt ( 88 * N + 81 ) ) / 22 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT i = 13 NEW_LINE if ( isTridecagon ( i ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to check if N is a Icosihenagonal number | Python3 implementation to check that a number is icosihenagonal number or not ; Function to check that the number is a icosihenagonal number ; Condition to check if the number is a icosihenagonal number ; Driver Code ; Function call
import math NEW_LINE def isicosihenagonal ( N ) : NEW_LINE INDENT n = ( 17 + math . sqrt ( 152 * N + 289 ) ) / 38 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT i = 21 NEW_LINE if isicosihenagonal ( i ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to check if N is a Icositrigonal number | Python3 implementation to check that a number is a icositrigonal number or not ; Function to check that the number is a icositrigonal number ; Condition to check if the number is a icositrigonal number ; Driver Code ; Function call
import math NEW_LINE def isicositrigonal ( N ) : NEW_LINE INDENT n = ( 19 + math . sqrt ( 168 * N + 361 ) ) / 42 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT i = 23 NEW_LINE if ( isicositrigonal ( i ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check whether triangle is valid or not if three points are given | Function to check if three points make a triangle ; Calculation the area of triangle . We have skipped multiplication with 0.5 to avoid floating point computations ; Condition to check if area is not equal to 0 ; Driver code
def checkTriangle ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT a = ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) NEW_LINE if a == 0 : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ( x1 , x2 , x3 ) = ( 1 , 2 , 3 ) NEW_LINE ( y1 , y2 , y3 ) = ( 1 , 2 , 3 ) NEW_LINE checkTriangle ( x1 , y1 , x2 , y2 , x3 , y3 ) NEW_LINE DEDENT
Total number of triplets ( A , B , C ) in which the points B and C are Equidistant to A | Function to count such triplets ; Iterate over all the points ; Iterate over all points other than the current point ; Compute squared euclidean distance for the current point ; Compute nP2 that is n * ( n - 1 ) ; Return the final result ; Driver code
def numTrip ( points ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( len ( points ) ) : NEW_LINE INDENT map = { } NEW_LINE for j in range ( len ( points ) ) : NEW_LINE INDENT if ( j == i ) : NEW_LINE INDENT continue NEW_LINE DEDENT dy = points [ i ] [ 1 ] - points [ j ] [ 1 ] NEW_LINE dx = points [ i ] [ 0 ] - points [ j ] [ 0 ] NEW_LINE key = dy * dy NEW_LINE key += dx * dx NEW_LINE map [ key ] = map . get ( key , 0 ) + 1 NEW_LINE DEDENT for p in map : NEW_LINE INDENT res += map [ p ] * ( map [ p ] - 1 ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 0 , 0 ] , [ 1 , 0 ] , [ 2 , 0 ] ] NEW_LINE print ( numTrip ( mat ) ) NEW_LINE DEDENT
Check if any point overlaps the given Circle and Rectangle | Function to check if any point overlaps the given Circle and Rectangle ; Find the nearest point on the rectangle to the center of the circle ; Find the distance between the nearest point and the center of the circle Distance between 2 points , ( x1 , y1 ) & ( x2 , y2 ) in 2D Euclidean space is ( ( x1 - x2 ) * * 2 + ( y1 - y2 ) * * 2 ) * * 0.5 ; Driver code
def checkOverlap ( R , Xc , Yc , X1 , Y1 , X2 , Y2 ) : NEW_LINE INDENT Xn = max ( X1 , min ( Xc , X2 ) ) NEW_LINE Yn = max ( Y1 , min ( Yc , Y2 ) ) NEW_LINE Dx = Xn - Xc NEW_LINE Dy = Yn - Yc NEW_LINE return ( Dx ** 2 + Dy ** 2 ) <= R ** 2 NEW_LINE DEDENT if ( __name__ == " _ _ main _ _ " ) : NEW_LINE INDENT R = 1 NEW_LINE Xc , Yc = 0 , 0 NEW_LINE X1 , Y1 = 1 , - 1 NEW_LINE X2 , Y2 = 3 , 1 NEW_LINE print ( checkOverlap ( R , Xc , Yc , X1 , Y1 , X2 , Y2 ) ) NEW_LINE DEDENT
Check if the tower of sight issue occurs or not | Function to check if point p lies in between the line joining p1 and p2 ; If parallel to X - axis ; Point p lies between p1 and p2 ; If parallel to Y - axis ; Point p lies between p1 and p2 ; If point p satisfies the equation of line joining p1 and p2 ; Function to check if tower of sight issue occurred ; B lies between AC ; D lies between AC ; A lies between BD ; C lies between BD ; Driver code ; Point A ; Point B ; Point C ; Point D
def checkIntersection ( p1 , p2 , p ) : NEW_LINE INDENT if ( p1 [ 1 ] == p2 [ 1 ] and p1 [ 1 ] == p [ 1 ] ) : NEW_LINE INDENT if ( p [ 0 ] <= max ( p1 [ 0 ] , p2 [ 0 ] ) and ( p [ 0 ] >= min ( p1 [ 0 ] , p2 [ 0 ] ) ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if ( p1 [ 0 ] == p2 [ 0 ] and p1 [ 0 ] == p [ 0 ] ) : NEW_LINE INDENT if ( p [ 1 ] <= max ( p1 [ 1 ] , p2 [ 1 ] ) and ( p [ 1 ] >= min ( p1 [ 1 ] , p2 [ 1 ] ) ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT val = ( ( p [ 1 ] - p1 [ 1 ] ) * ( p2 [ 0 ] - p1 [ 0 ] ) - ( p [ 0 ] - p1 [ 0 ] ) * ( p2 [ 1 ] - p1 [ 1 ] ) ) NEW_LINE if ( val == 0 ) : NEW_LINE INDENT if ( ( p [ 0 ] <= max ( p1 [ 0 ] , p2 [ 0 ] ) and ( p [ 0 ] >= min ( p1 [ 0 ] , p2 [ 0 ] ) ) ) and ( p [ 1 ] <= max ( p1 [ 1 ] , p2 [ 1 ] ) and ( p [ 1 ] >= min ( p1 [ 1 ] , p2 [ 1 ] ) ) ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT DEDENT return 0 NEW_LINE DEDENT def towerOfSight ( a , b , c , d ) : NEW_LINE INDENT flag = 0 NEW_LINE if ( checkIntersection ( a , c , b ) ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT elif ( checkIntersection ( a , c , d ) ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT elif ( checkIntersection ( b , d , a ) ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT elif ( checkIntersection ( b , d , c ) ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT if ( flag ) : 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 = [ 0 , 0 ] NEW_LINE b = [ 0 , - 2 ] NEW_LINE c = [ 2 , 0 ] NEW_LINE d = [ 0 , 2 ] NEW_LINE towerOfSight ( a , b , c , d ) NEW_LINE DEDENT
Number of lines from given N points not parallel to X or Y axis | Function to find the number of lines which are formed from given N points and not parallel to X or Y axis ; This will store the number of points has same x or y coordinates using the map as the value of coordinate can be very large ; Counting frequency of each x and y coordinates ; Total number of pairs can be formed ; We can not choose pairs from these as they have same x coordinatethus they will result line segment parallel to y axis ; we can not choose pairs from these as they have same y coordinate thus they will result line segment parallel to x - axis ; Return the required answer ; Driver Code ; Function call
def NotParallel ( p , n ) : NEW_LINE INDENT x_axis = { } ; y_axis = { } ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if p [ i ] [ 0 ] not in x_axis : NEW_LINE INDENT x_axis [ p [ i ] [ 0 ] ] = 0 ; NEW_LINE DEDENT x_axis [ p [ i ] [ 0 ] ] += 1 ; NEW_LINE if p [ i ] [ 1 ] not in y_axis : NEW_LINE INDENT y_axis [ p [ i ] [ 1 ] ] = 0 ; NEW_LINE DEDENT y_axis [ p [ i ] [ 1 ] ] += 1 ; NEW_LINE DEDENT total = ( n * ( n - 1 ) ) // 2 ; NEW_LINE for i in x_axis : NEW_LINE INDENT c = x_axis [ i ] ; NEW_LINE total -= ( c * ( c - 1 ) ) // 2 ; NEW_LINE DEDENT for i in y_axis : NEW_LINE INDENT c = y_axis [ i ] ; NEW_LINE total -= ( c * ( c - 1 ) ) // 2 ; NEW_LINE DEDENT return total ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = [ [ 1 , 2 ] , [ 1 , 5 ] , [ 1 , 15 ] , [ 2 , 10 ] ] ; NEW_LINE n = len ( p ) ; NEW_LINE print ( NotParallel ( p , n ) ) ; NEW_LINE DEDENT
Check whether two convex regular polygon have same center or not | Function to check whether two convex polygons have the same center or not ; Driver Code
def check ( n , m ) : NEW_LINE INDENT if ( m % n == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE m = 10 NEW_LINE check ( n , m ) NEW_LINE
Modulus of a Complex Number | Python 3 program to find the Modulus of a Complex Number ; Function to find modulus of a complex number ; Storing the index of '+ ; Storing the index of '- ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code
from math import sqrt NEW_LINE def findModulo ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE modulus = 0 NEW_LINE DEDENT ' NEW_LINE INDENT if ( ' + ' in s ) : NEW_LINE INDENT i = s . index ( ' + ' ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT else : NEW_LINE INDENT i = s . index ( ' - ' ) NEW_LINE DEDENT real = s [ 0 : i ] NEW_LINE imaginary = s [ i + 1 : l - 1 ] NEW_LINE x = int ( real ) NEW_LINE y = int ( imaginary ) NEW_LINE print ( int ( sqrt ( x * x + y * y ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "3 + 4i " NEW_LINE findModulo ( s ) NEW_LINE DEDENT
Minimum number of blocks required to form Hollow Rectangular Prism | Function to display output ; Function to return minimum no of layers required to form the hollow prism ; Function to calculate no of blocks required for each layer ; No of blocks required for each row ; Check for no of layers is minimum ; Driver function ; Length , width , height of each block ; Side of one wall ; height of each wall
def disp ( row_no , block ) : NEW_LINE INDENT print ( row_no * block ) NEW_LINE DEDENT def row ( ht , h ) : NEW_LINE INDENT return ht // h NEW_LINE DEDENT def calculate ( l , w , h , a , ht ) : NEW_LINE INDENT no_block = ( 4 * a ) // l NEW_LINE if ( h < w ) : NEW_LINE INDENT row_no = row ( ht , w ) NEW_LINE DEDENT else : NEW_LINE INDENT row_no = row ( ht , h ) NEW_LINE DEDENT disp ( row_no , no_block ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 50 NEW_LINE w = 20 NEW_LINE h = 35 NEW_LINE a = 700 NEW_LINE ht = 140 NEW_LINE calculate ( l , w , h , a , ht ) NEW_LINE DEDENT
Maximum area of rectangle inscribed in an equilateral triangle | Function to find the maximum area of the rectangle inscribed in an equilateral triangle of side S ; Maximum area of the rectangle inscribed in an equilateral triangle of side S ; Driver Code
def solve ( s ) : NEW_LINE INDENT area = ( 1.732 * s ** 2 ) / 8 NEW_LINE return area NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 14 NEW_LINE print ( solve ( n ) ) NEW_LINE DEDENT
Area of largest semicircle that can be drawn inside a square | Python3 program to find Area of semicircle in a square ; Function to find area of semicircle ; Driver code ; side of a square ; Call Function to find the area of semicircle
from math import sqrt NEW_LINE def find_Area ( a ) : NEW_LINE INDENT R = a * ( 2.0 - sqrt ( 2 ) ) ; NEW_LINE area = 3.14 * R * R / 2.0 ; NEW_LINE return area ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 4 ; NEW_LINE print ( " Area ▁ of ▁ semicircle ▁ = " , find_Area ( a ) ) ; NEW_LINE DEDENT
Count the number of times graph crosses X | Function to to count the number of times the graph crosses the x - axis . ; Iterate over the steps array ; Update the previous level and current level by value given in the steps array ; Condition to check that the graph crosses the origin . ; Driver Code
def times ( steps , n ) : NEW_LINE INDENT current_level = 0 NEW_LINE previous_level = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT previous_level = current_level NEW_LINE current_level = current_level + steps [ i ] NEW_LINE if ( ( previous_level < 0 and current_level >= 0 ) or ( previous_level > 0 and current_level <= 0 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT steps = [ 1 , - 1 , 0 , 0 , 1 , 1 , - 3 , 2 ] NEW_LINE n = len ( steps ) NEW_LINE print ( times ( steps , n ) ) NEW_LINE
Program to find X , Y and Z intercepts of a plane | Python program to find the X , Y and Z intercepts of a plane ; For finding the x - intercept put y = 0 and z = 0 ; For finding the y - intercept put x = 0 and z = 0 ; For finding the z - intercept put x = 0 and y = 0 ; For Finding value of A , B , C , D ; Calling the first created function ; Driver Code
def XandYandZintercept ( A , B , C , D ) : NEW_LINE INDENT x = - D / A NEW_LINE y = - D / B NEW_LINE z = - D / C NEW_LINE return [ x , y , z ] NEW_LINE DEDENT def equation_plane ( p , q , r ) : NEW_LINE INDENT x1 = p [ 0 ] NEW_LINE y1 = p [ 1 ] NEW_LINE z1 = p [ 2 ] NEW_LINE x2 = q [ 0 ] NEW_LINE y2 = q [ 1 ] NEW_LINE z2 = q [ 2 ] NEW_LINE x3 = r [ 0 ] NEW_LINE y3 = r [ 1 ] NEW_LINE z3 = r [ 2 ] NEW_LINE a1 = x2 - x1 NEW_LINE b1 = y2 - y1 NEW_LINE c1 = z2 - z1 NEW_LINE a2 = x3 - x1 NEW_LINE b2 = y3 - y1 NEW_LINE c2 = z3 - z1 NEW_LINE A = b1 * c2 - b2 * c1 NEW_LINE B = a2 * c1 - a1 * c2 NEW_LINE C = a1 * b2 - b1 * a2 NEW_LINE D = ( - A * x1 - B * y1 - C * z1 ) NEW_LINE print ( XandYandZintercept ( A , B , C , D ) ) NEW_LINE DEDENT x1 = - 1 NEW_LINE y1 = 2 NEW_LINE z1 = 1 NEW_LINE x2 = 0 NEW_LINE y2 = - 3 NEW_LINE z2 = 2 NEW_LINE x3 = 1 NEW_LINE y3 = 1 NEW_LINE z3 = - 4 NEW_LINE equation_plane ( ( x1 , y1 , z1 ) , ( x2 , y2 , z2 ) , ( x3 , y3 , z3 ) ) NEW_LINE
Percentage change in Hemisphere volume if radius is changed | Function to find the change in hemispheric volume ; Get the change in radius ; Calculate the change in hemispheric volume
def new_vol ( x ) : NEW_LINE INDENT if ( x > 0 ) : NEW_LINE INDENT print ( " % ▁ change ▁ in ▁ the ▁ volume ▁ of ▁ the ▁ hemisphere : ▁ " , pow ( x , 3 ) / 10000 + 3 * x + ( 3 * pow ( x , 2 ) ) / 100 , " % ▁ increase " ) NEW_LINE DEDENT elif ( x < 0 ) : NEW_LINE INDENT print ( " % ▁ change ▁ in ▁ the ▁ volume ▁ of ▁ the ▁ hemisphere : ▁ " , pow ( x , 3 ) / 10000 + 3 * x + ( 3 * pow ( x , 2 ) ) / 100 , " % ▁ decrease " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Volume ▁ remains ▁ the ▁ same . " ) NEW_LINE DEDENT DEDENT x = - 10.0 NEW_LINE new_vol ( x ) NEW_LINE
Find the integer points ( x , y ) with Manhattan distance atleast N | function to find all possible point ; Find all 4 corners of the square whose side length is n ; If n is even then the middle point of the square will be an integer , so we will take that point ; Driver Code ; Printing all possible points
def FindPoints ( n ) : NEW_LINE INDENT v = [ ] ; NEW_LINE v . append ( [ 0 , 0 ] ) ; NEW_LINE v . append ( [ 0 , n ] ) ; NEW_LINE v . append ( [ n , 0 ] ) ; NEW_LINE v . append ( [ n , n ] ) ; NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT v . append ( [ n // 2 , n // 2 ] ) ; NEW_LINE DEDENT return v ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 ; NEW_LINE v = FindPoints ( N ) ; NEW_LINE for element in v : NEW_LINE INDENT print ( " ( " , element [ 0 ] , " , " , element [ 1 ] , " ) " , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT
Find if the glass will be empty or not when the rate of drinking is given | Python3 implementation of the approach ; Function to return the time when the glass will be empty ; Check the condition when the glass will never be empty ; Find the time ; Driver code
pie = 3.1415926535897 NEW_LINE def findsolution ( d , h , m , n ) : NEW_LINE INDENT k = ( 4 * m ) / ( pie * d * d ) NEW_LINE if ( n > k ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT ans = ( h / ( k - n ) ) NEW_LINE return round ( ans , 5 ) NEW_LINE DEDENT d = 1 NEW_LINE h = 1 NEW_LINE m = 1 NEW_LINE n = 1 NEW_LINE print ( findsolution ( d , h , m , n ) ) NEW_LINE
Sum of internal angles of a Polygon | Function to return the sum of internal angles of an n - sided polygon ; Driver code
def sumOfInternalAngles ( n ) : NEW_LINE INDENT if ( n < 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( ( n - 2 ) * 180 ) NEW_LINE DEDENT n = 5 NEW_LINE print ( sumOfInternalAngles ( n ) ) NEW_LINE
Number of ways N can be divided into four parts to construct a rectangle | Function to return the required number of ways ; Driver code
def cntWays ( n ) : NEW_LINE INDENT if n % 2 == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return ( n - 2 ) // 4 NEW_LINE DEDENT DEDENT n = 18 NEW_LINE print ( cntWays ( n ) ) NEW_LINE
Forming triangles using points on a square | Function to return the count of possible triangles ; Driver code
def noOfTriangles ( n ) : NEW_LINE INDENT y = 4 * n NEW_LINE return ( ( y * ( y - 2 ) * ( y - 1 ) ) - ( 4 * n * ( n - 2 ) * ( n - 1 ) ) ) // 6 NEW_LINE DEDENT n = 1 NEW_LINE print ( noOfTriangles ( n ) ) NEW_LINE
Angle subtended by an arc at the centre of a circle | Function to find Angle subtended by an arc at the centre of a circle ; Driver code
def angle ( n ) : NEW_LINE INDENT return 2 * n NEW_LINE DEDENT n = 30 NEW_LINE print ( angle ( n ) ) NEW_LINE
Check if two given Circles are Orthogonal or not | Function to Check if the given circles are orthogonal ; calculating the square of the distance between C1 and C2 ; Check if the given circles are orthogonal ; Driver code
def orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) : NEW_LINE INDENT dsquare = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; NEW_LINE if ( dsquare == r1 * r1 + r2 * r2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT x1 , y1 = 4 , 3 NEW_LINE x2 , y2 = 0 , 1 NEW_LINE r1 , r2 = 2 , 4 NEW_LINE f = orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) NEW_LINE if ( f ) : NEW_LINE INDENT print ( " Given ▁ circles ▁ are ▁ orthogonal . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Given ▁ circles ▁ are ▁ not ▁ orthogonal . " ) NEW_LINE DEDENT
Check if a right | Storing all the possible changes to make the triangle right - angled ; Function to check if the triangle is right - angled or not ; Function to check if the triangle can be transformed to right - angled ; Boolean variable to return true or false ; If it is already right - angled ; Applying the changes on the co - ordinates ; Applying on the first co - ordinate ; Applying on the second co - ordinate ; Applying on the third co - ordinate ; If can 't be transformed ; Driver Code
dx = [ - 1 , 0 , 1 , 0 ] NEW_LINE dy = [ 0 , 1 , 0 , - 1 ] NEW_LINE def ifRight ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT a = ( ( x1 - x2 ) * ( x1 - x2 ) ) + ( ( y1 - y2 ) * ( y1 - y2 ) ) NEW_LINE b = ( ( x1 - x3 ) * ( x1 - x3 ) ) + ( ( y1 - y3 ) * ( y1 - y3 ) ) NEW_LINE c = ( ( x2 - x3 ) * ( x2 - x3 ) ) + ( ( y2 - y3 ) * ( y2 - y3 ) ) NEW_LINE if ( ( a == ( b + c ) and a != 0 and b != 0 and c != 0 ) or ( b == ( a + c ) and a != 0 and b != 0 and c != 0 ) or ( c == ( a + b ) and a != 0 and b != 0 and c != 0 ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT def isValidCombination ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT x , y = 0 , 0 NEW_LINE possible = 0 NEW_LINE if ( ifRight ( x1 , y1 , x2 , y2 , x3 , y3 ) ) : NEW_LINE INDENT print ( " ALREADY ▁ RIGHT ▁ ANGLED " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 4 ) : NEW_LINE INDENT x = dx [ i ] + x1 NEW_LINE y = dy [ i ] + y1 NEW_LINE if ( ifRight ( x , y , x2 , y2 , x3 , y3 ) ) : NEW_LINE INDENT print ( " POSSIBLE " ) NEW_LINE return NEW_LINE DEDENT x = dx [ i ] + x2 NEW_LINE y = dy [ i ] + y2 NEW_LINE if ( ifRight ( x1 , y1 , x , y , x3 , y3 ) ) : NEW_LINE INDENT print ( " POSSIBLE " ) NEW_LINE return NEW_LINE DEDENT x = dx [ i ] + x3 NEW_LINE y = dy [ i ] + y3 NEW_LINE if ( ifRight ( x1 , y1 , x2 , y2 , x , y ) ) : NEW_LINE INDENT print ( " POSSIBLE " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT if ( possible == 0 ) : NEW_LINE INDENT print ( " NOT ▁ POSSIBLE " ) NEW_LINE DEDENT DEDENT x1 = - 49 NEW_LINE y1 = 0 NEW_LINE x2 = 0 NEW_LINE y2 = 50 NEW_LINE x3 = 0 NEW_LINE y3 = - 50 NEW_LINE isValidCombination ( x1 , y1 , x2 , y2 , x3 , y3 ) NEW_LINE
Program to find Area of Triangle inscribed in N | Python3 Program to find the area of a triangle inscribed in N - sided regular polygon ; Function to find the area of the polygon ; area of a regular polygon with N sides and side length len ; Function to find the area of a triangle ; area of one triangle in an N - sided regular polygon ; area of inscribed triangle ; Driver code
import math NEW_LINE def area_of_regular_polygon ( n , len ) : NEW_LINE INDENT P = ( len * n ) ; NEW_LINE A = len / ( 2 * math . tan ( ( 180 / n ) * 3.14159 / 180 ) ) NEW_LINE area = ( P * A ) / 2 NEW_LINE return area NEW_LINE DEDENT def area_of_triangle_inscribed ( n , len ) : NEW_LINE INDENT area = area_of_regular_polygon ( n , len ) NEW_LINE triangle = area / n NEW_LINE ins_tri = ( triangle * 3 ) ; NEW_LINE return ins_tri NEW_LINE DEDENT n = 6 NEW_LINE len = 10 NEW_LINE print ( round ( area_of_triangle_inscribed ( n , len ) , 3 ) ) NEW_LINE
Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given | Function to find the area of quadrilateral ; Driver code
def Area ( d , h1 , h2 ) : NEW_LINE INDENT area = 0.5 * d * ( h1 + h2 ) ; NEW_LINE return area ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT d = 6 ; NEW_LINE h1 = 4 ; NEW_LINE h2 = 3 ; NEW_LINE print ( " Area ▁ of ▁ Quadrilateral ▁ = ▁ " , ( Area ( d , h1 , h2 ) ) ) ; NEW_LINE DEDENT
Find the diagonal of the Cube | Python3 program to find length of the diagonal of the cube ; Function to find length of diagonal of cube ; Formula to Find length of diagonal of cube ; Driver code ; Function call
from math import sqrt NEW_LINE def diagonal_length ( a ) : NEW_LINE INDENT L = 0 NEW_LINE L = a * sqrt ( 3 ) NEW_LINE return L NEW_LINE DEDENT a = 5 NEW_LINE print ( diagonal_length ( a ) ) NEW_LINE
Concentric Hexagonal Numbers | Function to find nth concentric hexagon number ; Driver code ; Function call
def concentric_Hexagon ( n ) : NEW_LINE INDENT return 3 * pow ( n , 2 ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( concentric_Hexagon ( n ) ) NEW_LINE
Find area of the larger circle when radius of the smaller circle and difference in the area is given | Python 3 implementation of the approach ; Function to return the area of the bigger circle ; Find the radius of the bigger circle ; Calculate the area of the bigger circle ; Driver code
PI = 3.14 NEW_LINE from math import pow , sqrt NEW_LINE def find_area ( r , d ) : NEW_LINE INDENT R = d / PI NEW_LINE R += pow ( r , 2 ) NEW_LINE R = sqrt ( R ) NEW_LINE area = PI * pow ( R , 2 ) NEW_LINE return area NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r = 4 NEW_LINE d = 5 NEW_LINE print ( find_area ( r , d ) ) NEW_LINE DEDENT
Check whether Quadrilateral is valid or not if angles are given | Function to check if a given quadrilateral is valid or not ; Check condition ; Driver code
def Valid ( a , b , c , d ) : NEW_LINE INDENT if ( a + b + c + d == 360 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT a = 80 ; b = 70 ; c = 100 ; d = 110 ; NEW_LINE if ( Valid ( a , b , c , d ) ) : NEW_LINE INDENT print ( " Valid ▁ quadrilateral " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid ▁ quadrilateral " ) ; NEW_LINE DEDENT
Central angle of a N sided Regular Polygon | Function to calculate central angle of a polygon ; Calculate the angle ; Driver code
def calculate_angle ( n ) : NEW_LINE INDENT total_angle = 360 ; NEW_LINE return ( total_angle // n ) NEW_LINE DEDENT N = 5 NEW_LINE print ( calculate_angle ( N ) ) NEW_LINE
Program to calculate the area of Kite | Function to return the area of kite ; use above formula ; Driver code
def areaOfKite ( d1 , d2 ) : NEW_LINE INDENT area = ( d1 * d2 ) / 2 ; NEW_LINE return area ; NEW_LINE DEDENT d1 = 4 ; NEW_LINE d2 = 6 ; NEW_LINE print ( " Area ▁ of ▁ Kite ▁ = ▁ " , areaOfKite ( d1 , d2 ) ) ; NEW_LINE
Program to calculate the area of Kite | Python implementation of the approach ; Function to return the area of the kite ; convert angle degree to radians ; use above formula ; Driver code
import math NEW_LINE PI = 3.14159 / 180 ; NEW_LINE def areaOfKite ( a , b , angle ) : NEW_LINE INDENT angle = angle * PI ; NEW_LINE area = a * b * math . sin ( angle ) ; NEW_LINE return area ; NEW_LINE DEDENT a = 4 ; b = 7 ; angle = 78 ; NEW_LINE print ( " Area ▁ of ▁ Kite ▁ = ▁ " , areaOfKite ( a , b , angle ) ) ; NEW_LINE
Program to calculate angle on circumference subtended by the chord when the central angle subtended by the chord is given | Python3 Program to calculate angle on the circumference subtended by the chord when the central angle subtended by the chord is given ; Angle on center
def angleOncirCumference ( z ) : NEW_LINE INDENT return ( z / 2 ) ; NEW_LINE DEDENT angle = 65 ; NEW_LINE z = angleOncirCumference ( angle ) ; NEW_LINE print ( " The ▁ angle ▁ is " , ( z ) , " degrees " ) ; NEW_LINE
Maximum and Minimum value of a quadratic function | Function to print the Maximum and Minimum values of the quadratic function ; Calculate the value of second part ; Print the values ; Open upward parabola function ; Open downward parabola function ; If a = 0 then it is not a quadratic function ; Driver code
def PrintMaxMinValue ( a , b , c ) : NEW_LINE INDENT secondPart = c * 1.0 - ( b * b / ( 4.0 * a ) ) ; NEW_LINE if ( a > 0 ) : NEW_LINE INDENT print ( " Maxvalue ▁ = " , " Infinity " ) ; NEW_LINE print ( " Minvalue ▁ = ▁ " , secondPart ) ; NEW_LINE DEDENT elif ( a < 0 ) : NEW_LINE INDENT print ( " Maxvalue ▁ = ▁ " , secondPart ) ; NEW_LINE print ( " Minvalue ▁ = " , " - Infinity " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ quadratic ▁ function " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = - 1 ; b = 3 ; c = - 2 ; NEW_LINE PrintMaxMinValue ( a , b , c ) ; NEW_LINE DEDENT
Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant | Python3 program to find percentage increase in the cylinder if the height is increased by given percentage but radius remains constant ; Driver code
def newvol ( x ) : NEW_LINE INDENT print ( " percentage ▁ increase ▁ in ▁ the ▁ volume ▁ of ▁ the ▁ cylinder ▁ is ▁ " , x , " % " ) NEW_LINE DEDENT x = 10.0 NEW_LINE newvol ( x ) NEW_LINE
Find the side of the squares which are inclined diagonally and lined in a row | Python program to find side of the squares inclined and touch each other externally at vertices and are lined in a row and distance between the centers of first and last squares is given ; Driver code
def radius ( n , d ) : NEW_LINE INDENT print ( " The ▁ side ▁ of ▁ each ▁ square ▁ is ▁ " , d / ( ( n - 1 ) * ( 2 ** ( 1 / 2 ) ) ) ) ; NEW_LINE DEDENT d = 42 ; n = 4 ; NEW_LINE radius ( n , d ) ; NEW_LINE
Program to calculate area of inner circle which passes through center of outer circle and touches its circumference | Function calculate the area of the inner circle ; the radius cannot be negative ; area of the circle ; Driver Code
def innerCirclearea ( radius ) : NEW_LINE INDENT if ( radius < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT r = radius / 2 ; NEW_LINE Area = ( 3.14 * pow ( r , 2 ) ) ; NEW_LINE return Area ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT radius = 4 ; NEW_LINE print ( " Area ▁ of ▁ circle ▁ c2 ▁ = " , innerCirclearea ( radius ) ) ; NEW_LINE DEDENT
Angle subtended by the chord when the angle subtended by another chord of same length is given | Python3 program to find the angle subtended at the center by the chord when the angle subtended at center by another chord of equal length is given ; Driver code
def angleequichord ( z ) : NEW_LINE INDENT print ( " The ▁ angle ▁ subtended ▁ at " , " the ▁ center ▁ is " , z , " degrees " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT z = 48 ; NEW_LINE angleequichord ( z ) ; NEW_LINE DEDENT
Distance of chord from center when distance between center and another equal length chord is given | Python 3 program to find the distance of chord from center when distance between center and another equal length chord is given ; Driver code
def lengequichord ( z ) : NEW_LINE INDENT print ( " The ▁ distance ▁ between ▁ the " , " chord ▁ and ▁ the ▁ center ▁ is " , z ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT z = 48 NEW_LINE lengequichord ( z ) NEW_LINE DEDENT
Length of the perpendicular bisector of the line joining the centers of two circles | Python program to find the Length of the perpendicular bisector of the line joining the centers of two circles in which one lies completely inside touching the bigger circle at one point ; Driver code
def lengperpbisect ( r1 , r2 ) : NEW_LINE INDENT z = 2 * ( ( ( r1 * r1 ) - ( ( r1 - r2 ) * ( r1 - r2 ) / 4 ) ) ** ( 1 / 2 ) ) ; NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ perpendicular ▁ bisector ▁ is ▁ " , z ) ; NEW_LINE DEDENT r1 = 5 ; r2 = 3 ; NEW_LINE lengperpbisect ( r1 , r2 ) ; NEW_LINE
Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given | Python3 program to find the length of the chord of the circle if length of the other chord which is equally inclined through the diameter is given ; Driver code
def lengchord ( z ) : NEW_LINE INDENT print ( " The ▁ length ▁ is ▁ " , end = " " ) ; NEW_LINE print ( z ) ; NEW_LINE DEDENT z = 48 ; NEW_LINE lengchord ( z ) ; NEW_LINE
Distance between centers of two intersecting circles if the radii and common chord length is given | Python program to find the distance between centers of two intersecting circles if the radii and common chord length is given ; Driver code
def distcenter ( r1 , r2 , x ) : NEW_LINE INDENT z = ( ( ( r1 * r1 ) - ( x / 2 * x / 2 ) ) ** ( 1 / 2 ) ) + ( ( ( r2 * r2 ) - ( x / 2 * x / 2 ) ) ** ( 1 / 2 ) ) ; NEW_LINE print ( " distance ▁ between ▁ thecenters ▁ is ▁ " , end = " " ) ; NEW_LINE print ( int ( z ) ) ; NEW_LINE DEDENT r1 = 24 ; r2 = 37 ; x = 40 ; NEW_LINE distcenter ( r1 , r2 , x ) ; NEW_LINE
Exterior angle of a cyclic quadrilateral when the opposite interior angle is given | Python program to find the exterior angle of a cyclic quadrilateral when the opposite interior angle is given ; Driver code
def angleextcycquad ( z ) : NEW_LINE INDENT print ( " The ▁ exterior ▁ angle ▁ of ▁ the " , end = " " ) ; NEW_LINE print ( " cyclic ▁ quadrilateral ▁ is ▁ " , end = " " ) ; NEW_LINE print ( z , " ▁ degrees " ) ; NEW_LINE DEDENT z = 48 ; NEW_LINE angleextcycquad ( z ) ; NEW_LINE
Angle between a chord and a tangent when angle in the alternate segment is given | Python3 program to find the angle between a chord and a tangent when angle in the alternate segment is given ; Driver code
def anglechordtang ( z ) : NEW_LINE INDENT print ( " The ▁ angle ▁ between ▁ tangent " , " and ▁ the ▁ chord ▁ is " , z , " degrees " ) ; NEW_LINE DEDENT z = 48 ; NEW_LINE anglechordtang ( z ) ; NEW_LINE
Print all Perfect Numbers from an array whose sum of digits is also a Perfect Number | Function to check if a number is perfect number or not ; Stores sum of proper divisors ; If sum of digits is equal to N , then it 's a perfect number ; Otherwise , not a perfect number ; Function to find the sum of digits of a number ; Stores sum of digits ; Return sum of digits ; Function to count perfect numbers from an array whose sum of digits is also perfect ; Traverse the array ; If number is perfect ; Stores sum of digits of the number ; If that is also perfect number ; Print that number ; Driver Code ; Given array ; Size of the array ; Function call to count perfect numbers having sum of digits also perfect
def isPerfect ( N ) : NEW_LINE INDENT sumOfDivisors = 1 ; NEW_LINE for i in range ( 2 , int ( N / 2 ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT sumOfDivisors += i ; NEW_LINE DEDENT DEDENT if ( sumOfDivisors == N ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def sumOfDigits ( N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( N != 0 ) : NEW_LINE INDENT sum += ( N % 10 ) ; NEW_LINE N = N // 10 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def countPerfectNumbers ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if ( isPerfect ( arr [ i ] ) ) : NEW_LINE INDENT sum = sumOfDigits ( arr [ i ] ) ; NEW_LINE if ( isPerfect ( sum ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 8 , 12 , 28 , 6 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE countPerfectNumbers ( arr , N ) ; NEW_LINE DEDENT
Sum of Bitwise AND of the sum of all leaf and non | Structure of a Binary tree node ; Helper function to allocate a new node with the given data and left and right pointers as None ; Function to calculate the sum of bitwise AND of the sum of all leaf nodes and non - leaf nodes for each level ; Initialize a queue and append root to it ; Store the required answer ; Stores the sum of leaf nodes at the current level ; Stores the sum of non - leaf nodes at the current level ; Get the size of the queue ; Iterate for all the nodes in the queue currently ; Dequeue a node from queue ; Check if the node is a leaf node ; If true , update the leaf node sum ; Otherwise , update the non - leaf node sum ; Enqueue left and right children of removed node ; Update the answer ; Return the answer ; Given Tree ; Function Call
class TreeNode : NEW_LINE INDENT def __init__ ( self , val = 0 , left = None , right = None ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def findSum ( root ) : NEW_LINE INDENT que = [ root ] NEW_LINE ans = 0 NEW_LINE while ( len ( que ) ) : NEW_LINE INDENT leaf = 0 NEW_LINE nonleaf = 0 NEW_LINE length = len ( que ) NEW_LINE while length : NEW_LINE INDENT temp = que . pop ( 0 ) NEW_LINE if not temp . left and not temp . right : NEW_LINE INDENT leaf += temp . val NEW_LINE DEDENT else : NEW_LINE INDENT nonleaf += temp . val NEW_LINE DEDENT if temp . left : NEW_LINE INDENT que . append ( temp . left ) NEW_LINE DEDENT if temp . right : NEW_LINE INDENT que . append ( temp . right ) NEW_LINE DEDENT length -= 1 NEW_LINE DEDENT ans += leaf & nonleaf NEW_LINE DEDENT return ans NEW_LINE DEDENT root = TreeNode ( 5 ) NEW_LINE root . left = TreeNode ( 3 ) NEW_LINE root . right = TreeNode ( 9 ) NEW_LINE root . left . left = TreeNode ( 6 ) NEW_LINE root . left . right = TreeNode ( 4 ) NEW_LINE root . left . left . right = TreeNode ( 7 ) NEW_LINE print ( findSum ( root ) ) NEW_LINE
Minimize replacements to make every element in an array exceed every element in another given array | Function to find the minimize replacements to make every element in the array A [ ] strictly greater than every element in B [ ] or vice - versa ; Store the final result ; Create two arrays and initialize with 0 s ; Traverse the array a [ ] ; Increment prefix_a [ a [ i ] ] by 1 ; Traverse the array b [ ] ; Increment prefix_b [ b [ i ] ] by 1 ; Calculate prefix sum of the array a [ ] ; Calculate prefix sum of the array b [ ] ; Iterate over the range [ 0 , 9 ] ; Make every element in array a [ ] strictly greater than digit i and make every element in the array b [ ] less than digit i ; Make every element in array b [ ] strictly greater than digit i and make every element in array a [ ] less than digit i ; Print the answer ; Driver Code
def MinTime ( a , b , n , m ) : NEW_LINE INDENT ans = float ( ' inf ' ) NEW_LINE prefix_a = [ 0 ] * 10 NEW_LINE prefix_b = [ 0 ] * 10 NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix_a [ a [ i ] ] += 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT prefix_b [ b [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT prefix_a [ i ] += prefix_a [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT prefix_b [ i ] += prefix_b [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT ans = min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) NEW_LINE ans = min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT A = [ 0 , 0 , 1 , 3 , 3 ] NEW_LINE B = [ 2 , 0 , 3 ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE MinTime ( A , B , N , M ) NEW_LINE
Fizz Buzz Implementation | Set 2 | Function to generate FizzBuzz sequence ; Stores count of multiples of 3 and 5 respectively ; Iterate from 1 to N ; Increment count3 by 1 ; Increment count5 by 1 ; Initialize a boolean variable to check if none of the condition matches ; Check if the value of count3 is equal to 3 ; Reset count3 to 0 , and set flag as True ; Check if the value of count5 is equal to 5 ; Reset count5 to 0 , and set flag as True ; If none of the condition matches ; Driver Code
def fizzBuzz ( N ) : NEW_LINE INDENT count3 = 0 NEW_LINE count5 = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT count3 += 1 NEW_LINE count5 += 1 NEW_LINE flag = False NEW_LINE if ( count3 == 3 ) : NEW_LINE INDENT print ( " Fizz " , end = " " ) NEW_LINE count3 = 0 NEW_LINE flag = True NEW_LINE DEDENT if ( count5 == 5 ) : NEW_LINE INDENT print ( " Buzz " , end = " " ) NEW_LINE count5 = 0 NEW_LINE flag = True NEW_LINE DEDENT if ( not flag ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT print ( end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 NEW_LINE fizzBuzz ( N ) NEW_LINE DEDENT
Check if diagonal elements of a Matrix are Prime or not | Stores if a number is prime or not ; Function to generate and store primes using Sieve Of Eratosthenes ; Set all numbers as prime memset ( prime , true , sizeof ( prime ) ) ; If p is a prime ; Set all its multiples as non - prime ; Function to check if all diagonal elements are prime or not ; Stores if all diagonal elements are prime or not ; Precompute primes ; Traverse the range [ 0 , N - 1 ] ; Check if numbers on the cross diagonal and main diagonal are primes or not ; If true , then pr " Yes " ; Otherwise , pr " No " ; Driver Code ; Function Call
prime = [ True ] * 1000005 NEW_LINE def SieveOfEratosthenes ( N ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , N + 1 ) : NEW_LINE INDENT if p * p > N : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , N + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def checkElementsOnDiagonal ( M , N ) : NEW_LINE INDENT flag = 1 NEW_LINE SieveOfEratosthenes ( 1000000 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT flag &= ( prime [ M [ i ] [ i ] ] and prime [ M [ i ] [ N - 1 - i ] ] ) NEW_LINE DEDENT if ( flag ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = [ [ 1 , 2 , 3 , 13 ] , [ 5 , 3 , 7 , 8 ] , [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 7 ] ] NEW_LINE N = len ( M ) NEW_LINE checkElementsOnDiagonal ( M , N ) NEW_LINE DEDENT
Generate a circular permutation with number of mismatching bits between pairs of adjacent elements exactly 1 | Function to find the permutation of integers from a given range such that number of mismatching bits between pairs of adjacent elements is 1 ; Initialize an arrayList to store the resultant permutation ; Store the index of rotation ; Iterate over the range [ 0 , N - 1 ] ; Traverse all the array elements up to ( 2 ^ k ) - th index in reverse ; If current element is S ; Check if S is zero ; Rotate the array by index value to the left ; Driver Code
def circularPermutation ( n , start ) : NEW_LINE INDENT res = [ 0 ] NEW_LINE ret = [ ] NEW_LINE index , add = - 1 , 1 NEW_LINE for k in range ( n ) : NEW_LINE INDENT add = 1 << k NEW_LINE for i in range ( len ( res ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( res [ i ] + add == start ) : NEW_LINE INDENT index = len ( res ) NEW_LINE DEDENT res . append ( res [ i ] + add ) NEW_LINE DEDENT add = 1 << k NEW_LINE DEDENT if ( start == 0 ) : NEW_LINE INDENT return res NEW_LINE DEDENT while ( len ( ret ) < len ( res ) ) : NEW_LINE INDENT ret . append ( res [ index ] ) NEW_LINE index = ( index + 1 ) % len ( res ) NEW_LINE DEDENT return ret NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , S = 2 , 3 NEW_LINE print ( circularPermutation ( N , S ) ) NEW_LINE DEDENT
Count pairs from an array having equal sum and quotient | Function to count all pairs ( i , j ) such that a [ i ] + [ j ] = a [ i ] / a [ j ] ; Stores total count of pairs ; Generate all possible pairs ; If a valid pair is found ; Increment count ; Return the final count ; Driver Code
def countPairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( a [ j ] != 0 and a [ i ] % a [ j ] == 0 ) : NEW_LINE INDENT if ( ( a [ i ] + a [ j ] ) == ( a [ i ] // a [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 4 , - 3 , 0 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Count pairs from an array having equal sum and quotient | Function to find number of pairs with equal sum and quotient from a given array ; Store the count of pairs ; Stores frequencies ; Traverse the array ; If y is neither 1 or 0 ; Evaluate x ; Increment count by frequency of x ; Update map ; Prthe final count ; Driver Code ; Function Call
def countPairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT y = a [ i ] NEW_LINE if ( y != 0 and y != 1 ) : NEW_LINE INDENT x = ( ( ( y * 1.0 ) // ( 1 - y ) ) * y ) NEW_LINE count += mp . get ( x , 0 ) NEW_LINE DEDENT mp [ y ] = mp . get ( y , 0 ) + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ - 4 , - 3 , 0 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE
Queries to count numbers from a range which does not contain digit K in their decimal or octal representation | Function to check if the given digit ' K ' is present in the decimal and octal representations of num or not ; Stores if the digit exists or not ; Iterate till nums is non - zero ; Find the remainder ; If the remainder is K ; Function to count the numbers in the range [ 1 , N ] such that it doesn ' t ▁ contain ▁ the ▁ digit ▁ ' K ' in its decimal and octal representation ; Stores count of numbers in the range [ 0 , i ] that contains the digit ' K ' in its octal or decimal representation ; Traverse the range [ 0 , 1e6 + 5 ] ; Check if i contains the digit ' K ' in its decimal or octal representation ; Update pref [ i ] ; Print the answer of queries ; Driver Code ; Function Call
def contains ( num , K , base ) : NEW_LINE INDENT isThere = 0 NEW_LINE while ( num ) : NEW_LINE INDENT remainder = num % base NEW_LINE if ( remainder == K ) : NEW_LINE INDENT isThere = 1 NEW_LINE DEDENT num //= base NEW_LINE DEDENT return isThere NEW_LINE DEDENT def count ( n , k , v ) : NEW_LINE INDENT pref = [ 0 ] * 1000005 NEW_LINE for i in range ( 1 , 10 ** 6 + 5 ) : NEW_LINE INDENT present = contains ( i , k , 10 ) or contains ( i , k , 8 ) NEW_LINE pref [ i ] += pref [ i - 1 ] + present NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( v [ i ] [ 1 ] - v [ i ] [ 0 ] + 1 - ( pref [ v [ i ] [ 1 ] ] - pref [ v [ i ] [ 0 ] - 1 ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 7 NEW_LINE Q = [ [ 2 , 5 ] , [ 1 , 15 ] ] NEW_LINE N = len ( Q ) NEW_LINE count ( N , K , Q ) NEW_LINE DEDENT