codeparrot/xlcost-text-to-code huggingface dataset:
Forming triangles using points on a square | Function to return the count of possible triangles ; Driver code
def noOfTriangles ( n ) : NEW_LINE INDENT y = 4 * n NEW_LINE return ( ( y * ( y - 2 ) * ( y - 1 ) ) - ( 4 * n * ( n - 2 ) * ( n - 1 ) ) ) // 6 NEW_LINE DEDENT n = 1 NEW_LINE print ( noOfTriangles ( n ) ) NEW_LINE
Angle subtended by an arc at the centre of a circle | Function to find Angle subtended by an arc at the centre of a circle ; Driver code
def angle ( n ) : NEW_LINE INDENT return 2 * n NEW_LINE DEDENT n = 30 NEW_LINE print ( angle ( n ) ) NEW_LINE
Check if two given Circles are Orthogonal or not | Function to Check if the given circles are orthogonal ; calculating the square of the distance between C1 and C2 ; Check if the given circles are orthogonal ; Driver code
def orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) : NEW_LINE INDENT dsquare = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; NEW_LINE if ( dsquare == r1 * r1 + r2 * r2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT x1 , y1 = 4 , 3 NEW_LINE x2 , y2 = 0 , 1 NEW_LINE r1 , r2 = 2 , 4 NEW_LINE f = orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) NEW_LINE if ( f ) : NEW_LINE INDENT print ( " Given ▁ circles ▁ are ▁ orthogonal . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Given ▁ circles ▁ are ▁ not ▁ orthogonal . " ) NEW_LINE DEDENT
Check if a right | Storing all the possible changes to make the triangle right - angled ; Function to check if the triangle is right - angled or not ; Function to check if the triangle can be transformed to right - angled ; Boolean variable to return true or false ; If it is already right - angled ; Applying the changes on the co - ordinates ; Applying on the first co - ordinate ; Applying on the second co - ordinate ; Applying on the third co - ordinate ; If can 't be transformed ; Driver Code
dx = [ - 1 , 0 , 1 , 0 ] NEW_LINE dy = [ 0 , 1 , 0 , - 1 ] NEW_LINE def ifRight ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT a = ( ( x1 - x2 ) * ( x1 - x2 ) ) + ( ( y1 - y2 ) * ( y1 - y2 ) ) NEW_LINE b = ( ( x1 - x3 ) * ( x1 - x3 ) ) + ( ( y1 - y3 ) * ( y1 - y3 ) ) NEW_LINE c = ( ( x2 - x3 ) * ( x2 - x3 ) ) + ( ( y2 - y3 ) * ( y2 - y3 ) ) NEW_LINE if ( ( a == ( b + c ) and a != 0 and b != 0 and c != 0 ) or ( b == ( a + c ) and a != 0 and b != 0 and c != 0 ) or ( c == ( a + b ) and a != 0 and b != 0 and c != 0 ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT def isValidCombination ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT x , y = 0 , 0 NEW_LINE possible = 0 NEW_LINE if ( ifRight ( x1 , y1 , x2 , y2 , x3 , y3 ) ) : NEW_LINE INDENT print ( " ALREADY ▁ RIGHT ▁ ANGLED " ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 4 ) : NEW_LINE INDENT x = dx [ i ] + x1 NEW_LINE y = dy [ i ] + y1 NEW_LINE if ( ifRight ( x , y , x2 , y2 , x3 , y3 ) ) : NEW_LINE INDENT print ( " POSSIBLE " ) NEW_LINE return NEW_LINE DEDENT x = dx [ i ] + x2 NEW_LINE y = dy [ i ] + y2 NEW_LINE if ( ifRight ( x1 , y1 , x , y , x3 , y3 ) ) : NEW_LINE INDENT print ( " POSSIBLE " ) NEW_LINE return NEW_LINE DEDENT x = dx [ i ] + x3 NEW_LINE y = dy [ i ] + y3 NEW_LINE if ( ifRight ( x1 , y1 , x2 , y2 , x , y ) ) : NEW_LINE INDENT print ( " POSSIBLE " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT if ( possible == 0 ) : NEW_LINE INDENT print ( " NOT ▁ POSSIBLE " ) NEW_LINE DEDENT DEDENT x1 = - 49 NEW_LINE y1 = 0 NEW_LINE x2 = 0 NEW_LINE y2 = 50 NEW_LINE x3 = 0 NEW_LINE y3 = - 50 NEW_LINE isValidCombination ( x1 , y1 , x2 , y2 , x3 , y3 ) NEW_LINE
Program to find Area of Triangle inscribed in N | Python3 Program to find the area of a triangle inscribed in N - sided regular polygon ; Function to find the area of the polygon ; area of a regular polygon with N sides and side length len ; Function to find the area of a triangle ; area of one triangle in an N - sided regular polygon ; area of inscribed triangle ; Driver code
import math NEW_LINE def area_of_regular_polygon ( n , len ) : NEW_LINE INDENT P = ( len * n ) ; NEW_LINE A = len / ( 2 * math . tan ( ( 180 / n ) * 3.14159 / 180 ) ) NEW_LINE area = ( P * A ) / 2 NEW_LINE return area NEW_LINE DEDENT def area_of_triangle_inscribed ( n , len ) : NEW_LINE INDENT area = area_of_regular_polygon ( n , len ) NEW_LINE triangle = area / n NEW_LINE ins_tri = ( triangle * 3 ) ; NEW_LINE return ins_tri NEW_LINE DEDENT n = 6 NEW_LINE len = 10 NEW_LINE print ( round ( area_of_triangle_inscribed ( n , len ) , 3 ) ) NEW_LINE
Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given | Function to find the area of quadrilateral ; Driver code
def Area ( d , h1 , h2 ) : NEW_LINE INDENT area = 0.5 * d * ( h1 + h2 ) ; NEW_LINE return area ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT d = 6 ; NEW_LINE h1 = 4 ; NEW_LINE h2 = 3 ; NEW_LINE print ( " Area ▁ of ▁ Quadrilateral ▁ = ▁ " , ( Area ( d , h1 , h2 ) ) ) ; NEW_LINE DEDENT
Find the diagonal of the Cube | Python3 program to find length of the diagonal of the cube ; Function to find length of diagonal of cube ; Formula to Find length of diagonal of cube ; Driver code ; Function call
from math import sqrt NEW_LINE def diagonal_length ( a ) : NEW_LINE INDENT L = 0 NEW_LINE L = a * sqrt ( 3 ) NEW_LINE return L NEW_LINE DEDENT a = 5 NEW_LINE print ( diagonal_length ( a ) ) NEW_LINE
Concentric Hexagonal Numbers | Function to find nth concentric hexagon number ; Driver code ; Function call
def concentric_Hexagon ( n ) : NEW_LINE INDENT return 3 * pow ( n , 2 ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( concentric_Hexagon ( n ) ) NEW_LINE
Find area of the larger circle when radius of the smaller circle and difference in the area is given | Python 3 implementation of the approach ; Function to return the area of the bigger circle ; Find the radius of the bigger circle ; Calculate the area of the bigger circle ; Driver code
PI = 3.14 NEW_LINE from math import pow , sqrt NEW_LINE def find_area ( r , d ) : NEW_LINE INDENT R = d / PI NEW_LINE R += pow ( r , 2 ) NEW_LINE R = sqrt ( R ) NEW_LINE area = PI * pow ( R , 2 ) NEW_LINE return area NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT r = 4 NEW_LINE d = 5 NEW_LINE print ( find_area ( r , d ) ) NEW_LINE DEDENT
Check whether Quadrilateral is valid or not if angles are given | Function to check if a given quadrilateral is valid or not ; Check condition ; Driver code
def Valid ( a , b , c , d ) : NEW_LINE INDENT if ( a + b + c + d == 360 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT a = 80 ; b = 70 ; c = 100 ; d = 110 ; NEW_LINE if ( Valid ( a , b , c , d ) ) : NEW_LINE INDENT print ( " Valid ▁ quadrilateral " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid ▁ quadrilateral " ) ; NEW_LINE DEDENT
Central angle of a N sided Regular Polygon | Function to calculate central angle of a polygon ; Calculate the angle ; Driver code
def calculate_angle ( n ) : NEW_LINE INDENT total_angle = 360 ; NEW_LINE return ( total_angle // n ) NEW_LINE DEDENT N = 5 NEW_LINE print ( calculate_angle ( N ) ) NEW_LINE
Program to calculate the area of Kite | Function to return the area of kite ; use above formula ; Driver code
def areaOfKite ( d1 , d2 ) : NEW_LINE INDENT area = ( d1 * d2 ) / 2 ; NEW_LINE return area ; NEW_LINE DEDENT d1 = 4 ; NEW_LINE d2 = 6 ; NEW_LINE print ( " Area ▁ of ▁ Kite ▁ = ▁ " , areaOfKite ( d1 , d2 ) ) ; NEW_LINE
Program to calculate the area of Kite | Python implementation of the approach ; Function to return the area of the kite ; convert angle degree to radians ; use above formula ; Driver code
import math NEW_LINE PI = 3.14159 / 180 ; NEW_LINE def areaOfKite ( a , b , angle ) : NEW_LINE INDENT angle = angle * PI ; NEW_LINE area = a * b * math . sin ( angle ) ; NEW_LINE return area ; NEW_LINE DEDENT a = 4 ; b = 7 ; angle = 78 ; NEW_LINE print ( " Area ▁ of ▁ Kite ▁ = ▁ " , areaOfKite ( a , b , angle ) ) ; NEW_LINE
Program to calculate angle on circumference subtended by the chord when the central angle subtended by the chord is given | Python3 Program to calculate angle on the circumference subtended by the chord when the central angle subtended by the chord is given ; Angle on center
def angleOncirCumference ( z ) : NEW_LINE INDENT return ( z / 2 ) ; NEW_LINE DEDENT angle = 65 ; NEW_LINE z = angleOncirCumference ( angle ) ; NEW_LINE print ( " The ▁ angle ▁ is " , ( z ) , " degrees " ) ; NEW_LINE
Maximum and Minimum value of a quadratic function | Function to print the Maximum and Minimum values of the quadratic function ; Calculate the value of second part ; Print the values ; Open upward parabola function ; Open downward parabola function ; If a = 0 then it is not a quadratic function ; Driver code
def PrintMaxMinValue ( a , b , c ) : NEW_LINE INDENT secondPart = c * 1.0 - ( b * b / ( 4.0 * a ) ) ; NEW_LINE if ( a > 0 ) : NEW_LINE INDENT print ( " Maxvalue ▁ = " , " Infinity " ) ; NEW_LINE print ( " Minvalue ▁ = ▁ " , secondPart ) ; NEW_LINE DEDENT elif ( a < 0 ) : NEW_LINE INDENT print ( " Maxvalue ▁ = ▁ " , secondPart ) ; NEW_LINE print ( " Minvalue ▁ = " , " - Infinity " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ quadratic ▁ function " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = - 1 ; b = 3 ; c = - 2 ; NEW_LINE PrintMaxMinValue ( a , b , c ) ; NEW_LINE DEDENT
Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant | Python3 program to find percentage increase in the cylinder if the height is increased by given percentage but radius remains constant ; Driver code
def newvol ( x ) : NEW_LINE INDENT print ( " percentage ▁ increase ▁ in ▁ the ▁ volume ▁ of ▁ the ▁ cylinder ▁ is ▁ " , x , " % " ) NEW_LINE DEDENT x = 10.0 NEW_LINE newvol ( x ) NEW_LINE
Find the side of the squares which are inclined diagonally and lined in a row | Python program to find side of the squares inclined and touch each other externally at vertices and are lined in a row and distance between the centers of first and last squares is given ; Driver code
def radius ( n , d ) : NEW_LINE INDENT print ( " The ▁ side ▁ of ▁ each ▁ square ▁ is ▁ " , d / ( ( n - 1 ) * ( 2 ** ( 1 / 2 ) ) ) ) ; NEW_LINE DEDENT d = 42 ; n = 4 ; NEW_LINE radius ( n , d ) ; NEW_LINE
Program to calculate area of inner circle which passes through center of outer circle and touches its circumference | Function calculate the area of the inner circle ; the radius cannot be negative ; area of the circle ; Driver Code
def innerCirclearea ( radius ) : NEW_LINE INDENT if ( radius < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT r = radius / 2 ; NEW_LINE Area = ( 3.14 * pow ( r , 2 ) ) ; NEW_LINE return Area ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT radius = 4 ; NEW_LINE print ( " Area ▁ of ▁ circle ▁ c2 ▁ = " , innerCirclearea ( radius ) ) ; NEW_LINE DEDENT
Angle subtended by the chord when the angle subtended by another chord of same length is given | Python3 program to find the angle subtended at the center by the chord when the angle subtended at center by another chord of equal length is given ; Driver code
def angleequichord ( z ) : NEW_LINE INDENT print ( " The ▁ angle ▁ subtended ▁ at " , " the ▁ center ▁ is " , z , " degrees " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT z = 48 ; NEW_LINE angleequichord ( z ) ; NEW_LINE DEDENT
Distance of chord from center when distance between center and another equal length chord is given | Python 3 program to find the distance of chord from center when distance between center and another equal length chord is given ; Driver code
def lengequichord ( z ) : NEW_LINE INDENT print ( " The ▁ distance ▁ between ▁ the " , " chord ▁ and ▁ the ▁ center ▁ is " , z ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT z = 48 NEW_LINE lengequichord ( z ) NEW_LINE DEDENT
Length of the perpendicular bisector of the line joining the centers of two circles | Python program to find the Length of the perpendicular bisector of the line joining the centers of two circles in which one lies completely inside touching the bigger circle at one point ; Driver code
def lengperpbisect ( r1 , r2 ) : NEW_LINE INDENT z = 2 * ( ( ( r1 * r1 ) - ( ( r1 - r2 ) * ( r1 - r2 ) / 4 ) ) ** ( 1 / 2 ) ) ; NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ perpendicular ▁ bisector ▁ is ▁ " , z ) ; NEW_LINE DEDENT r1 = 5 ; r2 = 3 ; NEW_LINE lengperpbisect ( r1 , r2 ) ; NEW_LINE
Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given | Python3 program to find the length of the chord of the circle if length of the other chord which is equally inclined through the diameter is given ; Driver code
def lengchord ( z ) : NEW_LINE INDENT print ( " The ▁ length ▁ is ▁ " , end = " " ) ; NEW_LINE print ( z ) ; NEW_LINE DEDENT z = 48 ; NEW_LINE lengchord ( z ) ; NEW_LINE
Distance between centers of two intersecting circles if the radii and common chord length is given | Python program to find the distance between centers of two intersecting circles if the radii and common chord length is given ; Driver code
def distcenter ( r1 , r2 , x ) : NEW_LINE INDENT z = ( ( ( r1 * r1 ) - ( x / 2 * x / 2 ) ) ** ( 1 / 2 ) ) + ( ( ( r2 * r2 ) - ( x / 2 * x / 2 ) ) ** ( 1 / 2 ) ) ; NEW_LINE print ( " distance ▁ between ▁ thecenters ▁ is ▁ " , end = " " ) ; NEW_LINE print ( int ( z ) ) ; NEW_LINE DEDENT r1 = 24 ; r2 = 37 ; x = 40 ; NEW_LINE distcenter ( r1 , r2 , x ) ; NEW_LINE
Exterior angle of a cyclic quadrilateral when the opposite interior angle is given | Python program to find the exterior angle of a cyclic quadrilateral when the opposite interior angle is given ; Driver code
def angleextcycquad ( z ) : NEW_LINE INDENT print ( " The ▁ exterior ▁ angle ▁ of ▁ the " , end = " " ) ; NEW_LINE print ( " cyclic ▁ quadrilateral ▁ is ▁ " , end = " " ) ; NEW_LINE print ( z , " ▁ degrees " ) ; NEW_LINE DEDENT z = 48 ; NEW_LINE angleextcycquad ( z ) ; NEW_LINE
Angle between a chord and a tangent when angle in the alternate segment is given | Python3 program to find the angle between a chord and a tangent when angle in the alternate segment is given ; Driver code
def anglechordtang ( z ) : NEW_LINE INDENT print ( " The ▁ angle ▁ between ▁ tangent " , " and ▁ the ▁ chord ▁ is " , z , " degrees " ) ; NEW_LINE DEDENT z = 48 ; NEW_LINE anglechordtang ( z ) ; NEW_LINE
Print all Perfect Numbers from an array whose sum of digits is also a Perfect Number | Function to check if a number is perfect number or not ; Stores sum of proper divisors ; If sum of digits is equal to N , then it 's a perfect number ; Otherwise , not a perfect number ; Function to find the sum of digits of a number ; Stores sum of digits ; Return sum of digits ; Function to count perfect numbers from an array whose sum of digits is also perfect ; Traverse the array ; If number is perfect ; Stores sum of digits of the number ; If that is also perfect number ; Print that number ; Driver Code ; Given array ; Size of the array ; Function call to count perfect numbers having sum of digits also perfect
def isPerfect ( N ) : NEW_LINE INDENT sumOfDivisors = 1 ; NEW_LINE for i in range ( 2 , int ( N / 2 ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT sumOfDivisors += i ; NEW_LINE DEDENT DEDENT if ( sumOfDivisors == N ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def sumOfDigits ( N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( N != 0 ) : NEW_LINE INDENT sum += ( N % 10 ) ; NEW_LINE N = N // 10 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def countPerfectNumbers ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT if ( isPerfect ( arr [ i ] ) ) : NEW_LINE INDENT sum = sumOfDigits ( arr [ i ] ) ; NEW_LINE if ( isPerfect ( sum ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 8 , 12 , 28 , 6 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE countPerfectNumbers ( arr , N ) ; NEW_LINE DEDENT
Sum of Bitwise AND of the sum of all leaf and non | Structure of a Binary tree node ; Helper function to allocate a new node with the given data and left and right pointers as None ; Function to calculate the sum of bitwise AND of the sum of all leaf nodes and non - leaf nodes for each level ; Initialize a queue and append root to it ; Store the required answer ; Stores the sum of leaf nodes at the current level ; Stores the sum of non - leaf nodes at the current level ; Get the size of the queue ; Iterate for all the nodes in the queue currently ; Dequeue a node from queue ; Check if the node is a leaf node ; If true , update the leaf node sum ; Otherwise , update the non - leaf node sum ; Enqueue left and right children of removed node ; Update the answer ; Return the answer ; Given Tree ; Function Call
class TreeNode : NEW_LINE INDENT def __init__ ( self , val = 0 , left = None , right = None ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = left NEW_LINE self . right = right NEW_LINE DEDENT DEDENT def findSum ( root ) : NEW_LINE INDENT que = [ root ] NEW_LINE ans = 0 NEW_LINE while ( len ( que ) ) : NEW_LINE INDENT leaf = 0 NEW_LINE nonleaf = 0 NEW_LINE length = len ( que ) NEW_LINE while length : NEW_LINE INDENT temp = que . pop ( 0 ) NEW_LINE if not temp . left and not temp . right : NEW_LINE INDENT leaf += temp . val NEW_LINE DEDENT else : NEW_LINE INDENT nonleaf += temp . val NEW_LINE DEDENT if temp . left : NEW_LINE INDENT que . append ( temp . left ) NEW_LINE DEDENT if temp . right : NEW_LINE INDENT que . append ( temp . right ) NEW_LINE DEDENT length -= 1 NEW_LINE DEDENT ans += leaf & nonleaf NEW_LINE DEDENT return ans NEW_LINE DEDENT root = TreeNode ( 5 ) NEW_LINE root . left = TreeNode ( 3 ) NEW_LINE root . right = TreeNode ( 9 ) NEW_LINE root . left . left = TreeNode ( 6 ) NEW_LINE root . left . right = TreeNode ( 4 ) NEW_LINE root . left . left . right = TreeNode ( 7 ) NEW_LINE print ( findSum ( root ) ) NEW_LINE
Minimize replacements to make every element in an array exceed every element in another given array | Function to find the minimize replacements to make every element in the array A [ ] strictly greater than every element in B [ ] or vice - versa ; Store the final result ; Create two arrays and initialize with 0 s ; Traverse the array a [ ] ; Increment prefix_a [ a [ i ] ] by 1 ; Traverse the array b [ ] ; Increment prefix_b [ b [ i ] ] by 1 ; Calculate prefix sum of the array a [ ] ; Calculate prefix sum of the array b [ ] ; Iterate over the range [ 0 , 9 ] ; Make every element in array a [ ] strictly greater than digit i and make every element in the array b [ ] less than digit i ; Make every element in array b [ ] strictly greater than digit i and make every element in array a [ ] less than digit i ; Print the answer ; Driver Code
def MinTime ( a , b , n , m ) : NEW_LINE INDENT ans = float ( ' inf ' ) NEW_LINE prefix_a = [ 0 ] * 10 NEW_LINE prefix_b = [ 0 ] * 10 NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix_a [ a [ i ] ] += 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT prefix_b [ b [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT prefix_a [ i ] += prefix_a [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT prefix_b [ i ] += prefix_b [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT ans = min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) NEW_LINE ans = min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT A = [ 0 , 0 , 1 , 3 , 3 ] NEW_LINE B = [ 2 , 0 , 3 ] NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE MinTime ( A , B , N , M ) NEW_LINE
Fizz Buzz Implementation | Set 2 | Function to generate FizzBuzz sequence ; Stores count of multiples of 3 and 5 respectively ; Iterate from 1 to N ; Increment count3 by 1 ; Increment count5 by 1 ; Initialize a boolean variable to check if none of the condition matches ; Check if the value of count3 is equal to 3 ; Reset count3 to 0 , and set flag as True ; Check if the value of count5 is equal to 5 ; Reset count5 to 0 , and set flag as True ; If none of the condition matches ; Driver Code
def fizzBuzz ( N ) : NEW_LINE INDENT count3 = 0 NEW_LINE count5 = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT count3 += 1 NEW_LINE count5 += 1 NEW_LINE flag = False NEW_LINE if ( count3 == 3 ) : NEW_LINE INDENT print ( " Fizz " , end = " " ) NEW_LINE count3 = 0 NEW_LINE flag = True NEW_LINE DEDENT if ( count5 == 5 ) : NEW_LINE INDENT print ( " Buzz " , end = " " ) NEW_LINE count5 = 0 NEW_LINE flag = True NEW_LINE DEDENT if ( not flag ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT print ( end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 NEW_LINE fizzBuzz ( N ) NEW_LINE DEDENT
Check if diagonal elements of a Matrix are Prime or not | Stores if a number is prime or not ; Function to generate and store primes using Sieve Of Eratosthenes ; Set all numbers as prime memset ( prime , true , sizeof ( prime ) ) ; If p is a prime ; Set all its multiples as non - prime ; Function to check if all diagonal elements are prime or not ; Stores if all diagonal elements are prime or not ; Precompute primes ; Traverse the range [ 0 , N - 1 ] ; Check if numbers on the cross diagonal and main diagonal are primes or not ; If true , then pr " Yes " ; Otherwise , pr " No " ; Driver Code ; Function Call
prime = [ True ] * 1000005 NEW_LINE def SieveOfEratosthenes ( N ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , N + 1 ) : NEW_LINE INDENT if p * p > N : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , N + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def checkElementsOnDiagonal ( M , N ) : NEW_LINE INDENT flag = 1 NEW_LINE SieveOfEratosthenes ( 1000000 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT flag &= ( prime [ M [ i ] [ i ] ] and prime [ M [ i ] [ N - 1 - i ] ] ) 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 M = [ [ 1 , 2 , 3 , 13 ] , [ 5 , 3 , 7 , 8 ] , [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 7 ] ] NEW_LINE N = len ( M ) NEW_LINE checkElementsOnDiagonal ( M , N ) NEW_LINE DEDENT
Generate a circular permutation with number of mismatching bits between pairs of adjacent elements exactly 1 | Function to find the permutation of integers from a given range such that number of mismatching bits between pairs of adjacent elements is 1 ; Initialize an arrayList to store the resultant permutation ; Store the index of rotation ; Iterate over the range [ 0 , N - 1 ] ; Traverse all the array elements up to ( 2 ^ k ) - th index in reverse ; If current element is S ; Check if S is zero ; Rotate the array by index value to the left ; Driver Code
def circularPermutation ( n , start ) : NEW_LINE INDENT res = [ 0 ] NEW_LINE ret = [ ] NEW_LINE index , add = - 1 , 1 NEW_LINE for k in range ( n ) : NEW_LINE INDENT add = 1 << k NEW_LINE for i in range ( len ( res ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( res [ i ] + add == start ) : NEW_LINE INDENT index = len ( res ) NEW_LINE DEDENT res . append ( res [ i ] + add ) NEW_LINE DEDENT add = 1 << k NEW_LINE DEDENT if ( start == 0 ) : NEW_LINE INDENT return res NEW_LINE DEDENT while ( len ( ret ) < len ( res ) ) : NEW_LINE INDENT ret . append ( res [ index ] ) NEW_LINE index = ( index + 1 ) % len ( res ) NEW_LINE DEDENT return ret NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , S = 2 , 3 NEW_LINE print ( circularPermutation ( N , S ) ) NEW_LINE DEDENT