Re-namespacing everything and fixing imports to match
This commit is contained in:
48
algs4/go/algs4-binarysearch/main.go
Normal file
48
algs4/go/algs4-binarysearch/main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/algs4/go/algs4"
|
||||
)
|
||||
|
||||
const USAGE string = `Usage: algs4-binarysearch <whitelist-file>
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
55
algs4/go/algs4-flips/main.go
Normal file
55
algs4/go/algs4-flips/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/algs4/go/algs4"
|
||||
)
|
||||
|
||||
const USAGE string = "Usage: algs4-flips <nflips>"
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println(USAGE)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
nflips, err := strconv.ParseInt(os.Args[1], 10, 32)
|
||||
if err != nil {
|
||||
fmt.Println("Wat.", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
heads := algs4.NewCounter("heads")
|
||||
tails := algs4.NewCounter("tails")
|
||||
|
||||
randMax := big.NewInt(2)
|
||||
|
||||
for t := int64(0); t < nflips; t += int64(1) {
|
||||
n, err := rand.Int(rand.Reader, randMax)
|
||||
if err != nil {
|
||||
fmt.Println("Ugh:", err)
|
||||
os.Exit(4)
|
||||
}
|
||||
|
||||
if n.Int64() < 1 {
|
||||
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))))
|
||||
}
|
34
algs4/go/algs4-gcd/main.go
Normal file
34
algs4/go/algs4-gcd/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/algs4/go/algs4"
|
||||
)
|
||||
|
||||
const USAGE string = `Usage: algs4-gcd <uint> <uint>`
|
||||
|
||||
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))
|
||||
}
|
35
algs4/go/algs4-rolls/main.go
Normal file
35
algs4/go/algs4-rolls/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/algs4/go/algs4"
|
||||
)
|
||||
|
||||
const (
|
||||
USAGE string = "Usage: algs4-rolls <nrolls>"
|
||||
SIDES = 6
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println(USAGE)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
nrolls, err := strconv.ParseInt(os.Args[1], 10, 32)
|
||||
if err != nil {
|
||||
fmt.Println("Wat.", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
rolls, err := algs4.Rolls(nrolls)
|
||||
|
||||
for i := 1; i <= algs4.ROLL_SIDES; i += 1 {
|
||||
fmt.Println(rolls[i])
|
||||
}
|
||||
}
|
21
algs4/go/algs4/binarysearch.go
Normal file
21
algs4/go/algs4/binarysearch.go
Normal file
@@ -0,0 +1,21 @@
|
||||
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
|
||||
}
|
21
algs4/go/algs4/binarysearch_test.go
Normal file
21
algs4/go/algs4/binarysearch_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package algs4_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/algs4/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)
|
||||
}
|
||||
}
|
37
algs4/go/algs4/counter.go
Normal file
37
algs4/go/algs4/counter.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
9
algs4/go/algs4/gcd.go
Normal file
9
algs4/go/algs4/gcd.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package algs4
|
||||
|
||||
func Gcd(p, q uint64) uint64 {
|
||||
if q == 0 {
|
||||
return p
|
||||
}
|
||||
r := p % q
|
||||
return Gcd(q, r)
|
||||
}
|
16
algs4/go/algs4/gcd_test.go
Normal file
16
algs4/go/algs4/gcd_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package algs4_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/algs4/go/algs4"
|
||||
)
|
||||
|
||||
func TestGcd(t *testing.T) {
|
||||
d := algs4.Gcd(uint64(81), uint64(72))
|
||||
if d != 9 {
|
||||
t.Error("WRONG")
|
||||
}
|
||||
}
|
39
algs4/go/algs4/io.go
Normal file
39
algs4/go/algs4/io.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package algs4
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
50
algs4/go/algs4/io_test.go
Normal file
50
algs4/go/algs4/io_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package algs4_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
import (
|
||||
"github.com/meatballhat/box-o-sand/algs4/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])
|
||||
}
|
||||
}
|
33
algs4/go/algs4/rolls.go
Normal file
33
algs4/go/algs4/rolls.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package algs4
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
const ROLL_SIDES = 6
|
||||
|
||||
func Rolls(nrolls int64) ([]*Counter, error) {
|
||||
var err error
|
||||
|
||||
rolls := make([]*Counter, ROLL_SIDES+1)
|
||||
for i := 1; i <= ROLL_SIDES; i += 1 {
|
||||
rolls[i] = NewCounter(fmt.Sprintf("%d's", i))
|
||||
}
|
||||
|
||||
var result *big.Int
|
||||
randMax := big.NewInt(ROLL_SIDES)
|
||||
|
||||
for t := int64(0); t < nrolls; t += 1 {
|
||||
result, err = rand.Int(rand.Reader, randMax)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rolls[int(result.Int64())+1].Increment()
|
||||
}
|
||||
|
||||
return rolls, nil
|
||||
}
|
Reference in New Issue
Block a user