codeparrot/xlcost-text-to-code huggingface dataset:
Check if it is possible to reach the point ( X , Y ) using distances given in an array | Python program for the above approach ; Function to check if the po ( X , Y ) is reachable from ( 0 , 0 ) or not ; Find the Euclidian Distance ; Calculate the maximum distance ; Case 1. ; Case 2. ; Otherwise , check for the polygon condition for each side ; Otherwise , prYes ; Driver Code
import math NEW_LINE def isPossibleToReach ( A , N , X , Y ) : NEW_LINE INDENT distance = math . sqrt ( X * X + Y * Y ) NEW_LINE mx = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT mx += A [ i ] NEW_LINE DEDENT if ( mx < distance ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return 0 NEW_LINE DEDENT if ( ( mx - distance ) < 0.000001 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE return 0 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( distance + mx < ( 2 ) * ( A [ i ] ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return 0 NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE return 0 NEW_LINE DEDENT A = [ 2 , 5 ] NEW_LINE X = 5 NEW_LINE Y = 4 NEW_LINE N = len ( A ) NEW_LINE isPossibleToReach ( A , N , X , Y ) NEW_LINE
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Termination Condition ; Otherwise , recursively call by decrementing 3 ^ i at each step ; Driver Code
def canReach ( X , Y , steps ) : NEW_LINE INDENT if ( X == 0 and Y == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( X < 0 or Y < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return ( canReach ( X - int ( pow ( 3 , steps ) ) , Y , steps + 1 ) | canReach ( X , Y - int ( pow ( 3 , steps ) ) , steps + 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 10 NEW_LINE Y = 30 NEW_LINE if ( canReach ( X , Y , 0 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Stores the number of steps performed to reach ( X , Y ) ; Value of X in base 3 ; Value of Y in base 3 ; Check if any has value 2 ; If both have value 1 ; If both have value 0 ; Otherwise , return true ; Driver Code
def canReach ( X , Y ) : NEW_LINE INDENT steps = 0 NEW_LINE while ( X != 0 or Y != 0 ) : NEW_LINE INDENT pos1 = X % 3 NEW_LINE pos2 = Y % 3 NEW_LINE if ( pos1 == 2 or pos2 == 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( pos1 == 1 and pos2 == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( pos1 == 0 and pos2 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT X /= 3 NEW_LINE Y /= 3 NEW_LINE steps += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT X = 10 NEW_LINE Y = 30 NEW_LINE if ( canReach ( X , Y ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Program to calculate Surface Area of Ellipsoid | Python3 program for the above approach ; Function to find the surface area of the given Ellipsoid ; Formula to find surface area of an Ellipsoid ; Print the area ; Driver Code
from math import pow NEW_LINE def findArea ( a , b , c ) : NEW_LINE INDENT area = ( 4 * 3.141592653 * pow ( ( pow ( a * b , 1.6 ) + pow ( a * c , 1.6 ) + pow ( b * c , 1.6 ) ) / 3 , 1 / 1.6 ) ) NEW_LINE print ( " { : . 2f } " . format ( round ( area , 2 ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 11 NEW_LINE B = 12 NEW_LINE C = 13 NEW_LINE findArea ( A , B , C ) NEW_LINE DEDENT
Descartes ' Circle Theorem with implementation | Python 3 implementation of the above formulae ; Function to find the fourth circle 's when three radius are given ; Driver code ; Radius of three circles ; Calculation of r4 using formula given above
from math import sqrt NEW_LINE def findRadius ( r1 , r2 , r3 ) : NEW_LINE INDENT r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) NEW_LINE return r4 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r1 = 1 NEW_LINE r2 = 1 NEW_LINE r3 = 1 NEW_LINE r4 = findRadius ( r1 , r2 , r3 ) NEW_LINE print ( " The ▁ radius ▁ of ▁ fourth ▁ circle : " , r4 ) NEW_LINE DEDENT
Make N pairs from Array as ( X , Y ) coordinate point that are enclosed inside a minimum area rectangle | Function to make N pairs of coordinates such that they are enclosed in a minimum area rectangle with sides parallel to the X and Y axes ; A variable to store the answer ; For the case where the maximum and minimum are in different partitions ; For the case where the maximum and minimum are in the same partition ; Return the answer ; Driver code ; Given Input ; Function call
def minimumRectangleArea ( A , N ) : NEW_LINE INDENT ans = 0 NEW_LINE A . sort ( ) NEW_LINE ans = ( A [ N - 1 ] - A [ 0 ] ) * ( A [ 2 * N - 1 ] - A [ N ] ) NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT ans = min ( ans , ( A [ 2 * N - 1 ] - A [ 0 ] ) * ( A [ i + N - 1 ] - A [ i ] ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 4 , 1 , 5 , 3 , 6 , 7 , 8 ] NEW_LINE N = len ( A ) NEW_LINE N //= 2 NEW_LINE print ( minimumRectangleArea ( A , N ) ) NEW_LINE DEDENT
Program to find the length of Latus Rectum of a Hyperbola | Function to calculate the length of the latus rectum of a hyperbola ; Store the length of major axis ; Store the length of minor axis ; Store the length of the latus rectum ; Return the length of the latus rectum ; Driver Code
def lengthOfLatusRectum ( A , B ) : NEW_LINE INDENT major = 2.0 * A NEW_LINE minor = 2.0 * B NEW_LINE latus_rectum = ( minor * minor ) / major NEW_LINE return latus_rectum NEW_LINE DEDENT A = 3.0 NEW_LINE B = 2.0 NEW_LINE print ( round ( lengthOfLatusRectum ( A , B ) , 5 ) ) NEW_LINE
Length of intercept cut off from a line by a Circle | Python3 program for the above approach ; Function to find the radius of a circle ; g and f are the coordinates of the center ; Case of invalid circle ; Apply the radius formula ; Function to find the perpendicular distance between circle center and the line ; Store the coordinates of center ; Stores the perpendicular distance between the line and the point ; Invalid Case ; Return the distance ; Function to find the length of intercept cut off from a line by a circle ; Calculate the value of radius ; Calculate the perpendicular distance between line and center ; Invalid Case ; If line do not cut circle ; Print the intercept length ; Driver Code ; Given Input ; Function Call
import math NEW_LINE def radius ( a , b , c ) : NEW_LINE INDENT g = a / 2 NEW_LINE f = b / 2 NEW_LINE if ( g * g + f * f - c < 0 ) : NEW_LINE INDENT return ( - 1 ) NEW_LINE DEDENT return ( math . sqrt ( g * g + f * f - c ) ) NEW_LINE DEDENT def centerDistanceFromLine ( a , b , i , j , k ) : NEW_LINE INDENT g = a / 2 NEW_LINE f = b / 2 NEW_LINE distance = ( abs ( i * g + j * f + k ) / ( math . sqrt ( i * i + j * j ) ) ) NEW_LINE if ( distance < 0 ) : NEW_LINE INDENT return ( - 1 ) NEW_LINE DEDENT return distance NEW_LINE DEDENT def interceptLength ( a , b , c , i , j , k ) : NEW_LINE INDENT rad = radius ( a , b , c ) NEW_LINE dist = centerDistanceFromLine ( a , b , i , j , k ) NEW_LINE if ( rad < 0 or dist < 0 ) : NEW_LINE INDENT print ( " circle ▁ not ▁ possible " ) NEW_LINE return NEW_LINE DEDENT if ( dist > rad ) : NEW_LINE INDENT print ( " Line ▁ not ▁ cutting ▁ circle " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 2 * math . sqrt ( rad * rad - dist * dist ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 0 NEW_LINE b = 0 NEW_LINE c = - 4 NEW_LINE i = 2 NEW_LINE j = - 1 NEW_LINE k = 1 NEW_LINE interceptLength ( a , b , c , i , j , k ) NEW_LINE DEDENT
Determine position of two points with respect to a 3D plane | Function to check position of two points with respect to a plane in 3D ; Put coordinates in plane equation ; If both values have same sign ; If both values have different sign ; If both values are zero ; If either of the two values is zero ; Driver Code ; Given Input ; Coordinates of points ; Function Call
def check_position ( a , b , c , d , x1 , y1 , z1 , x2 , y2 , z2 ) : NEW_LINE INDENT value_1 = a * x1 + b * y1 + c * z1 + d NEW_LINE value_2 = a * x2 + b * y2 + c * z2 + d NEW_LINE if ( ( value_1 > 0 and value_2 > 0 ) or ( value_1 < 0 and value_2 < 0 ) ) : NEW_LINE INDENT print ( " On ▁ same ▁ side " ) NEW_LINE DEDENT if ( ( value_1 > 0 and value_2 < 0 ) or ( value_1 < 0 and value_2 > 0 ) ) : NEW_LINE INDENT print ( " On ▁ different ▁ sides " ) NEW_LINE DEDENT if ( value_1 == 0 and value_2 == 0 ) : NEW_LINE INDENT print ( " Both ▁ on ▁ the ▁ plane " ) NEW_LINE DEDENT if ( value_1 == 0 and value_2 != 0 ) : NEW_LINE INDENT print ( " Point ▁ 1 ▁ on ▁ the ▁ plane " ) NEW_LINE DEDENT if ( value_1 != 0 and value_2 == 0 ) : NEW_LINE INDENT print ( " Point ▁ 2 ▁ on ▁ the ▁ plane " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b , c , d = 1 , 2 , 3 , 4 NEW_LINE x1 , y1 , z1 = - 2 , - 2 , 1 NEW_LINE x2 , y2 , z2 = - 4 , 11 , - 1 NEW_LINE check_position ( a , b , c , d , x1 , y1 , z1 , x2 , y2 , z2 ) NEW_LINE DEDENT
Check if any pair of semicircles intersect or not | Function to check if any pairs of the semicircles intersects or not ; Stores the coordinates of all the semicircles ; x and y are coordinates ; Store the minimum and maximum value of the pair ; Push the pair in vector ; Compare one pair with other pairs ; Generating the second pair ; Extract all the second pairs one by one ; 1 st condition ; 2 nd condition ; If any one condition is true ; If any pair of semicircles doesn 't exists ; Driver Code
def checkIntersection ( arr , N ) : NEW_LINE INDENT vec = [ ] NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT x = arr [ i ] NEW_LINE y = arr [ i + 1 ] NEW_LINE minn = min ( x , y ) NEW_LINE maxx = max ( x , y ) NEW_LINE vec . append ( [ minn , maxx ] ) NEW_LINE DEDENT for i in range ( len ( vec ) ) : NEW_LINE INDENT x = vec [ i ] NEW_LINE for j in range ( len ( vec ) ) : NEW_LINE INDENT y = vec [ j ] NEW_LINE cond1 = ( x [ 0 ] < y [ 0 ] and x [ 1 ] < y [ 1 ] and y [ 0 ] < x [ 1 ] ) NEW_LINE cond2 = ( y [ 0 ] < x [ 0 ] and y [ 1 ] < x [ 1 ] and x [ 0 ] < y [ 1 ] ) NEW_LINE if ( cond1 or cond2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 15 , 5 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE if ( checkIntersection ( arr , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find the angle between tangents drawn from a given external point to a Circle | Python 3 program for the above approach ; Function to find the distance between center and the exterior point ; Find the difference between the x and y coordinates ; Using the distance formula ; Function to find the angle between the pair of tangents drawn from the point ( X2 , Y2 ) to the circle . ; Calculate the distance between the center and exterior point ; Invalid Case ; Find the angle using the formula ; Print the resultant angle ; Driver Code
import math NEW_LINE def point_distance ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT p = ( x2 - x1 ) NEW_LINE q = ( y2 - y1 ) NEW_LINE distance = math . sqrt ( p * p + q * q ) NEW_LINE return distance NEW_LINE DEDENT def tangentAngle ( x1 , y1 , x2 , y2 , radius ) : NEW_LINE INDENT distance = point_distance ( x1 , y1 , x2 , y2 ) NEW_LINE if ( radius / distance > 1 or radius / distance < - 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT result = 2 * math . asin ( radius / distance ) * 180 / 3.1415 NEW_LINE print ( result , " ▁ degrees " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT radius = 4 NEW_LINE x1 = 7 NEW_LINE y1 = 12 NEW_LINE x2 = 3 NEW_LINE y2 = 4 NEW_LINE tangentAngle ( x1 , y1 , x2 , y2 , radius ) NEW_LINE DEDENT
Ratio of area of two nested polygons formed by connecting midpoints of sides of a regular N | Python3 code for the above approach ; Function to calculate the ratio of area of N - th and ( N + 1 ) - th nested polygons formed by connecting midpoints ; Stores the value of PI ; Calculating area the factor ; Printing the ratio precise upto 6 decimal places ; Driver Code
import math NEW_LINE def AreaFactor ( n ) : NEW_LINE INDENT pi = 3.14159265 NEW_LINE areaf = 1 / ( math . cos ( pi / n ) * math . cos ( pi / n ) ) NEW_LINE print ( ' % .6f ' % areaf ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE AreaFactor ( n ) NEW_LINE DEDENT
Find the length of Kth N | Python3 program for the above approach ; Function to calculate the interior angle of a N - sided regular polygon ; Function to find the K - th polygon formed inside the ( K - 1 ) th polygon ; Stores the interior angle ; Stores the side length of K - th regular polygon ; Return the length ; Driver Code
import math NEW_LINE PI = 3.14159265 NEW_LINE def findInteriorAngle ( n ) : NEW_LINE INDENT return ( n - 2 ) * PI / n NEW_LINE DEDENT def calculateSideLength ( L , N , K ) : NEW_LINE INDENT angle = findInteriorAngle ( N ) NEW_LINE length = L * pow ( math . sin ( angle / 2 ) , ( K - 1 ) ) NEW_LINE return length NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE L = 21 NEW_LINE K = 7 NEW_LINE print ( calculateSideLength ( L , N , K ) ) NEW_LINE DEDENT
Angle between a Pair of Lines | Python3 program for the above approach ; Function to find the angle between two lines ; Store the tan value of the angle ; Calculate tan inverse of the angle ; Convert the angle from radian to degree ; Print the result ; Driver Code
from math import atan NEW_LINE def findAngle ( M1 , M2 ) : NEW_LINE INDENT PI = 3.14159265 NEW_LINE angle = abs ( ( M2 - M1 ) / ( 1 + M1 * M2 ) ) NEW_LINE ret = atan ( angle ) NEW_LINE val = ( ret * 180 ) / PI NEW_LINE print ( round ( val , 4 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M1 = 1.75 NEW_LINE M2 = 0.27 NEW_LINE findAngle ( M1 , M2 ) NEW_LINE DEDENT
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