programmers- lv.1 (개인정보 수집 유효기간)

이예송·2023년 7월 19일

PS

목록 보기
51/97

문제링크: 개인정보 수집 유효기간

✍🏻 Information

content
언어python
난이도⭐️⭐️⭐️
풀이시간35분
제출횟수5
인터넷검색유무yes




🍒 My Code

def solution(today, terms, privacies):
    answer = []
    term = {}
    for t in terms:
        term[t.split()[0]]=t.split()[1]
    
    for idx, p in enumerate(privacies):
        date,types = p.split()[0],p.split()[1]
        year,month,day = int(date.split(".")[0]),int(date.split(".")[1]),int(date.split(".")[2])
        month = month+int(term[types])
        if month>12:
            year+=month//12
            month-=(month//12)*12
        day-=1
        if day==0:
            day = 28
            month-=1
        if month==0:
            month = 12
            year-=1
        #print(year,month,day)
        if int(today.split(".")[0])>year:
            answer.append(idx+1)
        elif int(today.split(".")[0])==year:
            if int(today.split(".")[1])>month:
                answer.append(idx+1)
            elif int(today.split(".")[1])==month:
                if int(today.split(".")[2])>day:
                    answer.append(idx+1)
    return answer




💡 What I learned

  • 질문하기 보고 1)month>12일땐 단순히 year+1해주고 month-12해줬었는데 2년이 더 지날수도 있다는걸 깨닫고 그 부분을 수정해줬고 2)내 코드로 돌리면 month가 0이 되는 경우도 있어서 month==0부분 처리해줘야되는거 알았다. 안되는거 있을때마다 막아주는 식으로 풀어서 아쉽다. 내가 스스로 틀린 부분 찾지 못한것도.
  • 아래와 같은 함수를 만들어서 아예 일수로만 바꿔버려서 비교하는게 더 좋을것같다. (문제에서 모든 달은 다 28일이라고 맞춰줬으므로)
def to_days(date):
    year, month, day = map(int, date.split("."))
    return year * 28 * 12 + month * 28 + day
  • map(function, iterable) : 리스트의 요소를 지정된 함수로 처리해주는 함수(원본 리스트를 변경하지 않고 새 리스트를 생성).
    두 번째 인자로 들어온 반복 가능한 자료형 (리스트나 튜플)을 첫 번째 인자로 들어온 함수에 하나씩 집어넣어서 함수를 수행하는 함수임.
    즉, map(적용시킬 함수, 적용할 값들)로 생각할 수 있음.
    다만, map 함수의 반환 값은 map객체 이기 때문에 해당 자료형을 list 혹은 tuple로 형 변환시켜주어야 함

    - iterable: 반복 가능한 자료형(리스트, 튜플 등)
    - list(map(함수, 리스트)) 
    - tuple(map(함수, 튜플))
a = list(map(str, range(10)))

>> a = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

0개의 댓글