Пример #1
0
func initializeForwardList() []sumLinkedListForwardTestPair {
	l1 := datastructures.LinkedList{}
	l2 := datastructures.LinkedList{}
	// l1 : 1->2->3->4 = 1234
	// l2 : 1->2->3    = 123
	// summed value    = 1357
	value := 1357

	// Create l1 : 1->2->3->4 = 1234
	for i := 1; i < 5; i++ {
		n := datastructures.Node{}
		n.SetValue(i)

		l1.Add(&n)
	}
	// Create l2 : 1->2->3    = 123
	for i := 1; i < 4; i++ {
		n := datastructures.Node{}
		n.SetValue(i)
		l2.Add(&n)
	}

	// we can expand test cases
	var forwardTests = []sumLinkedListForwardTestPair{
		{l1, l2, value},
	}
	return forwardTests
}
Пример #2
0
func TestRemoveDuplicates(t *testing.T) {
	l := datastructures.LinkedList{}
	for i := 0; i <= 10; i++ {
		n := &datastructures.Node{}
		n.SetValue(i % 3)
		l.Add(n)
	}

	RemoveDuplicates(&l)

	if l.Len() != 3 {
		t.Error("Wrong length, Len() must be", 3)
	}
	if l.Find(0) == nil || l.Find(1) == nil || l.Find(2) == nil {
		t.Error("Wrong deletion for one of the elements.")
	}
}
Пример #3
0
Файл: 2.4.go Проект: muraty/ctci
// partition given list into two parts due to the given value
// and then merge them together.
func PartitionList(l datastructures.LinkedList, value int) datastructures.LinkedList {
	beforeValue := datastructures.LinkedList{}
	afterValue := datastructures.LinkedList{}

	head := l.Head()
	for head != nil {
		n := datastructures.Node{}
		n.SetValue(head.GetValue())
		if head.GetValue() < value {
			beforeValue.Add(&n)
		} else {
			afterValue.Add(&n)
		}
		head = head.Next()
	}
	beforeValue.SetTail(afterValue.Head())
	return beforeValue
}
Пример #4
0
func initializeSlice() []sliceTestPair {
	orig := datastructures.LinkedList{}
	for i := 0; i < 10; i++ {
		n := &datastructures.Node{}
		n.SetValue(i)
		orig.Add(n)
	}

	target := datastructures.LinkedList{}
	for i := 3; i < 10; i++ {
		n := &datastructures.Node{}
		n.SetValue(i)
		target.Add(n)
	}
	var sliceTests = []sliceTestPair{
		{orig, &target, 3},
		{orig, nil, 30},
		{orig, nil, -3},
	}
	return sliceTests
}
Пример #5
0
func initializeReverseList() []sumLinkedListReverseTestPair {
	l1 := datastructures.LinkedList{}
	l2 := datastructures.LinkedList{}
	// l2 : 1->2->3    = 321
	// l1 : 1->2->3->4 = 4321
	// summed value    = 4642
	value := 4642

	// Create l1 : 1->2->3->4 = 4321
	for i := 1; i < 5; i++ {
		n := datastructures.Node{}
		n.SetValue(i)
		if i == 4 {
			l1.Add(&n)
			continue
		}
		l1.Add(&n)
	}
	// Create l2 : 1->2->3    = 321
	for i := 1; i < 4; i++ {
		n := datastructures.Node{}
		n.SetValue(i)
		l2.Add(&n)
	}
	// we can expand test cases
	var reverseTests = []sumLinkedListReverseTestPair{
		{l1, l2, value},
	}
	return reverseTests
}
Пример #6
0
func initializeLoopedList() []findCorruptNodeTestPair {
	l1 := ds.LinkedList{}
	l2 := ds.LinkedList{}
	node := &ds.Node{}

	// Create a looped linked list
	// 0->1->2->3->4->5->6->3->4->5->6->3->4->5->6...
	for i := 0; i < 6; i++ {
		n := ds.Node{}
		n.SetValue(i)
		l1.Add(&n)

		if i == 5 {
			temp := l1.Head().Next().Next().Next()
			node = temp
			l1.Add(temp)
		}
	}
	// Create a non-looped linked list
	for i := 0; i < 10; i++ {
		n := ds.Node{}
		n.SetValue(i)
		l2.Add(&n)
	}
	// we can expand test cases
	var loopTests = []findCorruptNodeTestPair{
		{l1, node},
		{l2, nil}, // no loop
	}
	return loopTests
}
Пример #7
0
func initializePalindromeTests() []palindromeTestPair {
	l1 := ds.LinkedList{} // palindrome
	l2 := ds.LinkedList{} // non-palindrome

	// create a palindrome list
	// move until half of the list to create a palindrome list
	for i := 0; i < 5; i++ {
		n := ds.Node{}
		n.SetValue(i)
		l1.Add(&n)
	}
	// move from half of the list to create a palindrome list
	for i := 5; i >= 0; i-- {
		n := ds.Node{}
		n.SetValue(i)
		l1.Add(&n)
	}

	// create a non-palindrome list
	for i := 0; i < 10; i++ {
		n := ds.Node{}
		n.SetValue(i)
		l2.Add(&n)
	}
	// we can expand test cases
	var palindromeTests = []palindromeTestPair{
		{l1, true},
		{l2, false},
	}
	return palindromeTests
}
Пример #8
0
func initializePartitionList() []partitionListTestPair {
	orig := datastructures.LinkedList{}
	result := datastructures.LinkedList{}
	value := 4
	for i := 0; i < 10; i++ {
		n := datastructures.Node{}
		n.SetValue(i)

		// Set before values
		if i < value {
			orig.Add(&n)
			// Set after values
		} else {
			result.Add(&n)
		}
	}
	// we can expand test cases
	var partitionListTests = []partitionListTestPair{
		{orig, result, value},
	}
	return partitionListTests
}
Пример #9
0
func TestDeleteNode(t *testing.T) {
	deleteNodeTests := initializeDeleteNode()
	for _, test := range deleteNodeTests {
		// save original list in a temporary list
		temp := datastructures.LinkedList{}
		for elem := range test.orig.GetElements() {
			n := datastructures.Node{}
			n.SetValue(elem)
			temp.Add(&n)
		}

		DeleteNode(&test.node)
		// item by item check through two lists
		for i := 0; i < test.orig.Len(); i++ {
			if test.orig.Find(i) != test.result.Find(i) {
				t.Error("For ", temp.GetElements(),
					"Expected", test.result.GetElements(),
					"got", test.orig.GetElements())
			}
		}
	}
}
Пример #10
0
func initializeDeleteNode() []deleteNodeTestPair {
	node := datastructures.Node{}
	orig := datastructures.LinkedList{}
	target := datastructures.LinkedList{}
	for i := 0; i < 10; i++ {
		n := datastructures.Node{}
		n.SetValue(i)
		orig.Add(&n)

		// will delete this node
		// so, don't add this node to result list
		if i == 4 {
			node = n
		} else {
			target.Add(&n)
		}
	}
	// we can expand test cases
	var deleteNodeTests = []deleteNodeTestPair{
		{orig, target, node},
	}
	return deleteNodeTests
}