Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- C++
- java
- Python
- Get
- programmers
- redux
- SW
- 자바
- 항해플러스
- 테코테코
- axios
- createSlice
- json-server
- JavaScript
- 프로그래머스
- react-redux
- react
- redux-toolkit
- 매일메일
- maeil-mail
- 항해99
- sw expert academy
- react-router
- 알고리즘
- 이코테
- redux-saga
- useDispatch
- 리액트
- 코딩테스트합격자되기
- Algorithm
Archives
- Today
- Total
Binary Journey
[프로그래머스] 문자열 다루기 기본 본문
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
** Javascript
1)
맨 처음 제출했던 풀이
function solution(s) {
return (s.length === 4 || s.length === 6) && s.replace(/[0-9]/g, "") === "";
}
2)
replace 안 써도 된다.
function solution(s) {
return (s.length === 4 || s.length === 6) && !s.match(/[a-zA-Z]/g);
}
3)
test 사용
function solution(s) {
return (s.length === 4 || s.length === 6) && !/[a-z]/i.test(s)
}
주의: Number 나 parseInt 를 사용하려 했더니 Number("e") 와 parseInt("e") 의 경우 숫자로 변환해준다고 한다.
** Java
1)
import java.util.regex.Pattern;
class Solution {
public boolean solution(String s) {
return (s.length() == 4 || s.length() == 6) && Pattern.matches("^[0-9]*$", s);
}
}
2)
정규식으로 글자수 제한까지 매치하는 경우
import java.util.regex.Pattern;
class Solution {
public boolean solution(String s) {
return Pattern.matches("^[0-9]{4}|{6}$", s);
}
}
1등 추천 풀이는 try-catch 를 썼다
class Solution {
public boolean solution(String s) {
if(s.length() == 4 || s.length() == 6){
try{
int x = Integer.parseInt(s);
return true;
} catch(NumberFormatException e){
return false;
}
}
else return false;
}
}
** Python
import re
def solution(s):
return (len(s) == 4 or len(s) == 6) and s.isdigit()
반응형
'프로그래머스 > level 1' 카테고리의 다른 글
[프로그래머스] 내적 (0) | 2021.09.01 |
---|---|
[프로그래머스] 로또의 최고 순위와 최저 순위 (0) | 2021.09.01 |
[프로그래머스][위클리챌린지] 4주차 직업군 추천하기 (0) | 2021.08.30 |
[프로그래머스] 실패율 (0) | 2021.08.23 |
[프로그래머스] 소수 찾기 (0) | 2021.08.23 |