codeparrot/xlcost-text-to-code huggingface dataset:
Check if N can be represented as sum of distinct powers of 3 | Function to try all permutations of distinct powers ; Base Case ; If the distinct powers sum is obtained ; Otherwise ; If current element not selected in power [ ] ; If current element selected in power [ ] ; Return 1 if any permutation found ; Function to check the N can be represented as the sum of the distinct powers of 3 ; Stores the all distincts powers of three to [ 0 , 15 ] ; Function Call ; Print ; Driver Code
def PermuteAndFind ( power , idx , SumSoFar , target ) : NEW_LINE INDENT if ( idx == len ( power ) ) : NEW_LINE INDENT if ( SumSoFar == target ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT select = PermuteAndFind ( power , idx + 1 , SumSoFar , target ) NEW_LINE notselect = PermuteAndFind ( power , idx + 1 , SumSoFar + power [ idx ] , target ) NEW_LINE return ( select or notselect ) NEW_LINE DEDENT def DistinctPowersOf3 ( N ) : NEW_LINE INDENT power = [ 0 for x in range ( 16 ) ] NEW_LINE power [ 0 ] = 1 NEW_LINE for i in range ( 1 , 16 ) : NEW_LINE INDENT power [ i ] = 3 * power [ i - 1 ] NEW_LINE DEDENT found = PermuteAndFind ( power , 0 , 0 , N ) NEW_LINE if ( found == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT N = 91 NEW_LINE DistinctPowersOf3 ( N ) NEW_LINE
Check if N can be represented as sum of distinct powers of 3 | Function to check whether the given N can be represented as the sum of the distinct powers of 3 ; Iterate until N is non - zero ; Termination Condition ; Right shift ternary bits by 1 for the next digit ; If N can be expressed as the sum of perfect powers of 3 ; Driver Code
def DistinctPowersOf3 ( N ) : NEW_LINE INDENT while ( N > 0 ) : NEW_LINE INDENT if ( N % 3 == 2 ) : NEW_LINE INDENT cout << " No " NEW_LINE return NEW_LINE DEDENT N //= 3 NEW_LINE DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 91 NEW_LINE DistinctPowersOf3 ( N ) NEW_LINE DEDENT
Generate an N | Function to print permutation of size N with absolute difference of adjacent elements in range [ 2 , 4 ] ; If N is less than 4 ; Check if N is even ; Traverse through odd integers ; Update the value of i ; Traverse through even integers ; Driver Code
def getPermutation ( N ) : NEW_LINE INDENT if ( N <= 3 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT i = N NEW_LINE if ( N % 2 == 0 ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT while ( i >= 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE i -= 2 NEW_LINE DEDENT print ( 4 , 2 , end = " ▁ " ) NEW_LINE i = 6 NEW_LINE while ( i <= N ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE i += 2 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9 NEW_LINE getPermutation ( N ) NEW_LINE DEDENT
Count of distinct values till C formed by adding or subtracting A , B , or 0 any number of times | Function to calculate gcd ; Function to find number of possible final values ; Find the gcd of two numbers ; Calculate number of distinct values ; Return values ; Driver Code
def gcd ( A , B ) : NEW_LINE INDENT if ( B == 0 ) : NEW_LINE INDENT return A ; NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( B , A % B ) ; NEW_LINE DEDENT DEDENT def getDistinctValues ( A , B , C ) : NEW_LINE INDENT g = gcd ( A , B ) ; NEW_LINE num_values = C / g ; NEW_LINE return int ( num_values ) ; NEW_LINE DEDENT A = 2 ; NEW_LINE B = 3 ; NEW_LINE C = 10 ; NEW_LINE print ( getDistinctValues ( A , B , C ) ) ; NEW_LINE
Find array whose elements are XOR of adjacent elements in given array | Function to reconstruct the array arr [ ] with xor of adjacent elements ; Iterate through each element ; Store the xor of current and next element in arr [ i ] ; Function to print array ; Driver Code ; Inputs ; Length of the array given ; Function call to reconstruct the arr [ ] ; Function call to prarr [ ]
def game_with_number ( arr , n ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i ] ^ arr [ i + 1 ] NEW_LINE DEDENT return arr NEW_LINE DEDENT def printt ( arr , n ) : NEW_LINE INDENT print ( * arr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 11 , 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE new_arr = game_with_number ( arr , n ) ; NEW_LINE printt ( new_arr , n ) NEW_LINE DEDENT
Maximize score of same | Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Traverse the current subarray ; Finding the score without reversing the subarray ; Calculating the score of the reversed subarray ; Return the score of subarray ; Function to find the subarray with the maximum score ; Stores the maximum score and the starting and the ending point of subarray with maximum score ; Traverse all the subarrays ; Store the score of the current subarray ; Update the maximum score ; Prthe maximum score ; Driver Code
def currSubArrayScore ( a , b , l , r ) : NEW_LINE INDENT straightScore = 0 NEW_LINE reverseScore = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT straightScore += a [ i ] * b [ i ] NEW_LINE reverseScore += a [ r - ( i - l ) ] * b [ i ] NEW_LINE DEDENT return max ( straightScore , reverseScore ) NEW_LINE DEDENT def maxScoreSubArray ( a , b , n ) : NEW_LINE INDENT res = 0 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT currScore = currSubArrayScore ( a , b , i , j ) NEW_LINE if ( currScore > res ) : NEW_LINE INDENT res = currScore NEW_LINE start = i NEW_LINE end = j NEW_LINE DEDENT DEDENT DEDENT print ( res ) NEW_LINE DEDENT A = [ 13 , 4 , 5 ] NEW_LINE B = [ 10 , 22 , 2 ] NEW_LINE N = len ( A ) NEW_LINE maxScoreSubArray ( A , B , N ) NEW_LINE
Maximize score of same | Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Store the required result ; Iterate in the range [ 0 , N - 1 ] ; Consider the case of odd length subarray ; Update the maximum score ; Expanding the subarray in both directions with equal length so that mid poremains same ; Update both the scores ; Consider the case of even length subarray ; Update both the scores ; Print the result ; Driver Code
def maxScoreSubArray ( a , b , n ) : NEW_LINE INDENT res = 0 NEW_LINE for mid in range ( n ) : NEW_LINE INDENT straightScore = a [ mid ] * b [ mid ] NEW_LINE reverseScore = a [ mid ] * a [ mid ] NEW_LINE prev = mid - 1 NEW_LINE next = mid + 1 NEW_LINE res = max ( res , max ( straightScore , reverseScore ) ) NEW_LINE while ( prev >= 0 and next < n ) : NEW_LINE INDENT straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) NEW_LINE reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) NEW_LINE res = max ( res , max ( straightScore , reverseScore ) ) NEW_LINE prev -= 1 NEW_LINE next += 1 NEW_LINE DEDENT straightScore = 0 NEW_LINE reverseScore = 0 NEW_LINE prev = mid - 1 NEW_LINE next = mid NEW_LINE while ( prev >= 0 and next < n ) : NEW_LINE INDENT straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) NEW_LINE reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) NEW_LINE res = max ( res , max ( straightScore , reverseScore ) ) NEW_LINE prev -= 1 NEW_LINE next += 1 NEW_LINE DEDENT DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 13 , 4 , 5 ] NEW_LINE B = [ 10 , 22 , 2 ] NEW_LINE N = len ( A ) NEW_LINE maxScoreSubArray ( A , B , N ) NEW_LINE DEDENT
Length of the smallest subarray with maximum possible sum | Function to find the minimum length of the subarray whose sum is maximum ; Stores the starting and the ending index of the resultant subarray ; Traverse the array until a non - zero element is encountered ; If the array contains only of 0 s ; Traverse the array in reverse until a non - zero element is encountered ; Return the resultant size of the subarray ; Driver Code
def minimumSizeSubarray ( arr , N ) : NEW_LINE INDENT i , j = 0 , N - 1 NEW_LINE while ( i < N and arr [ i ] == 0 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i == N ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT while ( j >= 0 and arr [ j ] == 0 ) : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT return ( j - i + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minimumSizeSubarray ( arr , N ) ) NEW_LINE DEDENT
Count ways to represent N as XOR of distinct integers not exceeding N | Python program for the above approach ; Function to count number of ways to represent N as the Bitwise XOR of distinct integers ; Count number of subsets using above - mentioned formula ; Print the resultant count ; Driver Code
from math import * NEW_LINE def countXorPartition ( N ) : NEW_LINE INDENT a = 2 ** floor ( N - log ( N + 1 ) / log ( 2 ) ) NEW_LINE print ( int ( a ) ) NEW_LINE DEDENT N = 5 NEW_LINE countXorPartition ( N ) NEW_LINE
Count numbers less than N whose modulo with A is equal to B | Function to count numbers less than N , whose modulo with A gives B ; If the value of B at least A ; If the value of B is 0 or not ; Stores the resultant count of numbers less than N ; Update the value of ans ; Print the value of ans ; Driver Code
def countValues ( A , B , C ) : NEW_LINE INDENT if ( B >= A ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT if ( B == 0 ) : NEW_LINE INDENT print ( C // A ) NEW_LINE return NEW_LINE DEDENT ans = C // A NEW_LINE if ( ans * A + B <= C ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 6 NEW_LINE B = 3 NEW_LINE N = 15 NEW_LINE countValues ( A , B , N ) NEW_LINE DEDENT
Maximum sum of a subsequence whose Bitwise AND is non | Function to find the maximum sum of a subsequence whose Bitwise AND is non - zero ; Stores the resultant maximum sum of the subsequence ; Iterate over all the bits ; Stores the sum of array elements whose i - th bit is set ; Traverse the array elements ; If the bit is set , then add its value to the sum ; Update the resultant maximum sum ; Return the maximum sum ; Driver Code
def maximumSum ( arr , N ) : NEW_LINE INDENT ans = 0 NEW_LINE for bit in range ( 32 ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] & ( 1 << bit ) ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT ans = max ( ans , sum ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 1 , 7 , 11 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maximumSum ( arr , N ) ) NEW_LINE DEDENT
Check if an array can be reduced to at most length K by removal of distinct elements | Function to check if it is possible to reduce the size of the array to K by removing the set of the distinct array elements ; Stores all distinct elements present in the array arr [ ] ; Traverse the given array ; Insert array elements into the set ; Condition for reducing size of the array to at most K ; Driver Code
def maxCount ( arr , N , K ) : NEW_LINE INDENT st = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT st . add ( arr [ i ] ) NEW_LINE DEDENT if ( N - len ( st ) <= K ) : 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 = [ 2 , 2 , 2 , 3 ] NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE maxCount ( arr , N , K ) NEW_LINE DEDENT
Maximum element present in the array after performing queries to add K to range of indices [ L , R ] | Function to find the max sum after processing q queries ; Store the cumulative sum ; Store the maximum sum ; Iterate over the range 0 to q ; Variables to extract values from vector ; Iterate over the range [ 1 , n ] ; Calculate cumulative sum ; Calculate maximum sum ; Return the maximum sum after q queries ; Stores the size of array and number of queries ; Stores the sum ; Storing input queries ; Function call to find the maximum sum
def max_sum ( a , v , q , n ) : NEW_LINE INDENT x = 0 ; NEW_LINE m = - 10 ** 9 ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT p = v [ i ] [ 0 ] [ 0 ] ; NEW_LINE q = v [ i ] [ 0 ] [ 1 ] ; NEW_LINE k = v [ i ] [ 1 ] ; NEW_LINE a [ p ] += k ; NEW_LINE if ( q + 1 <= n ) : NEW_LINE INDENT a [ q + 1 ] -= k ; NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT x += a [ i ] ; NEW_LINE m = max ( m , x ) ; NEW_LINE DEDENT return m ; NEW_LINE DEDENT n = 10 NEW_LINE q = 3 ; NEW_LINE a = [ 0 ] * ( n + 5 ) ; NEW_LINE v = [ [ [ 0 for i in range ( 2 ) ] for x in range ( 2 ) ] for z in range ( q ) ] NEW_LINE v [ 0 ] [ 0 ] [ 0 ] = 1 ; NEW_LINE v [ 0 ] [ 0 ] [ 1 ] = 5 ; NEW_LINE v [ 0 ] [ 1 ] = 3 ; NEW_LINE v [ 1 ] [ 0 ] [ 0 ] = 4 ; NEW_LINE v [ 1 ] [ 0 ] [ 1 ] = 8 ; NEW_LINE v [ 1 ] [ 1 ] = 7 ; NEW_LINE v [ 2 ] [ 0 ] [ 0 ] = 6 ; NEW_LINE v [ 2 ] [ 0 ] [ 1 ] = 9 ; NEW_LINE v [ 2 ] [ 1 ] = 1 ; NEW_LINE print ( max_sum ( a , v , q , n ) ) ; NEW_LINE
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Stores the Bitwise XOR of X & Y ; Traverse all possible pairs ; If a pair exists ; If a pair is found ; Driver Code
def findWinner ( X , Y ) : NEW_LINE INDENT playerA = ( X ^ Y ) NEW_LINE flag = False NEW_LINE for i in range ( 1 , X + 1 , 1 ) : NEW_LINE INDENT for j in range ( 1 , Y + 1 , 1 ) : NEW_LINE INDENT val = ( i ^ j ) NEW_LINE if ( val > playerA ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 2 NEW_LINE B = 4 NEW_LINE findWinner ( A , B ) NEW_LINE DEDENT
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Check for the invalid condition ; Otherwise , ; Driver Code
def findWinner ( X , Y ) : NEW_LINE INDENT first = ( X ^ Y ) NEW_LINE second = ( X + Y ) NEW_LINE if ( first == second ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B = 2 , 4 NEW_LINE findWinner ( A , B ) NEW_LINE DEDENT
Smallest number required to be added to M to make it divisible by N | Function to find the smallest number greater than or equal to N , that is divisible by k ; Function to find the smallest number required to be added to to M to make it divisible by N ; Stores the smallest multiple of N , greater than or equal to M ; Return the result ; Driver Code ; Given Input ; Function Call
def findNum ( N , K ) : NEW_LINE INDENT rem = ( N + K ) % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N + K - rem NEW_LINE DEDENT DEDENT def findSmallest ( M , N ) : NEW_LINE INDENT x = findNum ( M , N ) NEW_LINE return x - M NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 100 NEW_LINE N = 28 NEW_LINE print ( findSmallest ( M , N ) ) NEW_LINE DEDENT
Prefix Factorials of a Prefix Sum Array | Function to find the factorial of prefix sum at every possible index ; Find the prefix sum array ; Stores the factorial of all the element till the sum of array ; Find the factorial array ; Find the factorials of each array element ; Print the resultant array ; Driver code
def prefixFactorialArray ( A , N ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT A [ i ] += A [ i - 1 ] NEW_LINE DEDENT fact = [ 0 for x in range ( A [ N - 1 ] + 1 ) ] NEW_LINE fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , A [ N - 1 ] + 1 ) : NEW_LINE INDENT fact [ i ] = i * fact [ i - 1 ] NEW_LINE DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT A [ i ] = fact [ A [ i ] ] NEW_LINE DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE prefixFactorialArray ( arr , N ) NEW_LINE
Maximum frequency of any array element possible by at most K increments | Function to find the maximum possible frequency of a most frequent element after at most K increment operations ; Sort the input array ; Stores the sum of sliding window and the maximum possible frequency of any array element ; Traverse the array ; Add the current element to the window ; If it is not possible to make the array elements in the window equal ; Update the value of sum ; Increment the value of start ; Update the maximum possible frequency ; Print the frequency of the most frequent array element after K increments ; Driver code
def maxFrequency ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE sum = 0 NEW_LINE res = 0 NEW_LINE for end in range ( N ) : NEW_LINE INDENT sum += arr [ end ] NEW_LINE while ( ( end - start + 1 ) * arr [ end ] - sum > K ) : NEW_LINE INDENT sum -= arr [ start ] NEW_LINE start += 1 NEW_LINE DEDENT res = max ( res , end - start + 1 ) NEW_LINE DEDENT print ( res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 8 , 13 ] NEW_LINE N = 4 NEW_LINE K = 5 NEW_LINE maxFrequency ( arr , N , K ) NEW_LINE DEDENT
Count pairs ( i , j ) from an array such that i < j and arr [ j ] | Function to count the number of pairs ( i , j ) such that i < j and arr [ i ] - arr [ j ] = X * ( j - i ) ; Stores the count of all such pairs that satisfies the condition . ; Stores count of distinct values of arr [ i ] - x * i ; Iterate over the Map ; Increase count of pairs ; Print the count of such pairs ; Driver Code
def countPairs ( arr , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr [ i ] - x * i ) in mp ) : NEW_LINE INDENT mp [ arr [ i ] - x * i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] - x * i ] = 1 NEW_LINE DEDENT DEDENT for key , value in mp . items ( ) : NEW_LINE INDENT n = value NEW_LINE count += ( n * ( n - 1 ) ) // 2 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE x = 3 NEW_LINE arr = [ 5 , 4 , 8 , 11 , 13 , 16 ] NEW_LINE countPairs ( arr , n , x ) NEW_LINE DEDENT
Check if a triplet of buildings can be selected such that the third building is taller than the first building and smaller than the second building | Function to check if it is possible to select three buildings that satisfy the given condition ; Stores prefix min array ; Iterate over the range [ 1 , N - 1 ] ; Stores the element from the ending in increasing order ; Iterate until j is greater than or equal to 0 ; If current array element is greater than the prefix min upto j ; Iterate while stack is not empty and top element is less than or equal to preMin [ j ] ; Remove the top element ; If stack is not empty and top element of the stack is less than the current element ; Push the arr [ j ] in stack ; If none of the above case satisfy then return " No " ; Driver code ; Input
def recreationalSpot ( arr , N ) : NEW_LINE INDENT if ( N < 3 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT preMin = [ 0 ] * N NEW_LINE preMin [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT preMin [ i ] = min ( preMin [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT stack = [ ] NEW_LINE for j in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ j ] > preMin [ j ] ) : NEW_LINE INDENT while ( len ( stack ) > 0 and stack [ - 1 ] <= preMin [ j ] ) : NEW_LINE INDENT del stack [ - 1 ] NEW_LINE DEDENT if ( len ( stack ) > 0 and stack [ - 1 ] < arr [ j ] ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT stack . append ( arr [ j ] ) NEW_LINE DEDENT DEDENT return " No " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 7 , 11 , 5 , 13 , 2 ] NEW_LINE size = len ( arr ) NEW_LINE print ( recreationalSpot ( arr , size ) ) NEW_LINE DEDENT
Check if a number N can be expressed as the sum of powers of X or not | Function to check if the number N can be expressed as the sum of different powers of X or not ; While n is a positive number ; Find the remainder ; If rem is at least 2 , then representation is impossible ; Divide the value of N by x ; Driver Code
def ToCheckPowerofX ( n , x ) : NEW_LINE INDENT while ( n > 0 ) : NEW_LINE INDENT rem = n % x NEW_LINE if ( rem >= 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = n // x NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE X = 3 NEW_LINE if ( ToCheckPowerofX ( N , X ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Find the maximum between N and the number formed by reversing 32 | Function that obtains the number using said operations from N ; Stores the binary representation of the number N ; Find the binary representation of the number N ; Check for the set bits ; Reverse the string S ; Stores the obtained number ; Calculating the decimal value ; Check for set bits ; Function to find the maximum value between N and the obtained number ; Driver Code
def reverseBin ( N ) : NEW_LINE INDENT S = " " NEW_LINE i = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT if ( N & ( 1 << i ) ) : NEW_LINE INDENT S += '1' NEW_LINE DEDENT else : NEW_LINE INDENT S += '0' NEW_LINE DEDENT DEDENT S = list ( S ) NEW_LINE S = S [ : : - 1 ] NEW_LINE S = ' ' . join ( S ) NEW_LINE M = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT M += ( 1 << i ) NEW_LINE DEDENT DEDENT return M NEW_LINE DEDENT def maximumOfTwo ( N ) : NEW_LINE INDENT M = reverseBin ( N ) NEW_LINE return max ( N , M ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE print ( maximumOfTwo ( N ) ) NEW_LINE DEDENT
Sum of Euler Totient Functions obtained for each divisor of N | Function to find the sum of Euler Totient Function of divisors of N ; Return the value of N ; Driver Code
def sumOfDivisors ( N ) : NEW_LINE INDENT return N NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( sumOfDivisors ( N ) ) NEW_LINE DEDENT
Square root of two Complex Numbers | Python3 program for the above approach ; Function to find the square root of a complex number ; Stores all the square roots ; Stores the first square root ; Push the square root in the ans ; Stores the second square root ; If X2 is not 0 ; Push the square root in the array ans [ ] ; Stores the third square root ; If X3 is greater than 0 ; Push the square root in the array ans [ ] ; Stores the fourth square root ; Push the square root in the array ans [ ] ; Prints the square roots ; Driver Code
from math import sqrt NEW_LINE def complexRoot ( A , B ) : NEW_LINE INDENT ans = [ ] NEW_LINE X1 = abs ( sqrt ( ( A + sqrt ( A * A + B * B ) ) / 2 ) ) NEW_LINE Y1 = B / ( 2 * X1 ) NEW_LINE ans . append ( [ X1 , Y1 ] ) NEW_LINE X2 = - 1 * X1 NEW_LINE Y2 = B / ( 2 * X2 ) NEW_LINE if ( X2 != 0 ) : NEW_LINE INDENT ans . append ( [ X2 , Y2 ] ) NEW_LINE DEDENT X3 = ( A - sqrt ( A * A + B * B ) ) / 2 NEW_LINE if ( X3 > 0 ) : NEW_LINE INDENT X3 = abs ( sqrt ( X3 ) ) NEW_LINE Y3 = B / ( 2 * X3 ) NEW_LINE ans . append ( [ X3 , Y3 ] ) NEW_LINE X4 = - 1 * X3 NEW_LINE Y4 = B / ( 2 * X4 ) NEW_LINE if ( X4 != 0 ) : NEW_LINE INDENT ans . append ( [ X4 , Y4 ] ) NEW_LINE DEDENT DEDENT print ( " The ▁ Square ▁ roots ▁ are : ▁ " ) NEW_LINE for p in ans : NEW_LINE INDENT print ( round ( p [ 0 ] , 6 ) , end = " " ) NEW_LINE if ( p [ 1 ] > 0 ) : NEW_LINE INDENT print ( " + " , end = " " ) NEW_LINE DEDENT if ( p [ 1 ] ) : NEW_LINE INDENT print ( str ( round ( p [ 1 ] , 6 ) ) + " * i " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B = 0 , 1 NEW_LINE complexRoot ( A , B ) NEW_LINE DEDENT