codeparrot/xlcost-text-to-code huggingface dataset:
Count N | Python program for the above approach ; Function to find the value of x ^ y ; Stores the value of x ^ y ; Iterate until y is positive ; If y is odd ; Divide y by 2 ; Return the value of x ^ y ; Function to find the number of N - digit integers satisfying the given criteria ; Count of even positions ; Count of odd positions ; Return the resultant count ; Driver Code
import math NEW_LINE m = 10 ** 9 + 7 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE while y > 0 : NEW_LINE INDENT if ( y & 1 ) != 0 : NEW_LINE INDENT res = ( res * x ) % m NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % m NEW_LINE DEDENT return res NEW_LINE DEDENT def countNDigitNumber ( n : int ) -> None : NEW_LINE INDENT ne = N // 2 + N % 2 NEW_LINE no = N // 2 NEW_LINE return power ( 4 , ne ) * power ( 5 , no ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( countNDigitNumber ( N ) % m ) NEW_LINE DEDENT
Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | Utility function to find minimum steps ; Initialize count and result ; Traverse over the range [ 1 , N ] ; Update res ; Increment count ; Return res ; Driver Code ; Input ; Function call
def minSteps ( N ) : NEW_LINE INDENT count = 1 NEW_LINE res = 0 NEW_LINE for i in range ( 1 , N + 1 , count ) : NEW_LINE INDENT res = max ( res , count ) NEW_LINE count += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE print ( minSteps ( N ) ) NEW_LINE DEDENT
Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | Python 3 implementation of the above approach ; Utility function to find minimum steps ; Driver code ; Input integer ; Function call
from math import sqrt NEW_LINE def minSteps ( N ) : NEW_LINE INDENT res = int ( ( sqrt ( 1 + 8 * N ) - 1 ) // 2 ) NEW_LINE return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE print ( minSteps ( N ) ) NEW_LINE DEDENT
The dice problem | Function to find number written on the opposite face of the dice ; Stores number on opposite face of dice ; Print the answer ; Given value of N ; Function call to find number written on the opposite face of the dice
def oppositeFaceOfDice ( N ) : NEW_LINE INDENT ans = 7 - N NEW_LINE print ( ans ) NEW_LINE DEDENT N = 2 NEW_LINE oppositeFaceOfDice ( N ) NEW_LINE
Count numbers from a given range that can be visited moving any number of steps from the range [ L , R ] | Function to count points from the range [ X , Y ] that can be reached by moving by L or R steps ; Initialize difference array ; Initialize Count ; Marking starting point ; Iterating from X to Y ; Accumulate difference array ; If diff_arr [ i ] is greater than 1 ; Updating difference array ; Visited point found ; Driver Code ; Given Input ; Function Call
def countReachablePoints ( X , Y , L , R ) : NEW_LINE INDENT diff_arr = [ 0 for i in range ( 100000 ) ] NEW_LINE count = 0 NEW_LINE diff_arr [ X ] = 1 NEW_LINE diff_arr [ X + 1 ] = - 1 NEW_LINE for i in range ( X , Y + 1 , 1 ) : NEW_LINE INDENT diff_arr [ i ] += diff_arr [ i - 1 ] NEW_LINE if ( diff_arr [ i ] >= 1 ) : NEW_LINE INDENT diff_arr [ i + L ] += 1 NEW_LINE diff_arr [ i + R + 1 ] -= 1 NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 3 NEW_LINE Y = 12 NEW_LINE L = 2 NEW_LINE R = 3 NEW_LINE print ( countReachablePoints ( X , Y , L , R ) ) NEW_LINE DEDENT
Count of triplets having sum of product of any two numbers with the third number equal to N | Function to find the SPF [ i ] using the Sieve Of Erastothenes ; Stores whether i is prime or not ; Initializing smallest factor as 2 for all even numbers ; Iterate for all odd numbers < N ; SPF of i for a prime is the number itself ; Iterate for all the multiples of the current prime number ; The value i is smallest prime factor for i * j ; Function to generate prime factors and its power ; Current prime factor of N ; Stores the powers of the current prime factor ; Find all the prime factors and their powers ; Return the total count of factors ; Function to count the number of triplets satisfying the given criteria ; Stores the count of resultant triplets ; Add the count all factors of N - z to the variable CountTriplet ; Return total count of triplets ; Driver Code ; S [ i ] stores the smallest prime factor for each element i ; Find the SPF [ i ] ; Function Call
def sieveOfEratosthenes ( N , s ) : NEW_LINE INDENT prime = [ False ] * ( N + 1 ) NEW_LINE for i in range ( 2 , N + 1 , 2 ) : NEW_LINE INDENT s [ i ] = 2 NEW_LINE DEDENT for i in range ( 3 , N + 1 , 2 ) : NEW_LINE INDENT if ( prime [ i ] == False ) : NEW_LINE INDENT s [ i ] = i NEW_LINE for j in range ( i , int ( N / i ) + 1 , 2 ) : NEW_LINE INDENT if ( prime [ i * j ] == False ) : NEW_LINE INDENT prime [ i * j ] = True NEW_LINE s [ i * j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def generatePrimeFactors ( N ) : NEW_LINE INDENT curr = s [ N ] NEW_LINE cnt = { s [ N ] : 1 } NEW_LINE while ( N > 1 ) : NEW_LINE INDENT N //= s [ N ] NEW_LINE if N and s [ N ] : NEW_LINE INDENT if cnt . get ( s [ N ] , 0 ) == 0 : NEW_LINE INDENT cnt [ s [ N ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ s [ N ] ] += 1 NEW_LINE DEDENT DEDENT DEDENT if 0 in cnt : NEW_LINE INDENT cnt . pop ( 0 ) NEW_LINE DEDENT totfactor = 1 NEW_LINE for i in cnt . values ( ) : NEW_LINE INDENT totfactor *= i + 1 NEW_LINE DEDENT return totfactor NEW_LINE DEDENT def countTriplets ( N ) : NEW_LINE INDENT CountTriplet = 0 NEW_LINE for z in range ( 1 , N + 1 ) : NEW_LINE INDENT p = generatePrimeFactors ( N - z ) NEW_LINE if p > 1 : NEW_LINE INDENT CountTriplet += p NEW_LINE DEDENT DEDENT return CountTriplet + 1 NEW_LINE DEDENT N = 10 NEW_LINE s = [ 0 ] * ( N + 1 ) NEW_LINE sieveOfEratosthenes ( N , s ) NEW_LINE print ( countTriplets ( N ) ) NEW_LINE
Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M | Function to find the minimum length of the subarray to be changed . ; Stores the frequencies of array elements ; Stores the number of array elements that are present more than N / M times ; Iterate over the range ; Increment the frequency ; If the frequency of all array elements are already N / M ; Stores the resultant length of the subarray ; The left and right pointers ; Iterate over the range ; If the current element is ; If the value of c is 0 , then find the possible answer ; Iterate over the range ; If the element at left is making it extra ; Update the left pointer ; Update the right pointer ; Return the resultant length ; Driver Code
def minimumSubarray ( arr , n , m ) : NEW_LINE INDENT mapu = [ 0 for i in range ( m + 1 ) ] NEW_LINE c = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mapu [ arr [ i ] ] += 1 NEW_LINE if ( mapu [ arr [ i ] ] == ( n // m ) + 1 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT if ( c == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = n NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE while ( r < n ) : NEW_LINE INDENT mapu [ arr [ r ] ] -= 1 NEW_LINE if ( mapu [ arr [ r ] ] == ( n // m ) ) : NEW_LINE INDENT c -= 1 NEW_LINE DEDENT if ( c == 0 ) : NEW_LINE INDENT while ( l <= r and c == 0 ) : NEW_LINE INDENT ans = min ( ans , r - l + 1 ) NEW_LINE mapu [ arr [ l ] ] += 1 NEW_LINE if ( mapu [ arr [ l ] ] > ( n // m ) ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT l += 1 NEW_LINE DEDENT DEDENT r += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 1 , 1 , 2 ] NEW_LINE M = 2 NEW_LINE N = len ( arr ) NEW_LINE print ( minimumSubarray ( arr , N , M ) ) NEW_LINE DEDENT
Check if the sum of K least and most frequent array elements are equal or not | Function to compare the sum of K most and least occurrences ; Stores frequency of array element ; Stores the frequencies as indexes and putelements with the frequency in a vector ; Find the frequency ; Insert in the vector ; Stores the count of elements ; Traverse the frequency array ; Find the kleastfreqsum ; If the count is K , break ; Reinitialize the count to zero ; Traverse the frequency ; Find the kmostfreqsum ; If the count is K , break ; Comparing the sum ; Otherwise , return No ; Driver Code
def checkSum ( arr , n , k ) : NEW_LINE INDENT m = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in m : NEW_LINE INDENT m [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT freq = [ [ ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT f = m [ arr [ i ] ] NEW_LINE if ( f != - 1 ) : NEW_LINE INDENT freq [ f ] . append ( arr [ i ] ) NEW_LINE m [ arr [ i ] ] = - 1 NEW_LINE DEDENT DEDENT count = 0 NEW_LINE kleastfreqsum = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for x in freq [ i ] : NEW_LINE INDENT kleastfreqsum += x NEW_LINE count += 1 NEW_LINE if ( count == k ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( count == k ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT count = 0 NEW_LINE kmostfreqsum = 0 NEW_LINE i = n NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT for x in freq [ i ] : NEW_LINE INDENT kmostfreqsum += x NEW_LINE count += 1 NEW_LINE if ( count == k ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE if ( count == k ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( kleastfreqsum == kmostfreqsum ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 2 , 3 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( checkSum ( arr , N , K ) ) NEW_LINE DEDENT
Print all numbers from a given range that are made up of consecutive digits | Function to find the consecutive digit numbers in the given range ; Initialize num as empty string ; Stores the resultant number ; Iterate over the range [ 1 , 9 ] ; Check if the current number is within range ; Iterate on the digits starting from i ; Checking the consecutive digits numbers starting from i and ending at j is within range or not ; Sort the numbers in the increasing order ; Driver Code ; Print the required numbers
def solve ( start , end ) : NEW_LINE INDENT num = " " NEW_LINE ans = [ ] NEW_LINE for i in range ( 1 , 10 , 1 ) : NEW_LINE INDENT num = str ( i ) NEW_LINE value = int ( num ) NEW_LINE if ( value >= start and value <= end ) : NEW_LINE INDENT ans . append ( value ) NEW_LINE DEDENT for j in range ( i + 1 , 10 , 1 ) : NEW_LINE INDENT num += str ( j ) NEW_LINE value = int ( num ) NEW_LINE if ( value >= start and value <= end ) : NEW_LINE INDENT ans . append ( value ) NEW_LINE DEDENT DEDENT DEDENT ans . sort ( ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 12 NEW_LINE R = 87 NEW_LINE ans = solve ( 12 , 87 ) NEW_LINE for it in ans : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Minimum distance a person has to move in order to take a picture of every racer | Python3 program for the above approach ; Function to return the minimum distance the person has to move order to get the pictures ; To store the minimum ending point ; To store the maximum starting point ; Find the values of minSeg and maxSeg ; Impossible ; The person doesn 't need to move ; Get closer to the left point ; Get closer to the right point ; Driver code
import sys NEW_LINE def minDistance ( start , end , n , d ) : NEW_LINE INDENT left = - sys . maxsize NEW_LINE right = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT left = max ( left , start [ i ] ) NEW_LINE right = min ( right , end [ i ] ) NEW_LINE DEDENT if ( left > right ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( d >= left and d <= right ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( d < left ) : NEW_LINE INDENT return ( left - d ) NEW_LINE DEDENT if ( d > right ) : NEW_LINE INDENT return ( d - right ) NEW_LINE DEDENT DEDENT start = [ 0 , 2 , 4 ] NEW_LINE end = [ 7 , 14 , 6 ] NEW_LINE n = len ( start ) NEW_LINE d = 3 NEW_LINE print ( minDistance ( start , end , n , d ) ) NEW_LINE
Maximum number of Armstrong Numbers present in a subarray of size K | Function to calculate the value of x raised to the power y in O ( log y ) ; Base Case ; If the power y is even ; Otherwise ; Function to calculate the order of the number , i . e . count of digits ; Stores the total count of digits ; Iterate until num is 0 ; Function to check a number is an Armstrong Number or not ; Find the order of the number ; Check for Armstrong Number ; If Armstrong number condition is satisfied ; Utility function to find the maximum sum of a subarray of size K ; If k is greater than N ; Find the sum of first subarray of size K ; Find the sum of the remaining subarray ; Return the maximum sum of subarray of size K ; Function to find all the Armstrong Numbers in the array ; Traverse the array arr [ ] ; If arr [ i ] is an Armstrong Number , then replace it by 1. Otherwise , 0 ; Return the resultant count ; Driver Code
def power ( x , y ) : NEW_LINE INDENT if ( y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( y % 2 == 0 ) : NEW_LINE INDENT return power ( x , y // 2 ) * power ( x , y // 2 ) NEW_LINE DEDENT return x * power ( x , y // 2 ) * power ( x , y // 2 ) NEW_LINE DEDENT def order ( num ) : NEW_LINE INDENT count = 0 NEW_LINE while ( num ) : NEW_LINE INDENT count += 1 NEW_LINE num = num // 10 NEW_LINE DEDENT return count NEW_LINE DEDENT def isArmstrong ( N ) : NEW_LINE INDENT r = order ( N ) NEW_LINE temp = N NEW_LINE sum = 0 NEW_LINE while ( temp ) : NEW_LINE INDENT d = temp % 10 NEW_LINE sum += power ( d , r ) NEW_LINE temp = temp // 10 NEW_LINE DEDENT return ( sum == N ) NEW_LINE DEDENT def maxSum ( arr , N , K ) : NEW_LINE INDENT if ( N < K ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT res += arr [ i ] NEW_LINE DEDENT curr_sum = res NEW_LINE for i in range ( K , N , 1 ) : NEW_LINE INDENT curr_sum += arr [ i ] - arr [ i - K ] NEW_LINE res = max ( res , curr_sum ) NEW_LINE DEDENT return res NEW_LINE DEDENT def maxArmstrong ( arr , N , K ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] = isArmstrong ( arr [ i ] ) NEW_LINE DEDENT return maxSum ( arr , N , K ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 28 , 2 , 3 , 6 , 153 , 99 , 828 , 24 ] NEW_LINE K = 6 NEW_LINE N = len ( arr ) NEW_LINE print ( maxArmstrong ( arr , N , K ) ) NEW_LINE DEDENT
Count N | Function to calculate the power of n ^ k % p ; Update x if it is more than or equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now y = y >> 1 y = y / 2 ; Function to count the number of arrays satisfying required conditions ; Calculating N ^ K ; Driver Code
def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def countArrays ( n , k ) : NEW_LINE INDENT mod = 1000000007 NEW_LINE ans = power ( n , k , mod ) NEW_LINE return ans NEW_LINE DEDENT n = 3 NEW_LINE k = 5 NEW_LINE ans = countArrays ( n , k ) NEW_LINE print ( ans ) NEW_LINE
Count ways to replace ' ? ' in a Binary String to make the count of 0 s and 1 s same as that of another string | Function to find the factorial of the given number N ; Stores the factorial ; Iterate over the range [ 2 , N ] ; Return the resultant result ; Function to find the number of ways of choosing r objects from n distinct objects ; Function to find the number of ways to replace ' ? ' in string t to get the same count of 0 s and 1 s in the string S and T ; Traverse the string s ; If the current character is 1 ; Update the value of the sum1 ; Otherwise ; Traverse the string t ; If the current character is 1 , then update the value of sum2 ; If the current character is 0 ; Otherwise , update the value of K ; Check if P is greater than K or if K - P is odd ; Print the count of ways ; Driver Code
def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return fact ( n ) // ( fact ( r ) * fact ( n - r ) ) NEW_LINE DEDENT def countWays ( s , t ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE K = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT sum1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum1 -= 1 NEW_LINE DEDENT DEDENT m = len ( t ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( t [ i ] == '1' ) : NEW_LINE INDENT sum2 += 1 NEW_LINE DEDENT elif ( t [ i ] == '0' ) : NEW_LINE INDENT sum2 -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT K += 1 NEW_LINE DEDENT DEDENT P = abs ( sum1 - sum2 ) NEW_LINE if ( P > K or ( K - P ) % 2 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT print ( nCr ( K , ( P + K ) // 2 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = "1010" NEW_LINE S2 = "10 ? ? " NEW_LINE countWays ( S1 , S2 ) NEW_LINE DEDENT
Count pairs of indices having sum of indices same as the sum of elements at those indices | Function to find all possible pairs of the given array such that the sum of arr [ i ] + arr [ j ] is i + j ; Stores the total count of pairs ; Iterate over the range ; Iterate over the range ; Print the total count ; Driver code
def countPairs ( arr , N ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if arr [ i ] + arr [ j ] == i + j : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT DEDENT print ( answer ) NEW_LINE DEDENT arr = [ 0 , 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE countPairs ( arr , N ) NEW_LINE
Distribute R , B beans such that each packet has at least 1 R and 1 B bean with absolute difference at most D | Function to check if it is possible to distribute R red and B blue beans in packets such that the difference between the beans in each packet is atmost D ; Check for the condition to distributing beans ; Print the answer ; Distribution is not possible ; Driver Code
def checkDistribution ( R , B , D ) : NEW_LINE INDENT if ( max ( R , B ) <= min ( R , B ) * ( D + 1 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT R = 1 NEW_LINE B = 1 NEW_LINE D = 0 NEW_LINE checkDistribution ( R , B , D ) NEW_LINE
Generate a sequence with product N such that for every pair of indices ( i , j ) and i < j , arr [ j ] is divisible by arr [ i ] | Python3 program for the above approach ; Function to calculate the prime factors of N with their count ; Initialize a vector , say v ; Initialize the count ; Count the number of divisors ; Divide the value of N by 2 ; For factor 2 divides it ; Find all prime factors ; Count their frequency ; Push it to the vector ; Push N if it is not 1 ; Function to print the array that have the maximum size ; Stores the all prime factor and their powers ; Traverse the vector and find the maximum power of prime factor ; If max size is less than 2 ; Otherwise ; Print the maximum size of sequence ; Print the final sequence ; Print the prime factor ; Print the last value of the sequence ; Driver Code
from math import sqrt NEW_LINE def primeFactor ( N ) : NEW_LINE INDENT v = [ ] NEW_LINE count = 0 NEW_LINE while ( ( N % 2 ) == 0 ) : NEW_LINE INDENT N >>= 1 NEW_LINE count += 1 NEW_LINE DEDENT if ( count ) : NEW_LINE INDENT v . append ( [ 2 , count ] ) NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( N ) ) + 1 , 2 ) : NEW_LINE INDENT count = 0 NEW_LINE while ( N % i == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE N = N / i NEW_LINE DEDENT if ( count ) : NEW_LINE INDENT v . append ( [ i , count ] ) NEW_LINE DEDENT DEDENT if ( N > 2 ) : NEW_LINE INDENT v . append ( [ N , 1 ] ) NEW_LINE DEDENT return v NEW_LINE DEDENT def printAnswer ( n ) : NEW_LINE INDENT v = primeFactor ( n ) NEW_LINE maxi_size = 0 NEW_LINE prime_factor = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if ( maxi_size < v [ i ] [ 1 ] ) : NEW_LINE INDENT maxi_size = v [ i ] [ 1 ] NEW_LINE prime_factor = v [ i ] [ 0 ] NEW_LINE DEDENT DEDENT if ( maxi_size < 2 ) : NEW_LINE INDENT print ( 1 , n ) NEW_LINE DEDENT else : NEW_LINE INDENT product = 1 NEW_LINE print ( maxi_size ) NEW_LINE for i in range ( maxi_size - 1 ) : NEW_LINE INDENT print ( prime_factor , end = " ▁ " ) NEW_LINE product *= prime_factor NEW_LINE DEDENT print ( n // product ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 360 NEW_LINE printAnswer ( N ) NEW_LINE DEDENT
Check if it is possible to reach destination in even number of steps in an Infinite Matrix | Function to check destination can be reached from source in even number of steps ; Coordinates differences ; Minimum number of steps required ; Minsteps is even ; Minsteps is odd ; Driver Code ; Given Input ; Function Call
def IsEvenPath ( Source , Destination ) : NEW_LINE INDENT x_dif = abs ( Source [ 0 ] - Destination [ 0 ] ) NEW_LINE y_dif = abs ( Source [ 1 ] - Destination [ 1 ] ) NEW_LINE minsteps = x_dif + y_dif NEW_LINE if ( minsteps % 2 == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Source = [ 2 , 1 ] NEW_LINE Destination = [ 1 , 4 ] NEW_LINE IsEvenPath ( Source , Destination ) NEW_LINE DEDENT
Find the closest Fraction to given fraction having minimum absolute difference | Function to find the absolute value of x ; Function to find the fraction with minimum absolute difference ; Initialize the answer variables ; Iterate over the range ; Nearest fraction ; x / y - d / i < x / y - A / B ( B * x - y * A ) * ( i * y ) > ( i * x - y * d ) * ( B * y ) ; Check for d + 1 ; Print the answer ; Driver Code
def ABS ( x ) : NEW_LINE INDENT return max ( x , - x ) NEW_LINE DEDENT def findFraction ( x , y , n ) : NEW_LINE INDENT A = - 1 NEW_LINE B = - 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT d = ( i * x ) // y NEW_LINE if ( d >= 0 and ( A == - 1 or ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) : NEW_LINE INDENT A = d NEW_LINE B = i NEW_LINE DEDENT d += 1 NEW_LINE if ( d >= 0 and ( A == - 1 or ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) : NEW_LINE INDENT A = d NEW_LINE B = i NEW_LINE DEDENT DEDENT print ( str ( A ) + " / " + str ( B ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 3 NEW_LINE y = 7 NEW_LINE n = 6 NEW_LINE findFraction ( x , y , n ) NEW_LINE DEDENT
Represent a number N in base | Function to convert N to equivalent representation in base - 2 ; Stores the required answer ; Iterate until N is not equal to zero ; If N is Even ; Add char '0' in front of string ; Add char '1' in front of string ; Decrement N by 1 ; Divide N by - 2 ; If string is empty , that means N is zero ; Put '0' in string s ; Given Input ; Function Call
def BaseConversion ( N ) : NEW_LINE INDENT s = " " NEW_LINE while ( N != 0 ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT s = "0" + s NEW_LINE DEDENT else : NEW_LINE INDENT s = "1" + s NEW_LINE N -= 1 NEW_LINE DEDENT N /= - 2 NEW_LINE DEDENT if ( s == " " ) : NEW_LINE INDENT s = "0" NEW_LINE DEDENT return s NEW_LINE DEDENT N = - 9 NEW_LINE print ( BaseConversion ( N ) ) NEW_LINE