Section titled Problem
Problem
Section titled Description
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.
Section titled Rules
Rules
You may only use standard library packages. In addition, extra point is awarded if solution does not declare any global variables.
Section titled Specific%20rules%20for%20Go%20solution
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() {
...
}
Section titled Sample%20Input
Sample Input
2
4
3 -1 1 14
5
9 6 -53 32 16
Section titled Sample%20Output
Sample Output
206
1397
Section titled Solution
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:
- Get total input number
N
- Get total input number inline with space separator
- Get all number inline input, it should be same with total number defined in step 2
- Repeat step 2 and 3 until
N
- Print calculated the sum of squares of integers excluding negative numbers
Section titled Step%201
Step 1
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)
}
Section titled Step%203
Step 3
Read input as described in solution step above, line 62 is step 2 and the rest is step 3
Section titled Step%204
Step 4
Calculate positive square sum then store it in array with counter
index.
Section titled Step%205
Step 5
Call read
recursively with counter+1