Ask Your Question
2

When receiving a complete vector of objects, the function does not receive a single vector.

asked 2023-04-29 14:37:34 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-04-29 15:02:02 +0000

lalupa gravatar image

Rather, it receives a collection or an array of vectors, each of which contains objects. The function then iterates through these vectors and accesses the objects contained within them to perform its intended operation. For example:

def total_price(items):
    total = 0
    for item_set in items:
        for item in item_set:
            total += item.price
    return total

# Usage
items = [[Item("apple", 1.50), Item("banana", 0.75)], [Item("orange", 2.00)]]
print(total_price(items))  # prints 4.25

In this example, the total_price function receives a list of two lists, each of which contains Item objects. It iterates through both lists and calculates the total price by accessing the price attribute of each Item object. The final result is returned.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-04-29 14:37:34 +0000

Seen: 8 times

Last updated: Apr 29 '23