英文:
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'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 > x, emit ".", otherwise emit 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 ? "." : y + ""))
.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 -> x > 0, x -> x -1 ).boxed()
.flatMap(x -> IntStream.iterate(5 , y -> y > 0 , y -> y - 1)
.mapToObj(y -> y > x ? "." : y + ""))
.reduce("", (x,y) -> 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->x-1).limit(n*n)
.map(x -> x%n>x/n ? ".":String.valueOf(1+x%n))
.collect(Collectors.joining());
Sample for 6:
> 654321.54321..4321...321....21.....1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论