codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Areas of Rectangles possible for an array | Function to find area of rectangles ; sorting the array in descending order ; store the final sum of all the rectangles area possible ; temporary variable to store the length of rectangle ; Selecting the length of rectangle so that difference between any two number is 1 only . Here length is selected so flag is set ; flag is set means we have got length of rectangle ; length is set to a [ i + 1 ] so that if a [ i + 1 ] is less than a [ i ] by 1 then also we have the correct chice for length ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Selecting the width of rectangle so that difference between any two number is 1 only . Here width is selected so now flag is again unset for next rectangle ; area is calculated for rectangle ; flag is set false for another rectangle which we can get from elements in array ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Driver code
def MaxTotalRectangleArea ( a , n ) : NEW_LINE INDENT a . sort ( reverse = True ) NEW_LINE sum = 0 NEW_LINE flag = False NEW_LINE len = 0 NEW_LINE i = 0 NEW_LINE while ( i < n - 1 ) : NEW_LINE INDENT if ( i != 0 ) : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT if ( ( a [ i ] == a [ i + 1 ] or a [ i ] - a [ i + 1 ] == 1 ) and flag == False ) : NEW_LINE INDENT flag = True NEW_LINE len = a [ i + 1 ] NEW_LINE i = i + 1 NEW_LINE DEDENT elif ( ( a [ i ] == a [ i + 1 ] or a [ i ] - a [ i + 1 ] == 1 ) and flag == True ) : NEW_LINE INDENT sum = sum + a [ i + 1 ] * len NEW_LINE flag = False NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT a = [ 10 , 10 , 10 , 10 , 11 , 10 , 11 , 10 , 9 , 9 , 8 , 8 ] NEW_LINE n = len ( a ) NEW_LINE print ( MaxTotalRectangleArea ( a , n ) ) NEW_LINE
Insertion sort to sort even and odd positioned elements in different orders | Function to calculate the given problem . ; checking for odd positioned . ; Inserting even positioned elements in ascending order . ; sorting the even positioned . ; Inserting odd positioned elements in descending order . ; A utility function to print an array of size n ; Driver program
def evenOddInsertionSort ( arr , n ) : NEW_LINE INDENT for i in range ( 2 , n ) : NEW_LINE INDENT j = i - 2 NEW_LINE temp = arr [ i ] NEW_LINE if ( ( i + 1 ) & 1 == 1 ) : NEW_LINE INDENT while ( temp >= arr [ j ] and j >= 0 ) : NEW_LINE INDENT arr [ j + 2 ] = arr [ j ] NEW_LINE j -= 2 NEW_LINE DEDENT arr [ j + 2 ] = temp NEW_LINE DEDENT else : NEW_LINE INDENT while ( temp <= arr [ j ] and j >= 0 ) : NEW_LINE INDENT arr [ j + 2 ] = arr [ j ] NEW_LINE j -= 2 NEW_LINE DEDENT arr [ j + 2 ] = temp NEW_LINE DEDENT DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 12 , 11 , 13 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE evenOddInsertionSort ( arr , n ) NEW_LINE printArray ( arr , n ) NEW_LINE
Stable sort for descending order | Bubble sort implementation to sort elements in descending order . ; Sorts a [ ] in descending order using bubble sort . ; Driver code
def print1 ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n + 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT def sort ( a , n ) : NEW_LINE INDENT for i in range ( n , 0 , - 1 ) : NEW_LINE INDENT for j in range ( n , n - i , - 1 ) : NEW_LINE INDENT if ( a [ j ] > a [ j - 1 ] ) : NEW_LINE INDENT a [ j ] , a [ j - 1 ] = a [ j - 1 ] , a [ j ] NEW_LINE DEDENT DEDENT DEDENT print1 ( a , n ) NEW_LINE DEDENT n = 7 NEW_LINE a = [ 2 , 4 , 3 , 2 , 4 , 5 , 3 ] NEW_LINE sort ( a , n - 1 ) NEW_LINE
Subtraction in the Array | Function to perform the given operation on arr [ ] ; Skip elements which are 0 ; Pick smallest non - zero element ; If all the elements of arr [ ] are 0 ; Driver code
def operations ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE i = 0 ; sum = 0 ; NEW_LINE while ( k > 0 ) : NEW_LINE INDENT while ( i < n and arr [ i ] - sum == 0 ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( i < n and arr [ i ] - sum > 0 ) : NEW_LINE INDENT print ( arr [ i ] - sum , end = " ▁ " ) ; NEW_LINE sum = arr [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) ; NEW_LINE DEDENT k -= 1 ; NEW_LINE DEDENT DEDENT k = 5 ; NEW_LINE arr = [ 3 , 6 , 4 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE operations ( arr , n , k ) ; NEW_LINE
Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | Python C ++ program to sort an array of 0 s 1 s and 2 s . ; Variables to maintain the count of 0 ' s , ▁ ▁ 1' s and 2 's in the array ; Putting the 0 's in the array in starting. ; Putting the 1 ' s ▁ in ▁ the ▁ array ▁ after ▁ the ▁ 0' s . ; Putting the 2 ' s ▁ in ▁ the ▁ array ▁ after ▁ the ▁ 1' s ; Prints the array ; Driver code
import math NEW_LINE def sort012 ( arr , n ) : NEW_LINE INDENT count0 = 0 NEW_LINE count1 = 0 NEW_LINE count2 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count0 = count0 + 1 NEW_LINE DEDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT count1 = count1 + 1 NEW_LINE DEDENT if ( arr [ i ] == 2 ) : NEW_LINE INDENT count2 = count2 + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , count0 ) : NEW_LINE INDENT arr [ i ] = 0 NEW_LINE DEDENT for i in range ( count0 , ( count0 + count1 ) ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT for i in range ( ( count0 + count1 ) , n ) : NEW_LINE INDENT arr [ i ] = 2 NEW_LINE DEDENT return NEW_LINE DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT arr = [ 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE sort012 ( arr , n ) NEW_LINE printArray ( arr , n ) NEW_LINE
Alternate Lower Upper String Sort | Python3 program for unusul string sorting ; Function for alternate sorting of string ; Count occurrences of individual lower case and upper case characters ; Traverse through count arrays and one by one pick characters . Below loop takes O ( n ) time considering the MAX is constant . ; Driver code
MAX = 26 NEW_LINE def alternateSort ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE lCount = [ 0 for i in range ( MAX ) ] NEW_LINE uCount = [ 0 for i in range ( MAX ) ] NEW_LINE s = list ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] . isupper ( ) ) : NEW_LINE INDENT uCount [ ord ( s [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT lCount [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT i = 0 NEW_LINE j = 0 NEW_LINE k = 0 NEW_LINE while ( k < n ) : NEW_LINE INDENT while ( i < MAX and uCount [ i ] == 0 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i < MAX ) : NEW_LINE INDENT s [ k ] = chr ( ord ( ' A ' ) + i ) NEW_LINE k += 1 NEW_LINE uCount [ i ] -= 1 NEW_LINE DEDENT while ( j < MAX and lCount [ j ] == 0 ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( j < MAX ) : NEW_LINE INDENT s [ k ] = chr ( ord ( ' a ' ) + j ) NEW_LINE k += 1 NEW_LINE lCount [ j ] -= 1 NEW_LINE DEDENT DEDENT print ( " " . join ( s ) ) NEW_LINE DEDENT str = " bAwutndekWEdkd " NEW_LINE alternateSort ( str ) NEW_LINE
Sum of Manhattan distances between all pairs of points | Return the sum of distance between all the pair of points . ; for each point , finding distance to rest of the point ; Driven Code
def distancesum ( x , y , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT sum += ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) ) NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT x = [ - 1 , 1 , 3 , 2 ] NEW_LINE y = [ 5 , 6 , 5 , 3 ] NEW_LINE n = len ( x ) NEW_LINE print ( distancesum ( x , y , n ) ) NEW_LINE
Sum of Manhattan distances between all pairs of points | Return the sum of distance of one axis . ; sorting the array . ; for each point , finding the distance . ; Driven Code
def distancesum ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE res = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT res += ( arr [ i ] * i - sum ) NEW_LINE sum += arr [ i ] NEW_LINE DEDENT return res NEW_LINE DEDENT def totaldistancesum ( x , y , n ) : NEW_LINE INDENT return distancesum ( x , n ) + distancesum ( y , n ) NEW_LINE DEDENT x = [ - 1 , 1 , 3 , 2 ] NEW_LINE y = [ 5 , 6 , 5 , 3 ] NEW_LINE n = len ( x ) NEW_LINE print ( totaldistancesum ( x , y , n ) ) NEW_LINE
Sort an array with swapping only with a special element is allowed | n is total number of elements . index is index of 999 or space . k is number of elements yet to be sorted . ; Print the sorted array when loop reaches the base case ; Else if k > 0 and space is at 0 th index swap each number with space and store index at second last index ; If space is neither start of array nor last element of array and element before it greater than / the element next to space ; First swap space and element next to space in case of { 3 , 999 , 2 } make it { 3 , 2 , 999 } ; Than swap space and greater element convert { 3 , 2 , 999 } to { 999 , 2 , 3 } ; Wrapper over sortRec . ; Find index of space ( or 999 ) ; Invalid input ; Driver Code
def sortRec ( arr , index , k , n ) : NEW_LINE INDENT if ( k == 0 ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( 999 , end = " " ) NEW_LINE DEDENT elif ( k > 0 and index == 0 ) : NEW_LINE INDENT index = n - 2 NEW_LINE for i in range ( 1 , index + 1 ) : NEW_LINE INDENT arr [ i - 1 ] = arr [ i ] NEW_LINE DEDENT arr [ index ] = 999 NEW_LINE DEDENT if ( index - 1 >= 0 and index + 1 < n and arr [ index - 1 ] > arr [ index + 1 ] ) : NEW_LINE INDENT arr [ index ] , arr [ index + 1 ] = arr [ index + 1 ] , arr [ index ] NEW_LINE arr [ index - 1 ] , arr [ index + 1 ] = arr [ index + 1 ] , arr [ index - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT if ( index - 1 < 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT arr [ index ] , arr [ index - 1 ] = arr [ index - 1 ] , arr [ index ] NEW_LINE DEDENT sortRec ( arr , index - 1 , k - 1 , n ) NEW_LINE DEDENT def sort ( arr , n ) : NEW_LINE INDENT index = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 999 ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return NEW_LINE DEDENT sortRec ( arr , index , n , n ) NEW_LINE DEDENT arr = [ 3 , 2 , 999 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE sort ( arr , n ) NEW_LINE
Find array with k number of merge sort calls | Python program to find an array that can be sorted with k merge sort calls . ; We make two recursive calls , so reduce k by 2. ; Create an array with values in [ 1 , n ] ; calling unsort function ; Driver code
def unsort ( l , r , a , k ) : NEW_LINE INDENT if ( k < 1 or l + 1 == r ) : NEW_LINE INDENT return NEW_LINE DEDENT k -= 2 NEW_LINE mid = ( l + r ) // 2 NEW_LINE temp = a [ mid - 1 ] NEW_LINE a [ mid - 1 ] = a [ mid ] NEW_LINE a [ mid ] = temp NEW_LINE unsort ( l , mid , a , k ) NEW_LINE unsort ( mid , r , a , k ) NEW_LINE DEDENT def arrayWithKCalls ( n , k ) : NEW_LINE INDENT if ( k % 2 == 0 ) : NEW_LINE INDENT print ( " NO ▁ SOLUTION " ) NEW_LINE return NEW_LINE DEDENT a = [ 0 for i in range ( n + 2 ) ] NEW_LINE a [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = i + 1 NEW_LINE DEDENT k -= 1 NEW_LINE unsort ( 0 , n , a , k ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE k = 17 NEW_LINE arrayWithKCalls ( n , k ) NEW_LINE
Program to sort string in descending order | Python program to sort a string of characters in descending order ; function to print string in sorted order ; Hash array to keep count of characters . Initially count of all charters is initialized to zero . ; Traverse string and increment count of characters ; ' a ' - ' a ' will be 0 , ' b ' - ' a ' will be 1 , so for location of character in count array we wil do str [ i ] - ' a ' . ; Traverse the hash array and print characters ; Driver program to test above function
MAX_CHAR = 26 ; NEW_LINE def sortString ( str ) : NEW_LINE INDENT charCount = [ 0 ] * MAX_CHAR ; NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT charCount [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( MAX_CHAR - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( charCount [ i ] ) : NEW_LINE INDENT print ( chr ( 97 + i ) , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT s = " alkasingh " ; NEW_LINE sortString ( s ) ; NEW_LINE
Median after K additional integers | Find median of array after adding k elements ; sorting the array in increasing order . ; printing the median of array . Since n + K is always odd and K < n , so median of array always lies in the range of n . ; driver function
def printMedian ( arr , n , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE print ( arr [ int ( ( n + K ) / 2 ) ] ) NEW_LINE DEDENT arr = [ 5 , 3 , 2 , 8 ] NEW_LINE k = 3 NEW_LINE n = len ( arr ) NEW_LINE printMedian ( arr , n , k ) NEW_LINE
Dual pivot Quicksort | Python3 program to implement dual pivot QuickSort ; lp means left pivot and rp means right pivot ; p is the left pivot , and q is the right pivot . ; If elements are less than the left pivot ; If elements are greater than or equal to the right pivot ; Bring pivots to their appropriate positions . ; Returning the indices of the pivots ; Driver code
def dualPivotQuickSort ( arr , low , high ) : NEW_LINE INDENT if low < high : NEW_LINE INDENT lp , rp = partition ( arr , low , high ) NEW_LINE dualPivotQuickSort ( arr , low , lp - 1 ) NEW_LINE dualPivotQuickSort ( arr , lp + 1 , rp - 1 ) NEW_LINE dualPivotQuickSort ( arr , rp + 1 , high ) NEW_LINE DEDENT DEDENT def partition ( arr , low , high ) : NEW_LINE INDENT if arr [ low ] > arr [ high ] : NEW_LINE INDENT arr [ low ] , arr [ high ] = arr [ high ] , arr [ low ] NEW_LINE DEDENT j = k = low + 1 NEW_LINE g , p , q = high - 1 , arr [ low ] , arr [ high ] NEW_LINE while k <= g : NEW_LINE INDENT if arr [ k ] < p : NEW_LINE INDENT arr [ k ] , arr [ j ] = arr [ j ] , arr [ k ] NEW_LINE j += 1 NEW_LINE DEDENT elif arr [ k ] >= q : NEW_LINE INDENT while arr [ g ] > q and k < g : NEW_LINE INDENT g -= 1 NEW_LINE DEDENT arr [ k ] , arr [ g ] = arr [ g ] , arr [ k ] NEW_LINE g -= 1 NEW_LINE if arr [ k ] < p : NEW_LINE INDENT arr [ k ] , arr [ j ] = arr [ j ] , arr [ k ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT k += 1 NEW_LINE DEDENT j -= 1 NEW_LINE g += 1 NEW_LINE arr [ low ] , arr [ j ] = arr [ j ] , arr [ low ] NEW_LINE arr [ high ] , arr [ g ] = arr [ g ] , arr [ high ] NEW_LINE return j , g NEW_LINE DEDENT arr = [ 24 , 8 , 42 , 75 , 29 , 77 , 38 , 57 ] NEW_LINE dualPivotQuickSort ( arr , 0 , 7 ) NEW_LINE print ( ' Sorted ▁ array : ▁ ' , end = ' ' ) NEW_LINE for i in arr : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE
A sorting algorithm that slightly improves on selection sort | Python3 program to implement min max selection sort . ; shifting the min . ; Shifting the max . The equal condition happens if we shifted the max to arr [ min_i ] in the previous swap . ; Driver code
def minMaxSelectionSort ( arr , n ) : NEW_LINE INDENT i = 0 NEW_LINE j = n - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT min = arr [ i ] NEW_LINE max = arr [ i ] NEW_LINE min_i = i NEW_LINE max_i = i NEW_LINE for k in range ( i , j + 1 , 1 ) : NEW_LINE INDENT if ( arr [ k ] > max ) : NEW_LINE INDENT max = arr [ k ] NEW_LINE max_i = k NEW_LINE DEDENT elif ( arr [ k ] < min ) : NEW_LINE INDENT min = arr [ k ] NEW_LINE min_i = k NEW_LINE DEDENT DEDENT temp = arr [ i ] NEW_LINE arr [ i ] = arr [ min_i ] NEW_LINE arr [ min_i ] = temp NEW_LINE if ( arr [ min_i ] == max ) : NEW_LINE INDENT temp = arr [ j ] NEW_LINE arr [ j ] = arr [ min_i ] NEW_LINE arr [ min_i ] = temp NEW_LINE DEDENT else : NEW_LINE INDENT temp = arr [ j ] NEW_LINE arr [ j ] = arr [ max_i ] NEW_LINE arr [ max_i ] = temp NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT print ( " Sorted ▁ array : " , end = " ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 23 , 78 , 45 , 8 , 32 , 56 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE minMaxSelectionSort ( arr , n ) NEW_LINE DEDENT
Sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " | Python 3 program to sort an array based on absolute difference with a given value x . ; Below lines are similar to insertion sort ; Insert arr [ i ] at correct place ; Function to print the array ; Driver Code
def arrange ( arr , n , x ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT diff = abs ( arr [ i ] - x ) NEW_LINE j = i - 1 NEW_LINE if ( abs ( arr [ j ] - x ) > diff ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE while ( abs ( arr [ j ] - x ) > diff and j >= 0 ) : NEW_LINE INDENT arr [ j + 1 ] = arr [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT arr [ j + 1 ] = temp NEW_LINE DEDENT DEDENT DEDENT def print_1 ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 3 , 9 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE x = 7 NEW_LINE arrange ( arr , n , x ) NEW_LINE print_1 ( arr , n ) NEW_LINE DEDENT
Check if a grid can become row | v [ ] is vector of strings . len is length of strings in every row . ; Driver code ; l = 5 Length of strings
def check ( v , l ) : NEW_LINE INDENT n = len ( v ) NEW_LINE for i in v : NEW_LINE INDENT i = ' ' . join ( sorted ( i ) ) NEW_LINE DEDENT for i in range ( l - 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( v [ i ] [ j ] > v [ i + 1 ] [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT v = [ " ebcda " , " ihgfj " , " klmno " , " pqrst " , " yvwxu " ] NEW_LINE if check ( v , l ) : NEW_LINE INDENT print " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT print " No " NEW_LINE DEDENT
Sort first half in ascending and second half in descending order | 1 | function to print half of the array in ascending order and the other half in descending order ; sorting the array ; printing first half in ascending order ; printing second half in descending order ; Driver code
def printOrder ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT for j in range ( n - 1 , n // 2 - 1 , - 1 ) : NEW_LINE INDENT print ( arr [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , - 1 ] NEW_LINE n = len ( arr ) NEW_LINE printOrder ( arr , n ) NEW_LINE DEDENT
Find the Sub | Python 3 program to find subarray with sum closest to 0 ; Function to find the subarray ; Pick a starting point ; Consider current starting point as a subarray and update minimum sum and subarray indexes ; Try all subarrays starting with i ; update minimum sum and subarray indexes ; Return starting and ending indexes ; Driver Code
import sys NEW_LINE def findSubArray ( arr , n ) : NEW_LINE INDENT min_sum = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr_sum = arr [ i ] NEW_LINE if ( min_sum > abs ( curr_sum ) ) : NEW_LINE INDENT min_sum = abs ( curr_sum ) NEW_LINE start = i NEW_LINE end = i NEW_LINE DEDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT curr_sum = curr_sum + arr [ j ] NEW_LINE if ( min_sum > abs ( curr_sum ) ) : NEW_LINE INDENT min_sum = abs ( curr_sum ) NEW_LINE start = i NEW_LINE end = j NEW_LINE DEDENT DEDENT DEDENT p = [ start , end ] NEW_LINE return p NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , - 5 , 4 , - 6 , - 3 ] NEW_LINE n = len ( arr ) NEW_LINE point = findSubArray ( arr , n ) NEW_LINE print ( " Subarray ▁ starting ▁ from ▁ " , end = " " ) NEW_LINE print ( point [ 0 ] , " to " , point [ 1 ] ) NEW_LINE DEDENT
Find shortest unique prefix for every word in a given list | Set 2 ( Using Sorting ) | Python3 program to prshortest unique prefixes for every word . ; Create an array to store the results ; Sort the array of strings ; Compare the first with its only right neighbor ; Store the unique prefix of a [ 1 ] from its left neighbor ; Compute common prefix of a [ i ] unique from its right neighbor ; Compare the new prefix with previous prefix ; Store the prefix of a [ i + 1 ] unique from its left neighbour ; Compute the unique prefix for the last in sorted array ; Driver Code
def uniquePrefix ( a ) : NEW_LINE INDENT size = len ( a ) NEW_LINE res = [ 0 ] * ( size ) NEW_LINE a = sorted ( a ) NEW_LINE j = 0 NEW_LINE while ( j < min ( len ( a [ 0 ] ) - 1 , len ( a [ 1 ] ) - 1 ) ) : NEW_LINE INDENT if ( a [ 0 ] [ j ] == a [ 1 ] [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT ind = 0 NEW_LINE res [ ind ] = a [ 0 ] [ 0 : j + 1 ] NEW_LINE ind += 1 NEW_LINE temp_prefix = a [ 1 ] [ 0 : j + 1 ] NEW_LINE for i in range ( 1 , size - 1 ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < min ( len ( a [ i ] ) - 1 , len ( a [ i + 1 ] ) - 1 ) ) : NEW_LINE INDENT if ( a [ i ] [ j ] == a [ i + 1 ] [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT new_prefix = a [ i ] [ 0 : j + 1 ] NEW_LINE if ( len ( temp_prefix ) > len ( new_prefix ) ) : NEW_LINE INDENT res [ ind ] = temp_prefix NEW_LINE ind += 1 NEW_LINE DEDENT else : NEW_LINE INDENT res [ ind ] = new_prefix NEW_LINE ind += 1 NEW_LINE DEDENT temp_prefix = a [ i + 1 ] [ 0 : j + 1 ] NEW_LINE DEDENT j = 0 NEW_LINE sec_last = a [ size - 2 ] NEW_LINE last = a [ size - 1 ] NEW_LINE while ( j < min ( len ( sec_last ) - 1 , len ( last ) - 1 ) ) : NEW_LINE INDENT if ( sec_last [ j ] == last [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT res [ ind ] = last [ 0 : j + 1 ] NEW_LINE return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT input = [ " zebra " , " dog " , " duck " , " dove " ] NEW_LINE output = uniquePrefix ( input ) NEW_LINE print ( " The ▁ shortest ▁ unique ▁ prefixes ▁ " + " in ▁ sorted ▁ order ▁ are ▁ : ▁ " ) NEW_LINE for i in output : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Find the minimum and maximum amount to buy all N candies | Function to find the minimum amount to buy all candies ; Buy current candy ; And take k candies for free from the last ; Function to find the maximum amount to buy all candies ; Buy candy with maximum amount ; And get k candies for free from the starting ; Driver code ; Function call
def findMinimum ( arr , n , k ) : NEW_LINE INDENT res = 0 NEW_LINE i = 0 NEW_LINE while ( n ) : NEW_LINE INDENT res += arr [ i ] NEW_LINE n = n - k NEW_LINE i += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def findMaximum ( arr , n , k ) : NEW_LINE INDENT res = 0 NEW_LINE index = 0 NEW_LINE i = n - 1 NEW_LINE while ( i >= index ) : NEW_LINE INDENT res += arr [ i ] NEW_LINE index += k NEW_LINE i -= 1 NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 3 , 2 , 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE arr . sort ( ) NEW_LINE print ( findMinimum ( arr , n , k ) , " ▁ " , findMaximum ( arr , n , k ) ) NEW_LINE