codeparrot/xlcost-text-to-code huggingface dataset:
Convert given array to Arithmetic Progression by adding an element | Function to return the number to be added ; If difference of the current consecutive elements is different from the common difference ; If number has already been chosen then it 's not possible to add  another number ; If the current different is twice the common difference then a number can be added midway from current and previous element ; Number has been chosen ; It 's not possible to maintain  the common difference ; Return last element + common difference if no element is chosen and the array is already in AP ; Else return the chosen number ; Driver code
def getNumToAdd ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE d = arr [ 1 ] - arr [ 0 ] NEW_LINE numToAdd = - 1 NEW_LINE numAdded = False NEW_LINE for i in range ( 2 , n , 1 ) : NEW_LINE INDENT diff = arr [ i ] - arr [ i - 1 ] NEW_LINE if ( diff != d ) : NEW_LINE INDENT if ( numAdded ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( diff == 2 * d ) : NEW_LINE INDENT numToAdd = arr [ i ] - d NEW_LINE numAdded = True NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT DEDENT if ( numToAdd == - 1 ) : NEW_LINE INDENT return ( arr [ n - 1 ] + d ) NEW_LINE DEDENT return numToAdd NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 7 , 11 , 13 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getNumToAdd ( arr , n ) ) NEW_LINE DEDENT
Minimum Numbers of cells that are connected with the smallest path between 3 given cells | Function to return the minimum cells that are connected via the minimum length path ; Array to store column number of the given cells ; Sort cells in ascending order of row number ; Middle row number ; Set pair to store required cells ; Range of column number ; Store all cells of middle row within column number range ; Final step to store all the column number of 1 st and 3 rd cell upto MidRow ; Driver Code ; vector pair to store X , Y , Z
def Minimum_Cells ( v ) : NEW_LINE INDENT col = [ 0 ] * 3 NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT column_number = v [ i ] [ 1 ] NEW_LINE col [ i ] = column_number NEW_LINE DEDENT col . sort ( ) NEW_LINE v . sort ( ) NEW_LINE MidRow = v [ 1 ] [ 0 ] NEW_LINE s = set ( ) NEW_LINE Maxcol = col [ 2 ] NEW_LINE MinCol = col [ 0 ] NEW_LINE for i in range ( MinCol , int ( Maxcol ) + 1 ) : NEW_LINE INDENT s . add ( ( MidRow , i ) ) NEW_LINE DEDENT for i in range ( 3 ) : NEW_LINE INDENT if ( v [ i ] [ 0 ] == MidRow ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT for j in range ( min ( v [ i ] [ 0 ] , MidRow ) , max ( v [ i ] [ 0 ] , MidRow ) + 1 ) : NEW_LINE INDENT s . add ( ( j , v [ i ] [ 1 ] ) ) ; NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT v = [ ( 0 , 0 ) , ( 1 , 1 ) , ( 2 , 2 ) ] NEW_LINE print ( Minimum_Cells ( v ) ) NEW_LINE DEDENT
Get maximum items when other items of total cost of an item are free | Function to count the total number of items ; Sort the prices ; Choose the last element ; Initial count of item ; Sum to keep track of the total price of free items ; If total is less than or equal to z then we will add 1 to the answer ;
def items ( n , a ) : NEW_LINE INDENT a . sort ( ) NEW_LINE z = a [ n - 1 ] NEW_LINE x = 1 NEW_LINE s = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT s += a [ i ] NEW_LINE if ( s <= z ) : NEW_LINE INDENT x += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT / * Driver code * / NEW_LINE n = 5 NEW_LINE a = [ 5 , 3 , 1 , 5 , 6 ] NEW_LINE print ( items ( n , a ) ) NEW_LINE
Sorting array elements with set bits equal to K | Function to sort elements with set bits equal to k ; initialise two vectors ; first vector contains indices of required element ; second vector contains required elements ; sorting the elements in second vector ; replacing the elements with k set bits with the sorted elements ; printing the new sorted array elements ; Driver code
def sortWithSetbits ( arr , n , k ) : NEW_LINE INDENT v1 = [ ] NEW_LINE v2 = [ ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( bin ( arr [ i ] ) . count ( '1' ) == k ) : NEW_LINE INDENT v1 . append ( i ) NEW_LINE v2 . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v2 . sort ( reverse = False ) NEW_LINE for i in range ( 0 , len ( v1 ) , 1 ) : NEW_LINE INDENT arr [ v1 [ i ] ] = v2 [ i ] NEW_LINE DEDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 14 , 255 , 1 , 7 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE sortWithSetbits ( arr , n , k ) NEW_LINE DEDENT
Minimum boxes required to carry all gifts | Function to return number of boxes ; Sort the boxes in ascending order ; Try to fit smallest box with current heaviest box ( from right side ) ; Driver code
def numBoxex ( A , n , K ) : NEW_LINE INDENT A . sort ( ) NEW_LINE i = 0 NEW_LINE j = n - 1 NEW_LINE ans = 0 NEW_LINE while i <= j : NEW_LINE INDENT ans += 1 NEW_LINE if A [ i ] + A [ j ] <= K : NEW_LINE INDENT i += 1 NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 2 , 2 , 1 ] NEW_LINE K = 3 NEW_LINE n = len ( A ) NEW_LINE print ( numBoxex ( A , n , K ) ) NEW_LINE DEDENT
Count triplets in a sorted doubly linked list whose product is equal to a given value x | Python3 implementation to count triplets in a sorted doubly linked list whose product is equal to a given value 'x ; structure of node of doubly linked list ; function to count triplets in a sorted doubly linked list whose product is equal to a given value 'x ; unordered_map ' um ' implemented as hash table ; insert the tuple in 'um ; generate all possible pairs ; p_product = product of elements in the current pair ; if ' x / p _ product ' is present in ' um ' and either of the two nodes are not equal to the ' um [ x / p _ product ] ' node ; increment count ; required count of triplets division by 3 as each triplet is counted 3 times ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; put in the data ; Driver code ; start with an empty doubly linked list ; insert values in sorted order
' NEW_LINE from math import ceil NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE DEDENT DEDENT ' NEW_LINE def countTriplets ( head , x ) : NEW_LINE INDENT ptr , ptr1 , ptr2 = None , None , None NEW_LINE count = 0 NEW_LINE um = { } NEW_LINE DEDENT ' NEW_LINE INDENT ptr = head NEW_LINE while ptr != None : NEW_LINE INDENT um [ ptr . data ] = ptr NEW_LINE ptr = ptr . next NEW_LINE DEDENT ptr1 = head NEW_LINE while ptr1 != None : NEW_LINE INDENT ptr2 = ptr1 . next NEW_LINE while ptr2 != None : NEW_LINE INDENT p_product = ( ptr1 . data * ptr2 . data ) NEW_LINE if ( ( x / p_product ) in um and ( x / p_product ) != ptr1 and um [ x / p_product ] != ptr2 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT ptr2 = ptr2 . next NEW_LINE DEDENT ptr1 = ptr1 . next NEW_LINE DEDENT return ( count // 3 ) NEW_LINE DEDENT def insert ( head , data ) : NEW_LINE INDENT temp = Node ( data ) NEW_LINE temp . data = data NEW_LINE temp . next = temp . prev = None NEW_LINE if ( head == None ) : NEW_LINE INDENT head = temp NEW_LINE DEDENT else : NEW_LINE INDENT temp . next = head NEW_LINE head . prev = temp NEW_LINE head = temp NEW_LINE DEDENT return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = insert ( head , 9 ) NEW_LINE head = insert ( head , 8 ) NEW_LINE head = insert ( head , 6 ) NEW_LINE head = insert ( head , 5 ) NEW_LINE head = insert ( head , 4 ) NEW_LINE head = insert ( head , 2 ) NEW_LINE head = insert ( head , 1 ) NEW_LINE x = 8 NEW_LINE print ( " Count ▁ = " , countTriplets ( head , x ) ) NEW_LINE DEDENT
Maximize the profit by selling at | Function to find profit ; Calculating profit for each gadget ; sort the profit array in descending order ; variable to calculate total profit ; check for best M profits ; Driver Code
def solve ( N , M , cp , sp ) : NEW_LINE INDENT profit = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT profit . append ( sp [ i ] - cp [ i ] ) NEW_LINE DEDENT profit . sort ( reverse = True ) NEW_LINE sum = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT if profit [ i ] > 0 : NEW_LINE INDENT sum += profit [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M = 5 , 3 NEW_LINE CP = [ 5 , 10 , 35 , 7 , 23 ] NEW_LINE SP = [ 11 , 10 , 0 , 9 , 19 ] NEW_LINE print ( solve ( N , M , CP , SP ) ) NEW_LINE DEDENT
Find the largest number that can be formed with the given digits | Function to generate largest possible number with given digits ; sort the given array in descending order ; generate the number ; Driver code
def findMaxNum ( arr , n ) : NEW_LINE INDENT arr . sort ( rever num = arr [ 0 ] se = True ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT num = num * 10 + arr [ i ] NEW_LINE DEDENT return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxNum ( arr , n ) ) NEW_LINE DEDENT
Minimum partitions of maximum size 2 and sum limited by given value | Python 3 program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; if sum of ith smaller and jth larger element is less than key , then pack both numbers in a set otherwise pack the jth larger number alone in the set ; After ending of loop i will contain minimum number of sets ; Driver Code
def minimumSets ( arr , n , key ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE j = n - 1 NEW_LINE for i in range ( 0 , j + 1 , 1 ) : NEW_LINE INDENT if ( arr [ i ] + arr [ j ] <= key ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT return i + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 5 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE key = 5 NEW_LINE print ( minimumSets ( arr , n , key ) ) NEW_LINE DEDENT
Bubble Sort On Doubly Linked List | structure of a node ; Function to insert a node at the beginning of a linked list ; Function to print nodes in a given linked list ; Bubble sort the given linked list ; Checking for empty list ; Driver code ; start with empty linked list ; Create linked list from the array arr [ ] . Created linked list will be 1.11 . 2.56 . 12 ; print list before sorting ; sort the linked list ; print list after sorting
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . prev = None NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def insertAtTheBegin ( start_ref , data ) : NEW_LINE INDENT ptr1 = Node ( data ) NEW_LINE ptr1 . data = data ; NEW_LINE ptr1 . next = start_ref ; NEW_LINE if ( start_ref != None ) : NEW_LINE ( start_ref ) . prev = ptr1 ; NEW_LINE start_ref = ptr1 ; NEW_LINE return start_ref NEW_LINE DEDENT def printList ( start ) : NEW_LINE INDENT temp = start ; NEW_LINE print ( ) NEW_LINE while ( temp != None ) : NEW_LINE INDENT print ( temp . data , end = ' ▁ ' ) NEW_LINE temp = temp . next ; NEW_LINE DEDENT DEDENT def bubbleSort ( start ) : NEW_LINE INDENT swapped = 0 NEW_LINE lptr = None ; NEW_LINE if ( start == None ) : NEW_LINE INDENT return ; NEW_LINE DEDENT while True : NEW_LINE INDENT swapped = 0 ; NEW_LINE ptr1 = start ; NEW_LINE while ( ptr1 . next != lptr ) : NEW_LINE INDENT if ( ptr1 . data > ptr1 . next . data ) : NEW_LINE INDENT ptr1 . data , ptr1 . next . data = ptr1 . next . data , ptr1 . data NEW_LINE swapped = 1 ; NEW_LINE DEDENT ptr1 = ptr1 . next ; NEW_LINE DEDENT lptr = ptr1 ; NEW_LINE if swapped == 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 56 , 2 , 11 , 1 , 90 ] NEW_LINE start = None ; NEW_LINE for i in range ( 6 ) : NEW_LINE INDENT start = insertAtTheBegin ( start , arr [ i ] ) ; NEW_LINE DEDENT print ( " Linked ▁ list ▁ before ▁ sorting ▁ " , end = ' ' ) ; NEW_LINE printList ( start ) ; NEW_LINE bubbleSort ( start ) ; NEW_LINE print ( " Linked list after sorting   " , end = ' ' ) ; NEW_LINE printList ( start ) ; NEW_LINE DEDENT
Substring Sort | Alternative code to sort substrings ; sort the input array ; repeated length should be the same string ; two different strings with the same length input array cannot be sorted ; validate that each string is a substring of the following one ; get first element ; The array is valid and sorted print the strings in order ; Test 1 ; Test 2
def substringSort ( arr , n , maxLen ) : NEW_LINE INDENT count = [ 0 ] * ( maxLen ) NEW_LINE sortedArr = [ " " ] * ( maxLen ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = arr [ i ] NEW_LINE Len = len ( s ) NEW_LINE if ( count [ Len - 1 ] == 0 ) : NEW_LINE INDENT sortedArr [ Len - 1 ] = s NEW_LINE count [ Len - 1 ] = 1 NEW_LINE DEDENT elif ( sortedArr [ Len - 1 ] == s ) : NEW_LINE INDENT count [ Len - 1 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Cannot ▁ be ▁ sorted " ) NEW_LINE return NEW_LINE DEDENT DEDENT index = 0 NEW_LINE while ( count [ index ] == 0 ) : NEW_LINE INDENT index += 1 NEW_LINE DEDENT prev = index NEW_LINE prevString = sortedArr [ prev ] NEW_LINE index += 1 NEW_LINE while index < maxLen : NEW_LINE INDENT if ( count [ index ] != 0 ) : NEW_LINE INDENT current = sortedArr [ index ] NEW_LINE if ( prevString in current ) : NEW_LINE INDENT prev = index NEW_LINE prevString = current NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Cannot ▁ be ▁ sorted " ) NEW_LINE return NEW_LINE DEDENT DEDENT index += 1 NEW_LINE DEDENT for i in range ( maxLen ) : NEW_LINE INDENT s = sortedArr [ i ] NEW_LINE for j in range ( count [ i ] ) : NEW_LINE INDENT print ( s ) NEW_LINE DEDENT DEDENT DEDENT maxLen = 100 NEW_LINE arr1 = [ " d " , " zddsaaz " , " ds " , " ddsaa " , " dds " , " dds " ] NEW_LINE substringSort ( arr1 , len ( arr1 ) , maxLen ) NEW_LINE arr2 = [ " for " , " rof " ] NEW_LINE substringSort ( arr2 , len ( arr2 ) , maxLen ) NEW_LINE
Number of paths from source to destination in a directed acyclic graph | Python3 program for Number of paths from one vertex to another vertex in a Directed Acyclic Graph ; to make graph ; function to add edge in graph ; there is path from a to b . ; function to make topological sorting ; insert all vertices which don 't have any parent. ; using kahn 's algorithm  for topological sorting ; insert front element of queue to vector ; go through all it 's childs ; whenever the frequency is zero then add this vertex to queue . ; Function that returns the number of paths ; make topological sorting ; to store required answer . ; answer from destination to destination is 1. ; traverse in reverse order ; Driver code ; here vertices are numbered from 0 to n - 1. ; to count number of vertex which don 't  have any parents. ; to add all edges of graph ; Function that returns the number of paths
from collections import deque NEW_LINE MAXN = 1000005 NEW_LINE v = [ [ ] for i in range ( MAXN ) ] NEW_LINE def add_edge ( a , b , fre ) : NEW_LINE INDENT v [ a ] . append ( b ) NEW_LINE fre [ b ] += 1 NEW_LINE DEDENT def topological_sorting ( fre , n ) : NEW_LINE INDENT q = deque ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not fre [ i ] ) : NEW_LINE INDENT q . append ( i ) NEW_LINE DEDENT DEDENT l = [ ] NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT u = q . popleft ( ) NEW_LINE l . append ( u ) NEW_LINE for i in range ( len ( v [ u ] ) ) : NEW_LINE INDENT fre [ v [ u ] [ i ] ] -= 1 NEW_LINE if ( not fre [ v [ u ] [ i ] ] ) : NEW_LINE INDENT q . append ( v [ u ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT return l NEW_LINE DEDENT def numberofPaths ( source , destination , n , fre ) : NEW_LINE INDENT s = topological_sorting ( fre , n ) NEW_LINE dp = [ 0 ] * n NEW_LINE dp [ destination ] = 1 NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( len ( v [ s [ i ] ] ) ) : NEW_LINE INDENT dp [ s [ i ] ] += dp [ v [ s [ i ] ] [ j ] ] NEW_LINE DEDENT DEDENT return dp NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE source , destination = 0 , 4 NEW_LINE fre = [ 0 ] * n NEW_LINE add_edge ( 0 , 1 , fre ) NEW_LINE add_edge ( 0 , 2 , fre ) NEW_LINE add_edge ( 0 , 3 , fre ) NEW_LINE add_edge ( 0 , 4 , fre ) NEW_LINE add_edge ( 2 , 3 , fre ) NEW_LINE add_edge ( 3 , 4 , fre ) NEW_LINE print ( numberofPaths ( source , destination , n , fre ) ) NEW_LINE DEDENT
Print all the pairs that contains the positive and negative values of an element | Utility binary search ; Function ; Sort the array ; Traverse the array ; For every arr [ i ] < 0 element , do a binary search for arr [ i ] > 0. ; If found , print the pair . ; Driver code
def binary_search ( a , x , lo = 0 , hi = None ) : NEW_LINE INDENT if hi is None : NEW_LINE INDENT hi = len ( a ) NEW_LINE DEDENT while lo < hi : NEW_LINE INDENT mid = ( lo + hi ) // 2 NEW_LINE midval = a [ mid ] NEW_LINE if midval < x : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT elif midval > x : NEW_LINE INDENT hi = mid NEW_LINE DEDENT else : NEW_LINE INDENT return mid NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def printPairs ( arr , n ) : NEW_LINE INDENT pair_exists = Fals NEW_LINE arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT if ( binary_search ( arr , - arr [ i ] ) ) : NEW_LINE INDENT print ( arr [ i ] , " , ▁ " , - arr [ i ] ) NEW_LINE pair_exists = True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( pair_exists == False ) : NEW_LINE INDENT print ( " No ▁ such ▁ pair ▁ exists " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ] NEW_LINE n = len ( arr ) NEW_LINE printPairs ( arr , n ) NEW_LINE DEDENT
Multiset Equivalence Problem | Python3 program to check if two given multisets are equivalent ; sort the elements of both multisets ; Return true if both multisets are same . ; Driver code
def areSame ( a , b ) : NEW_LINE INDENT a . sort ( ) ; NEW_LINE b . sort ( ) ; NEW_LINE return ( a == b ) a = [ 7 , 7 , 5 ] ; NEW_LINE DEDENT b = [ 7 , 5 , 5 ] ; NEW_LINE if ( areSame ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; ; NEW_LINE DEDENT
Minimum number of edges between two vertices of a Graph | Python3 program to find minimum edge between given two vertex of Graph ; function for finding minimum no . of edge using BFS ; visited [ n ] for keeping track of visited node in BFS ; Initialize distances as 0 ; queue to do BFS . ; update distance for i ; function for addition of edge ; Driver Code ; To store adjacency list of graph
import queue NEW_LINE def minEdgeBFS ( edges , u , v , n ) : NEW_LINE INDENT visited = [ 0 ] * n NEW_LINE distance = [ 0 ] * n NEW_LINE Q = queue . Queue ( ) NEW_LINE distance [ u ] = 0 NEW_LINE Q . put ( u ) NEW_LINE visited [ u ] = True NEW_LINE while ( not Q . empty ( ) ) : NEW_LINE INDENT x = Q . get ( ) NEW_LINE for i in range ( len ( edges [ x ] ) ) : NEW_LINE INDENT if ( visited [ edges [ x ] [ i ] ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT distance [ edges [ x ] [ i ] ] = distance [ x ] + 1 NEW_LINE Q . put ( edges [ x ] [ i ] ) NEW_LINE visited [ edges [ x ] [ i ] ] = 1 NEW_LINE DEDENT DEDENT return distance [ v ] NEW_LINE DEDENT def addEdge ( edges , u , v ) : NEW_LINE INDENT edges [ u ] . append ( v ) NEW_LINE edges [ v ] . append ( u ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 9 NEW_LINE edges = [ [ ] for i in range ( n ) ] NEW_LINE addEdge ( edges , 0 , 1 ) NEW_LINE addEdge ( edges , 0 , 7 ) NEW_LINE addEdge ( edges , 1 , 7 ) NEW_LINE addEdge ( edges , 1 , 2 ) NEW_LINE addEdge ( edges , 2 , 3 ) NEW_LINE addEdge ( edges , 2 , 5 ) NEW_LINE addEdge ( edges , 2 , 8 ) NEW_LINE addEdge ( edges , 3 , 4 ) NEW_LINE addEdge ( edges , 3 , 5 ) NEW_LINE addEdge ( edges , 4 , 5 ) NEW_LINE addEdge ( edges , 5 , 6 ) NEW_LINE addEdge ( edges , 6 , 7 ) NEW_LINE addEdge ( edges , 7 , 8 ) NEW_LINE u = 0 NEW_LINE v = 5 NEW_LINE print ( minEdgeBFS ( edges , u , v , n ) ) NEW_LINE DEDENT
Minimum swaps so that binary search can be applied | Function to find minimum swaps . ; Here we are getting number of smaller and greater elements of k . ; Here we are calculating the actual position of k in the array . ; Implementing binary search as per the above - discussed cases . ; If we find the k . ; If we need minimum element swap . ; Else the element is at the right position . ; If we need maximum element swap . ; Else element is at the right position ; Calculating the required swaps . ; If it is impossible . ; Driver function
def findMinimumSwaps ( arr , n , k ) : NEW_LINE INDENT num_min = num_max = need_minimum = 0 NEW_LINE need_maximum = swaps = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < k ) : NEW_LINE INDENT num_min += 1 NEW_LINE DEDENT elif ( arr [ i ] > k ) : NEW_LINE INDENT num_max += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == k ) : NEW_LINE INDENT pos = i NEW_LINE break NEW_LINE DEDENT DEDENT left = 0 NEW_LINE right = n - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE if ( arr [ mid ] == k ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( arr [ mid ] > k ) : NEW_LINE INDENT if ( pos > mid ) : NEW_LINE INDENT need_minimum += 1 NEW_LINE DEDENT else : NEW_LINE INDENT num_min -= 1 NEW_LINE DEDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( pos < mid ) : NEW_LINE INDENT need_maximum += 1 NEW_LINE DEDENT else : NEW_LINE INDENT num_max -= 1 NEW_LINE DEDENT right = mid - 1 NEW_LINE DEDENT DEDENT if ( need_minimum > need_maximum ) : NEW_LINE INDENT swaps = swaps + need_maximum NEW_LINE num_min = num_min - need_maximum NEW_LINE need_minimum = ( need_minimum - need_maximum ) NEW_LINE need_maximum = 0 NEW_LINE DEDENT else : NEW_LINE INDENT swaps = swaps + need_minimum NEW_LINE num_max = num_max - need_minimum NEW_LINE need_maximum = ( need_maximum - need_minimum ) NEW_LINE need_minimum = 0 NEW_LINE DEDENT if ( need_maximum > num_max or need_minimum > num_min ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( swaps + need_maximum + need_minimum ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 10 , 6 , 7 , 2 , 5 , 4 ] NEW_LINE k = 4 NEW_LINE n = len ( arr ) NEW_LINE print ( findMinimumSwaps ( arr , n , k ) ) NEW_LINE DEDENT
Stable sort for descending order |
/ * Driver code * / NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE arr . sort ( reverse = True ) NEW_LINE print ( " Array ▁ after ▁ sorting ▁ : ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Check if both halves of the string have at least one different character | Function which break string into two halves Sorts the two halves separately Compares the two halves return true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
def function ( st ) : NEW_LINE INDENT st = list ( st ) NEW_LINE l = len ( st ) NEW_LINE st [ : l // 2 ] = sorted ( st [ : l // 2 ] ) NEW_LINE st [ l // 2 : ] = sorted ( st [ l // 2 : ] ) NEW_LINE for i in range ( l // 2 ) : NEW_LINE INDENT if ( st [ i ] != st [ l // 2 + i ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT st = " abcasdsabcae " NEW_LINE if function ( st ) : print ( " Yes , ▁ both ▁ halves ▁ differ ▁ " , " by ▁ at ▁ least ▁ one ▁ character " ) NEW_LINE else : print ( " No , ▁ both ▁ halves ▁ do ▁ not ▁ differ ▁ at ▁ all " ) NEW_LINE