Skip to content

Instantly share code, notes, and snippets.

@Titiaiev
Last active January 5, 2019 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Titiaiev/b07bafd225972c63e3b7f62bddbedd9c to your computer and use it in GitHub Desktop.
Save Titiaiev/b07bafd225972c63e3b7f62bddbedd9c to your computer and use it in GitHub Desktop.
Шифр Цезаря. Перебирает варианты сдвига в строке и возвращает массив вариантов строк.
// для теста
// const str = 'ПГПГ ПЮОГ УГПЦ';
// расшифровывается в "мама мыла раму"
const caesarCipher = function caesarCipher(string) {
const codesMap = {
а: 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,
ы: 29,
ь: 30,
э: 31,
ю: 32,
я: 33,
};
const charsMap = [
null,
'а',
'б',
'в',
'г',
'д',
'е',
'ё',
'ж',
'з',
'и',
'й',
'к',
'л',
'м',
'н',
'о',
'п',
'р',
'с',
'т',
'у',
'ф',
'х',
'ц',
'ч',
'ш',
'щ',
'ъ',
'ы',
'ь',
'э',
'ю',
'я',
];
let _string = string.toLocaleLowerCase();
const results = [];
let iterations = charsMap.length - 1;
while (iterations > 0) {
const chars = _string.split('');
// eslint-disable-next-line no-loop-func
const nextChars = chars.map((char) => {
if (codesMap[char]) {
let position = codesMap[char] + 1;
if (position > charsMap.length - 1) position = 1;
return charsMap[position];
}
return char;
});
_string = nextChars.join('');
results.push(_string);
iterations -= 1;
}
// console.log(results);
return results;
};
export default caesarCipher;
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="module" src="cezar.js"></script>
</body>
</html>
// eslint-disable-next-line import/extensions
import caesarCipher from './Caesar-cipher.js';
const str = 'ПГПГ ПЮОГ УГПЦ';
console.log(caesarCipher(str));
// ниже второй вариант
const results = [];
let iter = 32;
function convertToCode(str) {
let cursor = 0;
const charCodes = [];
while (cursor < str.length) {
charCodes.push(str.charCodeAt(cursor));
cursor += 1;
}
return charCodes;
}
function convertToWord(codes) {
if (!codes) return null;
const chars = [];
codes.forEach((code) => {
if (code) {
chars.push(String.fromCharCode(code));
}
});
return chars.join('');
}
function splitString(str = '') {
return str.toLocaleLowerCase().split(' ');
}
const splited = splitString(str);
let convertedAndSplited = [];
splited.forEach((word) => {
convertedAndSplited.push(convertToCode(word));
});
while (iter > 0) {
let temp = [];
convertedAndSplited.forEach((item) => {
// console.log(convertToWord(item));
const _item = item.map((i) => {
if (i < 1103) return i += 1;
i = 1072;
return i;
});
temp.push(_item);
});
let _temp = [];
temp.forEach((code) => {
_temp.push(convertToWord(code));
});
results.push(_temp.join(' '));
convertedAndSplited = temp;
temp = [];
_temp = [];
iter -= 1;
}
// console.log(results);
// а - 1072
// я - 1103
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment