bcb-0007
1.000
Proposed solution
```python
import csv
import collections
import operator
def task_func(csv_file_path):
"""
Find the best-selling product from a given CSV file with sales data.
This function parses a CSV file assumed to have a header followed by rows containing
two columns: 'product' and 'quantity'. It computes the total sales per product and
determines the product with the highest cumulative sales. The CSV file must include
at least these two columns, where 'product' is the name of the product as a string
and 'quantity' is the number of units sold as an integer.
Args:
csv_file_path (str): The file path to the CSV file containing sales data.
Returns:
str: The name of the top-selling product based on the total quantity sold.
Requirements:
- csv
- collections
- operator
Example:
>>> task_func("path/to/sales.csv")
'Product ABC'
"""
# Initialize a counter to track total sales per product
product_sales = collections.Counter()
# Read the CSV file
with open(csv_file_path, 'r') as file:
reader = csv.DictReader(file)
# Iterate through each row and accumulate quantities by product
for row in reader:
product = row['product']
quantity = int(row['quantity'])
product_sales[product] += quantity
# Find the product with the maximum total sales
best_selling_product = max(product_sales.items(), key=operator.itemgetter(1))[0]
return best_selling_product
```Test output (stdout)
....... [100%] 7 passed in 0.02s
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.