CodeWars 11 : What a "Classy" Song

김기욱·2021년 6월 7일
0

코딩테스트

목록 보기
48/68

문제설명

Your job is to create a class called Song.

A new Song will take two parameters, title and artist.

mount_moose = Song('Mount Moose', 'The Snazzy Moose')

mount_moose.title => 'Mount Moose'
mount_moose.artist => 'The Snazzy Moose'

ou will also have to create an instance method named howMany() (or how_many()).

The method takes an array of people who have listened to the song that day. The output should be how many new listeners the song gained on that day out of all listeners. Names should be treated in a case-insensitve manner, i.e. "John" is the same as "john".

요약 : 타이틀, 아티스트 리턴하는 함수, 사람 숫자 세는 함수가 있는 클래스를 만드시오
단 사람 숫자는 이전에 함수콜 했을 때 카운팅 된 사람은 제외

제한사항

'John' , 'joHn' , 'jOhN' 모두 다 같은 사람임

입출력 예시

mount_moose = Song('Mount Moose', 'The Snazzy Moose')

# day 1 (or test 1)
mount_moose.how_many(['John', 'Fred', 'BOb', 'carl', 'RyAn']) => 5
# These are all new since they are the first listeners.

# day 2
mount_moose.how_many(['JoHn', 'Luke', 'AmAndA']); => 2
# Luke and Amanda are new, john already listened to it the previous day

풀이

class Song:
    def __init__(self, a, b):
        self.title = a
        self.artist = b
        self.listener = []
        
    def title(self):
        return self.title
    
    def artist(self):
        return self.artist
    
    def how_many(self, people):
        count = []
        for v in people:
            if v.upper() in self.listener: # 대문자로 치환해서 비교
                continue
            else:
                self.listener.append(v.upper())
                count.append(v)
                
        return len(count)
profile
어려운 것은 없다, 다만 아직 익숙치않을뿐이다.

0개의 댓글