Search code examples
loopsoopfundamentals-ts

Python OOP challenge, problem with object and method


 class Song:
     def __init__(self, title, artist):
        self.title = title
        self.artist = artist


    def how_many(self, listener):
        print(listener) 
    


obj_1 = Song("Mount Moose", "The Snazzy Moose")
obj_1.how_many(['John', 'Fred', 'Bob', 'Carl', 'RyAn'])
obj_1.how_many(['Luke', 'AmAndA', 'JoHn']) here
 

#the listener contains 2 lists inside, anything I do produces two lists, is there a way to separate the list inside the listener without changing the objects calling the how_many method simultaneously. Thank you!!! in advance


Solution

    • I'm not sure the input JoHn in second wave is a typo or you need to capitalize all input. I assume you need capitalize it.
    • You can use set to deal with the remove the duplicate in mutiple input.

    example code:

    class Song:
        def __init__(self, title, artist):
            self.title = title
            self.artist = artist
            self.linstener = set()
    
        def how_many(self, listener):
            listener = [ele.capitalize() for ele in listener]
            print(len((self.linstener | set(listener)) ^ self.linstener))
            self.linstener.update(listener)
            # print(listener) 
    
    obj_1 = Song("Mount Moose", "The Snazzy Moose")
    obj_1.how_many(['John', 'Fred', 'Bob', 'Carl', 'RyAn'])
    obj_1.how_many(['Luke', 'AmAndA', 'JoHn'])
    

    result:

    5
    2