암호해독!

이종혁·2020년 6월 11일
0

알고리즘

목록 보기
1/4

문제1 : 암호해독

'   + -- + - + -   '
'   + --- + - +   '
'   + -- + - + -   '
'   + - + - + - +   '

해(1)와 달(0), Code의 세상안으로! (En-Coding).
출력조건 : 문자열

let result = '';
let arr = 
['   + -- + - + -   ', 
'   + --- + - +   ', 
'   + -- + - + -   ', 
'   + - + - + - +   '];

for(let elements of arr) {
    let binary = elements.replace(/ /g, '').replace(/\+/g, '1').replace(/-/g, '0');
    let decimal = parseInt(binary, 2);
    result += String.fromCharCode(decimal);
}
  1. 문자열에 있는 모든 빈 공간들을 없엔다.
  2. '+'는 '1'로 '-'는 '0'으로 변환한다.
  3. 2진법인 문자열을 10진법인 정수로 변환한다.
  4. 'UTF-16 Code Unit'을 문자로 변환한다.
    • 문제핵심 : 암호 -> 2진법 -> 10진법 -> 문자

사용된 메소드 :
replace() : 특정 패턴에 일치하는 문자열을 교체. 정규 표현식 사용하여 전역으로 교체하기.

parseInt(string, radix) : 문자열을 parse해서 정수로 변환.
radix: indicates that the number in the string should be parsed from a specified radix to a decimal number.

String.fromCharCode() :
returns a string created from 'UTF-16 Code Unit'
ex) String.fromCharCode(65, 66, 67) returns "ABC"

출처 : https://www.inflearn.com/course/코딩-테스트-전날

profile
Junior Developer

0개의 댓글