codeparrot/xlcost-text-to-code huggingface dataset:
Mirror of a point through a 3 D plane | Function to mirror image ; Driver Code ; function call
def mirror_point ( a , b , c , d , x1 , y1 , z1 ) : NEW_LINE INDENT k = ( - a * x1 - b * y1 - c * z1 - d ) / float ( ( a * a + b * b + c * c ) ) NEW_LINE x2 = a * k + x1 NEW_LINE y2 = b * k + y1 NEW_LINE z2 = c * k + z1 NEW_LINE x3 = 2 * x2 - x1 NEW_LINE y3 = 2 * y2 - y1 NEW_LINE z3 = 2 * z2 - z1 NEW_LINE print " x3 ▁ = " , x3 , NEW_LINE print " y3 ▁ = " , y3 , NEW_LINE print " z3 ▁ = " , z3 , NEW_LINE DEDENT a = 1 NEW_LINE b = - 2 NEW_LINE c = 0 NEW_LINE d = 0 NEW_LINE x1 = - 1 NEW_LINE y1 = 3 NEW_LINE z1 = 4 NEW_LINE mirror_point ( a , b , c , d , x1 , y1 , z1 ) NEW_LINE
Number of rectangles in a circle of radius R | Function to return the total possible rectangles that can be cut from the circle ; Diameter = 2 * Radius ; Square of diameter which is the square of the maximum length diagonal ; generate all combinations of a and b in the range ( 1 , ( 2 * Radius - 1 ) ) ( Both inclusive ) ; Calculate the Diagonal length of this rectangle ; If this rectangle 's Diagonal  Length is less than the  Diameter, it is a valid  rectangle, thus increment counter ; Radius of the circle
def countRectangles ( radius ) : NEW_LINE INDENT rectangles = 0 NEW_LINE diameter = 2 * radius NEW_LINE diameterSquare = diameter * diameter NEW_LINE for a in range ( 1 , 2 * radius ) : NEW_LINE INDENT for b in range ( 1 , 2 * radius ) : NEW_LINE INDENT diagonalLengthSquare = ( a * a + b * b ) NEW_LINE if ( diagonalLengthSquare <= diameterSquare ) : NEW_LINE INDENT rectangles += 1 NEW_LINE DEDENT DEDENT DEDENT return rectangles NEW_LINE DEDENT radius = 2 NEW_LINE totalRectangles = countRectangles ( radius ) NEW_LINE print ( totalRectangles , " rectangles ▁ can ▁ be " , " cut ▁ from ▁ a ▁ circle ▁ of ▁ Radius " , radius ) NEW_LINE
Program to check similarity of given two triangles | Function for AAA similarity ; Check for AAA ; Function for SAS similarity ; angle b / w two smallest sides is largest . ; since we take angle b / w the sides . ; Function for SSS similarity ; Check for SSS ; Driver Code ; function call for AAA similarity ; function call for SSS similarity ; function call for SAS similarity ; Check if triangles are similar or not
def simi_aaa ( a1 , a2 ) : NEW_LINE INDENT a1 = [ float ( i ) for i in a1 ] NEW_LINE a2 = [ float ( i ) for i in a2 ] NEW_LINE a1 . sort ( ) NEW_LINE a2 . sort ( ) NEW_LINE if a1 [ 0 ] == a2 [ 0 ] and a1 [ 1 ] == a2 [ 1 ] and a1 [ 2 ] == a2 [ 2 ] : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def simi_sas ( s1 , s2 , a1 , a2 ) : NEW_LINE INDENT s1 = [ float ( i ) for i in s1 ] NEW_LINE s2 = [ float ( i ) for i in s2 ] NEW_LINE a1 = [ float ( i ) for i in a1 ] NEW_LINE a2 = [ float ( i ) for i in a2 ] NEW_LINE s1 . sort ( ) NEW_LINE s2 . sort ( ) NEW_LINE a1 . sort ( ) NEW_LINE a2 . sort ( ) NEW_LINE if s1 [ 0 ] / s2 [ 0 ] == s1 [ 1 ] / s2 [ 1 ] : NEW_LINE INDENT if a1 [ 2 ] == a2 [ 2 ] : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if s1 [ 1 ] / s2 [ 1 ] == s1 [ 2 ] / s2 [ 2 ] : NEW_LINE INDENT if a1 [ 0 ] == a2 [ 0 ] : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if s1 [ 2 ] / s2 [ 2 ] == s1 [ 0 ] / s2 [ 0 ] : NEW_LINE INDENT if a1 [ 1 ] == a2 [ 1 ] : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT def simi_sss ( s1 , s2 ) : NEW_LINE INDENT s1 = [ float ( i ) for i in s1 ] NEW_LINE s2 = [ float ( i ) for i in s2 ] NEW_LINE s1 . sort ( ) NEW_LINE s2 . sort ( ) NEW_LINE if ( s1 [ 0 ] / s2 [ 0 ] == s1 [ 1 ] / s2 [ 1 ] and s1 [ 1 ] / s2 [ 1 ] == s1 [ 2 ] / s2 [ 2 ] and s1 [ 2 ] / s2 [ 2 ] == s1 [ 0 ] / s2 [ 0 ] ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT s1 = [ 2 , 3 , 3 ] NEW_LINE s2 = [ 4 , 6 , 6 ] NEW_LINE a1 = [ 80 , 60 , 40 ] NEW_LINE a2 = [ 40 , 60 , 80 ] NEW_LINE aaa = simi_aaa ( a1 , a2 ) NEW_LINE sss = simi_sss ( s1 , s2 ) NEW_LINE sas = simi_sas ( s1 , s2 , a1 , a2 ) NEW_LINE if aaa or sss or sas : NEW_LINE INDENT print " Triangles ▁ are ▁ similar ▁ by " , NEW_LINE if aaa : print " AAA " , NEW_LINE if sss : print " SSS " , NEW_LINE if sas : print " SAS " NEW_LINE DEDENT else : print " Triangles ▁ are ▁ not ▁ similar " NEW_LINE
Centered Pentadecagonal Number | centered pentadecagonal function ; Formula to calculate nth centered pentadecagonal number ; Driver Code
def center_pentadecagonal_num ( n ) : NEW_LINE INDENT return ( 15 * n * n - 15 * n + 2 ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( n , " rd ▁ number ▁ : ▁ " , center_pentadecagonal_num ( n ) ) NEW_LINE n = 10 NEW_LINE print ( n , " th ▁ number ▁ : ▁ " , center_pentadecagonal_num ( n ) ) NEW_LINE DEDENT
Centered nonadecagonal number | centered nonadecagonal function ; Formula to calculate nth centered nonadecagonal number & return it into main function . ; Driver Code
def center_nonadecagon_num ( n ) : NEW_LINE INDENT return ( 19 * n * n - 19 * n + 2 ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2 NEW_LINE print ( n , " nd ▁ centered ▁ nonadecagonal ▁ " + " number ▁ : ▁ " , center_nonadecagon_num ( n ) ) NEW_LINE n = 7 NEW_LINE print ( n , " nd ▁ centered ▁ nonadecagonal ▁ " + " number ▁ : ▁ " , center_nonadecagon_num ( n ) ) NEW_LINE DEDENT
Hendecagonal number | Function of Hendecagonal number ; Formula to calculate nth Hendecagonal number & return it into main function . ; Driver Code
def hendecagonal_num ( n ) : NEW_LINE INDENT return ( 9 * n * n - 7 * n ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( n , " rd ▁ Hendecagonal ▁ number ▁ : ▁ " , hendecagonal_num ( n ) ) NEW_LINE n = 10 NEW_LINE print ( n , " th ▁ Hendecagonal ▁ number ▁ : ▁ " , hendecagonal_num ( n ) ) NEW_LINE DEDENT
Centered Octagonal Number | Function to find centered octagonal number ; Formula to calculate nth centered octagonal number ; Driver Code
def cen_octagonalnum ( n ) : NEW_LINE INDENT return ( 4 * n * n - 4 * n + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE print ( n , " th ▁ Centered " , " octagonal ▁ number : ▁ " , cen_octagonalnum ( n ) ) NEW_LINE n = 11 NEW_LINE print ( n , " th ▁ Centered " , " octagonal ▁ number : ▁ " , cen_octagonalnum ( n ) ) NEW_LINE DEDENT
Number of ordered points pair satisfying line equation | Checks if ( i , j ) is valid , a point ( i , j ) is valid if point ( arr [ i ] , arr [ j ] ) satisfies the equation y = mx + c And i is not equal to j ; check if i equals to j ; Equation LHS = y , and RHS = mx + c ; Returns the number of ordered pairs ( i , j ) for which point ( arr [ i ] , arr [ j ] ) satisfies the equation of the line y = mx + c ; for every possible ( i , j ) check if ( a [ i ] , a [ j ] ) satisfies the equation y = mx + c ; ( firstIndex , secondIndex ) is same as ( i , j ) ; check if ( firstIndex , secondIndex ) is a valid point ; Driver Code ; equation of line is y = mx + c
def isValid ( arr , i , j , m , c ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT return False NEW_LINE DEDENT lhs = arr [ j ] ; NEW_LINE rhs = m * arr [ i ] + c NEW_LINE return ( lhs == rhs ) NEW_LINE DEDENT def findOrderedPoints ( arr , n , m , c ) : NEW_LINE INDENT counter = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT firstIndex = i NEW_LINE secondIndex = j NEW_LINE if ( isValid ( arr , firstIndex , secondIndex , m , c ) ) : NEW_LINE INDENT counter = counter + 1 NEW_LINE DEDENT DEDENT DEDENT return counter NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE m = 1 NEW_LINE c = 1 NEW_LINE print ( findOrderedPoints ( arr , n , m , c ) ) NEW_LINE
Check if a given circle lies completely inside the ring formed by two concentric circles | Python3 code to check if a circle lies in the ring ; Function to check if circle lies in the ring ; distance between center of circle center of concentric circles ( origin ) using Pythagoras theorem ; Condition to check if circle is strictly inside the ring ; Both circle with radius ' r ' and ' R ' have center ( 0 , 0 )
import math NEW_LINE def checkcircle ( r , R , r1 , x1 , y1 ) : NEW_LINE INDENT dis = int ( math . sqrt ( x1 * x1 + y1 * y1 ) ) NEW_LINE return ( dis - r1 >= R and dis + r1 <= r ) NEW_LINE DEDENT r = 8 ; R = 4 ; r1 = 2 ; x1 = 6 ; y1 = 0 NEW_LINE if ( checkcircle ( r , R , r1 , x1 , y1 ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT
Program for Surface Area of Octahedron | Python Program to calculate surface area of Octahedron . ; utility Function ; driver code
import math NEW_LINE def surface_area_octahedron ( side ) : NEW_LINE INDENT return ( 2 * ( math . sqrt ( 3 ) ) * ( side * side ) ) NEW_LINE DEDENT side = 7 NEW_LINE print ( " Surface ▁ area ▁ of ▁ octahedron ▁ = " , surface_area_octahedron ( side ) ) NEW_LINE
Count of different straight lines with total n points with m collinear | Returns value of binomial coefficient Code taken from https : goo . gl / vhy4jp ; C [ 0 ] = 1 nC0 is 1 ; Compute next row of pascal triangle using the previous row ; function to calculate number of straight lines can be formed ; Driven code
def nCk ( n , k ) : NEW_LINE INDENT C = [ 0 ] * ( k + 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT j = min ( i , k ) NEW_LINE while ( j > 0 ) : NEW_LINE INDENT C [ j ] = C [ j ] + C [ j - 1 ] NEW_LINE j = j - 1 NEW_LINE DEDENT DEDENT return C [ k ] NEW_LINE DEDENT def count_Straightlines ( n , m ) : NEW_LINE INDENT return ( nCk ( n , 2 ) - nCk ( m , 2 ) + 1 ) NEW_LINE DEDENT n = 4 NEW_LINE m = 3 NEW_LINE print ( count_Straightlines ( n , m ) ) ; NEW_LINE
Calculate Volume of Dodecahedron | Python3 program to calculate Volume of dodecahedron ; utility Function ; Driver Function
import math NEW_LINE def vol_of_dodecahedron ( side ) : NEW_LINE INDENT return ( ( ( 15 + ( 7 * ( math . sqrt ( 5 ) ) ) ) / 4 ) * ( math . pow ( side , 3 ) ) ) NEW_LINE DEDENT side = 4 NEW_LINE print ( " Volume ▁ of ▁ dodecahedron ▁ = " , round ( vol_of_dodecahedron ( side ) , 2 ) ) NEW_LINE
Program to check if water tank overflows when n solid balls are dipped in the water tank | function to find if tak will overflow or not ; cylinder capacity ; volume of water in tank ; volume of n balls ; total volume of water and n dipped balls ; condition to check if tank is in overflow state or not ; giving dimensions ; calling function
def overflow ( H , r , h , N , R ) : NEW_LINE INDENT tank_cap = 3.14 * r * r * H NEW_LINE water_vol = 3.14 * r * r * h NEW_LINE balls_vol = N * ( 4 / 3 ) * 3.14 * R * R * R NEW_LINE vol = water_vol + balls_vol NEW_LINE if vol > tank_cap : NEW_LINE INDENT print ( " Overflow " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ in ▁ overflow ▁ state " ) NEW_LINE DEDENT DEDENT H = 10 NEW_LINE r = 5 NEW_LINE h = 5 NEW_LINE N = 2 NEW_LINE R = 2 NEW_LINE overflow ( H , r , h , N , R ) NEW_LINE
Program to check if tank will overflow , underflow or filled in given time | function to calculate the volume of tank ; function to print overflow / filled / underflow accordingly ; radius of the tank ; height of the tank ; rate of flow of water ; time given ; calculate the required time ; printing the result
def volume ( radius , height ) : NEW_LINE INDENT return ( ( 22 / 7 ) * radius * radius * height ) NEW_LINE DEDENT def check_and_print ( required_time , given_time ) : NEW_LINE INDENT if required_time < given_time : NEW_LINE INDENT print ( " Overflow " ) NEW_LINE DEDENT elif required_time > given_time : NEW_LINE INDENT print ( " Underflow " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Filled " ) NEW_LINE DEDENT DEDENT radius = 5 NEW_LINE height = 10 NEW_LINE rate_of_flow = 10 NEW_LINE given_time = 70.0 NEW_LINE required_time = volume ( radius , height ) / rate_of_flow NEW_LINE check_and_print ( required_time , given_time ) NEW_LINE
Program to find third side of triangle using law of cosines | Python3 program to find third side of triangle using law of cosines ; Function to calculate cos value of angle c ; Converting degrees to radian ; Maps the sum along the series ; Holds the actual value of sin ( n ) ; Function to find third side ; Driver Code ; function call
import math as mt NEW_LINE def cal_cos ( n ) : NEW_LINE INDENT accuracy = 0.0001 NEW_LINE x1 , denominator , cosx , cosval = 0 , 0 , 0 , 0 NEW_LINE n = n * ( 3.142 / 180.0 ) NEW_LINE x1 = 1 NEW_LINE cosx = x1 NEW_LINE cosval = mt . cos ( n ) NEW_LINE i = 1 NEW_LINE while ( accuracy <= abs ( cosval - cosx ) ) : NEW_LINE INDENT denominator = 2 * i * ( 2 * i - 1 ) NEW_LINE x1 = - x1 * n * n / denominator NEW_LINE cosx = cosx + x1 NEW_LINE i = i + 1 NEW_LINE DEDENT return cosx NEW_LINE DEDENT def third_side ( a , b , c ) : NEW_LINE INDENT angle = cal_cos ( c ) NEW_LINE return mt . sqrt ( ( a * a ) + ( b * b ) - 2 * a * b * angle ) NEW_LINE DEDENT c = 49 NEW_LINE a , b = 5 , 8 NEW_LINE print ( third_side ( a , b , c ) ) NEW_LINE
Check whether given circle resides in boundary maintained by two other circles | Python3 program to check whether circle with given co - ordinates reside within the boundary of outer circle and inner circle ; function to check if given circle fit in boundary or not ; Distance from the center ; Checking the corners of circle ; Radius of outer circle and inner circle respectively ; Co - ordinates and radius of the circle to be checked
import math NEW_LINE def fitOrNotFit ( R , r , x , y , rad ) : NEW_LINE INDENT val = math . sqrt ( math . pow ( x , 2 ) + math . pow ( y , 2 ) ) NEW_LINE if ( val + rad <= R and val - rad >= R - r ) : NEW_LINE INDENT print ( " Fits " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Doesn ' t ▁ Fit " ) NEW_LINE DEDENT DEDENT R = 8 NEW_LINE r = 4 NEW_LINE x = 5 NEW_LINE y = 3 NEW_LINE rad = 3 NEW_LINE fitOrNotFit ( R , r , x , y , rad ) NEW_LINE
Program to find line passing through 2 Points | Function to find the line given two points ; Driver code
def lineFromPoints ( P , Q ) : NEW_LINE INDENT a = Q [ 1 ] - P [ 1 ] NEW_LINE b = P [ 0 ] - Q [ 0 ] NEW_LINE c = a * ( P [ 0 ] ) + b * ( P [ 1 ] ) NEW_LINE if ( b < 0 ) : NEW_LINE INDENT print ( " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ is : " , a , " x ▁ - ▁ " , b , " y ▁ = ▁ " , c ,   " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ is : ▁ " , a , " x ▁ + ▁ " , b , " y ▁ = ▁ " , c ,   " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT P = [ 3 , 2 ] NEW_LINE Q = [ 2 , 6 ] NEW_LINE lineFromPoints ( P , Q ) NEW_LINE DEDENT
Regular polygon using only 1 s in a binary numbered circle | Python3 program to find whether a regular polygon is possible in circle with 1 s as vertices ; method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( N / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; If i divides N then i and ( N / i ) will be divisors ; check polygon for both divisors ; Driver code
from math import sqrt NEW_LINE def checkPolygonWithMidpoints ( arr , N , midpoints ) : NEW_LINE INDENT for j in range ( midpoints ) : NEW_LINE INDENT val = 1 NEW_LINE for k in range ( j , N , midpoints ) : NEW_LINE INDENT val &= arr [ k ] NEW_LINE DEDENT if ( val and N // midpoints > 2 ) : NEW_LINE INDENT print ( " Polygon ▁ possible ▁ with ▁ side ▁ length " , ( N // midpoints ) ) NEW_LINE return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def isPolygonPossible ( arr , N ) : NEW_LINE INDENT limit = sqrt ( N ) NEW_LINE for i in range ( 1 , int ( limit ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( checkPolygonWithMidpoints ( arr , N , i ) or checkPolygonWithMidpoints ( arr , N , ( N // i ) ) ) : NEW_LINE INDENT return NEW_LINE DEDENT DEDENT DEDENT print ( " Not ▁ possiblen " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE isPolygonPossible ( arr , N ) NEW_LINE DEDENT
Minimum lines to cover all points | Utility method to get gcd of a and b ; method returns reduced form of dy / dx as a pair ; get sign of result ; method returns minimum number of lines to cover all points where all lines goes through ( xO , yO ) ; set to store slope as a pair ; loop over all points once ; get x and y co - ordinate of current point ; if this slope is not there in set , increase ans by 1 and insert in set ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def getReducedForm ( dy , dx ) : NEW_LINE INDENT g = gcd ( abs ( dy ) , abs ( dx ) ) NEW_LINE sign = ( dy < 0 ) ^ ( dx < 0 ) NEW_LINE if ( sign ) : NEW_LINE INDENT return ( - abs ( dy ) // g , abs ( dx ) // g ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( abs ( dy ) // g , abs ( dx ) // g ) NEW_LINE DEDENT DEDENT def minLinesToCoverPoints ( points , N , xO , yO ) : NEW_LINE INDENT st = dict ( ) NEW_LINE minLines = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT curX = points [ i ] [ 0 ] NEW_LINE curY = points [ i ] [ 1 ] NEW_LINE temp = getReducedForm ( curY - yO , curX - xO ) NEW_LINE if ( temp not in st ) : NEW_LINE INDENT st [ temp ] = 1 NEW_LINE minLines += 1 NEW_LINE DEDENT DEDENT return minLines NEW_LINE DEDENT xO = 1 NEW_LINE yO = 0 NEW_LINE points = [ [ - 1 , 3 ] , [ 4 , 3 ] , [ 2 , 1 ] , [ - 1 , - 2 ] , [ 3 , - 3 ] ] NEW_LINE N = len ( points ) NEW_LINE print ( minLinesToCoverPoints ( points , N , xO , yO ) ) NEW_LINE
Maximum height when coins are arranged in a triangle | Returns the square root of n . Note that the function ; We are using n itself as initial approximation This can definitely be improved ; e = 0.000001 e decides the accuracy level ; Method to find maximum height of arrangement of coins ; calculating portion inside the square root ; Driver code to test above method
def squareRoot ( n ) : NEW_LINE INDENT x = n NEW_LINE y = 1 NEW_LINE while ( x - y > e ) : NEW_LINE INDENT x = ( x + y ) / 2 NEW_LINE y = n / x NEW_LINE DEDENT return x NEW_LINE DEDENT def findMaximumHeight ( N ) : NEW_LINE INDENT n = 1 + 8 * N NEW_LINE maxH = ( - 1 + squareRoot ( n ) ) / 2 NEW_LINE return int ( maxH ) NEW_LINE DEDENT N = 12 NEW_LINE print ( findMaximumHeight ( N ) ) NEW_LINE
Number of Integral Points between Two Points | Class to represent an Integral point on XY plane . ; Utility function to find GCD of two numbers GCD of a and b ; Finds the no . of Integral points between two given points . ; If line joining p and q is parallel to x axis , then count is difference of y values ; If line joining p and q is parallel to y axis , then count is difference of x values ; Driver Code
class Point : NEW_LINE INDENT def __init__ ( self , a , b ) : NEW_LINE INDENT self . x = a NEW_LINE self . y = b NEW_LINE DEDENT DEDENT def gcd ( a , b ) : NEW_LINE INDENT if b == 0 : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def getCount ( p , q ) : NEW_LINE INDENT if p . x == q . x : NEW_LINE INDENT return abs ( p . y - q . y ) - 1 NEW_LINE DEDENT if p . y == q . y : NEW_LINE INDENT return abs ( p . x - q . x ) - 1 NEW_LINE DEDENT return gcd ( abs ( p . x - q . x ) , abs ( p . y - q . y ) ) - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = Point ( 1 , 9 ) NEW_LINE q = Point ( 8 , 16 ) NEW_LINE print ( " The ▁ number ▁ of ▁ integral ▁ points " , " between ▁ ( { } , ▁ { } ) ▁ and ▁ ( { } , ▁ { } ) ▁ is ▁ { } " . format ( p . x , p . y , q . x , q . y , getCount ( p , q ) ) ) NEW_LINE DEDENT
How to check if given four points form a square | A Python3 program to check if four given points form a square or not . ; Structure of a point in 2D space ; A utility function to find square of distance from point ' p ' to point 'q ; This function returns true if ( p1 , p2 , p3 , p4 ) form a square , otherwise false ; d2 = distSq ( p1 , p2 ) from p1 to p2 d3 = distSq ( p1 , p3 ) from p1 to p3 d4 = distSq ( p1 , p4 ) from p1 to p4 ; If lengths if ( p1 , p2 ) and ( p1 , p3 ) are same , then following conditions must be met to form a square . 1 ) Square of length of ( p1 , p4 ) is same as twice the square of ( p1 , p2 ) 2 ) Square of length of ( p2 , p3 ) is same as twice the square of ( p2 , p4 ) ; The below two cases are similar to above case ; Driver Code
class Point : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT ' NEW_LINE def distSq ( p , q ) : NEW_LINE INDENT return ( p . x - q . x ) * ( p . x - q . x ) + ( p . y - q . y ) * ( p . y - q . y ) NEW_LINE DEDENT def isSquare ( p1 , p2 , p3 , p4 ) : NEW_LINE INDENT if d2 == 0 or d3 == 0 or d4 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT if d2 == d3 and 2 * d2 == d4 and 2 * distSq ( p2 , p4 ) == distSq ( p2 , p3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if d3 == d4 and 2 * d3 == d2 and 2 * distSq ( p3 , p2 ) == distSq ( p3 , p4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if d2 == d4 and 2 * d2 == d3 and 2 * distSq ( p2 , p3 ) == distSq ( p2 , p4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p1 = Point ( 20 , 10 ) NEW_LINE p2 = Point ( 10 , 20 ) NEW_LINE p3 = Point ( 20 , 20 ) NEW_LINE p4 = Point ( 10 , 10 ) NEW_LINE if isSquare ( p1 , p2 , p3 , p4 ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Count triplets such that product of two numbers added with third number is N | Python program for the above approach ; function to find the divisors of the number ( N - i ) ; Stores the resultant count of divisors of ( N - i ) ; Iterate over range [ 1 , sqrt ( N ) ] ; Return the total divisors ; def to find the number of triplets such that A * B - C = N ; Loop to fix the value of C ; Adding the number of divisors in count ; Return count of triplets ; Driver Code Driver Code
import math NEW_LINE def countDivisors ( n ) : NEW_LINE INDENT divisors = 0 NEW_LINE for i in range ( 1 , math . ceil ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT divisors = divisors + 1 NEW_LINE DEDENT if ( i - ( n / i ) == 1 ) : NEW_LINE INDENT i = i - 1 NEW_LINE DEDENT DEDENT for i in range ( math . ceil ( math . sqrt ( n ) ) + 1 , 1 , - 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT divisors = divisors + 1 NEW_LINE DEDENT DEDENT return divisors NEW_LINE DEDENT def possibleTriplets ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT count = count + countDivisors ( N - i ) NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE print ( possibleTriplets ( N ) ) NEW_LINE DEDENT
Maximize count of planes that can be stopped per second with help of given initial position and speed | Function to find maximum number of planes that can be stopped from landing ; Stores the times needed for landing for each plane ; Iterate over the arrays ; Stores the time needed for landing of current plane ; Update the value of t ; Append the t in set St ; Return the answer ; Driver Code
def maxPlanes ( A , B , N ) : NEW_LINE INDENT St = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT t = 1 if ( A [ i ] % B [ i ] > 0 ) else 0 NEW_LINE t += ( A [ i ] // B [ i ] ) + t NEW_LINE St . add ( t ) NEW_LINE DEDENT return len ( St ) NEW_LINE DEDENT A = [ 1 , 3 , 5 , 4 , 8 ] NEW_LINE B = [ 1 , 2 , 2 , 1 , 2 ] NEW_LINE N = len ( A ) NEW_LINE print ( maxPlanes ( A , B , N ) ) NEW_LINE
Find the player who will win by choosing a number in range [ 1 , K ] with sum total N | Function to predict the winner ; Driver Code ; Given Input ; Function call
def predictTheWinner ( K , N ) : NEW_LINE INDENT if ( N % ( K + 1 ) == 0 ) : NEW_LINE INDENT print ( " Bob " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Alice " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 7 NEW_LINE N = 50 NEW_LINE predictTheWinner ( K , N ) NEW_LINE DEDENT