codeparrot/xlcost-text-to-code huggingface dataset:
Find Nth positive number whose digital root is X | Python3 program to find the N - th number whose digital root is X ; Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code
import sys NEW_LINE def findDigitalRoot ( num ) : NEW_LINE INDENT sum = sys . maxsize ; NEW_LINE tempNum = num ; NEW_LINE while ( sum >= 10 ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( tempNum > 0 ) : NEW_LINE INDENT sum += tempNum % 10 ; NEW_LINE tempNum //= 10 ; NEW_LINE DEDENT tempNum = sum ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def findAnswer ( X , N ) : NEW_LINE INDENT counter = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( counter < N ) : NEW_LINE INDENT i += 1 ; NEW_LINE digitalRoot = findDigitalRoot ( i ) ; NEW_LINE if ( digitalRoot == X ) : NEW_LINE INDENT counter += 1 ; NEW_LINE DEDENT if ( counter == N ) : NEW_LINE INDENT print ( i ) ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 1 ; NEW_LINE N = 3 ; NEW_LINE findAnswer ( X , N ) ; NEW_LINE DEDENT
Find Nth positive number whose digital root is X | Function to find the N - th number with digital root as X ; Driver Code
def findAnswer ( X , N ) : NEW_LINE INDENT return ( N - 1 ) * 9 + X ; NEW_LINE DEDENT X = 7 ; NEW_LINE N = 43 ; NEW_LINE print ( findAnswer ( X , N ) ) ; NEW_LINE
Sum of the natural numbers ( up to N ) whose modulo with K yield R | Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code
def count ( N , K , R ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( i % K == R ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 20 NEW_LINE K = 4 NEW_LINE R = 3 NEW_LINE print ( count ( N , K , R ) ) NEW_LINE DEDENT
Longest sub | Function to return the length of the longest required sub - sequence ; Find the maximum element from the array ; Insert all lucas numbers below max to the set a and b are first two elements of the Lucas sequence ; If current element is a Lucas number , increment count ; Return the count ; Driver code
def LucasSequence ( arr , n ) : NEW_LINE INDENT max = arr [ 0 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] > max ) : NEW_LINE INDENT max = arr [ i ] NEW_LINE DEDENT DEDENT s = set ( ) NEW_LINE a = 2 NEW_LINE b = 1 NEW_LINE s . add ( a ) NEW_LINE s . add ( b ) NEW_LINE while ( b < max ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE s . add ( b ) NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] in s ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( LucasSequence ( arr , n ) ) NEW_LINE DEDENT
Find the number of solutions to the given equation | Function to return the count of valid values of X ; Iterate through all possible sum of digits of X ; Get current value of X for sum of digits i ; Find sum of digits of cr ; If cr is a valid choice for X ; Return the count ; Driver code
def getCount ( a , b , c ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , 82 ) : NEW_LINE INDENT cr = b * pow ( i , a ) + c NEW_LINE tmp = cr NEW_LINE sm = 0 NEW_LINE while ( tmp ) : NEW_LINE INDENT sm += tmp % 10 NEW_LINE tmp //= 10 NEW_LINE DEDENT if ( sm == i and cr < 10 ** 9 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT a , b , c = 3 , 2 , 8 NEW_LINE print ( getCount ( a , b , c ) ) NEW_LINE
Check if an array of 1 s and 2 s can be divided into 2 parts with equal sum | Function to check if it is possible to split the array in two halfs with equal Sum ; Calculate Sum of elements and count of 1 's ; If total Sum is odd , return False ; If Sum of each half is even , return True ; If Sum of each half is even but there is atleast one 1 ; Driver Code
def isSpiltPossible ( n , a ) : NEW_LINE INDENT Sum = 0 NEW_LINE c1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += a [ i ] NEW_LINE if ( a [ i ] == 1 ) : NEW_LINE INDENT c1 += 1 NEW_LINE DEDENT DEDENT if ( Sum % 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( ( Sum // 2 ) % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( c1 > 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 3 NEW_LINE a = [ 1 , 1 , 2 ] NEW_LINE if ( isSpiltPossible ( n , a ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Make a tree with n vertices , d diameter and at most vertex degree k | Function to Make a tree with n vertices , d as it 's diameter and degree of each vertex is at most k ; If diameter > n - 1 impossible to build tree ; To store the degree of each vertex ; To store the edge between vertices ; To store maximum distance among all the paths from a vertex ; Make diameter of tree equals to d ; Add an edge between them ; Store maximum distance of each vertex from other vertices ; For next ( n - d - 1 ) edges ; If the vertex already has the degree k ; If not possible ; Increase the degree of vertices ; Add an edge between them ; Store the maximum distance of this vertex from other vertices ; Prall the edges of the built tree ; Driver code
def Make_tree ( n , d , k ) : NEW_LINE INDENT if ( d > n - 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT deg = [ 0 ] * ( n ) NEW_LINE ans = [ ] NEW_LINE q = { } NEW_LINE for i in range ( d ) : NEW_LINE INDENT deg [ i ] += 1 NEW_LINE deg [ i + 1 ] += 1 NEW_LINE if ( deg [ i ] > k or deg [ i + 1 ] > k ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT ans . append ( ( i , i + 1 ) ) NEW_LINE DEDENT for i in range ( 1 , d ) : NEW_LINE INDENT q [ ( max ( i , d - i ) , i ) ] = 1 NEW_LINE DEDENT for i in range ( d + 1 , n ) : NEW_LINE INDENT arr = list ( q . keys ( ) ) NEW_LINE while ( len ( q ) > 0 and deg [ arr [ 0 ] [ 1 ] ] == k ) : NEW_LINE INDENT del q [ arr [ 0 ] ] NEW_LINE DEDENT if ( len ( q ) == 0 or arr [ 0 ] [ 0 ] == d ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT deg [ i ] += 1 NEW_LINE deg [ arr [ 0 ] [ 1 ] ] += 1 NEW_LINE ans . append ( ( i , arr [ 0 ] [ 1 ] ) ) NEW_LINE q [ ( arr [ 0 ] [ 0 ] + 1 , i ) ] = 1 NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT print ( ans [ i ] [ 0 ] + 1 , ans [ i ] [ 1 ] + 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , d , k = 6 , 3 , 4 NEW_LINE Make_tree ( n , d , k ) NEW_LINE DEDENT
Sum of all Submatrices of a Given Matrix | Python3 program to find the sum of all possible submatrices of a given Matrix ; Function to find the sum of all possible submatrices of a given Matrix ; Variable to store the required sum ; Nested loop to find the number of submatrices , each number belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
n = 3 NEW_LINE def matrixSum ( arr ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT top_left = ( i + 1 ) * ( j + 1 ) ; NEW_LINE bottom_right = ( n - i ) * ( n - j ) ; NEW_LINE sum += ( top_left * bottom_right * arr [ i ] [ j ] ) ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ] ; NEW_LINE print ( matrixSum ( arr ) ) NEW_LINE DEDENT
Maximum Bitwise AND pair from given range | Function to return the maximum bitwise AND possible among all the possible pairs ; Maximum among all ( i , j ) pairs ; Driver code
def maxAND ( L , R ) : NEW_LINE INDENT maximum = L & R NEW_LINE for i in range ( L , R , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , R + 1 , 1 ) : NEW_LINE INDENT maximum = max ( maximum , ( i & j ) ) NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 1 NEW_LINE R = 632 NEW_LINE print ( maxAND ( L , R ) ) NEW_LINE DEDENT
Split the array into odd number of segments of odd lengths | Function to check ; Check the result by processing the first & last element and size ; Driver code
def checkArray ( arr , n ) : NEW_LINE INDENT return ( ( arr [ 0 ] % 2 ) and ( arr [ n - 1 ] % 2 ) and ( n % 2 ) ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) ; NEW_LINE if checkArray ( arr , n ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT
Minimum removals to make array sum odd | Function to find minimum removals ; Count odd numbers ; If the counter is odd return 0 otherwise return 1 ; Driver Code
def findCount ( arr , n ) : NEW_LINE INDENT countOdd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT countOdd += 1 ; NEW_LINE DEDENT DEDENT if ( countOdd % 2 == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findCount ( arr , n ) ) ; NEW_LINE DEDENT
Check whether the number can be made perfect square after adding 1 | Python3 implementation of the approach ; Function that returns true if x is a perfect square ; Find floating po value of square root of x ; If square root is an eger ; Function that returns true if n is a sunny number ; If ( n + 1 ) is a perfect square ; Driver code
import math as mt NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = mt . sqrt ( x ) NEW_LINE return ( ( sr - mt . floor ( sr ) ) == 0 ) NEW_LINE DEDENT def isSunnyNum ( n ) : NEW_LINE INDENT if ( isPerfectSquare ( n + 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 3 NEW_LINE if ( isSunnyNum ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Minimum operations to make counts of remainders same in an array | Function to return the minimum number of operations required ; To store modulos values ; If it 's size greater than k  it needed to be decreased ; If it 's size is less than k  it needed to be increased ; Driver code
def minOperations ( n , a , m ) : NEW_LINE INDENT k = n // m NEW_LINE val = [ [ ] for i in range ( m ) ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT val [ a [ i ] % m ] . append ( i ) NEW_LINE DEDENT ans = 0 NEW_LINE extra = [ ] NEW_LINE for i in range ( 0 , 2 * m ) : NEW_LINE INDENT cur = i % m NEW_LINE while len ( val [ cur ] ) > k : NEW_LINE INDENT elem = val [ cur ] . pop ( ) NEW_LINE extra . append ( ( elem , i ) ) NEW_LINE DEDENT while ( len ( val [ cur ] ) < k and len ( extra ) > 0 ) : NEW_LINE INDENT elem = extra [ - 1 ] [ 0 ] NEW_LINE mmod = extra [ - 1 ] [ 1 ] NEW_LINE extra . pop ( ) NEW_LINE val [ cur ] . append ( elem ) NEW_LINE ans += i - mmod NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = 3 NEW_LINE a = [ 3 , 2 , 0 , 6 , 10 , 12 ] NEW_LINE n = len ( a ) NEW_LINE print ( minOperations ( n , a , m ) ) NEW_LINE DEDENT
Count primes that can be expressed as sum of two consecutive primes and 1 | Python3 implementation of the approach ; To check if a number is prime or not ; To store possible numbers ; Function to return all prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count all possible prime numbers that can be expressed as the sum of two consecutive primes and one ; All possible prime numbers below N ; Driver code
from math import sqrt ; NEW_LINE N = 100005 ; NEW_LINE isprime = [ True ] * N ; NEW_LINE can = [ False ] * N ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( isprime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , N , p ) : NEW_LINE INDENT isprime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT primes = [ ] ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( isprime [ i ] ) : NEW_LINE INDENT primes . append ( i ) ; NEW_LINE DEDENT DEDENT return primes ; NEW_LINE DEDENT def Prime_Numbers ( n ) : NEW_LINE INDENT primes = SieveOfEratosthenes ( ) ; NEW_LINE for i in range ( len ( primes ) - 1 ) : NEW_LINE INDENT if ( primes [ i ] + primes [ i + 1 ] + 1 < N ) : NEW_LINE INDENT can [ primes [ i ] + primes [ i + 1 ] + 1 ] = True ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( can [ i ] and isprime [ i ] ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 50 ; NEW_LINE print ( Prime_Numbers ( n ) ) ; NEW_LINE DEDENT
Sum of bitwise AND of all subarrays | Python3 program to find Sum of bitwise AND of all subarrays ; Function to find the Sum of bitwise AND of all subarrays ; variable to store the final Sum ; multiplier ; variable to check if counting is on ; variable to store the length of the subarrays ; loop to find the contiguous segments ; updating the multiplier ; returning the Sum ; Driver Code
import math as mt NEW_LINE def findAndSum ( arr , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE mul = 1 NEW_LINE for i in range ( 30 ) : NEW_LINE INDENT count_on = 0 NEW_LINE l = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) : NEW_LINE INDENT if ( count_on ) : NEW_LINE INDENT l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_on = 1 NEW_LINE l += 1 NEW_LINE DEDENT DEDENT elif ( count_on ) : NEW_LINE INDENT Sum += ( ( mul * l * ( l + 1 ) ) // 2 ) NEW_LINE count_on = 0 NEW_LINE l = 0 NEW_LINE DEDENT DEDENT if ( count_on ) : NEW_LINE INDENT Sum += ( ( mul * l * ( l + 1 ) ) // 2 ) NEW_LINE count_on = 0 NEW_LINE l = 0 NEW_LINE DEDENT mul *= 2 NEW_LINE DEDENT return Sum NEW_LINE DEDENT arr = [ 7 , 1 , 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findAndSum ( arr , n ) ) NEW_LINE
Source to destination in 2 | Function that returns true if it is possible to move from source to the destination with the given moves ; Driver code
def isPossible ( Sx , Sy , Dx , Dy , x , y ) : NEW_LINE INDENT if ( abs ( Sx - Dx ) % x == 0 and abs ( Sy - Dy ) % y == 0 and ( abs ( Sx - Dx ) / x ) % 2 == ( abs ( Sy - Dy ) / y ) % 2 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT Sx = 0 ; NEW_LINE Sy = 0 ; NEW_LINE Dx = 0 ; NEW_LINE Dy = 0 ; NEW_LINE x = 3 ; NEW_LINE y = 4 ; NEW_LINE if ( isPossible ( Sx , Sy , Dx , Dy , x , y ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Count of pairs ( x , y ) in an array such that x < y | Function to return the number of pairs ( x , y ) such that x < y ; To store the number of valid pairs ; If a valid pair is found ; Return the count of valid pairs ; Driver code
def getPairs ( a ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT for j in range ( len ( a ) ) : NEW_LINE INDENT if ( a [ i ] < a [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 4 , 3 , 1 ] NEW_LINE print ( getPairs ( a ) ) NEW_LINE DEDENT
Composite numbers with digit sum 1 | Function that returns true if number n is a composite number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if the eventual digit sum of number nm is 1 ; Loop till the sum is not single digit number ; Intitialize the sum as zero ; Find the sum of digits ; If sum is eventually 1 ; Function to print the required numbers from the given range ; If i is one of the required numbers ; Driver code
def isComposite ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return False NEW_LINE DEDENT def isDigitSumOne ( nm ) : NEW_LINE INDENT while ( nm > 9 ) : NEW_LINE INDENT sum_digit = 0 NEW_LINE while ( nm != 0 ) : NEW_LINE INDENT digit = nm % 10 NEW_LINE sum_digit = sum_digit + digit NEW_LINE nm = nm // 10 NEW_LINE DEDENT nm = sum_digit NEW_LINE DEDENT if ( nm == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printValidNums ( m , n ) : NEW_LINE INDENT for i in range ( m , n + 1 ) : NEW_LINE INDENT if ( isComposite ( i ) and isDigitSumOne ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT l = 10 NEW_LINE r = 100 NEW_LINE printValidNums ( m , n ) NEW_LINE
Determine the count of Leaf nodes in an N | Function to calculate leaf nodes in n - ary tree ; Driver Code
def calcNodes ( N , I ) : NEW_LINE INDENT result = 0 NEW_LINE result = I * ( N - 1 ) + 1 NEW_LINE return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE I = 2 NEW_LINE print ( " Leaf ▁ nodes ▁ = ▁ " , calcNodes ( N , I ) ) NEW_LINE DEDENT
An application on Bertrand 's ballot theorem | Python3 implementation of the approach ; Function to calculate factorial of a number mod 1000000007 ; Factorial of i = factorial of ( i - 1 ) * i ; Taking mod along with calculation . ; Function for modular exponentiation ; If p is odd ; If p is even ; Function to return the count of required permutations ; Calculating multiplicative modular inverse for x ! and multiplying with ans ; Calculating multiplicative modular inverse for y ! and multiplying with ans ; Driver Code ; Pre - compute factorials
mod = 1000000007 NEW_LINE arr = [ 0 ] * ( 1000001 ) NEW_LINE def cal_factorial ( ) : NEW_LINE INDENT arr [ 0 ] = 1 NEW_LINE for i in range ( 1 , 1000001 ) : NEW_LINE INDENT arr [ i ] = ( arr [ i - 1 ] * i ) % mod NEW_LINE DEDENT DEDENT def mod_exponent ( num , p ) : NEW_LINE INDENT if ( p == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( p & 1 ) : NEW_LINE INDENT return ( ( num % mod ) * ( mod_exponent ( ( num * num ) % mod , p // 2 ) ) % mod ) % mod NEW_LINE DEDENT elif ( not ( p & 1 ) ) : NEW_LINE INDENT return ( mod_exponent ( ( num * num ) % mod , p // 2 ) ) % mod NEW_LINE DEDENT DEDENT def getCount ( x , y ) : NEW_LINE INDENT ans = arr [ x + y - 1 ] NEW_LINE ans *= mod_exponent ( arr [ x ] , mod - 2 ) NEW_LINE ans %= mod NEW_LINE ans *= mod_exponent ( arr [ y ] , mod - 2 ) NEW_LINE ans %= mod NEW_LINE ans *= ( x - y ) NEW_LINE ans %= mod NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT cal_factorial ( ) NEW_LINE x = 3 NEW_LINE y = 1 NEW_LINE print ( getCount ( x , y ) ) NEW_LINE DEDENT
Find the values of X and Y in the Given Equations | Function to find the values of X and Y ; base condition ; required answer ; Driver Code
def findValues ( a , b ) : NEW_LINE INDENT if ( ( a - b ) % 2 == 1 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT print ( ( a - b ) // 2 , ( a + b ) // 2 ) ; NEW_LINE DEDENT a = 12 ; b = 8 ; NEW_LINE findValues ( a , b ) ; NEW_LINE
Program to implement Inverse Interpolation using Lagrange Formula | Consider a structure to keep each pair of x and y together ; Function to calculate the inverse interpolation ; Initialize final x ; Calculate each term of the given formula ; Add term to final result ; Driver Code ; Sample dataset of 4 points Here we find the value of x when y = 4.5 ; Size of dataset ; Sample y value ; Using the Inverse Interpolation function to find the value of x when y = 4.5
class Data : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT def inv_interpolate ( d : list , n : int , y : float ) -> float : NEW_LINE INDENT x = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT xi = d [ i ] . x NEW_LINE for j in range ( n ) : NEW_LINE INDENT if j != i : NEW_LINE INDENT xi = ( xi * ( y - d [ j ] . y ) / ( d [ i ] . y - d [ j ] . y ) ) NEW_LINE DEDENT DEDENT x += xi NEW_LINE DEDENT return x NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT d = [ Data ( 1.27 , 2.3 ) , Data ( 2.25 , 2.95 ) , Data ( 2.5 , 3.5 ) , Data ( 3.6 , 5.1 ) ] NEW_LINE n = 4 NEW_LINE y = 4.5 NEW_LINE print ( " Value ▁ of ▁ x ▁ at ▁ y ▁ = ▁ 4.5 ▁ : " , round ( inv_interpolate ( d , n , y ) , 5 ) ) NEW_LINE DEDENT
Count triplet pairs ( A , B , C ) of points in 2 | Function to return the count of possible triplets ; Insert all the points in a set ; If the mid point exists in the set ; Return the count of valid triplets ; Driver code
def countTriplets ( n , points ) : NEW_LINE INDENT pts = [ ] NEW_LINE ct = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT pts . append ( points [ i ] ) ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT x = points [ i ] [ 0 ] + points [ j ] [ 0 ] ; NEW_LINE y = points [ i ] [ 1 ] + points [ j ] [ 1 ] ; NEW_LINE if ( x % 2 == 0 and y % 2 == 0 ) : NEW_LINE INDENT if [ x // 2 , y // 2 ] in pts : NEW_LINE INDENT ct += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ct NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT points = [ [ 1 , 1 ] , [ 2 , 2 ] , [ 3 , 3 ] ] NEW_LINE n = len ( points ) NEW_LINE print ( countTriplets ( n , points ) ) NEW_LINE DEDENT
Concentration of juice after mixing n glasses in equal proportion | Function to return the concentration of the resultant mixture ; Driver code
def mixtureConcentration ( n , p ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT res += p [ i ] ; NEW_LINE DEDENT res /= n ; NEW_LINE return res ; NEW_LINE DEDENT arr = [ 0 , 20 , 20 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( round ( mixtureConcentration ( n , arr ) , 4 ) ) ; NEW_LINE