algorithm codingame chuck-norris

Maliethy·2021년 4월 28일
0

algorithm

목록 보기
1/5

0. 문제

https://www.codingame.com/ide/puzzle/chuck-norris

1. 문제해결에 필요한 개념

인코딩(Encoding) = 문자를 임의의 코드로 변환하는 것
유니코드(Unicode) = 숫자와 글자, 즉 키와 값이 1:1로 매핑된 형태의 코드
https://norux.me/31

2. 문제해결 코드

function textToBin(text) {
  const length = text.length;
  let output = '';
  for (let i = 0; i < length; i++) {
    let bin = text.charCodeAt(i).toString(2);// ascii코드를 2진법으로 바꿔주기

    if (bin.length < 7) {
      bin = '0' + bin;
    }
    output += bin;
  }
  return output;
}
const encode = {
  0: '00',
  1: '0',
};
// const binaryText = textToBin('CC');
const binaryText = textToBin('%');
let currentSign = binaryText[0];
let output = encode[currentSign] + ' ' + '0';
for (let i = 1; i < binaryText.length; i++) {
  let nextBit = binaryText[i];
  output += nextBit !== currentSign ? ' ' + encode[nextBit] + ' ' + '0' : '0';
  currentSign = nextBit;
}
console.log(output);

참고도서:
게임으로 익히는 코딩 알고리즘
참고코드:
https://gist.github.com/dukeDanjou/73600a15e4977ef48c8d

profile
바꿀 수 있는 것에 주목하자

0개의 댓글