Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

why is def count _ duplicates ( self ) : seen = set ( ) duplicates = 0 for i in range ( len (

why is
def count_duplicates(self):
seen = set()
duplicates =0
for i in range(len(self)):
if self[i] in seen:
continue # Skip if already counted as a duplicate
for j in range(i+1, len(self)):
if self[i].person.__eq__(self[j]):
duplicates +=1
seen.add(self[i])
break # Once a duplicate is found, move to the next person
return duplicates
not working in this test case
def test_duplicates(self):
"""Test if two elements in contacts are duplicates. This also depends on
the equality check for Person. However, it also depends on how you define
duplicates in Contact as the method is not inherited from list.
"""
c1= contacts.Contacts()
# only one item in the list, there is no way we have duplicates
c1.append(self.a1)
self.assertEqual(0, c1.count_duplicates())
# the same person added, we now should have one duplicate
c1.append(self.a1)
self.assertEqual(1, c1.count_duplicates())
# a different instance is added for the same person's information, the duplicate count should stay the same
c1.append(self.a2)
self.assertEqual(1, c1.count_duplicates())
# we added a person that we know was never in the list, duplicate count should stay the same
c1.append(self.khaled)
self.assertEqual(1, c1.count_duplicates())
# we added a person with very close information as with the one above but not really the same.
c1.append(self.a_different_khaled)
self.assertEqual(1, c1.count_duplicates())
# we added the original instance for Khaled, the count should go up by one.
c1.append(self.khaled)
self.assertEqual(2, c1.count_duplicates())
this is the question:
Start implementing and testing the Contacts class. The Contacts should extend the built-in list class. Otherwise, you would have to implement all the methods already provided by list.
The Contacts class should NOT have an initializer. That is, it uses its parent initializer.
A method you have to add, however, is the count_duplicates method which takes only self as an argument and returns an integer. This will be highly dependent on your correct implementation of Person.__eq__(). The count duplicate (as given by the name) should count how many of its elements are the same. For example, if we have the list [a, b, a, a], the count of duplicates is 1(even if the value a was observed three times, we say that a itself has a duplicate thus the count is 1). In your case, you will be considering a person's object instead of the letters a or b.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions