codeparrot/xlcost-text-to-code huggingface dataset:
Boundary Value Analysis : Nature of Roots of a Quadratic equation | BVA for nature of roots of a quadratic equation ; If a = 0 , D / 2 a will yield exception Hence it is not a valid Quadratic Equation ; If D > 0 , it will be Real Roots ; If D == 0 , it will be Equal Roots ; If D < 0 , it will be Imaginary Roots ; Function to check for all testcases ; Driver Code
def nature_of_roots ( a , b , c ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT print ( " Not ▁ a ▁ Quadratic ▁ Equation " ) ; NEW_LINE return ; NEW_LINE DEDENT D = b * b - 4 * a * c ; NEW_LINE if ( D > 0 ) : NEW_LINE INDENT print ( " Real ▁ Roots " ) ; NEW_LINE DEDENT elif ( D == 0 ) : NEW_LINE INDENT print ( " Equal ▁ Roots " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Imaginary ▁ Roots " ) ; NEW_LINE DEDENT DEDENT def checkForAllTestCase ( ) : NEW_LINE INDENT print ( " Testcase TABSYMBOL a TABSYMBOL b TABSYMBOL c TABSYMBOL Actual ▁ Output " ) ; NEW_LINE print ( ) ; NEW_LINE a = b = c = 0 ; NEW_LINE testcase = 1 ; NEW_LINE while ( testcase <= 13 ) : NEW_LINE INDENT if ( testcase == 1 ) : NEW_LINE INDENT a = 0 ; NEW_LINE b = 50 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 2 ) : NEW_LINE INDENT a = 1 ; NEW_LINE b = 50 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 3 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 50 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 4 ) : NEW_LINE INDENT a = 99 ; NEW_LINE b = 50 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 5 ) : NEW_LINE INDENT a = 100 ; NEW_LINE b = 50 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 6 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 0 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 7 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 1 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 8 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 99 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 9 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 100 ; NEW_LINE c = 50 ; NEW_LINE DEDENT elif ( testcase == 10 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 50 ; NEW_LINE c = 0 ; NEW_LINE DEDENT elif ( testcase == 11 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 50 ; NEW_LINE c = 1 ; NEW_LINE DEDENT elif ( testcase == 12 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 50 ; NEW_LINE c = 99 ; NEW_LINE DEDENT elif ( testcase == 13 ) : NEW_LINE INDENT a = 50 ; NEW_LINE b = 50 ; NEW_LINE c = 100 ; NEW_LINE DEDENT print ( " TABSYMBOL " , testcase , " TABSYMBOL " , a , " TABSYMBOL " , b , " TABSYMBOL " , c , " TABSYMBOL " , end = " " ) ; NEW_LINE nature_of_roots ( a , b , c ) ; NEW_LINE print ( ) ; NEW_LINE testcase += 1 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT checkForAllTestCase ( ) ; NEW_LINE DEDENT
Find if the Vacation can be taken or not | Function to find if the Vacation is possible or not ; Find the required number of hours of study ; find the hours of study that can be done if the vacation is taken ; check if the required hours are less than or equal to the hours of study that can be done if the vacation is taken ; Driver Code
def isPossible ( N , S , C , H , L , T ) : NEW_LINE INDENT total_time_required = S * C * H NEW_LINE available_time_after_vacation = ( N - L ) * T NEW_LINE if ( available_time_after_vacation >= total_time_required ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT N = 12 NEW_LINE S = 5 NEW_LINE C = 8 NEW_LINE H = 3 NEW_LINE L = 2 NEW_LINE T = 20 NEW_LINE if ( isPossible ( N , S , C , H , L , T ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT N = 1 NEW_LINE S = 2 NEW_LINE C = 3 NEW_LINE H = 4 NEW_LINE L = 5 NEW_LINE T = 6 NEW_LINE if ( isPossible ( N , S , C , H , L , T ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find Maximum and Minimum of two numbers using Absolute function | Function to return maximum among the two numbers ; Function to return minimum among the two numbers ; Driver code ; Displaying the maximum value ; Displaying the minimum value
def maximum ( x , y ) : NEW_LINE INDENT return ( ( x + y + abs ( x - y ) ) // 2 ) NEW_LINE DEDENT def minimum ( x , y ) : NEW_LINE INDENT return ( ( x + y - abs ( x - y ) ) // 2 ) NEW_LINE DEDENT x = 99 NEW_LINE y = 18 NEW_LINE print ( " Maximum : " , maximum ( x , y ) ) NEW_LINE print ( " Minimum : " , minimum ( x , y ) ) NEW_LINE
Find an array of size N having exactly K subarrays with sum S | Function to find array with K subarrays with sum S ; Driver Code ; Function call
def SubarraysWithSumS ( n , k , s ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT print ( s , end = " ▁ " ) NEW_LINE DEDENT for i in range ( k , n ) : NEW_LINE INDENT print ( s + 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 4 NEW_LINE k = 2 NEW_LINE s = 3 NEW_LINE SubarraysWithSumS ( n , k , s ) NEW_LINE
Minimum number of cuts required to pay salary from N length Gold Bar | Python Implementation to find the minimum number of cuts to pay the worker . ; Function to find the minimum number of cuts to pay the worker . ; Nearest Integer to the Log value of the number N ; Driver Code ; Cuts Required in the Length of 15
import math NEW_LINE def pay ( n ) : NEW_LINE INDENT cuts = int ( math . log ( n , 2 ) ) NEW_LINE return cuts NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE cuts = pay ( n ) NEW_LINE print ( cuts ) NEW_LINE n = 15 NEW_LINE cuts = pay ( n ) NEW_LINE print ( cuts ) NEW_LINE DEDENT
Sum of Maximum and Minimum prime factor of every number in the Array | Python3 implementation of the approach ; max_prime [ i ] represent maximum prime number that divides the number i ; min_prime [ i ] represent minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; if i is a prime number min_prime number that divide prime number and max_prime number that divide prime number is the number itself . ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to find the sum of the minimum and the maximum prime factors of every number from the given array ; Pre - calculation ; For every element of the given array ; The sum of its smallest and largest prime factor ; Driver code
MAX = 100000 NEW_LINE max_prime = [ 0 ] * ( MAX + 1 ) NEW_LINE min_prime = [ 0 ] * ( MAX + 1 ) NEW_LINE def sieve ( n ) : NEW_LINE INDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( min_prime [ i ] > 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT min_prime [ i ] = i NEW_LINE max_prime [ i ] = i NEW_LINE j = i + i NEW_LINE while ( j <= n ) : NEW_LINE INDENT if ( min_prime [ j ] == 0 ) : NEW_LINE INDENT min_prime [ j ] = i NEW_LINE DEDENT max_prime [ j ] = i NEW_LINE j += i NEW_LINE DEDENT DEDENT DEDENT def findSum ( arr , n ) : NEW_LINE INDENT sieve ( MAX ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = min_prime [ arr [ i ] ] + max_prime [ arr [ i ] ] NEW_LINE print ( sum , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 5 , 10 , 15 , 20 , 25 , 30 ] NEW_LINE n = len ( arr ) NEW_LINE findSum ( arr , n ) NEW_LINE
Pair of integers ( a , b ) which satisfy the given equations | Function to count valid pairs ; Driver code
def pairCount ( n , m ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for b in range ( int ( pow ( m , 1 / 2 ) ) ) : NEW_LINE INDENT a = m - b * b ; NEW_LINE if ( a * a + b == n ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 9 ; NEW_LINE m = 3 ; NEW_LINE print ( pairCount ( n , m ) ) ; NEW_LINE DEDENT
Largest Divisor for each element in an array other than 1 and the number itself | Python3 implementation of the approach ; Divisors array to keep track of the maximum divisor ; Function to pre - compute the prime numbers and largest divisors ; Visited array to keep track of prime numbers ; 0 and 1 are not prime numbers ; Initialising divisors [ i ] = i ; For all the numbers divisible by 2 the maximum divisor will be number / 2 ; If divisors [ i ] is not equal to i then this means that divisors [ i ] contains minimum prime divisor for the number ; Update the answer to i / smallest_prime_divisor [ i ] ; Condition if i is a prime number ; If divisors [ j ] is equal to j then this means that i is the first prime divisor for j so we update divi [ j ] = i ; If the current element is prime then it has no divisors other than 1 and itself ; Driver code
maxin = 100001 ; NEW_LINE divisors = [ 0 ] * ( maxin + 1 ) ; NEW_LINE def Calc_Max_Div ( arr , n ) : NEW_LINE INDENT vis = [ 1 ] * ( maxin + 1 ) ; NEW_LINE vis [ 0 ] = vis [ 1 ] = 0 ; NEW_LINE for i in range ( 1 , maxin + 1 ) : NEW_LINE INDENT divisors [ i ] = i ; NEW_LINE DEDENT for i in range ( 4 , maxin + 1 , 2 ) : NEW_LINE INDENT vis [ i ] = 0 ; NEW_LINE divisors [ i ] = i // 2 ; NEW_LINE DEDENT for i in range ( 3 , maxin + 1 , 2 ) : NEW_LINE INDENT if ( divisors [ i ] != i ) : NEW_LINE INDENT divisors [ i ] = i // divisors [ i ] ; NEW_LINE DEDENT if ( vis [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i * i , maxin , i ) : NEW_LINE INDENT vis [ j ] = 0 ; NEW_LINE if ( divisors [ j ] == j ) : NEW_LINE INDENT divisors [ j ] = i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( divisors [ arr [ i ] ] == arr [ i ] ) : NEW_LINE INDENT print ( " - 1 ▁ " , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( divisors [ arr [ i ] ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 7 , 8 , 9 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE Calc_Max_Div ( arr , n ) ; NEW_LINE DEDENT
Given a number N in decimal base , find the sum of digits in any base B | Function to compute sum of Digits of Number N in base B ; Initialize sum with 0 ; Compute unit digit of the number ; Add unit digit in sum ; Update value of Number ; Driver code
def sumOfDigit ( n , b ) : NEW_LINE INDENT unitDigit = 0 NEW_LINE sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT unitDigit = n % b NEW_LINE sum += unitDigit NEW_LINE n = n // b NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 50 NEW_LINE b = 2 NEW_LINE print ( sumOfDigit ( n , b ) ) NEW_LINE
Find the Nth digit from right in base B of the given number in Decimal base | Function to compute Nth digit from right in base B ; Skip N - 1 Digits in Base B ; Nth Digit from right in Base B ; Driver Code
def nthDigit ( a , n , b ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT a = a // b NEW_LINE DEDENT return a % b NEW_LINE DEDENT a = 100 NEW_LINE n = 3 NEW_LINE b = 4 NEW_LINE print ( nthDigit ( a , n , b ) ) NEW_LINE
Find three prime numbers with given sum | Python3 implementation of the approach ; The vector primes holds the prime numbers ; Function to generate prime numbers ; Initialize the array elements to 0 s ; Set the non - primes to true ; Fill the vector primes with prime numbers which are marked as false in the numbers array ; Function to print three prime numbers which sum up to the number N ; Take the first prime number ; Take the second prime number ; Subtract the two prime numbers from the N to obtain the third number ; If the third number is prime ; Print the three prime numbers if the solution exists ; Driver code ; Function for generating prime numbers using Sieve of Eratosthenes ; Function to print the three prime numbers whose sum is equal to N
from math import sqrt NEW_LINE MAX = 100001 ; NEW_LINE primes = [ ] ; NEW_LINE def initialize ( ) : NEW_LINE INDENT numbers = [ 0 ] * ( MAX + 1 ) ; NEW_LINE n = MAX ; NEW_LINE for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( not numbers [ i ] ) : NEW_LINE INDENT for j in range ( i * i , n + 1 , i ) : NEW_LINE INDENT numbers [ j ] = True ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( numbers [ i ] == False ) : NEW_LINE INDENT primes . append ( i ) ; NEW_LINE DEDENT DEDENT DEDENT def findNums ( num ) : NEW_LINE INDENT ans = False ; NEW_LINE first = - 1 ; NEW_LINE second = - 1 ; NEW_LINE third = - 1 ; NEW_LINE for i in range ( num ) : NEW_LINE INDENT first = primes [ i ] ; NEW_LINE for j in range ( num ) : NEW_LINE INDENT second = primes [ j ] ; NEW_LINE third = num - first - second ; NEW_LINE if ( third in primes ) : NEW_LINE INDENT ans = True ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( ans ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( ans ) : NEW_LINE INDENT print ( first , second , third ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 101 ; NEW_LINE initialize ( ) ; NEW_LINE findNums ( n ) ; NEW_LINE DEDENT
Print all maximal increasing contiguous sub | Function to print each of maximal contiguous increasing subarray ; Loop to iterate through the array and print the maximal contiguous increasing subarray . ; Condition to check whether the element at i , is greater than its next neighbouring element or not . ; Driver function
def printmaxSubseq ( arr , n ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ i ] ) ; NEW_LINE DEDENT DEDENT print ( arr [ n - 1 ] ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 9 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE printmaxSubseq ( arr , n ) ; NEW_LINE DEDENT
Count of non decreasing arrays of length N formed with values in range L to R | Function to return the count of different arrays ; No such combination exists ; Arrays formed with single elements ; Driver code
def countSum ( N , L , R ) : NEW_LINE INDENT if ( L > R ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( N == 1 ) : NEW_LINE INDENT return R - L + 1 ; NEW_LINE DEDENT if ( N > 1 ) : NEW_LINE INDENT return ( N - 2 ) * ( R - L ) + 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , L , R = 4 , 4 , 6 ; NEW_LINE print ( countSum ( N , L , R ) ) ; NEW_LINE DEDENT
Minimum decrement operations to make Array elements equal by only decreasing K each time | Python3 implementation of the above approach ; Finding the minimum element ; Loop over all the elements and find the difference ; Solution found and returned ; Driver code
import sys NEW_LINE def solve ( arr , n , k ) : NEW_LINE INDENT minx = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT minx = min ( minx , arr [ i ] ) ; NEW_LINE DEDENT decrements = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr [ i ] - minx ) % k != 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT decrements += ( ( arr [ i ] - minx ) // k ) ; NEW_LINE DEDENT DEDENT return decrements ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE k = 3 ; NEW_LINE arr = [ 12 , 9 , 15 ] ; NEW_LINE print ( solve ( arr , n , k ) ) ; NEW_LINE DEDENT
Count non decreasing subarrays of size N from N Natural numbers | Returns value of Binomial Coefficient C ( n , k ) ; Since nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Function to find the count of required subarrays ; The required count is the binomial coefficient as explained in the approach above ; Driver Function
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ 0 ] * ( k + 1 ) ; NEW_LINE C [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) , 0 , - 1 ) : NEW_LINE INDENT C [ j ] = C [ j ] + C [ j - 1 ] ; NEW_LINE DEDENT DEDENT return C [ k ] ; NEW_LINE DEDENT def count_of_subarrays ( N ) : NEW_LINE INDENT count = binomialCoeff ( 2 * N - 1 , N ) ; NEW_LINE return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 ; NEW_LINE print ( count_of_subarrays ( N ) ) ; NEW_LINE DEDENT
Length of Smallest subarray in range 1 to N with sum greater than a given value | Function to do a binary search on a given range . ; Total sum is the sum of N numbers . ; Sum until mid ; If remaining sum is < the required value , then the required number is in the right half ; Driver code
def usingBinarySearch ( start , end , N , S ) : NEW_LINE INDENT if ( start >= end ) : NEW_LINE INDENT return start ; NEW_LINE DEDENT mid = start + ( end - start ) // 2 ; NEW_LINE totalSum = ( N * ( N + 1 ) ) // 2 ; NEW_LINE midSum = ( mid * ( mid + 1 ) ) // 2 ; NEW_LINE if ( ( totalSum - midSum ) <= S ) : NEW_LINE INDENT return usingBinarySearch ( start , mid , N , S ) ; NEW_LINE DEDENT return usingBinarySearch ( mid + 1 , end , N , S ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE S = 11 ; NEW_LINE print ( N - usingBinarySearch ( 1 , N , N , S ) + 1 ) ; NEW_LINE DEDENT
Distribution of a Number in Array within a Range | Function for the distribution of the number ; Distribute the number among k elements ; If there is some remaining sum to distribute ; If there are elements remaining to distribute i . e . ( n - k ) ; Divide the remaining sum into n - k elements ; Print the distribution ; Driver code
def distribution ( n , k , l , r , S , Sk ) : NEW_LINE INDENT a = [ 0 ] * n ; NEW_LINE len = k ; NEW_LINE temp , rem , s = 0 , 0 , 0 ; NEW_LINE diff = S - Sk ; NEW_LINE for i in range ( len ) : NEW_LINE INDENT temp = Sk / k ; NEW_LINE rem = Sk % k ; NEW_LINE if ( temp + rem >= l and temp + rem <= r ) : NEW_LINE INDENT a [ i ] = temp ; NEW_LINE DEDENT elif ( temp + rem > r ) : NEW_LINE INDENT a [ i ] = r ; NEW_LINE DEDENT elif ( temp + rem < r ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT Sk = Sk - a [ i ] ; NEW_LINE k = k - 1 ; NEW_LINE DEDENT if ( Sk > 0 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT if ( len != 0 ) : NEW_LINE INDENT k = n - len ; NEW_LINE for i in range ( len , n ) : NEW_LINE INDENT temp = diff / k ; NEW_LINE rem = diff % k ; NEW_LINE if ( temp + rem >= l and temp + rem <= r ) : NEW_LINE INDENT a [ i ] = temp ; NEW_LINE DEDENT elif ( temp + rem > r ) : NEW_LINE INDENT a [ i ] = r ; NEW_LINE DEDENT elif ( temp + rem < r ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT diff = diff - a [ i ] ; NEW_LINE k = k - 1 ; NEW_LINE DEDENT if ( diff != 0 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( int ( a [ i ] ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , k , l , r , S , Sk = 5 , 3 , 1 , 5 , 13 , 9 ; NEW_LINE distribution ( n , k , l , r , S , Sk ) ; NEW_LINE DEDENT
Number of valid indices in the permutation of first N natural numbers | Python3 implementation of the approach ; Function to return the number of i 's such that Pi <= Pj for all 1 <= j <= i in the permutation of first N natural numbers ; To store the count of such indices ; Store the mini value ; For all the elements ; Return the required answer ; Driver code
import sys NEW_LINE INT_MAX = sys . maxsize NEW_LINE def min_index ( p , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE mini = INT_MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( p [ i ] <= mini ) : NEW_LINE INDENT mini = p [ i ] ; NEW_LINE DEDENT if ( mini == p [ i ] ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT P = [ 4 , 2 , 5 , 1 , 3 ] ; NEW_LINE n = len ( P ) ; NEW_LINE print ( min_index ( P , n ) ) ; NEW_LINE DEDENT
Check if Sum and XOR of all elements of array is equal | Function to Check if Sum and XOR of all elements of array is equal ; Sum and XOR of all elements ; Checking Sum and XOR to be equal ; Driver Function ; Check Sum and XOR is equal
def equal_xor_sum ( arr , n ) : NEW_LINE INDENT Sum = 0 ; NEW_LINE Xor = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum = Sum + arr [ i ] ; NEW_LINE Xor = Xor ^ arr [ i ] ; NEW_LINE DEDENT if ( Sum == Xor ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 3 , 7 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE equal_xor_sum ( arr , n ) ; NEW_LINE DEDENT
Count of odd length contiguous Palindromic sequences in a Matrix | Python code to Count the odd length contiguous Palindromic sequences in the matrix ; Function to count the number of contiguous palindromic sequences in the matrix ; Add the total number of elements in the matrix to the count ; Length of possible sequence to be checked for palindrome horizontally and vertically ; Iterate through each element of the matrix and count the number of palindromic sequences in each row and column ; Find the possible length of sequences that can be a palindrome ; From i , check if the sequence formed by elements to its left and right is palindrome or not ; if the sequence [ i , j - k ] to [ i , j + k ] is a palindrome , increment the count by 1 ; From i , check if the sequence formed by elements to its above and below is palindrome or not ; if the sequence [ i - k , j ] to [ i + k , j ] is a palindrome , increment the count by 1 ; Return the total count of the palindromic sequences ; Driver code
MAX = 10 ; NEW_LINE def countPalindromes ( n , m , matrix ) : NEW_LINE INDENT count = n * m ; NEW_LINE length_of_sequence_row = 0 ; NEW_LINE length_of_sequence_column = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT length_of_sequence_row = min ( j , m - 1 - j ) ; NEW_LINE length_of_sequence_column = min ( i , n - i - 1 ) ; NEW_LINE for k in range ( 1 , length_of_sequence_row + 1 ) : NEW_LINE INDENT if ( matrix [ i ] [ j - k ] == matrix [ i ] [ j + k ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT for k in range ( 1 , length_of_sequence_column + 1 ) : NEW_LINE INDENT if ( matrix [ i - k ] [ j ] == matrix [ i + k ] [ j ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 3 ; NEW_LINE n = 3 ; NEW_LINE matrix = [ 2 , 1 , 2 ] , [ 1 , 1 , 1 ] , [ 2 , 1 , 2 ] ; NEW_LINE print ( countPalindromes ( n , m , matrix ) ) ; NEW_LINE DEDENT
Reduce a number to 1 by performing given operations | Set 2 | Function to return the number of set bits in n ; Function to return the minimum steps required to reach 1 ; If n is even then divide it by 2 ; If n is 3 or the number of set bits in ( n - 1 ) is less than the number of set bits in ( n + 1 ) ; Increment the number of steps ; Return the minimum number of steps ; Driver code
def set_bits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT count += n % 2 NEW_LINE n //= 2 NEW_LINE DEDENT return count NEW_LINE DEDENT def minSteps ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( n != 1 ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT n //= 2 NEW_LINE DEDENT elif ( n == 3 or set_bits ( n - 1 ) < set_bits ( n + 1 ) ) : NEW_LINE INDENT n -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT n += 1 NEW_LINE DEDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 15 NEW_LINE print ( minSteps ( n ) ) NEW_LINE