runweeklychallenge

package module
v1.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 4 Imported by: 14

README

Go library to facilitate running a solution to the Weekly Challenge using one or more sets of inputs provided as JSON command line arguments

Example usage running a "solution" to sum integers:

package main

import run "github.com/ysth/runweeklychallenge-go"

func SumOfInts(ints []int) (int) {
    sum := int(0)
    for _, num := range ints {
        sum += num
    }
    return sum
}

func main() {
    // runSolution runs the solution for a single set of inputs; it may
    // reformat the output if desired and can use helper functions from
    // this module to extract parts of the input to pass to the solution
    // and pass through returned errors if provided by the solution
    runSolution := func(inputs any) (any, error) {
        return SumOfInts(run.AsIntSlice(inputs, "ints")), nil
    }

    // inputs example for use in error message if incorrectly formatted
    // inputs are given
    inputsExample := `{"ints":[1,2,3]}`

    // jsonschema (draft2020-12) for a set of inputs; it should validate
    // types such that any runtime conversion from the unmarshalled json
    // will not fail
    inputsSchemaJSON := `{
        "type": "object",
        "properties": {
            "ints": {
                "type": "array",
                "items": { "type": "integer" }
            }
        },
        "required": ["ints"],
        "additionalProperties": false
    }`

    run.RunWeeklyChallenge(runSolution, inputsExample, inputsSchemaJSON)
}

Example output:

$ go run example.go '{"ints":[1,2,3]}' '{"ints":[]}'
Inputs: {"ints":[1,2,3]}
Output: 6
Inputs: {"ints":[]}
Output: 0

You must provide an example JSON inputs string (used in error messages), a JSON schema for inputs, and a shim function to run the solution given the decoded JSON inputs and reformat the output if desired. Helper functions are provided to extract properties of the JSON input as various types to pass to the solution in the shim function.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsInt

func AsInt(inputs any, key string) int

extract an attribute as an int

func AsIntSlice

func AsIntSlice(inputs any, key string) []int

extract an attribute as a slice of ints

func AsSliceSlice added in v1.0.11

func AsSliceSlice(inputs any, key string) [][]any

extract an attribute as a slice of slices of any

func AsString added in v1.0.12

func AsString(inputs any, key string) string

extract an attribute as a string

func AsStringSlice added in v1.0.9

func AsStringSlice(inputs any, key string) []string

extract an attribute as a slice of strings

func InputsAsIntSlice added in v1.0.7

func InputsAsIntSlice(inputs any) []int

entire inputs as a slice of ints

func InputsAsStringSlice added in v1.0.8

func InputsAsStringSlice(inputs any) []string

entire inputs as a slice of strings

func JSONEncode added in v1.0.10

func JSONEncode(inputs any) string

JSON encode output

func RunWeeklyChallenge

func RunWeeklyChallenge(runSolution func(inputs any) (string, error), inputsExample string, inputsSchemaJSON string)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL