cc

package module
v0.0.0-...-00a5acb Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2017 License: MIT Imports: 9 Imported by: 0

README

CC

Package cc provides functionalities related to coding challenges, where problems are read from a text file and solutions are written back in an other file.

The package name cc stands for "Coding Challenge"

Install

go get github.com/xeonx/cc

Docs

http://godoc.org/github.com/xeonx/cc

Usage

Implement the cc.Problem interface and call cc.Run in the main function.

package main

import(
	"github.com/xeonx/cc"
)

func main() {
	cc.Run(func() cc.Problem{
		return &problem{}
	})
}
	
type problem struct {
	a int
}

func (pb *problem) Init(lines []cc.String) int
	pb.a = lines[0].Int()
	return 1
}

func (pb *problem) Solve() interface{}
	if a%2 == 0 {
		return "EVEN"
	}
	return "ODD"
}

License

This code is licensed under the MIT license. See LICENSE.

Documentation

Overview

Package cc provides functionalities related to coding challenges, where problems are read from a text file and solutions are written back in an other file.

The package name "cc" stands for "Coding Challenge"

See sources at http://github.com/xeonx/cc

For example to create a program testing whether the input is odd or even:

package main

import(
	"github.com/xeonx/cc"
)

func main() {
	cc.Run(func() cc.Problem{
		return &problem{}
	})
}

type problem struct {
	a int
}

func (pb *problem) Init(lines []cc.String) int
	pb.a = lines[0].Int()
	return 1
}

func (pb *problem) Solve() interface{}
	if a%2 == 0 {
		return "ODD"
	}
	return "EVEN"
}

Index

Constants

This section is empty.

Variables

View Source
var (
	//DefaultInput is the default name for input files
	DefaultInput = "sample.in"
	//PrintFormat is the format for writing the result in the output file
	PrintFormat = "Case #%d: %v\n"
)

Functions

func Run

func Run(factory ProblemFactory)

Run reads from file, solves and writes problem results in files.

func RunFrom

func RunFrom(r io.Reader, w io.Writer, factory ProblemFactory)

RunFrom reads from io.Reader, solve and writes problem results in io.Writer

Types

type Problem

type Problem interface {
	Init([]String) int
	Solve() interface{}
}

Problem represents a item of a challenge that must be solved

type ProblemFactory

type ProblemFactory func() Problem

A ProblemFactory is a factory for creating Problems

type String

type String string

String provides helper function when reading problem data from string.

func (String) Int

func (s String) Int() int

Int convert the value into an int

func (String) Int2

func (s String) Int2() (int, int)

Int2 convert the value into two ints

func (String) Int3

func (s String) Int3() (int, int, int)

Int3 convert the value into three ints

func (String) Int64

func (s String) Int64() int64

Int64 convert the value into an int64

func (String) Int64Array

func (s String) Int64Array() []int64

Int64Array convert the value into an array of int64

func (String) Int642

func (s String) Int642() (int64, int64)

Int642 convert the value into two int64

func (String) Int643

func (s String) Int643() (int64, int64, int64)

Int643 convert the value into three int64

func (String) IntArray

func (s String) IntArray() []int

IntArray convert the value into an array of ints

func (String) SingleDigitIntArray

func (s String) SingleDigitIntArray() []int

SingleDigitIntArray convert the value into an array of ints. Each characters is converted individually.

func (String) Split

func (s String) Split() []String

Split the value into a array of String, using spaces as separator.

func (String) String

func (s String) String() string

String convert the value into a standard string

Directories

Path Synopsis
cc_template generates a new go package usable as a base for coding challenges.
cc_template generates a new go package usable as a base for coding challenges.

Jump to

Keyboard shortcuts

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