最高得分单词算法出现古怪错误。

huangapple go评论74阅读模式
英文:

Highest Scoring Word algorithm throwing quirky error

问题

以下是翻译好的部分:

我在 CodeWars 上参加一个挑战,遇到了一个奇怪的错误。这是问题陈述:

给定一个由单词组成的字符串,你需要找到得分最高的单词。每个单词中的每个字母根据其在字母表中的位置得分:a = 1,b = 2,c = 3,依此类推。你需要返回得分最高的单词作为字符串。如果有两个单词得分相同,则返回原始字符串中出现最早的单词。所有字母都将是小写,所有输入都将是有效的。

这是我编写的算法,但在某些情况下不起作用:

public static String high(String s) {
    String[] words = s.split(" ");
    int[] scores = new int[words.length];
    
    for (int j = 0; j < words.length; j++) {
        for (int i = 0; i < words[j].length(); i++) {
            scores[j] += (int) words[j].charAt(i);
        }
    }
    
    int highestWordIndex = 0;
    for (int i = 1; i < words.length; i++) {
        if (scores[i] > scores[highestWordIndex]) highestWordIndex = i;
    }
    
    return words[highestWordIndex];
}

然而,当我在嵌套的循环中添加了 - 96 后,它就起作用了。这是带有那段代码的版本:

public static String high(String s) {
    String[] words = s.split(" ");
    int[] scores = new int[words.length];
    
    for (int j = 0; j < words.length; j++) {
        for (int i = 0; i < words[j].length(); i++) {
            scores[j] += (int) words[j].charAt(i) - 96;
        }
    }
    
    int highestWordIndex = 0;
    for (int i = 1; i < words.length; i++) {
        if (scores[i] > scores[highestWordIndex]) highestWordIndex = i;
    }
    
    return words[highestWordIndex];
}

你知道为什么加上 - 96 会产生差异吗?

英文:

I'm doing a challenge on CodeWars and I'm getting a quirky error. This is the problem statement:

> Given a string of words, you need to find the highest scoring word. Each letter of a word scores points according to its position in the alphabet: a = 1, b = 2, c = 3 etc. You need to return the highest scoring word as a string. If two words score the same, return the word that appears earliest in the original string. All letters will be lowercase and all inputs will be valid.

This is the algorithm I wrote which does not work in some cases:

public static String high(String s) {
    String[] words = s.split(&quot; &quot;);
    int[] scores = new int[words.length];
    
    for (int j = 0; j &lt; words.length; j++) {
    	for (int i = 0; i &lt; words[j].length(); i++) {
    		scores[j] += (int) words[j].charAt(i);
    	}
    }
    
    int highestWordIndex = 0;
    for (int i = 1; i &lt; words.length; i++) {
    	if (scores[i] &gt; scores[highestWordIndex]) highestWordIndex = i;
    }
    
    return words[highestWordIndex];
}

However, when I add a "- 96" inside the nested for-loop it works. Here's that code:

public static String high(String s) {
    String[] words = s.split(&quot; &quot;);
    int[] scores = new int[words.length];
    
    for (int j = 0; j &lt; words.length; j++) {
    	for (int i = 0; i &lt; words[j].length(); i++) {
    		scores[j] += (int) words[j].charAt(i) - 96;
    	}
    }
    
    int highestWordIndex = 0;
    for (int i = 1; i &lt; words.length; i++) {
    	if (scores[i] &gt; scores[highestWordIndex]) highestWordIndex = i;
    }
    
    return words[highestWordIndex];
}

Any idea why that's making a difference?

答案1

得分: 1

'a' 的十六进制为 0x61,或十进制为 97。因此,当你减去 96 时,你使用了正确的计算。我会选择以下之一:

score += charAt() - 0x60;

或者

int offset = ('a' - 1);
...
score += charAt() - offset;

对于长度相等的单词,这不会有任何区别,但是当单词长度不同时,来自多余 96 的额外分数会使结果偏向更长的单词。

英文:

'a' is hex 0x61, or decimal 97. So when you -96, you are using the correct calculation. I would have done one of:

score += charAt() - 0x60;

OR

int offset = (&#39;a&#39; - 1);
...
score += charAt() - offset;

For words of equal length, this won't make a difference, but when words are of different length, the additional points from the excess 96 will skew results towards longer words.

答案2

得分: 0

它之所以在你减去96时有效,是因为ASCII中小写字符的十进制值从97开始。例如,ASCII中的"a"是97,所以97 - 96 = 1,正如问题所要求的那样。

英文:

The reason it works when you subtract 96 is because the decimal value of lowercase characters in ASCII starts at 97. For example, "a" in ASCII is 97, so 97 - 96 = 1, as the question required.

ASCII Chart for reference

答案3

得分: 0

"The (int) char version of 'a' is 97, 'b' is 98, 'c' is 99 and so on. The question asks you to cast these to 1,2,3,... for good reason. Consider the word cab vs the word ox.

'cab' should be 3 + 1 + 2 = 6. However, in your implementation it's 99+97+98=294.

'ox' should be 15 + 24 = 39. However, in your implementation it's 111 + 120 = 231.

'ox' SHOULD score higher than 'cab', but it doesn't because you cast your chars to their ASCII representative integers, rather than 1-26 as the question asks. As a result of this, your algorithm gives many more points to longer words, as each extra character gets an extra 96 points above what it should be getting according to the question. Subtracting 96 brings your score domain down from 97-122 to 1-26, which solves this problem for you.

Hope this helped :)"

(Note: I've provided the translated content as requested. If you have any further instructions or questions, feel free to let me know.)

英文:

The (int) char version of 'a' is 97, 'b' is 98, 'c' is 99 and so on. The question asks you to cast these to 1,2,3,... for good reason.
consider the word cab vs the word ox.

"cab" should be 3 + 1 + 2 = 6. However in your implementation it's 99+97+98=294

"ox" should be 15 + 24 = 39. However in your implementation it's 111 + 120 = 231

"ox" SHOULD score higher than "cab", but it doesn't because you cast your chars to their ascii representative integers, rather than 1-26 like the question asks. As a result of this, your algorithm gives many more points to longer words, as each extra character gets an extra 96 points above what it should be getting according to the question. Subtracting 96 casts your score domain down from 97-122 to 1-26, which solves this problem for you.

Hope this helped 最高得分单词算法出现古怪错误。

huangapple
  • 本文由 发表于 2020年8月18日 23:24:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63471789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定