codeparrot/xlcost-text-to-code huggingface dataset:
Sum of multiples of A and B less than N | Python 3 program to find the sum of all multiples of A and B below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of A and B below N ; Since , we need the sum of multiples less than N ; common factors of A and B ; Driver code
from math import gcd , sqrt NEW_LINE def sumAP ( n , d ) : NEW_LINE INDENT n = int ( n / d ) NEW_LINE return ( n ) * ( 1 + n ) * d / 2 NEW_LINE DEDENT def sumMultiples ( A , B , n ) : NEW_LINE INDENT n -= 1 NEW_LINE common = int ( ( A * B ) / gcd ( A , B ) ) NEW_LINE return ( sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 100 NEW_LINE A = 5 NEW_LINE B = 10 NEW_LINE print ( " Sum ▁ = " , int ( sumMultiples ( A , B , n ) ) ) NEW_LINE DEDENT
Check if a prime number can be expressed as sum of two Prime Numbers | Python3 program to check if a prime number can be expressed as sum of two Prime Numbers ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; if the number is prime , and number - 2 is also prime ; Driver code
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n == 2 : NEW_LINE INDENT return True NEW_LINE DEDENT if n % 2 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isPossible ( n ) : NEW_LINE INDENT if isPrime ( n ) and isPrime ( n - 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 13 NEW_LINE if isPossible ( n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Count pairs from two arrays whose modulo operation yields K | Function to return the total pairs of elements whose modulo yield K ; set is used to avoid duplicate pairs ; check which element is greater and proceed according to it ; check if modulo is equal to K ; return size of the set ; Driver code
def totalPairs ( arr1 , arr2 , K , n , m ) : NEW_LINE INDENT s = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( arr1 [ i ] > arr2 [ j ] ) : NEW_LINE INDENT if ( arr1 [ i ] % arr2 [ j ] == K ) : NEW_LINE INDENT s [ ( arr1 [ i ] , arr2 [ j ] ) ] = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( arr2 [ j ] % arr1 [ i ] == K ) : NEW_LINE INDENT s [ ( arr2 [ j ] , arr1 [ i ] ) ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return len ( s ) NEW_LINE DEDENT arr1 = [ 8 , 3 , 7 , 50 ] NEW_LINE arr2 = [ 5 , 1 , 10 , 4 ] NEW_LINE K = 3 NEW_LINE n = len ( arr1 ) NEW_LINE m = len ( arr2 ) NEW_LINE print ( totalPairs ( arr1 , arr2 , K , n , m ) ) NEW_LINE
Largest sub | Python 3 program to find the length of the largest sub - array of an array every element of whose is a perfect square ; function to return the length of the largest sub - array of an array every element of whose is a perfect square ; if both a and b are equal then arr [ i ] is a perfect square ; Driver code
from math import sqrt NEW_LINE def contiguousPerfectSquare ( arr , n ) : NEW_LINE INDENT current_length = 0 NEW_LINE max_length = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT b = sqrt ( arr [ i ] ) NEW_LINE a = int ( b ) NEW_LINE if ( a == b ) : NEW_LINE INDENT current_length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_length = 0 NEW_LINE DEDENT max_length = max ( max_length , current_length ) NEW_LINE DEDENT return max_length NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 75 , 4 , 64 , 121 , 25 ] NEW_LINE n = len ( arr ) NEW_LINE print ( contiguousPerfectSquare ( arr , n ) ) NEW_LINE DEDENT
Count pairs of numbers from 1 to N with Product divisible by their Sum | Function to count pairs ; variable to store count ; Generate all possible pairs such that 1 <= x < y < n ; Driver code
def countPairs ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for x in range ( 1 , n ) : NEW_LINE INDENT for y in range ( x + 1 , n + 1 ) : NEW_LINE INDENT if ( ( y * x ) % ( y + x ) == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT n = 15 NEW_LINE print ( countPairs ( n ) ) NEW_LINE
Find the index of the left pointer after possible moves in the array | Function that returns the index of the left pointer ; there 's only one element  in the array ; initially both are at end ; Driver code
def getIndex ( a , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ptrL = 0 NEW_LINE ptrR = n - 1 NEW_LINE sumL = a [ 0 ] NEW_LINE sumR = a [ n - 1 ] NEW_LINE while ( ptrR - ptrL > 1 ) : NEW_LINE INDENT if ( sumL < sumR ) : NEW_LINE INDENT ptrL += 1 NEW_LINE sumL += a [ ptrL ] NEW_LINE DEDENT elif ( sumL > sumR ) : NEW_LINE INDENT ptrR -= 1 NEW_LINE sumR += a [ ptrR ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ptrL NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 7 , 9 , 8 , 7 ] NEW_LINE n = len ( a ) NEW_LINE print ( getIndex ( a , n ) ) NEW_LINE DEDENT
Find the position of the last removed element from the array | Python3 program to find the position of the last removed element from the array ; Function to find the original position of the element which will be removed last ; take ceil of every number ; Since position is index + 1 ; Driver code
import math as mt NEW_LINE def getPosition ( a , n , m ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT a [ i ] = ( a [ i ] // m + ( a [ i ] % m != 0 ) ) NEW_LINE DEDENT ans , maxx = - 1 , - 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( maxx < a [ i ] ) : NEW_LINE INDENT maxx = a [ i ] NEW_LINE ans = i NEW_LINE DEDENT DEDENT return ans + 1 NEW_LINE DEDENT a = [ 2 , 5 , 4 ] NEW_LINE n = len ( a ) NEW_LINE m = 2 NEW_LINE print ( getPosition ( a , n , m ) ) NEW_LINE
Find the value of f ( n ) / f ( r ) * f ( n | Function to find value of given F ( n ) ; iterate over n ; calculate result ; return the result ; Driver code
def calcFunction ( n , r ) : NEW_LINE INDENT finalDenominator = 1 NEW_LINE mx = max ( r , n - r ) NEW_LINE for i in range ( mx + 1 , n + 1 ) : NEW_LINE INDENT denominator = pow ( i , i ) NEW_LINE numerator = pow ( i - mx , i - mx ) NEW_LINE finalDenominator = ( finalDenominator * denominator ) // numerator NEW_LINE DEDENT return finalDenominator NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE r = 2 NEW_LINE print ( "1 / " , end = " " ) NEW_LINE print ( calcFunction ( n , r ) ) NEW_LINE DEDENT
Check if the array has an element which is equal to sum of all the remaining elements | function to check if such element exists or not ; storing frequency in dict ; stores the sum ; traverse the array and count the array element ; Only possible if sum is even ; if half sum is available ; Driver code
def isExists ( a , n ) : NEW_LINE INDENT freq = { i : 0 for i in a } NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE Sum += a [ i ] NEW_LINE DEDENT if Sum % 2 == 0 : NEW_LINE INDENT if freq [ Sum // 2 ] : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT a = [ 5 , 1 , 2 , 2 ] NEW_LINE n = len ( a ) NEW_LINE if isExists ( a , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Print all numbers less than N with at | Python 3 program to print all the numbers less than N which have at most 2 unique digits ; Function to generate all possible numbers ; If the number is less than n ; If the number exceeds ; Check if it is not the same number ; Function to print all numbers ; All combination of digits ; Print all numbers ; Driver code
st = set ( ) NEW_LINE def generateNumbers ( n , num , a , b ) : NEW_LINE INDENT if ( num > 0 and num < n ) : NEW_LINE INDENT st . add ( num ) NEW_LINE DEDENT if ( num >= n ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( num * 10 + a > num ) : NEW_LINE INDENT generateNumbers ( n , num * 10 + a , a , b ) NEW_LINE DEDENT generateNumbers ( n , num * 10 + b , a , b ) NEW_LINE DEDENT def printNumbers ( n ) : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT for j in range ( i + 1 , 10 , 1 ) : NEW_LINE INDENT generateNumbers ( n , 0 , i , j ) NEW_LINE DEDENT DEDENT print ( " The ▁ numbers ▁ are : " , end = " ▁ " ) NEW_LINE l = list ( st ) NEW_LINE for i in l : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE printNumbers ( n ) NEW_LINE DEDENT
Smallest prime number missing in an array | This function finds all prime numbers upto 10 ^ 5 ; use sieve to find prime ; If number is prime , then store it in prime list ; Function to find the smallest prime missing ; first of all find all prime ; now store all number as index of freq arr so that we can improve searching ; now check for each prime ; Driver Code ; find smallest prime which is not present
def findPrime ( MAX ) : NEW_LINE INDENT pm = [ True ] * ( MAX + 1 ) NEW_LINE pm [ 0 ] , pm [ 1 ] = False , False NEW_LINE for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if pm [ i ] == True : NEW_LINE INDENT for j in range ( 2 * i , MAX + 1 , i ) : NEW_LINE INDENT pm [ j ] = False NEW_LINE DEDENT DEDENT DEDENT prime = [ ] NEW_LINE for i in range ( 0 , MAX + 1 ) : NEW_LINE INDENT if pm [ i ] == True : NEW_LINE INDENT prime . append ( i ) NEW_LINE DEDENT DEDENT return prime NEW_LINE DEDENT def findSmallest ( arr , n ) : NEW_LINE INDENT MAX = max ( arr ) NEW_LINE prime = findPrime ( MAX ) NEW_LINE s = set ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT ans = - 1 NEW_LINE for i in range ( 0 , len ( prime ) ) : NEW_LINE INDENT if prime [ i ] not in s : NEW_LINE INDENT ans = prime [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 0 , 1 , 2 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE if ( findSmallest ( arr , n ) == - 1 ) : NEW_LINE INDENT print ( " No ▁ prime ▁ number ▁ missing " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( findSmallest ( arr , n ) ) NEW_LINE DEDENT DEDENT
Find the number after successive division | Function to find the number ; Driver Code
def findNum ( div , rem , N ) : NEW_LINE INDENT num = rem [ N - 1 ] NEW_LINE i = N - 2 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT num = num * div [ i ] + rem [ i ] NEW_LINE i -= 1 NEW_LINE DEDENT return num NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT div = [ 8 , 3 ] NEW_LINE rem = [ 2 , 2 ] NEW_LINE N = len ( div ) NEW_LINE print ( findNum ( div , rem , N ) ) NEW_LINE DEDENT
Ways to form n / 2 pairs such that difference of pairs is minimum | Python3 implementation of the above approach ; Using mod because the number of ways might be very large ; ways is serving the same purpose as discussed ; pairing up zero people requires one way . ; map count stores count of s . ; sort according to value ; Iterating backwards . ; Checking if current count is odd . ; if current count = 5 , multiply ans by ways [ 4 ] . ; left out person will be selected in current_count ways ; left out person will pair with previous person in previous_count ways ; if previous count is odd , then multiply answer by ways [ prev_count - 1 ] . since one has already been reserved , remaining will be even . reduce prev_count = 0 , since we don 't need it now. ; if prev count is even , one will be reserved , therefore decrement by 1. In the next iteration , prev_count will become odd and it will be handled in the same way . ; if current count is even , then simply multiply ways [ current_count ] to answer . ; multiply answer by ways [ first__count ] since that is left out , after iterating the array . ; Driver code
from collections import defaultdict NEW_LINE mod = 1000000007 NEW_LINE MAX = 100000 NEW_LINE ways = [ None ] * ( MAX + 1 ) NEW_LINE def preCompute ( ) : NEW_LINE INDENT ways [ 0 ] = 1 NEW_LINE ways [ 2 ] = 1 NEW_LINE for i in range ( 4 , MAX + 1 , 2 ) : NEW_LINE INDENT ways [ i ] = ( ( 1 * ( i - 1 ) * ways [ i - 2 ] ) % mod ) NEW_LINE DEDENT DEDENT def countWays ( arr , n ) : NEW_LINE INDENT count = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT count_vector = [ ] NEW_LINE for key in count : NEW_LINE INDENT count_vector . append ( [ key , count [ key ] ] ) NEW_LINE DEDENT count_vector . sort ( ) NEW_LINE ans = 1 NEW_LINE for i in range ( len ( count_vector ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT current_count = count_vector [ i ] [ 1 ] NEW_LINE prev_count = count_vector [ i - 1 ] [ 1 ] NEW_LINE if current_count & 1 : NEW_LINE INDENT ans = ( ans * ways [ current_count - 1 ] ) % mod NEW_LINE ans = ( ans * current_count ) % mod NEW_LINE ans = ( ans * prev_count ) % mod NEW_LINE if prev_count & 1 : NEW_LINE INDENT ans = ( ans * ways [ prev_count - 1 ] ) % mod NEW_LINE count_vector [ i - 1 ] [ 1 ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT count_vector [ i - 1 ] [ 1 ] -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT ans = ( ans * ways [ current_count ] ) % mod NEW_LINE DEDENT DEDENT ans = ( ans * ways [ count_vector [ 0 ] [ 1 ] ] ) % mod NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT preCompute ( ) NEW_LINE arr = [ 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE countWays ( arr , n ) NEW_LINE DEDENT
Program to find the profit or loss when CP of N items is equal to SP of M items | Function to calculate Profit or loss ; Driver Code
def profitLoss ( N , M ) : NEW_LINE INDENT if ( N == M ) : NEW_LINE INDENT print ( " No ▁ Profit ▁ nor ▁ Loss " ) NEW_LINE DEDENT else : NEW_LINE INDENT result = 0.0 NEW_LINE result = float ( abs ( N - M ) ) / M NEW_LINE if ( N - M < 0 ) : NEW_LINE INDENT print ( " Loss ▁ = ▁ - " , ' { 0 : . 6 } ' . format ( result * 100 ) , " % " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Profit ▁ = ▁ " , ' { 0 : . 6 } ' . format ( result * 100 ) , " % " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE M = 9 NEW_LINE profitLoss ( N , M ) NEW_LINE DEDENT
Find all good indices in the given Array | Python3 program to find all good indices in the given array ; Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; Driver Code
from collections import defaultdict NEW_LINE def niceIndices ( A , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE Sum += A [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT k = Sum - A [ i ] NEW_LINE if k % 2 == 0 : NEW_LINE INDENT k = k >> 1 NEW_LINE if k in m : NEW_LINE INDENT if ( ( A [ i ] == k and m [ k ] > 1 ) or ( A [ i ] != k ) ) : NEW_LINE INDENT print ( ( i + 1 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 8 , 3 , 5 , 2 ] NEW_LINE n = len ( A ) NEW_LINE niceIndices ( A , n ) NEW_LINE DEDENT
Count pieces of circle after N cuts | Function to find number of pieces of circle after N cuts ; Driver Code
def countPieces ( N ) : NEW_LINE INDENT return 2 * N NEW_LINE DEDENT N = 100 NEW_LINE print ( countPieces ( N ) ) NEW_LINE
Sum of all the multiples of 3 and 7 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code
def sumAP ( n , d ) : NEW_LINE INDENT n = int ( n / d ) ; NEW_LINE return ( n ) * ( 1 + n ) * ( d / 2 ) ; NEW_LINE DEDENT def sumMultiples ( n ) : NEW_LINE INDENT n -= 1 ; NEW_LINE return int ( sumAP ( n , 3 ) + sumAP ( n , 7 ) - sumAP ( n , 21 ) ) ; NEW_LINE DEDENT n = 24 ; NEW_LINE print ( sumMultiples ( n ) ) ; NEW_LINE
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; if size is even ; if size is odd ; Driver code
def productSumDivisible ( n , size ) : NEW_LINE INDENT sum = 0 NEW_LINE product = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( size % 2 == 0 ) : NEW_LINE INDENT product *= n % 10 NEW_LINE DEDENT else : NEW_LINE INDENT sum += n % 10 NEW_LINE DEDENT n = n // 10 NEW_LINE size -= 1 NEW_LINE DEDENT if ( product % sum == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1234 NEW_LINE len = 4 NEW_LINE if ( productSumDivisible ( n , len ) ) : NEW_LINE INDENT print ( " TRUE " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " FALSE " ) NEW_LINE DEDENT DEDENT
Check whether product of digits at even places is divisible by sum of digits at odd place of a number | Below function checks whether product of digits at even places is divisible by sum of digits at odd places ; Converting integer to string ; Traveersing the string ; Driver code
def productSumDivisible ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE product = 1 NEW_LINE num = str ( n ) NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT product = product * int ( num [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + int ( num [ i ] ) NEW_LINE DEDENT DEDENT if ( product % sum == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1234 NEW_LINE if ( productSumDivisible ( n ) ) : NEW_LINE INDENT print ( " TRUE " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " FALSE " ) NEW_LINE DEDENT DEDENT
GCD of a number raised to some power and another number | Python 3 program to find GCD of a ^ n and b . ; Returns GCD of a ^ n and b . ; Driver code
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def powGCD ( a , n , b ) : NEW_LINE INDENT for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT a = a * a NEW_LINE DEDENT return gcd ( a , b ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE b = 5 NEW_LINE n = 2 NEW_LINE print ( powGCD ( a , n , b ) ) NEW_LINE DEDENT
Position after taking N steps to the right and left in an alternate manner | Function to return the last destination ; Driver Code
def lastCoordinate ( n , a , b ) : NEW_LINE INDENT return ( ( ( n + 1 ) // 2 ) * a - ( n // 2 ) * b ) NEW_LINE DEDENT n = 3 NEW_LINE a = 5 NEW_LINE b = 2 NEW_LINE print ( lastCoordinate ( n , a , b ) ) NEW_LINE
Smallest number greater than or equal to N divisible by K | Function to find the smallest number greater than or equal to N that is divisible by k ; Driver Code
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 N = 45 NEW_LINE K = 6 NEW_LINE print ( ' Smallest ▁ number ▁ greater ▁ than ' , ' or ▁ equal ▁ to ' , N , ' that ▁ is ▁ divisible ▁ by ' , K , ' is ' , findNum ( 45 , 6 ) ) NEW_LINE
Sum and Product of digits in a number that divide the number | Print the sum and product of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver code
def countDigit ( n ) : NEW_LINE INDENT temp = n NEW_LINE sum = 0 NEW_LINE product = 1 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 NEW_LINE temp //= 10 NEW_LINE if ( d > 0 and n % d == 0 ) : NEW_LINE INDENT sum += d NEW_LINE product *= d NEW_LINE DEDENT DEDENT print ( " Sum ▁ = " , sum ) NEW_LINE print ( " Product ▁ = " , product ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 1012 NEW_LINE countDigit ( n ) NEW_LINE DEDENT
Largest number smaller than or equal to N divisible by K | Function to find the largest number smaller than or equal to N that is divisible by k ; Driver code
def findNum ( N , K ) : NEW_LINE INDENT rem = N % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N - rem NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 45 NEW_LINE K = 6 NEW_LINE print ( " Largest ▁ number ▁ smaller ▁ than ▁ or ▁ equal ▁ to " + str ( N ) + " that ▁ is ▁ divisible ▁ by " + str ( K ) + " is " , findNum ( N , K ) ) NEW_LINE DEDENT
Check if any permutation of a number is divisible by 3 and is Palindromic | Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check if number is not divisible by 3 ; If more than one digits have odd frequency , palindromic permutation not possible ; Driver Code
def isDivisiblePalindrome ( n ) : NEW_LINE INDENT hash = [ 0 ] * 10 NEW_LINE digitSum = 0 NEW_LINE while ( n ) : NEW_LINE INDENT digitSum += n % 10 NEW_LINE hash [ n % 10 ] += 1 NEW_LINE n //= 10 NEW_LINE DEDENT if ( digitSum % 3 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT oddCount = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( hash [ i ] % 2 != 0 ) : NEW_LINE INDENT oddCount += 1 NEW_LINE DEDENT DEDENT if ( oddCount > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT n = 34734 NEW_LINE if ( isDivisiblePalindrome ( n ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT
Minimum time to return array to its original state after given modifications | Python implementation of above approach ; Function to return lcm of two numbers ; Make a graph ; Add edge ; Count reachable nodes from every node . ; Driver code
import math NEW_LINE def lcm ( a , b ) : NEW_LINE INDENT return ( a * b ) // ( math . gcd ( a , b ) ) NEW_LINE DEDENT def dfs ( src , adj , visited ) : NEW_LINE INDENT visited [ src ] = True NEW_LINE count = 1 NEW_LINE if adj [ src ] != 0 : NEW_LINE INDENT for i in range ( len ( adj [ src ] ) ) : NEW_LINE INDENT if ( not visited [ adj [ src ] [ i ] ] ) : NEW_LINE INDENT count += dfs ( adj [ src ] [ i ] , adj , visited ) NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT def findMinTime ( arr , P , n ) : NEW_LINE INDENT adj = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if adj [ arr [ i ] ] == 0 : NEW_LINE INDENT adj [ arr [ i ] ] = [ ] NEW_LINE DEDENT adj [ arr [ i ] ] . append ( P [ i ] ) NEW_LINE DEDENT visited = [ 0 ] * ( n + 1 ) NEW_LINE ans = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT ans = lcm ( ans , dfs ( i , adj , visited ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE P = [ 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinTime ( arr , P , n ) ) NEW_LINE
Check whether product of digits at even places of a number is divisible by K | below function checks whether product of digits at even places is divisible by K ; if position is even ; Driver code
def productDivisible ( n , k ) : NEW_LINE INDENT product = 1 NEW_LINE position = 1 NEW_LINE while n > 0 : NEW_LINE INDENT if position % 2 == 0 : NEW_LINE INDENT product *= n % 10 NEW_LINE DEDENT n = n / 10 NEW_LINE position += 1 NEW_LINE DEDENT if product % k == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 321922 NEW_LINE k = 3 NEW_LINE if productDivisible ( n , k ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT