Skip to content

Instantly share code, notes, and snippets.

@mohammadeslim22
Created September 3, 2020 11:16
Show Gist options
  • Save mohammadeslim22/981eb8de724a98b2e991032bc5a931f7 to your computer and use it in GitHub Desktop.
Save mohammadeslim22/981eb8de724a98b2e991032bc5a931f7 to your computer and use it in GitHub Desktop.
Go language Challenge
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
MyScanner := bufio.NewScanner(os.Stdin)
NumberChecker := 0
sumArray := []int{}
testCases := 0
manipulateInput(NumberChecker, testCases, sumArray, Scanner)
}
func manipulateInput(NumberChecker int, testCases int, sumArray []int, Scanner *bufio.Scanner){
Scanner.Scan()
input := Scanner.Text()
if NumberChecker==0 {
firstInput, err := strconv.Atoi(input)
if err != nil {
fmt.Println(err)
os.Exit(0)
}else if err == nil && firstInput == 0{
os.Exit(0)
}
testCases = firstInput
}
result := strings.Split(input, " ")
if NumberChecker >0 && NumberChecker%2 ==0{
sum := Sumsquare(result)
sumArray = append(sumArray,sum)
}
if(testCases == NumberChecker/2){
printArray(sumArray)
return
}else{
NumberChecker += 1
manipulateInput(NumberChecker, testCases, sumArray, Scanner)
}
return
}
func Sumsquare (testcase []string)(int){
i, err := strconv.Atoi(testcase[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
rest := testcase[1:]
square := i*i
switch {
case i <0 && len(rest) == 0:
return 0
case i < 0 && len(rest) > 0:
return 0 + Sumsquare(rest)
case i >=0 && len(rest) == 0:
return square
case i >=0 && len(rest) > 0:
return square + Sumsquare(rest)
}
return 0
}
func printArray (sumArray []int){
rest := sumArray[1:]
fmt.Println(sumArray[0])
if len(rest) == 0 {
return
}else{
printArray(rest)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment