codeparrot/xlcost-text-to-code huggingface dataset:
Generate a Binary String without any consecutive 0 ' s ▁ and ▁ at ▁ most ▁ K ▁ consecutive ▁ 1' s | Function to construct the binary string ; Conditions when string construction is not possible ; Stores maximum 1 's that  can be placed in between ; Place 0 's ; Place 1 's in between ; Count remaining M 's ; Place 1 's at the end ; Place 1 's at the beginning ; Return the final string ; Driver Code
def ConstructBinaryString ( N , M , K ) : NEW_LINE INDENT if ( M < ( N - 1 ) or M > K * ( N + 1 ) ) : NEW_LINE INDENT return ' - 1' NEW_LINE DEDENT ans = " " NEW_LINE l = min ( K , M // ( N - 1 ) ) NEW_LINE temp = N NEW_LINE while ( temp ) : NEW_LINE INDENT temp -= 1 NEW_LINE ans += '0' NEW_LINE if ( temp == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT for i in range ( l ) : NEW_LINE INDENT ans += '1' NEW_LINE DEDENT DEDENT M -= ( N - 1 ) * l NEW_LINE if ( M == 0 ) : NEW_LINE INDENT return ans NEW_LINE DEDENT l = min ( M , K ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT ans += '1' NEW_LINE DEDENT M -= l NEW_LINE while ( M > 0 ) : NEW_LINE INDENT ans = '1' + ans NEW_LINE M -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 9 NEW_LINE K = 2 NEW_LINE print ( ConstructBinaryString ( N , M , K ) ) NEW_LINE DEDENT
Find smallest number formed by inverting digits of given number N | Function to invert the digits of integer N to form minimum possible number ; Initialize the array ; Iterate till the number N exists ; Last digit of the number N ; Checking if the digit is smaller than 9 - digit ; Store the smaller digit in the array ; Reduce the number each time ; Check if the digit starts with 0 or not ; Print the answer ; Driver Code ; Given Number ; Function Call
def number ( num ) : NEW_LINE INDENT a = [ 0 ] * 20 NEW_LINE r , i , j = 0 , 0 , 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT r = num % 10 NEW_LINE if ( 9 - r > r ) : NEW_LINE INDENT a [ i ] = r NEW_LINE DEDENT else : NEW_LINE INDENT a [ i ] = 9 - r NEW_LINE DEDENT i += 1 NEW_LINE num = num // 10 NEW_LINE DEDENT if ( a [ i - 1 ] == 0 ) : NEW_LINE INDENT print ( 9 , end = " " ) NEW_LINE i -= 1 NEW_LINE DEDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( a [ j ] , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = 4545 NEW_LINE number ( num ) NEW_LINE DEDENT
Check if the concatenation of first N natural numbers is divisible by 3 | Function that returns True if concatenation of first N natural numbers is divisible by 3 ; Check using the formula ; Driver Code ; Given Number ; Function Call
def isDivisible ( N ) : NEW_LINE INDENT return ( N - 1 ) % 3 != 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE if ( isDivisible ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of N digit Numbers whose sum of every K consecutive digits is equal | Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits ; If sum is not equal then break the loop ; Increment the count if it satisfy the given condition ; Given N and K ; Function call
def countDigitSum ( N , K ) : NEW_LINE INDENT l = pow ( 10 , N - 1 ) NEW_LINE r = pow ( 10 , N ) - 1 NEW_LINE count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT num = i NEW_LINE digits = [ 0 ] * N NEW_LINE for j in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT digits [ j ] = num % 10 NEW_LINE num //= 10 NEW_LINE DEDENT sum = 0 NEW_LINE flag = 0 NEW_LINE for j in range ( 0 , K ) : NEW_LINE INDENT sum += digits [ j ] NEW_LINE DEDENT for j in range ( 1 , N - K + 1 ) : NEW_LINE INDENT curr_sum = 0 NEW_LINE for m in range ( j , j + K ) : NEW_LINE INDENT curr_sum += digits [ m ] NEW_LINE DEDENT if ( sum != curr_sum ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = 2 NEW_LINE K = 1 NEW_LINE print ( countDigitSum ( N , K ) ) NEW_LINE
Perfect Square factors of a Number | Function that returns the count of factors that are perfect squares ; Stores the count of number of times a prime number divides N . ; Stores the number of factors that are perfect square ; Count number of 2 's  that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Driver Code
def noOfFactors ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT count = 0 NEW_LINE ans = 1 NEW_LINE while ( N % 2 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE N = N // 2 NEW_LINE DEDENT ans *= ( count // 2 + 1 ) NEW_LINE i = 3 NEW_LINE while i * i <= N : NEW_LINE INDENT count = 0 NEW_LINE while ( N % i == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE N = N // i NEW_LINE DEDENT ans *= ( count // 2 + 1 ) NEW_LINE i += 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE print ( noOfFactors ( N ) ) NEW_LINE DEDENT
Nth angle of a Polygon whose initial angle and per angle increment is given | Function to check if the angle is possible or not ; Angular sum of a N - sided polygon ; Angular sum of N - sided given polygon ; Check if both sum are equal ; Function to find the nth angle ; Calculate nth angle ; Return the nth angle ; Driver Code ; Checks the possibility of the polygon exist or not ; Print nth angle of the polygon
def possible ( N , a , b , n ) : NEW_LINE INDENT sum_of_angle = 180 * ( N - 2 ) NEW_LINE Total_angle = ( N * ( ( 2 * a ) + ( N - 1 ) * b ) ) / 2 NEW_LINE if ( sum_of_angle != Total_angle ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def nth_angle ( N , a , b , n ) : NEW_LINE INDENT nth = 0 NEW_LINE nth = a + ( n - 1 ) * b NEW_LINE return nth NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE a = 30 NEW_LINE b = 30 NEW_LINE n = 3 NEW_LINE if ( possible ( N , a , b , n ) ) : NEW_LINE INDENT print ( nth_angle ( N , a , b , n ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT
Check if the given string is linear or not | Function to check if the given is linear or not ; Iterate over string ; If character is not same as the first character then return false ; Increment the tmp ; Driver Code ; Given String str ; Function call
def is_linear ( s ) : NEW_LINE INDENT tmp = 0 NEW_LINE first = s [ 0 ] NEW_LINE pos = 0 NEW_LINE while pos < len ( s ) : NEW_LINE INDENT if ( s [ pos ] != first ) : NEW_LINE INDENT return False NEW_LINE DEDENT tmp += 1 NEW_LINE pos += tmp NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aapaxyayziabcde " NEW_LINE if ( is_linear ( str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimize cost to color all the vertices of an Undirected Graph | Function to implement DFS Traversal to marks all the vertices visited from vertex U ; Mark U as visited ; Traverse the adjacency list of U ; Function to find the minimum cost to color all the vertices of graph ; To store adjacency list ; Loop through the edges to create adjacency list ; To check if a vertex of the graph is visited ; Mark visited to all the vertices that can be reached by colored vertices ; Perform DFS ; To store count of uncolored sub - graphs ; Loop through vertex to count uncolored sub - graphs ; If vertex not visited ; Increase count of uncolored sub - graphs ; Perform DFS to mark visited to all vertices of current sub - graphs ; Calculate minimum cost to color all vertices ; Print the result ; Driver Code ; Given number of vertices and edges ; Given edges ; Given cost of coloring and adding an edge ; Given array of colored vertices
def DFS ( U , vis , adj ) : NEW_LINE INDENT vis [ U ] = 1 NEW_LINE for V in adj [ U ] : NEW_LINE INDENT if ( vis [ V ] == 0 ) : NEW_LINE INDENT DFS ( V , vis , adj ) NEW_LINE DEDENT DEDENT DEDENT def minCost ( N , M , vCost , eCost , sorc , colored , destination ) : NEW_LINE INDENT adj = [ [ ] for i in range ( N + 1 ) ] NEW_LINE for i in range ( M ) : NEW_LINE INDENT adj [ sorc [ i ] ] . append ( destination [ i ] ) NEW_LINE adj [ destination [ i ] ] . append ( sorc [ i ] ) NEW_LINE DEDENT vis = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( len ( colored ) ) : NEW_LINE INDENT DFS ( colored [ i ] , vis , adj ) NEW_LINE DEDENT X = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( vis [ i ] == 0 ) : NEW_LINE INDENT X += 1 NEW_LINE DFS ( i , vis , adj ) NEW_LINE DEDENT DEDENT mincost = X * min ( vCost , eCost ) NEW_LINE print ( mincost ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE M = 1 NEW_LINE sorc = [ 1 ] NEW_LINE destination = [ 2 ] NEW_LINE vCost = 3 NEW_LINE eCost = 2 NEW_LINE colored = [ 1 ] NEW_LINE minCost ( N , M , vCost , eCost , sorc , colored , destination ) NEW_LINE DEDENT
Minimum cost to reduce the integer N to 1 as per given conditions | Function to find the minimum cost to reduce the integer N to 1 by the given operations ; Check if x is 1 ; Print the answer ; Prestore the answer ; Iterate till n exists ; Divide N by x ; Reduce n by x ; Add the cost ; Update the answer ; Return the answer ; Driver Code ; Initialize the variables ; Function call
def min_cost ( n , x , p , q ) : NEW_LINE INDENT if ( x == 1 ) : NEW_LINE INDENT print ( ( n - 1 ) * p ) NEW_LINE return 0 NEW_LINE DEDENT ans = ( n - 1 ) * p NEW_LINE pre = 0 NEW_LINE while ( n > 1 ) : NEW_LINE INDENT tmp = n // x NEW_LINE if ( tmp < 0 ) : NEW_LINE break NEW_LINE pre += ( n - tmp * x ) * p NEW_LINE n //= x NEW_LINE pre += q NEW_LINE ans = min ( ans , pre + ( n - 1 ) * p ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; x = 2 ; NEW_LINE p = 2 ; q = 3 ; NEW_LINE print ( min_cost ( n , x , p , q ) ) NEW_LINE DEDENT
Number of ways to sum up a total of N from limited denominations | Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the final result ; As one of the denominations is rupee 1 , so we can reduce the computation by checking the equality for N - ( A * 1 ) = N - A ; Increment the count for number of ways ; Driver Code ; Given Denominations ; Function Call
def calculateWays ( arr1 , arr2 , N ) : NEW_LINE INDENT A = arr2 [ 0 ] NEW_LINE B = arr2 [ 1 ] NEW_LINE C = arr2 [ 2 ] NEW_LINE D = arr2 [ 3 ] NEW_LINE ans , b , c , d = 0 , 0 , 0 , 0 NEW_LINE while b <= B and b * 5 <= ( N ) : NEW_LINE INDENT c = 0 NEW_LINE while ( c <= C and b * 5 + c * 10 <= ( N ) ) : NEW_LINE INDENT d = 0 NEW_LINE while ( d <= D and b * 5 + c * 10 + d * 20 <= ( N ) ) : NEW_LINE INDENT if ( ( b * 5 ) + ( c * 10 ) + ( d * 20 ) >= ( N - A ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT d += 1 NEW_LINE DEDENT c += 1 NEW_LINE DEDENT b += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 123 NEW_LINE arr1 = [ 1 , 5 , 10 , 20 ] NEW_LINE arr2 = [ 6 , 4 , 3 , 5 ] NEW_LINE print ( calculateWays ( arr1 , arr2 , N ) ) NEW_LINE DEDENT
Number of ways to sum up a total of N from limited denominations | Python3 program for the above approach ; Function to find the number of ways to sum up a total of N from limited denominations ; Store the count of denominations ; Stores the final result ; This will give the number of coins for all combinations of coins with value 1 and 5 ; L2 will sum the values of those indices of ways which is equal to ( N - ( c * 10 + d * 20 ) ) ; Return the final count ; Driver Code ; Given denominations ; Function call
ways = [ 0 for i in range ( 1010 ) ] ; NEW_LINE def calculateWays ( arr1 , arr2 , N ) : NEW_LINE INDENT A = arr2 [ 0 ] ; B = arr2 [ 1 ] ; NEW_LINE C = arr2 [ 2 ] ; D = arr2 [ 3 ] ; NEW_LINE ans = 0 ; NEW_LINE for b in range ( 0 , B + 1 ) : NEW_LINE INDENT if ( b * 5 > N ) : NEW_LINE INDENT break ; NEW_LINE DEDENT for a in range ( 0 , A + 1 ) : NEW_LINE INDENT if ( a + b * 5 > N ) : NEW_LINE INDENT break ; NEW_LINE DEDENT ways [ a + b * 5 ] += 5 ; NEW_LINE DEDENT DEDENT for c in range ( 0 , C ) : NEW_LINE INDENT if ( c * 10 > N ) : NEW_LINE INDENT break ; NEW_LINE DEDENT for d in range ( 0 , D ) : NEW_LINE INDENT if ( c * 10 + d * 20 > N ) : NEW_LINE INDENT break ; NEW_LINE DEDENT ans += ways [ N - c * 10 - d * 20 ] ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 123 ; NEW_LINE arr1 = [ 1 , 5 , 10 , 20 ] ; NEW_LINE arr2 = [ 6 , 4 , 3 , 5 ] ; NEW_LINE print ( calculateWays ( arr1 , arr2 , N ) ) ; NEW_LINE DEDENT
Count of Missing Numbers in a sorted array | Function that find the count of missing numbers in array a [ ] ; Calculate the count of missing numbers in the array ; Driver Code
def countMissingNum ( a , N ) : NEW_LINE INDENT count = a [ N - 1 ] - a [ 0 ] + 1 - N NEW_LINE print ( count ) NEW_LINE DEDENT arr = [ 5 , 10 , 20 , 40 ] NEW_LINE N = len ( arr ) NEW_LINE countMissingNum ( arr , N ) NEW_LINE
Length of longest subsequence whose XOR value is odd | Function for find max XOR subsequence having odd value ; Initialize odd and even count ; Count the number of odd and even numbers in given array ; If all values are odd in given array ; If all values are even in given array ; If both odd and even are present in given array ; Driver code ; Given array arr [ ] ; Function Call
def maxXorSubsequence ( arr , n ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] % 2 != 0 : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT if odd == n : NEW_LINE INDENT if odd % 2 == 0 : NEW_LINE INDENT maxlen = n - 1 NEW_LINE DEDENT else : NEW_LINE INDENT maxlen = n NEW_LINE DEDENT DEDENT elif even == n : NEW_LINE INDENT maxlen = 0 NEW_LINE DEDENT else : NEW_LINE INDENT if odd % 2 == 0 : NEW_LINE INDENT maxlen = even + odd - 1 NEW_LINE DEDENT else : NEW_LINE INDENT maxlen = even + odd NEW_LINE DEDENT DEDENT return maxlen NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxXorSubsequence ( arr , n ) ) NEW_LINE DEDENT
Count of pairs with sum N from first N natural numbers | Function to calculate the value of count ; If n is even ; Count of pairs ; Otherwise ; Driver code
def numberOfPairs ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return n // 2 - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return n // 2 ; NEW_LINE DEDENT DEDENT n = 8 ; NEW_LINE print ( numberOfPairs ( n ) ) ; NEW_LINE
Count pair of strings whose concatenation has every vowel | Function to return the count of all concatenated string with each vowel at least once ; Concatenating all possible pairs of string ; Creating an array which checks , the presence of each vowel ; Checking for each vowel by traversing the concatenated string ; Checking if all the elements are set in vowel [ ] ; Check if all vowels are present or not ; Return the final count ; Driver Code ; Given array of strings ; Function call
def good_pair ( st , N ) : NEW_LINE INDENT countStr = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT res = st [ i ] + st [ j ] NEW_LINE vowel = [ 0 ] * 5 NEW_LINE for k in range ( len ( res ) ) : NEW_LINE INDENT if ( res [ k ] == ' a ' ) : NEW_LINE INDENT vowel [ 0 ] = 1 NEW_LINE DEDENT elif ( res [ k ] == ' e ' ) : NEW_LINE INDENT vowel [ 1 ] = 1 NEW_LINE DEDENT elif ( res [ k ] == ' i ' ) : NEW_LINE INDENT vowel [ 2 ] = 1 NEW_LINE DEDENT elif ( res [ k ] == ' o ' ) : NEW_LINE INDENT vowel [ 3 ] = 1 NEW_LINE DEDENT elif ( res [ k ] == ' u ' ) : NEW_LINE INDENT vowel [ 4 ] = 1 NEW_LINE DEDENT DEDENT temp = 0 NEW_LINE for ind in range ( 5 ) : NEW_LINE INDENT if ( vowel [ ind ] == 1 ) : NEW_LINE INDENT temp += 1 NEW_LINE DEDENT DEDENT if ( temp == 5 ) : NEW_LINE INDENT countStr += 1 NEW_LINE DEDENT DEDENT DEDENT return countStr NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " aaweiolkju " , " oxdfgujkmi " ] NEW_LINE N = len ( arr ) NEW_LINE print ( good_pair ( arr , N ) ) NEW_LINE DEDENT
Find sum of exponents of prime factors of numbers 1 to N | Function to implement sieve of erastosthenes ; Create a boolean array and initialize all entries as false ; Initializing smallest factor equal to 2 for all the even numbers ; Iterate for odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " ; Function to generate prime factors and its power ; s [ i ] is going to store smallest prime factor of i ; Current prime factor of N ; Power of current prime factor ; Calculating prime factors and their powers sum ; Increment the count and continue the process ; Add count to the sum ; Reinitialize count ; Return the result ; Function to find the sum of all the power of prime factors of N ; Iterate for in [ 2 , N ] ; Driver Code ; Given number N ; Function call
def sieveOfEratosthenes ( N , s ) : NEW_LINE INDENT prime = [ False ] * ( N + 1 ) NEW_LINE for i in range ( 2 , N + 1 , 2 ) : NEW_LINE INDENT s [ i ] = 2 NEW_LINE DEDENT for i in range ( 3 , N + 1 , 2 ) : NEW_LINE INDENT if ( prime [ i ] == False ) : NEW_LINE INDENT s [ i ] = i NEW_LINE j = i NEW_LINE while ( j * i <= N ) : NEW_LINE INDENT if ( prime [ i * j ] == False ) : NEW_LINE INDENT prime [ i * j ] = True NEW_LINE s [ i * j ] = i NEW_LINE DEDENT j += 2 NEW_LINE DEDENT DEDENT DEDENT DEDENT def generatePrimeFactors ( N ) : NEW_LINE INDENT s = [ 0 ] * ( N + 1 ) NEW_LINE sum = 0 NEW_LINE sieveOfEratosthenes ( N , s ) NEW_LINE curr = s [ N ] NEW_LINE cnt = 1 NEW_LINE while ( N > 1 ) : NEW_LINE INDENT N //= s [ N ] NEW_LINE if ( curr == s [ N ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE continue NEW_LINE DEDENT sum = sum + cnt NEW_LINE curr = s [ N ] NEW_LINE cnt = 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT def findSum ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT sum += generatePrimeFactors ( i ) NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE findSum ( N ) NEW_LINE DEDENT
Minimum LCM of all subarrays of length at least 2 | Python3 program for the above approach ; Function to find LCM pf two numbers ; Initialise lcm value ; Check for divisibility of a and b by the lcm ; Function to find the Minimum LCM of all subarrays of length greater than 1 ; Store the minimum LCM ; Traverse the array ; Find LCM of consecutive element ; Check if the calculated LCM is less than the minLCM then update it ; Print the minimum LCM ; Given array arr [ ] ; Size of the array ; Function call
import sys NEW_LINE def LCM ( a , b ) : NEW_LINE INDENT lcm = a if a > b else b NEW_LINE while ( True ) : NEW_LINE INDENT if ( lcm % a == 0 and lcm % b == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT lcm += 1 NEW_LINE DEDENT DEDENT return lcm NEW_LINE DEDENT def findMinLCM ( arr , n ) : NEW_LINE INDENT minLCM = sys . maxsize NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT val = LCM ( arr [ i ] , arr [ i + 1 ] ) NEW_LINE if ( val < minLCM ) : NEW_LINE INDENT minLCM = val NEW_LINE DEDENT DEDENT print ( minLCM ) NEW_LINE DEDENT arr = [ 4 , 8 , 12 , 16 , 20 , 24 ] NEW_LINE n = len ( arr ) NEW_LINE findMinLCM ( arr , n ) NEW_LINE
Minimum integer with at most K bits set such that their bitwise AND with N is maximum | Function to find the integer with maximum bitwise with N ; Store answer in the bitset Initialized with 0 ; To maintain the count of set bits that should exceed k ; Start traversing from the Most significant if that bit is set in n then we will set in our answer i . e in X ; Checking if the ith bit is set in n or not ; Converting into integer ; Driver code ; Function Call
def find_max ( n , k ) : NEW_LINE INDENT X = [ 0 ] * 32 NEW_LINE cnt = 0 NEW_LINE i = 31 NEW_LINE while ( i >= 0 and cnt != k ) : NEW_LINE INDENT if ( ( n & ( 1 << i ) ) != 0 ) : NEW_LINE INDENT X [ i ] = 1 NEW_LINE cnt += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT s = " " NEW_LINE for i in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT if X [ i ] == 0 : NEW_LINE INDENT s += '0' NEW_LINE DEDENT else : NEW_LINE INDENT s += '1' NEW_LINE DEDENT DEDENT return int ( s , 2 ) NEW_LINE DEDENT n , k = 10 , 2 NEW_LINE print ( find_max ( n , k ) ) NEW_LINE
Convert string to integer without using any in | Function to convert string to integer without using functions ; Initialize a variable ; Iterate till length of the string ; Subtract 48 from the current digit ; Print the answer ; Driver code ; Given string of number ; Function Call
def convert ( s ) : NEW_LINE INDENT num = 0 NEW_LINE n = len ( s ) NEW_LINE for i in s : NEW_LINE INDENT num = num * 10 + ( ord ( i ) - 48 ) NEW_LINE DEDENT print ( num ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "123" NEW_LINE convert ( s ) NEW_LINE DEDENT
Permutation of Array such that sum of adjacent elements are not divisible by 3 | Python3 implementation to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; Function to segregate numbers based on their remainder when divided by three ; Loop to iterate over the elements of the given array ; Condition to check the remainder of the number ; Function to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; Condition to check when it 's impossible to arrange ; Condition to check when there are no zeros , and only ones or only twos ; Array to store the permutation ; Place the ones on alternate places in the answer array , leaving spaces for zeros remainder elements in the array ; Adding a zero to connect it with a two ; Place the twos on alternate places in the answer array , leaving spaces for zeros ; Fill the zeros finally , between the ones and the twos ; Print the arrangement of the array ; Function to solve the problem ; As there can be only 3 remainders ; Function Call ; Driver Code
hell = 1000000007 NEW_LINE N = 100005 NEW_LINE c_0 = 0 NEW_LINE c_1 = 0 NEW_LINE c_2 = 0 NEW_LINE def count_k ( arr , ones , twos , zeros ) : NEW_LINE INDENT global c_0 , c_1 , c_2 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] % 3 == 0 ) : NEW_LINE INDENT c_0 += 1 NEW_LINE zeros . append ( arr [ i ] ) NEW_LINE DEDENT elif ( arr [ i ] % 3 == 1 ) : NEW_LINE INDENT c_1 += 1 NEW_LINE ones . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT c_2 += 1 NEW_LINE twos . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT def printArrangement ( arr , ones , twos , zeros ) : NEW_LINE INDENT global c_0 , c_1 , c_2 NEW_LINE if ( ( c_0 == 0 and c_1 != 0 and c_2 != 0 ) or c_0 > c_1 + c_2 + 1 ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return NEW_LINE DEDENT if ( c_0 == 0 ) : NEW_LINE INDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT ans = [ - 1 ] * N NEW_LINE i , j = 1 , 0 NEW_LINE while ( j < c_1 ) : NEW_LINE INDENT ans [ i ] = ones [ - 1 ] NEW_LINE ones . pop ( ) NEW_LINE i += 2 NEW_LINE j += 1 NEW_LINE DEDENT ans [ i - 1 ] = zeros [ - 1 ] NEW_LINE zeros . pop ( ) NEW_LINE c_0 -= 1 NEW_LINE j = 0 NEW_LINE while ( j < c_2 ) : NEW_LINE INDENT ans [ i ] = twos [ - 1 ] NEW_LINE twos . pop ( ) NEW_LINE j += 1 NEW_LINE i += 2 NEW_LINE DEDENT k = 0 NEW_LINE while c_0 > 0 : NEW_LINE INDENT if ( ans [ k ] == - 1 ) : NEW_LINE INDENT ans [ k ] = zeros [ - 1 ] NEW_LINE c_0 -= 1 NEW_LINE DEDENT k += 2 NEW_LINE DEDENT for i1 in range ( N ) : NEW_LINE INDENT if ( ans [ i1 ] != - 1 ) : NEW_LINE INDENT print ( ans [ i1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT def solve ( n , arr ) : NEW_LINE INDENT ones = [ ] NEW_LINE zeros = [ ] NEW_LINE twos = [ ] NEW_LINE count_k ( arr , ones , twos , zeros ) NEW_LINE printArrangement ( arr , ones , twos , zeros ) NEW_LINE DEDENT n = 5 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE solve ( n , arr ) NEW_LINE