codeparrot/xlcost-text-to-code huggingface dataset:
Minimum subsequences of a string A required to be appended to obtain the string B | Python3 program for the above approach ; Function to count the minimum subsequences of a A required to be appended to obtain the B ; Size of the string ; Maps characters to their respective indices ; Insert indices of characters into the sets ; Stores the position of the last visited index in the A . Initially set it to - 1. ; Stores the required count ; Iterate over the characters of B ; If the character in B is not present in A , return - 1 ; Fetch the next index from B [ i ] 's set ; If the iterator points to the end of that set ; If it doesn 't poto the  end, update previous ; Prthe answer ; Driver Code
from bisect import bisect_right NEW_LINE def countminOpsToConstructAString ( A , B ) : NEW_LINE INDENT N = len ( A ) NEW_LINE i = 0 NEW_LINE mp = [ [ ] for i in range ( 26 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT mp [ ord ( A [ i ] ) - ord ( ' a ' ) ] . append ( i ) NEW_LINE DEDENT previous = - 1 NEW_LINE ans , i = 1 , 0 NEW_LINE while i < len ( B ) : NEW_LINE INDENT ch = B [ i ] NEW_LINE if ( len ( mp [ ord ( ch ) - ord ( ' a ' ) ] ) == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT it = bisect_right ( mp [ ord ( ch ) - ord ( ' a ' ) ] , previous ) NEW_LINE if ( it == len ( mp [ ord ( ch ) - ord ( ' a ' ) ] ) ) : NEW_LINE INDENT previous = - 1 NEW_LINE ans += 1 NEW_LINE continue NEW_LINE DEDENT previous = mp [ ord ( ch ) - ord ( ' a ' ) ] [ it ] NEW_LINE i += 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B = " abc " , " abac " NEW_LINE countminOpsToConstructAString ( A , B ) NEW_LINE DEDENT
Maximize difference between odd and even indexed array elements by shift operations | Function to minimize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the minimum possible value ; Function to maximize array elements by shift operations ; For checking all the left shift operations ; Left shift ; Consider the maximum possible value ; Function to maximize the absolute difference between even and odd indexed array elements ; To calculate the difference of odd indexed elements and even indexed elements ; To calculate the difference between odd and even indexed array elements ; Print the maximum value ; Driver Code
def minimize ( n ) : NEW_LINE INDENT optEle = n NEW_LINE strEle = str ( n ) NEW_LINE for idx in range ( len ( strEle ) ) : NEW_LINE INDENT temp = int ( strEle [ idx : ] + strEle [ : idx ] ) NEW_LINE optEle = min ( optEle , temp ) NEW_LINE DEDENT return optEle NEW_LINE DEDENT def maximize ( n ) : NEW_LINE INDENT optEle = n NEW_LINE strEle = str ( n ) NEW_LINE for idx in range ( len ( strEle ) ) : NEW_LINE INDENT temp = int ( strEle [ idx : ] + strEle [ : idx ] ) NEW_LINE optEle = max ( optEle , temp ) NEW_LINE DEDENT return optEle NEW_LINE DEDENT def minimumDifference ( arr ) : NEW_LINE INDENT caseOne = 0 NEW_LINE minVal = 0 NEW_LINE maxVal = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE minVal += minimize ( arr [ i ] ) NEW_LINE else : NEW_LINE maxVal += maximize ( arr [ i ] ) NEW_LINE DEDENT caseOne = abs ( maxVal - minVal ) NEW_LINE caseTwo = 0 NEW_LINE minVal = 0 NEW_LINE maxVal = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if i % 2 : NEW_LINE maxVal += maximize ( arr [ i ] ) NEW_LINE else : NEW_LINE minVal += minimize ( arr [ i ] ) NEW_LINE DEDENT caseTwo = abs ( maxVal - minVal ) NEW_LINE print ( max ( caseOne , caseTwo ) ) NEW_LINE DEDENT arr = [ 332 , 421 , 215 , 584 , 232 ] NEW_LINE minimumDifference ( arr ) NEW_LINE
Find if given vertical level of binary tree is sorted or not | Python program to determine whether vertical level l of binary tree is sorted or not . ; Structure of a tree node . ; Helper function to determine if vertical level l of given binary tree is sorted or not . ; If root is null , then the answer is an empty subset and an empty subset is always considered to be sorted . ; Variable to store previous value in vertical level l . ; Variable to store current level while traversing tree vertically . ; Variable to store current node while traversing tree vertically . ; Declare queue to do vertical order traversal . A pair is used as element of queue . The first element in pair represents the node and the second element represents vertical level of that node . ; Insert root in queue . Vertical level of root is 0. ; Do vertical order traversal until all the nodes are not visited . ; Check if level of node extracted from queue is required level or not . If it is the required level then check if previous value in that level is less than or equal to value of node . ; If left child is not NULL then push it in queue with level reduced by 1. ; If right child is not NULL then push it in queue with level increased by 1. ; If the level asked is not present in the given binary tree , that means that level will contain an empty subset . Therefore answer will be true . ; Driver Code ; 1 / \ 2 5 / \ 7 4 / 6
from collections import deque NEW_LINE from sys import maxsize NEW_LINE INT_MIN = - maxsize NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isSorted ( root : Node , level : int ) -> bool : NEW_LINE INDENT if root is None : NEW_LINE INDENT return True NEW_LINE DEDENT prevVal = INT_MIN NEW_LINE currLevel = 0 NEW_LINE currNode = Node ( 0 ) NEW_LINE q = deque ( ) NEW_LINE q . append ( ( root , 0 ) ) NEW_LINE while q : NEW_LINE INDENT currNode = q [ 0 ] [ 0 ] NEW_LINE currLevel = q [ 0 ] [ 1 ] NEW_LINE q . popleft ( ) NEW_LINE if currLevel == level : NEW_LINE INDENT if prevVal <= currNode . key : NEW_LINE INDENT prevVal = currNode . key NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if currNode . left : NEW_LINE INDENT q . append ( ( currNode . left , currLevel - 1 ) ) NEW_LINE DEDENT if currNode . right : NEW_LINE INDENT q . append ( ( currNode . right , currLevel + 1 ) ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . left = Node ( 7 ) NEW_LINE root . left . right = Node ( 4 ) NEW_LINE root . left . right . left = Node ( 6 ) NEW_LINE level = - 1 NEW_LINE if isSorted ( root , level ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimize operations of removing 2 i | Function to find minimum count of steps required to remove all the array elements ; Stores minimum count of steps required to remove all the array elements ; Update N ; Traverse each bit of N ; If current bit is set ; Update cntStep ; Driver Code
def minimumStepReqArr ( arr , N ) : NEW_LINE INDENT cntStep = 0 NEW_LINE N += 1 NEW_LINE i = 31 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( N & ( 1 << i ) ) : NEW_LINE INDENT cntStep += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return cntStep NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minimumStepReqArr ( arr , N ) ) NEW_LINE DEDENT
Minimize removal of alternating subsequences to empty given Binary String | Function to find the minimum number of operations required to empty the string ; Initialize variables ; Traverse the string ; If current character is 0 ; Update maximum consecutive 0 s and 1 s ; Print the minimum operation ; Driver code + ; input string ; length of string ; Function Call
def minOpsToEmptyString ( S , N ) : NEW_LINE INDENT one = 0 NEW_LINE zero = 0 NEW_LINE x0 = 0 NEW_LINE x1 = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT x0 += 1 NEW_LINE x1 = 0 NEW_LINE DEDENT else : NEW_LINE INDENT x1 += 1 NEW_LINE x0 = 0 NEW_LINE DEDENT zero = max ( x0 , zero ) NEW_LINE one = max ( x1 , one ) NEW_LINE DEDENT print ( max ( one , zero ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "0100100111" NEW_LINE N = len ( S ) NEW_LINE minOpsToEmptyString ( S , N ) NEW_LINE DEDENT
Swap upper and lower triangular halves of a given Matrix | Function to swap laterally inverted images of upper and lower triangular halves of a given matrix ; Store the matrix elements from upper & lower triangular halves ; Traverse the matrix mat [ ] [ ] ; Find the index ; If current element lies on the principal diagonal ; If current element lies below the principal diagonal ; If current element lies above the principal diagonal ; Traverse again to swap values ; Find the index ; Principal diagonal ; Below main diagonal ; Above main diagonal ; Traverse the matrix and pr ; Driver Code ; Given Matrix mat [ ] [ ] ; Swap the upper and lower triangular halves
def ReverseSwap ( mat , n ) : NEW_LINE INDENT lowerEle = [ [ ] for i in range ( n ) ] NEW_LINE upperEle = [ [ ] for i in range ( n ) ] NEW_LINE index = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT index = abs ( i - j ) NEW_LINE if ( i == j ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( j < i ) : NEW_LINE INDENT lowerEle [ index ] . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT else : NEW_LINE INDENT upperEle [ index ] . append ( mat [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT index = abs ( i - j ) NEW_LINE if ( i == j ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( j < i ) : NEW_LINE INDENT mat [ i ] [ j ] = upperEle [ index ] [ - 1 ] NEW_LINE del upperEle [ index ] [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT mat [ i ] [ j ] = lowerEle [ index ] [ - 1 ] NEW_LINE del lowerEle [ index ] [ - 1 ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT 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 mat = [ [ 1 , 2 ] , [ 4 , 5 ] ] NEW_LINE N = len ( mat ) NEW_LINE ReverseSwap ( mat , N ) NEW_LINE DEDENT
Minimize sum of K positive integers with given LCM | Function to find the prime power of X ; Stores prime powers of X ; Iterate over the range [ 2 , sqrt ( X ) ] ; If X is divisible by i ; Stores prime power ; Calculate prime power of X ; Update X ; Update p ; Insert prime powers into primePow [ ] ; If X exceeds 1 ; Function to calculate the sum of array elements ; Stores sum of array elements ; Traverse the array ; Update sum ; Function to partition array into K groups such that sum of elements of the K groups is minimum ; If count of prime powers is equal to pos ; Stores minimum sum of of each partition of arr [ ] into K groups ; Traverse the array arr [ ] ; Include primePow [ pos ] into i - th groups ; Update res ; Remove factors [ pos ] from i - th groups ; Utility function to calculate minimum sum of K positive integers whose LCM is X ; Stores all prime powers of X ; Stores count of prime powers ; Stores minimum sum of K positive integers whose LCM is X ; If n is less than or equal to k ; Traverse primePow [ ] array ; Update sum ; Update sum ; arr [ i ] : Stores element in i - th group by partitioning the primePow [ ] array ; Update sum ; Driver Code
def primePower ( X ) : NEW_LINE INDENT primePow = [ ] NEW_LINE for i in range ( 2 , X + 1 ) : NEW_LINE INDENT if i * i > X + 1 : NEW_LINE INDENT break NEW_LINE DEDENT if ( X % i == 0 ) : NEW_LINE INDENT p = 1 NEW_LINE while ( X % i == 0 ) : NEW_LINE INDENT X //= i NEW_LINE p *= i NEW_LINE DEDENT primePow . append ( p ) NEW_LINE DEDENT DEDENT if ( X > 1 ) : NEW_LINE INDENT primePow . append ( X ) NEW_LINE DEDENT return primePow NEW_LINE DEDENT def getSum ( ar ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in ar : NEW_LINE INDENT sum += i NEW_LINE DEDENT return sum NEW_LINE DEDENT def getMinSum ( pos , arr , primePow ) : NEW_LINE INDENT if ( pos == len ( primePow ) ) : NEW_LINE INDENT return getSum ( arr ) NEW_LINE DEDENT res = 10 ** 9 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT arr [ i ] *= primePow [ pos ] NEW_LINE res = min ( res , getMinSum ( pos + 1 , arr , primePow ) ) NEW_LINE arr [ i ] //= primePow [ pos ] NEW_LINE DEDENT return res NEW_LINE DEDENT def minimumSumWithGivenLCM ( k , x ) : NEW_LINE INDENT primePow = primePower ( x ) NEW_LINE n = len ( primePow ) NEW_LINE sum = 0 NEW_LINE if ( n <= k ) : NEW_LINE INDENT for i in primePow : NEW_LINE INDENT sum += i NEW_LINE DEDENT sum += k - n NEW_LINE DEDENT else : NEW_LINE INDENT arr = [ 1 ] * ( k ) NEW_LINE sum = getMinSum ( 0 , arr , primePow ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 3 NEW_LINE x = 210 NEW_LINE print ( minimumSumWithGivenLCM ( k , x ) ) NEW_LINE DEDENT
Minimum removal of subsequences of distinct consecutive characters required to empty a given string | Function to count minimum operations required to make the string an empty string ; Stores count of 1 s by removing consecutive distinct subsequence ; Stores count of 0 s by removing consecutive distinct subsequence ; Traverse the string ; If current character is 0 ; Update cntOne ; Update cntZero ; If current character is 1 ; Update cntZero ; Update cntOne ; Driver code
def findMinOperationsReqEmpStr ( str ) : NEW_LINE INDENT cntOne = 0 NEW_LINE cntZero = 0 NEW_LINE for element in str : NEW_LINE INDENT if element == '0' : NEW_LINE INDENT if cntOne > 0 : NEW_LINE INDENT cntOne = cntOne - 1 NEW_LINE DEDENT cntZero = cntZero + 1 NEW_LINE DEDENT else : NEW_LINE INDENT if cntZero > 0 : NEW_LINE INDENT cntZero = cntZero - 1 NEW_LINE DEDENT cntOne = cntOne + 1 NEW_LINE DEDENT DEDENT return cntOne + cntZero NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "0100100111" NEW_LINE print ( findMinOperationsReqEmpStr ( str ) ) NEW_LINE DEDENT
Minimum number of flips required such that the last cell of matrix can be reached from any other cell | Function to calculate the minimum number of flips required ; Dimensions of mat [ ] [ ] ; Initialize answer ; Count all ' D ' s in the last row ; Count all ' R ' s in the last column ; Print answer ; Given matrix ; Function call ; Print answer
def countChanges ( mat ) : NEW_LINE INDENT n = len ( mat ) NEW_LINE m = len ( mat [ 0 ] ) NEW_LINE ans = 0 NEW_LINE for j in range ( m - 1 ) : NEW_LINE INDENT if ( mat [ n - 1 ] [ j ] != ' R ' ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( mat [ i ] [ m - 1 ] != ' D ' ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ [ ' R ' , ' R ' , ' R ' , ' D ' ] , [ ' D ' , ' D ' , ' D ' , ' R ' ] , [ ' R ' , ' D ' , ' R ' , ' F ' ] ] NEW_LINE cnt = countChanges ( arr ) NEW_LINE print ( cnt ) NEW_LINE
Check if a binary tree is sorted level | Python3 program to determine whether binary tree is level sorted or not . ; Function to create new tree node . ; Function to determine if given binary tree is level sorted or not . ; to store maximum value of previous level . ; to store minimum value of current level . ; to store maximum value of current level . ; to store number of nodes in current level . ; queue to perform level order traversal . ; find number of nodes in current level . ; traverse current level and find minimum and maximum value of this level . ; if minimum value of this level is not greater than maximum value of previous level then given tree is not level sorted . ; maximum value of this level is previous maximum value for next level . ; Driver Code ; 1 / 4 \ 6 / \ 8 9 / \ 12 10
from queue import Queue NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def isSorted ( root ) : NEW_LINE INDENT prevMax = - 999999999999 NEW_LINE minval = None NEW_LINE maxval = None NEW_LINE levelSize = None NEW_LINE q = Queue ( ) NEW_LINE q . put ( root ) NEW_LINE while ( not q . empty ( ) ) : NEW_LINE INDENT levelSize = q . qsize ( ) NEW_LINE minval = 999999999999 NEW_LINE maxval = - 999999999999 NEW_LINE while ( levelSize > 0 ) : NEW_LINE INDENT root = q . queue [ 0 ] NEW_LINE q . get ( ) NEW_LINE levelSize -= 1 NEW_LINE minval = min ( minval , root . key ) NEW_LINE maxval = max ( maxval , root . key ) NEW_LINE if ( root . left ) : NEW_LINE INDENT q . put ( root . left ) NEW_LINE DEDENT if ( root . right ) : NEW_LINE INDENT q . put ( root . right ) NEW_LINE DEDENT DEDENT if ( minval <= prevMax ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT prevMax = maxval NEW_LINE DEDENT return 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 8 ) NEW_LINE root . left . right . right = newNode ( 9 ) NEW_LINE root . left . right . left . left = newNode ( 12 ) NEW_LINE root . left . right . right . right = newNode ( 10 ) NEW_LINE if ( isSorted ( root ) ) : NEW_LINE INDENT print ( " Sorted " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ sorted " ) NEW_LINE DEDENT DEDENT
Smallest subsequence having GCD equal to GCD of given array | Python3 program to implement the above approach ; Function to print the smallest subsequence that satisfies the condition ; Stores gcd of the array . ; Traverse the given array ; Update gcdArr ; Traverse the given array . ; If current element equal to gcd of array . ; Generate all possible pairs . ; If gcd of current pair equal to gcdArr ; Print current pair of the array ; Driver Code
import math NEW_LINE def printSmallSub ( arr , N ) : NEW_LINE INDENT gcdArr = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT gcdArr = math . gcd ( gcdArr , arr [ i ] ) NEW_LINE DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT if ( arr [ i ] == gcdArr ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( math . gcd ( arr [ i ] , arr [ j ] ) == gcdArr ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE print ( arr [ j ] , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 6 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE printSmallSub ( arr , N ) NEW_LINE DEDENT
Count pairs in an array whose product is composite number | Python3 program to implement the above approach ; Function to get all the prime numbers in the range [ 1 , X ] ; Stores the boolean value to check if a number is prime or not ; Mark all non prime numbers as false ; If i is prime number ; Mark j as a composite number ; Function to get the count of pairs whose product is a composite number ; Stores the boolean value to check if a number is prime or not ; Stores the count of 1 s ; Stores the count of prime numbers ; Traverse the given array . ; Stores count of pairs whose product is not a composite number ; Stores the count of pairs whose product is composite number ; Driver Code
X = 1000000 NEW_LINE def getPrimeNum ( ) : NEW_LINE INDENT isPrime = [ True ] * ( X ) NEW_LINE isPrime [ 0 ] = False NEW_LINE isPrime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i <= X : NEW_LINE INDENT if ( isPrime [ i ] == True ) : NEW_LINE INDENT for j in range ( i * i , X , i ) : NEW_LINE INDENT isPrime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return isPrime NEW_LINE DEDENT def cntPairs ( arr , N ) : NEW_LINE INDENT isPrime = getPrimeNum ( ) NEW_LINE cntOne = 0 NEW_LINE cntPrime = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT cntOne += 1 NEW_LINE DEDENT elif ( isPrime [ i ] ) : NEW_LINE INDENT cntPrime += 1 NEW_LINE DEDENT DEDENT cntNonComp = 0 NEW_LINE cntNonComp = ( cntPrime * cntOne + cntOne * ( cntOne - 1 ) // 2 ) NEW_LINE res = 0 NEW_LINE res = ( N * ( N - 1 ) // 2 - cntNonComp ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 2 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntPairs ( arr , N ) ) NEW_LINE DEDENT
Find the greater number closest to N having at most one non | Python3 program to implement the above approach ; Function to calculate X ^ n in log ( n ) ; Stores the value of X ^ n ; If N is odd ; Function to find the closest number > N having at most 1 non - zero digit ; Stores the count of digits in N ; Stores the power of 10 ^ ( n - 1 ) ; Stores the last ( n - 1 ) digits ; Store the answer ; Driver Code ; Function call
import math NEW_LINE def power ( X , n ) : NEW_LINE INDENT res = 1 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( n & 1 != 0 ) : 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 closestgtNum ( N ) : NEW_LINE INDENT n = int ( math . log10 ( N ) + 1 ) NEW_LINE P = power ( 10 , n - 1 ) NEW_LINE Y = N % P NEW_LINE res = N + ( P - Y ) NEW_LINE return res NEW_LINE DEDENT N = 120 NEW_LINE print ( closestgtNum ( N ) ) NEW_LINE
Rearrange an array to make similar indexed elements different from that of another array | Function to find the arrangement of array B [ ] such that element at each index of A [ ] and B [ ] are not equal ; Length of array ; Print not possible , if arrays only have single equal element ; Reverse array B ; Traverse over arrays to check if there is any index where A [ i ] and B [ i ] are equal ; Swap B [ i ] with B [ i - 1 ] ; Break the loop ; Print required arrangement of array B ; Given arrays A [ ] and B [ ] ; Function call
def RearrangeB ( A , B ) : NEW_LINE INDENT n = len ( A ) NEW_LINE if ( n == 1 and A [ 0 ] == B [ 0 ] ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT for i in range ( n // 2 ) : NEW_LINE INDENT t = B [ i ] NEW_LINE B [ i ] = B [ n - i - 1 ] NEW_LINE B [ n - i - 1 ] = t NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( B [ i ] == A [ i ] ) : NEW_LINE INDENT B [ i ] , B [ i - 1 ] = B [ i - 1 ] , B [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT for k in B : NEW_LINE INDENT print ( k , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = [ 2 , 4 , 5 , 8 ] NEW_LINE B = [ 2 , 4 , 5 , 8 ] NEW_LINE RearrangeB ( A , B ) NEW_LINE