text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
initialise the three integers | x = 1 NEW_LINE y = 5 NEW_LINE z = 8 NEW_LINE print ( get ( x , y , z ) ) NEW_LINE |
Function to find conjugate of a complex number | def solve ( s ) : NEW_LINE INDENT z = s NEW_LINE l = len ( s ) NEW_LINE i = 0 NEW_LINE if ( s . find ( ' + ' ) != - 1 ) : NEW_LINE DEDENT |
store index of '- | ' NEW_LINE INDENT i = s . find ( ' - ' ) NEW_LINE s = s . replace ( ' - ' , ' + ' , 1 ) NEW_LINE DEDENT |
print the result | print ( " Conjugate β of β " , z , " β = β " , s ) NEW_LINE |
initialise the complex number | s = "3-4i " NEW_LINE solve ( s ) NEW_LINE |
Python 3 implementation of the above approach | def countOfGreaterElements ( arr , n ) : NEW_LINE |
Store the frequency of the array elements | mp = { i : 0 for i in range ( 1000 ) } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT x = 0 NEW_LINE |
Store the sum of frequency of elements greater than the current element | p = [ ] NEW_LINE q = [ ] NEW_LINE m = [ ] NEW_LINE for key , value in mp . items ( ) : NEW_LINE INDENT m . append ( [ key , value ] ) NEW_LINE DEDENT m = m [ : : - 1 ] NEW_LINE for p in m : NEW_LINE INDENT temp = p [ 1 ] NEW_LINE mp [ p [ 0 ] ] = x NEW_LINE x += temp NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( mp [ arr [ i ] ] , end = " β " ) NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 9 , 5 , 2 , 1 , 3 , 4 , 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE countOfGreaterElements ( arr , n ) NEW_LINE DEDENT |
Python program to find minimum operations required to make two numbers equal | import math NEW_LINE |
Function to return the minimum operations required | def minOperations ( A , B ) : NEW_LINE |
Keeping B always greater | if ( A > B ) : NEW_LINE INDENT swap ( A , B ) NEW_LINE DEDENT |
Reduce B such that gcd ( A , B ) becomes 1. | B = B // math . gcd ( A , B ) ; NEW_LINE return B - 1 NEW_LINE |
Driver code | A = 7 NEW_LINE B = 15 NEW_LINE print ( minOperations ( A , B ) ) NEW_LINE |
Function to determine the quadrant of a complex number | def quadrant ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE DEDENT |
Storing the index of '+ | ' NEW_LINE INDENT if ( ' + ' in s ) : NEW_LINE INDENT i = s . index ( ' + ' ) NEW_LINE DEDENT DEDENT |
Storing the index of '- | ' NEW_LINE INDENT else : NEW_LINE INDENT i = s . index ( ' - ' ) NEW_LINE DEDENT DEDENT |
Finding the real part of the complex number | real = s [ 0 : i ] NEW_LINE |
Finding the imaginary part of the complex number | imaginary = s [ i + 1 : l - 1 ] NEW_LINE x = int ( real ) NEW_LINE y = int ( imaginary ) NEW_LINE if ( x > 0 and y > 0 ) : NEW_LINE INDENT print ( " Quadrant β 1" ) NEW_LINE DEDENT elif ( x < 0 and y > 0 ) : NEW_LINE INDENT print ( " Quadrant β 2" ) NEW_LINE DEDENT elif ( x < 0 and y < 0 ) : NEW_LINE INDENT print ( " Quadrant β 3" ) NEW_LINE DEDENT elif ( x > 0 and y < 0 ) : NEW_LINE INDENT print ( " Quadrant β 4" ) NEW_LINE DEDENT elif ( x == 0 and y > 0 ) : NEW_LINE INDENT print ( " Lies β on β positive " , " Imaginary β axis " ) NEW_LINE DEDENT elif ( x == 0 and y < 0 ) : NEW_LINE INDENT print ( " Lies β on β negative " , " Imaginary β axis " ) NEW_LINE DEDENT elif ( y == 0 and x < 0 ) : NEW_LINE INDENT print ( " Lies β on β negative " , " X - axis " ) NEW_LINE DEDENT elif ( y == 0 and x > 0 ) : NEW_LINE INDENT print ( " Lies β on β positive " , " X - axis " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Lies β on β the β Origin " ) NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "5 + 3i " NEW_LINE quadrant ( s ) NEW_LINE DEDENT |
Python3 implementation to find the sum and product of all of the Fibonacci nodes in a singly linked list | import sys NEW_LINE |
Node of the singly linked list | class Node ( ) : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT |
Function to insert a node at the beginning of the singly Linked List | def push ( head_ref , new_data ) : NEW_LINE |
Allocate new node | new_node = Node ( new_data ) NEW_LINE |
Link the old list to the new node | new_node . next = head_ref NEW_LINE |
Move the head to point the new node | head_ref = new_node NEW_LINE return head_ref NEW_LINE |
Function that returns the largest element from the linked list . | def largestElement ( head_ref ) : NEW_LINE |
Declare a max variable and initialize with INT_MIN | max = - sys . maxsize NEW_LINE head = head_ref NEW_LINE |
Check loop while head not equal to NULL | while ( head != None ) : NEW_LINE |
If max is less then head -> data then assign value of head -> data to max otherwise node points to next node . | if ( max < head . data ) : NEW_LINE INDENT max = head . data NEW_LINE DEDENT head = head . next NEW_LINE return max NEW_LINE |
Function to create a hash table to check Fibonacci numbers | def createHash ( hash , maxElement ) : NEW_LINE |
Inserting the first two numbers in the hash | prev = 0 NEW_LINE curr = 1 NEW_LINE hash . add ( prev ) NEW_LINE hash . add ( curr ) NEW_LINE |
Loop to add Fibonacci numbers upto the maximum element present in the linked list | while ( curr <= maxElement ) : NEW_LINE INDENT temp = curr + prev NEW_LINE hash . add ( temp ) NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE DEDENT return hash NEW_LINE |
Function to find the required sum and product | def sumAndProduct ( head_ref ) : NEW_LINE |
Find the largest node value in Singly Linked List | maxEle = largestElement ( head_ref ) NEW_LINE |
Creating a set containing all the fibonacci numbers upto the maximum data value in the Singly Linked List | hash = set ( ) NEW_LINE hash = createHash ( hash , maxEle ) NEW_LINE prod = 1 NEW_LINE sum = 0 NEW_LINE ptr = head_ref NEW_LINE |
Traverse the linked list | while ( ptr != None ) : NEW_LINE |
If current node is fibonacci | if ptr . data in hash : NEW_LINE |
Find the sum and the product | prod *= ptr . data NEW_LINE sum += ptr . data NEW_LINE ptr = ptr . next NEW_LINE print ( " Sum β = " , sum ) NEW_LINE print ( " Product β = " , prod ) NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT head = None ; NEW_LINE DEDENT |
Create the linked list 15 -> 16 -> 8 -> 6 -> 13 | head = push ( head , 13 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 16 ) NEW_LINE head = push ( head , 15 ) NEW_LINE sumAndProduct ( head ) NEW_LINE |
Function to find product of all subarrays | def product_subarrays ( arr , n ) : NEW_LINE |
Variable to store the product | product = 1 ; NEW_LINE |
Compute the product while traversing for subarrays | for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT for k in range ( i , j + 1 ) : NEW_LINE INDENT product *= arr [ k ] ; NEW_LINE DEDENT DEDENT DEDENT |
Printing product of all subarray | print ( product , " " ) ; NEW_LINE |
Driver code | arr = [ 10 , 3 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE |
Function call | product_subarrays ( arr , n ) ; NEW_LINE |
To return value of a char . | def val ( c ) : NEW_LINE INDENT if ( ord ( c ) >= ord ( '0' ) and ord ( c ) <= ord ( '9' ) ) : NEW_LINE INDENT return ord ( c ) - ord ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT return ord ( c ) - ord ( ' A ' ) + 10 NEW_LINE DEDENT DEDENT |
Function to convert a number from N base to decimal | def toDeci ( str , base ) : NEW_LINE INDENT Len = len ( str ) NEW_LINE DEDENT |
power of base | power = 1 NEW_LINE num = 0 NEW_LINE |
Decimal equivaLent is str [ Len - 1 ] * 1 + str [ Len - 1 ] * base + str [ Len - 1 ] * ( base ^ 2 ) + ... | for i in range ( Len - 1 , - 1 , - 1 ) : NEW_LINE |
A digit in input number must be less than number 's base | if ( val ( str [ i ] ) >= base ) : NEW_LINE INDENT print ( " Invalid β Number " ) NEW_LINE return - 1 NEW_LINE DEDENT num += val ( str [ i ] ) * power NEW_LINE power = power * base NEW_LINE return num NEW_LINE |
Returns true if n is even , else odd | def isEven ( num , N ) : NEW_LINE INDENT deci = toDeci ( num , N ) NEW_LINE return ( deci % 2 == 0 ) NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = "11A " NEW_LINE N = 16 NEW_LINE if ( isEven ( num , N ) ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT DEDENT |
Array that stores the factorial till 20 | fact = [ 0 ] * 21 NEW_LINE |
Function to pre - compute the factorial till 20 | def preCompute ( ) : NEW_LINE |
Precomputing factorials | fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , 18 ) : NEW_LINE INDENT fact [ i ] = ( fact [ i - 1 ] * i ) NEW_LINE DEDENT |
Function to return the next factorial number greater than N | def nextFactorial ( N ) : NEW_LINE |
Traverse the factorial array | for i in range ( 21 ) : NEW_LINE |
Find the next just greater factorial than N | if N < fact [ i ] : NEW_LINE INDENT print ( fact [ i ] ) NEW_LINE break NEW_LINE DEDENT |
Function to precalculate the factorial till 20 | preCompute ( ) NEW_LINE N = 120 NEW_LINE |
Function call | nextFactorial ( N ) NEW_LINE |
Function to find K odd positive integers such that their summ is N | def findDistinctOddsumm ( n , k ) : NEW_LINE |
Condition to check if there are enough values to check | if ( ( k * k ) <= n and ( n + k ) % 2 == 0 ) : NEW_LINE INDENT val = 1 NEW_LINE summ = 0 NEW_LINE for i in range ( 1 , k ) : NEW_LINE INDENT print ( val , end = " β " ) NEW_LINE summ += val NEW_LINE val += 2 NEW_LINE DEDENT print ( n - summ ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT |
Driver Code | n = 100 NEW_LINE k = 4 NEW_LINE findDistinctOddsumm ( n , k ) NEW_LINE |
Function to find the minimum number of operations in which array A can be converted to array B | def checkArray ( a , b , n ) : NEW_LINE INDENT operations = 0 ; NEW_LINE i = 0 ; NEW_LINE DEDENT |
Loop to iterate over the array | while ( i < n ) : NEW_LINE |
if both elements are equal then move to next element | if ( a [ i ] - b [ i ] == 0 ) : NEW_LINE INDENT i += 1 ; NEW_LINE continue ; NEW_LINE DEDENT |
Calculate the difference between two elements | diff = a [ i ] - b [ i ] ; NEW_LINE i += 1 ; NEW_LINE |
loop while the next pair of elements have same difference | while ( i < n and a [ i ] - b [ i ] == diff ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT |
Increase the number of operations by 1 | operations += 1 ; NEW_LINE |
Print the number of operations required | print ( operations ) ; NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 3 , 7 , 1 , 4 , 1 , 2 ] ; NEW_LINE b = [ 3 , 7 , 3 , 6 , 3 , 2 ] ; NEW_LINE size = len ( a ) ; NEW_LINE checkArray ( a , b , size ) ; NEW_LINE DEDENT |
Python3 program to check if a number is a perfect cube using prime factors | import math NEW_LINE |
Inserts the prime factor in HashMap if not present if present updates it 's frequency | def insertPF ( primeFact , fact ) : NEW_LINE INDENT if ( fact in primeFact ) : NEW_LINE INDENT primeFact [ fact ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT primeFact [ fact ] = 1 NEW_LINE DEDENT return primeFact NEW_LINE DEDENT |
A utility function to find all prime factors of a given number N | def primeFactors ( n ) : NEW_LINE INDENT primeFact = { } NEW_LINE DEDENT |
Insert the number of 2 s that divide n | while ( n % 2 == 0 ) : NEW_LINE INDENT primeFact = insertPF ( primeFact , 2 ) NEW_LINE n = n // 2 NEW_LINE DEDENT |
n must be odd at this point So we can skip one element | for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE |
while i divides n , insert i and divide n | while ( n % i == 0 ) : NEW_LINE INDENT primeFact = insertPF ( primeFact , i ) NEW_LINE n = n // i NEW_LINE DEDENT |
This condition is to handle the case when n is a prime number greater than 2 | if ( n > 2 ) : NEW_LINE INDENT primeFact = insertPF ( primeFact , n ) NEW_LINE DEDENT return primeFact NEW_LINE |
Function to check if a number is perfect cube | def perfectCube ( n ) : NEW_LINE INDENT primeFact = { } NEW_LINE primeFact = primeFactors ( n ) NEW_LINE DEDENT |
Iteration in Map | for x in primeFact : NEW_LINE INDENT if ( primeFact [ x ] % 3 != 0 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT return " Yes " NEW_LINE |
Driver Code | N = 216 NEW_LINE |
Function to check if N is perfect cube or not | print ( perfectCube ( N ) ) NEW_LINE |
Python3 implementation to find the number the number of ways to reach Nth stair by taking 1 or 2 steps at a time and 3 rd Step exactly once | import math NEW_LINE |
Function to find the number of ways | def ways ( n ) : NEW_LINE |
Base Case | if n < 3 : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
Count of 2 - steps | c2 = 0 NEW_LINE |
Count of 1 - steps | c1 = n - 3 NEW_LINE |
Initial length of sequence | l = c1 + 1 NEW_LINE s = 0 NEW_LINE |
expected count of 2 - steps | exp_c2 = c1 / 2 NEW_LINE |
Loop to find the ways for every possible sequence | while exp_c2 >= c2 : NEW_LINE INDENT f1 = math . factorial ( l ) NEW_LINE f2 = math . factorial ( c1 ) NEW_LINE f3 = math . factorial ( c2 ) NEW_LINE s += f1 // ( f2 * f3 ) NEW_LINE c2 += 1 NEW_LINE c1 -= 2 NEW_LINE l -= 1 NEW_LINE DEDENT return s NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE print ( ways ( N ) ) NEW_LINE DEDENT |
Map to precompute and store the factorials of the numbers | m = { } ; NEW_LINE |
Function to precompute factorial | def precompute ( ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 1 , 19 ) : NEW_LINE DEDENT |
Calculating the factorial for each i and storing in a map | fact = fact * i ; NEW_LINE m [ fact ] = i ; NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Precomputing the factorials | precompute ( ) ; NEW_LINE K = 120 ; NEW_LINE print ( m [ K ] ) ; NEW_LINE K = 6 ; NEW_LINE print ( m [ K ] ) ; NEW_LINE |
Function to calculate the number of leap years in range of ( 1 , year ) | def calNum ( year ) : NEW_LINE INDENT return ( year // 4 ) - ( year // 100 ) + ( year // 400 ) ; NEW_LINE DEDENT |
Function to calculate the number of leap years in given range | def leapNum ( l , r ) : NEW_LINE INDENT l -= 1 ; NEW_LINE num1 = calNum ( r ) ; NEW_LINE num2 = calNum ( l ) ; NEW_LINE print ( num1 - num2 ) ; NEW_LINE DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.