2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: aa7697ea2477d1316795783b
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 儿童黑话
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 5
|
2020-09-07 16:10:29 +08:00
|
|
|
|
forumTopicId: 16039
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: pig-latin
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
[儿童黑话](http://en.wikipedia.org/wiki/Pig_Latin),也叫 Pig Latin,是一种英语语言游戏。规则如下:
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
\- 如果单词以辅音开头,就把第一个辅音字母或第一组辅音簇移到单词的结尾,并在后面加上 "ay"。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
\- 如果单词以元音开头,只需要在结尾加上 "way"。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
在英语中,字母 a、e、i、o、u 为元音,其余的字母均为辅音。辅音簇的意思是连续的多个辅音字母。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
请把传入的字符串根据上述规则翻译成儿童黑话并返回结果。输入的字符串一定是一个小写的英文单词。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
`translatePigLatin("california")` 应返回 "aliforniacay"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert.deepEqual(translatePigLatin('california'), 'aliforniacay');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
`translatePigLatin("paragraphs")` 应返回 "aragraphspay"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.deepEqual(translatePigLatin('paragraphs'), 'aragraphspay');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
`translatePigLatin("glove")` 应返回 "oveglay"。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.deepEqual(translatePigLatin('glove'), 'oveglay');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
`translatePigLatin("algorithm")` 应返回 "algorithmway"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert.deepEqual(translatePigLatin('algorithm'), 'algorithmway');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
`translatePigLatin("eight")` 应返回 "eightway"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert.deepEqual(translatePigLatin('eight'), 'eightway');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
Should handle words where the first vowel comes in the middle of the word. `translatePigLatin("schwartz")` 应返回 "artzschway"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert.deepEqual(translatePigLatin('schwartz'), 'artzschway');
|
|
|
|
|
```
|
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
|
应可以处理不含元音的单词,`translatePigLatin("rhythm")` 应返回 "rhythmay"。
|
2020-09-07 16:10:29 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.deepEqual(translatePigLatin('rhythm'), 'rhythmay');
|
2020-09-07 16:10:29 +08:00
|
|
|
|
```
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function translatePigLatin(str) {
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
translatePigLatin("consonant");
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
function translatePigLatin(str) {
|
|
|
|
|
if (isVowel(str.charAt(0))) return str + "way";
|
|
|
|
|
var front = [];
|
|
|
|
|
str = str.split('');
|
|
|
|
|
while (str.length && !isVowel(str[0])) {
|
|
|
|
|
front.push(str.shift());
|
|
|
|
|
}
|
|
|
|
|
return [].concat(str, front).join('') + 'ay';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isVowel(c) {
|
|
|
|
|
return ['a', 'e', 'i', 'o', 'u'].indexOf(c.toLowerCase()) !== -1;
|
|
|
|
|
}
|
|
|
|
|
```
|