Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyp 20/21 sem 1 #588

Open
nanfang02 opened this issue Apr 26, 2022 · 8 comments
Open

pyp 20/21 sem 1 #588

nanfang02 opened this issue Apr 26, 2022 · 8 comments

Comments

@nanfang02
Copy link

cs2030(s) ay2020/21 qn6 streams:
Hiii im not very sure on how to do qn 6 (e).... please help thank you!
qn6

@pohja1999
Copy link

pohja1999 commented Apr 26, 2022

hmm can u try this. [.reduce(0, (a,b) -> a + b.length(), (a,b) -> a + b);] not sure if this is what the question is asking for. Or, you can do this .reduce("", (a,b) -> a + b).length(); i think both works.

@jingyulim1
Copy link

jingyulim1 commented Apr 26, 2022

I think this question is testing Stream's 2 arguments reduce in (d) and 3 arguments reduce in (e).

For (e), since we have to reduce directly from a stream of String and return int, the only reduce we can use is the 3 arguments reduce.

This is due to the difference in the seed/identity type, where for the reduce with 2 arguments the seed/identity must be the same type as the stream but not for the reduce with 3 arguments.

image

@Swagston20
Copy link

Use either reduce(0, (x,y) -> x + y.length(), (x,y) -> x + y); or .reduce("", (x,y) -> x + y).length()

@Lu-Yi-Fan
Copy link

Personally i defined it as int count(List words) {
return words.stream()
.reduce("", (x, y) -> x + y)
.length();
}

@summerthia02
Copy link

I used this: words.stream().reduce(0, (x,y) -> x + y.length(), (x,y) -> x+ y)

@nanfang02
Copy link
Author

thank you guysss :> all the best for finals !!

@calvinseptyanto
Copy link

int count(List words) {
return words.stream()
.reduce(0, (x,y) -> x+y.length(), (x,y) -> x+y);
}

@Zhenyubbx
Copy link

int count(List words) {
return words.stream().reduce(0, (x,y)-> x + y.length(), (x, y) -> x +y);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants