Skip to content

Instantly share code, notes, and snippets.

@izy521
Last active August 11, 2024 08:25
Show Gist options
  • Save izy521/bcd30d575b2a9128980e61b211aa3ec0 to your computer and use it in GitHub Desktop.
Save izy521/bcd30d575b2a9128980e61b211aa3ec0 to your computer and use it in GitHub Desktop.
HENNGE GoLang Code Challenge
package main
import (
"bufio"
"os"
"strconv"
"fmt"
"log"
"strings"
)
func main() {
var (
scanner *bufio.Scanner;
iterations int;
calculated_values []int;
)
scanner = bufio.NewScanner(os.Stdin);
iterations = read_iterations(scanner);
calculated_values = make([]int, iterations);
process_numbers_recursive(scanner, &calculated_values, 0, iterations);
print_values_recursive(calculated_values);
os.Exit(0);
}
func read_iterations(scanner *bufio.Scanner) int {
scanner.Scan();
n, err := strconv.Atoi(scanner.Text());
if err != nil {
log.Fatal(err);
}
return n;
}
func process_numbers_recursive(scanner *bufio.Scanner, calculated_values *[]int, iteration int, iterations int) {
if ( iteration > (iterations - 1) ) { return; }
//Skip amount of numbers
scanner.Scan();
//String split the actual numbers
scanner.Scan();
numbers_slice := strings.Split(scanner.Text(), " ");
solution := calculate_square_recurisve(numbers_slice);
(*calculated_values)[iteration] = solution;
process_numbers_recursive(scanner, calculated_values, iteration + 1, iterations);
}
func calculate_square_recurisve(number_slice []string) int {
if ( len(number_slice) <= 0 ) { return 0; }
number, err := strconv.Atoi(number_slice[0]);
if ( err != nil ) {
log.Fatal(err);
}
if ( number <= 0 ) {
return calculate_square_recurisve(number_slice[1:])
}
return (number * number) + calculate_square_recurisve(number_slice[1:]);
}
func print_values_recursive(calculated_values []int) {
if ( len(calculated_values) < 1 ) { return; }
fmt.Println( calculated_values[0] );
print_values_recursive( calculated_values[1:] );
}
@izy521
Copy link
Author

izy521 commented Aug 13, 2019

This was the code used to get me an interview at the Japanese company HENNGE, for a Back-End Software Developer.

The request was to make a script/program that handled an input stream of numbers and (ignoring negatives) square them, and add each together without using a for loop.

e.g.

2 (iterations)
4 (first iteration has 4 numbers)
5 32 -5 44 (4 numbers)
5 (second iteration has 5 numbers)
9 -1 81 72 -33 (5 numbers)

would return

2985
11826

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment