codeparrot/xlcost-text-to-code huggingface dataset:
Tree Isomorphism Problem | A Binary tree node ; Check if the binary tree is isomorphic or not ; Both roots are None , trees isomorphic by definition ; Exactly one of the n1 and n2 is None , trees are not isomorphic ; There are two possible cases for n1 and n2 to be isomorphic Case 1 : The subtrees rooted at these nodes have NOT been " Flipped " . Both of these subtrees have to be isomorphic , hence the && Case 2 : The subtrees rooted at these nodes have been " Flipped " ; Let us create trees shown in above diagram
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isIsomorphic ( n1 , n2 ) : NEW_LINE INDENT if n1 is None and n2 is None : NEW_LINE INDENT return True NEW_LINE DEDENT if n1 is None or n2 is None : NEW_LINE INDENT return False NEW_LINE DEDENT if n1 . data != n2 . data : NEW_LINE INDENT return False NEW_LINE DEDENT return ( ( isIsomorphic ( n1 . left , n2 . left ) and isIsomorphic ( n1 . right , n2 . right ) ) or ( isIsomorphic ( n1 . left , n2 . right ) and isIsomorphic ( n1 . right , n2 . left ) ) ) NEW_LINE DEDENT n1 = Node ( 1 ) NEW_LINE n1 . left = Node ( 2 ) NEW_LINE n1 . right = Node ( 3 ) NEW_LINE n1 . left . left = Node ( 4 ) NEW_LINE n1 . left . right = Node ( 5 ) NEW_LINE n1 . right . left = Node ( 6 ) NEW_LINE n1 . left . right . left = Node ( 7 ) NEW_LINE n1 . left . right . right = Node ( 8 ) NEW_LINE n2 = Node ( 1 ) NEW_LINE n2 . left = Node ( 3 ) NEW_LINE n2 . right = Node ( 2 ) NEW_LINE n2 . right . left = Node ( 4 ) NEW_LINE n2 . right . right = Node ( 5 ) NEW_LINE n2 . left . right = Node ( 6 ) NEW_LINE n2 . right . right . left = Node ( 8 ) NEW_LINE n2 . right . right . right = Node ( 7 ) NEW_LINE print " Yes " if ( isIsomorphic ( n1 , n2 ) == True ) else " No " NEW_LINE
Find smallest number with given number of digits and sum of digits under given constraints | Function to find the number having sum of digits as s and d number of digits such that the difference between the maximum and the minimum digit the minimum possible ; To store the final number ; To store the value that is evenly distributed among all the digits ; To store the remaining sum that still remains to be distributed among d digits ; rem stores the value that still remains to be distributed To keep the difference of digits minimum last rem digits are incremented by 1 ; In the last rem digits one is added to the value obtained by equal distribution ; Driver function
def findNumber ( s , d ) : NEW_LINE INDENT num = " " NEW_LINE val = s // d NEW_LINE rem = s % d NEW_LINE for i in range ( 1 , d - rem + 1 ) : NEW_LINE INDENT num = num + str ( val ) NEW_LINE DEDENT if ( rem ) : NEW_LINE INDENT val += 1 NEW_LINE for i in range ( d - rem + 1 , d + 1 ) : NEW_LINE INDENT num = num + str ( val ) NEW_LINE DEDENT DEDENT return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = 25 NEW_LINE d = 4 NEW_LINE print ( findNumber ( s , d ) ) NEW_LINE DEDENT
Find the sum of power of bit count raised to the power B | Function to calculate a ^ b mod m using fast - exponentiation method ; Function to calculate sum ; Itereate for all values of array A ; Calling fast - exponentiation and appending ans to sum ; Driver code .
def fastmod ( base , exp , mod ) : NEW_LINE INDENT if ( exp == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( exp % 2 == 0 ) : NEW_LINE INDENT ans = fastmod ( base , exp / 2 , mod ) ; NEW_LINE return ( ans % mod * ans % mod ) % mod ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; NEW_LINE DEDENT DEDENT def findPowerSum ( n , ar ) : NEW_LINE INDENT mod = int ( 1e9 ) + 7 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT base = bin ( ar [ i ] ) . count ( '1' ) ; NEW_LINE exp = ar [ i ] ; NEW_LINE sum += fastmod ( base , exp , mod ) ; NEW_LINE sum %= mod ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE ar = [ 1 , 2 , 3 ] ; NEW_LINE print ( findPowerSum ( n , ar ) ) ; NEW_LINE DEDENT
Count the total number of triangles after Nth operation | Function to return the total no . of Triangles ; For every subtriangle formed there are possibilities of generating ( curr * 3 ) + 2 ; Changing the curr value to Tri_count ;
def countTriangles ( n ) : NEW_LINE INDENT curr = 1 NEW_LINE Tri_count = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT Tri_count = ( curr * 3 ) + 2 NEW_LINE curr = Tri_count NEW_LINE DEDENT return Tri_count NEW_LINE DEDENT / * Driver code * / NEW_LINE n = 10 NEW_LINE print ( countTriangles ( n ) ) NEW_LINE
In how many ways the ball will come back to the first boy after N turns | Function to return the number of sequences that get back to Bob ; Driver code
def numSeq ( n ) : NEW_LINE INDENT return ( pow ( 3 , n ) + 3 * pow ( - 1 , n ) ) // 4 NEW_LINE DEDENT N = 10 NEW_LINE print ( numSeq ( N ) ) NEW_LINE
Count subarrays such that remainder after dividing sum of elements by K gives count of elements | Function to return the number of subarrays of the given array such that the remainder when dividing the sum of its elements by K is equal to the number of its elements ; To store prefix sum ; We are dealing with zero indexed array ; Taking modulus value ; Prefix sum ; To store the required answer , the left index and the right index ; To store si - i value ; Include sum ; Increment the right index ; If subarray has at least k elements ; Return the required answer ; Driver code ; Function call
def sub_arrays ( a , n , k ) : NEW_LINE INDENT sum = [ 0 for i in range ( n + 2 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] -= 1 NEW_LINE a [ i ] %= k NEW_LINE sum [ i + 1 ] += sum [ i ] + a [ i ] NEW_LINE sum [ i + 1 ] %= k NEW_LINE DEDENT ans = 0 NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE mp = dict ( ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if sum [ i ] in mp : NEW_LINE INDENT ans += mp [ sum [ i ] ] NEW_LINE DEDENT mp [ sum [ i ] ] = mp . get ( sum [ i ] , 0 ) + 1 NEW_LINE r += 1 NEW_LINE if ( r - l >= k ) : NEW_LINE INDENT mp [ sum [ l ] ] -= 1 NEW_LINE l += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 1 , 4 , 2 , 3 , 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 4 NEW_LINE print ( sub_arrays ( a , n , k ) ) NEW_LINE
Sum of all the prime numbers with the maximum position of set bit â ‰¤ D | Python 3 implementation of the approach ; Function for Sieve of Eratosthenes ; Function to return the sum of the required prime numbers ; Maximum number of the required range ; Sieve of Eratosthenes ; To store the required sum ; If current element is prime ; Driver code
from math import sqrt , pow NEW_LINE def sieve ( prime , n ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def sumPrime ( d ) : NEW_LINE INDENT maxVal = int ( pow ( 2 , d ) ) - 1 ; NEW_LINE prime = [ True for i in range ( maxVal + 1 ) ] NEW_LINE sieve ( prime , maxVal ) NEW_LINE sum = 0 NEW_LINE for i in range ( 2 , maxVal + 1 , 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT d = 8 NEW_LINE print ( sumPrime ( d ) ) NEW_LINE DEDENT
Check whether the exchange is possible or not | Python3 implementation of the approach ; Function that returns true if the exchange is possible ; Find the GCD of the array elements ; If the exchange is possible ; Driver code
from math import gcd as __gcd NEW_LINE def isPossible ( arr , n , p ) : NEW_LINE INDENT gcd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT gcd = __gcd ( gcd , arr [ i ] ) ; NEW_LINE DEDENT if ( p % gcd == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE p = 3 ; NEW_LINE if ( isPossible ( arr , n , p ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Choose two elements from the given array such that their sum is not present in any of the arrays | Function to find the numbers from the given arrays such that their sum is not present in any of the given array ; Find the maximum element from both the arrays ; Driver code
def findNum ( a , n , b , m ) : NEW_LINE INDENT x = max ( a ) ; NEW_LINE y = max ( b ) ; NEW_LINE print ( x , y ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 3 , 2 , 2 ] ; NEW_LINE n = len ( a ) ; NEW_LINE b = [ 1 , 5 , 7 , 7 , 9 ] ; NEW_LINE m = len ( b ) ; NEW_LINE findNum ( a , n , b , m ) ; NEW_LINE DEDENT
Generate an array B [ ] from the given array A [ ] which satisfies the given conditions | Python3 implementation of the approach ; Utility function to print the array elements ; Function to generate and print the required array ; To switch the ceil and floor function alternatively ; For every element of the array ; If the number is odd then print the ceil or floor value after division by 2 ; Use the ceil and floor alternatively ; If arr [ i ] is even then it will be completely divisible by 2 ; Driver code
from math import ceil , floor NEW_LINE def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def generateArr ( arr , n ) : NEW_LINE INDENT flip = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT flip ^= True NEW_LINE if ( flip ) : NEW_LINE INDENT print ( int ( ceil ( ( arr [ i ] ) / 2 ) ) , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( int ( floor ( ( arr [ i ] ) / 2 ) ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( int ( arr [ i ] / 2 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 3 , - 5 , - 7 , 9 , 2 , - 2 ] NEW_LINE n = len ( arr ) NEW_LINE generateArr ( arr , n ) NEW_LINE
Number of K length subsequences with minimum sum | Function to return the value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the count of valid subsequences ; Sort the array ; Maximum among the minimum K elements ; Y will store the frequency of num in the minimum K elements ; cntX will store the frequency of num in the complete array ; Driver code
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for i in range ( n + 1 ) ] for j in range ( k + 1 ) ] NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT for j in range ( 0 , min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] NEW_LINE DEDENT def cntSubSeq ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE num = arr [ k - 1 ] ; NEW_LINE Y = 0 ; NEW_LINE for i in range ( k - 1 , - 1 , 1 ) : NEW_LINE INDENT if ( arr [ i ] == num ) : NEW_LINE INDENT Y += 1 NEW_LINE DEDENT DEDENT cntX = Y ; NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT if ( arr [ i ] == num ) : NEW_LINE INDENT cntX += 1 NEW_LINE DEDENT DEDENT return binomialCoeff ( cntX , Y ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( cntSubSeq ( arr , n , k ) ) NEW_LINE
Find all even length binary sequences with same sum of first and second half bits | Iterative | Function to convert the number into binary and store the number into an array ; Function to check if the sum of the digits till the mid of the array and the sum of the digits from mid till n is the same , if they are same then print that binary ; Calculating the sum from 0 till mid and store in sum1 ; Calculating the sum from mid till n and store in sum2 ; If sum1 is same as sum2 print the binary ; Function to prsequence ; Creating the array ; '' Looping over powers of 2  ; Converting the number into binary first ; Checking if the sum of the first half of the array is same as the sum of the next half ; Driver Code
def convertToBinary ( num , a , n ) : NEW_LINE INDENT pointer = n - 1 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT a [ pointer ] = num % 2 NEW_LINE num = num // 2 NEW_LINE pointer -= 1 NEW_LINE DEDENT DEDENT def checkforsum ( a , n ) : NEW_LINE INDENT sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE mid = n // 2 NEW_LINE for i in range ( mid ) : NEW_LINE INDENT sum1 = sum1 + a [ i ] NEW_LINE DEDENT for j in range ( mid , n ) : NEW_LINE INDENT sum2 = sum2 + a [ j ] NEW_LINE DEDENT if ( sum1 == sum2 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def print_seq ( m ) : NEW_LINE INDENT n = ( 2 * m ) NEW_LINE a = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( pow ( 2 , n ) ) : NEW_LINE INDENT convertToBinary ( i , a , n ) NEW_LINE checkforsum ( a , n ) NEW_LINE DEDENT DEDENT m = 2 NEW_LINE print_seq ( m ) NEW_LINE
Find out the prime numbers in the form of A + nB or B + nA | Python3 implementation of the above approach ; Utility function to check whether two numbers is co - prime or not ; Utility function to check whether a number is prime or not ; Corner case ; Check from 2 to sqrt ( n ) ; finding the Prime numbers ; Checking whether given numbers are co - prime or not ; To store the N primes ; If ' possible ' is true ; Printing n numbers of prime ; checking the form of a + nb ; Checking the form of b + na ; If ' possible ' is false return - 1 ; Driver Code
from math import gcd , sqrt NEW_LINE def coprime ( a , b ) : NEW_LINE INDENT if ( gcd ( a , b ) == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( n == 2 or n == 3 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def findNumbers ( a , b , n ) : NEW_LINE INDENT possible = True ; NEW_LINE if ( not coprime ( a , b ) ) : NEW_LINE INDENT possible = False ; NEW_LINE DEDENT c1 = 1 ; NEW_LINE c2 = 1 ; NEW_LINE num1 = 0 ; NEW_LINE num2 = 0 ; NEW_LINE st = set ( ) ; NEW_LINE if ( possible ) : NEW_LINE INDENT while ( len ( st ) != n ) : NEW_LINE INDENT num1 = a + ( c1 * b ) ; NEW_LINE if ( isPrime ( num1 ) ) : NEW_LINE INDENT st . add ( num1 ) ; NEW_LINE DEDENT c1 += 1 ; NEW_LINE num2 = b + ( c2 * a ) ; NEW_LINE if ( isPrime ( num2 ) ) : NEW_LINE INDENT st . add ( num2 ) ; NEW_LINE DEDENT c2 += 1 ; NEW_LINE DEDENT for i in st : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 ; NEW_LINE b = 5 ; NEW_LINE n = 4 ; NEW_LINE findNumbers ( a , b , n ) ; NEW_LINE DEDENT
Find the integers that doesnot ends with T1 or T2 when squared and added X | Function to print the elements Not ending with T1 or T2 ; Flag to check if none of the elements Do not end with t1 or t2 ; Traverse through all the elements ; Temporary variable to store the value ; If the last digit is neither t1 nor t2 then ; Print the number ; Set the flag as False ; If none of the elements meets the specification ; Test case 1 ; Call the function ; Test case 2 ; Call the function
def findIntegers ( n , a , x , t1 , t2 ) : NEW_LINE INDENT flag = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp = pow ( a [ i ] , 2 ) + x NEW_LINE if ( temp % 10 != t1 and temp % 10 != t2 ) : NEW_LINE INDENT print ( temp , end = " ▁ " ) NEW_LINE flag = False NEW_LINE DEDENT DEDENT if flag : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT N , X , T1 , T2 = 4 , 10 , 5 , 6 NEW_LINE a = [ 3 , 1 , 4 , 7 ] NEW_LINE findIntegers ( N , a , X , T1 , T2 ) ; NEW_LINE print ( ) NEW_LINE N , X , T1 , T2 = 4 , 2 , 5 , 6 NEW_LINE b = [ 2 , 18 , 22 , 8 ] NEW_LINE findIntegers ( N , b , X , T1 , T2 ) NEW_LINE
First N terms whose sum of digits is a multiple of 10 | Python3 code for above implementation ; Function to return the sum of digits of n ; Add last digit to the sum ; Remove last digit ; Function to return the nth term of the required series ; If sum of digit is already a multiple of 10 then append 0 ; To store the minimum digit that must be appended ; Return n after appending the required digit ; Function to print the first n terms of the required series ; Driver code
TEN = 10 NEW_LINE def digitSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += n % TEN NEW_LINE n //= TEN NEW_LINE DEDENT return sum NEW_LINE DEDENT def getNthTerm ( n ) : NEW_LINE INDENT sum = digitSum ( n ) NEW_LINE if ( sum % TEN == 0 ) : NEW_LINE INDENT return ( n * TEN ) NEW_LINE DEDENT extra = TEN - ( sum % TEN ) NEW_LINE return ( ( n * TEN ) + extra ) NEW_LINE DEDENT def firstNTerms ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( getNthTerm ( i ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE firstNTerms ( n ) NEW_LINE
Nearest greater number by interchanging the digits | Python3 program to find nearest greater value ; Find all the possible permutation of Value A . ; Convert into Integer ; Find the minimum value of A by interchanging the digits of A and store min1 . ; Driver code ; Convert integer value into to find all the permutation of the number ; count = 1 means number greater than B exists
min1 = 10 ** 9 NEW_LINE _count = 0 NEW_LINE def permutation ( str1 , i , n , p ) : NEW_LINE INDENT global min1 , _count NEW_LINE if ( i == n ) : NEW_LINE INDENT str1 = " " . join ( str1 ) NEW_LINE q = int ( str1 ) NEW_LINE if ( q - p > 0 and q < min1 ) : NEW_LINE INDENT min1 = q NEW_LINE _count = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for j in range ( i , n + 1 ) : NEW_LINE INDENT str1 [ i ] , str1 [ j ] = str1 [ j ] , str1 [ i ] NEW_LINE permutation ( str1 , i + 1 , n , p ) NEW_LINE str1 [ i ] , str1 [ j ] = str1 [ j ] , str1 [ i ] NEW_LINE DEDENT DEDENT return min1 NEW_LINE DEDENT A = 213 NEW_LINE B = 111 NEW_LINE str2 = str ( A ) NEW_LINE str1 = [ i for i in str2 ] NEW_LINE le = len ( str1 ) NEW_LINE h = permutation ( str1 , 0 , le - 1 , B ) NEW_LINE if _count == 1 : NEW_LINE INDENT print ( h ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT
Alcuin 's Sequence | Python3 program for AlcuinaTMs Sequence ; find the nth term of Alcuin 's sequence ; return the ans ; print first n terms of Alcuin number ; display the number ; Driver code
from math import ceil , floor NEW_LINE def Alcuin ( n ) : NEW_LINE INDENT _n = n NEW_LINE ans = 0 NEW_LINE ans = ( round ( ( _n * _n ) / 12 ) - floor ( _n / 4 ) * floor ( ( _n + 2 ) / 4 ) ) NEW_LINE return ans NEW_LINE DEDENT def solve ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( Alcuin ( i ) , end = " , ▁ " ) NEW_LINE DEDENT DEDENT n = 15 NEW_LINE solve ( n ) NEW_LINE
Find the final sequence of the array after performing given operations | Python3 implementation of the approach ; Driver Code
def solve ( arr , n ) : NEW_LINE INDENT b = [ 0 for i in range ( n ) ] NEW_LINE p = 0 NEW_LINE i = n - 1 NEW_LINE while i >= 0 : NEW_LINE INDENT b [ p ] = arr [ i ] NEW_LINE i -= 1 NEW_LINE if ( i >= 0 ) : NEW_LINE INDENT b [ n - 1 - p ] = arr [ i ] NEW_LINE DEDENT p += 1 NEW_LINE i -= 1 NEW_LINE DEDENT return b NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE b = solve ( arr , n ) NEW_LINE print ( b ) NEW_LINE
Check if the product of every contiguous subsequence is different or not in a number | Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Driver code
def productsDistinct ( N ) : NEW_LINE INDENT s = " " NEW_LINE while ( N ) : NEW_LINE INDENT s += chr ( N % 10 + ord ( '0' ) ) NEW_LINE N //= 10 NEW_LINE DEDENT s = s [ : : - 1 ] NEW_LINE sz = len ( s ) NEW_LINE se = [ ] NEW_LINE for i in range ( sz ) : NEW_LINE INDENT product = 1 NEW_LINE for j in range ( i , sz , 1 ) : NEW_LINE INDENT product *= ord ( s [ j ] ) - ord ( '0' ) NEW_LINE for p in range ( len ( se ) ) : NEW_LINE INDENT if se [ p ] == product : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT se . append ( product ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2345 NEW_LINE if ( productsDistinct ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Number of ways to arrange 2 * N persons on the two sides of a table with X and Y persons on opposite sides | Function to find factorial of a number ; Function to find nCr ; Function to find the number of ways to arrange 2 * N persons ; Driver code ; Function call
def factorial ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return n * factorial ( n - 1 ) ; NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return ( factorial ( n ) / ( factorial ( n - r ) * factorial ( r ) ) ) ; NEW_LINE DEDENT def NumberOfWays ( n , x , y ) : NEW_LINE INDENT return ( nCr ( 2 * n - x - y , n - x ) * factorial ( n ) * factorial ( n ) ) ; NEW_LINE DEDENT n , x , y = 5 , 4 , 2 ; NEW_LINE print ( int ( NumberOfWays ( n , x , y ) ) ) ; NEW_LINE
Find smallest positive number Y such that Bitwise AND of X and Y is Zero | Method to find smallest number Y for a given value of X such that X AND Y is zero ; Convert the number into its binary form ; Case 1 : If all bits are ones , then return the next number ; Case 2 : find the first 0 - bit index and return the Y ; Driver Code
def findSmallestNonZeroY ( A_num ) : NEW_LINE INDENT A_binary = bin ( A_num ) NEW_LINE B = 1 NEW_LINE length = len ( A_binary ) ; NEW_LINE no_ones = ( A_binary ) . count ( '1' ) ; NEW_LINE if length == no_ones : NEW_LINE INDENT return A_num + 1 ; NEW_LINE DEDENT for i in range ( length ) : NEW_LINE INDENT ch = A_binary [ length - i - 1 ] ; NEW_LINE if ( ch == '0' ) : NEW_LINE INDENT B = pow ( 2.0 , i ) ; NEW_LINE break ; NEW_LINE DEDENT DEDENT return B ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = findSmallestNonZeroY ( 10 ) ; NEW_LINE print ( X ) NEW_LINE DEDENT
Count number of set bits in a range using bitset | Python3 implementation of above approach ; function for converting binary string into integer value ; function to count number of 1 's using bitset  ; Converting the string into bitset ; Bitwise operations Left shift ; Right shifts ; return count of one in [ L , R ] ; Driver code
N = 32 NEW_LINE def binStrToInt ( binary_str ) : NEW_LINE INDENT length = len ( binary_str ) NEW_LINE num = 0 NEW_LINE for i in range ( length ) : NEW_LINE INDENT num = num + int ( binary_str [ i ] ) NEW_LINE num = num * 2 NEW_LINE DEDENT return num / 2 NEW_LINE DEDENT def GetOne ( s , L , R ) : NEW_LINE INDENT length = len ( s ) ; NEW_LINE bit = s . zfill ( 32 - len ( s ) ) ; NEW_LINE bit = int ( binStrToInt ( bit ) ) NEW_LINE bit <<= ( N - length + L - 1 ) ; NEW_LINE bit >>= ( N - length + L - 1 ) ; NEW_LINE bit >>= ( length - R ) ; NEW_LINE return bin ( bit ) . count ( '1' ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "01010001011" ; NEW_LINE L = 2 ; R = 4 ; NEW_LINE print ( GetOne ( s , L , R ) ) ; NEW_LINE DEDENT