Calculating the Sum of Squares of Integers in Go

# Calculating the Sum of Squares of Integers in Go

January 25, 2023

Name
Moch Lutfi
@kaptenupi

## Problem

### Description

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

### Rules

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

#### Specific rules for Go solution

• 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

## Solution

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

## Step 1

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

main.go

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)
}
func FindPositiveSquareSum(input []string, n int) int {
if n <= 0 {
return 0
}
}
}
func Print(input []int, n int) {
if n >= len(input) {
return
}
fmt.Println(input[n])
Print(input, n+1)
return
}
scanner.Scan()
number, _ := strconv.Atoi(scanner.Text())
return number
}
scanner.Scan()
}
func read(scanner *bufio.Scanner, numTest, counter int, result []int) {
if numTest == counter {
// print
Print(result, 0)
return
}
if num != len(strNum) {
return
}
result[counter] = FindPositiveSquareSum(strNum, len(strNum))
}

## Step 3

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

main.go

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)
}
func FindPositiveSquareSum(input []string, n int) int {
if n <= 0 {
return 0
}
}
}
func Print(input []int, n int) {
if n >= len(input) {
return
}
fmt.Println(input[n])
Print(input, n+1)
return
}
scanner.Scan()
number, _ := strconv.Atoi(scanner.Text())
return number
}
scanner.Scan()
}
func read(scanner *bufio.Scanner, numTest, counter int, result []int) {
if numTest == counter {
// print
Print(result, 0)
return
}
if num != len(strNum) {
return
}
result[counter] = FindPositiveSquareSum(strNum, len(strNum))
}

## Step 4

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

main.go

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)
}
func FindPositiveSquareSum(input []string, n int) int {
if n <= 0 {
return 0
}
}
}
func Print(input []int, n int) {
if n >= len(input) {
return
}
fmt.Println(input[n])
Print(input, n+1)
return
}
scanner.Scan()
number, _ := strconv.Atoi(scanner.Text())
return number
}
scanner.Scan()
}
func read(scanner *bufio.Scanner, numTest, counter int, result []int) {
if numTest == counter {
// print
Print(result, 0)
return
}
if num != len(strNum) {
return
}
result[counter] = FindPositiveSquareSum(strNum, len(strNum))
}

## Step 5

main.go

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)
}
func FindPositiveSquareSum(input []string, n int) int {
if n <= 0 {
return 0
}
}
}
func Print(input []int, n int) {
if n >= len(input) {
return
}
fmt.Println(input[n])
Print(input, n+1)
return
}
scanner.Scan()
number, _ := strconv.Atoi(scanner.Text())
return number
}
scanner.Scan()
}
func read(scanner *bufio.Scanner, numTest, counter int, result []int) {
if numTest == counter {
// print
Print(result, 0)
return
}
if num != len(strNum) {
return
}
result[counter] = FindPositiveSquareSum(strNum, len(strNum))
}

## Step 1

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

## Step 3

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

## Step 4

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

## Step 5

main.go

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)
}
func FindPositiveSquareSum(input []string, n int) int {
if n <= 0 {
return 0
}
}
}
func Print(input []int, n int) {
if n >= len(input) {
return
}
fmt.Println(input[n])
Print(input, n+1)
return
}
scanner.Scan()
number, _ := strconv.Atoi(scanner.Text())
return number
}
scanner.Scan()
}
func read(scanner *bufio.Scanner, numTest, counter int, result []int) {
if numTest == counter {
// print
Print(result, 0)
return
}
if num != len(strNum) {