Created
November 2, 2017 21:01
-
-
Save benjamw/3dc84517f12809236ddac289d5757269 to your computer and use it in GitHub Desktop.
Scan and sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
) | |
type Liner interface { | |
Process(string) | |
PrintValues() | |
} | |
type ScoreProcessor struct { | |
Map map[int][]string | |
} | |
func (p *ScoreProcessor) Process(s string) { | |
b := strings.Split(s, ",") | |
name := strings.TrimSpace(b[0]) | |
score, err := strconv.Atoi(strings.TrimSpace(b[1])) | |
if err == nil && score >= 90 { | |
if p.Map[score] == nil { | |
p.Map[score] = make([]string, 0) | |
} | |
p.Map[score] = append(p.Map[score], name) | |
} | |
} | |
func (p *ScoreProcessor) PrintValues() { | |
for i := 90; i <= 100; i++ { | |
for _, v := range p.Map[i] { | |
fmt.Printf("%s - %d\n", v, i) | |
} | |
} | |
} | |
func main() { | |
fn := os.Args[1] | |
l := ScoreProcessor{ | |
Map: make(map[int][]string, 0), | |
} | |
err := LineByLine(fn, &l) | |
if err != nil { | |
fmt.Printf("error lining file: %v\n", err) | |
os.Exit(1) | |
} | |
os.Exit(0) | |
} | |
func LineByLine(fn string, l Liner) error { | |
f, err := os.Open(fn) | |
defer f.Close() | |
if err != nil { | |
return err | |
} | |
s := bufio.NewScanner(f) | |
for s.Scan() { | |
t := s.Text() | |
if s.Err() != nil { | |
return err | |
} | |
if len(t) == 0 { | |
break | |
} | |
l.Process(t) | |
} | |
l.PrintValues() | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment