codeparrot/xlcost-text-to-code huggingface dataset:
Range Queries for Longest Correct Bracket Subsequence Set | 2 | Function for precomputation ; Create a stack and push - 1 as initial index to it . ; Traverse all characters of given String ; If opening bracket , push index of it ; If closing bracket , i . e . , str [ i ] = ')  ; If closing bracket , i . e . , str [ i ] = ' ) ' && stack is not empty then mark both " open ▁ & ▁ close " bracket indexs as 1 . Pop the previous opening bracket 's index ; If stack is empty . ; Function return output of each query in O ( 1 ) ; Driver code
def constructBlanceArray ( BOP , BCP , str , n ) : NEW_LINE INDENT stk = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == ' ( ' ) : NEW_LINE INDENT stk . append ( i ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( len ( stk ) != 0 ) : NEW_LINE INDENT BCP [ i ] = 1 ; NEW_LINE BOP [ stk [ - 1 ] ] = 1 ; NEW_LINE stk . pop ( ) ; NEW_LINE DEDENT else : NEW_LINE INDENT BCP [ i ] = 0 ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT BCP [ i ] += BCP [ i - 1 ] ; NEW_LINE BOP [ i ] += BOP [ i - 1 ] ; NEW_LINE DEDENT DEDENT def query ( BOP , BCP , s , e ) : NEW_LINE INDENT if ( BOP [ s - 1 ] == BOP [ s ] ) : NEW_LINE INDENT return ( BCP [ e ] - BOP [ s ] ) * 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( BCP [ e ] - BOP [ s ] + 1 ) * 2 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT string = " ( ) ) ( ( ) ) ( ( ) ) ( " ; NEW_LINE n = len ( string ) NEW_LINE BCP = [ 0 for i in range ( n + 1 ) ] ; NEW_LINE BOP = [ 0 for i in range ( n + 1 ) ] ; NEW_LINE constructBlanceArray ( BOP , BCP , string , n ) ; NEW_LINE startIndex = 5 NEW_LINE endIndex = 11 ; NEW_LINE print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + str ( startIndex ) + " ▁ and ▁ " + str ( endIndex ) + " ▁ = ▁ " + str ( query ( BOP , BCP , startIndex , endIndex ) ) ) ; NEW_LINE startIndex = 4 ; NEW_LINE endIndex = 5 ; NEW_LINE print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + str ( startIndex ) + " ▁ and ▁ " + str ( endIndex ) + " ▁ = ▁ " + str ( query ( BOP , BCP , startIndex , endIndex ) ) ) NEW_LINE startIndex = 1 ; NEW_LINE endIndex = 5 ; NEW_LINE print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + str ( startIndex ) + " ▁ and ▁ " + str ( endIndex ) + " ▁ = ▁ " + str ( query ( BOP , BCP , startIndex , endIndex ) ) ) ; NEW_LINE DEDENT
HeapSort | To heapify subtree rooted at index i . n is size of heap ; Initialize largest as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; See if left child of root exists and is greater than root ; See if right child of root exists and is greater than root ; Change root , if needed ; Heapify the root . ; The main function to sort an array of given size ; Build a maxheap . ; One by one extract elements ; swap ; call max heapify on the reduced heap ; Driver code
def heapify ( arr , n , i ) : NEW_LINE INDENT largest = i NEW_LINE l = 2 * i + 1 NEW_LINE r = 2 * i + 2 NEW_LINE if l < n and arr [ largest ] < arr [ l ] : NEW_LINE INDENT largest = l NEW_LINE DEDENT if r < n and arr [ largest ] < arr [ r ] : NEW_LINE INDENT largest = r NEW_LINE DEDENT if largest != i : NEW_LINE INDENT arr [ i ] , arr [ largest ] = arr [ largest ] , arr [ i ] NEW_LINE heapify ( arr , n , largest ) NEW_LINE DEDENT DEDENT def heapSort ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE for i in range ( n // 2 - 1 , - 1 , - 1 ) : NEW_LINE INDENT heapify ( arr , n , i ) NEW_LINE DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT arr [ i ] , arr [ 0 ] = arr [ 0 ] , arr [ i ] NEW_LINE heapify ( arr , i , 0 ) NEW_LINE DEDENT DEDENT arr = [ 12 , 11 , 13 , 5 , 6 , 7 ] NEW_LINE heapSort ( arr ) NEW_LINE n = len ( arr ) NEW_LINE print ( " Sorted ▁ array ▁ is " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( " % d " % arr [ i ] ) , NEW_LINE DEDENT
Iterative HeapSort | function build Max Heap where value of each child is always smaller than value of their parent ; if child is bigger than parent ; swap child and parent until parent is smaller ; swap value of first indexed with last indexed ; maintaining heap property after each swapping ; if left child is smaller than right child point index variable to right child ; if parent is smaller than child then swapping parent with child having higher value ; Driver Code
def buildMaxHeap ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] > arr [ int ( ( i - 1 ) / 2 ) ] : NEW_LINE INDENT j = i NEW_LINE while arr [ j ] > arr [ int ( ( j - 1 ) / 2 ) ] : NEW_LINE INDENT ( arr [ j ] , arr [ int ( ( j - 1 ) / 2 ) ] ) = ( arr [ int ( ( j - 1 ) / 2 ) ] , arr [ j ] ) NEW_LINE j = int ( ( j - 1 ) / 2 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def heapSort ( arr , n ) : NEW_LINE INDENT buildMaxHeap ( arr , n ) NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT arr [ 0 ] , arr [ i ] = arr [ i ] , arr [ 0 ] NEW_LINE j , index = 0 , 0 NEW_LINE while True : NEW_LINE INDENT index = 2 * j + 1 NEW_LINE if ( index < ( i - 1 ) and arr [ index ] < arr [ index + 1 ] ) : NEW_LINE INDENT index += 1 NEW_LINE DEDENT if index < i and arr [ j ] < arr [ index ] : NEW_LINE INDENT arr [ j ] , arr [ index ] = arr [ index ] , arr [ j ] NEW_LINE DEDENT j = index NEW_LINE if index >= i : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 20 , 15 , 17 , 9 , 21 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ array : ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE heapSort ( arr , n ) NEW_LINE print ( " Sorted ▁ array : ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Flip Binary Tree | Python3 program to flip a binary tree ; A binary tree node structure ; method to flip the binary tree ; Initialization of pointers ; Iterate through all left nodes ; Swapping nodes now , need temp to keep the previous right child Making prev ' s ▁ right ▁ as ▁ curr ' s left child ; Storing curr 's right child ; Making prev as curr 's right  child ; Iterative method to do level order traversal line by line ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; nodeCount ( queue size ) indicates number of nodes at current level . ; Dequeue all nodes of current level and Enqueue all nodes of next level ; Driver code
from collections import deque NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def flipBinaryTree ( root ) : NEW_LINE INDENT curr = root NEW_LINE next = None NEW_LINE temp = None NEW_LINE prev = None NEW_LINE while ( curr ) : NEW_LINE INDENT next = curr . left NEW_LINE curr . left = temp NEW_LINE temp = curr . right NEW_LINE curr . right = prev NEW_LINE prev = curr NEW_LINE curr = next NEW_LINE DEDENT return prev NEW_LINE DEDENT def printLevelOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT q = deque ( ) NEW_LINE q . append ( root ) NEW_LINE while ( 1 ) : NEW_LINE INDENT nodeCount = len ( q ) NEW_LINE if ( nodeCount == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT while ( nodeCount > 0 ) : NEW_LINE INDENT node = q . popleft ( ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE if ( node . left != None ) : NEW_LINE INDENT q . append ( node . left ) NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT q . append ( node . right ) NEW_LINE DEDENT nodeCount -= 1 NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . right . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 5 ) NEW_LINE print ( " Level ▁ order ▁ traversal ▁ of ▁ given ▁ tree " ) NEW_LINE printLevelOrder ( root ) NEW_LINE root = flipBinaryTree ( root ) NEW_LINE print ( " Level order traversal of the flipped " ▁ " tree " ) NEW_LINE printLevelOrder ( root ) NEW_LINE DEDENT
Foldable Binary Trees | Utility function to create a new tree node ; Returns true if the given tree can be folded ; A utility function that checks if trees with roots as n1 and n2 are mirror of each other ; If both left and right subtrees are NULL , then return true ; If one of the trees is NULL and other is not , then return false ; Otherwise check if left and right subtrees are mirrors of their counterparts ; Driver code ; The constructed binary tree is 1 / \ 2 3 \ / 4 5
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def IsFoldable ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return True NEW_LINE DEDENT return IsFoldableUtil ( root . left , root . right ) NEW_LINE DEDENT def IsFoldableUtil ( n1 , n2 ) : NEW_LINE INDENT if n1 == None and n2 == None : NEW_LINE INDENT return True NEW_LINE DEDENT if n1 == None or n2 == None : NEW_LINE INDENT return False NEW_LINE DEDENT d1 = IsFoldableUtil ( n1 . left , n2 . right ) NEW_LINE d2 = IsFoldableUtil ( n1 . right , n2 . left ) NEW_LINE return d1 and d2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 4 ) NEW_LINE root . right . left = newNode ( 5 ) NEW_LINE if IsFoldable ( root ) : NEW_LINE INDENT print ( " Tree ▁ is ▁ foldable " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Tree ▁ is ▁ not ▁ foldable " ) NEW_LINE DEDENT DEDENT
Check for Children Sum Property in a Binary Tree | returns 1 if children sum property holds for the given node and both of its children ; left_data is left child data and right_data is for right child data ; If node is None or it 's a leaf   node then return true  ; If left child is not present then 0 is used as data of left child ; If right child is not present then 0 is used as data of right child ; if the node and both of its children satisfy the property return 1 else 0 ; Helper class that allocates a new node with the given data and None left and right pointers . ; Driver Code
def isSumProperty ( node ) : NEW_LINE INDENT left_data = 0 NEW_LINE right_data = 0 NEW_LINE if ( node == None or ( node . left == None and node . right == None ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( node . left != None ) : NEW_LINE INDENT left_data = node . left . data NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT right_data = node . right . data NEW_LINE DEDENT if ( ( node . data == left_data + right_data ) and isSumProperty ( node . left ) and isSumProperty ( node . right ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 8 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 2 ) NEW_LINE if ( isSumProperty ( root ) ) : NEW_LINE INDENT print ( " The ▁ given ▁ tree ▁ satisfies ▁ the " , " children ▁ sum ▁ property ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ given ▁ tree ▁ does ▁ not ▁ satisfy " , " the ▁ children ▁ sum ▁ property ▁ " ) NEW_LINE DEDENT DEDENT
How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; If a leaf node ; If an internal node and is greater than its children , and same is recursively true for the children ; Driver Code
def isHeap ( arr , i , n ) : NEW_LINE INDENT if i >= int ( ( n - 2 ) / 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( arr [ i ] >= arr [ 2 * i + 1 ] and arr [ i ] >= arr [ 2 * i + 2 ] and isHeap ( arr , 2 * i + 1 , n ) and isHeap ( arr , 2 * i + 2 , n ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ] NEW_LINE n = len ( arr ) - 1 NEW_LINE if isHeap ( arr , 0 , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
How to check if a given array represents a Binary Heap ? | Returns true if arr [ i . . n - 1 ] represents a max - heap ; Start from root and go till the last internal node ; If left child is greater , return false ; If right child is greater , return false ; Driver Code
def isHeap ( arr , n ) : NEW_LINE INDENT for i in range ( int ( ( n - 2 ) / 2 ) + 1 ) : NEW_LINE INDENT if arr [ 2 * i + 1 ] > arr [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT if ( 2 * i + 2 < n and arr [ 2 * i + 2 ] > arr [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE if isHeap ( arr , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Connect n ropes with minimum cost | Python3 program to connect n ropes with minimum cost ; Create a priority queue out of the given list ; Initializ result ; While size of priority queue is more than 1 ; Extract shortest two ropes from arr ; Connect the ropes : update result and insert the new rope to arr ; Driver code
import heapq NEW_LINE def minCost ( arr , n ) : NEW_LINE INDENT heapq . heapify ( arr ) NEW_LINE res = 0 NEW_LINE while ( len ( arr ) > 1 ) : NEW_LINE INDENT first = heapq . heappop ( arr ) NEW_LINE second = heapq . heappop ( arr ) NEW_LINE res += first + second NEW_LINE heapq . heappush ( arr , first + second ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT lengths = [ 4 , 3 , 2 , 6 ] NEW_LINE size = len ( lengths ) NEW_LINE print ( " Total ▁ cost ▁ for ▁ connecting ▁ ropes ▁ is ▁ " + str ( minCost ( lengths , size ) ) ) NEW_LINE DEDENT
Smallest Derangement of Sequence | Python3 program to generate smallest derangement using priority queue . ; Generate Sequence and insert into a priority queue . ; Generate Least Derangement ; Print Derangement ; Driver code
def generate_derangement ( N ) : NEW_LINE INDENT S = [ i for i in range ( N + 1 ) ] NEW_LINE PQ = [ ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT PQ . append ( S [ i ] ) NEW_LINE DEDENT D = [ 0 ] * ( N + 1 ) NEW_LINE PQ . sort ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT PQ . sort ( ) NEW_LINE d = PQ [ 0 ] NEW_LINE del PQ [ 0 ] NEW_LINE if ( d != S [ i ] ) or ( i == N ) : NEW_LINE INDENT D [ i ] = d NEW_LINE DEDENT else : NEW_LINE INDENT PQ . sort ( ) NEW_LINE D [ i ] = PQ [ 0 ] NEW_LINE del PQ [ 0 ] NEW_LINE PQ . append ( d ) NEW_LINE DEDENT DEDENT if D [ N ] == S [ N ] : NEW_LINE INDENT t = D [ N - 1 ] NEW_LINE D [ N - 1 ] = D [ N ] NEW_LINE D [ N ] = t NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( D [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT generate_derangement ( 10 ) NEW_LINE
Smallest Derangement of Sequence | Efficient Python3 program to find smallest derangement . ; Generate Sequence S ; Generate Derangement ; Only if i is odd Swap S [ N - 1 ] and S [ N ] ; Print Derangement ; Driver Code
def generate_derangement ( N ) : NEW_LINE INDENT S = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT S [ i ] = i NEW_LINE DEDENT D = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 , 2 ) : NEW_LINE INDENT if i == N : NEW_LINE INDENT D [ N ] = S [ N - 1 ] NEW_LINE D [ N - 1 ] = S [ N ] NEW_LINE DEDENT else : NEW_LINE INDENT D [ i ] = i + 1 NEW_LINE D [ i + 1 ] = i NEW_LINE DEDENT DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( D [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT generate_derangement ( 10 ) NEW_LINE DEDENT
Largest Derangement of a Sequence | Python3 program to find the largest derangement ; Stores result ; Insert all elements into a priority queue ; Fill Up res [ ] from left to right ; New Element poped equals the element in original sequence . Get the next largest element ; If given sequence is in descending order then we need to swap last two elements again ; Driver code
def printLargest ( seq , N ) : NEW_LINE INDENT res = [ 0 ] * N NEW_LINE pq = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT pq . append ( seq [ i ] ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT pq . sort ( ) NEW_LINE pq . reverse ( ) NEW_LINE d = pq [ 0 ] NEW_LINE del pq [ 0 ] NEW_LINE if ( d != seq [ i ] or i == N - 1 ) : NEW_LINE INDENT res [ i ] = d NEW_LINE DEDENT else : NEW_LINE INDENT res [ i ] = pq [ 0 ] NEW_LINE del pq [ 0 ] NEW_LINE pq . append ( d ) NEW_LINE DEDENT DEDENT if ( res [ N - 1 ] == seq [ N - 1 ] ) : NEW_LINE INDENT res [ N - 1 ] = res [ N - 2 ] NEW_LINE res [ N - 2 ] = seq [ N - 1 ] NEW_LINE DEDENT print ( " Largest ▁ Derangement " ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT seq = [ 92 , 3 , 52 , 13 , 2 , 31 , 1 ] NEW_LINE n = len ( seq ) NEW_LINE printLargest ( seq , n ) NEW_LINE
Program to calculate Profit Or Loss | Function to calculate Profit . ; Function to calculate Loss . ; Driver code
def Profit ( costPrice , sellingPrice ) : NEW_LINE INDENT profit = ( sellingPrice - costPrice ) NEW_LINE return profit NEW_LINE DEDENT def Loss ( costPrice , sellingPrice ) : NEW_LINE INDENT Loss = ( costPrice - sellingPrice ) NEW_LINE return Loss NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT costPrice , sellingPrice = 1500 , 2000 NEW_LINE if sellingPrice == costPrice : NEW_LINE INDENT print ( " No ▁ profit ▁ nor ▁ Loss " ) NEW_LINE DEDENT elif sellingPrice > costPrice : NEW_LINE INDENT print ( Profit ( costPrice , sellingPrice ) , " Profit " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( Loss ( costPrice , sellingPrice ) , " Loss " ) NEW_LINE DEDENT DEDENT
Find the Next perfect square greater than a given number | Python3 implementation of above approach ; Function to find the next perfect square ; Driver Code
import math NEW_LINE def nextPerfectSquare ( N ) : NEW_LINE INDENT nextN = math . floor ( math . sqrt ( N ) ) + 1 NEW_LINE return nextN * nextN NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 35 NEW_LINE print ( nextPerfectSquare ( N ) ) NEW_LINE DEDENT
Print all substring of a number without any conversion | Python3 implementation of above approach ; Function to print the substrings of a number ; Calculate the total number of digits ; 0.5 has been added because of it will return double value like 99.556 ; Print all the numbers from starting position ; Update the no . ; Update the no . of digits ; Driver code
import math NEW_LINE def printSubstrings ( n ) : NEW_LINE INDENT s = int ( math . log10 ( n ) ) ; NEW_LINE d = ( math . pow ( 10 , s ) ) ; NEW_LINE k = d ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT while ( d > 0 ) : NEW_LINE INDENT print ( int ( n // d ) ) ; NEW_LINE d = int ( d / 10 ) ; NEW_LINE DEDENT n = int ( n % k ) ; NEW_LINE k = int ( k // 10 ) ; NEW_LINE d = k ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 123 ; NEW_LINE printSubstrings ( n ) ; NEW_LINE DEDENT
Modulo power for large numbers represented as strings | Python3 program to find ( a ^ b ) % MOD where a and b may be very large and represented as strings . ; Returns modulo exponentiation for two numbers represented as long long int . It is used by powerStrings ( ) . Its complexity is log ( n ) ; Returns modulo exponentiation for two numbers represented as strings . It is used by powerStrings ( ) ; We convert strings to number ; calculating a % MOD ; calculating b % ( MOD - 1 ) ; Now a and b are long long int . We calculate a ^ b using modulo exponentiation ; As numbers are very large that is it may contains upto 10 ^ 6 digits . So , we use string .
MOD = 1000000007 ; NEW_LINE def powerLL ( x , n ) : NEW_LINE INDENT result = 1 ; NEW_LINE while ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT result = result * x % MOD ; NEW_LINE DEDENT n = int ( n / 2 ) ; NEW_LINE x = x * x % MOD ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def powerStrings ( sa , sb ) : NEW_LINE INDENT a = 0 ; NEW_LINE b = 0 ; NEW_LINE for i in range ( len ( sa ) ) : NEW_LINE INDENT a = ( a * 10 + ( ord ( sa [ i ] ) - ord ( '0' ) ) ) % MOD ; NEW_LINE DEDENT for i in range ( len ( sb ) ) : NEW_LINE INDENT b = ( b * 10 + ( ord ( sb [ i ] ) - ord ( '0' ) ) ) % ( MOD - 1 ) ; NEW_LINE DEDENT return powerLL ( a , b ) ; NEW_LINE DEDENT sa = "2" ; NEW_LINE sb = "3" ; NEW_LINE print ( powerStrings ( sa , sb ) ) ; NEW_LINE
Check if a number can be expressed as 2 ^ x + 2 ^ y | Utility function to check if a number is power of 2 or not ; Utility function to determine the value of previous power of 2 ; function to check if n can be expressed as 2 ^ x + 2 ^ y or not ; if value of n is 0 or 1 it can not be expressed as 2 ^ x + 2 ^ y ; if n is power of two then it can be expressed as sum of 2 ^ x + 2 ^ y ; if the remainder after subtracting previous power of 2 is also a power of 2 then it can be expressed as 2 ^ x + 2 ^ y ; driver code
def isPowerOfTwo ( n ) : NEW_LINE INDENT return ( n and ( not ( n & ( n - 1 ) ) ) ) NEW_LINE DEDENT def previousPowerOfTwo ( n ) : NEW_LINE INDENT while ( n & n - 1 ) : NEW_LINE INDENT n = n & n - 1 NEW_LINE DEDENT return n NEW_LINE DEDENT def checkSum ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( isPowerOfTwo ( n ) ) : NEW_LINE INDENT print ( n // 2 , n // 2 ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT x = previousPowerOfTwo ( n ) NEW_LINE y = n - x ; NEW_LINE if ( isPowerOfTwo ( y ) ) : NEW_LINE INDENT print ( x , y ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT n1 = 20 NEW_LINE if ( checkSum ( n1 ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT n2 = 11 NEW_LINE if ( checkSum ( n2 ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
10 's Complement of a decimal number | Python3 program to find 10 's complement ; Function to find 10 's complement ; Calculating total digits in num ; restore num ; calculate 10 's complement ; Driver code
import math NEW_LINE def complement ( num ) : NEW_LINE INDENT i = 0 ; NEW_LINE len = 0 ; NEW_LINE comp = 0 ; NEW_LINE temp = num ; NEW_LINE while ( 1 ) : NEW_LINE INDENT len += 1 ; NEW_LINE num = int ( num / 10 ) ; NEW_LINE if ( abs ( num ) == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT num = temp ; NEW_LINE comp = math . pow ( 10 , len ) - num ; NEW_LINE return int ( comp ) ; NEW_LINE DEDENT print ( complement ( 25 ) ) ; NEW_LINE print ( complement ( 456 ) ) ; NEW_LINE
Program to find HCF ( Highest Common Factor ) of 2 Numbers | Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; Driver program to test above function
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 and b == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return gcd ( a - b , b ) NEW_LINE DEDENT return gcd ( a , b - a ) NEW_LINE DEDENT a = 98 NEW_LINE b = 56 NEW_LINE if ( gcd ( a , b ) ) : NEW_LINE INDENT print ( ' GCD ▁ of ' , a , ' and ' , b , ' is ' , gcd ( a , b ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' not ▁ found ' ) NEW_LINE DEDENT