Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing DSAs #2

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Added missing DSAs #2

wants to merge 20 commits into from

Conversation

Rik034
Copy link

@Rik034 Rik034 commented Jun 5, 2023

I added all the remaining DSAs that were present in ThePrimeagen/kata-machine. I also included them in config.yaml so that they will be generated with go generate

func TestBfs(t *testing.T) {
list := dsa.AdjList2

result := Bfs(list, 6, 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected := []int{0, 1, 4, 5, 6}
assert.Equal(t, expected, result, "Array should be: %v\nBut got: %v", expected, result)

result = Bfs(list, 0, 6)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func TestBfs(t *testing.T) {
matrix := dsa.AdjMatrix1

result := Bfs(matrix, 6, 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected := []int{0, 1, 4, 5, 6}
assert.Equal(t, expected, result, "Array should be: %v\nBut got: %v", expected, result)

result = Bfs(matrix, 0, 6)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lru := NewLRU[string, int](3)

result := lru.Get("foo")
assert.Nil(t, result, "Expected: nil, got: %v", result)
Copy link
Owner

@nacknime-official nacknime-official Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lru.Get doesn't return nillable value, but if we'll return *V then it'll be mess in tests with a lot of value dereferencing.
That's why I return additional boolean that means is it nil or not (like in Queue

func (q *Queue[T]) Deque() (T, bool) {

val, ok := list.Deque()
assert.True(t, ok, "Deque should return true, but returned false")
assert.Equal(t, 5, val, "Value should be 5, but is %d", val)
assert.Equal(t, 2, list.Length, "Length should be 2, but is %d", list.Length)

)

assert.Equal(t, 69, result, "Expected: 69, got: %v", result)

result = mapp.Get("blaz")
assert.Nil(t, result, "Expected: nil, got: %v", result)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapp.Delete also returns a value, but it's never checked if deleting an unset key returns nil, the current test cases only check if the map is the same length after deleting an unset key. Should I modify mapp.Delete to also return a bool and add a test case for it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm beginning to think that I had a wrong assumption all along and Delete should not return any value. I'll make it return only a bool instead.

}

result := Solve(maze, "x", dsa.Point{X: 10, Y: 0}, dsa.Point{X: 1, Y: 5})
assert.Equal(t, expected, result, "Expected: %v, got: %v", expected, result)
Copy link
Owner

@nacknime-official nacknime-official Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThePrimeagen has additional func "drawPath" (https://github.com/ThePrimeagen/kata-machine/blob/master/src/__tests__/MazeSolver.ts#L36), maybe we also need that

trie.Insert("foolish")
trie.Insert("bar")

result := trie.Find("fo")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert.Equal(t, expected, result, "Expected: %v, got: %v", expected, result)

trie.Delete("fool")
expected = []string{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idx := rand.Intn(10001)

data := []bool{}
for i := 0; i < 10000; i++ {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can create the slice via make([]bool, 10000)

assert.Equal(t, idx, result, "Expected: %v, got: %v", idx, result)

data = []bool{}
for i := 0; i < 821; i++ {
Copy link
Owner

@nacknime-official nacknime-official Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make([]bool, 821)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants