[21/07/16 KATA NINJA ALGO] 회문문자열, 거리두기, 유효한 펠린드롬
2일차 (21/07/16)
등수구하기
끝
점수계산
끝
회문문자열
짝수와 홀수를 나누지말고 올림을 이용하여 두개를 같은 코드로 처리할 수 있게하자.
또 아래 포문 처럼 하나는 앞에서부터 , 다른 하나는 뒤에서부터 오며 검사하는 코드는 지양하고, js 내장 메서드를 이용해서 풀어보자
function solution(s) {
const string = s.toLowerCase();
const first = string.slice(0, string.length / 2);
// let second;
// if (string.length % 2 === 1) {
// second = string.slice(string.length / 2 + 1);
// }
// if (string.length % 2 === 0) {
// second = string.slice(string.length / 2);
// }
// for (let i = 0; i < first.length; i++) {
// if (first[i] !== second[second.length - 1 - i]) {
// return "NO";
// }
// }
return first === second.split("").reverse().join("");
}
문자열을 뒤집는 코드
string.split("").reverse().join("");
카카오 거리두기
function getMan(f, s) {
const row = Math.abs(f.row - s.row);
const column = Math.abs(f.column - s.column);
return row + column;
}
function getComb(array, sn) {
if (sn === 1) {
return array.map((i) => [i]);
}
const result = [];
array.forEach((item, index) => {
const target = item;
const tail = getComb(array.slice(index + 1), sn - 1);
tail.forEach((tailItem) => {
result.push([target, ...tailItem]);
});
});
return result;
}
function getTwoCheck(peoples, first, second) {
if (first.row === second.row) {
const index = (first.column + second.column) / 2;
return peoples[first.row][index] === "O";
}
if (first.column === second.column) {
const index = (first.row + second.row) / 2;
return peoples[index][first.column] === "O";
}
const data_1 = peoples[first.row][second.column];
const data_2 = peoples[second.row][first.column];
return data_1 === "O" || data_2 === "O";
}
function solution(places) {
var answer = [];
places = places.map((place) => place.map((string) => string.split("")));
places.forEach((place) => {
const peoples = [];
let result = 1;
place.forEach((daegisil, row) => {
daegisil.forEach((seat, column) => {
// 사람이 앉은 자리면 peoples 배열에 넣는다.
if (seat === "P") peoples.push({ row, column });
});
});
// 사람들의 자리를 비교해야하므로 조합을 구해놈.
const comb = getComb(
[...new Array(peoples.length)].map((_, index) => index),
2
);
// 사람들의 맨하튼 거리를 체크한다.
for (let check = 0; check < comb.length; check++) {
const [f, s] = comb[check];
const first = peoples[f];
const second = peoples[s];
const menhaton = getMan(first, second);
// 맨하튼이 1이면 거리두기를 지키지 않은 place이다.
if (menhaton === 1) {
result = 0;
break;
}
if (menhaton === 2) {
// 맨하튼이 2이면 거리두기를 지킨것인지 아닌지 체크해주어야함.
if (getTwoCheck(place, first, second)) {
result = 0;
break;
}
}
}
answer.push(result);
});
return answer;
}
유효한 펠린드롬
function solution(s) {
let answer = "YES";
console.log(s.match(/[a-z]/gi));
const onlyAlphabelString = s.toLowerCase().match(/[a-z]/gi);
//const only = s.toLowerCase().replace(/[^a-z]/g,'')
const first = onlyAlphabelString.slice(0, onlyAlphabelString.length / 2);
const second = onlyAlphabelString.slice(
Math.ceil(onlyAlphabelString.length / 2)
);
console.log(first.join("") === second.reverse().join("")); // false
return first.join("") === second.reverse().join(""); // true why?
// reverse는 원본배열을 바꾼다.
}
match(/[a-z]/)
알파벳인 문자들을 모두 배열화해줌
replace(/[^a-z]/g,'')
알파벳이 아닌 것들을 지운다
reverse()
는 원본배열 바꿈
숫자만 추출
function solution(string) {
return +string.match(/[0-9]/g).join("");
}
Author And Source
이 문제에 관하여([21/07/16 KATA NINJA ALGO] 회문문자열, 거리두기, 유효한 펠린드롬), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@rat8397/210716-KATA-NINJA-ALGO-등수구하기-점수계산-회문문자열-거리두기-유효한-펠린드롬저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)