Search code examples
pythonpython-3.xlistlist-manipulation

the multiplication of digits squares of numbers in a range


I want to obtain a list that provides specific requirements from a range my code can only multiply digits of numbers in a list. I want to get multiply "digits squares" of numbers in a list

For example: A range defined = (1,200)

wanted_list =[1^2,2^2,3^2,...,(34 = 3^2 * 4^2),(35 = 3^2 * 5^2),...,(199 = 1^2 * 9^2 * 9^2)]

Here is my code:

def mult(liste):
    a=1
    for i in liste:
        a*=i       #I think the problem is here
    return a

listemm = [x for x in range(1,200)]
print(listemm)
qe= [mult(int(digit) for digit in str(numb)) for numb in listemm]
print(qe)

Solution

  • I would do it like that:

    r = range(1, 200)
    
    
    def reduce_prod(n):
        p = 1
        for i in str(n):
            p *= int(i)**2
        return p
    
    
    wanted_list = [reduce_prod(x) for x in r]
    

    which produces:

    [1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 1, ...]
    #                                 ^
    #                                 from 10 -> 1^2 * 0^2 = 0