codeparrot/xlcost-text-to-code huggingface dataset:
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