codeparrot/xlcost-text-to-code huggingface dataset:
Program to Calculate e ^ x by Recursion ( using Taylor Series ) | Recursive Function global variables p and f ; Termination condition ; Recursive call ; Update the power of x ; Factorial ; Driver code
p = 1.0 NEW_LINE f = 1.0 NEW_LINE def e ( x , n ) : NEW_LINE INDENT global p , f NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT r = e ( x , n - 1 ) NEW_LINE p = p * x NEW_LINE f = f * n NEW_LINE return ( r + p / f ) NEW_LINE DEDENT x = 4 NEW_LINE n = 15 NEW_LINE print ( e ( x , n ) ) NEW_LINE
Check if each element of the given array is the product of exactly K prime numbers | Python3 program to check if each element of the given array is a product of exactly K prime factors ; initialise the global sieve array ; Function to generate Sieve ; NOTE : k value is necessarily more than 1 hence , 0 , 1 and any prime number cannot be represented as product of two or more prime numbers ; Function to check if each number of array satisfies the given condition ; Driver Code ; first construct the sieve
MAX = 1000000 NEW_LINE Sieve = [ 0 ] * ( MAX + 1 ) NEW_LINE def constructSieve ( ) : NEW_LINE INDENT for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if ( Sieve [ i ] == 0 ) : NEW_LINE INDENT for j in range ( 2 * i , MAX + 1 , i ) : NEW_LINE INDENT temp = j ; NEW_LINE while ( temp > 1 and temp % i == 0 ) : NEW_LINE INDENT Sieve [ j ] += 1 ; NEW_LINE temp = temp // i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def checkElements ( A , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( Sieve [ A [ i ] ] == k ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT constructSieve ( ) ; NEW_LINE k = 3 ; NEW_LINE A = [ 12 , 36 , 42 , 72 ] ; NEW_LINE n = len ( A ) ; NEW_LINE checkElements ( A , n , k ) ; NEW_LINE DEDENT
Fibonacci Power |  ; Iterative function to compute modular power ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Helper function that multiplies 2 matrices F and M of size 2 * 2 , and puts the multiplication result back to F [ ] [ ] ; Helper function that calculates F [ ] [ ] raise to the power n and puts the result in F [ ] [ ] Note that this function is designed only for fib ( ) and won 't work as general power function ; Function that returns nth Fibonacci number ; Driver Code
mod = 1000000007 ; NEW_LINE def modularexpo ( x , y , p ) : NEW_LINE INDENT res = 1 ; NEW_LINE x = x % p ; NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT y = y >> 1 ; NEW_LINE x = ( x * x ) % p ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def multiply ( F , M , m ) : NEW_LINE INDENT x = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; NEW_LINE y = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; NEW_LINE z = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; NEW_LINE w = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; NEW_LINE F [ 0 ] [ 0 ] = x ; NEW_LINE F [ 0 ] [ 1 ] = y ; NEW_LINE F [ 1 ] [ 0 ] = z ; NEW_LINE F [ 1 ] [ 1 ] = w ; NEW_LINE DEDENT def power ( F , n , m ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT M = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; NEW_LINE power ( F , n // 2 , m ) ; NEW_LINE multiply ( F , F , m ) ; NEW_LINE if ( n % 2 != 0 ) : NEW_LINE INDENT multiply ( F , M , m ) ; NEW_LINE DEDENT DEDENT def fib ( n , m ) : NEW_LINE INDENT F = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT power ( F , n - 1 , m ) ; NEW_LINE return F [ 0 ] [ 0 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE base = fib ( n , mod ) % mod ; NEW_LINE expo = fib ( n , mod - 1 ) % ( mod - 1 ) ; NEW_LINE result = modularexpo ( base , expo , mod ) % mod ; NEW_LINE print ( result ) ; NEW_LINE DEDENT
Count number of ways to divide an array into two halves with same sum | Function to count the number of ways to divide an array into two halves with same sum ; if length of array is 1 answer will be 0 as we have to split it into two non - empty halves ; variables to store total sum , current sum and count ; finding total sum ; checking if sum equals total_sum / 2 ; Driver Code
def cntWays ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT tot_sum = 0 ; sum = 0 ; ans = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT tot_sum += arr [ i ] ; NEW_LINE DEDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE if ( sum == tot_sum / 2 ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntWays ( arr , n ) ) ; NEW_LINE
Find N distinct numbers whose bitwise Or is equal to K | Pyhton 3 implementation of the approach ; Function to pre - calculate all the powers of 2 upto MAX ; Function to return the count of set bits in x ; To store the count of set bits ; Function to add num to the answer by setting all bit positions as 0 which are also 0 in K ; Bit i is 0 in K ; Function to find and print N distinct numbers whose bitwise OR is K ; Choosing K itself as one number ; Find the count of set bits in K ; Impossible to get N distinct integers ; Add i to the answer after setting all the bits as 0 which are 0 in K ; If N distinct numbers are generated ; Print the generated numbers ; Driver code ; Pre - calculate all the powers of 2
MAX = 32 NEW_LINE pow2 = [ 0 for i in range ( MAX ) ] NEW_LINE visited = [ False for i in range ( MAX ) ] NEW_LINE ans = [ ] NEW_LINE def power_2 ( ) : NEW_LINE INDENT an = 1 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT pow2 [ i ] = an NEW_LINE an *= 2 NEW_LINE DEDENT DEDENT def countSetBits ( x ) : NEW_LINE INDENT setBits = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT x = x & ( x - 1 ) NEW_LINE setBits += 1 NEW_LINE DEDENT return setBits NEW_LINE DEDENT def add ( num ) : NEW_LINE INDENT point = 0 NEW_LINE value = 0 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT if ( visited [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT if ( num & 1 ) : NEW_LINE INDENT value += ( 1 << i ) NEW_LINE DEDENT num = num // 2 NEW_LINE DEDENT DEDENT ans . append ( value ) NEW_LINE DEDENT def solve ( n , k ) : NEW_LINE INDENT ans . append ( k ) NEW_LINE countk = countSetBits ( k ) NEW_LINE if ( pow2 [ countk ] < n ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( pow2 [ countk ] - 1 ) : NEW_LINE INDENT add ( i ) NEW_LINE count += 1 NEW_LINE if ( count == n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE k = 5 NEW_LINE power_2 ( ) NEW_LINE solve ( n , k ) NEW_LINE DEDENT
Count of all possible values of X such that A % X = B | Function to return the count of all possible values for x such that ( A % x ) = B ; Case 1 ; Case 2 ; Case 3 ; Find the number of divisors of x which are greater than b ; Driver code
def countX ( a , b ) : NEW_LINE INDENT if ( b > a ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( a == b ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT x = a - b NEW_LINE ans = 0 NEW_LINE i = 1 NEW_LINE while i * i <= x : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT d1 = i NEW_LINE d2 = b - 1 NEW_LINE if ( i * i != x ) : NEW_LINE INDENT d2 = x // i NEW_LINE DEDENT if ( d1 > b ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT if ( d2 > b ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 21 NEW_LINE b = 5 NEW_LINE print ( countX ( a , b ) ) NEW_LINE DEDENT
Find the Jaccard Index and Jaccard Distance between the two given sets | Function to return the intersection set of s1 and s2 ; Find the intersection of the two sets ; Function to return the Jaccard index of two sets ; Sizes of both the sets ; Get the intersection set ; Size of the intersection set ; Calculate the Jaccard index using the formula ; Return the Jaccard index ; Function to return the Jaccard distance ; Calculate the Jaccard distance using the formula ; Return the Jaccard distance ; Driver code ; Elements of the 1 st set ; Elements of the 2 nd set ; Print the Jaccard index and Jaccard distance
def intersection ( s1 , s2 ) : NEW_LINE INDENT intersect = s1 & s2 ; NEW_LINE return intersect ; NEW_LINE DEDENT def jaccard_index ( s1 , s2 ) : NEW_LINE INDENT size_s1 = len ( s1 ) ; NEW_LINE size_s2 = len ( s2 ) ; NEW_LINE intersect = intersection ( s1 , s2 ) ; NEW_LINE size_in = len ( intersect ) ; NEW_LINE jaccard_in = size_in / ( size_s1 + size_s2 - size_in ) ; NEW_LINE return jaccard_in ; NEW_LINE DEDENT def jaccard_distance ( jaccardIndex ) : NEW_LINE INDENT jaccard_dist = 1 - jaccardIndex ; NEW_LINE return jaccard_dist ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = set ( ) ; NEW_LINE s1 . add ( 1 ) ; NEW_LINE s1 . add ( 2 ) ; NEW_LINE s1 . add ( 3 ) ; NEW_LINE s1 . add ( 4 ) ; NEW_LINE s1 . add ( 5 ) ; NEW_LINE s2 = set ( ) ; NEW_LINE s2 . add ( 4 ) ; NEW_LINE s2 . add ( 5 ) ; NEW_LINE s2 . add ( 6 ) ; NEW_LINE s2 . add ( 7 ) ; NEW_LINE s2 . add ( 8 ) ; NEW_LINE s2 . add ( 9 ) ; NEW_LINE s2 . add ( 10 ) ; NEW_LINE jaccardIndex = jaccard_index ( s1 , s2 ) ; NEW_LINE print ( " Jaccard ▁ index ▁ = ▁ " , jaccardIndex ) ; NEW_LINE print ( " Jaccard ▁ distance ▁ = ▁ " , jaccard_distance ( jaccardIndex ) ) ; NEW_LINE DEDENT
Find the sum of numbers from 1 to n excluding those which are powers of K | Function to return the sum of all the powers of k from the range [ 1 , n ] ; To store the sum of the series ; While current power of k <= n ; Add current power to the sum ; Next power of k ; Return the sum of the series ; Find to return the sum of the elements from the range [ 1 , n ] excluding those which are powers of k ; Sum of all the powers of k from [ 1 , n ] ; Sum of all the elements from [ 1 , n ] ; Return the required sum ; Driver code
def sumPowersK ( n , k ) : NEW_LINE INDENT sum = 0 ; num = 1 ; NEW_LINE while ( num <= n ) : NEW_LINE INDENT sum += num ; NEW_LINE num *= k ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def getSum ( n , k ) : NEW_LINE INDENT pwrK = sumPowersK ( n , k ) ; NEW_LINE sumAll = ( n * ( n + 1 ) ) / 2 ; NEW_LINE return ( sumAll - pwrK ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; k = 3 ; NEW_LINE print ( getSum ( n , k ) ) ; NEW_LINE DEDENT
Maximum number of people that can be killed with strength P | Python3 implementation of the approach ; Function to return the maximum number of people that can be killed ; Loop will break when the ith person cannot be killed ; Driver code
from math import sqrt NEW_LINE def maxPeople ( p ) : NEW_LINE INDENT tmp = 0 ; count = 0 ; NEW_LINE for i in range ( 1 , int ( sqrt ( p ) ) + 1 ) : NEW_LINE INDENT tmp = tmp + ( i * i ) ; NEW_LINE if ( tmp <= p ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = 14 ; NEW_LINE print ( maxPeople ( p ) ) ; NEW_LINE DEDENT
Maximum number of people that can be killed with strength P | Python3 implementation of the approach ; Function to return the maximum number of people that can be killed ; Storing the sum beforehand so that it can be used in each query ; lower_bound returns an iterator pointing to the first element greater than or equal to your val ; Previous value ; Returns the index in array upto which killing is possible with strength P ; Driver code
kN = 1000000 ; NEW_LINE def maxPeople ( p ) : NEW_LINE INDENT sums = [ 0 ] * kN ; NEW_LINE sums [ 0 ] = 0 ; NEW_LINE for i in range ( 1 , kN ) : NEW_LINE INDENT sums [ i ] = ( i * i ) + sums [ i - 1 ] ; NEW_LINE DEDENT it = lower_bound ( sums , 0 , kN , p ) ; NEW_LINE if ( it > p ) : NEW_LINE INDENT it -= 1 ; NEW_LINE DEDENT return it ; NEW_LINE DEDENT def lower_bound ( a , low , high , element ) : NEW_LINE INDENT while ( low < high ) : NEW_LINE INDENT middle = int ( low + ( high - low ) / 2 ) ; NEW_LINE if ( element > a [ middle ] ) : NEW_LINE INDENT low = middle + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT high = middle ; NEW_LINE DEDENT DEDENT return low ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 14 ; NEW_LINE print ( maxPeople ( p ) ) ; NEW_LINE DEDENT
Maximum number of people that can be killed with strength P | helper function which returns the sum of series ( 1 ^ 2 + 2 ^ 2 + ... + n ^ 2 ) ; maxPeople function which returns appropriate value using Binary Search in O ( logn ) ; Set the lower and higher values ; calculate the mid using low and high ; compare value with n ; return the ans
def squareSeries ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) * ( 2 * n + 1 ) ) // 6 NEW_LINE DEDENT def maxPeople ( n ) : NEW_LINE INDENT low = 0 NEW_LINE high = 1000000000000000 NEW_LINE while low <= high : NEW_LINE INDENT mid = low + ( ( high - low ) // 2 ) NEW_LINE value = squareSeries ( mid ) NEW_LINE if value <= n : NEW_LINE INDENT ans = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 14 NEW_LINE print ( maxPeople ( p ) ) NEW_LINE DEDENT
Find the index which is the last to be reduced to zero after performing a given operation | Function that returns the index which will be the last to become zero after performing given operation ; Initialize the result ; Finding the ceil value of each index ; Finding the index with maximum ceil value ; Driver code
def findIndex ( a , n , k ) : NEW_LINE INDENT index = - 1 NEW_LINE max_ceil = - 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] = ( a [ i ] + k - 1 ) // k NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] >= max_ceil ) : NEW_LINE INDENT max_ceil = a [ i ] NEW_LINE index = i NEW_LINE DEDENT DEDENT return index NEW_LINE DEDENT arr = [ 31 , 12 , 25 , 27 , 32 , 19 ] NEW_LINE K = 5 NEW_LINE N = len ( arr ) NEW_LINE print ( findIndex ( arr , N , K ) ) NEW_LINE
Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree | Python3 implementation of the approach ; Function to solve queries of the maximum xor value between the nodes in a given level L of a perfect binary tree and a given value X ; Initialize result ; Initialize array to store bits ; Initialize a copy of X and size of array ; Storing the bits of X in the array a [ ] ; Filling the array b [ ] ; Initializing variable which gives maximum xor ; Getting the maximum xor value ; Return the result ; Driver code ; Perform queries
MAXN = 60 NEW_LINE def solveQuery ( L , X ) : NEW_LINE INDENT res = 0 NEW_LINE a = [ 0 for i in range ( MAXN ) ] NEW_LINE b = [ 0 for i in range ( MAXN ) ] NEW_LINE ref = X NEW_LINE size_a = 0 NEW_LINE while ( ref > 0 ) : NEW_LINE INDENT a [ size_a ] = ref % 2 NEW_LINE ref //= 2 NEW_LINE size_a += 1 NEW_LINE DEDENT for i in range ( min ( size_a , L ) ) : NEW_LINE INDENT if ( a [ i ] == 1 ) : NEW_LINE INDENT b [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT b [ i ] = 1 NEW_LINE DEDENT DEDENT for i in range ( min ( size_a , L ) , L ) : NEW_LINE INDENT b [ i ] = 1 NEW_LINE DEDENT b [ L - 1 ] = 1 NEW_LINE temp = 0 NEW_LINE p = 1 NEW_LINE for i in range ( L ) : NEW_LINE INDENT temp += b [ i ] * p NEW_LINE p *= 2 NEW_LINE DEDENT res = temp ^ X NEW_LINE return res NEW_LINE DEDENT queries = [ [ 2 , 5 ] , [ 3 , 15 ] ] NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( solveQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) NEW_LINE DEDENT
Count index pairs which satisfy the given condition | Function to return the count of required index pairs ; To store the required count ; Array to store the left elements upto which current element is maximum ; Iterating through the whole permutation except first and last element ; If current element can be maximum in a subsegment ; Current maximum ; Iterating for smaller values then current maximum on left of it ; Storing left borders of the current maximum ; Iterating for smaller values then current maximum on right of it ; Condition satisfies ; Return count of subsegments ; Driver Code
def Count_Segment ( p , n ) : NEW_LINE INDENT count = 0 NEW_LINE upto = [ False ] * ( n + 1 ) NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if p [ i ] > p [ i - 1 ] and p [ i ] > p [ i + 1 ] : NEW_LINE INDENT curr = p [ i ] NEW_LINE j = i - 1 NEW_LINE while j >= 0 and p [ j ] < curr : NEW_LINE INDENT upto [ p [ j ] ] = curr NEW_LINE j -= 1 NEW_LINE DEDENT j = i + 1 NEW_LINE while j < n and p [ j ] < curr : NEW_LINE INDENT if upto [ curr - p [ j ] ] == curr : NEW_LINE INDENT count += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = [ 3 , 4 , 1 , 5 , 2 ] NEW_LINE n = len ( p ) NEW_LINE print ( Count_Segment ( p , n ) ) NEW_LINE DEDENT
Check whether a number can be represented as sum of K distinct positive integers | Function that returns true if n can be represented as the sum of exactly k distinct positive integers ; If n can be represented as 1 + 2 + 3 + ... + ( k - 1 ) + ( k + x ) ; Driver code
def solve ( n , k ) : NEW_LINE INDENT if ( n >= ( k * ( k + 1 ) ) // 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE k = 4 NEW_LINE if ( solve ( n , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check whether product of integers from a to b is positive , negative or zero | Function to check whether the product of integers of the range [ a , b ] is positive , negative or zero ; If both a and b are positive then the product will be positive ; If a is negative and b is positive then the product will be zero ; If both a and b are negative then we have to find the count of integers in the range ; Total integers in the range ; If n is even then the resultant product is positive ; If n is odd then the resultant product is negative ; Driver code
def solve ( a , b ) : NEW_LINE INDENT if ( a > 0 and b > 0 ) : NEW_LINE INDENT print ( " Positive " ) NEW_LINE DEDENT elif ( a <= 0 and b >= 0 ) : NEW_LINE INDENT print ( " Zero " ) NEW_LINE DEDENT else : NEW_LINE INDENT n = abs ( a - b ) + 1 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT print ( " Positive " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Negative " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = - 10 NEW_LINE b = - 2 NEW_LINE solve ( a , b ) NEW_LINE DEDENT
Bitwise AND of sub | Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; Driver code
def closetAND ( arr , n , k ) : NEW_LINE INDENT ans = 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT X = arr [ i ] NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT X &= arr [ j ] NEW_LINE ans = min ( ans , abs ( k - X ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 4 , 7 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 ; NEW_LINE print ( closetAND ( arr , n , k ) ) NEW_LINE
Program to find the rate percentage from compound interest of consecutive years | Function to return the required rate percentage ; Driver code
def Rate ( N1 , N2 ) : NEW_LINE INDENT rate = ( N2 - N1 ) * 100 // ( N1 ) ; NEW_LINE return rate NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N1 = 100 NEW_LINE N2 = 120 NEW_LINE print ( Rate ( N1 , N2 ) , " ▁ % " ) NEW_LINE DEDENT
Find prime number K in an array such that ( A [ i ] % K ) is maximum | Python 3 implementation of the approach ; Function to return the required prime number from the array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; To store the maximum prime number ; If current element is prime then update the maximum prime ; Return the maximum prime number from the array ; Driver code
from math import sqrt NEW_LINE def getPrime ( arr , n ) : NEW_LINE INDENT max_val = arr [ 0 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] > max_val ) : NEW_LINE INDENT max_val = arr [ i ] NEW_LINE DEDENT DEDENT prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( max_val ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT maximum = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT maximum = max ( maximum , arr [ i ] ) NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 10 , 15 , 7 , 6 , 8 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getPrime ( arr , n ) ) NEW_LINE DEDENT
Minimum integer such that it leaves a remainder 1 on dividing with any element from the range [ 2 , N ] | Python3 implementation of the approach ; Function to return the smallest number which on dividing with any element from the range [ 2 , N ] leaves a remainder of 1 ; Find the LCM of the elements from the range [ 2 , N ] ; Return the required number ; Driver code
from math import gcd NEW_LINE def getMinNum ( N ) : NEW_LINE INDENT lcm = 1 ; NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT lcm = ( ( i * lcm ) // ( gcd ( i , lcm ) ) ) ; NEW_LINE DEDENT return ( lcm + 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE print ( getMinNum ( N ) ) ; NEW_LINE DEDENT
Maximum number of edges in Bipartite graph | Function to return the maximum number of edges possible in a Bipartite graph with N vertices ; Driver code
def maxEdges ( N ) : NEW_LINE INDENT edges = 0 ; NEW_LINE edges = ( N * N ) // 4 ; NEW_LINE return edges ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE print ( maxEdges ( N ) ) ; NEW_LINE DEDENT