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

Mock Question 3b, c [2122 Sem 1] #590

Open
bryankwe opened this issue Apr 26, 2022 · 4 comments
Open

Mock Question 3b, c [2122 Sem 1] #590

bryankwe opened this issue Apr 26, 2022 · 4 comments

Comments

@bryankwe
Copy link

Source

2122 Semester 1 Finals

Description

Hi guys, anyone has any idea for solving 3b and c? I can't seem to think of the solution.

For 3a, I did this (I guess rangeClosed() will work too)
return IntStream .iterate(0, x -> x + 1) .limit(arr.length) .map(x -> fn.apply(list[x])) .toArray(); }

Using my 3a approach, how do we know the length of the stream to generate before mapping for 3b? Or is there a better approach?

Would be grateful for any discussion. Thank you in advance :)

@pearlynliu
Copy link

hi! i think my method is similar to yours for 3a, but instead of applying the function to list[x], i applied it to arr[x].

for 3b, i added a filter(x -> x != 0) after the mapping and before converting to array. when i map my function, i apply it to x directly instead of arr[x].

whereas for my function in 3c, i defined it as f = x -> x % 2 == 0 ? arr[x] * 2 : 0;

i am not sure if it is right though, i feel like i might be hardcoding it

@seantanyurong
Copy link

Hi, I think you actually already have what you need to solve 3(b), but just need to fit into the requirements slightly.

This is from my rough understanding. The difference between (a) and (b) is the function that is used as the argument.
• (a)'s function manipulates the values within the array. It takes in a value from the array and changes it.
• (b)'s function manipulates the entire array. It takes in the array, changes the value within the array, and returns an updated array. As for how it accesses the values in the array, you've already done it in the method body of (a) (ie. your code above). You just now have to recognise that the argument captures this logic, and leave it to do it.

For (c), similar to what you already did, I just generated indexes and used some methods to get the correct output.

To answer your question, the length of the stream should be determined by the array argument that you push into the function.

Not 100% sure this is correct, but this solution works through JShell. Probably better to update the Function to UnaryOperator as well.

Screenshot 2022-04-27 at 12 41 40 AM

@lukasleeyo
Copy link

lukasleeyo commented Apr 26, 2022

Hi there, this is how I approach the question. Firstly, I treat the question as it is using List instead of array as I have kinda forgotten how to work with arrays (*prays that arrays won't come out in our finals).

image
From the picture above, we can see the function f that we pass in to convert() is supposed to do everything for us i.e. pick alternate elements from the front and multiply these elements by 2.

image
The picture here also further elaborates that function f should be doing the stuff for us.

So for my 3b, my convert method would just look like this:
image
the function f would perform everything for me and I just need to apply the function to my list.

Then, the heavy lifting part where the function does pick alternate elements and multiply them by 2 is like this:
image

Hope it helps! and I really hope arrays don't come out for finals since we never deal with them in this module haha...

@Swagston20
Copy link

3a) Arrays.stream(arr).map(f).toArray()
3b) f.apply(arr)
3c) UnaryOperator<Integer []> f = arr -> IntStream.iterate(0, x -> x < arr.length, x -> x + 2).map(x -> Array.get(arr, x) * 2).toArray()

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

5 participants