Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

exactly even divisible using python recurssion

  def extractlyEvenlyDivisibleHelp(inputlst, number, outputlst = []):
        if number == 0:
            print([])
            return
        if inputlst[0] % number == 0:
            outputlst.append(inputlst[0])
            outputlst.sort()
    
    
        inputlst.remove(inputlst[0])
    
        if len(inputlst) == 0:
            print(outputlst)
        else:
            extractlyEvenlyDivisibleHelp(inputlst, number, outputlst)
    
    
    def extractlyEvenlyDivisible(input, number):
        extractlyEvenlyDivisibleHelp(input, number, [])

extractlyEvenlyDivisible([1,2,3,4,5,6,7,8,9], 0)

extractlyEvenlyDivisible([1,2,-3,4,5,-6,7,8,9,9,6], -3)

extractlyEvenlyDivisible([1,2,3,4,5,6,7,8,9,10, 10], 5)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Output:

[ ]

[-6, -3, 6, 9, 9]

[5, 10, 10]

Expected output:

[ ]

[-6, -3, 6, 9]

[5, 10]

I need help, as I need only one 9, I mean if I input couple times the same digit, it should return only one time

>Solution :

You could filter out duplicates from the input list before passing to the helper using set:

def extractlyEvenlyDivisible(inputlst, number):
    extractlyEvenlyDivisibleHelp(list(set(inputlst)), number, [])

Also it is bad practice to redefine built-ins like input even in a function, I would change this to a different variable name

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading