计算 IntStream 模式

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

Computing IntStream pattern

问题

IntStream.iterate(n, x -> x > 0, x -> x - 1)
    .flatMap(x -> IntStream.rangeClosed(1, x)
        .map(y -> x - y + 1))
    .mapToObj(x -> x == 1 ? "." : Integer.toString(x))
    .collect(Collectors.joining());

请注意,这只是代码的翻译部分,可能还需要进行测试和调试,以确保其在您的环境中正常工作。

英文:

> Question 1: Stream (10 points)
> By constructing a single stream pipeline, complete the following pattern method to generate the output as shown in the sample jshell session.
> Assume that the answer is saved in the file pattern.jsh, and begin your answer with:
> ```
> String pattern(int n) {
> return ...
> }
> jshell> import java.util.stream.*;
> jshell> /open pattern.jsh
> jshell> System.out.println(pattern(3));
> 321.21..1
> jshell> System.out.println(pattern(5));
> 54321.4321..321...21....1
> jshell> /exit

How do I attempt this question and print out the dots in order?

This is the code I have written:

IntStream.iterate(5, x -> x > 0, x -> x -1 )
    .flatMap(x -> IntStream.rangeClosed(0,x).map(y -> x - y))
    .mapToObj(x -> x == 0 ? "." : x + "")
    .reduce("", (x, y) -> x + y);

And this is the result i got:

    "54321.4321.321.21.1."

I just dont know how to increment the dot part. Will be grateful if someone can help me out on this. Thank you

</details>


# 答案1
**得分**: 1

这是一种方法:对于范围从5到1的每个数字x,生成一个范围从5到1的y序列。如果y > x,则输出“.”,否则输出y。

    String pattern = IntStream.iterate(5, x -> x > 0, x -> x - 1)
        .boxed()
        .flatMap(x -> IntStream.iterate(5, y -> y > 0, y -> y - 1)
            .mapToObj(y -> y > x ? "." : String.valueOf(y)))
        .collect(Collectors.joining());

<details>
<summary>英文:</summary>

Here&#39;s one way to do it: for each number x in range 5 to 1 generate a sequence of y 5 to 1. If y &gt; x, emit &quot;.&quot;, otherwise emit y.

    String pattern = IntStream.iterate(5, x -&gt; x &gt; 0, x -&gt; x - 1)
        .boxed()
        .flatMap(x -&gt; IntStream.iterate(5, y -&gt; y &gt; 0, y -&gt; y - 1)
            .mapToObj(y -&gt; y &gt; x ? &quot;.&quot; : y + &quot;&quot;))
        .collect(Collectors.joining());

</details>



# 答案2
**得分**: 0

```java
IntStream.iterate(5, x -> x > 0, x -> x - 1).boxed()
    .flatMap(x -> IntStream.iterate(5, y -> y > 0, y -> y - 1)
    .mapToObj(y -> y > x ? "." : String.valueOf(y)))
    .reduce("", (x, y) -> x + y);

With the help of @Joni, I managed to make my code work.
英文:
 IntStream.iterate(5, x -&gt; x &gt; 0, x -&gt; x -1 ).boxed()
.flatMap(x -&gt; IntStream.iterate(5 , y -&gt; y &gt; 0 , y -&gt; y - 1)
.mapToObj(y -&gt; y &gt; x ? &quot;.&quot; : y + &quot;&quot;))
.reduce(&quot;&quot;, (x,y) -&gt; x + y);

With the help of @Joni, I managed to make my code work.

答案3

得分: 0

在这种情况下,如果您也想要一种算法解决方案。我们可以观察字符串长度为n*n,如果取模大于除法结果,我们想要点,否则我们想要取模+1。

Stream.iterate(n*n-1,x->x-1).limit(n*n)
                .map(x -> x%n>x/n ? "." : String.valueOf(1+x%n))
                .collect(Collectors.joining());

6的示例:

654321.54321..4321...321....21.....1

英文:

In case you also want an algorithmic solution. We can observe that sting length is n*n and we want dots if modulo is bigger than division result, otherwise we want modulo+1.

Stream.iterate(n*n-1,x-&gt;x-1).limit(n*n)
                .map(x -&gt; x%n&gt;x/n ? &quot;.&quot;:String.valueOf(1+x%n))
                .collect(Collectors.joining());

Sample for 6:

> 654321.54321..4321...321....21.....1

huangapple
  • 本文由 发表于 2020年8月21日 19:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63522176.html
匿名

发表评论

匿名网友

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

确定