codeparrot/xlcost-text-to-code huggingface dataset:
Find the Kth number which is not divisible by N | Python3 implementation to find the K 'th non-divisible number of N ; Function to find the Kth not divisible by N ; Driver Code ; Function Call
import math NEW_LINE def kthNonDivisible ( N , K ) : NEW_LINE INDENT return K + math . floor ( ( K - 1 ) / ( N - 1 ) ) NEW_LINE DEDENT N = 3 NEW_LINE K = 6 NEW_LINE print ( kthNonDivisible ( N , K ) ) NEW_LINE
Find the maximum sum pair in an Array with even parity | Python3 implementation to find a pair with even parity and maximum sum ; Function that returns true if count of set bits in given number is even ; Parity will store the count of set bits ; Function to print the elements of the array ; Function to remove all the even parity elements from the given array ; Traverse the array ; If the current element has even parity ; Driver Code ; Function call
import sys NEW_LINE sz = 1e3 NEW_LINE def isEvenParity ( x ) : NEW_LINE INDENT parity = 0 NEW_LINE while x != 0 : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT parity = parity + 1 ; NEW_LINE DEDENT x = x >> 1 NEW_LINE DEDENT if ( parity % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT def findPairEvenParity ( arr , n ) : NEW_LINE INDENT firstMaximum = - 1 NEW_LINE secondMaximum = - 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if isEvenParity ( arr [ i ] ) == True : NEW_LINE INDENT if ( arr [ i ] >= firstMaximum ) : NEW_LINE INDENT secondMaximum = firstMaximum NEW_LINE firstMaximum = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] >= secondMaximum ) : NEW_LINE INDENT secondMaximum = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT print ( firstMaximum , secondMaximum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 18 , 15 , 8 , 9 , 14 ] NEW_LINE n = len ( arr ) NEW_LINE findPairEvenParity ( arr , n ) NEW_LINE DEDENT
Program to check if N is a Hexagonal Number or not | Python3 program to check if N is a hexagonal number ; Function to check if number is hexagonal ; Calculate the value for n ; Check if n - floor ( n ) is equal to 0 ; Driver code
from math import sqrt NEW_LINE def isHexagonal ( N ) : NEW_LINE INDENT val = 8 * N + 1 NEW_LINE x = 1 + sqrt ( val ) NEW_LINE n = x / 4 NEW_LINE if ( ( n - int ( n ) ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 14 NEW_LINE if ( isHexagonal ( N ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Program to convert Number in characters | Python3 program to convert number in characters ; To calculate the reverse of the number ; The remainder will give the last digit of the number ; Extract the first digit of the reversed number ; Match it with switch case ; Divide the number by 10 to get the next number ; Driver code
def NumbertoCharacter ( n ) : NEW_LINE INDENT rev = 0 ; r = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT r = n % 10 ; NEW_LINE rev = rev * 10 + r ; NEW_LINE n = n // 10 ; NEW_LINE DEDENT while ( rev > 0 ) : NEW_LINE INDENT r = rev % 10 ; NEW_LINE switcher = { 0 : " zero ▁ " , 1 : " one ▁ " , 2 : " two ▁ " , 3 : " three ▁ " , 4 : " four ▁ " , 5 : " five ▁ " , 6 : " six ▁ " , 7 : " seven ▁ " , 8 : " eight ▁ " , 9 : " nine ▁ " } NEW_LINE print ( switcher . get ( r , " UnValid " ) , end = " ▁ " ) ; NEW_LINE rev = rev // 10 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12345 ; NEW_LINE NumbertoCharacter ( n ) ; NEW_LINE DEDENT
Count all subarrays whose sum can be split as difference of squares of two Integers | Function to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; Loop to iterate over all the possible subsequences of the array ; Finding the sum of all the possible subsequences ; Condition to check whether the number can be split as difference of squares ; Driver code
def Solve ( arr , n ) : NEW_LINE INDENT temp = 0 ; count = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT temp = 0 ; NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT temp = temp + arr [ j ] ; NEW_LINE if ( ( temp + 2 ) % 4 != 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( Solve ( arr , N ) ) ; NEW_LINE
Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M | Python3 program to find the product of all the combinations of M elements from an array whose index in the sorted order divides M completely ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; If y is odd , multiply x with result ; y must be even now ; Iterative Function to calculate ( nCr ) % p and save in f [ n ] [ r ] C ( n , r ) % p = [ C ( n - 1 , r - 1 ) % p + C ( n - 1 , r ) % p ] % p and C ( n , 0 ) = C ( n , n ) = 1 ; If j > i then C ( i , j ) = 0 ; If i is equal to j then C ( i , j ) = 1 ; C ( i , j ) = ( C ( i - 1 , j ) + C ( i - 1 , j - 1 ) ) % p ; Initialize the answer ; For every element arr [ i ] , x is count of occurrence of arr [ i ] in different set such that index of arr [ i ] in those sets divides m completely . ; Finding the count of arr [ i ] by placing it at the index which divides m completely ; Using fermat 's little theorem ; Multiplying with the count ; Driver code
m = 4 NEW_LINE def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def nCr ( n , p , f ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m + 1 ) : NEW_LINE INDENT if ( j > i ) : NEW_LINE INDENT f [ i ] [ j ] = 0 NEW_LINE DEDENT elif ( j == 0 or j == i ) : NEW_LINE INDENT f [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT f [ i ] [ j ] = ( ( f [ i - 1 ] [ j ] + f [ i - 1 ] [ j - 1 ] ) % p ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def operations ( arr , n , f ) : NEW_LINE INDENT p = 1000000007 NEW_LINE nCr ( n , p - 1 , f ) NEW_LINE arr . sort ( ) NEW_LINE ans = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = 0 NEW_LINE for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( m % j == 0 ) : NEW_LINE INDENT x = ( ( x + ( f [ n - i - 1 ] [ m - j ] * f [ i ] [ j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ) NEW_LINE DEDENT DEDENT ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 5 , 7 , 9 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE f = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE operations ( arr , n , f ) NEW_LINE DEDENT
Check whether a number can be represented by the product of two squares | Function to check if there exist two numbers product of whose squares is n . ; Check whether the product of the square of both numbers is equal to N ; Driver code
def prodSquare ( n ) : NEW_LINE INDENT for i in range ( 2 , ( n ) + 1 ) : NEW_LINE INDENT if ( i * i < ( n + 1 ) ) : NEW_LINE INDENT for j in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( ( i * i * j * j ) == n ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 25 ; NEW_LINE if ( prodSquare ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Check whether a number can be represented by the product of two squares | Function to check if there exist two numbers product of whose squares is n ; Initialize dict / map ; Store square value in hashmap ; Driver Code
def prodSquare ( n ) : NEW_LINE INDENT s = dict ( ) NEW_LINE i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT s [ i * i ] = 1 NEW_LINE if ( ( n // ( i * i ) ) in s ) : NEW_LINE INDENT return True NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 25 NEW_LINE if ( prodSquare ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Print N distinct numbers following the given operations | Function to print the required array ; Check if number is a multiple of 4 ; Printing Left Half of the array ; Printing Right Half of the array ; Driver code
def printArr ( n ) : NEW_LINE INDENT if ( n % 4 == 0 ) : NEW_LINE INDENT for i in range ( 1 , ( n / 2 ) + 1 ) : NEW_LINE INDENT print ( i * 2 , end = " ▁ " ) NEW_LINE DEDENT for i in range ( 1 , n / 2 ) : NEW_LINE INDENT print ( i * 2 - 1 , end = " ▁ " ) NEW_LINE DEDENT print ( n + n / 2 - 1 , end =   " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT n = 22 NEW_LINE printArr ( n ) NEW_LINE
Sum of product of all subsets formed by only divisors of N | Function to find the sum of product of all the subsets formed by only divisors of N ; Vector to store all the divisors of n ; Loop to find out the divisors of N ; Both ' i ' and ' n / i ' are the divisors of n ; Check if ' i ' and ' n / i ' are equal or not ; Calculating the answer ; Excluding the value of the empty set ; Driver Code
def GetSum ( n ) : NEW_LINE INDENT divisors = [ ] NEW_LINE i = 1 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT divisors . append ( i ) NEW_LINE if ( i != n // i ) : NEW_LINE INDENT divisors . append ( n // i ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT ans = 1 NEW_LINE for i in divisors : NEW_LINE INDENT ans *= ( i + 1 ) NEW_LINE DEDENT ans = ans - 1 NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( GetSum ( N ) ) NEW_LINE DEDENT
Length of longest Powerful number subsequence in an Array | Python3 program to find the length of Longest Powerful Subsequence in an Array ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return false ; Check if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to find the longest subsequence which contain all powerful numbers ; Driver code
import math NEW_LINE def isPowerful ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n //= 2 NEW_LINE power += 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT for factor in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % factor == 0 ) : NEW_LINE INDENT n = n // factor NEW_LINE power += 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return ( n == 1 ) NEW_LINE DEDENT def longestPowerfulSubsequence ( arr , n ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isPowerful ( arr [ i ] ) ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT arr = [ 6 , 4 , 10 , 13 , 9 , 25 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestPowerfulSubsequence ( arr , n ) ) NEW_LINE
Maximum OR value of a pair in an Array without using OR operator | Python3 implementation of the approach ; Function to return the maximum bitwise OR for any pair of the given array without using bitwise OR operation ; Find maximum element in the array ; Finding complement will set all unset bits in a number ; Iterate through all other array elements to find maximum AND value ; c will give the maximum value that could be added to max_value to produce maximum OR value ; Driver code
from math import log2 , floor NEW_LINE def maxOR ( arr , n ) : NEW_LINE INDENT max_value = max ( arr ) NEW_LINE number_of_bits = floor ( log2 ( max_value ) ) + 1 NEW_LINE complement = ( ( ( 1 << number_of_bits ) - 1 ) ^ max_value ) NEW_LINE c = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != max_value ) : NEW_LINE INDENT c = max ( c , ( complement & arr [ i ] ) ) NEW_LINE DEDENT DEDENT return ( max_value + c ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 8 , 16 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxOR ( arr , n ) ) NEW_LINE DEDENT
Find the XOR of the elements in the given range [ L , R ] with the value K for a given set of queries | Function to perform the update operation on the given array ; Converting the indices to 0 indexing . ; Saving the XOR of K from the starting index in the range [ L , R ] . ; Saving the XOR of K at the ending index in the given [ L , R ] . ; Function to display the resulting array ; Finding the resultant value in the result array ; Combining the effects of the updates with the original array without changing the initial array . ; Driver code ; Query 1 ; Query 2 ; Query 3
def update ( res , L , R , K ) : NEW_LINE INDENT L = L - 1 NEW_LINE R = R - 1 NEW_LINE res [ L ] = res [ L ] ^ K NEW_LINE res [ R + 1 ] = res [ R + 1 ] ^ K NEW_LINE DEDENT def display ( arr , res , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT res [ i ] = res [ i ] ^ res [ i - 1 ] NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] ^ res [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 4 , 6 , 8 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE res = [ 0 ] * N NEW_LINE L = 1 NEW_LINE R = 3 NEW_LINE K = 2 NEW_LINE update ( res , L , R , K ) NEW_LINE L = 2 NEW_LINE R = 4 NEW_LINE K = 3 NEW_LINE update ( res , L , R , K ) NEW_LINE display ( arr , res , N ) NEW_LINE
Shortest subarray to be removed to make all Array elements unique | Python3 program to make array elements pairwise distinct by removing at most one subarray of minimum length ; Function to check if elements of Prefix and suffix of each sub array of size K are pairwise distinct or not ; Hash map to store frequencies of elements of prefix and suffix ; Variable to store number of occurrences of an element other than one ; Adding frequency of elements of suffix to hash for subarray starting from first index There is no prefix for this sub array ; Counting extra elements in current Hash map ; If there are no extra elements return true ; Check for remaining sub arrays ; First element of suffix is now part of subarray which is being removed so , check for extra elements ; Decrement frequency of first element of the suffix ; Increment frequency of last element of the prefix ; Check for extra elements ; If there are no extra elements return true ; Function for calculating minimum length of the subarray , which on removing make all elements pairwise distinct ; Possible range of length of subarray ; Binary search to find minimum ans ; Driver code
from collections import defaultdict NEW_LINE def check ( a , n , k ) : NEW_LINE INDENT m = defaultdict ( int ) NEW_LINE extra = 0 NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT m [ a [ i ] ] += 1 NEW_LINE DEDENT for x in m : NEW_LINE INDENT extra += m [ x ] - 1 NEW_LINE DEDENT if ( extra == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 1 , i + k - 1 < n ) : NEW_LINE INDENT if ( m [ a [ i + k - 1 ] ] > 1 ) : NEW_LINE INDENT extra -= 1 NEW_LINE DEDENT m [ a [ i + k - 1 ] ] -= 1 NEW_LINE m [ a [ i - 1 ] ] += 1 NEW_LINE if ( m [ a [ i - 1 ] ] > 1 ) : NEW_LINE INDENT extra += 1 NEW_LINE DEDENT if ( extra == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def minlength ( a , n ) : NEW_LINE INDENT lo = 0 NEW_LINE hi = n + 1 NEW_LINE ans = 0 NEW_LINE while ( lo < hi ) : NEW_LINE INDENT mid = ( lo + hi ) // 2 NEW_LINE if ( check ( a , n , mid ) ) : NEW_LINE INDENT ans = mid NEW_LINE hi = mid NEW_LINE DEDENT else : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( minlength ( a , n ) ) NEW_LINE DEDENT
Find N distinct integers with zero sum | Function to print distinct n numbers such that their sum is 0 ; Print 2 symmetric numbers ; Print a extra 0 if N is odd ; Driver code
def findNumbers ( N ) : NEW_LINE INDENT for i in range ( 1 , N // 2 + 1 ) : NEW_LINE INDENT print ( i , end = ' , ▁ ' ) NEW_LINE print ( - i , end = ' , ▁ ' ) NEW_LINE DEDENT if N % 2 == 1 : NEW_LINE INDENT print ( 0 , end = ' ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE findNumbers ( N ) NEW_LINE DEDENT
Program to calculate Electricity Bill | Function to calculate the electricity bill ; Condition to find the charges bar in which the units consumed is fall ; Driver Code
def calculateBill ( units ) : NEW_LINE INDENT if ( units <= 100 ) : NEW_LINE INDENT return units * 10 ; NEW_LINE DEDENT elif ( units <= 200 ) : NEW_LINE INDENT return ( ( 100 * 10 ) + ( units - 100 ) * 15 ) ; NEW_LINE DEDENT elif ( units <= 300 ) : NEW_LINE INDENT return ( ( 100 * 10 ) + ( 100 * 15 ) + ( units - 200 ) * 20 ) ; NEW_LINE DEDENT elif ( units > 300 ) : NEW_LINE INDENT return ( ( 100 * 10 ) + ( 100 * 15 ) + ( 100 * 20 ) + ( units - 300 ) * 25 ) ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT units = 250 ; NEW_LINE print ( calculateBill ( units ) ) ; NEW_LINE
Find all divisors of first N natural numbers | Function to find the factors of the numbers from 1 to N ; Loop to find the factors of the first N natural numbers of the integer ; Driver Code
def factors ( n ) : NEW_LINE INDENT print ( "1 ▁ - - > 1" ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT print ( i , " ▁ - - > " , end = " " ) ; NEW_LINE for j in range ( 1 , int ( pow ( i , 1 ) ) ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT print ( j , " , ▁ " , end = " " ) ; NEW_LINE if ( i // j != j ) : NEW_LINE INDENT print ( i // j , " , ▁ " , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT print ( end =   " " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE factors ( n ) ; NEW_LINE DEDENT
Find all divisors of first N natural numbers | Python3 program to find the factors of first N natural numbers ; Initialize divisor list ( array ) of sequence container ; Calculate all divisors of a number ; Function to find the factors of first n natural numbers ; Driver code ; Function call
MAX = 100001 NEW_LINE divisor = [ [ ] for x in range ( MAX ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT for j in range ( i , MAX , i ) : NEW_LINE INDENT divisor [ j ] . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def findNFactors ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( i , " ▁ - - > ▁ " , end = ' ' ) NEW_LINE for divi in divisor [ i ] : NEW_LINE INDENT print ( divi , " , ▁ " , end = ' ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE sieve ( ) NEW_LINE findNFactors ( n ) NEW_LINE DEDENT
Maximum number of prime factors a number can have with exactly x factors | Python3 implementation to find the maximum count of the prime factors by the count of factors of number ; Function to count number of prime factors of x ; Count variable is incremented form every prime factor of x ; Loop to count the number of the prime factors of the given number ; Driver Code
import math NEW_LINE def countPrimeFactors ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT cnt = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n = n // 2 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n = n // i NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT x = 8 NEW_LINE prime_factor_cnt = countPrimeFactors ( x ) NEW_LINE print ( prime_factor_cnt ) NEW_LINE
Count of N digit palindromic numbers divisible by 9 | Function to find the count of N digits palindromic numbers which are divisible by 9 ; If N is odd ; If N is even ; Driver Code
def countPalindromic ( n ) : NEW_LINE INDENT count = 0 NEW_LINE if ( n % 2 == 1 ) : NEW_LINE INDENT count = pow ( 9 , ( n - 1 ) // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT count = pow ( 9 , ( n - 2 ) // 2 ) NEW_LINE DEDENT return count NEW_LINE DEDENT n = 3 NEW_LINE print ( countPalindromic ( n ) ) NEW_LINE
Count of sub | Function that returns the count of sub - arrays with odd product ; Initialize the count variable ; Initialize variable to store the last index with even number ; Initialize variable to store count of continuous odd numbers ; Loop through the array ; Check if the number is even or not ; Calculate count of continuous odd numbers ; Increase the count of sub - arrays with odd product ; Store the index of last even number ; N considered as index of even number ; Driver Code ; Function call
def countSubArrayWithOddProduct ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE last = - 1 NEW_LINE K = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] % 2 == 0 ) : NEW_LINE INDENT K = ( i - last - 1 ) NEW_LINE count += ( K * ( K + 1 ) / 2 ) NEW_LINE last = i NEW_LINE DEDENT DEDENT K = ( N - last - 1 ) NEW_LINE count += ( K * ( K + 1 ) / 2 ) NEW_LINE return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( int ( countSubArrayWithOddProduct ( arr , n ) ) ) NEW_LINE DEDENT
Calculate the CGPA and CGPA % of marks obtained by a Student in N subjects | Python3 program to calculate the CGPA and CGPA percentage of a student ; Variable to store the grades in every subject ; Variables to store CGPA and the sum of all the grades ; Computing the grades ; Computing the sum of grades ; Computing the CGPA ; Driver code
def CgpaCalc ( marks , n ) : NEW_LINE INDENT grade = [ 0 ] * n NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE grade [ i ] = ( marks [ i ] / 10 ) NEW_LINE for i in range ( n ) : NEW_LINE Sum += grade [ i ] NEW_LINE cgpa = Sum / n NEW_LINE return cgpa NEW_LINE DEDENT n = 5 NEW_LINE marks = [ 90 , 80 , 70 , 80 , 90 ] NEW_LINE cgpa = CgpaCalc ( marks , n ) NEW_LINE print ( " CGPA ▁ = ▁ " , ' % .1f ' % cgpa ) NEW_LINE print ( " CGPA ▁ Percentage ▁ = ▁ " , ' % .2f ' % ( cgpa * 9.5 ) ) NEW_LINE
Find the subarray of size K with minimum XOR | Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Driver Code ; Subarray size ; Function Call
def findMinXORSubarray ( arr , n , k ) : NEW_LINE INDENT if ( n < k ) : NEW_LINE INDENT return NEW_LINE DEDENT res_index = 0 NEW_LINE curr_xor = 0 NEW_LINE for i in range ( 0 , k ) : NEW_LINE INDENT curr_xor = curr_xor ^ arr [ i ] NEW_LINE DEDENT min_xor = curr_xor NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) NEW_LINE if ( curr_xor < min_xor ) : NEW_LINE INDENT min_xor = curr_xor NEW_LINE res_index = ( i - k + 1 ) NEW_LINE DEDENT DEDENT print ( min_xor , end =   ' ' ) NEW_LINE DEDENT arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ] NEW_LINE k = 3 NEW_LINE n = len ( arr ) NEW_LINE findMinXORSubarray ( arr , n , k ) NEW_LINE