-1

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: There should be no output until all the input has been received. Note 2: Do not put blank lines between test cases solutions. Note 3: Take input from standard input, and output to standard output.

Rules Write your solution using Go Programming Language Your source code must be a single file (package main) Do not use any for statement You may only use standard library packages

"Problem which I am facing" 'square' function below is not getting executed the required number of times according to the input test cases. To meet specific requirements I wasn't allowed to use the 'for' statement. Please help me out. Language is Go.

package main

import "fmt"

var s []int

func square(l int) {
    i := 0
    sum := 0
Square:

    if l > 0 {
        s[i] = s[i] * s[i]
        sum = sum + s[i]
        i++
        l--
        goto Square

    }
    fmt.Println(sum)

}

func myfunc(a int) {
Here:
    if a > 0 {
        var b int
        fmt.Scanln(&b)
        if b > 0 {
            s = append(s, b)
        }
        a--
        goto Here
    }

}

func main() {
    var a int
    fmt.Scanln(&a)
TestCases:
    if a > 0 {
        var T int
        fmt.Scanln(&T)
        myfunc(T)
        a--
        goto TestCases
    }
    square(len(s))
}

2 Answers2

3

Here is an implementation using recursion:

package main

import "fmt"

func testCase(N int) {
    if N <= 0 {
        return
    }
    var X int
    fmt.Scanf("%d", &X)
    fmt.Println(sumOfSquare(X))
    testCase(N-1)
}

func sumOfSquare(X int) int {
    if X == 0 {
        return 0
    }
    var Y int
    fmt.Scanf("%d", &Y)
    if Y > 0 {
        return Y*Y + sumOfSquare(X-1)
    }
    return sumOfSquare(X-1)
}

func main() {
    var N int
    fmt.Scanf("%d", &N)
    testCase(N)
}

Here is an example output:

$ go run main.go
2 4 3 -1 1 14 5 9 6 -53 32 16
206
1397
chmike
  • 20,922
  • 21
  • 83
  • 106
  • Thank you for the answer @chmike but the question says there are multiple test cases for which the program should take input together and produce the output accordingly. – Shivam Srivastava Feb 10 '20 at 11:07
  • `Sample Input 2 4 3 -1 1 14 5 9 6 -53 32 16 Sample Output 206 1397` – Shivam Srivastava Feb 10 '20 at 11:08
  • In above sample input 2 is the number of test-cases for which the program has to take the number of elements. 4 is the number of elements and then 4 elements are taken as input and for the second test case, 5 is the number of elements. For output first, the sum of the first test case is printed and then sum for the second one is printed – Shivam Srivastava Feb 10 '20 at 11:14
  • @ShivamSrivastava I just read the problem statement. Indeed, you are allowed to use goto and not required to use recursion. The problem in your code is that you don't read the number or values of a test case in `myfunc`. Another problem is that the test case values are space separated, and Finally you don't exclude negative values. Another thing is that there is no need to construct a slice. – chmike Feb 10 '20 at 11:14
  • @ShivamSrivastava I rewrote my answer to answer the problem statement. It uses recursive calls and not goto. It yields the expected output. – chmike Feb 10 '20 at 11:46
0

I have this program in python using recursion, but not in go-lang. Here is the code

def squaresum(counter,alist):
if counter==0:
    return 0
else:
    if int(alist[counter-1])<0:
        return 0 + squaresum(counter-1, alist)
    else:
        return int(alist[counter-1])**2 + squaresum(counter-1,alist)


def testcase(a):
    if a==0:
        return
    terms = int(input())
    nums = input()
    list1 = nums.split()
    print(squaresum(terms,list1))
    testcase(a-1)
    

if __name__=='__main__':
    casecount = int(input())
    testcase(casecount)
rekxspein
  • 111
  • 4