|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/csv" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "io/ioutil" |
| 10 | + "math/rand" |
| 11 | + "os" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/dgraph-io/dgraph/x" |
| 15 | +) |
| 16 | + |
| 17 | +var home = os.Getenv("GOPATH") |
| 18 | +var fpath = flag.String("rest", |
| 19 | + home+"/src/github.com/dgraph-io/dgraph/food/rest.csv", |
| 20 | + "File path of restaurants file") |
| 21 | + |
| 22 | +type Restaurant struct { |
| 23 | + Name string |
| 24 | + Cuisine string |
| 25 | +} |
| 26 | + |
| 27 | +func main() { |
| 28 | + flag.Parse() |
| 29 | + rand.Seed(time.Now().UnixNano()) |
| 30 | + |
| 31 | + fmt.Printf("Reading from file: %s\n\n", *fpath) |
| 32 | + data, err := ioutil.ReadFile(*fpath) |
| 33 | + x.Checkf(err, "Unable to read restaurant file: %q", fpath) |
| 34 | + reader := csv.NewReader(bytes.NewReader(data)) |
| 35 | + |
| 36 | + rests := make([]Restaurant, 0, 30) |
| 37 | + for { |
| 38 | + fields, err := reader.Read() |
| 39 | + if err == io.EOF { |
| 40 | + break |
| 41 | + } |
| 42 | + x.Checkf(err, "Unable to read fields") |
| 43 | + if len(fields) < 2 { |
| 44 | + continue |
| 45 | + } |
| 46 | + r := Restaurant{Name: fields[0], Cuisine: fields[1]} |
| 47 | + rests = append(rests, r) |
| 48 | + } |
| 49 | + x.AssertTruef(len(rests) >= 5, "Can't parse even five restaurants. Found: %d", len(rests)) |
| 50 | + |
| 51 | + fmt.Printf("Choosing restaurant for 5 days from %d entries:\n", len(rests)) |
| 52 | + |
| 53 | + var last Restaurant |
| 54 | + chosen := make(map[int]struct{}) |
| 55 | + for len(chosen) < 5 { |
| 56 | + i := rand.Intn(len(rests)) |
| 57 | + if _, found := chosen[i]; found { |
| 58 | + continue |
| 59 | + } |
| 60 | + this := rests[i] |
| 61 | + if last.Cuisine == this.Cuisine { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + // Store this choice. |
| 66 | + chosen[i] = struct{}{} |
| 67 | + last = this |
| 68 | + |
| 69 | + fmt.Printf("Day [%d] Restaurant: %20s Cuisine: %10s\n", len(chosen), this.Name, this.Cuisine) |
| 70 | + } |
| 71 | +} |
0 commit comments