codeparrot/xlcost-text-to-code huggingface dataset:
Minimize operations to convert each node of N | Create adjacency list ; Function to add an edges in graph ; Function to perform the DFS of graph recursively from a given vertex u ; Check for the condition for the flipping of node 's initial value ; Traverse all the children of the current source node u ; Swap foo and foo1 signifies there is change of level ; Function to perform the DFSUtil ( ) for all the unvisited vertices ; Traverse the given set of nodes ; If the current node is unvisited ; Print the number of operations ; Function to count the number of flips required to change initial node values to final node value ; Add the given edges ; DFS Traversal ; Driver code
N = 3 NEW_LINE adj = [ ] NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT adj . append ( [ ] ) NEW_LINE DEDENT visited = [ ] NEW_LINE ans = 0 NEW_LINE def addEdges ( u , v ) : NEW_LINE INDENT global adj NEW_LINE adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT def DFSUtil ( u , foo , foo1 , initial , finall ) : NEW_LINE INDENT global visited , ans , adj NEW_LINE visited [ u ] = True NEW_LINE if ( ( initial [ u - 1 ] ^ foo ) ^ finall [ u - 1 ] == True ) : NEW_LINE INDENT ans += 1 NEW_LINE foo ^= True NEW_LINE DEDENT for i in range ( len ( adj [ u ] ) ) : NEW_LINE INDENT if ( visited [ adj [ u ] [ i ] ] == False ) : NEW_LINE INDENT DFSUtil ( adj [ u ] [ i ] , foo1 , foo , initial , finall ) NEW_LINE DEDENT DEDENT DEDENT def DFS ( V , initial , finall ) : NEW_LINE INDENT global ans , visited NEW_LINE ans = 0 NEW_LINE visited = [ False ] * V NEW_LINE for u in range ( 1 , 2 ) : NEW_LINE INDENT if ( visited [ u ] == False ) : NEW_LINE INDENT DFSUtil ( u , 0 , 0 , initial , finall ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT def countOperations ( N , initial , finall , Edges ) : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT addEdges ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) NEW_LINE DEDENT DFS ( N + 1 , initial , finall ) NEW_LINE DEDENT Edges = [ [ 1 , 2 ] , [ 1 , 3 ] ] NEW_LINE initial = [ True , True , False ] NEW_LINE finall = [ False , True , True ] NEW_LINE countOperations ( N , initial , finall , Edges ) NEW_LINE
Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number | Function to find the minimum number of moves to make N and M equal . ; Array to maintain the numbers included . ; Pair of vertex , count ; Run bfs from N ; If we reached goal ; Iterate in the range ; If i is a factor of aux ; If i is less than M - aux and is not included earlier . ; If aux / i is less than M - aux and is not included earlier . ; Not possible ; Driver code
def countOperations ( N , M ) : NEW_LINE INDENT visited = [ False ] * ( 100001 ) NEW_LINE Q = [ ] NEW_LINE Q . append ( [ N , 0 ] ) NEW_LINE visited [ N ] = True NEW_LINE while ( len ( Q ) > 0 ) : NEW_LINE INDENT aux = Q [ 0 ] [ 0 ] NEW_LINE cont = Q [ 0 ] [ 1 ] NEW_LINE Q . pop ( 0 ) NEW_LINE if ( aux == M ) : NEW_LINE INDENT return cont NEW_LINE DEDENT i = 2 NEW_LINE while i * i <= aux : NEW_LINE INDENT if ( aux % i == 0 ) : NEW_LINE INDENT if ( aux + i <= M and not visited [ aux + i ] ) : NEW_LINE INDENT Q . append ( [ aux + i , cont + 1 ] ) NEW_LINE visited [ aux + i ] = True NEW_LINE DEDENT if ( aux + int ( aux / i ) <= M and not visited [ aux + int ( aux / i ) ] ) : NEW_LINE INDENT Q . append ( [ aux + int ( aux / i ) , cont + 1 ] ) NEW_LINE visited [ aux + int ( aux / i ) ] = True NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT N , M = 4 , 24 NEW_LINE print ( countOperations ( N , M ) ) NEW_LINE
Finding Astronauts from different countries | Function to perform the DFS Traversal to find the count of connected components ; Marking vertex visited ; DFS call to neighbour vertices ; If the current node is not visited , then recursively call DFS ; Function to find the number of ways to choose two astronauts from the different countries ; Stores the Adjacency list ; Constructing the graph ; Stores the visited vertices ; Stores the size of every connected components ; DFS call to the graph ; Store size of every connected component ; Stores the total number of ways to count the pairs ; Traverse the array ; Print the value of ans ; Driver Code
adj = [ ] NEW_LINE visited = [ ] NEW_LINE num = 0 NEW_LINE def dfs ( v ) : NEW_LINE INDENT global adj , visited , num NEW_LINE visited [ v ] = True NEW_LINE num += 1 NEW_LINE for i in range ( len ( adj [ v ] ) ) : NEW_LINE INDENT if ( not visited [ adj [ v ] [ i ] ] ) : NEW_LINE INDENT dfs ( adj [ v ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT def numberOfPairs ( N , arr ) : NEW_LINE INDENT global adj , visited , num NEW_LINE adj = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT adj . append ( [ ] ) NEW_LINE DEDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT adj [ arr [ i ] [ 0 ] ] . append ( arr [ i ] [ 1 ] ) NEW_LINE adj [ arr [ i ] [ 1 ] ] . append ( arr [ i ] [ 0 ] ) NEW_LINE DEDENT visited = [ False ] * ( N ) NEW_LINE v = [ ] NEW_LINE num = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT dfs ( i ) NEW_LINE v . append ( num ) NEW_LINE num = 0 NEW_LINE DEDENT DEDENT ans = N * int ( ( N - 1 ) / 2 ) NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT ans -= ( v [ i ] * int ( ( v [ i ] - 1 ) / 2 ) ) NEW_LINE DEDENT ans += 1 NEW_LINE print ( ans ) NEW_LINE DEDENT N = 6 NEW_LINE arr = [ [ 0 , 1 ] , [ 0 , 2 ] , [ 2 , 5 ] ] NEW_LINE numberOfPairs ( N , arr ) NEW_LINE
Count of Perfect Numbers in given range for Q queries | python 3 program for the above approach ; Function to check whether a number is perfect Number ; Stores sum of divisors ; Itearate over the range [ 2 , sqrt ( N ) ] ; If sum of divisors is equal to N , then N is a perfect number ; Function to find count of perfect numbers in a given range ; Stores the count of perfect Numbers upto a every number less than MAX ; Iterate over the range [ 1 , MAX ] ; Traverse the array arr [ ] ; Print the count of perfect numbers in the range [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ; Driver Code
MAX = 100005 NEW_LINE from math import sqrt NEW_LINE def isPerfect ( N ) : NEW_LINE INDENT sum = 1 NEW_LINE for i in range ( 2 , int ( sqrt ( N ) ) + 1 , 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( i * i != N ) : NEW_LINE INDENT sum = sum + i + N // i NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + i NEW_LINE DEDENT DEDENT DEDENT if ( sum == N and N != 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def Query ( arr , N ) : NEW_LINE INDENT prefix = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE for i in range ( 2 , MAX + 1 , 1 ) : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] + isPerfect ( i ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( prefix [ arr [ i ] [ 1 ] ] - prefix [ arr [ i ] [ 0 ] - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 1000 ] , [ 1000 , 2000 ] , [ 2000 , 3000 ] ] NEW_LINE N = len ( arr ) NEW_LINE Query ( arr , N ) NEW_LINE DEDENT
Find any possible two coordinates of Rectangle whose two coordinates are given | Function to find the remaining two rectangle coordinates ; Pairs to store the position of given two coordinates of the rectangle . ; Pairs to store the remaining two coordinates of the rectangle . ; Traverse through matrix and find pairs p1 and p2 ; First Case ; Second Case ; Third Case ; Print the matrix ; Driver code ; Given Input ; Function Call
def Create_Rectangle ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = [ i for i in arr [ i ] ] NEW_LINE DEDENT p1 = [ - 1 , - 1 ] NEW_LINE p2 = [ - 1 , - 1 ] NEW_LINE p3 = [ ] NEW_LINE p4 = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( arr [ i ] [ j ] == '1' ) : NEW_LINE INDENT if ( p1 [ 0 ] == - 1 ) : NEW_LINE INDENT p1 = [ i , j ] NEW_LINE DEDENT else : NEW_LINE INDENT p2 = [ i , j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT p3 = p1 NEW_LINE p4 = p2 NEW_LINE if ( p1 [ 0 ] == p2 [ 0 ] ) : NEW_LINE INDENT p3 [ 0 ] = ( p1 [ 0 ] + 1 ) % n NEW_LINE p4 [ 0 ] = ( p2 [ 0 ] + 1 ) % n NEW_LINE DEDENT elif ( p1 [ 1 ] == p2 [ 1 ] ) : NEW_LINE INDENT p3 [ 1 ] = ( p1 [ 1 ] + 1 ) % n NEW_LINE p4 [ 1 ] = ( p2 [ 1 ] + 1 ) % n NEW_LINE DEDENT else : NEW_LINE INDENT p3 [ 0 ] , p4 [ 0 ] = p4 [ 0 ] , p3 [ 0 ] NEW_LINE DEDENT arr [ p3 [ 0 ] ] [ p3 [ 1 ] ] = '1' NEW_LINE arr [ p4 [ 0 ] ] [ p4 [ 1 ] ] = '1' NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( " " . join ( arr [ i ] ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE arr = [ "0010" , "0000" , "1000" , "0000" ] NEW_LINE Create_Rectangle ( arr , n ) NEW_LINE DEDENT
Sum of all the prime divisors of a number | Set 2 | Python3 program for the above approach ; Function to find sum of prime divisors of the given number N ; Add the number 2 if it divides N ; Traverse the loop from [ 3 , sqrt ( N ) ] ; If i divides N , add i and divide N ; This condition is to handle the case when N is a prime number greater than 2 ; Driver code ; Given input ; Function call
import math NEW_LINE def SumOfPrimeDivisors ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE if n % 2 == 0 : NEW_LINE INDENT sum += 2 NEW_LINE DEDENT while n % 2 == 0 : NEW_LINE INDENT n //= 2 NEW_LINE DEDENT k = int ( math . sqrt ( n ) ) NEW_LINE for i in range ( 3 , k + 1 , 2 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT sum += i NEW_LINE DEDENT while n % i == 0 : NEW_LINE INDENT n //= i NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT sum += n NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( SumOfPrimeDivisors ( n ) ) NEW_LINE DEDENT
Count bases which contains a set bit as the Most Significant Bit in the representation of N | Python 3 program for the above approach ; Function to count bases having MSB of N as a set bit ; Store the required count ; Iterate over the range [ 2 , N ] ; Store the MSB of N ; If MSB is 1 , then increment the count by 1 ; Return the count ; Driver Code
import math NEW_LINE def countOfBase ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT highestPower = int ( math . log ( N ) / math . log ( i ) ) NEW_LINE firstDigit = int ( N / int ( math . pow ( i , highestPower ) ) ) NEW_LINE if ( firstDigit == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = 6 NEW_LINE print ( countOfBase ( N ) ) NEW_LINE
K | Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Convert integer into string ; If k is greater than length of the temp ; Print the k digit from last ; Driver code ; Given Input ; Function call
def kthDigitFromLast ( n , k ) : NEW_LINE INDENT if ( k <= 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT temp = str ( n ) NEW_LINE if ( k > len ( temp ) ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ord ( temp [ len ( temp ) - k ] ) - ord ( '0' ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2354 NEW_LINE k = 2 NEW_LINE kthDigitFromLast ( n , k ) NEW_LINE DEDENT
Count ways to split array into three non | Function to count ways to split array into three subarrays with equal Bitwise XOR ; Stores the XOR value of arr [ ] ; Update the value of arr_xor ; Stores the XOR value of prefix and suffix array respectively ; Stores the ending points of all the required prefix arrays ; Stores the count of suffix arrays whose XOR value is equal to the total XOR value at each index ; Find all prefix arrays with XOR value equal to arr_xor ; Update pref_xor ; Fill the values of suff_inds [ ] ; Update suff_xor ; Update suff_inds [ i ] ; Stores the total number of ways ; Count total number of ways ; Return the final count ; Driver Code ; Given Input ; Function Call
def countWays ( arr , N ) : NEW_LINE INDENT arr_xor = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT arr_xor ^= arr [ i ] NEW_LINE DEDENT pref_xor , suff_xor = 0 , 0 NEW_LINE pref_ind = [ ] NEW_LINE suff_inds = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT pref_xor ^= arr [ i ] NEW_LINE if ( pref_xor == arr_xor ) : NEW_LINE INDENT pref_ind . append ( i ) NEW_LINE DEDENT DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT suff_xor ^= arr [ i ] NEW_LINE suff_inds [ i ] += suff_inds [ i + 1 ] NEW_LINE if ( suff_xor == arr_xor ) : NEW_LINE INDENT suff_inds [ i ] += 1 NEW_LINE DEDENT DEDENT tot_ways = 0 NEW_LINE for idx in pref_ind : NEW_LINE INDENT if ( idx < N - 1 ) : NEW_LINE INDENT tot_ways += suff_inds [ idx + 2 ] NEW_LINE DEDENT DEDENT return tot_ways NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 0 , 5 , 2 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countWays ( arr , N ) ) NEW_LINE DEDENT
Count pairs up to N having sum equal to their XOR | 2D array for memoization ; Recursive Function to count pairs ( x , y ) such that x + y = x ^ y ; If the string is traversed completely ; If the current subproblem is already calculated ; If bound = 1 and s [ i ] = = '0' , only ( 0 , 0 ) can be placed ; Otherwise ; Placing ( 0 , 1 ) and ( 1 , 0 ) are equivalent . Hence , multiply by 2. ; Place ( 0 , 0 ) at the current position . ; Return the answer ; Utility Function to convert N to its binary representation ; Function to count pairs ( x , y ) such that x + y = x ^ y ; Convert the number to equivalent binary representation ; Print answer returned by recursive function ; Driver code ; Input ; Function call
dp = [ [ - 1 for i in range ( 2 ) ] for j in range ( 1000 ) ] NEW_LINE def IsSumEqualsXor ( i , n , bound , s ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ i ] [ bound ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ bound ] NEW_LINE DEDENT ans = 0 NEW_LINE if ( bound and s [ i ] == '0' ) : NEW_LINE INDENT ans = IsSumEqualsXor ( i + 1 , n , 1 , s ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = 2 * IsSumEqualsXor ( i + 1 , n , bound & ( s [ i ] == '1' ) , s ) NEW_LINE ans += IsSumEqualsXor ( i + 1 , n , 0 , s ) NEW_LINE DEDENT dp [ i ] [ bound ] = ans NEW_LINE return ans NEW_LINE DEDENT def convertToBinary ( n ) : NEW_LINE INDENT ans = [ ] NEW_LINE while ( n ) : NEW_LINE INDENT rem = chr ( n % 2 + 48 ) NEW_LINE ans . append ( rem ) NEW_LINE n //= 2 NEW_LINE DEDENT ans = ans [ : : - 1 ] NEW_LINE return ans NEW_LINE DEDENT def IsSumEqualsXorUtil ( N ) : NEW_LINE INDENT s = convertToBinary ( N ) NEW_LINE print ( IsSumEqualsXor ( 0 , len ( s ) , 1 , s ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE IsSumEqualsXorUtil ( N ) NEW_LINE DEDENT
Mean of fourth powers of first N natural numbers | Function to find the average of the fourth power of first N natural numbers ; Stores the sum of the fourth powers of first N natural numbers ; Calculate the sum of fourth power ; Return the average ; Driver Code
def findAverage ( N ) : NEW_LINE INDENT S = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT S += i * i * i * i NEW_LINE DEDENT return round ( S / N , 4 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( findAverage ( N ) ) NEW_LINE DEDENT
Construct a graph from given degrees of all vertices | A function to print the adjacency matrix . ; n is number of vertices ; For each pair of vertex decrement the degree of both vertex . ; Print the result in specified form ; Driver Code
def printMat ( degseq , n ) : NEW_LINE INDENT mat = [ [ 0 ] * n for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( degseq [ i ] > 0 and degseq [ j ] > 0 ) : NEW_LINE INDENT degseq [ i ] -= 1 NEW_LINE degseq [ j ] -= 1 NEW_LINE mat [ i ] [ j ] = 1 NEW_LINE mat [ j ] [ i ] = 1 NEW_LINE DEDENT DEDENT DEDENT print ( " ▁ " , end = " ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( " ▁ " , " ( " , i , " ) " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE print ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( " ▁ " , " ( " , i , " ) " , end = " " ) NEW_LINE for j in range ( n ) : NEW_LINE INDENT print ( " ▁ " , mat [ i ] [ j ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT degseq = [ 2 , 2 , 1 , 1 , 1 ] NEW_LINE n = len ( degseq ) NEW_LINE printMat ( degseq , n ) NEW_LINE DEDENT
Decimal equivalent of concatenation of absolute difference of floor and rounded | Function to find the decimal equivalent of the new binary array constructed from absolute decimal of floor and the round - off values ; Traverse the givenarray from the end ; Stores the absolute difference between floor and round - off each array element ; If bit / difference is 1 , then calculate the bit by proper power of 2 and add it to result ; Increment the value of power ; Print the result ; Driver Code
def findDecimal ( arr , N ) : NEW_LINE INDENT power = 0 ; NEW_LINE result = 0 ; NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT bit = abs ( int ( arr [ i ] ) - round ( arr [ i ] ) ) ; NEW_LINE if ( bit ) : NEW_LINE INDENT result += pow ( 2 , power ) ; NEW_LINE DEDENT power += 1 ; NEW_LINE DEDENT print ( result ) ; NEW_LINE DEDENT arr = [ 1.2 , 2.6 , 4.2 , 6.9 , 3.1 , 21.6 , 91.2 ] ; NEW_LINE N = len ( arr ) NEW_LINE findDecimal ( arr , N ) ; NEW_LINE
Calculate money placed in boxes after N days based on given conditions | Function to find the total money placed in boxes after N days ; Stores the total money ; Iterate for N days ; Adding the Week number ; Adding previous amount + 1 ; Return the total amount ; Input ; Function call to find total money placed
def totalMoney ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT ans += i / 7 NEW_LINE ans += ( i % 7 + 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 15 NEW_LINE print ( totalMoney ( N ) ) NEW_LINE
Sum of absolute differences of indices of occurrences of each array element | Set 2 | Stores the count of occurrences and previous index of every element ; Constructor ; Function to calculate the sum of absolute differences of indices of occurrences of array element ; Stores the count of elements and their previous indices ; Initialize 2 arrays left [ ] and right [ ] of size N ; Traverse the given array ; If arr [ i ] is present in the Map ; Update left [ i ] to 0 and update the value of arr [ i ] in map ; Otherwise , get the value from the map and update left [ i ] ; Clear the map to calculate right [ ] array ; Traverse the array arr [ ] in reverse ; If arr [ i ] is present in theMap ; Update right [ i ] to 0 and update the value of arr [ i ] in the Map ; Otherwise get the value from the map and update right [ i ] ; Iterate in the range [ 0 , N - 1 ] and print the sum of left [ i ] and right [ i ] as the result ; Driver Code
class pair : NEW_LINE INDENT def __init__ ( self , count , prevIndex ) : NEW_LINE INDENT self . count = count ; NEW_LINE self . prevIndex = prevIndex ; NEW_LINE DEDENT DEDENT def findSum ( arr , n ) : NEW_LINE INDENT map = { } ; NEW_LINE left = [ 0 for i in range ( n ) ] ; NEW_LINE right = [ 0 for i in range ( n ) ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] not in map ) : NEW_LINE INDENT left [ i ] = 0 ; NEW_LINE map [ arr [ i ] ] = pair ( 1 , i ) ; NEW_LINE DEDENT else : NEW_LINE INDENT tmp = map [ arr [ i ] ] ; NEW_LINE left [ i ] = ( tmp . count ) * ( i - tmp . prevIndex ) + left [ tmp . prevIndex ] NEW_LINE map [ arr [ i ] ] = pair ( tmp . count + 1 , i ) ; NEW_LINE DEDENT DEDENT map . clear ( ) ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] not in map ) : NEW_LINE INDENT right [ i ] = 0 ; NEW_LINE map [ arr [ i ] ] = pair ( 1 , i ) ; NEW_LINE DEDENT else : NEW_LINE INDENT tmp = map [ arr [ i ] ] ; NEW_LINE right [ i ] = ( tmp . count ) * ( abs ( i - tmp . prevIndex ) ) + right [ tmp . prevIndex ] ; NEW_LINE map [ arr [ i ] ] = pair ( tmp . count + 1 , i ) ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( left [ i ] + right [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 1 , 1 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findSum ( arr , N ) ; NEW_LINE
Convert a number to another by dividing by its factor or removing first occurrence of a digit from an array | Python3 program for the above approach ; Function to check if a digit x is present in the number N or not ; Convert N to string ; Traverse the num ; Return first occurrence of the digit x ; Function to remove the character at a given index from the number ; Convert N to string ; Store the resultant string ; Traverse the num ; If the number becomes empty after deletion , then return - 1 ; Return the number ; Function to check if A can be reduced to B by performing the operations any number of times ; Create a queue ; Push A into the queue ; Hashmap to check if the element is present in the Queue or not ; Set A as visited ; Iterate while the queue is not empty ; Store the front value of the queue and pop it from it ; If top is equal to B , then return true ; Traverse the array , D [ ] ; Divide top by D [ i ] if it is possible and push the result in q ; If D [ i ] is present at the top ; Remove the first occurrence of D [ i ] from the top and store the new number ; Push newElement into the queue q ; Return false if A can not be reduced to B ; Driver Code
from collections import deque NEW_LINE def isPresent ( n , x ) : NEW_LINE INDENT num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( ( ord ( num [ i ] ) - ord ( '0' ) ) == x ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def removeDigit ( n , index ) : NEW_LINE INDENT num = str ( n ) NEW_LINE ans = " " NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i != index ) : NEW_LINE INDENT ans += num [ i ] NEW_LINE DEDENT DEDENT if ( ans == " " or ( len ( ans ) == 1 and ans [ 0 ] == '0' ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = int ( ans ) NEW_LINE return x NEW_LINE DEDENT def reduceNtoX ( a , b , d , n ) : NEW_LINE INDENT q = deque ( ) NEW_LINE q . append ( a ) NEW_LINE visited = { } NEW_LINE visited [ a ] = True NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT top = q . popleft ( ) NEW_LINE if ( top < 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( top == b ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( d [ i ] != 0 and top % d [ i ] == 0 and ( top // d [ i ] not in visited ) ) : NEW_LINE INDENT q . append ( top // d [ i ] ) NEW_LINE visited [ top // d [ i ] ] = True NEW_LINE DEDENT index = isPresent ( top , d [ i ] ) NEW_LINE if ( index != - 1 ) : NEW_LINE INDENT newElement = removeDigit ( top , index ) NEW_LINE if ( newElement != - 1 and ( newElement not in visited ) ) : NEW_LINE INDENT q . append ( newElement ) NEW_LINE visited [ newElement ] = True NEW_LINE DEDENT DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B = 5643 , 81 NEW_LINE D = [ 3 , 8 , 1 ] NEW_LINE N = len ( D ) NEW_LINE if ( reduceNtoX ( A , B , D , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT