codeparrot/xlcost-text-to-code huggingface dataset:
Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y | Function to check if it is possible to choose X and Y elements from a [ ] and b [ ] such that maximum element among X element is less than minimum element among Y elements ; Check if there are atleast X elements in arr1 [ ] and atleast Y elements in arr2 [ ] ; Sort arrays in ascending order ; Check if ( X - 1 ) - th element in arr1 [ ] is less than from M - Yth element in arr2 [ ] ; Return false ; Driver Code ; Function Call
def check ( a , b , Na , Nb , k , m ) : NEW_LINE INDENT if ( Na < k or Nb < m ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT a . sort ( ) NEW_LINE a . sort ( ) NEW_LINE if ( a [ k - 1 ] < b [ Nb - m ] ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT arr1 = [ 1 , 2 , 3 ] NEW_LINE arr2 = [ 3 , 4 , 5 ] NEW_LINE N = len ( arr1 ) NEW_LINE M = len ( arr2 ) NEW_LINE X = 2 NEW_LINE Y = 1 NEW_LINE print ( check ( arr1 , arr2 , N , M , X , Y ) ) NEW_LINE
Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum | Function to split the array into two subset such that the Bitwise XOR between the maximum of one subset and minimum of other is minimum ; Sort the array in increasing order ; Calculating the min Bitwise XOR between consecutive elements ; Return the final minimum Bitwise XOR ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
def splitArray ( arr , N ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE result = 10 ** 9 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT result = min ( result , arr [ i ] ^ arr [ i - 1 ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 2 , 6 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( splitArray ( arr , N ) ) NEW_LINE DEDENT
Sort an array by left shifting digits of array elements | Function to check if an array is sorted in increasing order or not ; Traverse the array ; Function to sort the array by left shifting digits of array elements ; Stores previous array element ; Traverse the array arr [ ] ; Stores current element ; Stores current element in string format ; Left - shift digits of current element in all possible ways ; Left - shift digits of current element by idx ; If temp greater than or equal to prev and temp less than optEle ; Update arr [ i ] ; Update prev ; If arr is in increasing order ; Otherwise ; Driver Code
def isIncreasing ( arr ) : NEW_LINE INDENT for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT if arr [ i ] > arr [ i + 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def sortArr ( arr ) : NEW_LINE INDENT prev = - 1 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT optEle = arr [ i ] NEW_LINE strEle = str ( arr [ i ] ) NEW_LINE for idx in range ( len ( strEle ) ) : NEW_LINE INDENT temp = int ( strEle [ idx : ] + strEle [ : idx ] ) NEW_LINE if temp >= prev and temp < optEle : NEW_LINE INDENT optEle = temp NEW_LINE DEDENT DEDENT arr [ i ] = optEle NEW_LINE prev = arr [ i ] NEW_LINE DEDENT if isIncreasing ( arr ) : NEW_LINE INDENT return arr NEW_LINE DEDENT else : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 511 , 321 , 323 , 432 , 433 ] NEW_LINE res = sortArr ( arr ) NEW_LINE for i in res : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Activity selection problem with K persons | Python3 program for the above approach ; Function to find maximum shops that can be visited by K persons ; Store opening and closing time of shops ; Sort the pair of array ; Stores the result ; Stores current number of persons visiting some shop with their ending time ; Check if current shop can be assigned to a person who 's  already visiting any other shop ; Checks if there is any person whose closing time <= current shop opening time ; Erase previous shop visited by the person satisfying the condition ; Insert new closing time of current shop for the person satisfying a1he condition ; Increment the count by one ; In case if no person have closing time <= current shop opening time but there are some persons left ; Finally pr the ans ; Driver Code ; Given starting and ending time ; Given K and N ; Function call
from bisect import bisect_left NEW_LINE def maximumShops ( opening , closing , n , k ) : NEW_LINE INDENT a = [ [ 0 , 0 ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] [ 0 ] = opening [ i ] NEW_LINE a [ i ] [ 1 ] = closing [ i ] NEW_LINE DEDENT a = sorted ( a ) NEW_LINE count = 1 NEW_LINE st = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT flag = False NEW_LINE if ( len ( st ) == 0 ) : NEW_LINE INDENT ar = list ( st . keys ( ) ) NEW_LINE it = bisect_left ( ar , a [ i ] [ 0 ] ) NEW_LINE if ( it != 0 ) : NEW_LINE INDENT it -= 1 NEW_LINE if ( ar [ it ] <= a [ i ] [ 0 ] ) : NEW_LINE INDENT del st [ it ] NEW_LINE st [ a [ i ] [ 1 ] ] = 1 NEW_LINE count += 1 NEW_LINE flag = True NEW_LINE DEDENT DEDENT DEDENT if ( len ( st ) < k and flag == False ) : NEW_LINE INDENT st [ a [ i ] [ 1 ] ] = 1 NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = [ 1 , 8 , 3 , 2 , 6 ] NEW_LINE E = [ 5 , 10 , 6 , 5 , 9 ] NEW_LINE K , N = 2 , len ( S ) NEW_LINE print ( maximumShops ( S , E , N , K ) ) NEW_LINE DEDENT
Maximize maximum possible subarray sum of an array by swapping with elements from another array | Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ] ; Stores elements from the arrays arr [ ] and brr [ ] ; Store elements of array arr [ ] and brr [ ] in the vector crr ; Sort the vector crr in descending order ; Stores maximum sum ; Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element ; Print the sum ; Driver code ; Given arrays and respective lengths ; Calculate maximum subarray sum
def maxSum ( arr , brr , N , K ) : NEW_LINE INDENT crr = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT crr . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( K ) : NEW_LINE INDENT crr . append ( brr [ i ] ) NEW_LINE DEDENT crr = sorted ( crr ) [ : : - 1 ] NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( crr [ i ] > 0 ) : NEW_LINE INDENT sum += crr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 2 , - 1 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE brr = [ 1 , 2 , 3 , 2 ] NEW_LINE K = len ( brr ) NEW_LINE maxSum ( arr , brr , N , K ) NEW_LINE DEDENT
Count pairs with Bitwise XOR greater than both the elements of the pair | Function that counts the pairs whose Bitwise XOR is greater than both the elements of pair ; Stores the count of pairs ; Generate all possible pairs ; Find the Bitwise XOR ; Find the maximum of two ; If xo < mx , increment count ; Print the value of count ; Driver Code ; Function Call
def countPairs ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT xo = ( A [ i ] ^ A [ j ] ) NEW_LINE mx = max ( A [ i ] , A [ j ] ) NEW_LINE if ( xo > mx ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE DEDENT
Possible arrangement of persons waiting to sit in a hall | Function to find the arrangement of seating ; Stores the row in which the ith person sits ; Stores the width of seats along with their index or row number ; Sort the array ; Store the seats and row for boy 's seat ; Stores the index of row upto which boys have taken seat ; Iterate the string ; Push the row number at index in vector and heap ; Increment the index to let the next boy in the next minimum width vacant row ; Otherwise ; If girl then take top of element of the max heap ; Pop from queue ; Print the values ; Driver Code ; Given N ; Given arr [ ] ; Given string ; Function Call
def findTheOrder ( arr , s , N ) : NEW_LINE INDENT ans = [ ] NEW_LINE A = [ [ ] for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT A [ i ] = [ arr [ i ] , i + 1 ] NEW_LINE DEDENT A = sorted ( A ) NEW_LINE q = [ ] NEW_LINE index = 0 NEW_LINE for i in range ( 2 * N ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT ans . append ( A [ index ] [ 1 ] ) NEW_LINE q . append ( A [ index ] ) NEW_LINE index += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( q [ - 1 ] [ 1 ] ) NEW_LINE del q [ - 1 ] NEW_LINE DEDENT q = sorted ( q ) NEW_LINE DEDENT for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE arr = [ 2 , 1 , 3 ] NEW_LINE s = "001011" NEW_LINE findTheOrder ( arr , s , N ) NEW_LINE DEDENT
Difference between sum of K maximum even and odd array elements | Function to find the absolute difference between sum of first K maximum even and odd numbers ; Stores index from where odd number starts ; Segregate even and odd number ; If current element is even ; Sort in decreasing order even part ; Sort in decreasing order odd part ; Calculate sum of k maximum even number ; Calculate sum of k maximum odd number ; Print the absolute difference ; Given array [ ] arr ; Size of array ; Function Call
def evenOddDiff ( a , n , k ) : NEW_LINE INDENT j = - 1 NEW_LINE even = [ ] NEW_LINE odd = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT even . append ( a [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT odd . append ( a [ i ] ) NEW_LINE DEDENT DEDENT j += 1 NEW_LINE even . sort ( ) NEW_LINE even . reverse ( ) NEW_LINE odd . sort ( ) NEW_LINE odd . reverse ( ) NEW_LINE evenSum , oddSum = 0 , 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT evenSum += even [ i ] NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT oddSum += odd [ i ] NEW_LINE DEDENT print ( abs ( evenSum - oddSum ) ) NEW_LINE DEDENT arr = [ 1 , 8 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE evenOddDiff ( arr , N , K ) NEW_LINE
Rearrange array to make product of prefix sum array non zero | Function to rearrange array that satisfies the given condition ; Stores sum of elements of the given array ; Calculate totalSum ; If the totalSum is equal to 0 ; No possible way to rearrange array ; If totalSum exceeds 0 ; Rearrange the array in descending order ; Otherwise ; Rearrange the array in ascending order ; Driver Code
def rearrangeArr ( arr , N ) : NEW_LINE INDENT totalSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE DEDENT if ( totalSum == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT elif ( totalSum > 0 ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE print ( * arr , sep = ' ▁ ' ) NEW_LINE DEDENT else : NEW_LINE INDENT arr . sort ( ) NEW_LINE print ( * arr , sep = ' ▁ ' ) NEW_LINE DEDENT DEDENT arr = [ 1 , - 1 , - 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE rearrangeArr ( arr , N ) ; NEW_LINE
Count ways to distribute exactly one coin to each worker | Python3 program for the above approach ; Function to find number of way to distribute coins giving exactly one coin to each person ; Sort the given arrays ; Start from bigger salary ; Increment the amount ; Reduce amount of valid coins by one each time ; Return the result ; Driver code ; Given two arrays ; Function call
MOD = 1000000007 NEW_LINE def solve ( values , salary ) : NEW_LINE INDENT ret = 1 NEW_LINE amt = 0 NEW_LINE values = sorted ( values ) NEW_LINE salary = sorted ( salary ) NEW_LINE while ( len ( salary ) > 0 ) : NEW_LINE INDENT while ( ( len ( values ) and values [ - 1 ] >= salary [ - 1 ] ) ) : NEW_LINE INDENT amt += 1 NEW_LINE del values [ - 1 ] NEW_LINE DEDENT if ( amt == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ret *= amt NEW_LINE amt -= 1 NEW_LINE ret %= MOD NEW_LINE del salary [ - 1 ] NEW_LINE DEDENT return ret NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT values = [ 1 , 2 ] NEW_LINE salary = [ 2 ] NEW_LINE print ( solve ( values , salary ) ) NEW_LINE DEDENT
Count of index pairs with equal elements in an array | Set 2 | Function that counts the pair in the array arr [ ] ; Sort the array ; Initialize two pointers ; Add all valid pairs to answer ; Return the answer ; Driver Code ; Given array arr [ ] ; Function call
def countPairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE arr . sort ( ) NEW_LINE left = 0 NEW_LINE right = 1 ; NEW_LINE while ( right < n ) : NEW_LINE INDENT if ( arr [ left ] == arr [ right ] ) : NEW_LINE INDENT ans += right - left ; NEW_LINE DEDENT else : NEW_LINE INDENT left = right ; NEW_LINE DEDENT right += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 2 , 3 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Maximize the Sum of the given array using given operations | Comparator to sort the array in ascending order ; Function to maximise the sum of the given array ; Stores { A [ i ] , B [ i ] } pairs ; Sort in descending order of the values in the array A [ ] ; Stores the maximum sum ; If B [ i ] is equal to 0 ; Simply add A [ i ] to the sum ; Add the highest K numbers ; Subtract from the sum ; Return the sum ; Driver Code
def compare ( p1 , p2 ) : NEW_LINE INDENT return p1 [ 0 ] > p2 [ 0 ] NEW_LINE DEDENT def maximiseScore ( A , B , K , N ) : NEW_LINE INDENT pairs = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT pairs . append ( [ A [ i ] , B [ i ] ] ) NEW_LINE DEDENT pairs . sort ( key = lambda x : x [ 0 ] , reverse = True ) NEW_LINE Sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( pairs [ i ] [ 1 ] == 0 ) : NEW_LINE INDENT Sum += pairs [ i ] [ 0 ] NEW_LINE DEDENT elif ( pairs [ i ] [ 1 ] == 1 ) : NEW_LINE INDENT if ( K > 0 ) : NEW_LINE INDENT Sum += pairs [ i ] [ 0 ] NEW_LINE K -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT Sum -= pairs [ i ] [ 0 ] NEW_LINE DEDENT DEDENT DEDENT return Sum NEW_LINE DEDENT A = [ 5 , 4 , 6 , 2 , 8 ] NEW_LINE B = [ 1 , 0 , 1 , 1 , 0 ] NEW_LINE K = 2 NEW_LINE N = len ( A ) NEW_LINE print ( maximiseScore ( A , B , K , N ) ) NEW_LINE
Minimize Cost to sort a String in Increasing Order of Frequencies of Characters | Python3 program to implement the above approach ; For a single character ; Stores count of repetitions of a character ; If repeating character ; Otherwise ; Store frequency ; Reset count ; Insert the last character block ; Sort the frequencies ; Stores the minimum cost of all operations ; Store the absolute difference of i - th frequencies of ordered and unordered sequences ; Return the minimum cost ; Driver Code
def sortString ( S ) : NEW_LINE INDENT sorted1 = [ ] NEW_LINE original = [ ] NEW_LINE insert = False NEW_LINE if ( len ( S ) == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT curr = 1 NEW_LINE for i in range ( len ( S ) - 1 ) : NEW_LINE INDENT if ( S [ i ] == S [ i + 1 ] ) : NEW_LINE INDENT curr += 1 NEW_LINE insert = False NEW_LINE DEDENT else : NEW_LINE INDENT sorted1 . append ( curr ) NEW_LINE original . append ( curr ) NEW_LINE curr = 1 NEW_LINE insert = True NEW_LINE DEDENT DEDENT if ( ( S [ ( len ( S ) - 1 ) ] != S [ ( len ( S ) - 2 ) ] ) or insert == False ) : NEW_LINE INDENT sorted1 . append ( curr ) NEW_LINE original . append ( curr ) NEW_LINE DEDENT sorted1 . sort ( ) NEW_LINE t_cost = 0 NEW_LINE for i in range ( len ( sorted1 ) ) : NEW_LINE INDENT t_cost += abs ( sorted1 [ i ] - original [ i ] ) NEW_LINE DEDENT return ( t_cost // 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " aabbcccdeffffggghhhhhii " NEW_LINE print ( sortString ( S ) ) NEW_LINE DEDENT
Sort numbers based on count of letters required to represent them in words | letters [ i ] stores the count of letters required to represent the digit i ; Function to return the sum of letters required to represent N ; Function to sort the array according to the sum of letters to represent n ; List to store the digit sum with respective elements ; Inserting digit sum with elements in the list pair ; Making the vector pair ; Sort the list , this will sort the pair according to the sum of letters to represent n ; Print the sorted list content ; Driver code
letters = [ 4 , 3 , 3 , 3 , 4 , 4 , 3 , 5 , 5 , 4 ] NEW_LINE def sumOfLetters ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += letters [ n % 10 ] NEW_LINE n = n // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT vp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( [ sumOfLetters ( arr [ i ] ) , arr [ i ] ] ) NEW_LINE DEDENT vp . sort ( key = lambda x : x [ 0 ] ) NEW_LINE for i in vp : NEW_LINE INDENT print ( i [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 10 , 31 , 18 ] NEW_LINE n = len ( arr ) NEW_LINE sortArr ( arr , n ) NEW_LINE DEDENT
Divide a sorted array in K parts with sum of difference of max and min minimized in each part | Function to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Array to store the differences between two adjacent elements ; Iterating through the array ; Appending differences to p ; Sorting p in descending order ; Sum of the first k - 1 values of p ; Computing the result ;
def calculate_minimum_split ( a , k ) : NEW_LINE INDENT p = [ ] NEW_LINE n = len ( a ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT p . append ( a [ i ] - a [ i - 1 ] ) NEW_LINE DEDENT p . sort ( reverse = True ) NEW_LINE min_sum = sum ( p [ : k - 1 ] ) NEW_LINE res = a [ n - 1 ] - a [ 0 ] - min_sum NEW_LINE return res NEW_LINE DEDENT / * Driver code * / NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 8 , 15 , 16 , 23 , 42 ] NEW_LINE K = 3 NEW_LINE print ( calculate_minimum_split ( arr , K ) ) NEW_LINE DEDENT
Sort an Array of dates in ascending order using Custom Comparator | Python3 implementation to sort the array of dates in the form of " DD - MM - YYYY " using custom comparator ; Comparator to sort the array of dates ; Condition to check the year ; Condition to check the month ; Condition to check the day ; Function that prints the dates in ascensding order ; Sort the dates using library sort function with custom Comparator ; Loop to print the dates ; Driver Code
from functools import cmp_to_key NEW_LINE def myCompare ( date1 , date2 ) : NEW_LINE INDENT day1 = date1 [ 0 : 2 ] NEW_LINE month1 = date1 [ 3 : 3 + 2 ] NEW_LINE year1 = date1 [ 6 : 6 + 4 ] NEW_LINE day2 = date2 [ 0 : 2 ] NEW_LINE month2 = date2 [ 3 : 3 + 2 ] NEW_LINE year2 = date2 [ 6 : 6 + 4 ] NEW_LINE if ( year1 < year2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( year1 > year2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( month1 < month2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( month1 > month2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( day1 < day2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( day1 > day2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT def printDatesAscending ( arr ) : NEW_LINE INDENT arr = sorted ( arr , key = cmp_to_key ( lambda a , b : myCompare ( a , b ) ) ) NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT print ( arr [ i ] ) NEW_LINE DEDENT DEDENT arr = [ ] NEW_LINE arr . append ( "25-08-1996" ) NEW_LINE arr . append ( "03-08-1970" ) NEW_LINE arr . append ( "09-04-1994" ) NEW_LINE arr . append ( "29-08-1996" ) NEW_LINE arr . append ( "14-02-1972" ) NEW_LINE printDatesAscending ( arr ) NEW_LINE
Sort an Array of Strings according to the number of Vowels in them | Function to check the Vowel ; Returns count of vowels in str ; Check for vowel ; Function to sort the array according to the number of the vowels ; Vector to store the number of vowels with respective elements ; Inserting number of vowels with respective strings in the vector pair ; Sort the vector , this will sort the pair according to the number of vowels ; Print the sorted vector content ; Driver code
def isVowel ( ch ) : NEW_LINE INDENT ch = ch . upper ( ) ; NEW_LINE return ( ch == ' A ' or ch == ' E ' or ch == ' I ' or ch == ' O ' or ch == ' U ' ) ; NEW_LINE DEDENT def countVowels ( string ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( isVowel ( string [ i ] ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT vp = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( ( countVowels ( 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 = [ " lmno " , " pqrst " , " aeiou " , " xyz " ] ; NEW_LINE n = len ( arr ) ; NEW_LINE sortArr ( arr , n ) ; NEW_LINE DEDENT
Find the minimum cost to cross the River | Function to return the minimum cost ; Sort the price array ; Calculate minimum price of n - 2 most costly person ; Both the ways as discussed above ; Calculate the minimum price of the two cheapest person ; Driver code
def minimumCost ( price , n ) : NEW_LINE INDENT price = sorted ( price ) NEW_LINE totalCost = 0 NEW_LINE for i in range ( n - 1 , 1 , - 2 ) : NEW_LINE INDENT if ( i == 2 ) : NEW_LINE INDENT totalCost += price [ 2 ] + price [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] NEW_LINE price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] NEW_LINE totalCost += min ( price_first , price_second ) NEW_LINE DEDENT DEDENT if ( n == 1 ) : NEW_LINE INDENT totalCost += price [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT totalCost += price [ 1 ] NEW_LINE DEDENT return totalCost NEW_LINE DEDENT price = [ 30 , 40 , 60 , 70 ] NEW_LINE n = len ( price ) NEW_LINE print ( minimumCost ( price , n ) ) NEW_LINE
Minimize the sum of differences of consecutive elements after removing exactly K elements | Python3 implementation of the above approach . ; states of DP ; function to find minimum sum ; base - case ; if state is solved before , return ; marking the state as solved ; recurrence relation ; driver function ; input values ; calling the required function ;
import numpy as np NEW_LINE N = 100 NEW_LINE INF = 1000000 NEW_LINE dp = np . zeros ( ( N , N ) ) ; NEW_LINE vis = np . zeros ( ( N , N ) ) ; NEW_LINE def findSum ( arr , n , k , l , r ) : NEW_LINE INDENT if ( ( l ) + ( n - 1 - r ) == k ) : NEW_LINE INDENT return arr [ r ] - arr [ l ] ; NEW_LINE DEDENT if ( vis [ l ] [ r ] ) : NEW_LINE INDENT return dp [ l ] [ r ] ; NEW_LINE DEDENT vis [ l ] [ r ] = 1 ; NEW_LINE dp [ l ] [ r ] = min ( findSum ( arr , n , k , l , r - 1 ) , findSum ( arr , n , k , l + 1 , r ) ) ; NEW_LINE return dp [ l ] [ r ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 100 , 120 , 140 ] ; NEW_LINE k = 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findSum ( arr , n , k , 0 , n - 1 ) ) ; NEW_LINE DEDENT
Find Kth element in an array containing odd elements first and then even elements | Function to return the kth element in the modified array ; Finding the index from where the even numbers will be stored ; Return the kth element ; Driver code
def getNumber ( n , k ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT pos = n // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT pos = ( n // 2 ) + 1 ; NEW_LINE DEDENT if ( k <= pos ) : NEW_LINE INDENT return ( k * 2 - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( k - pos ) * 2 ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 ; k = 5 ; NEW_LINE print ( getNumber ( n , k ) ) ; NEW_LINE DEDENT