Last active
February 11, 2025 19:00
-
-
Save lae/1ccc9f86bc16a7eb06aeaeb8e84b8c42 to your computer and use it in GitHub Desktop.
Sum of Squares Challenge for HENNGE (made public after interviews)
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
// nyan. | |
// as the main constraint for this challenge appears to be no use | |
// of for statements, it primarily makes use of recursion. | |
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
) | |
// reads stdin, processes problems/test cases, then prints results | |
func main() { | |
nums := readInts() | |
results, _ := problemSolver(nums[0], nums[1:]) | |
printResults(results) | |
} | |
// recursively prints the results slice in ascending order | |
func printResults(results []int) { | |
if len(results) == 0 { | |
return | |
} | |
// swapping the following two lines would reverse output order | |
fmt.Println(results[0]) | |
printResults(results[1:]) | |
} | |
/* | |
* This function collects the sum of squares for each test case by recursively | |
* executing <cases>+1 times (e.g. 3 times if cases == 2), but returns an | |
* initial empty slice and an index of 0 that we use to keep track of where we | |
* are in the data slice (that contains our test case data). It increments the | |
* index after a sum has been collected and the test case has been processed. | |
*/ | |
func problemSolver(cases int, data []int) ([]int, int) { | |
if cases == 0 { | |
// initial state of sums slice and index | |
return []int{}, 0 | |
} | |
sums, index := problemSolver(cases-1, data) | |
totalInts := data[index] | |
// moves index to first integer to be squared | |
index += 1 | |
// subslice that contains the integers used for the sum of squares | |
problem := data[index : index+totalInts] | |
sums = append(sums, sumOfSquares(problem)) | |
// moves index to the next case's data | |
index += totalInts | |
return sums, index | |
} | |
// recursively collects sum of squares of positive integers | |
func sumOfSquares(nums []int) int { | |
if len(nums) == 0 { | |
// initial state | |
return 0 | |
} | |
sum := sumOfSquares(nums[1:]) | |
if nums[0] > 0 { | |
sum += nums[0] * nums[0] | |
} | |
return sum | |
} | |
/* | |
* This function recursively reads integers from standard input until it hits | |
* an EOF control character, and effectively creates queue (slice) of ints for | |
* us to use in the problemSolver function. | |
*/ | |
func readInts() []int { | |
var num int | |
// naive input validation since nothing's really specified in requirements | |
// (and I'm not familiar with golang), so I'll trust all given input are | |
// numbers. wouldn't be skimping over this in an actual application. | |
_, err := fmt.Scan(&num) | |
if err != nil { | |
if err == io.EOF { | |
return []int{} | |
} | |
log.Fatal(err) | |
} | |
nums := append([]int{num}, readInts()...) | |
return nums | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment