Calculating the Sum of Squares of Integers in Go

2 min read Tweet this post

-   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: 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.

You may only use standard library packages. In addition, extra point is awarded if solution does not declare any global variables.

  • Your source code must be a single file
  • Do not use any for and goto statement
package main

func main() {
  ...
}
2
4
3 -1 1 14
5
9 6 -53 32 16
206
1397

The first solution that comes up to my mind is recursive approach to replace for loop process. In simple step will break down into:

  1. Get total input number N
  2. Get total input number inline with space separator
  3. Get all number inline input, it should be same with total number defined in step 2
  4. Repeat step 2 and 3 until N
  5. Print calculated the sum of squares of integers excluding negative numbers

Entry point application, focus on get first integer only then call recursive read(scanner, numTests, 0, result) to do the iteration

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	numTests, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic(err)
	}
	result := make([]int, numTests)
	read(scanner, numTests, 0, result)
}

func FindPositiveSquareSum(input []string, n int) int {
	if n <= 0 {
		return 0
	}

	addition, _ := strconv.Atoi(input[n-1])
	if addition < 0 {
		addition = 0
	}
	return (FindPositiveSquareSum(input, n-1) + addition*addition)
}

func Print(input []int, n int) {
	if n >= len(input) {
		return
	}

	fmt.Println(input[n])
	Print(input, n+1)
	return
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	number, _ := strconv.Atoi(scanner.Text())
	return number
}

func readMultiple(scanner *bufio.Scanner) []string {
	scanner.Scan()
	readline := scanner.Text()
	return strings.Split(readline, " ")
}

func read(scanner *bufio.Scanner, numTest, counter int, result []int) {
	if numTest == counter {
		// print
		Print(result, 0)
		return
	}
	num := readInt(scanner)
	strNum := readMultiple(scanner)
	if num != len(strNum) {
		read(scanner, numTest, counter, result)
		return
	}

	result[counter] = FindPositiveSquareSum(strNum, len(strNum))
	read(scanner, numTest, counter+1, result)
}

Read input as described in solution step above, line 62 is step 2 and the rest is step 3


Calculate positive square sum then store it in array with counter index.

Call read recursively with counter+1

go CodingChallenge