Skip to content

Commit d471f6d

Browse files
committed
Stop fighting over food. Use a random algorithm to decide which restaurant to order from.
1 parent 7bdb276 commit d471f6d

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

food/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/food

food/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

food/rest.csv

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Maya Da Dhaba, Indian
2+
Hari's Vegetarian, Indian
3+
Alex Lee Kitchen, Singaporean
4+
Zambrero, Mexican
5+
Dominos Pizza, Italian
6+
Chur Burger, Australian
7+
BL Burgers, Australian
8+
Ribs & Burgers, Australian
9+
Thanon Khao San, Thai
10+
Red Elephant House, Thai

0 commit comments

Comments
 (0)