codeparrot/xlcost-text-to-code huggingface dataset:
Check whether a number is Non | Python3 program to check if a given number is Non - Hypotenuse number or not . ; Function to find prime factor and check if it is of the form 4 k + 1 or not ; 2 is a prime number but not of the form 4 k + 1 so , keep Dividing n by 2 until n is divisible by 2 ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; if i divides n check if i is of the form 4 k + 1 or not ; while i divides n divide n by i and update n ; This condition is to handle the case when n is a prime number greater than 2 ; Test function ; Driver code
from math import sqrt NEW_LINE def isNonHypotenuse ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT n = n // 2 NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( ( i - 1 ) % 4 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT while ( n % i == 0 ) : NEW_LINE INDENT n = n // i NEW_LINE DEDENT DEDENT DEDENT if ( n > 2 and ( n - 1 ) % 4 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT def test ( n ) : NEW_LINE INDENT print ( " Testing ▁ for " , n , " : " , end = " ▁ " ) NEW_LINE if ( isNonHypotenuse ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 11 NEW_LINE test ( n ) NEW_LINE n = 10 NEW_LINE test ( n ) NEW_LINE DEDENT
Find the n | Python3 implementation of the above approach ; Function to return the nth string in the required sequence ; Length of the resultant string ; Relative index ; Initial string of length len consists of all a 's since the list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Driver Code
from math import log2 NEW_LINE def obtain_str ( n ) : NEW_LINE INDENT length = int ( log2 ( n + 1 ) ) NEW_LINE rel_ind = n + 1 - pow ( 2 , length ) NEW_LINE i = 0 NEW_LINE string = " " NEW_LINE for i in range ( length ) : NEW_LINE INDENT string += ' a ' NEW_LINE DEDENT i = 0 NEW_LINE string_list = list ( string ) NEW_LINE while ( rel_ind > 0 ) : NEW_LINE INDENT if ( rel_ind % 2 == 1 ) : NEW_LINE INDENT string_list [ i ] = ' b ' NEW_LINE DEDENT rel_ind //= 2 NEW_LINE i += 1 NEW_LINE DEDENT string_list . reverse ( ) NEW_LINE string = " " . join ( string_list ) NEW_LINE return string NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 11 NEW_LINE print ( obtain_str ( n ) ) NEW_LINE DEDENT
Program to find the Nth term of the series 0 , 3 / 1 , 8 / 3 , 15 / 5. . ... ... | Function to return the nth term of the given series ; nth term ; Driver code
def Nthterm ( n ) : NEW_LINE INDENT numerator = n ** 2 - 1 NEW_LINE denomenator = 2 * n - 3 NEW_LINE print ( numerator , " / " , denomenator ) NEW_LINE DEDENT n = 3 NEW_LINE Nthterm ( n ) NEW_LINE
Sum of each element raised to ( prime | Function to return the required sum ; Driver code
def getSum ( arr , p ) : NEW_LINE INDENT return len ( arr ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 8 ] NEW_LINE p = 7 NEW_LINE print ( getSum ( arr , p ) ) NEW_LINE DEDENT
Count numbers upto N which are both perfect square and perfect cube | Function to return required count ; Driver code ; function call to print required answer
def SquareCube ( N ) : NEW_LINE INDENT cnt , i = 0 , 1 NEW_LINE while ( i ** 6 <= N ) : NEW_LINE INDENT cnt += 1 NEW_LINE i += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT N = 100000 NEW_LINE print ( SquareCube ( N ) ) NEW_LINE
Sum of integers upto N with given unit digit | Function to return the required sum ; Driver code
def getSum ( n , d ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( d <= n ) : NEW_LINE INDENT sum += d NEW_LINE d += 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 30 NEW_LINE d = 3 NEW_LINE print ( getSum ( n , d ) ) NEW_LINE
Summing the sum series | Python3 program to calculate the terms of summing of sum series ; Function to calculate twice of sum of first N natural numbers ; Function to calculate the terms of summing of sum series ;
MOD = 1000000007 NEW_LINE def Sum ( N ) : NEW_LINE INDENT val = N * ( N + 1 ) NEW_LINE val = val % MOD NEW_LINE return val NEW_LINE DEDENT def sumX ( N , M , K ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT N = int ( Sum ( K + N ) ) NEW_LINE DEDENT N = N % MOD NEW_LINE return N NEW_LINE DEDENT / * Driver code * / NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , K = 1 , 2 , 3 NEW_LINE print ( sumX ( N , M , K ) ) NEW_LINE DEDENT
Logarithm | Python 3 program to find log ( n ) using Recursion ; Driver code
def Log2n ( n ) : NEW_LINE INDENT return 1 + Log2n ( n / 2 ) if ( n > 1 ) else 0 NEW_LINE DEDENT n = 32 NEW_LINE print ( Log2n ( n ) ) NEW_LINE
Mode | Function that sort input array a [ ] and calculate mode and median using counting sort . ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Driver Code
def printMode ( a , n ) : NEW_LINE INDENT max_element = max ( a ) NEW_LINE t = max_element + 1 NEW_LINE count = [ 0 ] * t NEW_LINE for i in range ( t ) : NEW_LINE INDENT count [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT count [ a [ i ] ] += 1 NEW_LINE DEDENT mode = 0 NEW_LINE k = count [ 0 ] NEW_LINE for i in range ( 1 , t ) : NEW_LINE INDENT if ( count [ i ] > k ) : NEW_LINE INDENT k = count [ i ] NEW_LINE mode = i NEW_LINE DEDENT DEDENT print ( " mode ▁ = ▁ " , mode ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 ] NEW_LINE n = len ( a ) NEW_LINE printMode ( a , n ) NEW_LINE DEDENT
Harmonic Progression | Python3 program to check if a given array can form harmonic progression ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . , first Sort the reciprocal array , then check difference between consecutive elements ; Driver code ; series to check whether it is in H . P ; Checking a series is in H . P or not
def checkIsHP ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT rec = [ ] NEW_LINE for i in range ( 0 , len ( arr ) ) : NEW_LINE INDENT a = 1 / arr [ i ] NEW_LINE rec . append ( a ) NEW_LINE DEDENT return ( rec ) NEW_LINE rec . sort ( ) NEW_LINE d = rec [ 1 ] - rec [ 0 ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( rec [ i ] - rec [ i - 1 ] != d ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 / 5 , 1 / 10 , 1 / 15 , 1 / 20 , 1 / 25 ] NEW_LINE if ( checkIsHP ( arr ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Arithmetic Mean | Prints N arithmetic means between A and B . ; Calculate common difference ( d ) ; For finding N the arithmetic mean between A and B ; Driver code
def printAMeans ( A , B , N ) : NEW_LINE INDENT d = ( B - A ) / ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( int ( A + i * d ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = 20 ; B = 32 ; N = 5 NEW_LINE printAMeans ( A , B , N ) NEW_LINE
Prime Factor | Python program to print prime factors ; A function to print all prime factors of a given number n ; Print the number of two 's that divide n ; n must be odd at this point so a skip of 2 ( i = i + 2 ) can be used ; while i divides n , print i ad divide n ; Condition if n is a prime number greater than 2 ; Driver Program to test above function
import math NEW_LINE def primeFactors ( n ) : NEW_LINE INDENT while n % 2 == 0 : NEW_LINE INDENT print 2 , NEW_LINE n = n / 2 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT print i , NEW_LINE n = n / i NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT print n NEW_LINE DEDENT DEDENT n = 315 NEW_LINE primeFactors ( n ) NEW_LINE
Time taken by two persons to meet on a circular track | Python 3 implementation of above approach ; Function to return the time when both the persons will meet at the starting point ; Time to cover 1 round by both ; Finding LCM to get the meeting point ; Function to return the time when both the persons will meet for the first time ; Driver Code ; Calling function
from math import gcd NEW_LINE def startingPoint ( Length , Speed1 , Speed2 ) : NEW_LINE INDENT result1 = 0 NEW_LINE result2 = 0 NEW_LINE time1 = Length // Speed1 NEW_LINE time2 = Length // Speed2 NEW_LINE result1 = gcd ( time1 , time2 ) NEW_LINE result2 = time1 * time2 // ( result1 ) NEW_LINE return result2 NEW_LINE DEDENT def firstTime ( Length , Speed1 , Speed2 ) : NEW_LINE INDENT result = 0 NEW_LINE relativeSpeed = abs ( Speed1 - Speed2 ) NEW_LINE result = Length / relativeSpeed NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 30 NEW_LINE S1 = 5 NEW_LINE S2 = 2 NEW_LINE first_Time = firstTime ( L , S1 , S2 ) NEW_LINE starting_Point = startingPoint ( L , S1 , S2 ) NEW_LINE print ( " Met ▁ first ▁ time ▁ after " , first_Time , " hrs " ) NEW_LINE print ( " Met ▁ at ▁ starting ▁ point ▁ after " , starting_Point , " hrs " ) NEW_LINE DEDENT
Check if the array has an element which is equal to product of remaining elements | Function to Check if the array has an element which is equal to product of all the remaining elements ; Calculate the product of all the elements ; Return true if any such element is found ; If no element is found ; Driver code
def CheckArray ( arr , n ) : NEW_LINE INDENT prod = 1 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT prod *= arr [ i ] NEW_LINE DEDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] == prod / arr [ i ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 12 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( CheckArray ( arr , n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Sum of common divisors of two numbers A and B | print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver Code ; print the sum of common factors
def sum ( a , b ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , min ( a , b ) ) : NEW_LINE INDENT if ( a % i == 0 and b % i == 0 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT A = 10 NEW_LINE B = 15 NEW_LINE print ( " Sum ▁ = " , sum ( A , B ) ) NEW_LINE
Minimum number of cuts required to make circle segments equal sized | Python 3 program to find the minimum number of additional cuts required to make circle segments equal sized ; Function to find the minimum number of additional cuts required to make circle segments are equal sized ; Sort the array ; Initial gcd value ; Including the last segment ; Driver code
import math NEW_LINE def minimumCuts ( a , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE gcd = a [ 1 ] - a [ 0 ] NEW_LINE s = gcd NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT gcd = math . gcd ( gcd , a [ i ] - a [ i - 1 ] ) NEW_LINE s += a [ i ] - a [ i - 1 ] NEW_LINE DEDENT if ( 360 - s > 0 ) : NEW_LINE INDENT gcd = math . gcd ( gcd , 360 - s ) NEW_LINE DEDENT return ( 360 // gcd ) - n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 30 , 60 , 180 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimumCuts ( arr , n ) ) NEW_LINE DEDENT
Find a number that divides maximum array elements | Python3 program to find a number that divides maximum array elements ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; Function to find a number that divides maximum array elements ; precalculating Smallest Prime Factor ; Hash to store frequency of each divisors ; Traverse the array and get spf of each element ; calling getFactorization function ; Review set view ; Driver Code
import math as mt NEW_LINE MAXN = 100001 NEW_LINE spf = [ 0 for i in range ( MAXN ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT spf [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAXN ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAXN , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT for i in range ( 3 , mt . ceil ( mt . sqrt ( MAXN + 1 ) ) ) : NEW_LINE INDENT if ( spf [ i ] == i ) : NEW_LINE INDENT for j in range ( 2 * i , MAXN , i ) : NEW_LINE INDENT if ( spf [ j ] == j ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def getFactorization ( x ) : NEW_LINE INDENT ret = list ( ) NEW_LINE while ( x != 1 ) : NEW_LINE INDENT temp = spf [ x ] NEW_LINE ret . append ( temp ) NEW_LINE while ( x % temp == 0 ) : NEW_LINE INDENT x = x // temp NEW_LINE DEDENT DEDENT return ret NEW_LINE DEDENT def maxElement ( A , n ) : NEW_LINE INDENT sieve ( ) NEW_LINE m = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT p = getFactorization ( A [ i ] ) NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT if p [ i ] in m . keys ( ) : NEW_LINE INDENT m [ p [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ p [ i ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT cnt = 0 NEW_LINE ans = 10 ** 9 + 7 NEW_LINE for i in m : NEW_LINE INDENT if ( m [ i ] >= cnt ) : NEW_LINE INDENT cnt = m [ i ] NEW_LINE if ans > i : NEW_LINE INDENT ans = i NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT A = [ 2 , 5 , 10 ] NEW_LINE n = len ( A ) NEW_LINE print ( maxElement ( A , n ) ) NEW_LINE
Find Selling Price from given Profit Percentage and Cost | Function to calculate the Selling Price ; Decimal Equivalent of Profit Percentage ; Find the Selling Price ; return the calculated Selling Price ; Driver code ; Get the CP and Profit % ; Printing the returned value
def SellingPrice ( CP , PP ) : NEW_LINE INDENT Pdecimal = 1 + ( PP / 100 ) NEW_LINE res = Pdecimal * CP NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT C = 720 NEW_LINE P = 13 NEW_LINE print ( SellingPrice ( C , P ) ) NEW_LINE DEDENT
Product of all the Composite Numbers in an array | Python3 program to find product of all the composite numberes in given array ; function to find the product of all composite niumbers in the given array ; find the maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers   ; if prime [ p ] is not changed , than it is prime ; update all multiples of p ; find the product of all composite numbers in the arr [ ] ; Driver code
import math as mt NEW_LINE def compositeProduct ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = True NEW_LINE prime [ 1 ] = True NEW_LINE for p in range ( 2 , mt . ceil ( mt . sqrt ( max_val ) ) ) : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT product = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if prime [ arr [ i ] ] == False : NEW_LINE INDENT product *= arr [ i ] NEW_LINE DEDENT DEDENT return product NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( compositeProduct ( arr , n ) ) NEW_LINE
Primality test for the sum of digits at odd places of a number | Function that return sum of the digits at odd places ; Function to check if a number is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; driver code ; Get the sum of the digits at odd places
def sum_odd ( n ) : NEW_LINE INDENT sums = 0 NEW_LINE pos = 1 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( pos % 2 == 1 ) : NEW_LINE INDENT sums += n % 10 NEW_LINE DEDENT n = n // 10 NEW_LINE pos += 1 NEW_LINE DEDENT return sums NEW_LINE DEDENT def check_prime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , n , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT n = 223 NEW_LINE sums = sum_odd ( n ) NEW_LINE if ( check_prime ( sums ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Find amount to be added to achieve target ratio in a given mixture | Python3 program to find amount of water to be added to achieve given target ratio . ; Driver code
def findAmount ( X , W , Y ) : NEW_LINE INDENT return ( X * ( Y - W ) / ( 100 - Y ) ) NEW_LINE DEDENT X = 100 NEW_LINE W = 50 ; Y = 60 NEW_LINE print ( " Water ▁ to ▁ be ▁ added " , findAmount ( X , W , Y ) ) NEW_LINE
Check if a number is a Mystery Number | Finds reverse of given num x . ; if found print the pair , return
def reverseNum ( x ) : NEW_LINE INDENT s = str ( x ) NEW_LINE s = s [ : : - 1 ] NEW_LINE return int ( s ) NEW_LINE DEDENT def isMysteryNumber ( n ) : NEW_LINE INDENT for i in range ( 1 , n // 2 + 1 ) : NEW_LINE INDENT j = reverseNum ( i ) NEW_LINE if i + j == n : NEW_LINE INDENT print ( i , j ) NEW_LINE return True NEW_LINE DEDENT DEDENT print ( " Not ▁ a ▁ Mystery ▁ Number " ) NEW_LINE return False NEW_LINE DEDENT n = 121 NEW_LINE isMysteryNumber ( n ) NEW_LINE
Replace every element of the array by product of all other elements | Python 3 program to Replace every element by the product of all other elements ; Calculate the product of all the elements ; Replace every element product of all other elements ; Driver Code ; Print the modified array .
def ReplaceElements ( arr , n ) : NEW_LINE INDENT prod = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT prod *= arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = prod // arr [ i ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE ReplaceElements ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Check if there is any pair in a given range with GCD is divisible by k | function to count such possible numbers ; if i is divisible by k ; if count of such numbers is greater than one ; Driver code
def Check_is_possible ( l , r , k ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( i % k == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return ( count > 1 ) ; NEW_LINE DEDENT l = 4 ; NEW_LINE r = 12 ; NEW_LINE k = 5 ; NEW_LINE if ( Check_is_possible ( l , r , k ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT
Check if any permutation of N equals any power of K | function to check if N and K are anagrams ; Function to check if any permutation of N exist such that it is some power of K ; generate all power of K under 10 ^ 18 ; check if any power of K is valid ; Driver Code ; function call to print required answer
def isValid ( N , K ) : NEW_LINE INDENT m1 = [ ] NEW_LINE m2 = [ ] NEW_LINE while ( N > 0 ) : NEW_LINE INDENT m1 . append ( N % 10 ) NEW_LINE N //= 10 NEW_LINE DEDENT while ( K > 0 ) : NEW_LINE INDENT m2 . append ( K % 10 ) NEW_LINE K //= 10 NEW_LINE DEDENT if ( m1 == m2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def anyPermutation ( N , K ) : NEW_LINE INDENT powK = [ 0 ] * 100 NEW_LINE Limit = pow ( 10 , 18 ) NEW_LINE powK [ 0 ] = K NEW_LINE i = 1 NEW_LINE while ( powK [ i - 1 ] * K < Limit ) : NEW_LINE INDENT powK [ i ] = powK [ i - 1 ] * K NEW_LINE i += 1 NEW_LINE DEDENT for j in range ( i ) : NEW_LINE INDENT if ( isValid ( N , powK [ j ] ) ) : NEW_LINE INDENT return " True " NEW_LINE DEDENT DEDENT return " False " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 96889010407 NEW_LINE K = 7 NEW_LINE print ( anyPermutation ( N , K ) ) NEW_LINE DEDENT
Sum of first N natural numbers which are divisible by 2 and 7 | Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code
def sum ( N ) : NEW_LINE INDENT S1 = ( ( N // 2 ) ) * ( 2 * 2 + ( N // 2 - 1 ) * 2 ) // 2 NEW_LINE S2 = ( ( N // 7 ) ) * ( 2 * 7 + ( N // 7 - 1 ) * 7 ) // 2 NEW_LINE S3 = ( ( N // 14 ) ) * ( 2 * 14 + ( N // 14 - 1 ) * 14 ) // 2 NEW_LINE return S1 + S2 - S3 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 20 NEW_LINE print ( sum ( N ) ) NEW_LINE DEDENT
Ways to color a skewed tree such that parent and child have different colors | fast_way is recursive method to calculate power ; Driver Code
def fastPow ( N , K ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT temp = fastPow ( N , int ( K / 2 ) ) ; NEW_LINE if ( K % 2 == 0 ) : NEW_LINE INDENT return temp * temp ; NEW_LINE DEDENT else : NEW_LINE INDENT return N * temp * temp ; NEW_LINE DEDENT DEDENT def countWays ( N , K ) : NEW_LINE INDENT return K * fastPow ( K - 1 , N - 1 ) ; NEW_LINE DEDENT N = 3 ; NEW_LINE K = 3 ; NEW_LINE print ( countWays ( N , K ) ) ; NEW_LINE
Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | Python3 program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; m is the size of the array
def sumNth ( A , B , m , n ) : NEW_LINE INDENT res = 0 ; NEW_LINE if ( n == 1 ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT res = res + A [ i ] ; NEW_LINE DEDENT DEDENT elif ( n == 2 ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT res = res + B [ i ] * m ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT f = [ 0 ] * n ; NEW_LINE f [ 0 ] = 0 ; NEW_LINE f [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT res = ( res + ( m * ( B [ i ] * f [ n - 1 ] ) ) + ( m * ( A [ i ] * f [ n - 2 ] ) ) ) ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT A = [ 1 , 2 , 3 ] ; NEW_LINE B = [ 4 , 5 , 6 ] ; NEW_LINE n = 3 ; NEW_LINE m = len ( A ) ; NEW_LINE print ( sumNth ( A , B , m , n ) ) ; NEW_LINE
Puzzle | Minimum distance for Lizard | Python3 program to find minimum distance to be travelled by lizard ; side of cube ; understand from diagram ; understand from diagram ; minimum distance
import math NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 NEW_LINE AC = a NEW_LINE CE = 2 * a NEW_LINE shortestDistace = math . sqrt ( AC * AC + CE * CE ) NEW_LINE print ( shortestDistace ) NEW_LINE DEDENT
Find Sum of Series 1 ^ 2 | Function to find sum of series ; If n is even ; If n is odd ; return the result ; Driver Code ; Get n ; Find the sum ; Get n ; Find the sum
def sum_of_series ( n ) : NEW_LINE INDENT result = 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT result = - ( n * ( n + 1 ) ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT result = ( n * ( n + 1 ) ) // 2 NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE n = 10 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE DEDENT