codeparrot/xlcost-text-to-code huggingface dataset:
Count substring of Binary string such that each character belongs to a palindrome of size greater than 1 | Function to find the substrings in binary string such that every character belongs to a palindrome ; Total substrings ; Loop to store the count of continuous characters in the given string ; Subtract non special strings from answer ; Driver Code ; Given string ; Function call
def countSubstrings ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE answer = ( n * ( n - 1 ) ) // 2 NEW_LINE cnt = 1 NEW_LINE v = [ ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == s [ i - 1 ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( cnt ) NEW_LINE cnt = 1 NEW_LINE DEDENT DEDENT if ( cnt > 0 ) : NEW_LINE INDENT v . append ( cnt ) NEW_LINE DEDENT for i in range ( len ( v ) - 1 ) : NEW_LINE INDENT answer -= ( v [ i ] + v [ i + 1 ] - 1 ) NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "00111" NEW_LINE print ( countSubstrings ( s ) ) NEW_LINE DEDENT
Minimum minutes needed to make the time palindromic | Function to get the required minutes ; Storing hour and minute value in integral form ; Keep iterating till first digit hour becomes equal to second digit of minute and second digit of hour becomes equal to first digit of minute ; If mins is 60 , increase hour , and reinitilialized to 0 ; If hours is 60 , reinitialized to 0 ; Return the required time ; Given Time as a string ; Function call
def get_palindrome_time ( str ) : NEW_LINE INDENT hh = ( ( ord ( str [ 0 ] ) - 48 ) * 10 + ( ord ( str [ 1 ] ) - 48 ) ) NEW_LINE mm = ( ( ord ( str [ 3 ] ) - 48 ) * 10 + ( ord ( str [ 4 ] ) - 48 ) ) NEW_LINE requiredTime = 0 NEW_LINE while ( hh % 10 != mm // 10 or hh // 10 != mm % 10 ) : NEW_LINE INDENT mm += 1 NEW_LINE if ( mm == 60 ) : NEW_LINE INDENT mm = 0 NEW_LINE hh += 1 NEW_LINE DEDENT if ( hh == 24 ) : NEW_LINE INDENT hh = 0 NEW_LINE DEDENT requiredTime += 1 ; NEW_LINE DEDENT return requiredTime NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "05:39" ; NEW_LINE print ( get_palindrome_time ( str ) ) ; NEW_LINE DEDENT
Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed | Function to find the maximum sum of all subarrays ; Initialize maxsum and prefixArray ; Find the frequency using prefix Array ; Perform prefix sum ; Sort both arrays to get a greedy result ; Finally multiply largest frequency with largest array element . ; Return the answer ; Driver Code ; Initial Array ; Subarrays ; Function Call
def maximumSubarraySum ( a , n , subarrays ) : NEW_LINE INDENT maxsum = 0 NEW_LINE prefixArray = [ 0 ] * n NEW_LINE for i in range ( len ( subarrays ) ) : NEW_LINE INDENT prefixArray [ subarrays [ i ] [ 0 ] - 1 ] += 1 NEW_LINE prefixArray [ subarrays [ i ] [ 1 ] ] -= 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT prefixArray [ i ] += prefixArray [ i - 1 ] NEW_LINE DEDENT prefixArray . sort ( ) NEW_LINE prefixArray . reverse ( ) NEW_LINE a . sort ( ) NEW_LINE a . reverse ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxsum += a [ i ] * prefixArray [ i ] NEW_LINE DEDENT return maxsum NEW_LINE DEDENT n = 6 NEW_LINE a = [ 4 , 1 , 2 , 1 , 9 , 2 ] NEW_LINE subarrays = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 3 , 4 ] ] NEW_LINE print ( maximumSubarraySum ( a , n , subarrays ) ) NEW_LINE
Maximum profit such that total stolen value is less than K to get bonus | Function to find the maximum profit from the given values ; Iterating over every possible permutation ; Driver Code ; Function Call
def maxProfit ( value , N , K ) : NEW_LINE INDENT value . sort ( ) NEW_LINE maxval = value [ N - 1 ] NEW_LINE maxProfit = 0 NEW_LINE while True : NEW_LINE INDENT curr_val = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT curr_val += value [ i ] NEW_LINE if ( curr_val <= K ) : NEW_LINE INDENT maxProfit = max ( curr_val + maxval * ( i + 1 ) , maxProfit ) NEW_LINE DEDENT DEDENT if not next_permutation ( value ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return maxProfit NEW_LINE DEDENT def next_permutation ( p ) : NEW_LINE INDENT for a in range ( len ( p ) - 2 , - 1 , - 1 ) : NEW_LINE INDENT if p [ a ] < p [ a + 1 ] : NEW_LINE INDENT b = len ( p ) - 1 NEW_LINE while True : NEW_LINE INDENT if p [ b ] > p [ a ] : NEW_LINE INDENT t = p [ a ] NEW_LINE p [ a ] = p [ b ] NEW_LINE p [ b ] = t NEW_LINE a += 1 NEW_LINE b = len ( p ) - 1 NEW_LINE while a < b : NEW_LINE INDENT t = p [ a ] NEW_LINE p [ a ] = p [ b ] NEW_LINE p [ b ] = t NEW_LINE a += 1 NEW_LINE b -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT b -= 1 NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT N , K = 4 , 6 NEW_LINE values = [ 5 , 2 , 7 , 3 ] NEW_LINE print ( maxProfit ( values , N , K ) ) NEW_LINE
Min operations to reduce N by multiplying by any number or taking square root | Python3 program for the above approach ; Function to reduce N to its minimum possible value by the given operations ; Keep replacing n until is an integer ; Keep replacing n until n is divisible by i * i ; Print the answer ; Given N ; Function call
import math NEW_LINE def MinValue ( n ) : NEW_LINE INDENT while ( int ( math . sqrt ( n ) ) == math . sqrt ( n ) and n > 1 ) : NEW_LINE INDENT n = math . sqrt ( n ) NEW_LINE DEDENT for i in range ( int ( math . sqrt ( n ) ) , 1 , - 1 ) : NEW_LINE INDENT while ( n % ( i * i ) == 0 ) : NEW_LINE INDENT n /= i NEW_LINE DEDENT DEDENT print ( n ) NEW_LINE DEDENT n = 20 NEW_LINE MinValue ( n ) NEW_LINE
Count of ways to split given string into two non | Function to check whether the substring from l to r is palindrome or not ; If characters at l and r differ ; Not a palindrome ; If the string is a palindrome ; Function to count and return the number of possible splits ; Stores the count of splits ; Check if the two substrings after the split are palindromic or not ; If both are palindromes ; Print the final count ; Driver Code
def isPalindrome ( l , r , s ) : NEW_LINE INDENT while ( l <= r ) : NEW_LINE INDENT if ( s [ l ] != s [ r ] ) : NEW_LINE INDENT return bool ( False ) NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT return bool ( True ) NEW_LINE DEDENT def numWays ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( isPalindrome ( 0 , i , s ) and isPalindrome ( i + 1 , n - 1 , s ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT S = " aaaaa " NEW_LINE print ( numWays ( S ) ) NEW_LINE
Final direction after visiting every cell of Matrix starting from ( 0 , 0 ) | Function to find the direction when stopped moving ; Given size of NxM grid ; Function Call
def findDirection ( n , m ) : NEW_LINE INDENT if ( n > m ) : NEW_LINE INDENT if ( m % 2 == 0 ) : NEW_LINE INDENT print ( " Up " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Down " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT print ( " Left " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Right " ) ; NEW_LINE DEDENT DEDENT DEDENT n = 3 ; m = 3 ; NEW_LINE findDirection ( n , m ) ; NEW_LINE
Maximize the sum of modulus with every Array element | Function to return the maximum sum of modulus with every array element ; Sum of array elements ; Return the answer ; Driver Code
def maxModulosum ( a , n ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum1 += a [ i ] ; NEW_LINE DEDENT return sum1 - n ; NEW_LINE DEDENT a = [ 3 , 4 , 6 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( maxModulosum ( a , n ) ) ; NEW_LINE
Minimum jumps required to group all 1 s together in a given Binary string | Function to get the minimum jump value ; Store all indices of ones ; Populating one 's indices ; Calculate median ; Jumps required for 1 's  to the left of median ; Jumps required for 1 's  to the right of median ; Return the final answer ; Driver Code
def getMinJumps ( s ) : NEW_LINE INDENT ones = [ ] NEW_LINE jumps , median , ind = 0 , 0 , 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT ones . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( ones ) == 0 ) : NEW_LINE INDENT return jumps NEW_LINE DEDENT median = ones [ len ( ones ) // 2 ] NEW_LINE ind = median NEW_LINE for i in range ( ind , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT jumps += ind - i NEW_LINE ind -= 1 NEW_LINE DEDENT DEDENT ind = median NEW_LINE for i in range ( ind , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT jumps += i - ind NEW_LINE ind += 1 NEW_LINE DEDENT DEDENT return jumps NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "00100000010011" NEW_LINE print ( getMinJumps ( s ) ) NEW_LINE DEDENT
Find GCD of each subtree of a given node in an N | Maximum number of nodes ; Tree represented as adjacency list ; For storing value associates with node ; For storing GCD of every subarray ; Number of nodes ; Function to find GCD of two numbers . Using Euclidean algo ; If b == 0 then simply return a ; DFS function to traverse the tree ; Initializing answer with GCD of this node . ; Iterate over each child of current node ; Skipping the parent ; Call DFS for each child ; Taking GCD of the answer of the child to find node 's GCD ; Calling DFS from the root ( 1 ) for precomputing answers ; Function to find and prGCD for Q queries ; Doing preprocessing ; Iterate over each given query ; Driver code ; Tree : 1 ( 2 ) / \ 2 ( 3 ) 3 ( 4 ) / \ 4 ( 8 ) 5 ( 16 ) ; Making a undirected tree ; Values associated with nodes ; Function call
N = 10 ** 5 + 5 NEW_LINE v = [ [ ] for i in range ( N ) ] NEW_LINE val = [ 0 ] * ( N ) NEW_LINE answer = [ 0 ] * ( N ) NEW_LINE n = 0 NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def DFS ( node , parent ) : NEW_LINE INDENT answer [ node ] = val [ node ] NEW_LINE for child in v [ node ] : NEW_LINE INDENT if ( child == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT DFS ( child , node ) NEW_LINE answer [ node ] = gcd ( answer [ node ] , answer [ child ] ) NEW_LINE DEDENT DEDENT def preprocess ( ) : NEW_LINE INDENT DFS ( 1 , - 1 ) NEW_LINE DEDENT def findGCD ( queries , q ) : NEW_LINE INDENT preprocess ( ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT GCD = answer [ queries [ i ] ] NEW_LINE print ( " For ▁ subtree ▁ of ▁ " , queries [ i ] , " , ▁ GCD ▁ = ▁ " , GCD ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE v [ 1 ] . append ( 2 ) NEW_LINE v [ 2 ] . append ( 1 ) NEW_LINE v [ 1 ] . append ( 3 ) NEW_LINE v [ 3 ] . append ( 1 ) NEW_LINE v [ 3 ] . append ( 4 ) NEW_LINE v [ 4 ] . append ( 3 ) NEW_LINE v [ 3 ] . append ( 5 ) NEW_LINE v [ 5 ] . append ( 3 ) NEW_LINE val [ 1 ] = 2 NEW_LINE val [ 2 ] = 3 NEW_LINE val [ 3 ] = 4 NEW_LINE val [ 4 ] = 8 NEW_LINE val [ 5 ] = 16 NEW_LINE queries = [ 2 , 3 , 1 ] NEW_LINE q = len ( queries ) NEW_LINE findGCD ( queries , q ) NEW_LINE DEDENT
Minimize cost to convert given two integers to zero using given operations | Function to find out the minimum cost to make two number X and Y equal to zero ; If x is greater than y then swap ; Cost of making y equal to x ; Cost if we choose 1 st operation ; Cost if we choose 2 nd operation ; Total cost ;
def makeZero ( x , y , a , b ) : NEW_LINE INDENT if ( x > y ) : NEW_LINE INDENT x , y = y , x NEW_LINE DEDENT tot_cost = ( y - x ) * a NEW_LINE cost1 = 2 * x * a NEW_LINE cost2 = x * b NEW_LINE tot_cost += min ( cost1 , cost2 ) NEW_LINE print ( tot_cost ) NEW_LINE DEDENT / * Driver code * / NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X , Y = 1 , 3 NEW_LINE cost1 , cost2 = 391 , 555 NEW_LINE makeZero ( X , Y , cost1 , cost2 ) NEW_LINE DEDENT
Find N fractions that sum upto a given fraction N / D | Function to split the fraction into the N parts ; Loop to find the N - 1 fraction ; Loop to print the Fractions ; Driver Code ; Function Call
def splitFraction ( n , d ) : NEW_LINE INDENT ar = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT ar . append ( 0 ) NEW_LINE DEDENT first = d + n - 1 NEW_LINE ar [ 0 ] = first NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT temp = first - 1 NEW_LINE ar [ i ] = first * temp NEW_LINE first -= 1 NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ar [ i ] % n == 0 : NEW_LINE INDENT print ( "1 / " , int ( ar [ i ] / n ) , " , " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n , " / " , ar [ i ] , " , " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT N = 4 NEW_LINE D = 2 NEW_LINE splitFraction ( N , D ) NEW_LINE
Count of pairs ( i , j ) in the array such that arr [ i ] is a factor of arr [ j ] | Function to return the count of Pairs ; Driver code
def numPairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if arr [ j ] % arr [ i ] == 0 : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 1 , 2 , 2 , 3 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( numPairs ( arr , n ) ) NEW_LINE
Check if array can be converted into strictly decreasing sequence | Function to check that array can be converted into a strictly decreasing sequence ; Loop to check that each element is greater than the ( N - index ) ; If element is less than ( N - index ) ; If array can be converted ; Driver Code ; Function calling
def check ( arr , n ) : NEW_LINE INDENT flag = True ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < n - i ) : NEW_LINE INDENT flag = False ; NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 11 , 11 , 11 , 11 ] ; NEW_LINE n1 = len ( arr1 ) ; NEW_LINE if ( check ( arr1 , n1 ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Minimize the non | Function to minimize the non - zero elements in the given array ; To store the min pos needed ; Loop to iterate over the elements of the given array ; If current position A [ i ] is occupied the we can place A [ i ] , A [ i + 1 ] and A [ i + 2 ] elements together at A [ i + 1 ] if exists . ; Driver Code ; Function Call
def minOccupiedPosition ( A , n ) : NEW_LINE INDENT minPos = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT if ( A [ i ] > 0 ) : NEW_LINE INDENT minPos += 1 NEW_LINE i += 2 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return minPos NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 8 , 0 , 7 , 0 , 0 , 6 ] NEW_LINE n = len ( A ) NEW_LINE print ( minOccupiedPosition ( A , n ) ) NEW_LINE DEDENT
Find minimum number K such that sum of array after multiplication by K exceed S | Python3 implementation of the approach ; Function to return the minimum value of k that satisfies the given condition ; store sum of array elements ; Calculate the sum after ; return minimum possible K ; Driver code
import math NEW_LINE def findMinimumK ( a , n , S ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT return math . ceil ( ( ( S + 1 ) * 1.0 ) / ( sum * 1.0 ) ) NEW_LINE DEDENT a = [ 10 , 7 , 8 , 10 , 12 , 19 ] NEW_LINE n = len ( a ) NEW_LINE s = 200 NEW_LINE print ( findMinimumK ( a , n , s ) ) NEW_LINE
Find the largest number smaller than integer N with maximum number of set bits | Function to return the largest number less than N ; Iterate through all possible values ; Multiply the number by 2 i times ; Return the final result ; Driver code
def largestNum ( n ) : NEW_LINE INDENT num = 0 ; NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT x = ( 1 << i ) ; NEW_LINE if ( ( x - 1 ) <= n ) : NEW_LINE INDENT num = ( 1 << i ) - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return num ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 345 ; NEW_LINE print ( largestNum ( N ) ) ; NEW_LINE DEDENT
Find the String having each substring with exactly K distinct characters | Function to find the required output string ; Each element at index i is modulus of K ; Driver code ; initialise integers N and K
def findString ( N , K ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( chr ( ord ( ' A ' ) + i % K ) , end = " " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 ; NEW_LINE K = 3 ; NEW_LINE findString ( N , K ) ; NEW_LINE DEDENT
Find total no of collisions taking place between the balls in which initial direction of each ball is given | Function to count no of collision ; Length of the string ; Driver code
def count ( s ) : NEW_LINE INDENT cnt , ans = 0 , 0 NEW_LINE N = len ( s ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( s [ i ] == ' R ' ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( s [ i ] == ' L ' ) : NEW_LINE INDENT ans += cnt NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT s = " RRLL " NEW_LINE print ( count ( s ) ) NEW_LINE
Maximum number on 7 | Function to find the maximum number that can be displayed using the N segments ; Condition to check base case ; Condition to check if the number is even ; Condition to check if the number is odd ; Driver Code
def segments ( n ) : NEW_LINE INDENT if ( n == 1 or n == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT print ( "1" , end = " " ) ; NEW_LINE segments ( n - 2 ) ; NEW_LINE DEDENT elif ( n % 2 == 1 ) : NEW_LINE INDENT print ( "7" , end = " " ) ; NEW_LINE segments ( n - 3 ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 11 ; NEW_LINE segments ( n ) ; NEW_LINE DEDENT
Minimum number of subsequences required to convert one string to another using Greedy Algorithm | Function to find the minimum number of subsequences required to convert one to another S2 == A and S1 == B ; At least 1 subsequence is required Even in best case , when A is same as B ; size of B ; size of A ; Create an 2D array next [ ] [ ] of size 26 * sizeOfB to store the next occurrence of a character ( ' a ' to ' z ' ) as an index [ 0 , sizeOfA - 1 ] ; Loop to Store the values of index ; If the value of next [ i ] [ j ] is infinite then update it with next [ i ] [ j + 1 ] ; Greedy algorithm to obtain the maximum possible subsequence of B to cover the remaining of A using next subsequence ; Loop to iterate over the A ; Condition to check if the character is not present in the B ; Condition to check if there is an element in B matching with character A [ i ] on or next to B [ pos ] given by next [ A [ i ] - ' a ' ] [ pos ] ; Condition to check if reached at the end of B or no such element exists on or next to A [ pos ] , thus increment number by one and reinitialise pos to zero ; Driver Code
def findMinimumSubsequences ( A , B ) : NEW_LINE INDENT numberOfSubsequences = 1 NEW_LINE sizeOfB = len ( B ) NEW_LINE sizeOfA = len ( A ) NEW_LINE inf = 1000000 NEW_LINE next = [ [ inf for i in range ( sizeOfB ) ] for i in range ( 26 ) ] NEW_LINE for i in range ( sizeOfB ) : NEW_LINE INDENT next [ ord ( B [ i ] ) - ord ( ' a ' ) ] [ i ] = i NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT for j in range ( sizeOfB - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( next [ i ] [ j ] == inf ) : NEW_LINE INDENT next [ i ] [ j ] = next [ i ] [ j + 1 ] NEW_LINE DEDENT DEDENT DEDENT pos = 0 NEW_LINE i = 0 NEW_LINE while ( i < sizeOfA ) : NEW_LINE INDENT if ( pos == 0 and next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] == inf ) : NEW_LINE INDENT numberOfSubsequences = - 1 NEW_LINE break NEW_LINE DEDENT elif ( pos < sizeOfB and next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] < inf ) : NEW_LINE INDENT nextIndex = next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] + 1 NEW_LINE pos = nextIndex NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT numberOfSubsequences += 1 NEW_LINE pos = 0 NEW_LINE DEDENT DEDENT return numberOfSubsequences NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " aacbe " NEW_LINE B = " aceab " NEW_LINE print ( findMinimumSubsequences ( A , B ) ) NEW_LINE DEDENT
Vertical and Horizontal retrieval ( MRT ) on Tapes | Python3 program to print Horizontal filling ; It is used for checking whether tape is full or not ; It is used for calculating total retrieval time ; It is used for calculating mean retrieval time ; vector is used because n number of records can insert in one tape with size constraint ; Null vector obtained to use fresh vector 'v ; initialize variables to 0 for each iteration ; sum is used for checking whether i 'th tape is full or not ; check sum less than size of tape ; increment in j for next record ; calculating total retrieval time ; MRT formula ; calculating mean retrieval time using formula ; v . size ( ) is function of vector is used to get size of vector ; Driver Code ; store the size of records [ ] ; store the size of tapes [ ] ; sorting of an array is required to attain greedy approach of algorithm
def horizontalFill ( records , tape , nt ) : NEW_LINE INDENT sum = 0 ; NEW_LINE Retrieval_Time = 0 ; NEW_LINE current = 0 ; NEW_LINE v = [ ] ; NEW_LINE for i in range ( nt ) : NEW_LINE DEDENT ' NEW_LINE INDENT v . clear ( ) ; NEW_LINE Retrieval_Time = 0 ; NEW_LINE sum = 0 ; NEW_LINE print ( " tape " , i + 1 , " : ▁ [ ▁ " , end = " " ) ; NEW_LINE sum += records [ current ] ; NEW_LINE while ( sum <= tape [ i ] ) : NEW_LINE INDENT print ( records [ current ] , end = " ▁ " ) ; NEW_LINE v . append ( records [ current ] ) ; NEW_LINE current += 1 ; NEW_LINE sum += records [ current ] ; print ( " ] " , end = " " ) ; NEW_LINE DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT Retrieval_Time += v [ i ] * ( len ( v ) - i ) ; NEW_LINE DEDENT Mrt = Retrieval_Time / len ( v ) ; NEW_LINE print ( " tMRT ▁ : " , Mrt ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT records = [ 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 ] ; NEW_LINE tape = [ 25 , 80 , 160 ] ; NEW_LINE n = len ( records ) ; NEW_LINE m = len ( tape ) ; NEW_LINE records . sort ( ) ; NEW_LINE horizontalFill ( records , tape , m ) ; NEW_LINE DEDENT
Circular Convolution using Matrix Method | Python program to compute circular convolution of two arrays ; Function to find circular convolution ; Finding the maximum size between the two input sequence sizes ; Copying elements of x to row_vec and padding zeros if size of x < maxSize ; Copying elements of h to col_vec and padding zeros if size of h is less than maxSize ; Generating 2D matrix of circularly shifted elements ; Computing result by matrix multiplication and printing results ; Driver program
MAX_SIZE = 10 ; NEW_LINE def convolution ( x , h , n , m ) : NEW_LINE INDENT row_vec = [ 0 ] * MAX_SIZE ; NEW_LINE col_vec = [ 0 ] * MAX_SIZE ; NEW_LINE out = [ 0 ] * MAX_SIZE ; NEW_LINE circular_shift_mat = [ [ 0 for i in range ( MAX_SIZE ) ] for j in range ( MAX_SIZE ) ] ; NEW_LINE if ( n > m ) : NEW_LINE INDENT maxSize = n ; NEW_LINE DEDENT else : NEW_LINE INDENT maxSize = m ; NEW_LINE DEDENT for i in range ( maxSize ) : NEW_LINE INDENT if ( i >= n ) : NEW_LINE INDENT row_vec [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT row_vec [ i ] = x [ i ] ; NEW_LINE DEDENT DEDENT for i in range ( maxSize ) : NEW_LINE INDENT if ( i >= m ) : NEW_LINE INDENT col_vec [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT col_vec [ i ] = h [ i ] ; NEW_LINE DEDENT DEDENT k = 0 ; NEW_LINE d = 0 ; NEW_LINE for i in range ( maxSize ) : NEW_LINE INDENT curIndex = k - d ; NEW_LINE for j in range ( maxSize ) : NEW_LINE INDENT circular_shift_mat [ j ] [ i ] = row_vec [ curIndex % maxSize ] ; NEW_LINE curIndex += 1 ; NEW_LINE DEDENT k = maxSize ; NEW_LINE d += 1 ; NEW_LINE DEDENT for i in range ( maxSize ) : NEW_LINE INDENT for j in range ( maxSize ) : NEW_LINE INDENT out [ i ] += circular_shift_mat [ i ] [ j ] * col_vec [ j ] ; NEW_LINE DEDENT print ( out [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = [ 5 , 7 , 3 , 2 ] ; NEW_LINE n = len ( x ) ; NEW_LINE h = [ 1 , 5 ] ; NEW_LINE m = len ( h ) ; NEW_LINE convolution ( x , h , n , m ) ; NEW_LINE DEDENT