codeparrot/xlcost-text-to-code huggingface dataset:
Find all the possible remainders when N is divided by all positive integers from 1 to N + 1 | Python3 implementation of the approach ; Function to find all the distinct remainders when n is divided by all the elements from the range [ 1 , n + 1 ] ; Set will be used to store the remainders in order to eliminate duplicates ; Find the remainders ; Print the contents of the set ; Driver code
from math import ceil , floor , sqrt NEW_LINE def findRemainders ( n ) : NEW_LINE INDENT vc = dict ( ) NEW_LINE for i in range ( 1 , ceil ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT vc [ n // i ] = 1 NEW_LINE DEDENT for i in range ( n // ceil ( sqrt ( n ) ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT vc [ i ] = 1 NEW_LINE DEDENT for it in sorted ( vc ) : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE findRemainders ( n ) NEW_LINE
Count of primes below N which can be expressed as the sum of two primes | Python3 implementation of the approach ; Function for Sieve of Eratosthenes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to return the count of primes less than or equal to n which can be expressed as the sum of two primes ; To store the required count ; If the integer is prime and it can be expressed as the sum of 2 and a prime number ; Driver code
MAX = 100005 NEW_LINE prime = [ True for i in range ( MAX ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( MAX ) : NEW_LINE INDENT if ( p * p > MAX ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( 2 * p , MAX , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def countPrimes ( n ) : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE cnt = 0 NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( prime [ i ] and prime [ i - 2 ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT n = 11 NEW_LINE print ( countPrimes ( n ) ) NEW_LINE
Print all palindrome dates between the given years | Python 3 implementation of the approach ; Returns true if given year is valid ; Return true if year is a multiple pf 4 and not multiple of 100. OR year is multiple of 400. ; Returns true if given year is valid or not . ; If year , month and day are not in given range ; Handle February month with leap year ; Months of April , June , Sept and Nov must have number of days less than or equal to 30. ; Function to print the palindrome dates between the given years ; For every year ; Current year as a string ; To store the reverse of year ; Get the day and the month ; If the current palindrome date is valid ; Driver code
MAX_VALID_YR = 9999 NEW_LINE MIN_VALID_YR = 1800 NEW_LINE def isLeap ( year ) : NEW_LINE INDENT return ( ( ( year % 4 == 0 ) and ( year % 100 != 0 ) ) or ( year % 400 == 0 ) ) NEW_LINE DEDENT def isValidDate ( d , m , y ) : NEW_LINE INDENT if ( y > MAX_VALID_YR or y < MIN_VALID_YR ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( m < 1 or m > 12 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( d < 1 or d > 31 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( m == 2 ) : NEW_LINE INDENT if ( isLeap ( y ) ) : NEW_LINE INDENT return ( d <= 29 ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( d <= 28 ) NEW_LINE DEDENT DEDENT if ( m == 4 or m == 6 or m == 9 or m == 11 ) : NEW_LINE INDENT return ( d <= 30 ) NEW_LINE DEDENT return True NEW_LINE DEDENT def printPalindromeDates ( y1 , y2 ) : NEW_LINE INDENT for year in range ( y1 , y2 + 1 , 1 ) : NEW_LINE INDENT str1 = str ( year ) NEW_LINE rev = str1 NEW_LINE rev = rev [ : : - 1 ] NEW_LINE day = int ( rev [ 0 : 2 ] ) NEW_LINE month = int ( rev [ 2 : 4 ] ) NEW_LINE rev += str1 NEW_LINE if ( isValidDate ( day , month , year ) ) : NEW_LINE INDENT print ( rev ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT y1 = 2001 NEW_LINE y2 = 2005 NEW_LINE printPalindromeDates ( y1 , y2 ) NEW_LINE DEDENT
Find an integer in the given range that satisfies the given conditions | Python3 implementation of the approach ; Function that returns true if x contains all distinct digits ; Last digit of x ; If current digit has appeared before ; Mark the current digit to present ; Remove the last digit ; Function to return the required value of k ; To store the maximum value for the given expression ; If i contains all distinct digits ; If the value of the expression is also maximum then update k and the expression ; Driver code
import sys NEW_LINE MAX = 10 NEW_LINE def distinctDigits ( x ) : NEW_LINE INDENT present = [ False for i in range ( MAX ) ] NEW_LINE while ( x > 0 ) : NEW_LINE INDENT digit = x % 10 NEW_LINE if ( present [ digit ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT present [ digit ] = True NEW_LINE x = x // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def findK ( l , r ) : NEW_LINE INDENT maxExp = - sys . maxsize - 1 NEW_LINE k = - 1 NEW_LINE for i in range ( l , r + 1 , 1 ) : NEW_LINE INDENT if ( distinctDigits ( i ) ) : NEW_LINE INDENT exp = ( l - i ) * ( i - r ) NEW_LINE if ( exp >= maxExp ) : NEW_LINE INDENT k = i ; NEW_LINE maxExp = exp NEW_LINE DEDENT DEDENT DEDENT return k NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 50 NEW_LINE r = 60 NEW_LINE print ( findK ( l , r ) ) NEW_LINE DEDENT
Convert Decimal To Hexa | Function to convert decimal no . to hexadecimal number ; map for decimal to hexa , 0 - 9 are straightforward , alphabets a - f used for 10 to 15. ; string to be returned ; check if num is 0 and directly return "0" ; if num > 0 , use normal technique as discussed in other post ; if num < 0 , we need to use the elaborated trick above , lets see this ; store num in a u_int , size of u_it is greater , it will be positive since msb is 0 ; use the same remainder technique . ; Driver Code
def Hex ( num ) : NEW_LINE INDENT m = dict . fromkeys ( range ( 16 ) , 0 ) ; NEW_LINE digit = ord ( '0' ) ; NEW_LINE c = ord ( ' a ' ) ; NEW_LINE for i in range ( 16 ) : NEW_LINE INDENT if ( i < 10 ) : NEW_LINE INDENT m [ i ] = chr ( digit ) ; NEW_LINE digit += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT m [ i ] = chr ( c ) ; NEW_LINE c += 1 NEW_LINE DEDENT DEDENT res = " " ; NEW_LINE if ( not num ) : NEW_LINE INDENT return "0" ; NEW_LINE DEDENT if ( num > 0 ) : NEW_LINE INDENT while ( num ) : NEW_LINE INDENT res = m [ num % 16 ] + res ; NEW_LINE num //= 16 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT n = num + 2 ** 32 ; NEW_LINE while ( n ) : NEW_LINE INDENT res = m [ n % 16 ] + res ; NEW_LINE n //= 16 ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 134 ; y = - 1 ; z = - 234 ; NEW_LINE print ( " Hexa ▁ representation ▁ for " ) ; NEW_LINE print ( x , " is " , Hex ( x ) ) ; NEW_LINE print ( y , " is " , Hex ( y ) ) ; NEW_LINE print ( z , " is " , Hex ( z ) ) ; NEW_LINE DEDENT
Find the player who will win the Coin game | Function to check the wining player ; As discussed in the above approach ; Driver Code
def findWinner ( n ) : NEW_LINE INDENT if ( ( n - 1 ) % 6 == 0 ) : NEW_LINE INDENT print ( " Second ▁ Player ▁ wins ▁ the ▁ game " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " First ▁ Player ▁ wins ▁ the ▁ game " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 ; NEW_LINE findWinner ( n ) ; NEW_LINE DEDENT
Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls | Python3 implementation of the approach ; To store the factorial and the factorial mod inverse of a number ; Function to find ( a ^ m1 ) % mod ; Function to find factorial of all the numbers ; Function to find the factorial mod inverse of all the numbers ; Function to return nCr ; Function to find ways to arrange K green balls among N balls such that we need exactly i moves to collect all K green balls ; Driver code ; Function call
N = 100005 NEW_LINE mod = ( int ) ( 1e9 + 7 ) NEW_LINE factorial = [ 0 ] * N ; NEW_LINE modinverse = [ 0 ] * N ; NEW_LINE def power ( a , m1 ) : NEW_LINE INDENT if ( m1 == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( m1 == 1 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT elif ( m1 == 2 ) : NEW_LINE INDENT return ( a * a ) % mod ; NEW_LINE DEDENT elif ( m1 & 1 ) : NEW_LINE INDENT return ( a * power ( power ( a , m1 // 2 ) , 2 ) ) % mod ; NEW_LINE DEDENT else : NEW_LINE INDENT return power ( power ( a , m1 // 2 ) , 2 ) % mod ; NEW_LINE DEDENT DEDENT def factorialfun ( ) : NEW_LINE INDENT factorial [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; NEW_LINE DEDENT DEDENT def modinversefun ( ) : NEW_LINE INDENT modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; NEW_LINE DEDENT DEDENT def binomial ( n , r ) : NEW_LINE INDENT if ( r > n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; NEW_LINE a = ( a * modinverse [ r ] ) % mod ; NEW_LINE return a ; NEW_LINE DEDENT def arrange_balls ( n , k ) : NEW_LINE INDENT factorialfun ( ) ; NEW_LINE modinversefun ( ) ; NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT print ( ( binomial ( n - k + 1 , i ) * binomial ( k - 1 , i - 1 ) ) % mod , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; k = 3 ; NEW_LINE arrange_balls ( n , k ) ; NEW_LINE DEDENT
Minimum inversions required so that no two adjacent elements are same | Function to return the minimum inversions required so that no two adjacent elements are same ; To store the inversions required to make the array { 1 , 0 , 1 , 0 , 1 , 0 , 1 , ... } and { 0 , 1 , 0 , 1 , 0 , 1 , 0 , ... } respectively ; Find all the changes required ; Return the required answer ; Driver code
def min_changes ( a , n ) : NEW_LINE INDENT ans_a = 0 ; NEW_LINE ans_b = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT ans_a += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans_b += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT ans_b += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans_a += 1 ; NEW_LINE DEDENT DEDENT DEDENT return min ( ans_a , ans_b ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( min_changes ( a , n ) ) ; NEW_LINE DEDENT
Sum of all the numbers present at given level in Modified Pascal ’ s triangle | Function to calculate sum ; Driver Code
def ans ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( "1" , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" , end = " " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE ans ( n ) ; NEW_LINE DEDENT
Find if there exists multiple ways to draw line through ( x , y ) to cut rectangle in equal halfs | Function that returns true if multiple lines are possible passing through ( x , y ) that divide the given rectangle into two equal parts ; If the point ( x , y ) is the centre of the rectangle ; Driver code
def isPossible ( w , h , x , y ) : NEW_LINE INDENT if ( x * 2 == w and y * 2 == h ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT w = 1 NEW_LINE h = 2 NEW_LINE x = 1 NEW_LINE y = 2 NEW_LINE if ( isPossible ( w , h , x , y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimize the number by changing at most K digits | Function to return the minimized number ; Total digits in the number ; If the string is empty or there are no operations to perform ; "0" is a valid number ; If the first digit is not already 1 then update it to 1 and decrement k ; While there are operations left and the number can still be updated ; If the current digit is not already 0 then update it to 0 and decrement k ; Return the minimised number ; Driver code
def minNum ( num , k ) : NEW_LINE INDENT len_ = len ( num ) NEW_LINE if len_ == 0 or k == 0 : NEW_LINE INDENT return num NEW_LINE DEDENT if len_ == 1 : NEW_LINE INDENT return "0" NEW_LINE DEDENT if num [ 0 ] != '1' : NEW_LINE INDENT num = '1' + num [ 1 : ] NEW_LINE k -= 1 NEW_LINE DEDENT i = 1 NEW_LINE while k > 0 and i < len_ : NEW_LINE INDENT if num [ i ] != '0' : NEW_LINE INDENT num = num [ : i ] + '0' + num [ i + 1 : ] NEW_LINE k -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return num NEW_LINE DEDENT num = "91945" NEW_LINE k = 3 NEW_LINE print ( minNum ( num , k ) ) NEW_LINE
Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M | Function to calculate the minimum number of steps required total steps taken is divisible by m and only 1 or 2 steps can be taken at a time ; If m > n ans is - 1 ; else discussed above approach ; Driver code
def minsteps ( n , m ) : NEW_LINE INDENT if ( m > n ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( n + 1 ) // 2 + m - 1 ) // m * m ; NEW_LINE DEDENT DEDENT n = 17 NEW_LINE m = 4 NEW_LINE ans = minsteps ( n , m ) NEW_LINE print ( ans ) NEW_LINE
Compare numbers represented by Linked Lists | Structure for a linked list node ; A helper function to remove zeros from the start of the linked list ; A helper function to find the length of linked list ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Allocate node ; Set the data ; Link the old list after the new node ; Set the head to point to the new node ; Function to compare the numbers represented as linked lists ; Remover leading zeroes from the linked lists ; Since the number represented by a has a greater length , it will be greater ; If the lengths of two numbers are equal we have to check their magnitudes ; If we reach here , then a and b are not None and their data is same , so move to next nodes in both lists ; If linked lists are identical , then we need to return zero ; Driver code ; The constructed linked lists are : a : 5.6 . 7 b : 2.3 . 3
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = 0 NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def removeLeadingZeros ( a ) : NEW_LINE INDENT if ( a != None and a . data == 0 ) : NEW_LINE INDENT return removeLeadingZeros ( a . next ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return a ; NEW_LINE DEDENT DEDENT def getSize ( a ) : NEW_LINE INDENT sz = 0 ; NEW_LINE while ( a != None ) : NEW_LINE INDENT a = a . next ; NEW_LINE sz += 1 NEW_LINE DEDENT return sz ; NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) NEW_LINE new_node . data = new_data ; NEW_LINE new_node . next = ( head_ref ) ; NEW_LINE ( head_ref ) = new_node ; NEW_LINE return head_ref NEW_LINE DEDENT def compare ( a , b ) : NEW_LINE INDENT a = removeLeadingZeros ( a ) ; NEW_LINE b = removeLeadingZeros ( b ) ; NEW_LINE lenA = getSize ( a ) ; NEW_LINE lenB = getSize ( b ) ; NEW_LINE if ( lenA > lenB ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( lenB > lenA ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT while ( a != None and b != None ) : NEW_LINE INDENT if ( a . data > b . data ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( a . data < b . data ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT a = a . next ; NEW_LINE b = b . next ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = None ; NEW_LINE a = push ( a , 7 ) ; NEW_LINE a = push ( a , 6 ) ; NEW_LINE a = push ( a , 5 ) ; NEW_LINE b = None ; NEW_LINE b = push ( b , 3 ) ; NEW_LINE b = push ( b , 3 ) ; NEW_LINE b = push ( b , 2 ) ; NEW_LINE print ( compare ( a , b ) ) NEW_LINE DEDENT
Sum of the updated array after performing the given operation | Utility function to return the sum of the array ; Function to return the sum of the modified array ; Find the sum of the subarray arr [ i + 1. . . n - 1 ] ; Subtract the subarray sum ; Return the sum of the modified array ; Driver code
def sumArr ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumModArr ( arr , n ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT subSum = 0 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT subSum += arr [ j ] NEW_LINE DEDENT arr [ i ] -= subSum NEW_LINE DEDENT return sumArr ( arr , n ) NEW_LINE DEDENT arr = [ 40 , 25 , 12 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumModArr ( arr , n ) ) NEW_LINE
Find the minimum possible health of the winning player | Python3 implementation of the approach ; Function to return the minimum possible health of the last player ; Find the GCD of the array elements ; Driver code
from math import gcd NEW_LINE def minHealth ( health , n ) : NEW_LINE INDENT __gcd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT __gcd = gcd ( __gcd , health [ i ] ) ; NEW_LINE DEDENT return __gcd ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT health = [ 5 , 6 , 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( health ) ; NEW_LINE print ( minHealth ( health , n ) ) ; NEW_LINE DEDENT
Construct an array from its pair | Python3 implementation of the approach ; Utility function to print the array ; Function to generate the original array from the pair - product array ; First element of the resulting array ; Find all the other elements ; Print the elements of the generated array ; Driver code
from math import sqrt NEW_LINE def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def constructArr ( pair , n ) : NEW_LINE INDENT size = int ( ( 1 + sqrt ( 1 + 8 * n ) ) // 2 ) ; NEW_LINE arr = [ 0 ] * ( size ) ; NEW_LINE arr [ 0 ] = int ( sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ) ; NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT arr [ i ] = pair [ i - 1 ] // arr [ 0 ] ; NEW_LINE DEDENT printArr ( arr , size ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT pair = [ 48 , 18 , 24 , 24 , 32 , 12 ] ; NEW_LINE n = len ( pair ) ; NEW_LINE constructArr ( pair , n ) ; NEW_LINE DEDENT
Count of odd and even sum pairs in an array | Function to find the count of pairs with odd sum and the count of pairs with even sum ; To store the count of even and odd number from the array ; If the current element is even ; If it is odd ; To store the count of pairs with even sum ; All the even elements will make pairs with each other and the sum of the pair will be even ; All the odd elements will make pairs with each other and the sum of the pair will be even ; To store the count of pairs with odd sum ; All the even elements will make pairs with all the odd element and the sum of the pair will be odd ; Driver code
def findPairs ( arr , n ) : NEW_LINE INDENT cntEven = 0 ; cntOdd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT cntEven += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT cntOdd += 1 ; NEW_LINE DEDENT DEDENT evenPairs = 0 ; NEW_LINE evenPairs += ( ( cntEven * ( cntEven - 1 ) ) // 2 ) ; NEW_LINE evenPairs += ( ( cntOdd * ( cntOdd - 1 ) ) // 2 ) ; NEW_LINE oddPairs = 0 ; NEW_LINE oddPairs += ( cntEven * cntOdd ) ; NEW_LINE print ( " Odd ▁ pairs ▁ = ▁ " , oddPairs ) ; NEW_LINE print ( " Even ▁ pairs ▁ = ▁ " , evenPairs ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findPairs ( arr , n ) ; NEW_LINE DEDENT
Number of ways to distribute N Paper Set among M students | Python3 implementation of the approach ; Function to return n ! % 1000000007 ; To store the factorial ; Find the factorial ; Function to return the count of possible ways ; Driver code
MOD = 1000000007 ; NEW_LINE def factMod ( n ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact *= ( i % MOD ) ; NEW_LINE fact %= MOD ; NEW_LINE DEDENT return fact ; NEW_LINE DEDENT def countWays ( n , m ) : NEW_LINE INDENT return factMod ( m ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; m = 2 ; NEW_LINE print ( countWays ( n , m ) ) ; NEW_LINE DEDENT
Program for nth Fuss – Catalan Number | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth FussCatalan number in O ( n ) time ; Calculate value of 3 nCn ; return 3 nCn / ( 2 n + 1 ) ; Driver code
def binomialCoeff ( n , k ) : NEW_LINE INDENT res = 1 ; NEW_LINE if ( k > n - k ) : NEW_LINE INDENT k = n - k ; NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT res *= ( n - i ) ; NEW_LINE res //= ( i + 1 ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def Fuss_catalan ( n ) : NEW_LINE INDENT c = binomialCoeff ( 3 * n , n ) ; NEW_LINE return c // ( 2 * n + 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT print ( Fuss_catalan ( i ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT
Check if a number is Euler Pseudoprime | Python3 program for nth FussCatalan Number ; Function that returns true if n is composite ; Check if there is any divisor of n . we only need check divisor till sqrt ( n ) because if there is divisor which is greater than sqrt ( n ) then there must be a divisor which is less than sqrt ( n ) ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is greater than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function that returns true if N is Euler Pseudoprime to the base A ; Invalid base ; N is not a composite odd number ; If A and N are not coprime ; All the conditions for Euler Pseudoprime are satisfied ; Driver code
from math import gcd , sqrt NEW_LINE def isComposite ( n ) : NEW_LINE INDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT def Power ( x , y , p ) : NEW_LINE INDENT res = 1 ; NEW_LINE x = x % p ; NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT x = ( x * x ) % p ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def isEulerPseudoprime ( N , A ) : NEW_LINE INDENT if ( A <= 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( N % 2 == 0 or not isComposite ( N ) ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( gcd ( A , N ) != 1 ) : NEW_LINE INDENT return false ; NEW_LINE DEDENT mod = Power ( A , ( N - 1 ) // 2 , N ) ; NEW_LINE if ( mod != 1 and mod != N - 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 121 ; A = 3 ; NEW_LINE if ( isEulerPseudoprime ( N , A ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT