#python #anaconda #jupyter-notebook #visual-code #html #css #javascript #http

제목월수 Quiz4 문제 및 풀이2019-12-03 13:59
작성자

* AdvList 클래스는 list를 상속받은 클래스로, 멤버 메서드로 removeAll과 replace만을 가진다. 

* removeAll의 입력 파라미터는 value가 있으며, list내의 모든 value를 삭제한다. removeAll의 반환값은 없다.

* replace의 입력 파라미터는 old, new, cnt가 있으며, cnt의 값만큼 순차적으로 old를 new로 replace를 한다. replace 메서드의 반환값은 없다.

* cnt는 -1 이상의 정수이다. cnt == -1 혹은 cnt > number of old라면, 존재하는 모든 old를 new로 replace한다.


class AdvList(list): # list 상속시킴

def removeAll(self, value):

while value in self: # 리스트 내에 value가 있다면 계속 반복

self.remove(value) # 삭제

def replace(self, old, new, cnt):

if cnt >= -1: # cnt가 -1 이상의 정수이므로

while (cnt != 0) and (old in self): # cnt 만큼 바꿔주는식으로.. 하지만 cnt가 리스트 내의 old 개수보다 클 경우 list내의 old가 전부 replace될 때 까지 반복

self[self.index(old)] = new # old의 첫번째 index를 받아온 뒤 리스트의 해당 index의 값을 new로 바꿔줌

cnt = cnt - 1 # 카운트다운


댓글
이전class 의 상속에 대한 질문이요2019-12-03
다음quiz42019-12-03