► JS Algorithm/Programmers
[Programmers] JavaScript 알고리즘 | Lv.0 왼쪽 오른쪽
다람트리
2024. 8. 15. 17:04
반응형
🔒 문제 설명
문자열 리스트 str_list에는 "u", "d", "l", "r" 네 개의 문자열이 여러 개 저장되어 있습니다. str_list에서 "l"과 "r" 중 먼저 나오는 문자열이 "l"이라면 해당 문자열을 기준으로 왼쪽에 있는 문자열들을 순서대로 담은 리스트를, 먼저 나오는 문자열이 "r"이라면 해당 문자열을 기준으로 오른쪽에 있는 문자열들을 순서대로 담은 리스트를 return하도록 solution 함수를 완성해주세요. "l"이나 "r"이 없다면 빈 리스트를 return합니다.
🔒 제한사항
- 1 ≤ str_list의 길이 ≤ 20
- str_list는 "u", "d", "l", "r" 네 개의 문자열로 이루어져 있습니다.
🔒 입출력 예
str_list | result |
["u", "u", "l", "r"] | ["u", "u"] |
["l"] | [] |
🔒 입출력 예 설명
입출력 예 #1
- "r"보다 "l"이 먼저 나왔기 때문에 "l"의 왼쪽에 있는 문자열들을 담은 리스트인 ["u", "u"]를 return합니다.
입출력 예 #2
- "l"의 왼쪽에 문자열이 없기 때문에 빈 리스트를 return합니다.
🔐 solution of mine
Array.prototype.indexOf()
Array.prototype.splice()
function solution(str_list) {
const lIdx = str_list.indexOf("l");
const rIdx = str_list.indexOf("r");
if ((-1 < lIdx && lIdx < rIdx) || (-1 === rIdx && -1 < lIdx)) {
str_list.splice(lIdx);
} else if ((-1 < rIdx && rIdx < lIdx) || (-1 === lIdx && -1 < rIdx)) {
str_list.splice(0, rIdx + 1);
} else {
str_list.splice(0);
}
return console.log(str_list);
}
solution(["u", "u", "l", "r"]); // expected output: ["u", "u"]
solution(["u", "u", "r", "l", "u", "u"]); // expected output: ["l", "u", "u"]
solution(["d", "r", "u", "d", "d"]); // expected output: ["u", "d", "d"]
solution(["d", "l", "u", "d", "d"]); // expected output: ["d"]
solution(["l"]); // expected output: []
solution(["d", "d"]); // expected output: []
🔐 GPT's solution
Array.prototype.indexOf()
Array.prototype.slice()
function solution(str_list) {
// "l"과 "r"의 위치를 찾습니다.
const indexL = str_list.indexOf("l");
const indexR = str_list.indexOf("r");
// "l"과 "r" 둘 다 없을 경우 빈 리스트를 반환
if (indexL === -1 && indexR === -1) {
return console.log([]);
}
// "l"이 먼저 나왔을 경우
if (indexL !== -1 && (indexR === -1 || indexL < indexR)) {
return console.log(str_list.slice(0, indexL));
}
// "r"이 먼저 나왔을 경우
if (indexR !== -1 && (indexL === -1 || indexR < indexL)) {
return console.log(str_list.slice(indexR + 1));
}
// 기본적으로 빈 리스트를 반환 (사실 이 부분은 도달하지 않음)
return console.log([]);
}
solution(["u", "u", "l", "r"]); // expected output: ["u", "u"]
solution(["u", "u", "r", "l", "u", "u"]); // expected output: ["l", "u", "u"]
solution(["d", "r", "u", "d", "d"]); // expected output: ["u", "d", "d"]
solution(["d", "l", "u", "d", "d"]); // expected output: ["d"]
solution(["l"]); // expected output: []
solution(["d", "d"]); // expected output: []
🔐 solution of others
Array.prototype.slice()
function solution(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === "l") {
return console.log(arr.slice(0, i));
}
if (arr[i] === "r") {
return console.log(arr.slice(i + 1));
}
}
return console.log([]);
}
solution(["u", "u", "l", "r"]); // expected output: ["u", "u"]
solution(["u", "u", "r", "l", "u", "u"]); // expected output: ["l", "u", "u"]
solution(["d", "r", "u", "d", "d"]); // expected output: ["u", "d", "d"]
solution(["d", "l", "u", "d", "d"]); // expected output: ["d"]
solution(["l"]); // expected output: []
solution(["d", "d"]); // expected output: []
🔐 solution of others
Array.prototype.findIndex()
RegExp.prototype.test()
Array.prototype.slice()
function solution(arr) {
const i = arr.findIndex((str) => /r|l/.test(str));
if (i === -1) return console.log([]);
return console.log(arr[i] === "l" ? arr.slice(0, i) : arr.slice(i + 1));
}
solution(["u", "u", "l", "r"]); // expected output: ["u", "u"]
solution(["u", "u", "r", "l", "u", "u"]); // expected output: ["l", "u", "u"]
solution(["d", "r", "u", "d", "d"]); // expected output: ["u", "d", "d"]
solution(["d", "l", "u", "d", "d"]); // expected output: ["d"]
solution(["l"]); // expected output: []
solution(["d", "d"]); // expected output: []
🔐 solution of others
Array.prototype.findIndex()
Array.prototype.slice()
function solution(str_list) {
const idx = str_list.findIndex((el) => el === "l" || el === "r");
return console.log(
str_list[idx] === "l"
? str_list.slice(0, idx)
: str_list[idx] === "r"
? str_list.slice(idx + 1)
: []
);
}
반응형