codeparrot/xlcost-text-to-code huggingface dataset:
Product of count of set bits present in binary representations of elements in an array | Function to count the set bits in an integer ; Stores the count of set bits ; Iterate while N is not equal to 0 ; Increment count by 1 ; Divide N by 2 ; Return the total count obtained ; Function to find the product of count of set bits present in each element of an array ; Stores the resultant product ; Traverse the array arr [ ] ; Stores the count of set bits of arr [ i ] ; Update the product ; Return the resultant product ; Driver Code
def countbits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT n = n // 2 NEW_LINE DEDENT return count NEW_LINE DEDENT def BitProduct ( arr , N ) : NEW_LINE INDENT product = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT bits = countbits ( arr [ i ] ) NEW_LINE product *= bits NEW_LINE DEDENT return product NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 4 , 1 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( BitProduct ( arr , N ) ) NEW_LINE DEDENT
Queries to calculate sum of array elements consisting of odd number of divisors | Python3 program for the above approach ; Function to find the sum of elements having odd number of divisors in index range [ L , R ] for Q queries ; Initialize the dp [ ] array ; Traverse the array , arr [ ] ; If a [ i ] is a perfect square , then update value of DP [ i ] to a [ i ] ; Find the prefix sum of DP [ ] array ; Iterate through all the queries ; Find the sum for each query ; Driver Code
from math import sqrt NEW_LINE def OddDivisorsSum ( n , q , a , Query ) : NEW_LINE INDENT DP = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = sqrt ( a [ i ] ) NEW_LINE if ( x * x == a [ i ] ) : NEW_LINE INDENT DP [ i ] = a [ i ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT DP [ i ] = DP [ i - 1 ] + DP [ i ] NEW_LINE DEDENT for i in range ( q ) : NEW_LINE INDENT l = Query [ i ] [ 0 ] NEW_LINE r = Query [ i ] [ 1 ] NEW_LINE if ( l == 0 ) : NEW_LINE INDENT print ( DP [ r ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( DP [ r ] - DP [ l - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 5 , 6 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE Q = 3 NEW_LINE Query = [ [ 0 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] ] NEW_LINE OddDivisorsSum ( N , Q , arr , Query ) NEW_LINE DEDENT
Smallest subset of maximum sum possible by splitting array into two subsets | Python 3 program for the above approach ; Function to split array elements into two subsets having sum of the smaller subset maximized ; Stores the size of the array ; Stores the frequency of array elements ; Stores the total sum of the array ; Stores the sum of the resultant set ; Stores if it is possible to split the array that satisfies the conditions ; Stores the elements of the first subseta ; Traverse the array arr [ ] ; Increment total sum ; Increment count of arr [ i ] ; Sort the array arr [ ] ; Stores the index of the last element of the array ; Traverse the array arr [ ] ; Stores the frequency of arr [ i ] ; If frq + ans . size ( ) is at most remaining size ; Append arr [ i ] to ans ; Decrement totSum by arr [ i ] ; Increment s by arr [ i ] ; Otherwise , decrement i by frq ; If s is greater than totSum ; Mark flag 1 ; If flag is equal to 1 ; Print the arrList ans ; Otherwise , print " - 1" ; Driver Code
from collections import defaultdict NEW_LINE def findSubset ( arr ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE mp = defaultdict ( int ) NEW_LINE totSum = 0 NEW_LINE s = 0 NEW_LINE flag = 0 NEW_LINE ans = [ ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT totSum += arr [ i ] NEW_LINE mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 NEW_LINE DEDENT arr . sort ( ) NEW_LINE i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT frq = mp [ arr [ i ] ] NEW_LINE if ( ( frq + len ( ans ) ) < ( N - ( frq + len ( ans ) ) ) ) : NEW_LINE INDENT for k in range ( frq ) : NEW_LINE INDENT ans . append ( arr [ i ] ) NEW_LINE totSum -= arr [ i ] NEW_LINE s += arr [ i ] NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT i -= frq NEW_LINE DEDENT if ( s > totSum ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT for i in range ( len ( ans ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 3 , 2 , 4 , 1 , 2 ] NEW_LINE findSubset ( arr ) NEW_LINE DEDENT
Minimize Bitwise XOR of array elements with 1 required to make sum of array at least K | Function to find minimum number of Bitwise XOR of array elements with 1 required to make sum of the array at least K ; Stores the count of even array elements ; Stores sum of the array ; Traverse the array arr [ ] ; Increment sum ; If array element is even ; Increase count of even ; If S is at least K ; If S + E is less than K ; Otherwise , moves = K - S ; Driver Code
def minStepK ( arr , N , K ) : NEW_LINE INDENT E = 0 NEW_LINE S = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S += arr [ i ] NEW_LINE if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT E += 1 NEW_LINE DEDENT DEDENT if ( S >= K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( S + E < K ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return K - S NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 1 , 1 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE K = 4 NEW_LINE print ( minStepK ( arr , N , K ) ) NEW_LINE DEDENT
Maximize difference between odd and even | Function to find maximum and minimum value of a number that can be obtained by rotating bits ; Stores the value of N ; Stores the maximum value ; Stores the minimum value ; If temp is odd ; Update the maximum and the minimum value ; If flag is 1 , then return the maximum value ; Otherwise , return the maximum value ; Function to find the maximum difference between the sum of odd and even - indexed array elements possible by rotating bits ; Stores the maximum difference ; Stores the sum of elements present at odd indices ; Stores the sum of elements present at even indices ; Traverse the given array ; If the index is even ; Update the caseOne ; Stores the maximum difference ; Stores the sum of elements placed at odd positions ; Stores the sum of elements placed at even positions ; Traverse the array ; If the index is even ; Update the caseTwo ; Return the maximum of caseOne and caseTwo ; Driver Code
def Rotate ( n , f ) : NEW_LINE INDENT temp = n NEW_LINE maxi = n NEW_LINE mini = n NEW_LINE for idx in range ( 7 ) : NEW_LINE INDENT if temp & 1 : NEW_LINE INDENT temp >>= 1 NEW_LINE temp += 2 ** 7 NEW_LINE DEDENT else : NEW_LINE INDENT temp >>= 1 NEW_LINE DEDENT mini = min ( mini , temp ) NEW_LINE maxi = max ( maxi , temp ) NEW_LINE DEDENT if ( f ) : NEW_LINE INDENT return ( maxi ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( mini ) NEW_LINE DEDENT DEDENT def calcMinDiff ( arr ) : NEW_LINE INDENT caseOne = 0 NEW_LINE sumOfodd = 0 NEW_LINE sumOfeven = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE INDENT sumOfodd += Rotate ( arr [ i ] , 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT sumOfeven += Rotate ( arr [ i ] , 1 ) NEW_LINE DEDENT DEDENT caseOne = abs ( sumOfodd - sumOfeven ) NEW_LINE caseTwo = 0 NEW_LINE sumOfodd = 0 NEW_LINE sumOfeven = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE INDENT sumOfodd += Rotate ( arr [ i ] , 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT sumOfeven += Rotate ( arr [ i ] , 0 ) NEW_LINE DEDENT DEDENT caseTwo = abs ( sumOfodd - sumOfeven ) NEW_LINE return max ( caseOne , caseTwo ) NEW_LINE DEDENT arr = [ 123 , 86 , 234 , 189 ] NEW_LINE print ( calcMinDiff ( arr ) ) NEW_LINE
Array element with minimum sum of absolute differences | Set 2 | Function to find the element with minimum sum of differences between any elements in the array ; Stores the required X and sum of absolute differences ; Calculate sum of array elements ; The sum of absolute differences can 't be greater than sum ; Update res that gives the minimum sum ; If the current difference is less than the previous difference ; Update min_diff and res ; Print the resultant value ; Driver Code
def minimumDiff ( arr , N ) : NEW_LINE INDENT res = arr [ 0 ] NEW_LINE sum1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum1 += arr [ i ] NEW_LINE DEDENT min_diff = sum1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( abs ( sum1 - ( arr [ i ] * N ) ) < min_diff ) : NEW_LINE INDENT min_diff = abs ( sum1 - ( arr [ i ] * N ) ) NEW_LINE res = arr [ i ] NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE minimumDiff ( arr , N ) NEW_LINE DEDENT
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Stores the array elements ; Traverse the array ; Stores if the operation should be formed or not ; Traverse the vector V ; If flag is false and if v [ i ] is divisible by K ; Otherwise , set flag as true ; Increment the sum by v [ i % N ] ; Return the resultant sum ; Driver Code
def Sum ( arr , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE v = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT flag = False NEW_LINE i = 0 NEW_LINE lenn = len ( v ) NEW_LINE while ( i < lenn ) : NEW_LINE INDENT if ( flag == False and ( v [ i ] % K == 0 ) ) : NEW_LINE INDENT v . append ( v [ i ] // K ) NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE DEDENT sum += v [ i % N ] NEW_LINE i += 1 NEW_LINE lenn = len ( v ) NEW_LINE DEDENT return sum NEW_LINE DEDENT arr = [ 4 , 6 , 8 , 2 ] NEW_LINE K = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( Sum ( arr , N , K ) ) NEW_LINE
Count array elements whose count of divisors is a prime number | Function to count the array elements whose count of divisors is prime ; Stores the maximum element ; Find the maximum element ; Store if i - th element is prime ( 0 ) or non - prime ( 1 ) ; Base Case ; If i is a prime number ; Mark all multiples of i as non - prime ; Stores the count of divisors ; Base Case ; Iterate to count factors ; Stores the count of array elements whose count of divisors is a prime number ; Traverse the array arr [ ] ; If count of divisors is prime ; Return the resultant count ; Driver Code
def primeDivisors ( arr , N ) : NEW_LINE INDENT K = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT K = max ( K , arr [ i ] ) NEW_LINE DEDENT prime = [ 0 ] * ( K + 1 ) NEW_LINE prime [ 0 ] = 1 NEW_LINE prime [ 1 ] = 1 NEW_LINE for i in range ( 2 , K + 1 ) : NEW_LINE INDENT if ( not prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 * i , K + 1 , i ) : NEW_LINE INDENT prime [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT factor = [ 0 ] * ( K + 1 ) NEW_LINE factor [ 0 ] = 0 NEW_LINE factor [ 1 ] = 1 NEW_LINE for i in range ( 2 , K + 1 ) : NEW_LINE INDENT factor [ i ] += 1 NEW_LINE for j in range ( i , K + 1 , i ) : NEW_LINE INDENT factor [ j ] += 1 NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( prime [ factor [ arr [ i ] ] ] == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 13 , 17 , 25 ] NEW_LINE N = len ( arr ) NEW_LINE print ( primeDivisors ( arr , N ) ) NEW_LINE DEDENT
Count prime numbers up to N that can be represented as a sum of two prime numbers | Function to store all prime numbers up to N using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is prime ; Set all multiples of p as non - prime ; Function to count prime numbers up to N that can be represented as the sum of two prime numbers ; Stores all the prime numbers ; Update the prime array ; Create a dp array of size n + 1 ; Update dp [ 1 ] = 0 ; Iterate over the range [ 2 , N ] ; Add the previous count value ; Increment dp [ i ] by 1 if i and ( i - 2 ) are both prime ; Print the result ; Driver Code
def SieveOfEratosthenes ( n , prime ) : NEW_LINE INDENT prime [ 0 ] = 0 NEW_LINE prime [ 1 ] = 0 NEW_LINE p = 2 NEW_LINE while p * p <= n : NEW_LINE INDENT 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 p += 1 NEW_LINE DEDENT DEDENT def countPrime ( n ) : NEW_LINE INDENT prime = [ True ] * ( n + 1 ) NEW_LINE SieveOfEratosthenes ( n , prime ) NEW_LINE dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 1 ] = 0 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT dp [ i ] += dp [ i - 1 ] NEW_LINE if ( prime [ i ] == 1 and prime [ i - 2 ] == 1 ) : NEW_LINE INDENT dp [ i ] += 1 NEW_LINE DEDENT DEDENT print ( dp [ n ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE countPrime ( N ) NEW_LINE DEDENT
Minimize sum of an array having Bitwise AND of all its pairs present in a given matrix | Function to find the minimum sum of the array such that Bitwise AND of arr [ i ] ana arr [ j ] is mat [ i ] [ j ] ; Stores the minimum possible sum ; Traverse the range [ 0 , N - 1 ] ; Stores the Bitwise OR of all the element of a row ; Traverse the range [ 0 , N - 1 ] ; If i not equal to j ; Update the value of res ; Increment the sum by res ; Return minimum possible sum ; Driver code
def findMinSum ( mat , N ) : NEW_LINE INDENT sum1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT res = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT res |= mat [ i ] [ j ] NEW_LINE DEDENT DEDENT sum1 += res NEW_LINE DEDENT return sum1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ - 1 , 2 , 3 ] , [ 9 , - 1 , 7 ] , [ 4 , 5 , - 1 ] ] NEW_LINE N = 3 NEW_LINE print ( findMinSum ( mat , N ) ) NEW_LINE DEDENT
LCM of unique elements present in an array | Python3 program for the above approach ; Function to find GCD of two numbers ; Base Case ; Recursively find the GCD ; Function to find LCM of two numbers ; Function to find LCM of unique elements present in the array ; Stores the frequency of each number of the array ; Store the frequency of each element of the array ; Store the required result ; Traverse the map freq ; If the frequency of the current element is 1 , then update ans ; If there is no unique element , set lcm to - 1 ; Print the result ; Driver Code ; Function Call
from collections import defaultdict NEW_LINE def findGCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return findGCD ( b , a % b ) NEW_LINE DEDENT def findLCM ( a , b ) : NEW_LINE INDENT return ( a * b ) // findGCD ( a , b ) NEW_LINE DEDENT def uniqueElementsLCM ( arr , N ) : NEW_LINE INDENT freq = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT lcm = 1 NEW_LINE for i in freq : NEW_LINE INDENT if ( freq [ i ] == 1 ) : NEW_LINE INDENT lcm = findLCM ( lcm , i ) NEW_LINE DEDENT DEDENT if ( lcm == 1 ) : NEW_LINE INDENT lcm = - 1 NEW_LINE DEDENT print ( lcm ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 3 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE uniqueElementsLCM ( arr , N ) NEW_LINE DEDENT
Minimize maximum difference between adjacent elements possible by removing a single array element | Python3 program to implement the above approach ; Function to find maximum difference between adjacent array elements ; Store the maximum difference ; Traverse the array ; Update maximum difference ; Function to calculate the minimum of maximum difference between adjacent array elements possible by removing a single array element ; Stores the required minimum ; Stores the updated array ; Skip the i - th element ; Update MinValue ; return MinValue ; Driver Code
import sys NEW_LINE def maxAdjacentDifference ( A ) : NEW_LINE INDENT diff = 0 NEW_LINE for i in range ( 1 , len ( A ) , 1 ) : NEW_LINE INDENT diff = max ( diff , A [ i ] - A [ i - 1 ] ) NEW_LINE DEDENT return diff NEW_LINE DEDENT def MinimumValue ( arr , N ) : NEW_LINE INDENT MinValue = sys . maxsize NEW_LINE for i in range ( N ) : NEW_LINE INDENT new_arr = [ ] NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT continue NEW_LINE DEDENT new_arr . append ( arr [ j ] ) NEW_LINE DEDENT MinValue = min ( MinValue , maxAdjacentDifference ( new_arr ) ) NEW_LINE DEDENT return MinValue NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 7 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE print ( MinimumValue ( arr , N ) ) NEW_LINE DEDENT
Number of Antisymmetric Relations on a set of N elements | Python3 program for the above approach ; Function to calculate the value of x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of antisymmetric relations in a set consisting of N elements ; Print the count of antisymmetric relations ; Driver Code
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % mod NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % mod NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % mod NEW_LINE DEDENT return res NEW_LINE DEDENT def antisymmetricRelation ( N ) : NEW_LINE INDENT return ( power ( 2 , N ) * power ( 3 , ( N * N - N ) // 2 ) ) % mod NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE print ( antisymmetricRelation ( N ) ) NEW_LINE DEDENT
Number of Asymmetric Relations on a set of N elements | Python3 program for the above approach ; Function to calculate x ^ y modulo ( 10 ^ 9 + 7 ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the final value of x ^ y ; Function to count the number of asymmetric relations in a set consisting of N elements ; Return the resultant count ; Driver Code
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % mod NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % mod ; NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % mod NEW_LINE DEDENT return res NEW_LINE DEDENT def asymmetricRelation ( N ) : NEW_LINE INDENT return power ( 3 , ( N * N - N ) // 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE print ( asymmetricRelation ( N ) ) NEW_LINE DEDENT
Check if two vectors are collinear or not | Function to calculate cross product of two vectors ; Update cross_P [ 0 ] ; Update cross_P [ 1 ] ; Update cross_P [ 2 ] ; Function to check if two given vectors are collinear or not ; Store the first and second vectors ; Store their cross product ; Calculate their cross product ; Check if their cross product is a NULL Vector or not ; Driver Code ; Given coordinates of the two vectors
def crossProduct ( vect_A , vect_B , cross_P ) : NEW_LINE INDENT cross_P [ 0 ] = ( vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ) NEW_LINE cross_P [ 1 ] = ( vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ) NEW_LINE cross_P [ 2 ] = ( vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ) NEW_LINE DEDENT def checkCollinearity ( x1 , y1 , z1 , x2 , y2 , z2 ) : NEW_LINE INDENT A = [ x1 , y1 , z1 ] NEW_LINE B = [ x2 , y2 , z2 ] NEW_LINE cross_P = [ 0 for i in range ( 3 ) ] NEW_LINE crossProduct ( A , B , cross_P ) NEW_LINE if ( cross_P [ 0 ] == 0 and cross_P [ 1 ] == 0 and cross_P [ 2 ] == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x1 = 4 NEW_LINE y1 = 8 NEW_LINE z1 = 12 NEW_LINE x2 = 8 NEW_LINE y2 = 16 NEW_LINE z2 = 24 NEW_LINE checkCollinearity ( x1 , y1 , z1 , x2 , y2 , z2 ) NEW_LINE DEDENT
Program to calculate Kinetic Energy and Potential Energy | Function to calculate Kinetic Energy ; Stores the Kinetic Energy ; Function to calculate Potential Energy ; Stores the Potential Energy ; Driver Code
def kineticEnergy ( M , V ) : NEW_LINE INDENT KineticEnergy = 0.5 * M * V * V NEW_LINE return KineticEnergy NEW_LINE DEDENT def potentialEnergy ( M , H ) : NEW_LINE INDENT PotentialEnergy = M * 9.8 * H NEW_LINE return PotentialEnergy NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT M = 5.5 NEW_LINE H = 23.5 NEW_LINE V = 10.5 NEW_LINE print ( " Kinetic ▁ Energy ▁ = ▁ " , kineticEnergy ( M , V ) ) NEW_LINE print ( " Potential ▁ Energy ▁ = ▁ " , potentialEnergy ( M , H ) ) NEW_LINE DEDENT
Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR | Function to count the number of pairs required to be removed from the array and replaced by their Bitwise OR values ; Stores the count of even array elements ; Traverse the given array ; Increment the count of even array elements ; If the array contains at least one odd array element ; Otherwise , print 0 ; Driver Code
def countPairs ( arr , N ) : NEW_LINE INDENT even = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT if ( N - even >= 1 ) : NEW_LINE INDENT print ( even ) NEW_LINE return NEW_LINE DEDENT print ( 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 4 , 7 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT
Distribute M objects starting from Sth person such that every ith person gets arr [ i ] objects | Function to find distribution of M objects among all array elements ; Stores the distribution of M objects ; Stores the indices of distribution ; Stores the remaining objects ; Iterate until rem is positive ; If the number of remaining objects exceeds required the number of objects ; Increase the number of objects for the index ptr by arr [ ptr ] ; Decrease remaining objects by arr [ ptr ] ; Increase the number of objects for the index ptr by rem ; Decrease remaining objects to 0 ; Increase ptr by 1 ; Print the final distribution ; Driver Code
def distribute ( N , K , M , arr ) : NEW_LINE INDENT distribution = [ 0 ] * N NEW_LINE ptr = K - 1 NEW_LINE rem = M NEW_LINE while ( rem > 0 ) : NEW_LINE INDENT if ( rem >= arr [ ptr ] ) : NEW_LINE INDENT distribution [ ptr ] += arr [ ptr ] NEW_LINE rem -= arr [ ptr ] NEW_LINE DEDENT else : NEW_LINE INDENT distribution [ ptr ] += rem NEW_LINE rem = 0 NEW_LINE DEDENT ptr = ( ptr + 1 ) % N NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( distribution [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 2 , 1 , 4 ] NEW_LINE M = 11 NEW_LINE S = 2 NEW_LINE N = len ( arr ) NEW_LINE distribute ( N , S , M , arr ) NEW_LINE
Sum of squares of differences between all pairs of an array | Function to calculate sum of squares of differences of all possible pairs ; Stores the final sum ; Stores temporary values ; Traverse the array ; Final sum ; Prthe answer ; Driver Code ; Given array ; Size of the array ; Function call to find sum of square of differences of all possible pairs
def sumOfSquaredDifferences ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE sumA , sumB = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sumA += ( arr [ i ] * arr [ i ] ) NEW_LINE sumB += arr [ i ] NEW_LINE DEDENT sumA = N * sumA NEW_LINE sumB = ( sumB * sumB ) NEW_LINE ans = sumA - sumB NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 8 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE sumOfSquaredDifferences ( arr , N ) NEW_LINE DEDENT