-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #374 from vaskoz/day180
Day180
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package day180 | ||
|
||
// Stack is a stack with only push & pop operations. | ||
type Stack struct { | ||
data []interface{} | ||
} | ||
|
||
// Push adds an element to the stack. | ||
func (s *Stack) Push(v interface{}) { | ||
s.data = append(s.data, v) | ||
} | ||
|
||
// Pop removes and element from the stack. | ||
func (s *Stack) Pop() interface{} { | ||
var v interface{} | ||
v, s.data = s.data[len(s.data)-1], s.data[:len(s.data)-1] | ||
return v | ||
} | ||
|
||
// Len returns the number of items in the stack. | ||
func (s *Stack) Len() int { | ||
return len(s.data) | ||
} | ||
|
||
// Queue is a simple FIFO queue. | ||
type Queue struct { | ||
data []interface{} | ||
} | ||
|
||
// Enqueue adds to the queue. | ||
func (q *Queue) Enqueue(v interface{}) { | ||
q.data = append(q.data, v) | ||
} | ||
|
||
// Dequeue removes an element from the queue. | ||
func (q *Queue) Dequeue() interface{} { | ||
var v interface{} | ||
v, q.data = q.data[0], q.data[1:] | ||
return v | ||
} | ||
|
||
// Len returns the number of items in the queue. | ||
func (q *Queue) Len() int { | ||
return len(q.data) | ||
} | ||
|
||
// InterleaveStack interleaves the first half of the stack | ||
// with the reverse of the second half. | ||
// This modifies the stack in place. | ||
// Uses only a queue for additional help. | ||
func InterleaveStack(s Stack) { | ||
var q Queue | ||
for cycles := s.Len() - 1; cycles > 0; cycles-- { | ||
for i := 0; i < cycles; i++ { | ||
q.Enqueue(s.Pop()) | ||
} | ||
for q.Len() != 0 { | ||
s.Push(q.Dequeue()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package day180 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
input, expected []interface{} | ||
}{ | ||
{[]interface{}{1, 2, 3, 4, 5}, []interface{}{1, 5, 2, 4, 3}}, | ||
{[]interface{}{1, 2, 3, 4}, []interface{}{1, 4, 2, 3}}, | ||
{[]interface{}{}, []interface{}{}}, | ||
{nil, nil}, | ||
} | ||
|
||
func TestInterleaveStack(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
var s Stack | ||
for _, v := range tc.input { | ||
s.Push(v) | ||
} | ||
InterleaveStack(s) | ||
for i := range tc.expected { | ||
v := tc.expected[len(tc.expected)-1-i] | ||
if result := s.Pop(); result != v { | ||
t.Errorf("Expected (%v) got (%v)", v, result) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkInterleaveStack(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
var s Stack | ||
for _, v := range tc.input { | ||
s.Push(v) | ||
} | ||
InterleaveStack(s) | ||
} | ||
} | ||
} |