Skip to content

Instantly share code, notes, and snippets.

@kristi
Created January 16, 2014 09:23
Show Gist options
  • Save kristi/8452068 to your computer and use it in GitHub Desktop.
Save kristi/8452068 to your computer and use it in GitHub Desktop.
A template for parsing integers from Stdin. InputParser struct uses embedding (go-flavored inheritance, see http://golang.org/doc/effective_go.html#embedding ) While it may be simpler to use multiple calls to fmt.Scan or fmt.Scanf, Scan is slow for really large arrays. Don't mix Scan calls with the bufio.Reader because the Reader is buffered so …
package main
import (
"fmt"
"strings"
"io"
"bufio"
"os"
"strconv"
)
func solve(a []int) (ans int) {
return
}
func main() {
input := NewInputParser(os.Stdin)
cases := input.ParseInt()
for c:=0; c<cases; c++ {
size := input.ParseInt()
a := input.ParseIntArray(size)
n := solve(a)
fmt.Println(n)
}
}
/* Prints an array without the brakets */
func printArray(a []int) {
fmt.Println(strings.Trim(fmt.Sprint(a),"[]"))
}
type InputParser struct {
*bufio.Reader
}
func NewInputParser(rd io.Reader) *InputParser {
return &InputParser{ bufio.NewReader(rd) }
}
/* Returns a single line of input (without the ending newline) */
func (in *InputParser) ParseString() string {
s,err := in.ReadString('\n')
if err != nil { panic(err) }
s = strings.TrimSpace(s)
return s
}
/* Parses a line of input as a single integer
If there are multiple integers on the same line, this only returns
the first integer.
*/
func (in *InputParser) ParseInt() int {
s := in.ParseString()
i,err := strconv.Atoi(s)
if err != nil { panic(err) }
return i
}
/* Parses an array of integers separated by space */
func (in *InputParser) ParseIntArray(capacity int) []int {
a := make([]int, 0, capacity)
s := in.ParseString()
for _,w := range strings.Split(s, " ") {
n,err := strconv.Atoi(w)
if err != nil { panic(err) }
a = append(a, n)
}
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment