0

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)

  • Why are you using lists if you are supposed to be working on strings? – Padraic Cunningham Sep 18 '16 at 17:00
  • You should have a look at `numpy` – Moses Koledoye Sep 18 '16 at 17:01
  • `vector1[i]` and `vector2[i]` – Jeon Sep 18 '16 at 17:02
  • 1
    `sum(v1*v2 for v1,v2 in zip(vector1, vector2))` – idjaw Sep 18 '16 at 17:02
  • Possible duplicate of [How to perform element-wise multiplication of two lists in Python?](http://stackoverflow.com/questions/10271484/how-to-perform-element-wise-multiplication-of-two-lists-in-python) – idjaw Sep 18 '16 at 17:03
  • @PadraicCunningham that's how the input I'm supposed to test comes through. This is one of them : dotProduct([1,2,3], [-2,3,5]). Hence why I use the above the way i use it. – Alexis Nonya Sep 18 '16 at 17:03
  • @AlexisNonya, but they are lists, anyway the logic is the same, zip would work for strings., list, generators .. – Padraic Cunningham Sep 18 '16 at 17:06
  • @PadraicCunningham I see. Thanks to you guys I see where I went wrong. I'll go and try and use the corrected code now and make a function out of it. – Alexis Nonya Sep 18 '16 at 17:15
  • 1
    @idjaw Thanks for your help. I saw that post you had linked to this question but I couldn't make heads or tails of it in order to solve my current code. – Alexis Nonya Sep 18 '16 at 17:16
  • @Jeon Thanks for your help. I can't believe it was that simple. I thought I would have to restructure the whole code. – Alexis Nonya Sep 18 '16 at 17:16
  • I think kkaosninja deserved the points more plus when you have 107k you don't need anymore rep. – thesonyman101 Sep 18 '16 at 18:18
  • Okay I reassigned it. Also I can't find a question I have but I'm not sure if that means it hasn't been asked yet. So far all questions I've asked had an answer somewhere. I just didn't word my query correctly – Alexis Nonya Sep 20 '16 at 02:28

2 Answers2

2

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
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Ah okay thanks for this. I tried the specific function you mentioned above with the test parameters I need and it returns none. Now that I know what was wrong with my first code I can make the function now. Thanks for your help – Alexis Nonya Sep 18 '16 at 17:13
  • @AlexisNonya, raising a ValueError is a general approach, if you are running thsi though some test automation software you will need to return whatever is expected when the lengths are different. – Padraic Cunningham Sep 18 '16 at 17:22
  • I already finished the function. I'd paste it here but it's too big to fit. And yes I do have an "if" statement in there that checks to see if both strings are of equal length. if that test fails it returns a line that says " Your inputs are invalid". Edit: I didn't use zip or numpy – Alexis Nonya Sep 18 '16 at 17:29
-1

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

kkaosninja
  • 1,301
  • 11
  • 21