text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Possible value of Q | val_Q . append ( X + i ) NEW_LINE print ( len ( val_Q ) ) NEW_LINE |
Print all the numbers | for i in range ( len ( val_Q ) ) : NEW_LINE INDENT print ( val_Q [ i ] , end = " ▁ " ) NEW_LINE DEDENT |
Driver Code | X = 3 NEW_LINE values_of_Q ( X ) NEW_LINE |
Function that finds gcd of 2 strings | def gcd ( str1 , str2 ) : NEW_LINE |
If str1 length is less than that of str2 then recur with gcd ( str2 , str1 ) | if ( len ( str1 ) < len ( str2 ) ) : NEW_LINE INDENT return gcd ( str2 , str1 ) NEW_LINE DEDENT |
If str1 is not the concatenation of str2 | elif ( not str1 . startswith ( str2 ) ) : NEW_LINE INDENT return " " NEW_LINE DEDENT elif ( len ( str2 ) == 0 ) : NEW_LINE |
GCD string is found | return str1 NEW_LINE else : NEW_LINE |
Cut off the common prefix part of str1 & then recur | return gcd ( str1 [ len ( str2 ) : ] , str2 ) NEW_LINE |
Function to find GCD of array of strings | def findGCD ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT result = gcd ( result , arr [ i ] ) NEW_LINE DEDENT DEDENT |
Return the GCD of strings | return result NEW_LINE |
Given array of strings | arr = [ " GFGGFG " , " GFGGFG " , " GFGGFGGFGGFG " ] NEW_LINE n = len ( arr ) NEW_LINE |
Function Call | print ( findGCD ( arr , n ) ) NEW_LINE |
Define the size of the matrix | N = 4 NEW_LINE M = 4 NEW_LINE |
Function to check given matrix balanced or unbalanced | def balancedMatrix ( mat ) : NEW_LINE |
Flag for check matrix is balanced or unbalanced | is_balanced = True NEW_LINE |
Iterate row until condition is true | i = 0 NEW_LINE while i < N and is_balanced : NEW_LINE |
Iterate cols until condition is true | j = 0 NEW_LINE while j < N and is_balanced : NEW_LINE |
Check for corner edge elements | if ( ( i == 0 or i == N - 1 ) and ( j == 0 or j == M - 1 ) ) : NEW_LINE INDENT if mat [ i ] [ j ] >= 2 : NEW_LINE INDENT isbalanced = False NEW_LINE DEDENT DEDENT |
Check for border elements | elif ( i == 0 or i == N - 1 or j == 0 or j == M - 1 ) : NEW_LINE INDENT if mat [ i ] [ j ] >= 3 : NEW_LINE INDENT is_balanced = False NEW_LINE DEDENT DEDENT |
Check for the middle ones | else : NEW_LINE INDENT if mat [ i ] [ j ] >= 4 : NEW_LINE INDENT is_balanced = False NEW_LINE DEDENT DEDENT j += 1 NEW_LINE i += 1 NEW_LINE |
Return balanced or not | if is_balanced : NEW_LINE INDENT return " Balanced " NEW_LINE DEDENT else : NEW_LINE INDENT return " Unbalanced " NEW_LINE DEDENT |
Given matrix mat [ ] [ ] | mat = [ [ 1 , 2 , 3 , 4 ] , [ 3 , 5 , 2 , 6 ] , [ 5 , 3 , 6 , 1 ] , [ 9 , 5 , 6 , 0 ] ] NEW_LINE |
Function call | print ( balancedMatrix ( mat ) ) NEW_LINE |
Function to convert unix time to Human readable format | def unixTimeToHumanReadable ( seconds ) : NEW_LINE |
Save the time in Human readable format | ans = " " NEW_LINE |
Number of days in month in normal year | daysOfMonth = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] NEW_LINE ( currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag ) = ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) NEW_LINE |
Calculate total days unix time T | daysTillNow = seconds // ( 24 * 60 * 60 ) NEW_LINE extraTime = seconds % ( 24 * 60 * 60 ) NEW_LINE currYear = 1970 NEW_LINE |
Calculating current year | while ( daysTillNow >= 365 ) : NEW_LINE INDENT if ( currYear % 400 == 0 or ( currYear % 4 == 0 and currYear % 100 != 0 ) ) : NEW_LINE INDENT daysTillNow -= 366 NEW_LINE DEDENT else : NEW_LINE INDENT daysTillNow -= 365 NEW_LINE DEDENT currYear += 1 NEW_LINE DEDENT |
Updating extradays because it will give days till previous day and we have include current day | extraDays = daysTillNow + 1 NEW_LINE if ( currYear % 400 == 0 or ( currYear % 4 == 0 and currYear % 100 != 0 ) ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT |
Calculating MONTH and DATE | month = 0 NEW_LINE index = 0 NEW_LINE if ( flag == 1 ) : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT if ( index == 1 ) : NEW_LINE INDENT if ( extraDays - 29 < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT month += 1 NEW_LINE extraDays -= 29 NEW_LINE DEDENT else : NEW_LINE INDENT if ( extraDays - daysOfMonth [ index ] < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT month += 1 NEW_LINE extraDays -= daysOfMonth [ index ] NEW_LINE DEDENT index += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT if ( extraDays - daysOfMonth [ index ] < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT month += 1 NEW_LINE extraDays -= daysOfMonth [ index ] NEW_LINE index += 1 NEW_LINE DEDENT DEDENT |
Current Month | if ( extraDays > 0 ) : NEW_LINE INDENT month += 1 NEW_LINE date = extraDays NEW_LINE DEDENT else : NEW_LINE INDENT if ( month == 2 and flag == 1 ) : NEW_LINE INDENT date = 29 NEW_LINE DEDENT else : NEW_LINE INDENT date = daysOfMonth [ month - 1 ] NEW_LINE DEDENT DEDENT |
Calculating HH : MM : YYYY | hours = extraTime // 3600 NEW_LINE minutes = ( extraTime % 3600 ) // 60 NEW_LINE secondss = ( extraTime % 3600 ) % 60 NEW_LINE ans += str ( date ) NEW_LINE ans += " / " NEW_LINE ans += str ( month ) NEW_LINE ans += " / " NEW_LINE ans += str ( currYear ) NEW_LINE ans += " ▁ " NEW_LINE ans += str ( hours ) NEW_LINE ans += " : " NEW_LINE ans += str ( minutes ) NEW_LINE ans += " : " NEW_LINE ans += str ( secondss ) NEW_LINE |
Return the time | return ans NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Given unix time | T = 1595497956 NEW_LINE |
Function call to convert unix time to human read able | ans = unixTimeToHumanReadable ( T ) NEW_LINE |
Print time in format DD : MM : YYYY : HH : MM : SS | print ( ans ) NEW_LINE |
Function to find area of rectangle inscribed another rectangle of length L and width W | def AreaofRectangle ( L , W ) : NEW_LINE |
Area of rectangle | INDENT area = ( W + L ) * ( W + L ) / 2 NEW_LINE DEDENT |
Return the area | INDENT return area NEW_LINE DEDENT |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Given Dimensions | INDENT L = 18 NEW_LINE W = 12 NEW_LINE DEDENT |
Function Call | INDENT print ( AreaofRectangle ( L , W ) ) NEW_LINE DEDENT |
Python3 program to implement the above approach | import math NEW_LINE import sys NEW_LINE |
Function to count the minimum steps required to reduce n | def downToZero ( n ) : NEW_LINE |
Base case | if ( n <= 3 ) : NEW_LINE INDENT return n NEW_LINE DEDENT |
Allocate memory for storing intermediate results | dp = [ - 1 ] * ( n + 1 ) NEW_LINE |
Store base values | dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = 1 NEW_LINE dp [ 2 ] = 2 NEW_LINE dp [ 3 ] = 3 NEW_LINE |
Stores square root of each number | for i in range ( 4 , n + 1 ) : NEW_LINE |
Compute square root | sqr = ( int ) ( math . sqrt ( i ) ) NEW_LINE best = sys . maxsize NEW_LINE |
Use rule 1 to find optimized answer | while ( sqr > 1 ) : NEW_LINE |
Check if it perfectly divides n | if ( i % sqr == 0 ) : NEW_LINE INDENT best = min ( best , 1 + dp [ sqr ] ) NEW_LINE DEDENT sqr -= 1 NEW_LINE |
Use of rule 2 to find the optimized answer | best = min ( best , 1 + dp [ i - 1 ] ) NEW_LINE |
Store computed value | dp [ i ] = best NEW_LINE |
Return answer | return dp [ n ] NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( downToZero ( n ) ) NEW_LINE DEDENT |
Function to find the minimum steps required to reduce n | def downToZero ( n ) : NEW_LINE |
Base case | if ( n <= 3 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT |
Return answer based on parity of n | if ( n % 2 == 0 ) : NEW_LINE INDENT return 3 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 4 ; NEW_LINE DEDENT |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE print ( downToZero ( n ) ) ; NEW_LINE DEDENT |
Function to find minimum number of elements required to form A [ ] by performing mirroring operation | def minimumrequired ( A , N ) : NEW_LINE |
Initialize K | K = N NEW_LINE while ( K > 0 ) : NEW_LINE |
Odd length array cannot be formed by mirror operation | if ( K % 2 ) == 1 : NEW_LINE INDENT ans = K NEW_LINE break NEW_LINE DEDENT ispalindrome = 1 NEW_LINE |
Check if prefix of length K is palindrome | for i in range ( 0 , K // 2 ) : NEW_LINE |
Check if not a palindrome | if ( A [ i ] != A [ K - 1 - i ] ) : NEW_LINE INDENT ispalindrome = 0 NEW_LINE DEDENT |
If found to be palindrome | if ( ispalindrome == 1 ) : NEW_LINE INDENT ans = K // 2 NEW_LINE K = K // 2 NEW_LINE DEDENT |
Otherwise | else : NEW_LINE INDENT ans = K NEW_LINE break NEW_LINE DEDENT |
Return the final answer | return ans NEW_LINE |
Driver code | A = [ 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( minimumrequired ( A , N ) ) NEW_LINE |
Function to find the sum of the product of all the integers and their positive divisors up to N | def sumOfFactors ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE DEDENT |
Iterate for every number between 1 and N | for i in range ( 1 , N + 1 ) : NEW_LINE |
Find the first multiple of i between 1 and N | first = i NEW_LINE |
Find the last multiple of i between 1 and N | last = ( N // i ) * i NEW_LINE |
Find the total count of multiple of in [ 1 , N ] | factors = ( last - first ) // i + 1 NEW_LINE |
Compute the contribution of i using the formula | totalContribution = ( ( ( factors * ( factors + 1 ) ) // 2 ) * i ) NEW_LINE |
Add the contribution of i to the answer | ans += totalContribution NEW_LINE |
Return the result | return ans NEW_LINE |
Given N | N = 3 NEW_LINE |
Function call | print ( sumOfFactors ( N ) ) NEW_LINE |
Function to return M < N such that N ^ M - N & M is maximum | def getMaxDifference ( N ) : NEW_LINE |
Initialize variables | M = - 1 ; NEW_LINE maxDiff = 0 ; NEW_LINE |
Iterate for all values < N | for i in range ( N ) : NEW_LINE |
Find the difference between Bitwise XOR and AND | diff = ( N ^ i ) - ( N & i ) ; NEW_LINE |
Check if new difference is greater than previous maximum | if ( diff >= maxDiff ) : NEW_LINE |
Update variables | maxDiff = diff ; NEW_LINE M = i ; NEW_LINE |
Return the answer | return M ; NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given number N | N = 6 ; NEW_LINE |
Function call | print ( getMaxDifference ( N ) ) ; NEW_LINE |
Python3 program for the above approach | import math NEW_LINE |
Function to flip all bits of N | def findM ( N ) : NEW_LINE INDENT M = 0 ; NEW_LINE DEDENT |
Finding most significant bit of N | MSB = int ( math . log ( N ) ) ; NEW_LINE |
Calculating required number | for i in range ( MSB ) : NEW_LINE INDENT if ( ( N & ( 1 << i ) ) == 0 ) : NEW_LINE INDENT M += ( 1 << i ) ; NEW_LINE DEDENT DEDENT |
Return the answer | return M ; NEW_LINE |
Driver Code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given number | N = 6 ; NEW_LINE |
Function call | print ( findM ( N ) ) ; NEW_LINE |
Matrix of containers | cont = [ [ 0 for i in range ( 1000 ) ] for j in range ( 1000 ) ] NEW_LINE |
Function to find the number of containers that will be filled in X seconds | def num_of_containers ( n , x ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT |
Container on top level | cont [ 1 ] [ 1 ] = x NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE DEDENT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.