Note that question becomes estion-quay, which is the correct translation according to Wikipedia article. In fact, the above words and translations are taken from the article.
The way the regex work is as follows:
First, all words are prefixed with w just in case it's needed
Then, skipping that w, look for either qu or a non-empty sequence of consonants. If neither can be found, then the actual word starts with a vowel, so grab the w using capturing lookbehind
Then just rearrange the components to get the translation
public String convert(String word)
{
int position = 0;
while(!isVowel(word.charAt(position)))
{
++position;
}
if(position == 0)
{
return word + "-way";
}
else if(word.charAt(0) == 'q')
{
++position;
}
return word.substring(position) + "-" + word.substring(0, position) + "ay";
}
public boolean isVowel(char character)
{
switch(character)
{
case 'a': case 'e': case 'i': case 'o': case 'u':
return true;
default:
return false;
}
}
首先代码会找到第一个元音的位置,然后跳出循环。这比使用 for 循环遍历每个字母并使用 break 更简单;跳出循环。其次,这将匹配维基百科网站上的所有测试用例。最后,由于 char 实际上是有限范围的 int,因此可以使用 switch 语句来提高性能和可读性。
I know this question is well over a year old, but I thought I would put my modification of it. There are several improvements in this code.
public String convert(String word)
{
int position = 0;
while(!isVowel(word.charAt(position)))
{
++position;
}
if(position == 0)
{
return word + "-way";
}
else if(word.charAt(0) == 'q')
{
++position;
}
return word.substring(position) + "-" + word.substring(0, position) + "ay";
}
public boolean isVowel(char character)
{
switch(character)
{
case 'a': case 'e': case 'i': case 'o': case 'u':
return true;
default:
return false;
}
}
First the code will find the position of the first vowel, and then jump out of the loop. This is simpler than using a for loop to iterate through each letter and using break; to jump out of the loop. Secondly, this will match all the testcases on the Wikipedia site. Lastly, since chars are actually a limited range int, a switch statement can be used to improve performance and readability.
IsVowel 可以重写 return c == 'a' || c == 'e' 等等。由于短路,比较次数方面的性能应该相似。
I'm years removed from Java, but overall, your code looks fine. If you wanted to be nitpicky, here are some comments:
Add comments. It doesn't have to follow the Javadoc specification, but at least explicitly describe the accepted argument and the expected return value and perhaps give some hint as to how it works (behaving differently depending on whether the first character is a vowel)
You might want to catch IndexOutOfBoundsException, which I think might happen if you pass it a zero length string.
Method names should be lower case.
IsVowel can be rewritten return c == 'a' || c == 'e' and so on. Due to short-circuiting, the performance in terms of number of comparisons should be similar.
发布评论
评论(7)
我将重写
isVowel(char ch)
如下:我将编写以下内容:
我还将重命名方法名称以遵循编码约定。
当然,对于我来说,我也会使用正则表达式而不是
substring
和for
循环。参考文献
I'd rewrite
isVowel(char ch)
as follows:And I'd write the following instead:
I'd also rename method names to follow coding convention.
And of course, being me, I'd also use regex instead of
substring
andfor
loop.References
免责声明:我不懂 Java。
反向逻辑令人困惑,请这样编写您的
if
语句:您甚至可以删除
else
。IsVowel
可能需要是私有的。它还可以使用单个||
链或"".indexOf
(或 Java 中的任何内容)进行重写。您的
for
逻辑可以简化为一个简短的while
:Disclaimer: I don't know Java.
Inverted logic is confusing please write your
if
statement as such:You can even drop the
else
.IsVowel
may need to be private. It can also be rewritten using a single||
chain, or as a"".indexOf
(or whatever it is in Java).Your
for
logic can be simplified int a shortwhile
:这是一个完整的重写,如果您知道如何阅读正则表达式,则可以使代码更具可读性:
此打印 (as see on ideone.com< /a>):
请注意,
question
变为estion-quay
,根据 维基百科文章。事实上,以上文字和翻译均摘自文章。正则表达式的工作方式如下:
w
为前缀,以防需要。w
,查找qu< /code> 或非空辅音序列。如果两者都找不到,则实际单词以元音开头,因此使用捕获后行捕获
w
即:
References
[…]
, < a href="http://www.regular-expressions.info/alternation.html" rel="nofollow noreferrer">替换:|
, 重复:+
,*
, Lookaround:(?<=…)
和 捕获:(…)
Here's a complete rewrite that makes the code more readable if you know how to read regex:
This prints (as seen on ideone.com):
Note that
question
becomesestion-quay
, which is the correct translation according to Wikipedia article. In fact, the above words and translations are taken from the article.The way the regex work is as follows:
w
just in case it's neededw
, look for eitherqu
or a non-empty sequence of consonants. If neither can be found, then the actual word starts with a vowel, so grab thew
using capturing lookbehindThat is:
References
[…]
, Alternation:|
, Repetition:+
,*
, Lookaround:(?<=…)
, and Capturing:(…)
我知道这个问题已经存在一年多了,但我想我应该对其进行修改。这段代码有几处改进。
首先代码会找到第一个元音的位置,然后跳出循环。这比使用 for 循环遍历每个字母并使用 break 更简单;跳出循环。其次,这将匹配维基百科网站上的所有测试用例。最后,由于 char 实际上是有限范围的 int,因此可以使用 switch 语句来提高性能和可读性。
I know this question is well over a year old, but I thought I would put my modification of it. There are several improvements in this code.
First the code will find the position of the first vowel, and then jump out of the loop. This is simpler than using a for loop to iterate through each letter and using break; to jump out of the loop. Secondly, this will match all the testcases on the Wikipedia site. Lastly, since chars are actually a limited range int, a switch statement can be used to improve performance and readability.
严格来说这并不是一种改进,但 Java 约定规定方法应以小写字母开头。
Not strictly an improvement as such, but Java convention dictates that methods should start with a lowercase letter.
我已经离开 Java 好几年了,但总的来说,你的代码看起来不错。如果您想挑剔,这里有一些评论:
IsVowel
可以重写return c == 'a' || c == 'e'
等等。由于短路,比较次数方面的性能应该相似。I'm years removed from Java, but overall, your code looks fine. If you wanted to be nitpicky, here are some comments:
IndexOutOfBoundsException
, which I think might happen if you pass it a zero length string.IsVowel
can be rewrittenreturn c == 'a' || c == 'e'
and so on. Due to short-circuiting, the performance in terms of number of comparisons should be similar.这是作业吗?如果是这样,请将其标记为这样。
Is this homework? If so, tag it as such.