w0lf
w0lf

Reputation: 29

Golang assignment

We want to calculate a sum of squares of some integers, excepting negatives The first line of the input will be an integer N (1 <= N <= 100) Each of the following N test cases consists of one line containing an integer X (0 < X <= 100), followed by X integers (Yn, -100 <= Yn <= 100) space-separated on the next line For each test case, calculate the sum of squares of the integers excepting negatives, and print the calculated sum to the output. No blank line between test cases (Take input from standard input, and output to standard output) Do not use the for statement Use only standard libraries Write it in the Go programming language

Sample input

2

4

3 -1 1 14

5

9 6 -53 32 16

Sample Output

206

1397

So I am new to Golang , and I managed to solve this using for statements. How can I abide by the given and not use for? using only standard libraries? Any pointers would be greatly appreciated

package main

import "fmt"

func main() {
    var N int
    fmt.Scan(&N)
    for a := 0; a < N; a++ {
        var X int
        var res int = 0
        fmt.Scan(&X)
        for b := 0; b < X; b++ {
            var Y int
            fmt.Scan(&Y)
            if Y > 0 {
                res = res + Y*Y
            }
        }
        fmt.Println(res)
    }
}

// I used fmt to read data from console. Sum of squares is found out only if the number is positive. Then computed sum is displayed to the screen

I got the same output expected, but not using the required method

This is how I did it in Java

import java.util.Scanner;

    public class SumSquares {

        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt(), m, num;
            int i = 0;
            while (i < n) {
                int j = 0, sum = 0;
                m = in.nextInt();
                while (j < m) {
                    num = in.nextInt();
                    if (num > 0) {
                        sum += num*num;
                    }
                    j++;
                }
                System.out.println(sum);
                i++;
            }
        }
    }

Upvotes: 1

Views: 960

Answers (1)

Peter
Peter

Reputation: 31751

Go does not have the while, until, or foreach loop constructs you may be familiar with from other languages. In Go, the for and range statements replace them all:

// Three expressions, i.e. the usual
for i := 0; i < n; i++ {
}

// Single expression; same as while(condition) in other languages
for condition { 
}

// No expressions; endless loop, i.e. same as while(true) or for(;;)
for {
}

// for with range; foreach and similar in other languages. Works with slices, maps, and channels.
for i, x := range []T{} {
}

If you are not allowed to use Go's single loop construct, you are left with either recursion or the goto statement:

package main

import (
    "fmt"
)

func main() {
    var N int
    fmt.Scan(&N)

    fmt.Println(f(N, 0))
}

func f(n, sum int) int {
    if n == 0 {
        return sum
    }

    var Y int
    fmt.Scan(&Y)
    if Y > 0 {
        sum += Y * Y
    }

    return f(n-1, sum)
}

With goto:

package main

import (
    "fmt"
)

func main() {
    var N, Y, sum int
    fmt.Scan(&N)

again:
    fmt.Scan(&Y)
    if Y > 0 {
        sum += Y * Y
    }

    N--
    if N > 0 {
        goto again
    }

    fmt.Println(sum)
}

Upvotes: 3

Related Questions