반응형
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
- TCP
- 소켓
- Node.js
- 알고리즘
- ip
- HTTP
- 백준
- 타입 챌린지
- 그래프
- Crawling
- dfs
- type challenge
- dp
- typescript
- javascript
- 자바스크립트
- 수학
- 문자열
- socket
- HTTP 완벽 가이드
- 가천대
- 쉬운 문제
- 레벨 1
- BFS
- 프로그래머스 레벨 2
- 크롤링
- 프로그래머스
- Algorithm
- 타입스크립트
- Nestjs
Archives
- Today
- Total
kakasoo
[node.js] 신규 아이디 추천 ( 프로그래머스 레벨1 ) 본문
반응형
// 프로그래머스 레벨1 신규 아이디 추천을 풀었습니다.
const firstRule = (str) => str.toLowerCase();
const secondRule = (str) =>
str
.split("")
.filter((el) => {
const charCode = el.charCodeAt();
const aCode = "a".charCodeAt();
const zCode = "z".charCodeAt();
const zeroCode = "0".charCodeAt();
const nineCode = "9".charCodeAt();
if (
(zeroCode <= charCode && charCode <= nineCode) ||
(aCode <= charCode && charCode <= zCode) ||
["-", "_", "."].includes(el)
) {
return true;
}
return false;
})
.join("");
const thirdRule = (str) => {
while (str.includes("..")) {
str = str.replace("..", ".");
}
return str;
};
const fourthRule = (str) =>
str
.split("")
.filter((el, i, arr) => {
if (el === "." && (i === 0 || i === arr.length - 1)) {
return false;
}
return true;
})
.join("");
const fifthRule = (str) => (str.length ? str : "a");
const sixthRule = (str) => {
const next = str.length >= 16 ? str.slice(0, 15) : str;
return next
.split("")
.filter((el, i, arr) => {
if (el === "." && (i === 0 || i === arr.length - 1)) {
return false;
}
return true;
})
.join("");
};
const seventhRule = (str) => {
if (str.length >= 3) {
return str;
}
const last = str[str.length - 1];
while (str.length < 3) {
str += last;
}
return str;
};
const solution = (new_id) => {
const first = firstRule(new_id);
const second = secondRule(first);
const third = thirdRule(second);
const fourth = fourthRule(third);
const fifth = fifthRule(fourth);
const sixth = sixthRule(fifth);
const seventh = seventhRule(sixth);
return seventh;
};
반응형
'프로그래밍 > 알고리즘 풀이' 카테고리의 다른 글
[node.js] 게임 맵 최단 거리 ( 프로그래머스 레벨2 ) (0) | 2021.06.30 |
---|---|
[node.js] 수식 최대화 ( 프로그래머스 레벨2 ) (0) | 2021.06.30 |
[node.js] 로또의 최고 순위와 최저 순위 ( 프로그래머스 레벨1 ) (0) | 2021.06.30 |
[node.js] 폰켓몬 ( 프로그래머스 레벨1 ) (0) | 2021.06.30 |
[node.js] 음양 더하기 ( 프로그래머스 레벨1 ) (0) | 2021.06.30 |