text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Function to perform dfs | def dfs ( node , parent , h ) : NEW_LINE |
Store the height of node | height [ node ] = h NEW_LINE for to in graph [ node ] : NEW_LINE INDENT if ( to == parent ) : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( to , node , h + 1 ) NEW_LINE DEDENT |
Function to find the nodes at prime height | def primeHeightNode ( N ) : NEW_LINE |
To precompute prime number till 10 ^ 5 | SieveOfEratosthenes ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE |
Check if height [ node ] is prime | if ( Prime [ height [ i ] ] ) : NEW_LINE INDENT print ( i , end = ' β ' ) NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Number of nodes | N = 5 NEW_LINE |
Edges of the tree | graph [ 1 ] . append ( 2 ) NEW_LINE graph [ 1 ] . append ( 3 ) NEW_LINE graph [ 2 ] . append ( 4 ) NEW_LINE graph [ 2 ] . append ( 5 ) NEW_LINE dfs ( 1 , 1 , 0 ) NEW_LINE primeHeightNode ( N ) NEW_LINE |
Python3 program to find all prime adam numbers in the given range | def reverse ( a ) : NEW_LINE INDENT rev = 0 ; NEW_LINE while ( a != 0 ) : NEW_LINE INDENT r = a % 10 ; NEW_LINE DEDENT DEDENT |
Reversing a number by taking remainder at a time | rev = rev * 10 + r ; NEW_LINE a = a // 10 ; NEW_LINE return ( rev ) ; NEW_LINE |
Function to check if a number is a prime or not | def prime ( a ) : NEW_LINE INDENT k = 0 ; NEW_LINE DEDENT |
Iterating till the number | for i in range ( 2 , a ) : NEW_LINE |
Checking for factors | if ( a % i == 0 ) : NEW_LINE INDENT k = 1 ; NEW_LINE break ; NEW_LINE DEDENT |
Returning 1 if the there are no factors of the number other than 1 or itself | if ( k == 1 ) : NEW_LINE INDENT return ( 0 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( 1 ) ; NEW_LINE DEDENT |
Function to check whether a number is an adam number or not | def adam ( a ) : NEW_LINE |
Reversing given number | r1 = reverse ( a ) ; NEW_LINE |
Squaring given number | s1 = a * a ; NEW_LINE |
Squaring reversed number | s2 = r1 * r1 ; NEW_LINE |
Reversing the square of the reversed number | r2 = reverse ( s2 ) ; NEW_LINE |
Checking if the square of the number and the square of its reverse are equal or not | if ( s1 == r2 ) : NEW_LINE INDENT return ( 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( 0 ) ; NEW_LINE DEDENT |
Function to find all the prime adam numbers in the given range | def find ( m , n ) : NEW_LINE |
If the first number is greater than the second number , print invalid | if ( m > n ) : NEW_LINE INDENT print ( " INVALID INPUT " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT c = 0 ; NEW_LINE DEDENT |
Iterating through all the numbers in the given range | for i in range ( m , n ) : NEW_LINE |
Checking for prime number | l = prime ( i ) ; NEW_LINE |
Checking for Adam number | k = adam ( i ) ; NEW_LINE if ( ( l == 1 ) and ( k == 1 ) ) : NEW_LINE INDENT print ( i , " TABSYMBOL " , end = " β " ) ; NEW_LINE DEDENT |
Driver code | L = 5 ; R = 100 ; NEW_LINE find ( L , R ) ; NEW_LINE |
Function to get sum of digits of a number | def sumDig ( n ) : NEW_LINE INDENT s = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT s = s + int ( n % 10 ) NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return s NEW_LINE DEDENT |
Function to check if the number is peculiar | def Pec ( n ) : NEW_LINE |
Store a duplicate of n | dup = n NEW_LINE dig = sumDig ( n ) NEW_LINE if ( dig * 3 == dup ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT |
Driver code | n = 36 NEW_LINE if Pec ( n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Function to calculate the sum of digits | def digit_sum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE DEDENT |
Loop to iterate through every digit of the number | while ( n > 0 ) : NEW_LINE INDENT m = n % 10 ; NEW_LINE sum = sum + m ; NEW_LINE n = n // 10 NEW_LINE DEDENT |
Returning the sum of digits | return ( sum ) NEW_LINE |
Function to calculate the reverse of a number | def reverse ( n ) : NEW_LINE INDENT r = 0 NEW_LINE DEDENT |
Loop to calculate the reverse of the number | while ( n != 0 ) : NEW_LINE INDENT r = r * 10 NEW_LINE r = r + n % 10 NEW_LINE n = n // 10 NEW_LINE DEDENT |
Return the reverse of the number | return ( r ) NEW_LINE |
Function to print the first N numbers such that every number and the reverse of the number is divisible by its sum of digits | def operation ( n ) : NEW_LINE INDENT i = 1 NEW_LINE count = 0 NEW_LINE DEDENT |
Loop to continuously check and generate number until there are n outputs | while ( count < n ) : NEW_LINE |
Variable to hold the sum of the digit of the number | a = digit_sum ( i ) NEW_LINE |
Computing the reverse of the number | r = reverse ( i ) NEW_LINE |
Checking if the condition satisfies . Increment the count and print the number if it satisfies . | if ( i % a == 0 and r % a == 0 ) : NEW_LINE INDENT print ( i , end = " β " ) NEW_LINE count += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE operation ( n ) NEW_LINE DEDENT |
Function to create and print the two sets | def createSets ( N ) : NEW_LINE |
No such split possible for N <= 2 | if ( N <= 2 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT |
Print the first set consisting of even elements | for i in range ( 2 , N + 1 , 2 ) : NEW_LINE INDENT print ( i , end = " β " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE |
Print the second set consisting of odd ones | for i in range ( 1 , N + 1 , 2 ) : NEW_LINE INDENT print ( i , end = " β " ) ; NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE createSets ( N ) ; NEW_LINE DEDENT |
Python3 implementation to Count the Nodes in the given tree whose weight is a powerful number | graph = [ [ ] for i in range ( 100 ) ] NEW_LINE weight = [ 0 ] * 100 NEW_LINE ans = 0 NEW_LINE |
Function to check if the number is powerful | def isPowerful ( n ) : NEW_LINE |
First divide the number repeatedly by 2 | while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 ; NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n /= 2 ; NEW_LINE power += 1 ; NEW_LINE DEDENT DEDENT |
Check if only 2 ^ 1 divides n , then return False | if ( power == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT |
Check if n is not a power of 2 then this loop will execute | factor = 3 NEW_LINE while ( factor * factor <= n ) : NEW_LINE |
Find highest power of " factor " that divides n | power = 0 ; NEW_LINE while ( n % factor == 0 ) : NEW_LINE INDENT n = n / factor ; NEW_LINE power += 1 ; NEW_LINE DEDENT |
Check if only factor ^ 1 divides n , then return False | if ( power == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT factor += 2 ; NEW_LINE |
n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return False if n is not 1. | return ( n == 1 ) ; NEW_LINE |
Function to perform dfs | def dfs ( Node , parent ) : NEW_LINE |
Check if weight of the current Node is a powerful number | global ans ; NEW_LINE if ( isPowerful ( weight [ Node ] ) ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT for to in graph [ Node ] : NEW_LINE INDENT if ( to == parent ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT dfs ( to , Node ) ; NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Weights of the Node | weight [ 1 ] = 5 ; NEW_LINE weight [ 2 ] = 10 ; NEW_LINE weight [ 3 ] = 11 ; NEW_LINE weight [ 4 ] = 8 ; NEW_LINE weight [ 5 ] = 6 ; NEW_LINE |
Edges of the tree | graph [ 1 ] . append ( 2 ) ; NEW_LINE graph [ 2 ] . append ( 3 ) ; NEW_LINE graph [ 2 ] . append ( 4 ) ; NEW_LINE graph [ 1 ] . append ( 5 ) ; NEW_LINE dfs ( 1 , 1 ) ; NEW_LINE print ( ans ) ; NEW_LINE |
Function to compute all way to fill the boundary of all sides of the unit square | def CountWays ( N , M ) : NEW_LINE INDENT count = 1 NEW_LINE DEDENT |
Count possible ways to fill all upper and left side of the rectangle M * N | count = pow ( 3 , M + N ) NEW_LINE |
Count possible ways to fill all side of the all squares unit size | count *= pow ( 2 , M * N ) ; NEW_LINE return count NEW_LINE |
Number of rows | N = 3 NEW_LINE |
Number of columns | M = 2 NEW_LINE print ( CountWays ( N , M ) ) NEW_LINE |
Return Nth number with absolute difference between all adjacent digits at most 1. | def findNthNumber ( N ) : NEW_LINE |
To store all such numbers | arr = [ 0 for i in range ( N + 1 ) ] NEW_LINE q = [ ] NEW_LINE |
Enqueue all integers from 1 to 9 in increasing order . | for i in range ( 1 , 10 , 1 ) : NEW_LINE INDENT q . append ( i ) NEW_LINE DEDENT |
Perform the operation N times so that we can get all such N numbers . | for i in range ( 1 , N + 1 , 1 ) : NEW_LINE |
Store the front element of queue , in array and pop it from queue . | arr [ i ] = q [ 0 ] NEW_LINE q . remove ( q [ 0 ] ) NEW_LINE |
If the last digit of dequeued integer is not 0 , then enqueue the next such number . | if ( arr [ i ] % 10 != 0 ) : NEW_LINE INDENT q . append ( arr [ i ] * 10 + arr [ i ] % 10 - 1 ) NEW_LINE DEDENT |
Enqueue the next such number | q . append ( arr [ i ] * 10 + arr [ i ] % 10 ) NEW_LINE |
If the last digit of dequeued integer is not 9 , then enqueue the next such number . | if ( arr [ i ] % 10 != 9 ) : NEW_LINE INDENT q . append ( arr [ i ] * 10 + arr [ i ] % 10 + 1 ) NEW_LINE DEDENT print ( arr [ N ] ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 21 NEW_LINE findNthNumber ( N ) NEW_LINE DEDENT |
Function that find the unique element in the array arr [ ] | def findUniqueElements ( arr , N , K ) : NEW_LINE |
Store all unique element in set | s = set ( ) NEW_LINE for x in arr : NEW_LINE INDENT s . add ( x ) NEW_LINE DEDENT |
Sum of all element of the array | arr_sum = sum ( arr ) NEW_LINE |
Sum of element in the set | set_sum = 0 NEW_LINE for x in s : NEW_LINE INDENT set_sum += x NEW_LINE DEDENT |
Print the unique element using formula | print ( ( K * set_sum - arr_sum ) // ( K - 1 ) ) NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE DEDENT |
Function call | findUniqueElements ( arr , N , K ) NEW_LINE |
Function to find the cubic equation whose roots are a , b and c | def findEquation ( a , b , c ) : NEW_LINE |
Find the value of coefficient | X = ( a + b + c ) ; NEW_LINE Y = ( a * b ) + ( b * c ) + ( c * a ) ; NEW_LINE Z = ( a * b * c ) ; NEW_LINE |
Print the equation as per the above coefficients | print ( " x ^ 3 β - β " , X , " x ^ 2 β + β " , Y , " x β - β " , Z , " β = β 0" ) ; NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 ; NEW_LINE b = 2 ; NEW_LINE c = 3 ; NEW_LINE DEDENT |
Function Call | findEquation ( a , b , c ) ; NEW_LINE |
Python3 program to implement Gill 's method | from math import sqrt NEW_LINE |
A sample differential equation " dy / dx β = β ( x β - β y ) /2" | def dydx ( x , y ) : NEW_LINE INDENT return ( x - y ) / 2 NEW_LINE DEDENT |
Finds value of y for a given x using step size h and initial value y0 at x0 | def Gill ( x0 , y0 , x , h ) : NEW_LINE |
Count number of iterations using step size or height h | n = ( ( x - x0 ) / h ) NEW_LINE |
Initial value of y ( 0 ) | y = y0 NEW_LINE |
Iterate for number of iteration | for i in range ( 1 , int ( n + 1 ) , 1 ) : NEW_LINE |
Value of K1 | k1 = h * dydx ( x0 , y ) NEW_LINE |
Value of K2 | k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) NEW_LINE |
Value of K3 | k3 = h * dydx ( x0 + 0.5 * h , y + 0.5 * ( - 1 + sqrt ( 2 ) ) * k1 + k2 * ( 1 - 0.5 * sqrt ( 2 ) ) ) NEW_LINE |
Value of K4 | k4 = h * dydx ( x0 + h , y - ( 0.5 * sqrt ( 2 ) ) * k2 + k3 * ( 1 + 0.5 * sqrt ( 2 ) ) ) NEW_LINE |
Find the next value of y ( n + 1 ) using y ( n ) and values of K in the above steps | y = y + ( 1 / 6 ) * ( k1 + ( 2 - sqrt ( 2 ) ) * k2 + ( 2 + sqrt ( 2 ) ) * k3 + k4 ) NEW_LINE |
Update next value of x | x0 = x0 + h NEW_LINE |
Return the final value of dy / dx | return y NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x0 = 0 NEW_LINE y = 3.0 NEW_LINE x = 5.0 NEW_LINE h = 0.2 NEW_LINE print ( " y ( x ) β = " , round ( Gill ( x0 , y , x , h ) , 6 ) ) NEW_LINE DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.