Skip to content

Instantly share code, notes, and snippets.

@oguzcan-yavuz
Created November 29, 2018 00:10
Show Gist options
  • Save oguzcan-yavuz/15312f4605abf4d935e38e2016bf0628 to your computer and use it in GitHub Desktop.
Save oguzcan-yavuz/15312f4605abf4d935e38e2016bf0628 to your computer and use it in GitHub Desktop.
My solution for the HDE Challenge 003
package main
import "fmt"
func square(x int) int {
return x * x
}
func calculateSum(numberCount int, sum int) int {
if numberCount == 0 {
return sum
} else {
var num int
// get the next number
fmt.Scanf("%d", &num)
if num > 0 {
sum += square(num)
}
// keep getting the next number
return calculateSum(numberCount - 1, sum)
}
}
func loopFunc(n int, sums []int) []int {
if n == 0 {
return sums
} else {
var numberCount int
// get the number count which will be given in the current loop
fmt.Scanf("%d", &numberCount)
sum := calculateSum(numberCount, 0)
sums = append(sums, sum)
return loopFunc(n - 1, sums)
}
}
func printOutputs(n int, printIndex int, outputs []int) {
if n == 0 {
return
} else {
fmt.Println(outputs[printIndex])
printOutputs(n - 1, printIndex + 1, outputs)
}
}
func main() {
var n int
// get the initial loop count
fmt.Scanf("%d", &n)
var emptyArr []int
outputs := loopFunc(n, emptyArr)
printOutputs(n, 0, outputs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment