Skip to content

Instantly share code, notes, and snippets.

@tehnerd
Created March 30, 2015 05:14
Show Gist options
  • Save tehnerd/d86901b4a480550f0681 to your computer and use it in GitHub Desktop.
Save tehnerd/d86901b4a480550f0681 to your computer and use it in GitHub Desktop.
// Package bench is a package which contains
// programs of Go Benchmark Competition.
package bench
import (
"bufio"
"errors"
"os"
"strconv"
)
// Find reads the text file on the `path`,
// finds the `s` words on the file and
// returns the row numbers and indices
// in the form of `r:c,r:c,...r:c`,
// at which the `s` word exists.
func Find(path, s string) (string, error) {
// TODO: Implement this function.
if len(s) == 0 {
return "", errors.New("len of S is equal to zero")
}
fd, _ := os.Open(path)
sLen := len(s)
scanner := bufio.NewScanner(fd)
resp := ""
i := 1
f := 0
for scanner.Scan() {
is := strconv.Itoa(i)
line := scanner.Bytes()
for cntr := 0; cntr+sLen < len(line); cntr++ {
if string(line[cntr:cntr+sLen]) == s {
cs := strconv.Itoa(cntr)
if f != 0 {
resp += ("," + is + ":" + cs)
} else {
resp += (is + ":" + cs)
f = 1
}
}
}
i++
}
return resp, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment