codeparrot/xlcost-text-to-code huggingface dataset:
Count of Unique Direct Path Between N Points On a Plane | Function to count the total number of direct paths ; Driver Code
def countDirectPath ( N ) : NEW_LINE INDENT return N + ( N * ( N - 3 ) ) // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE print ( countDirectPath ( N ) ) NEW_LINE DEDENT
Find the coordinate that does not belong to any square | Function to find the point that is not a part of the side of a square ; Traverse each pair of coordinates ; Minimize x - coordinate in all the points except current point ; Maximize x - coordinate in all the points except the current point ; Minimize y - coordinate in all the points except current point ; Maximize y - coordinate in all the points except current point ; If x - coordinate matches with other same line ; If y coordinate matches with other same line ; Check if the condition for square exists or not ; Print the output ; Driver Code
def findPoint ( n , p ) : NEW_LINE INDENT for i in range ( n * 4 + 1 ) : NEW_LINE INDENT x1 = 2e9 NEW_LINE x2 = - 2e9 NEW_LINE y1 = 2e9 NEW_LINE y2 = - 2e9 NEW_LINE for j in range ( n * 4 + 1 ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT x1 = min ( x1 , p [ j ] [ 0 ] ) NEW_LINE x2 = max ( x2 , p [ j ] [ 0 ] ) NEW_LINE y1 = min ( y1 , p [ j ] [ 1 ] ) NEW_LINE y2 = max ( y2 , p [ j ] [ 1 ] ) NEW_LINE DEDENT DEDENT ok = 1 NEW_LINE c1 = 0 NEW_LINE c2 = 0 NEW_LINE c3 = 0 NEW_LINE c4 = 0 NEW_LINE for j in range ( 1 , n * 4 + 1 , 1 ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT if ( ( p [ j ] [ 0 ] == x1 or p [ j ] [ 0 ] == x2 ) or ( p [ j ] [ 1 ] == y1 or p [ j ] [ 1 ] == y2 ) ) : NEW_LINE INDENT if ( p [ j ] [ 0 ] == x1 ) : NEW_LINE INDENT c1 += 1 NEW_LINE DEDENT if ( p [ j ] [ 0 ] == x2 ) : NEW_LINE INDENT c2 += 1 NEW_LINE DEDENT if ( p [ j ] [ 1 ] == y1 ) : NEW_LINE INDENT c3 += 1 NEW_LINE DEDENT if ( p [ j ] [ 1 ] == y2 ) : NEW_LINE INDENT c4 += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT ok = 0 NEW_LINE DEDENT DEDENT DEDENT if ( ok and c1 >= n and c2 >= n and c3 >= n and c4 >= n and x2 - x1 == y2 - y1 ) : NEW_LINE INDENT print ( p [ i ] [ 0 ] , p [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE arr = [ [ 0 , 0 ] , [ 0 , 1 ] , [ 0 , 2 ] , [ 1 , 0 ] , [ 1 , 1 ] , [ 1 , 2 ] , [ 2 , 0 ] , [ 2 , 1 ] , [ 2 , 2 ] ] NEW_LINE findPoint ( N , arr ) NEW_LINE DEDENT
Triacontagon Number | Finding the nth triacontagonal Number ; Driver Code
def triacontagonalNum ( n ) : NEW_LINE INDENT return ( 28 * n * n - 26 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ triacontagonal ▁ Number ▁ is ▁ = ▁ " , triacontagonalNum ( n ) ) NEW_LINE
Hexacontagon Number | Finding the nth hexacontagon Number ; Driver Code
def hexacontagonNum ( n ) : NEW_LINE INDENT return ( 58 * n * n - 56 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ hexacontagon ▁ Number ▁ is ▁ = ▁ " , hexacontagonNum ( n ) ) ; NEW_LINE
Enneacontagon Number | Finding the nth enneacontagon Number ; Driver Code
def enneacontagonNum ( n ) : NEW_LINE INDENT return ( 88 * n * n - 86 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ enneacontagon ▁ Number ▁ is ▁ = ▁ " , enneacontagonNum ( n ) ) NEW_LINE
Triacontakaidigon Number | Finding the nth triacontakaidigon Number ; Driver Code
def triacontakaidigonNum ( n ) : NEW_LINE INDENT return ( 30 * n * n - 28 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ triacontakaidigon ▁ Number ▁ is ▁ = ▁ " , triacontakaidigonNum ( n ) ) NEW_LINE
Icosihexagonal Number | Finding the nth Icosihexagonal Number ; Driver Code
def IcosihexagonalNum ( n ) : NEW_LINE INDENT return ( 24 * n * n - 22 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ Icosihexagonal ▁ Number ▁ is ▁ = ▁ " , IcosihexagonalNum ( n ) ) NEW_LINE
Icosikaioctagon or Icosioctagon Number | Finding the nth icosikaioctagonal Number ; Driver Code
def icosikaioctagonalNum ( n ) : NEW_LINE INDENT return ( 26 * n * n - 24 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ icosikaioctagonal ▁ Number ▁ is ▁ = ▁ " , icosikaioctagonalNum ( n ) ) NEW_LINE
Octacontagon Number | Finding the nth octacontagon number ; Driver code
def octacontagonNum ( n ) : NEW_LINE INDENT return ( 78 * n * n - 76 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ octacontagon ▁ Number ▁ is ▁ = ▁ " , octacontagonNum ( n ) ) NEW_LINE
Hectagon Number | Finding the nth hectagon number ; Driver code
def hectagonNum ( n ) : NEW_LINE INDENT return ( 98 * n * n - 96 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ hectagon ▁ Number ▁ is ▁ = ▁ " , hectagonNum ( n ) ) NEW_LINE
Tetracontagon Number | Finding the nth tetracontagon Number ; Driver Code
def tetracontagonNum ( n ) : NEW_LINE INDENT return ( 38 * n * n - 36 * n ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( "3rd ▁ tetracontagon ▁ Number ▁ is ▁ = ▁ " , tetracontagonNum ( n ) ) NEW_LINE
Perimeter of an Ellipse | Python3 program to find perimeter of an Ellipse ; Function to find the perimeter of an Ellipse ; Compute perimeter ; Driver code ; Function call
from math import sqrt NEW_LINE def Perimeter ( a , b ) : NEW_LINE INDENT perimeter = 0 NEW_LINE perimeter = ( 2 * 3.14 * sqrt ( ( a * a + b * b ) / ( 2 * 1.0 ) ) ) ; NEW_LINE print ( perimeter ) NEW_LINE DEDENT a = 3 NEW_LINE b = 2 NEW_LINE Perimeter ( a , b ) NEW_LINE
Biggest Reuleaux Triangle within A Square | Function to find the Area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reuleaux triangle ; Driver code
def ReuleauxArea ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT A = 0.70477 * pow ( a , 2 ) ; NEW_LINE return A NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 6 NEW_LINE print ( ReuleauxArea ( a ) ) NEW_LINE DEDENT
Largest hexagon that can be inscribed within a square | Function to return the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
def hexagonside ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT x = 0.5176 * a ; NEW_LINE return x ; NEW_LINE DEDENT a = 6 ; NEW_LINE print ( hexagonside ( a ) ) ; NEW_LINE
Largest hexagon that can be inscribed within an equilateral triangle | function to find the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
def hexagonside ( a ) : NEW_LINE INDENT if a < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = a // 3 NEW_LINE return x NEW_LINE DEDENT a = 6 NEW_LINE print ( hexagonside ( a ) ) NEW_LINE
Find middle point segment from given segment lengths | Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code
def findSegment ( n , m , segment_length ) : NEW_LINE INDENT meet_point = ( 1.0 * n ) / 2.0 NEW_LINE sum = 0 NEW_LINE segment_number = 0 NEW_LINE for i in range ( 0 , m , 1 ) : NEW_LINE INDENT sum += segment_length [ i ] NEW_LINE if ( sum == meet_point ) : NEW_LINE INDENT segment_number = - 1 NEW_LINE break NEW_LINE DEDENT if ( sum > meet_point ) : NEW_LINE INDENT segment_number = i + 1 NEW_LINE break NEW_LINE DEDENT DEDENT return segment_number NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 13 NEW_LINE m = 3 NEW_LINE segment_length = [ 3 , 2 , 8 ] NEW_LINE ans = findSegment ( n , m , segment_length ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Maximum points of intersection n lines | nC2 = ( n ) * ( n - 1 ) / 2 ; Driver code ; n is number of line
def countMaxIntersect ( n ) : NEW_LINE INDENT return int ( n * ( n - 1 ) / 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 NEW_LINE print ( countMaxIntersect ( n ) ) NEW_LINE DEDENT
Program to find volume and surface area of pentagonal prism | function for surface area ; function for VOlume ; Driver Code
def surfaceArea ( a , b , h ) : NEW_LINE INDENT return 5 * a * b + 5 * b * h NEW_LINE DEDENT def volume ( b , h ) : NEW_LINE INDENT return ( 5 * b * h ) / 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 NEW_LINE b = 3 NEW_LINE h = 7 NEW_LINE print ( " surface ▁ area ▁ = " , surfaceArea ( a , b , h ) , " , " , " volume ▁ = " , volume ( b , h ) ) NEW_LINE DEDENT
Check if a point is inside , outside or on the parabola | Function to check the point ; checking the equation of parabola with the given point ; Driver code
def checkpoint ( h , k , x , y , a ) : NEW_LINE INDENT p = pow ( ( y - k ) , 2 ) - 4 * a * ( x - h ) NEW_LINE return p NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT h = 0 NEW_LINE k = 0 NEW_LINE x = 2 NEW_LINE y = 1 NEW_LINE a = 4 NEW_LINE if checkpoint ( h , k , x , y , a ) > 0 : NEW_LINE INDENT print ( " Outside " ) NEW_LINE DEDENT elif checkpoint ( h , k , x , y , a ) == 0 : NEW_LINE INDENT print ( " On the parabola " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Inside " ) ; NEW_LINE DEDENT DEDENT
Check if a point is inside , outside or on the ellipse | Python 3 Program to check if the point lies within the ellipse or not ; Function to check the point ; checking the equation of ellipse with the given point ; Driver code
import math NEW_LINE def checkpoint ( h , k , x , y , a , b ) : NEW_LINE INDENT p = ( ( math . pow ( ( x - h ) , 2 ) // math . pow ( a , 2 ) ) + ( math . pow ( ( y - k ) , 2 ) // math . pow ( b , 2 ) ) ) NEW_LINE return p NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT h = 0 NEW_LINE k = 0 NEW_LINE x = 2 NEW_LINE y = 1 NEW_LINE a = 4 NEW_LINE b = 5 NEW_LINE if ( checkpoint ( h , k , x , y , a , b ) > 1 ) : NEW_LINE INDENT print ( " Outside " ) NEW_LINE DEDENT elif ( checkpoint ( h , k , x , y , a , b ) == 1 ) : NEW_LINE INDENT print ( " On ▁ the ▁ ellipse " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Inside " ) NEW_LINE DEDENT DEDENT
Area of circle inscribed within rhombus | Function to find the area of the inscribed circle ; the diagonals cannot be negative ; area of the circle ; Driver code
def circlearea ( a , b ) : NEW_LINE INDENT if ( a < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT A = ( ( 3.14 * pow ( a , 2 ) * pow ( b , 2 ) ) / ( 4 * ( pow ( a , 2 ) + pow ( b , 2 ) ) ) ) NEW_LINE return A NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 8 NEW_LINE b = 10 NEW_LINE print ( circlearea ( a , b ) ) NEW_LINE DEDENT
The biggest possible circle that can be inscribed in a rectangle | Function to find the area of the biggest circle ; the length and breadth cannot be negative ; area of the circle ; Driver code
def circlearea ( l , b ) : NEW_LINE INDENT if ( l < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( l < b ) : NEW_LINE INDENT return 3.14 * pow ( l // 2 , 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 3.14 * pow ( b // 2 , 2 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 4 NEW_LINE b = 8 NEW_LINE print ( circlearea ( l , b ) ) NEW_LINE DEDENT
Centered cube number | Centered cube number function ; Formula to calculate nth Centered cube number return it into main function . ; Driver Code
def centered_cube ( n ) : NEW_LINE INDENT return ( 2 * n + 1 ) * ( n * n + n + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( n , " th ▁ Centered ▁ cube ▁ " + " number ▁ : ▁ " , centered_cube ( n ) ) NEW_LINE n = 10 NEW_LINE print ( n , " th ▁ Centered ▁ cube ▁ " + " number ▁ : ▁ " , centered_cube ( n ) ) NEW_LINE DEDENT
Find the center of the circle using endpoints of diameter | Function to find the center of the circle ; Driver Code
def center ( x1 , x2 , y1 , y2 ) : NEW_LINE INDENT print ( int ( ( x1 + x2 ) / 2 ) , end = " " ) NEW_LINE print ( " , " , int ( ( y1 + y2 ) / 2 ) ) NEW_LINE DEDENT x1 = - 9 ; y1 = 3 ; x2 = 5 ; y2 = - 7 NEW_LINE center ( x1 , x2 , y1 , y2 ) NEW_LINE
Program to calculate volume of Octahedron | Python3 Program to calculate volume of Octahedron ; utility Function ; Driver Function
import math NEW_LINE def vol_of_octahedron ( side ) : NEW_LINE INDENT return ( ( side * side * side ) * ( math . sqrt ( 2 ) / 3 ) ) NEW_LINE DEDENT side = 3 NEW_LINE print ( " Volume ▁ of ▁ octahedron ▁ = " , round ( vol_of_octahedron ( side ) , 4 ) ) NEW_LINE
Program to calculate volume of Ellipsoid | Python3 program to Volume of ellipsoid ; Function To calculate Volume ; Driver Code
import math NEW_LINE def volumeOfEllipsoid ( r1 , r2 , r3 ) : NEW_LINE INDENT return 1.33 * math . pi * r1 * r2 * r3 NEW_LINE DEDENT r1 = float ( 2.3 ) NEW_LINE r2 = float ( 3.4 ) NEW_LINE r3 = float ( 5.7 ) NEW_LINE print ( " Volume ▁ of ▁ ellipsoid ▁ is ▁ : ▁ " , volumeOfEllipsoid ( r1 , r2 , r3 ) ) NEW_LINE
Program to calculate Area Of Octagon | Python3 program to find area of octagon ; Utility function ; Driver function
import math NEW_LINE def areaOctagon ( side ) : NEW_LINE INDENT return ( 2 * ( 1 + ( math . sqrt ( 2 ) ) ) * side * side ) NEW_LINE DEDENT side = 4 NEW_LINE print ( " Area ▁ of ▁ Regular ▁ Octagon ▁ = " , round ( areaOctagon ( side ) , 4 ) ) NEW_LINE
Program for Volume and Surface Area of Cube | utility function ; driver function
def areaCube ( a ) : NEW_LINE INDENT return ( a * a * a ) NEW_LINE DEDENT def surfaceCube ( a ) : NEW_LINE INDENT return ( 6 * a * a ) NEW_LINE DEDENT a = 5 NEW_LINE print ( " Area ▁ = " , areaCube ( a ) ) NEW_LINE print ( " Total ▁ surface ▁ area ▁ = " , surfaceCube ( a ) ) NEW_LINE
Find mirror image of a point in 2 | Python function which finds coordinates of mirror image . This function return a pair of double ; Driver code to test above function
def mirrorImage ( a , b , c , x1 , y1 ) : NEW_LINE INDENT temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) NEW_LINE x = temp * a + x1 NEW_LINE y = temp * b + y1 NEW_LINE return ( x , y ) NEW_LINE DEDENT a = - 1.0 NEW_LINE b = 1.0 NEW_LINE c = 0.0 NEW_LINE x1 = 1.0 NEW_LINE y1 = 0.0 NEW_LINE x , y = mirrorImage ( a , b , c , x1 , y1 ) ; NEW_LINE print ( " Image ▁ of ▁ point ▁ ( " + str ( x1 ) + " , ▁ " + str ( y1 ) + " ) ▁ " ) NEW_LINE print ( " by ▁ mirror ▁ ( " + str ( a ) + " ) x ▁ + ▁ ( " + str ( b ) + " ) y ▁ + ▁ ( " + str ( c ) + " ) ▁ = ▁ 0 , ▁ is ▁ : " ) NEW_LINE print ( " ( " + str ( x ) + " , ▁ " + str ( y ) + " ) " ) NEW_LINE
Minimum revolutions to move center of a circle to a target | Python program to find minimum number of revolutions to reach a target center ; Minimum revolutions to move center from ( x1 , y1 ) to ( x2 , y2 ) ; Driver code
import math NEW_LINE def minRevolutions ( r , x1 , y1 , x2 , y2 ) : NEW_LINE INDENT d = math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) NEW_LINE return math . ceil ( d / ( 2 * r ) ) NEW_LINE DEDENT r = 2 NEW_LINE x1 = 0 NEW_LINE y1 = 0 NEW_LINE x2 = 0 NEW_LINE y2 = 4 NEW_LINE print ( minRevolutions ( r , x1 , y1 , x2 , y2 ) ) NEW_LINE
Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | limit for float comparison define eps 1e-6 ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver code to test above methods
import math NEW_LINE def getArea ( base , hypotenuse ) : NEW_LINE INDENT height = math . sqrt ( hypotenuse * hypotenuse - base * base ) ; NEW_LINE return 0.5 * base * height NEW_LINE DEDENT def printRightAngleTriangle ( hypotenuse , area ) : NEW_LINE INDENT hsquare = hypotenuse * hypotenuse NEW_LINE sideForMaxArea = math . sqrt ( hsquare / 2.0 ) NEW_LINE maxArea = getArea ( sideForMaxArea , hypotenuse ) NEW_LINE if ( area > maxArea ) : NEW_LINE INDENT print ( " Not ▁ possiblen " ) NEW_LINE return NEW_LINE DEDENT low = 0.0 NEW_LINE high = sideForMaxArea NEW_LINE while ( abs ( high - low ) > 1e-6 ) : NEW_LINE INDENT base = ( low + high ) / 2.0 NEW_LINE if ( getArea ( base , hypotenuse ) >= area ) : NEW_LINE INDENT high = base NEW_LINE DEDENT else : NEW_LINE INDENT low = base NEW_LINE DEDENT DEDENT height = math . ceil ( math . sqrt ( hsquare - base * base ) ) NEW_LINE base = math . floor ( base ) NEW_LINE print ( base , height ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT hypotenuse = 5 NEW_LINE area = 6 NEW_LINE printRightAngleTriangle ( hypotenuse , area ) NEW_LINE DEDENT
Circle and Lattice Points | Python3 program to find countLattice podefs on a circle ; Function to count Lattice podefs on a circle ; Initialize result as 4 for ( r , 0 ) , ( - r . 0 ) , ( 0 , r ) and ( 0 , - r ) ; Check every value that can be potential x ; Find a potential y ; checking whether square root is an defeger or not . Count increments by 4 for four different quadrant values ; Driver program
import math NEW_LINE def countLattice ( r ) : NEW_LINE INDENT if ( r <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT result = 4 NEW_LINE for x in range ( 1 , r ) : NEW_LINE INDENT ySquare = r * r - x * x NEW_LINE y = int ( math . sqrt ( ySquare ) ) NEW_LINE if ( y * y == ySquare ) : NEW_LINE INDENT result += 4 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT r = 5 NEW_LINE print ( countLattice ( r ) ) NEW_LINE
Count of subarrays of size K with average at least M | Function to count the subarrays of size K having average at least M ; Stores the resultant count of subarray ; Stores the sum of subarrays of size K ; Add the values of first K elements to the sum ; Increment the count if the current subarray is valid ; Traverse the given array ; Find the updated sum ; Check if current subarray is valid or not ; Return the count of subarrays ; Driver Code
def countSubArrays ( arr , N , K , M ) : NEW_LINE INDENT count = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if sum >= K * M : NEW_LINE INDENT count += 1 NEW_LINE DEDENT for i in range ( K , N ) : NEW_LINE INDENT sum += ( arr [ i ] - arr [ i - K ] ) NEW_LINE if sum >= K * M : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 3 , 2 , 1 , 3 , 9 ] NEW_LINE K = 2 NEW_LINE M = 4 NEW_LINE N = len ( arr ) NEW_LINE count = countSubArrays ( arr , N , K , M ) NEW_LINE print ( count ) NEW_LINE DEDENT
Count of Ks in the Array for a given range of indices after array updates for Q queries | ''Function to perform all the queries ; ''Stores the count of 0s ; ''Count the number of 0s for  query of type 1 ; '' Update the array element for  query of type 2 ; ''Driver Code
def performQueries ( n , q , k , arr , query ) : NEW_LINE INDENT for i in range ( 1 , q + 1 , 1 ) : NEW_LINE count = 0 NEW_LINE if ( query [ i - 1 ] [ 0 ] == 1 ) : NEW_LINE DEDENT for j in range ( query [ i - 1 ] [ 1 ] , query [ i - 1 ] [ 2 ] + 1 , 1 ) : NEW_LINE INDENT if ( arr [ j ] == k ) : NEW_LINE DEDENT count += 1 NEW_LINE INDENT print ( count ) NEW_LINE else : NEW_LINE DEDENT arr [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 ] NEW_LINE Q = 5 NEW_LINE INDENT query = [ [ 1 , 5 , 14 ] , [ 2 , 6 , 1 ] , [ 1 , 0 , 8 ] , [ 2 , 13 , 0 ] , [ 1 , 6 , 18 ] ] NEW_LINE DEDENT N = len ( arr ) NEW_LINE DEDENT K = 0 NEW_LINE INDENT performQueries ( N , Q , K , arr , query ) NEW_LINE DEDENT
Minimum count of consecutive integers till N whose bitwise AND is 0 with N | Function to count the minimum count of integers such that bitwise AND of that many consecutive elements is equal to 0 ; Stores the binary representation of N ; Excludes first two characters "0b " ; Stores the MSB bit ; Stores the count of numbers ; Return res ; Driver Code Given Input ; Function Call
def count ( N ) : NEW_LINE INDENT a = bin ( N ) NEW_LINE a = a [ 2 : ] NEW_LINE m = len ( a ) - 1 NEW_LINE res = N - ( 2 ** m - 1 ) NEW_LINE return res NEW_LINE DEDENT N = 18 NEW_LINE print ( count ( N ) ) NEW_LINE
Find smallest value of K such that bitwise AND of numbers in range [ N , N | Function is to find the largest no which gives the sequence n & ( n - 1 ) & ( n - 2 ) & ... . . & ( n - k ) = 0. ; Since , we need the largest no , we start from n itself , till 0 ; Driver Code
def findSmallestNumK ( n ) : NEW_LINE INDENT cummAnd = n NEW_LINE i = n - 1 NEW_LINE while ( cummAnd != 0 ) : NEW_LINE INDENT cummAnd = cummAnd & i NEW_LINE if ( cummAnd == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 17 NEW_LINE lastNum = findSmallestNumK ( N ) ; NEW_LINE K = lastNum if lastNum == - 1 else N - lastNum NEW_LINE print ( K ) NEW_LINE DEDENT
Program to find the value of P ( N + r ) for a polynomial of a degree N such that P ( i ) = 1 for 1 Ã ¢ â €°¤ i Ã ¢ â €°¤ N and P ( N + 1 ) = a | ''Function to calculate factorial of N ; '' Base Case ; '' Otherwise, recursively  calculate the factorial ; ''Function to find the value of P(n + r) for polynomial P(X) ; ''Stores the value of k ; '' Store the required answer ; '' Iterate in the range [1, N] and  multiply (n + r - i) with answer ; '' Add the constant value C as 1 ; '' Return the result ; ''Driver Code
def fact ( n ) : NEW_LINE INDENT if n == 1 or n == 0 : NEW_LINE return 1 NEW_LINE else : NEW_LINE return n * fact ( n - 1 ) NEW_LINE DEDENT def findValue ( n , r , a ) : NEW_LINE INDENT k = ( a - 1 ) // fact ( n ) NEW_LINE answer = k NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE answer = answer * ( n + r - i ) NEW_LINE DEDENT answer = answer + 1 NEW_LINE INDENT return answer NEW_LINE DEDENT N = 1 NEW_LINE A = 2 NEW_LINE R = 3 NEW_LINE print ( findValue ( N , R , A ) ) NEW_LINE
Find winner in game of N balls , in which a player can remove any balls in range [ A , B ] in a single move | Function to find the winner of the game ; Stores sum of A and B ; If N is of the form m * ( A + B ) + y ; Otherwise , ; Input ; Function call
def NimGame ( N , A , B ) : NEW_LINE INDENT sum = A + B NEW_LINE if ( N % sum <= A - 1 ) : NEW_LINE INDENT return " Bob " NEW_LINE DEDENT else : NEW_LINE INDENT return " Alice " NEW_LINE DEDENT DEDENT N = 3 NEW_LINE A = 1 NEW_LINE B = 2 NEW_LINE print ( NimGame ( N , A , B ) ) NEW_LINE
Find Nth number in a sequence which is not a multiple of a given number | Python3 program for the above approach ; Function to find Nth number not a multiple of A in range [ L , R ] ; Calculate the Nth no ; Check for the edge case ;  ; Input parameters ; Function Call
import math NEW_LINE def countNo ( A , N , L , R ) : NEW_LINE INDENT ans = L - 1 + N + math . floor ( ( N - 1 ) / ( A - 1 ) ) NEW_LINE if ans % A == 0 : NEW_LINE INDENT ans = ans + 1 ; NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT / * Driver Code * / NEW_LINE A , N , L , R = 5 , 10 , 4 , 20 NEW_LINE countNo ( A , N , L , R ) NEW_LINE
Count of paths in given Binary Tree with odd bitwise AND for Q queries | Function to count number of paths in binary tree such that bitwise AND of all nodes is Odd ; vector dp to store the count of bitwise odd paths till that vertex ; Precomputing for each value ; Check for odd value ; Number of odd elements will be + 1 till the parent node ; For even case ; Since node is even Number of odd elements will be 0 ; Even value node will not contribute in answer hence dp [ i ] = previous answer ; Printing the answer for each query ; Vector to store queries
def compute ( query ) : NEW_LINE INDENT v = [ None ] * 100001 NEW_LINE dp = [ None ] * 100001 NEW_LINE v [ 1 ] = 1 NEW_LINE v [ 2 ] = 0 NEW_LINE dp [ 1 ] = 0 NEW_LINE dp [ 2 ] = 0 NEW_LINE for i in range ( 3 , 100001 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT if ( ( i // 2 ) % 2 == 0 ) : NEW_LINE INDENT v [ i ] = 1 NEW_LINE dp [ i ] = dp [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT v [ i ] = v [ i // 2 ] + 1 NEW_LINE dp [ i ] = dp [ i - 1 ] + v [ i ] - 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT v [ i ] = 0 NEW_LINE dp [ i ] = dp [ i - 1 ] NEW_LINE DEDENT DEDENT for x in query : NEW_LINE INDENT print ( dp [ x ] ) NEW_LINE DEDENT DEDENT query = [ 5 , 2 ] NEW_LINE compute ( query ) NEW_LINE