2. Duplicated Products You are given a complex list of n products, each with a name,…
- User
- Duplicated Products You are given a complex list of n products, each with a name, price, and weight. Find out how many duplicate products are present within the list. Duplicate products contain identical parameters for all fields in the list (i.e. nam price, and weight). Example: There are n = 5 products with attributes listed in three arrays, aligned by index. name = [ball, bat, glove, glove, glove] price 1, 2, 1] weight =[2,5, 1, 1, 1] A complete item description for item O: (name[O], prices[O], weight[O]) is (ball, 2, 2) Name ball bat glove glove glove Price Weight 2 3 1 2 1 2 5 1 1 1 The first two items are unique. The two gloves at indices 2 and 4are equal in all three attributes so there is 7 duplicate. The last glove at index 3 has a different price from the other two, so it is not a duplicate. There is 1 duplicate item in the original list. Function Description Complete the function numDup/icates in the editor below. The function must return an integer denoting the number of duplicates within the product list. Complete the function numDup/icates in the editor below. The function must return an integer denoting the nu of duplicates within the product list. numDup/icates has the following parameter(s): string name[nj. string array of size n, where names[i] denotes the name of the product at the index of i. int price[nj. int array of size n, where prices[i] denotes the price of the product at the index of i. int weight[n]: int array of size n, where weights[i] denotes the weight of the product at the index of i. Constraints • Isns105 0 name[i]is non-empty, has at most 10 characters, and all its characters are lowercase English letteÅö ascii[a-z] • 1 sprice[i], 1000 Input Format Format for Custom Testing V Sample Case O Sample Input STDIN 5 box bait ball box 5 2 2 2 2 2 5 1 2 1 1 Function namer J size n name = ['ball' ' box' price[] size n - 5 price - weight [J size n -5 weight 'ball' 'ball' , 'box'] 2] 3]
- Assistant
To solve the problem of counting duplicate products based on their name, price, and weight, we'll create a function called
numDuplicates. This function will iterate through all the products and keep track of how many times each unique product appears.Here's how the function works:
-
Use a dictionary (or hashmap) to store the counts of each unique product. The key will be a tuple consisting of the product's name, price, and weight.
-
Iterate through all the products using a loop, and for each product, increment its count in the dictionary.
-
After counting all products, iterate through the dictionary's values to calculate the total number of duplicates. For each product that appears more than once, add
(count - 1)to the total duplicates count. -
Return the total duplicates count.
Here is the implementation in Python:
def numDuplicates(name, price, weight): from collections import defaultdict counts = defaultdict(int) for n, p, w in zip(name, price, weight): key = (n, p, w) counts[key] += 1 duplicates = sum(count -1 for count in counts.values() if count >1) return duplicates -