codeparrot/xlcost-text-to-code huggingface dataset:
Given a binary string , count number of substrings that start and end with 1. | A simple Python 3 program to count number of substrings starting and ending with 1 ; Initialize result ; Pick a starting point ; Search for all possible ending point ; Driver program to test above function
def countSubStr ( st , n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( st [ i ] == '1' ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( st [ j ] == '1' ) : NEW_LINE INDENT res = res + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return res NEW_LINE DEDENT st = "00100101" ; NEW_LINE list ( st ) NEW_LINE n = len ( st ) NEW_LINE print ( countSubStr ( st , n ) , end = " " ) NEW_LINE
An in | A utility function to reverse string str [ low . . high ] ; Cycle leader algorithm to move all even positioned elements at the end . ; odd index ; even index ; keep the back - up of element at new position ; The main function to transform a string . This function mainly uses cycleLeader ( ) to transform ; Step 1 : Find the largest prefix subarray of the form 3 ^ k + 1 ; Step 2 : Apply cycle leader algorithm for the largest subarrau ; Step 4.1 : Reverse the second half of first subarray ; Step 4.2 : Reverse the first half of second sub - string ; Step 4.3 Reverse the second half of first sub - string and first half of second sub - string together ; Increase the length of first subarray ; Driver Code
def Reverse ( string : list , low : int , high : int ) : NEW_LINE INDENT while low < high : NEW_LINE INDENT string [ low ] , string [ high ] = string [ high ] , string [ low ] NEW_LINE low += 1 NEW_LINE high -= 1 NEW_LINE DEDENT DEDENT def cycleLeader ( string : list , shift : int , len : int ) : NEW_LINE INDENT i = 1 NEW_LINE while i < len : NEW_LINE INDENT j = i NEW_LINE item = string [ j + shift ] NEW_LINE while True : NEW_LINE INDENT if j & 1 : NEW_LINE INDENT j = len // 2 + j // 2 NEW_LINE DEDENT else : NEW_LINE INDENT j //= 2 NEW_LINE DEDENT string [ j + shift ] , item = item , string [ j + shift ] NEW_LINE if j == i : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i *= 3 NEW_LINE DEDENT DEDENT def moveNumberToSecondHalf ( string : list ) : NEW_LINE INDENT k , lenFirst = 0 , 0 NEW_LINE lenRemaining = len ( string ) NEW_LINE shift = 0 NEW_LINE while lenRemaining : NEW_LINE INDENT k = 0 NEW_LINE while pow ( 3 , k ) + 1 <= lenRemaining : NEW_LINE INDENT k += 1 NEW_LINE DEDENT lenFirst = pow ( 3 , k - 1 ) + 1 NEW_LINE lenRemaining -= lenFirst NEW_LINE cycleLeader ( string , shift , lenFirst ) NEW_LINE Reverse ( string , shift // 2 , shift - 1 ) NEW_LINE Reverse ( string , shift , shift + lenFirst // 2 - 1 ) NEW_LINE Reverse ( string , shift // 2 , shift + lenFirst // 2 - 1 ) NEW_LINE shift += lenFirst NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " a1b2c3d4e5f6g7" NEW_LINE string = list ( string ) NEW_LINE moveNumberToSecondHalf ( string ) NEW_LINE print ( ' ' . join ( string ) ) NEW_LINE DEDENT
Remove nodes on root to leaf paths of length < K | New node of a tree ; Utility method that actually removes the nodes which are not on the pathLen >= k . This method can change the root as well . ; Base condition ; Traverse the tree in postorder fashion so that if a leaf node path length is shorter than k , then that node and all of its descendants till the node which are not on some other path are removed . ; If root is a leaf node and it 's level   is less than k then remove this node.   This goes up and check for the ancestor   nodes also for the same condition till  it finds a node which is a part of other   path(s) too.  ; Return root ; Method which calls the utitlity method to remove the short path nodes . ; Method to print the tree in inorder fashion . ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def removeShortPathNodesUtil ( root , level , k ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT root . left = removeShortPathNodesUtil ( root . left , level + 1 , k ) NEW_LINE root . right = removeShortPathNodesUtil ( root . right , level + 1 , k ) NEW_LINE if ( root . left == None and root . right == None and level < k ) : NEW_LINE INDENT return None NEW_LINE DEDENT return root NEW_LINE DEDENT def removeShortPathNodes ( root , k ) : NEW_LINE INDENT pathLen = 0 NEW_LINE return removeShortPathNodesUtil ( root , 1 , k ) NEW_LINE DEDENT def prInorder ( root ) : NEW_LINE INDENT if ( root ) : NEW_LINE INDENT prInorder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE prInorder ( root . right ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 4 NEW_LINE root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . left . left . left = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE root . right . right . left = newNode ( 8 ) NEW_LINE print ( " Inorder ▁ Traversal ▁ of ▁ Original ▁ tree " ) NEW_LINE prInorder ( root ) NEW_LINE print ( ) NEW_LINE print ( " Inorder ▁ Traversal ▁ of ▁ Modified ▁ tree " ) NEW_LINE res = removeShortPathNodes ( root , k ) NEW_LINE prInorder ( res ) NEW_LINE DEDENT
Remove duplicates from a given string | Python program to remove duplicate character from character array and print in sorted order ; Create a set using String characters ; Print content of the set ; Driver code
def removeDuplicate ( str , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in str : NEW_LINE INDENT s . add ( i ) NEW_LINE DEDENT st = " " NEW_LINE for i in s : NEW_LINE INDENT st = st + i NEW_LINE DEDENT return st NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE n = len ( str ) NEW_LINE print ( removeDuplicate ( list ( str ) , n ) ) NEW_LINE
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | function to check for the given sum in the array ; checking for condition ; driver code
def printPairs ( arr , arr_size , sum ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT temp = sum - arr [ i ] NEW_LINE if ( temp in s ) : NEW_LINE INDENT print " Pair ▁ with ▁ given ▁ sum ▁ " + str ( sum ) + NEW_LINE DEDENT DEDENT " ▁ is ▁ ( " + str ( arr [ i ] ) + " , ▁ " + str ( temp ) + " ) " NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT DEDENT A = [ 1 , 4 , 45 , 6 , 10 , 8 ] NEW_LINE n = 16 NEW_LINE printPairs ( A , len ( A ) , n ) NEW_LINE
Longest consecutive sequence in Binary tree | A utility class to create a node ; Utility method to return length of longest consecutive sequence of tree ; if root data has one more than its parent then increase current length ; update the maximum by current length ; recursively call left and right subtree with expected value 1 more than root data ; method returns length of longest consecutive sequence rooted at node root ; call utility method with current length 0 ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def longestConsecutiveUtil ( root , curLength , expected , res ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . data == expected ) : NEW_LINE INDENT curLength += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curLength = 1 NEW_LINE DEDENT res [ 0 ] = max ( res [ 0 ] , curLength ) NEW_LINE longestConsecutiveUtil ( root . left , curLength , root . data + 1 , res ) NEW_LINE longestConsecutiveUtil ( root . right , curLength , root . data + 1 , res ) NEW_LINE DEDENT def longestConsecutive ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = [ 0 ] NEW_LINE longestConsecutiveUtil ( root , 0 , root . data , res ) NEW_LINE return res [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 6 ) NEW_LINE root . right = newNode ( 9 ) NEW_LINE root . right . left = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 10 ) NEW_LINE root . right . right . right = newNode ( 11 ) NEW_LINE print ( longestConsecutive ( root ) ) NEW_LINE DEDENT
Find remainder when a number A raised to N factorial is divided by P | Function to calculate ( A ^ N ! ) % P in O ( log y ) ; Initialize result ; Update x if it is more than or Equal to p ; In case x is divisible by p ; If y is odd , multiply x with result ; y must be even now ; Returning modular power ; Function to calculate resultant remainder ; Initializing ans to store final remainder ; Calculating remainder ; Returning resultant remainder ; Given input ; Function Call
def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def remainder ( n , a , p ) : NEW_LINE INDENT ans = a % p NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = power ( ans , i , p ) NEW_LINE DEDENT return ans NEW_LINE DEDENT A = 2 NEW_LINE N = 1 NEW_LINE P = 2 NEW_LINE print ( remainder ( N , A , P ) ) NEW_LINE
Find all array elements occurring more than âŒŠN / 3 âŒ ‹ times | ''Function to find Majority element in an array ; '' if this element is previously seen,  increment count1. ; '' if this element is previously seen,  increment count2. ; '' if current element is different from  both the previously seen variables, decrement both the counts. ;  ;
def findMajority ( arr , n ) : NEW_LINE INDENT count1 = 0 NEW_LINE DEDENT count2 = 0 NEW_LINE = first = 2147483647 NEW_LINE INDENT second = 2147483647 NEW_LINE flag = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if first == arr [ i ] : NEW_LINE count1 += 1 NEW_LINE elif second == arr [ i ] : NEW_LINE count2 += 1 NEW_LINE elif count1 == 0 : NEW_LINE count1 += 1 NEW_LINE DEDENT first = arr [ i ] NEW_LINE INDENT elif count2 == 0 : NEW_LINE count2 += 1 NEW_LINE second = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT count1 -= 1 NEW_LINE count2 -= 1 NEW_LINE count1 = 0 NEW_LINE count2 = 0 NEW_LINE DEDENT ' gain traverse the array and find the NEW_LINE INDENT actual counts . ''' STRNEWLINE ▁ for ▁ i ▁ in ▁ range ( n ) : STRNEWLINE ▁ if ▁ arr [ i ] ▁ = = ▁ first : STRNEWLINE ▁ count1 ▁ + = ▁ 1 STRNEWLINE ▁ elif ▁ arr [ i ] ▁ = = ▁ second : STRNEWLINE count2 ▁ + = ▁ 1 STRNEWLINE ▁ if ▁ count1 ▁ > ▁ int ( n ▁ / ▁ 3 ) : STRNEWLINE ▁ print ( first , ▁ end ▁ = ▁ " ▁ " ) STRNEWLINE ▁ flag ▁ = ▁ 1 STRNEWLINE ▁ if ▁ count2 ▁ > ▁ int ( n ▁ / ▁ 3 ) : STRNEWLINE print ( second , ▁ end ▁ = ▁ " ▁ " ) STRNEWLINE ▁ flag ▁ = ▁ 1 STRNEWLINE ▁ if ▁ flag ▁ = = ▁ 0 : STRNEWLINE ▁ print ( " No ▁ Majority ▁ Element " ) STRNEWLINE ' Driver ▁ code ▁ ''' NEW_LINE DEDENT arr = [ 2 , 2 , 3 , 1 , 3 , 2 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE findMajority ( arr , n ) NEW_LINE
Modular exponentiation ( Recursive ) | Recursive Python program to compute modular power ; Base Cases ; If B is Even ; If B is Odd ; Driver Code
def exponentMod ( A , B , C ) : NEW_LINE INDENT if ( A == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( B == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT y = 0 NEW_LINE if ( B % 2 == 0 ) : NEW_LINE INDENT y = exponentMod ( A , B / 2 , C ) NEW_LINE y = ( y * y ) % C NEW_LINE DEDENT else : NEW_LINE INDENT y = A % C NEW_LINE y = ( y * exponentMod ( A , B - 1 , C ) % C ) % C NEW_LINE DEDENT return ( ( y + C ) % C ) NEW_LINE DEDENT A = 2 NEW_LINE B = 5 NEW_LINE C = 13 NEW_LINE print ( " Power ▁ is " , exponentMod ( A , B , C ) ) NEW_LINE
Find frequency of each element in a limited range array in less than O ( n ) time | python program to count number of occurrences of each element in the array in O ( n ) time and O ( 1 ) space ; check if the current element is equal to previous element . ; reset the frequency ; print the last element and its frequency ;
def findFrequencies ( ele , n ) : NEW_LINE INDENT freq = 1 NEW_LINE idx = 1 NEW_LINE element = ele [ 0 ] NEW_LINE while ( idx < n ) : NEW_LINE INDENT if ( ele [ idx - 1 ] == ele [ idx ] ) : NEW_LINE INDENT freq += 1 NEW_LINE idx += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( element , " ▁ " , freq ) ; NEW_LINE element = ele [ idx ] NEW_LINE idx += 1 NEW_LINE freq = 1 NEW_LINE DEDENT DEDENT print ( element , " ▁ " , freq ) ; NEW_LINE DEDENT / * Driver code * / NEW_LINE print ( " - - - frequencies ▁ in ▁ a ▁ sorted ▁ array - - - - " ) ; NEW_LINE arr = [ 10 , 20 , 30 , 30 , 30 , 40 , 50 , 50 , 50 , 50 , 70 ] ; NEW_LINE n = len ( arr ) NEW_LINE findFrequencies ( arr , n ) NEW_LINE
Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2
def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) != 0 ) : NEW_LINE INDENT res = res * x NEW_LINE DEDENT DEDENT DEDENT y = y >> 1 NEW_LINE x = x * x NEW_LINE INDENT return res NEW_LINE DEDENT
Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Driver Code
def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) == 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 y = y / 2 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT x = 2 ; y = 5 ; p = 13 NEW_LINE print ( " Power ▁ is ▁ " , power ( x , y , p ) ) NEW_LINE
Path length having maximum number of bends | Utility function to create a new node ; Recursive function to calculate the path Length having maximum number of bends . The following are parameters for this function . node - . pointer to the current node Dir - . determines whether the current node is left or right child of it 's parent node bends -. number of bends so far in the current path. maxBends -. maximum number of bends in a path from root to leaf soFar -. Length of the current path so far traversed Len -. Length of the path having maximum number of bends ; Base Case ; Leaf node ; Having both left and right child ; Helper function to call findMaxBendsUtil ( ) ; Call for left subtree of the root ; Call for right subtree of the root ; Include the root node as well in the path Length ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2 \ 1 / 9
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . left = None NEW_LINE self . right = None NEW_LINE self . key = key NEW_LINE DEDENT DEDENT def findMaxBendsUtil ( node , Dir , bends , maxBends , soFar , Len ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( node . left == None and node . right == None ) : NEW_LINE INDENT if ( bends > maxBends [ 0 ] ) : NEW_LINE INDENT maxBends [ 0 ] = bends NEW_LINE Len [ 0 ] = soFar NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( Dir == ' l ' ) : NEW_LINE INDENT findMaxBendsUtil ( node . left , Dir , bends , maxBends , soFar + 1 , Len ) NEW_LINE findMaxBendsUtil ( node . right , ' r ' , bends + 1 , maxBends , soFar + 1 , Len ) NEW_LINE DEDENT else : NEW_LINE INDENT findMaxBendsUtil ( node . right , Dir , bends , maxBends , soFar + 1 , Len ) NEW_LINE findMaxBendsUtil ( node . left , ' l ' , bends + 1 , maxBends , soFar + 1 , Len ) NEW_LINE DEDENT DEDENT DEDENT def findMaxBends ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT Len = [ 0 ] NEW_LINE bends = 0 NEW_LINE maxBends = [ - 1 ] NEW_LINE if ( node . left ) : NEW_LINE INDENT findMaxBendsUtil ( node . left , ' l ' , bends , maxBends , 1 , Len ) NEW_LINE DEDENT if ( node . right ) : NEW_LINE INDENT findMaxBendsUtil ( node . right , ' r ' , bends , maxBends , 1 , Len ) NEW_LINE DEDENT Len [ 0 ] += 1 NEW_LINE return Len [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 8 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 2 ) NEW_LINE root . right . left . right = newNode ( 1 ) NEW_LINE root . right . left . right . left = newNode ( 9 ) NEW_LINE print ( findMaxBends ( root ) - 1 ) NEW_LINE DEDENT
Program to calculate angle between two N | Python3 program for the above approach ; Function to find the magnitude of the given vector ; Stores the final magnitude ; Traverse the array ; Return square root of magnitude ; Function to find the dot product of two vectors ; Stores dot product ; Traverse the array ; Return the product ; Stores dot product of two vectors ; Stores magnitude of vector A ; Stores magnitude of vector B ; Stores angle between given vectors ; Print the angle ; Driver Code ; Given magnitude arrays ; Size of the array ; Function call to find the angle between two vectors
import math NEW_LINE def magnitude ( arr , N ) : NEW_LINE INDENT magnitude = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT magnitude += arr [ i ] * arr [ i ] NEW_LINE DEDENT return math . sqrt ( magnitude ) NEW_LINE DEDENT def dotProduct ( arr , brr , N ) : NEW_LINE INDENT product = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT product = product + arr [ i ] * brr [ i ] NEW_LINE DEDENT return product NEW_LINE DEDENT def angleBetweenVectors ( arr , brr , N ) : NEW_LINE INDENT dotProductOfVectors = dotProduct ( arr , brr , N ) NEW_LINE magnitudeOfA = magnitude ( arr , N ) NEW_LINE magnitudeOfB = magnitude ( brr , N ) NEW_LINE angle = ( dotProductOfVectors / ( magnitudeOfA * magnitudeOfB ) ) NEW_LINE print ( ' % .5f ' % angle ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 0.5 , - 2 , 1 ] NEW_LINE brr = [ - 1 , - 1 , 0.3 ] NEW_LINE N = len ( arr ) NEW_LINE angleBetweenVectors ( arr , brr , N ) NEW_LINE DEDENT
Sum of sides of largest and smallest child polygons possible from a given polygon | Function to find the sum of largest and smallest secondary polygons if possible ; Count edges of primary polygon ; Calculate edges present in the largest secondary polygon ; Driver Code ; Given Exterior Angle
def secondary_polygon ( Angle ) : NEW_LINE INDENT edges_primary = 360 // Angle NEW_LINE if edges_primary >= 6 : NEW_LINE INDENT edges_max_secondary = edges_primary // 2 NEW_LINE return edges_max_secondary + 3 NEW_LINE DEDENT else : NEW_LINE INDENT return " Not ▁ Possible " NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Angle = 45 NEW_LINE print ( secondary_polygon ( Angle ) ) NEW_LINE DEDENT
Area of Circumcircle of an Equilateral Triangle using Median | Python3 implementation to find the equation of circle which inscribes equilateral triangle of median M ; Function to find the equation of circle whose center is ( x1 , y1 ) and the radius of circle is r ; Function to find the equation of circle which inscribes equilateral triangle of median M ; Function to find the circle equation ; Driver code ; Function call
pi = 3.14159265358979323846 NEW_LINE def circleArea ( r ) : NEW_LINE INDENT print ( round ( pi * r * r , 4 ) ) NEW_LINE DEDENT def findCircleAreaByMedian ( m ) : NEW_LINE INDENT r = 2 * m / 3 NEW_LINE circleArea ( r ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 3 NEW_LINE findCircleAreaByMedian ( m ) NEW_LINE DEDENT
Find the type of triangle from the given sides | Function to find the type of triangle with the help of sides ; Driver Code ; Function Call
def checkTypeOfTriangle ( a , b , c ) : NEW_LINE INDENT sqa = pow ( a , 2 ) NEW_LINE sqb = pow ( b , 2 ) NEW_LINE sqc = pow ( c , 2 ) NEW_LINE if ( sqa == sqa + sqb or sqb == sqa + sqc or sqc == sqa + sqb ) : NEW_LINE INDENT print ( " Right - angled ▁ Triangle " ) NEW_LINE DEDENT elif ( sqa > sqc + sqb or sqb > sqa + sqc or sqc > sqa + sqb ) : NEW_LINE INDENT print ( " Obtuse - angled ▁ Triangle " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Acute - angled ▁ Triangle " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 2 NEW_LINE b = 2 NEW_LINE c = 2 NEW_LINE checkTypeOfTriangle ( a , b , c ) NEW_LINE DEDENT
Check whether the triangle is valid or not if angles are given | Function to check if sum of the three angles is 180 or not ; Check condition ; Driver code ; function calling and print output
def Valid ( a , b , c ) : NEW_LINE INDENT if ( ( a + b + c == 180 ) and a != 0 and b != 0 and c != 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 60 NEW_LINE b = 40 NEW_LINE c = 80 NEW_LINE if ( Valid ( a , b , c ) ) : NEW_LINE INDENT print ( " Valid " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE DEDENT DEDENT
Area of the Largest Triangle inscribed in a Hexagon | Python3 Program to find the biggest triangle which can be inscribed within the hexagon ; Function to find the area of the triangle ; side cannot be negative ; area of the triangle ; Driver code
import math NEW_LINE def trianglearea ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT area = ( 3 * math . sqrt ( 3 ) * math . pow ( a , 2 ) ) / 4 ; NEW_LINE return area ; NEW_LINE DEDENT a = 6 ; NEW_LINE print ( trianglearea ( a ) ) NEW_LINE
Equation of ellipse from its focus , directrix , and eccentricity | Function to find equation of ellipse . ; Driver Code
def equation_ellipse ( x1 , y1 , a , b , c , e ) : NEW_LINE INDENT t = a * a + b * b NEW_LINE a1 = t - e * ( a * a ) NEW_LINE b1 = t - e * ( b * b ) NEW_LINE c1 = ( - 2 * t * x1 ) - ( 2 * e * c * a ) NEW_LINE d1 = ( - 2 * t * y1 ) - ( 2 * e * c * b ) NEW_LINE e1 = - 2 * e * a * b NEW_LINE f1 = ( - e * c * c ) + ( t * x1 * x1 ) + ( t * y1 * y1 ) NEW_LINE print ( " Equation ▁ of ▁ ellipse ▁ is " , a1 , " x ^ 2 ▁ + " , b1 , " y ^ 2 ▁ + " , c1 , " x ▁ + " , d1 , " y ▁ + " , e1 , " xy ▁ + " , f1 , " = ▁ 0" ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x1 , y1 , a , b , c , e = 1 , 1 , 1 , - 1 , 3 , 0.5 * 0.5 NEW_LINE equation_ellipse ( x1 , y1 , a , b , c , e ) NEW_LINE DEDENT