codeparrot/xlcost-text-to-code huggingface dataset:
DFS for a n | DFS on tree ; Printing traversed node ; Traversing adjacent edges ; Not traversing the parent node ; Driver Code ; Number of nodes ; Adjacency List ; Designing the tree ; Function call
def dfs ( List , node , arrival ) : NEW_LINE INDENT print ( node ) NEW_LINE for i in range ( len ( List [ node ] ) ) : NEW_LINE INDENT if ( List [ node ] [ i ] != arrival ) : NEW_LINE INDENT dfs ( List , List [ node ] [ i ] , node ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT nodes = 5 NEW_LINE List = [ [ ] for i in range ( 10000 ) ] NEW_LINE List [ 1 ] . append ( 2 ) NEW_LINE List [ 2 ] . append ( 1 ) NEW_LINE List [ 1 ] . append ( 3 ) NEW_LINE List [ 3 ] . append ( 1 ) NEW_LINE List [ 2 ] . append ( 4 ) NEW_LINE List [ 4 ] . append ( 2 ) NEW_LINE List [ 3 ] . append ( 5 ) NEW_LINE List [ 5 ] . append ( 3 ) NEW_LINE dfs ( List , 1 , 0 ) NEW_LINE DEDENT
Maximum number of edges to be added to a tree so that it stays a Bipartite graph | To store counts of nodes with two colors ; Increment count of nodes with current color ; Traversing adjacent nodes ; Not recurring for the parent node ; Finds maximum number of edges that can be added without violating Bipartite property . ; Do a DFS to count number of nodes of each color ; Driver code To store counts of nodes with two colors
def dfs ( adj , node , parent , color ) : NEW_LINE INDENT count_color [ color ] += 1 NEW_LINE for i in range ( len ( adj [ node ] ) ) : NEW_LINE INDENT if ( adj [ node ] [ i ] != parent ) : NEW_LINE INDENT dfs ( adj , adj [ node ] [ i ] , node , not color ) NEW_LINE DEDENT DEDENT DEDENT def findMaxEdges ( adj , n ) : NEW_LINE INDENT dfs ( adj , 1 , 0 , 0 ) NEW_LINE return ( count_color [ 0 ] * count_color [ 1 ] - ( n - 1 ) ) NEW_LINE DEDENT count_color = [ 0 , 0 ] NEW_LINE n = 5 NEW_LINE adj = [ [ ] for i in range ( n + 1 ) ] NEW_LINE adj [ 1 ] . append ( 2 ) NEW_LINE adj [ 1 ] . append ( 3 ) NEW_LINE adj [ 2 ] . append ( 4 ) NEW_LINE adj [ 3 ] . append ( 5 ) NEW_LINE print ( findMaxEdges ( adj , n ) ) NEW_LINE
Min steps to empty an Array by removing a pair each time with sum at most K | Function to count minimum steps ; Function to sort the array ; Run while loop ; Condition to check whether sum exceed the target or not ; Increment the step by 1 ; Return minimum steps ; Given array arr [ ] ; Given target value ; Function call
def countMinSteps ( arr , target , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE minimumSteps = 0 NEW_LINE i , j = 0 , n - 1 NEW_LINE while i <= j : NEW_LINE INDENT if arr [ i ] + arr [ j ] <= target : NEW_LINE INDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT minimumSteps += 1 NEW_LINE DEDENT return minimumSteps NEW_LINE DEDENT arr = [ 4 , 6 , 2 , 9 , 6 , 5 , 8 , 10 ] NEW_LINE target = 11 NEW_LINE size = len ( arr ) NEW_LINE print ( countMinSteps ( arr , target , size ) ) NEW_LINE
Sort the strings based on the numbers of matchsticks required to represent them | Stick [ ] stores the count of sticks required to represent the alphabets ; Number [ ] stores the count of sticks required to represent the numerals ; Function that return the count of sticks required to represent the given string str ; Loop to iterate over every character of the string ; Add the count of sticks required to represent the current character ; Function to sort the array according to the number of sticks required to represent it ; Vector to store the number of sticks required with respective strings ; Inserting number of sticks with respective strings ; Sort the vector ; Print the sorted vector ; Driver Code
sticks = [ 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 ] NEW_LINE number = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE def countSticks ( st ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT ch = st [ i ] NEW_LINE if ( ch >= ' A ' and ch <= ' Z ' ) : NEW_LINE INDENT cnt += sticks [ ord ( ch ) - ord ( ' A ' ) ] NEW_LINE DEDENT else : NEW_LINE INDENT cnt += number [ ord ( ch ) - ord ( '0' ) ] NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT vp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( [ countSticks ( arr [ i ] ) , arr [ i ] ] ) NEW_LINE DEDENT vp . sort ( ) NEW_LINE for i in range ( len ( vp ) ) : NEW_LINE INDENT print ( vp [ i ] [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " GEEKS " , " FOR " , " GEEKSFORGEEKS " ] NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n ) NEW_LINE DEDENT
Count number of pairs with positive sum in an array | Returns number of pairs in arr [ 0. . n - 1 ] with positive sum ; Initialize result ; Consider all possible pairs and check their sums ; If arr [ i ] & arr [ j ] form valid pair ; Driver 's Code ; Function call to find the count of pairs
def CountPairs ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] + arr [ j ] > 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 7 , - 1 , 3 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( CountPairs ( arr , n ) ) ; NEW_LINE DEDENT
Find if there exists a direction for ranges such that no two range intersect | Python implementation of the approach ; Function that returns true if the assignment of directions is possible ; Structure to hold details of each interval ; Sort the intervals based on velocity ; Test the condition for all intervals with same velocity ; If for any velocity , 3 or more intervals share a common poreturn false ; Driver code
MAX = 100001 NEW_LINE def isPossible ( rangee , N ) : NEW_LINE INDENT test = [ [ 0 for x in range ( 3 ) ] for x in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT test [ i ] [ 0 ] = rangee [ i ] [ 0 ] NEW_LINE test [ i ] [ 1 ] = rangee [ i ] [ 1 ] NEW_LINE test [ i ] [ 2 ] = rangee [ i ] [ 2 ] NEW_LINE DEDENT test . sort ( key = lambda x : x [ 2 ] ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT count = [ 0 ] * MAX NEW_LINE current_velocity = test [ i ] [ 2 ] NEW_LINE j = i NEW_LINE while ( j < N and test [ j ] [ 2 ] == current_velocity ) : NEW_LINE INDENT for k in range ( test [ j ] [ 0 ] , test [ j ] [ 1 ] + 1 ) : NEW_LINE INDENT count [ k ] += 1 NEW_LINE if ( count [ k ] >= 3 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT i = j - 1 NEW_LINE DEDENT return True NEW_LINE DEDENT rangee = [ [ 1 , 2 , 3 ] , [ 2 , 5 , 1 ] , [ 3 , 10 , 1 ] , [ 4 , 4 , 1 ] , [ 5 , 7 , 10 ] ] NEW_LINE n = len ( rangee ) NEW_LINE if ( isPossible ( rangee , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
a | Function to return the modified string ; To store the previous character ; To store the starting indices of all the groups ; Starting index of the first group ; If the current character and the previous character differ ; Push the starting index of the new group ; The current character now becomes previous ; Sort the first group ; Sort all the remaining groups ; print ( ''.join(st)) ; Return the modified string ; Driver code
def get_string ( st , n ) : NEW_LINE INDENT prev = st [ 0 ] NEW_LINE result = [ ] NEW_LINE result . append ( 0 ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( st [ i ] . isdigit ( ) != prev . isdigit ( ) ) : NEW_LINE INDENT result . append ( i ) NEW_LINE DEDENT prev = st [ i ] NEW_LINE DEDENT st = list ( st ) NEW_LINE st [ : result [ 0 ] ] . sort ( ) NEW_LINE p = st . copy ( ) NEW_LINE for i in range ( len ( result ) - 1 ) : NEW_LINE INDENT p [ result [ i ] : result [ i + 1 ] ] = sorted ( st [ result [ i ] : result [ i + 1 ] ] ) NEW_LINE DEDENT p [ len ( p ) - 2 : ] = sorted ( st [ result [ - 1 ] : ] ) NEW_LINE return ' ' . join ( p ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "121geeks21" NEW_LINE n = len ( st ) NEW_LINE print ( get_string ( st , n ) ) NEW_LINE DEDENT
Find the kth smallest number with sum of digits as m | Python3 implementation of the approach ; Recursively moving to add all the numbers upto a limit with sum of digits as m ; Max nber of digits allowed in a nber for this implementation ; Function to return the kth number with sum of digits as m ; The kth smallest number is found ; Driver code
N = 2005 NEW_LINE ans = dict ( ) NEW_LINE def dfs ( n , left , ct ) : NEW_LINE INDENT if ( ct >= 15 ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( left == 0 ) : NEW_LINE INDENT ans [ n ] = 1 NEW_LINE DEDENT for i in range ( min ( left , 9 ) + 1 ) : NEW_LINE INDENT dfs ( n * 10 + i , left - i , ct + 1 ) NEW_LINE DEDENT DEDENT def getKthNum ( m , k ) : NEW_LINE INDENT dfs ( 0 , m , 0 ) NEW_LINE c = 0 NEW_LINE for it in sorted ( ans . keys ( ) ) : NEW_LINE INDENT c += 1 NEW_LINE if ( c == k ) : NEW_LINE INDENT return it NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT m = 5 NEW_LINE k = 3 NEW_LINE print ( getKthNum ( m , k ) ) NEW_LINE
Remove minimum elements from the array such that 2 * min becomes more than max | Python3 program to remove minimum elements from the array such that 2 * min becomes more than max ; Function to remove minimum elements from the array such that 2 * min becomes more than max ; Sort the array ; To store the required answer ; Traverse from left to right ; Update the answer ; Return the required answer ; Driver code ; Function call
from bisect import bisect_left as upper_bound NEW_LINE def Removal ( v , n ) : NEW_LINE INDENT v = sorted ( v ) NEW_LINE ans = 10 ** 9 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT j = upper_bound ( v , ( 2 * ( a [ i ] ) ) ) NEW_LINE ans = min ( ans , n - ( j - i - 1 ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT a = [ 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ] NEW_LINE n = len ( a ) NEW_LINE print ( Removal ( a , n ) ) NEW_LINE
Sort the Queue using Recursion | defining a class Queue ; Function to push element in last by popping from front until size becomes 0 ; Base condition ; pop front element and push this last in a queue ; Recursive call for pushing element ; Function to push an element in the queue while maintaining the sorted order ; Base condition ; If current element is less than the element at the front ; Call stack with front of queue ; Recursive call for inserting a front element of the queue to the last ; Push front element into last in a queue ; Recursive call for pushing element in a queue ; Function to sort the given queue using recursion ; Return if queue is empty ; Get the front element which will be stored in this variable throughout the recursion stack ; Recursive call ; Push the current element into the queue according to the sorting order ; Driver code ; Data is inserted into Queue using put ( ) Data is inserted at the end ; Sort the queue ; Print the elements of the queue after sorting
class Queue : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . queue = [ ] NEW_LINE DEDENT def put ( self , item ) : NEW_LINE INDENT self . queue . append ( item ) NEW_LINE DEDENT def get ( self ) : NEW_LINE INDENT if len ( self . queue ) < 1 : NEW_LINE INDENT return None NEW_LINE DEDENT return self . queue . pop ( 0 ) NEW_LINE DEDENT def front ( self ) : NEW_LINE INDENT return self . queue [ 0 ] NEW_LINE DEDENT def size ( self ) : NEW_LINE INDENT return len ( self . queue ) NEW_LINE DEDENT def empty ( self ) : NEW_LINE INDENT return not ( len ( self . queue ) ) NEW_LINE DEDENT DEDENT def FrontToLast ( q , qsize ) : NEW_LINE INDENT if qsize <= 0 : NEW_LINE INDENT return NEW_LINE DEDENT q . put ( q . get ( ) ) NEW_LINE FrontToLast ( q , qsize - 1 ) NEW_LINE DEDENT def pushInQueue ( q , temp , qsize ) : NEW_LINE INDENT if q . empty ( ) or qsize == 0 : NEW_LINE INDENT q . put ( temp ) NEW_LINE return NEW_LINE DEDENT elif temp <= q . front ( ) : NEW_LINE INDENT q . put ( temp ) NEW_LINE FrontToLast ( q , qsize ) NEW_LINE DEDENT else : NEW_LINE INDENT q . put ( q . get ( ) ) NEW_LINE pushInQueue ( q , temp , qsize - 1 ) NEW_LINE DEDENT DEDENT def sortQueue ( q ) : NEW_LINE INDENT if q . empty ( ) : NEW_LINE INDENT return NEW_LINE DEDENT temp = q . get ( ) NEW_LINE sortQueue ( q ) NEW_LINE pushInQueue ( q , temp , q . size ( ) ) NEW_LINE DEDENT qu = Queue ( ) NEW_LINE qu . put ( 10 ) NEW_LINE qu . put ( 7 ) NEW_LINE qu . put ( 16 ) NEW_LINE qu . put ( 9 ) NEW_LINE qu . put ( 20 ) NEW_LINE qu . put ( 5 ) NEW_LINE sortQueue ( qu ) NEW_LINE while not qu . empty ( ) : NEW_LINE INDENT print ( qu . get ( ) , end = ' ▁ ' ) NEW_LINE DEDENT
Sort the character array based on ASCII % N | This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; pivot ; Index of smaller element ; If current element is smaller than or equal to pivot Instead of values , ASCII % m values are compared ; Increment index of smaller element ; swap ; The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to print the given array ; Driver code ; Sort the given array ; Print the sorted array
def partition ( arr , low , high , mod ) : NEW_LINE INDENT pivot = ord ( arr [ high ] ) ; NEW_LINE i = ( low - 1 ) ; NEW_LINE piv = pivot % mod ; NEW_LINE for j in range ( low , high ) : NEW_LINE INDENT a = ord ( arr [ j ] ) % mod ; NEW_LINE if ( a <= piv ) : NEW_LINE INDENT i += 1 ; NEW_LINE arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE DEDENT DEDENT arr [ i + 1 ] , arr [ high ] = arr [ high ] , arr [ i + 1 ] NEW_LINE return ( i + 1 ) ; NEW_LINE DEDENT def quickSort ( arr , low , high , mod ) : NEW_LINE INDENT if ( low < high ) : NEW_LINE INDENT pi = partition ( arr , low , high , mod ) ; NEW_LINE quickSort ( arr , low , pi - 1 , mod ) ; NEW_LINE quickSort ( arr , pi + 1 , high , mod ) ; NEW_LINE return arr NEW_LINE DEDENT DEDENT def printArray ( arr , size ) : NEW_LINE INDENT for i in range ( 0 , size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ ' g ' , ' e ' , ' e ' , ' k ' , ' s ' ] ; NEW_LINE n = len ( arr ) ; NEW_LINE mod = 8 ; NEW_LINE arr = quickSort ( arr , 0 , n - 1 , mod ) ; NEW_LINE printArray ( arr , n ) ; NEW_LINE DEDENT
Iterative selection sort for linked list | Linked List Node ; Function to sort a linked list using selection sort algorithm by swapping the next pointers ; While b is not the last node ; While d is pointing to a valid node ; If d appears immediately after b ; Case 1 : b is the head of the linked list ; Move d before b ; Swap b and d pointers ; Update the head ; Skip to the next element as it is already in order ; Case 2 : b is not the head of the linked list ; Move d before b ; Swap b and d pointers ; Skip to the next element as it is already in order ; If b and d have some non - zero number of nodes in between them ; Case 3 : b is the head of the linked list ; Swap b . next and d . next ; Swap b and d pointers ; Skip to the next element as it is already in order ; Update the head ; Case 4 : b is not the head of the linked list ; Swap b . next and d . next ; Swap b and d pointers ; Skip to the next element as it is already in order ; Update c and skip to the next element as it is already in order ; Function to print the list ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . data = val NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def selectionSort ( head ) : NEW_LINE INDENT a = b = head NEW_LINE while b . next : NEW_LINE INDENT c = d = b . next NEW_LINE while d : NEW_LINE INDENT if b . data > d . data : NEW_LINE INDENT if b . next == d : NEW_LINE INDENT if b == head : NEW_LINE INDENT b . next = d . next NEW_LINE d . next = b NEW_LINE b , d = d , b NEW_LINE c = d NEW_LINE head = b NEW_LINE d = d . next NEW_LINE DEDENT else : NEW_LINE INDENT b . next = d . next NEW_LINE d . next = b NEW_LINE a . next = d NEW_LINE b , d = d , b NEW_LINE c = d NEW_LINE d = d . next NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if b == head : NEW_LINE INDENT r = b . next NEW_LINE b . next = d . next NEW_LINE d . next = r NEW_LINE c . next = b NEW_LINE b , d = d , b NEW_LINE c = d NEW_LINE d = d . next NEW_LINE head = b NEW_LINE DEDENT else : NEW_LINE INDENT r = b . next NEW_LINE b . next = d . next NEW_LINE d . next = r NEW_LINE c . next = b NEW_LINE a . next = d NEW_LINE b , d = d , b NEW_LINE c = d NEW_LINE d = d . next NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT c = d NEW_LINE d = d . next NEW_LINE DEDENT DEDENT a = b NEW_LINE b = b . next NEW_LINE DEDENT return head NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT while head : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT head = Node ( 5 ) NEW_LINE head . next = Node ( 4 ) NEW_LINE head . next . next = Node ( 3 ) NEW_LINE head = selectionSort ( head ) NEW_LINE printList ( head ) NEW_LINE DEDENT
Find original numbers from gcd ( ) every pair | Python 3 implementation of the approach ; Utility function to print the contents of an array ; Function to find the required numbers ; Sort array in decreasing order ; Count frequency of each element ; Size of the resultant array ; Store the highest element in the resultant array ; Decrement the frequency of that element ; Compute GCD ; Decrement GCD value by 2 ;  ; Driver code
from math import sqrt , gcd NEW_LINE def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def findNumbers ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE freq = [ 0 for i in range ( arr [ 0 ] + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT size = int ( sqrt ( n ) ) NEW_LINE brr = [ 0 for i in range ( len ( arr ) ) ] NEW_LINE l = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( freq [ arr [ i ] ] > 0 ) : NEW_LINE INDENT brr [ l ] = arr [ i ] NEW_LINE freq [ brr [ l ] ] -= 1 NEW_LINE l += 1 NEW_LINE for j in range ( l ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT x = gcd ( arr [ i ] , brr [ j ] ) NEW_LINE freq [ x ] -= 2 NEW_LINE DEDENT DEDENT DEDENT DEDENT printArr ( brr , size ) NEW_LINE DEDENT / * reverse array * / NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 5 , 5 , 5 , 7 , 10 , 12 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE findNumbers ( arr , n ) NEW_LINE DEDENT
Bitwise AND of N binary strings | Python3 implementation of the above approach ; Function to find the bitwise AND of all the binary strings ; To store the largest and the smallest string ' s ▁ size , ▁ We ▁ need ▁ this ▁ to ▁ add ▁ ' 0 's  in the resultant string ; Reverse each string Since we need to perform AND operation on bits from Right to Left ; Update the respective length values ; Traverse bits from 0 to smallest string 's size ; If at this bit position , there is a 0 in any of the given strings then AND operation on current bit position will be 0 ; Add resultant bit to result ; Add 0 's to the string. ; Reverse the string Since we started from LEFT to RIGHT ; Return the resultant string ; Driver code
import sys ; NEW_LINE def strBitwiseAND ( arr , n ) : NEW_LINE INDENT res = " " NEW_LINE smallest_size = sys . maxsize ; NEW_LINE largest_size = - ( sys . maxsize - 1 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = arr [ i ] [ : : - 1 ] ; NEW_LINE smallest_size = min ( smallest_size , len ( arr [ i ] ) ) ; NEW_LINE largest_size = max ( largest_size , len ( arr [ i ] ) ) ; NEW_LINE DEDENT for i in range ( smallest_size ) : NEW_LINE INDENT all_ones = True ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr [ j ] [ i ] == '0' ) : NEW_LINE INDENT all_ones = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if all_ones : NEW_LINE INDENT res += '1' ; NEW_LINE DEDENT else : NEW_LINE INDENT res += '0' ; NEW_LINE DEDENT DEDENT for i in range ( largest_size - smallest_size ) : NEW_LINE INDENT res += '0' ; NEW_LINE DEDENT res = res [ : : - 1 ] ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ "101" , "110110" , "111" ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( strBitwiseAND ( arr , n ) ) ; NEW_LINE DEDENT
Print all paths from a given source to a destination using BFS | Python3 program to print all paths of source to destination in given graph ; Utility function for printing the found path in graph ; Utility function to check if current vertex is already present in path ; Utility function for finding paths in graph from source to destination ; Create a queue which stores the paths ; Path vector to store the current path ; If last vertex is the desired destination then print the path ; Traverse to all the nodes connected to current vertex and push new path to queue ; Driver code ; Number of vertices ; Construct a graph ; Function for finding the paths
from typing import List NEW_LINE from collections import deque NEW_LINE def printpath ( path : List [ int ] ) -> None : NEW_LINE INDENT size = len ( path ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT print ( path [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def isNotVisited ( x : int , path : List [ int ] ) -> int : NEW_LINE INDENT size = len ( path ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( path [ i ] == x ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT def findpaths ( g : List [ List [ int ] ] , src : int , dst : int , v : int ) -> None : NEW_LINE INDENT q = deque ( ) NEW_LINE path = [ ] NEW_LINE path . append ( src ) NEW_LINE q . append ( path . copy ( ) ) NEW_LINE while q : NEW_LINE INDENT path = q . popleft ( ) NEW_LINE last = path [ len ( path ) - 1 ] NEW_LINE if ( last == dst ) : NEW_LINE INDENT printpath ( path ) NEW_LINE DEDENT for i in range ( len ( g [ last ] ) ) : NEW_LINE INDENT if ( isNotVisited ( g [ last ] [ i ] , path ) ) : NEW_LINE INDENT newpath = path . copy ( ) NEW_LINE newpath . append ( g [ last ] [ i ] ) NEW_LINE q . append ( newpath ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT v = 4 NEW_LINE g = [ [ ] for _ in range ( 4 ) ] NEW_LINE g [ 0 ] . append ( 3 ) NEW_LINE g [ 0 ] . append ( 1 ) NEW_LINE g [ 0 ] . append ( 2 ) NEW_LINE g [ 1 ] . append ( 3 ) NEW_LINE g [ 2 ] . append ( 0 ) NEW_LINE g [ 2 ] . append ( 1 ) NEW_LINE src = 2 NEW_LINE dst = 3 NEW_LINE print ( " path ▁ from ▁ src ▁ { } ▁ to ▁ dst ▁ { } ▁ are " . format ( src , dst ) ) NEW_LINE findpaths ( g , src , dst , v ) NEW_LINE DEDENT
Rearrange Odd and Even values in Alternate Fashion in Ascending Order | Python3 implementation of the above approach ; Sort the array ; v1 = list ( ) to insert even values v2 = list ( ) to insert odd values ; Set flag to true if first element is even ; Start rearranging array ; If first element is even ; Else , first element is Odd ; Print the rearranged array ; Driver code
def AlternateRearrange ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT v1 . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT v2 . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT index = 0 NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE flag = False NEW_LINE if ( arr [ 0 ] % 2 == 0 ) : NEW_LINE INDENT flag = True NEW_LINE DEDENT while ( index < n ) : NEW_LINE INDENT if ( flag == True and i < len ( v1 ) ) : NEW_LINE INDENT arr [ index ] = v1 [ i ] NEW_LINE index += 1 NEW_LINE i += 1 NEW_LINE flag = ~ flag NEW_LINE DEDENT elif j < len ( v2 ) : NEW_LINE INDENT arr [ index ] = v2 [ j ] NEW_LINE index += 1 NEW_LINE j += 1 NEW_LINE flag = ~ flag NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 9 , 8 , 13 , 2 , 19 , 14 , 21 , 23 , 25 ] NEW_LINE n = len ( arr ) NEW_LINE AlternateRearrange ( arr , n ) NEW_LINE