codeparrot/xlcost-text-to-code huggingface dataset:
Find Prime Adam integers in the given range [ L , R ] | Python3 program to find all prime adam numbers in the given range ; Reversing a number by taking remainder at a time ; Function to check if a number is a prime or not ; Iterating till the number ; Checking for factors ; Returning 1 if the there are no factors of the number other than 1 or itself ; Function to check whether a number is an adam number or not ; Reversing given number ; Squaring given number ; Squaring reversed number ; Reversing the square of the reversed number ; Checking if the square of the number and the square of its reverse are equal or not ; Function to find all the prime adam numbers in the given range ; If the first number is greater than the second number , print invalid ; Iterating through all the numbers in the given range ; Checking for prime number ; Checking for Adam number ; Driver code
def reverse ( a ) : NEW_LINE INDENT rev = 0 ; NEW_LINE while ( a != 0 ) : NEW_LINE INDENT r = a % 10 ; NEW_LINE rev = rev * 10 + r ; NEW_LINE a = a // 10 ; NEW_LINE DEDENT return ( rev ) ; NEW_LINE DEDENT def prime ( a ) : NEW_LINE INDENT k = 0 ; NEW_LINE for i in range ( 2 , a ) : NEW_LINE INDENT if ( a % i == 0 ) : NEW_LINE INDENT k = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( k == 1 ) : NEW_LINE INDENT return ( 0 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( 1 ) ; NEW_LINE DEDENT DEDENT def adam ( a ) : NEW_LINE INDENT r1 = reverse ( a ) ; NEW_LINE s1 = a * a ; NEW_LINE s2 = r1 * r1 ; NEW_LINE r2 = reverse ( s2 ) ; NEW_LINE if ( s1 == r2 ) : NEW_LINE INDENT return ( 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( 0 ) ; NEW_LINE DEDENT DEDENT def find ( m , n ) : NEW_LINE INDENT if ( m > n ) : NEW_LINE INDENT print ( " INVALID INPUT " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT c = 0 ; NEW_LINE DEDENT for i in range ( m , n ) : NEW_LINE INDENT l = prime ( i ) ; NEW_LINE k = adam ( i ) ; NEW_LINE if ( ( l == 1 ) and ( k == 1 ) ) : NEW_LINE INDENT print ( i , " TABSYMBOL " , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT L = 5 ; R = 100 ; NEW_LINE find ( L , R ) ; NEW_LINE
Determine whether the given integer N is a Peculiar Number or not | Function to get sum of digits of a number ; Function to check if the number is peculiar ; Store a duplicate of n ; Driver code
def sumDig ( n ) : NEW_LINE INDENT s = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT s = s + int ( n % 10 ) NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return s NEW_LINE DEDENT def Pec ( n ) : NEW_LINE INDENT dup = n NEW_LINE dig = sumDig ( n ) NEW_LINE if ( dig * 3 == dup ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT n = 36 NEW_LINE if Pec ( n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find N numbers such that a number and its reverse are divisible by sum of its digits | Function to calculate the sum of digits ; Loop to iterate through every digit of the number ; Returning the sum of digits ; Function to calculate the reverse of a number ; Loop to calculate the reverse of the number ; Return the reverse of the number ; Function to print the first N numbers such that every number and the reverse of the number is divisible by its sum of digits ; Loop to continuously check and generate number until there are n outputs ; Variable to hold the sum of the digit of the number ; Computing the reverse of the number ; Checking if the condition satisfies . Increment the count and print the number if it satisfies . ; Driver code
def digit_sum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT m = n % 10 ; NEW_LINE sum = sum + m ; NEW_LINE n = n // 10 NEW_LINE DEDENT return ( sum ) NEW_LINE DEDENT def reverse ( n ) : NEW_LINE INDENT r = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT r = r * 10 NEW_LINE r = r + n % 10 NEW_LINE n = n // 10 NEW_LINE DEDENT return ( r ) NEW_LINE DEDENT def operation ( n ) : NEW_LINE INDENT i = 1 NEW_LINE count = 0 NEW_LINE while ( count < n ) : NEW_LINE INDENT a = digit_sum ( i ) NEW_LINE r = reverse ( i ) NEW_LINE if ( i % a == 0 and r % a == 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE count += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE operation ( n ) NEW_LINE DEDENT
Split N natural numbers into two sets having GCD of their sums greater than 1 | Function to create and print the two sets ; No such split possible for N <= 2 ; Print the first set consisting of even elements ; Print the second set consisting of odd ones ; Driver Code
def createSets ( N ) : NEW_LINE INDENT if ( N <= 2 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT for i in range ( 2 , N + 1 , 2 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE for i in range ( 1 , N + 1 , 2 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE createSets ( N ) ; NEW_LINE DEDENT
Count the nodes in the given tree whose weight is a powerful number | Python3 implementation to Count the Nodes in the given tree whose weight is a powerful number ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return False ; Check if n is not a power of 2 then this loop will execute ; Find highest power of " factor " that divides n ; Check if only factor ^ 1 divides n , then return False ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return False if n is not 1. ; Function to perform dfs ; Check if weight of the current Node is a powerful number ; Driver code ; Weights of the Node ; Edges of the tree
graph = [ [ ] for i in range ( 100 ) ] NEW_LINE weight = [ 0 ] * 100 NEW_LINE ans = 0 NEW_LINE def isPowerful ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 ; NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n /= 2 ; NEW_LINE power += 1 ; NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT factor = 3 NEW_LINE while ( factor * factor <= n ) : NEW_LINE INDENT power = 0 ; NEW_LINE while ( n % factor == 0 ) : NEW_LINE INDENT n = n / factor ; NEW_LINE power += 1 ; NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT factor += 2 ; NEW_LINE DEDENT return ( n == 1 ) ; NEW_LINE DEDENT def dfs ( Node , parent ) : NEW_LINE INDENT global ans ; NEW_LINE if ( isPowerful ( weight [ Node ] ) ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT for to in graph [ Node ] : NEW_LINE INDENT if ( to == parent ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT dfs ( to , Node ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT weight [ 1 ] = 5 ; NEW_LINE weight [ 2 ] = 10 ; NEW_LINE weight [ 3 ] = 11 ; NEW_LINE weight [ 4 ] = 8 ; NEW_LINE weight [ 5 ] = 6 ; NEW_LINE graph [ 1 ] . append ( 2 ) ; NEW_LINE graph [ 2 ] . append ( 3 ) ; NEW_LINE graph [ 2 ] . append ( 4 ) ; NEW_LINE graph [ 1 ] . append ( 5 ) ; NEW_LINE dfs ( 1 , 1 ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT
Number of ways to color boundary of each block of M * N table | Function to compute all way to fill the boundary of all sides of the unit square ; Count possible ways to fill all upper and left side of the rectangle M * N ; Count possible ways to fill all side of the all squares unit size ; Number of rows ; Number of columns
def CountWays ( N , M ) : NEW_LINE INDENT count = 1 NEW_LINE count = pow ( 3 , M + N ) NEW_LINE count *= pow ( 2 , M * N ) ; NEW_LINE return count NEW_LINE DEDENT N = 3 NEW_LINE M = 2 NEW_LINE print ( CountWays ( N , M ) ) NEW_LINE
Nth positive number whose absolute difference of adjacent digits is at most 1 | Return Nth number with absolute difference between all adjacent digits at most 1. ; To store all such numbers ; Enqueue all integers from 1 to 9 in increasing order . ; Perform the operation N times so that we can get all such N numbers . ; Store the front element of queue , in array and pop it from queue . ; If the last digit of dequeued integer is not 0 , then enqueue the next such number . ; Enqueue the next such number ; If the last digit of dequeued integer is not 9 , then enqueue the next such number . ; Driver Code
def findNthNumber ( N ) : NEW_LINE INDENT arr = [ 0 for i in range ( N + 1 ) ] NEW_LINE q = [ ] NEW_LINE for i in range ( 1 , 10 , 1 ) : NEW_LINE INDENT q . append ( i ) NEW_LINE DEDENT for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT arr [ i ] = q [ 0 ] NEW_LINE q . remove ( q [ 0 ] ) NEW_LINE if ( arr [ i ] % 10 != 0 ) : NEW_LINE INDENT q . append ( arr [ i ] * 10 + arr [ i ] % 10 - 1 ) NEW_LINE DEDENT q . append ( arr [ i ] * 10 + arr [ i ] % 10 ) NEW_LINE if ( arr [ i ] % 10 != 9 ) : NEW_LINE INDENT q . append ( arr [ i ] * 10 + arr [ i ] % 10 + 1 ) NEW_LINE DEDENT DEDENT print ( arr [ N ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 21 NEW_LINE findNthNumber ( N ) NEW_LINE DEDENT
Unique element in an array where all elements occur K times except one | Set 2 | Function that find the unique element in the array arr [ ] ; Store all unique element in set ; Sum of all element of the array ; Sum of element in the set ; Print the unique element using formula ; Driver Code ; Function call
def findUniqueElements ( arr , N , K ) : NEW_LINE INDENT s = set ( ) NEW_LINE for x in arr : NEW_LINE INDENT s . add ( x ) NEW_LINE DEDENT arr_sum = sum ( arr ) NEW_LINE set_sum = 0 NEW_LINE for x in s : NEW_LINE INDENT set_sum += x NEW_LINE DEDENT print ( ( K * set_sum - arr_sum ) // ( K - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE findUniqueElements ( arr , N , K ) NEW_LINE DEDENT
Form the Cubic equation from the given roots | Function to find the cubic equation whose roots are a , b and c ; Find the value of coefficient ; Print the equation as per the above coefficients ; Driver Code ; Function Call
def findEquation ( a , b , c ) : NEW_LINE INDENT X = ( a + b + c ) ; NEW_LINE Y = ( a * b ) + ( b * c ) + ( c * a ) ; NEW_LINE Z = ( a * b * c ) ; NEW_LINE print ( " x ^ 3 ▁ - ▁ " , X , " x ^ 2 ▁ + ▁ " , Y , " x ▁ - ▁ " , Z , " ▁ = ▁ 0" ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 ; NEW_LINE b = 2 ; NEW_LINE c = 3 ; NEW_LINE findEquation ( a , b , c ) ; NEW_LINE DEDENT
Gill 's 4th Order Method to solve Differential Equations | Python3 program to implement Gill 's method ; A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 ; Count number of iterations using step size or height h ; Initial value of y ( 0 ) ; Iterate for number of iteration ; Value of K1 ; Value of K2 ; Value of K3 ; Value of K4 ; Find the next value of y ( n + 1 ) using y ( n ) and values of K in the above steps ; Update next value of x ; Return the final value of dy / dx ; Driver Code
from math import sqrt NEW_LINE def dydx ( x , y ) : NEW_LINE INDENT return ( x - y ) / 2 NEW_LINE DEDENT def Gill ( x0 , y0 , x , h ) : NEW_LINE INDENT n = ( ( x - x0 ) / h ) NEW_LINE y = y0 NEW_LINE for i in range ( 1 , int ( n + 1 ) , 1 ) : NEW_LINE INDENT k1 = h * dydx ( x0 , y ) NEW_LINE k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) NEW_LINE k3 = h * dydx ( x0 + 0.5 * h , y + 0.5 * ( - 1 + sqrt ( 2 ) ) * k1 + k2 * ( 1 - 0.5 * sqrt ( 2 ) ) ) NEW_LINE k4 = h * dydx ( x0 + h , y - ( 0.5 * sqrt ( 2 ) ) * k2 + k3 * ( 1 + 0.5 * sqrt ( 2 ) ) ) NEW_LINE y = y + ( 1 / 6 ) * ( k1 + ( 2 - sqrt ( 2 ) ) * k2 + ( 2 + sqrt ( 2 ) ) * k3 + k4 ) NEW_LINE x0 = x0 + h NEW_LINE DEDENT return y NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x0 = 0 NEW_LINE y = 3.0 NEW_LINE x = 5.0 NEW_LINE h = 0.2 NEW_LINE print ( " y ( x ) ▁ = " , round ( Gill ( x0 , y , x , h ) , 6 ) ) NEW_LINE DEDENT
Program to print numbers from N to 1 in reverse order | Recursive function to print from N to 1 ; Driver code
def PrintReverseOrder ( N ) : NEW_LINE INDENT for i in range ( N , 0 , - 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE PrintReverseOrder ( N ) ; NEW_LINE DEDENT
Find count of numbers from 0 to n which satisfies the given equation for a value K | Function to find the values ; Calculate the LCM ; Calculate the multiples of lcm ; Find the values which satisfies the given condition ; Subtract the extra values ; Return the final result ; Driver code
def findAns ( a , b , n ) : NEW_LINE INDENT lcm = ( a * b ) // __gcd ( a , b ) ; NEW_LINE multiples = ( n // lcm ) + 1 ; NEW_LINE answer = max ( a , b ) * multiples ; NEW_LINE lastvalue = lcm * ( n // lcm ) + max ( a , b ) ; NEW_LINE if ( lastvalue > n ) : NEW_LINE INDENT answer = answer - ( lastvalue - n - 1 ) ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT def __gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT else : NEW_LINE INDENT return __gcd ( b , a % b ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 1 ; NEW_LINE b = 13 ; NEW_LINE n = 500 ; NEW_LINE print ( findAns ( a , b , n ) ) ; NEW_LINE DEDENT
Program to find if two numbers and their AM and HM are present in an array using STL | Python3 program to check if two numbers are present in an array then their AM and HM are also present . Finally , find the GM of the numbers ; Function to find the arithmetic mean of 2 numbers ; Function to find the harmonic mean of 2 numbers ; Following function checks and computes the desired results based on the means ; Calculate means ; Hash container ( set ) to store elements ; Insertion of array elements in the set ; Conditionals to check if numbers are present in array by Hashing ; Conditionals to check if the AM and HM of the numbers are present in array ; If all conditions are satisfied , the Geometric Mean is calculated ; If numbers are found but the respective AM and HM are not found in the array ; If none of the conditions are satisfied ; Driver Code
from math import sqrt NEW_LINE def ArithmeticMean ( A , B ) : NEW_LINE INDENT return ( A + B ) / 2 NEW_LINE DEDENT def HarmonicMean ( A , B ) : NEW_LINE INDENT return ( 2 * A * B ) / ( A + B ) NEW_LINE DEDENT def CheckArithmeticHarmonic ( arr , A , B , N ) : NEW_LINE INDENT AM = ArithmeticMean ( A , B ) NEW_LINE HM = HarmonicMean ( A , B ) NEW_LINE Hash = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT Hash . add ( arr [ i ] ) NEW_LINE DEDENT if ( A in Hash and B in Hash ) : NEW_LINE INDENT if ( AM in Hash and HM in Hash ) : NEW_LINE INDENT print ( " GM ▁ = " , round ( sqrt ( AM * HM ) , 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " AM ▁ and ▁ HM ▁ not ▁ found " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " Numbers ▁ not ▁ found " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1.0 , 2.0 , 2.5 , 3.0 , 4.0 , 4.5 , 5.0 , 6.0 ] NEW_LINE N = len ( arr ) NEW_LINE A = 3.0 NEW_LINE B = 6.0 NEW_LINE CheckArithmeticHarmonic ( arr , A , B , N ) NEW_LINE DEDENT
Minimum decrements to make integer A divisible by integer B | Function that print number of moves required ; Calculate modulo ; Print the required answer ; Driver Code ; Initialise A and B
def movesRequired ( a , b ) : NEW_LINE INDENT total_moves = a % b NEW_LINE print ( total_moves ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 10 NEW_LINE B = 3 NEW_LINE movesRequired ( A , B ) NEW_LINE DEDENT
Pythagorean Triplet with given sum using single loop | Function to calculate the Pythagorean triplet in O ( n ) ; Iterate a from 1 to N - 1. ; Calculate value of b ; The value of c = n - a - b ; Driver code ; Function call
def PythagoreanTriplet ( n ) : NEW_LINE INDENT flag = 0 NEW_LINE for a in range ( 1 , n , 1 ) : NEW_LINE INDENT b = ( n * n - 2 * n * a ) // ( 2 * n - 2 * a ) NEW_LINE c = n - a - b NEW_LINE if ( a * a + b * b == c * c and b > 0 and c > 0 ) : NEW_LINE INDENT print ( a , b , c ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE PythagoreanTriplet ( N ) NEW_LINE DEDENT
Check if there exists a number with X factors out of which exactly K are prime | Python3 program to check if there exists a number with X factors out of which exactly K are prime ; Function to check if such number exists ; To store the sum of powers of prime factors of X which determines the maximum count of numbers whose product can form X ; Determining the prime factors of X ; To check if the number is prime ; If X is 1 , then we cannot form a number with 1 factor and K prime factor ( as K is atleast 1 ) ; If X itself is prime then it can be represented as a power of only 1 prime factor w0hich is X itself so we return true ; If sum of the powers of prime factors of X is greater than or equal to K , which means X can be represented as a product of K numbers , we return true ; In any other case , we return false as we cannot form a number with X factors and K prime factors ; Driver code
from math import sqrt NEW_LINE def check ( X , K ) : NEW_LINE INDENT prime = 0 NEW_LINE temp = X NEW_LINE sqr = int ( sqrt ( X ) ) NEW_LINE for i in range ( 2 , sqr + 1 , 1 ) : NEW_LINE INDENT while ( temp % i == 0 ) : NEW_LINE INDENT temp = temp // i NEW_LINE prime += 1 NEW_LINE DEDENT DEDENT if ( temp > 2 ) : NEW_LINE INDENT prime += 1 NEW_LINE DEDENT if ( X == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( prime == 1 and K == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( prime >= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 4 NEW_LINE K = 2 NEW_LINE if ( check ( X , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Number of subsets with same AND , OR and XOR values in an Array | Python3 implementation to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Traverse through all the subsets ; Finding the subsets with the bits of ' i ' which are set ; Computing the bitwise AND ; Computing the bitwise OR ; Computing the bitwise XOR ; Comparing all the three values ; Driver code
mod = 1000000007 ; NEW_LINE def countSubsets ( a , n ) : NEW_LINE INDENT answer = 0 ; NEW_LINE for i in range ( 1 << n ) : NEW_LINE INDENT bitwiseAND = - 1 ; NEW_LINE bitwiseOR = 0 ; NEW_LINE bitwiseXOR = 0 ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT if ( bitwiseAND == - 1 ) : NEW_LINE INDENT bitwiseAND = a [ j ] ; NEW_LINE DEDENT else : NEW_LINE INDENT bitwiseAND &= a [ j ] ; NEW_LINE DEDENT bitwiseOR |= a [ j ] ; NEW_LINE bitwiseXOR ^= a [ j ] ; NEW_LINE DEDENT DEDENT if ( bitwiseAND == bitwiseOR and bitwiseOR == bitwiseXOR ) : NEW_LINE INDENT answer = ( answer + 1 ) % mod ; NEW_LINE DEDENT DEDENT return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 ; NEW_LINE A = [ 1 , 3 , 2 , 1 , 2 , 1 ] ; NEW_LINE print ( countSubsets ( A , N ) ) ; NEW_LINE DEDENT
Count of Subsets containing only the given value K | Function to find the number of subsets formed by the given value K ; Count is used to maintain the number of continuous K 's ; Iterating through the array ; If the element in the array is equal to K ; count * ( count + 1 ) / 2 is the total number of subsets with only K as their element ; Change count to 0 because other element apart from K has been found ; To handle the last set of K 's ; Driver code
def count ( arr , N , K ) : NEW_LINE INDENT count = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == K ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += ( count * ( count + 1 ) ) // 2 NEW_LINE count = 0 NEW_LINE DEDENT DEDENT ans = ans + ( count * ( count + 1 ) ) // 2 NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 0 , 1 , 1 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE K = 0 NEW_LINE print ( count ( arr , N , K ) ) NEW_LINE DEDENT
Ternary number system or Base 3 numbers | Function to convert a decimal number to a ternary number ; Base case ; Finding the remainder when N is divided by 3 ; Recursive function to call the function for the integer division of the value N / 3 ; Handling the negative cases ; Function to convert the decimal to ternary ; If the number is greater than 0 , compute the ternary representation of the number ; Driver Code
def convertToTernary ( N ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT x = N % 3 ; NEW_LINE N //= 3 ; NEW_LINE if ( x < 0 ) : NEW_LINE INDENT N += 1 ; NEW_LINE DEDENT convertToTernary ( N ) ; NEW_LINE if ( x < 0 ) : NEW_LINE INDENT print ( x + ( 3 * - 1 ) , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( x , end = " " ) ; NEW_LINE DEDENT DEDENT def convert ( Decimal ) : NEW_LINE INDENT print ( " Ternary ▁ number ▁ of ▁ " , Decimal , " ▁ is : ▁ " , end = " " ) ; NEW_LINE if ( Decimal != 0 ) : NEW_LINE INDENT convertToTernary ( Decimal ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" , end = " " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Decimal = 2747 ; NEW_LINE convert ( Decimal ) ; NEW_LINE DEDENT
Largest number less than or equal to Z that leaves a remainder X when divided by Y | Function to get the number ; remainder can ' t ▁ be ▁ larger ▁ ▁ than ▁ the ▁ largest ▁ number , ▁ ▁ if ▁ so ▁ then ▁ answer ▁ doesn ' t exist . ; reduce number by x ; finding the possible number that is divisible by y ; this number is always <= x as we calculated over z - x ; initialise the three integers
def get ( x , y , z ) : NEW_LINE INDENT if ( x > z ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT val = z - x NEW_LINE div = ( z - x ) // y NEW_LINE ans = div * y + x NEW_LINE return ans NEW_LINE DEDENT x = 1 NEW_LINE y = 5 NEW_LINE z = 8 NEW_LINE print ( get ( x , y , z ) ) NEW_LINE
Count of greater elements for each element in the Array | Python 3 implementation of the above approach ; Store the frequency of the array elements ; Store the sum of frequency of elements greater than the current element ; Driver code
def countOfGreaterElements ( arr , n ) : NEW_LINE INDENT mp = { i : 0 for i in range ( 1000 ) } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT x = 0 NEW_LINE p = [ ] NEW_LINE q = [ ] NEW_LINE m = [ ] NEW_LINE for key , value in mp . items ( ) : NEW_LINE INDENT m . append ( [ key , value ] ) NEW_LINE DEDENT m = m [ : : - 1 ] NEW_LINE for p in m : NEW_LINE INDENT temp = p [ 1 ] NEW_LINE mp [ p [ 0 ] ] = x NEW_LINE x += temp NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( mp [ arr [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 9 , 5 , 2 , 1 , 3 , 4 , 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE countOfGreaterElements ( arr , n ) NEW_LINE DEDENT
Minimum operations required to make two numbers equal | Python program to find minimum operations required to make two numbers equal ; Function to return the minimum operations required ; Keeping B always greater ; Reduce B such that gcd ( A , B ) becomes 1. ; Driver code
import math NEW_LINE def minOperations ( A , B ) : NEW_LINE INDENT if ( A > B ) : NEW_LINE INDENT swap ( A , B ) NEW_LINE DEDENT B = B // math . gcd ( A , B ) ; NEW_LINE return B - 1 NEW_LINE DEDENT A = 7 NEW_LINE B = 15 NEW_LINE print ( minOperations ( A , B ) ) NEW_LINE