codeparrot/xlcost-text-to-code huggingface dataset:
Program to print the series 1 , 9 , 17 , 33 , 49 , 73 , 97. . . till N terms | Function to prthe series ; Generate the ith term and print ; Driver Code
def printSeries ( N ) : NEW_LINE INDENT ith_term = 0 ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT ith_term = 0 ; NEW_LINE if ( i % 2 == 0 ) : NEW_LINE INDENT ith_term = 2 * i * i + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ith_term = 2 * i * i - 1 ; NEW_LINE DEDENT print ( ith_term , end = " , ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 ; NEW_LINE printSeries ( N ) ; NEW_LINE DEDENT
Find Nth term of the series where each term differs by 6 and 2 alternately | Function to find Nth term ; Iterate from 1 till Nth term ; Check if i is even and then add 6 ; Else add 2 ; Print ans ; Driver Code
def findNthTerm ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT ans = ans + 6 NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans + 2 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE findNthTerm ( N ) NEW_LINE DEDENT
Find the number of ordered pairs such that a * p + b * q = N , where p and q are primes | Python3 program to find the number of ordered pairs such that a * p + b * q = N where p and q are primes ; Sieve of erastothenes to store the prime numbers and their frequency in form a * p + b * q ; Performing Sieve of Eratosthenes to find the prime numbers unto 10001 ; Loop to find the number of ordered pairs for every combination of the prime numbers ; Driver code ; Printing the number of ordered pairs for every query
from math import sqrt NEW_LINE size = 1000 NEW_LINE prime = [ 0 for i in range ( size ) ] NEW_LINE freq = [ 0 for i in range ( size ) ] NEW_LINE def sieve ( a , b ) : NEW_LINE INDENT prime [ 1 ] = 1 NEW_LINE for i in range ( 2 , int ( sqrt ( size ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i * 2 , size , i ) : NEW_LINE INDENT prime [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT for p in range ( 1 , size , 1 ) : NEW_LINE INDENT for q in range ( 1 , size , 1 ) : NEW_LINE INDENT if ( prime [ p ] == 0 and prime [ q ] == 0 and a * p + b * q < size ) : NEW_LINE INDENT freq [ a * p + b * q ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT queries = 2 NEW_LINE a = 1 NEW_LINE b = 2 NEW_LINE sieve ( a , b ) NEW_LINE arr = [ 15 , 25 ] NEW_LINE for i in range ( queries ) : NEW_LINE INDENT print ( freq [ arr [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Find the Sum of the series 1 / 2 | Function to find the sum of series ; Generate the ith term and add it to the sum if i is even and subtract if i is odd ; Print the sum ; Driver Code
def printSeriesSum ( N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT sum += i / ( i + 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT sum -= i / ( i + 1 ) ; NEW_LINE DEDENT DEDENT print ( sum ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 ; NEW_LINE printSeriesSum ( N ) ; NEW_LINE DEDENT
Find the Sum of the series 1 + 2 + 9 + 64 + 625 + 7776 . . . till N terms | Function to find the summ of series ; Generate the ith term and add it to the summ ; Prthe summ ; Driver Code
def printSeriessumm ( N ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT summ += pow ( i , i - 1 ) NEW_LINE DEDENT print ( summ ) NEW_LINE DEDENT N = 5 NEW_LINE printSeriessumm ( N ) NEW_LINE
Find the Sum of the series 1 + 1 / 3 + 1 / 5 + 1 / 7 + ... till N terms | Function to find the sum of the given series ; Initialise the sum to 0 ; Generate the ith term and add it to the sum ; Print the final sum ; Driver Code
def printSumSeries ( N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT sum += 1.0 / ( 2 * i - 1 ) ; NEW_LINE DEDENT print ( sum ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 ; NEW_LINE printSumSeries ( N ) ; NEW_LINE DEDENT
Find the Sum of the series 1 , 2 , 3 , 6 , 9 , 18 , 27 , 54 , ... till N terms | Function to find the sum of series ; Flag to find the multiplicating factor . . i . e , by 2 or 3 / 2 ; First term ; If flag is true , multiply by 2 ; If flag is false , multiply by 3 / 2 ; Update the previous element to nextElement ; Print the sum ; Driver Code
def printSeriesSum ( N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE a = 1 ; NEW_LINE cnt = 0 ; NEW_LINE flag = True ; NEW_LINE sum += a ; NEW_LINE while ( cnt < N ) : NEW_LINE INDENT nextElement = None ; NEW_LINE if ( flag ) : NEW_LINE INDENT nextElement = a * 2 ; NEW_LINE sum += nextElement ; NEW_LINE flag = not flag ; NEW_LINE DEDENT else : NEW_LINE INDENT nextElement = a * ( 3 / 2 ) ; NEW_LINE sum += nextElement ; NEW_LINE flag = not flag ; NEW_LINE DEDENT a = nextElement ; NEW_LINE cnt += 1 NEW_LINE DEDENT print ( sum ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 ; NEW_LINE printSeriesSum ( N ) ; NEW_LINE DEDENT
Program to add two integers of given base | Function to find the sum of two integers of base B ; Padding 0 in front of the number to make both numbers equal ; Condition to check if the strings have lengths mis - match ; Loop to find the find the sum of two integers of base B ; Current Place value for the resultant sum ; Update carry ; Find current digit ; Update sum result ; Driver Code ; Function Call
def sumBaseB ( a , b , base ) : NEW_LINE INDENT len_a = len ( a ) NEW_LINE len_b = len ( b ) NEW_LINE s = " " ; NEW_LINE sum = " " ; NEW_LINE diff = abs ( len_a - len_b ) ; NEW_LINE for i in range ( 1 , diff + 1 ) : NEW_LINE INDENT s += "0" NEW_LINE DEDENT if ( len_a < len_b ) : NEW_LINE INDENT a = s + a NEW_LINE DEDENT else : NEW_LINE INDENT b = s + b ; NEW_LINE DEDENT carry = 0 ; NEW_LINE for i in range ( max ( len_a , len_b ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT curr = carry + ( ord ( a [ i ] ) - ord ( '0' ) ) + ( ord ( b [ i ] ) - ord ( '0' ) ) ; NEW_LINE carry = curr // base NEW_LINE curr = curr % base ; NEW_LINE sum = chr ( curr + ord ( '0' ) ) + sum NEW_LINE DEDENT if ( carry > 0 ) : NEW_LINE INDENT sum = chr ( carry + ord ( '0' ) ) + sum ; NEW_LINE DEDENT return sum NEW_LINE DEDENT a = "123" NEW_LINE b = "234" NEW_LINE base = 6 NEW_LINE sum = sumBaseB ( a , b , base ) ; NEW_LINE print ( sum ) NEW_LINE
Minimum number of swaps required to make a number divisible by 60 | Function that prminimum number of swap operations required ; Condition if more than one zero exist ; Condition if zero_exist ; Computing total sum of all digits ; Condition if zero does not exist or the sum is not divisible by 3 ; Condition to find a digit that is multiple of 2 other than one zero ; Condition if multiple of 2 do not exist ; Condition for zero swaps means the number is already is divisible by 60 ; Condition for only one swap ; Otherwise 2 swaps required ; Driver Code
def MinimumSwapOperations ( s ) : NEW_LINE INDENT zero_exist = False NEW_LINE multiple_of_2 = False NEW_LINE sum = 0 NEW_LINE index_of_zero = 0 NEW_LINE more_zero = False NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( zero_exist == True ) : NEW_LINE INDENT more_zero = True NEW_LINE DEDENT if ( val == 0 ) : NEW_LINE INDENT zero_exist = True NEW_LINE index_of_zero = i NEW_LINE DEDENT sum += val NEW_LINE DEDENT if ( zero_exist == False or sum % 3 != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( val % 2 == 0 and i != index_of_zero ) : NEW_LINE INDENT multiple_of_2 = True NEW_LINE DEDENT DEDENT if ( multiple_of_2 == False ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT last_val = ord ( s [ len ( s ) - 1 ] ) - ord ( '0' ) NEW_LINE second_last_val = ord ( s [ len ( s ) - 2 ] ) - ord ( '0' ) NEW_LINE if ( last_val == 0 and second_last_val % 2 == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT elif ( ( last_val == 0 and second_last_val % 2 != 0 ) or ( last_val % 2 == 0 and second_last_val == 0 ) ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT elif ( more_zero == True and ( last_val == 0 and second_last_val % 2 != 0 ) ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 2 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = "20" NEW_LINE MinimumSwapOperations ( N ) NEW_LINE DEDENT
Largest palindrome which is product of two N | Function to check if a number is a Palindrome or not ; Taking the string value of the number ; Loop to check if every i - th character from beginning is equal to every ( N - i ) th char ; Function to find the largest palindrome which is a product of two N digited numbers ; Find lowerBound , upperBound for a given nDigits . for n = 2 [ 10 , 99 ] ; Result variables ; Keep p decrementing by 11 ; Find the nearest number divisible by 11 ; Keep decrementing q by 1 ; Update the result if t > r and is a palindrome ; Printing the final result ; Driver code
def isPalindrome ( x ) : NEW_LINE INDENT num = str ( x ) NEW_LINE result = True NEW_LINE i = 0 NEW_LINE j = len ( num ) - 1 NEW_LINE while ( i < j and result ) : NEW_LINE INDENT result = num [ i ] == num [ j ] NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return result NEW_LINE DEDENT def find ( nDigits ) : NEW_LINE INDENT lowerBound = pow ( 10 , nDigits - 1 ) NEW_LINE upperBound = ( lowerBound * 10 ) - 1 NEW_LINE resultP = 0 NEW_LINE resultQ = 0 NEW_LINE resultR = 0 NEW_LINE for p in range ( upperBound , lowerBound , - 11 ) : NEW_LINE INDENT while ( p % 11 != 0 ) : NEW_LINE INDENT p -= 1 NEW_LINE DEDENT for q in range ( upperBound , lowerBound , - 1 ) : NEW_LINE INDENT t = p * q NEW_LINE if ( t > resultR and isPalindrome ( t ) ) : NEW_LINE INDENT resultP = p NEW_LINE resultQ = q NEW_LINE resultR = t NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( resultR ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE find ( N ) NEW_LINE DEDENT
Minimize the cost of selecting two numbers whose product is X | Python3 implementation of the above approach ; max_prime [ i ] represents maximum prime number that divides the number i ; min_prime [ i ] represents minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; If i is a prime number , then both minimum and maximum prime numbers that divide the number is the number itself ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to minimize the cost of finding two numbers for every number such that the product of those two is equal to X ; Pre - calculation ; If X == 1 , then there is no way to find N and M . Print - 1 ; Case 3 is always valid and cost for that is C + X C for choosing 1 and M = X / 1 ; Case 1 N is prime , first number cost is fixed N is max_prime number divides this number ; If X is prime then the maximum prime number is the number itself . For this case , M becomes 1 and this shouldn 't be considered. ; Find M for this case ; Add cost for the second number also ; Update min_cost , if the cost for prime is minimum ; Case 2 If N is composite For this find the minimum prime number that divides A [ i ] and consider this as M ; Find N for that number ; Check if this number is composite or not if N is prime then there is no way to find any composite number that divides X If N = min_prime [ N ] then N is prime ; Update min_cost , if the cost for the composite is minimum ; Driver code
MAX = 10000 ; NEW_LINE max_prime = [ 0 ] * MAX ; NEW_LINE min_prime = [ 0 ] * MAX ; NEW_LINE def sieve ( n ) : NEW_LINE INDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( min_prime [ i ] > 0 ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT min_prime [ i ] = i ; NEW_LINE max_prime [ i ] = i ; NEW_LINE j = i + i ; NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( min_prime [ j ] == 0 ) : NEW_LINE INDENT min_prime [ j ] = i ; NEW_LINE DEDENT max_prime [ j ] = i ; NEW_LINE j += i ; NEW_LINE DEDENT DEDENT DEDENT def findCost ( A , B , C , X ) : NEW_LINE INDENT sieve ( MAX ) ; NEW_LINE if ( X == 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT min_cost = C + X ; NEW_LINE cost_for_prime = A ; NEW_LINE N = max_prime [ X ] ; NEW_LINE if ( N != X ) : NEW_LINE INDENT M = X // N ; NEW_LINE cost_for_prime += M ; NEW_LINE min_cost = min ( min_cost , cost_for_prime ) ; NEW_LINE DEDENT M = min_prime [ X ] ; NEW_LINE N = X // M ; NEW_LINE if ( N != min_prime [ N ] ) : NEW_LINE INDENT cost_for_comp = B + M ; NEW_LINE min_cost = min ( min_cost , cost_for_comp ) ; NEW_LINE DEDENT return min_cost ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 7 ; B = 11 ; C = 2 ; X = 20 ; NEW_LINE print ( findCost ( A , B , C , X ) ) ; NEW_LINE DEDENT
Count the occurrence of digit K in a given number N using Recursion | Function to count the digit K in the given number N ; Extracting least significant digit ; Driver Code
def countdigits ( n , k ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT digit = n % 10 NEW_LINE if digit == k : NEW_LINE INDENT return 1 + countdigits ( n / 10 , k ) NEW_LINE DEDENT return countdigits ( n / 10 , k ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1000 ; NEW_LINE k = 0 ; NEW_LINE print ( countdigits ( n , k ) ) NEW_LINE DEDENT
Product of all Subarrays of an Array | Function to find product of all subarrays ; Variable to store the product ; Compute the product while traversing for subarrays ; Printing product of all subarray ; Driver code ; Function call
def product_subarrays ( arr , n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT product = 1 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT product *= arr [ j ] ; NEW_LINE res = res * product NEW_LINE DEDENT DEDENT print ( res ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 3 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE product_subarrays ( arr , n ) ; NEW_LINE DEDENT
Find numbers that divide X and Y to produce the same remainder | Function to find the required number as M ; Finding the maximum value among X and Y ; Loop to iterate through maximum value among X and Y . ; If the condition satisfies , then print the value of M ; Driver code
def printModulus ( X , Y ) : NEW_LINE INDENT n = max ( X , Y ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( X % i == Y % i ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT X = 10 NEW_LINE Y = 20 NEW_LINE printModulus ( X , Y ) NEW_LINE
Find numbers that divide X and Y to produce the same remainder | Function to prall the possible values of M such that X % M = Y % M ; Finding the absolute difference of X and Y ; Iterating from 1 ; Loop to prall the factors of D ; If i is a factor of d , then pri ; If d / i is a factor of d , then prd / i ; Driver code
def printModulus ( X , Y ) : NEW_LINE INDENT d = abs ( X - Y ) ; NEW_LINE i = 1 ; NEW_LINE while ( i * i <= d ) : NEW_LINE INDENT if ( d % i == 0 ) : NEW_LINE INDENT print ( i , end = " " ) ; NEW_LINE if ( d // i != i ) : NEW_LINE INDENT print ( d // i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 10 ; NEW_LINE Y = 26 ; NEW_LINE printModulus ( X , Y ) ; NEW_LINE DEDENT
Check whether a number can be represented as difference of two squares | Function to check whether a number can be represented by the difference of two squares ; Checking if n % 4 = 2 or not ; Driver code
def difSquare ( n ) : NEW_LINE INDENT if ( n % 4 != 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 45 NEW_LINE if ( difSquare ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of Fibonacci divisors of a given number | Python3 program to count number of divisors of N which are Fibonacci numbers ; Function to create hash table to check Fibonacci numbers ; Function to count number of divisors of N which are fibonacci numbers ; If divisors are equal , check and count only one ; Otherwise check and count both ; Driver code
from math import sqrt , ceil , floor NEW_LINE def createHash ( maxElement ) : NEW_LINE INDENT prev = 0 NEW_LINE curr = 1 NEW_LINE d = dict ( ) NEW_LINE d [ prev ] = 1 NEW_LINE d [ curr ] = 1 NEW_LINE while ( curr <= maxElement ) : NEW_LINE INDENT temp = curr + prev NEW_LINE d [ temp ] = 1 NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE DEDENT return d NEW_LINE DEDENT def countFibonacciDivisors ( n ) : NEW_LINE INDENT hash = createHash ( n ) NEW_LINE cnt = 0 NEW_LINE for i in range ( 1 , ceil ( sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( ( n // i == i ) and ( n // i in hash ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( n // i in hash ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( n // ( n // i ) in hash ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT n = 12 NEW_LINE print ( countFibonacciDivisors ( n ) ) NEW_LINE
Count of subtrees in a Binary Tree having XOR value K | A binary tree node ; A utility function to allocate a new node ; Base Case : If node is None , return 0 ; Calculating the XOR of the current subtree ; Increment res if xr is equal to k ; Return the XOR value of the current subtree ; Function to find the required count ; Initialize result variable 'res ; Recursively traverse the tree and compute the count ; return the count 'res ; Driver program ; Create the binary tree by adding nodes to it
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT newNode = Node ( data ) NEW_LINE return newNode NEW_LINE DEDENT def rec ( root , res , k ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return [ 0 , res ] ; NEW_LINE DEDENT xr = root . data ; NEW_LINE tmp , res = rec ( root . left , res , k ) ; NEW_LINE xr ^= tmp NEW_LINE tmp , res = rec ( root . right , res , k ) ; NEW_LINE xr ^= tmp NEW_LINE if ( xr == k ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT return xr , res ; NEW_LINE DEDENT def findCount ( root , K ) : NEW_LINE ' NEW_LINE INDENT res = 0 ; NEW_LINE tmp , res = rec ( root , res , K ) ; NEW_LINE DEDENT ' NEW_LINE INDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 2 ) ; NEW_LINE root . left = newNode ( 1 ) ; NEW_LINE root . right = newNode ( 9 ) ; NEW_LINE root . left . left = newNode ( 10 ) ; NEW_LINE root . left . right = newNode ( 5 ) ; NEW_LINE K = 5 ; NEW_LINE print ( findCount ( root , K ) ) NEW_LINE DEDENT
Make array elements equal with minimum cost | Function to find the minimum cost required to make array elements equal ; Loop to find the count of odd elements ; Driver Code
def makearrayequal ( arr , n ) : NEW_LINE INDENT x = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT x += arr [ i ] & 1 ; NEW_LINE DEDENT print ( min ( x , n - x ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 2 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE makearrayequal ( arr , n ) ; NEW_LINE DEDENT
Count of Prime digits in a Number | Function to find the count of prime digits in a number ; Loop to compute all the digits of the number ; Finding every digit of the given number ; Checking if digit is prime or not Only 2 , 3 , 5 and 7 are prime one - digit number ; Driver code
def countDigit ( n ) : NEW_LINE INDENT temp = n NEW_LINE count = 0 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 NEW_LINE temp //= 10 NEW_LINE if ( d == 2 or d == 3 or d == 5 or d == 7 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 1234567890 NEW_LINE print ( countDigit ( n ) ) NEW_LINE DEDENT
Check if an Array is a permutation of numbers from 1 to N : Set 2 | Function to check if an array represents a permutation or not ; Counting the frequency ; Check if each frequency is 1 only ; Driver code
def permutation ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash [ arr [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( hash [ i ] != 1 ) : NEW_LINE INDENT return " No " ; NEW_LINE DEDENT DEDENT return " Yes " ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 5 , 5 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( permutation ( arr , n ) ) ; NEW_LINE DEDENT
Check if the XOR of an array of integers is Even or Odd | Function to check if the XOR of an array of integers is Even or Odd ; Count the number of odd elements ; If count of odd elements is odd , then XOR will be odd ; Else even ; Driver Code ; Function call
def check ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT count = count + 1 ; NEW_LINE DEDENT DEDENT if ( count & 1 ) : NEW_LINE INDENT return " Odd " ; NEW_LINE DEDENT else : NEW_LINE INDENT return " Even " ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 9 , 12 , 13 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( check ( arr , n ) ) NEW_LINE DEDENT
Length of Longest Prime Subsequence in an Array | Python 3 program to find the length of Longest Prime Subsequence in an Array ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find the longest subsequence which contain all prime numbers ; Precompute N primes ; Find the length of longest prime subsequence ; Driver code ; Function call
N = 100005 NEW_LINE def SieveOfEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= p_size : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * 2 , p_size + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def longestPrimeSubsequence ( arr , n ) : NEW_LINE INDENT prime = [ True ] * ( N + 1 ) NEW_LINE SieveOfEratosthenes ( prime , N ) NEW_LINE answer = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 4 , 11 , 2 , 9 , 21 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestPrimeSubsequence ( arr , n ) ) NEW_LINE DEDENT
Check if an Octal number is Even or Odd | Check if the number is odd or even ; Check if the last digit is either '0' , '2' , '4' , or '6 ; Driver code
def even_or_odd ( N ) : NEW_LINE INDENT l = len ( N ) ; NEW_LINE DEDENT ' NEW_LINE INDENT if ( N [ l - 1 ] == '0' or N [ l - 1 ] == '2' or N [ l - 1 ] == '4' or N [ l - 1 ] == '6' ) : NEW_LINE INDENT return ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " Odd " ) NEW_LINE DEDENT DEDENT N = "735" NEW_LINE print ( even_or_odd ( N ) ) NEW_LINE