파이썬/문제

[코드트리] 함수를 이용한 369 (미해결)

낑깡H 2022. 9. 10. 22:54

🍒 문제 링크

https://www.codetree.ai/missions/5/problems/369-games-using-functions/description

 

🍒 문제 분석

 

 

🍒 코드

a, b = map(int,input().split())
cnt = 0

def letter(n):
    if n // 10 == 3 or n // 10 == 6 or n // 10 == 9 or n % 10 == 3 or n % 10 == 6 or n % 10 == 9:
        return True



def digit(n):
    if (n % 3 == 0) or letter(n):
        return True

for i in range (a, b+1, 1):
    if digit(i):
        cnt += 1 

print(cnt)



# letter:  3,6,9 들어가거나 
# 3의 배수거나 : n % 3 == 0

답은 맞았다고 뜨는데 코드는 에러 발생,, 왜지

🍓 내 해결 과정

 

 

🌽 다른 사람 코드

# 변수 선언 및 입력:
a, b = tuple(map(int, input().split()))


# 3, 6, 9 숫자가 
# 단 하나라도 포함되었는지를 확인합니다.
def contains_369(n):
    # 계속 10으로 나눠주며
    # 일의 자리를 조사합니다.
    while n > 0:
        if n % 10 == 3 or n % 10 == 6 or n % 10 == 9:
            return True

        n //= 10

    return False


# 3, 6, 9를 포함하거나 3의 배수인지를 판단합니다.
def is_369_number(n):
    return contains_369(n) or (n % 3 == 0)


cnt = 0
for i in range(a, b + 1):
    if is_369_number(i):
        cnt += 1

print(cnt)

 

어떨 때 True, False, 어떨 땐 그냥 바로 return (조건문)?

contains 저렇게 하면 십의 자리에 369 오는 경우 해결 안되지 않나,,

n//=10 용도?

 

 

 

🍉 깨달은 점 및 정리

함수 이름만 넣어서 큰 구조 짠뒤, 디테일하게 구조화하자!