codeparrot/xlcost-text-to-code huggingface dataset:
Minimum integer that can be obtained by swapping adjacent digits of different parity | Function to return the minimum number ; Store the elements which are divisible by two in stack1 ; Store the elements which are not divisible by two in stack2 . ; Concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element ; Concatenate the answer with remaining values of stack1 . ; Concatenate the answer with remaining values of stack2 . ; Driver code ; Function calling
def minimumNo ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE stack1 = [ ] NEW_LINE stack2 = [ ] NEW_LINE while ( n != 0 ) : NEW_LINE INDENT r = n % 10 NEW_LINE if ( r % 2 == 0 ) : NEW_LINE INDENT stack1 . append ( r ) NEW_LINE DEDENT else : NEW_LINE INDENT stack2 . append ( r ) NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT while ( len ( stack1 ) > 0 and len ( stack2 ) > 0 ) : NEW_LINE INDENT if ( stack1 [ - 1 ] < stack2 [ - 1 ] ) : NEW_LINE INDENT ans = ans * 10 + stack1 [ - 1 ] NEW_LINE del stack1 [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans * 10 + stack2 [ - 1 ] NEW_LINE del stack2 [ - 1 ] NEW_LINE DEDENT DEDENT while ( len ( stack1 ) > 0 ) : NEW_LINE INDENT ans = ans * 10 + stack1 [ - 1 ] NEW_LINE del stack1 [ - 1 ] NEW_LINE DEDENT while ( len ( stack2 ) > 0 ) : NEW_LINE INDENT ans = ans * 10 + stack2 [ - 1 ] NEW_LINE del stack2 [ - 1 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT n1 = 64432 NEW_LINE print ( minimumNo ( n1 ) ) NEW_LINE n2 = 3137 NEW_LINE print ( minimumNo ( n2 ) ) NEW_LINE
Find two numbers with the given LCM and minimum possible difference | Python3 implementation of the approach ; Function to return the LCM of a and b ; Function to find and print the two numbers ; To find the factors ; To check if i is a factor of x and the minimum possible number satisfying the given conditions ; Driver code
from math import gcd as __gcd , sqrt , ceil NEW_LINE def lcm ( a , b ) : NEW_LINE INDENT return ( a // __gcd ( a , b ) * b ) NEW_LINE DEDENT def findNums ( x ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , ceil ( sqrt ( x ) ) ) : NEW_LINE INDENT if ( x % i == 0 and lcm ( i , x // i ) == x ) : NEW_LINE INDENT ans = i NEW_LINE DEDENT DEDENT print ( ans , ( x // ans ) ) NEW_LINE DEDENT x = 12 NEW_LINE findNums ( x ) NEW_LINE
Find an integer that is common in the maximum number of given arithmetic progressions | Python implementation of the approach ; Function to return element common in maximum number of APs ; Initialize the count variable ; Increment count for every element of an AP ; Find the index with maximum count ; Return the maximum common element ; Driver code
MAXN = 1000000 NEW_LINE def maxCommonElement ( A , D , N ) : NEW_LINE INDENT cnt = [ 0 ] * MAXN NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( A [ i ] , MAXN , D [ i ] ) : NEW_LINE INDENT cnt [ j ] += 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE com = 0 NEW_LINE for i in range ( MAXN ) : NEW_LINE INDENT if cnt [ i ] > ans : NEW_LINE INDENT ans = cnt [ i ] NEW_LINE com = i NEW_LINE DEDENT DEDENT return com NEW_LINE DEDENT A = [ 13 , 1 , 2 , 5 ] NEW_LINE D = [ 5 , 10 , 1 , 12 ] NEW_LINE N = len ( A ) NEW_LINE print ( maxCommonElement ( A , D , N ) ) NEW_LINE
Number of trailing zeros in N * ( N | Function to return the count of trailing 0 s in the given function ; If n is odd ; If n is even ; Find the trailing zeros in n / 2 factorial ; Return the required answer ; Driver code
def findTrailingZeros ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT ans = 0 NEW_LINE n //= 2 NEW_LINE while ( n ) : NEW_LINE INDENT ans += n // 5 NEW_LINE n //= 5 NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT n = 12 NEW_LINE print ( findTrailingZeros ( n ) ) NEW_LINE
Divide N into K unique parts such that gcd of those parts is maximum | Python3 implementation of the approach ; Function to calculate maximum GCD ; Minimum possible sum for K unique positive integers ; It is not possible to divide N into K unique parts ; All the factors greater than sqrt ( N ) are complementary of the factors less than sqrt ( N ) ; If i is a factor of N ; Driver code
from math import sqrt , ceil , floor NEW_LINE def maxGCD ( N , K ) : NEW_LINE INDENT minSum = ( K * ( K + 1 ) ) / 2 NEW_LINE if ( N < minSum ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT i = ceil ( sqrt ( N ) ) NEW_LINE res = 1 NEW_LINE while ( i >= 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( i >= minSum ) : NEW_LINE INDENT res = max ( res , N / i ) NEW_LINE DEDENT if ( N / i >= minSum ) : NEW_LINE INDENT res = max ( res , i ) NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE DEDENT return res NEW_LINE DEDENT N = 18 NEW_LINE K = 3 NEW_LINE print ( maxGCD ( N , K ) ) NEW_LINE
Radius of the inscribed circle within three tangent circles | Python3 implementation of the approach ; Radius of the 3 given circles ; Calculation of area of a triangle by Heron 's formula ; Applying binary search to find the radius r4 of the required circle ; Area of main triangle ; Loop runs until l and h becomes approximately equal ; Area of smaller triangles ; If sum of smaller triangles is less than main triangle ; If sum of smaller triangles is greater than or equal to main triangle ; Taking r1 , r2 , r3 as input ; Call to function binary search
import math NEW_LINE r1 = 0 NEW_LINE r2 = 0 NEW_LINE r3 = 0 NEW_LINE def area ( a , b , c ) : NEW_LINE INDENT p = ( a + b + c ) / 2 NEW_LINE return ( ( math . sqrt ( p ) ) * ( math . sqrt ( p - a ) ) * ( math . sqrt ( p - b ) ) * ( math . sqrt ( p - c ) ) ) NEW_LINE DEDENT def binary_search ( ) : NEW_LINE INDENT global r1 , r2 , r3 NEW_LINE s = area ( r1 + r2 , r2 + r3 , r3 + r1 ) NEW_LINE l = 0 NEW_LINE h = s / ( r1 + r2 + r3 ) NEW_LINE while ( h - l > 0.00000001 ) : NEW_LINE INDENT mid = ( l + h ) / 2 NEW_LINE s1 = area ( mid + r1 , mid + r2 , r1 + r2 ) NEW_LINE s2 = area ( mid + r1 , mid + r3 , r1 + r3 ) NEW_LINE s3 = area ( mid + r2 , mid + r3 , r2 + r3 ) NEW_LINE if ( s1 + s2 + s3 < s ) : NEW_LINE INDENT l = mid NEW_LINE DEDENT else : NEW_LINE INDENT h = mid NEW_LINE DEDENT DEDENT return ( ( l + h ) / 2 ) NEW_LINE DEDENT r1 = 1 NEW_LINE r2 = 2 NEW_LINE r3 = 3 NEW_LINE print ( " { 0 : . 6f } " . format ( binary_search ( ) ) ) NEW_LINE
Radius of the inscribed circle within three tangent circles | Python3 implementation of the approach ; Taking r1 , r2 , r3 as input ; Calculation of r4 using formula given above
from math import sqrt NEW_LINE r1 = 1 NEW_LINE r2 = 2 NEW_LINE r3 = 3 NEW_LINE r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) NEW_LINE print ( round ( r4 , 6 ) ) NEW_LINE
Make all elements zero by decreasing any two elements by one at a time | Function that returns true if all the array elements can be made 0 with the given operation ; Find the maximum element and the sum ; Check the required condition ; Driver code
def checkZeroArray ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE maximum = - 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + arr [ i ] NEW_LINE maximum = max ( maximum , arr [ i ] ) NEW_LINE DEDENT if ( sum % 2 == 0 and maximum <= sum // 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT arr = [ 1 , 2 , 1 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( checkZeroArray ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Subarray permutation that satisfies the given condition | Function that returns true if the required subarray exists in the given array ; Map to store the positions of each integer in the original permutation ; To store the address of each entry in arr [ n ] but with 1 - based indexing ; To track minimum position sumcur for sum of a positions ti this position ; Summing up addresses ; Tracking minimum address encountered ti now ; The sum of the addresses if it forms the required subarray ; If current sum of address is equal to val ; Driver code
def subArray ( arr , n , m ) : NEW_LINE INDENT i = 0 NEW_LINE mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] = i + 1 NEW_LINE DEDENT sumcur = 0 NEW_LINE p = 10 ** 9 NEW_LINE ans = [ ] NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT sumcur += mp [ i ] NEW_LINE p = min ( p , mp [ i ] ) NEW_LINE val = p * i - i + ( i * ( i + 1 ) ) / 2 NEW_LINE if ( i == m ) : NEW_LINE INDENT if ( val == sumcur ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 4 , 5 , 1 , 3 , 2 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE m = 3 NEW_LINE if ( subArray ( arr , n , m ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Sum of all N digit palindrome numbers | Python program for the above approach ; Function to check palindrome ; Function to calculate the sum of n - digit palindrome ; Run a loop to check all possible palindrome ; If palindrome append sum ; Driver code
import math NEW_LINE def isPalindrome ( s ) : NEW_LINE INDENT left = 0 NEW_LINE right = len ( s ) - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT if ( s [ left ] != s [ right ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT left = left + 1 NEW_LINE right = right - 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def getSum ( n ) : NEW_LINE INDENT start = int ( math . pow ( 10 , n - 1 ) ) NEW_LINE end = int ( math . pow ( 10 , n ) ) - 1 NEW_LINE sum = 0 NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT s = str ( i ) NEW_LINE if ( isPalindrome ( s ) ) : NEW_LINE INDENT sum = sum + i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 1 NEW_LINE ans = getSum ( n ) NEW_LINE print ( ans ) NEW_LINE
Sum of all N digit palindrome numbers | Function to calculate sum of n digit number ; Corner case ; Using above approach ; Driver code
def getSum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE if ( n == 1 ) : NEW_LINE INDENT sum = 45.0 ; NEW_LINE DEDENT else : NEW_LINE INDENT sum = ( 99.0 / 2.0 ) * pow ( 10 , n - 1 ) * pow ( 10 , ( n - 1 ) / 2 ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 ; NEW_LINE ans = int ( getSum ( n ) ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT
Count number of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] | Function to return the count of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] ; Increment count if condition satisfy ; Return count of pairs ; Driver code ; Get and print count of pairs
def countPairs ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] * arr [ j ] == arr [ i ] + arr [ j ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 0 , 3 , 2 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countPairs ( arr , n ) ) ; NEW_LINE DEDENT
Count number of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] | Function to return the count of pairs ( i , j ) such that arr [ i ] * arr [ j ] = arr [ i ] + arr [ j ] ; Count number of 0 ' s ▁ and ▁ 2' s in the array ; Total pairs due to occurrence of 0 's  ; Total pairs due to occurrence of 2 's  ; Return count of all pairs ; Driver code ; Get and print count of pairs
def countPairs ( arr , n ) : NEW_LINE INDENT countZero = 0 ; NEW_LINE countTwo = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT countZero += 1 ; NEW_LINE DEDENT elif ( arr [ i ] == 2 ) : NEW_LINE INDENT countTwo += 1 ; NEW_LINE DEDENT DEDENT pair0 = ( countZero * ( countZero - 1 ) ) // 2 ; NEW_LINE pair2 = ( countTwo * ( countTwo - 1 ) ) // 2 ; NEW_LINE return pair0 + pair2 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 0 , 3 , 2 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countPairs ( arr , n ) ) ; NEW_LINE DEDENT
Number of subsequences with positive product | Python 3 implementation of the approach ; Function to return the count of all the subsequences with positive product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; For the empty subsequence ; Driver code
import math NEW_LINE def cntSubSeq ( arr , n ) : NEW_LINE INDENT pos_count = 0 ; NEW_LINE neg_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT pos_count += 1 NEW_LINE DEDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT neg_count += 1 NEW_LINE DEDENT DEDENT result = int ( math . pow ( 2 , pos_count ) ) NEW_LINE if ( neg_count > 0 ) : NEW_LINE INDENT result *= int ( math . pow ( 2 , neg_count - 1 ) ) NEW_LINE DEDENT result -= 1 NEW_LINE return result NEW_LINE DEDENT arr = [ 2 , - 3 , - 1 , 4 ] NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntSubSeq ( arr , n ) ) NEW_LINE
Smallest number dividing minimum number of elements in the array | Set 2 | Function to return the smallest number that divides minimum number of elements in the given array ; m stores the maximum in the array ; Frequency array ; Sieve ; Incrementing j ; If no multiples of j are in the array ; Driver code
def findMin ( arr , n ) : NEW_LINE INDENT m = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT m = max ( m , arr [ i ] ) NEW_LINE DEDENT freq = [ 0 ] * ( m + 2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , m + 2 ) : NEW_LINE INDENT j = i NEW_LINE cnt = 0 NEW_LINE while ( j <= m ) : NEW_LINE INDENT cnt += freq [ j ] NEW_LINE j += i NEW_LINE DEDENT if ( not cnt ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return m + 1 NEW_LINE DEDENT arr = [ 2 , 12 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMin ( arr , n ) ) NEW_LINE
Smallest number dividing minimum number of elements in the Array | Function to return the smallest number that divides minimum number of elements ; m stores the maximum in the array ; Frequency table ; Loop to factorize ; sqrt factorization of the numbers ; Finding the smallest number with zero multiples ; Driver code
def findMin ( arr , n ) : NEW_LINE INDENT m = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT m = max ( m , arr [ i ] ) NEW_LINE DEDENT cnt = [ 0 ] * ( m + 2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = 1 NEW_LINE while j * j <= arr [ i ] : NEW_LINE INDENT if ( arr [ i ] % j == 0 ) : NEW_LINE INDENT if ( j * j == arr [ i ] ) : NEW_LINE INDENT cnt [ j ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ j ] += 1 NEW_LINE cnt [ arr [ i ] // j ] += 1 NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , m + 2 ) : NEW_LINE INDENT if ( cnt [ i ] == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 2 , 12 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMin ( arr , n ) ) NEW_LINE
Remove two consecutive integers from 1 to N to make sum equal to S | Function to find the numbers to be removed ; typecast appropriately so that answer is float ; return the obtained result ; Convert i to integer ; If i is an integer is 0 then answer is Yes ; Driver code
def findNumber ( N , S ) : NEW_LINE INDENT i = ( ( ( N ) * ( N + 1 ) ) / 4 ) - ( ( S + 1 ) / 2 ) ; NEW_LINE return i ; NEW_LINE DEDENT def check ( N , S ) : NEW_LINE INDENT i = findNumber ( N , S ) ; NEW_LINE integerI = int ( i ) ; NEW_LINE if ( i - integerI == 0 ) : NEW_LINE INDENT print ( " Yes : " , integerI , " , " , integerI + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; NEW_LINE S = 3 ; NEW_LINE check ( N , S ) ; NEW_LINE N = 5 ; NEW_LINE S = 3 ; NEW_LINE check ( N , S ) ; NEW_LINE DEDENT
Remove all the prime numbers from the given array | Python3 implementation of the approach ; Function for Sieve of Eratosthenes ; Function to pr the elements of the array ; Function to remove all the prime numbers ; Generate primes ; Traverse the array ; If the current element is prime ; Shift all the elements on the right of it to the left ; Decrease the loop counter by 1 to check the shifted element ; Decrease the length ; Pr the updated array ; Driver code
sz = 10 ** 5 NEW_LINE isPrime = [ True for i in range ( sz + 1 ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i < sz : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , sz , i ) : NEW_LINE INDENT isPrime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def prArray ( arr , lenn ) : NEW_LINE INDENT for i in range ( lenn ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def removePrimes ( arr , lenn ) : NEW_LINE INDENT sieve ( ) NEW_LINE i = 0 NEW_LINE while i < lenn : NEW_LINE INDENT if ( isPrime [ arr [ i ] ] ) : NEW_LINE INDENT for j in range ( i , lenn - 1 ) : NEW_LINE INDENT arr [ j ] = arr [ j + 1 ] NEW_LINE DEDENT i -= 1 NEW_LINE lenn -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT prArray ( arr , lenn ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 6 , 5 , 3 , 8 , 7 , 10 , 11 , 14 , 15 ] NEW_LINE lenn = len ( arr ) NEW_LINE removePrimes ( arr , lenn ) NEW_LINE DEDENT
Remove one element to get minimum OR value | Function to return the minimized OR after removing an element from the array ; Base case ; Prefix and suffix OR array ; Computing prefix / suffix OR arrays ; To store the final answer ; Finding the final answer ; Returning the final answer ; Driver code
def minOR ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT pre = [ 0 ] * n NEW_LINE suf = [ 0 ] * n NEW_LINE pre [ 0 ] = arr [ 0 ] NEW_LINE suf [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT pre [ i ] = ( pre [ i - 1 ] arr [ i ] ) NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suf [ i ] = ( suf [ i + 1 ] arr [ i ] ) NEW_LINE DEDENT ans = min ( pre [ n - 2 ] , suf [ 1 ] ) NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT ans = min ( ans , ( pre [ i - 1 ] suf [ i + 1 ] ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minOR ( arr , n ) ) NEW_LINE DEDENT
Check if there exists a prime number which gives Y after being repeatedly subtracted from X | Function that returns true if any prime number satisfies the given conditions ; No such prime exists ; Driver code
def isPossible ( x , y ) : NEW_LINE INDENT if ( ( x - y ) == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT x = 100 NEW_LINE y = 98 NEW_LINE if ( isPossible ( x , y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find number of square of area Z which can be built in a matrix having blocked regions | Python3 implementation of the approach ; Function to calculate the number of square areas of size K * K ; Row array and column array to store the lengths of differences between consecutive rows / columns ; Fill the conrow vector ; Fill the concol vector ; To store the required answer ; Every pair of row size and column size would result in an unblocked region ; Driver code
from math import sqrt NEW_LINE def subgrids ( N , Z , row , col , r , d ) : NEW_LINE INDENT conrow = [ ] ; NEW_LINE concol = [ ] ; NEW_LINE K = int ( sqrt ( Z ) ) ; NEW_LINE conrow . append ( row [ 0 ] - 0 - 1 ) NEW_LINE conrow . append ( N + 1 - row [ r - 1 ] - 1 ) NEW_LINE for i in range ( 1 , r ) : NEW_LINE INDENT conrow . append ( row [ i ] - row [ i - 1 ] - 1 ) ; NEW_LINE DEDENT concol . append ( col [ 0 ] - 0 - 1 ) NEW_LINE concol . append ( N + 1 - col [ d - 1 ] - 1 ) NEW_LINE for i in range ( 1 , d ) : NEW_LINE INDENT concol . append ( col [ i ] - col [ i - 1 ] - 1 ) ; NEW_LINE DEDENT row_size = len ( conrow ) NEW_LINE col_size = len ( concol ) NEW_LINE answer = 0 NEW_LINE for i in range ( row_size ) : NEW_LINE INDENT for j in range ( col_size ) : NEW_LINE INDENT total = ( concol [ j ] // K ) * ( conrow [ i ] // K ) NEW_LINE answer += ( total ) NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 ; Z = 4 NEW_LINE row = [ 4 , 6 ] NEW_LINE col = [ 3 , 8 ] NEW_LINE r = len ( row ) NEW_LINE d = len ( col ) NEW_LINE print ( subgrids ( N , Z , row , col , r , d ) ) NEW_LINE DEDENT
Count of numbers whose sum of increasing powers of digits is equal to the number itself | Function to return the count of digits of n ; Function to return the sum of increasing powers of N ; To store the required answer ; Count of digits in n which will be the power of the last digit ; While there are digits left ; Get the last digit ; Add the last digit after raising it to the required power ; Decrement the power for the previous digit ; Remove the last digit ; Function to return the count of integers which satisfy the given conditions ; If current element satisfies the given condition ; Driver code
def countDigits ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n //= 10 NEW_LINE DEDENT return cnt NEW_LINE DEDENT def digitPowSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE pw = countDigits ( n ) NEW_LINE while ( n > 0 ) : NEW_LINE INDENT d = n % 10 NEW_LINE sum += pow ( d , pw ) NEW_LINE pw -= 1 NEW_LINE n //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def countNum ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( i == digitPowSum ( i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 200 NEW_LINE print ( countNum ( n ) ) NEW_LINE
Print all perfect squares from the given range | Function to print all the perfect squares from the given range ; For every element from the range ; If current element is a perfect square ; Driver code
def perfectSquares ( l , r ) : NEW_LINE INDENT for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( i ** ( .5 ) == int ( i ** ( .5 ) ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT l = 2 NEW_LINE r = 24 NEW_LINE perfectSquares ( l , r ) NEW_LINE
Print all perfect squares from the given range | Python3 implementation of the approach ; Function to print all the perfect squares from the given range ; Getting the very first number ; First number 's square ; Next number is at the difference of ; While the perfect squares are from the range ; Print the perfect square ; Get the next perfect square ; Next odd number to be added ; Driver code
from math import ceil , sqrt NEW_LINE def perfectSquares ( l , r ) : NEW_LINE INDENT number = ceil ( sqrt ( l ) ) ; NEW_LINE n2 = number * number ; NEW_LINE number = ( number * 2 ) + 1 ; NEW_LINE while ( ( n2 >= l and n2 <= r ) ) : NEW_LINE INDENT print ( n2 , end = " ▁ " ) ; NEW_LINE n2 = n2 + number ; NEW_LINE number += 2 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 2 ; r = 24 ; NEW_LINE perfectSquares ( l , r ) ; NEW_LINE DEDENT
Find the value of N XOR 'ed to itself K times | Function to return n ^ n ^ ... k times ; Find the result ; Driver code
def xorK ( n , k ) : NEW_LINE INDENT res = n NEW_LINE for i in range ( 1 , k ) : NEW_LINE INDENT res = ( res ^ n ) NEW_LINE DEDENT return n NEW_LINE DEDENT n = 123 NEW_LINE k = 3 NEW_LINE print ( xorK ( n , k ) ) NEW_LINE