题解 | #简单密码#
简单密码
http://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
//总感觉这么列出来有点傻
let str = 'abcdefghijklmnopqrstuvwxyz';
let book = {
2: 'abc',
3: 'def',
4: 'ghi',
5: 'jkl',
6: 'mno',
7: 'pqrs',
8: 'tuv',
9: 'wxyz'
}
while(line = readline()) {
const arr = line.split('');
let newArr = arr.map((item) => {
if(/[A-Z]/.test(item)) {
let char = item.toLowerCase();
let index = str.indexOf(char);
return index != 25 ? str.charAt(index+1) : 'a';
} else if(/[a-z]/.test(item)) {
return getBook(item);
} else {
return item;
}
})
print(newArr.join(''));
}
function getBook(target) {
let reg = new RegExp(target, 'i');
let num;
for (let key in book) {
if (book[key].match(reg)) {
num = key;
break;
}
}
return num;
}