codeparrot/xlcost-text-to-code huggingface dataset:
Coordinates of rectangle with given points lie inside | function to print coordinate of smallest rectangle ; find Xmax and Xmin ; find Ymax and Ymin ; print all four coordinates ; driver program
def printRect ( X , Y , n ) : NEW_LINE INDENT Xmax = max ( X ) NEW_LINE Xmin = min ( X ) NEW_LINE Ymax = max ( Y ) NEW_LINE Ymin = min ( Y ) NEW_LINE print ( " { " , Xmin , " , ▁ " , Ymin , " } " , sep = " " ) NEW_LINE print ( " { " , Xmin , " , ▁ " , Ymax , " } " , sep = " " ) NEW_LINE print ( " { " , Xmax , " , ▁ " , Ymax , " } " , sep = " " ) NEW_LINE print ( " { " , Xmax , " , ▁ " , Ymin , " } " , sep = " " ) NEW_LINE DEDENT X = [ 4 , 3 , 6 , 1 , - 1 , 12 ] NEW_LINE Y = [ 4 , 1 , 10 , 3 , 7 , - 1 ] NEW_LINE n = len ( X ) NEW_LINE printRect ( X , Y , n ) NEW_LINE
Check if a line passes through the origin | Python program to find if line passing through two coordinates also passes through origin or not ; Driver code
def checkOrigin ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT return ( x1 * ( y2 - y1 ) == y1 * ( x2 - x1 ) ) NEW_LINE DEDENT if ( checkOrigin ( 1 , 28 , 2 , 56 ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Number of horizontal or vertical line segments to connect 3 points | Python program to find number of horizontal ( or vertical line segments needed to connect three points . ; Function to check if the third point forms a rectangle with other two points at corners ; Returns true if point k can be used as a joining point to connect using two line segments ; Check for the valid polyline with two segments ; Check whether the X - coordinates or Y - cocordinates are same . ; Iterate over all pairs to check for two line segments ; Otherwise answer is three . ; driver code
import math NEW_LINE def isBetween ( a , b , c ) : NEW_LINE INDENT return min ( a , b ) <= c and c <= max ( a , b ) NEW_LINE DEDENT def canJoin ( x , y , i , j , k ) : NEW_LINE INDENT return ( x [ k ] == x [ i ] or x [ k ] == x [ j ] ) and isBetween ( y [ i ] , y [ j ] , y [ k ] ) or ( y [ k ] == y [ i ] or y [ k ] == y [ j ] ) and isBetween ( x [ i ] , x [ j ] , x [ k ] ) NEW_LINE DEDENT def countLineSegments ( x , y ) : NEW_LINE INDENT if ( ( x [ 0 ] == x [ 1 ] and x [ 1 ] == x [ 2 ] ) or ( y [ 0 ] == y [ 1 ] and y [ 1 ] == y [ 2 ] ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( canJoin ( x , y , 0 , 1 , 2 ) or canJoin ( x , y , 0 , 2 , 1 ) or canJoin ( x , y , 1 , 2 , 0 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT x = [ - 1 , - 1 , 4 ] NEW_LINE y = [ - 1 , 3 , 3 ] NEW_LINE print ( countLineSegments ( x , y ) ) NEW_LINE
Pythagorean Quadruple | Python code to detect Pythagorean Quadruples . ; function for checking ; driver code
import math NEW_LINE def pythagorean_quadruple ( a , b , c , d ) : NEW_LINE INDENT sum = a * a + b * b + c * c ; NEW_LINE if ( d * d == sum ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT a = 1 NEW_LINE b = 2 NEW_LINE c = 2 NEW_LINE d = 3 NEW_LINE if ( pythagorean_quadruple ( a , b , c , d ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program for Volume and Surface area of Frustum of Cone | Python3 code to calculate Volume and Surface area of frustum of cone ; Function to calculate Volume of frustum of cone ; Function to calculate Curved Surface area of frustum of cone ; Function to calculate Total Surface area of frustum of cone ; Driver Code ; Printing value of volume and surface area
import math NEW_LINE pi = math . pi NEW_LINE def volume ( r , R , h ) : NEW_LINE INDENT return 1 / 3 * pi * h * ( r * r + R * R + r * R ) NEW_LINE DEDENT def curved_surface_area ( r , R , l ) : NEW_LINE INDENT return pi * l * ( R + r ) NEW_LINE DEDENT def total_surface_area ( r , R , l , h ) : NEW_LINE INDENT return pi * l * ( R + r ) + pi * ( r * r + R * R ) NEW_LINE DEDENT small_radius = 3 NEW_LINE big_radius = 8 NEW_LINE slant_height = 13 NEW_LINE height = 12 NEW_LINE print ( " Volume ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " , end = ' ' ) NEW_LINE print ( volume ( small_radius , big_radius , height ) ) NEW_LINE print ( " Curved ▁ Surface ▁ Area ▁ Of ▁ Frustum " + " ▁ of ▁ Cone ▁ : ▁ " , end = ' ' ) NEW_LINE print ( curved_surface_area ( small_radius , big_radius , slant_height ) ) NEW_LINE print ( " Total ▁ Surface ▁ Area ▁ Of ▁ Frustum " + " ▁ of ▁ Cone ▁ : ▁ " , end = ' ' ) NEW_LINE print ( total_surface_area ( small_radius , big_radius , slant_height , height ) ) NEW_LINE
Program to find Perimeter / Circumference of Square and Rectangle | Python3 Program to find Circumference of a square ; Driver code
def Circumference ( a ) : NEW_LINE INDENT return ( 4 * a ) NEW_LINE DEDENT a = 5 NEW_LINE c = Circumference ( a ) NEW_LINE print ( " Circumference ▁ of ▁ a ▁ " + " square ▁ is ▁ % ▁ d " % ( c ) ) NEW_LINE
Maximum area of quadrilateral | Python3 program to find maximum area of a quadrilateral ; Calculating the semi - perimeter of the given quadrilateral ; Applying Brahmagupta 's formula to  get maximum area of quadrilateral ; Driver code
import math NEW_LINE def maxArea ( a , b , c , d ) : NEW_LINE INDENT semiperimeter = ( a + b + c + d ) / 2 NEW_LINE return math . sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) NEW_LINE DEDENT a = 1 NEW_LINE b = 2 NEW_LINE c = 1 NEW_LINE d = 2 NEW_LINE print ( " % .2f " % maxArea ( a , b , c , d ) ) NEW_LINE
Direction of a Point from a Line Segment | Structure for point in cartesian plane . ; Constant integers for directions ; Subtracting co - ordinates of point A from B and P , to make A as origin ; Determining cross Product ; Return RIGHT if cross product is positive ; Return LEFT if cross product is negative ; Return ZERO if cross product is zero ; Driver code ; A . y = 10 A ( - 30 , 10 ) ; B . y = - 15 B ( 29 , - 15 ) ; P . y = 28 P ( 15 , 28 )
class point : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . x = 0 NEW_LINE self . y = 0 NEW_LINE DEDENT DEDENT RIGHT = 1 NEW_LINE LEFT = - 1 NEW_LINE ZERO = 0 NEW_LINE def directionOfPoint ( A , B , P ) : NEW_LINE INDENT global RIGHT , LEFT , ZERO NEW_LINE B . x -= A . x NEW_LINE B . y -= A . y NEW_LINE P . x -= A . x NEW_LINE P . y -= A . y NEW_LINE cross_product = B . x * P . y - B . y * P . x NEW_LINE if ( cross_product > 0 ) : NEW_LINE INDENT return RIGHT NEW_LINE DEDENT if ( cross_product < 0 ) : NEW_LINE INDENT return LEFT NEW_LINE DEDENT return ZERO NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = point ( ) NEW_LINE B = point ( ) NEW_LINE P = point ( ) NEW_LINE A . x = - 30 NEW_LINE B . x = 29 NEW_LINE P . x = 15 NEW_LINE direction = directionOfPoint ( A , B , P ) NEW_LINE if ( direction == 1 ) : NEW_LINE INDENT print ( " Right ▁ Direction " ) NEW_LINE DEDENT elif ( direction == - 1 ) : NEW_LINE INDENT print ( " Left ▁ Direction " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Point ▁ is ▁ on ▁ the ▁ Line " ) NEW_LINE DEDENT DEDENT
Find minimum radius such that atleast k point lie inside the circle | Return minimum distance required so that aleast k point lie inside the circle . ; Finding distance between of each point from origin ; Sorting the distance ; Driver Program
def minRadius ( k , x , y , n ) : NEW_LINE INDENT dis = [ 0 ] * n NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] NEW_LINE DEDENT dis . sort ( ) NEW_LINE return dis [ k - 1 ] NEW_LINE DEDENT k = 3 NEW_LINE x = [ 1 , - 1 , 1 ] NEW_LINE y = [ 1 , - 1 , - 1 ] NEW_LINE n = len ( x ) NEW_LINE print ( minRadius ( k , x , y , n ) ) NEW_LINE
Program for Area And Perimeter Of Rectangle | Utility function ; Driver function
def areaRectangle ( a , b ) : NEW_LINE INDENT return ( a * b ) NEW_LINE DEDENT def perimeterRectangle ( a , b ) : NEW_LINE INDENT return ( 2 * ( a + b ) ) NEW_LINE DEDENT a = 5 ; NEW_LINE b = 6 ; NEW_LINE print ( " Area ▁ = ▁ " , areaRectangle ( a , b ) ) NEW_LINE print ( " Perimeter ▁ = ▁ " , perimeterRectangle ( a , b ) ) NEW_LINE
Program for Area Of Square | Python3 code to find the area of the square ; Driver Code
def areaSquare ( side ) : NEW_LINE INDENT area = side * side NEW_LINE return area NEW_LINE DEDENT side = 4 NEW_LINE print ( areaSquare ( side ) ) NEW_LINE
Minimum Perimeter of n blocks | Python3 program to find minimum perimeter using n blocks . ; if n is a perfect square ; Number of rows ; perimeter of the rectangular grid ; if there are blocks left ; Driver code
import math NEW_LINE def minPerimeter ( n ) : NEW_LINE INDENT l = math . sqrt ( n ) NEW_LINE sq = l * l NEW_LINE if ( sq == n ) : NEW_LINE INDENT return l * 4 NEW_LINE DEDENT else : NEW_LINE INDENT row = n / l NEW_LINE perimeter = 2 * ( l + row ) NEW_LINE if ( n % l != 0 ) : NEW_LINE INDENT perimeter += 2 NEW_LINE DEDENT return perimeter NEW_LINE DEDENT DEDENT n = 10 NEW_LINE print ( int ( minPerimeter ( n ) ) ) NEW_LINE
Find if it 's possible to rotate the page by an angle or not. | Function to find if it 's possible to rotate page or not ; Calculating distance b / w points ; If distance is not equal ; If the points are in same line ; Points a , b , and c
def possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) : NEW_LINE INDENT dis1 = ( pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ) NEW_LINE dis2 = ( pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ) NEW_LINE if ( dis1 != dis2 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT elif ( b1 == ( ( a1 + c1 ) // 2.0 ) and b2 == ( ( a2 + c2 ) // 2.0 ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT a1 , b1 , c1 = 1 , 2 , 3 NEW_LINE a2 = b2 = c2 = 0 NEW_LINE possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) NEW_LINE
Check if two given circles touch or intersect each other | Python3 program to check if two circles touch each other or not . ; Driver code
def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) : NEW_LINE INDENT distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; NEW_LINE radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; NEW_LINE if ( distSq == radSumSq ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( distSq > radSumSq ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT x1 = - 10 NEW_LINE y1 = 8 NEW_LINE x2 = 14 NEW_LINE y2 = - 24 NEW_LINE r1 = 30 NEW_LINE r2 = 10 NEW_LINE t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) NEW_LINE if ( t == 1 ) : NEW_LINE INDENT print ( " Circle ▁ touch ▁ to ▁ each ▁ other . " ) NEW_LINE DEDENT elif ( t < 0 ) : NEW_LINE INDENT print ( " Circle ▁ not ▁ touch ▁ to ▁ each ▁ other . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Circle ▁ intersect ▁ to ▁ each ▁ other . " ) NEW_LINE DEDENT
Count of obtuse angles in a circle with ' k ' equidistant points between 2 given points | C ++ program to count number of obtuse angles for given two points . ; There are two arcs connecting a and b . Let us count points on both arcs . ; Both arcs have same number of points ; Points on smaller arc is answer ; Driver code
def countObtuseAngles ( a , b , k ) : NEW_LINE INDENT c1 = ( b - a ) - 1 NEW_LINE c2 = ( k - b ) + ( a - 1 ) NEW_LINE if ( c1 == c2 ) : NEW_LINE return 0 NEW_LINE return min ( c1 , c2 ) NEW_LINE DEDENT k , a , b = 6 , 1 , 3 NEW_LINE print countObtuseAngles ( a , b , k ) NEW_LINE
Count of acute , obtuse and right triangles with given sides | Find the number of acute , right , obtuse triangle that can be formed from given array . ; Finding the square of each element of array ; Sort the sides of array and their squares . ; x for acute triangles y for right triangles z for obtuse triangles ; Finding the farthest point p where a ^ 2 + b ^ 2 >= c ^ 2. ; Finding the farthest point q where a + b > c . ; If point p make right triangle . ; All triangle between j and p are acute triangles . So add p - j - 1 in x . ; Increment y by 1. ; All triangle between q and p are acute triangles . So add q - p in z . ; If no right triangle ; All triangle between j and p are acute triangles . So add p - j in x . ; All triangle between q and p are acute triangles . So add q - p in z . ; Driver Code
def findTriangle ( a , n ) : NEW_LINE INDENT b = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT b . append ( a [ i ] * a [ i ] ) NEW_LINE DEDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE x , y , z = 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT p = i + 1 NEW_LINE q = i + 1 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT while ( p < n - 1 and b [ i ] + b [ j ] >= b [ p + 1 ] ) : NEW_LINE INDENT p += 1 NEW_LINE DEDENT q = max ( q , p ) NEW_LINE while ( q < n - 1 and a [ i ] + a [ j ] > a [ q + 1 ] ) : NEW_LINE INDENT q += 1 NEW_LINE DEDENT if ( b [ i ] + b [ j ] == b [ p ] ) : NEW_LINE INDENT x += max ( p - j - 1 , 0 ) NEW_LINE y += 1 NEW_LINE z += q - p NEW_LINE DEDENT else : NEW_LINE INDENT x += max ( p - j , 0 ) NEW_LINE z += q - p NEW_LINE DEDENT DEDENT DEDENT print ( " Acute ▁ Triangle : " , x ) NEW_LINE print ( " Right ▁ Triangle : " , y ) NEW_LINE print ( " Obtuse ▁ Triangle : " , z ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 9 , 10 , 12 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE findTriangle ( arr , n ) NEW_LINE DEDENT
Check whether a point exists in circle sector or not . | Python3 program to check if a point lies inside a circle sector . ; calculate endAngle ; Calculate polar co - ordinates ; Check whether polarradius is less then radius of circle or not and Angle is between startAngle and endAngle or not ; Driver code
import math NEW_LINE def checkPoint ( radius , x , y , percent , startAngle ) : NEW_LINE INDENT endAngle = 360 / percent + startAngle NEW_LINE polarradius = math . sqrt ( x * x + y * y ) NEW_LINE Angle = math . atan ( y / x ) NEW_LINE if ( Angle >= startAngle and Angle <= endAngle and polarradius < radius ) : NEW_LINE INDENT print ( " Point ▁ ( " , x , " , " , y , " ) ▁ " " exist ▁ in ▁ the ▁ circle ▁ sector " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Point ▁ ( " , x , " , " , y , " ) ▁ " " does ▁ not ▁ exist ▁ in ▁ the ▁ circle ▁ sector " ) NEW_LINE DEDENT DEDENT radius , x , y = 8 , 3 , 4 NEW_LINE percent , startAngle = 12 , 0 NEW_LINE checkPoint ( radius , x , y , percent , startAngle ) NEW_LINE
Area of a polygon with given n ordered vertices | ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; Return absolute value ; Driver program to test above function
def polygonArea ( X , Y , n ) : NEW_LINE INDENT area = 0.0 NEW_LINE j = n - 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) NEW_LINE DEDENT return int ( abs ( area / 2.0 ) ) NEW_LINE DEDENT X = [ 0 , 2 , 4 ] NEW_LINE Y = [ 1 , 3 , 7 ] NEW_LINE n = len ( X ) NEW_LINE print ( polygonArea ( X , Y , n ) ) NEW_LINE
Difference of count of distinct elements present to left and right for each array element | Function to find the difference of count of distince elements to the left and right for each array elements ; Stores distinct array element in the left and right ; Traverse the array ; Insert all element to the left in the set S1 ; Insert all element to the right in the set S2 ; Print the difference ; Driver Code
def findDifferenceArray ( arr , N ) : NEW_LINE INDENT S1 = set ( ) ; NEW_LINE S2 = set ( ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT S1 . add ( arr [ j ] ) ; NEW_LINE DEDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT S2 . add ( arr [ j ] ) ; NEW_LINE DEDENT print ( abs ( len ( S1 ) - len ( S2 ) ) , end = ' ▁ ' ) ; NEW_LINE S1 . clear ( ) ; NEW_LINE S2 . clear ( ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 7 , 3 , 2 , 3 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findDifferenceArray ( arr , N ) ; NEW_LINE DEDENT
Maximum subarray sum with same first and last element formed by removing elements | Python3 program for the above approach ; Function to find the maximum sum of sub - array ; Initialize the variables ; Traverse over the range ; Add the current value to the variable currsum for prefix sum ; Calculate the result ; Return the answer ; Driver Code
import sys NEW_LINE def maximumSubarraySum ( arr ) : NEW_LINE INDENT N = len ( arr ) ; NEW_LINE memo = { } ; NEW_LINE res = - ( sys . maxsize - 1 ) ; NEW_LINE currsum = 0 ; NEW_LINE currval = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT currval = arr [ i ] ; NEW_LINE currsum = currsum + currval ; NEW_LINE if currval in memo : NEW_LINE INDENT if ( currval > 0 ) : NEW_LINE INDENT res = max ( res , currsum - memo [ currval ] + currval ) ; NEW_LINE DEDENT else : NEW_LINE INDENT res = max ( res , currsum - memo [ currval ] + 2 * currval ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT memo [ currval ] = currsum ; NEW_LINE DEDENT if ( currval < 0 ) : NEW_LINE INDENT currsum = currsum - currval ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 1 , - 3 , 4 , 0 , - 1 , - 2 ] ; NEW_LINE print ( maximumSubarraySum ( arr ) ) ; NEW_LINE DEDENT
Find Nth root of a number using Bisection method | Function that returns the value of the function at a given value of x ; calculating the value of the differential of the function ; The function that returns the root of given number ; Defining range on which answer can be found ; finding mid value ; Driver code
def f ( x , p , num ) : NEW_LINE INDENT return pow ( x , p ) - num NEW_LINE DEDENT def f_prime ( x , p ) : NEW_LINE INDENT return p * pow ( x , p - 1 ) NEW_LINE DEDENT def root ( num , p ) : NEW_LINE INDENT left = - num NEW_LINE right = num NEW_LINE x = 0 NEW_LINE while ( True ) : NEW_LINE INDENT x = ( left + right ) / 2.0 NEW_LINE value = f ( x , p , num ) NEW_LINE prime = f_prime ( x , p ) NEW_LINE if ( value * prime <= 0 ) : NEW_LINE INDENT left = x NEW_LINE DEDENT else : NEW_LINE INDENT right = x NEW_LINE DEDENT if ( value < 0.000001 and value >= 0 ) : NEW_LINE INDENT return x NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT P = 1234321 NEW_LINE N = 2 NEW_LINE ans = root ( P , N ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Count of permutations of an Array having maximum MEXs sum of prefix arrays | To calculate the factorial ; To return the number of permutations of an array with maximum MEXs sum of prefix array ; Map to store the frequency of each element ; Running a loop from i = 0 to i < n ; If continuity breaks , then break the loop ; Considering choices available to be filled at this position , i . e . mp [ i ] ; Decrement the count of remaining right elements ; Adding all permutations of the elements present to the right of the point where continuity breaks . ; Driver Code
def factorial ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res *= i NEW_LINE DEDENT return res NEW_LINE DEDENT def countPermutations ( ar , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE ans = 1 NEW_LINE cnt = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ar [ i ] in mp ) : NEW_LINE INDENT mp [ ar [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ ar [ i ] ] = 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( i not in mp ) : NEW_LINE INDENT break NEW_LINE DEDENT ans = ( ans * mp [ i ] ) NEW_LINE cnt -= 1 NEW_LINE DEDENT ans = ans * factorial ( cnt ) NEW_LINE return ans NEW_LINE DEDENT arr = [ 1 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPermutations ( arr , N ) ) NEW_LINE
Convert 0 to N by adding 1 or multiplying by 2 in minimum steps | Function to count number of set bits in N ; Stores the count of set bits ; If N is odd , then it a set bit ; Return the result ; Driver Code
def minimumAdditionOperation ( N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( N ) : NEW_LINE INDENT if ( N & 1 == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT N = N >> 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE print ( minimumAdditionOperation ( N ) ) NEW_LINE DEDENT
Count of unordered pairs of semi | python program of the above approach ; Stores the count of distinct prime number in factor of current number ; Function to return the vector of semi prime numbers in range [ 1 , N ] ; Count of distinct prime number in the factor of current number using Sieve of Eratosthenes ; If current number is prime ; Stores the semi prime numbers ; If p has 2 distinct prime factors ; Return vector ; Function to count unordered pairs of semi prime numbers with prime sum ; Stores the final count ; Loop to iterate over al the l unordered pairs ; If sum of current semi prime numbers is a prime number ; Return answer ; Driver Code
maxn = 100000 NEW_LINE prime = [ 0 for _ in range ( maxn ) ] NEW_LINE def semiPrimes ( N ) : NEW_LINE INDENT for p in range ( 2 , maxn ) : NEW_LINE INDENT if ( prime [ p ] == 0 ) : NEW_LINE INDENT for i in range ( 2 * p , maxn , p ) : NEW_LINE INDENT prime [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT sPrimes = [ ] NEW_LINE for p in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( prime [ p ] == 2 ) : NEW_LINE INDENT sPrimes . append ( p ) NEW_LINE DEDENT DEDENT return sPrimes NEW_LINE DEDENT def countPairs ( semiPrimes ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 0 , len ( semiPrimes ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( semiPrimes ) ) : NEW_LINE INDENT if ( prime [ semiPrimes [ i ] + semiPrimes [ j ] ] == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE print ( countPairs ( semiPrimes ( N ) ) ) NEW_LINE DEDENT
Minimum divide by 2 operations required to make GCD odd for given Array | python program for the above approac ; Function to find the minimum number of operations to make the GCD of the array odd ; Stores the minimum operations required ; Stores the powers of two for the current array element ; Dividing by 2 ; Increment the count ; Update the minimum operation required ; Return the result required ; Driver Code
INT_MAX = 2147483647 NEW_LINE def minimumOperations ( arr , N ) : NEW_LINE INDENT mini = INT_MAX NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT count = 0 NEW_LINE while ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i ] // 2 NEW_LINE count += 1 NEW_LINE DEDENT if ( mini > count ) : NEW_LINE INDENT mini = count NEW_LINE DEDENT DEDENT return mini NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minimumOperations ( arr , N ) ) NEW_LINE DEDENT
Minimum size of the array with MEX as A and XOR of the array elements as B | Function to find the minimum size of array with given MEX and XOR ; Find the XOR of values from 0 to A - 1 ; If A is a multiple of 4 ; If A % 4 gives remainder 1 ; If A % 4 gives remainder 2 ; Initializing array size by A ; If XOR of all values of array is equal to B ; If the required integer to be added is equal to A ; Else any integer can be added ; Driver Code
def minimumSizeArr ( A , B ) : NEW_LINE INDENT currXor = 0 NEW_LINE reminder = ( A - 1 ) % 4 NEW_LINE if ( reminder == 0 ) : NEW_LINE INDENT currXor = A - 1 NEW_LINE DEDENT elif ( reminder == 1 ) : NEW_LINE INDENT currXor = 1 NEW_LINE DEDENT elif ( reminder == 2 ) : NEW_LINE INDENT currXor = A NEW_LINE DEDENT minSize = A NEW_LINE if ( currXor == B ) : NEW_LINE INDENT return minSize NEW_LINE DEDENT elif ( currXor ^ B == A ) : NEW_LINE INDENT return minSize + 2 NEW_LINE DEDENT else : NEW_LINE INDENT return minSize + 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 1 NEW_LINE B = 999 NEW_LINE print ( minimumSizeArr ( A , B ) ) NEW_LINE DEDENT