| title | tags | |||
|---|---|---|---|---|
Determine Top Product by Sales |
|
Write a function top_selling_product that processes a list of tuples containing product names and their sales. The function should:
- Sort the products by their sales in descending order.
- In case of a tie, choose the product that comes first alphabetically.
- Return the name of the top-selling product.
A list of tuples where each tuple contains:
product_name(str): The name of the product.sales(int): The total sales of the product.
A string containing the name of the top-selling product.
<prefix>
</prefix>
<template>
def top_selling_product(sales_data):
'''
Determine the top-selling product based on sales data.
Arguments:
sales_data: list of tuples - [(product_name (str), sales (int))]
Returns:
str - The name of the top-selling product.
'''
<los>...</los>
<sol>
# Sort products by sales in descending order and by name alphabetically
sorted_data = sorted(sales_data, key=lambda x: (-x[1], x[0]))
return sorted_data[0][0] # Return the product name with the highest sales
</sol>
</template>
<suffix>
input_data = input().strip()
sales_data = eval(input_data)
result = top_selling_product(sales_data)
print(result)
</suffix>
<suffix_invisible>
</suffix_invisible>[('ProductA', 150), ('ProductB', 200), ('ProductC', 200)]
ProductB
[('Alpha', 300), ('Beta', 300), ('Gamma', 250)]
Alpha
[('WidgetX', 400), ('WidgetY', 100), ('WidgetZ', 400)]
WidgetX
[('Item1', 50), ('Item2', 75), ('Item3', 75)]
Item2
[('Phone', 500), ('Laptop', 500), ('Tablet', 300)]
Laptop
[('Chair', 10), ('Table', 15), ('Desk', 15)]
Desk