codeparrot/xlcost-text-to-code huggingface dataset:
Check if a right | Python3 program to implement the above approach ; Function to check if N is a perfect square number or not ; If N is a non positive integer ; Stores square root of N ; Check for perfect square ; If N is not a perfect square number ; Function to check if given two sides of a triangle forms a right - angled triangle ; If the value of ( A * A + B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( A * A - B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( B * B - A * A ) is a perfect square number ; Update checkTriangle ; Driver Code ; If the given two sides of a triangle forms a right - angled triangle ; Otherwise
from math import sqrt , floor , ceil NEW_LINE def checkPerfectSquare ( N ) : NEW_LINE INDENT if ( N <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT sq = sqrt ( N ) NEW_LINE if ( floor ( sq ) == ceil ( sq ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def checktwoSidesareRighTriangle ( A , B ) : NEW_LINE INDENT checkTriangle = False NEW_LINE if ( checkPerfectSquare ( A * A + B * B ) ) : NEW_LINE INDENT checkTriangle = True NEW_LINE DEDENT if ( checkPerfectSquare ( A * A - B * B ) ) : NEW_LINE INDENT checkTriangle = True NEW_LINE DEDENT if ( checkPerfectSquare ( B * B - A * A ) ) : NEW_LINE INDENT checkTriangle = True NEW_LINE DEDENT return checkTriangle NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 3 NEW_LINE B = 4 NEW_LINE if ( checktwoSidesareRighTriangle ( A , B ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimize increments or decrements required to make sum and product of array elements non | Function to find the sum of array ; Return the sum ; Function that counts the minimum operations required to make the sum and product of array non - zero ; Stores count of zero elements ; Iterate over the array to count zero elements ; Sum of elements of the array ; Print result ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
def array_sum ( 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 countOperations ( arr , N ) : NEW_LINE INDENT count_zeros = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count_zeros += 1 NEW_LINE DEDENT DEDENT sum = array_sum ( arr , N ) NEW_LINE if ( count_zeros ) : NEW_LINE INDENT return count_zeros NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 1 , - 1 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countOperations ( arr , N ) ) NEW_LINE DEDENT
Bitwise OR of all unordered pairs from a given array | Function to find the bitwise OR of all possible pairs of the array ; Stores bitwise OR of all possible pairs of arr ; Traverse the array arr ; Update totalOR ; Return bitwise OR of all possible pairs of arr ; Driver Code
def TotalBitwiseORPair ( arr , N ) : NEW_LINE INDENT totalOR = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT totalOR |= arr [ i ] ; NEW_LINE DEDENT return totalOR ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 5 , 12 , 15 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( TotalBitwiseORPair ( arr , N ) ) ; NEW_LINE DEDENT
Difference between lexicographical ranks of two given permutations | Function the print the difference between the lexicographical ranks ; Store the permutations in lexicographic order ; Initial permutation ; Initial variables ; Check permutation ; Initialize second permutation ; Check permutation ; Print difference ; Given array P [ ] ; Given array Q [ ] ; Given size ; Function call
def findDifference ( p , q , N ) : NEW_LINE INDENT A = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT A [ i ] = i + 1 NEW_LINE DEDENT IsCorrect = False NEW_LINE a , b = 1 , 1 NEW_LINE while True : NEW_LINE INDENT IsCorrect = True NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] != p [ i ] ) : NEW_LINE INDENT IsCorrect = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( IsCorrect ) : NEW_LINE INDENT break NEW_LINE DEDENT a += 1 NEW_LINE if ( not next_permutation ( A ) ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT A [ i ] = i + 1 NEW_LINE DEDENT while True : NEW_LINE INDENT IsCorrect = True NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] != q [ i ] ) : NEW_LINE INDENT IsCorrect = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( IsCorrect ) : NEW_LINE INDENT break NEW_LINE DEDENT b += 1 NEW_LINE if ( not next_permutation ( A ) ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( abs ( a - b ) ) NEW_LINE DEDENT def next_permutation ( p ) : NEW_LINE INDENT for a in range ( len ( p ) - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( p [ a ] < p [ a + 1 ] ) : NEW_LINE INDENT b = len ( p ) - 1 NEW_LINE while True : NEW_LINE INDENT if ( p [ b ] > p [ a ] ) : NEW_LINE INDENT t = p [ a ] NEW_LINE p [ a ] = p [ b ] NEW_LINE p [ b ] = t NEW_LINE a += 1 NEW_LINE b = len ( p ) - 1 NEW_LINE while a < b : NEW_LINE INDENT t = p [ a ] NEW_LINE p [ a ] = p [ b ] NEW_LINE p [ b ] = t NEW_LINE a += 1 NEW_LINE b -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT b -= 1 NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT p = [ 1 , 3 , 2 ] NEW_LINE q = [ 3 , 1 , 2 ] NEW_LINE n = len ( p ) NEW_LINE findDifference ( p , q , n ) NEW_LINE
Count of packets placed in each box after performing given operations | Function to print final array after performing all the operations ; Initialize variables ; Traverse through all operations ; Operation Type ; Move left ; Move right ; Pick a packet ; Drop a packet ; Exit ; Print final array ; Driver Code ; Given capacity ; Given array with initial values ; Array size ; Operations ; Number of operations ; Function call
def printFinalArray ( a , n , operations , p , capacity ) : NEW_LINE INDENT curr = 0 NEW_LINE picked = False NEW_LINE for i in range ( p ) : NEW_LINE INDENT s = operations [ i ] NEW_LINE flag = False NEW_LINE if ( curr != 0 ) : NEW_LINE INDENT curr -= 1 NEW_LINE break NEW_LINE DEDENT if ( curr != n - 1 ) : NEW_LINE INDENT curr += 1 NEW_LINE break NEW_LINE DEDENT if ( picked == False and a [ curr ] != 0 ) : NEW_LINE INDENT picked = True NEW_LINE a [ curr ] -= 1 NEW_LINE break NEW_LINE DEDENT if ( picked == True and a [ curr ] != capacity ) : NEW_LINE INDENT picked = False NEW_LINE a [ curr ] += 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT flag = True NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT capacity = 5 NEW_LINE a = [ 2 , 5 , 2 ] NEW_LINE N = len ( a ) NEW_LINE operations = [ 3 , 2 , 4 , 1 , 4 , 5 ] NEW_LINE M = len ( operations ) NEW_LINE printFinalArray ( a , N , operations , M , capacity ) NEW_LINE DEDENT
Check if sum of count of digits of array elements is Prime or not | Python3 program for the above approach ; Function to check whether a number is prime or not ; Corner cases ; If given number is a multiple of 2 or 3 ; Function to check if sum of count of digits of all array elements is prime or not ; Initialize sum with 0 ; Traverse over the array ; Convert array element to string ; Add the count of digits to sum ; Print the result ; Drive Code ; Function call
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( math . sqrt ( n ) + 1 ) , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def CheckSumPrime ( A , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT s = str ( A [ i ] ) NEW_LINE sum += len ( s ) NEW_LINE DEDENT if ( isPrime ( sum ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 11 , 12 ] NEW_LINE N = len ( A ) NEW_LINE CheckSumPrime ( A , N ) NEW_LINE DEDENT
Probability of obtaining Prime Numbers as product of values obtained by throwing N dices | Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the probability of obtaining a prime number as the product of N thrown dices ; Stores count of favorable outcomes ; Stores count of sample space ; Prthe required probability ; Driver code
def power ( x , N ) : NEW_LINE INDENT res = 1 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N % 2 == 1 ) : NEW_LINE INDENT res = ( res * x ) NEW_LINE DEDENT x = ( x * x ) NEW_LINE N = N >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def probablityPrimeprod ( N ) : NEW_LINE INDENT N_E = 3 * N NEW_LINE N_S = power ( 6 , N ) NEW_LINE print ( N_E , " ▁ / ▁ " , N_S ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE probablityPrimeprod ( N ) NEW_LINE DEDENT
Smaller palindromic number closest to N | Function to check if a number is palindrome or not ; Stores reverse of N ; Stores the value of N ; Calculate reverse of N ; Update rev ; Update N ; Update N ; If N is equal to rev of N ; Function to find the closest smaller palindromic number to N ; Calculate closest smaller palindromic number to N ; Update N ; Driver Code
def checkPalindrome ( N ) : NEW_LINE INDENT rev = 0 NEW_LINE temp = N NEW_LINE while ( N != 0 ) : NEW_LINE INDENT rev = rev * 10 + N % 10 NEW_LINE N = N // 10 NEW_LINE DEDENT N = temp NEW_LINE if ( N == rev ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def closestSmallerPalindrome ( N ) : NEW_LINE INDENT while N >= 0 and not checkPalindrome ( N ) : NEW_LINE INDENT N -= 1 NEW_LINE DEDENT return N NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4000 NEW_LINE print ( closestSmallerPalindrome ( N ) ) NEW_LINE DEDENT
Minimize increments or decrements by 2 to convert given value to a perfect square | Function to find the minimum number of operations required to make N a perfect square ; Stores count of operations by performing decrements ; Stores value of N ; Decrement the value of temp ; Stores square root of temp ; If temp is a perfect square ; Update temp ; Store count of operations by performing increments ; Increment the value of N ; Stores sqrt of N ; If N is a perfect square ; Update temp ; Return the minimum count ; Driver code
def MinimumOperationReq ( N ) : NEW_LINE INDENT cntDecr = 0 ; NEW_LINE temp = N ; NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT X = int ( pow ( temp , 1 / 2 ) ) NEW_LINE if ( X * X == temp ) : NEW_LINE INDENT break ; NEW_LINE DEDENT temp = temp - 2 ; NEW_LINE cntDecr += 1 ; NEW_LINE DEDENT cntIncr = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT X = int ( pow ( N , 1 / 2 ) ) NEW_LINE if ( X * X == N ) : NEW_LINE INDENT break ; NEW_LINE DEDENT N = N + 2 ; NEW_LINE cntIncr += 1 ; NEW_LINE DEDENT return min ( cntIncr , cntDecr ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 ; NEW_LINE print ( MinimumOperationReq ( N ) ) ; NEW_LINE DEDENT
First term from given Nth term of the equation F ( N ) = ( 2 * F ( N | Python3 program to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the value of F ( 1 ) ; Stores power ( 2 , N - 1 ) ; Stores modulo multiplicative inverse of P_2 under modulo M ; Stores the value of F ( 1 ) ; Update res ; Driver code
M = 1000000007 NEW_LINE def power ( x , N ) : NEW_LINE INDENT res = 1 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT res = ( res * x ) % M NEW_LINE DEDENT x = ( x * x ) % M NEW_LINE N = N >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def moduloInverse ( X ) : NEW_LINE INDENT return power ( X , M - 2 ) NEW_LINE DEDENT def F_1 ( N , F_N ) : NEW_LINE INDENT P_2 = power ( 2 , N - 1 ) NEW_LINE modInv = moduloInverse ( P_2 ) NEW_LINE res = 0 NEW_LINE res = ( ( modInv % M ) * ( F_N % M ) ) % M NEW_LINE return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE F_N = 6 NEW_LINE print ( F_1 ( N , F_N ) ) NEW_LINE DEDENT
Queries to replace every array element by its XOR with a given value with updates | Function to insert an element into the array ; Function to update every array element a [ i ] by a [ i ] ^ x ; Function to compute the final results after the operations ; Driver Code ; Queries ; Function call
def add ( arr , x ) : NEW_LINE INDENT arr . append ( x ) NEW_LINE DEDENT def update ( effect , x ) : NEW_LINE INDENT effect = effect ^ x NEW_LINE return effect NEW_LINE DEDENT def computeResults ( arr , effect ) : NEW_LINE INDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT arr [ i ] = arr [ i ] ^ effect NEW_LINE print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 ] NEW_LINE effect = 0 NEW_LINE add ( arr , 5 ) NEW_LINE effect = update ( effect , 2 ) NEW_LINE computeResults ( arr , effect ) NEW_LINE DEDENT
Check if a subarray of size K exists whose elements form a number divisible by 3 | Function to find the K size subarray ; Check if the first K elements forms a number which is divisible by 3 ; Using Sliding window technique ; Calculate sum of next K size subarray ; Check if sum is divisible by 3 ; Update the indices of the subarray ; If no such subarray is found ; Print the subarray ; Driver code ; Given array and K ; Function call
def findSubArray ( arr , k ) : NEW_LINE INDENT ans = [ ( 0 , 0 ) ] NEW_LINE sm = 0 NEW_LINE i = 0 NEW_LINE found = 0 NEW_LINE while ( i < k ) : NEW_LINE INDENT sm += arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT if ( sm % 3 == 0 ) : NEW_LINE INDENT ans = [ ( 0 , i - 1 ) ] NEW_LINE found = 1 NEW_LINE DEDENT for j in range ( i , len ( arr ) , 1 ) : NEW_LINE INDENT if ( found == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT sm = sm + arr [ j ] - arr [ j - k ] NEW_LINE if ( sm % 3 == 0 ) : NEW_LINE INDENT ans = [ ( j - k + 1 , j ) ] NEW_LINE found = 1 NEW_LINE DEDENT DEDENT if ( found == 0 ) : NEW_LINE INDENT ans = [ ( - 1 , 0 ) ] NEW_LINE DEDENT if ( ans [ 0 ] [ 0 ] == - 1 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( ans [ 0 ] [ 0 ] , ans [ 0 ] [ 1 ] + 1 , 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 84 , 23 , 45 , 12 , 56 , 82 ] NEW_LINE K = 3 NEW_LINE findSubArray ( arr , K ) NEW_LINE DEDENT
Count permutations of given array that generates the same Binary Search Tree ( BST ) | Function to precompute the factorial of 1 to N ; Function to get the value of nCr ; nCr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; Function to count the number of ways to rearrange the array to obtain same BST ; Store the size of the array ; Base case ; Store the elements of the left subtree of BST ; Store the elements of the right subtree of BST ; Store the root node ; Push all the elements of the left subtree ; Push all the elements of the right subtree ; Store the size of leftSubTree ; Store the size of rightSubTree ; Recurrence relation ; Driver Code ; Store the size of arr ; Store the factorial up to N ; Precompute the factorial up to N
def calculateFact ( fact : list , N : int ) -> None : NEW_LINE INDENT fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE DEDENT DEDENT def nCr ( fact : list , N : int , R : int ) -> int : NEW_LINE INDENT if ( R > N ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = fact [ N ] // fact [ R ] NEW_LINE res //= fact [ N - R ] NEW_LINE return res NEW_LINE DEDENT def countWays ( arr : list , fact : list ) -> int : NEW_LINE INDENT N = len ( arr ) NEW_LINE if ( N <= 2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT leftSubTree = [ ] NEW_LINE rightSubTree = [ ] NEW_LINE root = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] < root ) : NEW_LINE INDENT leftSubTree . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT rightSubTree . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT N1 = len ( leftSubTree ) NEW_LINE N2 = len ( rightSubTree ) NEW_LINE countLeft = countWays ( leftSubTree , fact ) NEW_LINE countRight = countWays ( rightSubTree , fact ) NEW_LINE return ( nCr ( fact , N - 1 , N1 ) * countLeft * countRight ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 5 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE fact = [ 0 ] * N NEW_LINE calculateFact ( fact , N ) NEW_LINE print ( countWays ( arr , fact ) ) NEW_LINE DEDENT
Sum of all ordered pair | Function to calculate the sum of all pair - products ; Stores sum of array ; Update sum of the array ; Driver Code
def sumOfProd ( 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 * sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 1 , 5 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( sumOfProd ( arr , N ) ) NEW_LINE DEDENT
Check if a given number is one less than twice its reverse | Iterative function to reverse digits of num ; Loop to extract all digits of the number ; Function to check if N satisfies given equation ; Driver Code
def rev ( num ) : NEW_LINE INDENT rev_num = 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT rev_num = ( rev_num * 10 + num % 10 ) NEW_LINE num = num // 10 NEW_LINE DEDENT return rev_num NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT return ( 2 * rev ( n ) == n + 1 ) NEW_LINE DEDENT n = 73 NEW_LINE if ( check ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if a given number is one less than twice its reverse | Python3 program to implement the above approach ; Function to check y is a power of x ; logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Function to check if N satisfies the equation 2 * reverse ( n ) = n + 1 ; Driver Code
import math NEW_LINE def isPower ( x , y ) : NEW_LINE INDENT res1 = math . log ( y ) // math . log ( x ) NEW_LINE res2 = math . log ( y ) // math . log ( x ) NEW_LINE return ( res1 == res2 ) NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT x = ( n + 7 ) // 8 NEW_LINE if ( ( n + 7 ) % 8 == 0 and isPower ( 10 , x ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 73 NEW_LINE if ( check ( n ) != 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Nth term of a recurrence relation generated by two given arrays | Python3 program for the above approach ; Function to calculate Nth term of general recurrence relations ; Stores the generated sequence ; Current term is sum of previous k terms ; Print the nth term ; Driver code ; Given Array F [ ] and C [ ] ; Given N and K ; Function call
mod = 1e9 + 7 NEW_LINE def NthTerm ( F , C , K , n ) : NEW_LINE INDENT ans = [ 0 ] * ( n + 1 ) NEW_LINE i = 0 NEW_LINE while i < K : NEW_LINE INDENT ans [ i ] = F [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i = K NEW_LINE while i <= n : NEW_LINE INDENT j = i - K NEW_LINE while j < i : NEW_LINE INDENT ans [ i ] += ans [ j ] NEW_LINE ans [ i ] %= mod NEW_LINE j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT print ( int ( ans [ n ] ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT F = [ 0 , 1 ] NEW_LINE C = [ 1 , 1 ] NEW_LINE K = 2 NEW_LINE N = 10 NEW_LINE NthTerm ( F , C , K , N ) NEW_LINE DEDENT
Mode of frequencies of given array elements | Python3 program of the above approach ; Function to find the mode of the frequency of the array ; Stores the frequencies of array elements ; Traverse through array elements and count frequencies ; Stores the frequencies 's of  frequencies of array elements ; Stores the minimum value ; Find the Mode in 2 nd map ; search for this Mode ; When mode is find then return to main function . ; If mode is not found ; Driver Code
from collections import defaultdict NEW_LINE import sys NEW_LINE def countFreq ( arr , n ) : NEW_LINE INDENT mp1 = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp1 [ arr [ i ] ] += 1 NEW_LINE mp2 = defaultdict ( int ) NEW_LINE for it in mp1 : NEW_LINE mp2 [ mp1 [ it ] ] += 1 NEW_LINE M = - sys . maxsize - 1 NEW_LINE for it in mp2 : NEW_LINE INDENT M = max ( M , mp2 [ it ] ) NEW_LINE for it in mp2 : NEW_LINE if ( M == mp2 [ it ] ) : NEW_LINE INDENT return it NEW_LINE DEDENT return 0 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countFreq ( arr , n ) ) NEW_LINE DEDENT
Maximize count of nodes disconnected from all other nodes in a Graph | Function which returns the maximum number of isolated nodes ; Used nodes ; Remaining edges ; Count nodes used ; If given edges are non - zero ; Driver Code ; Given N and E ; Function call
def maxDisconnected ( N , E ) : NEW_LINE INDENT curr = 1 NEW_LINE rem = E NEW_LINE while ( rem > 0 ) : NEW_LINE INDENT rem = rem - min ( curr , rem ) NEW_LINE curr += 1 NEW_LINE DEDENT if ( curr > 1 ) : NEW_LINE INDENT return N - curr NEW_LINE DEDENT else : NEW_LINE INDENT return N NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE E = 1 NEW_LINE print ( maxDisconnected ( N , E ) ) NEW_LINE DEDENT
Check if number is palindrome or not in base B | Function to check if N in base B is palindrome or not ; Stores the reverse of N ; Stores the value of N ; Extract all the digits of N ; Generate its reverse ; Driver code
def checkPalindromeB ( N , B ) : NEW_LINE INDENT rev = 0 ; NEW_LINE N1 = N ; NEW_LINE while ( N1 > 0 ) : NEW_LINE INDENT rev = rev * B + N1 % B ; NEW_LINE N1 = N1 // B ; NEW_LINE DEDENT return N == rev ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; B = 2 ; NEW_LINE if ( checkPalindromeB ( N , B ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Product of all numbers up to N that are co | Function to return gcd of a and b ; Base Case ; Recursive GCD ; Function to find the product of all the numbers till N that are relatively prime to N ; Stores the resultant product ; Iterate over [ 2 , N ] ; If gcd is 1 , then find the product with result ; Return the final product ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def findProduct ( N ) : NEW_LINE INDENT result = 1 ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( gcd ( i , N ) == 1 ) : NEW_LINE INDENT result *= i ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE print ( findProduct ( N ) ) ; NEW_LINE DEDENT
Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum | Function to count maximum number of array elements equal ; Driver Code ; Function call
def countEqual ( n ) : NEW_LINE INDENT return ( n + 1 ) // 2 NEW_LINE DEDENT lst = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( lst ) NEW_LINE print ( countEqual ( n ) ) NEW_LINE