codeparrot/xlcost-text-to-code huggingface dataset:
Split an array into minimum number of non | Function to split the array into minimum count of subarrays such that each subarray is either non - increasing or non - decreasing ; Initialize variable to keep track of current sequence ; Stores the required result ; Traverse the array , arr [ ] ; If current sequence is neither non - increasing nor non - decreasing ; If previous element is greater ; Update current ; If previous element is equal to the current element ; Update current ; Otherwise ; Update current ; If current sequence is in non - decreasing ; I f previous element is less than or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; If current sequence is Non - Increasing ; If previous element is greater or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; Print the answer ; Driver Code ; Given array ; Given size of array
def minimumSubarrays ( arr , n ) : NEW_LINE INDENT current = ' N ' NEW_LINE answer = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( current == ' N ' ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT current = ' D ' NEW_LINE DEDENT elif ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT current = ' N ' NEW_LINE DEDENT else : NEW_LINE INDENT current = ' I ' NEW_LINE DEDENT DEDENT elif ( current == ' I ' ) : NEW_LINE INDENT if ( arr [ i ] >= arr [ i - 1 ] ) : NEW_LINE INDENT current = ' I ' NEW_LINE DEDENT else : NEW_LINE INDENT current = ' N ' NEW_LINE answer += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( arr [ i ] <= arr [ i - 1 ] ) : NEW_LINE INDENT current = ' D ' NEW_LINE DEDENT else : NEW_LINE INDENT current = ' N ' NEW_LINE answer += 1 NEW_LINE DEDENT DEDENT DEDENT print ( answer ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 9 , 5 , 4 , 6 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE minimumSubarrays ( arr , n ) NEW_LINE DEDENT
Convert a Matrix into another Matrix of given dimensions | Function to construct a matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , mat [ ] [ ] ; Update idx ; Print the resultant matrix ; Driver Code
def ConstMatrix ( mat , N , M , A , B ) : NEW_LINE INDENT if ( N * M != A * B ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT idx = 0 ; NEW_LINE res = [ [ 0 for i in range ( B ) ] for i in range ( A ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT res [ idx // B ] [ idx % B ] = mat [ i ] [ j ] ; NEW_LINE idx += 1 NEW_LINE DEDENT DEDENT for i in range ( A ) : NEW_LINE INDENT for j in range ( B ) : NEW_LINE INDENT print ( res [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] ] NEW_LINE A = 2 NEW_LINE B = 3 NEW_LINE N = len ( mat ) NEW_LINE M = len ( mat [ 0 ] ) NEW_LINE ConstMatrix ( mat , N , M , A , B ) NEW_LINE DEDENT
Modify given array by reducing each element by its next smaller element | Function to print the final array after reducing each array element by its next smaller element ; Initialize stack ; To store the corresponding element ; If stack is not empty ; If top element is smaller than the current element ; Keep popping until stack is empty or top element is greater than the current element ; If stack is not empty ; Push current element ; Final array ; Driver Code ; Given array ; Function Call
def printFinalPrices ( arr ) : NEW_LINE INDENT minStk = [ ] NEW_LINE reduce = [ 0 ] * len ( arr ) NEW_LINE for i in range ( len ( arr ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if minStk : NEW_LINE INDENT if minStk [ - 1 ] <= arr [ i ] : NEW_LINE INDENT reduce [ i ] = minStk [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT while minStk and minStk [ - 1 ] > arr [ i ] : NEW_LINE INDENT minStk . pop ( ) NEW_LINE DEDENT if minStk : NEW_LINE INDENT reduce [ i ] = minStk [ - 1 ] NEW_LINE DEDENT DEDENT DEDENT minStk . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( len ( arr ) ) : NEW_LINE INDENT print ( arr [ i ] - reduce [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 4 , 6 , 2 , 3 ] NEW_LINE printFinalPrices ( arr ) NEW_LINE DEDENT
Generate array having differences between count of occurrences of every array element on its left and right | Function to construct array of differences of counts on the left and right of the given array ; Initialize left and right frequency arrays ; Construct left cumulative frequency table ; Construct right cumulative frequency table ; Print the result ; Driver Code ; Function Call
def constructArray ( A , N ) : NEW_LINE INDENT left = [ 0 ] * ( N + 1 ) NEW_LINE right = [ 0 ] * ( N + 1 ) NEW_LINE X = [ 0 ] * ( N + 1 ) NEW_LINE Y = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT X [ i ] = left [ A [ i ] ] NEW_LINE left [ A [ i ] ] += 1 NEW_LINE DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT Y [ i ] = right [ A [ i ] ] NEW_LINE right [ A [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT print ( Y [ i ] - X [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 2 , 1 , 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE constructArray ( A , N ) NEW_LINE DEDENT
Generate an alternate odd | Function to prthe required sequence ; Print ith odd number ; Driver Code
def findNumbers ( n ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i <= n ) : NEW_LINE INDENT print ( 2 * i * i + 4 * i + 1 + i % 2 , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT n = 6 NEW_LINE findNumbers ( n ) NEW_LINE
Program to calculate gross salary of a person | Function to calculate the salary of the person ; Condition to compute the allowance for the person ; Calculate gross salary ; Driver code ; Function call
def computeSalary ( basic , grade ) : NEW_LINE INDENT hra = 0.2 * basic NEW_LINE da = 0.5 * basic NEW_LINE pf = 0.11 * basic NEW_LINE if grade == ' A ' : NEW_LINE INDENT allowance = 1700.0 NEW_LINE DEDENT elif grade == ' B ' : NEW_LINE INDENT allowance = 1500.0 NEW_LINE DEDENT else : NEW_LINE INDENT allowance = 1300.0 ; NEW_LINE DEDENT gross = round ( basic + hra + da + allowance - pf ) NEW_LINE return gross NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT basic = 10000 NEW_LINE grade = ' A ' NEW_LINE print ( computeSalary ( basic , grade ) ) ; NEW_LINE DEDENT
Rearrange and update array elements as specified by the given queries | Python3 program to implement the above approach ; Function to perform the given operations ; Dequeue to store the array elements ; Insert all element of the array into the dequeue ; Stores the size of the queue ; Traverse each query ; Query for left shift . ; Extract the element at the front of the queue ; Pop the element at the front of the queue ; Push the element at the back of the queue ; Query for right shift ; Extract the element at the back of the queue ; Pop the element at the back of the queue ; Push the element at the front of the queue ; Query for update ; Query to get the value ; Driver Code ; All possible Queries
from collections import deque NEW_LINE def Queries ( arr , N , Q ) : NEW_LINE INDENT dq = deque ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT dq . append ( arr [ i ] ) NEW_LINE DEDENT sz = len ( Q ) NEW_LINE for i in range ( sz ) : NEW_LINE INDENT if ( Q [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT front = dq [ 0 ] NEW_LINE dq . popleft ( ) NEW_LINE dq . appendleft ( front ) NEW_LINE DEDENT elif ( Q [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT back = dq [ N - 1 ] NEW_LINE dq . popleft ( ) NEW_LINE dq . appendleft ( back ) NEW_LINE DEDENT elif ( Q [ i ] [ 0 ] == 2 ) : NEW_LINE INDENT dq [ Q [ i ] [ 1 ] ] = Q [ i ] [ 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT print ( dq [ Q [ i ] [ 1 ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE Q = [ [ 0 ] , [ 1 ] , [ 3 , 1 ] , [ 2 , 2 , 54 ] , [ 3 , 2 ] ] NEW_LINE Queries ( arr , N , Q ) NEW_LINE DEDENT
Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal | Python3 program to implement the above approach ; Function ot find the length of the smallest subarray ; Stores ( prefix Sum , index ) as ( key , value ) mappings ; Iterate till N ; Update the prefixSum ; Update the length ; Put the latest index to find the minimum length ; Update the length ; Return the answer ; Function to find the length of the largest subarray ; Stores the sum of all array elements after modification ; Change greater than k to 1 ; Change smaller than k to - 1 ; Change equal to k to 0 ; Update total_sum ; No deletion required , return 0 ; Delete smallest subarray that has sum = total_sum ; Driver Code ; Function call
import sys NEW_LINE def smallSubarray ( arr , n , total_sum ) : NEW_LINE INDENT m = { } NEW_LINE length = sys . maxsize NEW_LINE prefixSum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefixSum += arr [ i ] NEW_LINE if ( prefixSum == total_sum ) : NEW_LINE INDENT length = min ( length , i + 1 ) NEW_LINE DEDENT m [ prefixSum ] = i NEW_LINE if ( ( prefixSum - total_sum ) in m . keys ( ) ) : NEW_LINE INDENT length = min ( length , i - m [ prefixSum - total_sum ] ) NEW_LINE DEDENT DEDENT return length NEW_LINE DEDENT def smallestSubarrayremoved ( arr , n , k ) : NEW_LINE INDENT total_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > k ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT elif ( arr [ i ] < k ) : NEW_LINE INDENT arr [ i ] = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = 0 NEW_LINE DEDENT total_sum += arr [ i ] NEW_LINE DEDENT if ( total_sum == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return smallSubarray ( arr , n , total_sum ) NEW_LINE DEDENT DEDENT arr = [ 12 , 16 , 12 , 13 , 10 ] NEW_LINE K = 13 NEW_LINE n = len ( arr ) NEW_LINE print ( smallestSubarrayremoved ( arr , n , K ) ) NEW_LINE
Balanced Ternary Number System | Python3 program to convert positive decimals into balanced ternary system ; Driver Code
def balancedTernary ( n ) : NEW_LINE INDENT output = " " NEW_LINE while ( n > 0 ) : NEW_LINE INDENT rem = n % 3 NEW_LINE n = n // 3 NEW_LINE if ( rem == 2 ) : NEW_LINE INDENT rem = - 1 NEW_LINE n += 1 NEW_LINE DEDENT if ( rem == 0 ) : NEW_LINE INDENT output = '0' + output NEW_LINE DEDENT else : NEW_LINE INDENT if ( rem == 1 ) : NEW_LINE INDENT output = '1' + output NEW_LINE DEDENT else : NEW_LINE INDENT output = ' Z ' + output NEW_LINE DEDENT DEDENT DEDENT return output NEW_LINE DEDENT n = 238 NEW_LINE print ( " Equivalent ▁ Balanced ▁ Ternary ▁ of " , n , " is : " , balancedTernary ( n ) ) NEW_LINE
Spt function or Smallest Parts Function of a given number | Python3 implementation to find the Spt Function to given number ; Variable to store spt function of a number ; Function to add value of frequency of minimum element among all representations of n ; Find the value of frequency of minimum element ; Calculate spt ; Recursive function to find different ways in which n can be written as a sum of at one or more positive integers ; If sum becomes n , consider this representation ; Start from previous element in the representation till n ; Include current element from representation ; Call function again with reduced sum ; Backtrack - remove current element from representation ; Function to find the spt function ; Using recurrence find different ways in which n can be written as a sum of at 1 or more positive integers ; Driver Code
import sys NEW_LINE spt = 0 NEW_LINE def printVector ( arr ) : NEW_LINE INDENT global spt NEW_LINE min_i = sys . maxsize NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT min_i = min ( min_i , arr [ i ] ) NEW_LINE DEDENT freq = arr . count ( min_i ) NEW_LINE spt += freq NEW_LINE DEDENT def findWays ( arr , i , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT printVector ( arr ) NEW_LINE DEDENT for j in range ( i , n + 1 ) : NEW_LINE INDENT arr . append ( j ) NEW_LINE findWays ( arr , j , n - j ) NEW_LINE del arr [ - 1 ] NEW_LINE DEDENT DEDENT def spt_function ( n ) : NEW_LINE INDENT arr = [ ] NEW_LINE findWays ( arr , 1 , n ) NEW_LINE print ( spt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE spt_function ( N ) NEW_LINE DEDENT
Find initial integral solution of Linear Diophantine equation if finite solution exists | Python3 program for the above approach ; Function to implement the extended euclid algorithm ; Base Case ; Recursively find the gcd ; Function to prlet the solutions of the given equations ax + by = c ; Condition for infinite solutions ; Condition for no solutions exist ; Condition for no solutions exist ; Print the solution ; Given coefficients ; Function Call
import math NEW_LINE x , y = 0 , 0 NEW_LINE def gcd_extend ( a , b ) : NEW_LINE INDENT global x , y NEW_LINE if ( b == 0 ) : NEW_LINE INDENT x = 1 NEW_LINE y = 0 NEW_LINE return a NEW_LINE DEDENT else : NEW_LINE INDENT g = gcd_extend ( b , a % b ) NEW_LINE x1 , y1 = x , y NEW_LINE x = y1 NEW_LINE y = x1 - math . floor ( a / b ) * y1 NEW_LINE return g NEW_LINE DEDENT DEDENT def print_solution ( a , b , c ) : NEW_LINE INDENT if ( a == 0 and b == 0 ) : NEW_LINE INDENT if ( c == 0 ) : NEW_LINE INDENT print ( " Infinite ▁ Solutions ▁ Exist " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Solution ▁ exists " ) NEW_LINE DEDENT DEDENT gcd = gcd_extend ( a , b ) NEW_LINE if ( c % gcd != 0 ) : NEW_LINE INDENT print ( " No ▁ Solution ▁ exists " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " x ▁ = ▁ " , int ( x * ( c / gcd ) ) , " , ▁ y ▁ = ▁ " , int ( y * ( c / gcd ) ) , sep = " " ) NEW_LINE DEDENT DEDENT a = 4 NEW_LINE b = 18 NEW_LINE c = 10 NEW_LINE print_solution ( a , b , c ) NEW_LINE
Count of pairs whose bitwise AND is a power of 2 | Function to check if x is power of 2 ; Returns true if x is a power of 2 ; Function to return the number of valid pairs ; Iterate for all possible pairs ; Bitwise and value of the pair is passed ; Return the final count ;  ; Given array ; Function Call
def check ( x ) : NEW_LINE INDENT return x and ( not ( x & ( x - 1 ) ) ) NEW_LINE DEDENT def count ( arr , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if check ( arr [ i ] & arr [ j ] ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT / * Driver Code * / NEW_LINE arr = [ 6 , 4 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( count ( arr , n ) ) NEW_LINE
Check if the Matrix follows the given constraints or not | Python3 implementation of the above approach ; Function checks if n is prime or not ; Corner case ; Check from 2 to sqrt ( n ) ; Function returns sum of all elements of matrix ; Stores the sum of the matrix ; Function to check if all a [ i ] [ j ] with prime ( i + j ) are prime ; If index is prime ; If element not prime ; Driver code ; Check for both conditions
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def takeSum ( a , n , m ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , m ) : NEW_LINE INDENT sum += a [ i ] [ j ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def checkIndex ( n , m , a ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , m ) : NEW_LINE INDENT if ( isPrime ( i + j ) ) : NEW_LINE INDENT if ( isPrime ( a [ i ] [ j ] ) != True ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT n = 4 NEW_LINE m = 5 NEW_LINE a = [ [ 1 , 2 , 3 , 2 , 2 ] , [ 2 , 2 , 7 , 7 , 7 ] , [ 7 , 7 , 21 , 7 , 10 ] , [ 2 , 2 , 3 , 6 , 7 ] ] NEW_LINE sum = takeSum ( a , n , m ) NEW_LINE if ( isPrime ( sum ) and checkIndex ( n , m , a ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Count of all possible pairs of array elements with same parity | Function to return the answer ; Generate all possible pairs ; Increment the count if both even or both odd ; Driver Code
def countPairs ( A , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( ( A [ i ] % 2 == 0 and A [ j ] % 2 == 0 ) or ( A [ i ] % 2 != 0 and A [ j ] % 2 != 0 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 , 1 , 3 ] NEW_LINE n = len ( A ) NEW_LINE print ( countPairs ( A , n ) ) NEW_LINE DEDENT
Smallest number whose sum of digits is square of N | Function to return smallest number whose sum of digits is n ^ 2 ; Driver Code
def smallestNum ( n ) : NEW_LINE INDENT return ( ( n * n % 9 + 1 ) * pow ( 10 , int ( n * n / 9 ) ) - 1 ) NEW_LINE DEDENT N = 4 NEW_LINE print ( smallestNum ( N ) ) NEW_LINE
Second heptagonal numbers | Function to find N - th term in the series ; Driver code ; Function call
def findNthTerm ( n ) : NEW_LINE INDENT print ( n * ( 5 * n + 3 ) // 2 ) NEW_LINE DEDENT N = 4 NEW_LINE findNthTerm ( N ) NEW_LINE
Weakly Prime Numbers | function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to check if n is a weakly prime Number ; number should be prime ; converting N to string ; loop to change digit at every character one by one . ; loop to store every digit one by one at index j ; Driver code
def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( n ** 0.5 ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isWeaklyPrimeNum ( N ) : NEW_LINE INDENT if ( isPrime ( N ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT s = str ( N ) NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT str1 = s NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT c = str ( i ) NEW_LINE str1 = str1 [ : j ] + c + str1 [ j + 1 : ] NEW_LINE Num = int ( str1 ) NEW_LINE if ( str1 [ j ] != s [ j ] and isPrime ( Num ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT n = 294001 NEW_LINE if ( isWeaklyPrimeNum ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Unprimeable Numbers | function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to check if n is a unprimeable Number ; number should be composite ; converting N to string ; loop to change digit at every character one by one . ; loop to store every digit one by one at index j ; Driver code ; Function Call
def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( n ** 0.5 ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isUnPrimeableNum ( N ) : NEW_LINE INDENT if ( isPrime ( N ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT s = str ( N ) NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT str1 = s NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT c = str ( i ) NEW_LINE str1 = str1 [ : j ] + c + str1 [ j + 1 : ] NEW_LINE Num = int ( str1 ) NEW_LINE if ( str1 [ j ] != s [ j ] and isPrime ( Num ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT n = 200 NEW_LINE if ( isUnPrimeableNum ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the Nth row in Pascal 's Triangle | Function to find the elements of rowIndex in Pascal 's Triangle ; 1 st element of every row is 1 ; Check if the row that has to be returned is the first row ; Generate the previous row ; Generate the elements of the current row by the help of the previous row ; Return the row ; Driver Program
def getRow ( rowIndex ) : NEW_LINE INDENT currow = [ ] NEW_LINE currow . append ( 1 ) NEW_LINE if ( rowIndex == 0 ) : NEW_LINE INDENT return currow NEW_LINE DEDENT prev = getRow ( rowIndex - 1 ) NEW_LINE for i in range ( 1 , len ( prev ) ) : NEW_LINE INDENT curr = prev [ i - 1 ] + prev [ i ] NEW_LINE currow . append ( curr ) NEW_LINE DEDENT currow . append ( 1 ) NEW_LINE return currow NEW_LINE DEDENT n = 3 NEW_LINE arr = getRow ( n ) NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( i == ( len ( arr ) - 1 ) ) : NEW_LINE INDENT print ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ i ] , end = " , ▁ " ) NEW_LINE DEDENT DEDENT
All possible values of floor ( N / K ) for all values of K | Function to print all possible values of floor ( N / K ) ; Iterate from 1 to N + 1 ; Driver code
def allQuotients ( N ) : NEW_LINE INDENT s = set ( ) NEW_LINE for k in range ( 1 , N + 2 ) : NEW_LINE INDENT s . add ( N // k ) NEW_LINE DEDENT for it in s : NEW_LINE INDENT print ( it , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE allQuotients ( N ) NEW_LINE DEDENT
Greatest odd factor of an even number | Python3 program for the above approach ; Function to print greatest odd factor ; Initialize i with 1 ; Iterate till i <= pow_2 ; find the pow ( 2 , i ) ; If factor is odd , then print the number and break ; Given Number ; Function Call
import math NEW_LINE def greatestOddFactor ( n ) : NEW_LINE INDENT pow_2 = int ( math . log ( n , 2 ) ) NEW_LINE i = 1 NEW_LINE while i <= pow_2 : NEW_LINE INDENT fac_2 = ( 2 ** i ) NEW_LINE if ( n % fac_2 == 0 ) : NEW_LINE if ( ( n // fac_2 ) % 2 == 1 ) : NEW_LINE INDENT print ( n // fac_2 ) NEW_LINE break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT N = 8642 NEW_LINE greatestOddFactor ( N ) NEW_LINE