-3

The task is to find the difference in the products of the digits and sum of digits. For example, an input of 4 5 6 would give 105. The issue I'm having is dealing with previous iterations. I know it has something to do with the indexes of the list but I'm having difficulty figuring it out. Here's my code:

# find the difference between the product of all digits and sum of all digits
nums = input()
nums = list(nums.split())

def findProduct():
    for i in range(len(nums)):
        int(nums[i]) *= int(nums[i+1])


findProduct()
stuckyp
  • 45
  • 1
  • 6
  • What is the issue that you have with previous iterations? – mkrieger1 Jan 26 '20 at 23:19
  • Does this answer your question? [Returning the product of a list](https://stackoverflow.com/questions/2104782/returning-the-product-of-a-list). Also, [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – Tomerikoo Jan 26 '20 at 23:20
  • Variable and function names should follow the `lower_case_with_underscores` style. – AMC Jan 26 '20 at 23:46
  • You can't assign to the result of a function call, so `int(nums[i]) *= int(nums[i+1])` is a syntax error. – ekhumoro Jan 27 '20 at 00:08
  • @AMC Depends on the context. This was a [LeetCode problem](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/) just a few weeks ago and they use camelCase. – Kelly Bundy Jan 27 '20 at 00:12
  • @HeapOverflow Alright then, I'll be ticked off at LeetCode instead ;) – AMC Jan 27 '20 at 00:34

4 Answers4

1

You could use numpy:

import numpy as np

l = [4,5,6]

print(np.prod(l) - sum(l))
BpY
  • 403
  • 5
  • 12
0

You can use a built-in function for the sum (it's called sum), and for the product, multiplying all items in a list is an answered question:

from functools import reduce

l = [4,5,6]

def findProduct(nums):
    return reduce(lambda x, y: x*y, nums) - sum(nums)

print (findProduct(l))

Since Python 3.8 there is a function math.prod so it can be further reduced to

return math.prod(nums) - sum(nums)

(and it requires import math instead of functools).

math.prod tip comes courtesy of Heap Overflow

Jongware
  • 22,200
  • 8
  • 54
  • 100
0

math.prod does what you want. Just feed the array into it:

def fn(array):
    return math.prod(array) - sum(array)
Alec
  • 8,529
  • 8
  • 37
  • 63
0
  • numpy.prod() multiplies all the numbers in an array.
  • sum() adds all the numbers in an array.
  • Instead of messing around with len, you can simply say, for i in arrayName: and get what are trying to get

Here's what I did:

import numpy as np
Num = input("Enter the number: ")
def getDifference(num):
    '''Takes an input of a number (as a string) and
       returns the difference between the sum and product
       of its digits'''
    numList = []
    for i in num:
        numList.append(int(i))
    Sum = sum(numlist)
    Product = np.prod(numList)
    Difference = abs(Product - Sum)
    return Difference
print(getDifference(Num))

This works as you want it to. Note that you could make this code much smaller, as:

import numpy as np
Num = input("Enter the number: ")
def getDifference(num):
    '''Takes an input of a number (as a string) and
       returns the difference between the sum and product
       of its digits'''
    numList = [int(i) for i in str(num)]
    return abs(np.prod(numList)-sum(numList))
print(getDifference(Num))
Mike Smith
  • 527
  • 2
  • 6
  • 20