diff --git a/algs4/src/go/algs4/Makefile b/algs4/src/go/algs4/Makefile deleted file mode 100644 index ce71d6c..0000000 --- a/algs4/src/go/algs4/Makefile +++ /dev/null @@ -1,31 +0,0 @@ -REPO_BASE := github.com/meatballhat/box-o-sand/algs4/src/go -TARGETS := \ - $(REPO_BASE)/algs4 \ - $(REPO_BASE)/algs4/algs4-binarysearch \ - $(REPO_BASE)/algs4/algs4-flips \ - $(REPO_BASE)/algs4/algs4-gcd \ - $(REPO_BASE)/algs4/algs4-rolls \ - $(REPO_BASE)/algs4/algs4-randomseq \ - $(REPO_BASE)/algs4/algs4-average \ - $(REPO_BASE)/algs4/algs4-interval2d \ - $(REPO_BASE)/algs4/algs4-date \ - $(REPO_BASE)/algs4/algs4-test-accumulator \ - $(REPO_BASE)/algs4/algs4-stats - - -test: build - go test -x $(TARGETS) - -build: deps - go install -x $(TARGETS) - -fmt: - go fmt -x $(TARGETS) - -deps: - go get -x -n $(TARGETS) - -clean: - go clean -x -x $(TARGETS) - -.PHONY: test build deps clean fmt diff --git a/algs4/src/go/algs4/accumulator.go b/algs4/src/go/algs4/accumulator.go deleted file mode 100644 index 62c2f01..0000000 --- a/algs4/src/go/algs4/accumulator.go +++ /dev/null @@ -1,28 +0,0 @@ -package algs4 - -import "fmt" - -type Accumulator struct { - total float64 - n int -} - -func NewAccumulator() *Accumulator { - return &Accumulator{ - float64(0.0), - 0, - } -} - -func (me *Accumulator) AddDataValue(val float64) { - me.n++ - me.total += val -} - -func (me *Accumulator) Mean() float64 { - return me.total / float64(me.n) -} - -func (me *Accumulator) String() string { - return fmt.Sprintf("Mean (%d values): %7.5f", me.n, me.Mean()) -} diff --git a/algs4/src/go/algs4/algs4-average/main.go b/algs4/src/go/algs4/algs4-average/main.go deleted file mode 100644 index 208dea0..0000000 --- a/algs4/src/go/algs4/algs4-average/main.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "fmt" - "io" - "os" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -func die(err error) { - fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) - os.Exit(1) -} - -func main() { - sum := 0.0 - cnt := 0 - - for { - d, err := algs4.Stdin.ReadDouble() - if err == io.EOF { - break - } - - if err != nil { - die(err) - } - - sum += d - cnt++ - } - - fmt.Printf("Average is %.5f\n", sum/float64(cnt)) -} diff --git a/algs4/src/go/algs4/algs4-binarysearch/main.go b/algs4/src/go/algs4/algs4-binarysearch/main.go deleted file mode 100644 index b383f0c..0000000 --- a/algs4/src/go/algs4/algs4-binarysearch/main.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "bufio" - "fmt" - "os" - "sort" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -const USAGE string = `Usage: algs4-binarysearch - -You must also provide input on stdin.` - -func main() { - if len(os.Args) < 2 { - fmt.Println(USAGE) - os.Exit(2) - } - - whiteListFile, err := os.Open(string(os.Args[1])) - if err != nil { - fmt.Println("Failed to open whitelist file:", err) - } - - whiteList, err := algs4.ReadInts(bufio.NewReader(whiteListFile)) - if err != nil { - fmt.Println("UGH: ", err) - os.Exit(1) - } - - sort.Ints(whiteList) - - inputs, err := algs4.ReadInts(bufio.NewReader(os.Stdin)) - if err != nil { - fmt.Println("Failed to read inputs:", err) - os.Exit(1) - } - - for _, key := range inputs { - if algs4.BinarySearchRank(key, whiteList) == -1 { - fmt.Println(key) - } - } -} diff --git a/algs4/src/go/algs4/algs4-date/main.go b/algs4/src/go/algs4/algs4-date/main.go deleted file mode 100644 index b655014..0000000 --- a/algs4/src/go/algs4/algs4-date/main.go +++ /dev/null @@ -1,39 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "os" - "strconv" - - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -func die(err error) { - fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) - os.Exit(1) -} - -func main() { - if len(os.Args) < 4 { - die(errors.New("We need 3 ints!")) - } - - m, err := strconv.Atoi(os.Args[1]) - if err != nil { - die(err) - } - - d, err := strconv.Atoi(os.Args[2]) - if err != nil { - die(err) - } - - y, err := strconv.Atoi(os.Args[3]) - if err != nil { - die(err) - } - - date := algs4.NewDate(m, d, y) - fmt.Println(date) -} diff --git a/algs4/src/go/algs4/algs4-flips/main.go b/algs4/src/go/algs4/algs4-flips/main.go deleted file mode 100644 index ac9f79c..0000000 --- a/algs4/src/go/algs4/algs4-flips/main.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "fmt" - "math" - "math/rand" - "os" - "strconv" - "time" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -const USAGE string = "Usage: algs4-flips " - -func main() { - if len(os.Args) < 2 { - fmt.Println(USAGE) - os.Exit(1) - } - - rand.Seed(time.Now().UTC().UnixNano()) - - nflips, err := strconv.Atoi(os.Args[1]) - if err != nil { - fmt.Println("Wat.", err) - os.Exit(2) - } - - heads := algs4.NewCounter("heads") - tails := algs4.NewCounter("tails") - - for t := 0; t < nflips; t++ { - if rand.Float64() > 0.5 { - heads.Increment() - } else { - tails.Increment() - } - } - - fmt.Println(heads) - fmt.Println(tails) - - d := heads.Tally() - tails.Tally() - - fmt.Printf("delta: %d\n", int(math.Abs(float64(d)))) -} diff --git a/algs4/src/go/algs4/algs4-gcd/main.go b/algs4/src/go/algs4/algs4-gcd/main.go deleted file mode 100644 index aefdd0c..0000000 --- a/algs4/src/go/algs4/algs4-gcd/main.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strconv" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -const USAGE string = `Usage: algs4-gcd ` - -func main() { - if len(os.Args) < 3 { - fmt.Println(USAGE) - os.Exit(1) - } - - p, err := strconv.ParseUint(os.Args[1], 10, 0) - if err != nil { - fmt.Println("Wherps: ", err) - os.Exit(1) - } - - q, err := strconv.ParseUint(os.Args[2], 10, 0) - if err != nil { - fmt.Println("Wherps: ", err) - os.Exit(1) - } - - fmt.Println(algs4.Gcd(p, q)) -} diff --git a/algs4/src/go/algs4/algs4-interval2d/main.go b/algs4/src/go/algs4/algs4-interval2d/main.go deleted file mode 100644 index 0acdec2..0000000 --- a/algs4/src/go/algs4/algs4-interval2d/main.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "math/rand" - "os" - "strconv" - "time" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -func die(err error) { - fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) - os.Exit(1) -} - -func main() { - if len(os.Args) < 6 { - die(errors.New("We need 4 floats and an int!")) - } - - rand.Seed(time.Now().UTC().UnixNano()) - - xlo, err := strconv.ParseFloat(os.Args[1], 32) - if err != nil { - die(err) - } - - xhi, err := strconv.ParseFloat(os.Args[2], 32) - if err != nil { - die(err) - } - - ylo, err := strconv.ParseFloat(os.Args[3], 32) - if err != nil { - die(err) - } - - yhi, err := strconv.ParseFloat(os.Args[4], 32) - if err != nil { - die(err) - } - - npoints, err := strconv.Atoi(os.Args[5]) - if err != nil { - die(err) - } - - xint := algs4.NewInterval1D(xlo, xhi) - yint := algs4.NewInterval1D(ylo, yhi) - box := algs4.NewInterval2D(xint, yint, nil) - box.Draw() - - c := algs4.NewCounter("hits") - for t := 0; t < npoints; t++ { - x := rand.Float64() - y := rand.Float64() - p := algs4.NewPoint2D(x, y, nil) - if box.Contains(p) { - c.Increment() - } else { - p.Draw() - } - } - - err = algs4.Draw() - if err != nil { - die(err) - } - - fmt.Println(c) - fmt.Printf("area = %.2f\n", box.Area()) -} diff --git a/algs4/src/go/algs4/algs4-randomseq/main.go b/algs4/src/go/algs4/algs4-randomseq/main.go deleted file mode 100644 index 89130a8..0000000 --- a/algs4/src/go/algs4/algs4-randomseq/main.go +++ /dev/null @@ -1,42 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "os" - "strconv" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -func die(err error) { - fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) - os.Exit(1) -} - -func main() { - if len(os.Args) < 4 { - die(errors.New("We need an int and two floats!")) - } - n, err := strconv.Atoi(os.Args[1]) - if err != nil { - die(err) - } - - lo, err := strconv.ParseFloat(os.Args[2], 32) - if err != nil { - die(err) - } - - hi, err := strconv.ParseFloat(os.Args[3], 32) - if err != nil { - die(err) - } - - for i := 0; i < n; i++ { - x := algs4.RandomUniform(lo, hi) - fmt.Printf("%.2f\n", x) - } -} diff --git a/algs4/src/go/algs4/algs4-rolls/main.go b/algs4/src/go/algs4/algs4-rolls/main.go deleted file mode 100644 index ea89e5e..0000000 --- a/algs4/src/go/algs4/algs4-rolls/main.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "fmt" - "os" - "strconv" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -const ( - USAGE = "Usage: algs4-rolls " - SIDES = 6 -) - -func die(err error) { - fmt.Println("Wat.", err) - os.Exit(2) -} - -func main() { - if len(os.Args) < 2 { - fmt.Println(USAGE) - os.Exit(1) - } - - nrolls, err := strconv.Atoi(os.Args[1]) - if err != nil { - die(err) - } - - for _, roll := range algs4.Rolls(nrolls) { - fmt.Println(roll) - } -} diff --git a/algs4/src/go/algs4/algs4-stats/main.go b/algs4/src/go/algs4/algs4-stats/main.go deleted file mode 100644 index 7be5878..0000000 --- a/algs4/src/go/algs4/algs4-stats/main.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "fmt" - "math" - "os" - - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -func die(err error) { - fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) - os.Exit(1) -} - -func main() { - numbers := algs4.NewBag() - - for !algs4.Stdin.IsEmpty() { - d, err := algs4.Stdin.ReadDouble() - if err != nil { - break - } - numbers.Add(d) - } - - n := numbers.Size() - sum := float64(0.0) - - for x := range numbers.Each() { - if val, ok := x.(float64); ok { - sum += val - } else { - fmt.Fprintf(os.Stderr, "%v is not a float64!", x) - } - } - - mean := sum / float64(n) - - sum = float64(0.0) - for x := range numbers.Each() { - if val, ok := x.(float64); ok { - sum += (val - mean) * (val - mean) - } else { - fmt.Fprintf(os.Stderr, "%v is not a float64!", x) - } - } - - std := math.Sqrt(sum / (float64(n) - float64(1))) - - fmt.Printf("Mean: %.2f\n", mean) - fmt.Printf("Std dev: %.2f\n", std) -} diff --git a/algs4/src/go/algs4/algs4-test-accumulator/main.go b/algs4/src/go/algs4/algs4-test-accumulator/main.go deleted file mode 100644 index fea140a..0000000 --- a/algs4/src/go/algs4/algs4-test-accumulator/main.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "os" - "strconv" - - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -func die(err error) { - fmt.Fprintf(os.Stderr, "ERROR: %v\n", err) - os.Exit(1) -} - -func main() { - if len(os.Args) < 2 { - die(errors.New("We need an int!")) - } - - nValues, err := strconv.Atoi(os.Args[1]) - if err != nil { - die(err) - } - - a := algs4.NewAccumulator() - for t := 0; t < nValues; t++ { - a.AddDataValue(algs4.Random()) - } - - fmt.Println(a) - return -} diff --git a/algs4/src/go/algs4/bag.go b/algs4/src/go/algs4/bag.go deleted file mode 100644 index 53833a0..0000000 --- a/algs4/src/go/algs4/bag.go +++ /dev/null @@ -1,50 +0,0 @@ -package algs4 - -import ( - "fmt" - "reflect" -) - -type Bag struct { - items map[string]interface{} -} - -type Item struct { - Name string -} - -func NewBag() *Bag { - return &Bag{ - items: map[string]interface{}{}, - } -} - -func (me *Bag) Add(item interface{}) { - me.items[me.hashItem(item)] = item -} - -func (me *Bag) IsEmpty() bool { - return len(me.items) == 0 -} - -func (me *Bag) Size() int { - return len(me.items) -} - -func (me *Bag) Each() <-chan interface{} { - out := make(chan interface{}) - - go func() { - defer close(out) - for _, item := range me.items { - out <- item - } - }() - - return (<-chan interface{})(out) -} - -func (me *Bag) hashItem(item interface{}) string { - t := reflect.TypeOf(item) - return fmt.Sprintf("%s%s%v", t.Name(), t.String(), &t) -} diff --git a/algs4/src/go/algs4/binarysearch.go b/algs4/src/go/algs4/binarysearch.go deleted file mode 100644 index a8abca0..0000000 --- a/algs4/src/go/algs4/binarysearch.go +++ /dev/null @@ -1,21 +0,0 @@ -package algs4 - -func BinarySearchRank(key int, a []int) int { - lo := 0 - hi := len(a) - 1 - - var mid int - - for lo <= hi { - mid = lo + (hi-lo)/2 - if key < a[mid] { - hi = mid - 1 - } else if key > a[mid] { - lo = mid + 1 - } else { - return mid - } - } - - return -1 -} diff --git a/algs4/src/go/algs4/binarysearch_test.go b/algs4/src/go/algs4/binarysearch_test.go deleted file mode 100644 index f98991b..0000000 --- a/algs4/src/go/algs4/binarysearch_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package algs4_test - -import ( - "testing" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -func TestBinarySearchRank(t *testing.T) { - arr := [...]int{0, 2, 9, 14, 23} - - if algs4.BinarySearchRank(9, arr[:]) != 2 { - t.Error("Failed to find 9 in", arr) - } - - if algs4.BinarySearchRank(0, arr[:]) != 0 { - t.Error("Failed to find 0 in", arr) - } -} diff --git a/algs4/src/go/algs4/canvas.go b/algs4/src/go/algs4/canvas.go deleted file mode 100644 index d987a43..0000000 --- a/algs4/src/go/algs4/canvas.go +++ /dev/null @@ -1,159 +0,0 @@ -package algs4 - -import ( - "errors" - "fmt" - "image" - "image/draw" - "math" - "os" - - "code.google.com/p/x-go-binding/ui" - "code.google.com/p/x-go-binding/ui/x11" -) - -var ( - DefaultCanvas, DefaultCanvasError = NewCanvas(512, 512, - float64(0.0), float64(1.0), float64(0.0), float64(1.0)) - - halfWidthNegativeError = errors.New("half width can't be negative") - halfHeightNegativeError = errors.New("half height can't be negative") -) - -func init() { - if DefaultCanvasError != nil { - fmt.Fprintf(os.Stderr, - "Failed to initialize DefaultCanvas: %v\n", DefaultCanvasError) - panic(DefaultCanvasError) - } -} - -type Canvas struct { - Height int - Width int - Xmin float64 - Xmax float64 - Ymin float64 - Ymax float64 - - img *image.RGBA - window ui.Window -} - -func NewCanvas(height, width int, xmin, xmax, ymin, ymax float64) (*Canvas, error) { - canvas := &Canvas{ - Height: height, - Width: width, - Xmin: xmin, - Xmax: xmax, - Ymin: ymin, - Ymax: ymax, - - img: image.NewRGBA(image.Rect(0, 0, height, width)), - window: nil, - } - - return canvas, nil -} - -func (me *Canvas) ensureHasWindow() error { - if me.window != nil { - return nil - } - - window, err := x11.NewWindow() - if err != nil { - return err - } - - me.window = window - - return nil -} - -func (me *Canvas) Image() *image.RGBA { - return me.img -} - -func (me *Canvas) Bounds() image.Rectangle { - return me.img.Bounds() -} - -func (me *Canvas) Rectangle(x, y, halfWidth, halfHeight float64) error { - if halfWidth < 0 { - return halfWidthNegativeError - } - - if halfHeight < 0 { - return halfHeightNegativeError - } - - f2 := float64(2.0) - - xs := me.scaleX(x) - ys := me.scaleY(y) - ws := me.factorX(f2 * halfWidth) - hs := me.factorY(f2 * halfHeight) - - if ws <= 1 && hs <= 1 { - me.Pixel(x, y) - return nil - } - - originX, originY := int(xs-ws/f2), int(ys-hs/f2) - draw.Draw(me.img, - image.Rect(originX, originY, originX+int(ws), originY+int(hs)), - &image.Uniform{image.White}, image.ZP, draw.Src) - - return nil -} - -func (me *Canvas) Pixel(x, y float64) error { - err := me.ensureHasWindow() - if err != nil { - return err - } - - originX, originY := int(me.scaleX(x)), int(me.scaleY(y)) - draw.Draw(me.img, image.Rect(originX, originY, originX+1, originY+1), - &image.Uniform{image.White}, image.ZP, draw.Src) - me.window.FlushImage() - - return nil -} - -func (me *Canvas) Draw() error { - err := me.ensureHasWindow() - if err != nil { - return err - } - - draw.Draw(me.window.Screen(), me.window.Screen().Bounds(), me.img, image.ZP, draw.Src) - me.window.FlushImage() - - for evt := range me.window.EventChan() { - switch evt := evt.(type) { - case ui.KeyEvent: - if evt.Key == 'q' { - return nil - } - } - } - return nil -} - -func (me *Canvas) scaleX(x float64) float64 { - return float64(me.Width) * (x - me.Xmin) / (me.Xmax - me.Xmin) -} - -func (me *Canvas) scaleY(y float64) float64 { - return float64(me.Height) * (me.Ymax - y) / (me.Ymax - me.Ymin) -} - -func (me *Canvas) factorX(w float64) float64 { - return w * float64(me.Width) / math.Abs(me.Xmax-me.Xmin) -} - -func (me *Canvas) factorY(h float64) float64 { - return h * float64(me.Height) / math.Abs(me.Ymax-me.Ymin) -} diff --git a/algs4/src/go/algs4/counter.go b/algs4/src/go/algs4/counter.go deleted file mode 100644 index a278cab..0000000 --- a/algs4/src/go/algs4/counter.go +++ /dev/null @@ -1,37 +0,0 @@ -package algs4 - -import ( - "fmt" -) - -type Counter struct { - name string - count int -} - -func NewCounter(name string) *Counter { - counter := &Counter{} - counter.name = name - return counter -} - -func (c *Counter) Increment() { - c.count += 1 -} - -func (c *Counter) Tally() int { - return c.count -} - -func (c *Counter) String() string { - return fmt.Sprintf("%d %s", c.count, c.name) -} - -func (c *Counter) Cmp(other *Counter) int { - if c.count < other.count { - return -1 - } else if c.count > other.count { - return 1 - } - return 0 -} diff --git a/algs4/src/go/algs4/date.go b/algs4/src/go/algs4/date.go deleted file mode 100644 index d0ad2a0..0000000 --- a/algs4/src/go/algs4/date.go +++ /dev/null @@ -1,23 +0,0 @@ -package algs4 - -import ( - "fmt" -) - -type Date struct { - Month int - Day int - Year int -} - -func NewDate(m, d, y int) *Date { - return &Date{ - Month: m, - Day: d, - Year: y, - } -} - -func (me *Date) String() string { - return fmt.Sprintf("%d/%d/%d", me.Month, me.Day, me.Year) -} diff --git a/algs4/src/go/algs4/draw.go b/algs4/src/go/algs4/draw.go deleted file mode 100644 index 71fe894..0000000 --- a/algs4/src/go/algs4/draw.go +++ /dev/null @@ -1,5 +0,0 @@ -package algs4 - -func Draw() error { - return DefaultCanvas.Draw() -} diff --git a/algs4/src/go/algs4/gcd.go b/algs4/src/go/algs4/gcd.go deleted file mode 100644 index 0a06355..0000000 --- a/algs4/src/go/algs4/gcd.go +++ /dev/null @@ -1,9 +0,0 @@ -package algs4 - -func Gcd(p, q uint64) uint64 { - if q == 0 { - return p - } - r := p % q - return Gcd(q, r) -} diff --git a/algs4/src/go/algs4/gcd_test.go b/algs4/src/go/algs4/gcd_test.go deleted file mode 100644 index 08aadd2..0000000 --- a/algs4/src/go/algs4/gcd_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package algs4_test - -import ( - "testing" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -func TestGcd(t *testing.T) { - d := algs4.Gcd(uint64(81), uint64(72)) - if d != 9 { - t.Error("WRONG") - } -} diff --git a/algs4/src/go/algs4/interval1d.go b/algs4/src/go/algs4/interval1d.go deleted file mode 100644 index 200773f..0000000 --- a/algs4/src/go/algs4/interval1d.go +++ /dev/null @@ -1,21 +0,0 @@ -package algs4 - -type Interval1D struct { - Left float64 - Right float64 -} - -func NewInterval1D(left, right float64) *Interval1D { - return &Interval1D{ - Left: left, - Right: right, - } -} - -func (me *Interval1D) Contains(x float64) bool { - return me.Left <= x && x <= me.Right -} - -func (me *Interval1D) Length() float64 { - return me.Right - me.Left -} diff --git a/algs4/src/go/algs4/interval2d.go b/algs4/src/go/algs4/interval2d.go deleted file mode 100644 index 25e2a36..0000000 --- a/algs4/src/go/algs4/interval2d.go +++ /dev/null @@ -1,33 +0,0 @@ -package algs4 - -type Interval2D struct { - Xint *Interval1D - Yint *Interval1D - Canvas *Canvas -} - -func NewInterval2D(xint, yint *Interval1D, canvas *Canvas) *Interval2D { - if canvas == nil { - canvas = DefaultCanvas - } - - return &Interval2D{ - Xint: xint, - Yint: yint, - Canvas: canvas, - } -} - -func (me *Interval2D) Contains(p *Point2D) bool { - return me.Xint.Contains(p.X) && me.Yint.Contains(p.Y) -} - -func (me *Interval2D) Draw() { - xc := (me.Xint.Left + me.Xint.Right) / 2.0 - yc := (me.Yint.Left + me.Yint.Right) / 2.0 - me.Canvas.Rectangle(xc, yc, me.Xint.Length()/2.0, me.Yint.Length()/2.0) -} - -func (me *Interval2D) Area() float64 { - return me.Xint.Length() * me.Yint.Length() -} diff --git a/algs4/src/go/algs4/io.go b/algs4/src/go/algs4/io.go deleted file mode 100644 index fe859bb..0000000 --- a/algs4/src/go/algs4/io.go +++ /dev/null @@ -1,76 +0,0 @@ -package algs4 - -import ( - "bufio" - "io" - "os" - "strconv" - "strings" -) - -type inputWrapper struct { - Inbuf *bufio.Reader -} - -var ( - Stdin = &inputWrapper{} -) - -func init() { - Stdin.Inbuf = bufio.NewReader(os.Stdin) -} - -func (in *inputWrapper) ReadDouble() (float64, error) { - line, err := in.Inbuf.ReadString('\n') - - if err != nil { - return float64(0), err - } - - dbl, err := strconv.ParseFloat(strings.TrimSpace(line), 64) - if err != nil { - return float64(0), err - } - - return dbl, nil -} - -func (in *inputWrapper) IsEmpty() bool { - _, err := in.Inbuf.Peek(1) - if err != nil { - return true - } - - return false -} - -func ReadInts(inbuf io.Reader) ([]int, error) { - ints := make([]int, 0) - - var i64 int64 - - fileReader := bufio.NewReader(inbuf) - line, err := fileReader.ReadString('\n') - - for err == nil { - line = strings.TrimSpace(line) - - if len(line) > 0 { - i64, err = strconv.ParseInt(line, 10, 32) - - if err != nil { - return nil, err - } - - ints = append(ints, int(i64)) - } - - line, err = fileReader.ReadString('\n') - } - - if err != nil && err != io.EOF { - return nil, err - } - - return ints, nil -} diff --git a/algs4/src/go/algs4/io_test.go b/algs4/src/go/algs4/io_test.go deleted file mode 100644 index 11ca7f0..0000000 --- a/algs4/src/go/algs4/io_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package algs4_test - -import ( - "fmt" - "strings" - "testing" -) - -import ( - "github.com/meatballhat/box-o-sand/algs4/src/go/algs4" -) - -const INTS_STRING = ` -999 -848 --7271 -7384 -71878 -92 -0 -0 -0 --99 -` - -func TestReadInts(t *testing.T) { - ints, err := algs4.ReadInts(strings.NewReader(INTS_STRING)) - - if err != nil { - t.Error("Wrong wrong wrong: ", err) - } - - if ints == nil { - t.Error("Nothing in the ints!: ", ints) - } - - fmt.Println("ints =", ints) - - if ints[0] != 999 { - t.Error("fail on 0: 999 !=", ints[0]) - } - - if ints[4] != 71878 { - t.Error("fail on 4: 71878 !=", ints[4]) - } - - if ints[6] != 0 { - t.Error("fail on 6: 0 !=", ints[6]) - } -} diff --git a/algs4/src/go/algs4/point2d.go b/algs4/src/go/algs4/point2d.go deleted file mode 100644 index 81d1c43..0000000 --- a/algs4/src/go/algs4/point2d.go +++ /dev/null @@ -1,25 +0,0 @@ -package algs4 - -type Point2D struct { - X float64 - Y float64 - - canvas *Canvas -} - -func NewPoint2D(x, y float64, canvas *Canvas) *Point2D { - if canvas == nil { - canvas = DefaultCanvas - } - - return &Point2D{ - X: x, - Y: y, - - canvas: canvas, - } -} - -func (me *Point2D) Draw() { - me.canvas.Pixel(me.X, me.Y) -} diff --git a/algs4/src/go/algs4/queue.go b/algs4/src/go/algs4/queue.go deleted file mode 100644 index 9f34dfa..0000000 --- a/algs4/src/go/algs4/queue.go +++ /dev/null @@ -1,34 +0,0 @@ -package algs4 - -type Queue struct { - items []interface{} -} - -func NewQueue() *Queue { - return &Queue{ - items: []interface{}{}, - } -} - -func (me *Queue) Enqueue(item interface{}) { - me.items = append(me.items, item) -} - -func (me *Queue) Dequeue() interface{} { - if me.IsEmpty() { - return nil - } - - ret := me.items[0] - me.items = me.items[1:] - - return ret -} - -func (me *Queue) Size() int { - return len(me.items) -} - -func (me *Queue) IsEmpty() bool { - return len(me.items) == 0 -} diff --git a/algs4/src/go/algs4/random.go b/algs4/src/go/algs4/random.go deleted file mode 100644 index 8cb1708..0000000 --- a/algs4/src/go/algs4/random.go +++ /dev/null @@ -1,18 +0,0 @@ -package algs4 - -import ( - "math/rand" - "time" -) - -func init() { - rand.Seed(time.Now().UTC().UnixNano()) -} - -func RandomUniform(min, max float64) float64 { - return rand.Float64()*(max-min) + min -} - -func Random() float64 { - return rand.Float64() -} diff --git a/algs4/src/go/algs4/rolls.go b/algs4/src/go/algs4/rolls.go deleted file mode 100644 index 577058c..0000000 --- a/algs4/src/go/algs4/rolls.go +++ /dev/null @@ -1,25 +0,0 @@ -package algs4 - -import ( - "fmt" -) - -const ROLL_SIDES = 6 - -func Rolls(nrolls int) []*Counter { - var rolls []*Counter - - for i := 0; i < ROLL_SIDES; i++ { - rolls = append(rolls, NewCounter(fmt.Sprintf("%d's", i+1))) - } - - randMax := float64(ROLL_SIDES) - zero := float64(0.0) - - for t := 0; t < nrolls; t++ { - result := int(RandomUniform(zero, randMax)) - rolls[result].Increment() - } - - return rolls -} diff --git a/algs4/src/go/algs4/stack.go b/algs4/src/go/algs4/stack.go deleted file mode 100644 index e1d022a..0000000 --- a/algs4/src/go/algs4/stack.go +++ /dev/null @@ -1,35 +0,0 @@ -package algs4 - -type Stack struct { - items []interface{} -} - -func NewStack() *Stack { - return &Stack{ - items: []interface{}{}, - } -} - -func (me *Stack) Push(item interface{}) { - me.items = append(me.items, item) -} - -func (me *Stack) Pop() interface{} { - if me.IsEmpty() { - return nil - } - - lastIdx := me.Size() - 1 - lastItem := me.items[lastIdx] - me.items = me.items[:lastIdx] - - return lastItem -} - -func (me *Stack) IsEmpty() bool { - return len(me.items) == 0 -} - -func (me *Stack) Size() int { - return len(me.items) -}