codeparrot/xlcost-text-to-code huggingface dataset:
Sort an array when two halves are sorted | Merge two sorted halves of Array into single sorted array ; Starting index of second half ; Temp Array store sorted resultant array ; First Find the point where array is divide into two half ; If Given array is all - ready sorted ; Merge two sorted arrays in single sorted array ; Copy the remaining elements of A [ i to half_ ! ] ; Copy the remaining elements of A [ half_ ! to n ] ; Driver code ; Print sorted Array
def mergeTwoHalf ( A , n ) : NEW_LINE INDENT half_i = 0 NEW_LINE temp = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( A [ i ] > A [ i + 1 ] ) : NEW_LINE INDENT half_i = i + 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( half_i == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT i = 0 NEW_LINE j = half_i NEW_LINE k = 0 NEW_LINE while ( i < half_i and j < n ) : NEW_LINE INDENT if ( A [ i ] < A [ j ] ) : NEW_LINE INDENT temp [ k ] = A [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp [ k ] = A [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT while i < half_i : NEW_LINE INDENT temp [ k ] = A [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT while ( j < n ) : NEW_LINE INDENT temp [ k ] = A [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT A [ i ] = temp [ i ] NEW_LINE DEDENT DEDENT A = [ 2 , 3 , 8 , - 1 , 7 , 10 ] NEW_LINE n = len ( A ) NEW_LINE mergeTwoHalf ( A , n ) NEW_LINE print ( * A , sep = ' ▁ ' ) NEW_LINE
Chocolate Distribution Problem | arr [ 0. . n - 1 ] represents sizes of packets m is number of students . Returns minimum difference between maximum and minimum values of distribution . ; if there are no chocolates or number of students is 0 ; Sort the given packets ; Number of students cannot be more than number of packets ; Largest number of chocolates ; Find the subarray of size m such that difference between last ( maximum in case of sorted ) and first ( minimum in case of sorted ) elements of subarray is minimum . ; Driver Code ; m = 7 Number of students
def findMinDiff ( arr , n , m ) : NEW_LINE INDENT if ( m == 0 or n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT arr . sort ( ) NEW_LINE if ( n < m ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT min_diff = arr [ n - 1 ] - arr [ 0 ] NEW_LINE for i in range ( len ( arr ) - m + 1 ) : NEW_LINE INDENT min_diff = min ( min_diff , arr [ i + m - 1 ] - arr [ i ] ) NEW_LINE DEDENT return min_diff NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 , 30 , 40 , 28 , 42 , 30 , 44 , 48 , 43 , 50 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ difference ▁ is " , findMinDiff ( arr , n , m ) ) NEW_LINE DEDENT
Absolute distinct count in a sorted array | This function returns number of distinct absolute values among the elements of the array ; set keeps all unique elements ; Driver Code
def distinctCount ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s . add ( abs ( arr [ i ] ) ) NEW_LINE DEDENT return len ( s ) NEW_LINE DEDENT arr = [ - 2 , - 1 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Count ▁ of ▁ absolute ▁ distinct ▁ values : " , distinctCount ( arr , n ) ) NEW_LINE
Absolute distinct count in a sorted array | The function returns return number of distinct absolute values among the elements of the array ; initialize count as number of elements ; Remove duplicate elements from the left of the current window ( i , j ) and also decrease the count ; Remove duplicate elements from the right of the current window ( i , j ) and also decrease the count ; break if only one element is left ; Now look for the zero sum pair in current window ( i , j ) ; decrease the count if ( positive , negative ) pair is encountered ; Driver code
def distinctCount ( arr , n ) : NEW_LINE INDENT count = n ; NEW_LINE i = 0 ; j = n - 1 ; sum = 0 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT while ( i != j and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE i = i + 1 ; NEW_LINE DEDENT while ( i != j and arr [ j ] == arr [ j - 1 ] ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT if ( i == j ) : NEW_LINE INDENT break ; NEW_LINE DEDENT sum = arr [ i ] + arr [ j ] ; NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE i = i + 1 ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT elif ( sum < 0 ) : NEW_LINE INDENT i = i + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT j = j - 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT arr = [ - 2 , - 1 , 0 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " , distinctCount ( arr , n ) ) ; NEW_LINE
Pancake sorting | Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already at  the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; A utility function to print an array of size n ; Driver program
def flip ( arr , i ) : NEW_LINE INDENT start = 0 NEW_LINE while start < i : NEW_LINE INDENT temp = arr [ start ] NEW_LINE arr [ start ] = arr [ i ] NEW_LINE arr [ i ] = temp NEW_LINE start += 1 NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT def findMax ( arr , n ) : NEW_LINE INDENT mi = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] > arr [ mi ] : NEW_LINE INDENT mi = i NEW_LINE DEDENT DEDENT return mi NEW_LINE DEDENT def pancakeSort ( arr , n ) : NEW_LINE INDENT curr_size = n NEW_LINE while curr_size > 1 : NEW_LINE INDENT mi = findMax ( arr , curr_size ) NEW_LINE if mi != curr_size - 1 : NEW_LINE INDENT flip ( arr , mi ) NEW_LINE flip ( arr , curr_size - 1 ) NEW_LINE DEDENT curr_size -= 1 NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( " % d " % ( arr [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 23 , 10 , 20 , 11 , 12 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE pancakeSort ( arr , n ) ; NEW_LINE print ( " Sorted ▁ Array ▁ " ) NEW_LINE printArray ( arr , n ) NEW_LINE
Lexicographically smallest numeric string having odd digit counts | Function to construct lexicographically smallest numeric string having an odd count of each characters ; Stores the resultant string ; If N is even ; Otherwise ; Driver code
def genString ( N ) : NEW_LINE INDENT ans = " " NEW_LINE if ( N % 2 == 0 ) : NEW_LINE INDENT ans = " " . join ( "1" for i in range ( N - 1 ) ) NEW_LINE ans = ans + "2" NEW_LINE DEDENT else : NEW_LINE INDENT ans = " " . join ( "1" for i in range ( N ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE print ( genString ( N ) ) NEW_LINE DEDENT
Count of values chosen for X such that N is reduced to 0 after given operations | Function to check if the value of X reduces N to 0 or not ; Update the value of N as N - x ; Check if x is a single digit integer ; Function to find the number of values X such that N can be reduced to 0 after performing the given operations ; Number of digits in N ; Stores the count of value of X ; Iterate over all possible value of X ; Check if x follow the conditions ; Return the total count ; Driver Code
def check ( x , N ) : NEW_LINE INDENT while True : NEW_LINE INDENT N -= x NEW_LINE if len ( str ( x ) ) == 1 : NEW_LINE INDENT break NEW_LINE DEDENT x = sum ( list ( map ( int , str ( x ) ) ) ) NEW_LINE DEDENT if len ( str ( x ) ) == 1 and N == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def countNoOfsuchX ( N ) : NEW_LINE INDENT k = len ( str ( N ) ) NEW_LINE count = 0 NEW_LINE for x in range ( N - k * ( k + 1 ) * 5 , N + 1 ) : NEW_LINE INDENT if check ( x , N ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = 9939 NEW_LINE print ( countNoOfsuchX ( N ) ) NEW_LINE
Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element | Returns the count of subarrays which contains both the maximum and minimum elements in the given vector ; Initialize the low and high of array ; If current element is less than least element ; If current element is more than highest element ; If current element is equal to low or high then update the pointers ; Update number of subarrays ; Return the result ; Function to find the maximum count of subarrays ; Iterate the array to find the maximum and minimum element ; Vector after removing the minimum element ; Using assignment operator to copy one vector to other ; Vector after removing the maximum element ; Given array ; Function Call
def proc ( v ) : NEW_LINE INDENT n = len ( v ) ; NEW_LINE low = v [ n - 1 ] NEW_LINE high = v [ n - 1 ] NEW_LINE p1 = n NEW_LINE p2 = n ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT x = v [ i ] ; NEW_LINE if ( x < low ) : NEW_LINE low = x ; NEW_LINE ans = 0 ; NEW_LINE elif ( x > high ) : NEW_LINE high = x ; NEW_LINE ans = 0 ; NEW_LINE if ( x == low ) : p1 = i ; NEW_LINE if ( x == high ) : p2 = i ; NEW_LINE ans += n - max ( p1 , p2 ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def subarray ( v ) : NEW_LINE INDENT n = len ( v ) ; NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT ans = proc ( v ) ; NEW_LINE low = v [ 0 ] NEW_LINE pos_low = 0 NEW_LINE high = v [ 0 ] NEW_LINE pos_high = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT x = v [ i ] ; NEW_LINE if ( x < low ) : NEW_LINE low = x ; NEW_LINE pos_low = i ; NEW_LINE elif ( x > high ) : NEW_LINE high = x ; NEW_LINE pos_high = i ; NEW_LINE DEDENT u = v [ : ] ; NEW_LINE del u [ pos_low ] ; NEW_LINE ans = max ( ans , proc ( u ) ) ; NEW_LINE w = v [ : ] ; NEW_LINE del w [ pos_high ] ; NEW_LINE return max ( ans , proc ( w ) ) ; NEW_LINE DEDENT v = [ ] ; NEW_LINE v . append ( 7 ) ; NEW_LINE v . append ( 2 ) ; NEW_LINE v . append ( 5 ) ; NEW_LINE v . append ( 4 ) ; NEW_LINE v . append ( 3 ) ; NEW_LINE v . append ( 1 ) ; NEW_LINE print ( subarray ( v ) ) ; NEW_LINE
Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2 | Function to find the minimum number of increment and decrement of pairs required to make all array elements equal ; Stores the sum of the array ; If sum is not divisible by N ; Update sum ; Store the minimum number of operations ; Iterate while i is less than N ; Add absolute difference of current element with k to ans ; Increase i bye 1 ; Return the value in ans2 ; Driver Code ; Given Input ; Function Call
def find ( arr , N ) : NEW_LINE INDENT Sum = sum ( arr ) NEW_LINE if Sum % N : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT k = Sum // N NEW_LINE ans = 0 NEW_LINE i = 0 NEW_LINE while i < N : NEW_LINE INDENT ans = ans + abs ( k - arr [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT return ans // 2 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 1 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE print ( find ( arr , N ) ) NEW_LINE DEDENT
Convert A into B by incrementing or decrementing 1 , 2 , or 5 any number of times | Function to find minimum number of moves required to convert A into B ; Stores the minimum number of moves required ; Stores the absolute difference ; FInd the number of moves ; Return cnt ; Input ; Function call
def minimumSteps ( a , b ) : NEW_LINE INDENT cnt = 0 NEW_LINE a = abs ( a - b ) NEW_LINE cnt = ( a // 5 ) + ( a % 5 ) // 2 + ( a % 5 ) % 2 NEW_LINE return cnt NEW_LINE DEDENT A = 3 NEW_LINE B = 9 NEW_LINE print ( minimumSteps ( A , B ) ) NEW_LINE
Lexicographically smallest string which is not a subsequence of given string | Function to find lexicographically smallest string that is not a subsequence of S ; Variable to store frequency of a ; Calculate frequency of a ; Initialize string consisting of freq number of a ; Change the last digit to b ; Add another ' a ' to the string ; Return tha answer string ; Driver code ; Input ; Function call
def smallestNonSubsequence ( S , N ) : NEW_LINE INDENT freq = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] == ' a ' ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT DEDENT ans = " " NEW_LINE for i in range ( freq ) : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT if ( freq == N ) : NEW_LINE INDENT ans = ans . replace ( ans [ freq - 1 ] , ' b ' ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcdefghijklmnopqrstuvwxyz " NEW_LINE N = len ( S ) NEW_LINE print ( smallestNonSubsequence ( S , N ) ) NEW_LINE DEDENT
Print all numbers that can be obtained by adding A or B to N exactly M times | Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be obtained ; If A and B are equal , then only one number can be obtained , i . e . N + M * A ; For finding others numbers , subtract A from number once and add B to number once ; Driver Code ; Given Input ; Function Call
def possibleNumbers ( N , M , A , B ) : NEW_LINE INDENT if ( A > B ) : NEW_LINE INDENT temp = A NEW_LINE A = B NEW_LINE B = temp NEW_LINE DEDENT number = N + M * A NEW_LINE print ( number , end = " ▁ " ) NEW_LINE if ( A != B ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT number = number - A + B NEW_LINE print ( number , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 3 NEW_LINE A = 4 NEW_LINE B = 6 NEW_LINE possibleNumbers ( N , M , A , B ) NEW_LINE DEDENT
Maximize number of circular buildings that can be covered by L length wire | Python3 program for the above approach ; Function to find the maximum number of buildings covered ; Store the current sum ; Traverse the array ; Add the length of wire required for current building to cur_sum ; Add extra unit distance 1 ; If curr_sum <= length of wire increment count by 1 ; If curr_sum > length of wire increment start by 1 and decrement count by 1 and update the new curr_sum ; Update the max_count ; Return the max_count ; Driver Code ; Given Input ; Size of the array ; Function Call
Pi = 3.141592 NEW_LINE def MaxBuildingsCovered ( arr , N , L ) : NEW_LINE INDENT curr_sum = 0 NEW_LINE start = 0 NEW_LINE curr_count = 0 NEW_LINE max_count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT curr_sum = curr_sum + ( arr [ i ] * Pi ) NEW_LINE if ( i != 0 ) : NEW_LINE INDENT curr_sum += 1 NEW_LINE DEDENT if ( curr_sum <= L ) : NEW_LINE INDENT curr_count += 1 NEW_LINE DEDENT elif ( curr_sum > L ) : NEW_LINE INDENT curr_sum = curr_sum - ( arr [ start ] * Pi ) NEW_LINE curr_sum -= 1 NEW_LINE start += 1 NEW_LINE curr_count -= 1 NEW_LINE DEDENT max_count = max ( curr_count , max_count ) NEW_LINE DEDENT return max_count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 1 , 6 , 2 ] NEW_LINE L = 24 NEW_LINE N = len ( arr ) NEW_LINE print ( MaxBuildingsCovered ( arr , N , L ) ) NEW_LINE DEDENT
Count numbers less than N whose Bitwise AND with N is zero | Function to count number of unset bits in the integer N ; Stores the number of unset bits in N ; Check if N is even ; Increment the value of c ; Right shift N by 1 ; Return the value of count of unset bits ; Function to count numbers whose Bitwise AND with N equal to 0 ; Stores the number of unset bits in N ; Prthe value of 2 to the power of unsetBits ; Driver Code
def countUnsetBits ( N ) : NEW_LINE INDENT c = 0 NEW_LINE while ( N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT N = N >> 1 NEW_LINE DEDENT return c NEW_LINE DEDENT def countBitwiseZero ( N ) : NEW_LINE INDENT unsetBits = countUnsetBits ( N ) NEW_LINE print ( ( 1 << unsetBits ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9 NEW_LINE countBitwiseZero ( N ) NEW_LINE DEDENT
Check if a Binary String can be split into disjoint subsequences which are equal to "010" | Function to check if the given string can be partitioned into a number of subsequences all of which are equal to "010" ; Store the size of the string ; Store the count of 0 s and 1 s ; Traverse the given string in the forward direction ; If the character is '0' , increment count_0 by 1 ; If the character is '1' increment count_1 by 1 ; If at any point , count_1 > count_0 , return false ; If count_0 is not equal to twice count_1 , return false ; Reset the value of count_0 and count_1 ; Traverse the string in the reverse direction ; If the character is '0' increment count_0 ; If the character is '1' increment count_1 ; If count_1 > count_0 , return false ; Driver Code ; Given string ; Function Call
def isPossible ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE count_0 , count_1 = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT if ( count_1 > count_0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( count_0 != ( 2 * count_1 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT count_0 , count_1 = 0 , 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT if ( count_1 > count_0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "010100" NEW_LINE if ( isPossible ( s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimum number of bricks that can be intersected | Python 3 program for the above approach ; Function to find a line across a wall such that it intersects minimum number of bricks ; Declare a hashmap ; Store the maximum number of brick ending a point on x - axis ; Iterate over all the rows ; Initialize width as 0 ; Iterate over individual bricks ; Add the width of the current brick to the total width ; Increment number of bricks ending at this width position ; Update the variable , res ; Print the answer ; Driver Code ; Given Input ; Function Call
from collections import defaultdict NEW_LINE def leastBricks ( wall ) : NEW_LINE INDENT map = defaultdict ( int ) NEW_LINE res = 0 NEW_LINE for list in wall : NEW_LINE INDENT width = 0 NEW_LINE for i in range ( len ( list ) - 1 ) : NEW_LINE INDENT width += list [ i ] NEW_LINE map [ width ] += 1 NEW_LINE res = max ( res , map [ width ] ) NEW_LINE DEDENT DEDENT print ( len ( wall ) - res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 2 , 2 , 1 ] , [ 3 , 1 , 2 ] , [ 1 , 3 , 2 ] , [ 2 , 4 ] , [ 3 , 1 , 2 ] , [ 1 , 3 , 1 , 1 ] ] NEW_LINE leastBricks ( arr ) NEW_LINE DEDENT
Find an N | Function to print the required permutation ; Driver code
def findPermutation ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( " " , ▁ end ▁ = ▁ " " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE findPermutation ( N ) NEW_LINE DEDENT
Maximize count of distinct profits possible by N transactions | Function to count distinct profits possible ; Stores the minimum total profit ; Stores the maximum total profit ; Return count of distinct profits ; Driver code ; Input ; Function call
def numberOfWays ( N , X , Y ) : NEW_LINE INDENT S1 = ( N - 1 ) * X + Y NEW_LINE S2 = ( N - 1 ) * Y + X NEW_LINE return ( S2 - S1 + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE X = 13 NEW_LINE Y = 15 NEW_LINE print ( numberOfWays ( N , X , Y ) ) NEW_LINE DEDENT
Minimum number of operations required to make a permutation of first N natural numbers equal | Function to find the minimum number of operations required to make all array elements equal ; Store the count of operations required ; Increment by K - 1 , as the last element will be used again for the next K consecutive elements ; Increment count by 1 ; Return the result ; Driver Code ; Given Input
def MinimumOperations ( A , N , K ) : NEW_LINE INDENT Count = 0 NEW_LINE i = 0 NEW_LINE while ( i < N - 1 ) : NEW_LINE INDENT i = i + K - 1 NEW_LINE Count += 1 NEW_LINE DEDENT return Count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 5 , 4 , 3 , 1 , 2 ] NEW_LINE K = 3 NEW_LINE N = len ( A ) NEW_LINE print ( MinimumOperations ( A , N , K ) ) NEW_LINE DEDENT
Consecutive Prime numbers greater than equal to given number . | python 3 program for the above approach Function to check prime . ; Function to check prime . ; It means it is not a prime ; No factor other than 1 therefore prime number ; Function to find out the required consecutive primes . ; Finding first prime just less than sqrt ( n ) . ; Finding prime just greater than sqrt ( n ) . ; Product of both prime is greater than n then print it ; Finding prime greater than second ; Driver Program
from math import sqrt NEW_LINE def is_prime ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def consecutive_primes ( n ) : NEW_LINE INDENT first = - 1 NEW_LINE second = - 1 NEW_LINE i = int ( sqrt ( n ) ) NEW_LINE while ( i >= 2 ) : NEW_LINE INDENT if ( is_prime ( i ) ) : NEW_LINE INDENT first = i NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT for i in range ( int ( sqrt ( n ) ) + 1 , n // 2 + 1 , 1 ) : NEW_LINE INDENT if ( is_prime ( i ) ) : NEW_LINE INDENT second = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( first * second >= n ) : NEW_LINE INDENT print ( first , second ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( second + 1 , n + 1 , 1 ) : NEW_LINE INDENT if ( is_prime ( i ) ) : NEW_LINE INDENT print ( second , i ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 14 NEW_LINE consecutive_primes ( n ) NEW_LINE DEDENT
Reorder characters of a string to valid English representations of digits | Python program to implement the above approach ; Function to construct the original set of digits from the string in ascending order ; Store the unique characters corresponding to word and number ; Store the required result ; Store the frequency of each character of S ; Traverse the unique characters ; Store the count of k [ i ] in S ; Traverse the corresponding word ; Decrement the frequency of characters by x ; Append the digit x times to ans ; Sort the digits in ascending order ; Given string , s ; Function Call
from collections import Counter NEW_LINE def construct_digits ( s ) : NEW_LINE INDENT k = [ " z " , " w " , " u " , " x " , " g " , " h " , " o " , " f " , " v " , " i " ] NEW_LINE l = [ " zero " , " two " , " four " , " six " , " eight " , " three " , " one " , " five " , " seven " , " nine " ] NEW_LINE c = [ 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 ] NEW_LINE ans = [ ] NEW_LINE d = Counter ( s ) NEW_LINE for i in range ( len ( k ) ) : NEW_LINE INDENT x = d . get ( k [ i ] , 0 ) NEW_LINE for j in range ( len ( l [ i ] ) ) : NEW_LINE INDENT d [ l [ i ] [ j ] ] -= x NEW_LINE DEDENT ans . append ( str ( c [ i ] ) * x ) NEW_LINE DEDENT ans . sort ( ) NEW_LINE return " " . join ( ans ) NEW_LINE DEDENT s = " fviefuro " NEW_LINE print ( construct_digits ( s ) ) NEW_LINE