본문 바로가기
[스파르타코딩클럽]데이터분석 과정/PYTHON

[Python 코드카타] 70~72번(프로그래머스)

by doo_ 2024. 2. 1.

70번 바탕화면정리

def solution(wallpaper):
    answer = []
    row = []
    column = []
    #lux, rdx 구하기
    for i in range(len(wallpaper)) :
        if wallpaper[i].find('#') != -1 :
            row.append(i)
    lux, rdx = row[0], row[-1]+1
    #luy, rdy 구하기
    for j in wallpaper :
        for idx, file in enumerate(j) :
           if file == '#':
               column.append(idx) 
    column.sort()
    luy, rdy = column[0], column[-1]+1
    # 순서대로 대입하기
    answer.append(lux)
    answer.append(luy)
    answer.append(rdx)
    answer.append(rdy)
    return answer

> 위를 줄인 해답

def solution(wall):
    row, column = [], []
    for i in range(len(wall)):    # for i, row in enumerate(wallpaper):
        for j in range(len(wall[i])):  # for j, col in enumerate(row):
            if wall[i][j] == "#":
                row.append(i)
                column.append(j)
    return [min(row), min(column), max(row) + 1, max(column) + 1]

 

71번 개인정보수집유효기간

> 코드를 잘 못짜서 결국 답안 참고ㅠ

def solution(today, terms, privacies):
    d ={}
    answer = []
    today_lst = list(map(int,today.split('.'))) # 오늘 날짜 리스트로 변환
    
    for term in terms: # 약관종류와 개월수 딕셔너리로 저장
        n, m = term.split()
        d[n] = int(m)*28 # 한 달 일 수 곱해줌
    
    for i in range(len(privacies)):
        date, s = privacies[i].split()
        date_lst = list(map(int, date.split('.'))) # 수집일자 리스트로 변환
        year = (today_lst[0] - date_lst[0])*336 # 연도 차이에 일 수 곱해주기
        month = (today_lst[1] - date_lst[1])*28 # 달 수 차이에 일 수 곱해주기
        day = today_lst[2] - date_lst[2]
        total = year+month+day
        if d[s] <= total:
            answer.append(i+1)
    return answer

 

 

72번 달리기경주

> 시간초과 문제

def solution(players, callings):
    for call in callings :
        idx = players.index(call)
        players[idx] = players[idx-1]
        players[idx-1] = call
    return players

> dictionary를 통해 해당 idx를 찾는 시간 단축, 해답 참고

def solution(players, callings):
	# dict를 통해 해당 선수 이름과 순위
    result = {player: rank for rank, player in enumerate(players)}
    for call in callings:
        idx = result[call] # 호명된 선수의 현재 등수
        result[call] -= 1 # 하나 앞 등수로 바꿔줌 -1
        result[players[idx-1]] += 1 # 앞에 위치했던 선수의 등수 +1
        players[idx-1], players[idx] = players[idx], players[idx-1] # 위치 변경
    return players