codeparrot/xlcost-text-to-code huggingface dataset:
Find the number of integers from 1 to n which contains digits 0 ' s ▁ and ▁ 1' s only | Function to find the number of integers from 1 to n which contains 0 ' s ▁ and ▁ 1' s only ; If number is greater than n ; otherwise add count this number and call two functions ; Driver code
def countNumbers ( x , n ) : NEW_LINE INDENT if x > n : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 120 ; NEW_LINE print ( countNumbers ( 1 , n ) ) ; NEW_LINE DEDENT
Check if factorial of N is divisible by the sum of squares of first N natural numbers | Python 3 implementation of the approach ; Function to count number of times prime P divide factorial N ; Lengendre Formula ; Function to find count number of times all prime P divide summation ; Formula for summation of square after removing n and constant 6 ; Loop to traverse over all prime P which divide summation ; If Number itself is a Prime Number ; Driver Code
from math import sqrt NEW_LINE def checkfact ( N , countprime , prime ) : NEW_LINE INDENT countfact = 0 NEW_LINE if ( prime == 2 or prime == 3 ) : NEW_LINE INDENT countfact += 1 NEW_LINE DEDENT divide = prime NEW_LINE while ( int ( N / divide ) != 0 ) : NEW_LINE INDENT countfact += int ( N / divide ) NEW_LINE divide = divide * divide NEW_LINE DEDENT if ( countfact >= countprime ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def check ( N ) : NEW_LINE INDENT sumsquares = ( N + 1 ) * ( 2 * N + 1 ) NEW_LINE countprime = 0 NEW_LINE for i in range ( 2 , int ( sqrt ( sumsquares ) ) + 1 , 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE while ( sumsquares % i == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE countprime += 1 NEW_LINE sumsquares /= i NEW_LINE DEDENT if ( flag ) : NEW_LINE INDENT if ( checkfact ( N - 1 , countprime , i ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT countprime = 0 NEW_LINE DEDENT DEDENT if ( sumsquares != 1 ) : NEW_LINE INDENT if ( checkfact ( N - 1 , 1 , sumsquares ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE if ( check ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count the number of non | Python3 program to count number of non increasing subarrays ; Initialize result ; Initialize length of current non - increasing subarray ; Traverse through the array ; If arr [ i + 1 ] is less than or equal to arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver code
def countNonIncreasing ( arr , n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE len = 1 ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i + 1 ] <= arr [ i ] ) : NEW_LINE INDENT len += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT cnt += ( ( ( len + 1 ) * len ) / 2 ) ; NEW_LINE len = 1 ; NEW_LINE DEDENT DEDENT if ( len > 1 ) : NEW_LINE INDENT cnt += ( ( ( len + 1 ) * len ) / 2 ) ; NEW_LINE DEDENT return int ( cnt ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 2 , 3 , 7 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countNonIncreasing ( arr , n ) ) ; NEW_LINE DEDENT
Minimum array elements to be changed to make Recaman 's sequence | Python3 implementation of the approach ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula ; If arr [ i - 1 ] - i is negative or already exists ; Function that returns minimum changes required ; Set to store first n Recaman numbers ; Generate and store first n Recaman numbers ; Insert first n Recaman numbers to set ; If current element of the array is present in the set ; Return the remaining number of elements in the set ; Driver code
def recamanGenerator ( arr , n ) : NEW_LINE INDENT arr [ 0 ] = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT temp = arr [ i - 1 ] - i NEW_LINE j = 0 NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( ( arr [ j ] == temp ) or temp < 0 ) : NEW_LINE INDENT temp = arr [ i - 1 ] + i NEW_LINE break NEW_LINE DEDENT DEDENT arr [ i ] = temp NEW_LINE DEDENT DEDENT def recamanArray ( arr , n ) : NEW_LINE INDENT s = dict ( ) NEW_LINE recaman = [ 0 for i in range ( n ) ] NEW_LINE recamanGenerator ( recaman , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s [ recaman [ i ] ] = s . get ( recaman [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in s . keys ( ) : NEW_LINE INDENT del s [ arr [ i ] ] NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT arr = [ 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( recamanArray ( arr , n ) ) NEW_LINE
Check if product of array containing prime numbers is a perfect square | Function that returns true if the product of all the array elements is a perfect square ; Update the frequencies of all the array elements ; If frequency of some element in the array is odd ; Driver code
def isPerfectSquare ( arr , n ) : NEW_LINE INDENT umap = dict . fromkeys ( arr , n ) ; NEW_LINE for key in arr : NEW_LINE INDENT umap [ key ] += 1 ; NEW_LINE DEDENT for key in arr : NEW_LINE INDENT if ( umap [ key ] % 2 == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 2 , 7 , 7 ] ; NEW_LINE n = len ( arr ) NEW_LINE if ( isPerfectSquare ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Find the good permutation of first N natural numbers | Function to print the good permutation of first N natural numbers ; If n is odd ; Otherwise ; Driver code
def printPermutation ( n ) : NEW_LINE INDENT if ( n % 2 != 0 ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 1 , ( n // 2 ) + 1 ) : NEW_LINE INDENT print ( ( 2 * i ) , ( 2 * i - 1 ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT n = 4 ; NEW_LINE printPermutation ( n ) ; NEW_LINE
Minimum number operations required to convert n to m | Set | Python 3 implementation of the above approach ; Function to find the minimum number of steps ; If n exceeds M ; If N reaches the target ; The minimum of both the states will be the answer ; Driver code
MAXN = 10000000 NEW_LINE def minimumSteps ( n , m , a , b ) : NEW_LINE INDENT if ( n > m ) : NEW_LINE INDENT return MAXN NEW_LINE DEDENT if ( n == m ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return min ( 1 + minimumSteps ( n * a , m , a , b ) , 1 + minimumSteps ( n * b , m , a , b ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 120 NEW_LINE m = 51840 NEW_LINE a = 2 NEW_LINE b = 3 NEW_LINE print ( minimumSteps ( n , m , a , b ) ) NEW_LINE DEDENT
Minimum number of given operation required to convert n to m | Function to return the minimum operations required ; Counting all 2 s ; Counting all 3 s ; If q contained only 2 and 3 as the only prime factors then it must be 1 now ; Driver code
def minOperations ( n , m ) : NEW_LINE INDENT if ( m % n != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT minOperations = 0 NEW_LINE q = int ( m / n ) NEW_LINE while ( q % 2 == 0 ) : NEW_LINE INDENT q = int ( q / 2 ) NEW_LINE minOperations += 1 NEW_LINE DEDENT while ( q % 3 == 0 ) : NEW_LINE INDENT q = int ( q / 3 ) NEW_LINE minOperations += 1 NEW_LINE DEDENT if ( q == 1 ) : NEW_LINE INDENT return minOperations NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 120 NEW_LINE m = 51840 NEW_LINE print ( minOperations ( n , m ) ) NEW_LINE DEDENT
Sum of Fibonacci Numbers in a range | Function to return the nth Fibonacci number ; Function to return the required sum ; To store the sum ; Calculate the sum ; Driver Code
def fib ( n ) : NEW_LINE INDENT phi = ( ( 1 + ( 5 ** ( 1 / 2 ) ) ) / 2 ) ; NEW_LINE return round ( ( phi ** n ) / ( 5 ** ( 1 / 2 ) ) ) ; NEW_LINE DEDENT def calculateSum ( l , r ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT sum += fib ( i ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l , r = 4 , 8 ; NEW_LINE print ( calculateSum ( l , r ) ) ; NEW_LINE DEDENT
Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone | Program to find the biggest sphere which is inscribed within a cube which in turn inscribed within a right circular cone ; Function to find the radius of the sphere ; height and radius cannot be negative ; radius of the sphere ; Driver code
import math NEW_LINE def sphereSide ( h , r ) : NEW_LINE INDENT if h < 0 and r < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT R = ( ( ( h * r * math . sqrt ( 2 ) ) ) / ( h + math . sqrt ( 2 ) * r ) / 2 ) NEW_LINE return R NEW_LINE DEDENT h = 5 ; r = 6 NEW_LINE print ( sphereSide ( h , r ) ) NEW_LINE
Find the number of ways to divide number into four parts such that a = c and b = d | Function to find the number of ways to divide N into four parts such that a = c and b = d ; Driver code
def possibleways ( n ) : NEW_LINE INDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT elif ( n % 4 == 0 ) : NEW_LINE INDENT return n // 4 - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return n // 4 ; NEW_LINE DEDENT DEDENT n = 20 ; NEW_LINE print ( possibleways ( n ) ) ; NEW_LINE
Count sub | Function to count sub - arrays whose product is divisible by K ; Calculate the product of the current sub - array ; If product of the current sub - array is divisible by K ; Driver code
def countSubarrays ( arr , n , K ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT product = 1 NEW_LINE for x in range ( i , j + 1 ) : NEW_LINE INDENT product *= arr [ x ] NEW_LINE DEDENT if ( product % K == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE K = 4 NEW_LINE print ( countSubarrays ( arr , n , K ) ) NEW_LINE DEDENT
Count sub | Python3 implementation of the approach ; Segment tree implemented as an array ; Function to build the segment tree ; Function to query product of sub - array [ l . . r ] in O ( log n ) time ; Function to count sub - arrays whose product is divisible by K ; Query segment tree to find product % k of the sub - array [ i . . j ] ; Driver code ; Build the segment tree
MAX = 100002 NEW_LINE tree = [ 0 for i in range ( 4 * MAX ) ] ; NEW_LINE def build ( node , start , end , arr , k ) : NEW_LINE INDENT if ( start == end ) : NEW_LINE INDENT tree [ node ] = ( arr [ start ] ) % k ; NEW_LINE return ; NEW_LINE DEDENT mid = ( start + end ) >> 1 ; NEW_LINE build ( 2 * node , start , mid , arr , k ) ; NEW_LINE build ( 2 * node + 1 , mid + 1 , end , arr , k ) ; NEW_LINE tree [ node ] = ( tree [ 2 * node ] * tree [ 2 * node + 1 ] ) % k ; NEW_LINE DEDENT def query ( node , start , end , l , r , k ) : NEW_LINE INDENT if ( start > end or start > r or end < l ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( start >= l and end <= r ) : NEW_LINE INDENT return tree [ node ] % k ; NEW_LINE DEDENT mid = ( start + end ) >> 1 ; NEW_LINE q1 = query ( 2 * node , start , mid , l , r , k ) ; NEW_LINE q2 = query ( 2 * node + 1 , mid + 1 , end , l , r , k ) ; NEW_LINE return ( q1 * q2 ) % k ; NEW_LINE DEDENT def countSubarrays ( arr , n , k ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT product_mod_k = query ( 1 , 0 , n - 1 , i , j , k ) ; NEW_LINE if ( product_mod_k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 ; NEW_LINE build ( 1 , 0 , n - 1 , arr , k ) ; NEW_LINE print ( countSubarrays ( arr , n , k ) ) NEW_LINE DEDENT
Find a pair from the given array with maximum nCr value | Function to print the pair that gives maximum nCr ; This gives the value of N in nCr ; Case 1 : When N is odd ; Case 2 : When N is even ; Driver code
def printMaxValPair ( v , n ) : NEW_LINE INDENT v . sort ( ) NEW_LINE N = v [ n - 1 ] NEW_LINE if N % 2 == 1 : NEW_LINE INDENT first_maxima = N // 2 NEW_LINE second_maxima = first_maxima + 1 NEW_LINE ans1 , ans2 = 3 * ( 10 ** 18 ) , 3 * ( 10 ** 18 ) NEW_LINE from_left , from_right = - 1 , - 1 NEW_LINE _from = - 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if v [ i ] > first_maxima : NEW_LINE INDENT _from = i NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT diff = first_maxima - v [ i ] NEW_LINE if diff < ans1 : NEW_LINE INDENT ans1 = diff NEW_LINE from_left = v [ i ] NEW_LINE DEDENT DEDENT DEDENT from_right = v [ _from ] NEW_LINE diff1 = first_maxima - from_left NEW_LINE diff2 = from_right - second_maxima NEW_LINE if diff1 < diff2 : NEW_LINE INDENT print ( N , from_left ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( N , from_right ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT maxima = N // 2 NEW_LINE ans1 = 3 * ( 10 ** 18 ) NEW_LINE R = - 1 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT diff = abs ( v [ i ] - maxima ) NEW_LINE if diff < ans1 : NEW_LINE INDENT ans1 = diff NEW_LINE R = v [ i ] NEW_LINE DEDENT DEDENT print ( N , R ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT v = [ 1 , 1 , 2 , 3 , 6 , 1 ] NEW_LINE n = len ( v ) NEW_LINE printMaxValPair ( v , n ) NEW_LINE DEDENT
Find the number of good permutations | Function to return the count of good permutations ; For m = 0 , ans is 1 ; If k is greater than 1 ; If k is greater than 2 ; If k is greater than 3 ; Driver code
def Permutations ( n , k ) : NEW_LINE INDENT ans = 1 NEW_LINE if k >= 2 : NEW_LINE INDENT ans += ( n ) * ( n - 1 ) // 2 NEW_LINE DEDENT if k >= 3 : NEW_LINE INDENT ans += ( ( n ) * ( n - 1 ) * ( n - 2 ) * 2 // 6 ) NEW_LINE DEDENT if k >= 4 : NEW_LINE INDENT ans += ( ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) * 9 // 24 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 5 , 2 NEW_LINE print ( Permutations ( n , k ) ) NEW_LINE DEDENT
Count integers in a range which are divisible by their euler totient value | Function to return a ^ n ; Function to return count of integers that satisfy n % phi ( n ) = 0 ; Driver Code
def power ( a , n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT p = power ( a , n // 2 ) NEW_LINE p = p * p NEW_LINE if n & 1 : NEW_LINE INDENT p = p * a NEW_LINE DEDENT return p NEW_LINE DEDENT def countIntegers ( l , r ) : NEW_LINE INDENT ans , i = 0 , 1 NEW_LINE v = power ( 2 , i ) NEW_LINE while v <= r : NEW_LINE INDENT while v <= r : NEW_LINE INDENT if v >= l : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT v = v * 3 NEW_LINE DEDENT i += 1 NEW_LINE v = power ( 2 , i ) NEW_LINE DEDENT if l == 1 : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , r = 12 , 21 NEW_LINE print ( countIntegers ( l , r ) ) NEW_LINE DEDENT
Number of pairs from the first N natural numbers whose sum is divisible by K | Function to find the number of pairs from the set of natural numbers up to N whose sum is divisible by K ; Declaring a Hash to store count ; Storing the count of integers with a specific remainder in Hash array ; Check if K is even ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Count of pairs when both the remainders are K / 2 ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Driver code ; Print the count of pairs
def findPairCount ( N , K ) : NEW_LINE INDENT count = 0 ; NEW_LINE rem = [ 0 ] * K ; NEW_LINE rem [ 0 ] = N // K ; NEW_LINE for i in range ( 1 , K ) : NEW_LINE INDENT rem [ i ] = ( N - i ) // K + 1 ; NEW_LINE DEDENT if ( K % 2 == 0 ) : NEW_LINE INDENT count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) // 2 ; NEW_LINE for i in range ( 1 , K // 2 ) : NEW_LINE INDENT count += rem [ i ] * rem [ K - i ] ; NEW_LINE DEDENT count += ( rem [ K // 2 ] * ( rem [ K // 2 ] - 1 ) ) // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) // 2 ; NEW_LINE for i in rage ( 1 , K // 2 + 1 ) : NEW_LINE INDENT count += rem [ i ] * rem [ K - i ] ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 ; K = 4 ; NEW_LINE print ( findPairCount ( N , K ) ) ; NEW_LINE DEDENT
Find the sum of all Truncatable primes below N | Python3 implementation of the above approach ; To check if a number is prime or not ; Sieve of Eratosthenes function to find all prime numbers ; Function to return the sum of all truncatable primes below n ; To store the required sum ; Check every number below n ; Check from right to left ; If number is not prime at any stage ; Check from left to right ; If number is not prime at any stage ; If flag is still true ; Return the required sum ; Driver code
N = 1000005 NEW_LINE prime = [ True for i in range ( N ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE prime [ 0 ] = False NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT for j in range ( i * 2 , N , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def sumTruncatablePrimes ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT num = i NEW_LINE flag = True NEW_LINE while ( num ) : NEW_LINE INDENT if ( prime [ num ] == False ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT num //= 10 NEW_LINE DEDENT num = i NEW_LINE power = 10 NEW_LINE while ( num // power ) : NEW_LINE INDENT if ( prime [ num % power ] == False ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT power *= 10 NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 25 NEW_LINE sieve ( ) NEW_LINE print ( sumTruncatablePrimes ( n ) ) NEW_LINE
Smallest and Largest N | Python3 implementation of the approach ; Function to print the largest and the smallest n - digit perfect squares ; Smallest n - digit perfect square ; Largest n - digit perfect square ; Driver code
import math NEW_LINE def nDigitPerfectSquares ( n ) : NEW_LINE INDENT print ( pow ( math . ceil ( math . sqrt ( pow ( 10 , n - 1 ) ) ) , 2 ) , end = " ▁ " ) ; NEW_LINE print ( pow ( math . ceil ( math . sqrt ( pow ( 10 , n ) ) ) - 1 , 2 ) ) ; NEW_LINE DEDENT n = 4 ; NEW_LINE nDigitPerfectSquares ( n ) ; NEW_LINE
Maximum trace possible for any sub | Python 3 implementation of the approach ; Function to return the maximum trace possible for a sub - matrix of the given matrix ; Calculate the trace for each of the sub - matrix with top left corner at cell ( r , s ) ; Update the maximum trace ; Return the maximum trace ; Driver code
N = 3 NEW_LINE def MaxTraceSub ( mat ) : NEW_LINE INDENT max_trace = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT r = i NEW_LINE s = j NEW_LINE trace = 0 NEW_LINE while ( r < N and s < N ) : NEW_LINE INDENT trace += mat [ r ] NEW_LINE r += 1 NEW_LINE s += 1 NEW_LINE max_trace = max ( trace , max_trace ) NEW_LINE DEDENT DEDENT DEDENT return max_trace NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 10 , 2 , 5 ] , [ 6 , 10 , 4 ] , [ 2 , 7 , - 10 ] ] NEW_LINE print ( MaxTraceSub ( mat ) ) NEW_LINE DEDENT
Check if matrix can be converted to another matrix by transposing square sub | Python 3 implementation of the approach ; Function that returns true if matrix1 can be converted to matrix2 with the given operation ; Traverse all the diagonals starting at first column ; Traverse in diagonal ; Store the diagonal elements ; Move up ; Sort the elements ; Check if they are same ; Traverse all the diagonals starting at last row ; Traverse in the diagonal ; Store diagonal elements ; Sort all elements ; Check for same ; If every element matches ; Driver code
n = 3 NEW_LINE m = 3 NEW_LINE def check ( a , b ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT v1 = [ ] NEW_LINE v2 = [ ] NEW_LINE r = i NEW_LINE col = 0 NEW_LINE while ( r >= 0 and col < m ) : NEW_LINE INDENT v1 . append ( a [ r ] [ col ] ) NEW_LINE v2 . append ( b [ r ] [ col ] ) NEW_LINE r -= 1 NEW_LINE col += 1 NEW_LINE DEDENT v1 . sort ( reverse = False ) NEW_LINE v2 . sort ( reverse = False ) NEW_LINE for i in range ( len ( v1 ) ) : NEW_LINE INDENT if ( v1 [ i ] != v2 [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT for j in range ( 1 , m ) : NEW_LINE INDENT v1 = [ ] NEW_LINE v2 = [ ] NEW_LINE r = n - 1 NEW_LINE col = j NEW_LINE while ( r >= 0 and col < m ) : NEW_LINE INDENT v1 . append ( a [ r ] [ col ] ) NEW_LINE v2 . append ( b [ r ] [ col ] ) NEW_LINE r -= 1 NEW_LINE col += 1 NEW_LINE DEDENT v1 . sort ( reverse = False ) NEW_LINE v2 . sort ( reverse = False ) NEW_LINE for i in range ( len ( v1 ) ) : NEW_LINE INDENT if ( v1 [ i ] != v2 [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE b = [ [ 1 , 4 , 7 ] , [ 2 , 5 , 6 ] , [ 3 , 8 , 9 ] ] NEW_LINE if ( check ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Last digit of Product of two Large or Small numbers ( a * b ) | Function to print the last digit of product a * b ; Driver code
def lastDigit ( a , b ) : NEW_LINE INDENT lastDig = ( ( int ( a [ len ( a ) - 1 ] ) - int ( '0' ) ) * ( int ( b [ len ( b ) - 1 ] ) - int ( '0' ) ) ) NEW_LINE print ( lastDig % 10 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b = "1234567891233" , "1234567891233156" NEW_LINE lastDigit ( a , b ) NEW_LINE DEDENT
Smallest and Largest Palindrome with N Digits | Python 3 implementation of the above approach ; Function to print the smallest and largest palindrome with N digits ; Driver Code
from math import pow NEW_LINE def printPalindrome ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( " Smallest ▁ Palindrome : ▁ 0" ) NEW_LINE print ( " Largest ▁ Palindrome : ▁ 9" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Smallest ▁ Palindrome : " , int ( pow ( 10 , n - 1 ) ) + 1 ) NEW_LINE print ( " Largest ▁ Palindrome : " , int ( pow ( 10 , n ) ) - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE printPalindrome ( n ) NEW_LINE DEDENT
Addition of two numbers without propagating Carry | Function to prsum of 2 numbers without propagating carry ; Reverse a ; Reverse b ; Generate sum Since length of both a and b are same , take any one of them . ; Extract digits from a and b and add ; If sum is single digit ; If sum is not single digit reverse sum ; Extract digits from sum and append to result ; Driver code
def printSum ( a , b ) : NEW_LINE INDENT res , temp1 , temp2 = 0 , 0 , 0 NEW_LINE while a > 0 : NEW_LINE INDENT temp1 = temp1 * 10 + ( a % 10 ) NEW_LINE a //= 10 NEW_LINE DEDENT a = temp1 NEW_LINE while b > 0 : NEW_LINE INDENT temp2 = temp2 * 10 + ( b % 10 ) NEW_LINE b //= 10 NEW_LINE DEDENT b = temp2 NEW_LINE while a : NEW_LINE INDENT Sum = a % 10 + b % 10 NEW_LINE if Sum // 10 == 0 : NEW_LINE INDENT res = res * 10 + Sum NEW_LINE DEDENT else : NEW_LINE INDENT temp1 = 0 NEW_LINE while Sum > 0 : NEW_LINE INDENT temp1 = temp1 * 10 + ( Sum % 10 ) NEW_LINE Sum //= 10 NEW_LINE DEDENT Sum = temp1 NEW_LINE while Sum > 0 : NEW_LINE INDENT res = res * 10 + ( Sum % 10 ) NEW_LINE Sum //= 10 NEW_LINE DEDENT DEDENT a //= 10 NEW_LINE b //= 10 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 7752 , 8834 NEW_LINE print ( printSum ( a , b ) ) NEW_LINE DEDENT