codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number has bits in alternate pattern | Set 1 | Returns true if n has alternate bit pattern else false ; Store last bit ; Traverse through remaining bits ; If current bit is same as previous ; Driver code
def findPattern ( n ) : NEW_LINE INDENT prev = n % 2 NEW_LINE n = n // 2 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT curr = n % 2 NEW_LINE if ( curr == prev ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev = curr NEW_LINE n = n // 2 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 10 NEW_LINE print ( " Yes " ) if ( findPattern ( n ) ) else print ( " No " ) NEW_LINE
XOR counts of 0 s and 1 s in binary representation | Returns XOR of counts 0 s and 1 s in binary representation of n . ; calculating count of zeros and ones ; Driver Code
def countXOR ( n ) : NEW_LINE INDENT count0 , count1 = 0 , 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT n //= 2 NEW_LINE DEDENT return ( count0 ^ count1 ) NEW_LINE DEDENT n = 31 NEW_LINE print ( countXOR ( n ) ) NEW_LINE
Count all pairs with given XOR | Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty map that stores counts of individual elements of array . ; If there exist an element in map m with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Increment count of current element ; return total count of pairs with XOR equal to x ; Driver Code
def xorPairCount ( arr , n , x ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr_xor = x ^ arr [ i ] NEW_LINE if ( curr_xor in m . keys ( ) ) : NEW_LINE INDENT result += m [ curr_xor ] NEW_LINE DEDENT if arr [ i ] in m . keys ( ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 2 , 5 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE x = 0 NEW_LINE print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " , xorPairCount ( arr , n , x ) ) NEW_LINE
Bitwise and ( or & ) of a range | Find position of MSB in n . For example if n = 17 , then position of MSB is 4. If n = 7 , value of MSB is 3 ; Function to find Bit - wise & of all numbers from x to y . ; res = 0 Initialize result ; Find positions of MSB in x and y ; If positions are not same , return ; Add 2 ^ msb_p1 to result ; subtract 2 ^ msb_p1 from x and y . ; Driver code
def msbPos ( n ) : NEW_LINE INDENT msb_p = - 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT n = n >> 1 NEW_LINE msb_p += 1 NEW_LINE DEDENT return msb_p NEW_LINE DEDENT def andOperator ( x , y ) : NEW_LINE INDENT while ( x > 0 and y > 0 ) : NEW_LINE INDENT msb_p1 = msbPos ( x ) NEW_LINE msb_p2 = msbPos ( y ) NEW_LINE if ( msb_p1 != msb_p2 ) : NEW_LINE INDENT break NEW_LINE DEDENT msb_val = ( 1 << msb_p1 ) NEW_LINE res = res + msb_val NEW_LINE x = x - msb_val NEW_LINE y = y - msb_val NEW_LINE DEDENT return res NEW_LINE DEDENT x , y = 10 , 15 NEW_LINE print ( andOperator ( x , y ) ) NEW_LINE
Multiply a number with 10 without using multiplication operator | Function to find multiplication of n with 10 without using multiplication operator ; Driver program to run the case
def multiplyTen ( n ) : NEW_LINE INDENT return ( n << 1 ) + ( n << 3 ) NEW_LINE DEDENT n = 50 NEW_LINE print ( multiplyTen ( n ) ) NEW_LINE
Equal Sum and XOR | function to count number of values less than equal to n that satisfy the given condition ; Traverse all numbers from 0 to n and increment result only when given condition is satisfied . ; Driver Code
def countValues ( n ) : NEW_LINE INDENT countV = 0 ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( ( n + i ) == ( n ^ i ) ) : NEW_LINE INDENT countV += 1 ; NEW_LINE DEDENT DEDENT return countV ; NEW_LINE DEDENT n = 12 ; NEW_LINE print ( countValues ( n ) ) ; NEW_LINE
Equal Sum and XOR | function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; Return 2 ^ unset_bits ; Driver code
def countValues ( n ) : NEW_LINE INDENT unset_bits = 0 NEW_LINE while ( n ) : NEW_LINE INDENT if n & 1 == 0 : NEW_LINE INDENT unset_bits += 1 NEW_LINE DEDENT n = n >> 1 NEW_LINE DEDENT return 1 << unset_bits NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE print ( countValues ( n ) ) NEW_LINE DEDENT
Find profession in a special family | Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Base case ; Recursively find parent 's profession. If parent  is a Doctor, this node will be a Doctor if it is  at odd position and an engineer if at even position ; If parent is an engineer , then current node will be an engineer if at add position and doctor if even position . ; Driver code
def findProffesion ( level , pos ) : NEW_LINE INDENT if ( level == 1 ) : NEW_LINE INDENT return ' e ' NEW_LINE DEDENT if ( findProffesion ( level - 1 , ( pos + 1 ) // 2 ) == ' d ' ) : NEW_LINE INDENT if ( pos % 2 ) : NEW_LINE INDENT return ' d ' NEW_LINE DEDENT else : NEW_LINE INDENT return ' e ' NEW_LINE DEDENT DEDENT if ( pos % 2 ) : NEW_LINE INDENT return ' e ' NEW_LINE DEDENT else : NEW_LINE INDENT return ' d ' NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT level = 3 NEW_LINE pos = 4 NEW_LINE if ( findProffesion ( level , pos ) == ' e ' ) : NEW_LINE INDENT print ( " Engineer " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Doctor " ) NEW_LINE DEDENT DEDENT
Print first n numbers with exactly two set bits | Prints first n numbers with two set bits ; Initialize higher of two sets bits ; Keep reducing n for every number with two set bits . ; Consider all lower set bits for current higher set bit ; Print current number ; If we have found n numbers ; Consider next lower bit for current higher bit . ; Increment higher set bit ; Driver code
def printTwoSetBitNums ( n ) : NEW_LINE INDENT x = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT y = 0 NEW_LINE while ( y < x ) : NEW_LINE INDENT print ( ( 1 << x ) + ( 1 << y ) , end = " ▁ " ) NEW_LINE n -= 1 NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT y += 1 NEW_LINE DEDENT x += 1 NEW_LINE DEDENT DEDENT printTwoSetBitNums ( 4 ) NEW_LINE
Find even occurring elements in an array of limited range | Function to find the even occurring elements in given array ; do for each element of array ; left - shift 1 by value of current element ; Toggle the bit every time element gets repeated ; Traverse array again and use _xor to find even occurring elements ; left - shift 1 by value of current element ; Each 0 in _xor represents an even occurrence ; print the even occurring numbers ; set bit as 1 to avoid printing duplicates ; Driver code
def printRepeatingEven ( arr , n ) : NEW_LINE INDENT axor = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT pos = 1 << arr [ i ] ; NEW_LINE axor ^= pos ; NEW_LINE DEDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT pos = 1 << arr [ i ] ; NEW_LINE if ( not ( pos & axor ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE axor ^= pos ; NEW_LINE DEDENT DEDENT DEDENT arr = [ 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE printRepeatingEven ( arr , n ) ; NEW_LINE
Cyclic Redundancy Check and Modulo | Returns XOR of ' a ' and ' b ' ( both of same length ) ; initialize result ; Traverse all bits , if bits are same , then XOR is 0 , else 1 ; Performs Modulo - 2 division ; Number of bits to be XORed at a time . ; Slicing the divident to appropriate length for particular step ; replace the divident by the result of XOR and pull 1 bit down ; else : If leftmost bit is '0' If the leftmost bit of the dividend ( or the part used in each step ) is 0 , the step cannot use the regular divisor ; we need to use an all - 0 s divisor . ; increment pick to move further ; For the last n bits , we have to carry it out normally as increased value of pick will cause Index Out of Bounds . ; Function used at the sender side to encode data by appending remainder of modular division at the end of data . ; Appends n - 1 zeroes at end of data ; Append remainder in the original data ; Driver code
def xor ( a , b ) : NEW_LINE INDENT result = [ ] NEW_LINE for i in range ( 1 , len ( b ) ) : NEW_LINE INDENT if a [ i ] == b [ i ] : NEW_LINE INDENT result . append ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT result . append ( '1' ) NEW_LINE DEDENT DEDENT return ' ' . join ( result ) NEW_LINE DEDENT def mod2div ( divident , divisor ) : NEW_LINE INDENT pick = len ( divisor ) NEW_LINE tmp = divident [ 0 : pick ] NEW_LINE while pick < len ( divident ) : NEW_LINE INDENT if tmp [ 0 ] == '1' : NEW_LINE INDENT tmp = xor ( divisor , tmp ) + divident [ pick ] NEW_LINE tmp = xor ( '0' * pick , tmp ) + divident [ pick ] NEW_LINE DEDENT pick += 1 NEW_LINE DEDENT if tmp [ 0 ] == '1' : NEW_LINE INDENT tmp = xor ( divisor , tmp ) NEW_LINE DEDENT else : NEW_LINE INDENT tmp = xor ( '0' * pick , tmp ) NEW_LINE DEDENT checkword = tmp NEW_LINE return checkword NEW_LINE DEDENT def encodeData ( data , key ) : NEW_LINE INDENT l_key = len ( key ) NEW_LINE appended_data = data + '0' * ( l_key - 1 ) NEW_LINE remainder = mod2div ( appended_data , key ) NEW_LINE codeword = data + remainder NEW_LINE print ( " Remainder ▁ : ▁ " , remainder ) NEW_LINE print ( " Encoded ▁ Data ▁ ( Data ▁ + ▁ Remainder ) ▁ : ▁ " , codeword ) NEW_LINE DEDENT data = "100100" NEW_LINE key = "1101" NEW_LINE encodeData ( data , key ) NEW_LINE
Check if a number is Bleak | Function to get no of set bits in binary representation of passed binary no . ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak. ; Driver code
def countSetBits ( x ) : NEW_LINE INDENT count = 0 NEW_LINE while ( x ) : NEW_LINE INDENT x = x & ( x - 1 ) NEW_LINE count = count + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def isBleak ( n ) : NEW_LINE INDENT for x in range ( 1 , n ) : NEW_LINE INDENT if ( x + countSetBits ( x ) == n ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if ( isBleak ( 3 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT if ( isBleak ( 4 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Given a set , find XOR of the XOR 's of all subsets. | Returns XOR of all XOR 's of given subset ; XOR is 1 only when n is 1 , else 0 ; Driver code
def findXOR ( Set , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return Set [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT Set = [ 1 , 2 , 3 ] NEW_LINE n = len ( Set ) NEW_LINE print ( " XOR ▁ of ▁ XOR ' s ▁ of ▁ all ▁ subsets ▁ is ▁ " , findXOR ( Set , n ) ) ; NEW_LINE
Find XOR of two number without using XOR operator | Returns XOR of x and y ; Assuming 32 - bit Integer ; Find current bits in x and y ; If both are 1 then 0 else xor is same as OR ; Update result ; Driver Code
def myXOR ( x , y ) : NEW_LINE INDENT for i in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT b1 = x & ( 1 << i ) NEW_LINE b2 = y & ( 1 << i ) NEW_LINE b1 = min ( b1 , 1 ) NEW_LINE b2 = min ( b2 , 1 ) NEW_LINE xoredBit = 0 NEW_LINE if ( b1 & b2 ) : NEW_LINE INDENT xoredBit = 0 NEW_LINE DEDENT else : NEW_LINE INDENT xoredBit = ( b1 b2 ) NEW_LINE DEDENT res <<= 1 ; NEW_LINE res |= xoredBit NEW_LINE DEDENT return res NEW_LINE DEDENT x = 3 NEW_LINE y = 5 NEW_LINE print ( " XOR ▁ is " , myXOR ( x , y ) ) NEW_LINE
Find XOR of two number without using XOR operator | Returns XOR of x and y ; Driver Code
def myXOR ( x , y ) : NEW_LINE INDENT return ( ( x y ) & ( ~ x ~ y ) ) NEW_LINE DEDENT x = 3 NEW_LINE y = 5 NEW_LINE print ( " XOR ▁ is " , myXOR ( x , y ) ) NEW_LINE
How to swap two bits in a given integer ? | This function swaps bit at positions p1 and p2 in an integer n ; Move p1 'th to rightmost side ; Move p2 'th to rightmost side ; XOR the two bits ; Put the xor bit back to their original positions ; XOR ' x ' with the original number so that the two sets are swapped ; Driver program to test above function
def swapBits ( n , p1 , p2 ) : NEW_LINE INDENT bit1 = ( n >> p1 ) & 1 NEW_LINE bit2 = ( n >> p2 ) & 1 NEW_LINE x = ( bit1 ^ bit2 ) NEW_LINE x = ( x << p1 ) | ( x << p2 ) NEW_LINE result = n ^ x NEW_LINE return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT res = swapBits ( 28 , 0 , 3 ) NEW_LINE print ( " Result ▁ = ▁ " , res ) NEW_LINE DEDENT
Write a function that returns 2 for input 1 and returns 1 for 2 |
def invert ( x ) : NEW_LINE INDENT if ( x == 1 ) : NEW_LINE INDENT return 2 NEW_LINE else : NEW_LINE return 1 NEW_LINE DEDENT DEDENT
Write a function that returns 2 for input 1 and returns 1 for 2 |
def invertSub ( x ) : NEW_LINE INDENT return ( 3 - x ) NEW_LINE DEDENT
Maximum length sub | Function to return the maximum length of the required sub - array ; For the first consecutive pair of elements ; While a consecutive pair can be selected ; If current pair forms a valid sub - array ; 2 is the length of the current sub - array ; To extend the sub - array both ways ; While elements at indices l and r are part of a valid sub - array ; Update the maximum length so far ; Select the next consecutive pair ; Return the maximum length ; Driver code
def maxLength ( arr , n ) : NEW_LINE INDENT maxLen = 0 NEW_LINE i = 0 NEW_LINE j = i + 1 NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( arr [ i ] != arr [ j ] ) : NEW_LINE INDENT maxLen = max ( maxLen , 2 ) NEW_LINE l = i - 1 NEW_LINE r = j + 1 NEW_LINE while ( l >= 0 and r < n and arr [ l ] == arr [ i ] and arr [ r ] == arr [ j ] ) : NEW_LINE INDENT l -= 1 NEW_LINE r += 1 NEW_LINE DEDENT maxLen = max ( maxLen , 2 * ( r - j ) ) NEW_LINE DEDENT i += 1 NEW_LINE j = i + 1 NEW_LINE DEDENT return maxLen NEW_LINE DEDENT arr = [ 1 , 1 , 1 , 0 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxLength ( arr , n ) ) NEW_LINE
Freivaldâ €™ s Algorithm to check if a matrix is product of two | Python3 code to implement FreivaldaTMs Algorithm ; Function to check if ABx = Cx ; Generate a random vector ; Now comput B * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput C * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput A * ( B * r ) for evaluating expression A * ( B * r ) - ( C * r ) ; Finally check if value of expression A * ( B * r ) - ( C * r ) is 0 or not ; Runs k iterations Freivald . The value of k determines accuracy . Higher value means higher accuracy . ; Driver code
import random NEW_LINE N = 2 NEW_LINE def freivald ( a , b , c ) : NEW_LINE INDENT r = [ 0 ] * N NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT r [ i ] = ( int ) ( random . randrange ( 509090009 ) % 2 ) NEW_LINE DEDENT br = [ 0 ] * N NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT br [ i ] = br [ i ] + b [ i ] [ j ] * r [ j ] NEW_LINE DEDENT DEDENT cr = [ 0 ] * N NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT cr [ i ] = cr [ i ] + c [ i ] [ j ] * r [ j ] NEW_LINE DEDENT DEDENT axbr = [ 0 ] * N NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT axbr [ i ] = axbr [ i ] + a [ i ] [ j ] * br [ j ] NEW_LINE DEDENT DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT if ( axbr [ i ] - cr [ i ] != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isProduct ( a , b , c , k ) : NEW_LINE INDENT for i in range ( 0 , k ) : NEW_LINE INDENT if ( freivald ( a , b , c ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT a = [ [ 1 , 1 ] , [ 1 , 1 ] ] NEW_LINE b = [ [ 1 , 1 ] , [ 1 , 1 ] ] NEW_LINE c = [ [ 2 , 2 ] , [ 2 , 2 ] ] NEW_LINE k = 2 NEW_LINE if ( isProduct ( a , b , c , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Expectation or expected value of an array | Function to calculate expectation ; variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; Driver program ; Function for calculating expectation ; Display expectation of given array
def calc_Expectation ( a , n ) : NEW_LINE INDENT prb = 1 / n NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += ( a [ i ] * prb ) NEW_LINE DEDENT return float ( sum ) NEW_LINE DEDENT n = 6 ; NEW_LINE a = [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ] NEW_LINE expect = calc_Expectation ( a , n ) NEW_LINE print ( " Expectation ▁ of ▁ array ▁ E ( X ) ▁ is ▁ : ▁ " , expect ) NEW_LINE
Program to generate CAPTCHA and verify user | Python program to automatically generate CAPTCHA and verify user ; Returns true if given two strings are same ; Generates a CAPTCHA of given length ; Characters to be included ; Generate n characters from above set and add these characters to captcha . ; Generate a random CAPTCHA ; Ask user to enter a CAPTCHA ; Notify user about matching status
import random NEW_LINE def checkCaptcha ( captcha , user_captcha ) : NEW_LINE INDENT if captcha == user_captcha : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def generateCaptcha ( n ) : NEW_LINE INDENT chrs = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" NEW_LINE captcha = " " NEW_LINE while ( n ) : NEW_LINE INDENT captcha += chrs [ random . randint ( 1 , 1000 ) % 62 ] NEW_LINE n -= 1 NEW_LINE DEDENT return captcha NEW_LINE DEDENT captcha = generateCaptcha ( 9 ) NEW_LINE print ( captcha ) NEW_LINE print ( " Enter ▁ above ▁ CAPTCHA : " ) NEW_LINE usr_captcha = input ( ) NEW_LINE if ( checkCaptcha ( captcha , usr_captcha ) ) : NEW_LINE INDENT print ( " CAPTCHA ▁ Matched " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " CAPTCHA ▁ Not ▁ Matched " ) NEW_LINE DEDENT
Sum of N | Function to calculate the sum recursively ; Base cases ; If n is odd ; If n is even ; Function to print the value of Sum ; Driver Program ; first element ; common difference ; Number of elements ; Mod value
def SumGPUtil ( r , n , m ) : NEW_LINE INDENT if n == 0 : return 1 NEW_LINE if n == 1 : return ( 1 + r ) % m NEW_LINE if n % 2 == 1 : NEW_LINE INDENT ans = ( 1 + r ) * SumGPUtil ( r * r % m , ( n - 1 ) // 2 , m ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = 1 + r * ( 1 + r ) * SumGPUtil ( r * r % m , n // 2 - 1 , m ) NEW_LINE DEDENT return ans % m NEW_LINE DEDENT def SumGP ( a , r , N , M ) : NEW_LINE INDENT answer = a * SumGPUtil ( r , N , M ) NEW_LINE answer = answer % M NEW_LINE print ( answer ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE a = 1 NEW_LINE r = 4 NEW_LINE N = 10000 NEW_LINE M = 100000 NEW_LINE INDENT SumGP ( a , r , N , M ) NEW_LINE DEDENT
Length of longest subarray in which elements greater than K are more than elements not greater than K | Function to find the Length of a longest subarray in which elements greater than K are more than elements not greater than K ; Create a new array in which we store 1 if a [ i ] > k otherwise we store - 1. ; Taking prefix sum over it ; Len will store maximum Length of subarray ; This indicate there is at least one subarray of Length mid that has sum > 0 ; Check every subarray of Length mid if it has sum > 0 or not if sum > 0 then it will satisfy our required condition ; x will store the sum of subarray of Length mid ; Satisfy our given condition ; Check for higher Length as we get Length mid ; Check for lower Length as we did not get Length mid ; Driver code
def LongestSubarray ( a , n , k ) : NEW_LINE INDENT pre = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] > k ) : NEW_LINE INDENT pre [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT pre [ i ] = - 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + pre [ i ] NEW_LINE DEDENT Len = 0 NEW_LINE lo = 1 NEW_LINE hi = n NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT mid = ( lo + hi ) // 2 NEW_LINE ok = False NEW_LINE for i in range ( mid - 1 , n ) : NEW_LINE INDENT x = pre [ i ] NEW_LINE if ( i - mid >= 0 ) : NEW_LINE INDENT x -= pre [ i - mid ] NEW_LINE DEDENT if ( x > 0 ) : NEW_LINE INDENT ok = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( ok == True ) : NEW_LINE INDENT Len = mid NEW_LINE lo = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hi = mid - 1 NEW_LINE DEDENT DEDENT return Len NEW_LINE DEDENT a = [ 2 , 3 , 4 , 5 , 3 , 7 ] NEW_LINE k = 3 NEW_LINE n = len ( a ) NEW_LINE print ( LongestSubarray ( a , n , k ) ) NEW_LINE
Choose points from two ranges such that no point lies in both the ranges | Function to find the required points ; Driver code
def findPoints ( l1 , r1 , l2 , r2 ) : NEW_LINE INDENT x = min ( l1 , l2 ) if ( l1 != l2 ) else - 1 NEW_LINE y = max ( r1 , r2 ) if ( r1 != r2 ) else - 1 NEW_LINE print ( x , y ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l1 = 5 NEW_LINE r1 = 10 NEW_LINE l2 = 1 NEW_LINE r2 = 7 NEW_LINE findPoints ( l1 , r1 , l2 , r2 ) NEW_LINE DEDENT
Tail Recursion | A NON - tail - recursive function . The function is not tail recursive because the value returned by fact ( n - 1 ) is used in fact ( n ) and call to fact ( n - 1 ) is not the last thing done by fact ( n ) ; Driver program to test above function
def fact ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return n * fact ( n - 1 ) NEW_LINE DEDENT print ( fact ( 5 ) ) NEW_LINE
Minimum number of working days required to achieve each of the given scores | Function to find the minimum number of days required to work to at least arr [ i ] points for every array element ; Traverse the array P [ ] ; Find the prefix sum ; Traverse the array arr [ ] ; Find the minimum index of the array having value at least arr [ i ] ; If the index is not - 1 ; Otherwise ; Function to find the lower bound of N using binary search ; Stores the lower bound ; Stores the upper bound ; Stores the minimum index having value is at least N ; Iterater while i <= j ; Stores the mid index of the range [ i , j ] ; If P [ mid ] is at least N ; Update the value of mid to index ; Update the value of j ; Update the value of i ; Return the resultant index ; Driver Code
def minDays ( P , arr ) : NEW_LINE INDENT for i in range ( 1 , len ( P ) ) : NEW_LINE INDENT P [ i ] += P [ i ] + P [ i - 1 ] NEW_LINE DEDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT index = binarySeach ( P , arr [ i ] ) NEW_LINE if ( index != - 1 ) : NEW_LINE INDENT print ( index + 1 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT def binarySeach ( P , N ) : NEW_LINE INDENT i = 0 NEW_LINE j = len ( P ) - 1 NEW_LINE index = - 1 NEW_LINE while ( i <= j ) : NEW_LINE INDENT mid = i + ( j - i ) // 2 NEW_LINE if ( P [ mid ] >= N ) : NEW_LINE INDENT index = mid NEW_LINE j = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT i = mid + 1 NEW_LINE DEDENT DEDENT return index NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 400 , 200 , 700 , 900 , 1400 ] NEW_LINE P = [ 100 , 300 , 400 , 500 , 600 ] NEW_LINE minDays ( P , arr ) NEW_LINE DEDENT
Maximize profit that can be earned by selling an item among N buyers | Python3 program for the above approach ; Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Sort the array ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Update the maximum profit ; Return the maximum possible price ; Driver code
import sys NEW_LINE def maximumProfit ( arr , N ) : NEW_LINE INDENT ans = - sys . maxsize - 1 NEW_LINE price = 0 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT count = ( N - i ) NEW_LINE if ( ans < count * arr [ i ] ) : NEW_LINE INDENT price = arr [ i ] NEW_LINE ans = count * arr [ i ] NEW_LINE DEDENT DEDENT return price NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 22 , 87 , 9 , 50 , 56 , 43 ] NEW_LINE print ( maximumProfit ( arr , 6 ) ) NEW_LINE DEDENT