0

I'm trying to writhe a function that receives two lists in its input and returns their standard internal multiplication. I tried to do this with the FOR loop help but without success. The function returns None if the list length is not the same and also returns "0" if the lists are empty. I would be very happy if you examine my code and tell me what the problem might be and why the action is not being done to me.

Code:

def inner_product(vec_1, vec_2):
    counter = 0
    for i in range(len(vec_1), len(vec_2)):
        if len(vec_1) != len(vec_2):
            return None 
        elif len(vec_1) or len(vec_2) == []: 
            return "0" 
        else: 
            counter = vec_1[i]*vec_2[i] 
            return counter 

print(inner_product([1, 2, 3], [4, 5, 6])) 
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • here is my code: –  Nov 13 '19 at 19:11
  • def inner_product(vec_1, vec_2): counter = 0 for i in range(len(vec_1), len(vec_2)): if len(vec_1) != len(vec_2): return None elif len(vec_1) or len(vec_2) == []: return "0" else: counter = vec_1[i]*vec_2[i] return counter print(inner_product([1, 2, 3], [4, 5, 6])) –  Nov 13 '19 at 19:11
  • 1
    Does this answer your question? [Why does \`a == b or c or d\` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – Green Cloak Guy Nov 13 '19 at 19:26
  • In addition to that, `len()` returns an integer length; if you want to check if a list is empty, you can either do `len(vec_1) == 0`, `vec_1 == []`, or even just `vec_1`, but not what you're currently doing. – Green Cloak Guy Nov 13 '19 at 19:27
  • unfortantly its not the same quastion of my one. but thank you anyway –  Nov 13 '19 at 19:34
  • "Standard internal multiplication", what does this mean? – kaya3 Nov 13 '19 at 19:52
  • 1
    FYI, if your question has been answered, please see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – NicholasM Dec 05 '19 at 18:13

2 Answers2

0

If I understand what you want, you're looking to do three things:

  1. If the "vectors" (in python, lists) are not the same length, return None
  2. If either list is empty, return 0
  3. Otherwise, multiply the numbers together from the same index in each list. e.g element 1 * element 1, element 2 * element 2.
  4. Return that list from your function to be printed
def inner_product(vec_1, vec_2):
    # Requirement 1
    if len(vec_1) != len(vec_2):
            return None
    # Requirement 2 
    elif vec_1 == [] or vec_2 == []: 
        return 0

    # Define a list to hold your values until you return it
    counter = [] 

    # range() takes a beginning integer (index in this case)
    # and an ending one. However, the range is exclusive of the last number
    # so even though len(vec_1) == 3, range will stop at 2

    for i in range(0, len(vec_2)):
        # Append the output to your counter array on each pass
        counter.append(vec_1[i]*vec_2[i])

    # Once the for loop is done, return the value    
    return counter

# These should all return True

assert inner_product([], []) == 0
assert inner_product([1,2,3], [1,2]) is None
assert inner_product([1,2,3], [3,4,5]) == [3,8,15]
currand60
  • 119
  • 8
0

Assuming you want to compute the sum of the element-wise products, you can use the zip and sum builtins. The zip function will iterate over two or more sequences together, in lockstep.

def inner_product(vec_1, vec_2):
    """ Return the inner product of `vec_1` and `vec_2` if they have the 
    same length, or None otherwise.

    EXAMPLES
    >>> inner_product([], [])
    0
    >>> inner_product([1, 2], [3, 4])
    11
    >>> inner_product([1, 2, 3], [7]) is None
    True
    """
    if len(vec_1) != len(vec_2):
        return None

    return sum(e1 * e2 for e1, e2 in zip(vec_1, vec_2))

In this function, the empty list case is handled implicitly, since the default starting value for sum is 0.

NicholasM
  • 4,557
  • 1
  • 20
  • 47