Skip to content

Commit

Permalink
Merge pull request #374 from vaskoz/day180
Browse files Browse the repository at this point in the history
Day180
  • Loading branch information
vaskoz committed Feb 18, 2019
2 parents 2799dd2 + 8528c72 commit ee3216b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,4 @@ problems from
* [Day 177](https://github.com/vaskoz/dailycodingproblem-go/issues/365)
* [Day 178](https://github.com/vaskoz/dailycodingproblem-go/issues/367)
* [Day 179](https://github.com/vaskoz/dailycodingproblem-go/issues/371)
* [Day 180](https://github.com/vaskoz/dailycodingproblem-go/issues/373)
61 changes: 61 additions & 0 deletions day180/problem.go
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())
}
}
}
41 changes: 41 additions & 0 deletions day180/problem_test.go
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)
}
}
}

0 comments on commit ee3216b

Please sign in to comment.