Skip to content

Commit

Permalink
chore: add sherlock and squares problem
Browse files Browse the repository at this point in the history
  • Loading branch information
leometzger committed Aug 13, 2023
1 parent 203d4f6 commit 89112cd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
27 changes: 27 additions & 0 deletions implementation/sherlock_and_squares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package implementation

import "math"

// https://www.hackerrank.com/challenges/sherlock-and-squares/problem?isFullScreen=false

func squares(a int32, b int32) int32 {
var counter int32 = 0
var first int32 = 0

for i := a; i <= b; i++ {
sqrt := math.Sqrt(float64(i))

if math.Ceil(sqrt) == math.Floor(sqrt) {
first = int32(sqrt)
break
}
}

if first != 0 {
for i := first; i*i <= b; i++ {
counter++
}
}

return counter
}
43 changes: 43 additions & 0 deletions implementation/sherlock_and_squares_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package implementation

import (
"testing"

"github.com/stretchr/testify/assert"
)

type SherlockAndSquaresTestCase struct {
a int32
b int32
result int32
}

func TestSherlockAndSquares(t *testing.T) {
tests := []SherlockAndSquaresTestCase{
{
a: 3,
b: 9,
result: 2,
},
{
a: 17,
b: 24,
result: 0,
},
{
a: 465868129,
b: 988379794,
result: 9855,
},
{

a: 181510012,
b: 293922871,
result: 3672,
},
}

for _, test := range tests {
assert.Equal(t, test.result, squares(test.a, test.b))
}
}

0 comments on commit 89112cd

Please sign in to comment.