Created
September 3, 2020 11:16
-
-
Save mohammadeslim22/981eb8de724a98b2e991032bc5a931f7 to your computer and use it in GitHub Desktop.
Go language Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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