Alexis Nonya
Alexis Nonya

Reputation: 3

How to Multiply the numbers within a string with another string of equal length and then add each product in Python

I am trying to write a function that takes each element within two strings of equal length and multiply them by each other and then add each product to get a single answer. vector1 = [1, 2, 3] vector2 = [4, 5, 6]

For example the above would be (1*4) + (2*5) + (3 * 6) = 32

So far this is what I have:

vector1 = [1, 2, 3]
vector2 = [4, 5, 6]
ans = 0


if len(vector1) == 0 or len(vector2) == 0:
    print("Invalid vectors")
elif len(vector1) != len(vector2):
    print("Invalid vectors")
else:
    for i in range (len(vector1)):
        temp = vector1(i) * vector2(i)
        ans = ans + temp

The code gives me the following error when compiling:

TypeError: 'list' object is not callable

I tried changing the above code to be more like

vector1 = [1, 2, 3]
vector2 = [4, 5, 6]
ans = 0


if len(vector1) == 0 or len(vector2) == 0:
    print("Invalid vectors")
elif len(vector1) != len(vector2):
    print("Invalid vectors")
else:
    for i in range (len(vector1)) and i in range(len(vector2)):
        temp = vector1(i) * vector2(i)
        ans = ans + temp

But that doesn't work either. What am I doing wrong here?

Edit: After running the code through Python Visualizer, the following line is giving me the specific problem here:

temp = vector1(i) * vector2(i)

Upvotes: 0

Views: 136

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180482

zip and sum, you also need to cast to int presuming you have strings of digits:

def func(s1, s2):
    if len(s1) != len(s2):
        raise ValueError("Strings must be the same length")
    return sum(int(i) * int(j) for i,j in zip(s1, s2))

zip would still work with uneven length strings but if they should be the same length then a ValueError is probably the most appropriate response.

The error in your own code is how you are trying to index:

temp = vector1(i) * vector2(i)

should be:

temp = vector1[i] * vector2[i]

vector1(i) is trying to call the list as if it were a reference to a function. The [i] actually indexes the list. This is tailor made for zip but if you were to use a for loop and index, you should use enumerate:

def func(s1, s2):
    if len(s1) != len(s2):
        raise ValueError("Strings must be the same length")
    total = 0
    for ind, ele in enumerate(s1):
        total += int(ele) * int(s2[ind])
    return total

Upvotes: 2

wandering-geek
wandering-geek

Reputation: 1378

The corrected version of your code.

vector1 = [1, 2, 3]
vector2 = [4, 5, 6]
ans = 0


if len(vector1) == 0 or len(vector2) == 0:
    print("Invalid vectors")
elif len(vector1) != len(vector2):
    print("Invalid vectors")
else:
    for i in range(len(vector1)):
        temp = vector1[i] * vector2[i]
        ans += temp
print ans

All you have to do is change vector1(i) to vector[i], to access the element at index i in the lists

Upvotes: -1

Related Questions