[프로그래머스] LV.0 제곱수 판별하기 / 파이썬(Python)

디딧·2022년 11월 11일

프로그래머스

목록 보기
7/36
post-thumbnail

문제 설명

어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다. 정수 n이 매개변수로 주어질 때, n이 제곱수라면 1을 아니라면 2를 return하도록 solution 함수를 완성해주세요.

풀이 과정

코드

def solution(n):
    tmp = n ** (1/2)
    if round(tmp,1)==tmp:
        answer = 1
    else:
        answer=2
    return answer

다른사람 풀이

def solution(n):
    return 1 if (n ** 0.5).is_integer() else 2


파이썬 정수 판별

is_integer() 함수

# float 객체가 유한한 정수이면 True, 그렇지 않으면 False 반환

# Syntax
float.is_integer()

# example
1.5.is_integer() # False
1.0.is_integer() # True
profile
M.S. in Statistics, 2022 - present

0개의 댓글