codeparrot/xlcost-text-to-code huggingface dataset:
Minimum Cost to make all array elements equal using given operations | Python3 implementation to find the minimum cost to make all array elements equal ; Checks if the value is less than middle element of the array ; Function that returns the cost of making all elements equal to current element ; Compute the lower bound of current element ; Calculate the requirement of add operation ; Calculate the requirement of subtract operation ; Compute minimum of left and right ; Computing the total cost of add and subtract operations ; Function that prints minimum cost of making all elements equal ; Sort the given array ; Calculate minimum from a + r and m ; Compute prefix sum and store in pref array ; Find the minimum cost from the given elements ; Finding the minimum cost from the other cases where minimum cost can occur ; Printing the minimum cost of making all elements equal ; Driver Code ; Function call
def lowerBound ( array , length , value ) : NEW_LINE INDENT low = 0 NEW_LINE high = length NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( value <= array [ mid ] ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT def costCalculation ( current , arr , n , pref , a , r , minimum ) : NEW_LINE INDENT index = lowerBound ( arr , len ( arr ) , current ) NEW_LINE left = index * current - pref [ index ] NEW_LINE right = ( pref [ n ] - pref [ index ] - ( n - index ) * current ) NEW_LINE res = min ( left , right ) NEW_LINE left -= res NEW_LINE right -= res NEW_LINE total = res * minimum NEW_LINE total += left * a NEW_LINE total += right * r NEW_LINE return total NEW_LINE DEDENT def solve ( arr , n , a , r , m ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE minimum = min ( a + r , m ) NEW_LINE pref = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT pref [ i + 1 ] = pref [ i ] + arr [ i ] NEW_LINE DEDENT ans = 10000 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = min ( ans , costCalculation ( arr [ i ] , arr , n , pref , a , r , minimum ) ) NEW_LINE DEDENT ans = min ( ans , costCalculation ( pref [ n ] // n , arr , n , pref , a , r , minimum ) ) NEW_LINE ans = min ( ans , costCalculation ( pref [ n ] // n + 1 , arr , n , pref , a , r , minimum ) ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 5 , 3 , 6 , 5 ] NEW_LINE A = 1 NEW_LINE R = 2 NEW_LINE M = 4 NEW_LINE size = len ( arr ) NEW_LINE solve ( arr , size , A , R , M ) NEW_LINE DEDENT
Count of integers up to N which represent a Binary number | Python3 program to count the number of integers upto N which are of the form of binary representations ; Function to return the count ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained ; Driver Code
from math import * NEW_LINE def countBinaries ( N ) : NEW_LINE INDENT ctr = 1 NEW_LINE ans = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N % 10 == 1 ) : NEW_LINE INDENT ans += pow ( 2 , ctr - 1 ) NEW_LINE DEDENT elif ( N % 10 > 1 ) : NEW_LINE INDENT ans = pow ( 2 , ctr ) - 1 NEW_LINE DEDENT ctr += 1 NEW_LINE N //= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 20 NEW_LINE print ( int ( countBinaries ( N ) ) ) NEW_LINE DEDENT
Count of integers up to N which represent a Binary number | Function to return the count ; PreCompute and store the powers of 2 ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained ; Driver code
def countBinaries ( N ) : NEW_LINE INDENT powersOfTwo = [ 0 ] * 11 NEW_LINE powersOfTwo [ 0 ] = 1 NEW_LINE for i in range ( 1 , 11 ) : NEW_LINE INDENT powersOfTwo [ i ] = powersOfTwo [ i - 1 ] * 2 NEW_LINE DEDENT ctr = 1 NEW_LINE ans = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N % 10 == 1 ) : NEW_LINE INDENT ans += powersOfTwo [ ctr - 1 ] NEW_LINE DEDENT elif ( N % 10 > 1 ) : NEW_LINE INDENT ans = powersOfTwo [ ctr ] - 1 NEW_LINE DEDENT ctr += 1 NEW_LINE N = N // 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 20 NEW_LINE print ( countBinaries ( N ) ) NEW_LINE
Find the sum of the first Nth Centered Hexadecagonal Number | Centered_Hexadecagonal number function ; Formula to calculate nth Centered_Hexadecagonal number & return it into main function . ; Function to find the sum of the first N Centered Hexadecagonal number ; Variable to store the sum ; Loop to iterate through the first N numbers ; Find the sum ; Driver Code ; display first Nth Centered_Hexadecagonal number
def Centered_Hexadecagonal_num ( n ) : NEW_LINE INDENT return ( 8 * n * n - 8 * n + 1 ) NEW_LINE DEDENT def sum_Centered_Hexadecagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_Hexadecagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_Hexadecagonal_num ( n ) ) NEW_LINE DEDENT
Find the sum of the first N Centered heptagonal number | Function to find N - th centered heptagonal number ; Formula to calculate nth centered heptagonal number ; Function to find the sum of the first N centered heptagonal numbers ; Variable to store the sum ; Iterate through the range 1 to N ; Driver code
def center_heptagonal_num ( n ) : NEW_LINE INDENT return ( 7 * n * n - 7 * n + 2 ) // 2 NEW_LINE DEDENT def sum_center_heptagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += center_heptagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_center_heptagonal_num ( n ) ) NEW_LINE DEDENT
Find the sum of the first N Centered Dodecagonal Number | Function to find the N - th Centered Dodecagonal number ; Formula to calculate nth Centered_Dodecagonal number ; Function to find the sum of the first N Centered_Dodecagonal number ; Variable to store the sum ; Iterating from 1 to N ; Finding the sum ; Driver code
def Centered_Dodecagonal_num ( n ) : NEW_LINE INDENT return 6 * n * ( n - 1 ) + 1 NEW_LINE DEDENT def sum_Centered_Dodecagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_Dodecagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_Dodecagonal_num ( n ) ) NEW_LINE DEDENT
Find the sum of the first N Centered Octagonal Number | Function to find N - th Centered Octagonal number ; Formula to calculate nth centered Octagonal number ; Function to find the sum of the first N Centered Octagonal numbers ; Variable to store the sum ; Iterating through the first N numbers ; Driver code
def center_Octagonal_num ( n ) : NEW_LINE INDENT return ( 4 * n * n - 4 * n + 1 ) NEW_LINE DEDENT def sum_center_Octagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += center_Octagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_center_Octagonal_num ( n ) ) NEW_LINE DEDENT
Find the sum of the first N Centered Decagonal Numbers | Function to find the N - th centred decagonal number ; Formula to calculate nth Centered_decagonal number & return it into main function . ; Function to find the sum of the first N Centered decagonal numbers ; Variable to store the sum ; Iterating through the range ; Driver code ; display first Nth Centered_decagonal number
def Centered_decagonal_num ( n ) : NEW_LINE INDENT return ( 5 * n * n - 5 * n + 1 ) NEW_LINE DEDENT def sum_Centered_decagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_decagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_decagonal_num ( n ) ) NEW_LINE DEDENT
Find the sum of the first N Centered Octadecagonal Numbers | Function to find the N - th Centered octadecagonal number ; Formula to calculate nth centered octadecagonal number ; Function to find the sum of the first N Centered octadecagonal numbers ; Variable to store the sum ; Iterating through the range 1 to N ; Driver code
def center_octadecagon_num ( n ) : NEW_LINE INDENT return ( 9 * n * n - 9 * n + 1 ) NEW_LINE DEDENT def sum_center_octadecagon_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += center_octadecagon_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( sum_center_octadecagon_num ( n ) ) NEW_LINE DEDENT
Find the sum of the first Nth Centered Pentadecagonal Number | Function to find the Centered_Pentadecagonal number ; Formula to calculate N - th Centered_Pentadecagonal number ; Function to find the sum of the first N Centered_Pentadecagonal numbers ; Variable to store the sum ; Driver code
def Centered_Pentadecagonal_num ( n ) : NEW_LINE INDENT return ( 15 * n * n - 15 * n + 2 ) // 2 NEW_LINE DEDENT def sum_Centered_Pentadecagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_Pentadecagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_Pentadecagonal_num ( n ) ) NEW_LINE DEDENT
Program to check if N is a Octagonal Number | Python3 program for the above approach ; Function to check if N is a octagonal number ; Condition to check if the number is a octagonal number ; Driver Code ; Given number ; Function call
from math import sqrt NEW_LINE def isoctagonal ( N ) : NEW_LINE INDENT n = ( 2 + sqrt ( 12 * N + 4 ) ) / 6 ; NEW_LINE return ( n - int ( n ) ) == 0 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 ; NEW_LINE if ( isoctagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Program to check if N is a Pentadecagonal Number | Python3 program for the above approach ; Function to check if N is a pentadecagon number ; Condition to check if the number is a pentadecagon number ; Driver Code ; Given number ; Function call
from math import sqrt NEW_LINE def isPentadecagon ( N ) : NEW_LINE INDENT n = ( 11 + sqrt ( 104 * N + 121 ) ) / 26 ; NEW_LINE return ( n - int ( n ) == 0 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 15 ; NEW_LINE if ( isPentadecagon ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Program to check if N is a Tetradecagonal Number | Python3 program for the above approach ; Function to check if N is a Tetradecagonal Number ; Condition to check if the number is a tetradecagonal number ; Given Number ; Function call
import math NEW_LINE def istetradecagonal ( N ) : NEW_LINE INDENT n = ( 10 + math . sqrt ( 96 * N + 100 ) ) / 24 NEW_LINE if ( n - int ( n ) ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT N = 11 NEW_LINE if ( istetradecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the sum of the first Nth Icosagonal Numbers | Function to calculate the N - th Icosagonal number ; Formula to calculate nth Icosagonal number & return it ; Function to find the sum of the first N Icosagonal numbers ; Variable to store the sum ; Loop to iterate through the first N values and find the sum of first N Icosagonal numbers ; function to get the Icosagonal_num ; Driver Code ; Display the sum of first N Icosagonal number
def Icosagonal_num ( n ) : NEW_LINE INDENT return ( 18 * n * n - 16 * n ) // 2 NEW_LINE DEDENT def sum_Icosagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Icosagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Icosagonal_num ( n ) ) NEW_LINE DEDENT
Find the sum of the first N Centered Pentagonal Number | Function to find the Centered_Pentagonal number ; Formula to calculate nth Centered_Pentagonal number & return it into main function . ; Function to find the sum of the first N Centered_Pentagonal numbers ; To get the sum ; Function to get the Centered_Pentagonal_num ; Driver Code ; display first Nth Centered_Pentagonal number
def Centered_Pentagonal_num ( n ) : NEW_LINE INDENT return ( 5 * n * n - 5 * n + 2 ) // 2 NEW_LINE DEDENT def sum_Centered_Pentagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_Pentagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_Pentagonal_num ( n ) ) NEW_LINE DEDENT
Find the sum of the first Nth Centered Tridecagonal Numbers | Function to calculate the N - th Centered tridecagonal number ; Formula to calculate Nth Centered tridecagonal number & return it ; Function to find the sum of the first N Centered tridecagonal numbers ; Variable to store the sum ; Loop to iterate and find the sum of first N Centered tridecagonal numbers ; Driver Code
def Centered_tridecagonal_num ( n ) : NEW_LINE INDENT return ( 13 * n * ( n - 1 ) + 2 ) // 2 NEW_LINE DEDENT def sum_Centered_tridecagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += Centered_tridecagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_Centered_tridecagonal_num ( n ) ) NEW_LINE DEDENT
Program to check if N is a Concentric Hexagonal Number | Python3 program to check if N is a concentric hexagonal number ; Function to check if the number is a concentric hexagonal number ; Condition to check if the number is a concentric hexagonal number ; Driver code ; Function call
import math NEW_LINE def isConcentrichexagonal ( N ) : NEW_LINE INDENT n = math . sqrt ( ( 2 * N ) / 3 ) NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT N = 6 NEW_LINE if isConcentrichexagonal ( N ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Count Sexy Prime Pairs in the given array | A utility function that find the Prime Numbers till N ; Resize the Prime Number ; Loop till sqrt ( N ) to find prime numbers and make their multiple false in the bool array Prime ; Function that returns the count of SPP ( Sexy Prime Pair ) Pairs ; Find the maximum element in the given array arr [ ] ; Function to calculate the prime numbers till N ; To store the count of pairs ; To store the frequency of element in the array arr [ ] ; Sort before traversing the array ; Traverse the array and find the pairs with SPP ( Sexy Prime Pair ) s ; If current element is Prime , then check for ( current element + 6 ) ; Return the count of pairs ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) s pair
def computePrime ( N ) : NEW_LINE INDENT Prime = [ True ] * ( N + 1 ) NEW_LINE Prime [ 0 ] = False NEW_LINE Prime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i <= N : NEW_LINE INDENT if ( Prime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , N , i ) : NEW_LINE INDENT Prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return Prime NEW_LINE DEDENT def countSexyPairs ( arr , n ) : NEW_LINE INDENT maxE = max ( arr ) NEW_LINE Prime = computePrime ( maxE ) NEW_LINE count = 0 NEW_LINE freq = [ 0 ] * ( maxE + 6 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( Prime [ arr [ i ] ] ) : NEW_LINE INDENT if ( ( arr [ i ] + 6 ) <= ( maxE ) and freq [ arr [ i ] + 6 ] > 0 and Prime [ arr [ i ] + 6 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 7 , 5 , 11 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countSexyPairs ( arr , n ) ) NEW_LINE DEDENT
Count of ways to write N as a sum of three numbers | Function to find the number of ways ; Check if number is less than 2 ; Calculate the sum ; Driver code
def countWays ( N ) : NEW_LINE INDENT if ( N <= 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = ( N - 1 ) * ( N - 2 ) / 2 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE countWays ( N ) NEW_LINE DEDENT
Logarithm tricks for Competitive Programming | Python3 implementation to check that a integer is a power of two ; Function to check if the number is a power of two ; Driver code
import math NEW_LINE def isPowerOfTwo ( n ) : NEW_LINE INDENT return ( math . ceil ( math . log ( n ) // math . log ( 2 ) ) == math . floor ( math . log ( n ) // math . log ( 2 ) ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE if isPowerOfTwo ( N ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT
Count of pairs having bit size at most X and Bitwise OR equal to X | Function to count the pairs ; Initializing answer with 1 ; Iterating through bits of x ; Check if bit is 1 ; Multiplying ans by 3 if bit is 1 ; Driver code
def count_pairs ( x ) : NEW_LINE INDENT ans = 1 ; NEW_LINE while ( x > 0 ) : NEW_LINE INDENT if ( x % 2 == 1 ) : NEW_LINE INDENT ans = ans * 3 ; NEW_LINE DEDENT x = x // 2 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 6 ; NEW_LINE print ( count_pairs ( X ) ) ; NEW_LINE DEDENT
Find the Kth number which is not divisible by N | Python3 implementation for above approach ; Function to find the Kth not divisible by N ; Lowest possible value ; Highest possible value ; To store the Kth non divisible number of N ; Using binary search ; Calculating mid value ; Sol would have the value by subtracting all multiples of n till mid ; Check if sol is greater than k ; H should be reduced to find minimum possible value ; Check if sol is less than k then L will be mid + 1 ; Check if sol is equal to k ; ans will be mid ; H would be reduced to find any more possible value ; Print the answer ; Driver Code ; Function call
import sys NEW_LINE def kthNonDivisible ( N , K ) : NEW_LINE INDENT L = 1 NEW_LINE H = sys . maxsize NEW_LINE ans = 0 NEW_LINE while ( L <= H ) : NEW_LINE INDENT mid = ( L + H ) // 2 NEW_LINE sol = mid - mid // N NEW_LINE if ( sol > K ) : NEW_LINE INDENT H = mid - 1 NEW_LINE DEDENT elif ( sol < K ) : NEW_LINE L = mid + 1 NEW_LINE else : NEW_LINE INDENT ans = mid NEW_LINE H = mid - 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT N = 3 NEW_LINE K = 7 NEW_LINE kthNonDivisible ( N , K ) NEW_LINE
Print any pair of integers with sum of GCD and LCM equals to N | Function to print the required pair ; Print the pair ; Driver code
def printPair ( n ) : NEW_LINE INDENT print ( "1" , end = " ▁ " ) NEW_LINE print ( n - 1 ) NEW_LINE DEDENT n = 14 NEW_LINE printPair ( n ) NEW_LINE
Find the length of largest subarray in which all elements are Autobiographical Numbers | Function to check number is autobiographical ; Convert integer to string ; Iterate for every digit to check for their total count ; Check occurrence of every number and count them ; Check if any position mismatches with total count them return with false else continue with loop ; Function to return the length of the largest subarray whose every element is a autobiographical number ; Utility function which checks every element of array for autobiographical number ; Check if element arr [ i ] is an autobiographical number ; Increment the current length ; Update max_length value ; Return the final result ; Driver code
def isAutoBiographyNum ( number ) : NEW_LINE INDENT count = 0 ; NEW_LINE NUM = str ( number ) ; NEW_LINE size = len ( NUM ) ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT position = ord ( NUM [ i ] ) - ord ( '0' ) ; NEW_LINE count = 0 ; NEW_LINE for j in range ( size ) : NEW_LINE INDENT digit = ord ( NUM [ j ] ) - ord ( '0' ) ; NEW_LINE if ( digit == i ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT if ( position != count ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def checkArray ( arr , n ) : NEW_LINE INDENT current_length = 0 ; NEW_LINE max_length = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isAutoBiographyNum ( arr [ i ] ) ) : NEW_LINE INDENT current_length += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT current_length = 0 ; NEW_LINE DEDENT max_length = max ( max_length , current_length ) ; NEW_LINE DEDENT return max_length ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 21200 , 1 , 1303 , 1210 , 2020 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( checkArray ( arr , n ) ) ; NEW_LINE DEDENT
Print the nodes of the Binary Tree whose height is a Prime number | Python3 implementation of nodes at prime height in the given tree ; To store Prime Numbers ; To store height of each node ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to perform dfs ; Store the height of node ; Function to find the nodes at prime height ; To precompute prime number till 10 ^ 5 ; Check if height [ node ] is prime ; Driver code ; Number of nodes ; Edges of the tree
MAX = 100000 NEW_LINE graph = [ [ ] for i in range ( MAX + 1 ) ] NEW_LINE Prime = [ True for i in range ( MAX + 1 ) ] NEW_LINE height = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT Prime [ 0 ] = Prime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i <= MAX : NEW_LINE INDENT if ( Prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 * i , MAX , i ) : NEW_LINE INDENT Prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def dfs ( node , parent , h ) : NEW_LINE INDENT height [ node ] = h NEW_LINE for to in graph [ node ] : NEW_LINE INDENT if ( to == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( to , node , h + 1 ) NEW_LINE DEDENT DEDENT def primeHeightNode ( N ) : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( Prime [ height [ i ] ] ) : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE graph [ 1 ] . append ( 2 ) NEW_LINE graph [ 1 ] . append ( 3 ) NEW_LINE graph [ 2 ] . append ( 4 ) NEW_LINE graph [ 2 ] . append ( 5 ) NEW_LINE dfs ( 1 , 1 , 0 ) NEW_LINE primeHeightNode ( N ) NEW_LINE DEDENT