codeparrot/xlcost-text-to-code huggingface dataset:
Find the element in a linked list with frequency at least N / 3 | Structure of a node for the linked list ; Function to find and return the element with frequency of at least N / 3 ; Candidates for being the required majority element ; Store the frequencies of the respective candidates ; Iterate all nodes ; Increase frequency of candidate s ; Increase frequency of candidate t ; Set the new sting as candidate for majority ; Set the new sting as second candidate for majority ; Decrease the frequency ; Check the frequency of two final selected candidate linklist ; Increase the frequency of first candidate ; Increase the frequency of second candidate ; Return the string with higher frequency ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , s ) : NEW_LINE INDENT self . i = s NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def Majority_in_linklist ( head ) : NEW_LINE INDENT s , t = " " , " " NEW_LINE p , q = 0 , 0 NEW_LINE ptr = None NEW_LINE while head != None : NEW_LINE INDENT if s == head . i : NEW_LINE INDENT p = p + 1 NEW_LINE DEDENT else : NEW_LINE INDENT if t == head . i : NEW_LINE INDENT q = q + 1 NEW_LINE DEDENT else : NEW_LINE INDENT if p == 0 : NEW_LINE INDENT s = head . i NEW_LINE p = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if q == 0 : NEW_LINE INDENT t = head . i NEW_LINE q = 1 NEW_LINE DEDENT else : NEW_LINE INDENT p = p - 1 NEW_LINE q = q - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT head = head . next NEW_LINE DEDENT head = ptr NEW_LINE p = 0 NEW_LINE q = 0 NEW_LINE while head != None : NEW_LINE INDENT if s == head . i : NEW_LINE INDENT p = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if t == head . i : NEW_LINE INDENT q = 1 NEW_LINE DEDENT DEDENT head = head . next NEW_LINE DEDENT if p > q : NEW_LINE INDENT return s NEW_LINE DEDENT else : NEW_LINE INDENT return t NEW_LINE DEDENT DEDENT ptr = None NEW_LINE head = Node ( " geeks " ) NEW_LINE head . next = Node ( " geeks " ) NEW_LINE head . next . next = Node ( " abcd " ) NEW_LINE head . next . next . next = Node ( " game " ) NEW_LINE head . next . next . next . next = Node ( " game " ) NEW_LINE head . next . next . next . next . next = Node ( " knight " ) NEW_LINE head . next . next . next . next . next . next = Node ( " harry " ) NEW_LINE head . next . next . next . next . next . next . next = Node ( " geeks " ) NEW_LINE print ( Majority_in_linklist ( head ) ) NEW_LINE
Form smallest number using indices of numbers chosen from Array with sum less than S | Function to find the minimum number which have maximum length ; Find Maximum length of number ; Find minimum number WHich have maximum length ; Driver Code
def max_number ( arr , sum ) : NEW_LINE INDENT frac = [ 0 ] * 9 NEW_LINE maxi = - 10 ** 9 NEW_LINE pos = 0 NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT frac [ i ] = sum // arr [ i ] NEW_LINE if ( frac [ i ] > maxi ) : NEW_LINE INDENT pos = i NEW_LINE maxi = frac [ i ] NEW_LINE DEDENT DEDENT an = str ( ( pos + 1 ) ) * maxi NEW_LINE sum -= maxi * arr [ pos ] NEW_LINE ans = [ i for i in an ] NEW_LINE for i in range ( maxi ) : NEW_LINE INDENT for j in range ( 1 , 10 ) : NEW_LINE INDENT if ( sum + arr [ pos ] - arr [ j - 1 ] >= 0 ) : NEW_LINE INDENT ans [ i ] = str ( j ) NEW_LINE sum += arr [ pos ] - arr [ j - 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( maxi == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return " " . join ( ans ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 2 , 4 , 6 , 5 , 4 , 2 , 3 ] NEW_LINE s = 13 NEW_LINE print ( max_number ( arr , s ) ) NEW_LINE DEDENT
Check if given intervals can be made non | Function to check if two intervals overlap with each other ; Condition to check if the intervals overlap ; Function to check if there is a existing overlapping intervals ; Path compression ; Union of two intervals Returns True if there is a overlapping with the same another interval ; Both have same another overlapping interval ; Function to check if the intervals can be added by X to form non - overlapping intervals ; If the intervals overlaps we will union them ; There is no cycle ; Driver Code
def checkOverlapping ( a , b ) : NEW_LINE INDENT a , b = max ( a , b ) , min ( a , b ) NEW_LINE if b [ 0 ] <= a [ 0 ] <= b [ 1 ] : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def find ( a , i ) : NEW_LINE INDENT if a [ i ] == i : NEW_LINE INDENT return i NEW_LINE DEDENT a [ i ] = find ( a , a [ i ] ) NEW_LINE return a [ i ] NEW_LINE DEDENT def union ( a , x , y ) : NEW_LINE INDENT xs = find ( a , x ) NEW_LINE ys = find ( a , y ) NEW_LINE if xs == ys : NEW_LINE INDENT return True NEW_LINE DEDENT a [ ys ] = xs NEW_LINE return False NEW_LINE DEDENT def checkNonOverlapping ( arr , n ) : NEW_LINE INDENT dsu = [ i for i in range ( n + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if checkOverlapping ( arr [ i ] ,   \ arr [ j ] ) : NEW_LINE INDENT if union ( dsu , i , j ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 4 ] , [ 2 , 2 ] , [ 2 , 3 ] ] NEW_LINE n = len ( arr ) NEW_LINE print ( " YES " if checkNonOverlapping ( arr , n ) else " NO " ) NEW_LINE DEDENT
Number formed after K times repeated addition of smallest divisor of N | Python3 program to find the Kth number formed after repeated addition of smallest divisor of N ; If N is even ; If N is odd ; Add smallest divisor to N ; Updated N is even ; Driver code
import math NEW_LINE def FindValue ( N , K ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT N = N + 2 * K NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 2 , ( int ) ( math . sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT N = N + i NEW_LINE N = N + 2 * ( K - 1 ) NEW_LINE DEDENT print ( N ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 NEW_LINE K = 4 NEW_LINE FindValue ( N , K ) NEW_LINE DEDENT
Find last digit in factorial | Python3 program to find last digit in factorial n . ; Explicitly handle all numbers less than or equal to 4 ; For all numbers greater than 4 the last digit is 0 ; Driver code
def lastDigitFactorial ( n ) : NEW_LINE INDENT if ( n == 0 ) : return 1 NEW_LINE elif ( n <= 2 ) : return n NEW_LINE elif ( n == 3 ) : return 6 NEW_LINE elif ( n == 4 ) : return 4 NEW_LINE else : return 0 NEW_LINE DEDENT print ( lastDigitFactorial ( 6 ) ) NEW_LINE
Program to convert Hexa | Function to convert Haxadecimal to BCD ; Iterating through the digits ; Conversion into equivalent BCD ; Driver code ; Function call
def HexaDecimaltoBCD ( str ) : NEW_LINE INDENT for i in range ( len ( str ) ) : NEW_LINE INDENT print ( " { 0:04b } " . format ( int ( str [ i ] , 16 ) ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT str = "11F " NEW_LINE HexaDecimaltoBCD ( str ) NEW_LINE
Find the sum of the first Nth Heptadecagonal Number | Function to find the N - th heptadecagonal number ; Formula to calculate nth heptadecagonal number ; Function to find the sum of the first N heptadecagonal numbers ; Variable to store the sum ; Iterate from 1 to N ; Finding the sum ; Driver code
def heptadecagonal_num ( n ) : NEW_LINE INDENT return ( ( 15 * n * n ) - 13 * n ) // 2 NEW_LINE DEDENT def sum_heptadecagonal_num ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ += heptadecagonal_num ( i ) NEW_LINE DEDENT return summ NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( sum_heptadecagonal_num ( n ) ) NEW_LINE DEDENT
Program to check if N is a Centered Pentagonal Number or not | Python3 program for the above approach ; Function to check if number N is a centered pentagonal number ; Condition to check if N is a centered pentagonal number ; Given Number ; Function call
import numpy as np NEW_LINE def isCenteredpentagonal ( N ) : NEW_LINE INDENT n = ( 5 + np . sqrt ( 40 * N - 15 ) ) / 10 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT N = 6 NEW_LINE if ( isCenteredpentagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to check if N is a Dodecagonal Number | Python3 program for the above approach ; Function to check if number N is a dodecagonal number or not ; Condition to check if the N is a dodecagonal number ; Given Number ; Function call
import numpy as np NEW_LINE def isdodecagonal ( N ) : NEW_LINE INDENT n = ( 4 + np . sqrt ( 20 * N + 16 ) ) / 10 NEW_LINE return ( n - int ( n ) ) == 0 NEW_LINE DEDENT N = 12 NEW_LINE if ( isdodecagonal ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Logarithm tricks for Competitive Programming | Python3 implementation to find the previous and next power of K ; Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Driver Code
from math import log NEW_LINE def prevPowerofK ( n , k ) : NEW_LINE INDENT p = ( int ) ( log ( n ) / log ( k ) ) ; NEW_LINE return pow ( k , p ) ; NEW_LINE DEDENT def nextPowerOfK ( n , k ) : NEW_LINE INDENT return prevPowerofK ( n , k ) * k ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE K = 2 NEW_LINE print ( prevPowerofK ( N , K ) , end = " ▁ " ) NEW_LINE print ( nextPowerOfK ( N , K ) ) NEW_LINE DEDENT
Sum of all composite numbers lying in the range [ L , R ] for Q queries | Prefix array to precompute the sum of all composite number ; Function that return number num if num is composite else return 0 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to precompute the sum of all composite numbers upto 100000 ; checkcomposite ( ) return the number i if i is composite else return 0 ; Function to print the sum for each query ; Function to prsum of all composite numbers between ; Function that pre computes the sum of all composite numbers ; Iterate over all Queries to print the sum ; Driver code ; Queries ; Function that print the the sum of all composite number in Range [ L , R ]
pref = [ 0 ] * 100001 NEW_LINE def isComposite ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def preCompute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + isComposite ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT print ( pref [ R ] - pref [ L - 1 ] ) NEW_LINE DEDENT def printSumcomposite ( arr , Q ) : NEW_LINE INDENT preCompute ( ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Q = 2 NEW_LINE arr = [ [ 10 , 13 ] , [ 12 , 21 ] ] NEW_LINE printSumcomposite ( arr , Q ) NEW_LINE DEDENT
Probability of getting a perfect square when a random number is chosen in a given range | Python3 implementation to find the probability of getting a perfect square number ; Function to return the probability of getting a perfect square number in a range ; Count of perfect squares ; Total numbers in range l to r ; Calculating probability ; Driver code ; Function Call
import math NEW_LINE def findProb ( l , r ) : NEW_LINE INDENT countOfPS = ( math . floor ( math . sqrt ( r ) ) - math . ceil ( math . sqrt ( l ) ) + 1 ) NEW_LINE total = r - l + 1 NEW_LINE prob = countOfPS / total NEW_LINE return prob NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 16 NEW_LINE R = 25 NEW_LINE print ( findProb ( L , R ) ) NEW_LINE DEDENT
Find the length of factorial of a number in any given base | A optimised program to find the number of digits in a factorial in base b ; Returns the number of digits present in n ! in base b Since the result can be large long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code ; calling findDigits ( Number , Base )
from math import log10 , floor NEW_LINE def findDigits ( n , b ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT M_PI = 3.141592 NEW_LINE M_E = 2.7182 NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT x = ( ( n * log10 ( n / M_E ) + log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( log10 ( b ) ) NEW_LINE return floor ( x ) + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( findDigits ( 4 , 16 ) ) NEW_LINE print ( findDigits ( 5 , 8 ) ) NEW_LINE print ( findDigits ( 12 , 16 ) ) NEW_LINE print ( findDigits ( 19 , 13 ) ) NEW_LINE DEDENT
Sum of all Non | Python3 implementation to find the sum of all non - fibonacci numbers in a range from L to R ; Array to precompute the sum of non - fibonacci numbers ; Function to find if a number is a perfect square ; Function that returns N if N is non - fibonacci number ; N is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; Function to precompute sum of non - fibonacci Numbers ; Function to find the sum of all non - fibonacci numbers in a range ; Pre - computation ; Loop to find the sum for each query
from math import sqrt NEW_LINE pref = [ 0 ] * 100010 NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( sqrt ( x ) ) NEW_LINE if ( s * s == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isNonFibonacci ( n ) : NEW_LINE INDENT x = 5 * n * n NEW_LINE if ( isPerfectSquare ( x + 4 ) or isPerfectSquare ( x - 4 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return n NEW_LINE DEDENT DEDENT def compute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT sum = pref [ R ] - pref [ L - 1 ] NEW_LINE print ( sum , end = " ▁ " ) NEW_LINE DEDENT compute ( ) NEW_LINE Q = 2 NEW_LINE arr = [ [ 1 , 5 ] , [ 6 , 10 ] ] NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT
Count twin prime pairs in an Array | Python 3 program to count Twin Prime pairs in array ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are Twin Primes or not ; Function to find Twin Prime pairs from the given array ; Iterate through all pairs ; Increment count if twin prime pair ; Driver 's code ; Function call to find Twin Primes pair
from math import sqrt NEW_LINE 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 ( sqrt ( n ) ) + 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 twinPrime ( n1 , n2 ) : NEW_LINE INDENT return ( isPrime ( n1 ) and isPrime ( n2 ) and abs ( n1 - n2 ) == 2 ) NEW_LINE DEDENT def countTwinPairs ( arr , 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 ( twinPrime ( arr [ i ] , arr [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countTwinPairs ( arr , n ) ) NEW_LINE DEDENT
Fill the missing numbers in the array of N natural numbers such that arr [ i ] not equal to i | Function to fill the position with arr [ i ] = 0 ; Inserting all elements in missing [ ] set from 1 to N ; Inserting unfilled positions ; Removing allocated_elements ; Loop for filling the positions with arr [ i ] != i ; Checking for any arr [ i ] = i ; Finding the suitable position in the array to swap with found i for which arr [ i ] = i ; Checking if the position is present in unfilled_position ; Swapping arr [ i ] & arr [ pos ] ( arr [ pos ] = pos ) ; Function to Print the array ; Driver Code
def solve ( arr , n ) : NEW_LINE INDENT unfilled_indices = { } NEW_LINE missing = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT missing [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT unfilled_indices [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT del missing [ arr [ i ] ] NEW_LINE DEDENT DEDENT it2 = list ( missing . keys ( ) ) NEW_LINE m = len ( it2 ) NEW_LINE for it in unfilled_indices : NEW_LINE INDENT arr [ it ] = it2 [ m - 1 ] NEW_LINE m -= 1 NEW_LINE DEDENT pos = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == i ) : NEW_LINE INDENT pos = i NEW_LINE DEDENT DEDENT x = 0 NEW_LINE if ( pos != 0 ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( pos != i ) : NEW_LINE INDENT if i in unfilled_indices : NEW_LINE INDENT x = arr [ i ] NEW_LINE arr [ i ] = pos NEW_LINE arr [ pos ] = x NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT printArray ( arr , n ) NEW_LINE DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE solve ( arr , n ) NEW_LINE DEDENT
Find Kth number from sorted array formed by multiplying any two numbers in the array | Function to find number of pairs ; Negative and Negative ; Add Possible Pairs ; Positive and Positive ; Add Possible pairs ; Negative and Positive ; Add Possible pairs ; Function to find the kth element in the list ; Separate Positive and Negative elements ; Sort the Elements ; Binary search ; Return the required answer ; Driver code ; Function call
def check ( x , pos , neg , k ) : NEW_LINE INDENT pairs = 0 NEW_LINE p = len ( neg ) - 1 NEW_LINE nn = len ( neg ) - 1 NEW_LINE pp = len ( pos ) - 1 NEW_LINE for i in range ( len ( neg ) ) : NEW_LINE INDENT while ( p >= 0 and neg [ i ] * neg [ p ] <= x ) : NEW_LINE INDENT p -= 1 NEW_LINE DEDENT pairs += min ( nn - p , nn - i ) NEW_LINE DEDENT p = 0 NEW_LINE for i in range ( len ( pos ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( p < len ( pos ) and pos [ i ] * pos [ p ] <= x ) : NEW_LINE INDENT p += 1 NEW_LINE DEDENT pairs += min ( p , i ) NEW_LINE DEDENT p = len ( pos ) - 1 NEW_LINE for i in range ( len ( neg ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( p >= 0 and neg [ i ] * pos [ p ] <= x ) : NEW_LINE INDENT p -= 1 NEW_LINE DEDENT pairs += pp - p NEW_LINE DEDENT return ( pairs >= k ) NEW_LINE DEDENT def kth_element ( a , n , k ) : NEW_LINE INDENT pos , neg = [ ] , [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] >= 0 ) : NEW_LINE INDENT pos . append ( a [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT neg . append ( a [ i ] ) NEW_LINE DEDENT DEDENT pos = sorted ( pos ) NEW_LINE neg = sorted ( neg ) NEW_LINE l = - 10 ** 18 NEW_LINE ans = 0 NEW_LINE r = 10 ** 18 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) >> 1 NEW_LINE if ( check ( mid , pos , neg , k ) ) : NEW_LINE INDENT ans = mid NEW_LINE r = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ - 4 , - 2 , 3 , 3 ] NEW_LINE k = 3 NEW_LINE n = len ( a ) NEW_LINE print ( kth_element ( a , n , k ) ) NEW_LINE
Find root of a number using Newton 's method | Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code
def squareRoot ( n , l ) : NEW_LINE INDENT x = n NEW_LINE count = 0 NEW_LINE while ( 1 ) : NEW_LINE INDENT count += 1 NEW_LINE root = 0.5 * ( x + ( n / x ) ) NEW_LINE if ( abs ( root - x ) < l ) : NEW_LINE INDENT break NEW_LINE DEDENT x = root NEW_LINE DEDENT return root NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 327 NEW_LINE l = 0.00001 NEW_LINE print ( squareRoot ( n , l ) ) NEW_LINE DEDENT
Count the occurrence of Nth term in first N terms of Van Eck 's sequence | Python3 program to count the occurrence of nth term in first n terms of Van Eck 's sequence  ; Utility function to compute Van Eck 's sequence  ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Get nth term of the sequence ; Count the occurrence of nth term in first n terms of the sequence ; Return count ; Driver code ; Pre - compute Van Eck 's sequence  ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
MAX = 10000 NEW_LINE sequence = [ 0 ] * ( MAX + 1 ) ; NEW_LINE def vanEckSequence ( ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( sequence [ j ] == sequence [ i ] ) : NEW_LINE INDENT sequence [ i + 1 ] = i - j ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def getCount ( n ) : NEW_LINE INDENT nthTerm = sequence [ n - 1 ] ; NEW_LINE count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( sequence [ i ] == nthTerm ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT vanEckSequence ( ) ; NEW_LINE n = 5 ; NEW_LINE print ( getCount ( n ) ) ; NEW_LINE n = 11 ; NEW_LINE print ( getCount ( n ) ) ; NEW_LINE DEDENT
Count the occurrence of Nth term in first N terms of Van Eck 's sequence | Python3 program to count the occurrence of nth term in first n terms of Van Eck 's sequence  ; Utility function to compute Van Eck 's sequence  ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Initialize count as 1 ; Increment count if ( i + 1 ) th term is non - zero ; Previous occurrence of sequence [ i ] will be it ( i - sequence [ i + 1 ] ) th position ; Return the count of occurrence ; Driver code ; Pre - compute Van Eck 's sequence  ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
MAX = 10000 NEW_LINE sequence = [ 0 ] * ( MAX + 1 ) ; NEW_LINE def vanEckSequence ( ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( sequence [ j ] == sequence [ i ] ) : NEW_LINE INDENT sequence [ i + 1 ] = i - j ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def getCount ( n ) : NEW_LINE INDENT count = 1 ; NEW_LINE i = n - 1 ; NEW_LINE while ( sequence [ i + 1 ] != 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE i = i - sequence [ i + 1 ] ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT vanEckSequence ( ) ; NEW_LINE n = 5 ; NEW_LINE print ( getCount ( n ) ) ; NEW_LINE n = 11 ; NEW_LINE print ( getCount ( n ) ) ; NEW_LINE DEDENT
Goldbach 's Weak Conjecture for Odd numbers | Function to check if a number can be represent as as a sum of 3 prime ; Driver code
def check ( n ) : NEW_LINE INDENT if n % 2 == 1 and n > 5 : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT DEDENT def main ( ) : NEW_LINE INDENT a = 3 NEW_LINE b = 7 NEW_LINE check ( a ) NEW_LINE check ( b ) NEW_LINE DEDENT main ( ) NEW_LINE