How to do this question without Recursion or a For loop in GoLang?

I am referring to this problem in this question: https://www.vivasoftltd.com/getting-testcase-based-i-o-without-any-loop-in-golang/

PROBLEM STATEMENT – We want you to calculate the sum of squares of given integers, excluding any negatives. – The first line of the input will be an integer N (1 <= N <= 100), indicating the number of test cases to follow. – Each of the test cases will consist of a line with an integer X (0 < X <= 100), followed by another line consisting of X number of space-separated integers Yn(-100 <= Yn<= 100). – For each test case, calculate the sum of squares of the integers, excluding any negatives, and print the calculated sum in the output.

Note 1: Do not put blank lines between test case solutions. Note 2: Take input from standard input and output to standard output.

RULES – Do not use any Loop statement – You may only use standard library packages – There should be no output until all the input has been received.

Sample Input:

2

4

3 -1 1 14

5

9 6 -53 32 16

Sample Output:

206

1397

The question gets rid of the “no for loop” rule by using recursion. But responses on this thread lead be to believe that this question is not intended to be done with recursion:

They suggest using channels and goroutines to solve this question. I am having a hard time understanding how that would work.

You can not solve the reading input part without without a form of looping.

The exercise explicitely talks about “Loop” statements, so recursion does not violate this rule.

Using recursion for this is not idiomatic in go and will not benefit from TCO.

Just use for!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.