codeparrot/xlcost-text-to-code huggingface dataset:
Longest Uncommon Subsequence | Python program to find longest uncommon subsequence using naive method ; function to calculate length of longest uncommon subsequence ; Case 1 : If strings are equal ; for case 2 and case 3 ; input strings
import math NEW_LINE def findLUSlength ( a , b ) : NEW_LINE INDENT if ( a == b ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return max ( len ( a ) , len ( b ) ) NEW_LINE DEDENT a = " abcdabcd " NEW_LINE b = " abcabc " NEW_LINE print ( findLUSlength ( a , b ) ) NEW_LINE
Interchanging first and second halves of strings | Function to concatenate two different halves of given strings ; Creating new strings by exchanging the first half of a and b . Please refer below for details of substr . https : www . geeksforgeeks . org / stdsubstr - in - ccpp / ; Driver function ;
def swapTwoHalves ( a , b ) : NEW_LINE INDENT la = len ( a ) NEW_LINE lb = len ( b ) NEW_LINE c = a [ 0 : la // 2 ] + b [ lb // 2 : lb ] NEW_LINE d = b [ 0 : lb // 2 ] + a [ la // 2 : la ] NEW_LINE print ( c ,   " " , d ) NEW_LINE DEDENT a = " remuneration " NEW_LINE b = " day " NEW_LINE / * Calling function * / NEW_LINE swapTwoHalves ( a , b ) NEW_LINE
Magical Indices in an array | Function to count number of magical indices . ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Initialize the arrays . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code
def solve ( A , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE parent = [ None ] * ( n + 1 ) NEW_LINE vis = [ None ] * ( n + 1 ) NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT parent [ i ] = - 1 NEW_LINE vis [ i ] = 0 NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT j = i NEW_LINE if ( parent [ j ] == - 1 ) : NEW_LINE INDENT while ( parent [ j ] == - 1 ) : NEW_LINE INDENT parent [ j ] = i NEW_LINE j = ( j + A [ j ] + 1 ) % n NEW_LINE DEDENT if ( parent [ j ] == i ) : NEW_LINE INDENT while ( vis [ j ] == 0 ) : NEW_LINE INDENT vis [ j ] = 1 NEW_LINE cnt = cnt + 1 NEW_LINE j = ( j + A [ j ] + 1 ) % n NEW_LINE DEDENT DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT A = [ 0 , 0 , 0 , 2 ] NEW_LINE n = len ( A ) NEW_LINE print ( solve ( A , n ) ) NEW_LINE
Longest path between any pair of vertices | visited [ ] array to make nodes visited src is starting node for DFS traversal prev_len is sum of cable length till current node max_len is pointer which stores the maximum length of cable value after DFS traversal ; Mark the src node visited ; curr_len is for length of cable from src city to its adjacent city ; Adjacent is pair type which stores destination city and cable length ; Traverse all adjacent ; Adjacent element ; If node or city is not visited ; Total length of cable from src city to its adjacent ; Call DFS for adjacent city ; If total cable length till now greater than previous length then update it ; make curr_len = 0 for next adjacent ; n is number of cities or nodes in graph cable_lines is total cable_lines among the cities or edges in graph ; maximum length of cable among the connected cities ; call DFS for each city to find maximum length of cable ; initialize visited array with 0 ; Call DFS for src vertex i ; Driver Code ; n is number of cities ; create undirected graph first edge ; second edge ; third edge ; fourth edge ; fifth edge
def DFS ( graph , src , prev_len , max_len , visited ) : NEW_LINE INDENT visited [ src ] = 1 NEW_LINE curr_len = 0 NEW_LINE adjacent = None NEW_LINE for i in range ( len ( graph [ src ] ) ) : NEW_LINE INDENT adjacent = graph [ src ] [ i ] NEW_LINE if ( not visited [ adjacent [ 0 ] ] ) : NEW_LINE INDENT curr_len = prev_len + adjacent [ 1 ] NEW_LINE DFS ( graph , adjacent [ 0 ] , curr_len , max_len , visited ) NEW_LINE DEDENT if ( max_len [ 0 ] < curr_len ) : NEW_LINE INDENT max_len [ 0 ] = curr_len NEW_LINE DEDENT curr_len = 0 NEW_LINE DEDENT DEDENT def longestCable ( graph , n ) : NEW_LINE INDENT max_len = [ - 999999999999 ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT visited = [ False ] * ( n + 1 ) NEW_LINE DFS ( graph , i , 0 , max_len , visited ) NEW_LINE DEDENT return max_len [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE graph = [ [ ] for i in range ( n + 1 ) ] NEW_LINE graph [ 1 ] . append ( [ 2 , 3 ] ) NEW_LINE graph [ 2 ] . append ( [ 1 , 3 ] ) NEW_LINE graph [ 2 ] . append ( [ 3 , 4 ] ) NEW_LINE graph [ 3 ] . append ( [ 2 , 4 ] ) NEW_LINE graph [ 2 ] . append ( [ 6 , 2 ] ) NEW_LINE graph [ 6 ] . append ( [ 2 , 2 ] ) NEW_LINE graph [ 4 ] . append ( [ 6 , 6 ] ) NEW_LINE graph [ 6 ] . append ( [ 4 , 6 ] ) NEW_LINE graph [ 5 ] . append ( [ 6 , 5 ] ) NEW_LINE graph [ 6 ] . append ( [ 5 , 5 ] ) NEW_LINE print ( " Maximum ▁ length ▁ of ▁ cable ▁ = " , longestCable ( graph , n ) ) NEW_LINE DEDENT
Minimum cost to connect all cities | Function to find out minimum valued node among the nodes which are not yet included in MST ; Loop through all the values of the nodes which are not yet included in MST and find the minimum valued one . ; Function to find out the MST and the cost of the MST . ; Array to store the parent node of a particular node . ; Array to store key value of each node . ; Boolean Array to hold bool values whether a node is included in MST or not . ; Set all the key values to infinite and none of the nodes is included in MST . ; Start to find the MST from node 0. Parent of node 0 is none so set - 1. key value or minimum cost to reach 0 th node from 0 th node is 0. ; Find the rest n - 1 nodes of MST . ; First find out the minimum node among the nodes which are not yet included in MST . ; Now the uth node is included in MST . ; Update the values of neighbor nodes of u which are not yet included in MST . ; Find out the cost by adding the edge values of MST . ; Driver Code ; Input 1 ; Input 2
def minnode ( n , keyval , mstset ) : NEW_LINE INDENT mini = 999999999999 NEW_LINE mini_index = None NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( mstset [ i ] == False and keyval [ i ] < mini ) : NEW_LINE INDENT mini = keyval [ i ] NEW_LINE mini_index = i NEW_LINE DEDENT DEDENT return mini_index NEW_LINE DEDENT def findcost ( n , city ) : NEW_LINE INDENT parent = [ None ] * n NEW_LINE keyval = [ None ] * n NEW_LINE mstset = [ None ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT keyval [ i ] = 9999999999999 NEW_LINE mstset [ i ] = False NEW_LINE DEDENT parent [ 0 ] = - 1 NEW_LINE keyval [ 0 ] = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT u = minnode ( n , keyval , mstset ) NEW_LINE mstset [ u ] = True NEW_LINE for v in range ( n ) : NEW_LINE INDENT if ( city [ u ] [ v ] and mstset [ v ] == False and city [ u ] [ v ] < keyval [ v ] ) : NEW_LINE INDENT keyval [ v ] = city [ u ] [ v ] NEW_LINE parent [ v ] = u NEW_LINE DEDENT DEDENT DEDENT cost = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT cost += city [ parent [ i ] ] [ i ] NEW_LINE DEDENT print ( cost ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n1 = 5 NEW_LINE city1 = [ [ 0 , 1 , 2 , 3 , 4 ] , [ 1 , 0 , 5 , 0 , 7 ] , [ 2 , 5 , 0 , 6 , 0 ] , [ 3 , 0 , 6 , 0 , 0 ] , [ 4 , 7 , 0 , 0 , 0 ] ] NEW_LINE findcost ( n1 , city1 ) NEW_LINE n2 = 6 NEW_LINE city2 = [ [ 0 , 1 , 1 , 100 , 0 , 0 ] , [ 1 , 0 , 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 0 , 0 , 0 ] , [ 100 , 0 , 0 , 0 , 2 , 2 ] , [ 0 , 0 , 0 , 2 , 0 , 2 ] , [ 0 , 0 , 0 , 2 , 2 , 0 ] ] NEW_LINE findcost ( n2 , city2 ) NEW_LINE DEDENT
Insertion in a Binary Tree in level order | A binary tree node has key , pointer to left child and a pointer to right child ; Inorder traversal of a binary tree ; function to insert element in binary tree ; Do level order traversal until we find an empty place . ; Driver code
class newNode ( ) : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . key = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def inorder ( temp ) : NEW_LINE INDENT if ( not temp ) : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( temp . left ) NEW_LINE print ( temp . key , end = " ▁ " ) NEW_LINE inorder ( temp . right ) NEW_LINE DEDENT def insert ( temp , key ) : NEW_LINE INDENT if not temp : NEW_LINE INDENT root = newNode ( key ) NEW_LINE return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( temp ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( not temp . left ) : NEW_LINE INDENT temp . left = newNode ( key ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( not temp . right ) : NEW_LINE INDENT temp . right = newNode ( key ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 11 ) NEW_LINE root . left . left = newNode ( 7 ) NEW_LINE root . right = newNode ( 9 ) NEW_LINE root . right . left = newNode ( 15 ) NEW_LINE root . right . right = newNode ( 8 ) NEW_LINE print ( " Inorder ▁ traversal ▁ before ▁ insertion : " , end = " ▁ " ) NEW_LINE inorder ( root ) NEW_LINE key = 12 NEW_LINE insert ( root , key ) NEW_LINE print ( ) NEW_LINE print ( " Inorder ▁ traversal ▁ after ▁ insertion : " , end = " ▁ " ) NEW_LINE inorder ( root ) NEW_LINE DEDENT
Tug of War | function that tries every possible solution by calling itself recursively ; checks whether the it is going out of bound ; checks that the numbers of elements left are not less than the number of elements required to form the solution ; consider the cases when current element is not included in the solution ; add the current element to the solution ; checks if a solution is formed ; checks if the solution formed is better than the best solution so far ; consider the cases where current element is included in the solution ; removes current element before returning to the caller of this function ; main function that generate an arr ; the boolean array that contains the inclusion and exclusion of an element in current set . The number excluded automatically form the other set ; The inclusion / exclusion array for final solution ; Find the solution using recursive function TOWUtil ( ) ; Print the solution ; Driver Code
def TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , min_diff , Sum , curr_sum , curr_position ) : NEW_LINE INDENT if ( curr_position == n ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( ( int ( n / 2 ) - no_of_selected_elements ) > ( n - curr_position ) ) : NEW_LINE INDENT return NEW_LINE DEDENT TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , min_diff , Sum , curr_sum , curr_position + 1 ) NEW_LINE no_of_selected_elements += 1 NEW_LINE curr_sum = curr_sum + arr [ curr_position ] NEW_LINE curr_elements [ curr_position ] = True NEW_LINE if ( no_of_selected_elements == int ( n / 2 ) ) : NEW_LINE INDENT if ( abs ( int ( Sum / 2 ) - curr_sum ) < min_diff [ 0 ] ) : NEW_LINE INDENT min_diff [ 0 ] = abs ( int ( Sum / 2 ) - curr_sum ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT soln [ i ] = curr_elements [ i ] NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , min_diff , Sum , curr_sum , curr_position + 1 ) NEW_LINE DEDENT curr_elements [ curr_position ] = False NEW_LINE DEDENT def tugOfWar ( arr , n ) : NEW_LINE INDENT curr_elements = [ None ] * n NEW_LINE soln = [ None ] * n NEW_LINE min_diff = [ 999999999999 ] NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE curr_elements [ i ] = soln [ i ] = False NEW_LINE DEDENT TOWUtil ( arr , n , curr_elements , 0 , soln , min_diff , Sum , 0 , 0 ) NEW_LINE print ( " The ▁ first ▁ subset ▁ is : ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( soln [ i ] == True ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE print ( " The ▁ second ▁ subset ▁ is : ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( soln [ i ] == False ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 23 , 45 , - 34 , 12 , 0 , 98 , - 99 , 4 , 189 , - 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE tugOfWar ( arr , n ) NEW_LINE DEDENT
The Knight 's tour problem | Backtracking | Python3 program to solve Knight Tour problem using Backtracking Chessboard Size ; A utility function to check if i , j are valid indexes for N * N chessboard ; A utility function to print Chessboard matrix ; This function solves the Knight Tour problem using Backtracking . This function mainly uses solveKTUtil ( ) to solve the problem . It returns false if no complete tour is possible , otherwise return true and prints the tour . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; Initialization of Board matrix ; move_x and move_y define next move of Knight . move_x is for next value of x coordinate move_y is for next value of y coordinate ; Since the Knight is initially at the first block ; Checking if solution exists or not ; A recursive utility function to solve Knight Tour problem ; Try all next moves from the current coordinate x , y ; Backtracking ; Driver Code ; Function Call
n = 8 NEW_LINE def isSafe ( x , y , board ) : NEW_LINE INDENT if ( x >= 0 and y >= 0 and x < n and y < n and board [ x ] [ y ] == - 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def printSolution ( n , board ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( board [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def solveKT ( n ) : NEW_LINE INDENT board = [ [ - 1 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE move_x = [ 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 ] NEW_LINE move_y = [ 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 ] NEW_LINE board [ 0 ] [ 0 ] = 0 NEW_LINE pos = 1 NEW_LINE if ( not solveKTUtil ( n , board , 0 , 0 , move_x , move_y , pos ) ) : NEW_LINE INDENT print ( " Solution ▁ does ▁ not ▁ exist " ) NEW_LINE DEDENT else : NEW_LINE INDENT printSolution ( n , board ) NEW_LINE DEDENT DEDENT def solveKTUtil ( n , board , curr_x , curr_y , move_x , move_y , pos ) : NEW_LINE INDENT if ( pos == n ** 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 8 ) : NEW_LINE INDENT new_x = curr_x + move_x [ i ] NEW_LINE new_y = curr_y + move_y [ i ] NEW_LINE if ( isSafe ( new_x , new_y , board ) ) : NEW_LINE INDENT board [ new_x ] [ new_y ] = pos NEW_LINE if ( solveKTUtil ( n , board , new_x , new_y , move_x , move_y , pos + 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT board [ new_x ] [ new_y ] = - 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT solveKT ( n ) NEW_LINE DEDENT
Rat in a Maze | Backtracking | Maze size ; A utility function to print solution matrix sol ; A utility function to check if x , y is valid index for N * N Maze ; if ( x , y outside maze ) return false ; This function solves the Maze problem using Backtracking . It mainly uses solveMazeUtil ( ) to solve the problem . It returns false if no path is possible , otherwise return true and prints the path in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasable solutions . ; A recursive utility function to solve Maze problem ; if ( x , y is goal ) return True ; Check if maze [ x ] [ y ] is valid ; Check if the current block is already part of solution path . ; mark x , y as part of solution path ; Move forward in x direction ; If moving in x direction doesn 't give solution  then Move down in y direction ; If moving in y direction doesn 't give solution then  Move back in x direction ; If moving in backwards in x direction doesn 't give solution  then Move upwards in y direction ; If none of the above movements work then BACKTRACK : unmark x , y as part of solution path ; Driver program to test above function
N = 4 NEW_LINE def printSolution ( sol ) : NEW_LINE INDENT for i in sol : NEW_LINE INDENT for j in i : NEW_LINE INDENT print ( str ( j ) + " ▁ " , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT def isSafe ( maze , x , y ) : NEW_LINE INDENT if x >= 0 and x < N and y >= 0 and y < N and maze [ x ] [ y ] == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def solveMaze ( maze ) : NEW_LINE INDENT sol = [ [ 0 for j in range ( 4 ) ] for i in range ( 4 ) ] NEW_LINE if solveMazeUtil ( maze , 0 , 0 , sol ) == False : NEW_LINE INDENT print ( " Solution ▁ doesn ' t ▁ exist " ) ; NEW_LINE return False NEW_LINE DEDENT printSolution ( sol ) NEW_LINE return True NEW_LINE DEDENT def solveMazeUtil ( maze , x , y , sol ) : NEW_LINE INDENT if x == N - 1 and y == N - 1 and maze [ x ] [ y ] == 1 : NEW_LINE INDENT sol [ x ] [ y ] = 1 NEW_LINE return True NEW_LINE DEDENT if isSafe ( maze , x , y ) == True : NEW_LINE INDENT if sol [ x ] [ y ] == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT sol [ x ] [ y ] = 1 NEW_LINE if solveMazeUtil ( maze , x + 1 , y , sol ) == True : NEW_LINE INDENT return True NEW_LINE DEDENT if solveMazeUtil ( maze , x , y + 1 , sol ) == True : NEW_LINE INDENT return True NEW_LINE DEDENT if solveMazeUtil ( maze , x - 1 , y , sol ) == True : NEW_LINE INDENT return True NEW_LINE DEDENT if solveMazeUtil ( maze , x , y - 1 , sol ) == True : NEW_LINE INDENT return True NEW_LINE DEDENT sol [ x ] [ y ] = 0 NEW_LINE return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT maze = [ [ 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 0 ] , [ 1 , 1 , 1 , 1 ] ] NEW_LINE solveMaze ( maze ) NEW_LINE DEDENT
m Coloring Problem | Backtracking | A utility function to prsolution ; check if the colored graph is safe or not ; check for every edge ; This function solves the m Coloring problem using recursion . It returns false if the m colours cannot be assigned , otherwise , return true and prints assignments of colours to all vertices . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; if current index reached end ; if coloring is safe ; Prthe solution ; Assign each color from 1 to m ; Recur of the rest vertices ; Driver code ; Create following graph and test whether it is 3 colorable ( 3 ) -- - ( 2 ) | / | | / | | / | ( 0 ) -- - ( 1 ) ; Number of colors ; Initialize all color values as 0. This initialization is needed correct functioning of isSafe ( )
def printSolution ( color ) : NEW_LINE INDENT print ( " Solution ▁ Exists : " " ▁ Following ▁ are ▁ the ▁ assigned ▁ colors ▁ " ) NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT print ( color [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def isSafe ( graph , color ) : NEW_LINE INDENT for i in range ( 4 ) : NEW_LINE INDENT for j in range ( i + 1 , 4 ) : NEW_LINE INDENT if ( graph [ i ] [ j ] and color [ j ] == color [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def graphColoring ( graph , m , i , color ) : NEW_LINE INDENT if ( i == 4 ) : NEW_LINE INDENT if ( isSafe ( graph , color ) ) : NEW_LINE INDENT printSolution ( color ) NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE DEDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT color [ i ] = j NEW_LINE if ( graphColoring ( graph , m , i + 1 , color ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT color [ i ] = 0 NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT graph = [ [ 0 , 1 , 1 , 1 ] , [ 1 , 0 , 1 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 1 , 0 , 1 , 0 ] , ] NEW_LINE m = 3 NEW_LINE color = [ 0 for i in range ( 4 ) ] NEW_LINE if ( not graphColoring ( graph , m , 0 , color ) ) : NEW_LINE INDENT print ( " Solution ▁ does ▁ not ▁ exist " ) NEW_LINE DEDENT DEDENT
Maximize pair decrements required to reduce all array elements except one to 0 | Function to count maximum number of steps to make ( N - 1 ) array elements to 0 ; Stores maximum count of steps to make ( N - 1 ) elements equal to 0 ; Stores array elements ; Traverse the array ; Insert arr [ i ] into PQ ; Extract top 2 elements from the array while ( N - 1 ) array elements become 0 ; Stores top element of PQ ; Pop the top element of PQ . ; Stores top element of PQ ; Pop the top element of PQ . ; Update X ; Update Y ; If X is not equal to 0 ; Insert X into PQ ; if Y is not equal to 0 ; Insert Y into PQ ; Update cntOp ; Driver Code
def cntMaxOperationToMakeN_1_0 ( arr , N ) : NEW_LINE INDENT cntOp = 0 NEW_LINE PQ = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT PQ . append ( arr [ i ] ) NEW_LINE DEDENT PQ = sorted ( PQ ) NEW_LINE while ( len ( PQ ) > 1 ) : NEW_LINE INDENT X = PQ [ - 1 ] NEW_LINE del PQ [ - 1 ] NEW_LINE Y = PQ [ - 1 ] NEW_LINE del PQ [ - 1 ] NEW_LINE X -= 1 NEW_LINE Y -= 1 NEW_LINE if ( X != 0 ) : NEW_LINE INDENT PQ . append ( X ) NEW_LINE DEDENT if ( Y != 0 ) : NEW_LINE INDENT PQ . append ( Y ) NEW_LINE DEDENT cntOp += 1 NEW_LINE PQ = sorted ( PQ ) NEW_LINE DEDENT return cntOp NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( cntMaxOperationToMakeN_1_0 ( arr , N ) ) NEW_LINE DEDENT
Flip all K | Function to flip all K - bits of an unsigned number N ; Stores ( 2 ^ K ) - 1 ; Update N ; Print the answer ; Driver Code
def flippingBits ( N , K ) : NEW_LINE INDENT X = ( 1 << ( K - 1 ) ) - 1 NEW_LINE N = X - N NEW_LINE print ( N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , K = 1 , 8 NEW_LINE flippingBits ( N , K ) NEW_LINE DEDENT
Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices | Function to construct a matrix such that the sum elements in both diagonals of every 2 * 2 matrices is even ; Stores odd numbers ; Stores even numbers ; Store matrix elements such that sum of elements in both diagonals of every 2 * 2 submatrices is even ; Fill all the values of matrix elements ; Update odd ; Update even ; Prthe matrix ; Driver Code
def generateMatrix ( N ) : NEW_LINE INDENT odd = 1 ; NEW_LINE even = 2 ; NEW_LINE mat = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( ( i + j ) % 2 == 0 ) : NEW_LINE INDENT mat [ i ] [ j ] = odd ; NEW_LINE odd += 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT mat [ i ] [ j ] = even ; NEW_LINE even += 2 ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 ; NEW_LINE generateMatrix ( N ) ; NEW_LINE DEDENT