Consider the following Scala code: def f(collection: List[Int], init_acc: Int):
ID: 3823708 • Letter: C
Question
Consider the following Scala code: def f(collection: List[Int], init_acc: Int): Int = {____ .foldLeft (_______) {(acc, ___________) Rightarrow//work which updates the value of _____ in the next iteration, often based on the current value of both acc and ci}} The following pseudo code represents a similar concept acc = ______ for (_______ in-left-to-right _______) {//work which updates the value of _____ in the next iteration, often based on the current value of both acc and ci} Suppose instead we want the analogous pseudo code for foldRight acc = _______ for (_______ __________ _________) {//work which updates the value of _______ in the next iteration, often based on the current value of both acc and ci}Explanation / Answer
Solution for Part 1:
def f(collection: List[int], init_acc: Int = { List.foldLeft(0){(acc,ci)=>acc+ci) } }
Solution for Part 2(Pseudo code):
acc = init_acc
for(acc in-left-to-right ci ){
collection = acc+ci
}
Solution for Part 3(Pseudo code):
acc = init_acc
for(ci in-right-to-left acc ){
collection = acc+ci
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.