첫번째 문제는
namelist([ {'name': 'Bart'}, {'name': 'Lisa'}, {'name': 'Maggie'} ])
#returns 'Bart, Lisa & Maggie'
namelist([ {'name': 'Bart'}, {'name': 'Lisa'} ])
#returns 'Bart & Lisa'
namelist([ {'name': 'Bart'} ])
#returns 'Bart'
namelist([])
#returns ''
def namelist(names): el=[] fi = [] for i in names: el.append(i['name']) if len(el)<=1: return "".join(el) else: two = ' & '.join(el[-2:]) for i in el[:-2]: fi.append(i + ',') fi.append(two) return " ".join(fi)
빈 리스트 두개를 만든 후 첫번째 리스트에 입력값의 벨류값을 받아준다 만약 그 길이가 1이라면 그냥 리턴해주고 그게 아닐 시 이름의 입력이 두개라면 fi는 여전히 빈 리스트이고 거기에 two를 더해서 리턴하고 이름이 세게일 경우 fi에 two를 더한 후 리턴해준다
두번째는
delete_nth ([1,1,1,1],2) # return [1,1]
delete_nth ([20,37,20,21],1) # return [20,37,21]
def delete_nth(order,max_e): count = {} res = [] for i in order: if i not in count : count[i] = 0 else: count[i] += 1 if count[i] <max_e: res.append(i) return res
반복문을 통해서 order의 값들을 dict 형태로 바꾸어준다. 그 다음 만약 count[i]의 값이 max_e보다 작을경우 결과 리스트에 i를 추가해준다.