Skip to content

Instantly share code, notes, and snippets.

@aa-tan
Last active December 21, 2017 09:30
Show Gist options
  • Save aa-tan/9efdefb4dae5196e8daf306fc369495a to your computer and use it in GitHub Desktop.
Save aa-tan/9efdefb4dae5196e8daf306fc369495a to your computer and use it in GitHub Desktop.
HDE Challenge 003 Submission
package main
import (
"fmt"
"strings"
"strconv"
"os"
"bufio"
)
func get_length(lengthString string) string {
fmt.Println("Input number of terms")
fmt.Scanf("%s", &lengthString)
return lengthString
}
func get_line(line string) string {
fmt.Println("Input terms")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
line = scanner.Text()
return line
}
func convert_to_int(toConvert string) int {
converted, err := strconv.Atoi(toConvert)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
return converted
}
func no_for_allowed(arr []string, lengthInt int) int{
ret := convert_to_int(arr[0])
if lengthInt == 1 {
if ret > 0 {
return ret*ret
} else{
return 0
}
} else {
arr = append(arr[:0], arr[1:]...)
if ret > 0 {
return ret*ret + no_for_allowed(arr, lengthInt-1)
} else {
return 0 + no_for_allowed(arr, lengthInt-1)
}
}
}
func main() {
var lengthString, line string
var lengthInt int
lengthString = get_length(lengthString)
lengthInt = convert_to_int(lengthString)
line = get_line(line)
arr := strings.Split(line, " ")
fmt.Println(no_for_allowed(arr, lengthInt))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment