Answered step by step
Verified Expert Solution
Question
1 Approved Answer
why is def count _ duplicates ( self ) : seen = set ( ) duplicates = 0 for i in range ( len (
why is
def countduplicatesself:
seen set
duplicates
for i in rangelenself:
if selfi in seen:
continue # Skip if already counted as a duplicate
for j in rangei lenself:
if selfiperson.eqselfj:
duplicates
seen.addselfi
break # Once a duplicate is found, move to the next person
return duplicates
not working in this test case
def testduplicatesself:
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.
c contacts.Contacts
# only one item in the list, there is no way we have duplicates
cappendselfa
self.assertEqual ccountduplicates
# the same person added, we now should have one duplicate
cappendselfa
self.assertEqual ccountduplicates
# a different instance is added for the same person's information, the duplicate count should stay the same
cappendselfa
self.assertEqual ccountduplicates
# we added a person that we know was never in the list, duplicate count should stay the same
cappendselfkhaled
self.assertEqual ccountduplicates
# we added a person with very close information as with the one above but not really the same.
cappendselfadifferentkhaled
self.assertEqual ccountduplicates
# we added the original instance for Khaled, the count should go up by one.
cappendselfkhaled
self.assertEqual ccountduplicates
this is the question:
Start implementing and testing the Contacts class. The Contacts should extend the builtin 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 countduplicates 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 even if the value a was observed three times, we say that a itself has a duplicate thus the count is 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started