Scrabble分数使用流处理

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

Scrabble Scores with Streams

问题

以下是您提供的代码的翻译:

我是一个初学者试图解决我的一个项目中的一些流语法问题我目前遇到的问题是
1. 所有单词的平均分数
2. 高于平均值的单词
3. 低于平均值的单词

我应该如何将我的单词值映射为一个可以获取所有单词平均值并显示这些平均值的 double我应该如何使用 partitioningBy 方法将值分割为高于和低于平均值的值请记住这必须通过流来完成

以下是我目前的代码

import java.util.Arrays;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

// 类中的方法

public static int scrapleVal(String word) {
    Map<Character, Integer> letterValues = new HashMap<>();
    letterValues.put('a', 1);
    letterValues.put('b', 3);
    letterValues.put('c', 3);
    letterValues.put('d', 2);
    letterValues.put('e', 1);
    letterValues.put('f', 4);
    letterValues.put('g', 2);
    letterValues.put('h', 4);
    letterValues.put('i', 1);
    letterValues.put('j', 8);
    letterValues.put('k', 5);
    letterValues.put('l', 1);
    letterValues.put('m', 3);
    letterValues.put('n', 1);
    letterValues.put('o', 1);
    letterValues.put('p', 3);
    letterValues.put('q', 10);
    letterValues.put('r', 1);
    letterValues.put('s', 1);
    letterValues.put('t', 1);
    letterValues.put('u', 1);
    letterValues.put('v', 8);
    letterValues.put('w', 4);
    letterValues.put('x', 8);
    letterValues.put('y', 4);
    letterValues.put('z', 10);

    return word.toLowerCase().chars().map(e -> letterValues.get((char) e)).sum();
}

public static void main(String[] args) {

    String[] words = {"Java", "program", "list", "string", "unix",
            "hours", "syntax", "error"};
    System.out.println("前三个单词是:");
    Stream.of(words).sorted((e1, e2) -> scrapleVal(e2) - scrapleVal(e1)).limit(3).forEach
            (e -> System.out.println(e + " : " + scrapleVal(e)));

    System.out.println("\n单词的平均值是:");
    Stream.of(words).sorted((e1, e2) -> scrapleVal(e2) - scrapleVal(e1)).forEach
            (e -> System.out.println(scrapleVal(e).average()));
}
英文:

I'm a beginner here trying to work out some stream syntax for one of my projects. I am currently having issues trying to find:

  1. the average score of all the words
  2. those word above average
  3. those words below average

How would I map my word value into a double that I can grab the average of for all of the words and display those averages? And how would I split the values to above and below average using the partitioningBy method? Keeping in mind that this has to be done via Streams.

Here is my code so far:

import java.util.Arrays;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
// methods within a class
public static int scrapleVal(String word) {
Map&lt;Character, Integer&gt; letterValues = new HashMap&lt;&gt;();
letterValues.put(&#39;a&#39;, 1);
letterValues.put(&#39;b&#39;, 3);
letterValues.put(&#39;c&#39;, 3);
letterValues.put(&#39;d&#39;, 2);
letterValues.put(&#39;e&#39;, 1);
letterValues.put(&#39;f&#39;, 4);
letterValues.put(&#39;g&#39;, 2);
letterValues.put(&#39;h&#39;, 4);
letterValues.put(&#39;i&#39;, 1);
letterValues.put(&#39;j&#39;, 8);
letterValues.put(&#39;k&#39;, 5);
letterValues.put(&#39;l&#39;, 1);
letterValues.put(&#39;m&#39;, 3);
letterValues.put(&#39;n&#39;, 1);
letterValues.put(&#39;o&#39;, 1);
letterValues.put(&#39;p&#39;, 3);
letterValues.put(&#39;q&#39;, 10);
letterValues.put(&#39;r&#39;, 1);
letterValues.put(&#39;s&#39;, 1);
letterValues.put(&#39;t&#39;, 1);
letterValues.put(&#39;u&#39;, 1);
letterValues.put(&#39;v&#39;, 8);
letterValues.put(&#39;w&#39;, 4);
letterValues.put(&#39;x&#39;, 8);
letterValues.put(&#39;y&#39;, 4);
letterValues.put(&#39;z&#39;, 10);
return word.toLowerCase().chars().map(e -&gt; letterValues.get((char)e)).sum();
}
public static void main(String[] args) {
String [] words = {&quot;Java&quot;, &quot;program&quot;, &quot;list&quot;, &quot;string&quot;, &quot;unix&quot;,
&quot;hours&quot;, &quot;syntax&quot;, &quot;error&quot;};
System.out.println(&quot;Top three words are: &quot;);
Stream.of(words).sorted((e1,e2)-&gt;scrapleVal(e2)-scrapleVal(e1)).limit(3).forEach
(e-&gt;System.out.println(e+&quot; : &quot;+scrapleVal(e)));
System.out.println(&quot;\nAverage value fo words is: &quot;);
Stream.of(words).sorted((e1,e2)-&gt;scrapleVal(e2)-scrapleVal(e1)).forEach
(e-&gt;System.out.println(scrapleVal(e).average()));
}

答案1

得分: 1

要找到平均的scrapleValue,一种方法是找到所有值的总和,然后除以单词的总数。当然,这个sum操作类似于您在scrapleValue方法内执行的操作。

要使它与您的操作类似,您需要使用mapToInt获得一个IntStream,您可以在其中执行sum操作,甚至直接执行average操作。因此,映射将如下所示:

System.out.println("单词的平均scraple值是:");
double averageScrapleValue = Stream.of(words)
        .mapToInt(YourClass::scrapleVal)
        .average()
        .orElse(0.0);

另外注意,当您想要根据它们的值对单词进行排序时,只需使用您的方法返回的整数值进行比较:

.sorted(Comparator.comparingInt(YourClass::scrapleVal))

要解决获取平均值以下和平均值以上的值的问题,正确方向的提示是寻找“partioningBy”,这是上面代码块中计算的平均值。

英文:

To find the average scrapleValue, one way is to find the sum of all values and divide it by the total number of words. Ofcourse, the sum would be similar to the operation that you've performed inside your scrapleValue method.

What it would though need you to make things similar would be to mapToInt to get an IntStream where you could perform a sum or even directly an average. Hence the mapping would look like :

System.out.println(&quot;Average scraple value of words is: &quot;);
double averageScrapleValue = Stream.of(words)
.mapToInt(YourClass::scrapleVal)
.average()
.orElse(0.0);

Further note, when you want to sort words with their values, just compare them using the integer value returned by your method

.sorted(Comparator.comparingInt(YourClass::scrapleVal))

and to solve for getting the values below and above average, the hint in the right direction would be to look for "partioningBy" the average calculated in the above block.

huangapple
  • 本文由 发表于 2020年8月2日 09:12:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63211469.html
匿名

发表评论

匿名网友

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

确定