codeparrot/xlcost-text-to-code huggingface dataset:
Coordinates of the last cell in a Matrix on which performing given operations exits from the Matrix | Function to check if the indices ( i , j ) are valid indices in a Matrix or not ; Cases for invalid cells ; Return true if valid ; Function to find indices of cells of a matrix from which traversal leads to out of the matrix ; Starting from cell ( 0 , 0 ) , traverse in right direction ; Stores direction changes ; Iterate until the current cell exceeds beyond the matrix ; Current index ; If the current cell is 1 ; Update arr [ i ] [ j ] = 0 ; Update indices according to the direction ; Otherwise ; Update indices according to the direction ; The exit cooridnates ; Number of rows ; Number of columns ; Given matrix arr [ ] [ ]
def issafe ( m , n , i , j ) : NEW_LINE INDENT if i < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT if j < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT if i >= m : NEW_LINE INDENT return False NEW_LINE DEDENT if j >= n : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def endpoints ( arr , m , n ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE current_d = ' r ' NEW_LINE rcd = { ' l ' : ' u ' , ' u ' : ' r ' , ' r ' : ' d ' , ' d ' : ' l ' } NEW_LINE while issafe ( m , n , i , j ) : NEW_LINE INDENT current_i = i NEW_LINE current_j = j NEW_LINE if arr [ i ] [ j ] == 1 : NEW_LINE INDENT move_in = rcd [ current_d ] NEW_LINE arr [ i ] [ j ] = 0 NEW_LINE if move_in == ' u ' : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT elif move_in == ' d ' : NEW_LINE INDENT i += 1 NEW_LINE DEDENT elif move_in == ' l ' : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT elif move_in == ' r ' : NEW_LINE INDENT j += 1 NEW_LINE DEDENT current_d = move_in NEW_LINE DEDENT else : NEW_LINE INDENT if current_d == ' u ' : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT elif current_d == ' d ' : NEW_LINE INDENT i += 1 NEW_LINE DEDENT elif current_d == ' l ' : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT elif current_d == ' r ' : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT return ( current_i , current_j ) NEW_LINE DEDENT M = 3 NEW_LINE N = 5 NEW_LINE arr = [ [ 0 , 1 , 1 , 1 , 0 ] , [ 1 , 0 , 1 , 0 , 1 ] , [ 1 , 1 , 1 , 0 , 0 ] , ] NEW_LINE print ( endpoints ( arr , M , N ) ) NEW_LINE
Check if a number can be represented as sum of two positive perfect cubes | Function to check if N can be represented as sum of two perfect cubes or not ; Stores the perfect cubes of first N natural numbers ; Traverse the map ; Stores first number ; Stores second number ; Search the pair for the first number to obtain sum N from the Map ; If N cannot be represented as sum of two positive perfect cubes ; Function call to check if N can be represented as sum of two perfect cubes or not
def sumOfTwoPerfectCubes ( N ) : NEW_LINE INDENT cubes = { } NEW_LINE i = 1 NEW_LINE while i * i * i <= N : NEW_LINE INDENT cubes [ i * i * i ] = i NEW_LINE i += 1 NEW_LINE DEDENT for itr in cubes : NEW_LINE INDENT firstNumber = itr NEW_LINE secondNumber = N - itr NEW_LINE if secondNumber in cubes : NEW_LINE INDENT print ( " True " , end = " " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " False " , end = " " ) NEW_LINE DEDENT N = 28 NEW_LINE sumOfTwoPerfectCubes ( N ) NEW_LINE
Count subsets consisting of each element as a factor of the next element in that subset | Function to find number of subsets satisfying the given condition ; Stores number of required sets ; Stores maximum element of arr [ ] that defines the size of sieve ; Iterate through the arr [ ] ; If current element > maxE , then update maxE ; Declare an array sieve of size N + 1 ; Mark all elements corresponding in the array , by one as there will always exists a singleton set ; Iterate from range [ 1 , N ] ; If element is present in array ; Traverse through all its multiples <= n ; Update them if they are present in array ; Iterate from the range [ 1 , N ] ; Update the value of cnt ; Prthe result ; Driver Code ; Function Call
def countSets ( arr , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE maxE = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( maxE < arr [ i ] ) : NEW_LINE INDENT maxE = arr [ i ] NEW_LINE DEDENT DEDENT sieve = [ 0 ] * ( maxE + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT sieve [ arr [ i ] ] = 1 NEW_LINE DEDENT for i in range ( 1 , maxE + 1 ) : NEW_LINE INDENT if ( sieve [ i ] != 0 ) : NEW_LINE INDENT for j in range ( i * 2 , maxE + 1 , i ) : NEW_LINE INDENT if ( sieve [ j ] != 0 ) : NEW_LINE INDENT sieve [ j ] = ( sieve [ j ] + sieve [ i ] ) % 1000000007 NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( maxE + 1 ) : NEW_LINE INDENT cnt = ( cnt % 1000000007 + sieve [ i ] % 1000000007 ) % 1000000007 NEW_LINE DEDENT print ( cnt % 1000000007 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 16 , 18 , 6 , 7 , 2 , 19 , 20 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE countSets ( arr , N ) NEW_LINE DEDENT
Find the first day of a given year from a base year having first day as Monday | Function to find the day of 1 st January of Y year ; Count years between years Y and B ; Count leap years ; Non leap years ; Total number of days in the years lying between the years Y and B ; Actual day ; Driver code
def findDay ( Y , B ) : NEW_LINE INDENT lyear , rest , totaldays , day = 0 , 0 , 0 , 0 ; NEW_LINE Y = ( Y - 1 ) - B ; NEW_LINE lyear = Y // 4 ; NEW_LINE rest = Y - lyear ; NEW_LINE totaldays = ( rest * 365 ) + ( lyear * 366 ) + 1 ; NEW_LINE day = ( totaldays % 7 ) ; NEW_LINE if ( day == 0 ) : NEW_LINE INDENT print ( " Monday " ) ; NEW_LINE DEDENT elif ( day == 1 ) : NEW_LINE INDENT print ( " Tuesday " ) ; NEW_LINE DEDENT elif ( day == 2 ) : NEW_LINE INDENT print ( " Wednesday " ) ; NEW_LINE DEDENT elif ( day == 3 ) : NEW_LINE INDENT print ( " Thursday " ) ; NEW_LINE DEDENT elif ( day == 4 ) : NEW_LINE INDENT print ( " Friday " ) ; NEW_LINE DEDENT elif ( day == 5 ) : NEW_LINE INDENT print ( " Saturday " ) ; NEW_LINE DEDENT elif ( day == 6 ) : NEW_LINE INDENT print ( " Sunday " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " INPUT ▁ YEAR ▁ IS ▁ WRONG ! " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Y = 2020 ; B = 1900 ; NEW_LINE findDay ( Y , B ) ; NEW_LINE DEDENT
Queries to update array elements in a range [ L , R ] to satisfy given conditions | Function to prthe array ; Function to perform the query in range [ L , R ] such that arr [ i ] += i - L + 1 ; Initialize array ; Traverse the query array ; Stores range in 1 - based index ; Update arr1 [ L ] ; Update arr1 [ R + 1 ] ; Update arr2 [ R + 1 ] ; Calculate prefix sum ; Traverse the array , arr2 [ ] ; Copy arr2 [ ] into arr [ ] ; Driver Code ; Given array ; Size of the array ; Stores count of query ; Function Call
def printArray ( arr , N ) : NEW_LINE INDENT print ( * arr ) NEW_LINE DEDENT def modifyArray ( arr , N , Q , cntQuery ) : NEW_LINE INDENT arr1 = [ 0 for i in range ( N + 2 ) ] NEW_LINE arr2 = [ 0 for i in range ( N + 2 ) ] NEW_LINE for i in range ( cntQuery ) : NEW_LINE INDENT L = Q [ i ] [ 0 ] + 1 NEW_LINE R = Q [ i ] [ 1 ] + 1 NEW_LINE arr1 [ L ] += 1 NEW_LINE arr1 [ R + 1 ] -= 1 NEW_LINE arr2 [ R + 1 ] -= R - L + 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT arr1 [ i ] += arr1 [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT arr2 [ i ] += arr2 [ i - 1 ] + arr1 [ i ] NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT arr [ i - 1 ] = arr2 [ i ] NEW_LINE DEDENT printArray ( arr , N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE Q = [ [ 1 , 3 ] , [ 0 , 1 ] ] NEW_LINE cntQuery = len ( Q ) NEW_LINE modifyArray ( arr , N , Q , cntQuery ) NEW_LINE DEDENT
Maximize product obtained by taking one element from each array of a given list | Function to return the product of 2 numbers ; If any of the two numbers is None ; Otherwise , return the product ; Function to calculate maximum product by taking only one element from each array present in the list ; Find the maximum and minimum present in the current array ; If last index is reached , then return the highest ( positive ) and lowest ( negative ) values ; Store the positive and negative products returned by calculating for the remaining arrays ; Store highest positive product ; Store product of highest with negative ; Store product of lowest with positive ; Store lowest negative product ; Return the maximum positive and minimum negative product ; Count of given arrays ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product
def findProduct ( number_1 , number_2 ) : NEW_LINE INDENT if ( number_1 == None or number_2 == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return number_1 * number_2 NEW_LINE DEDENT DEDENT def calculateProduct ( List , index ) : NEW_LINE INDENT highest = max ( List [ index ] ) NEW_LINE lowest = min ( List [ index ] ) NEW_LINE if ( index + 1 == len ( List ) ) : NEW_LINE INDENT if ( lowest < 0 and highest >= 0 ) : NEW_LINE INDENT return [ highest , lowest ] NEW_LINE DEDENT elif ( lowest <= 0 and highest <= 0 ) : NEW_LINE INDENT return [ None , lowest ] NEW_LINE DEDENT elif ( lowest >= 0 and highest >= 0 ) : NEW_LINE INDENT return [ highest , None ] NEW_LINE DEDENT DEDENT [ positive , negative ] = calculateProduct ( List , index + 1 ) NEW_LINE highPos = findProduct ( highest , positive ) NEW_LINE highNeg = findProduct ( highest , negative ) NEW_LINE lowPos = findProduct ( lowest , positive ) NEW_LINE lowNeg = findProduct ( lowest , negative ) NEW_LINE if ( lowest < 0 and highest >= 0 ) : NEW_LINE INDENT return [ max ( highPos , lowNeg ) , min ( highNeg , lowPos ) ] NEW_LINE DEDENT elif ( lowest <= 0 and highest <= 0 ) : NEW_LINE INDENT return [ lowNeg , lowPos ] NEW_LINE DEDENT elif ( lowest >= 0 and highest >= 0 ) : NEW_LINE INDENT return [ max ( lowPos , highPos ) , min ( lowNeg , highNeg ) ] NEW_LINE DEDENT DEDENT N = 2 NEW_LINE arr = [ [ - 3 , - 4 ] , [ 1 , 2 , - 3 ] ] NEW_LINE positive , negative = calculateProduct ( arr , 0 ) NEW_LINE print ( positive ) NEW_LINE
Reduce a number N by at most D to maximize count of trailing nines | Python3 program for the above approach ; Function to find a number with maximum count of trailing nine ; Stores count of digits in n ; Stores power of 10 ; If last i digits greater than or equal to d ; Update res ; Update p10 ; Driver Code ; Function Call
from math import log10 NEW_LINE def maxNumTrailNine ( n , d ) : NEW_LINE INDENT res = n NEW_LINE cntDigits = int ( log10 ( n ) + 1 ) NEW_LINE p10 = 10 NEW_LINE for i in range ( 1 , cntDigits + 1 ) : NEW_LINE INDENT if ( n % p10 >= d ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT res = n - n % p10 - 1 NEW_LINE DEDENT p10 = p10 * 10 NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , d = 1025 , 6 NEW_LINE maxNumTrailNine ( n , d ) NEW_LINE DEDENT
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once | Function to find minimum count of ways to split the array into two subset such that elements of each pair occurs in two different subset ; Stores minimum count of ways to split array into two subset such that elements of each pair occurs in two different subset ; if n is odd ; driver code
def MinimumNoOfWays ( arr , n ) : NEW_LINE INDENT min_no_of_ways = 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT mini_no_of_ways = n // 2 NEW_LINE DEDENT else : NEW_LINE INDENT mini_no_of_ways = n // 2 + 1 NEW_LINE DEDENT return mini_no_of_ways NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 1 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( MinimumNoOfWays ( arr , n ) ) NEW_LINE DEDENT
Chocolate Distribution Problem | Set 2 | Function to return minimum number of chocolates ; decreasing sequence ; add the chocolates received by that person ; end point of decreasing sequence ; val = 1 reset value at that index ; increasing sequence ; flat sequence ; add value of chocolates at position n - 1 ; Helper function to get sum of decreasing sequence ; value obtained from decreasing sequence also the count of values in the sequence ; assigning max of values obtained from increasing and decreasing sequences ; sum of count - 1 values & peak value sum of natural numbers : ( n * ( n + 1 ) ) / 2 ; Driver code
def minChocolates ( a , n ) : NEW_LINE INDENT i , j = 0 , 0 NEW_LINE val , res = 1 , 0 NEW_LINE while ( j < n - 1 ) : NEW_LINE INDENT if ( a [ j ] > a [ j + 1 ] ) : NEW_LINE INDENT j += 1 NEW_LINE continue NEW_LINE DEDENT if ( i == j ) : NEW_LINE INDENT res += val NEW_LINE DEDENT else : NEW_LINE INDENT res += get_sum ( val , i , j ) NEW_LINE DEDENT if ( a [ j ] < a [ j + 1 ] ) : NEW_LINE INDENT val += 1 NEW_LINE DEDENT else : NEW_LINE INDENT val = 1 NEW_LINE DEDENT j += 1 NEW_LINE i = j NEW_LINE DEDENT if ( i == j ) : NEW_LINE INDENT res += val NEW_LINE DEDENT else : NEW_LINE INDENT res += get_sum ( val , i , j ) NEW_LINE DEDENT return res NEW_LINE DEDENT def get_sum ( peak , start , end ) : NEW_LINE INDENT count = end - start + 1 NEW_LINE peak = max ( peak , count ) NEW_LINE s = peak + ( ( ( count - 1 ) * count ) >> 1 ) NEW_LINE return s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 5 , 5 , 4 , 3 , 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( ' Minimum ▁ number ▁ of ▁ chocolates ▁ = ' , minChocolates ( a , n ) ) NEW_LINE DEDENT
Remove array elements to reduce frequency of each array element to at most K | Function to remove array elements such that frequency of each distinct array element is at most K ; Base Case ; Stores index of array element by removing the array element such that the frequency of array elements is at most K ; Traverse the array ; If j < k or arr [ i ] > arr [ j - k ] ; Update arr [ j ] ; Remove array elements ; Function to print the array ; Traverse the array ; Utility Function to remove array elements such that frequency of each distinct array element is at most K ; Print updated array ; Driver Code
def RemoveElemArr ( arr , n , k ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return arr NEW_LINE DEDENT j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( j < k or arr [ i ] > arr [ j - k ] ) : NEW_LINE INDENT arr [ j ] , j = arr [ i ] , j + 1 NEW_LINE DEDENT DEDENT while ( len ( arr ) > j ) : NEW_LINE INDENT del arr [ - 1 ] NEW_LINE DEDENT return arr NEW_LINE DEDENT def printArray ( arr ) : NEW_LINE INDENT for i in arr : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT def UtilRemov ( arr , n , k ) : NEW_LINE INDENT arr = RemoveElemArr ( arr , n , k ) NEW_LINE printArray ( arr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE UtilRemov ( arr , n , k ) NEW_LINE DEDENT
Count pairs of equal array elements remaining after every removal | Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Driver Code ; Given Array
def pairs_after_removing ( arr , N ) : NEW_LINE INDENT cntPairs = 0 NEW_LINE mp = { } NEW_LINE for i in arr : NEW_LINE INDENT mp [ i ] = mp . get ( i , 0 ) + 1 NEW_LINE DEDENT for element in mp : NEW_LINE INDENT i = element NEW_LINE cntPairs += mp [ i ] * ( mp [ i ] - 1 ) // 2 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT pairs_after_arr_i_removed = cntPairs + 1 - mp [ arr [ i ] ] NEW_LINE print ( pairs_after_arr_i_removed , end = ' ▁ ' ) NEW_LINE DEDENT return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 3 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE pairs_after_removing ( arr , N ) NEW_LINE DEDENT
Modify N by adding its smallest positive divisor exactly K times | Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; If N is an even number ; Update N ; If N is an odd number ; Update N ; Driver Code
def smallestDivisorGr1 ( N ) : NEW_LINE INDENT for i in range ( sqrt ( N ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( N % i == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT return N NEW_LINE DEDENT def findValOfNWithOperat ( N , K ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT N += K * 2 NEW_LINE DEDENT else : NEW_LINE INDENT N += smallestDivisorGr1 ( N ) + ( K - 1 ) * 2 NEW_LINE DEDENT return N NEW_LINE DEDENT N = 6 NEW_LINE K = 4 NEW_LINE print ( findValOfNWithOperat ( N , K ) ) NEW_LINE
Partition array into minimum number of equal length subsets consisting of a single distinct value | Python3 program to implement the above approach ; Function to find the minimum count of subsets by partitioning the array with given conditions ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores GCD of frequency of each distinct element of the array ; Update freqGCD ; Driver Code
from math import gcd NEW_LINE def CntOfSubsetsByPartitioning ( arr , N ) : NEW_LINE INDENT freq = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq [ arr [ i ] ] = freq . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT freqGCD = 0 NEW_LINE for i in freq : NEW_LINE INDENT freqGCD = gcd ( freqGCD , freq [ i ] ) NEW_LINE DEDENT return ( N ) // freqGCD NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( CntOfSubsetsByPartitioning ( arr , N ) ) NEW_LINE DEDENT
Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix | Initialize rows and columns ; Store all 8 directions ; Function to check if a cell ( i , j ) is valid or not ; Function to find sum of adjacent cells for cell ( i , j ) ; Initialize sum ; Visit all 8 directions ; Check if cell is valid ; Return sum ; Function to print sum of adjacent elements ; Stores the resultant matrix ; Iterate each elements of matrix ; Find adjacent sum ; Driver code ; Given matrix ; Size of matrix ; Function call
r , c = 0 , 0 ; NEW_LINE dir = [ [ 1 , 0 ] , [ - 1 , 0 ] , [ 0 , 1 ] , [ 0 , - 1 ] , [ - 1 , - 1 ] , [ - 1 , 1 ] , [ 1 , 1 ] , [ 1 , - 1 ] ] ; NEW_LINE def valid ( i , j ) : NEW_LINE INDENT if ( i >= 0 and j >= 0 and i < r and j < c ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def find ( i , j , v ) : NEW_LINE INDENT s = 0 ; NEW_LINE for k in range ( 8 ) : NEW_LINE INDENT ni = i + dir [ k ] [ 0 ] ; NEW_LINE nj = j + dir [ k ] [ 1 ] ; NEW_LINE if ( valid ( ni , nj ) ) : NEW_LINE INDENT s += v [ ni ] [ nj ] ; NEW_LINE DEDENT DEDENT return s ; NEW_LINE DEDENT def findsumofneighbors ( M ) : NEW_LINE INDENT v = [ [ 0 for i in range ( c ) ] for j in range ( r ) ] ; NEW_LINE for i in range ( r ) : NEW_LINE INDENT for j in range ( c ) : NEW_LINE INDENT v [ i ] [ j ] = find ( i , j , M ) ; NEW_LINE print ( v [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = [ [ 1 , 4 , 1 ] , [ 2 , 4 , 5 ] , [ 3 , 1 , 2 ] ] ; NEW_LINE r = len ( M [ 0 ] ) ; NEW_LINE c = len ( M [ 1 ] ) ; NEW_LINE findsumofneighbors ( M ) ; NEW_LINE DEDENT
Nth term of a recurrence relation generated by two given arrays | Python3 program for the above approach ; Declare T [ ] [ ] as global matrix ; Result matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to calculate matrix ^ n using binary exponentaion ; Initialize result matrix and unity matrix ; Function to calculate nth term of general recurrence ; Fill T [ ] [ ] with appropriate value ; Function Call to calculate T ^ n ; Calculate nth term as first element of F * ( T ^ n ) ; Print the result ; Driver Code ; Given Initial terms ; Given coefficients ; Given K ; Given N ; Function Call
mod = 1e9 + 7 NEW_LINE T = [ [ 0 for x in range ( 2000 ) ] for y in range ( 2000 ) ] NEW_LINE result = [ [ 0 for x in range ( 2000 ) ] for y in range ( 2000 ) ] NEW_LINE def mul_2 ( K ) : NEW_LINE INDENT temp = [ [ 0 for x in range ( K + 1 ) ] for y in range ( K + 1 ) ] NEW_LINE for i in range ( 1 , K + 1 ) : NEW_LINE INDENT for j in range ( 1 , K + 1 ) : NEW_LINE INDENT for k in range ( 1 , K + 1 ) : NEW_LINE INDENT temp [ i ] [ j ] = ( ( temp [ i ] [ j ] + ( T [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT for j in range ( 1 , K + 1 ) : NEW_LINE INDENT T [ i ] [ j ] = temp [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT def mul_1 ( K ) : NEW_LINE INDENT temp = [ [ 0 for x in range ( K + 1 ) ] for y in range ( K + 1 ) ] NEW_LINE for i in range ( 1 , K + 1 ) : NEW_LINE INDENT for j in range ( 1 , K + 1 ) : NEW_LINE INDENT for k in range ( 1 , K + 1 ) : NEW_LINE INDENT temp [ i ] [ j ] = ( ( temp [ i ] [ j ] + ( result [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT for j in range ( 1 , K + 1 ) : NEW_LINE INDENT result [ i ] [ j ] = temp [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT def matrix_pow ( K , n ) : NEW_LINE INDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT for j in range ( 1 , K + 1 ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT result [ i ] [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT while ( n > 0 ) : NEW_LINE INDENT if ( n % 2 == 1 ) : NEW_LINE INDENT mul_1 ( K ) NEW_LINE DEDENT mul_2 ( K ) NEW_LINE n //= 2 NEW_LINE DEDENT DEDENT def NthTerm ( F , C , K , n ) : NEW_LINE INDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT T [ i ] [ K ] = C [ K - i ] NEW_LINE DEDENT for i in range ( 1 , K + 1 ) : NEW_LINE INDENT T [ i + 1 ] [ i ] = 1 NEW_LINE DEDENT matrix_pow ( K , n ) NEW_LINE answer = 0 NEW_LINE for i in range ( 1 , K + 1 ) : NEW_LINE INDENT answer += F [ i - 1 ] * result [ i ] [ 1 ] NEW_LINE DEDENT print ( int ( answer ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT F = [ 1 , 2 , 3 ] NEW_LINE C = [ 1 , 1 , 1 ] NEW_LINE K = 3 NEW_LINE N = 10 NEW_LINE NthTerm ( F , C , K , N ) NEW_LINE DEDENT
Calculate Root Mean Kth power of all array elements | Python3 program for the above approach ; Function to find the Nth root ; Initially guessing random numberbetween 0 and 9 ; Smaller eps for more accuracy ; Initialize difference between the two roots by INT_MAX ; xK denotes current value of x ; Iterate until desired accuracy is reached ; Find the current value from previous value by newton 's method ; Function to calculate the Root Mean kth power of array elements ; Calculate sum of kth power ; Calculate Mean ; Calculate kth Root of mean ; Driver Code ; Function Call
import sys NEW_LINE import random NEW_LINE def nthRoot ( A , N ) : NEW_LINE INDENT xPre = random . random ( ) % 10 NEW_LINE eps = 1e-3 NEW_LINE delX = sys . maxsize NEW_LINE xK = 0 NEW_LINE while ( delX > eps ) : NEW_LINE INDENT xK = ( ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N ) NEW_LINE delX = abs ( xK - xPre ) NEW_LINE xPre = xK NEW_LINE DEDENT return xK NEW_LINE DEDENT def RMNValue ( arr , n , k ) : NEW_LINE INDENT Nth = 0 NEW_LINE mean = 0.0 NEW_LINE root = 0.0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Nth += pow ( arr [ i ] , k ) NEW_LINE DEDENT mean = ( Nth // ( n ) ) NEW_LINE root = nthRoot ( mean , k ) NEW_LINE return root NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 4 , 6 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( RMNValue ( arr , N , K ) ) NEW_LINE DEDENT