Home > Mobile >  exactly even divisible using python recurssion
exactly even divisible using python recurssion

Time:04-23

  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)

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

CodePudding user response:

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

  • Related