Home > Mobile >  Find the sum of Two Arrays in Python
Find the sum of Two Arrays in Python

Time:01-08

I am trying to find the sum of two lists/arrays in python.

For example:

You are given with two random integer lists as lst1 and lst2 with size n and m respectively. Both the lists contain numbers from 0 to 9(i.e. single digit integer is present at every index).

The idea here is to represent each list as an integer in itself of digits N and M.

You need to find the sum of both the input list treating them as two integers and put the result in another list i.e. output list will also contain only single digit at every index.

Following is the code which I have tried:

def list_sum(lst1, n, lst2, m) :
i, j, sum, carry = 0, 0, 0, 0
new_lst = []
if n == 0 and m == 0:
    new_lst.append(0)
elif n > 0 and m>0:
    while n > 0 and m > 0:
        sum = lst1[n - 1]   lst2[m - 1]   carry
        if sum >= 10:
            carry = 1
        else:
            carry = 0
        new_lst.append(sum % 10)
        n -= 1
        m -= 1
    while n > 0:
        if (lst1[n-1]   carry) >= 10:
            new_lst.append((lst1[n-1]   carry) % 10)
            carry = 1
        else:
            new_lst.append(lst1[n-1])
            carry = 0
        n -= 1
    while m > 0:
        if (lst2[m-1]   carry) >= 10:
            new_lst.append((lst2[m-1]   carry) % 10)
            carry = 1
        else:
            new_lst.append(lst1[m-1])
            carry = 0
        m -= 1
    if carry == 1:
        new_lst.append(1)
    new_lst.reverse()
elif n == 0 and m > 0:
    new_lst.append(0)
    new_lst = new_lst   lst2
elif n > 0 and m == 0:
    new_lst.append(0)
    new_lst = new_lst   lst1
print(new_lst)

however I feel I am missing something here and which is nto giving me proper answer for the combination.

The Example input:

n = 3
lst1 = [6, 9, 8] 
m = 3
lst2 = [5, 9, 2]

output:

[1, 2, 9, 0]

Here, each element if summed and then if the sum >=10 then we get a carry = 1 and which will be added with the next sum.

i.e

1. 8 2= 10 >=10 hence carry=1 in first sum
2. 9 9 1( carry) = 19 >=10 hence carry=1
3. 6 5 1( carry) = 12>=10 hence carry=1
4. upend the carry to next position as 1
Hence resultant list would be [1, 2, 9, 0]

Please help me with best possible solution on this problem.

CodePudding user response:

If I understand correctly you want it like this: [6, 9, 8], [5, 9, 2] -> 698 592 = 1290 -> [1, 2, 9, 0]

In that case my first idea would be to turn the numbers into strings, combine them to one string and turn it into an int, then add both values together and turn into a list of integers again... you can try this:

def get_sum_as_list(list1, list2):
    first_int = int(''.join(map(str,list1)))
    second_int = int(''.join(map(str,list2)))
    result = [int(num) for num in str(first_int second_int)]
    return result

CodePudding user response:

I would do it this way:

def add_lists_of_digits(lst1, lst2):
    n1 = sum(i*10**j for j, i in enumerate(lst1[::-1]))
    n2 = sum(i*10**j for j, i in enumerate(lst2[::-1]))
    return [int(i) for i in str(n1   n2)]
  •  Tags:  
  • Related