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

1920 Special Semester Q7 #399

Open
wenshienyap opened this issue Apr 27, 2021 · 16 comments
Open

1920 Special Semester Q7 #399

wenshienyap opened this issue Apr 27, 2021 · 16 comments
Labels
🧠 Finals Q&A Questions related to Finals

Comments

@wenshienyap
Copy link

Summary

Hi! I'm not too sure on how to go about solving this question, I think what confused me was how to initialise the points for the circle objects to be created. If anyone has any suggested solutions I would love to know how you guys did it!

Screenshots (if any):

Screenshot 2021-04-28 at 12 22 26 AM

@wenshienyap wenshienyap added the 🧠 Finals Q&A Questions related to Finals label Apr 27, 2021
@gracefour11
Copy link

there's a pattern in which the different circle's centre coordinates are created.

if it's getCircles(10) --> (1,10) , (2,9) ...... (9,2), (10,1)

since you can't use for loops or helper class, u can use streams! (Hint: IntStream)

Hope this helps!

@chinjuanlee
Copy link

you can try using intStream.mapToObj(x -> new Circle(..........))

@gableejh
Copy link

i did this return Stream.iterate(1, x -> x <= n, x -> x + 1).map(x -> new Circle(new Point(x, n + 1 - x))).collect(Collectors.toList());

@wenshienyap
Copy link
Author

Actually, was wondering, is there a difference between using IntStream and Stream? not really right, like since we know that our stream will be all ints?

@chamath2030
Copy link

all the same so long as u can produce the right output

@ruthkangyr
Copy link

you can try using intStream.mapToObj(x -> new Circle(..........))

I know using map only will be wrong but why is it the case where we have to use mapToObj?

@seannzx
Copy link

seannzx commented Apr 27, 2021

you can try using intStream.mapToObj(x -> new Circle(..........))

I know using map only will be wrong but why is it the case where we have to use mapToObj?

IntStream has a Stream type of Integer. Since you want the resulting Stream to store Circle, this IntStream has to be mapped to an appropriate Stream<Circle> via mapToObj

@seannzx
Copy link

seannzx commented Apr 27, 2021

This is how I did it

List<Circle> getCircles(int n) {
        return Stream.iterate(new Circle(new Point(1, n)), circle -> new Circle(new Point(circle.centre.x + 1, circle.centre.y - 1)))
                    .limit(n)
                    .collect(Collectors.toList());
}

``

@chooweixiang98
Copy link

image

@e0543492
Copy link

image

@rljw
Copy link

rljw commented Apr 28, 2021

I had a slightly different approach to iteration that does not require the use of .limit(), hope this offers a new perspective!

List<Circle> getCircles(int n) {
       return Stream.iterate(1, x -> x < n + 1, x -> x+1)
       .map(x -> new Point(n - x + 1, x))
       .map(x -> new Circle(x))
       .collect(Collectors.toList());
  
 }

Nonetheless both ways should still work. You can also use IntStream as the others have mentioned, but remember to use mapToObj instead of the usual map (map would give an error)

@marvinljw
Copy link

I did this method.

Stream.rangeClosed(1,n)
.map(x -> new Circle(new Point(x,n-x+1)))
.collect(Collectors.toList());

@Deshun99
Copy link

i did this, hope it helps

List getCircles(int n) {
return Stream.iterate(1, x -> n + 1, x -> x + 1).
map(x -> new Circle(new Point(x, n + 1 - x)).
collect(Collectors.toList());
}

@zhiqixn
Copy link

zhiqixn commented Apr 28, 2021

Screenshot 2021-04-28 at 11 58 42 AM

Hi, I'm not sure if there were IntStream implementations for this question so here's my method for this question! :-)

@danieltwh
Copy link

danieltwh commented Apr 28, 2021

you can try using intStream.mapToObj(x -> new Circle(..........))

I know using map only will be wrong but why is it the case where we have to use mapToObj?

Hey @ruthkangyr, IntStream's .map() takes in an IntUnaryOperator. Hence, the function provided must return an Integer. This makes sense as IntStream is a stream of integers (Note that it is not a Stream<Integer>. It is a separate class altogether).

Since you want a Stream, then you would have to call .mapToObject( x -> new Circle(...)). Alternatively, you can also do intStream.boxed() to convert an IntStream to a Stream<Integer> before calling .map(x -> new Circle(...)).

Hope this help! :)

@Herrekt
Copy link

Herrekt commented Apr 28, 2021

Screenshot 2021-04-28 at 11 58 42 AM

Hi, I'm not sure if there were IntStream implementations for this question so here's my method for this question! :-)

IntStream would need to be mapped to Object (via mapToObj) I think, as normal outputs for IntStream would still be integers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧠 Finals Q&A Questions related to Finals
Projects
None yet
Development

No branches or pull requests