codeparrot/xlcost-text-to-code huggingface dataset:
Maximum element in an array such that its previous and next element product is maximum | Function to return the largest element such that its previous and next element product is maximum ; Calculate the product of the previous and the next element for the current element ; Update the maximum product ; If current product is equal to the current maximum product then choose the maximum element ; Driver code
def maxElement ( a , n ) : NEW_LINE INDENT if n < 3 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT maxElement = a [ 0 ] NEW_LINE maxProd = a [ n - 1 ] * a [ 1 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT currprod = a [ i - 1 ] * a [ ( i + 1 ) % n ] NEW_LINE if currprod > maxProd : NEW_LINE INDENT maxProd = currprod NEW_LINE maxElement = a [ i ] NEW_LINE DEDENT elif currprod == maxProd : NEW_LINE INDENT maxElement = max ( maxElement , a [ i ] ) NEW_LINE DEDENT DEDENT return maxElement NEW_LINE DEDENT a = [ 5 , 6 , 4 , 3 , 2 ] NEW_LINE n = len ( a ) sizeof ( a [ 0 ] ) NEW_LINE print ( maxElement ( a , n ) ) NEW_LINE
Total ways of choosing X men and Y women from a total of M men and W women | Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the count of required ways ;
def ncr ( n , r ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE INDENT ans *= ( n - r + i ) NEW_LINE ans //= i NEW_LINE DEDENT return ans NEW_LINE DEDENT def totalWays ( X , Y , M , W ) : NEW_LINE INDENT return ( ncr ( M , X ) * ncr ( W , Y ) ) NEW_LINE DEDENT / * Driver code * / NEW_LINE X = 4 NEW_LINE Y = 3 NEW_LINE M = 6 NEW_LINE W = 5 NEW_LINE print ( totalWays ( X , Y , M , W ) ) NEW_LINE
Generate elements of the array following given conditions | Function to generate the required array ; Initialize cnt variable for assigning unique value to prime and its multiples ; When we get a prime for the first time then assign a unique smallest value to it and all of its multiples ; Print the generated array ; Driver code
def specialSieve ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE prime = [ 0 ] * ( n + 1 ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( not prime [ i ] ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE for j in range ( i , n + 1 , i ) : NEW_LINE INDENT prime [ j ] = cnt ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT print ( prime [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 ; NEW_LINE specialSieve ( n ) ; NEW_LINE DEDENT
Find the node whose sum with X has maximum set bits | Python implementation of the approach ; Function to perform dfs to find the maximum set bits value ; If current set bits value is greater than the current maximum ; If count is equal to the maximum then choose the node with minimum value ; Driver Code ; Weights of the node ; Edges of the tree
from sys import maxsize NEW_LINE maximum , x , ans = - maxsize , None , maxsize NEW_LINE graph = [ [ ] for i in range ( 100 ) ] NEW_LINE weight = [ 0 ] * 100 NEW_LINE def dfs ( node , parent ) : NEW_LINE INDENT global x , ans , graph , weight , maximum NEW_LINE a = bin ( weight [ node ] + x ) . count ( '1' ) NEW_LINE if maximum < a : NEW_LINE INDENT maximum = a NEW_LINE ans = node NEW_LINE DEDENT elif maximum == a : NEW_LINE INDENT ans = min ( ans , node ) NEW_LINE DEDENT for to in graph [ node ] : NEW_LINE INDENT if to == parent : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( to , node ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 15 NEW_LINE weight [ 1 ] = 5 NEW_LINE weight [ 2 ] = 10 NEW_LINE weight [ 3 ] = 11 NEW_LINE weight [ 4 ] = 8 NEW_LINE weight [ 5 ] = 6 NEW_LINE graph [ 1 ] . append ( 2 ) NEW_LINE graph [ 2 ] . append ( 3 ) NEW_LINE graph [ 2 ] . append ( 4 ) NEW_LINE graph [ 1 ] . append ( 5 ) NEW_LINE dfs ( 1 , 1 ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Count Pairs from two arrays with even sum | Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a [ ] ; Find the count of odd and even elements in b [ ] ; Count the number of pairs ; Return the number of pairs ; Driver code
def count_pairs ( a , b , n , m ) : NEW_LINE INDENT odd1 = 0 NEW_LINE even1 = 0 NEW_LINE odd2 = 0 NEW_LINE even2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 1 ) : NEW_LINE INDENT odd1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even1 += 1 NEW_LINE DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT if ( b [ i ] % 2 == 1 ) : NEW_LINE INDENT odd2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even2 += 1 NEW_LINE DEDENT DEDENT pairs = min ( odd1 , odd2 ) + min ( even1 , even2 ) NEW_LINE return pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 9 , 14 , 6 , 2 , 11 ] NEW_LINE b = [ 8 , 4 , 7 , 20 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( count_pairs ( a , b , n , m ) ) NEW_LINE DEDENT
Find an index such that difference between product of elements before and after it is minimum |  ; Function to find index ; Array to store log values of elements ; Prefix Array to Maintain Sum of log values till index i ; Answer Index ; Find minimum absolute value ; Driver Code
import math NEW_LINE def solve ( Array , N ) : NEW_LINE INDENT Arraynew = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT Arraynew [ i ] = math . log ( Array [ i ] ) NEW_LINE DEDENT prefixsum = [ 0 ] * N NEW_LINE prefixsum [ 0 ] = Arraynew [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] NEW_LINE DEDENT answer = 0 NEW_LINE minabs = abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT ans1 = abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) NEW_LINE if ( ans1 < minabs ) : NEW_LINE INDENT minabs = ans1 NEW_LINE answer = i NEW_LINE DEDENT DEDENT print ( " Index ▁ is : ▁ " , answer ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Array = [ 1 , 4 , 12 , 2 , 6 ] NEW_LINE N = 5 NEW_LINE solve ( Array , N ) NEW_LINE DEDENT
Find the value of N when F ( N ) = f ( a ) + f ( b ) where a + b is the minimum possible and a * b = N | Function to return the value of F ( N ) ; Base cases ; Count the number of times a number if divisible by 2 ; Return the summation ; Driver code
def getValueOfF ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT cnt = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE n /= 2 NEW_LINE DEDENT return 2 * cnt NEW_LINE DEDENT n = 20 NEW_LINE print ( getValueOfF ( n ) ) NEW_LINE
Find the sum of the number of divisors | Python3 code for above given approach ; To store the number of divisors ; Function to find the number of divisors of all numbers in the range 1 to n ; For every number 1 to n ; Increase divisors count for every number ; Function to find the sum of divisors ; To store sum ; Count the divisors ; Driver code ; Function call
N = 100005 NEW_LINE mod = 1000000007 NEW_LINE cnt = [ 0 ] * N ; NEW_LINE def Divisors ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , N // i ) : NEW_LINE INDENT cnt [ i * j ] += 1 ; NEW_LINE DEDENT DEDENT DEDENT def Sumofdivisors ( A , B , C ) : NEW_LINE INDENT sum = 0 ; NEW_LINE Divisors ( ) ; NEW_LINE for i in range ( 1 , A + 1 ) : NEW_LINE INDENT for j in range ( 1 , B + 1 ) : NEW_LINE INDENT for k in range ( 1 , C + 1 ) : NEW_LINE INDENT x = i * j * k ; NEW_LINE sum += cnt [ x ] ; NEW_LINE if ( sum >= mod ) : NEW_LINE INDENT sum -= mod ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 5 ; B = 6 ; C = 7 ; NEW_LINE print ( Sumofdivisors ( A , B , C ) ) ; NEW_LINE DEDENT
Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver code
def A_mod_B ( N , a ) : NEW_LINE INDENT Len = len ( N ) NEW_LINE ans = 0 NEW_LINE for i in range ( Len ) : NEW_LINE INDENT ans = ( ans * 10 + int ( N [ i ] ) ) % a NEW_LINE DEDENT return ans % a NEW_LINE DEDENT def findMod ( N ) : NEW_LINE INDENT mod = A_mod_B ( N , 4 ) NEW_LINE ans = ( 1 + pow ( 2 , mod ) + pow ( 3 , mod ) + pow ( 4 , mod ) ) NEW_LINE return ans % 5 NEW_LINE DEDENT N = "4" NEW_LINE print ( findMod ( N ) ) NEW_LINE
Elements greater than the previous and next element in an Array | Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver Code
def printElements ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n - 1 , 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE printElements ( arr , n ) NEW_LINE DEDENT
Sum of the series Kn + ( K ( n |  ; Recursive python3 program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code
/ * Python program of above approach * / NEW_LINE def exponent ( A , B ) : NEW_LINE INDENT if ( A == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( B == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( B % 2 == 0 ) : NEW_LINE INDENT y = exponent ( A , B / 2 ) ; NEW_LINE y = ( y * y ) ; NEW_LINE DEDENT else : NEW_LINE INDENT y = A ; NEW_LINE y = ( y * exponent ( A , B - 1 ) ) ; NEW_LINE DEDENT return y ; NEW_LINE DEDENT def sum ( k , n ) : NEW_LINE INDENT sum = exponent ( k , n + 1 ) - exponent ( k - 1 , n + 1 ) ; NEW_LINE return sum ; NEW_LINE DEDENT n = 3 ; NEW_LINE K = 3 ; NEW_LINE print ( sum ( K , n ) ) ; NEW_LINE
Minimize the cost to split a number | Python3 implementation of the approach ; check if a number is prime or not ; run a loop upto square root of x ; Function to return the minimized cost ; If n is prime ; If n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; Every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; Driver code
from math import sqrt NEW_LINE def isPrime ( x ) : NEW_LINE INDENT for i in range ( 2 , int ( sqrt ( x ) ) + 1 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT return 1 ; NEW_LINE DEDENT def minimumCost ( n ) : NEW_LINE INDENT if ( isPrime ( n ) ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( n % 2 == 1 and isPrime ( n - 2 ) ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT return 3 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 ; NEW_LINE print ( minimumCost ( n ) ) ; NEW_LINE DEDENT
Find amount of water wasted after filling the tank | Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; Driver code
def wastedWater ( V , M , N ) : NEW_LINE INDENT amt_per_min = M - N NEW_LINE time_to_fill = V / amt_per_min NEW_LINE wasted_amt = N * time_to_fill NEW_LINE return wasted_amt NEW_LINE DEDENT V = 700 NEW_LINE M = 10 NEW_LINE N = 3 NEW_LINE print ( wastedWater ( V , M , N ) ) NEW_LINE V = 1000 NEW_LINE M = 100 NEW_LINE N = 50 NEW_LINE print ( wastedWater ( V , M , N ) ) NEW_LINE
Smallest and Largest N | Python3 implementation of the approach ; Function to print the largest and the smallest n - digit perfect cube ; Smallest n - digit perfect cube ; Largest n - digit perfect cube ; Driver code
from math import ceil NEW_LINE def nDigitPerfectCubes ( n ) : NEW_LINE INDENT print ( pow ( ceil ( ( pow ( 10 , ( n - 1 ) ) ) ** ( 1 / 3 ) ) , 3 ) , end = " ▁ " ) NEW_LINE print ( pow ( ceil ( ( pow ( 10 , ( n ) ) ) ** ( 1 / 3 ) ) - 1 , 3 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE nDigitPerfectCubes ( n ) NEW_LINE DEDENT
Count numbers which are divisible by all the numbers from 2 to 10 | Function to return the count of numbers from 1 to n which are divisible by all the numbers from 2 to 10 ; Driver code
def countNumbers ( n ) : NEW_LINE INDENT return n // 2520 NEW_LINE DEDENT n = 3000 NEW_LINE print ( countNumbers ( n ) ) NEW_LINE
Sum of all odd factors of numbers in the range [ l , r ] | Python3 implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code
MAX = 100001 ; NEW_LINE prefix = [ 0 ] * MAX ; NEW_LINE def sieve_modified ( ) : NEW_LINE INDENT for i in range ( 1 , MAX , 2 ) : NEW_LINE INDENT for j in range ( i , MAX , i ) : NEW_LINE INDENT prefix [ j ] += i ; NEW_LINE DEDENT DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT prefix [ i ] += prefix [ i - 1 ] ; NEW_LINE DEDENT DEDENT def sumOddFactors ( L , R ) : NEW_LINE INDENT return ( prefix [ R ] - prefix [ L - 1 ] ) ; NEW_LINE DEDENT sieve_modified ( ) ; NEW_LINE l = 6 ; NEW_LINE r = 10 ; NEW_LINE print ( sumOddFactors ( l , r ) ) ; NEW_LINE
Number of ways in which an item returns back to its initial position in N swaps in array of size K | Python3 program to find the number of ways in which an item returns back to its initial position in N swaps in an array of size K ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return the number of ways ; Base case ; Recursive case F ( n ) = ( k - 1 ) ^ ( n - 1 ) - F ( n - 1 ) . ;  ; Function calling
mod = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT p = mod NEW_LINE res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) != 0 : 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 solve ( n , k ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( power ( ( k - 1 ) , n - 1 ) % mod - solve ( n - 1 , k ) + mod ) % mod NEW_LINE DEDENT / * Driver code * / NEW_LINE n , k = 4 , 5 NEW_LINE print ( solve ( n , k ) ) NEW_LINE
XOR of a submatrix queries | Python implementation of the approach ; Function to pre - compute the xor ; Left to right prefix xor for each row ; Top to bottom prefix xor for each column ; Function to process the queries x1 , x2 , y1 , y2 represent the positions of the top - left and bottom right corners ; To store the xor values ; Finding the values we need to xor with value at ( x2 , y2 ) in prefix - xor matrix ; Return the required prefix xor ; Driver code ; To store pre - computed xor ; Pre - computing xor ; Queries
n = 3 NEW_LINE def preComputeXor ( arr , prefix_xor ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT prefix_xor [ i ] [ j ] = arr [ i ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT prefix_xor [ i ] [ j ] = ( prefix_xor [ i ] [ j - 1 ] ^ arr [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( 1 , n ) : NEW_LINE INDENT prefix_xor [ j ] [ i ] = ( prefix_xor [ j - 1 ] [ i ] ^ prefix_xor [ j ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT def ansQuerie ( prefix_xor , x1 , y1 , x2 , y2 ) : NEW_LINE INDENT xor_1 , xor_2 , xor_3 = 0 , 0 , 0 NEW_LINE if ( x1 != 0 ) : NEW_LINE INDENT xor_1 = prefix_xor [ x1 - 1 ] [ y2 ] NEW_LINE DEDENT if ( y1 != 0 ) : NEW_LINE INDENT xor_2 = prefix_xor [ x2 ] [ y1 - 1 ] NEW_LINE DEDENT if ( x1 != 0 and y1 != 0 ) : NEW_LINE INDENT xor_3 = prefix_xor [ x1 - 1 ] [ y1 - 1 ] NEW_LINE DEDENT return ( ( prefix_xor [ x2 ] [ y2 ] ^ xor_1 ) ^ ( xor_2 ^ xor_3 ) ) NEW_LINE DEDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE prefix_xor = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE preComputeXor ( arr , prefix_xor ) NEW_LINE print ( ansQuerie ( prefix_xor , 1 , 1 , 2 , 2 ) ) NEW_LINE print ( ansQuerie ( prefix_xor , 1 , 2 , 2 , 2 ) ) NEW_LINE
Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1 | Python3 implementation of the approach ; Function that returns true if the eventual digit sum of number nm is 1 ; if reminder will 1 then eventual sum is 1 ; Function to print the required numbers less than n ; If it is the required perfect cube ; Driver code
import math NEW_LINE def isDigitSumOne ( nm ) : NEW_LINE INDENT if ( nm % 9 == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printValidNums ( n ) : NEW_LINE INDENT cbrt_n = math . ceil ( n ** ( 1. / 3. ) ) NEW_LINE for i in range ( 1 , cbrt_n + 1 ) : NEW_LINE INDENT cube = i * i * i NEW_LINE if ( cube >= 1 and cube <= n and isDigitSumOne ( cube ) ) : NEW_LINE INDENT print ( cube , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 1000 NEW_LINE printValidNums ( n ) NEW_LINE
Count the number of rhombi possible inside a rectangle of given size | Function to return the count of rhombi possible ; All possible diagonal lengths ; Update rhombi possible with the current diagonal lengths ; Return the total count of rhombi possible ; Driver code
def countRhombi ( h , w ) : NEW_LINE INDENT ct = 0 ; NEW_LINE for i in range ( 2 , h + 1 , 2 ) : NEW_LINE INDENT for j in range ( 2 , w + 1 , 2 ) : NEW_LINE INDENT ct += ( h - i + 1 ) * ( w - j + 1 ) NEW_LINE DEDENT DEDENT return ct NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT h = 2 NEW_LINE w = 2 NEW_LINE print ( countRhombi ( h , w ) ) NEW_LINE DEDENT
Program to calculate the area between two Concentric Circles | Function to find area between the two given concentric circles ; Declare value of pi ; Calculate area of outer circle ; Calculate area of inner circle ; Difference in areas ; Driver Code
def calculateArea ( x , y ) : NEW_LINE INDENT pi = 3.1415926536 NEW_LINE arx = pi * x * x NEW_LINE ary = pi * y * y NEW_LINE return arx - ary NEW_LINE DEDENT x = 2 NEW_LINE y = 1 NEW_LINE print ( calculateArea ( x , y ) ) NEW_LINE
Find the winner by adding Pairwise difference of elements in the array until Possible | Python3 implementation of the approach ; Function to return the winner of the game ; To store the gcd of the original array ; To store the maximum element from the original array ; If number of moves are odd ; Driver Code
from math import gcd NEW_LINE def getWinner ( arr , n ) : NEW_LINE INDENT __gcd = arr [ 0 ] ; NEW_LINE maxEle = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT __gcd = gcd ( __gcd , arr [ i ] ) ; NEW_LINE maxEle = max ( maxEle , arr [ i ] ) ; NEW_LINE DEDENT totalMoves = ( maxEle / __gcd ) - n ; NEW_LINE if ( totalMoves % 2 == 1 ) : NEW_LINE INDENT return ' A ' ; NEW_LINE DEDENT return ' B ' ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 7 ] ; NEW_LINE n = len ( arr ) NEW_LINE print ( getWinner ( arr , n ) ) NEW_LINE DEDENT
Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | Function to return the count of required pairs ; Number which will give the Max value for ( ( n % i ) % j ) % n ; To store the Maximum possible value of ( ( n % i ) % j ) % n ; To store the count of possible pairs ; Check all possible pairs ; Calculating the value of ( ( n % i ) % j ) % n ; If value is equal to Maximum ; Return the number of possible pairs ; Driver code
def countPairs ( n ) : NEW_LINE INDENT num = ( ( n // 2 ) + 1 ) NEW_LINE Max = n % num NEW_LINE count = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT val = ( ( n % i ) % j ) % n NEW_LINE if ( val == Max ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT n = 5 NEW_LINE print ( countPairs ( n ) ) NEW_LINE
Program to check if a number is divisible by any of its digits | Function to check if given number is divisible by any of its digits ; check if any of digit divides n ; check if K divides N ; Driver Code
def isDivisible ( n ) : NEW_LINE INDENT temp = n NEW_LINE while ( n ) : NEW_LINE INDENT k = n % 10 NEW_LINE if ( temp % k == 0 ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT n /= 10 ; NEW_LINE DEDENT return " NO " NEW_LINE DEDENT n = 9876543 NEW_LINE print ( isDivisible ( n ) ) NEW_LINE
Program to find sum of harmonic series | Function to return sum of harmonic series ; Driver Code
def sum ( n ) : NEW_LINE INDENT i = 1 NEW_LINE s = 0.0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT s = s + 1 / i ; NEW_LINE DEDENT return s ; NEW_LINE DEDENT n = 5 NEW_LINE print ( " Sum ▁ is " , round ( sum ( n ) , 6 ) ) NEW_LINE
Check if a number can be expressed as sum two abundant numbers | Python 3 program to check if number n is expressed as sum of two abundant numbers ; Function to return all abundant numbers This function will be helpful for multiple queries ; To store abundant numbers ; to store sum of the divisors include 1 in the sum ; if j is proper divisor ; if i is not a perfect square ; if sum is greater than i then i is a abundant numbe ; Check if number n is expressed as sum of two abundant numbers ; if both i and n - i are abundant numbers ; can not be expressed ; Driver code
from math import sqrt NEW_LINE N = 100005 NEW_LINE def ABUNDANT ( ) : NEW_LINE INDENT v = set ( ) ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT sum = 1 NEW_LINE for j in range ( 2 , int ( sqrt ( i ) ) + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT sum += j NEW_LINE DEDENT if ( i / j != j ) : NEW_LINE INDENT sum += i // j NEW_LINE DEDENT DEDENT if ( sum > i ) : NEW_LINE INDENT v . add ( i ) NEW_LINE DEDENT DEDENT return v NEW_LINE DEDENT def SumOfAbundant ( n ) : NEW_LINE INDENT v = ABUNDANT ( ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( list ( v ) . count ( i ) and list ( v ) . count ( n - i ) ) : NEW_LINE INDENT print ( i , " ▁ " , n - i ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 24 NEW_LINE SumOfAbundant ( n ) NEW_LINE DEDENT
Find nth term of the series 5 2 13 41 | Python3 program to find nth term of the series 5 2 13 41 ; function to calculate nth term of the series ; if n is even number ; if n is odd number ; return nth term ; Driver code
from math import pow NEW_LINE def nthTermOfTheSeries ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT nthTerm = pow ( n - 1 , 2 ) + n NEW_LINE DEDENT else : NEW_LINE INDENT nthTerm = pow ( n + 1 , 2 ) + n NEW_LINE DEDENT return nthTerm NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE n = 12 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE n = 102 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE n = 999 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE n = 9999 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE DEDENT
Find cost price from given selling price and profit or loss percentage | Function to calculate cost price with profit ; required formula to calculate CP with profit ; Function to calculate cost price with loss ; required formula to calculate CP with loss ; Driver code
def CPwithProfit ( sellingPrice , profit ) : NEW_LINE INDENT costPrice = ( ( sellingPrice * 100.0 ) / ( 100 + profit ) ) NEW_LINE return costPrice NEW_LINE DEDENT def CPwithLoss ( sellingPrice , loss ) : NEW_LINE INDENT costPrice = ( ( sellingPrice * 100.0 ) / ( 100 - loss ) ) NEW_LINE return costPrice NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT SP = 1020 NEW_LINE profit = 20 NEW_LINE print ( " Cost ▁ Price ▁ = " , CPwithProfit ( SP , profit ) ) NEW_LINE SP = 900 NEW_LINE loss = 10 NEW_LINE print ( " Cost ▁ Price ▁ = " , CPwithLoss ( SP , loss ) ) NEW_LINE SP = 42039 NEW_LINE profit = 8 NEW_LINE print ( " Cost ▁ Price ▁ = " , int ( CPwithProfit ( SP , profit ) ) ) NEW_LINE DEDENT