본문 바로가기
스터디/Python

[CodingTest][2022-12-05]프로그래머스 스킬 테스트 Level 1

by SeO.V 2022. 12. 7.

스킬 체크 테스트 Level.1 / 문제 1

https://programmers.co.kr/skill_checks/443156?challenge_id=978

 

문제

프로그래머스 스킬 테스트 Level 1 문제 1

from datetime import date

def solution(a, b):
    answer = ''
    days = ['MON','TUE','WED','THU','FRI','SAT','SUN']
    years = 2016
    mydate = date(years, a, b).weekday()
    answer = days[mydate]
    # Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a Wednesday.
    return answer

 

해설

더보기

import datetime 과 date를 기억하고 있어서 가능.
참조 : https://docs.python.org/3/library/datetime.html#datetime.date.weekday
date.weekday()
Return the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, date(2002, 12, 4).weekday() == 2, a Wednesday.
요일의 수를 정수로 반환하며 기준은 월요일은 0, 일요일은 6이다. 이를 이용하서 숫자에 따른 list를 만들고 거기에 반환 처리 함.

 


스킬 체크 테스트 Level.1 / 문제 1

def solution(num):
    answer = 0
    if num == 1:
        return 0 
    while True:
        if num % 2 == 0:
            num //= 2
            answer += 1
        else:
            num *= 3
            num += 1
            answer += 1
        if num == 1:
            return answer
        if answer >= 500:
            return -1
    return answer

 

해설

더보기

문제 그대로. 시작 전에 num 이 1이면 0 반환 하고 아니면 answer의 횟수가 500이 되면 return 을 시키거나 num이 1이 되면 answer를 반환 처리 함.

 


스킬 체크 테스트 Level.1 / 문제 2

def solution(s):
    answer = True
    if len(s) == 4 or len(s) == 6:
        if s.isdigit():
            answer = True
        else:
            answer = False
    else:
        answer = False
    return answer

 

해설

더보기

문제 그대로. 길이 먼저 4 혹은 6인지 if condition으로 처리 하고 나서 python의 메서드 중에서 isdigit()으로 체크

참조 : https://docs.python.org/3/library/stdtypes.html?highlight=isdigit#str.isdigit

str.isdigit()

Return True if all characters in the string are digits and there is at least one character, False otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

True의 조건 : 문자열이 숫자로 이루어져 있고 최소 1글자 이상일 경우, 그 이외에는 False 반환(return)한다. Digit이라고 인지하는 기준은 십진법의 0에서 9까지의 아라비아 숫자들과 호환 가능한 밑수나 승수 같은 것들을 포함한 숫자들을 포함하여 일컫는다.