Skip to content

Commit

Permalink
Merge pull request #224 from vaskoz/day106
Browse files Browse the repository at this point in the history
Day106
  • Loading branch information
vaskoz committed Dec 6, 2018
2 parents 6adc4c2 + 41e50dc commit e3d5c7c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ problems from
* [Day 103](https://github.com/vaskoz/dailycodingproblem-go/issues/217)
* [Day 104](https://github.com/vaskoz/dailycodingproblem-go/issues/219)
* [Day 105](https://github.com/vaskoz/dailycodingproblem-go/issues/221)
* [Day 106](https://github.com/vaskoz/dailycodingproblem-go/issues/223)
13 changes: 13 additions & 0 deletions day106/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package day106

// CanHopToEnd can you hop from 0 to the end.
func CanHopToEnd(hops []int) bool {
start, end := 0, len(hops)-1
for start < end {
if hops[start] == 0 {
break
}
start += hops[start]
}
return start == end
}
28 changes: 28 additions & 0 deletions day106/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package day106

import "testing"

var testcases = []struct {
hops []int
expected bool
}{
{[]int{2, 0, 1, 0}, true},
{[]int{1, 1, 0, 1}, false},
}

func TestCanHopToEnd(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := CanHopToEnd(tc.hops); result != tc.expected {
t.Errorf("For %v expected %v got %v", tc.hops, tc.expected, result)
}
}
}

func BenchmarkCanHopToEnd(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
CanHopToEnd(tc.hops)
}
}
}

0 comments on commit e3d5c7c

Please sign in to comment.