codeparrot/xlcost-text-to-code huggingface dataset:
Generate an alternate increasing and decreasing Array | Function that returns generated array ; Dynamically allocate array ; START , END = 0 , N ; Iterate over array ; If Str [ i ] = = ' I ' assign arr [ i ] as START and increment START ; If str [ i ] = = ' D ' assign arr [ i ] as END and decrement END ; Assign A [ N ] as START ; Return starting address of array A ; Driver code
def DiStirngMatch ( Str ) : NEW_LINE INDENT N = len ( Str ) NEW_LINE arr = ( N + 1 ) * [ 0 ] NEW_LINE START , END = 0 , N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( Str [ i ] == ' I ' ) : NEW_LINE INDENT arr [ i ] = START NEW_LINE START += 1 NEW_LINE DEDENT if ( Str [ i ] == ' D ' ) : NEW_LINE INDENT arr [ i ] = END NEW_LINE END -= 1 NEW_LINE DEDENT DEDENT arr [ N ] = START NEW_LINE return arr NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Str = " IDID " NEW_LINE N = len ( Str ) NEW_LINE ptr = DiStirngMatch ( Str ) NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT print ( ptr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Count of square free divisors of a given number | Python3 program to find the square free divisors of a given number ; The function to check if a number is prime or not ; If the number is even then its not prime ; Stores the count of distinct prime factors ; Print the number of square - free divisors
import math NEW_LINE def IsPrime ( i ) : NEW_LINE INDENT if ( i % 2 == 0 and i != 2 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT for j in range ( 3 , int ( math . sqrt ( i ) + 1 ) , 2 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT return 1 ; NEW_LINE DEDENT DEDENT c = 0 ; NEW_LINE N = 72 ; NEW_LINE for i in range ( 2 , int ( math . sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( IsPrime ( i ) ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT c = c + 1 NEW_LINE if ( IsPrime ( N / i ) and i != ( N / i ) ) : NEW_LINE INDENT c = c + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( pow ( 2 , c ) - 1 ) NEW_LINE
Generate Array whose difference of each element with its left yields the given Array | Function to find the sequence ; Initializing 1 st element ; Creating sequence in terms of x ; Finding min element ; Finding value of x ; Creating original sequence ; Output original sequence ; Driver code
def find_seq ( arr , m , n ) : NEW_LINE INDENT b = [ ] NEW_LINE x = 0 NEW_LINE b . append ( x ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT b . append ( x + arr [ i ] + b [ i ] ) NEW_LINE DEDENT mn = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT mn = min ( mn , b [ i ] ) NEW_LINE DEDENT x = 1 - mn NEW_LINE for i in range ( n ) : NEW_LINE INDENT b [ i ] += x NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( b [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE arr = [ - 2 , 1 ] NEW_LINE M = len ( arr ) NEW_LINE find_seq ( arr , M , N ) NEW_LINE DEDENT
Check whether count of odd and even factors of a number are equal | Python3 code for the naive approach ; Function to check condition ; Initialize even_count and od_count ; loop runs till square root ; Driver Code
import math NEW_LINE def isEqualFactors ( N ) : NEW_LINE INDENT ev_count = 0 NEW_LINE od_count = 0 NEW_LINE for i in range ( 1 , ( int ) ( math . sqrt ( N ) ) + 2 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( i == N // i ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT ev_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT od_count += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT ev_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT od_count += 1 NEW_LINE DEDENT if ( ( N // i ) % 2 == 0 ) : NEW_LINE INDENT ev_count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT od_count += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( ev_count == od_count ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT N = 10 NEW_LINE isEqualFactors ( N ) NEW_LINE
Check whether count of odd and even factors of a number are equal | Function to check condition ; Driver Code
def isEqualFactors ( N ) : NEW_LINE INDENT if ( ( N % 2 == 0 ) and ( N % 4 != 0 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT N = 10 NEW_LINE isEqualFactors ( N ) NEW_LINE N = 125 ; NEW_LINE isEqualFactors ( N ) NEW_LINE
Count of all values of N in [ L , R ] such that count of primes upto N is also prime | Function to count the number of crazy primes in the given range [ L , R ] ; Stores all primes ; Stores count of primes ; Stores if frequency of primes is a prime or not upto each index ; Sieve of Eratosthenes ; Count primes ; If i is a prime ; Stores frequency of primes ; If the frequency of primes is a prime ; Increase count of required numbers ; Return the required count ; Driver Code ; Given Range ; Function Call
def count_crazy_primes ( L , R ) : NEW_LINE INDENT prime = [ 0 ] * ( R + 1 ) NEW_LINE countPrime = [ 0 ] * ( R + 1 ) NEW_LINE freqPrime = [ 0 ] * ( R + 1 ) NEW_LINE prime [ 0 ] = prime [ 1 ] = 1 NEW_LINE p = 2 NEW_LINE while p * p <= R : NEW_LINE INDENT if ( prime [ p ] == 0 ) : NEW_LINE INDENT for i in range ( p * p , R + 1 , p ) : NEW_LINE INDENT prime [ i ] = 1 NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for i in range ( 1 , R + 1 ) : NEW_LINE INDENT countPrime [ i ] = countPrime [ i - 1 ] NEW_LINE if ( not prime [ i ] ) : NEW_LINE INDENT countPrime [ i ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , R + 1 ) : NEW_LINE INDENT freqPrime [ i ] = freqPrime [ i - 1 ] NEW_LINE if ( not prime [ countPrime [ i ] ] ) : NEW_LINE INDENT freqPrime [ i ] += 1 NEW_LINE DEDENT DEDENT return ( freqPrime [ R ] - freqPrime [ L - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 4 NEW_LINE R = 12 NEW_LINE print ( count_crazy_primes ( L , R ) ) NEW_LINE DEDENT
Largest N digit number in Base B | Function to print the largest N - digit numbers of base b ; Find the largest N digit number in base b using the formula B ^ N - 1 ; Print the largest number ; Given number and base ; Function Call
def findNumbers ( n , b ) : NEW_LINE INDENT largest = pow ( b , n ) - 1 NEW_LINE print ( largest ) NEW_LINE DEDENT N , B = 2 , 5 NEW_LINE findNumbers ( N , B ) NEW_LINE
Generate an Array such with elements maximized through swapping bits | Function to generate the maximized array elements ; Traverse the array ; Iterate to count set and unset bits ; Count of unset bits ; Count of set bits ; Bitwise right shift ; Shifting all 1 ' s ▁ to ▁ MSB ▁ ▁ and ▁ 0' s to LSB ; Driver Code
def maximizedArray ( arr , N ) : NEW_LINE INDENT i = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT num = arr [ i ] NEW_LINE one = 0 NEW_LINE zero = 0 NEW_LINE while ( num ) : NEW_LINE INDENT if ( num % 2 == 0 ) : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT one += 1 NEW_LINE DEDENT num = num >> 1 NEW_LINE DEDENT for j in range ( zero , ( one + zero ) ) : NEW_LINE INDENT num += ( 1 << j ) NEW_LINE DEDENT print ( num , end = " " ) NEW_LINE i += 1 NEW_LINE if ( N > 0 ) : NEW_LINE INDENT print ( " , ▁ " , end = " " ) NEW_LINE DEDENT N -= 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 15 , 9 , 10 , 14 ] NEW_LINE N = len ( arr ) NEW_LINE maximizedArray ( arr , N ) NEW_LINE DEDENT
Find the ratio of LCM to GCD of a given Array | Python3 program to implement above approach ; Function to calculate and return GCD of the given array ; Initialise GCD ; Once GCD is 1 , it will always be 1 with all other elements ; Return GCD ; Function to calculate and return LCM of the given array ; Initialise LCM ; LCM of two numbers is evaluated as [ ( a * b ) / gcd ( a , b ) ] ; Return LCM ; Function to print the ratio of LCM to GCD of the given array ; Driver Code
import math NEW_LINE def findGCD ( arr , n ) : NEW_LINE INDENT gcd = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT gcd = int ( math . gcd ( arr [ i ] , gcd ) ) NEW_LINE if ( gcd == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return gcd NEW_LINE DEDENT def findLCM ( arr , n ) : NEW_LINE INDENT lcm = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT lcm = int ( ( ( ( arr [ i ] * lcm ) ) / ( math . gcd ( arr [ i ] , lcm ) ) ) ) NEW_LINE DEDENT return lcm NEW_LINE DEDENT def findRatio ( arr , n ) : NEW_LINE INDENT gcd = findGCD ( arr , n ) NEW_LINE lcm = findLCM ( arr , n ) NEW_LINE print ( int ( lcm / gcd ) , " : " , "1" ) NEW_LINE DEDENT arr = [ 6 , 12 , 36 ] NEW_LINE N = len ( arr ) NEW_LINE findRatio ( arr , N ) NEW_LINE
Minimum operations required to make all Array elements divisible by K | Python3 implementation to find the Minimum number of moves required to update the array such that each of its element is divisible by K ; Function to find the Minimum number of moves required to update the array such that each of its element is divisible by K ; Initialize Map data structure ; Iterate for all the elements of the array ; Calculate the value to be added ; Check if the value equals to 0 then simply continue ; Check if the value to be added is present in the map ; Update the answer ; Print the required result We need to add 1 to maxX because we cant ignore the first move where initially X = 0 and we need to increase it by 1 to make some changes in array ; Driver code
from collections import defaultdict NEW_LINE def compute ( a , N , K ) : NEW_LINE INDENT eqVal = defaultdict ( int ) NEW_LINE maxX = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT val = a [ i ] % K NEW_LINE if ( val != 0 ) : NEW_LINE INDENT val = K - val NEW_LINE DEDENT if ( val == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( val in eqVal ) : NEW_LINE INDENT numVal = eqVal [ val ] NEW_LINE maxX = max ( maxX , val + ( K * numVal ) ) NEW_LINE eqVal [ val ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT eqVal [ val ] += 1 NEW_LINE maxX = max ( maxX , val ) NEW_LINE DEDENT DEDENT if maxX == 0 : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( maxX + 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 3 NEW_LINE a = [ 1 , 2 , 2 , 18 ] NEW_LINE N = len ( a ) NEW_LINE compute ( a , N , K ) NEW_LINE DEDENT
Array sum after replacing all occurrences of X by Y for Q queries | Function that print the sum of the array for Q queries ; Stores the frequencies of array elements ; Calculate the sum of the initial array and store the frequency of each element in map ; Iterate for all the queries ; Store query values ; Decrement the sum accordingly ; Increment the sum accordingly ; Set count of Y [ i ] ; Reset count of X [ i ] ; Print the sum ; Driver Code ; Function call
def sumOfTheArrayForQuery ( A , N , X , Y , Q ) : NEW_LINE INDENT sum = 0 NEW_LINE count = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE if A [ i ] in count : NEW_LINE INDENT count [ A [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count [ A [ i ] ] = 1 NEW_LINE DEDENT DEDENT for i in range ( Q ) : NEW_LINE INDENT x = X [ i ] NEW_LINE y = Y [ i ] NEW_LINE if X [ i ] not in count : NEW_LINE INDENT count [ X [ i ] ] = 0 NEW_LINE DEDENT if Y [ i ] not in count : NEW_LINE INDENT count [ Y [ i ] ] = 0 NEW_LINE DEDENT sum -= ( count [ X [ i ] ] * X [ i ] ) NEW_LINE sum += count [ X [ i ] ] * Y [ i ] NEW_LINE count [ Y [ i ] ] += count [ X [ i ] ] NEW_LINE count [ X [ i ] ] = 0 NEW_LINE print ( sum , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 1 , 3 , 2 , ] NEW_LINE X = [ 2 , 3 , 5 ] NEW_LINE Y = [ 3 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE Q = len ( X ) NEW_LINE sumOfTheArrayForQuery ( arr , N , X , Y , Q ) NEW_LINE
Maximum OR value of a pair in an Array | Set 2 | Function to return the maximum bitwise OR for any pair of the given array in O ( n ) time complexity . ; Find the maximum element in ; Stores the maximum OR value the array ; Traverse the array and perform Bitwise OR between every array element with the maximum element ; Driver Code
def maxOR ( arr , n ) : NEW_LINE INDENT max_value = max ( arr ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = max ( ans , ( max_value arr [ i ] ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 6 , 8 , 16 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxOR ( arr , n ) ) NEW_LINE DEDENT
Count of elements on the left which are divisible by current element | Set 2 | Utility function to prthe elements of the array ; Function to increment the count for each factor of given val ; Function to generate and print the required array ; Find max element of arr ; Create count array of maxi size ; For every element of the array ; Count [ A [ i ] ] denotes how many previous elements are there whose factor is the current element . ; Increment in count array for factors of A [ i ] ; Print the generated array ; Driver code
def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def IncrementFactors ( count , val ) : NEW_LINE INDENT i = 1 NEW_LINE while ( i * i <= val ) : NEW_LINE INDENT if ( val % i == 0 ) : NEW_LINE INDENT if ( i == val // i ) : NEW_LINE INDENT count [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count [ i ] += 1 NEW_LINE count [ val // i ] += 1 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def generateArr ( A , n ) : NEW_LINE INDENT B = [ 0 ] * n NEW_LINE maxi = max ( A ) NEW_LINE count = [ 0 ] * ( maxi + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT B [ i ] = count [ A [ i ] ] NEW_LINE IncrementFactors ( count , A [ i ] ) NEW_LINE DEDENT printArr ( B , n ) NEW_LINE DEDENT arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE generateArr ( arr , n ) NEW_LINE
Minimum changes required to make all Array elements Prime | Function to generate all primes ; If p is a prime ; Mark all its multiples as non - prime ; Store all prime numbers ; Return the list of primes ; Function to calculate the minimum increments to convert every array elements to a prime ; Extract maximum element of the given array ; Extract the index which has the next greater prime ; Store the difference between the prime and the array element ; Driver code
def SieveOfEratosthenes ( n ) : NEW_LINE INDENT prime = [ True for i in range ( 2 * n + 1 ) ] NEW_LINE p = 2 NEW_LINE while ( p * p <= 2 * n ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT i = p * p NEW_LINE while ( i <= n * 2 ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE i += p NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT primes = [ ] NEW_LINE for p in range ( 2 , ( 2 * n ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT primes . append ( p ) NEW_LINE DEDENT DEDENT return primes NEW_LINE DEDENT def minChanges ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE ans = 0 NEW_LINE maxi = max ( arr ) NEW_LINE primes = SieveOfEratosthenes ( maxi ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = - 1 NEW_LINE for j in range ( len ( primes ) ) : NEW_LINE INDENT if ( arr [ i ] == primes [ j ] ) : NEW_LINE INDENT x = j NEW_LINE break NEW_LINE DEDENT elif ( arr [ i ] < primes [ j ] ) : NEW_LINE INDENT x = j NEW_LINE break NEW_LINE DEDENT DEDENT minm = abs ( primes [ x ] - arr [ i ] ) NEW_LINE if ( x > 1 ) : NEW_LINE INDENT minm = min ( minm , abs ( primes [ x - 1 ] - arr [ i ] ) ) NEW_LINE DEDENT ans += minm NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 4 , 25 , 13 , 6 , 20 ] NEW_LINE print ( minChanges ( arr ) ) NEW_LINE
Sum of multiples of Array elements within a given range [ L , R ] | Function to find the sum of all multiples of N up to K ; Calculate the sum ; Return the sum ; Function to find the total sum ; If L is divisible by a [ i ] ; Otherwise ; Return the final sum ; Driver code ; Given array arr [ ] ; Given range ; Function call
def calcSum ( k , n ) : NEW_LINE INDENT value = ( k * n * ( n + 1 ) ) // 2 NEW_LINE return value NEW_LINE DEDENT def findSum ( a , n , L , R ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( L % a [ i ] == 0 and L != 0 ) : NEW_LINE INDENT sum += ( calcSum ( a [ i ] , R // a [ i ] ) - calcSum ( a [ i ] , ( L - 1 ) // a [ i ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT sum += ( calcSum ( a [ i ] , R // a [ i ] ) - calcSum ( a [ i ] , L // a [ i ] ) ) NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 7 , 3 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE L = 7 NEW_LINE R = 20 NEW_LINE print ( findSum ( arr , N , L , R ) ) NEW_LINE DEDENT
Count all prime numbers in a given range whose sum of digits is also prime | Python3 program for the above approach ; Create an array for storing primes ; Create a prefix array that will contain whether sum is prime or not ; Function to find primes in the range and check whether the sum of digits of a prime number is prime or not ; Initialise Prime array arr [ ] ; Since 0 and 1 are not prime numbers we mark them as '0 ; Using Sieve Of Eratosthenes ; If the number is prime ; Mark all the multiples of i starting from square of i with '0' ie . composite ; '0' represents not prime ; Initialise a sum variable as 0 ; Check if the number is prime ; A temporary variable to store the number ; Loop to calculate the sum of digits ; Check if the sum of prime number is prime ; If prime mark 1 ; If not prime mark 0 ; Computing prefix array ; Function to count the prime numbers in the range [ L , R ] ; Function call to find primes ; Print the result ; Driver Code ; Input range ; Function call
maxN = 1000000 NEW_LINE arr = [ 0 ] * ( 1000001 ) NEW_LINE prefix = [ 0 ] * ( 1000001 ) NEW_LINE def findPrimes ( ) : NEW_LINE INDENT for i in range ( 1 , maxN + 1 ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT arr [ 0 ] = 0 NEW_LINE arr [ 1 ] = 0 NEW_LINE i = 2 NEW_LINE while i * i <= maxN : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i * i , maxN , i ) : NEW_LINE INDENT arr [ j ] = 0 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT sum = 0 NEW_LINE prefix [ 0 ] = 0 NEW_LINE for i in range ( 1 , maxN + 1 ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT temp = i NEW_LINE sum = 0 NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT x = temp % 10 NEW_LINE sum += x NEW_LINE temp = temp // 10 NEW_LINE if ( arr [ sum ] == 1 ) : NEW_LINE INDENT prefix [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT prefix [ i ] = 0 NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( 1 , maxN + 1 ) : NEW_LINE INDENT prefix [ i ] += prefix [ i - 1 ] NEW_LINE DEDENT DEDENT def countNumbersInRange ( l , r ) : NEW_LINE INDENT findPrimes ( ) NEW_LINE result = ( prefix [ r ] - prefix [ l - 1 ] ) NEW_LINE print ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 5 NEW_LINE r = 20 NEW_LINE countNumbersInRange ( l , r ) NEW_LINE DEDENT
Find the concentration of a solution using given Mass and Volume | Function to calculate concentration from the given mass of solute and volume of a solution ; Driver code
def get_concentration ( mass , volume ) : NEW_LINE INDENT if ( volume == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( mass / volume ) * 1000 ; NEW_LINE DEDENT DEDENT mass = 100.00 ; NEW_LINE volume = 500.00 ; NEW_LINE print ( get_concentration ( mass , volume ) ) NEW_LINE
Practical Numbers | Python3 program to check if a number is Practical or not . ; Returns true if there is a subset of set [ ] with sun equal to given sum ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; Function to store divisors of N in a vector ; Find all divisors which divides 'num ; If ' i ' is divisor of 'n ; If both divisors are same then store it once else store both divisors ; Returns true if num is Practical ; Vector to store all divisors of N ; To check all numbers from 1 to < N ; Driver code
import math NEW_LINE def isSubsetSum ( Set , n , Sum ) : NEW_LINE INDENT subset = [ [ False for i in range ( Sum + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT subset [ i ] [ 0 ] = True NEW_LINE DEDENT for i in range ( 1 , Sum + 1 ) : NEW_LINE INDENT subset [ 0 ] [ i ] = False NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , Sum + 1 ) : NEW_LINE INDENT if ( j < Set [ i - 1 ] ) : NEW_LINE INDENT subset [ i ] [ j ] = subset [ i - 1 ] [ j ] NEW_LINE DEDENT if ( j >= Set [ i - 1 ] ) : NEW_LINE INDENT subset [ i ] [ j ] = ( subset [ i - 1 ] [ j ] or subset [ i - 1 ] [ j - Set [ i - 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT return subset [ n ] [ Sum ] NEW_LINE DEDENT def storeDivisors ( n , div ) : NEW_LINE ' NEW_LINE INDENT for i in range ( 1 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( i == int ( n / i ) ) : NEW_LINE INDENT div . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT div . append ( i ) NEW_LINE div . append ( int ( n / i ) ) NEW_LINE DEDENT DEDENT DEDENT def isPractical ( N ) : NEW_LINE INDENT div = [ ] NEW_LINE storeDivisors ( N , div ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( not isSubsetSum ( div , len ( div ) , i ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT N = 18 NEW_LINE if ( isPractical ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Super Niven Numbers | Checks if sums of all subsets of digits array divides the number N ; To calculate length of array arr ; There are totoal 2 ^ n subsets ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider binary representation of current i to decide which elements to pick . ; Check sum of picked elements . ; Function to check if a number is a super - niven number ; To store digits of N ; Driver code
def isDivBySubsetSums ( arr , num ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE total = 1 << n NEW_LINE i = 0 NEW_LINE while i < total : NEW_LINE INDENT sum = 0 NEW_LINE j = 0 NEW_LINE while j < n : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT sum += arr [ j ] NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( sum != 0 ) and ( num % sum != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def isSuperNivenNum ( n ) : NEW_LINE INDENT temp = n NEW_LINE digits = [ ] NEW_LINE while ( n > 1 ) : NEW_LINE INDENT digit = int ( n ) % 10 NEW_LINE digits . append ( digit ) NEW_LINE n = n / 10 NEW_LINE DEDENT return isDivBySubsetSums ( digits , temp ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 500 NEW_LINE if isSuperNivenNum ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT