Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This is a proof of concept. I use it to mentor, and enhance/extend it as I go.
- Difference of Squares
- Luhn
- Parallel Letter Frequency
- Grains

Exalysis will do its format, lint, and test checks on any solution, but specific suggestions have so far been implemented for only the exercises above. If you'd like to add support for a new exercise, please submit a PR! (See 'Contributions' below.)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/atotto/clipboard v0.1.1 h1:WSoEbAS70E5gw8FbiqFlp69MGsB6dUb4l+0AGGLiVG
github.com/atotto/clipboard v0.1.1/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7 h1:2hRPrmiwPrp3fQX967rNJIhQPtiGXdlQWAxKbKw3VHA=
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/kevinburke/go-bindata v3.13.0+incompatible h1:hThDhUBH4KjTyhfXfOgacEPfFBNjltnzl/xzfLfrPoQ=
github.com/kevinburke/go-bindata v3.13.0+incompatible/go.mod h1:/pEEZ72flUW2p0yi30bslSp9YqD9pysLxunQDdb2CPM=
Expand Down
10 changes: 6 additions & 4 deletions suggestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/tehsphinx/exalysis/extypes"
"github.com/tehsphinx/exalysis/gtpl"
"github.com/tehsphinx/exalysis/track/diffsquares"
"github.com/tehsphinx/exalysis/track/grains"
"github.com/tehsphinx/exalysis/track/hamming"
"github.com/tehsphinx/exalysis/track/isogram"
"github.com/tehsphinx/exalysis/track/luhn"
Expand All @@ -23,14 +24,15 @@ import (
)

var exercisePkgs = map[string]extypes.SuggestionFunc{
"twofer": twofer.Suggest,
"diffsquares": diffsquares.Suggest,
"grains": grains.Suggest,
"hamming": hamming.Suggest,
"raindrops": raindrops.Suggest,
"scrabble": scrabble.Suggest,
"isogram": isogram.Suggest,
"diffsquares": diffsquares.Suggest,
"luhn": luhn.Suggest,
"letter": paraletterfreq.Suggest,
"raindrops": raindrops.Suggest,
"scrabble": scrabble.Suggest,
"twofer": twofer.Suggest,
}

//GetSuggestions selects the package suggestion routine and returns the suggestions
Expand Down
25 changes: 25 additions & 0 deletions track/grains/grains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package grains

import (
"github.com/tehsphinx/astrav"
"github.com/tehsphinx/exalysis/extypes"
"github.com/tehsphinx/exalysis/track/grains/tpl"
)

// Suggest builds suggestions for the exercise solution
func Suggest(pkg *astrav.Package, r *extypes.Response) {
for _, tf := range exFuncs {
tf(pkg, r)
}
}

var exFuncs = []extypes.SuggestionFunc{
examIgnoreError,
}

func examIgnoreError(pkg *astrav.Package, r *extypes.Response) {
blank := pkg.FindFirstIdentByName("_")
if blank != nil {
r.AppendImprovement(tpl.CheckError)
}
}
44 changes: 44 additions & 0 deletions track/grains/grains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package grains

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/tehsphinx/exalysis/extypes"
"github.com/tehsphinx/exalysis/gtpl"
"github.com/tehsphinx/exalysis/testhelper"
"github.com/tehsphinx/exalysis/track/grains/tpl"
)

var suggestTests = []struct {
path string
suggestion gtpl.Template
expected bool
}{
{path: "./solutions/1", suggestion: tpl.BitsRotate64, expected: true},
{path: "./solutions/1", suggestion: tpl.BitsRotate, expected: true},
{path: "./solutions/1", suggestion: tpl.StaticTotalNice, expected: true},
{path: "./solutions/1", suggestion: tpl.ErrorFormatted, expected: false},
{path: "./solutions/2", suggestion: tpl.MathPow, expected: true},
{path: "./solutions/2", suggestion: tpl.StaticTotal, expected: true},
{path: "./solutions/2", suggestion: tpl.ErrorFormatted, expected: true},
{path: "./solutions/3", suggestion: tpl.StaticTotal, expected: true},
{path: "./solutions/3", suggestion: tpl.MathPow, expected: true},
{path: "./solutions/3", suggestion: tpl.CheckError, expected: true},
}

func Test_Suggest(t *testing.T) {
for _, test := range suggestTests {
_, pkg, err := testhelper.LoadExample(test.path, "grains")
if err != nil {
t.Fatal(err)
}

r := extypes.NewResponse()
Suggest(pkg, r)

assert.Equal(t, test.expected, r.HasSuggestion(test.suggestion),
fmt.Sprintf("test failed: %+v", test))
}
}
23 changes: 23 additions & 0 deletions track/grains/solutions/1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Package grains calculate the number of grains of wheat on a chessboard given that the number on each square doubles.
*/
package grains

import (
"errors"
"math"
"math/bits"
)

// Square returns number of grains on the specified square
func Square(number int) (uint64, error) {
if number < 1 || number > 64 {
return 0, errors.New("invalid number of squares")
}
return uint64(bits.RotateLeft(1, number-1)), nil
}

// Total returns total number of grains from 64 squares (2^64 - 1)
func Total() uint64 {
return math.MaxUint64
}
21 changes: 21 additions & 0 deletions track/grains/solutions/2/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package grains

import (
"fmt"
"math"
)

// Square returns the number of grains on a square on a chess board where the
// first square has 1 and every subsequent square doubles the number.
func Square(num int) (uint64, error) {
if num < 1 || num > 64 {
return 0, fmt.Errorf("Num [%d] is invalid.", num)
}
return uint64(math.Pow(2, float64(num-1))), nil
}

// Total returns the total number of grains of the chessboard.
func Total() uint64 {
// uint64's range: 0-18446744073709551615.
return uint64(math.Pow(2, 63))*2 - 1
}
25 changes: 25 additions & 0 deletions track/grains/solutions/3/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package grains

import (
"errors"
"math"
)

//Square returns the number of grains on the input square
func Square(input int) (uint64, error) {
if input <= 0 || input > 64 {
return 0, errors.New("Invalid input")
}
result := uint64(math.Exp2(float64(input - 1)))
return result, nil
}

//Total returns all the grains on the board.
func Total() uint64 {
sum := uint64(0)
for i := 1; i <= 64; i++ {
sum1, _ := Square(i)
sum += sum1
}
return sum
}
Loading