見習村26 - Tongues

26 - Tongues

Don’t say so much, just coding…

Instruction

Gandalf’s writings have long been available for study, but no one has yet figured out what language they are written in. Recently, due to programming work by a hacker known only by the code name ROT13, it has been discovered that Gandalf used nothing but a simple letter substitution scheme, and further, that it is its own inverse|the same operation scrambles the message as unscrambles it.

This operation is performed by replacing vowels in the sequence 'a' 'i' 'y' 'e' 'o' 'u' with the vowel three advanced, cyclicly, while preserving case (i.e., lower or upper).

Similarly, consonants are replaced from the sequence 'b' 'k' 'x' 'z' 'n' 'h' 'd' 'c' 'w' 'g' 'p' 'v' 'j' 'q' 't' 's' 'r' 'l' 'm' 'f' by advancing ten letters.

So for instance the phrase 'One ring to rule them all.' translates to 'Ita dotf ni dyca nsaw ecc.'

The fascinating thing about this transformation is that the resulting language yields pronounceable words. For this problem, you will write code to translate Gandalf’s manuscripts into plain text.

Your job is to write a function that decodes Gandalf’s writings.

Input

The function will be passed a string for the function to decode. Each string will contain up to 100 characters, representing some text written by Gandalf. All characters will be plain ASCII, in the range space (32) to tilde (126).

Output

For each string passed to the decode function return its translation.

Ruby

Init

1
2
3
def tongues(code)
#your code here
end

Sample Testing

1
2
3
4
5
Test.assert_equals(tongues('Ita dotf ni dyca nsaw ecc.'), 'One ring to rule them all.')
Test.assert_equals(tongues('Tim oh nsa nowa gid ecc fiir wat ni liwa ni nsa eor ig nsaod liytndu.'), 'Now is the time for all good men to come to the aid of their country.')
Test.assert_equals(tongues('Giydhlida etr hakat uaedh efi iyd gidagensadh pdiyfsn ytni nsoh'), 'Fourscore and seven years ago our forefathers brought unto this')
Test.assert_equals(tongues('litnotatn e tam tenoit.'), 'continent a new nation.')
Test.assert_equals(tongues('Nsa zyolv pdimt gij xywbar ikad nsa cequ rifh.'), 'The quick brown fox jumped over the lazy dogs.')

Javascript

Init

1
2
3
function tongues(code) {

}

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
describe("Tongues", () => {
it("Basic tests", () => {
assert.strictEqual(tongues('Ita dotf ni dyca nsaw ecc.'), 'One ring to rule them all.');
assert.strictEqual(tongues('Tim oh nsa nowa gid ecc fiir wat ni liwa ni nsa eor ig nsaod liytndu.'), 'Now is the time for all good men to come to the aid of their country.');
assert.strictEqual(tongues('Giydhlida etr hakat uaedh efi iyd gidagensadh pdiyfsn ytni nsoh'), 'Fourscore and seven years ago our forefathers brought unto this');
assert.strictEqual(tongues('litnotatn e tam tenoit.'), 'continent a new nation.');
assert.strictEqual(tongues('Nsa zyolv pdimt gij xywbar ikad nsa cequ rifh.'), 'The quick brown fox jumped over the lazy dogs.');
assert.strictEqual(tongues('Tywpadh (1234567890) etr bytlnyenoit, nsau hsiycr pins pa ytlsetfar!'), 'Numbers (1234567890) and punctuation, they should both be unchanged!');
assert.strictEqual(tongues(' '), ' ');
assert.strictEqual(tongues('Nsoh oh tin Vcotfit pyn on liycr pa e roggadatn gidaoft cetfyefa.'), 'This is not Klingon but it could be a different foreign language.');
assert.strictEqual(tongues('0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'), '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789');
assert.strictEqual(tongues('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee');
assert.strictEqual(tongues('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm'), 'wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww');
assert.strictEqual(tongues('z'), 'q');
assert.strictEqual(tongues('n'), 't');
assert.strictEqual(tongues('****************************************************************************************************'), '****************************************************************************************************');
assert.strictEqual(tongues('q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1q1'), 'z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1z1');
});
});

Thinking

想法(1): 大小寫得轉換字,沒想到有 tr 這種方法,真的平常都不會有機會用到 ·_·

https://ithelp.ithome.com.tw/upload/images/20201011/2012082662JAenI2IR.jpg
圖片來源:Unsplash Thought Catalog

Hint & Reference

Solution

Ruby

1
2
3
4
5
6
7
8
9
10
11
12
# Solution 1
def tongues(code)
code = code.tr('aeiouy','eaoiyu')
code = code.tr('AEIOUY','EAOIYU')
code = code.tr('bkxznhdcwgpvjqtsrlmf','pvjqtsrlmfbkxznhdcwg')
code = code.tr('BKXZNHDCWGPVJQTSRLMF','PVJQTSRLMFBKXZNHDCWG')
end

# Solution 2
def tongues(code)
code.tr('eouaiypvjqtsrlmfbkxznhdcwg', 'aiyeoubkxznhdcwgpvjqtsrlmf').tr('eouaiypvjqtsrlmfbkxznhdcwg'.upcase, 'aiyeoubkxznhdcwgpvjqtsrlmf'.upcase)
end

Javascript

1
2
3
4
5
6
7
8
// Solution 1
function tongues(code) {
var alpha = 'aiyeoubkxznhdcwgpvjqtsrlmf', repl = 'eouaiypvjqtsrlmfbkxznhdcwg';
return code.replace(/[a-z]/gi, function(m) {
var lower = m.toLowerCase();
return lower == m ? repl[alpha.indexOf(lower)] : repl[alpha.indexOf(lower)].toUpperCase();
});
}