Skip to content

Instantly share code, notes, and snippets.

@benjic
Forked from codeeval/sample.py
Last active December 18, 2015 18:19
Show Gist options
  • Save benjic/5824778 to your computer and use it in GitHub Desktop.
Save benjic/5824778 to your computer and use it in GitHub Desktop.
package main
/*
Here is a sample construct that reads a file name supplied as an argument and
allows you, the programmer, to easily work with the supplied parameters. Have Fun!
*/
import (
"bufio"
"os"
//"strconv"
"strings"
)
func main() {
// Open your file using the second command line arguement becuase the binary name is the first.
file, err := os.Open(os.Args[1])
// Anticipate errors
if err == nil {
reader := bufio.NewReader(file)
str, err := reader.ReadString('\n')
for err == nil {
parameters := strings.Split(strings.Trim(s, "\n"), " ")
// Parameters is a slice of strings that represent the space delimited
// items located on each line of the file.
// At this point you would parse your strings if you need a different type ie
// a := strconv.Atoi(params[0])
// In my applicaitons I have used a single funciton to perform the proper action
// and return the anticipated result which is printed ie:
// fmt.Println(a)
str, err = reader.ReadString('\n')
}
} else {
// You can catch file propblems if you want. It is proper... :)
}
}
# On CodeEval, test cases are read in from a file which is the first argument to your program
# Open the file and read in line by line. Each line represents a different test case
# (unless given different instructions in the challenge description)
import sys
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
# ignore test if it is an empty line
# 'test' represents the test case, do something with it
# ...
# ...
test_cases.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment