Skip to content

add keep repeated items #88

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

Merged
merged 5 commits into from
Jul 8, 2023
Merged
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 @@ -38,6 +38,7 @@ Welcome to **Data Structures and Algorithms in Go**! 🎉 This project is design
* [Linked List Serialization](./linkedlist/serialization_test.go)
* [Reverse a Linked List In-place](./linkedlist/reverse_in_place_test.go)
* [Join Two Sorted Linked Lists](./linkedlist/join_sorted_lists_test.go)
* [Keep Repetitions](./linkedlist/keep_repetitions_test.go), [Solution](./linkedlist/keep_repetitions_test.go)
* [Copy Linked List with Random Pointer](./linkedlist/copy_linklist_with_random_pointer_test.go)
* [Implement LRU Cache](./linkedlist/lru_cache_test.go)
* [Stacks](./stack/README.md)
Expand Down
11 changes: 6 additions & 5 deletions linkedlist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ A music album is a good example of linked lists. The tracks come in an order, an

## Rehearsal

* [Solution](serialization.go) [Linked List Serialization](serialization_test.go)
* [Solution](reverse_in_place.go) [Reverse a Linked List In-place](reverse_in_place_test.go)
* [Solution](join_sorted_lists.go) [Join Two Sorted Linked Lists](join_sorted_lists_test.go)
* [Solution](copy_linklist_with_random_pointer.go) [Copy Linked List with Random Pointer](copy_linklist_with_random_pointer_test.go)
* [Solution](lru_cache.go) [Implement LRU Cache](lru_cache_test.go)
* [Linked List Serialization](./serialization_test.go), [Solution](./serialization.go)
* [Reverse a Linked List In-place](./reverse_in_place_test.go), [Solution](./reverse_in_place.go)
* [Join Two Sorted Linked Lists](./join_sorted_lists_test.go), [Solution](./join_sorted_lists.go)
* [Keep Repetitions](./keep_repetitions_test.go), [Solution](./keep_repetitions_test.go)
* [Copy Linked List with Random Pointer](./copy_linklist_with_random_pointer_test.go), [Solution](./copy_linklist_with_random_pointer.go)
* [Implement LRU Cache](./lru_cache_test.go), [Solution](./lru_cache.go)
2 changes: 1 addition & 1 deletion linkedlist/join_sorted_lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestJoinTwoSortedLinkedLists(t *testing.T) {
for i, test := range tests {
got := Serialize(JoinTwoSortedLinkedLists(Unserialize(test.list1), Unserialize(test.list2)))
if got != test.joined {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.joined, got)
t.Fatalf("Failed test case #%d. Want %s got %s", i, test.joined, got)
}
}
}
32 changes: 32 additions & 0 deletions linkedlist/keep repetitions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package linkedlist

import (
"math"
)

// KeepRepetitions solves the problem in O(n) time and O(1) space.
func KeepRepetitions(head *Node) *Node {
if head == nil || head.Next == nil {
return nil
}

var output, tmp *Node
lastValue := math.MinInt
for head != nil {
if lastValue == head.Val {
if tmp == nil {
tmp = NewNode(head.Val)
output = tmp
continue
}
if tmp.Val != head.Val {
tmp.Next = NewNode(head.Val)
tmp = tmp.Next
}
}
lastValue = head.Val
head = head.Next
}

return output
}
35 changes: 35 additions & 0 deletions linkedlist/keep_repetitions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package linkedlist

import (
"testing"
)

/*
TestKeepRepetitions tests solution(s) with the following signature and problem description:

func KeepRepetitions(head *Node) *Node

Given a linked-list of sorted integers, create a copy of the list that contains one example of
each repeated item.
*/
func TestKeepRepetitions(t *testing.T) {
tests := []struct {
list, solution string
}{
{"", ""},
{"1", ""},
{"1->1", "1"},
{"1->4->6", ""},
{"1->4->4->4->6", "4"},
{"1->1->4->4->4->6", "1->4"},
{"1->1->4->4->4->6->6", "1->4->6"},
{"1->1->4->4->4->6->7->7", "1->4->7"},
}

for i, test := range tests {
got := Serialize(KeepRepetitions(Unserialize(test.list)))
if got != test.solution {
t.Fatalf("Failed test case #%d. Want %s got %s", i, test.solution, got)
}
}
}
2 changes: 1 addition & 1 deletion linkedlist/reverse_in_place_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestReverseLinkedList(t *testing.T) {
for i, test := range tests {
got := Serialize(ReverseLinkedList(Unserialize(test.list)))
if got != test.reversed {
t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.reversed, got)
t.Fatalf("Failed test case #%d. Want %s got %s", i, test.reversed, got)
}
}
}
14 changes: 11 additions & 3 deletions tree/serialize_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ import "testing"
/*
TestSerializeAndUnserializeBinaryTree tests solution(s) with the following signature and problem description:

func Serialize(root *BinaryTreeNode) string
func Unserialize(s string) *BinaryTreeNode
func Serialize(root *BinaryTreeNode) string
func Unserialize(s string) *BinaryTreeNode

Write two functions to serialize and unserialize a binary tree like (B) in _Figure 2_ in README.md to and

4
/ \
/ \
2 6
/ \ / \
3 5

Write two functions to serialize and unserialize a binary tree like the one above to and
from a string like `4,2,6,nil,3,5,nil`.
*/
func TestSerializeAndUnserializeBinaryTree(t *testing.T) {
Expand Down
9 changes: 8 additions & 1 deletion tree/traverse_binary_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ TestTraverseBinaryTree tests solution(s) with the following signature and proble

func TraverseBinaryTree(root *BinaryTreeNode) ([]int, []int, []int)

Given a binary tree like _figure 1_, in README.md write three functions to traverse the
4
/ \
/ \
2 6
/ \ / \
1 3 5 7

Given a binary tree like the above one create three functions to traverse the
tree in-order, pre-order, and post-order.
*/
func TestTraverseBinaryTree(t *testing.T) {
Expand Down