codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to reach ( X , Y ) in a matrix starting from the origin | Python3 implementation of the approach ; To store the factorial and factorial mod inverse of the numbers ; Function to find ( a ^ m1 ) % mod ; Function to find the factorial of all the numbers ; Function to find the factorial modinverse of all the numbers ; Function to return nCr ; Function to return the number of ways to reach ( X , Y ) in a matrix with the given moves starting from the origin ; Driver code
N = 1000005 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 ways ( x , y ) : NEW_LINE INDENT factorialfun ( ) ; NEW_LINE modinversefun ( ) ; NEW_LINE if ( ( 2 * x - y ) % 3 == 0 and ( 2 * y - x ) % 3 == 0 ) : NEW_LINE INDENT m = ( 2 * x - y ) // 3 ; NEW_LINE n = ( 2 * y - x ) // 3 ; NEW_LINE return binomial ( n + m , n ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 3 ; y = 3 ; NEW_LINE print ( ways ( x , y ) ) ; NEW_LINE DEDENT
Find Nth even length palindromic number formed using digits X and Y | Python3 program to find nth even palindromic number of only even length composing of 4 ' s ▁ and ▁ 5' s . ; Utility function to compute n 'th palindrome number  ; Calculate the length from above formula as discussed above ; Calculate rank for length L ; Mask to check if i 't bit   is set or not  ; If bit is set append '5' else append '4' ; Driver Code
from math import ceil , log2 NEW_LINE def solve ( n , x , y ) : NEW_LINE INDENT length = ceil ( log2 ( n + 2 ) ) - 1 ; NEW_LINE rank = n - ( 1 << length ) + 1 ; NEW_LINE left = " " ; right = " " ; NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT mask = ( 1 << i ) ; NEW_LINE bit = ( mask & rank ) ; NEW_LINE if ( bit ) : NEW_LINE INDENT left += y ; NEW_LINE right += y ; NEW_LINE DEDENT else : NEW_LINE INDENT left += x ; NEW_LINE right += x ; NEW_LINE DEDENT DEDENT right = right [ : : - 1 ] ; NEW_LINE res = left + right ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 23 ; NEW_LINE x = '4' ; NEW_LINE y = '5' ; NEW_LINE ans = solve ( n , x , y ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT
Maximum of all the integers in the given level of Pascal triangle | Function for the binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the maximum value in the nth level of the Pascal 's triangle ; Driver code
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for i in range ( k + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] NEW_LINE DEDENT def findMax ( n ) : NEW_LINE INDENT return binomialCoeff ( n , n // 2 ) NEW_LINE DEDENT n = 5 NEW_LINE print ( findMax ( n ) ) NEW_LINE
Length of the smallest sub | Python 3 program to find the length of the smallest substring consisting of maximum distinct characters ; Find maximum distinct characters in any string ; Initialize all character 's  count with 0 ; Increase the count in array if a character is found ; size of given string ; Find maximum distinct characters in any string ; result ; Brute force approach to find all substrings ; We have to check here both conditions together 1. substring ' s ▁ distinct ▁ characters ▁ is ▁ equal ▁ ▁ to ▁ maximum ▁ distinct ▁ characters ▁ ▁ 2 . ▁ substring ' s length should be minimum ; Driver Code ; Input String
NO_OF_CHARS = 256 NEW_LINE def max_distinct_char ( str , n ) : NEW_LINE INDENT count = [ 0 ] * NO_OF_CHARS NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT max_distinct = 0 NEW_LINE for i in range ( NO_OF_CHARS ) : NEW_LINE INDENT if ( count [ i ] != 0 ) : NEW_LINE INDENT max_distinct += 1 NEW_LINE DEDENT DEDENT return max_distinct NEW_LINE DEDENT def smallesteSubstr_maxDistictChar ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE max_distinct = max_distinct_char ( str , n ) NEW_LINE minl = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT subs = str [ i : j ] NEW_LINE subs_lenght = len ( subs ) NEW_LINE sub_distinct_char = max_distinct_char ( subs , subs_lenght ) NEW_LINE if ( subs_lenght < minl and max_distinct == sub_distinct_char ) : NEW_LINE INDENT minl = subs_lenght NEW_LINE DEDENT DEDENT DEDENT return minl NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " AABBBCBB " NEW_LINE l = smallesteSubstr_maxDistictChar ( str ) ; NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ smallest ▁ substring " , " consisting ▁ of ▁ maximum ▁ distinct " , " characters ▁ : " , l ) NEW_LINE DEDENT
Find two co | Python3 implementation of the approach ; Function to find the required numbers ; GCD of the given numbers ; Printing the required numbers ; Driver code
from math import gcd NEW_LINE def findNumbers ( a , b ) : NEW_LINE INDENT __gcd = gcd ( a , b ) ; NEW_LINE print ( ( a // __gcd ) , ( b // __gcd ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 12 ; b = 16 ; NEW_LINE findNumbers ( a , b ) ; NEW_LINE DEDENT
Find the remaining balance after the transaction | Function to find the balance ; Check if the transaction can be successful or not ; Transaction is successful ; Transaction is unsuccessful ; Driver Code
def findBalance ( x , bal ) : NEW_LINE INDENT if ( x % 10 == 0 and ( x + 1.50 ) <= bal ) : NEW_LINE INDENT print ( round ( bal - x - 1.50 , 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( round ( bal , 2 ) ) NEW_LINE DEDENT DEDENT x = 50 NEW_LINE bal = 100.50 NEW_LINE findBalance ( x , bal ) NEW_LINE
Count number of pairs in array having sum divisible by K | SET 2 | Python Program to count pairs whose sum divisible by 'K ; Program to count pairs whose sum divisible by ' K ' ; Create a frequency array to count occurrences of all remainders when divided by K ; To store count of pairs . ; Traverse the array , compute the remainder and add k - remainder value hash count to ans ; Count number of ( A [ i ] , ( K - rem ) % K ) pairs ; Increment count of remainder in hash map ; Driver code
' NEW_LINE def countKdivPairs ( A , n , K ) : NEW_LINE INDENT freq = [ 0 for i in range ( K ) ] NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT rem = A [ i ] % K NEW_LINE ans += freq [ ( K - rem ) % K ] NEW_LINE freq [ rem ] += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 2 , 1 , 7 , 5 , 3 ] NEW_LINE n = len ( A ) NEW_LINE K = 4 NEW_LINE print ( countKdivPairs ( A , n , K ) ) NEW_LINE DEDENT
Minimum number of Bottles visible when a bottle can be enclosed inside another Bottle | Python3 code for the above approach ; Driver code ; Find the solution
def min_visible_bottles ( arr , n ) : NEW_LINE INDENT m = dict ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] = m . get ( arr [ i ] , 0 ) + 1 NEW_LINE ans = max ( ans , m [ arr [ i ] ] ) NEW_LINE DEDENT print ( " Minimum ▁ number ▁ of " , " Visible ▁ Bottles ▁ are : ▁ " , ans ) NEW_LINE DEDENT n = 8 NEW_LINE arr = [ 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 ] NEW_LINE min_visible_bottles ( arr , n ) NEW_LINE
Find the final sequence of the array after performing given operations | Function that generates the array b [ ] when n is even ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function that generates the array b [ ] when n is odd ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function to find the final array b [ ] after n operations of given type ; Create the array b ; If the array size is even ; Print the final array elements ; Driver code
def solveEven ( n , arr , b ) : NEW_LINE INDENT left = n - 1 NEW_LINE for i in range ( ( n // 2 ) ) : NEW_LINE INDENT b [ i ] = arr [ left ] NEW_LINE left = left - 2 NEW_LINE if ( left < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT right = 0 NEW_LINE for i in range ( n // 2 , n , 1 ) : NEW_LINE INDENT b [ i ] = arr [ right ] NEW_LINE right = right + 2 NEW_LINE if ( right > n - 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT def solveOdd ( n , arr , b ) : NEW_LINE INDENT left = n - 1 NEW_LINE for i in range ( n // 2 + 1 ) : NEW_LINE INDENT b [ i ] = arr [ left ] NEW_LINE left = left - 2 NEW_LINE if ( left < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT right = 1 NEW_LINE for i in range ( n // 2 + 1 , n , 1 ) : NEW_LINE INDENT b [ i ] = arr [ right ] NEW_LINE right = right + 2 NEW_LINE if ( right > n - 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT def solve ( n , arr ) : NEW_LINE INDENT b = [ 0 for i in range ( n ) ] NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT solveEven ( n , arr , b ) NEW_LINE DEDENT else : NEW_LINE INDENT solveOdd ( n , arr , b ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( b [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE solve ( n , arr ) NEW_LINE DEDENT
Find the sum of the first half and second half elements of an array | Function to find the sum of the first half elements and second half elements of an array ; Add elements in first half sum ; Add elements in the second half sum ; Driver Code ; Function call
def sum_of_elements ( arr , n ) : NEW_LINE INDENT sumfirst = 0 ; NEW_LINE sumsecond = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i < n // 2 ) : NEW_LINE INDENT sumfirst += arr [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT sumsecond += arr [ i ] ; NEW_LINE DEDENT DEDENT print ( " Sum ▁ of ▁ first ▁ half ▁ elements ▁ is " , sumfirst , end =   " " ) ; NEW_LINE print ( " Sum ▁ of ▁ second ▁ half ▁ elements ▁ is " , sumsecond , end =   " " ) ; NEW_LINE DEDENT arr = [ 20 , 30 , 60 , 10 , 25 , 15 , 40 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE sum_of_elements ( arr , n ) ; NEW_LINE
Sequence with sum K and minimum sum of absolute differences between consecutive elements | Function to return the minimized sum ; If k is divisible by n then the answer will be 0 ; Else the answer will be 1 ; Driver code
def minimum_sum ( n , k ) : NEW_LINE INDENT if ( k % n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT return 1 NEW_LINE DEDENT n = 3 NEW_LINE k = 56 NEW_LINE print ( minimum_sum ( n , k ) ) NEW_LINE
Print first N terms of Lower Wythoff sequence | Python3 implementation of the approach ; Function to print the first n terms of the lower Wythoff sequence ; Calculate value of phi ; Find the numbers ; a ( n ) = floor ( n * phi ) ; Print the nth numbers ; Driver code
from math import sqrt , floor NEW_LINE def lowerWythoff ( n ) : NEW_LINE INDENT phi = ( 1 + sqrt ( 5 ) ) / 2 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = floor ( i * phi ) ; NEW_LINE print ( ans , end = " " ) ; NEW_LINE if ( i != n ) : NEW_LINE INDENT print ( " , ▁ " , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE lowerWythoff ( n ) ; NEW_LINE DEDENT
Create new linked list from two given linked list with greater element at each node | Node class ; Function to insert node in a linked list ; Function which returns new linked list ; Compare for greater node ; Driver Code ; First linked list ; Second linked list
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def insert ( root , item ) : NEW_LINE INDENT temp = Node ( 0 ) NEW_LINE temp . data = item NEW_LINE temp . next = None NEW_LINE if ( root == None ) : NEW_LINE INDENT root = temp NEW_LINE DEDENT else : NEW_LINE INDENT ptr = root NEW_LINE while ( ptr . next != None ) : NEW_LINE INDENT ptr = ptr . next NEW_LINE DEDENT ptr . next = temp NEW_LINE DEDENT return root NEW_LINE DEDENT def newList ( root1 , root2 ) : NEW_LINE INDENT ptr1 = root1 NEW_LINE ptr2 = root2 NEW_LINE root = None NEW_LINE while ( ptr1 != None ) : NEW_LINE INDENT temp = Node ( 0 ) NEW_LINE temp . next = None NEW_LINE if ( ptr1 . data < ptr2 . data ) : NEW_LINE INDENT temp . data = ptr2 . data NEW_LINE DEDENT else : NEW_LINE INDENT temp . data = ptr1 . data NEW_LINE DEDENT if ( root == None ) : NEW_LINE INDENT root = temp NEW_LINE DEDENT else : NEW_LINE INDENT ptr = root NEW_LINE while ( ptr . next != None ) : NEW_LINE INDENT ptr = ptr . next NEW_LINE DEDENT ptr . next = temp NEW_LINE DEDENT ptr1 = ptr1 . next NEW_LINE ptr2 = ptr2 . next NEW_LINE DEDENT return root NEW_LINE DEDENT def display ( root ) : NEW_LINE INDENT while ( root != None ) : NEW_LINE INDENT print ( root . data , " - > " , end = " ▁ " ) NEW_LINE root = root . next NEW_LINE DEDENT print ( " ▁ " ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = None NEW_LINE root2 = None NEW_LINE root = None NEW_LINE root1 = insert ( root1 , 5 ) NEW_LINE root1 = insert ( root1 , 2 ) NEW_LINE root1 = insert ( root1 , 3 ) NEW_LINE root1 = insert ( root1 , 8 ) NEW_LINE print ( " First ▁ List : ▁ " , end = " ▁ " ) NEW_LINE display ( root1 ) NEW_LINE root2 = insert ( root2 , 1 ) NEW_LINE root2 = insert ( root2 , 7 ) NEW_LINE root2 = insert ( root2 , 4 ) NEW_LINE root2 = insert ( root2 , 5 ) NEW_LINE print ( " Second ▁ List : ▁ " , end = " ▁ " ) NEW_LINE display ( root2 ) NEW_LINE root = newList ( root1 , root2 ) NEW_LINE print ( " New ▁ List : ▁ " , end = " ▁ " ) NEW_LINE display ( root ) NEW_LINE DEDENT
Count of quadruplets from range [ L , R ] having GCD equal to K | Python 3 implementation of the approach ; Function to return the count of quadruplets having gcd = k ; To store the required count ; Check every quadruplet pair whether its gcd is k ; Return the required count ; Driver code
from math import gcd NEW_LINE def countQuadruplets ( l , r , k ) : NEW_LINE INDENT count = 0 NEW_LINE for u in range ( l , r + 1 , 1 ) : NEW_LINE INDENT for v in range ( l , r + 1 , 1 ) : NEW_LINE INDENT for w in range ( l , r + 1 , 1 ) : NEW_LINE INDENT for x in range ( l , r + 1 , 1 ) : NEW_LINE INDENT if ( gcd ( gcd ( u , v ) , gcd ( w , x ) ) == k ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 1 NEW_LINE r = 10 NEW_LINE k = 2 NEW_LINE print ( countQuadruplets ( l , r , k ) ) NEW_LINE DEDENT
Find an index such that difference between product of elements before and after it is minimum | Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code
def findIndex ( a , n ) : NEW_LINE INDENT res , min_diff = None , float ( ' inf ' ) NEW_LINE prod = [ None ] * n NEW_LINE prod [ 0 ] = a [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prod [ i ] = prod [ i - 1 ] * a [ i ] NEW_LINE DEDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT curr_diff = abs ( ( prod [ n - 1 ] // prod [ i ] ) - prod [ i ] ) NEW_LINE if curr_diff < min_diff : NEW_LINE INDENT min_diff = curr_diff NEW_LINE res = i NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 5 , 7 , 2 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findIndex ( arr , N ) ) NEW_LINE DEDENT
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | Python3 program to implement the above approach ; Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Driver Code
from math import gcd NEW_LINE def printNumbers ( a , n , x ) : NEW_LINE INDENT flag = False NEW_LINE for i in range ( n ) : NEW_LINE INDENT num = a [ i ] NEW_LINE g = gcd ( num , x ) NEW_LINE while ( g != 1 ) : NEW_LINE INDENT num //= g NEW_LINE g = gcd ( num , x ) NEW_LINE DEDENT if ( num == 1 ) : NEW_LINE INDENT flag = True ; NEW_LINE print ( a [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if ( not flag ) : NEW_LINE INDENT print ( " There ▁ are ▁ no ▁ such ▁ numbers " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 60 NEW_LINE a = [ 2 , 5 , 10 , 7 , 17 ] NEW_LINE n = len ( a ) NEW_LINE printNumbers ( a , n , x ) NEW_LINE DEDENT
Find the final radiations of each Radiated Stations | Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; Driver code ; 1 - based indexing
def printf ( rStation , n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT print ( rStation [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , ▁ end ▁ = ▁ "   " ) NEW_LINE DEDENT def radiated_Station ( station , n ) : NEW_LINE INDENT rStation = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT li = i - 1 NEW_LINE ri = i + 1 NEW_LINE lRad = station [ i ] - 1 NEW_LINE rRad = station [ i ] - 1 NEW_LINE rStation [ i ] += station [ i ] NEW_LINE while ( li >= 1 and lRad >= 1 ) : NEW_LINE INDENT rStation [ li ] += lRad NEW_LINE lRad -= 1 NEW_LINE li -= 1 NEW_LINE DEDENT while ( ri <= n and rRad >= 1 ) : NEW_LINE INDENT rStation [ ri ] += rRad NEW_LINE rRad -= 1 NEW_LINE ri += 1 NEW_LINE DEDENT DEDENT printf ( rStation , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT station = [ 0 , 7 , 9 , 12 , 2 , 5 ] NEW_LINE n = len ( station ) - 1 NEW_LINE radiated_Station ( station , n ) NEW_LINE DEDENT
Program to find the maximum difference between the index of any two different numbers | Function to return the maximum difference ; Iteratively check from back ; Different numbers ; Iteratively check from the beginning ; Different numbers ; Driver code
def findMaximumDiff ( a , n ) : NEW_LINE INDENT ind1 = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ 0 ] != a [ i ] ) : NEW_LINE INDENT ind1 = i NEW_LINE break NEW_LINE DEDENT DEDENT ind2 = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( a [ n - 1 ] != a [ i ] ) : NEW_LINE INDENT ind2 = ( n - 1 - i ) NEW_LINE break NEW_LINE DEDENT DEDENT return max ( ind1 , ind2 ) NEW_LINE DEDENT a = [ 1 , 2 , 3 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( findMaximumDiff ( a , n ) ) NEW_LINE
Find the Kth node in the DFS traversal of a given subtree in a Tree | Python3 program to find the Kth node in the DFS traversal of the subtree of given vertex V in a Tree ; To store nodes ; To store the current index of vertex in DFS ; To store the starting index and ending index of vertex in the DFS traversal array ; To store the DFS of vertex 1 ; Function to add edge between two nodes ; Initialize the vectors ; Function to perform DFS of a vertex 1. stores the DFS of the vertex 1 in vector p , 2. store the start index of DFS of every vertex 3. store the end index of DFS of every vertex ; store staring index of node ch ; store ending index ; Function to find the Kth node in DFS of vertex V ; check if kth number exits or not ; number of nodes ; add edges ; store DFS of 1 st node
N = 100005 NEW_LINE n = 10 NEW_LINE tree = [ [ ] for i in range ( N ) ] NEW_LINE static int n ; NEW_LINE static ArrayList tree = new ArrayList ( ) ; NEW_LINE currentIdx = 0 NEW_LINE startIdx = [ 0 for i in range ( n ) ] NEW_LINE endIdx = [ 0 for i in range ( n ) ] NEW_LINE p = [ 0 for i in range ( n ) ] NEW_LINE def Add_edge ( u , v ) : NEW_LINE INDENT tree [ u ] . append ( v ) NEW_LINE tree [ v ] . append ( u ) NEW_LINE DEDENT def intisalise ( ) : NEW_LINE INDENT pass NEW_LINE DEDENT def Dfs ( ch , par ) : NEW_LINE INDENT global currentIdx NEW_LINE p [ currentIdx ] = ch NEW_LINE startIdx [ ch ] = currentIdx NEW_LINE currentIdx += 1 NEW_LINE for c in tree [ ch ] : NEW_LINE INDENT if ( c != par ) : NEW_LINE INDENT Dfs ( c , ch ) NEW_LINE DEDENT DEDENT endIdx [ ch ] = currentIdx - 1 NEW_LINE DEDENT def findNode ( v , k ) : NEW_LINE INDENT k += startIdx [ v ] - 1 NEW_LINE if ( k <= endIdx [ v ] ) : NEW_LINE INDENT return p [ k ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT n = 9 NEW_LINE Add_edge ( 1 , 2 ) NEW_LINE Add_edge ( 1 , 3 ) NEW_LINE Add_edge ( 1 , 4 ) NEW_LINE Add_edge ( 3 , 5 ) NEW_LINE Add_edge ( 3 , 7 ) NEW_LINE Add_edge ( 5 , 6 ) NEW_LINE Add_edge ( 5 , 8 ) NEW_LINE Add_edge ( 7 , 9 ) NEW_LINE intisalise ( ) NEW_LINE Dfs ( 1 , 0 ) NEW_LINE v , k = 3 , 4 NEW_LINE print ( findNode ( v , k ) ) NEW_LINE
Sum of the series Kn + ( K ( n | Function to return sum ; Driver code
def sum ( k , n ) : NEW_LINE INDENT sum = ( pow ( k , n + 1 ) - pow ( k - 1 , n + 1 ) ) ; NEW_LINE return sum ; NEW_LINE DEDENT n = 3 ; NEW_LINE K = 3 ; NEW_LINE print ( sum ( K , n ) ) ; NEW_LINE
Check whether factorial of N is divisible by sum of first N natural numbers | Function to check whether a number is prime or not . ; Count variable to store the number of factors of 'num ; Counting the number of factors ; If number is prime return true ; Function to check for divisibility ; if ' n ' equals 1 then divisibility is possible ; Else check whether ' n + 1' is prime or not ; If ' n + 1' is prime then ' n ! ' is not divisible by 'n*(n+1)/2 ; else divisibility occurs ; Test for n = 3 ; Test for n = 4
def is_prime ( num ) : NEW_LINE ' NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , num + 1 ) : NEW_LINE INDENT if i * i > num : NEW_LINE INDENT break NEW_LINE DEDENT if ( ( num ) % i == 0 ) : NEW_LINE INDENT if ( i * i != ( num ) ) : NEW_LINE INDENT count += 2 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT if ( count == 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def is_divisible ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT else : NEW_LINE DEDENT ' NEW_LINE INDENT if ( is_prime ( n + 1 ) ) : NEW_LINE INDENT return " NO " NEW_LINE DEDENT else : NEW_LINE INDENT return " YES " NEW_LINE DEDENT DEDENT n = 3 NEW_LINE print ( is_divisible ( n ) ) NEW_LINE n = 4 NEW_LINE print ( is_divisible ( n ) ) NEW_LINE