지구정복

[문자열] 프로그래머스 - 문자열 다루기 기본(12918) 본문

데이터 엔지니어링 정복/Algorithm

[문자열] 프로그래머스 - 문자열 다루기 기본(12918)

eeaarrtthh 2023. 1. 1. 20:56
728x90
반응형
SMALL

-문제

https://school.programmers.co.kr/learn/courses/30/lessons/12918?language=python3 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

-풀이

먼저 문자열 s가 4 또는 6일 때 false return하게 했고

try catch문을 이용해서

문자열 s를 숫자로 바꿀 수 있으면 True

바꿀 수 없다면 False를 return하도록 했다.

자바와 파이썬 모두 동일하게 풀었다.

 

-자바

class Solution {
    public boolean solution(String s) {
        boolean answer = true;
        
        int len = s.length();
        if( !(len==4 || len==6) ) return false;
        try{
            int chkInt = Integer.parseInt(s);
        } catch(Exception e) {
            return false;
        }
        return answer;
    }
}

 

-파이썬

def solution(s):
    answer = True
    l = len(s)
    if l==4 or l==6:
        try:
            a = int(s)
        except:
            return False
    else:
        return False
    return answer

 

728x90
반응형
LIST
Comments