codeparrot/xlcost-text-to-code huggingface dataset:
Add two numbers represented by Stacks | Function to return the stack that contains the sum of two numbers ; Calculate the sum of the top elements of both the stacks ; Push the sum into the stack ; Store the carry ; Pop the top elements ; If N1 is not empty ; If N2 is not empty ; If carry remains ; Reverse the stack . so that most significant digit is at the bottom of the stack ; Function to display the resultamt stack ; Driver Code
def addStack ( N1 , N2 ) : NEW_LINE INDENT res = [ ] NEW_LINE s = 0 NEW_LINE rem = 0 NEW_LINE while ( len ( N1 ) != 0 and len ( N2 ) != 0 ) : NEW_LINE INDENT s = ( rem + N1 [ - 1 ] + N2 [ - 1 ] ) NEW_LINE res . append ( s % 10 ) NEW_LINE rem = s // 10 NEW_LINE N1 . pop ( - 1 ) NEW_LINE N2 . pop ( - 1 ) NEW_LINE DEDENT while ( len ( N1 ) != 0 ) : NEW_LINE INDENT s = rem + N1 [ - 1 ] NEW_LINE res . append ( s % 10 ) NEW_LINE rem = s // 10 NEW_LINE N1 . pop ( - 1 ) NEW_LINE DEDENT while ( len ( N2 ) != 0 ) : NEW_LINE INDENT s = rem + N2 [ - 1 ] NEW_LINE res . append ( s % 10 ) NEW_LINE rem = s // 10 NEW_LINE N2 . pop ( - 1 ) NEW_LINE DEDENT while ( rem > 0 ) : NEW_LINE INDENT res . append ( rem ) NEW_LINE rem //= 10 NEW_LINE DEDENT res = res [ : : - 1 ] NEW_LINE return res NEW_LINE DEDENT def display ( res ) : NEW_LINE INDENT s = " " NEW_LINE for i in res : NEW_LINE INDENT s += str ( i ) NEW_LINE DEDENT print ( s ) NEW_LINE DEDENT N1 = [ ] NEW_LINE N1 . append ( 5 ) NEW_LINE N1 . append ( 8 ) NEW_LINE N1 . append ( 7 ) NEW_LINE N1 . append ( 4 ) NEW_LINE N2 = [ ] NEW_LINE N2 . append ( 2 ) NEW_LINE N2 . append ( 1 ) NEW_LINE N2 . append ( 3 ) NEW_LINE res = addStack ( N1 , N2 ) NEW_LINE display ( res ) NEW_LINE
Check if a number has an odd count of odd divisors and even count of even divisors | Python3 implementation of the above approach ; Function to check if the number is a perfect square ; Find floating povalue of square root of x . ; If square root is an integer ; Function to check if count of even divisors is even and count of odd divisors is odd ; Driver code
import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = pow ( x , 1 / 2 ) ; NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) ; NEW_LINE DEDENT def checkFactors ( x ) : NEW_LINE INDENT if ( isPerfectSquare ( x ) ) : 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 = 36 ; NEW_LINE checkFactors ( N ) ; NEW_LINE DEDENT
Queries to find the XOR of an Array after replacing all occurrences of X by Y | Stores the bitwise XOR of array elements ; Function to find the total xor ; Loop to find the xor of all the elements ; Function to find the XOR after replacing all occurrences of X by Y for Q queries ; Remove contribution of X from total_xor ; Adding contribution of Y to total_xor ; Print total_xor ; Driver Code
global total_xor NEW_LINE total_xor = 0 NEW_LINE def initialize_xor ( arr , n ) : NEW_LINE INDENT global total_xor NEW_LINE for i in range ( n ) : NEW_LINE INDENT total_xor = total_xor ^ arr [ i ] NEW_LINE DEDENT DEDENT def find_xor ( X , Y ) : NEW_LINE INDENT global total_xor NEW_LINE total_xor = total_xor ^ X NEW_LINE total_xor = total_xor ^ Y NEW_LINE print ( total_xor ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE initialize_xor ( arr , n ) NEW_LINE Q = [ [ 5 , 6 ] , [ 8 , 1 ] ] NEW_LINE for i in range ( len ( Q ) ) : NEW_LINE INDENT find_xor ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT
Check whether a given number is an ugly number or not | Function to check if a number is an ugly number or not ; Base Cases ; Condition to check if the number is divided by 2 , 3 , or 5 ; Driver Code
def isUgly ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return ( isUgly ( n // 2 ) ) NEW_LINE DEDENT if ( n % 3 == 0 ) : NEW_LINE INDENT return ( isUgly ( n // 3 ) ) NEW_LINE DEDENT if ( n % 5 == 0 ) : NEW_LINE INDENT return ( isUgly ( n // 5 ) ) NEW_LINE DEDENT return 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT no = isUgly ( 14 ) NEW_LINE if ( no == 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Maximum possible GCD for a pair of integers with sum N | Function to find the required GCD value ; If i is a factor of N ; Return the largest factor possible ; If N is a prime number ; Driver Code
def maxGCD ( N ) : NEW_LINE INDENT i = 2 NEW_LINE while ( i * i <= N ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT return N // i NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT N = 33 NEW_LINE print ( " Maximum ▁ Possible ▁ GCD ▁ value ▁ is ▁ : ▁ " , maxGCD ( N ) ) NEW_LINE
Minimize K whose XOR with given array elements leaves array unchanged | Function to find the minimum value of K in given range ; Declare a set ; Initialize count variable ; Iterate in range [ 1 , 1024 ] ; counter set as 0 ; Iterating through the Set ; Check if the XOR calculated is present in the Set ; If the value of Bitwise XOR inside the given set then increment count ; Check if the value of count is equal to the size of set ; Return minimum value of K ; If no such K is found ; Given array ; Function Call
def min_value ( arr , N ) : NEW_LINE INDENT x , X , K = 0 , 0 , 0 NEW_LINE S = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT S . add ( arr [ i ] ) NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 1 , 1024 ) : NEW_LINE INDENT count = 0 NEW_LINE for it in S : NEW_LINE INDENT X = ( ( i it ) - ( i & it ) ) NEW_LINE if X in S : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == len ( S ) ) : NEW_LINE INDENT K = i NEW_LINE return K NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 1 , 0 , 3 , 3 , 0 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( min_value ( arr , N ) ) NEW_LINE
Junction Numbers | Python3 program for the above approach ; Function to find the sum Of digits of N ; To store sum of N and sumOfdigitsof ( N ) ; extracting digit ; Function to check Junction numbers ; To store count of ways n can be represented as i + SOD ( i ) ; Driver Code ; Function Call
import math NEW_LINE def sum1 ( n ) : NEW_LINE INDENT sum1 = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT r = n % 10 NEW_LINE sum1 = sum1 + r NEW_LINE n = n // 10 NEW_LINE DEDENT return sum1 NEW_LINE DEDENT def isJunction ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i + sum1 ( i ) == n ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count >= 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 111 NEW_LINE if ( isJunction ( n ) == 1 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Pointer | Function to find the product of digits of a number N ; Function that returns true if n is prime else returns false ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; Function to check Pointer - Prime numbers ; Driver Code ; Given number N ; Function call
def digProduct ( n ) : NEW_LINE INDENT product = 1 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT product = product * ( n % 10 ) NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return product NEW_LINE DEDENT def isPrime ( 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 i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def nextPrime ( N ) : NEW_LINE INDENT if ( N <= 1 ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT prime = N NEW_LINE found = False NEW_LINE while ( not found ) : NEW_LINE INDENT prime = prime + 1 NEW_LINE if ( isPrime ( prime ) ) : NEW_LINE INDENT found = True NEW_LINE DEDENT DEDENT return prime NEW_LINE DEDENT def isPointerPrime ( n ) : NEW_LINE INDENT if ( isPrime ( n ) and ( n + digProduct ( n ) == nextPrime ( n ) ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 23 NEW_LINE if ( isPointerPrime ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Perfect Digital Invariants number | Function to find the sum of divisors ; Function to check whether the given number is Perfect Digital Invariant number or not ; For each digit in temp ; If satisfies Perfect Digital Invariant condition ; If sum exceeds n , then not possible ; Given Number N ; Function Call
def power ( x , y ) : NEW_LINE INDENT if ( y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( y % 2 == 0 ) : NEW_LINE INDENT return ( power ( x , y // 2 ) * power ( x , y // 2 ) ) NEW_LINE DEDENT return ( x * power ( x , y // 2 ) * power ( x , y // 2 ) ) NEW_LINE DEDENT def isPerfectDigitalInvariant ( x ) : NEW_LINE INDENT fixed_power = 0 NEW_LINE while True : NEW_LINE INDENT fixed_power += 1 NEW_LINE temp = x NEW_LINE summ = 0 NEW_LINE while ( temp ) : NEW_LINE INDENT r = temp % 10 NEW_LINE summ = summ + power ( r , fixed_power ) NEW_LINE temp = temp // 10 NEW_LINE DEDENT if ( summ == x ) : NEW_LINE INDENT return ( True ) NEW_LINE DEDENT if ( summ > x ) : NEW_LINE INDENT return ( False ) NEW_LINE DEDENT DEDENT DEDENT N = 4150 NEW_LINE if ( isPerfectDigitalInvariant ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Brilliant Numbers | Python3 program for the above approach ; Function to generate all prime numbers less than n ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to return the number of digits in a number ; Function to check if N is a Brilliant number ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Given Number ; Function Call
import math NEW_LINE def SieveOfEratosthenes ( n , isPrime ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT isPrime [ i ] = True NEW_LINE DEDENT p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def countDigit ( n ) : NEW_LINE INDENT return math . floor ( math . log10 ( n ) + 1 ) NEW_LINE DEDENT def isBrilliant ( n ) : NEW_LINE INDENT flag = 0 NEW_LINE isPrime = [ 0 ] * ( n + 1 ) NEW_LINE SieveOfEratosthenes ( n , isPrime ) NEW_LINE for i in range ( 2 , n , 1 ) : NEW_LINE INDENT x = n // i NEW_LINE if ( isPrime [ i ] and isPrime [ x ] and x * i == n ) : NEW_LINE INDENT if ( countDigit ( i ) == countDigit ( x ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT n = 1711 NEW_LINE if ( isBrilliant ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Icosikaienneagonal Number | Function to Find the Nth icosikaienneagonal Number ; Driver Code
def icosikaienneagonalNum ( n ) : NEW_LINE INDENT return ( 27 * n * n - 25 * n ) // 2 NEW_LINE DEDENT N = 3 NEW_LINE print ( icosikaienneagonalNum ( N ) ) NEW_LINE
Find the largest contiguous pair sum in given Array | Python3 program to find the a contiguous pair from the which has the largest sum ; Function to find and return the largest sum contiguous pair ; Stores the contiguous pair ; Initialize maximum sum ; Compare sum of pair with max_sum ; Insert the pair ; Driver Code
import sys NEW_LINE def largestSumpair ( arr , n ) : NEW_LINE INDENT pair = [ ] NEW_LINE max_sum = - sys . maxsize - 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if max_sum < ( arr [ i ] + arr [ i - 1 ] ) : NEW_LINE INDENT max_sum = arr [ i ] + arr [ i - 1 ] NEW_LINE if pair == [ ] : NEW_LINE INDENT pair . append ( arr [ i - 1 ] ) NEW_LINE pair . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT pair [ 0 ] = arr [ i - 1 ] NEW_LINE pair [ 1 ] = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return pair NEW_LINE DEDENT arr = [ 11 , - 5 , 9 , - 3 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE pair = largestSumpair ( arr , N ) NEW_LINE print ( pair [ 0 ] , end = " ▁ " ) NEW_LINE print ( pair [ 1 ] , end = " ▁ " ) NEW_LINE
Sum of minimum value of x and y satisfying the equation ax + by = c | Python3 program for the above approach ; x and y store solution of equation ax + by = g ; Euclidean Algorithm ; store_gcd returns the gcd of a and b ; Function to find any possible solution ; Condition if solution does not exists ; Adjusting the sign of x0 and y0 ; Function to shift solution ; Shifting to obtain another solution ; Function to find minimum value of x and y ; g is the gcd of a and b ; Store sign of a and b ; If x is less than 0 , then shift solution ; If y is less than 0 , then shift solution ; Find intersection such that both x and y are positive ; miny is value of y corresponding to minx ; Returns minimum value of x + y ; Given a , b , and c ; Function call
x , y , x1 , y1 = 0 , 0 , 0 , 0 NEW_LINE x0 , y0 , g = 0 , 0 , 0 NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT global x , y , x1 , y1 NEW_LINE if ( b == 0 ) : NEW_LINE INDENT x = 1 NEW_LINE y = 0 NEW_LINE return a NEW_LINE DEDENT store_gcd = gcd ( b , a % b ) NEW_LINE x = y1 NEW_LINE y = x1 - y1 * ( a // b ) NEW_LINE return store_gcd NEW_LINE DEDENT def possible_solution ( a , b , c ) : NEW_LINE INDENT global x0 , y0 , g NEW_LINE g = gcd ( abs ( a ) , abs ( b ) ) NEW_LINE if ( c % g != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT x0 *= c // g NEW_LINE y0 *= c // g NEW_LINE if ( a < 0 ) : NEW_LINE INDENT x0 *= - 1 NEW_LINE DEDENT if ( b < 0 ) : NEW_LINE INDENT y0 *= - 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def shift_solution ( a , b , shift_var ) : NEW_LINE INDENT global x , y NEW_LINE x += shift_var * b NEW_LINE y -= shift_var * a NEW_LINE DEDENT def find_min_sum ( a , b , c ) : NEW_LINE INDENT global x , y , g NEW_LINE x , y , g = 0 , 0 , 0 NEW_LINE if ( possible_solution ( a , b , c ) == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( g != 0 ) : NEW_LINE INDENT a //= g NEW_LINE b //= g NEW_LINE DEDENT if a > 0 : NEW_LINE INDENT sign_a = 1 NEW_LINE DEDENT else : NEW_LINE INDENT sign_a = - 1 NEW_LINE DEDENT if b > 0 : NEW_LINE INDENT sign_b = 1 NEW_LINE DEDENT else : NEW_LINE INDENT sign_b = - 1 NEW_LINE DEDENT shift_solution ( a , b , - x // b ) NEW_LINE if ( x < 0 ) : NEW_LINE INDENT shift_solution ( a , b , sign_b ) NEW_LINE DEDENT minx1 = x NEW_LINE shift_solution ( a , b , y // a ) NEW_LINE if ( y < 0 ) : NEW_LINE INDENT shift_solution ( a , b , - sign_a ) NEW_LINE DEDENT minx2 = x NEW_LINE if ( minx2 > x ) : NEW_LINE INDENT temp = minx2 NEW_LINE minx2 = x NEW_LINE x = temp NEW_LINE DEDENT minx = max ( minx1 , minx2 ) NEW_LINE if ( minx > x ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT miny = ( c - a * x ) // b NEW_LINE return ( miny + minx ) NEW_LINE DEDENT a , b , c = 2 , 2 , 0 NEW_LINE print ( find_min_sum ( a , b , c ) ) NEW_LINE
Super | Function to check if N is a super - d number ; Driver Code
def isSuperdNum ( n ) : NEW_LINE INDENT for d in range ( 2 , 10 ) : NEW_LINE INDENT substring = str ( d ) * d ; NEW_LINE if substring in str ( d * pow ( n , d ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT n = 261 NEW_LINE if isSuperdNum ( n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Second hexagonal numbers | Function to find N - th term in the series ; Driver code
def findNthTerm ( n ) : NEW_LINE INDENT print ( n * ( 2 * n + 1 ) ) NEW_LINE DEDENT N = 4 NEW_LINE findNthTerm ( N ) NEW_LINE
Apocalyptic Number | Function to check if a number N is Apocalyptic ; Given Number N ; Function Call
def isApocalyptic ( n ) : NEW_LINE INDENT if '666' in str ( 2 ** n ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT N = 157 NEW_LINE if ( isApocalyptic ( 157 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Kummer Numbers | Python3 program to find Kummer number upto n terms ; list to store all prime less than and equal to 10 ^ 6 ; Function for the Sieve of Sundaram . This function stores all prime numbers less than MAX in primes ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This list is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to calculate nth Kummer number ; Multiply first n primes ; return nth Kummer number ; Driver code
import math NEW_LINE MAX = 1000000 NEW_LINE primes = [ ] NEW_LINE def sieveSundaram ( ) : NEW_LINE INDENT marked = [ 0 ] * int ( MAX / 2 + 1 ) NEW_LINE for i in range ( 1 , int ( ( math . sqrt ( MAX ) - 1 ) // 2 ) + 1 ) : NEW_LINE INDENT for j in range ( ( i * ( i + 1 ) ) << 1 , MAX // 2 + 1 , 2 * i + 1 ) : NEW_LINE INDENT marked [ j ] = True NEW_LINE DEDENT DEDENT primes . append ( 2 ) NEW_LINE for i in range ( 1 , MAX // 2 + 1 ) : NEW_LINE INDENT if ( marked [ i ] == False ) : NEW_LINE INDENT primes . append ( 2 * i + 1 ) NEW_LINE DEDENT DEDENT DEDENT def calculateKummer ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT result = result * primes [ i ] NEW_LINE DEDENT return ( - 1 + result ) NEW_LINE DEDENT N = 5 NEW_LINE sieveSundaram ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( calculateKummer ( i ) , end = " , ▁ " ) NEW_LINE DEDENT
Check if given number contains a digit which is the average of all other digits | Function which checks if a digits exists in n which is the average of all other digits ; Calculate sum of digits in n ; Store digits ; Increase the count of digits in n ; If average of all digits is integer ; Otherwise ; Driver code
def check ( n ) : NEW_LINE INDENT digits = set ( ) NEW_LINE temp = n NEW_LINE Sum = 0 NEW_LINE count = 0 NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT Sum += temp % 10 NEW_LINE digits . add ( temp % 10 ) NEW_LINE count += 1 NEW_LINE temp = temp // 10 NEW_LINE DEDENT if ( ( Sum % count == 0 ) and ( ( int ) ( Sum / count ) in digits ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT n = 42644 NEW_LINE check ( n ) NEW_LINE
Count of lexicographically smaller characters on right | function to count the smaller characters at the right of index i ; store the length of String ; for each index initialize count as zero ; increment the count if characters are smaller than at ith index ; print the count of characters smaller than the index i ; Driver code ; input String
def countSmaller ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( str [ j ] < str [ i ] ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT print ( cnt , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " edcba " ; NEW_LINE countSmaller ( str ) ; NEW_LINE DEDENT
Find the smallest contiguous sum pair in an Array | Python3 program to find the smallest sum contiguous pair ; Function to find the smallest sum contiguous pair ; Contiguous pair ; isntialize minimum sum with maximum value ; checking for minimum value ; Add to pair ; Updating pair ; Driver code
import sys NEW_LINE def smallestSumpair ( arr , n ) : NEW_LINE INDENT pair = [ ] NEW_LINE min_sum = sys . maxsize NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if min_sum > ( arr [ i ] + arr [ i - 1 ] ) : NEW_LINE INDENT min_sum = arr [ i ] + arr [ i - 1 ] NEW_LINE if pair == [ ] : NEW_LINE INDENT pair . append ( arr [ i - 1 ] ) NEW_LINE pair . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT pair [ 0 ] = arr [ i - 1 ] NEW_LINE pair [ 1 ] = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return pair NEW_LINE DEDENT arr = [ 4 , 9 , - 3 , 2 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE pair = smallestSumpair ( arr , n ) NEW_LINE print ( pair [ 0 ] , pair [ 1 ] ) NEW_LINE
Sum of the products of same placed digits of two numbers | Function to find the sum of the products of their corresponding digits ; Loop until one of the numbers have no digits remaining ; Driver Code
def sumOfProductOfDigits ( n1 , n2 ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE while ( n1 > 0 and n2 > 0 ) : NEW_LINE INDENT sum1 += ( ( n1 % 10 ) * ( n2 % 10 ) ) ; NEW_LINE n1 = n1 // 10 ; NEW_LINE n2 = n2 // 10 ; NEW_LINE DEDENT return sum1 ; NEW_LINE DEDENT n1 = 25 ; NEW_LINE n2 = 1548 ; NEW_LINE print ( sumOfProductOfDigits ( n1 , n2 ) ) ; NEW_LINE
Count of ways to represent N as sum of a prime number and twice of a square | Python3 implementation to count the number of ways a number can be written as sum of prime number and twice a square ; Function to mark all the prime numbers using sieve ; Loop to mark the prime numbers upto the Square root of N ; Loop to store the prime numbers in an array ; Function to find the number ways to represent a number as the sum of prime number and square of a number ; Loop to iterate over all the possible prime numbers ; Increment the count if the given number is a valid number ; Driver Code ; Function call
import math NEW_LINE n = 500000 - 2 NEW_LINE v = [ ] NEW_LINE def sieveoferanthones ( ) : NEW_LINE INDENT prime = [ 1 ] * ( n + 1 ) NEW_LINE for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( prime [ i ] != 0 ) : NEW_LINE INDENT for j in range ( i * i , n + 1 , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( prime [ i ] != 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def numberOfWays ( n ) : NEW_LINE INDENT count = 0 NEW_LINE j = 1 NEW_LINE while ( 2 * ( pow ( j , 2 ) ) < n ) : NEW_LINE INDENT i = 1 NEW_LINE while ( v [ i ] + 2 <= n ) : NEW_LINE INDENT if ( n == v [ i ] + ( 2 * ( math . pow ( j , 2 ) ) ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT sieveoferanthones ( ) NEW_LINE n = 9 NEW_LINE numberOfWays ( n ) NEW_LINE
Program to convert Degree to Radian | Function for conversion ; Driver code
def Convert ( degree ) : NEW_LINE INDENT pi = 3.14159265359 ; NEW_LINE return ( degree * ( pi / 180 ) ) ; NEW_LINE DEDENT degree = 30 ; NEW_LINE radian = Convert ( degree ) ; NEW_LINE print ( radian ) ; NEW_LINE